blob: aff284e0de5b89725cda880975f6d9c5adb1c921 [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));
654static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000655static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000656static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000657static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000658static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000659static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000660static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000661static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000662static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
664static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
665static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
666static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
667static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
668static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
669static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
670static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
671static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
672static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
673static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
674static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000675static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
676static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000677static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000678static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000679
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000680static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000681static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000682static int get_env_len __ARGS((char_u **arg));
683static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000684static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000685static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
686#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
687#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
688 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000689static 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 +0000690static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000691static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000692static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
693static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000694static typval_T *alloc_tv __ARGS((void));
695static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000696static void init_tv __ARGS((typval_T *varp));
697static long get_tv_number __ARGS((typval_T *varp));
698static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000699static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000700static char_u *get_tv_string __ARGS((typval_T *varp));
701static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000702static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000703static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000704static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000705static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
706static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
707static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000708static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
709static 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 +0000710static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
711static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000712static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000713static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000714static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000715static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000716static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
717static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
718static int eval_fname_script __ARGS((char_u *p));
719static int eval_fname_sid __ARGS((char_u *p));
720static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000721static ufunc_T *find_func __ARGS((char_u *name));
722static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000723static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000724#ifdef FEAT_PROFILE
725static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000726static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
727static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
728static int
729# ifdef __BORLANDC__
730 _RTLENTRYF
731# endif
732 prof_total_cmp __ARGS((const void *s1, const void *s2));
733static int
734# ifdef __BORLANDC__
735 _RTLENTRYF
736# endif
737 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000738#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000739static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000740static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000741static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000742static void func_free __ARGS((ufunc_T *fp));
743static void func_unref __ARGS((char_u *name));
744static void func_ref __ARGS((char_u *name));
745static 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));
746static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000747static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
748static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000749static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000750static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000751static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000752
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000753/* Character used as separated in autoload function/variable names. */
754#define AUTOLOAD_CHAR '#'
755
Bram Moolenaar33570922005-01-25 22:26:29 +0000756/*
757 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000758 */
759 void
760eval_init()
761{
Bram Moolenaar33570922005-01-25 22:26:29 +0000762 int i;
763 struct vimvar *p;
764
765 init_var_dict(&globvardict, &globvars_var);
766 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000767 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000768 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000769
770 for (i = 0; i < VV_LEN; ++i)
771 {
772 p = &vimvars[i];
773 STRCPY(p->vv_di.di_key, p->vv_name);
774 if (p->vv_flags & VV_RO)
775 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
776 else if (p->vv_flags & VV_RO_SBX)
777 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
778 else
779 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000780
781 /* add to v: scope dict, unless the value is not always available */
782 if (p->vv_type != VAR_UNKNOWN)
783 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000784 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000785 /* add to compat scope dict */
786 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000787 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000788}
789
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000790#if defined(EXITFREE) || defined(PROTO)
791 void
792eval_clear()
793{
794 int i;
795 struct vimvar *p;
796
797 for (i = 0; i < VV_LEN; ++i)
798 {
799 p = &vimvars[i];
800 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000801 {
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000802 vim_free(p->vv_di.di_tv.vval.v_string);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000803 p->vv_di.di_tv.vval.v_string = NULL;
804 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000805 }
806 hash_clear(&vimvarht);
807 hash_clear(&compat_hashtab);
808
809 /* script-local variables */
810 for (i = 1; i <= ga_scripts.ga_len; ++i)
811 vars_clear(&SCRIPT_VARS(i));
812 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000813 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000814
815 /* global variables */
816 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000817
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000818 /* functions */
Bram Moolenaard9fba312005-06-26 22:34:35 +0000819 free_all_functions();
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000820 hash_clear(&func_hashtab);
821
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000822 /* autoloaded script names */
823 ga_clear_strings(&ga_loaded);
824
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000825 /* unreferenced lists and dicts */
826 (void)garbage_collect();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000827}
828#endif
829
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000830/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 * Return the name of the executed function.
832 */
833 char_u *
834func_name(cookie)
835 void *cookie;
836{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000837 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838}
839
840/*
841 * Return the address holding the next breakpoint line for a funccall cookie.
842 */
843 linenr_T *
844func_breakpoint(cookie)
845 void *cookie;
846{
Bram Moolenaar33570922005-01-25 22:26:29 +0000847 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848}
849
850/*
851 * Return the address holding the debug tick for a funccall cookie.
852 */
853 int *
854func_dbg_tick(cookie)
855 void *cookie;
856{
Bram Moolenaar33570922005-01-25 22:26:29 +0000857 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858}
859
860/*
861 * Return the nesting level for a funccall cookie.
862 */
863 int
864func_level(cookie)
865 void *cookie;
866{
Bram Moolenaar33570922005-01-25 22:26:29 +0000867 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868}
869
870/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000871funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872
873/*
874 * Return TRUE when a function was ended by a ":return" command.
875 */
876 int
877current_func_returned()
878{
879 return current_funccal->returned;
880}
881
882
883/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 * Set an internal variable to a string value. Creates the variable if it does
885 * not already exist.
886 */
887 void
888set_internal_string_var(name, value)
889 char_u *name;
890 char_u *value;
891{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000892 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000893 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894
895 val = vim_strsave(value);
896 if (val != NULL)
897 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000898 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000899 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000901 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000902 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 }
904 }
905}
906
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000907static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000908static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000909static char_u *redir_endp = NULL;
910static char_u *redir_varname = NULL;
911
912/*
913 * Start recording command output to a variable
914 * Returns OK if successfully completed the setup. FAIL otherwise.
915 */
916 int
917var_redir_start(name, append)
918 char_u *name;
919 int append; /* append to an existing variable */
920{
921 int save_emsg;
922 int err;
923 typval_T tv;
924
925 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000926 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000927 {
928 EMSG(_(e_invarg));
929 return FAIL;
930 }
931
932 redir_varname = vim_strsave(name);
933 if (redir_varname == NULL)
934 return FAIL;
935
936 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
937 if (redir_lval == NULL)
938 {
939 var_redir_stop();
940 return FAIL;
941 }
942
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000943 /* The output is stored in growarray "redir_ga" until redirection ends. */
944 ga_init2(&redir_ga, (int)sizeof(char), 500);
945
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000946 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000947 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
948 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000949 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
950 {
951 if (redir_endp != NULL && *redir_endp != NUL)
952 /* Trailing characters are present after the variable name */
953 EMSG(_(e_trailing));
954 else
955 EMSG(_(e_invarg));
956 var_redir_stop();
957 return FAIL;
958 }
959
960 /* check if we can write to the variable: set it to or append an empty
961 * string */
962 save_emsg = did_emsg;
963 did_emsg = FALSE;
964 tv.v_type = VAR_STRING;
965 tv.vval.v_string = (char_u *)"";
966 if (append)
967 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
968 else
969 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
970 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000971 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000972 if (err)
973 {
974 var_redir_stop();
975 return FAIL;
976 }
977 if (redir_lval->ll_newkey != NULL)
978 {
979 /* Dictionary item was created, don't do it again. */
980 vim_free(redir_lval->ll_newkey);
981 redir_lval->ll_newkey = NULL;
982 }
983
984 return OK;
985}
986
987/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000988 * Append "value[value_len]" to the variable set by var_redir_start().
989 * The actual appending is postponed until redirection ends, because the value
990 * appended may in fact be the string we write to, changing it may cause freed
991 * memory to be used:
992 * :redir => foo
993 * :let foo
994 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000995 */
996 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000997var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000998 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000999 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001000{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001001 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001002
1003 if (redir_lval == NULL)
1004 return;
1005
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001006 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001007 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001008 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001009 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001010
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001011 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001012 {
1013 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001014 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001015 }
1016 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001017 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001018}
1019
1020/*
1021 * Stop redirecting command output to a variable.
1022 */
1023 void
1024var_redir_stop()
1025{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001026 typval_T tv;
1027
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001028 if (redir_lval != NULL)
1029 {
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001030 /* Append the trailing NUL. */
1031 ga_append(&redir_ga, NUL);
1032
1033 /* Assign the text to the variable. */
1034 tv.v_type = VAR_STRING;
1035 tv.vval.v_string = redir_ga.ga_data;
1036 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1037 vim_free(tv.vval.v_string);
1038
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001039 clear_lval(redir_lval);
1040 vim_free(redir_lval);
1041 redir_lval = NULL;
1042 }
1043 vim_free(redir_varname);
1044 redir_varname = NULL;
1045}
1046
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047# if defined(FEAT_MBYTE) || defined(PROTO)
1048 int
1049eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1050 char_u *enc_from;
1051 char_u *enc_to;
1052 char_u *fname_from;
1053 char_u *fname_to;
1054{
1055 int err = FALSE;
1056
1057 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1058 set_vim_var_string(VV_CC_TO, enc_to, -1);
1059 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1060 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1061 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1062 err = TRUE;
1063 set_vim_var_string(VV_CC_FROM, NULL, -1);
1064 set_vim_var_string(VV_CC_TO, NULL, -1);
1065 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1066 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1067
1068 if (err)
1069 return FAIL;
1070 return OK;
1071}
1072# endif
1073
1074# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1075 int
1076eval_printexpr(fname, args)
1077 char_u *fname;
1078 char_u *args;
1079{
1080 int err = FALSE;
1081
1082 set_vim_var_string(VV_FNAME_IN, fname, -1);
1083 set_vim_var_string(VV_CMDARG, args, -1);
1084 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1085 err = TRUE;
1086 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1087 set_vim_var_string(VV_CMDARG, NULL, -1);
1088
1089 if (err)
1090 {
1091 mch_remove(fname);
1092 return FAIL;
1093 }
1094 return OK;
1095}
1096# endif
1097
1098# if defined(FEAT_DIFF) || defined(PROTO)
1099 void
1100eval_diff(origfile, newfile, outfile)
1101 char_u *origfile;
1102 char_u *newfile;
1103 char_u *outfile;
1104{
1105 int err = FALSE;
1106
1107 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1108 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1109 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1110 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1111 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1112 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1113 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1114}
1115
1116 void
1117eval_patch(origfile, difffile, outfile)
1118 char_u *origfile;
1119 char_u *difffile;
1120 char_u *outfile;
1121{
1122 int err;
1123
1124 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1125 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1126 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1127 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1128 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1129 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1130 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1131}
1132# endif
1133
1134/*
1135 * Top level evaluation function, returning a boolean.
1136 * Sets "error" to TRUE if there was an error.
1137 * Return TRUE or FALSE.
1138 */
1139 int
1140eval_to_bool(arg, error, nextcmd, skip)
1141 char_u *arg;
1142 int *error;
1143 char_u **nextcmd;
1144 int skip; /* only parse, don't execute */
1145{
Bram Moolenaar33570922005-01-25 22:26:29 +00001146 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 int retval = FALSE;
1148
1149 if (skip)
1150 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001151 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 else
1154 {
1155 *error = FALSE;
1156 if (!skip)
1157 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001158 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001159 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 }
1161 }
1162 if (skip)
1163 --emsg_skip;
1164
1165 return retval;
1166}
1167
1168/*
1169 * Top level evaluation function, returning a string. If "skip" is TRUE,
1170 * only parsing to "nextcmd" is done, without reporting errors. Return
1171 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1172 */
1173 char_u *
1174eval_to_string_skip(arg, nextcmd, skip)
1175 char_u *arg;
1176 char_u **nextcmd;
1177 int skip; /* only parse, don't execute */
1178{
Bram Moolenaar33570922005-01-25 22:26:29 +00001179 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 char_u *retval;
1181
1182 if (skip)
1183 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001184 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 retval = NULL;
1186 else
1187 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001188 retval = vim_strsave(get_tv_string(&tv));
1189 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 }
1191 if (skip)
1192 --emsg_skip;
1193
1194 return retval;
1195}
1196
1197/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001198 * Skip over an expression at "*pp".
1199 * Return FAIL for an error, OK otherwise.
1200 */
1201 int
1202skip_expr(pp)
1203 char_u **pp;
1204{
Bram Moolenaar33570922005-01-25 22:26:29 +00001205 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001206
1207 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001208 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001209}
1210
1211/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 * Top level evaluation function, returning a string.
1213 * Return pointer to allocated memory, or NULL for failure.
1214 */
1215 char_u *
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001216eval_to_string(arg, nextcmd, dolist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 char_u *arg;
1218 char_u **nextcmd;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001219 int dolist; /* turn List into sequence of lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220{
Bram Moolenaar33570922005-01-25 22:26:29 +00001221 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001223 garray_T ga;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001225 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 retval = NULL;
1227 else
1228 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001229 if (dolist && tv.v_type == VAR_LIST)
1230 {
1231 ga_init2(&ga, (int)sizeof(char), 80);
1232 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
1233 ga_append(&ga, NUL);
1234 retval = (char_u *)ga.ga_data;
1235 }
1236 else
1237 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001238 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 }
1240
1241 return retval;
1242}
1243
1244/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001245 * Call eval_to_string() without using current local variables and using
1246 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 */
1248 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001249eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 char_u *arg;
1251 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001252 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253{
1254 char_u *retval;
1255 void *save_funccalp;
1256
1257 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001258 if (use_sandbox)
1259 ++sandbox;
1260 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001261 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001262 if (use_sandbox)
1263 --sandbox;
1264 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 restore_funccal(save_funccalp);
1266 return retval;
1267}
1268
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269/*
1270 * Top level evaluation function, returning a number.
1271 * Evaluates "expr" silently.
1272 * Returns -1 for an error.
1273 */
1274 int
1275eval_to_number(expr)
1276 char_u *expr;
1277{
Bram Moolenaar33570922005-01-25 22:26:29 +00001278 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001280 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281
1282 ++emsg_off;
1283
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001284 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 retval = -1;
1286 else
1287 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001288 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001289 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 }
1291 --emsg_off;
1292
1293 return retval;
1294}
1295
Bram Moolenaara40058a2005-07-11 22:42:07 +00001296/*
1297 * Prepare v: variable "idx" to be used.
1298 * Save the current typeval in "save_tv".
1299 * When not used yet add the variable to the v: hashtable.
1300 */
1301 static void
1302prepare_vimvar(idx, save_tv)
1303 int idx;
1304 typval_T *save_tv;
1305{
1306 *save_tv = vimvars[idx].vv_tv;
1307 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1308 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1309}
1310
1311/*
1312 * Restore v: variable "idx" to typeval "save_tv".
1313 * When no longer defined, remove the variable from the v: hashtable.
1314 */
1315 static void
1316restore_vimvar(idx, save_tv)
1317 int idx;
1318 typval_T *save_tv;
1319{
1320 hashitem_T *hi;
1321
Bram Moolenaara40058a2005-07-11 22:42:07 +00001322 vimvars[idx].vv_tv = *save_tv;
1323 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1324 {
1325 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1326 if (HASHITEM_EMPTY(hi))
1327 EMSG2(_(e_intern2), "restore_vimvar()");
1328 else
1329 hash_remove(&vimvarht, hi);
1330 }
1331}
1332
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001333#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001334/*
1335 * Evaluate an expression to a list with suggestions.
1336 * For the "expr:" part of 'spellsuggest'.
1337 */
1338 list_T *
1339eval_spell_expr(badword, expr)
1340 char_u *badword;
1341 char_u *expr;
1342{
1343 typval_T save_val;
1344 typval_T rettv;
1345 list_T *list = NULL;
1346 char_u *p = skipwhite(expr);
1347
1348 /* Set "v:val" to the bad word. */
1349 prepare_vimvar(VV_VAL, &save_val);
1350 vimvars[VV_VAL].vv_type = VAR_STRING;
1351 vimvars[VV_VAL].vv_str = badword;
1352 if (p_verbose == 0)
1353 ++emsg_off;
1354
1355 if (eval1(&p, &rettv, TRUE) == OK)
1356 {
1357 if (rettv.v_type != VAR_LIST)
1358 clear_tv(&rettv);
1359 else
1360 list = rettv.vval.v_list;
1361 }
1362
1363 if (p_verbose == 0)
1364 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001365 restore_vimvar(VV_VAL, &save_val);
1366
1367 return list;
1368}
1369
1370/*
1371 * "list" is supposed to contain two items: a word and a number. Return the
1372 * word in "pp" and the number as the return value.
1373 * Return -1 if anything isn't right.
1374 * Used to get the good word and score from the eval_spell_expr() result.
1375 */
1376 int
1377get_spellword(list, pp)
1378 list_T *list;
1379 char_u **pp;
1380{
1381 listitem_T *li;
1382
1383 li = list->lv_first;
1384 if (li == NULL)
1385 return -1;
1386 *pp = get_tv_string(&li->li_tv);
1387
1388 li = li->li_next;
1389 if (li == NULL)
1390 return -1;
1391 return get_tv_number(&li->li_tv);
1392}
1393#endif
1394
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001395/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001396 * Top level evaluation function.
1397 * Returns an allocated typval_T with the result.
1398 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001399 */
1400 typval_T *
1401eval_expr(arg, nextcmd)
1402 char_u *arg;
1403 char_u **nextcmd;
1404{
1405 typval_T *tv;
1406
1407 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001408 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001409 {
1410 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001411 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001412 }
1413
1414 return tv;
1415}
1416
1417
Bram Moolenaar4f688582007-07-24 12:34:30 +00001418#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1419 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001421 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 * Uses argv[argc] for the function arguments.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001423 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001425 static int
1426call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 char_u *func;
1428 int argc;
1429 char_u **argv;
1430 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001431 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432{
Bram Moolenaar33570922005-01-25 22:26:29 +00001433 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 long n;
1435 int len;
1436 int i;
1437 int doesrange;
1438 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001439 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001441 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001443 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444
1445 for (i = 0; i < argc; i++)
1446 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001447 /* Pass a NULL or empty argument as an empty string */
1448 if (argv[i] == NULL || *argv[i] == NUL)
1449 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001450 argvars[i].v_type = VAR_STRING;
1451 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001452 continue;
1453 }
1454
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 /* Recognize a number argument, the others must be strings. */
1456 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1457 if (len != 0 && len == (int)STRLEN(argv[i]))
1458 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001459 argvars[i].v_type = VAR_NUMBER;
1460 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 }
1462 else
1463 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001464 argvars[i].v_type = VAR_STRING;
1465 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 }
1467 }
1468
1469 if (safe)
1470 {
1471 save_funccalp = save_funccal();
1472 ++sandbox;
1473 }
1474
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001475 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1476 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001478 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 if (safe)
1480 {
1481 --sandbox;
1482 restore_funccal(save_funccalp);
1483 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001484 vim_free(argvars);
1485
1486 if (ret == FAIL)
1487 clear_tv(rettv);
1488
1489 return ret;
1490}
1491
Bram Moolenaar4f688582007-07-24 12:34:30 +00001492# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001493/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001494 * Call vimL function "func" and return the result as a string.
1495 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001496 * Uses argv[argc] for the function arguments.
1497 */
1498 void *
1499call_func_retstr(func, argc, argv, safe)
1500 char_u *func;
1501 int argc;
1502 char_u **argv;
1503 int safe; /* use the sandbox */
1504{
1505 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001506 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001507
1508 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1509 return NULL;
1510
1511 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001512 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 return retval;
1514}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001515# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001516
Bram Moolenaar4f688582007-07-24 12:34:30 +00001517# if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001518/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001519 * Call vimL function "func" and return the result as a number.
1520 * Returns -1 when calling the function fails.
1521 * Uses argv[argc] for the function arguments.
1522 */
1523 long
1524call_func_retnr(func, argc, argv, safe)
1525 char_u *func;
1526 int argc;
1527 char_u **argv;
1528 int safe; /* use the sandbox */
1529{
1530 typval_T rettv;
1531 long retval;
1532
1533 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1534 return -1;
1535
1536 retval = get_tv_number_chk(&rettv, NULL);
1537 clear_tv(&rettv);
1538 return retval;
1539}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001540# endif
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001541
1542/*
1543 * Call vimL function "func" and return the result as a list
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001544 * Uses argv[argc] for the function arguments.
1545 */
1546 void *
1547call_func_retlist(func, argc, argv, safe)
1548 char_u *func;
1549 int argc;
1550 char_u **argv;
1551 int safe; /* use the sandbox */
1552{
1553 typval_T rettv;
1554
1555 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1556 return NULL;
1557
1558 if (rettv.v_type != VAR_LIST)
1559 {
1560 clear_tv(&rettv);
1561 return NULL;
1562 }
1563
1564 return rettv.vval.v_list;
1565}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566#endif
1567
Bram Moolenaar4f688582007-07-24 12:34:30 +00001568
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569/*
1570 * Save the current function call pointer, and set it to NULL.
1571 * Used when executing autocommands and for ":source".
1572 */
1573 void *
1574save_funccal()
1575{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001576 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 current_funccal = NULL;
1579 return (void *)fc;
1580}
1581
1582 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001583restore_funccal(vfc)
1584 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001586 funccall_T *fc = (funccall_T *)vfc;
1587
1588 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589}
1590
Bram Moolenaar05159a02005-02-26 23:04:13 +00001591#if defined(FEAT_PROFILE) || defined(PROTO)
1592/*
1593 * Prepare profiling for entering a child or something else that is not
1594 * counted for the script/function itself.
1595 * Should always be called in pair with prof_child_exit().
1596 */
1597 void
1598prof_child_enter(tm)
1599 proftime_T *tm; /* place to store waittime */
1600{
1601 funccall_T *fc = current_funccal;
1602
1603 if (fc != NULL && fc->func->uf_profiling)
1604 profile_start(&fc->prof_child);
1605 script_prof_save(tm);
1606}
1607
1608/*
1609 * Take care of time spent in a child.
1610 * Should always be called after prof_child_enter().
1611 */
1612 void
1613prof_child_exit(tm)
1614 proftime_T *tm; /* where waittime was stored */
1615{
1616 funccall_T *fc = current_funccal;
1617
1618 if (fc != NULL && fc->func->uf_profiling)
1619 {
1620 profile_end(&fc->prof_child);
1621 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1622 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1623 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1624 }
1625 script_prof_restore(tm);
1626}
1627#endif
1628
1629
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630#ifdef FEAT_FOLDING
1631/*
1632 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1633 * it in "*cp". Doesn't give error messages.
1634 */
1635 int
1636eval_foldexpr(arg, cp)
1637 char_u *arg;
1638 int *cp;
1639{
Bram Moolenaar33570922005-01-25 22:26:29 +00001640 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 int retval;
1642 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001643 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1644 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645
1646 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001647 if (use_sandbox)
1648 ++sandbox;
1649 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001651 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 retval = 0;
1653 else
1654 {
1655 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001656 if (tv.v_type == VAR_NUMBER)
1657 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001658 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 retval = 0;
1660 else
1661 {
1662 /* If the result is a string, check if there is a non-digit before
1663 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001664 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 if (!VIM_ISDIGIT(*s) && *s != '-')
1666 *cp = *s++;
1667 retval = atol((char *)s);
1668 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001669 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 }
1671 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001672 if (use_sandbox)
1673 --sandbox;
1674 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675
1676 return retval;
1677}
1678#endif
1679
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001681 * ":let" list all variable values
1682 * ":let var1 var2" list variable values
1683 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001684 * ":let var += expr" assignment command.
1685 * ":let var -= expr" assignment command.
1686 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001687 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 */
1689 void
1690ex_let(eap)
1691 exarg_T *eap;
1692{
1693 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001694 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001695 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001697 int var_count = 0;
1698 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001699 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001700 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001701 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702
Bram Moolenaardb552d602006-03-23 22:59:57 +00001703 argend = skip_var_list(arg, &var_count, &semicolon);
1704 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001705 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001706 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1707 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001708 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001709 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001711 /*
1712 * ":let" without "=": list variables
1713 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001714 if (*arg == '[')
1715 EMSG(_(e_invarg));
1716 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001717 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001718 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001719 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001720 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001721 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001722 list_glob_vars(&first);
1723 list_buf_vars(&first);
1724 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001725#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001726 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001727#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001728 list_script_vars(&first);
1729 list_func_vars(&first);
1730 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 eap->nextcmd = check_nextcmd(arg);
1733 }
1734 else
1735 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001736 op[0] = '=';
1737 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001738 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001739 {
1740 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1741 op[0] = expr[-1]; /* +=, -= or .= */
1742 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001743 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001744
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 if (eap->skip)
1746 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001747 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 if (eap->skip)
1749 {
1750 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001751 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 --emsg_skip;
1753 }
1754 else if (i != FAIL)
1755 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001756 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001757 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001758 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 }
1760 }
1761}
1762
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001763/*
1764 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1765 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001766 * When "nextchars" is not NULL it points to a string with characters that
1767 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1768 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001769 * Returns OK or FAIL;
1770 */
1771 static int
1772ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1773 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001774 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001775 int copy; /* copy values from "tv", don't move */
1776 int semicolon; /* from skip_var_list() */
1777 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001778 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001779{
1780 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001781 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001782 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001783 listitem_T *item;
1784 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001785
1786 if (*arg != '[')
1787 {
1788 /*
1789 * ":let var = expr" or ":for var in list"
1790 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001791 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001792 return FAIL;
1793 return OK;
1794 }
1795
1796 /*
1797 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1798 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001799 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001800 {
1801 EMSG(_(e_listreq));
1802 return FAIL;
1803 }
1804
1805 i = list_len(l);
1806 if (semicolon == 0 && var_count < i)
1807 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001808 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001809 return FAIL;
1810 }
1811 if (var_count - semicolon > i)
1812 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001813 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001814 return FAIL;
1815 }
1816
1817 item = l->lv_first;
1818 while (*arg != ']')
1819 {
1820 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001821 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001822 item = item->li_next;
1823 if (arg == NULL)
1824 return FAIL;
1825
1826 arg = skipwhite(arg);
1827 if (*arg == ';')
1828 {
1829 /* Put the rest of the list (may be empty) in the var after ';'.
1830 * Create a new list for this. */
1831 l = list_alloc();
1832 if (l == NULL)
1833 return FAIL;
1834 while (item != NULL)
1835 {
1836 list_append_tv(l, &item->li_tv);
1837 item = item->li_next;
1838 }
1839
1840 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001841 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001842 ltv.vval.v_list = l;
1843 l->lv_refcount = 1;
1844
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001845 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1846 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001847 clear_tv(&ltv);
1848 if (arg == NULL)
1849 return FAIL;
1850 break;
1851 }
1852 else if (*arg != ',' && *arg != ']')
1853 {
1854 EMSG2(_(e_intern2), "ex_let_vars()");
1855 return FAIL;
1856 }
1857 }
1858
1859 return OK;
1860}
1861
1862/*
1863 * Skip over assignable variable "var" or list of variables "[var, var]".
1864 * Used for ":let varvar = expr" and ":for varvar in expr".
1865 * For "[var, var]" increment "*var_count" for each variable.
1866 * for "[var, var; var]" set "semicolon".
1867 * Return NULL for an error.
1868 */
1869 static char_u *
1870skip_var_list(arg, var_count, semicolon)
1871 char_u *arg;
1872 int *var_count;
1873 int *semicolon;
1874{
1875 char_u *p, *s;
1876
1877 if (*arg == '[')
1878 {
1879 /* "[var, var]": find the matching ']'. */
1880 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001881 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001882 {
1883 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1884 s = skip_var_one(p);
1885 if (s == p)
1886 {
1887 EMSG2(_(e_invarg2), p);
1888 return NULL;
1889 }
1890 ++*var_count;
1891
1892 p = skipwhite(s);
1893 if (*p == ']')
1894 break;
1895 else if (*p == ';')
1896 {
1897 if (*semicolon == 1)
1898 {
1899 EMSG(_("Double ; in list of variables"));
1900 return NULL;
1901 }
1902 *semicolon = 1;
1903 }
1904 else if (*p != ',')
1905 {
1906 EMSG2(_(e_invarg2), p);
1907 return NULL;
1908 }
1909 }
1910 return p + 1;
1911 }
1912 else
1913 return skip_var_one(arg);
1914}
1915
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001916/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00001917 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00001918 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001919 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001920 static char_u *
1921skip_var_one(arg)
1922 char_u *arg;
1923{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001924 if (*arg == '@' && arg[1] != NUL)
1925 return arg + 2;
1926 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1927 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001928}
1929
Bram Moolenaara7043832005-01-21 11:56:39 +00001930/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001931 * List variables for hashtab "ht" with prefix "prefix".
1932 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001933 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001934 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001935list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00001936 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001937 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001938 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001939 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00001940{
Bram Moolenaar33570922005-01-25 22:26:29 +00001941 hashitem_T *hi;
1942 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001943 int todo;
1944
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001945 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001946 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1947 {
1948 if (!HASHITEM_EMPTY(hi))
1949 {
1950 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001951 di = HI2DI(hi);
1952 if (empty || di->di_tv.v_type != VAR_STRING
1953 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001954 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001955 }
1956 }
1957}
1958
1959/*
1960 * List global variables.
1961 */
1962 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001963list_glob_vars(first)
1964 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00001965{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001966 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001967}
1968
1969/*
1970 * List buffer variables.
1971 */
1972 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001973list_buf_vars(first)
1974 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00001975{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001976 char_u numbuf[NUMBUFLEN];
1977
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001978 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:",
1979 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001980
1981 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001982 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
1983 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001984}
1985
1986/*
1987 * List window variables.
1988 */
1989 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001990list_win_vars(first)
1991 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00001992{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001993 list_hashtable_vars(&curwin->w_vars.dv_hashtab,
1994 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001995}
1996
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001997#ifdef FEAT_WINDOWS
1998/*
1999 * List tab page variables.
2000 */
2001 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002002list_tab_vars(first)
2003 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002004{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002005 list_hashtable_vars(&curtab->tp_vars.dv_hashtab,
2006 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002007}
2008#endif
2009
Bram Moolenaara7043832005-01-21 11:56:39 +00002010/*
2011 * List Vim variables.
2012 */
2013 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002014list_vim_vars(first)
2015 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002016{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002017 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002018}
2019
2020/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002021 * List script-local variables, if there is a script.
2022 */
2023 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002024list_script_vars(first)
2025 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002026{
2027 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002028 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2029 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002030}
2031
2032/*
2033 * List function variables, if there is a function.
2034 */
2035 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002036list_func_vars(first)
2037 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002038{
2039 if (current_funccal != NULL)
2040 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002041 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002042}
2043
2044/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002045 * List variables in "arg".
2046 */
2047 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002048list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002049 exarg_T *eap;
2050 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002051 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002052{
2053 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002054 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002055 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002056 char_u *name_start;
2057 char_u *arg_subsc;
2058 char_u *tofree;
2059 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002060
2061 while (!ends_excmd(*arg) && !got_int)
2062 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002063 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002064 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002065 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002066 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2067 {
2068 emsg_severe = TRUE;
2069 EMSG(_(e_trailing));
2070 break;
2071 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002072 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002073 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002074 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002075 /* get_name_len() takes care of expanding curly braces */
2076 name_start = name = arg;
2077 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2078 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002079 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002080 /* This is mainly to keep test 49 working: when expanding
2081 * curly braces fails overrule the exception error message. */
2082 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002083 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002084 emsg_severe = TRUE;
2085 EMSG2(_(e_invarg2), arg);
2086 break;
2087 }
2088 error = TRUE;
2089 }
2090 else
2091 {
2092 if (tofree != NULL)
2093 name = tofree;
2094 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002095 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002096 else
2097 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002098 /* handle d.key, l[idx], f(expr) */
2099 arg_subsc = arg;
2100 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002101 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002102 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002103 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002104 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002105 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002106 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002107 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002108 case 'g': list_glob_vars(first); break;
2109 case 'b': list_buf_vars(first); break;
2110 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002111#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002112 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002113#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002114 case 'v': list_vim_vars(first); break;
2115 case 's': list_script_vars(first); break;
2116 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002117 default:
2118 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002119 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002120 }
2121 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002122 {
2123 char_u numbuf[NUMBUFLEN];
2124 char_u *tf;
2125 int c;
2126 char_u *s;
2127
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002128 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002129 c = *arg;
2130 *arg = NUL;
2131 list_one_var_a((char_u *)"",
2132 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002133 tv.v_type,
2134 s == NULL ? (char_u *)"" : s,
2135 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002136 *arg = c;
2137 vim_free(tf);
2138 }
2139 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002140 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002141 }
2142 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002143
2144 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002145 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002146
2147 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002148 }
2149
2150 return arg;
2151}
2152
2153/*
2154 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2155 * Returns a pointer to the char just after the var name.
2156 * Returns NULL if there is an error.
2157 */
2158 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002159ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002160 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002161 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002162 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002163 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002164 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002165{
2166 int c1;
2167 char_u *name;
2168 char_u *p;
2169 char_u *arg_end = NULL;
2170 int len;
2171 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002172 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002173
2174 /*
2175 * ":let $VAR = expr": Set environment variable.
2176 */
2177 if (*arg == '$')
2178 {
2179 /* Find the end of the name. */
2180 ++arg;
2181 name = arg;
2182 len = get_env_len(&arg);
2183 if (len == 0)
2184 EMSG2(_(e_invarg2), name - 1);
2185 else
2186 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002187 if (op != NULL && (*op == '+' || *op == '-'))
2188 EMSG2(_(e_letwrong), op);
2189 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002190 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002191 EMSG(_(e_letunexp));
2192 else
2193 {
2194 c1 = name[len];
2195 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002196 p = get_tv_string_chk(tv);
2197 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002198 {
2199 int mustfree = FALSE;
2200 char_u *s = vim_getenv(name, &mustfree);
2201
2202 if (s != NULL)
2203 {
2204 p = tofree = concat_str(s, p);
2205 if (mustfree)
2206 vim_free(s);
2207 }
2208 }
2209 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002210 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002211 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002212 if (STRICMP(name, "HOME") == 0)
2213 init_homedir();
2214 else if (didset_vim && STRICMP(name, "VIM") == 0)
2215 didset_vim = FALSE;
2216 else if (didset_vimruntime
2217 && STRICMP(name, "VIMRUNTIME") == 0)
2218 didset_vimruntime = FALSE;
2219 arg_end = arg;
2220 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002221 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002222 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002223 }
2224 }
2225 }
2226
2227 /*
2228 * ":let &option = expr": Set option value.
2229 * ":let &l:option = expr": Set local option value.
2230 * ":let &g:option = expr": Set global option value.
2231 */
2232 else if (*arg == '&')
2233 {
2234 /* Find the end of the name. */
2235 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002236 if (p == NULL || (endchars != NULL
2237 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002238 EMSG(_(e_letunexp));
2239 else
2240 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002241 long n;
2242 int opt_type;
2243 long numval;
2244 char_u *stringval = NULL;
2245 char_u *s;
2246
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002247 c1 = *p;
2248 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002249
2250 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002251 s = get_tv_string_chk(tv); /* != NULL if number or string */
2252 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002253 {
2254 opt_type = get_option_value(arg, &numval,
2255 &stringval, opt_flags);
2256 if ((opt_type == 1 && *op == '.')
2257 || (opt_type == 0 && *op != '.'))
2258 EMSG2(_(e_letwrong), op);
2259 else
2260 {
2261 if (opt_type == 1) /* number */
2262 {
2263 if (*op == '+')
2264 n = numval + n;
2265 else
2266 n = numval - n;
2267 }
2268 else if (opt_type == 0 && stringval != NULL) /* string */
2269 {
2270 s = concat_str(stringval, s);
2271 vim_free(stringval);
2272 stringval = s;
2273 }
2274 }
2275 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002276 if (s != NULL)
2277 {
2278 set_option_value(arg, n, s, opt_flags);
2279 arg_end = p;
2280 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002281 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002282 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002283 }
2284 }
2285
2286 /*
2287 * ":let @r = expr": Set register contents.
2288 */
2289 else if (*arg == '@')
2290 {
2291 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002292 if (op != NULL && (*op == '+' || *op == '-'))
2293 EMSG2(_(e_letwrong), op);
2294 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002295 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002296 EMSG(_(e_letunexp));
2297 else
2298 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002299 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002300 char_u *s;
2301
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002302 p = get_tv_string_chk(tv);
2303 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002304 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002305 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002306 if (s != NULL)
2307 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002308 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002309 vim_free(s);
2310 }
2311 }
2312 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002313 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002314 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002315 arg_end = arg + 1;
2316 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002317 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002318 }
2319 }
2320
2321 /*
2322 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002323 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002324 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002325 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002326 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002327 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002328
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002329 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002330 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002331 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002332 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2333 EMSG(_(e_letunexp));
2334 else
2335 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002336 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002337 arg_end = p;
2338 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002339 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002340 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002341 }
2342
2343 else
2344 EMSG2(_(e_invarg2), arg);
2345
2346 return arg_end;
2347}
2348
2349/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002350 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2351 */
2352 static int
2353check_changedtick(arg)
2354 char_u *arg;
2355{
2356 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2357 {
2358 EMSG2(_(e_readonlyvar), arg);
2359 return TRUE;
2360 }
2361 return FALSE;
2362}
2363
2364/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002365 * Get an lval: variable, Dict item or List item that can be assigned a value
2366 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2367 * "name.key", "name.key[expr]" etc.
2368 * Indexing only works if "name" is an existing List or Dictionary.
2369 * "name" points to the start of the name.
2370 * If "rettv" is not NULL it points to the value to be assigned.
2371 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2372 * wrong; must end in space or cmd separator.
2373 *
2374 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002375 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002376 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002377 */
2378 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002379get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002380 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002381 typval_T *rettv;
2382 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002383 int unlet;
2384 int skip;
2385 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002386 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002387{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002388 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002389 char_u *expr_start, *expr_end;
2390 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002391 dictitem_T *v;
2392 typval_T var1;
2393 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002394 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002395 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002396 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002397 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002398 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002399
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002400 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002401 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002402
2403 if (skip)
2404 {
2405 /* When skipping just find the end of the name. */
2406 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002407 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002408 }
2409
2410 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002411 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002412 if (expr_start != NULL)
2413 {
2414 /* Don't expand the name when we already know there is an error. */
2415 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2416 && *p != '[' && *p != '.')
2417 {
2418 EMSG(_(e_trailing));
2419 return NULL;
2420 }
2421
2422 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2423 if (lp->ll_exp_name == NULL)
2424 {
2425 /* Report an invalid expression in braces, unless the
2426 * expression evaluation has been cancelled due to an
2427 * aborting error, an interrupt, or an exception. */
2428 if (!aborting() && !quiet)
2429 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002430 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002431 EMSG2(_(e_invarg2), name);
2432 return NULL;
2433 }
2434 }
2435 lp->ll_name = lp->ll_exp_name;
2436 }
2437 else
2438 lp->ll_name = name;
2439
2440 /* Without [idx] or .key we are done. */
2441 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2442 return p;
2443
2444 cc = *p;
2445 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002446 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002447 if (v == NULL && !quiet)
2448 EMSG2(_(e_undefvar), lp->ll_name);
2449 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002450 if (v == NULL)
2451 return NULL;
2452
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002453 /*
2454 * Loop until no more [idx] or .key is following.
2455 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002456 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002457 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002458 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002459 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2460 && !(lp->ll_tv->v_type == VAR_DICT
2461 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002462 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002463 if (!quiet)
2464 EMSG(_("E689: Can only index a List or Dictionary"));
2465 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002466 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002467 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002468 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002469 if (!quiet)
2470 EMSG(_("E708: [:] must come last"));
2471 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002472 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002473
Bram Moolenaar8c711452005-01-14 21:53:12 +00002474 len = -1;
2475 if (*p == '.')
2476 {
2477 key = p + 1;
2478 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2479 ;
2480 if (len == 0)
2481 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002482 if (!quiet)
2483 EMSG(_(e_emptykey));
2484 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002485 }
2486 p = key + len;
2487 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002488 else
2489 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002490 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002491 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002492 if (*p == ':')
2493 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002494 else
2495 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002496 empty1 = FALSE;
2497 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002498 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002499 if (get_tv_string_chk(&var1) == NULL)
2500 {
2501 /* not a number or string */
2502 clear_tv(&var1);
2503 return NULL;
2504 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002505 }
2506
2507 /* Optionally get the second index [ :expr]. */
2508 if (*p == ':')
2509 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002510 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002511 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002512 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002513 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002514 if (!empty1)
2515 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002516 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002517 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002518 if (rettv != NULL && (rettv->v_type != VAR_LIST
2519 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002520 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002521 if (!quiet)
2522 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002523 if (!empty1)
2524 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002525 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002526 }
2527 p = skipwhite(p + 1);
2528 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002529 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002530 else
2531 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002532 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002533 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2534 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002535 if (!empty1)
2536 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002537 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002538 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002539 if (get_tv_string_chk(&var2) == NULL)
2540 {
2541 /* not a number or string */
2542 if (!empty1)
2543 clear_tv(&var1);
2544 clear_tv(&var2);
2545 return NULL;
2546 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002547 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002548 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002549 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002550 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002551 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002552
Bram Moolenaar8c711452005-01-14 21:53:12 +00002553 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002554 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002555 if (!quiet)
2556 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002557 if (!empty1)
2558 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002559 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002560 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002561 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002562 }
2563
2564 /* Skip to past ']'. */
2565 ++p;
2566 }
2567
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002568 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002569 {
2570 if (len == -1)
2571 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002572 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002573 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002574 if (*key == NUL)
2575 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002576 if (!quiet)
2577 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002578 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002579 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002580 }
2581 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002582 lp->ll_list = NULL;
2583 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002584 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002585 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002586 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002587 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002588 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002589 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002590 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002591 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002592 if (len == -1)
2593 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002594 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002595 }
2596 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002597 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002598 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002599 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002600 if (len == -1)
2601 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002602 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002603 p = NULL;
2604 break;
2605 }
2606 if (len == -1)
2607 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002608 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002609 }
2610 else
2611 {
2612 /*
2613 * Get the number and item for the only or first index of the List.
2614 */
2615 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002616 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002617 else
2618 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002619 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002620 clear_tv(&var1);
2621 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002622 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002623 lp->ll_list = lp->ll_tv->vval.v_list;
2624 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2625 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002626 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002627 if (lp->ll_n1 < 0)
2628 {
2629 lp->ll_n1 = 0;
2630 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2631 }
2632 }
2633 if (lp->ll_li == NULL)
2634 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002635 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002636 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002637 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002638 }
2639
2640 /*
2641 * May need to find the item or absolute index for the second
2642 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002643 * When no index given: "lp->ll_empty2" is TRUE.
2644 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002645 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002646 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002647 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002648 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002649 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002650 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002651 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002652 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002653 if (ni == NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002654 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002655 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002656 }
2657
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002658 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2659 if (lp->ll_n1 < 0)
2660 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2661 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002662 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002663 }
2664
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002665 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002666 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002667 }
2668
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002669 return p;
2670}
2671
2672/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002673 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002674 */
2675 static void
2676clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002677 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002678{
2679 vim_free(lp->ll_exp_name);
2680 vim_free(lp->ll_newkey);
2681}
2682
2683/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002684 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002685 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002686 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002687 */
2688 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002689set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002690 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002691 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002692 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002693 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002694 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002695{
2696 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002697 listitem_T *ri;
2698 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002699
2700 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002701 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002702 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002703 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002704 cc = *endp;
2705 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002706 if (op != NULL && *op != '=')
2707 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002708 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002709
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002710 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002711 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002712 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002713 {
2714 if (tv_op(&tv, rettv, op) == OK)
2715 set_var(lp->ll_name, &tv, FALSE);
2716 clear_tv(&tv);
2717 }
2718 }
2719 else
2720 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002721 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002722 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002723 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002724 else if (tv_check_lock(lp->ll_newkey == NULL
2725 ? lp->ll_tv->v_lock
2726 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2727 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002728 else if (lp->ll_range)
2729 {
2730 /*
2731 * Assign the List values to the list items.
2732 */
2733 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002734 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002735 if (op != NULL && *op != '=')
2736 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2737 else
2738 {
2739 clear_tv(&lp->ll_li->li_tv);
2740 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2741 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002742 ri = ri->li_next;
2743 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2744 break;
2745 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002746 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002747 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002748 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002749 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002750 ri = NULL;
2751 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002752 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002753 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002754 lp->ll_li = lp->ll_li->li_next;
2755 ++lp->ll_n1;
2756 }
2757 if (ri != NULL)
2758 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002759 else if (lp->ll_empty2
2760 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002761 : lp->ll_n1 != lp->ll_n2)
2762 EMSG(_("E711: List value has not enough items"));
2763 }
2764 else
2765 {
2766 /*
2767 * Assign to a List or Dictionary item.
2768 */
2769 if (lp->ll_newkey != NULL)
2770 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002771 if (op != NULL && *op != '=')
2772 {
2773 EMSG2(_(e_letwrong), op);
2774 return;
2775 }
2776
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002777 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002778 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002779 if (di == NULL)
2780 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002781 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2782 {
2783 vim_free(di);
2784 return;
2785 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002786 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002787 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002788 else if (op != NULL && *op != '=')
2789 {
2790 tv_op(lp->ll_tv, rettv, op);
2791 return;
2792 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002793 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002794 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002795
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002796 /*
2797 * Assign the value to the variable or list item.
2798 */
2799 if (copy)
2800 copy_tv(rettv, lp->ll_tv);
2801 else
2802 {
2803 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002804 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002805 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002806 }
2807 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002808}
2809
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002810/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002811 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2812 * Returns OK or FAIL.
2813 */
2814 static int
2815tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002816 typval_T *tv1;
2817 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002818 char_u *op;
2819{
2820 long n;
2821 char_u numbuf[NUMBUFLEN];
2822 char_u *s;
2823
2824 /* Can't do anything with a Funcref or a Dict on the right. */
2825 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2826 {
2827 switch (tv1->v_type)
2828 {
2829 case VAR_DICT:
2830 case VAR_FUNC:
2831 break;
2832
2833 case VAR_LIST:
2834 if (*op != '+' || tv2->v_type != VAR_LIST)
2835 break;
2836 /* List += List */
2837 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2838 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2839 return OK;
2840
2841 case VAR_NUMBER:
2842 case VAR_STRING:
2843 if (tv2->v_type == VAR_LIST)
2844 break;
2845 if (*op == '+' || *op == '-')
2846 {
2847 /* nr += nr or nr -= nr*/
2848 n = get_tv_number(tv1);
2849 if (*op == '+')
2850 n += get_tv_number(tv2);
2851 else
2852 n -= get_tv_number(tv2);
2853 clear_tv(tv1);
2854 tv1->v_type = VAR_NUMBER;
2855 tv1->vval.v_number = n;
2856 }
2857 else
2858 {
2859 /* str .= str */
2860 s = get_tv_string(tv1);
2861 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2862 clear_tv(tv1);
2863 tv1->v_type = VAR_STRING;
2864 tv1->vval.v_string = s;
2865 }
2866 return OK;
2867 }
2868 }
2869
2870 EMSG2(_(e_letwrong), op);
2871 return FAIL;
2872}
2873
2874/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002875 * Add a watcher to a list.
2876 */
2877 static void
2878list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002879 list_T *l;
2880 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002881{
2882 lw->lw_next = l->lv_watch;
2883 l->lv_watch = lw;
2884}
2885
2886/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002887 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002888 * No warning when it isn't found...
2889 */
2890 static void
2891list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002892 list_T *l;
2893 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002894{
Bram Moolenaar33570922005-01-25 22:26:29 +00002895 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002896
2897 lwp = &l->lv_watch;
2898 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2899 {
2900 if (lw == lwrem)
2901 {
2902 *lwp = lw->lw_next;
2903 break;
2904 }
2905 lwp = &lw->lw_next;
2906 }
2907}
2908
2909/*
2910 * Just before removing an item from a list: advance watchers to the next
2911 * item.
2912 */
2913 static void
2914list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002915 list_T *l;
2916 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002917{
Bram Moolenaar33570922005-01-25 22:26:29 +00002918 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002919
2920 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2921 if (lw->lw_item == item)
2922 lw->lw_item = item->li_next;
2923}
2924
2925/*
2926 * Evaluate the expression used in a ":for var in expr" command.
2927 * "arg" points to "var".
2928 * Set "*errp" to TRUE for an error, FALSE otherwise;
2929 * Return a pointer that holds the info. Null when there is an error.
2930 */
2931 void *
2932eval_for_line(arg, errp, nextcmdp, skip)
2933 char_u *arg;
2934 int *errp;
2935 char_u **nextcmdp;
2936 int skip;
2937{
Bram Moolenaar33570922005-01-25 22:26:29 +00002938 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002939 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002940 typval_T tv;
2941 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002942
2943 *errp = TRUE; /* default: there is an error */
2944
Bram Moolenaar33570922005-01-25 22:26:29 +00002945 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002946 if (fi == NULL)
2947 return NULL;
2948
2949 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2950 if (expr == NULL)
2951 return fi;
2952
2953 expr = skipwhite(expr);
2954 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2955 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002956 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002957 return fi;
2958 }
2959
2960 if (skip)
2961 ++emsg_skip;
2962 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2963 {
2964 *errp = FALSE;
2965 if (!skip)
2966 {
2967 l = tv.vval.v_list;
2968 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002969 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002970 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002971 clear_tv(&tv);
2972 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002973 else
2974 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002975 /* No need to increment the refcount, it's already set for the
2976 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002977 fi->fi_list = l;
2978 list_add_watch(l, &fi->fi_lw);
2979 fi->fi_lw.lw_item = l->lv_first;
2980 }
2981 }
2982 }
2983 if (skip)
2984 --emsg_skip;
2985
2986 return fi;
2987}
2988
2989/*
2990 * Use the first item in a ":for" list. Advance to the next.
2991 * Assign the values to the variable (list). "arg" points to the first one.
2992 * Return TRUE when a valid item was found, FALSE when at end of list or
2993 * something wrong.
2994 */
2995 int
2996next_for_item(fi_void, arg)
2997 void *fi_void;
2998 char_u *arg;
2999{
Bram Moolenaar33570922005-01-25 22:26:29 +00003000 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003001 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003002 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003003
3004 item = fi->fi_lw.lw_item;
3005 if (item == NULL)
3006 result = FALSE;
3007 else
3008 {
3009 fi->fi_lw.lw_item = item->li_next;
3010 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3011 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3012 }
3013 return result;
3014}
3015
3016/*
3017 * Free the structure used to store info used by ":for".
3018 */
3019 void
3020free_for_info(fi_void)
3021 void *fi_void;
3022{
Bram Moolenaar33570922005-01-25 22:26:29 +00003023 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003024
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003025 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003026 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003027 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003028 list_unref(fi->fi_list);
3029 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003030 vim_free(fi);
3031}
3032
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3034
3035 void
3036set_context_for_expression(xp, arg, cmdidx)
3037 expand_T *xp;
3038 char_u *arg;
3039 cmdidx_T cmdidx;
3040{
3041 int got_eq = FALSE;
3042 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003043 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003045 if (cmdidx == CMD_let)
3046 {
3047 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003048 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003049 {
3050 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003051 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003052 {
3053 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003054 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003055 if (vim_iswhite(*p))
3056 break;
3057 }
3058 return;
3059 }
3060 }
3061 else
3062 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3063 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064 while ((xp->xp_pattern = vim_strpbrk(arg,
3065 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3066 {
3067 c = *xp->xp_pattern;
3068 if (c == '&')
3069 {
3070 c = xp->xp_pattern[1];
3071 if (c == '&')
3072 {
3073 ++xp->xp_pattern;
3074 xp->xp_context = cmdidx != CMD_let || got_eq
3075 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3076 }
3077 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003078 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003080 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3081 xp->xp_pattern += 2;
3082
3083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 }
3085 else if (c == '$')
3086 {
3087 /* environment variable */
3088 xp->xp_context = EXPAND_ENV_VARS;
3089 }
3090 else if (c == '=')
3091 {
3092 got_eq = TRUE;
3093 xp->xp_context = EXPAND_EXPRESSION;
3094 }
3095 else if (c == '<'
3096 && xp->xp_context == EXPAND_FUNCTIONS
3097 && vim_strchr(xp->xp_pattern, '(') == NULL)
3098 {
3099 /* Function name can start with "<SNR>" */
3100 break;
3101 }
3102 else if (cmdidx != CMD_let || got_eq)
3103 {
3104 if (c == '"') /* string */
3105 {
3106 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3107 if (c == '\\' && xp->xp_pattern[1] != NUL)
3108 ++xp->xp_pattern;
3109 xp->xp_context = EXPAND_NOTHING;
3110 }
3111 else if (c == '\'') /* literal string */
3112 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003113 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3115 /* skip */ ;
3116 xp->xp_context = EXPAND_NOTHING;
3117 }
3118 else if (c == '|')
3119 {
3120 if (xp->xp_pattern[1] == '|')
3121 {
3122 ++xp->xp_pattern;
3123 xp->xp_context = EXPAND_EXPRESSION;
3124 }
3125 else
3126 xp->xp_context = EXPAND_COMMANDS;
3127 }
3128 else
3129 xp->xp_context = EXPAND_EXPRESSION;
3130 }
3131 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003132 /* Doesn't look like something valid, expand as an expression
3133 * anyway. */
3134 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 arg = xp->xp_pattern;
3136 if (*arg != NUL)
3137 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3138 /* skip */ ;
3139 }
3140 xp->xp_pattern = arg;
3141}
3142
3143#endif /* FEAT_CMDL_COMPL */
3144
3145/*
3146 * ":1,25call func(arg1, arg2)" function call.
3147 */
3148 void
3149ex_call(eap)
3150 exarg_T *eap;
3151{
3152 char_u *arg = eap->arg;
3153 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003155 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003157 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 linenr_T lnum;
3159 int doesrange;
3160 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003161 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003163 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003164 if (fudi.fd_newkey != NULL)
3165 {
3166 /* Still need to give an error message for missing key. */
3167 EMSG2(_(e_dictkey), fudi.fd_newkey);
3168 vim_free(fudi.fd_newkey);
3169 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003170 if (tofree == NULL)
3171 return;
3172
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003173 /* Increase refcount on dictionary, it could get deleted when evaluating
3174 * the arguments. */
3175 if (fudi.fd_dict != NULL)
3176 ++fudi.fd_dict->dv_refcount;
3177
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003178 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003179 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003180 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181
Bram Moolenaar532c7802005-01-27 14:44:31 +00003182 /* Skip white space to allow ":call func ()". Not good, but required for
3183 * backward compatibility. */
3184 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003185 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186
3187 if (*startarg != '(')
3188 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003189 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 goto end;
3191 }
3192
3193 /*
3194 * When skipping, evaluate the function once, to find the end of the
3195 * arguments.
3196 * When the function takes a range, this is discovered after the first
3197 * call, and the loop is broken.
3198 */
3199 if (eap->skip)
3200 {
3201 ++emsg_skip;
3202 lnum = eap->line2; /* do it once, also with an invalid range */
3203 }
3204 else
3205 lnum = eap->line1;
3206 for ( ; lnum <= eap->line2; ++lnum)
3207 {
3208 if (!eap->skip && eap->addr_count > 0)
3209 {
3210 curwin->w_cursor.lnum = lnum;
3211 curwin->w_cursor.col = 0;
3212 }
3213 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003214 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003215 eap->line1, eap->line2, &doesrange,
3216 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 {
3218 failed = TRUE;
3219 break;
3220 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003221
3222 /* Handle a function returning a Funcref, Dictionary or List. */
3223 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3224 {
3225 failed = TRUE;
3226 break;
3227 }
3228
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003229 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 if (doesrange || eap->skip)
3231 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003232
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003234 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003235 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003236 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 if (aborting())
3238 break;
3239 }
3240 if (eap->skip)
3241 --emsg_skip;
3242
3243 if (!failed)
3244 {
3245 /* Check for trailing illegal characters and a following command. */
3246 if (!ends_excmd(*arg))
3247 {
3248 emsg_severe = TRUE;
3249 EMSG(_(e_trailing));
3250 }
3251 else
3252 eap->nextcmd = check_nextcmd(arg);
3253 }
3254
3255end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003256 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003257 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258}
3259
3260/*
3261 * ":unlet[!] var1 ... " command.
3262 */
3263 void
3264ex_unlet(eap)
3265 exarg_T *eap;
3266{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003267 ex_unletlock(eap, eap->arg, 0);
3268}
3269
3270/*
3271 * ":lockvar" and ":unlockvar" commands
3272 */
3273 void
3274ex_lockvar(eap)
3275 exarg_T *eap;
3276{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003278 int deep = 2;
3279
3280 if (eap->forceit)
3281 deep = -1;
3282 else if (vim_isdigit(*arg))
3283 {
3284 deep = getdigits(&arg);
3285 arg = skipwhite(arg);
3286 }
3287
3288 ex_unletlock(eap, arg, deep);
3289}
3290
3291/*
3292 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3293 */
3294 static void
3295ex_unletlock(eap, argstart, deep)
3296 exarg_T *eap;
3297 char_u *argstart;
3298 int deep;
3299{
3300 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003303 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304
3305 do
3306 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003307 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003308 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3309 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003310 if (lv.ll_name == NULL)
3311 error = TRUE; /* error but continue parsing */
3312 if (name_end == NULL || (!vim_iswhite(*name_end)
3313 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003315 if (name_end != NULL)
3316 {
3317 emsg_severe = TRUE;
3318 EMSG(_(e_trailing));
3319 }
3320 if (!(eap->skip || error))
3321 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 break;
3323 }
3324
3325 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003326 {
3327 if (eap->cmdidx == CMD_unlet)
3328 {
3329 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3330 error = TRUE;
3331 }
3332 else
3333 {
3334 if (do_lock_var(&lv, name_end, deep,
3335 eap->cmdidx == CMD_lockvar) == FAIL)
3336 error = TRUE;
3337 }
3338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003340 if (!eap->skip)
3341 clear_lval(&lv);
3342
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 arg = skipwhite(name_end);
3344 } while (!ends_excmd(*arg));
3345
3346 eap->nextcmd = check_nextcmd(arg);
3347}
3348
Bram Moolenaar8c711452005-01-14 21:53:12 +00003349 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003350do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003351 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003352 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003353 int forceit;
3354{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003355 int ret = OK;
3356 int cc;
3357
3358 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003359 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003360 cc = *name_end;
3361 *name_end = NUL;
3362
3363 /* Normal name or expanded name. */
3364 if (check_changedtick(lp->ll_name))
3365 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003366 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003367 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003368 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003369 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003370 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3371 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003372 else if (lp->ll_range)
3373 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003374 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003375
3376 /* Delete a range of List items. */
3377 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3378 {
3379 li = lp->ll_li->li_next;
3380 listitem_remove(lp->ll_list, lp->ll_li);
3381 lp->ll_li = li;
3382 ++lp->ll_n1;
3383 }
3384 }
3385 else
3386 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003387 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003388 /* unlet a List item. */
3389 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003390 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003391 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003392 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003393 }
3394
3395 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003396}
3397
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398/*
3399 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003400 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 */
3402 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003403do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003405 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406{
Bram Moolenaar33570922005-01-25 22:26:29 +00003407 hashtab_T *ht;
3408 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003409 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410
Bram Moolenaar33570922005-01-25 22:26:29 +00003411 ht = find_var_ht(name, &varname);
3412 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003414 hi = hash_find(ht, varname);
3415 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003416 {
Bram Moolenaar4e957af2006-09-02 11:41:07 +00003417 if (var_check_fixed(HI2DI(hi)->di_flags, name))
3418 return FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003419 if (var_check_ro(HI2DI(hi)->di_flags, name))
3420 return FAIL;
3421 delete_var(ht, hi);
3422 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003425 if (forceit)
3426 return OK;
3427 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 return FAIL;
3429}
3430
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003431/*
3432 * Lock or unlock variable indicated by "lp".
3433 * "deep" is the levels to go (-1 for unlimited);
3434 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3435 */
3436 static int
3437do_lock_var(lp, name_end, deep, lock)
3438 lval_T *lp;
3439 char_u *name_end;
3440 int deep;
3441 int lock;
3442{
3443 int ret = OK;
3444 int cc;
3445 dictitem_T *di;
3446
3447 if (deep == 0) /* nothing to do */
3448 return OK;
3449
3450 if (lp->ll_tv == NULL)
3451 {
3452 cc = *name_end;
3453 *name_end = NUL;
3454
3455 /* Normal name or expanded name. */
3456 if (check_changedtick(lp->ll_name))
3457 ret = FAIL;
3458 else
3459 {
3460 di = find_var(lp->ll_name, NULL);
3461 if (di == NULL)
3462 ret = FAIL;
3463 else
3464 {
3465 if (lock)
3466 di->di_flags |= DI_FLAGS_LOCK;
3467 else
3468 di->di_flags &= ~DI_FLAGS_LOCK;
3469 item_lock(&di->di_tv, deep, lock);
3470 }
3471 }
3472 *name_end = cc;
3473 }
3474 else if (lp->ll_range)
3475 {
3476 listitem_T *li = lp->ll_li;
3477
3478 /* (un)lock a range of List items. */
3479 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3480 {
3481 item_lock(&li->li_tv, deep, lock);
3482 li = li->li_next;
3483 ++lp->ll_n1;
3484 }
3485 }
3486 else if (lp->ll_list != NULL)
3487 /* (un)lock a List item. */
3488 item_lock(&lp->ll_li->li_tv, deep, lock);
3489 else
3490 /* un(lock) a Dictionary item. */
3491 item_lock(&lp->ll_di->di_tv, deep, lock);
3492
3493 return ret;
3494}
3495
3496/*
3497 * Lock or unlock an item. "deep" is nr of levels to go.
3498 */
3499 static void
3500item_lock(tv, deep, lock)
3501 typval_T *tv;
3502 int deep;
3503 int lock;
3504{
3505 static int recurse = 0;
3506 list_T *l;
3507 listitem_T *li;
3508 dict_T *d;
3509 hashitem_T *hi;
3510 int todo;
3511
3512 if (recurse >= DICT_MAXNEST)
3513 {
3514 EMSG(_("E743: variable nested too deep for (un)lock"));
3515 return;
3516 }
3517 if (deep == 0)
3518 return;
3519 ++recurse;
3520
3521 /* lock/unlock the item itself */
3522 if (lock)
3523 tv->v_lock |= VAR_LOCKED;
3524 else
3525 tv->v_lock &= ~VAR_LOCKED;
3526
3527 switch (tv->v_type)
3528 {
3529 case VAR_LIST:
3530 if ((l = tv->vval.v_list) != NULL)
3531 {
3532 if (lock)
3533 l->lv_lock |= VAR_LOCKED;
3534 else
3535 l->lv_lock &= ~VAR_LOCKED;
3536 if (deep < 0 || deep > 1)
3537 /* recursive: lock/unlock the items the List contains */
3538 for (li = l->lv_first; li != NULL; li = li->li_next)
3539 item_lock(&li->li_tv, deep - 1, lock);
3540 }
3541 break;
3542 case VAR_DICT:
3543 if ((d = tv->vval.v_dict) != NULL)
3544 {
3545 if (lock)
3546 d->dv_lock |= VAR_LOCKED;
3547 else
3548 d->dv_lock &= ~VAR_LOCKED;
3549 if (deep < 0 || deep > 1)
3550 {
3551 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003552 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003553 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3554 {
3555 if (!HASHITEM_EMPTY(hi))
3556 {
3557 --todo;
3558 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3559 }
3560 }
3561 }
3562 }
3563 }
3564 --recurse;
3565}
3566
Bram Moolenaara40058a2005-07-11 22:42:07 +00003567/*
3568 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3569 * it refers to a List or Dictionary that is locked.
3570 */
3571 static int
3572tv_islocked(tv)
3573 typval_T *tv;
3574{
3575 return (tv->v_lock & VAR_LOCKED)
3576 || (tv->v_type == VAR_LIST
3577 && tv->vval.v_list != NULL
3578 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3579 || (tv->v_type == VAR_DICT
3580 && tv->vval.v_dict != NULL
3581 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3582}
3583
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3585/*
3586 * Delete all "menutrans_" variables.
3587 */
3588 void
3589del_menutrans_vars()
3590{
Bram Moolenaar33570922005-01-25 22:26:29 +00003591 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003592 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593
Bram Moolenaar33570922005-01-25 22:26:29 +00003594 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003595 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003596 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003597 {
3598 if (!HASHITEM_EMPTY(hi))
3599 {
3600 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003601 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3602 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003603 }
3604 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003605 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606}
3607#endif
3608
3609#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3610
3611/*
3612 * Local string buffer for the next two functions to store a variable name
3613 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3614 * get_user_var_name().
3615 */
3616
3617static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3618
3619static char_u *varnamebuf = NULL;
3620static int varnamebuflen = 0;
3621
3622/*
3623 * Function to concatenate a prefix and a variable name.
3624 */
3625 static char_u *
3626cat_prefix_varname(prefix, name)
3627 int prefix;
3628 char_u *name;
3629{
3630 int len;
3631
3632 len = (int)STRLEN(name) + 3;
3633 if (len > varnamebuflen)
3634 {
3635 vim_free(varnamebuf);
3636 len += 10; /* some additional space */
3637 varnamebuf = alloc(len);
3638 if (varnamebuf == NULL)
3639 {
3640 varnamebuflen = 0;
3641 return NULL;
3642 }
3643 varnamebuflen = len;
3644 }
3645 *varnamebuf = prefix;
3646 varnamebuf[1] = ':';
3647 STRCPY(varnamebuf + 2, name);
3648 return varnamebuf;
3649}
3650
3651/*
3652 * Function given to ExpandGeneric() to obtain the list of user defined
3653 * (global/buffer/window/built-in) variable names.
3654 */
3655/*ARGSUSED*/
3656 char_u *
3657get_user_var_name(xp, idx)
3658 expand_T *xp;
3659 int idx;
3660{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003661 static long_u gdone;
3662 static long_u bdone;
3663 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003664#ifdef FEAT_WINDOWS
3665 static long_u tdone;
3666#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003667 static int vidx;
3668 static hashitem_T *hi;
3669 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670
3671 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003672 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003673 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003674#ifdef FEAT_WINDOWS
3675 tdone = 0;
3676#endif
3677 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003678
3679 /* Global variables */
3680 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003682 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003683 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003684 else
3685 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003686 while (HASHITEM_EMPTY(hi))
3687 ++hi;
3688 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3689 return cat_prefix_varname('g', hi->hi_key);
3690 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003692
3693 /* b: variables */
3694 ht = &curbuf->b_vars.dv_hashtab;
3695 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003697 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003698 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003699 else
3700 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003701 while (HASHITEM_EMPTY(hi))
3702 ++hi;
3703 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003705 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003707 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 return (char_u *)"b:changedtick";
3709 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003710
3711 /* w: variables */
3712 ht = &curwin->w_vars.dv_hashtab;
3713 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003715 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003716 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003717 else
3718 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003719 while (HASHITEM_EMPTY(hi))
3720 ++hi;
3721 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003723
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003724#ifdef FEAT_WINDOWS
3725 /* t: variables */
3726 ht = &curtab->tp_vars.dv_hashtab;
3727 if (tdone < ht->ht_used)
3728 {
3729 if (tdone++ == 0)
3730 hi = ht->ht_array;
3731 else
3732 ++hi;
3733 while (HASHITEM_EMPTY(hi))
3734 ++hi;
3735 return cat_prefix_varname('t', hi->hi_key);
3736 }
3737#endif
3738
Bram Moolenaar33570922005-01-25 22:26:29 +00003739 /* v: variables */
3740 if (vidx < VV_LEN)
3741 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742
3743 vim_free(varnamebuf);
3744 varnamebuf = NULL;
3745 varnamebuflen = 0;
3746 return NULL;
3747}
3748
3749#endif /* FEAT_CMDL_COMPL */
3750
3751/*
3752 * types for expressions.
3753 */
3754typedef enum
3755{
3756 TYPE_UNKNOWN = 0
3757 , TYPE_EQUAL /* == */
3758 , TYPE_NEQUAL /* != */
3759 , TYPE_GREATER /* > */
3760 , TYPE_GEQUAL /* >= */
3761 , TYPE_SMALLER /* < */
3762 , TYPE_SEQUAL /* <= */
3763 , TYPE_MATCH /* =~ */
3764 , TYPE_NOMATCH /* !~ */
3765} exptype_T;
3766
3767/*
3768 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003769 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3771 */
3772
3773/*
3774 * Handle zero level expression.
3775 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003776 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003777 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 * Return OK or FAIL.
3779 */
3780 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003781eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003783 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 char_u **nextcmd;
3785 int evaluate;
3786{
3787 int ret;
3788 char_u *p;
3789
3790 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003791 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 if (ret == FAIL || !ends_excmd(*p))
3793 {
3794 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003795 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 /*
3797 * Report the invalid expression unless the expression evaluation has
3798 * been cancelled due to an aborting error, an interrupt, or an
3799 * exception.
3800 */
3801 if (!aborting())
3802 EMSG2(_(e_invexpr2), arg);
3803 ret = FAIL;
3804 }
3805 if (nextcmd != NULL)
3806 *nextcmd = check_nextcmd(p);
3807
3808 return ret;
3809}
3810
3811/*
3812 * Handle top level expression:
3813 * expr1 ? expr0 : expr0
3814 *
3815 * "arg" must point to the first non-white of the expression.
3816 * "arg" is advanced to the next non-white after the recognized expression.
3817 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003818 * Note: "rettv.v_lock" is not set.
3819 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 * Return OK or FAIL.
3821 */
3822 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003823eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003825 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 int evaluate;
3827{
3828 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003829 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830
3831 /*
3832 * Get the first variable.
3833 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003834 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 return FAIL;
3836
3837 if ((*arg)[0] == '?')
3838 {
3839 result = FALSE;
3840 if (evaluate)
3841 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003842 int error = FALSE;
3843
3844 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003846 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003847 if (error)
3848 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 }
3850
3851 /*
3852 * Get the second variable.
3853 */
3854 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003855 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 return FAIL;
3857
3858 /*
3859 * Check for the ":".
3860 */
3861 if ((*arg)[0] != ':')
3862 {
3863 EMSG(_("E109: Missing ':' after '?'"));
3864 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003865 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 return FAIL;
3867 }
3868
3869 /*
3870 * Get the third variable.
3871 */
3872 *arg = skipwhite(*arg + 1);
3873 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3874 {
3875 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003876 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 return FAIL;
3878 }
3879 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003880 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 }
3882
3883 return OK;
3884}
3885
3886/*
3887 * Handle first level expression:
3888 * expr2 || expr2 || expr2 logical OR
3889 *
3890 * "arg" must point to the first non-white of the expression.
3891 * "arg" is advanced to the next non-white after the recognized expression.
3892 *
3893 * Return OK or FAIL.
3894 */
3895 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003896eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003898 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 int evaluate;
3900{
Bram Moolenaar33570922005-01-25 22:26:29 +00003901 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 long result;
3903 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003904 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905
3906 /*
3907 * Get the first variable.
3908 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003909 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 return FAIL;
3911
3912 /*
3913 * Repeat until there is no following "||".
3914 */
3915 first = TRUE;
3916 result = FALSE;
3917 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3918 {
3919 if (evaluate && first)
3920 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003921 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003923 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003924 if (error)
3925 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 first = FALSE;
3927 }
3928
3929 /*
3930 * Get the second variable.
3931 */
3932 *arg = skipwhite(*arg + 2);
3933 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3934 return FAIL;
3935
3936 /*
3937 * Compute the result.
3938 */
3939 if (evaluate && !result)
3940 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003941 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003943 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003944 if (error)
3945 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 }
3947 if (evaluate)
3948 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003949 rettv->v_type = VAR_NUMBER;
3950 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 }
3952 }
3953
3954 return OK;
3955}
3956
3957/*
3958 * Handle second level expression:
3959 * expr3 && expr3 && expr3 logical AND
3960 *
3961 * "arg" must point to the first non-white of the expression.
3962 * "arg" is advanced to the next non-white after the recognized expression.
3963 *
3964 * Return OK or FAIL.
3965 */
3966 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003967eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003969 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 int evaluate;
3971{
Bram Moolenaar33570922005-01-25 22:26:29 +00003972 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 long result;
3974 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003975 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976
3977 /*
3978 * Get the first variable.
3979 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003980 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 return FAIL;
3982
3983 /*
3984 * Repeat until there is no following "&&".
3985 */
3986 first = TRUE;
3987 result = TRUE;
3988 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3989 {
3990 if (evaluate && first)
3991 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003992 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003994 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003995 if (error)
3996 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 first = FALSE;
3998 }
3999
4000 /*
4001 * Get the second variable.
4002 */
4003 *arg = skipwhite(*arg + 2);
4004 if (eval4(arg, &var2, evaluate && result) == FAIL)
4005 return FAIL;
4006
4007 /*
4008 * Compute the result.
4009 */
4010 if (evaluate && result)
4011 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004012 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004014 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004015 if (error)
4016 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 }
4018 if (evaluate)
4019 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004020 rettv->v_type = VAR_NUMBER;
4021 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 }
4023 }
4024
4025 return OK;
4026}
4027
4028/*
4029 * Handle third level expression:
4030 * var1 == var2
4031 * var1 =~ var2
4032 * var1 != var2
4033 * var1 !~ var2
4034 * var1 > var2
4035 * var1 >= var2
4036 * var1 < var2
4037 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004038 * var1 is var2
4039 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 *
4041 * "arg" must point to the first non-white of the expression.
4042 * "arg" is advanced to the next non-white after the recognized expression.
4043 *
4044 * Return OK or FAIL.
4045 */
4046 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004047eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004049 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 int evaluate;
4051{
Bram Moolenaar33570922005-01-25 22:26:29 +00004052 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 char_u *p;
4054 int i;
4055 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004056 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 int len = 2;
4058 long n1, n2;
4059 char_u *s1, *s2;
4060 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4061 regmatch_T regmatch;
4062 int ic;
4063 char_u *save_cpo;
4064
4065 /*
4066 * Get the first variable.
4067 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004068 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 return FAIL;
4070
4071 p = *arg;
4072 switch (p[0])
4073 {
4074 case '=': if (p[1] == '=')
4075 type = TYPE_EQUAL;
4076 else if (p[1] == '~')
4077 type = TYPE_MATCH;
4078 break;
4079 case '!': if (p[1] == '=')
4080 type = TYPE_NEQUAL;
4081 else if (p[1] == '~')
4082 type = TYPE_NOMATCH;
4083 break;
4084 case '>': if (p[1] != '=')
4085 {
4086 type = TYPE_GREATER;
4087 len = 1;
4088 }
4089 else
4090 type = TYPE_GEQUAL;
4091 break;
4092 case '<': if (p[1] != '=')
4093 {
4094 type = TYPE_SMALLER;
4095 len = 1;
4096 }
4097 else
4098 type = TYPE_SEQUAL;
4099 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004100 case 'i': if (p[1] == 's')
4101 {
4102 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4103 len = 5;
4104 if (!vim_isIDc(p[len]))
4105 {
4106 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4107 type_is = TRUE;
4108 }
4109 }
4110 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 }
4112
4113 /*
4114 * If there is a comparitive operator, use it.
4115 */
4116 if (type != TYPE_UNKNOWN)
4117 {
4118 /* extra question mark appended: ignore case */
4119 if (p[len] == '?')
4120 {
4121 ic = TRUE;
4122 ++len;
4123 }
4124 /* extra '#' appended: match case */
4125 else if (p[len] == '#')
4126 {
4127 ic = FALSE;
4128 ++len;
4129 }
4130 /* nothing appened: use 'ignorecase' */
4131 else
4132 ic = p_ic;
4133
4134 /*
4135 * Get the second variable.
4136 */
4137 *arg = skipwhite(p + len);
4138 if (eval5(arg, &var2, evaluate) == FAIL)
4139 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004140 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 return FAIL;
4142 }
4143
4144 if (evaluate)
4145 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004146 if (type_is && rettv->v_type != var2.v_type)
4147 {
4148 /* For "is" a different type always means FALSE, for "notis"
4149 * it means TRUE. */
4150 n1 = (type == TYPE_NEQUAL);
4151 }
4152 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4153 {
4154 if (type_is)
4155 {
4156 n1 = (rettv->v_type == var2.v_type
4157 && rettv->vval.v_list == var2.vval.v_list);
4158 if (type == TYPE_NEQUAL)
4159 n1 = !n1;
4160 }
4161 else if (rettv->v_type != var2.v_type
4162 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4163 {
4164 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004165 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004166 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004167 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004168 clear_tv(rettv);
4169 clear_tv(&var2);
4170 return FAIL;
4171 }
4172 else
4173 {
4174 /* Compare two Lists for being equal or unequal. */
4175 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4176 if (type == TYPE_NEQUAL)
4177 n1 = !n1;
4178 }
4179 }
4180
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004181 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4182 {
4183 if (type_is)
4184 {
4185 n1 = (rettv->v_type == var2.v_type
4186 && rettv->vval.v_dict == var2.vval.v_dict);
4187 if (type == TYPE_NEQUAL)
4188 n1 = !n1;
4189 }
4190 else if (rettv->v_type != var2.v_type
4191 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4192 {
4193 if (rettv->v_type != var2.v_type)
4194 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4195 else
4196 EMSG(_("E736: Invalid operation for Dictionary"));
4197 clear_tv(rettv);
4198 clear_tv(&var2);
4199 return FAIL;
4200 }
4201 else
4202 {
4203 /* Compare two Dictionaries for being equal or unequal. */
4204 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4205 if (type == TYPE_NEQUAL)
4206 n1 = !n1;
4207 }
4208 }
4209
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004210 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4211 {
4212 if (rettv->v_type != var2.v_type
4213 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4214 {
4215 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004216 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004217 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004218 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004219 clear_tv(rettv);
4220 clear_tv(&var2);
4221 return FAIL;
4222 }
4223 else
4224 {
4225 /* Compare two Funcrefs for being equal or unequal. */
4226 if (rettv->vval.v_string == NULL
4227 || var2.vval.v_string == NULL)
4228 n1 = FALSE;
4229 else
4230 n1 = STRCMP(rettv->vval.v_string,
4231 var2.vval.v_string) == 0;
4232 if (type == TYPE_NEQUAL)
4233 n1 = !n1;
4234 }
4235 }
4236
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 /*
4238 * If one of the two variables is a number, compare as a number.
4239 * When using "=~" or "!~", always compare as string.
4240 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004241 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4243 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004244 n1 = get_tv_number(rettv);
4245 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 switch (type)
4247 {
4248 case TYPE_EQUAL: n1 = (n1 == n2); break;
4249 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4250 case TYPE_GREATER: n1 = (n1 > n2); break;
4251 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4252 case TYPE_SMALLER: n1 = (n1 < n2); break;
4253 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4254 case TYPE_UNKNOWN:
4255 case TYPE_MATCH:
4256 case TYPE_NOMATCH: break; /* avoid gcc warning */
4257 }
4258 }
4259 else
4260 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004261 s1 = get_tv_string_buf(rettv, buf1);
4262 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4264 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4265 else
4266 i = 0;
4267 n1 = FALSE;
4268 switch (type)
4269 {
4270 case TYPE_EQUAL: n1 = (i == 0); break;
4271 case TYPE_NEQUAL: n1 = (i != 0); break;
4272 case TYPE_GREATER: n1 = (i > 0); break;
4273 case TYPE_GEQUAL: n1 = (i >= 0); break;
4274 case TYPE_SMALLER: n1 = (i < 0); break;
4275 case TYPE_SEQUAL: n1 = (i <= 0); break;
4276
4277 case TYPE_MATCH:
4278 case TYPE_NOMATCH:
4279 /* avoid 'l' flag in 'cpoptions' */
4280 save_cpo = p_cpo;
4281 p_cpo = (char_u *)"";
4282 regmatch.regprog = vim_regcomp(s2,
4283 RE_MAGIC + RE_STRING);
4284 regmatch.rm_ic = ic;
4285 if (regmatch.regprog != NULL)
4286 {
4287 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4288 vim_free(regmatch.regprog);
4289 if (type == TYPE_NOMATCH)
4290 n1 = !n1;
4291 }
4292 p_cpo = save_cpo;
4293 break;
4294
4295 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4296 }
4297 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004298 clear_tv(rettv);
4299 clear_tv(&var2);
4300 rettv->v_type = VAR_NUMBER;
4301 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 }
4303 }
4304
4305 return OK;
4306}
4307
4308/*
4309 * Handle fourth level expression:
4310 * + number addition
4311 * - number subtraction
4312 * . string concatenation
4313 *
4314 * "arg" must point to the first non-white of the expression.
4315 * "arg" is advanced to the next non-white after the recognized expression.
4316 *
4317 * Return OK or FAIL.
4318 */
4319 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004320eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004322 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 int evaluate;
4324{
Bram Moolenaar33570922005-01-25 22:26:29 +00004325 typval_T var2;
4326 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 int op;
4328 long n1, n2;
4329 char_u *s1, *s2;
4330 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4331 char_u *p;
4332
4333 /*
4334 * Get the first variable.
4335 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004336 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 return FAIL;
4338
4339 /*
4340 * Repeat computing, until no '+', '-' or '.' is following.
4341 */
4342 for (;;)
4343 {
4344 op = **arg;
4345 if (op != '+' && op != '-' && op != '.')
4346 break;
4347
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004348 if (op != '+' || rettv->v_type != VAR_LIST)
4349 {
4350 /* For "list + ...", an illegal use of the first operand as
4351 * a number cannot be determined before evaluating the 2nd
4352 * operand: if this is also a list, all is ok.
4353 * For "something . ...", "something - ..." or "non-list + ...",
4354 * we know that the first operand needs to be a string or number
4355 * without evaluating the 2nd operand. So check before to avoid
4356 * side effects after an error. */
4357 if (evaluate && get_tv_string_chk(rettv) == NULL)
4358 {
4359 clear_tv(rettv);
4360 return FAIL;
4361 }
4362 }
4363
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 /*
4365 * Get the second variable.
4366 */
4367 *arg = skipwhite(*arg + 1);
4368 if (eval6(arg, &var2, evaluate) == FAIL)
4369 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004370 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 return FAIL;
4372 }
4373
4374 if (evaluate)
4375 {
4376 /*
4377 * Compute the result.
4378 */
4379 if (op == '.')
4380 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004381 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4382 s2 = get_tv_string_buf_chk(&var2, buf2);
4383 if (s2 == NULL) /* type error ? */
4384 {
4385 clear_tv(rettv);
4386 clear_tv(&var2);
4387 return FAIL;
4388 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004389 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004390 clear_tv(rettv);
4391 rettv->v_type = VAR_STRING;
4392 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004394 else if (op == '+' && rettv->v_type == VAR_LIST
4395 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004396 {
4397 /* concatenate Lists */
4398 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4399 &var3) == FAIL)
4400 {
4401 clear_tv(rettv);
4402 clear_tv(&var2);
4403 return FAIL;
4404 }
4405 clear_tv(rettv);
4406 *rettv = var3;
4407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 else
4409 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004410 int error = FALSE;
4411
4412 n1 = get_tv_number_chk(rettv, &error);
4413 if (error)
4414 {
4415 /* This can only happen for "list + non-list".
4416 * For "non-list + ..." or "something - ...", we returned
4417 * before evaluating the 2nd operand. */
4418 clear_tv(rettv);
4419 return FAIL;
4420 }
4421 n2 = get_tv_number_chk(&var2, &error);
4422 if (error)
4423 {
4424 clear_tv(rettv);
4425 clear_tv(&var2);
4426 return FAIL;
4427 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004428 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 if (op == '+')
4430 n1 = n1 + n2;
4431 else
4432 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004433 rettv->v_type = VAR_NUMBER;
4434 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004436 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 }
4438 }
4439 return OK;
4440}
4441
4442/*
4443 * Handle fifth level expression:
4444 * * number multiplication
4445 * / number division
4446 * % number modulo
4447 *
4448 * "arg" must point to the first non-white of the expression.
4449 * "arg" is advanced to the next non-white after the recognized expression.
4450 *
4451 * Return OK or FAIL.
4452 */
4453 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004454eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004456 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 int evaluate;
4458{
Bram Moolenaar33570922005-01-25 22:26:29 +00004459 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 int op;
4461 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004462 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463
4464 /*
4465 * Get the first variable.
4466 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004467 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 return FAIL;
4469
4470 /*
4471 * Repeat computing, until no '*', '/' or '%' is following.
4472 */
4473 for (;;)
4474 {
4475 op = **arg;
4476 if (op != '*' && op != '/' && op != '%')
4477 break;
4478
4479 if (evaluate)
4480 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004481 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004482 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004483 if (error)
4484 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 }
4486 else
4487 n1 = 0;
4488
4489 /*
4490 * Get the second variable.
4491 */
4492 *arg = skipwhite(*arg + 1);
4493 if (eval7(arg, &var2, evaluate) == FAIL)
4494 return FAIL;
4495
4496 if (evaluate)
4497 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004498 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004499 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004500 if (error)
4501 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502
4503 /*
4504 * Compute the result.
4505 */
4506 if (op == '*')
4507 n1 = n1 * n2;
4508 else if (op == '/')
4509 {
4510 if (n2 == 0) /* give an error message? */
4511 n1 = 0x7fffffffL;
4512 else
4513 n1 = n1 / n2;
4514 }
4515 else
4516 {
4517 if (n2 == 0) /* give an error message? */
4518 n1 = 0;
4519 else
4520 n1 = n1 % n2;
4521 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004522 rettv->v_type = VAR_NUMBER;
4523 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 }
4525 }
4526
4527 return OK;
4528}
4529
4530/*
4531 * Handle sixth level expression:
4532 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004533 * "string" string constant
4534 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 * &option-name option value
4536 * @r register contents
4537 * identifier variable value
4538 * function() function call
4539 * $VAR environment variable
4540 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004541 * [expr, expr] List
4542 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 *
4544 * Also handle:
4545 * ! in front logical NOT
4546 * - in front unary minus
4547 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004548 * trailing [] subscript in String or List
4549 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 *
4551 * "arg" must point to the first non-white of the expression.
4552 * "arg" is advanced to the next non-white after the recognized expression.
4553 *
4554 * Return OK or FAIL.
4555 */
4556 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004557eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004559 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 int evaluate;
4561{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 long n;
4563 int len;
4564 char_u *s;
4565 int val;
4566 char_u *start_leader, *end_leader;
4567 int ret = OK;
4568 char_u *alias;
4569
4570 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004571 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004572 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004574 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575
4576 /*
4577 * Skip '!' and '-' characters. They are handled later.
4578 */
4579 start_leader = *arg;
4580 while (**arg == '!' || **arg == '-' || **arg == '+')
4581 *arg = skipwhite(*arg + 1);
4582 end_leader = *arg;
4583
4584 switch (**arg)
4585 {
4586 /*
4587 * Number constant.
4588 */
4589 case '0':
4590 case '1':
4591 case '2':
4592 case '3':
4593 case '4':
4594 case '5':
4595 case '6':
4596 case '7':
4597 case '8':
4598 case '9':
4599 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4600 *arg += len;
4601 if (evaluate)
4602 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004603 rettv->v_type = VAR_NUMBER;
4604 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 }
4606 break;
4607
4608 /*
4609 * String constant: "string".
4610 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004611 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 break;
4613
4614 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004615 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004617 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004618 break;
4619
4620 /*
4621 * List: [expr, expr]
4622 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004623 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 break;
4625
4626 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004627 * Dictionary: {key: val, key: val}
4628 */
4629 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4630 break;
4631
4632 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004633 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004635 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 break;
4637
4638 /*
4639 * Environment variable: $VAR.
4640 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004641 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 break;
4643
4644 /*
4645 * Register contents: @r.
4646 */
4647 case '@': ++*arg;
4648 if (evaluate)
4649 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004650 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004651 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 }
4653 if (**arg != NUL)
4654 ++*arg;
4655 break;
4656
4657 /*
4658 * nested expression: (expression).
4659 */
4660 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004661 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 if (**arg == ')')
4663 ++*arg;
4664 else if (ret == OK)
4665 {
4666 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004667 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 ret = FAIL;
4669 }
4670 break;
4671
Bram Moolenaar8c711452005-01-14 21:53:12 +00004672 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 break;
4674 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004675
4676 if (ret == NOTDONE)
4677 {
4678 /*
4679 * Must be a variable or function name.
4680 * Can also be a curly-braces kind of name: {expr}.
4681 */
4682 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004683 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004684 if (alias != NULL)
4685 s = alias;
4686
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004687 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004688 ret = FAIL;
4689 else
4690 {
4691 if (**arg == '(') /* recursive! */
4692 {
4693 /* If "s" is the name of a variable of type VAR_FUNC
4694 * use its contents. */
4695 s = deref_func_name(s, &len);
4696
4697 /* Invoke the function. */
4698 ret = get_func_tv(s, len, rettv, arg,
4699 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004700 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004701 /* Stop the expression evaluation when immediately
4702 * aborting on error, or when an interrupt occurred or
4703 * an exception was thrown but not caught. */
4704 if (aborting())
4705 {
4706 if (ret == OK)
4707 clear_tv(rettv);
4708 ret = FAIL;
4709 }
4710 }
4711 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004712 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004713 else
4714 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004715 }
4716
4717 if (alias != NULL)
4718 vim_free(alias);
4719 }
4720
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 *arg = skipwhite(*arg);
4722
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004723 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4724 * expr(expr). */
4725 if (ret == OK)
4726 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727
4728 /*
4729 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4730 */
4731 if (ret == OK && evaluate && end_leader > start_leader)
4732 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004733 int error = FALSE;
4734
4735 val = get_tv_number_chk(rettv, &error);
4736 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004738 clear_tv(rettv);
4739 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004741 else
4742 {
4743 while (end_leader > start_leader)
4744 {
4745 --end_leader;
4746 if (*end_leader == '!')
4747 val = !val;
4748 else if (*end_leader == '-')
4749 val = -val;
4750 }
4751 clear_tv(rettv);
4752 rettv->v_type = VAR_NUMBER;
4753 rettv->vval.v_number = val;
4754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 }
4756
4757 return ret;
4758}
4759
4760/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004761 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4762 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004763 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4764 */
4765 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004766eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004767 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004768 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004769 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004770 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004771{
4772 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004773 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004774 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004775 long len = -1;
4776 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004777 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004778 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004779
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004780 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004781 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004782 if (verbose)
4783 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004784 return FAIL;
4785 }
4786
Bram Moolenaar8c711452005-01-14 21:53:12 +00004787 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004788 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004789 /*
4790 * dict.name
4791 */
4792 key = *arg + 1;
4793 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4794 ;
4795 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004796 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004797 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004798 }
4799 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004800 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004801 /*
4802 * something[idx]
4803 *
4804 * Get the (first) variable from inside the [].
4805 */
4806 *arg = skipwhite(*arg + 1);
4807 if (**arg == ':')
4808 empty1 = TRUE;
4809 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4810 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004811 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4812 {
4813 /* not a number or string */
4814 clear_tv(&var1);
4815 return FAIL;
4816 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004817
4818 /*
4819 * Get the second variable from inside the [:].
4820 */
4821 if (**arg == ':')
4822 {
4823 range = TRUE;
4824 *arg = skipwhite(*arg + 1);
4825 if (**arg == ']')
4826 empty2 = TRUE;
4827 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4828 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004829 if (!empty1)
4830 clear_tv(&var1);
4831 return FAIL;
4832 }
4833 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4834 {
4835 /* not a number or string */
4836 if (!empty1)
4837 clear_tv(&var1);
4838 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004839 return FAIL;
4840 }
4841 }
4842
4843 /* Check for the ']'. */
4844 if (**arg != ']')
4845 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004846 if (verbose)
4847 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004848 clear_tv(&var1);
4849 if (range)
4850 clear_tv(&var2);
4851 return FAIL;
4852 }
4853 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004854 }
4855
4856 if (evaluate)
4857 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004858 n1 = 0;
4859 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004860 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004861 n1 = get_tv_number(&var1);
4862 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004863 }
4864 if (range)
4865 {
4866 if (empty2)
4867 n2 = -1;
4868 else
4869 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004870 n2 = get_tv_number(&var2);
4871 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004872 }
4873 }
4874
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004875 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004876 {
4877 case VAR_NUMBER:
4878 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004879 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004880 len = (long)STRLEN(s);
4881 if (range)
4882 {
4883 /* The resulting variable is a substring. If the indexes
4884 * are out of range the result is empty. */
4885 if (n1 < 0)
4886 {
4887 n1 = len + n1;
4888 if (n1 < 0)
4889 n1 = 0;
4890 }
4891 if (n2 < 0)
4892 n2 = len + n2;
4893 else if (n2 >= len)
4894 n2 = len;
4895 if (n1 >= len || n2 < 0 || n1 > n2)
4896 s = NULL;
4897 else
4898 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4899 }
4900 else
4901 {
4902 /* The resulting variable is a string of a single
4903 * character. If the index is too big or negative the
4904 * result is empty. */
4905 if (n1 >= len || n1 < 0)
4906 s = NULL;
4907 else
4908 s = vim_strnsave(s + n1, 1);
4909 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004910 clear_tv(rettv);
4911 rettv->v_type = VAR_STRING;
4912 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004913 break;
4914
4915 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004916 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004917 if (n1 < 0)
4918 n1 = len + n1;
4919 if (!empty1 && (n1 < 0 || n1 >= len))
4920 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004921 /* For a range we allow invalid values and return an empty
4922 * list. A list index out of range is an error. */
4923 if (!range)
4924 {
4925 if (verbose)
4926 EMSGN(_(e_listidx), n1);
4927 return FAIL;
4928 }
4929 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004930 }
4931 if (range)
4932 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004933 list_T *l;
4934 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004935
4936 if (n2 < 0)
4937 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004938 else if (n2 >= len)
4939 n2 = len - 1;
4940 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004941 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004942 l = list_alloc();
4943 if (l == NULL)
4944 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004945 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004946 n1 <= n2; ++n1)
4947 {
4948 if (list_append_tv(l, &item->li_tv) == FAIL)
4949 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00004950 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004951 return FAIL;
4952 }
4953 item = item->li_next;
4954 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004955 clear_tv(rettv);
4956 rettv->v_type = VAR_LIST;
4957 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004958 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004959 }
4960 else
4961 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004962 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004963 clear_tv(rettv);
4964 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004965 }
4966 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004967
4968 case VAR_DICT:
4969 if (range)
4970 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004971 if (verbose)
4972 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004973 if (len == -1)
4974 clear_tv(&var1);
4975 return FAIL;
4976 }
4977 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004978 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004979
4980 if (len == -1)
4981 {
4982 key = get_tv_string(&var1);
4983 if (*key == NUL)
4984 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004985 if (verbose)
4986 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004987 clear_tv(&var1);
4988 return FAIL;
4989 }
4990 }
4991
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004992 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004993
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004994 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004995 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004996 if (len == -1)
4997 clear_tv(&var1);
4998 if (item == NULL)
4999 return FAIL;
5000
5001 copy_tv(&item->di_tv, &var1);
5002 clear_tv(rettv);
5003 *rettv = var1;
5004 }
5005 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005006 }
5007 }
5008
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005009 return OK;
5010}
5011
5012/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 * Get an option value.
5014 * "arg" points to the '&' or '+' before the option name.
5015 * "arg" is advanced to character after the option name.
5016 * Return OK or FAIL.
5017 */
5018 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005019get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005021 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 int evaluate;
5023{
5024 char_u *option_end;
5025 long numval;
5026 char_u *stringval;
5027 int opt_type;
5028 int c;
5029 int working = (**arg == '+'); /* has("+option") */
5030 int ret = OK;
5031 int opt_flags;
5032
5033 /*
5034 * Isolate the option name and find its value.
5035 */
5036 option_end = find_option_end(arg, &opt_flags);
5037 if (option_end == NULL)
5038 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005039 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 EMSG2(_("E112: Option name missing: %s"), *arg);
5041 return FAIL;
5042 }
5043
5044 if (!evaluate)
5045 {
5046 *arg = option_end;
5047 return OK;
5048 }
5049
5050 c = *option_end;
5051 *option_end = NUL;
5052 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005053 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054
5055 if (opt_type == -3) /* invalid name */
5056 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005057 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 EMSG2(_("E113: Unknown option: %s"), *arg);
5059 ret = FAIL;
5060 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005061 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 {
5063 if (opt_type == -2) /* hidden string option */
5064 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005065 rettv->v_type = VAR_STRING;
5066 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 }
5068 else if (opt_type == -1) /* hidden number option */
5069 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005070 rettv->v_type = VAR_NUMBER;
5071 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 }
5073 else if (opt_type == 1) /* number option */
5074 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005075 rettv->v_type = VAR_NUMBER;
5076 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 }
5078 else /* string option */
5079 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005080 rettv->v_type = VAR_STRING;
5081 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 }
5083 }
5084 else if (working && (opt_type == -2 || opt_type == -1))
5085 ret = FAIL;
5086
5087 *option_end = c; /* put back for error messages */
5088 *arg = option_end;
5089
5090 return ret;
5091}
5092
5093/*
5094 * Allocate a variable for a string constant.
5095 * Return OK or FAIL.
5096 */
5097 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005098get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005100 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 int evaluate;
5102{
5103 char_u *p;
5104 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 int extra = 0;
5106
5107 /*
5108 * Find the end of the string, skipping backslashed characters.
5109 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005110 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 {
5112 if (*p == '\\' && p[1] != NUL)
5113 {
5114 ++p;
5115 /* A "\<x>" form occupies at least 4 characters, and produces up
5116 * to 6 characters: reserve space for 2 extra */
5117 if (*p == '<')
5118 extra += 2;
5119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 }
5121
5122 if (*p != '"')
5123 {
5124 EMSG2(_("E114: Missing quote: %s"), *arg);
5125 return FAIL;
5126 }
5127
5128 /* If only parsing, set *arg and return here */
5129 if (!evaluate)
5130 {
5131 *arg = p + 1;
5132 return OK;
5133 }
5134
5135 /*
5136 * Copy the string into allocated memory, handling backslashed
5137 * characters.
5138 */
5139 name = alloc((unsigned)(p - *arg + extra));
5140 if (name == NULL)
5141 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005142 rettv->v_type = VAR_STRING;
5143 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144
Bram Moolenaar8c711452005-01-14 21:53:12 +00005145 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 {
5147 if (*p == '\\')
5148 {
5149 switch (*++p)
5150 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005151 case 'b': *name++ = BS; ++p; break;
5152 case 'e': *name++ = ESC; ++p; break;
5153 case 'f': *name++ = FF; ++p; break;
5154 case 'n': *name++ = NL; ++p; break;
5155 case 'r': *name++ = CAR; ++p; break;
5156 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157
5158 case 'X': /* hex: "\x1", "\x12" */
5159 case 'x':
5160 case 'u': /* Unicode: "\u0023" */
5161 case 'U':
5162 if (vim_isxdigit(p[1]))
5163 {
5164 int n, nr;
5165 int c = toupper(*p);
5166
5167 if (c == 'X')
5168 n = 2;
5169 else
5170 n = 4;
5171 nr = 0;
5172 while (--n >= 0 && vim_isxdigit(p[1]))
5173 {
5174 ++p;
5175 nr = (nr << 4) + hex2nr(*p);
5176 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005177 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178#ifdef FEAT_MBYTE
5179 /* For "\u" store the number according to
5180 * 'encoding'. */
5181 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005182 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 else
5184#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005185 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 break;
5188
5189 /* octal: "\1", "\12", "\123" */
5190 case '0':
5191 case '1':
5192 case '2':
5193 case '3':
5194 case '4':
5195 case '5':
5196 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005197 case '7': *name = *p++ - '0';
5198 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005200 *name = (*name << 3) + *p++ - '0';
5201 if (*p >= '0' && *p <= '7')
5202 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005204 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 break;
5206
5207 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005208 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 if (extra != 0)
5210 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005211 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 break;
5213 }
5214 /* FALLTHROUGH */
5215
Bram Moolenaar8c711452005-01-14 21:53:12 +00005216 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 break;
5218 }
5219 }
5220 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005221 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005224 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 *arg = p + 1;
5226
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 return OK;
5228}
5229
5230/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005231 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 * Return OK or FAIL.
5233 */
5234 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005235get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005237 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 int evaluate;
5239{
5240 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005241 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005242 int reduce = 0;
5243
5244 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005245 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005246 */
5247 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5248 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005249 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005250 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005251 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005252 break;
5253 ++reduce;
5254 ++p;
5255 }
5256 }
5257
Bram Moolenaar8c711452005-01-14 21:53:12 +00005258 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005259 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005260 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005261 return FAIL;
5262 }
5263
Bram Moolenaar8c711452005-01-14 21:53:12 +00005264 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005265 if (!evaluate)
5266 {
5267 *arg = p + 1;
5268 return OK;
5269 }
5270
5271 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005272 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005273 */
5274 str = alloc((unsigned)((p - *arg) - reduce));
5275 if (str == NULL)
5276 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005277 rettv->v_type = VAR_STRING;
5278 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005279
Bram Moolenaar8c711452005-01-14 21:53:12 +00005280 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005281 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005282 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005283 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005284 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005285 break;
5286 ++p;
5287 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005288 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005289 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005290 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005291 *arg = p + 1;
5292
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005293 return OK;
5294}
5295
5296/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005297 * Allocate a variable for a List and fill it from "*arg".
5298 * Return OK or FAIL.
5299 */
5300 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005301get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005302 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005303 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005304 int evaluate;
5305{
Bram Moolenaar33570922005-01-25 22:26:29 +00005306 list_T *l = NULL;
5307 typval_T tv;
5308 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005309
5310 if (evaluate)
5311 {
5312 l = list_alloc();
5313 if (l == NULL)
5314 return FAIL;
5315 }
5316
5317 *arg = skipwhite(*arg + 1);
5318 while (**arg != ']' && **arg != NUL)
5319 {
5320 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5321 goto failret;
5322 if (evaluate)
5323 {
5324 item = listitem_alloc();
5325 if (item != NULL)
5326 {
5327 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005328 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005329 list_append(l, item);
5330 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005331 else
5332 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005333 }
5334
5335 if (**arg == ']')
5336 break;
5337 if (**arg != ',')
5338 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005339 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005340 goto failret;
5341 }
5342 *arg = skipwhite(*arg + 1);
5343 }
5344
5345 if (**arg != ']')
5346 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005347 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005348failret:
5349 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005350 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005351 return FAIL;
5352 }
5353
5354 *arg = skipwhite(*arg + 1);
5355 if (evaluate)
5356 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005357 rettv->v_type = VAR_LIST;
5358 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005359 ++l->lv_refcount;
5360 }
5361
5362 return OK;
5363}
5364
5365/*
5366 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005367 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005368 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005369 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005370list_alloc()
5371{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005372 list_T *l;
5373
5374 l = (list_T *)alloc_clear(sizeof(list_T));
5375 if (l != NULL)
5376 {
5377 /* Prepend the list to the list of lists for garbage collection. */
5378 if (first_list != NULL)
5379 first_list->lv_used_prev = l;
5380 l->lv_used_prev = NULL;
5381 l->lv_used_next = first_list;
5382 first_list = l;
5383 }
5384 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005385}
5386
5387/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005388 * Allocate an empty list for a return value.
5389 * Returns OK or FAIL.
5390 */
5391 static int
5392rettv_list_alloc(rettv)
5393 typval_T *rettv;
5394{
5395 list_T *l = list_alloc();
5396
5397 if (l == NULL)
5398 return FAIL;
5399
5400 rettv->vval.v_list = l;
5401 rettv->v_type = VAR_LIST;
5402 ++l->lv_refcount;
5403 return OK;
5404}
5405
5406/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005407 * Unreference a list: decrement the reference count and free it when it
5408 * becomes zero.
5409 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005410 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005411list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005412 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005413{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005414 if (l != NULL && --l->lv_refcount <= 0)
5415 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005416}
5417
5418/*
5419 * Free a list, including all items it points to.
5420 * Ignores the reference count.
5421 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005422 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005423list_free(l, recurse)
5424 list_T *l;
5425 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005426{
Bram Moolenaar33570922005-01-25 22:26:29 +00005427 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005428
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005429 /* Remove the list from the list of lists for garbage collection. */
5430 if (l->lv_used_prev == NULL)
5431 first_list = l->lv_used_next;
5432 else
5433 l->lv_used_prev->lv_used_next = l->lv_used_next;
5434 if (l->lv_used_next != NULL)
5435 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5436
Bram Moolenaard9fba312005-06-26 22:34:35 +00005437 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005438 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005439 /* Remove the item before deleting it. */
5440 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005441 if (recurse || (item->li_tv.v_type != VAR_LIST
5442 && item->li_tv.v_type != VAR_DICT))
5443 clear_tv(&item->li_tv);
5444 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005445 }
5446 vim_free(l);
5447}
5448
5449/*
5450 * Allocate a list item.
5451 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005452 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005453listitem_alloc()
5454{
Bram Moolenaar33570922005-01-25 22:26:29 +00005455 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005456}
5457
5458/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005459 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005460 */
5461 static void
5462listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005463 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005464{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005465 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005466 vim_free(item);
5467}
5468
5469/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005470 * Remove a list item from a List and free it. Also clears the value.
5471 */
5472 static void
5473listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005474 list_T *l;
5475 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005476{
5477 list_remove(l, item, item);
5478 listitem_free(item);
5479}
5480
5481/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005482 * Get the number of items in a list.
5483 */
5484 static long
5485list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005486 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005487{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005488 if (l == NULL)
5489 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005490 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005491}
5492
5493/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005494 * Return TRUE when two lists have exactly the same values.
5495 */
5496 static int
5497list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005498 list_T *l1;
5499 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005500 int ic; /* ignore case for strings */
5501{
Bram Moolenaar33570922005-01-25 22:26:29 +00005502 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005503
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005504 if (l1 == l2)
5505 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005506 if (list_len(l1) != list_len(l2))
5507 return FALSE;
5508
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005509 for (item1 = l1->lv_first, item2 = l2->lv_first;
5510 item1 != NULL && item2 != NULL;
5511 item1 = item1->li_next, item2 = item2->li_next)
5512 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5513 return FALSE;
5514 return item1 == NULL && item2 == NULL;
5515}
5516
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005517#if defined(FEAT_PYTHON) || defined(PROTO)
5518/*
5519 * Return the dictitem that an entry in a hashtable points to.
5520 */
5521 dictitem_T *
5522dict_lookup(hi)
5523 hashitem_T *hi;
5524{
5525 return HI2DI(hi);
5526}
5527#endif
5528
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005529/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005530 * Return TRUE when two dictionaries have exactly the same key/values.
5531 */
5532 static int
5533dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005534 dict_T *d1;
5535 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005536 int ic; /* ignore case for strings */
5537{
Bram Moolenaar33570922005-01-25 22:26:29 +00005538 hashitem_T *hi;
5539 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005540 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005541
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005542 if (d1 == d2)
5543 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005544 if (dict_len(d1) != dict_len(d2))
5545 return FALSE;
5546
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005547 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00005548 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005549 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005550 if (!HASHITEM_EMPTY(hi))
5551 {
5552 item2 = dict_find(d2, hi->hi_key, -1);
5553 if (item2 == NULL)
5554 return FALSE;
5555 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5556 return FALSE;
5557 --todo;
5558 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005559 }
5560 return TRUE;
5561}
5562
5563/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005564 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005565 * Compares the items just like "==" would compare them, but strings and
5566 * numbers are different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005567 */
5568 static int
5569tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005570 typval_T *tv1;
5571 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005572 int ic; /* ignore case */
5573{
5574 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005575 char_u *s1, *s2;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005576 static int recursive = 0; /* cach recursive loops */
5577 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005578
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005579 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005580 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005581 /* Catch lists and dicts that have an endless loop by limiting
5582 * recursiveness to 1000. We guess they are equal then. */
5583 if (recursive >= 1000)
5584 return TRUE;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005585
5586 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005587 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005588 case VAR_LIST:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005589 ++recursive;
5590 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5591 --recursive;
5592 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005593
5594 case VAR_DICT:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005595 ++recursive;
5596 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5597 --recursive;
5598 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005599
5600 case VAR_FUNC:
5601 return (tv1->vval.v_string != NULL
5602 && tv2->vval.v_string != NULL
5603 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5604
5605 case VAR_NUMBER:
5606 return tv1->vval.v_number == tv2->vval.v_number;
5607
5608 case VAR_STRING:
5609 s1 = get_tv_string_buf(tv1, buf1);
5610 s2 = get_tv_string_buf(tv2, buf2);
5611 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005612 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005613
5614 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005615 return TRUE;
5616}
5617
5618/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005619 * Locate item with index "n" in list "l" and return it.
5620 * A negative index is counted from the end; -1 is the last item.
5621 * Returns NULL when "n" is out of range.
5622 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005623 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005624list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005625 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005626 long n;
5627{
Bram Moolenaar33570922005-01-25 22:26:29 +00005628 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005629 long idx;
5630
5631 if (l == NULL)
5632 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005633
5634 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005635 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005636 n = l->lv_len + n;
5637
5638 /* Check for index out of range. */
5639 if (n < 0 || n >= l->lv_len)
5640 return NULL;
5641
5642 /* When there is a cached index may start search from there. */
5643 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005644 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005645 if (n < l->lv_idx / 2)
5646 {
5647 /* closest to the start of the list */
5648 item = l->lv_first;
5649 idx = 0;
5650 }
5651 else if (n > (l->lv_idx + l->lv_len) / 2)
5652 {
5653 /* closest to the end of the list */
5654 item = l->lv_last;
5655 idx = l->lv_len - 1;
5656 }
5657 else
5658 {
5659 /* closest to the cached index */
5660 item = l->lv_idx_item;
5661 idx = l->lv_idx;
5662 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005663 }
5664 else
5665 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005666 if (n < l->lv_len / 2)
5667 {
5668 /* closest to the start of the list */
5669 item = l->lv_first;
5670 idx = 0;
5671 }
5672 else
5673 {
5674 /* closest to the end of the list */
5675 item = l->lv_last;
5676 idx = l->lv_len - 1;
5677 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005678 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005679
5680 while (n > idx)
5681 {
5682 /* search forward */
5683 item = item->li_next;
5684 ++idx;
5685 }
5686 while (n < idx)
5687 {
5688 /* search backward */
5689 item = item->li_prev;
5690 --idx;
5691 }
5692
5693 /* cache the used index */
5694 l->lv_idx = idx;
5695 l->lv_idx_item = item;
5696
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005697 return item;
5698}
5699
5700/*
Bram Moolenaara5525202006-03-02 22:52:09 +00005701 * Get list item "l[idx]" as a number.
5702 */
5703 static long
5704list_find_nr(l, idx, errorp)
5705 list_T *l;
5706 long idx;
5707 int *errorp; /* set to TRUE when something wrong */
5708{
5709 listitem_T *li;
5710
5711 li = list_find(l, idx);
5712 if (li == NULL)
5713 {
5714 if (errorp != NULL)
5715 *errorp = TRUE;
5716 return -1L;
5717 }
5718 return get_tv_number_chk(&li->li_tv, errorp);
5719}
5720
5721/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005722 * Locate "item" list "l" and return its index.
5723 * Returns -1 when "item" is not in the list.
5724 */
5725 static long
5726list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005727 list_T *l;
5728 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005729{
5730 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005731 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005732
5733 if (l == NULL)
5734 return -1;
5735 idx = 0;
5736 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5737 ++idx;
5738 if (li == NULL)
5739 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005740 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005741}
5742
5743/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005744 * Append item "item" to the end of list "l".
5745 */
5746 static void
5747list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005748 list_T *l;
5749 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005750{
5751 if (l->lv_last == NULL)
5752 {
5753 /* empty list */
5754 l->lv_first = item;
5755 l->lv_last = item;
5756 item->li_prev = NULL;
5757 }
5758 else
5759 {
5760 l->lv_last->li_next = item;
5761 item->li_prev = l->lv_last;
5762 l->lv_last = item;
5763 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005764 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005765 item->li_next = NULL;
5766}
5767
5768/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005769 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005770 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005771 */
5772 static int
5773list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005774 list_T *l;
5775 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005776{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005777 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005778
Bram Moolenaar05159a02005-02-26 23:04:13 +00005779 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005780 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005781 copy_tv(tv, &li->li_tv);
5782 list_append(l, li);
5783 return OK;
5784}
5785
5786/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005787 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005788 * Return FAIL when out of memory.
5789 */
5790 int
5791list_append_dict(list, dict)
5792 list_T *list;
5793 dict_T *dict;
5794{
5795 listitem_T *li = listitem_alloc();
5796
5797 if (li == NULL)
5798 return FAIL;
5799 li->li_tv.v_type = VAR_DICT;
5800 li->li_tv.v_lock = 0;
5801 li->li_tv.vval.v_dict = dict;
5802 list_append(list, li);
5803 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005804 return OK;
5805}
5806
5807/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005808 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00005809 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005810 * Returns FAIL when out of memory.
5811 */
5812 static int
Bram Moolenaar4463f292005-09-25 22:20:24 +00005813list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005814 list_T *l;
5815 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00005816 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005817{
5818 listitem_T *li = listitem_alloc();
5819
5820 if (li == NULL)
5821 return FAIL;
5822 list_append(l, li);
5823 li->li_tv.v_type = VAR_STRING;
5824 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005825 if (str == NULL)
5826 li->li_tv.vval.v_string = NULL;
5827 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005828 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005829 return FAIL;
5830 return OK;
5831}
5832
5833/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00005834 * Append "n" to list "l".
5835 * Returns FAIL when out of memory.
5836 */
5837 static int
5838list_append_number(l, n)
5839 list_T *l;
5840 varnumber_T n;
5841{
5842 listitem_T *li;
5843
5844 li = listitem_alloc();
5845 if (li == NULL)
5846 return FAIL;
5847 li->li_tv.v_type = VAR_NUMBER;
5848 li->li_tv.v_lock = 0;
5849 li->li_tv.vval.v_number = n;
5850 list_append(l, li);
5851 return OK;
5852}
5853
5854/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005855 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005856 * If "item" is NULL append at the end.
5857 * Return FAIL when out of memory.
5858 */
5859 static int
5860list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005861 list_T *l;
5862 typval_T *tv;
5863 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005864{
Bram Moolenaar33570922005-01-25 22:26:29 +00005865 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005866
5867 if (ni == NULL)
5868 return FAIL;
5869 copy_tv(tv, &ni->li_tv);
5870 if (item == NULL)
5871 /* Append new item at end of list. */
5872 list_append(l, ni);
5873 else
5874 {
5875 /* Insert new item before existing item. */
5876 ni->li_prev = item->li_prev;
5877 ni->li_next = item;
5878 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005879 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005880 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005881 ++l->lv_idx;
5882 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005883 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005884 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005885 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005886 l->lv_idx_item = NULL;
5887 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005888 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005889 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005890 }
5891 return OK;
5892}
5893
5894/*
5895 * Extend "l1" with "l2".
5896 * If "bef" is NULL append at the end, otherwise insert before this item.
5897 * Returns FAIL when out of memory.
5898 */
5899 static int
5900list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005901 list_T *l1;
5902 list_T *l2;
5903 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005904{
Bram Moolenaar33570922005-01-25 22:26:29 +00005905 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005906
5907 for (item = l2->lv_first; item != NULL; item = item->li_next)
5908 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5909 return FAIL;
5910 return OK;
5911}
5912
5913/*
5914 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5915 * Return FAIL when out of memory.
5916 */
5917 static int
5918list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005919 list_T *l1;
5920 list_T *l2;
5921 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005922{
Bram Moolenaar33570922005-01-25 22:26:29 +00005923 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005924
5925 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005926 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005927 if (l == NULL)
5928 return FAIL;
5929 tv->v_type = VAR_LIST;
5930 tv->vval.v_list = l;
5931
5932 /* append all items from the second list */
5933 return list_extend(l, l2, NULL);
5934}
5935
5936/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005937 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005938 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005939 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005940 * Returns NULL when out of memory.
5941 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005942 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005943list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005944 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005945 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005946 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005947{
Bram Moolenaar33570922005-01-25 22:26:29 +00005948 list_T *copy;
5949 listitem_T *item;
5950 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005951
5952 if (orig == NULL)
5953 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005954
5955 copy = list_alloc();
5956 if (copy != NULL)
5957 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005958 if (copyID != 0)
5959 {
5960 /* Do this before adding the items, because one of the items may
5961 * refer back to this list. */
5962 orig->lv_copyID = copyID;
5963 orig->lv_copylist = copy;
5964 }
5965 for (item = orig->lv_first; item != NULL && !got_int;
5966 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005967 {
5968 ni = listitem_alloc();
5969 if (ni == NULL)
5970 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005971 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005972 {
5973 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5974 {
5975 vim_free(ni);
5976 break;
5977 }
5978 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005979 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005980 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005981 list_append(copy, ni);
5982 }
5983 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005984 if (item != NULL)
5985 {
5986 list_unref(copy);
5987 copy = NULL;
5988 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005989 }
5990
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005991 return copy;
5992}
5993
5994/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005995 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005996 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005997 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005998 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005999list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006000 list_T *l;
6001 listitem_T *item;
6002 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006003{
Bram Moolenaar33570922005-01-25 22:26:29 +00006004 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006005
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006006 /* notify watchers */
6007 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006008 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006009 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006010 list_fix_watch(l, ip);
6011 if (ip == item2)
6012 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006013 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006014
6015 if (item2->li_next == NULL)
6016 l->lv_last = item->li_prev;
6017 else
6018 item2->li_next->li_prev = item->li_prev;
6019 if (item->li_prev == NULL)
6020 l->lv_first = item2->li_next;
6021 else
6022 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006023 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006024}
6025
6026/*
6027 * Return an allocated string with the string representation of a list.
6028 * May return NULL.
6029 */
6030 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006031list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006032 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006033 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006034{
6035 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006036
6037 if (tv->vval.v_list == NULL)
6038 return NULL;
6039 ga_init2(&ga, (int)sizeof(char), 80);
6040 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006041 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006042 {
6043 vim_free(ga.ga_data);
6044 return NULL;
6045 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006046 ga_append(&ga, ']');
6047 ga_append(&ga, NUL);
6048 return (char_u *)ga.ga_data;
6049}
6050
6051/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006052 * Join list "l" into a string in "*gap", using separator "sep".
6053 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006054 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006055 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006056 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006057list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006058 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006059 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006060 char_u *sep;
6061 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006062 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006063{
6064 int first = TRUE;
6065 char_u *tofree;
6066 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006067 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006068 char_u *s;
6069
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006070 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006071 {
6072 if (first)
6073 first = FALSE;
6074 else
6075 ga_concat(gap, sep);
6076
6077 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006078 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006079 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006080 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006081 if (s != NULL)
6082 ga_concat(gap, s);
6083 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006084 if (s == NULL)
6085 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006086 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006087 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006088}
6089
6090/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006091 * Garbage collection for lists and dictionaries.
6092 *
6093 * We use reference counts to be able to free most items right away when they
6094 * are no longer used. But for composite items it's possible that it becomes
6095 * unused while the reference count is > 0: When there is a recursive
6096 * reference. Example:
6097 * :let l = [1, 2, 3]
6098 * :let d = {9: l}
6099 * :let l[1] = d
6100 *
6101 * Since this is quite unusual we handle this with garbage collection: every
6102 * once in a while find out which lists and dicts are not referenced from any
6103 * variable.
6104 *
6105 * Here is a good reference text about garbage collection (refers to Python
6106 * but it applies to all reference-counting mechanisms):
6107 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006108 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006109
6110/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006111 * Do garbage collection for lists and dicts.
6112 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006113 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006114 int
6115garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006116{
6117 dict_T *dd;
6118 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006119 int copyID = ++current_copyID;
6120 buf_T *buf;
6121 win_T *wp;
6122 int i;
6123 funccall_T *fc;
6124 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006125#ifdef FEAT_WINDOWS
6126 tabpage_T *tp;
6127#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006128
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006129 /* Only do this once. */
6130 want_garbage_collect = FALSE;
6131 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006132 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006133
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006134 /*
6135 * 1. Go through all accessible variables and mark all lists and dicts
6136 * with copyID.
6137 */
6138 /* script-local variables */
6139 for (i = 1; i <= ga_scripts.ga_len; ++i)
6140 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6141
6142 /* buffer-local variables */
6143 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6144 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6145
6146 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006147 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006148 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6149
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006150#ifdef FEAT_WINDOWS
6151 /* tabpage-local variables */
6152 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6153 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6154#endif
6155
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006156 /* global variables */
6157 set_ref_in_ht(&globvarht, copyID);
6158
6159 /* function-local variables */
6160 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6161 {
6162 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6163 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6164 }
6165
6166 /*
6167 * 2. Go through the list of dicts and free items without the copyID.
6168 */
6169 for (dd = first_dict; dd != NULL; )
6170 if (dd->dv_copyID != copyID)
6171 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006172 /* Free the Dictionary and ordinary items it contains, but don't
6173 * recurse into Lists and Dictionaries, they will be in the list
6174 * of dicts or list of lists. */
6175 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006176 did_free = TRUE;
6177
6178 /* restart, next dict may also have been freed */
6179 dd = first_dict;
6180 }
6181 else
6182 dd = dd->dv_used_next;
6183
6184 /*
6185 * 3. Go through the list of lists and free items without the copyID.
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006186 * But don't free a list that has a watcher (used in a for loop), these
6187 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006188 */
6189 for (ll = first_list; ll != NULL; )
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006190 if (ll->lv_copyID != copyID && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006191 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006192 /* Free the List and ordinary items it contains, but don't recurse
6193 * into Lists and Dictionaries, they will be in the list of dicts
6194 * or list of lists. */
6195 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006196 did_free = TRUE;
6197
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006198 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006199 ll = first_list;
6200 }
6201 else
6202 ll = ll->lv_used_next;
6203
6204 return did_free;
6205}
6206
6207/*
6208 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6209 */
6210 static void
6211set_ref_in_ht(ht, copyID)
6212 hashtab_T *ht;
6213 int copyID;
6214{
6215 int todo;
6216 hashitem_T *hi;
6217
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006218 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006219 for (hi = ht->ht_array; todo > 0; ++hi)
6220 if (!HASHITEM_EMPTY(hi))
6221 {
6222 --todo;
6223 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6224 }
6225}
6226
6227/*
6228 * Mark all lists and dicts referenced through list "l" with "copyID".
6229 */
6230 static void
6231set_ref_in_list(l, copyID)
6232 list_T *l;
6233 int copyID;
6234{
6235 listitem_T *li;
6236
6237 for (li = l->lv_first; li != NULL; li = li->li_next)
6238 set_ref_in_item(&li->li_tv, copyID);
6239}
6240
6241/*
6242 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6243 */
6244 static void
6245set_ref_in_item(tv, copyID)
6246 typval_T *tv;
6247 int copyID;
6248{
6249 dict_T *dd;
6250 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006251
6252 switch (tv->v_type)
6253 {
6254 case VAR_DICT:
6255 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006256 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006257 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006258 /* Didn't see this dict yet. */
6259 dd->dv_copyID = copyID;
6260 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006261 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006262 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006263
6264 case VAR_LIST:
6265 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006266 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006267 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006268 /* Didn't see this list yet. */
6269 ll->lv_copyID = copyID;
6270 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006271 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006272 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006273 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006274 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006275}
6276
6277/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006278 * Allocate an empty header for a dictionary.
6279 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006280 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006281dict_alloc()
6282{
Bram Moolenaar33570922005-01-25 22:26:29 +00006283 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006284
Bram Moolenaar33570922005-01-25 22:26:29 +00006285 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006286 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006287 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006288 /* Add the list to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006289 if (first_dict != NULL)
6290 first_dict->dv_used_prev = d;
6291 d->dv_used_next = first_dict;
6292 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006293 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006294
Bram Moolenaar33570922005-01-25 22:26:29 +00006295 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006296 d->dv_lock = 0;
6297 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006298 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006299 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006300 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006301}
6302
6303/*
6304 * Unreference a Dictionary: decrement the reference count and free it when it
6305 * becomes zero.
6306 */
6307 static void
6308dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006309 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006310{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006311 if (d != NULL && --d->dv_refcount <= 0)
6312 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006313}
6314
6315/*
6316 * Free a Dictionary, including all items it contains.
6317 * Ignores the reference count.
6318 */
6319 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006320dict_free(d, recurse)
6321 dict_T *d;
6322 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006323{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006324 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006325 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006326 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006327
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006328 /* Remove the dict from the list of dicts for garbage collection. */
6329 if (d->dv_used_prev == NULL)
6330 first_dict = d->dv_used_next;
6331 else
6332 d->dv_used_prev->dv_used_next = d->dv_used_next;
6333 if (d->dv_used_next != NULL)
6334 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6335
6336 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006337 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006338 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006339 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006340 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006341 if (!HASHITEM_EMPTY(hi))
6342 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006343 /* Remove the item before deleting it, just in case there is
6344 * something recursive causing trouble. */
6345 di = HI2DI(hi);
6346 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00006347 if (recurse || (di->di_tv.v_type != VAR_LIST
6348 && di->di_tv.v_type != VAR_DICT))
6349 clear_tv(&di->di_tv);
6350 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006351 --todo;
6352 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006353 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006354 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006355 vim_free(d);
6356}
6357
6358/*
6359 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006360 * The "key" is copied to the new item.
6361 * Note that the value of the item "di_tv" still needs to be initialized!
6362 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006363 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006364 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006365dictitem_alloc(key)
6366 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006367{
Bram Moolenaar33570922005-01-25 22:26:29 +00006368 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006369
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006370 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006371 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006372 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006373 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006374 di->di_flags = 0;
6375 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006376 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006377}
6378
6379/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006380 * Make a copy of a Dictionary item.
6381 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006382 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006383dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006384 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006385{
Bram Moolenaar33570922005-01-25 22:26:29 +00006386 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006387
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006388 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
6389 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006390 if (di != NULL)
6391 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006392 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006393 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006394 copy_tv(&org->di_tv, &di->di_tv);
6395 }
6396 return di;
6397}
6398
6399/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006400 * Remove item "item" from Dictionary "dict" and free it.
6401 */
6402 static void
6403dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006404 dict_T *dict;
6405 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006406{
Bram Moolenaar33570922005-01-25 22:26:29 +00006407 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006408
Bram Moolenaar33570922005-01-25 22:26:29 +00006409 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006410 if (HASHITEM_EMPTY(hi))
6411 EMSG2(_(e_intern2), "dictitem_remove()");
6412 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006413 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006414 dictitem_free(item);
6415}
6416
6417/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006418 * Free a dict item. Also clears the value.
6419 */
6420 static void
6421dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006422 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006423{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006424 clear_tv(&item->di_tv);
6425 vim_free(item);
6426}
6427
6428/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006429 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6430 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006431 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006432 * Returns NULL when out of memory.
6433 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006434 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006435dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006436 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006437 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006438 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006439{
Bram Moolenaar33570922005-01-25 22:26:29 +00006440 dict_T *copy;
6441 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006442 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006443 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006444
6445 if (orig == NULL)
6446 return NULL;
6447
6448 copy = dict_alloc();
6449 if (copy != NULL)
6450 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006451 if (copyID != 0)
6452 {
6453 orig->dv_copyID = copyID;
6454 orig->dv_copydict = copy;
6455 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006456 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006457 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006458 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006459 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006460 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006461 --todo;
6462
6463 di = dictitem_alloc(hi->hi_key);
6464 if (di == NULL)
6465 break;
6466 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006467 {
6468 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6469 copyID) == FAIL)
6470 {
6471 vim_free(di);
6472 break;
6473 }
6474 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006475 else
6476 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6477 if (dict_add(copy, di) == FAIL)
6478 {
6479 dictitem_free(di);
6480 break;
6481 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006482 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006483 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006484
Bram Moolenaare9a41262005-01-15 22:18:47 +00006485 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006486 if (todo > 0)
6487 {
6488 dict_unref(copy);
6489 copy = NULL;
6490 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006491 }
6492
6493 return copy;
6494}
6495
6496/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006497 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006498 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006499 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006500 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006501dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006502 dict_T *d;
6503 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006504{
Bram Moolenaar33570922005-01-25 22:26:29 +00006505 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006506}
6507
Bram Moolenaar8c711452005-01-14 21:53:12 +00006508/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006509 * Add a number or string entry to dictionary "d".
6510 * When "str" is NULL use number "nr", otherwise use "str".
6511 * Returns FAIL when out of memory and when key already exists.
6512 */
6513 int
6514dict_add_nr_str(d, key, nr, str)
6515 dict_T *d;
6516 char *key;
6517 long nr;
6518 char_u *str;
6519{
6520 dictitem_T *item;
6521
6522 item = dictitem_alloc((char_u *)key);
6523 if (item == NULL)
6524 return FAIL;
6525 item->di_tv.v_lock = 0;
6526 if (str == NULL)
6527 {
6528 item->di_tv.v_type = VAR_NUMBER;
6529 item->di_tv.vval.v_number = nr;
6530 }
6531 else
6532 {
6533 item->di_tv.v_type = VAR_STRING;
6534 item->di_tv.vval.v_string = vim_strsave(str);
6535 }
6536 if (dict_add(d, item) == FAIL)
6537 {
6538 dictitem_free(item);
6539 return FAIL;
6540 }
6541 return OK;
6542}
6543
6544/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006545 * Get the number of items in a Dictionary.
6546 */
6547 static long
6548dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006549 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006550{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006551 if (d == NULL)
6552 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006553 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006554}
6555
6556/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006557 * Find item "key[len]" in Dictionary "d".
6558 * If "len" is negative use strlen(key).
6559 * Returns NULL when not found.
6560 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006561 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006562dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006563 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006564 char_u *key;
6565 int len;
6566{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006567#define AKEYLEN 200
6568 char_u buf[AKEYLEN];
6569 char_u *akey;
6570 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006571 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006572
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006573 if (len < 0)
6574 akey = key;
6575 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006576 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006577 tofree = akey = vim_strnsave(key, len);
6578 if (akey == NULL)
6579 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006580 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006581 else
6582 {
6583 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006584 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006585 akey = buf;
6586 }
6587
Bram Moolenaar33570922005-01-25 22:26:29 +00006588 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006589 vim_free(tofree);
6590 if (HASHITEM_EMPTY(hi))
6591 return NULL;
6592 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006593}
6594
6595/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006596 * Get a string item from a dictionary.
6597 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00006598 * Returns NULL if the entry doesn't exist or out of memory.
6599 */
6600 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006601get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00006602 dict_T *d;
6603 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006604 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006605{
6606 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006607 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006608
6609 di = dict_find(d, key, -1);
6610 if (di == NULL)
6611 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006612 s = get_tv_string(&di->di_tv);
6613 if (save && s != NULL)
6614 s = vim_strsave(s);
6615 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006616}
6617
6618/*
6619 * Get a number item from a dictionary.
6620 * Returns 0 if the entry doesn't exist or out of memory.
6621 */
6622 long
6623get_dict_number(d, key)
6624 dict_T *d;
6625 char_u *key;
6626{
6627 dictitem_T *di;
6628
6629 di = dict_find(d, key, -1);
6630 if (di == NULL)
6631 return 0;
6632 return get_tv_number(&di->di_tv);
6633}
6634
6635/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006636 * Return an allocated string with the string representation of a Dictionary.
6637 * May return NULL.
6638 */
6639 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006640dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006641 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006642 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006643{
6644 garray_T ga;
6645 int first = TRUE;
6646 char_u *tofree;
6647 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006648 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006649 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006650 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006651 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006652
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006653 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006654 return NULL;
6655 ga_init2(&ga, (int)sizeof(char), 80);
6656 ga_append(&ga, '{');
6657
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006658 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006659 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006660 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006661 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006662 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006663 --todo;
6664
6665 if (first)
6666 first = FALSE;
6667 else
6668 ga_concat(&ga, (char_u *)", ");
6669
6670 tofree = string_quote(hi->hi_key, FALSE);
6671 if (tofree != NULL)
6672 {
6673 ga_concat(&ga, tofree);
6674 vim_free(tofree);
6675 }
6676 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006677 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006678 if (s != NULL)
6679 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006680 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006681 if (s == NULL)
6682 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006683 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006684 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006685 if (todo > 0)
6686 {
6687 vim_free(ga.ga_data);
6688 return NULL;
6689 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006690
6691 ga_append(&ga, '}');
6692 ga_append(&ga, NUL);
6693 return (char_u *)ga.ga_data;
6694}
6695
6696/*
6697 * Allocate a variable for a Dictionary and fill it from "*arg".
6698 * Return OK or FAIL. Returns NOTDONE for {expr}.
6699 */
6700 static int
6701get_dict_tv(arg, rettv, evaluate)
6702 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006703 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006704 int evaluate;
6705{
Bram Moolenaar33570922005-01-25 22:26:29 +00006706 dict_T *d = NULL;
6707 typval_T tvkey;
6708 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00006709 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006710 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006711 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006712 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00006713
6714 /*
6715 * First check if it's not a curly-braces thing: {expr}.
6716 * Must do this without evaluating, otherwise a function may be called
6717 * twice. Unfortunately this means we need to call eval1() twice for the
6718 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006719 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006720 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006721 if (*start != '}')
6722 {
6723 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
6724 return FAIL;
6725 if (*start == '}')
6726 return NOTDONE;
6727 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006728
6729 if (evaluate)
6730 {
6731 d = dict_alloc();
6732 if (d == NULL)
6733 return FAIL;
6734 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006735 tvkey.v_type = VAR_UNKNOWN;
6736 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006737
6738 *arg = skipwhite(*arg + 1);
6739 while (**arg != '}' && **arg != NUL)
6740 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006741 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006742 goto failret;
6743 if (**arg != ':')
6744 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006745 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006746 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006747 goto failret;
6748 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00006749 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006750 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00006751 key = get_tv_string_buf_chk(&tvkey, buf);
6752 if (key == NULL || *key == NUL)
6753 {
6754 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6755 if (key != NULL)
6756 EMSG(_(e_emptykey));
6757 clear_tv(&tvkey);
6758 goto failret;
6759 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006760 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006761
6762 *arg = skipwhite(*arg + 1);
6763 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6764 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00006765 if (evaluate)
6766 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006767 goto failret;
6768 }
6769 if (evaluate)
6770 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006771 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006772 if (item != NULL)
6773 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00006774 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006775 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006776 clear_tv(&tv);
6777 goto failret;
6778 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006779 item = dictitem_alloc(key);
6780 clear_tv(&tvkey);
6781 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006782 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006783 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006784 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006785 if (dict_add(d, item) == FAIL)
6786 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006787 }
6788 }
6789
6790 if (**arg == '}')
6791 break;
6792 if (**arg != ',')
6793 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006794 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006795 goto failret;
6796 }
6797 *arg = skipwhite(*arg + 1);
6798 }
6799
6800 if (**arg != '}')
6801 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006802 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006803failret:
6804 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00006805 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006806 return FAIL;
6807 }
6808
6809 *arg = skipwhite(*arg + 1);
6810 if (evaluate)
6811 {
6812 rettv->v_type = VAR_DICT;
6813 rettv->vval.v_dict = d;
6814 ++d->dv_refcount;
6815 }
6816
6817 return OK;
6818}
6819
Bram Moolenaar8c711452005-01-14 21:53:12 +00006820/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006821 * Return a string with the string representation of a variable.
6822 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006823 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006824 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006825 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006826 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006827 */
6828 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006829echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006830 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006831 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006832 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006833 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006834{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006835 static int recurse = 0;
6836 char_u *r = NULL;
6837
Bram Moolenaar33570922005-01-25 22:26:29 +00006838 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006839 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006840 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006841 *tofree = NULL;
6842 return NULL;
6843 }
6844 ++recurse;
6845
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006846 switch (tv->v_type)
6847 {
6848 case VAR_FUNC:
6849 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006850 r = tv->vval.v_string;
6851 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006852
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006853 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006854 if (tv->vval.v_list == NULL)
6855 {
6856 *tofree = NULL;
6857 r = NULL;
6858 }
6859 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
6860 {
6861 *tofree = NULL;
6862 r = (char_u *)"[...]";
6863 }
6864 else
6865 {
6866 tv->vval.v_list->lv_copyID = copyID;
6867 *tofree = list2string(tv, copyID);
6868 r = *tofree;
6869 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006870 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006871
Bram Moolenaar8c711452005-01-14 21:53:12 +00006872 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006873 if (tv->vval.v_dict == NULL)
6874 {
6875 *tofree = NULL;
6876 r = NULL;
6877 }
6878 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
6879 {
6880 *tofree = NULL;
6881 r = (char_u *)"{...}";
6882 }
6883 else
6884 {
6885 tv->vval.v_dict->dv_copyID = copyID;
6886 *tofree = dict2string(tv, copyID);
6887 r = *tofree;
6888 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006889 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006890
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006891 case VAR_STRING:
6892 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006893 *tofree = NULL;
6894 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006895 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006896
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006897 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006898 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006899 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006900 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006901
6902 --recurse;
6903 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006904}
6905
6906/*
6907 * Return a string with the string representation of a variable.
6908 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6909 * "numbuf" is used for a number.
6910 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006911 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006912 */
6913 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006914tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006915 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006916 char_u **tofree;
6917 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006918 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006919{
6920 switch (tv->v_type)
6921 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006922 case VAR_FUNC:
6923 *tofree = string_quote(tv->vval.v_string, TRUE);
6924 return *tofree;
6925 case VAR_STRING:
6926 *tofree = string_quote(tv->vval.v_string, FALSE);
6927 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006928 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006929 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006930 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006931 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006932 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006933 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006934 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006935 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006936}
6937
6938/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006939 * Return string "str" in ' quotes, doubling ' characters.
6940 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006941 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006942 */
6943 static char_u *
6944string_quote(str, function)
6945 char_u *str;
6946 int function;
6947{
Bram Moolenaar33570922005-01-25 22:26:29 +00006948 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006949 char_u *p, *r, *s;
6950
Bram Moolenaar33570922005-01-25 22:26:29 +00006951 len = (function ? 13 : 3);
6952 if (str != NULL)
6953 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006954 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00006955 for (p = str; *p != NUL; mb_ptr_adv(p))
6956 if (*p == '\'')
6957 ++len;
6958 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006959 s = r = alloc(len);
6960 if (r != NULL)
6961 {
6962 if (function)
6963 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006964 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006965 r += 10;
6966 }
6967 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006968 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006969 if (str != NULL)
6970 for (p = str; *p != NUL; )
6971 {
6972 if (*p == '\'')
6973 *r++ = '\'';
6974 MB_COPY_CHAR(p, r);
6975 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006976 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006977 if (function)
6978 *r++ = ')';
6979 *r++ = NUL;
6980 }
6981 return s;
6982}
6983
6984/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985 * Get the value of an environment variable.
6986 * "arg" is pointing to the '$'. It is advanced to after the name.
6987 * If the environment variable was not set, silently assume it is empty.
6988 * Always return OK.
6989 */
6990 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006991get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006993 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 int evaluate;
6995{
6996 char_u *string = NULL;
6997 int len;
6998 int cc;
6999 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007000 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001
7002 ++*arg;
7003 name = *arg;
7004 len = get_env_len(arg);
7005 if (evaluate)
7006 {
7007 if (len != 0)
7008 {
7009 cc = name[len];
7010 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007011 /* first try vim_getenv(), fast for normal environment vars */
7012 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007014 {
7015 if (!mustfree)
7016 string = vim_strsave(string);
7017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 else
7019 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007020 if (mustfree)
7021 vim_free(string);
7022
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023 /* next try expanding things like $VIM and ${HOME} */
7024 string = expand_env_save(name - 1);
7025 if (string != NULL && *string == '$')
7026 {
7027 vim_free(string);
7028 string = NULL;
7029 }
7030 }
7031 name[len] = cc;
7032 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007033 rettv->v_type = VAR_STRING;
7034 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035 }
7036
7037 return OK;
7038}
7039
7040/*
7041 * Array with names and number of arguments of all internal functions
7042 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7043 */
7044static struct fst
7045{
7046 char *f_name; /* function name */
7047 char f_min_argc; /* minimal number of arguments */
7048 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007049 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007050 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051} functions[] =
7052{
Bram Moolenaar0d660222005-01-07 21:51:51 +00007053 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 {"append", 2, 2, f_append},
7055 {"argc", 0, 0, f_argc},
7056 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007057 {"argv", 0, 1, f_argv},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007059 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060 {"bufexists", 1, 1, f_bufexists},
7061 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7062 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7063 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7064 {"buflisted", 1, 1, f_buflisted},
7065 {"bufloaded", 1, 1, f_bufloaded},
7066 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007067 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068 {"bufwinnr", 1, 1, f_bufwinnr},
7069 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007070 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007071 {"call", 2, 3, f_call},
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007072 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073 {"char2nr", 1, 1, f_char2nr},
7074 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007075 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007077#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007078 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007079 {"complete_add", 1, 1, f_complete_add},
7080 {"complete_check", 0, 0, f_complete_check},
7081#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007083 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007084 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007086 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007087 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 {"delete", 1, 1, f_delete},
7089 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007090 {"diff_filler", 1, 1, f_diff_filler},
7091 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007092 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007094 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095 {"eventhandler", 0, 0, f_eventhandler},
7096 {"executable", 1, 1, f_executable},
7097 {"exists", 1, 1, f_exists},
7098 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007099 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007100 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7102 {"filereadable", 1, 1, f_filereadable},
7103 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007104 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007105 {"finddir", 1, 3, f_finddir},
7106 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107 {"fnamemodify", 2, 2, f_fnamemodify},
7108 {"foldclosed", 1, 1, f_foldclosed},
7109 {"foldclosedend", 1, 1, f_foldclosedend},
7110 {"foldlevel", 1, 1, f_foldlevel},
7111 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007112 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007114 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007115 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007116 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007117 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118 {"getbufvar", 2, 2, f_getbufvar},
7119 {"getchar", 0, 1, f_getchar},
7120 {"getcharmod", 0, 0, f_getcharmod},
7121 {"getcmdline", 0, 0, f_getcmdline},
7122 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007123 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007125 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007126 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127 {"getfsize", 1, 1, f_getfsize},
7128 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007129 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007130 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007131 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007132 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaara5525202006-03-02 22:52:09 +00007133 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007134 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007135 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007137 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138 {"getwinposx", 0, 0, f_getwinposx},
7139 {"getwinposy", 0, 0, f_getwinposy},
7140 {"getwinvar", 2, 2, f_getwinvar},
7141 {"glob", 1, 1, f_glob},
7142 {"globpath", 2, 2, f_globpath},
7143 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007144 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007145 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007146 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7148 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7149 {"histadd", 2, 2, f_histadd},
7150 {"histdel", 1, 2, f_histdel},
7151 {"histget", 1, 2, f_histget},
7152 {"histnr", 1, 1, f_histnr},
7153 {"hlID", 1, 1, f_hlID},
7154 {"hlexists", 1, 1, f_hlexists},
7155 {"hostname", 0, 0, f_hostname},
7156 {"iconv", 3, 3, f_iconv},
7157 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007158 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007159 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007161 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162 {"inputrestore", 0, 0, f_inputrestore},
7163 {"inputsave", 0, 0, f_inputsave},
7164 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007165 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007167 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007168 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007169 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007170 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007172 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173 {"libcall", 3, 3, f_libcall},
7174 {"libcallnr", 3, 3, f_libcallnr},
7175 {"line", 1, 1, f_line},
7176 {"line2byte", 1, 1, f_line2byte},
7177 {"lispindent", 1, 1, f_lispindent},
7178 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007179 {"map", 2, 2, f_map},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007180 {"maparg", 1, 3, f_maparg},
7181 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007182 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007183 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007184 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007185 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007186 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007187 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007188 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007189 {"max", 1, 1, f_max},
7190 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007191#ifdef vim_mkdir
7192 {"mkdir", 1, 3, f_mkdir},
7193#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194 {"mode", 0, 0, f_mode},
7195 {"nextnonblank", 1, 1, f_nextnonblank},
7196 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007197 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007199 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007200 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007201 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007202 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007203 {"reltime", 0, 2, f_reltime},
7204 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205 {"remote_expr", 2, 3, f_remote_expr},
7206 {"remote_foreground", 1, 1, f_remote_foreground},
7207 {"remote_peek", 1, 2, f_remote_peek},
7208 {"remote_read", 1, 1, f_remote_read},
7209 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007210 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007212 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007214 {"reverse", 1, 1, f_reverse},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00007215 {"search", 1, 3, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007216 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00007217 {"searchpair", 3, 6, f_searchpair},
7218 {"searchpairpos", 3, 6, f_searchpairpos},
7219 {"searchpos", 1, 3, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220 {"server2client", 2, 2, f_server2client},
7221 {"serverlist", 0, 0, f_serverlist},
7222 {"setbufvar", 3, 3, f_setbufvar},
7223 {"setcmdpos", 1, 1, f_setcmdpos},
7224 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007225 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007226 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007227 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007228 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229 {"setreg", 2, 3, f_setreg},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007230 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar60a495f2006-10-03 12:44:42 +00007232 {"shellescape", 1, 1, f_shellescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007234 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007235 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007236 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007237 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007238 {"split", 1, 3, f_split},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007239 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240#ifdef HAVE_STRFTIME
7241 {"strftime", 1, 2, f_strftime},
7242#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007243 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007244 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245 {"strlen", 1, 1, f_strlen},
7246 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007247 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248 {"strtrans", 1, 1, f_strtrans},
7249 {"submatch", 1, 1, f_submatch},
7250 {"substitute", 4, 4, f_substitute},
7251 {"synID", 3, 3, f_synID},
7252 {"synIDattr", 2, 3, f_synIDattr},
7253 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007254 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007255 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007256 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007257 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007258 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007259 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007261 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262 {"tolower", 1, 1, f_tolower},
7263 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007264 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007266 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 {"virtcol", 1, 1, f_virtcol},
7268 {"visualmode", 0, 1, f_visualmode},
7269 {"winbufnr", 1, 1, f_winbufnr},
7270 {"wincol", 0, 0, f_wincol},
7271 {"winheight", 1, 1, f_winheight},
7272 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007273 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007275 {"winrestview", 1, 1, f_winrestview},
7276 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007278 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279};
7280
7281#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7282
7283/*
7284 * Function given to ExpandGeneric() to obtain the list of internal
7285 * or user defined function names.
7286 */
7287 char_u *
7288get_function_name(xp, idx)
7289 expand_T *xp;
7290 int idx;
7291{
7292 static int intidx = -1;
7293 char_u *name;
7294
7295 if (idx == 0)
7296 intidx = -1;
7297 if (intidx < 0)
7298 {
7299 name = get_user_func_name(xp, idx);
7300 if (name != NULL)
7301 return name;
7302 }
7303 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7304 {
7305 STRCPY(IObuff, functions[intidx].f_name);
7306 STRCAT(IObuff, "(");
7307 if (functions[intidx].f_max_argc == 0)
7308 STRCAT(IObuff, ")");
7309 return IObuff;
7310 }
7311
7312 return NULL;
7313}
7314
7315/*
7316 * Function given to ExpandGeneric() to obtain the list of internal or
7317 * user defined variable or function names.
7318 */
7319/*ARGSUSED*/
7320 char_u *
7321get_expr_name(xp, idx)
7322 expand_T *xp;
7323 int idx;
7324{
7325 static int intidx = -1;
7326 char_u *name;
7327
7328 if (idx == 0)
7329 intidx = -1;
7330 if (intidx < 0)
7331 {
7332 name = get_function_name(xp, idx);
7333 if (name != NULL)
7334 return name;
7335 }
7336 return get_user_var_name(xp, ++intidx);
7337}
7338
7339#endif /* FEAT_CMDL_COMPL */
7340
7341/*
7342 * Find internal function in table above.
7343 * Return index, or -1 if not found
7344 */
7345 static int
7346find_internal_func(name)
7347 char_u *name; /* name of the function */
7348{
7349 int first = 0;
7350 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7351 int cmp;
7352 int x;
7353
7354 /*
7355 * Find the function name in the table. Binary search.
7356 */
7357 while (first <= last)
7358 {
7359 x = first + ((unsigned)(last - first) >> 1);
7360 cmp = STRCMP(name, functions[x].f_name);
7361 if (cmp < 0)
7362 last = x - 1;
7363 else if (cmp > 0)
7364 first = x + 1;
7365 else
7366 return x;
7367 }
7368 return -1;
7369}
7370
7371/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007372 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7373 * name it contains, otherwise return "name".
7374 */
7375 static char_u *
7376deref_func_name(name, lenp)
7377 char_u *name;
7378 int *lenp;
7379{
Bram Moolenaar33570922005-01-25 22:26:29 +00007380 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007381 int cc;
7382
7383 cc = name[*lenp];
7384 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007385 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007386 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007387 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007388 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007389 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007390 {
7391 *lenp = 0;
7392 return (char_u *)""; /* just in case */
7393 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007394 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00007395 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007396 }
7397
7398 return name;
7399}
7400
7401/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 * Allocate a variable for the result of a function.
7403 * Return OK or FAIL.
7404 */
7405 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007406get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7407 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408 char_u *name; /* name of the function */
7409 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007410 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411 char_u **arg; /* argument, pointing to the '(' */
7412 linenr_T firstline; /* first line of range */
7413 linenr_T lastline; /* last line of range */
7414 int *doesrange; /* return: function handled range */
7415 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007416 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417{
7418 char_u *argp;
7419 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007420 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421 int argcount = 0; /* number of arguments found */
7422
7423 /*
7424 * Get the arguments.
7425 */
7426 argp = *arg;
7427 while (argcount < MAX_FUNC_ARGS)
7428 {
7429 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7430 if (*argp == ')' || *argp == ',' || *argp == NUL)
7431 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7433 {
7434 ret = FAIL;
7435 break;
7436 }
7437 ++argcount;
7438 if (*argp != ',')
7439 break;
7440 }
7441 if (*argp == ')')
7442 ++argp;
7443 else
7444 ret = FAIL;
7445
7446 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007447 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007448 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007450 {
7451 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007452 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007453 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007454 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456
7457 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007458 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459
7460 *arg = skipwhite(argp);
7461 return ret;
7462}
7463
7464
7465/*
7466 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00007467 * Return OK when the function can't be called, FAIL otherwise.
7468 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469 */
7470 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007471call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007472 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 char_u *name; /* name of the function */
7474 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007475 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007477 typval_T *argvars; /* vars for arguments, must have "argcount"
7478 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479 linenr_T firstline; /* first line of range */
7480 linenr_T lastline; /* last line of range */
7481 int *doesrange; /* return: function handled range */
7482 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007483 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484{
7485 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486#define ERROR_UNKNOWN 0
7487#define ERROR_TOOMANY 1
7488#define ERROR_TOOFEW 2
7489#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007490#define ERROR_DICT 4
7491#define ERROR_NONE 5
7492#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493 int error = ERROR_NONE;
7494 int i;
7495 int llen;
7496 ufunc_T *fp;
7497 int cc;
7498#define FLEN_FIXED 40
7499 char_u fname_buf[FLEN_FIXED + 1];
7500 char_u *fname;
7501
7502 /*
7503 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7504 * Change <SNR>123_name() to K_SNR 123_name().
7505 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7506 */
7507 cc = name[len];
7508 name[len] = NUL;
7509 llen = eval_fname_script(name);
7510 if (llen > 0)
7511 {
7512 fname_buf[0] = K_SPECIAL;
7513 fname_buf[1] = KS_EXTRA;
7514 fname_buf[2] = (int)KE_SNR;
7515 i = 3;
7516 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7517 {
7518 if (current_SID <= 0)
7519 error = ERROR_SCRIPT;
7520 else
7521 {
7522 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7523 i = (int)STRLEN(fname_buf);
7524 }
7525 }
7526 if (i + STRLEN(name + llen) < FLEN_FIXED)
7527 {
7528 STRCPY(fname_buf + i, name + llen);
7529 fname = fname_buf;
7530 }
7531 else
7532 {
7533 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7534 if (fname == NULL)
7535 error = ERROR_OTHER;
7536 else
7537 {
7538 mch_memmove(fname, fname_buf, (size_t)i);
7539 STRCPY(fname + i, name + llen);
7540 }
7541 }
7542 }
7543 else
7544 fname = name;
7545
7546 *doesrange = FALSE;
7547
7548
7549 /* execute the function if no errors detected and executing */
7550 if (evaluate && error == ERROR_NONE)
7551 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007552 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553 error = ERROR_UNKNOWN;
7554
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007555 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556 {
7557 /*
7558 * User defined function.
7559 */
7560 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007561
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007563 /* Trigger FuncUndefined event, may load the function. */
7564 if (fp == NULL
7565 && apply_autocmds(EVENT_FUNCUNDEFINED,
7566 fname, fname, TRUE, NULL)
7567 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007569 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570 fp = find_func(fname);
7571 }
7572#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007573 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007574 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007575 {
7576 /* loaded a package, search for the function again */
7577 fp = find_func(fname);
7578 }
7579
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580 if (fp != NULL)
7581 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007582 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007584 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007586 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007588 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007589 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590 else
7591 {
7592 /*
7593 * Call the user function.
7594 * Save and restore search patterns, script variables and
7595 * redo buffer.
7596 */
7597 save_search_patterns();
7598 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007599 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007600 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007601 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007602 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7603 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7604 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007605 /* Function was unreferenced while being used, free it
7606 * now. */
7607 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608 restoreRedobuff();
7609 restore_search_patterns();
7610 error = ERROR_NONE;
7611 }
7612 }
7613 }
7614 else
7615 {
7616 /*
7617 * Find the function name in the table, call its implementation.
7618 */
7619 i = find_internal_func(fname);
7620 if (i >= 0)
7621 {
7622 if (argcount < functions[i].f_min_argc)
7623 error = ERROR_TOOFEW;
7624 else if (argcount > functions[i].f_max_argc)
7625 error = ERROR_TOOMANY;
7626 else
7627 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007628 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007629 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630 error = ERROR_NONE;
7631 }
7632 }
7633 }
7634 /*
7635 * The function call (or "FuncUndefined" autocommand sequence) might
7636 * have been aborted by an error, an interrupt, or an explicitly thrown
7637 * exception that has not been caught so far. This situation can be
7638 * tested for by calling aborting(). For an error in an internal
7639 * function or for the "E132" error in call_user_func(), however, the
7640 * throw point at which the "force_abort" flag (temporarily reset by
7641 * emsg()) is normally updated has not been reached yet. We need to
7642 * update that flag first to make aborting() reliable.
7643 */
7644 update_force_abort();
7645 }
7646 if (error == ERROR_NONE)
7647 ret = OK;
7648
7649 /*
7650 * Report an error unless the argument evaluation or function call has been
7651 * cancelled due to an aborting error, an interrupt, or an exception.
7652 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007653 if (!aborting())
7654 {
7655 switch (error)
7656 {
7657 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007658 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007659 break;
7660 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007661 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007662 break;
7663 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007664 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00007665 name);
7666 break;
7667 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007668 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00007669 name);
7670 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007671 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007672 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00007673 name);
7674 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007675 }
7676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677
7678 name[len] = cc;
7679 if (fname != name && fname != fname_buf)
7680 vim_free(fname);
7681
7682 return ret;
7683}
7684
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007685/*
7686 * Give an error message with a function name. Handle <SNR> things.
7687 */
7688 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00007689emsg_funcname(ermsg, name)
7690 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007691 char_u *name;
7692{
7693 char_u *p;
7694
7695 if (*name == K_SPECIAL)
7696 p = concat_str((char_u *)"<SNR>", name + 3);
7697 else
7698 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00007699 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007700 if (p != name)
7701 vim_free(p);
7702}
7703
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704/*********************************************
7705 * Implementation of the built-in functions
7706 */
7707
7708/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007709 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710 */
7711 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007712f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007713 typval_T *argvars;
7714 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715{
Bram Moolenaar33570922005-01-25 22:26:29 +00007716 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007718 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007719 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007721 if ((l = argvars[0].vval.v_list) != NULL
7722 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7723 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007724 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007725 }
7726 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007727 EMSG(_(e_listreq));
7728}
7729
7730/*
7731 * "append(lnum, string/list)" function
7732 */
7733 static void
7734f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007735 typval_T *argvars;
7736 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007737{
7738 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007739 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007740 list_T *l = NULL;
7741 listitem_T *li = NULL;
7742 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007743 long added = 0;
7744
Bram Moolenaar0d660222005-01-07 21:51:51 +00007745 lnum = get_tv_lnum(argvars);
7746 if (lnum >= 0
7747 && lnum <= curbuf->b_ml.ml_line_count
7748 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007749 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007750 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007751 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007752 l = argvars[1].vval.v_list;
7753 if (l == NULL)
7754 return;
7755 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007756 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007757 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007758 for (;;)
7759 {
7760 if (l == NULL)
7761 tv = &argvars[1]; /* append a string */
7762 else if (li == NULL)
7763 break; /* end of list */
7764 else
7765 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007766 line = get_tv_string_chk(tv);
7767 if (line == NULL) /* type error */
7768 {
7769 rettv->vval.v_number = 1; /* Failed */
7770 break;
7771 }
7772 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007773 ++added;
7774 if (l == NULL)
7775 break;
7776 li = li->li_next;
7777 }
7778
7779 appended_lines_mark(lnum, added);
7780 if (curwin->w_cursor.lnum > lnum)
7781 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007783 else
7784 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785}
7786
7787/*
7788 * "argc()" function
7789 */
7790/* ARGSUSED */
7791 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007792f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007793 typval_T *argvars;
7794 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007796 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797}
7798
7799/*
7800 * "argidx()" function
7801 */
7802/* ARGSUSED */
7803 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007804f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007805 typval_T *argvars;
7806 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007808 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809}
7810
7811/*
7812 * "argv(nr)" function
7813 */
7814 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007815f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007816 typval_T *argvars;
7817 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818{
7819 int idx;
7820
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007821 if (argvars[0].v_type != VAR_UNKNOWN)
7822 {
7823 idx = get_tv_number_chk(&argvars[0], NULL);
7824 if (idx >= 0 && idx < ARGCOUNT)
7825 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
7826 else
7827 rettv->vval.v_string = NULL;
7828 rettv->v_type = VAR_STRING;
7829 }
7830 else if (rettv_list_alloc(rettv) == OK)
7831 for (idx = 0; idx < ARGCOUNT; ++idx)
7832 list_append_string(rettv->vval.v_list,
7833 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834}
7835
7836/*
7837 * "browse(save, title, initdir, default)" function
7838 */
7839/* ARGSUSED */
7840 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007841f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007842 typval_T *argvars;
7843 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844{
7845#ifdef FEAT_BROWSE
7846 int save;
7847 char_u *title;
7848 char_u *initdir;
7849 char_u *defname;
7850 char_u buf[NUMBUFLEN];
7851 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007852 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007854 save = get_tv_number_chk(&argvars[0], &error);
7855 title = get_tv_string_chk(&argvars[1]);
7856 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7857 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007859 if (error || title == NULL || initdir == NULL || defname == NULL)
7860 rettv->vval.v_string = NULL;
7861 else
7862 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007863 do_browse(save ? BROWSE_SAVE : 0,
7864 title, defname, NULL, initdir, NULL, curbuf);
7865#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007866 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007867#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007868 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007869}
7870
7871/*
7872 * "browsedir(title, initdir)" function
7873 */
7874/* ARGSUSED */
7875 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007876f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007877 typval_T *argvars;
7878 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007879{
7880#ifdef FEAT_BROWSE
7881 char_u *title;
7882 char_u *initdir;
7883 char_u buf[NUMBUFLEN];
7884
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007885 title = get_tv_string_chk(&argvars[0]);
7886 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007887
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007888 if (title == NULL || initdir == NULL)
7889 rettv->vval.v_string = NULL;
7890 else
7891 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007892 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007894 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007896 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897}
7898
Bram Moolenaar33570922005-01-25 22:26:29 +00007899static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007900
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901/*
7902 * Find a buffer by number or exact name.
7903 */
7904 static buf_T *
7905find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007906 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907{
7908 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007910 if (avar->v_type == VAR_NUMBER)
7911 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007912 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007914 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007915 if (buf == NULL)
7916 {
7917 /* No full path name match, try a match with a URL or a "nofile"
7918 * buffer, these don't use the full path. */
7919 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7920 if (buf->b_fname != NULL
7921 && (path_with_url(buf->b_fname)
7922#ifdef FEAT_QUICKFIX
7923 || bt_nofile(buf)
7924#endif
7925 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007926 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007927 break;
7928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929 }
7930 return buf;
7931}
7932
7933/*
7934 * "bufexists(expr)" function
7935 */
7936 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007937f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007938 typval_T *argvars;
7939 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007941 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942}
7943
7944/*
7945 * "buflisted(expr)" function
7946 */
7947 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007948f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007949 typval_T *argvars;
7950 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951{
7952 buf_T *buf;
7953
7954 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007955 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956}
7957
7958/*
7959 * "bufloaded(expr)" function
7960 */
7961 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007962f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007963 typval_T *argvars;
7964 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965{
7966 buf_T *buf;
7967
7968 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007969 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970}
7971
Bram Moolenaar33570922005-01-25 22:26:29 +00007972static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007973
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974/*
7975 * Get buffer by number or pattern.
7976 */
7977 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007978get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007979 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007981 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982 int save_magic;
7983 char_u *save_cpo;
7984 buf_T *buf;
7985
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007986 if (tv->v_type == VAR_NUMBER)
7987 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007988 if (tv->v_type != VAR_STRING)
7989 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990 if (name == NULL || *name == NUL)
7991 return curbuf;
7992 if (name[0] == '$' && name[1] == NUL)
7993 return lastbuf;
7994
7995 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7996 save_magic = p_magic;
7997 p_magic = TRUE;
7998 save_cpo = p_cpo;
7999 p_cpo = (char_u *)"";
8000
8001 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
8002 TRUE, FALSE));
8003
8004 p_magic = save_magic;
8005 p_cpo = save_cpo;
8006
8007 /* If not found, try expanding the name, like done for bufexists(). */
8008 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008009 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010
8011 return buf;
8012}
8013
8014/*
8015 * "bufname(expr)" function
8016 */
8017 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008018f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008019 typval_T *argvars;
8020 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021{
8022 buf_T *buf;
8023
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008024 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008026 buf = get_buf_tv(&argvars[0]);
8027 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008029 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008031 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032 --emsg_off;
8033}
8034
8035/*
8036 * "bufnr(expr)" function
8037 */
8038 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008039f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008040 typval_T *argvars;
8041 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042{
8043 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008044 int error = FALSE;
8045 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008047 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008048 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008049 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008050 --emsg_off;
8051
8052 /* If the buffer isn't found and the second argument is not zero create a
8053 * new buffer. */
8054 if (buf == NULL
8055 && argvars[1].v_type != VAR_UNKNOWN
8056 && get_tv_number_chk(&argvars[1], &error) != 0
8057 && !error
8058 && (name = get_tv_string_chk(&argvars[0])) != NULL
8059 && !error)
8060 buf = buflist_new(name, NULL, (linenr_T)1, 0);
8061
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008063 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008065 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066}
8067
8068/*
8069 * "bufwinnr(nr)" function
8070 */
8071 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008072f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008073 typval_T *argvars;
8074 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075{
8076#ifdef FEAT_WINDOWS
8077 win_T *wp;
8078 int winnr = 0;
8079#endif
8080 buf_T *buf;
8081
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008082 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008084 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085#ifdef FEAT_WINDOWS
8086 for (wp = firstwin; wp; wp = wp->w_next)
8087 {
8088 ++winnr;
8089 if (wp->w_buffer == buf)
8090 break;
8091 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008092 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008094 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095#endif
8096 --emsg_off;
8097}
8098
8099/*
8100 * "byte2line(byte)" function
8101 */
8102/*ARGSUSED*/
8103 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008104f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008105 typval_T *argvars;
8106 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107{
8108#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008109 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110#else
8111 long boff = 0;
8112
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008113 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008115 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008117 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118 (linenr_T)0, &boff);
8119#endif
8120}
8121
8122/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008123 * "byteidx()" function
8124 */
8125/*ARGSUSED*/
8126 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008127f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008128 typval_T *argvars;
8129 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008130{
8131#ifdef FEAT_MBYTE
8132 char_u *t;
8133#endif
8134 char_u *str;
8135 long idx;
8136
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008137 str = get_tv_string_chk(&argvars[0]);
8138 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008139 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008140 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008141 return;
8142
8143#ifdef FEAT_MBYTE
8144 t = str;
8145 for ( ; idx > 0; idx--)
8146 {
8147 if (*t == NUL) /* EOL reached */
8148 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008149 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008150 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008151 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008152#else
8153 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008154 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008155#endif
8156}
8157
8158/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008159 * "call(func, arglist)" function
8160 */
8161 static void
8162f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008163 typval_T *argvars;
8164 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008165{
8166 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008167 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008168 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008169 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008170 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00008171 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008172
8173 rettv->vval.v_number = 0;
8174 if (argvars[1].v_type != VAR_LIST)
8175 {
8176 EMSG(_(e_listreq));
8177 return;
8178 }
8179 if (argvars[1].vval.v_list == NULL)
8180 return;
8181
8182 if (argvars[0].v_type == VAR_FUNC)
8183 func = argvars[0].vval.v_string;
8184 else
8185 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008186 if (*func == NUL)
8187 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008188
Bram Moolenaare9a41262005-01-15 22:18:47 +00008189 if (argvars[2].v_type != VAR_UNKNOWN)
8190 {
8191 if (argvars[2].v_type != VAR_DICT)
8192 {
8193 EMSG(_(e_dictreq));
8194 return;
8195 }
8196 selfdict = argvars[2].vval.v_dict;
8197 }
8198
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008199 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
8200 item = item->li_next)
8201 {
8202 if (argc == MAX_FUNC_ARGS)
8203 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008204 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008205 break;
8206 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008207 /* Make a copy of each argument. This is needed to be able to set
8208 * v_lock to VAR_FIXED in the copy without changing the original list.
8209 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008210 copy_tv(&item->li_tv, &argv[argc++]);
8211 }
8212
8213 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008214 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008215 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
8216 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008217
8218 /* Free the arguments. */
8219 while (argc > 0)
8220 clear_tv(&argv[--argc]);
8221}
8222
8223/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008224 * "changenr()" function
8225 */
8226/*ARGSUSED*/
8227 static void
8228f_changenr(argvars, rettv)
8229 typval_T *argvars;
8230 typval_T *rettv;
8231{
8232 rettv->vval.v_number = curbuf->b_u_seq_cur;
8233}
8234
8235/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236 * "char2nr(string)" function
8237 */
8238 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008239f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008240 typval_T *argvars;
8241 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242{
8243#ifdef FEAT_MBYTE
8244 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008245 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 else
8247#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008248 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249}
8250
8251/*
8252 * "cindent(lnum)" function
8253 */
8254 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008255f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008256 typval_T *argvars;
8257 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258{
8259#ifdef FEAT_CINDENT
8260 pos_T pos;
8261 linenr_T lnum;
8262
8263 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008264 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8266 {
8267 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008268 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269 curwin->w_cursor = pos;
8270 }
8271 else
8272#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008273 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274}
8275
8276/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008277 * "clearmatches()" function
8278 */
8279/*ARGSUSED*/
8280 static void
8281f_clearmatches(argvars, rettv)
8282 typval_T *argvars;
8283 typval_T *rettv;
8284{
8285#ifdef FEAT_SEARCH_EXTRA
8286 clear_matches(curwin);
8287#endif
8288}
8289
8290/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291 * "col(string)" function
8292 */
8293 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008294f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008295 typval_T *argvars;
8296 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297{
8298 colnr_T col = 0;
8299 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008300 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008302 fp = var2fpos(&argvars[0], FALSE, &fnum);
8303 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 {
8305 if (fp->col == MAXCOL)
8306 {
8307 /* '> can be MAXCOL, get the length of the line then */
8308 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008309 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310 else
8311 col = MAXCOL;
8312 }
8313 else
8314 {
8315 col = fp->col + 1;
8316#ifdef FEAT_VIRTUALEDIT
8317 /* col(".") when the cursor is on the NUL at the end of the line
8318 * because of "coladd" can be seen as an extra column. */
8319 if (virtual_active() && fp == &curwin->w_cursor)
8320 {
8321 char_u *p = ml_get_cursor();
8322
8323 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
8324 curwin->w_virtcol - curwin->w_cursor.coladd))
8325 {
8326# ifdef FEAT_MBYTE
8327 int l;
8328
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008329 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 col += l;
8331# else
8332 if (*p != NUL && p[1] == NUL)
8333 ++col;
8334# endif
8335 }
8336 }
8337#endif
8338 }
8339 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008340 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341}
8342
Bram Moolenaar572cb562005-08-05 21:35:02 +00008343#if defined(FEAT_INS_EXPAND)
8344/*
Bram Moolenaarade00832006-03-10 21:46:58 +00008345 * "complete()" function
8346 */
8347/*ARGSUSED*/
8348 static void
8349f_complete(argvars, rettv)
8350 typval_T *argvars;
8351 typval_T *rettv;
8352{
8353 int startcol;
8354
8355 if ((State & INSERT) == 0)
8356 {
8357 EMSG(_("E785: complete() can only be used in Insert mode"));
8358 return;
8359 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00008360
8361 /* Check for undo allowed here, because if something was already inserted
8362 * the line was already saved for undo and this check isn't done. */
8363 if (!undo_allowed())
8364 return;
8365
Bram Moolenaarade00832006-03-10 21:46:58 +00008366 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
8367 {
8368 EMSG(_(e_invarg));
8369 return;
8370 }
8371
8372 startcol = get_tv_number_chk(&argvars[0], NULL);
8373 if (startcol <= 0)
8374 return;
8375
8376 set_completion(startcol - 1, argvars[1].vval.v_list);
8377}
8378
8379/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00008380 * "complete_add()" function
8381 */
8382/*ARGSUSED*/
8383 static void
8384f_complete_add(argvars, rettv)
8385 typval_T *argvars;
8386 typval_T *rettv;
8387{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00008388 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00008389}
8390
8391/*
8392 * "complete_check()" function
8393 */
8394/*ARGSUSED*/
8395 static void
8396f_complete_check(argvars, rettv)
8397 typval_T *argvars;
8398 typval_T *rettv;
8399{
8400 int saved = RedrawingDisabled;
8401
8402 RedrawingDisabled = 0;
8403 ins_compl_check_keys(0);
8404 rettv->vval.v_number = compl_interrupted;
8405 RedrawingDisabled = saved;
8406}
8407#endif
8408
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409/*
8410 * "confirm(message, buttons[, default [, type]])" function
8411 */
8412/*ARGSUSED*/
8413 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008414f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008415 typval_T *argvars;
8416 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417{
8418#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
8419 char_u *message;
8420 char_u *buttons = NULL;
8421 char_u buf[NUMBUFLEN];
8422 char_u buf2[NUMBUFLEN];
8423 int def = 1;
8424 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008425 char_u *typestr;
8426 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008428 message = get_tv_string_chk(&argvars[0]);
8429 if (message == NULL)
8430 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008431 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008433 buttons = get_tv_string_buf_chk(&argvars[1], buf);
8434 if (buttons == NULL)
8435 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008436 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008438 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008439 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008441 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
8442 if (typestr == NULL)
8443 error = TRUE;
8444 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008446 switch (TOUPPER_ASC(*typestr))
8447 {
8448 case 'E': type = VIM_ERROR; break;
8449 case 'Q': type = VIM_QUESTION; break;
8450 case 'I': type = VIM_INFO; break;
8451 case 'W': type = VIM_WARNING; break;
8452 case 'G': type = VIM_GENERIC; break;
8453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454 }
8455 }
8456 }
8457 }
8458
8459 if (buttons == NULL || *buttons == NUL)
8460 buttons = (char_u *)_("&Ok");
8461
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008462 if (error)
8463 rettv->vval.v_number = 0;
8464 else
8465 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466 def, NULL);
8467#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008468 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469#endif
8470}
8471
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008472/*
8473 * "copy()" function
8474 */
8475 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008476f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008477 typval_T *argvars;
8478 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008479{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008480 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008481}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482
8483/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008484 * "count()" function
8485 */
8486 static void
8487f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008488 typval_T *argvars;
8489 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008490{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008491 long n = 0;
8492 int ic = FALSE;
8493
Bram Moolenaare9a41262005-01-15 22:18:47 +00008494 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008495 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008496 listitem_T *li;
8497 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008498 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008499
Bram Moolenaare9a41262005-01-15 22:18:47 +00008500 if ((l = argvars[0].vval.v_list) != NULL)
8501 {
8502 li = l->lv_first;
8503 if (argvars[2].v_type != VAR_UNKNOWN)
8504 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008505 int error = FALSE;
8506
8507 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008508 if (argvars[3].v_type != VAR_UNKNOWN)
8509 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008510 idx = get_tv_number_chk(&argvars[3], &error);
8511 if (!error)
8512 {
8513 li = list_find(l, idx);
8514 if (li == NULL)
8515 EMSGN(_(e_listidx), idx);
8516 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008517 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008518 if (error)
8519 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008520 }
8521
8522 for ( ; li != NULL; li = li->li_next)
8523 if (tv_equal(&li->li_tv, &argvars[1], ic))
8524 ++n;
8525 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008526 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008527 else if (argvars[0].v_type == VAR_DICT)
8528 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008529 int todo;
8530 dict_T *d;
8531 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008532
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008533 if ((d = argvars[0].vval.v_dict) != NULL)
8534 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008535 int error = FALSE;
8536
Bram Moolenaare9a41262005-01-15 22:18:47 +00008537 if (argvars[2].v_type != VAR_UNKNOWN)
8538 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008539 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008540 if (argvars[3].v_type != VAR_UNKNOWN)
8541 EMSG(_(e_invarg));
8542 }
8543
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008544 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008545 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008546 {
8547 if (!HASHITEM_EMPTY(hi))
8548 {
8549 --todo;
8550 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
8551 ++n;
8552 }
8553 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008554 }
8555 }
8556 else
8557 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008558 rettv->vval.v_number = n;
8559}
8560
8561/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8563 *
8564 * Checks the existence of a cscope connection.
8565 */
8566/*ARGSUSED*/
8567 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008568f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008569 typval_T *argvars;
8570 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571{
8572#ifdef FEAT_CSCOPE
8573 int num = 0;
8574 char_u *dbpath = NULL;
8575 char_u *prepend = NULL;
8576 char_u buf[NUMBUFLEN];
8577
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008578 if (argvars[0].v_type != VAR_UNKNOWN
8579 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008581 num = (int)get_tv_number(&argvars[0]);
8582 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008583 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008584 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585 }
8586
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008587 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008589 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590#endif
8591}
8592
8593/*
8594 * "cursor(lnum, col)" function
8595 *
8596 * Moves the cursor to the specified line and column
8597 */
8598/*ARGSUSED*/
8599 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008600f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008601 typval_T *argvars;
8602 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008603{
8604 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008605#ifdef FEAT_VIRTUALEDIT
8606 long coladd = 0;
8607#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608
Bram Moolenaara5525202006-03-02 22:52:09 +00008609 if (argvars[1].v_type == VAR_UNKNOWN)
8610 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008611 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00008612
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008613 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00008614 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008615 line = pos.lnum;
8616 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008617#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008618 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00008619#endif
8620 }
8621 else
8622 {
8623 line = get_tv_lnum(argvars);
8624 col = get_tv_number_chk(&argvars[1], NULL);
8625#ifdef FEAT_VIRTUALEDIT
8626 if (argvars[2].v_type != VAR_UNKNOWN)
8627 coladd = get_tv_number_chk(&argvars[2], NULL);
8628#endif
8629 }
8630 if (line < 0 || col < 0
8631#ifdef FEAT_VIRTUALEDIT
8632 || coladd < 0
8633#endif
8634 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008635 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636 if (line > 0)
8637 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638 if (col > 0)
8639 curwin->w_cursor.col = col - 1;
8640#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00008641 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642#endif
8643
8644 /* Make sure the cursor is in a valid position. */
8645 check_cursor();
8646#ifdef FEAT_MBYTE
8647 /* Correct cursor for multi-byte character. */
8648 if (has_mbyte)
8649 mb_adjust_cursor();
8650#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008651
8652 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653}
8654
8655/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008656 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008657 */
8658 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008659f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008660 typval_T *argvars;
8661 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008662{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008663 int noref = 0;
8664
8665 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008666 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008667 if (noref < 0 || noref > 1)
8668 EMSG(_(e_invarg));
8669 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008670 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008671}
8672
8673/*
8674 * "delete()" function
8675 */
8676 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008677f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008678 typval_T *argvars;
8679 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680{
8681 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008682 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008683 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008684 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685}
8686
8687/*
8688 * "did_filetype()" function
8689 */
8690/*ARGSUSED*/
8691 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008692f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008693 typval_T *argvars;
8694 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695{
8696#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008697 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008699 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700#endif
8701}
8702
8703/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008704 * "diff_filler()" function
8705 */
8706/*ARGSUSED*/
8707 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008708f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008709 typval_T *argvars;
8710 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008711{
8712#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008713 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008714#endif
8715}
8716
8717/*
8718 * "diff_hlID()" function
8719 */
8720/*ARGSUSED*/
8721 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008722f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008723 typval_T *argvars;
8724 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008725{
8726#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008727 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008728 static linenr_T prev_lnum = 0;
8729 static int changedtick = 0;
8730 static int fnum = 0;
8731 static int change_start = 0;
8732 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00008733 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008734 int filler_lines;
8735 int col;
8736
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008737 if (lnum < 0) /* ignore type error in {lnum} arg */
8738 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008739 if (lnum != prev_lnum
8740 || changedtick != curbuf->b_changedtick
8741 || fnum != curbuf->b_fnum)
8742 {
8743 /* New line, buffer, change: need to get the values. */
8744 filler_lines = diff_check(curwin, lnum);
8745 if (filler_lines < 0)
8746 {
8747 if (filler_lines == -1)
8748 {
8749 change_start = MAXCOL;
8750 change_end = -1;
8751 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8752 hlID = HLF_ADD; /* added line */
8753 else
8754 hlID = HLF_CHD; /* changed line */
8755 }
8756 else
8757 hlID = HLF_ADD; /* added line */
8758 }
8759 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008760 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008761 prev_lnum = lnum;
8762 changedtick = curbuf->b_changedtick;
8763 fnum = curbuf->b_fnum;
8764 }
8765
8766 if (hlID == HLF_CHD || hlID == HLF_TXD)
8767 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008768 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008769 if (col >= change_start && col <= change_end)
8770 hlID = HLF_TXD; /* changed text */
8771 else
8772 hlID = HLF_CHD; /* changed line */
8773 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008774 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008775#endif
8776}
8777
8778/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008779 * "empty({expr})" function
8780 */
8781 static void
8782f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008783 typval_T *argvars;
8784 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008785{
8786 int n;
8787
8788 switch (argvars[0].v_type)
8789 {
8790 case VAR_STRING:
8791 case VAR_FUNC:
8792 n = argvars[0].vval.v_string == NULL
8793 || *argvars[0].vval.v_string == NUL;
8794 break;
8795 case VAR_NUMBER:
8796 n = argvars[0].vval.v_number == 0;
8797 break;
8798 case VAR_LIST:
8799 n = argvars[0].vval.v_list == NULL
8800 || argvars[0].vval.v_list->lv_first == NULL;
8801 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008802 case VAR_DICT:
8803 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008804 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008805 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008806 default:
8807 EMSG2(_(e_intern2), "f_empty()");
8808 n = 0;
8809 }
8810
8811 rettv->vval.v_number = n;
8812}
8813
8814/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815 * "escape({string}, {chars})" function
8816 */
8817 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008818f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008819 typval_T *argvars;
8820 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821{
8822 char_u buf[NUMBUFLEN];
8823
Bram Moolenaar758711c2005-02-02 23:11:38 +00008824 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8825 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008826 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827}
8828
8829/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008830 * "eval()" function
8831 */
8832/*ARGSUSED*/
8833 static void
8834f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008835 typval_T *argvars;
8836 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008837{
8838 char_u *s;
8839
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008840 s = get_tv_string_chk(&argvars[0]);
8841 if (s != NULL)
8842 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008843
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008844 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8845 {
8846 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008847 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008848 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008849 else if (*s != NUL)
8850 EMSG(_(e_trailing));
8851}
8852
8853/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854 * "eventhandler()" function
8855 */
8856/*ARGSUSED*/
8857 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008858f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008859 typval_T *argvars;
8860 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008862 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008863}
8864
8865/*
8866 * "executable()" function
8867 */
8868 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008869f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008870 typval_T *argvars;
8871 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008873 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874}
8875
8876/*
8877 * "exists()" function
8878 */
8879 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008880f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008881 typval_T *argvars;
8882 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883{
8884 char_u *p;
8885 char_u *name;
8886 int n = FALSE;
8887 int len = 0;
8888
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008889 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890 if (*p == '$') /* environment variable */
8891 {
8892 /* first try "normal" environment variables (fast) */
8893 if (mch_getenv(p + 1) != NULL)
8894 n = TRUE;
8895 else
8896 {
8897 /* try expanding things like $VIM and ${HOME} */
8898 p = expand_env_save(p);
8899 if (p != NULL && *p != '$')
8900 n = TRUE;
8901 vim_free(p);
8902 }
8903 }
8904 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00008905 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008906 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00008907 if (*skipwhite(p) != NUL)
8908 n = FALSE; /* trailing garbage */
8909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008910 else if (*p == '*') /* internal or user defined function */
8911 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008912 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913 }
8914 else if (*p == ':')
8915 {
8916 n = cmd_exists(p + 1);
8917 }
8918 else if (*p == '#')
8919 {
8920#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00008921 if (p[1] == '#')
8922 n = autocmd_supported(p + 2);
8923 else
8924 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925#endif
8926 }
8927 else /* internal variable */
8928 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008929 char_u *tofree;
8930 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008932 /* get_name_len() takes care of expanding curly braces */
8933 name = p;
8934 len = get_name_len(&p, &tofree, TRUE, FALSE);
8935 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008937 if (tofree != NULL)
8938 name = tofree;
8939 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8940 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008941 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008942 /* handle d.key, l[idx], f(expr) */
8943 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8944 if (n)
8945 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946 }
8947 }
Bram Moolenaar79783442006-05-05 21:18:03 +00008948 if (*p != NUL)
8949 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008951 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952 }
8953
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008954 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955}
8956
8957/*
8958 * "expand()" function
8959 */
8960 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008961f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008962 typval_T *argvars;
8963 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008964{
8965 char_u *s;
8966 int len;
8967 char_u *errormsg;
8968 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8969 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008970 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008971
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008972 rettv->v_type = VAR_STRING;
8973 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008974 if (*s == '%' || *s == '#' || *s == '<')
8975 {
8976 ++emsg_off;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008977 rettv->vval.v_string = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978 --emsg_off;
8979 }
8980 else
8981 {
8982 /* When the optional second argument is non-zero, don't remove matches
8983 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008984 if (argvars[1].v_type != VAR_UNKNOWN
8985 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008986 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008987 if (!error)
8988 {
8989 ExpandInit(&xpc);
8990 xpc.xp_context = EXPAND_FILES;
8991 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008992 }
8993 else
8994 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008995 }
8996}
8997
8998/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008999 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00009000 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009001 */
9002 static void
9003f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009004 typval_T *argvars;
9005 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009006{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009007 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009008 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009009 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009010 list_T *l1, *l2;
9011 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009012 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009013 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009014
Bram Moolenaare9a41262005-01-15 22:18:47 +00009015 l1 = argvars[0].vval.v_list;
9016 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009017 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
9018 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009019 {
9020 if (argvars[2].v_type != VAR_UNKNOWN)
9021 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009022 before = get_tv_number_chk(&argvars[2], &error);
9023 if (error)
9024 return; /* type error; errmsg already given */
9025
Bram Moolenaar758711c2005-02-02 23:11:38 +00009026 if (before == l1->lv_len)
9027 item = NULL;
9028 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009029 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009030 item = list_find(l1, before);
9031 if (item == NULL)
9032 {
9033 EMSGN(_(e_listidx), before);
9034 return;
9035 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009036 }
9037 }
9038 else
9039 item = NULL;
9040 list_extend(l1, l2, item);
9041
Bram Moolenaare9a41262005-01-15 22:18:47 +00009042 copy_tv(&argvars[0], rettv);
9043 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009044 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009045 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
9046 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009047 dict_T *d1, *d2;
9048 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009049 char_u *action;
9050 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00009051 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009052 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009053
9054 d1 = argvars[0].vval.v_dict;
9055 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009056 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
9057 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009058 {
9059 /* Check the third argument. */
9060 if (argvars[2].v_type != VAR_UNKNOWN)
9061 {
9062 static char *(av[]) = {"keep", "force", "error"};
9063
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009064 action = get_tv_string_chk(&argvars[2]);
9065 if (action == NULL)
9066 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009067 for (i = 0; i < 3; ++i)
9068 if (STRCMP(action, av[i]) == 0)
9069 break;
9070 if (i == 3)
9071 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009072 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009073 return;
9074 }
9075 }
9076 else
9077 action = (char_u *)"force";
9078
9079 /* Go over all entries in the second dict and add them to the
9080 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009081 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009082 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009083 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009084 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009085 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009086 --todo;
9087 di1 = dict_find(d1, hi2->hi_key, -1);
9088 if (di1 == NULL)
9089 {
9090 di1 = dictitem_copy(HI2DI(hi2));
9091 if (di1 != NULL && dict_add(d1, di1) == FAIL)
9092 dictitem_free(di1);
9093 }
9094 else if (*action == 'e')
9095 {
9096 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
9097 break;
9098 }
9099 else if (*action == 'f')
9100 {
9101 clear_tv(&di1->di_tv);
9102 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
9103 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009104 }
9105 }
9106
Bram Moolenaare9a41262005-01-15 22:18:47 +00009107 copy_tv(&argvars[0], rettv);
9108 }
9109 }
9110 else
9111 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009112}
9113
9114/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009115 * "feedkeys()" function
9116 */
9117/*ARGSUSED*/
9118 static void
9119f_feedkeys(argvars, rettv)
9120 typval_T *argvars;
9121 typval_T *rettv;
9122{
9123 int remap = TRUE;
9124 char_u *keys, *flags;
9125 char_u nbuf[NUMBUFLEN];
9126 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009127 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009128
Bram Moolenaar3d43a662007-04-27 20:15:55 +00009129 /* This is not allowed in the sandbox. If the commands would still be
9130 * executed in the sandbox it would be OK, but it probably happens later,
9131 * when "sandbox" is no longer set. */
9132 if (check_secure())
9133 return;
9134
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009135 rettv->vval.v_number = 0;
9136 keys = get_tv_string(&argvars[0]);
9137 if (*keys != NUL)
9138 {
9139 if (argvars[1].v_type != VAR_UNKNOWN)
9140 {
9141 flags = get_tv_string_buf(&argvars[1], nbuf);
9142 for ( ; *flags != NUL; ++flags)
9143 {
9144 switch (*flags)
9145 {
9146 case 'n': remap = FALSE; break;
9147 case 'm': remap = TRUE; break;
9148 case 't': typed = TRUE; break;
9149 }
9150 }
9151 }
9152
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009153 /* Need to escape K_SPECIAL and CSI before putting the string in the
9154 * typeahead buffer. */
9155 keys_esc = vim_strsave_escape_csi(keys);
9156 if (keys_esc != NULL)
9157 {
9158 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009159 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009160 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009161 if (vgetc_busy)
9162 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009163 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009164 }
9165}
9166
9167/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168 * "filereadable()" function
9169 */
9170 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009171f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009172 typval_T *argvars;
9173 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009174{
9175 FILE *fd;
9176 char_u *p;
9177 int n;
9178
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009179 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009180 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
9181 {
9182 n = TRUE;
9183 fclose(fd);
9184 }
9185 else
9186 n = FALSE;
9187
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009188 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009189}
9190
9191/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009192 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00009193 * rights to write into.
9194 */
9195 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009196f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009197 typval_T *argvars;
9198 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009200 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201}
9202
Bram Moolenaar33570922005-01-25 22:26:29 +00009203static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009204
9205 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00009206findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00009207 typval_T *argvars;
9208 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009209 int dir;
9210{
9211#ifdef FEAT_SEARCHPATH
9212 char_u *fname;
9213 char_u *fresult = NULL;
9214 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
9215 char_u *p;
9216 char_u pathbuf[NUMBUFLEN];
9217 int count = 1;
9218 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009219 int error = FALSE;
9220#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009221
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009222 rettv->vval.v_string = NULL;
9223 rettv->v_type = VAR_STRING;
9224
9225#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009226 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009227
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009228 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009229 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009230 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
9231 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009232 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009233 else
9234 {
9235 if (*p != NUL)
9236 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009237
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009238 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009239 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009240 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009241 }
9242
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009243 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
9244 error = TRUE;
9245
9246 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009247 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009248 do
9249 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009250 if (rettv->v_type == VAR_STRING)
9251 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009252 fresult = find_file_in_path_option(first ? fname : NULL,
9253 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar5b6b1ca2007-03-27 08:19:43 +00009254 0, first, path, dir, curbuf->b_ffname,
Bram Moolenaare580b0c2006-03-21 21:33:03 +00009255 dir ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009256 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009257
9258 if (fresult != NULL && rettv->v_type == VAR_LIST)
9259 list_append_string(rettv->vval.v_list, fresult, -1);
9260
9261 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009262 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009263
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009264 if (rettv->v_type == VAR_STRING)
9265 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009266#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009267}
9268
Bram Moolenaar33570922005-01-25 22:26:29 +00009269static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
9270static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009271
9272/*
9273 * Implementation of map() and filter().
9274 */
9275 static void
9276filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00009277 typval_T *argvars;
9278 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009279 int map;
9280{
9281 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00009282 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00009283 listitem_T *li, *nli;
9284 list_T *l = NULL;
9285 dictitem_T *di;
9286 hashtab_T *ht;
9287 hashitem_T *hi;
9288 dict_T *d = NULL;
9289 typval_T save_val;
9290 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009291 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009292 int todo;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009293 char_u *ermsg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009294 int save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009295
9296 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009297 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009298 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009299 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009300 || (map && tv_check_lock(l->lv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009301 return;
9302 }
9303 else if (argvars[0].v_type == VAR_DICT)
9304 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009305 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009306 || (map && tv_check_lock(d->dv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009307 return;
9308 }
9309 else
9310 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009311 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009312 return;
9313 }
9314
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009315 expr = get_tv_string_buf_chk(&argvars[1], buf);
9316 /* On type errors, the preceding call has already displayed an error
9317 * message. Avoid a misleading error message for an empty string that
9318 * was not passed as argument. */
9319 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009320 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009321 prepare_vimvar(VV_VAL, &save_val);
9322 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009323
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009324 /* We reset "did_emsg" to be able to detect whether an error
9325 * occurred during evaluation of the expression. */
9326 save_did_emsg = did_emsg;
9327 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009328
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009329 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009330 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009331 prepare_vimvar(VV_KEY, &save_key);
9332 vimvars[VV_KEY].vv_type = VAR_STRING;
9333
9334 ht = &d->dv_hashtab;
9335 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009336 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009337 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009338 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009339 if (!HASHITEM_EMPTY(hi))
9340 {
9341 --todo;
9342 di = HI2DI(hi);
Bram Moolenaar89d40322006-08-29 15:30:07 +00009343 if (tv_check_lock(di->di_tv.v_lock, ermsg))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009344 break;
9345 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009346 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009347 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009348 break;
9349 if (!map && rem)
9350 dictitem_remove(d, di);
9351 clear_tv(&vimvars[VV_KEY].vv_tv);
9352 }
9353 }
9354 hash_unlock(ht);
9355
9356 restore_vimvar(VV_KEY, &save_key);
9357 }
9358 else
9359 {
9360 for (li = l->lv_first; li != NULL; li = nli)
9361 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009362 if (tv_check_lock(li->li_tv.v_lock, ermsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009363 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009364 nli = li->li_next;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009365 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009366 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009367 break;
9368 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009369 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009370 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009371 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009372
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009373 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009374
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009375 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009376 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009377
9378 copy_tv(&argvars[0], rettv);
9379}
9380
9381 static int
9382filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009383 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009384 char_u *expr;
9385 int map;
9386 int *remp;
9387{
Bram Moolenaar33570922005-01-25 22:26:29 +00009388 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009389 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009390 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009391
Bram Moolenaar33570922005-01-25 22:26:29 +00009392 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009393 s = expr;
9394 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009395 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009396 if (*s != NUL) /* check for trailing chars after expr */
9397 {
9398 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009399 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009400 }
9401 if (map)
9402 {
9403 /* map(): replace the list item value */
9404 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +00009405 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009406 *tv = rettv;
9407 }
9408 else
9409 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009410 int error = FALSE;
9411
Bram Moolenaare9a41262005-01-15 22:18:47 +00009412 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009413 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009414 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009415 /* On type error, nothing has been removed; return FAIL to stop the
9416 * loop. The error message was given by get_tv_number_chk(). */
9417 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009418 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009419 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009420 retval = OK;
9421theend:
Bram Moolenaar33570922005-01-25 22:26:29 +00009422 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009423 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009424}
9425
9426/*
9427 * "filter()" function
9428 */
9429 static void
9430f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009431 typval_T *argvars;
9432 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009433{
9434 filter_map(argvars, rettv, FALSE);
9435}
9436
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009437/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009438 * "finddir({fname}[, {path}[, {count}]])" function
9439 */
9440 static void
9441f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009442 typval_T *argvars;
9443 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009444{
9445 findfilendir(argvars, rettv, TRUE);
9446}
9447
9448/*
9449 * "findfile({fname}[, {path}[, {count}]])" function
9450 */
9451 static void
9452f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009453 typval_T *argvars;
9454 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009455{
9456 findfilendir(argvars, rettv, FALSE);
9457}
9458
9459/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009460 * "fnamemodify({fname}, {mods})" function
9461 */
9462 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009463f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009464 typval_T *argvars;
9465 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009466{
9467 char_u *fname;
9468 char_u *mods;
9469 int usedlen = 0;
9470 int len;
9471 char_u *fbuf = NULL;
9472 char_u buf[NUMBUFLEN];
9473
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009474 fname = get_tv_string_chk(&argvars[0]);
9475 mods = get_tv_string_buf_chk(&argvars[1], buf);
9476 if (fname == NULL || mods == NULL)
9477 fname = NULL;
9478 else
9479 {
9480 len = (int)STRLEN(fname);
9481 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
9482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009484 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009486 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009487 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009488 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009489 vim_free(fbuf);
9490}
9491
Bram Moolenaar33570922005-01-25 22:26:29 +00009492static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009493
9494/*
9495 * "foldclosed()" function
9496 */
9497 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009498foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00009499 typval_T *argvars;
9500 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501 int end;
9502{
9503#ifdef FEAT_FOLDING
9504 linenr_T lnum;
9505 linenr_T first, last;
9506
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009507 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9509 {
9510 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
9511 {
9512 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009513 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009514 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009515 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009516 return;
9517 }
9518 }
9519#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009520 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009521}
9522
9523/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009524 * "foldclosed()" function
9525 */
9526 static void
9527f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009528 typval_T *argvars;
9529 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009530{
9531 foldclosed_both(argvars, rettv, FALSE);
9532}
9533
9534/*
9535 * "foldclosedend()" function
9536 */
9537 static void
9538f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009539 typval_T *argvars;
9540 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009541{
9542 foldclosed_both(argvars, rettv, TRUE);
9543}
9544
9545/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009546 * "foldlevel()" function
9547 */
9548 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009549f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009550 typval_T *argvars;
9551 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009552{
9553#ifdef FEAT_FOLDING
9554 linenr_T lnum;
9555
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009556 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009557 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009558 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009559 else
9560#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009561 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009562}
9563
9564/*
9565 * "foldtext()" function
9566 */
9567/*ARGSUSED*/
9568 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009569f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009570 typval_T *argvars;
9571 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009572{
9573#ifdef FEAT_FOLDING
9574 linenr_T lnum;
9575 char_u *s;
9576 char_u *r;
9577 int len;
9578 char *txt;
9579#endif
9580
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009581 rettv->v_type = VAR_STRING;
9582 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009583#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00009584 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
9585 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
9586 <= curbuf->b_ml.ml_line_count
9587 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009588 {
9589 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009590 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
9591 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009592 {
9593 if (!linewhite(lnum))
9594 break;
9595 ++lnum;
9596 }
9597
9598 /* Find interesting text in this line. */
9599 s = skipwhite(ml_get(lnum));
9600 /* skip C comment-start */
9601 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009602 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009604 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00009605 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009606 {
9607 s = skipwhite(ml_get(lnum + 1));
9608 if (*s == '*')
9609 s = skipwhite(s + 1);
9610 }
9611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009612 txt = _("+-%s%3ld lines: ");
9613 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009614 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009615 + 20 /* for %3ld */
9616 + STRLEN(s))); /* concatenated */
9617 if (r != NULL)
9618 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009619 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
9620 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
9621 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009622 len = (int)STRLEN(r);
9623 STRCAT(r, s);
9624 /* remove 'foldmarker' and 'commentstring' */
9625 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009626 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009627 }
9628 }
9629#endif
9630}
9631
9632/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009633 * "foldtextresult(lnum)" function
9634 */
9635/*ARGSUSED*/
9636 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009637f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009638 typval_T *argvars;
9639 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009640{
9641#ifdef FEAT_FOLDING
9642 linenr_T lnum;
9643 char_u *text;
9644 char_u buf[51];
9645 foldinfo_T foldinfo;
9646 int fold_count;
9647#endif
9648
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009649 rettv->v_type = VAR_STRING;
9650 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009651#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009652 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009653 /* treat illegal types and illegal string values for {lnum} the same */
9654 if (lnum < 0)
9655 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009656 fold_count = foldedCount(curwin, lnum, &foldinfo);
9657 if (fold_count > 0)
9658 {
9659 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
9660 &foldinfo, buf);
9661 if (text == buf)
9662 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009663 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009664 }
9665#endif
9666}
9667
9668/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009669 * "foreground()" function
9670 */
9671/*ARGSUSED*/
9672 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009673f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009674 typval_T *argvars;
9675 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009676{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009677 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009678#ifdef FEAT_GUI
9679 if (gui.in_use)
9680 gui_mch_set_foreground();
9681#else
9682# ifdef WIN32
9683 win32_set_foreground();
9684# endif
9685#endif
9686}
9687
9688/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009689 * "function()" function
9690 */
9691/*ARGSUSED*/
9692 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009693f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009694 typval_T *argvars;
9695 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009696{
9697 char_u *s;
9698
Bram Moolenaara7043832005-01-21 11:56:39 +00009699 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009700 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009701 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009702 EMSG2(_(e_invarg2), s);
9703 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009704 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009705 else
9706 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009707 rettv->vval.v_string = vim_strsave(s);
9708 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009709 }
9710}
9711
9712/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009713 * "garbagecollect()" function
9714 */
9715/*ARGSUSED*/
9716 static void
9717f_garbagecollect(argvars, rettv)
9718 typval_T *argvars;
9719 typval_T *rettv;
9720{
Bram Moolenaar9fecb462006-09-05 10:59:47 +00009721 /* This is postponed until we are back at the toplevel, because we may be
9722 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
9723 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00009724
9725 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
9726 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009727}
9728
9729/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009730 * "get()" function
9731 */
9732 static void
9733f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009734 typval_T *argvars;
9735 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009736{
Bram Moolenaar33570922005-01-25 22:26:29 +00009737 listitem_T *li;
9738 list_T *l;
9739 dictitem_T *di;
9740 dict_T *d;
9741 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009742
Bram Moolenaare9a41262005-01-15 22:18:47 +00009743 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009744 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009745 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009746 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009747 int error = FALSE;
9748
9749 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9750 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009751 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009752 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009753 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009754 else if (argvars[0].v_type == VAR_DICT)
9755 {
9756 if ((d = argvars[0].vval.v_dict) != NULL)
9757 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009758 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009759 if (di != NULL)
9760 tv = &di->di_tv;
9761 }
9762 }
9763 else
9764 EMSG2(_(e_listdictarg), "get()");
9765
9766 if (tv == NULL)
9767 {
9768 if (argvars[2].v_type == VAR_UNKNOWN)
9769 rettv->vval.v_number = 0;
9770 else
9771 copy_tv(&argvars[2], rettv);
9772 }
9773 else
9774 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009775}
9776
Bram Moolenaar342337a2005-07-21 21:11:17 +00009777static 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 +00009778
9779/*
9780 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +00009781 * Return a range (from start to end) of lines in rettv from the specified
9782 * buffer.
9783 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009784 */
9785 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +00009786get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009787 buf_T *buf;
9788 linenr_T start;
9789 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009790 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009791 typval_T *rettv;
9792{
9793 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009794
Bram Moolenaar342337a2005-07-21 21:11:17 +00009795 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009796 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009797 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009798 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009799 }
9800 else
9801 rettv->vval.v_number = 0;
9802
9803 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
9804 return;
9805
9806 if (!retlist)
9807 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009808 if (start >= 1 && start <= buf->b_ml.ml_line_count)
9809 p = ml_get_buf(buf, start, FALSE);
9810 else
9811 p = (char_u *)"";
9812
9813 rettv->v_type = VAR_STRING;
9814 rettv->vval.v_string = vim_strsave(p);
9815 }
9816 else
9817 {
9818 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009819 return;
9820
9821 if (start < 1)
9822 start = 1;
9823 if (end > buf->b_ml.ml_line_count)
9824 end = buf->b_ml.ml_line_count;
9825 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009826 if (list_append_string(rettv->vval.v_list,
9827 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009828 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009829 }
9830}
9831
9832/*
9833 * "getbufline()" function
9834 */
9835 static void
9836f_getbufline(argvars, rettv)
9837 typval_T *argvars;
9838 typval_T *rettv;
9839{
9840 linenr_T lnum;
9841 linenr_T end;
9842 buf_T *buf;
9843
9844 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9845 ++emsg_off;
9846 buf = get_buf_tv(&argvars[0]);
9847 --emsg_off;
9848
Bram Moolenaar661b1822005-07-28 22:36:45 +00009849 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009850 if (argvars[2].v_type == VAR_UNKNOWN)
9851 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009852 else
Bram Moolenaar661b1822005-07-28 22:36:45 +00009853 end = get_tv_lnum_buf(&argvars[2], buf);
9854
Bram Moolenaar342337a2005-07-21 21:11:17 +00009855 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009856}
9857
Bram Moolenaar0d660222005-01-07 21:51:51 +00009858/*
9859 * "getbufvar()" function
9860 */
9861 static void
9862f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009863 typval_T *argvars;
9864 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009865{
9866 buf_T *buf;
9867 buf_T *save_curbuf;
9868 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009869 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009870
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009871 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9872 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009873 ++emsg_off;
9874 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009875
9876 rettv->v_type = VAR_STRING;
9877 rettv->vval.v_string = NULL;
9878
9879 if (buf != NULL && varname != NULL)
9880 {
9881 if (*varname == '&') /* buffer-local-option */
9882 {
9883 /* set curbuf to be our buf, temporarily */
9884 save_curbuf = curbuf;
9885 curbuf = buf;
9886
9887 get_option_tv(&varname, rettv, TRUE);
9888
9889 /* restore previous notion of curbuf */
9890 curbuf = save_curbuf;
9891 }
9892 else
9893 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009894 if (*varname == NUL)
9895 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9896 * scope prefix before the NUL byte is required by
9897 * find_var_in_ht(). */
9898 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009899 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009900 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009901 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009902 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009903 }
9904 }
9905
9906 --emsg_off;
9907}
9908
9909/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009910 * "getchar()" function
9911 */
9912 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009913f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009914 typval_T *argvars;
9915 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009916{
9917 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009918 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009919
Bram Moolenaar4015b2c2006-06-22 19:01:34 +00009920 /* Position the cursor. Needed after a message that ends in a space. */
9921 windgoto(msg_row, msg_col);
9922
Bram Moolenaar071d4272004-06-13 20:20:40 +00009923 ++no_mapping;
9924 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +00009925 for (;;)
9926 {
9927 if (argvars[0].v_type == VAR_UNKNOWN)
9928 /* getchar(): blocking wait. */
9929 n = safe_vgetc();
9930 else if (get_tv_number_chk(&argvars[0], &error) == 1)
9931 /* getchar(1): only check if char avail */
9932 n = vpeekc();
9933 else if (error || vpeekc() == NUL)
9934 /* illegal argument or getchar(0) and no char avail: return zero */
9935 n = 0;
9936 else
9937 /* getchar(0) and char avail: return char */
9938 n = safe_vgetc();
9939 if (n == K_IGNORE)
9940 continue;
9941 break;
9942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009943 --no_mapping;
9944 --allow_keys;
9945
Bram Moolenaar219b8702006-11-01 14:32:36 +00009946 vimvars[VV_MOUSE_WIN].vv_nr = 0;
9947 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
9948 vimvars[VV_MOUSE_COL].vv_nr = 0;
9949
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009950 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009951 if (IS_SPECIAL(n) || mod_mask != 0)
9952 {
9953 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9954 int i = 0;
9955
9956 /* Turn a special key into three bytes, plus modifier. */
9957 if (mod_mask != 0)
9958 {
9959 temp[i++] = K_SPECIAL;
9960 temp[i++] = KS_MODIFIER;
9961 temp[i++] = mod_mask;
9962 }
9963 if (IS_SPECIAL(n))
9964 {
9965 temp[i++] = K_SPECIAL;
9966 temp[i++] = K_SECOND(n);
9967 temp[i++] = K_THIRD(n);
9968 }
9969#ifdef FEAT_MBYTE
9970 else if (has_mbyte)
9971 i += (*mb_char2bytes)(n, temp + i);
9972#endif
9973 else
9974 temp[i++] = n;
9975 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009976 rettv->v_type = VAR_STRING;
9977 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +00009978
9979#ifdef FEAT_MOUSE
9980 if (n == K_LEFTMOUSE
9981 || n == K_LEFTMOUSE_NM
9982 || n == K_LEFTDRAG
9983 || n == K_LEFTRELEASE
9984 || n == K_LEFTRELEASE_NM
9985 || n == K_MIDDLEMOUSE
9986 || n == K_MIDDLEDRAG
9987 || n == K_MIDDLERELEASE
9988 || n == K_RIGHTMOUSE
9989 || n == K_RIGHTDRAG
9990 || n == K_RIGHTRELEASE
9991 || n == K_X1MOUSE
9992 || n == K_X1DRAG
9993 || n == K_X1RELEASE
9994 || n == K_X2MOUSE
9995 || n == K_X2DRAG
9996 || n == K_X2RELEASE
9997 || n == K_MOUSEDOWN
9998 || n == K_MOUSEUP)
9999 {
10000 int row = mouse_row;
10001 int col = mouse_col;
10002 win_T *win;
10003 linenr_T lnum;
10004# ifdef FEAT_WINDOWS
10005 win_T *wp;
10006# endif
10007 int n = 1;
10008
10009 if (row >= 0 && col >= 0)
10010 {
10011 /* Find the window at the mouse coordinates and compute the
10012 * text position. */
10013 win = mouse_find_win(&row, &col);
10014 (void)mouse_comp_pos(win, &row, &col, &lnum);
10015# ifdef FEAT_WINDOWS
10016 for (wp = firstwin; wp != win; wp = wp->w_next)
10017 ++n;
10018# endif
10019 vimvars[VV_MOUSE_WIN].vv_nr = n;
10020 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
10021 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
10022 }
10023 }
10024#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010025 }
10026}
10027
10028/*
10029 * "getcharmod()" function
10030 */
10031/*ARGSUSED*/
10032 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010033f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010034 typval_T *argvars;
10035 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010036{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010037 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010038}
10039
10040/*
10041 * "getcmdline()" function
10042 */
10043/*ARGSUSED*/
10044 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010045f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010046 typval_T *argvars;
10047 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010048{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010049 rettv->v_type = VAR_STRING;
10050 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010051}
10052
10053/*
10054 * "getcmdpos()" function
10055 */
10056/*ARGSUSED*/
10057 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010058f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010059 typval_T *argvars;
10060 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010061{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010062 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010063}
10064
10065/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010066 * "getcmdtype()" function
10067 */
10068/*ARGSUSED*/
10069 static void
10070f_getcmdtype(argvars, rettv)
10071 typval_T *argvars;
10072 typval_T *rettv;
10073{
10074 rettv->v_type = VAR_STRING;
10075 rettv->vval.v_string = alloc(2);
10076 if (rettv->vval.v_string != NULL)
10077 {
10078 rettv->vval.v_string[0] = get_cmdline_type();
10079 rettv->vval.v_string[1] = NUL;
10080 }
10081}
10082
10083/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010084 * "getcwd()" function
10085 */
10086/*ARGSUSED*/
10087 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010088f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010089 typval_T *argvars;
10090 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010091{
10092 char_u cwd[MAXPATHL];
10093
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010094 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010095 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010096 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010097 else
10098 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010099 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010100#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +000010101 if (rettv->vval.v_string != NULL)
10102 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010103#endif
10104 }
10105}
10106
10107/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010108 * "getfontname()" function
10109 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010110/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010111 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010112f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010113 typval_T *argvars;
10114 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010115{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010116 rettv->v_type = VAR_STRING;
10117 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010118#ifdef FEAT_GUI
10119 if (gui.in_use)
10120 {
10121 GuiFont font;
10122 char_u *name = NULL;
10123
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010124 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010125 {
10126 /* Get the "Normal" font. Either the name saved by
10127 * hl_set_font_name() or from the font ID. */
10128 font = gui.norm_font;
10129 name = hl_get_font_name();
10130 }
10131 else
10132 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010133 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010134 if (STRCMP(name, "*") == 0) /* don't use font dialog */
10135 return;
10136 font = gui_mch_get_font(name, FALSE);
10137 if (font == NOFONT)
10138 return; /* Invalid font name, return empty string. */
10139 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010140 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010141 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010142 gui_mch_free_font(font);
10143 }
10144#endif
10145}
10146
10147/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010148 * "getfperm({fname})" function
10149 */
10150 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010151f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010152 typval_T *argvars;
10153 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010154{
10155 char_u *fname;
10156 struct stat st;
10157 char_u *perm = NULL;
10158 char_u flags[] = "rwx";
10159 int i;
10160
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010161 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010162
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010163 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010164 if (mch_stat((char *)fname, &st) >= 0)
10165 {
10166 perm = vim_strsave((char_u *)"---------");
10167 if (perm != NULL)
10168 {
10169 for (i = 0; i < 9; i++)
10170 {
10171 if (st.st_mode & (1 << (8 - i)))
10172 perm[i] = flags[i % 3];
10173 }
10174 }
10175 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010176 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010177}
10178
10179/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010180 * "getfsize({fname})" function
10181 */
10182 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010183f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010184 typval_T *argvars;
10185 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010186{
10187 char_u *fname;
10188 struct stat st;
10189
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010190 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010191
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010192 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010193
10194 if (mch_stat((char *)fname, &st) >= 0)
10195 {
10196 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010197 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010198 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000010199 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010200 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000010201
10202 /* non-perfect check for overflow */
10203 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
10204 rettv->vval.v_number = -2;
10205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010206 }
10207 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010208 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010209}
10210
10211/*
10212 * "getftime({fname})" function
10213 */
10214 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010215f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010216 typval_T *argvars;
10217 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010218{
10219 char_u *fname;
10220 struct stat st;
10221
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010222 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010223
10224 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010225 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010226 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010227 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010228}
10229
10230/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010231 * "getftype({fname})" function
10232 */
10233 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010234f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010235 typval_T *argvars;
10236 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010237{
10238 char_u *fname;
10239 struct stat st;
10240 char_u *type = NULL;
10241 char *t;
10242
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010243 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010244
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010245 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010246 if (mch_lstat((char *)fname, &st) >= 0)
10247 {
10248#ifdef S_ISREG
10249 if (S_ISREG(st.st_mode))
10250 t = "file";
10251 else if (S_ISDIR(st.st_mode))
10252 t = "dir";
10253# ifdef S_ISLNK
10254 else if (S_ISLNK(st.st_mode))
10255 t = "link";
10256# endif
10257# ifdef S_ISBLK
10258 else if (S_ISBLK(st.st_mode))
10259 t = "bdev";
10260# endif
10261# ifdef S_ISCHR
10262 else if (S_ISCHR(st.st_mode))
10263 t = "cdev";
10264# endif
10265# ifdef S_ISFIFO
10266 else if (S_ISFIFO(st.st_mode))
10267 t = "fifo";
10268# endif
10269# ifdef S_ISSOCK
10270 else if (S_ISSOCK(st.st_mode))
10271 t = "fifo";
10272# endif
10273 else
10274 t = "other";
10275#else
10276# ifdef S_IFMT
10277 switch (st.st_mode & S_IFMT)
10278 {
10279 case S_IFREG: t = "file"; break;
10280 case S_IFDIR: t = "dir"; break;
10281# ifdef S_IFLNK
10282 case S_IFLNK: t = "link"; break;
10283# endif
10284# ifdef S_IFBLK
10285 case S_IFBLK: t = "bdev"; break;
10286# endif
10287# ifdef S_IFCHR
10288 case S_IFCHR: t = "cdev"; break;
10289# endif
10290# ifdef S_IFIFO
10291 case S_IFIFO: t = "fifo"; break;
10292# endif
10293# ifdef S_IFSOCK
10294 case S_IFSOCK: t = "socket"; break;
10295# endif
10296 default: t = "other";
10297 }
10298# else
10299 if (mch_isdir(fname))
10300 t = "dir";
10301 else
10302 t = "file";
10303# endif
10304#endif
10305 type = vim_strsave((char_u *)t);
10306 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010307 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010308}
10309
10310/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010311 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000010312 */
10313 static void
10314f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010315 typval_T *argvars;
10316 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010317{
10318 linenr_T lnum;
10319 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010320 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010321
10322 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010323 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010324 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010325 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010326 retlist = FALSE;
10327 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010328 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000010329 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000010330 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010331 retlist = TRUE;
10332 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010333
Bram Moolenaar342337a2005-07-21 21:11:17 +000010334 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010335}
10336
10337/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010338 * "getmatches()" function
10339 */
10340/*ARGSUSED*/
10341 static void
10342f_getmatches(argvars, rettv)
10343 typval_T *argvars;
10344 typval_T *rettv;
10345{
10346#ifdef FEAT_SEARCH_EXTRA
10347 dict_T *dict;
10348 matchitem_T *cur = curwin->w_match_head;
10349
10350 rettv->vval.v_number = 0;
10351
10352 if (rettv_list_alloc(rettv) == OK)
10353 {
10354 while (cur != NULL)
10355 {
10356 dict = dict_alloc();
10357 if (dict == NULL)
10358 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010359 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
10360 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
10361 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
10362 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
10363 list_append_dict(rettv->vval.v_list, dict);
10364 cur = cur->next;
10365 }
10366 }
10367#endif
10368}
10369
10370/*
Bram Moolenaara5525202006-03-02 22:52:09 +000010371 * "getpos(string)" function
10372 */
10373 static void
10374f_getpos(argvars, rettv)
10375 typval_T *argvars;
10376 typval_T *rettv;
10377{
10378 pos_T *fp;
10379 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010380 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010381
10382 if (rettv_list_alloc(rettv) == OK)
10383 {
10384 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010385 fp = var2fpos(&argvars[0], TRUE, &fnum);
10386 if (fnum != -1)
10387 list_append_number(l, (varnumber_T)fnum);
10388 else
10389 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000010390 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
10391 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000010392 list_append_number(l, (fp != NULL)
10393 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000010394 : (varnumber_T)0);
10395 list_append_number(l,
10396#ifdef FEAT_VIRTUALEDIT
10397 (fp != NULL) ? (varnumber_T)fp->coladd :
10398#endif
10399 (varnumber_T)0);
10400 }
10401 else
10402 rettv->vval.v_number = FALSE;
10403}
10404
10405/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000010406 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000010407 */
Bram Moolenaar280f1262006-01-30 00:14:18 +000010408/*ARGSUSED*/
Bram Moolenaar2641f772005-03-25 21:58:17 +000010409 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000010410f_getqflist(argvars, rettv)
10411 typval_T *argvars;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010412 typval_T *rettv;
10413{
10414#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000010415 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010416#endif
10417
Bram Moolenaar77f66d62007-02-20 02:16:18 +000010418 rettv->vval.v_number = 0;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010419#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010420 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000010421 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000010422 wp = NULL;
10423 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
10424 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010425 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010426 if (wp == NULL)
10427 return;
10428 }
10429
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010430 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000010431 }
10432#endif
10433}
10434
10435/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010436 * "getreg()" function
10437 */
10438 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010439f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010440 typval_T *argvars;
10441 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010442{
10443 char_u *strregname;
10444 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010445 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010446 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010447
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010448 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010449 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010450 strregname = get_tv_string_chk(&argvars[0]);
10451 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010452 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010453 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010454 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010455 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010456 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010457 regname = (strregname == NULL ? '"' : *strregname);
10458 if (regname == 0)
10459 regname = '"';
10460
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010461 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010462 rettv->vval.v_string = error ? NULL :
10463 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010464}
10465
10466/*
10467 * "getregtype()" function
10468 */
10469 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010470f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010471 typval_T *argvars;
10472 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010473{
10474 char_u *strregname;
10475 int regname;
10476 char_u buf[NUMBUFLEN + 2];
10477 long reglen = 0;
10478
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010479 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010480 {
10481 strregname = get_tv_string_chk(&argvars[0]);
10482 if (strregname == NULL) /* type error; errmsg already given */
10483 {
10484 rettv->v_type = VAR_STRING;
10485 rettv->vval.v_string = NULL;
10486 return;
10487 }
10488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010489 else
10490 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010491 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010492
10493 regname = (strregname == NULL ? '"' : *strregname);
10494 if (regname == 0)
10495 regname = '"';
10496
10497 buf[0] = NUL;
10498 buf[1] = NUL;
10499 switch (get_reg_type(regname, &reglen))
10500 {
10501 case MLINE: buf[0] = 'V'; break;
10502 case MCHAR: buf[0] = 'v'; break;
10503#ifdef FEAT_VISUAL
10504 case MBLOCK:
10505 buf[0] = Ctrl_V;
10506 sprintf((char *)buf + 1, "%ld", reglen + 1);
10507 break;
10508#endif
10509 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010510 rettv->v_type = VAR_STRING;
10511 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010512}
10513
10514/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010515 * "gettabwinvar()" function
10516 */
10517 static void
10518f_gettabwinvar(argvars, rettv)
10519 typval_T *argvars;
10520 typval_T *rettv;
10521{
10522 getwinvar(argvars, rettv, 1);
10523}
10524
10525/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010526 * "getwinposx()" function
10527 */
10528/*ARGSUSED*/
10529 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010530f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010531 typval_T *argvars;
10532 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010533{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010534 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010535#ifdef FEAT_GUI
10536 if (gui.in_use)
10537 {
10538 int x, y;
10539
10540 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010541 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010542 }
10543#endif
10544}
10545
10546/*
10547 * "getwinposy()" function
10548 */
10549/*ARGSUSED*/
10550 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010551f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010552 typval_T *argvars;
10553 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010554{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010555 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010556#ifdef FEAT_GUI
10557 if (gui.in_use)
10558 {
10559 int x, y;
10560
10561 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010562 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010563 }
10564#endif
10565}
10566
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010567/*
10568 * Find window specifed by "vp" in tabpage "tp".
10569 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000010570 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010571find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000010572 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010573 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000010574{
10575#ifdef FEAT_WINDOWS
10576 win_T *wp;
10577#endif
10578 int nr;
10579
10580 nr = get_tv_number_chk(vp, NULL);
10581
10582#ifdef FEAT_WINDOWS
10583 if (nr < 0)
10584 return NULL;
10585 if (nr == 0)
10586 return curwin;
10587
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010588 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
10589 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000010590 if (--nr <= 0)
10591 break;
10592 return wp;
10593#else
10594 if (nr == 0 || nr == 1)
10595 return curwin;
10596 return NULL;
10597#endif
10598}
10599
Bram Moolenaar071d4272004-06-13 20:20:40 +000010600/*
10601 * "getwinvar()" function
10602 */
10603 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010604f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010605 typval_T *argvars;
10606 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010607{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010608 getwinvar(argvars, rettv, 0);
10609}
10610
10611/*
10612 * getwinvar() and gettabwinvar()
10613 */
10614 static void
10615getwinvar(argvars, rettv, off)
10616 typval_T *argvars;
10617 typval_T *rettv;
10618 int off; /* 1 for gettabwinvar() */
10619{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010620 win_T *win, *oldcurwin;
10621 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010622 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010623 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010624
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010625#ifdef FEAT_WINDOWS
10626 if (off == 1)
10627 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
10628 else
10629 tp = curtab;
10630#endif
10631 win = find_win_by_nr(&argvars[off], tp);
10632 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010633 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010634
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010635 rettv->v_type = VAR_STRING;
10636 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010637
10638 if (win != NULL && varname != NULL)
10639 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000010640 /* Set curwin to be our win, temporarily. Also set curbuf, so
10641 * that we can get buffer-local options. */
10642 oldcurwin = curwin;
10643 curwin = win;
10644 curbuf = win->w_buffer;
10645
Bram Moolenaar071d4272004-06-13 20:20:40 +000010646 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010647 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010648 else
10649 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010650 if (*varname == NUL)
10651 /* let getwinvar({nr}, "") return the "w:" dictionary. The
10652 * scope prefix before the NUL byte is required by
10653 * find_var_in_ht(). */
10654 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010655 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010656 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010657 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010658 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010659 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000010660
10661 /* restore previous notion of curwin */
10662 curwin = oldcurwin;
10663 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010664 }
10665
10666 --emsg_off;
10667}
10668
10669/*
10670 * "glob()" function
10671 */
10672 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010673f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010674 typval_T *argvars;
10675 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010676{
10677 expand_T xpc;
10678
10679 ExpandInit(&xpc);
10680 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010681 rettv->v_type = VAR_STRING;
10682 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010683 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010684}
10685
10686/*
10687 * "globpath()" function
10688 */
10689 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010690f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010691 typval_T *argvars;
10692 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010693{
10694 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010695 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010696
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010697 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010698 if (file == NULL)
10699 rettv->vval.v_string = NULL;
10700 else
10701 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010702}
10703
10704/*
10705 * "has()" function
10706 */
10707 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010708f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010709 typval_T *argvars;
10710 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010711{
10712 int i;
10713 char_u *name;
10714 int n = FALSE;
10715 static char *(has_list[]) =
10716 {
10717#ifdef AMIGA
10718 "amiga",
10719# ifdef FEAT_ARP
10720 "arp",
10721# endif
10722#endif
10723#ifdef __BEOS__
10724 "beos",
10725#endif
10726#ifdef MSDOS
10727# ifdef DJGPP
10728 "dos32",
10729# else
10730 "dos16",
10731# endif
10732#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000010733#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010734 "mac",
10735#endif
10736#if defined(MACOS_X_UNIX)
10737 "macunix",
10738#endif
10739#ifdef OS2
10740 "os2",
10741#endif
10742#ifdef __QNX__
10743 "qnx",
10744#endif
10745#ifdef RISCOS
10746 "riscos",
10747#endif
10748#ifdef UNIX
10749 "unix",
10750#endif
10751#ifdef VMS
10752 "vms",
10753#endif
10754#ifdef WIN16
10755 "win16",
10756#endif
10757#ifdef WIN32
10758 "win32",
10759#endif
10760#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
10761 "win32unix",
10762#endif
10763#ifdef WIN64
10764 "win64",
10765#endif
10766#ifdef EBCDIC
10767 "ebcdic",
10768#endif
10769#ifndef CASE_INSENSITIVE_FILENAME
10770 "fname_case",
10771#endif
10772#ifdef FEAT_ARABIC
10773 "arabic",
10774#endif
10775#ifdef FEAT_AUTOCMD
10776 "autocmd",
10777#endif
10778#ifdef FEAT_BEVAL
10779 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000010780# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
10781 "balloon_multiline",
10782# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010783#endif
10784#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
10785 "builtin_terms",
10786# ifdef ALL_BUILTIN_TCAPS
10787 "all_builtin_terms",
10788# endif
10789#endif
10790#ifdef FEAT_BYTEOFF
10791 "byte_offset",
10792#endif
10793#ifdef FEAT_CINDENT
10794 "cindent",
10795#endif
10796#ifdef FEAT_CLIENTSERVER
10797 "clientserver",
10798#endif
10799#ifdef FEAT_CLIPBOARD
10800 "clipboard",
10801#endif
10802#ifdef FEAT_CMDL_COMPL
10803 "cmdline_compl",
10804#endif
10805#ifdef FEAT_CMDHIST
10806 "cmdline_hist",
10807#endif
10808#ifdef FEAT_COMMENTS
10809 "comments",
10810#endif
10811#ifdef FEAT_CRYPT
10812 "cryptv",
10813#endif
10814#ifdef FEAT_CSCOPE
10815 "cscope",
10816#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010817#ifdef CURSOR_SHAPE
10818 "cursorshape",
10819#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010820#ifdef DEBUG
10821 "debug",
10822#endif
10823#ifdef FEAT_CON_DIALOG
10824 "dialog_con",
10825#endif
10826#ifdef FEAT_GUI_DIALOG
10827 "dialog_gui",
10828#endif
10829#ifdef FEAT_DIFF
10830 "diff",
10831#endif
10832#ifdef FEAT_DIGRAPHS
10833 "digraphs",
10834#endif
10835#ifdef FEAT_DND
10836 "dnd",
10837#endif
10838#ifdef FEAT_EMACS_TAGS
10839 "emacs_tags",
10840#endif
10841 "eval", /* always present, of course! */
10842#ifdef FEAT_EX_EXTRA
10843 "ex_extra",
10844#endif
10845#ifdef FEAT_SEARCH_EXTRA
10846 "extra_search",
10847#endif
10848#ifdef FEAT_FKMAP
10849 "farsi",
10850#endif
10851#ifdef FEAT_SEARCHPATH
10852 "file_in_path",
10853#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010854#if defined(UNIX) && !defined(USE_SYSTEM)
10855 "filterpipe",
10856#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010857#ifdef FEAT_FIND_ID
10858 "find_in_path",
10859#endif
10860#ifdef FEAT_FOLDING
10861 "folding",
10862#endif
10863#ifdef FEAT_FOOTER
10864 "footer",
10865#endif
10866#if !defined(USE_SYSTEM) && defined(UNIX)
10867 "fork",
10868#endif
10869#ifdef FEAT_GETTEXT
10870 "gettext",
10871#endif
10872#ifdef FEAT_GUI
10873 "gui",
10874#endif
10875#ifdef FEAT_GUI_ATHENA
10876# ifdef FEAT_GUI_NEXTAW
10877 "gui_neXtaw",
10878# else
10879 "gui_athena",
10880# endif
10881#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010882#ifdef FEAT_GUI_GTK
10883 "gui_gtk",
10884# ifdef HAVE_GTK2
10885 "gui_gtk2",
10886# endif
10887#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000010888#ifdef FEAT_GUI_GNOME
10889 "gui_gnome",
10890#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010891#ifdef FEAT_GUI_MAC
10892 "gui_mac",
10893#endif
10894#ifdef FEAT_GUI_MOTIF
10895 "gui_motif",
10896#endif
10897#ifdef FEAT_GUI_PHOTON
10898 "gui_photon",
10899#endif
10900#ifdef FEAT_GUI_W16
10901 "gui_win16",
10902#endif
10903#ifdef FEAT_GUI_W32
10904 "gui_win32",
10905#endif
10906#ifdef FEAT_HANGULIN
10907 "hangul_input",
10908#endif
10909#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10910 "iconv",
10911#endif
10912#ifdef FEAT_INS_EXPAND
10913 "insert_expand",
10914#endif
10915#ifdef FEAT_JUMPLIST
10916 "jumplist",
10917#endif
10918#ifdef FEAT_KEYMAP
10919 "keymap",
10920#endif
10921#ifdef FEAT_LANGMAP
10922 "langmap",
10923#endif
10924#ifdef FEAT_LIBCALL
10925 "libcall",
10926#endif
10927#ifdef FEAT_LINEBREAK
10928 "linebreak",
10929#endif
10930#ifdef FEAT_LISP
10931 "lispindent",
10932#endif
10933#ifdef FEAT_LISTCMDS
10934 "listcmds",
10935#endif
10936#ifdef FEAT_LOCALMAP
10937 "localmap",
10938#endif
10939#ifdef FEAT_MENU
10940 "menu",
10941#endif
10942#ifdef FEAT_SESSION
10943 "mksession",
10944#endif
10945#ifdef FEAT_MODIFY_FNAME
10946 "modify_fname",
10947#endif
10948#ifdef FEAT_MOUSE
10949 "mouse",
10950#endif
10951#ifdef FEAT_MOUSESHAPE
10952 "mouseshape",
10953#endif
10954#if defined(UNIX) || defined(VMS)
10955# ifdef FEAT_MOUSE_DEC
10956 "mouse_dec",
10957# endif
10958# ifdef FEAT_MOUSE_GPM
10959 "mouse_gpm",
10960# endif
10961# ifdef FEAT_MOUSE_JSB
10962 "mouse_jsbterm",
10963# endif
10964# ifdef FEAT_MOUSE_NET
10965 "mouse_netterm",
10966# endif
10967# ifdef FEAT_MOUSE_PTERM
10968 "mouse_pterm",
10969# endif
10970# ifdef FEAT_MOUSE_XTERM
10971 "mouse_xterm",
10972# endif
10973#endif
10974#ifdef FEAT_MBYTE
10975 "multi_byte",
10976#endif
10977#ifdef FEAT_MBYTE_IME
10978 "multi_byte_ime",
10979#endif
10980#ifdef FEAT_MULTI_LANG
10981 "multi_lang",
10982#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010983#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010984#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010985 "mzscheme",
10986#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010987#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010988#ifdef FEAT_OLE
10989 "ole",
10990#endif
10991#ifdef FEAT_OSFILETYPE
10992 "osfiletype",
10993#endif
10994#ifdef FEAT_PATH_EXTRA
10995 "path_extra",
10996#endif
10997#ifdef FEAT_PERL
10998#ifndef DYNAMIC_PERL
10999 "perl",
11000#endif
11001#endif
11002#ifdef FEAT_PYTHON
11003#ifndef DYNAMIC_PYTHON
11004 "python",
11005#endif
11006#endif
11007#ifdef FEAT_POSTSCRIPT
11008 "postscript",
11009#endif
11010#ifdef FEAT_PRINTER
11011 "printer",
11012#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000011013#ifdef FEAT_PROFILE
11014 "profile",
11015#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000011016#ifdef FEAT_RELTIME
11017 "reltime",
11018#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011019#ifdef FEAT_QUICKFIX
11020 "quickfix",
11021#endif
11022#ifdef FEAT_RIGHTLEFT
11023 "rightleft",
11024#endif
11025#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
11026 "ruby",
11027#endif
11028#ifdef FEAT_SCROLLBIND
11029 "scrollbind",
11030#endif
11031#ifdef FEAT_CMDL_INFO
11032 "showcmd",
11033 "cmdline_info",
11034#endif
11035#ifdef FEAT_SIGNS
11036 "signs",
11037#endif
11038#ifdef FEAT_SMARTINDENT
11039 "smartindent",
11040#endif
11041#ifdef FEAT_SNIFF
11042 "sniff",
11043#endif
11044#ifdef FEAT_STL_OPT
11045 "statusline",
11046#endif
11047#ifdef FEAT_SUN_WORKSHOP
11048 "sun_workshop",
11049#endif
11050#ifdef FEAT_NETBEANS_INTG
11051 "netbeans_intg",
11052#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000011053#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011054 "spell",
11055#endif
11056#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011057 "syntax",
11058#endif
11059#if defined(USE_SYSTEM) || !defined(UNIX)
11060 "system",
11061#endif
11062#ifdef FEAT_TAG_BINS
11063 "tag_binary",
11064#endif
11065#ifdef FEAT_TAG_OLDSTATIC
11066 "tag_old_static",
11067#endif
11068#ifdef FEAT_TAG_ANYWHITE
11069 "tag_any_white",
11070#endif
11071#ifdef FEAT_TCL
11072# ifndef DYNAMIC_TCL
11073 "tcl",
11074# endif
11075#endif
11076#ifdef TERMINFO
11077 "terminfo",
11078#endif
11079#ifdef FEAT_TERMRESPONSE
11080 "termresponse",
11081#endif
11082#ifdef FEAT_TEXTOBJ
11083 "textobjects",
11084#endif
11085#ifdef HAVE_TGETENT
11086 "tgetent",
11087#endif
11088#ifdef FEAT_TITLE
11089 "title",
11090#endif
11091#ifdef FEAT_TOOLBAR
11092 "toolbar",
11093#endif
11094#ifdef FEAT_USR_CMDS
11095 "user-commands", /* was accidentally included in 5.4 */
11096 "user_commands",
11097#endif
11098#ifdef FEAT_VIMINFO
11099 "viminfo",
11100#endif
11101#ifdef FEAT_VERTSPLIT
11102 "vertsplit",
11103#endif
11104#ifdef FEAT_VIRTUALEDIT
11105 "virtualedit",
11106#endif
11107#ifdef FEAT_VISUAL
11108 "visual",
11109#endif
11110#ifdef FEAT_VISUALEXTRA
11111 "visualextra",
11112#endif
11113#ifdef FEAT_VREPLACE
11114 "vreplace",
11115#endif
11116#ifdef FEAT_WILDIGN
11117 "wildignore",
11118#endif
11119#ifdef FEAT_WILDMENU
11120 "wildmenu",
11121#endif
11122#ifdef FEAT_WINDOWS
11123 "windows",
11124#endif
11125#ifdef FEAT_WAK
11126 "winaltkeys",
11127#endif
11128#ifdef FEAT_WRITEBACKUP
11129 "writebackup",
11130#endif
11131#ifdef FEAT_XIM
11132 "xim",
11133#endif
11134#ifdef FEAT_XFONTSET
11135 "xfontset",
11136#endif
11137#ifdef USE_XSMP
11138 "xsmp",
11139#endif
11140#ifdef USE_XSMP_INTERACT
11141 "xsmp_interact",
11142#endif
11143#ifdef FEAT_XCLIPBOARD
11144 "xterm_clipboard",
11145#endif
11146#ifdef FEAT_XTERM_SAVE
11147 "xterm_save",
11148#endif
11149#if defined(UNIX) && defined(FEAT_X11)
11150 "X11",
11151#endif
11152 NULL
11153 };
11154
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011155 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011156 for (i = 0; has_list[i] != NULL; ++i)
11157 if (STRICMP(name, has_list[i]) == 0)
11158 {
11159 n = TRUE;
11160 break;
11161 }
11162
11163 if (n == FALSE)
11164 {
11165 if (STRNICMP(name, "patch", 5) == 0)
11166 n = has_patch(atoi((char *)name + 5));
11167 else if (STRICMP(name, "vim_starting") == 0)
11168 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011169#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
11170 else if (STRICMP(name, "balloon_multiline") == 0)
11171 n = multiline_balloon_available();
11172#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011173#ifdef DYNAMIC_TCL
11174 else if (STRICMP(name, "tcl") == 0)
11175 n = tcl_enabled(FALSE);
11176#endif
11177#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
11178 else if (STRICMP(name, "iconv") == 0)
11179 n = iconv_enabled(FALSE);
11180#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000011181#ifdef DYNAMIC_MZSCHEME
11182 else if (STRICMP(name, "mzscheme") == 0)
11183 n = mzscheme_enabled(FALSE);
11184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011185#ifdef DYNAMIC_RUBY
11186 else if (STRICMP(name, "ruby") == 0)
11187 n = ruby_enabled(FALSE);
11188#endif
11189#ifdef DYNAMIC_PYTHON
11190 else if (STRICMP(name, "python") == 0)
11191 n = python_enabled(FALSE);
11192#endif
11193#ifdef DYNAMIC_PERL
11194 else if (STRICMP(name, "perl") == 0)
11195 n = perl_enabled(FALSE);
11196#endif
11197#ifdef FEAT_GUI
11198 else if (STRICMP(name, "gui_running") == 0)
11199 n = (gui.in_use || gui.starting);
11200# ifdef FEAT_GUI_W32
11201 else if (STRICMP(name, "gui_win32s") == 0)
11202 n = gui_is_win32s();
11203# endif
11204# ifdef FEAT_BROWSE
11205 else if (STRICMP(name, "browse") == 0)
11206 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
11207# endif
11208#endif
11209#ifdef FEAT_SYN_HL
11210 else if (STRICMP(name, "syntax_items") == 0)
11211 n = syntax_present(curbuf);
11212#endif
11213#if defined(WIN3264)
11214 else if (STRICMP(name, "win95") == 0)
11215 n = mch_windows95();
11216#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000011217#ifdef FEAT_NETBEANS_INTG
11218 else if (STRICMP(name, "netbeans_enabled") == 0)
11219 n = usingNetbeans;
11220#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011221 }
11222
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011223 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011224}
11225
11226/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000011227 * "has_key()" function
11228 */
11229 static void
11230f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011231 typval_T *argvars;
11232 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011233{
11234 rettv->vval.v_number = 0;
11235 if (argvars[0].v_type != VAR_DICT)
11236 {
11237 EMSG(_(e_dictreq));
11238 return;
11239 }
11240 if (argvars[0].vval.v_dict == NULL)
11241 return;
11242
11243 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011244 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011245}
11246
11247/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000011248 * "haslocaldir()" function
11249 */
11250/*ARGSUSED*/
11251 static void
11252f_haslocaldir(argvars, rettv)
11253 typval_T *argvars;
11254 typval_T *rettv;
11255{
11256 rettv->vval.v_number = (curwin->w_localdir != NULL);
11257}
11258
11259/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011260 * "hasmapto()" function
11261 */
11262 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011263f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011264 typval_T *argvars;
11265 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011266{
11267 char_u *name;
11268 char_u *mode;
11269 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000011270 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011271
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011272 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011273 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011274 mode = (char_u *)"nvo";
11275 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000011276 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011277 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000011278 if (argvars[2].v_type != VAR_UNKNOWN)
11279 abbr = get_tv_number(&argvars[2]);
11280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011281
Bram Moolenaar2c932302006-03-18 21:42:09 +000011282 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011283 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011284 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011285 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011286}
11287
11288/*
11289 * "histadd()" function
11290 */
11291/*ARGSUSED*/
11292 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011293f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011294 typval_T *argvars;
11295 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011296{
11297#ifdef FEAT_CMDHIST
11298 int histype;
11299 char_u *str;
11300 char_u buf[NUMBUFLEN];
11301#endif
11302
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011303 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011304 if (check_restricted() || check_secure())
11305 return;
11306#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011307 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11308 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011309 if (histype >= 0)
11310 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011311 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011312 if (*str != NUL)
11313 {
11314 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011315 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011316 return;
11317 }
11318 }
11319#endif
11320}
11321
11322/*
11323 * "histdel()" function
11324 */
11325/*ARGSUSED*/
11326 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011327f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011328 typval_T *argvars;
11329 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011330{
11331#ifdef FEAT_CMDHIST
11332 int n;
11333 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011334 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011335
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011336 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11337 if (str == NULL)
11338 n = 0;
11339 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011340 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011341 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011342 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011343 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011344 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011345 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011346 else
11347 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011348 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011349 get_tv_string_buf(&argvars[1], buf));
11350 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011351#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011352 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011353#endif
11354}
11355
11356/*
11357 * "histget()" function
11358 */
11359/*ARGSUSED*/
11360 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011361f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011362 typval_T *argvars;
11363 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011364{
11365#ifdef FEAT_CMDHIST
11366 int type;
11367 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011368 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011369
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011370 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11371 if (str == NULL)
11372 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011373 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011374 {
11375 type = get_histtype(str);
11376 if (argvars[1].v_type == VAR_UNKNOWN)
11377 idx = get_history_idx(type);
11378 else
11379 idx = (int)get_tv_number_chk(&argvars[1], NULL);
11380 /* -1 on type error */
11381 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
11382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011383#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011384 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011385#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011386 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011387}
11388
11389/*
11390 * "histnr()" function
11391 */
11392/*ARGSUSED*/
11393 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011394f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011395 typval_T *argvars;
11396 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011397{
11398 int i;
11399
11400#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011401 char_u *history = get_tv_string_chk(&argvars[0]);
11402
11403 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011404 if (i >= HIST_CMD && i < HIST_COUNT)
11405 i = get_history_idx(i);
11406 else
11407#endif
11408 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011409 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011410}
11411
11412/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011413 * "highlightID(name)" function
11414 */
11415 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011416f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011417 typval_T *argvars;
11418 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011419{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011420 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011421}
11422
11423/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011424 * "highlight_exists()" function
11425 */
11426 static void
11427f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011428 typval_T *argvars;
11429 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011430{
11431 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
11432}
11433
11434/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011435 * "hostname()" function
11436 */
11437/*ARGSUSED*/
11438 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011439f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011440 typval_T *argvars;
11441 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011442{
11443 char_u hostname[256];
11444
11445 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011446 rettv->v_type = VAR_STRING;
11447 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011448}
11449
11450/*
11451 * iconv() function
11452 */
11453/*ARGSUSED*/
11454 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011455f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011456 typval_T *argvars;
11457 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011458{
11459#ifdef FEAT_MBYTE
11460 char_u buf1[NUMBUFLEN];
11461 char_u buf2[NUMBUFLEN];
11462 char_u *from, *to, *str;
11463 vimconv_T vimconv;
11464#endif
11465
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011466 rettv->v_type = VAR_STRING;
11467 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011468
11469#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011470 str = get_tv_string(&argvars[0]);
11471 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
11472 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011473 vimconv.vc_type = CONV_NONE;
11474 convert_setup(&vimconv, from, to);
11475
11476 /* If the encodings are equal, no conversion needed. */
11477 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011478 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011479 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011480 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011481
11482 convert_setup(&vimconv, NULL, NULL);
11483 vim_free(from);
11484 vim_free(to);
11485#endif
11486}
11487
11488/*
11489 * "indent()" function
11490 */
11491 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011492f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011493 typval_T *argvars;
11494 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011495{
11496 linenr_T lnum;
11497
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011498 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011499 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011500 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011501 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011502 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011503}
11504
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011505/*
11506 * "index()" function
11507 */
11508 static void
11509f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011510 typval_T *argvars;
11511 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011512{
Bram Moolenaar33570922005-01-25 22:26:29 +000011513 list_T *l;
11514 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011515 long idx = 0;
11516 int ic = FALSE;
11517
11518 rettv->vval.v_number = -1;
11519 if (argvars[0].v_type != VAR_LIST)
11520 {
11521 EMSG(_(e_listreq));
11522 return;
11523 }
11524 l = argvars[0].vval.v_list;
11525 if (l != NULL)
11526 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011527 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011528 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011529 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011530 int error = FALSE;
11531
Bram Moolenaar758711c2005-02-02 23:11:38 +000011532 /* Start at specified item. Use the cached index that list_find()
11533 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011534 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000011535 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011536 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011537 ic = get_tv_number_chk(&argvars[3], &error);
11538 if (error)
11539 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011540 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011541
Bram Moolenaar758711c2005-02-02 23:11:38 +000011542 for ( ; item != NULL; item = item->li_next, ++idx)
11543 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011544 {
11545 rettv->vval.v_number = idx;
11546 break;
11547 }
11548 }
11549}
11550
Bram Moolenaar071d4272004-06-13 20:20:40 +000011551static int inputsecret_flag = 0;
11552
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011553static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
11554
Bram Moolenaar071d4272004-06-13 20:20:40 +000011555/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011556 * This function is used by f_input() and f_inputdialog() functions. The third
11557 * argument to f_input() specifies the type of completion to use at the
11558 * prompt. The third argument to f_inputdialog() specifies the value to return
11559 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011560 */
11561 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011562get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000011563 typval_T *argvars;
11564 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011565 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011566{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011567 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011568 char_u *p = NULL;
11569 int c;
11570 char_u buf[NUMBUFLEN];
11571 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011572 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011573 int xp_type = EXPAND_NOTHING;
11574 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011575
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011576 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000011577 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011578
11579#ifdef NO_CONSOLE_INPUT
11580 /* While starting up, there is no place to enter text. */
11581 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000011582 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011583#endif
11584
11585 cmd_silent = FALSE; /* Want to see the prompt. */
11586 if (prompt != NULL)
11587 {
11588 /* Only the part of the message after the last NL is considered as
11589 * prompt for the command line */
11590 p = vim_strrchr(prompt, '\n');
11591 if (p == NULL)
11592 p = prompt;
11593 else
11594 {
11595 ++p;
11596 c = *p;
11597 *p = NUL;
11598 msg_start();
11599 msg_clr_eos();
11600 msg_puts_attr(prompt, echo_attr);
11601 msg_didout = FALSE;
11602 msg_starthere();
11603 *p = c;
11604 }
11605 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011606
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011607 if (argvars[1].v_type != VAR_UNKNOWN)
11608 {
11609 defstr = get_tv_string_buf_chk(&argvars[1], buf);
11610 if (defstr != NULL)
11611 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011612
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011613 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000011614 {
11615 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000011616 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000011617 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011618
Bram Moolenaar4463f292005-09-25 22:20:24 +000011619 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011620
Bram Moolenaar4463f292005-09-25 22:20:24 +000011621 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
11622 if (xp_name == NULL)
11623 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011624
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011625 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011626
Bram Moolenaar4463f292005-09-25 22:20:24 +000011627 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
11628 &xp_arg) == FAIL)
11629 return;
11630 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011631 }
11632
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011633 if (defstr != NULL)
11634 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011635 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
11636 xp_type, xp_arg);
11637
11638 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011639
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011640 /* since the user typed this, no need to wait for return */
11641 need_wait_return = FALSE;
11642 msg_didout = FALSE;
11643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011644 cmd_silent = cmd_silent_save;
11645}
11646
11647/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011648 * "input()" function
11649 * Also handles inputsecret() when inputsecret is set.
11650 */
11651 static void
11652f_input(argvars, rettv)
11653 typval_T *argvars;
11654 typval_T *rettv;
11655{
11656 get_user_input(argvars, rettv, FALSE);
11657}
11658
11659/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011660 * "inputdialog()" function
11661 */
11662 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011663f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011664 typval_T *argvars;
11665 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011666{
11667#if defined(FEAT_GUI_TEXTDIALOG)
11668 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
11669 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
11670 {
11671 char_u *message;
11672 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011673 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000011674
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011675 message = get_tv_string_chk(&argvars[0]);
11676 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000011677 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000011678 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011679 else
11680 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011681 if (message != NULL && defstr != NULL
11682 && do_dialog(VIM_QUESTION, NULL, message,
11683 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011684 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011685 else
11686 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011687 if (message != NULL && defstr != NULL
11688 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011689 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011690 rettv->vval.v_string = vim_strsave(
11691 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011692 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011693 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011694 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011695 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011696 }
11697 else
11698#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011699 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011700}
11701
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011702/*
11703 * "inputlist()" function
11704 */
11705 static void
11706f_inputlist(argvars, rettv)
11707 typval_T *argvars;
11708 typval_T *rettv;
11709{
11710 listitem_T *li;
11711 int selected;
11712 int mouse_used;
11713
11714 rettv->vval.v_number = 0;
11715#ifdef NO_CONSOLE_INPUT
11716 /* While starting up, there is no place to enter text. */
11717 if (no_console_input())
11718 return;
11719#endif
11720 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
11721 {
11722 EMSG2(_(e_listarg), "inputlist()");
11723 return;
11724 }
11725
11726 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000011727 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011728 lines_left = Rows; /* avoid more prompt */
11729 msg_scroll = TRUE;
11730 msg_clr_eos();
11731
11732 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
11733 {
11734 msg_puts(get_tv_string(&li->li_tv));
11735 msg_putchar('\n');
11736 }
11737
11738 /* Ask for choice. */
11739 selected = prompt_for_number(&mouse_used);
11740 if (mouse_used)
11741 selected -= lines_left;
11742
11743 rettv->vval.v_number = selected;
11744}
11745
11746
Bram Moolenaar071d4272004-06-13 20:20:40 +000011747static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
11748
11749/*
11750 * "inputrestore()" function
11751 */
11752/*ARGSUSED*/
11753 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011754f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011755 typval_T *argvars;
11756 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011757{
11758 if (ga_userinput.ga_len > 0)
11759 {
11760 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011761 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
11762 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011763 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011764 }
11765 else if (p_verbose > 1)
11766 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000011767 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011768 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011769 }
11770}
11771
11772/*
11773 * "inputsave()" function
11774 */
11775/*ARGSUSED*/
11776 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011777f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011778 typval_T *argvars;
11779 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011780{
11781 /* Add an entry to the stack of typehead storage. */
11782 if (ga_grow(&ga_userinput, 1) == OK)
11783 {
11784 save_typeahead((tasave_T *)(ga_userinput.ga_data)
11785 + ga_userinput.ga_len);
11786 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011787 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011788 }
11789 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011790 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011791}
11792
11793/*
11794 * "inputsecret()" function
11795 */
11796 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011797f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011798 typval_T *argvars;
11799 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011800{
11801 ++cmdline_star;
11802 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011803 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011804 --cmdline_star;
11805 --inputsecret_flag;
11806}
11807
11808/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011809 * "insert()" function
11810 */
11811 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011812f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011813 typval_T *argvars;
11814 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011815{
11816 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011817 listitem_T *item;
11818 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011819 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011820
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011821 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011822 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011823 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011824 else if ((l = argvars[0].vval.v_list) != NULL
11825 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011826 {
11827 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011828 before = get_tv_number_chk(&argvars[2], &error);
11829 if (error)
11830 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011831
Bram Moolenaar758711c2005-02-02 23:11:38 +000011832 if (before == l->lv_len)
11833 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011834 else
11835 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011836 item = list_find(l, before);
11837 if (item == NULL)
11838 {
11839 EMSGN(_(e_listidx), before);
11840 l = NULL;
11841 }
11842 }
11843 if (l != NULL)
11844 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011845 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011846 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011847 }
11848 }
11849}
11850
11851/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011852 * "isdirectory()" function
11853 */
11854 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011855f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011856 typval_T *argvars;
11857 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011858{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011859 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011860}
11861
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011862/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011863 * "islocked()" function
11864 */
11865 static void
11866f_islocked(argvars, rettv)
11867 typval_T *argvars;
11868 typval_T *rettv;
11869{
11870 lval_T lv;
11871 char_u *end;
11872 dictitem_T *di;
11873
11874 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000011875 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
11876 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011877 if (end != NULL && lv.ll_name != NULL)
11878 {
11879 if (*end != NUL)
11880 EMSG(_(e_trailing));
11881 else
11882 {
11883 if (lv.ll_tv == NULL)
11884 {
11885 if (check_changedtick(lv.ll_name))
11886 rettv->vval.v_number = 1; /* always locked */
11887 else
11888 {
11889 di = find_var(lv.ll_name, NULL);
11890 if (di != NULL)
11891 {
11892 /* Consider a variable locked when:
11893 * 1. the variable itself is locked
11894 * 2. the value of the variable is locked.
11895 * 3. the List or Dict value is locked.
11896 */
11897 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
11898 || tv_islocked(&di->di_tv));
11899 }
11900 }
11901 }
11902 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011903 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011904 else if (lv.ll_newkey != NULL)
11905 EMSG2(_(e_dictkey), lv.ll_newkey);
11906 else if (lv.ll_list != NULL)
11907 /* List item. */
11908 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
11909 else
11910 /* Dictionary item. */
11911 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
11912 }
11913 }
11914
11915 clear_lval(&lv);
11916}
11917
Bram Moolenaar33570922005-01-25 22:26:29 +000011918static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011919
11920/*
11921 * Turn a dict into a list:
11922 * "what" == 0: list of keys
11923 * "what" == 1: list of values
11924 * "what" == 2: list of items
11925 */
11926 static void
11927dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000011928 typval_T *argvars;
11929 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011930 int what;
11931{
Bram Moolenaar33570922005-01-25 22:26:29 +000011932 list_T *l2;
11933 dictitem_T *di;
11934 hashitem_T *hi;
11935 listitem_T *li;
11936 listitem_T *li2;
11937 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011938 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011939
11940 rettv->vval.v_number = 0;
11941 if (argvars[0].v_type != VAR_DICT)
11942 {
11943 EMSG(_(e_dictreq));
11944 return;
11945 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011946 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011947 return;
11948
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011949 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011950 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011951
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011952 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000011953 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011954 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011955 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011956 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011957 --todo;
11958 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011959
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011960 li = listitem_alloc();
11961 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011962 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011963 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011964
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011965 if (what == 0)
11966 {
11967 /* keys() */
11968 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011969 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011970 li->li_tv.vval.v_string = vim_strsave(di->di_key);
11971 }
11972 else if (what == 1)
11973 {
11974 /* values() */
11975 copy_tv(&di->di_tv, &li->li_tv);
11976 }
11977 else
11978 {
11979 /* items() */
11980 l2 = list_alloc();
11981 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011982 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011983 li->li_tv.vval.v_list = l2;
11984 if (l2 == NULL)
11985 break;
11986 ++l2->lv_refcount;
11987
11988 li2 = listitem_alloc();
11989 if (li2 == NULL)
11990 break;
11991 list_append(l2, li2);
11992 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011993 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011994 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
11995
11996 li2 = listitem_alloc();
11997 if (li2 == NULL)
11998 break;
11999 list_append(l2, li2);
12000 copy_tv(&di->di_tv, &li2->li_tv);
12001 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012002 }
12003 }
12004}
12005
12006/*
12007 * "items(dict)" function
12008 */
12009 static void
12010f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012011 typval_T *argvars;
12012 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012013{
12014 dict_list(argvars, rettv, 2);
12015}
12016
Bram Moolenaar071d4272004-06-13 20:20:40 +000012017/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012018 * "join()" function
12019 */
12020 static void
12021f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012022 typval_T *argvars;
12023 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012024{
12025 garray_T ga;
12026 char_u *sep;
12027
12028 rettv->vval.v_number = 0;
12029 if (argvars[0].v_type != VAR_LIST)
12030 {
12031 EMSG(_(e_listreq));
12032 return;
12033 }
12034 if (argvars[0].vval.v_list == NULL)
12035 return;
12036 if (argvars[1].v_type == VAR_UNKNOWN)
12037 sep = (char_u *)" ";
12038 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012039 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012040
12041 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012042
12043 if (sep != NULL)
12044 {
12045 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000012046 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012047 ga_append(&ga, NUL);
12048 rettv->vval.v_string = (char_u *)ga.ga_data;
12049 }
12050 else
12051 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012052}
12053
12054/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012055 * "keys()" function
12056 */
12057 static void
12058f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012059 typval_T *argvars;
12060 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012061{
12062 dict_list(argvars, rettv, 0);
12063}
12064
12065/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012066 * "last_buffer_nr()" function.
12067 */
12068/*ARGSUSED*/
12069 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012070f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012071 typval_T *argvars;
12072 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012073{
12074 int n = 0;
12075 buf_T *buf;
12076
12077 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
12078 if (n < buf->b_fnum)
12079 n = buf->b_fnum;
12080
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012081 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012082}
12083
12084/*
12085 * "len()" function
12086 */
12087 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012088f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012089 typval_T *argvars;
12090 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012091{
12092 switch (argvars[0].v_type)
12093 {
12094 case VAR_STRING:
12095 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012096 rettv->vval.v_number = (varnumber_T)STRLEN(
12097 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012098 break;
12099 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012100 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012101 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012102 case VAR_DICT:
12103 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
12104 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012105 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012106 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012107 break;
12108 }
12109}
12110
Bram Moolenaar33570922005-01-25 22:26:29 +000012111static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012112
12113 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012114libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000012115 typval_T *argvars;
12116 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012117 int type;
12118{
12119#ifdef FEAT_LIBCALL
12120 char_u *string_in;
12121 char_u **string_result;
12122 int nr_result;
12123#endif
12124
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012125 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012126 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012127 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012128 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012129 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012130
12131 if (check_restricted() || check_secure())
12132 return;
12133
12134#ifdef FEAT_LIBCALL
12135 /* The first two args must be strings, otherwise its meaningless */
12136 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
12137 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012138 string_in = NULL;
12139 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012140 string_in = argvars[2].vval.v_string;
12141 if (type == VAR_NUMBER)
12142 string_result = NULL;
12143 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012144 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012145 if (mch_libcall(argvars[0].vval.v_string,
12146 argvars[1].vval.v_string,
12147 string_in,
12148 argvars[2].vval.v_number,
12149 string_result,
12150 &nr_result) == OK
12151 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012152 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012153 }
12154#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012155}
12156
12157/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012158 * "libcall()" function
12159 */
12160 static void
12161f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012162 typval_T *argvars;
12163 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012164{
12165 libcall_common(argvars, rettv, VAR_STRING);
12166}
12167
12168/*
12169 * "libcallnr()" function
12170 */
12171 static void
12172f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012173 typval_T *argvars;
12174 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012175{
12176 libcall_common(argvars, rettv, VAR_NUMBER);
12177}
12178
12179/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012180 * "line(string)" function
12181 */
12182 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012183f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012184 typval_T *argvars;
12185 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012186{
12187 linenr_T lnum = 0;
12188 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012189 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012190
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012191 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012192 if (fp != NULL)
12193 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012194 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012195}
12196
12197/*
12198 * "line2byte(lnum)" function
12199 */
12200/*ARGSUSED*/
12201 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012202f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012203 typval_T *argvars;
12204 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012205{
12206#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012207 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012208#else
12209 linenr_T lnum;
12210
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012211 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012212 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012213 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012214 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012215 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
12216 if (rettv->vval.v_number >= 0)
12217 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012218#endif
12219}
12220
12221/*
12222 * "lispindent(lnum)" function
12223 */
12224 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012225f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012226 typval_T *argvars;
12227 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012228{
12229#ifdef FEAT_LISP
12230 pos_T pos;
12231 linenr_T lnum;
12232
12233 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012234 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012235 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12236 {
12237 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012238 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012239 curwin->w_cursor = pos;
12240 }
12241 else
12242#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012243 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012244}
12245
12246/*
12247 * "localtime()" function
12248 */
12249/*ARGSUSED*/
12250 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012251f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012252 typval_T *argvars;
12253 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012254{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012255 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012256}
12257
Bram Moolenaar33570922005-01-25 22:26:29 +000012258static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012259
12260 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012261get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000012262 typval_T *argvars;
12263 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012264 int exact;
12265{
12266 char_u *keys;
12267 char_u *which;
12268 char_u buf[NUMBUFLEN];
12269 char_u *keys_buf = NULL;
12270 char_u *rhs;
12271 int mode;
12272 garray_T ga;
Bram Moolenaar2c932302006-03-18 21:42:09 +000012273 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012274
12275 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012276 rettv->v_type = VAR_STRING;
12277 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012278
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012279 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012280 if (*keys == NUL)
12281 return;
12282
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012283 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000012284 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012285 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012286 if (argvars[2].v_type != VAR_UNKNOWN)
12287 abbr = get_tv_number(&argvars[2]);
12288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012289 else
12290 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012291 if (which == NULL)
12292 return;
12293
Bram Moolenaar071d4272004-06-13 20:20:40 +000012294 mode = get_map_mode(&which, 0);
12295
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000012296 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012297 rhs = check_map(keys, mode, exact, FALSE, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012298 vim_free(keys_buf);
12299 if (rhs != NULL)
12300 {
12301 ga_init(&ga);
12302 ga.ga_itemsize = 1;
12303 ga.ga_growsize = 40;
12304
12305 while (*rhs != NUL)
12306 ga_concat(&ga, str2special(&rhs, FALSE));
12307
12308 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012309 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012310 }
12311}
12312
12313/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012314 * "map()" function
12315 */
12316 static void
12317f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012318 typval_T *argvars;
12319 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012320{
12321 filter_map(argvars, rettv, TRUE);
12322}
12323
12324/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012325 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012326 */
12327 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012328f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012329 typval_T *argvars;
12330 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012331{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012332 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012333}
12334
12335/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012336 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012337 */
12338 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012339f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012340 typval_T *argvars;
12341 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012342{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012343 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012344}
12345
Bram Moolenaar33570922005-01-25 22:26:29 +000012346static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012347
12348 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012349find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000012350 typval_T *argvars;
12351 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012352 int type;
12353{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012354 char_u *str = NULL;
12355 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012356 char_u *pat;
12357 regmatch_T regmatch;
12358 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012359 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000012360 char_u *save_cpo;
12361 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012362 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012363 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012364 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012365 list_T *l = NULL;
12366 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012367 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012368 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012369
12370 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12371 save_cpo = p_cpo;
12372 p_cpo = (char_u *)"";
12373
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012374 rettv->vval.v_number = -1;
12375 if (type == 3)
12376 {
12377 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012378 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012379 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012380 }
12381 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012382 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012383 rettv->v_type = VAR_STRING;
12384 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012386
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012387 if (argvars[0].v_type == VAR_LIST)
12388 {
12389 if ((l = argvars[0].vval.v_list) == NULL)
12390 goto theend;
12391 li = l->lv_first;
12392 }
12393 else
12394 expr = str = get_tv_string(&argvars[0]);
12395
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012396 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
12397 if (pat == NULL)
12398 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012399
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012400 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012401 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012402 int error = FALSE;
12403
12404 start = get_tv_number_chk(&argvars[2], &error);
12405 if (error)
12406 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012407 if (l != NULL)
12408 {
12409 li = list_find(l, start);
12410 if (li == NULL)
12411 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012412 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012413 }
12414 else
12415 {
12416 if (start < 0)
12417 start = 0;
12418 if (start > (long)STRLEN(str))
12419 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012420 /* When "count" argument is there ignore matches before "start",
12421 * otherwise skip part of the string. Differs when pattern is "^"
12422 * or "\<". */
12423 if (argvars[3].v_type != VAR_UNKNOWN)
12424 startcol = start;
12425 else
12426 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012427 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012428
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012429 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012430 nth = get_tv_number_chk(&argvars[3], &error);
12431 if (error)
12432 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012433 }
12434
12435 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12436 if (regmatch.regprog != NULL)
12437 {
12438 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012439
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012440 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012441 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012442 if (l != NULL)
12443 {
12444 if (li == NULL)
12445 {
12446 match = FALSE;
12447 break;
12448 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012449 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012450 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012451 if (str == NULL)
12452 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012453 }
12454
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012455 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012456
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012457 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012458 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012459 if (l == NULL && !match)
12460 break;
12461
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012462 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012463 if (l != NULL)
12464 {
12465 li = li->li_next;
12466 ++idx;
12467 }
12468 else
12469 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012470#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012471 startcol = (colnr_T)(regmatch.startp[0]
12472 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012473#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012474 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012475#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012476 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012477 }
12478
12479 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012480 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012481 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012482 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012483 int i;
12484
12485 /* return list with matched string and submatches */
12486 for (i = 0; i < NSUBEXP; ++i)
12487 {
12488 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000012489 {
12490 if (list_append_string(rettv->vval.v_list,
12491 (char_u *)"", 0) == FAIL)
12492 break;
12493 }
12494 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000012495 regmatch.startp[i],
12496 (int)(regmatch.endp[i] - regmatch.startp[i]))
12497 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012498 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012499 }
12500 }
12501 else if (type == 2)
12502 {
12503 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012504 if (l != NULL)
12505 copy_tv(&li->li_tv, rettv);
12506 else
12507 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000012508 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012509 }
12510 else if (l != NULL)
12511 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012512 else
12513 {
12514 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012515 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012516 (varnumber_T)(regmatch.startp[0] - str);
12517 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012518 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012519 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012520 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012521 }
12522 }
12523 vim_free(regmatch.regprog);
12524 }
12525
12526theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012527 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012528 p_cpo = save_cpo;
12529}
12530
12531/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012532 * "match()" function
12533 */
12534 static void
12535f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012536 typval_T *argvars;
12537 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012538{
12539 find_some_match(argvars, rettv, 1);
12540}
12541
12542/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012543 * "matchadd()" function
12544 */
12545 static void
12546f_matchadd(argvars, rettv)
12547 typval_T *argvars;
12548 typval_T *rettv;
12549{
12550#ifdef FEAT_SEARCH_EXTRA
12551 char_u buf[NUMBUFLEN];
12552 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
12553 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
12554 int prio = 10; /* default priority */
12555 int id = -1;
12556 int error = FALSE;
12557
12558 rettv->vval.v_number = -1;
12559
12560 if (grp == NULL || pat == NULL)
12561 return;
12562 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000012563 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012564 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000012565 if (argvars[3].v_type != VAR_UNKNOWN)
12566 id = get_tv_number_chk(&argvars[3], &error);
12567 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012568 if (error == TRUE)
12569 return;
12570 if (id >= 1 && id <= 3)
12571 {
12572 EMSGN("E798: ID is reserved for \":match\": %ld", id);
12573 return;
12574 }
12575
12576 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
12577#endif
12578}
12579
12580/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012581 * "matcharg()" function
12582 */
12583 static void
12584f_matcharg(argvars, rettv)
12585 typval_T *argvars;
12586 typval_T *rettv;
12587{
12588 if (rettv_list_alloc(rettv) == OK)
12589 {
12590#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012591 int id = get_tv_number(&argvars[0]);
12592 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012593
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012594 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012595 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012596 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
12597 {
12598 list_append_string(rettv->vval.v_list,
12599 syn_id2name(m->hlg_id), -1);
12600 list_append_string(rettv->vval.v_list, m->pattern, -1);
12601 }
12602 else
12603 {
12604 list_append_string(rettv->vval.v_list, NUL, -1);
12605 list_append_string(rettv->vval.v_list, NUL, -1);
12606 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012607 }
12608#endif
12609 }
12610}
12611
12612/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012613 * "matchdelete()" function
12614 */
12615 static void
12616f_matchdelete(argvars, rettv)
12617 typval_T *argvars;
12618 typval_T *rettv;
12619{
12620#ifdef FEAT_SEARCH_EXTRA
12621 rettv->vval.v_number = match_delete(curwin,
12622 (int)get_tv_number(&argvars[0]), TRUE);
12623#endif
12624}
12625
12626/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012627 * "matchend()" function
12628 */
12629 static void
12630f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012631 typval_T *argvars;
12632 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012633{
12634 find_some_match(argvars, rettv, 0);
12635}
12636
12637/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012638 * "matchlist()" function
12639 */
12640 static void
12641f_matchlist(argvars, rettv)
12642 typval_T *argvars;
12643 typval_T *rettv;
12644{
12645 find_some_match(argvars, rettv, 3);
12646}
12647
12648/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012649 * "matchstr()" function
12650 */
12651 static void
12652f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012653 typval_T *argvars;
12654 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012655{
12656 find_some_match(argvars, rettv, 2);
12657}
12658
Bram Moolenaar33570922005-01-25 22:26:29 +000012659static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012660
12661 static void
12662max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000012663 typval_T *argvars;
12664 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012665 int domax;
12666{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012667 long n = 0;
12668 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012669 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012670
12671 if (argvars[0].v_type == VAR_LIST)
12672 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012673 list_T *l;
12674 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012675
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012676 l = argvars[0].vval.v_list;
12677 if (l != NULL)
12678 {
12679 li = l->lv_first;
12680 if (li != NULL)
12681 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012682 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012683 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012684 {
12685 li = li->li_next;
12686 if (li == NULL)
12687 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012688 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012689 if (domax ? i > n : i < n)
12690 n = i;
12691 }
12692 }
12693 }
12694 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012695 else if (argvars[0].v_type == VAR_DICT)
12696 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012697 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012698 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000012699 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012700 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012701
12702 d = argvars[0].vval.v_dict;
12703 if (d != NULL)
12704 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012705 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000012706 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012707 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012708 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000012709 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012710 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012711 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012712 if (first)
12713 {
12714 n = i;
12715 first = FALSE;
12716 }
12717 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012718 n = i;
12719 }
12720 }
12721 }
12722 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012723 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000012724 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012725 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012726}
12727
12728/*
12729 * "max()" function
12730 */
12731 static void
12732f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012733 typval_T *argvars;
12734 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012735{
12736 max_min(argvars, rettv, TRUE);
12737}
12738
12739/*
12740 * "min()" function
12741 */
12742 static void
12743f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012744 typval_T *argvars;
12745 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012746{
12747 max_min(argvars, rettv, FALSE);
12748}
12749
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012750static int mkdir_recurse __ARGS((char_u *dir, int prot));
12751
12752/*
12753 * Create the directory in which "dir" is located, and higher levels when
12754 * needed.
12755 */
12756 static int
12757mkdir_recurse(dir, prot)
12758 char_u *dir;
12759 int prot;
12760{
12761 char_u *p;
12762 char_u *updir;
12763 int r = FAIL;
12764
12765 /* Get end of directory name in "dir".
12766 * We're done when it's "/" or "c:/". */
12767 p = gettail_sep(dir);
12768 if (p <= get_past_head(dir))
12769 return OK;
12770
12771 /* If the directory exists we're done. Otherwise: create it.*/
12772 updir = vim_strnsave(dir, (int)(p - dir));
12773 if (updir == NULL)
12774 return FAIL;
12775 if (mch_isdir(updir))
12776 r = OK;
12777 else if (mkdir_recurse(updir, prot) == OK)
12778 r = vim_mkdir_emsg(updir, prot);
12779 vim_free(updir);
12780 return r;
12781}
12782
12783#ifdef vim_mkdir
12784/*
12785 * "mkdir()" function
12786 */
12787 static void
12788f_mkdir(argvars, rettv)
12789 typval_T *argvars;
12790 typval_T *rettv;
12791{
12792 char_u *dir;
12793 char_u buf[NUMBUFLEN];
12794 int prot = 0755;
12795
12796 rettv->vval.v_number = FAIL;
12797 if (check_restricted() || check_secure())
12798 return;
12799
12800 dir = get_tv_string_buf(&argvars[0], buf);
12801 if (argvars[1].v_type != VAR_UNKNOWN)
12802 {
12803 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012804 prot = get_tv_number_chk(&argvars[2], NULL);
12805 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012806 mkdir_recurse(dir, prot);
12807 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012808 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012809}
12810#endif
12811
Bram Moolenaar0d660222005-01-07 21:51:51 +000012812/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012813 * "mode()" function
12814 */
12815/*ARGSUSED*/
12816 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012817f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012818 typval_T *argvars;
12819 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012820{
12821 char_u buf[2];
12822
12823#ifdef FEAT_VISUAL
12824 if (VIsual_active)
12825 {
12826 if (VIsual_select)
12827 buf[0] = VIsual_mode + 's' - 'v';
12828 else
12829 buf[0] = VIsual_mode;
12830 }
12831 else
12832#endif
12833 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
12834 buf[0] = 'r';
12835 else if (State & INSERT)
12836 {
12837 if (State & REPLACE_FLAG)
12838 buf[0] = 'R';
12839 else
12840 buf[0] = 'i';
12841 }
12842 else if (State & CMDLINE)
12843 buf[0] = 'c';
12844 else
12845 buf[0] = 'n';
12846
12847 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012848 rettv->vval.v_string = vim_strsave(buf);
12849 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012850}
12851
12852/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012853 * "nextnonblank()" function
12854 */
12855 static void
12856f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012857 typval_T *argvars;
12858 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012859{
12860 linenr_T lnum;
12861
12862 for (lnum = get_tv_lnum(argvars); ; ++lnum)
12863 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012864 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012865 {
12866 lnum = 0;
12867 break;
12868 }
12869 if (*skipwhite(ml_get(lnum)) != NUL)
12870 break;
12871 }
12872 rettv->vval.v_number = lnum;
12873}
12874
12875/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012876 * "nr2char()" function
12877 */
12878 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012879f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012880 typval_T *argvars;
12881 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012882{
12883 char_u buf[NUMBUFLEN];
12884
12885#ifdef FEAT_MBYTE
12886 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012887 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012888 else
12889#endif
12890 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012891 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012892 buf[1] = NUL;
12893 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012894 rettv->v_type = VAR_STRING;
12895 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012896}
12897
12898/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012899 * "pathshorten()" function
12900 */
12901 static void
12902f_pathshorten(argvars, rettv)
12903 typval_T *argvars;
12904 typval_T *rettv;
12905{
12906 char_u *p;
12907
12908 rettv->v_type = VAR_STRING;
12909 p = get_tv_string_chk(&argvars[0]);
12910 if (p == NULL)
12911 rettv->vval.v_string = NULL;
12912 else
12913 {
12914 p = vim_strsave(p);
12915 rettv->vval.v_string = p;
12916 if (p != NULL)
12917 shorten_dir(p);
12918 }
12919}
12920
12921/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012922 * "prevnonblank()" function
12923 */
12924 static void
12925f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012926 typval_T *argvars;
12927 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012928{
12929 linenr_T lnum;
12930
12931 lnum = get_tv_lnum(argvars);
12932 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
12933 lnum = 0;
12934 else
12935 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
12936 --lnum;
12937 rettv->vval.v_number = lnum;
12938}
12939
Bram Moolenaara6c840d2005-08-22 22:59:46 +000012940#ifdef HAVE_STDARG_H
12941/* This dummy va_list is here because:
12942 * - passing a NULL pointer doesn't work when va_list isn't a pointer
12943 * - locally in the function results in a "used before set" warning
12944 * - using va_start() to initialize it gives "function with fixed args" error */
12945static va_list ap;
12946#endif
12947
Bram Moolenaar8c711452005-01-14 21:53:12 +000012948/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012949 * "printf()" function
12950 */
12951 static void
12952f_printf(argvars, rettv)
12953 typval_T *argvars;
12954 typval_T *rettv;
12955{
12956 rettv->v_type = VAR_STRING;
12957 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000012958#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012959 {
12960 char_u buf[NUMBUFLEN];
12961 int len;
12962 char_u *s;
12963 int saved_did_emsg = did_emsg;
12964 char *fmt;
12965
12966 /* Get the required length, allocate the buffer and do it for real. */
12967 did_emsg = FALSE;
12968 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012969 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012970 if (!did_emsg)
12971 {
12972 s = alloc(len + 1);
12973 if (s != NULL)
12974 {
12975 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012976 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012977 }
12978 }
12979 did_emsg |= saved_did_emsg;
12980 }
12981#endif
12982}
12983
12984/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000012985 * "pumvisible()" function
12986 */
12987/*ARGSUSED*/
12988 static void
12989f_pumvisible(argvars, rettv)
12990 typval_T *argvars;
12991 typval_T *rettv;
12992{
12993 rettv->vval.v_number = 0;
12994#ifdef FEAT_INS_EXPAND
12995 if (pum_visible())
12996 rettv->vval.v_number = 1;
12997#endif
12998}
12999
13000/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013001 * "range()" function
13002 */
13003 static void
13004f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013005 typval_T *argvars;
13006 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013007{
13008 long start;
13009 long end;
13010 long stride = 1;
13011 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013012 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013013
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013014 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013015 if (argvars[1].v_type == VAR_UNKNOWN)
13016 {
13017 end = start - 1;
13018 start = 0;
13019 }
13020 else
13021 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013022 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013023 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013024 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013025 }
13026
13027 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013028 if (error)
13029 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000013030 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013031 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000013032 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013033 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013034 else
13035 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013036 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013037 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013038 if (list_append_number(rettv->vval.v_list,
13039 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013040 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013041 }
13042}
13043
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013044/*
13045 * "readfile()" function
13046 */
13047 static void
13048f_readfile(argvars, rettv)
13049 typval_T *argvars;
13050 typval_T *rettv;
13051{
13052 int binary = FALSE;
13053 char_u *fname;
13054 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013055 listitem_T *li;
13056#define FREAD_SIZE 200 /* optimized for text lines */
13057 char_u buf[FREAD_SIZE];
13058 int readlen; /* size of last fread() */
13059 int buflen; /* nr of valid chars in buf[] */
13060 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
13061 int tolist; /* first byte in buf[] still to be put in list */
13062 int chop; /* how many CR to chop off */
13063 char_u *prev = NULL; /* previously read bytes, if any */
13064 int prevlen = 0; /* length of "prev" if not NULL */
13065 char_u *s;
13066 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013067 long maxline = MAXLNUM;
13068 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013069
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013070 if (argvars[1].v_type != VAR_UNKNOWN)
13071 {
13072 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
13073 binary = TRUE;
13074 if (argvars[2].v_type != VAR_UNKNOWN)
13075 maxline = get_tv_number(&argvars[2]);
13076 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013077
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013078 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013079 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013080
13081 /* Always open the file in binary mode, library functions have a mind of
13082 * their own about CR-LF conversion. */
13083 fname = get_tv_string(&argvars[0]);
13084 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
13085 {
13086 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
13087 return;
13088 }
13089
13090 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013091 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013092 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013093 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013094 buflen = filtd + readlen;
13095 tolist = 0;
13096 for ( ; filtd < buflen || readlen <= 0; ++filtd)
13097 {
13098 if (buf[filtd] == '\n' || readlen <= 0)
13099 {
13100 /* Only when in binary mode add an empty list item when the
13101 * last line ends in a '\n'. */
13102 if (!binary && readlen == 0 && filtd == 0)
13103 break;
13104
13105 /* Found end-of-line or end-of-file: add a text line to the
13106 * list. */
13107 chop = 0;
13108 if (!binary)
13109 while (filtd - chop - 1 >= tolist
13110 && buf[filtd - chop - 1] == '\r')
13111 ++chop;
13112 len = filtd - tolist - chop;
13113 if (prev == NULL)
13114 s = vim_strnsave(buf + tolist, len);
13115 else
13116 {
13117 s = alloc((unsigned)(prevlen + len + 1));
13118 if (s != NULL)
13119 {
13120 mch_memmove(s, prev, prevlen);
13121 vim_free(prev);
13122 prev = NULL;
13123 mch_memmove(s + prevlen, buf + tolist, len);
13124 s[prevlen + len] = NUL;
13125 }
13126 }
13127 tolist = filtd + 1;
13128
13129 li = listitem_alloc();
13130 if (li == NULL)
13131 {
13132 vim_free(s);
13133 break;
13134 }
13135 li->li_tv.v_type = VAR_STRING;
13136 li->li_tv.v_lock = 0;
13137 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013138 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013139
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013140 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013141 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013142 if (readlen <= 0)
13143 break;
13144 }
13145 else if (buf[filtd] == NUL)
13146 buf[filtd] = '\n';
13147 }
13148 if (readlen <= 0)
13149 break;
13150
13151 if (tolist == 0)
13152 {
13153 /* "buf" is full, need to move text to an allocated buffer */
13154 if (prev == NULL)
13155 {
13156 prev = vim_strnsave(buf, buflen);
13157 prevlen = buflen;
13158 }
13159 else
13160 {
13161 s = alloc((unsigned)(prevlen + buflen));
13162 if (s != NULL)
13163 {
13164 mch_memmove(s, prev, prevlen);
13165 mch_memmove(s + prevlen, buf, buflen);
13166 vim_free(prev);
13167 prev = s;
13168 prevlen += buflen;
13169 }
13170 }
13171 filtd = 0;
13172 }
13173 else
13174 {
13175 mch_memmove(buf, buf + tolist, buflen - tolist);
13176 filtd -= tolist;
13177 }
13178 }
13179
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013180 /*
13181 * For a negative line count use only the lines at the end of the file,
13182 * free the rest.
13183 */
13184 if (maxline < 0)
13185 while (cnt > -maxline)
13186 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013187 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013188 --cnt;
13189 }
13190
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013191 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013192 fclose(fd);
13193}
13194
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013195#if defined(FEAT_RELTIME)
13196static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
13197
13198/*
13199 * Convert a List to proftime_T.
13200 * Return FAIL when there is something wrong.
13201 */
13202 static int
13203list2proftime(arg, tm)
13204 typval_T *arg;
13205 proftime_T *tm;
13206{
13207 long n1, n2;
13208 int error = FALSE;
13209
13210 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
13211 || arg->vval.v_list->lv_len != 2)
13212 return FAIL;
13213 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
13214 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
13215# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000013216 tm->HighPart = n1;
13217 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013218# else
13219 tm->tv_sec = n1;
13220 tm->tv_usec = n2;
13221# endif
13222 return error ? FAIL : OK;
13223}
13224#endif /* FEAT_RELTIME */
13225
13226/*
13227 * "reltime()" function
13228 */
13229 static void
13230f_reltime(argvars, rettv)
13231 typval_T *argvars;
13232 typval_T *rettv;
13233{
13234#ifdef FEAT_RELTIME
13235 proftime_T res;
13236 proftime_T start;
13237
13238 if (argvars[0].v_type == VAR_UNKNOWN)
13239 {
13240 /* No arguments: get current time. */
13241 profile_start(&res);
13242 }
13243 else if (argvars[1].v_type == VAR_UNKNOWN)
13244 {
13245 if (list2proftime(&argvars[0], &res) == FAIL)
13246 return;
13247 profile_end(&res);
13248 }
13249 else
13250 {
13251 /* Two arguments: compute the difference. */
13252 if (list2proftime(&argvars[0], &start) == FAIL
13253 || list2proftime(&argvars[1], &res) == FAIL)
13254 return;
13255 profile_sub(&res, &start);
13256 }
13257
13258 if (rettv_list_alloc(rettv) == OK)
13259 {
13260 long n1, n2;
13261
13262# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000013263 n1 = res.HighPart;
13264 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013265# else
13266 n1 = res.tv_sec;
13267 n2 = res.tv_usec;
13268# endif
13269 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
13270 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
13271 }
13272#endif
13273}
13274
13275/*
13276 * "reltimestr()" function
13277 */
13278 static void
13279f_reltimestr(argvars, rettv)
13280 typval_T *argvars;
13281 typval_T *rettv;
13282{
13283#ifdef FEAT_RELTIME
13284 proftime_T tm;
13285#endif
13286
13287 rettv->v_type = VAR_STRING;
13288 rettv->vval.v_string = NULL;
13289#ifdef FEAT_RELTIME
13290 if (list2proftime(&argvars[0], &tm) == OK)
13291 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
13292#endif
13293}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013294
Bram Moolenaar0d660222005-01-07 21:51:51 +000013295#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
13296static void make_connection __ARGS((void));
13297static int check_connection __ARGS((void));
13298
13299 static void
13300make_connection()
13301{
13302 if (X_DISPLAY == NULL
13303# ifdef FEAT_GUI
13304 && !gui.in_use
13305# endif
13306 )
13307 {
13308 x_force_connect = TRUE;
13309 setup_term_clip();
13310 x_force_connect = FALSE;
13311 }
13312}
13313
13314 static int
13315check_connection()
13316{
13317 make_connection();
13318 if (X_DISPLAY == NULL)
13319 {
13320 EMSG(_("E240: No connection to Vim server"));
13321 return FAIL;
13322 }
13323 return OK;
13324}
13325#endif
13326
13327#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000013328static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013329
13330 static void
13331remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000013332 typval_T *argvars;
13333 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013334 int expr;
13335{
13336 char_u *server_name;
13337 char_u *keys;
13338 char_u *r = NULL;
13339 char_u buf[NUMBUFLEN];
13340# ifdef WIN32
13341 HWND w;
13342# else
13343 Window w;
13344# endif
13345
13346 if (check_restricted() || check_secure())
13347 return;
13348
13349# ifdef FEAT_X11
13350 if (check_connection() == FAIL)
13351 return;
13352# endif
13353
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013354 server_name = get_tv_string_chk(&argvars[0]);
13355 if (server_name == NULL)
13356 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013357 keys = get_tv_string_buf(&argvars[1], buf);
13358# ifdef WIN32
13359 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
13360# else
13361 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
13362 < 0)
13363# endif
13364 {
13365 if (r != NULL)
13366 EMSG(r); /* sending worked but evaluation failed */
13367 else
13368 EMSG2(_("E241: Unable to send to %s"), server_name);
13369 return;
13370 }
13371
13372 rettv->vval.v_string = r;
13373
13374 if (argvars[2].v_type != VAR_UNKNOWN)
13375 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013376 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000013377 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013378 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013379
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013380 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000013381 v.di_tv.v_type = VAR_STRING;
13382 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013383 idvar = get_tv_string_chk(&argvars[2]);
13384 if (idvar != NULL)
13385 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000013386 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013387 }
13388}
13389#endif
13390
13391/*
13392 * "remote_expr()" function
13393 */
13394/*ARGSUSED*/
13395 static void
13396f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013397 typval_T *argvars;
13398 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013399{
13400 rettv->v_type = VAR_STRING;
13401 rettv->vval.v_string = NULL;
13402#ifdef FEAT_CLIENTSERVER
13403 remote_common(argvars, rettv, TRUE);
13404#endif
13405}
13406
13407/*
13408 * "remote_foreground()" function
13409 */
13410/*ARGSUSED*/
13411 static void
13412f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013413 typval_T *argvars;
13414 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013415{
13416 rettv->vval.v_number = 0;
13417#ifdef FEAT_CLIENTSERVER
13418# ifdef WIN32
13419 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013420 {
13421 char_u *server_name = get_tv_string_chk(&argvars[0]);
13422
13423 if (server_name != NULL)
13424 serverForeground(server_name);
13425 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013426# else
13427 /* Send a foreground() expression to the server. */
13428 argvars[1].v_type = VAR_STRING;
13429 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
13430 argvars[2].v_type = VAR_UNKNOWN;
13431 remote_common(argvars, rettv, TRUE);
13432 vim_free(argvars[1].vval.v_string);
13433# endif
13434#endif
13435}
13436
13437/*ARGSUSED*/
13438 static void
13439f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013440 typval_T *argvars;
13441 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013442{
13443#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000013444 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013445 char_u *s = NULL;
13446# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013447 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013448# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013449 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013450
13451 if (check_restricted() || check_secure())
13452 {
13453 rettv->vval.v_number = -1;
13454 return;
13455 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013456 serverid = get_tv_string_chk(&argvars[0]);
13457 if (serverid == NULL)
13458 {
13459 rettv->vval.v_number = -1;
13460 return; /* type error; errmsg already given */
13461 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013462# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013463 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013464 if (n == 0)
13465 rettv->vval.v_number = -1;
13466 else
13467 {
13468 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
13469 rettv->vval.v_number = (s != NULL);
13470 }
13471# else
13472 rettv->vval.v_number = 0;
13473 if (check_connection() == FAIL)
13474 return;
13475
13476 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013477 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013478# endif
13479
13480 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
13481 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013482 char_u *retvar;
13483
Bram Moolenaar33570922005-01-25 22:26:29 +000013484 v.di_tv.v_type = VAR_STRING;
13485 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013486 retvar = get_tv_string_chk(&argvars[1]);
13487 if (retvar != NULL)
13488 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000013489 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013490 }
13491#else
13492 rettv->vval.v_number = -1;
13493#endif
13494}
13495
13496/*ARGSUSED*/
13497 static void
13498f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013499 typval_T *argvars;
13500 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013501{
13502 char_u *r = NULL;
13503
13504#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013505 char_u *serverid = get_tv_string_chk(&argvars[0]);
13506
13507 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000013508 {
13509# ifdef WIN32
13510 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013511 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013512
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013513 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013514 if (n != 0)
13515 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
13516 if (r == NULL)
13517# else
13518 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013519 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013520# endif
13521 EMSG(_("E277: Unable to read a server reply"));
13522 }
13523#endif
13524 rettv->v_type = VAR_STRING;
13525 rettv->vval.v_string = r;
13526}
13527
13528/*
13529 * "remote_send()" function
13530 */
13531/*ARGSUSED*/
13532 static void
13533f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013534 typval_T *argvars;
13535 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013536{
13537 rettv->v_type = VAR_STRING;
13538 rettv->vval.v_string = NULL;
13539#ifdef FEAT_CLIENTSERVER
13540 remote_common(argvars, rettv, FALSE);
13541#endif
13542}
13543
13544/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013545 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013546 */
13547 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013548f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013549 typval_T *argvars;
13550 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013551{
Bram Moolenaar33570922005-01-25 22:26:29 +000013552 list_T *l;
13553 listitem_T *item, *item2;
13554 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013555 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013556 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013557 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000013558 dict_T *d;
13559 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013560
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013561 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013562 if (argvars[0].v_type == VAR_DICT)
13563 {
13564 if (argvars[2].v_type != VAR_UNKNOWN)
13565 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013566 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000013567 && !tv_check_lock(d->dv_lock, (char_u *)"remove() argument"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013568 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013569 key = get_tv_string_chk(&argvars[1]);
13570 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013571 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013572 di = dict_find(d, key, -1);
13573 if (di == NULL)
13574 EMSG2(_(e_dictkey), key);
13575 else
13576 {
13577 *rettv = di->di_tv;
13578 init_tv(&di->di_tv);
13579 dictitem_remove(d, di);
13580 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013581 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013582 }
13583 }
13584 else if (argvars[0].v_type != VAR_LIST)
13585 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013586 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000013587 && !tv_check_lock(l->lv_lock, (char_u *)"remove() argument"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013588 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013589 int error = FALSE;
13590
13591 idx = get_tv_number_chk(&argvars[1], &error);
13592 if (error)
13593 ; /* type error: do nothing, errmsg already given */
13594 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013595 EMSGN(_(e_listidx), idx);
13596 else
13597 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013598 if (argvars[2].v_type == VAR_UNKNOWN)
13599 {
13600 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013601 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013602 *rettv = item->li_tv;
13603 vim_free(item);
13604 }
13605 else
13606 {
13607 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013608 end = get_tv_number_chk(&argvars[2], &error);
13609 if (error)
13610 ; /* type error: do nothing */
13611 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013612 EMSGN(_(e_listidx), end);
13613 else
13614 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013615 int cnt = 0;
13616
13617 for (li = item; li != NULL; li = li->li_next)
13618 {
13619 ++cnt;
13620 if (li == item2)
13621 break;
13622 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013623 if (li == NULL) /* didn't find "item2" after "item" */
13624 EMSG(_(e_invrange));
13625 else
13626 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013627 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013628 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013629 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013630 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013631 l->lv_first = item;
13632 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013633 item->li_prev = NULL;
13634 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013635 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013636 }
13637 }
13638 }
13639 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013640 }
13641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013642}
13643
13644/*
13645 * "rename({from}, {to})" function
13646 */
13647 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013648f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013649 typval_T *argvars;
13650 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013651{
13652 char_u buf[NUMBUFLEN];
13653
13654 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013655 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013656 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013657 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
13658 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013659}
13660
13661/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013662 * "repeat()" function
13663 */
13664/*ARGSUSED*/
13665 static void
13666f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013667 typval_T *argvars;
13668 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013669{
13670 char_u *p;
13671 int n;
13672 int slen;
13673 int len;
13674 char_u *r;
13675 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013676
13677 n = get_tv_number(&argvars[1]);
13678 if (argvars[0].v_type == VAR_LIST)
13679 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013680 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013681 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013682 if (list_extend(rettv->vval.v_list,
13683 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013684 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013685 }
13686 else
13687 {
13688 p = get_tv_string(&argvars[0]);
13689 rettv->v_type = VAR_STRING;
13690 rettv->vval.v_string = NULL;
13691
13692 slen = (int)STRLEN(p);
13693 len = slen * n;
13694 if (len <= 0)
13695 return;
13696
13697 r = alloc(len + 1);
13698 if (r != NULL)
13699 {
13700 for (i = 0; i < n; i++)
13701 mch_memmove(r + i * slen, p, (size_t)slen);
13702 r[len] = NUL;
13703 }
13704
13705 rettv->vval.v_string = r;
13706 }
13707}
13708
13709/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013710 * "resolve()" function
13711 */
13712 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013713f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013714 typval_T *argvars;
13715 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013716{
13717 char_u *p;
13718
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013719 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013720#ifdef FEAT_SHORTCUT
13721 {
13722 char_u *v = NULL;
13723
13724 v = mch_resolve_shortcut(p);
13725 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013726 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013727 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013728 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013729 }
13730#else
13731# ifdef HAVE_READLINK
13732 {
13733 char_u buf[MAXPATHL + 1];
13734 char_u *cpy;
13735 int len;
13736 char_u *remain = NULL;
13737 char_u *q;
13738 int is_relative_to_current = FALSE;
13739 int has_trailing_pathsep = FALSE;
13740 int limit = 100;
13741
13742 p = vim_strsave(p);
13743
13744 if (p[0] == '.' && (vim_ispathsep(p[1])
13745 || (p[1] == '.' && (vim_ispathsep(p[2])))))
13746 is_relative_to_current = TRUE;
13747
13748 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013749 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013750 has_trailing_pathsep = TRUE;
13751
13752 q = getnextcomp(p);
13753 if (*q != NUL)
13754 {
13755 /* Separate the first path component in "p", and keep the
13756 * remainder (beginning with the path separator). */
13757 remain = vim_strsave(q - 1);
13758 q[-1] = NUL;
13759 }
13760
13761 for (;;)
13762 {
13763 for (;;)
13764 {
13765 len = readlink((char *)p, (char *)buf, MAXPATHL);
13766 if (len <= 0)
13767 break;
13768 buf[len] = NUL;
13769
13770 if (limit-- == 0)
13771 {
13772 vim_free(p);
13773 vim_free(remain);
13774 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013775 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013776 goto fail;
13777 }
13778
13779 /* Ensure that the result will have a trailing path separator
13780 * if the argument has one. */
13781 if (remain == NULL && has_trailing_pathsep)
13782 add_pathsep(buf);
13783
13784 /* Separate the first path component in the link value and
13785 * concatenate the remainders. */
13786 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
13787 if (*q != NUL)
13788 {
13789 if (remain == NULL)
13790 remain = vim_strsave(q - 1);
13791 else
13792 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000013793 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013794 if (cpy != NULL)
13795 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013796 vim_free(remain);
13797 remain = cpy;
13798 }
13799 }
13800 q[-1] = NUL;
13801 }
13802
13803 q = gettail(p);
13804 if (q > p && *q == NUL)
13805 {
13806 /* Ignore trailing path separator. */
13807 q[-1] = NUL;
13808 q = gettail(p);
13809 }
13810 if (q > p && !mch_isFullName(buf))
13811 {
13812 /* symlink is relative to directory of argument */
13813 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
13814 if (cpy != NULL)
13815 {
13816 STRCPY(cpy, p);
13817 STRCPY(gettail(cpy), buf);
13818 vim_free(p);
13819 p = cpy;
13820 }
13821 }
13822 else
13823 {
13824 vim_free(p);
13825 p = vim_strsave(buf);
13826 }
13827 }
13828
13829 if (remain == NULL)
13830 break;
13831
13832 /* Append the first path component of "remain" to "p". */
13833 q = getnextcomp(remain + 1);
13834 len = q - remain - (*q != NUL);
13835 cpy = vim_strnsave(p, STRLEN(p) + len);
13836 if (cpy != NULL)
13837 {
13838 STRNCAT(cpy, remain, len);
13839 vim_free(p);
13840 p = cpy;
13841 }
13842 /* Shorten "remain". */
13843 if (*q != NUL)
Bram Moolenaar452a81b2007-08-06 20:28:43 +000013844 mch_memmove(remain, q - 1, STRLEN(q - 1) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013845 else
13846 {
13847 vim_free(remain);
13848 remain = NULL;
13849 }
13850 }
13851
13852 /* If the result is a relative path name, make it explicitly relative to
13853 * the current directory if and only if the argument had this form. */
13854 if (!vim_ispathsep(*p))
13855 {
13856 if (is_relative_to_current
13857 && *p != NUL
13858 && !(p[0] == '.'
13859 && (p[1] == NUL
13860 || vim_ispathsep(p[1])
13861 || (p[1] == '.'
13862 && (p[2] == NUL
13863 || vim_ispathsep(p[2]))))))
13864 {
13865 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013866 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013867 if (cpy != NULL)
13868 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013869 vim_free(p);
13870 p = cpy;
13871 }
13872 }
13873 else if (!is_relative_to_current)
13874 {
13875 /* Strip leading "./". */
13876 q = p;
13877 while (q[0] == '.' && vim_ispathsep(q[1]))
13878 q += 2;
13879 if (q > p)
13880 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
13881 }
13882 }
13883
13884 /* Ensure that the result will have no trailing path separator
13885 * if the argument had none. But keep "/" or "//". */
13886 if (!has_trailing_pathsep)
13887 {
13888 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013889 if (after_pathsep(p, q))
13890 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013891 }
13892
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013893 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013894 }
13895# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013896 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013897# endif
13898#endif
13899
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013900 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013901
13902#ifdef HAVE_READLINK
13903fail:
13904#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013905 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013906}
13907
13908/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013909 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013910 */
13911 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013912f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013913 typval_T *argvars;
13914 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013915{
Bram Moolenaar33570922005-01-25 22:26:29 +000013916 list_T *l;
13917 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013918
Bram Moolenaar0d660222005-01-07 21:51:51 +000013919 rettv->vval.v_number = 0;
13920 if (argvars[0].v_type != VAR_LIST)
13921 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013922 else if ((l = argvars[0].vval.v_list) != NULL
13923 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013924 {
13925 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013926 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013927 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013928 while (li != NULL)
13929 {
13930 ni = li->li_prev;
13931 list_append(l, li);
13932 li = ni;
13933 }
13934 rettv->vval.v_list = l;
13935 rettv->v_type = VAR_LIST;
13936 ++l->lv_refcount;
13937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013938}
13939
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013940#define SP_NOMOVE 0x01 /* don't move cursor */
13941#define SP_REPEAT 0x02 /* repeat to find outer pair */
13942#define SP_RETCOUNT 0x04 /* return matchcount */
13943#define SP_SETPCMARK 0x08 /* set previous context mark */
13944#define SP_START 0x10 /* accept match at start position */
13945#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
13946#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013947
Bram Moolenaar33570922005-01-25 22:26:29 +000013948static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013949
13950/*
13951 * Get flags for a search function.
13952 * Possibly sets "p_ws".
13953 * Returns BACKWARD, FORWARD or zero (for an error).
13954 */
13955 static int
13956get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013957 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013958 int *flagsp;
13959{
13960 int dir = FORWARD;
13961 char_u *flags;
13962 char_u nbuf[NUMBUFLEN];
13963 int mask;
13964
13965 if (varp->v_type != VAR_UNKNOWN)
13966 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013967 flags = get_tv_string_buf_chk(varp, nbuf);
13968 if (flags == NULL)
13969 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013970 while (*flags != NUL)
13971 {
13972 switch (*flags)
13973 {
13974 case 'b': dir = BACKWARD; break;
13975 case 'w': p_ws = TRUE; break;
13976 case 'W': p_ws = FALSE; break;
13977 default: mask = 0;
13978 if (flagsp != NULL)
13979 switch (*flags)
13980 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013981 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013982 case 'e': mask = SP_END; break;
13983 case 'm': mask = SP_RETCOUNT; break;
13984 case 'n': mask = SP_NOMOVE; break;
13985 case 'p': mask = SP_SUBPAT; break;
13986 case 'r': mask = SP_REPEAT; break;
13987 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013988 }
13989 if (mask == 0)
13990 {
13991 EMSG2(_(e_invarg2), flags);
13992 dir = 0;
13993 }
13994 else
13995 *flagsp |= mask;
13996 }
13997 if (dir == 0)
13998 break;
13999 ++flags;
14000 }
14001 }
14002 return dir;
14003}
14004
Bram Moolenaar071d4272004-06-13 20:20:40 +000014005/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014006 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000014007 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014008 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014009search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014010 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014011 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014012 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014013{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014014 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014015 char_u *pat;
14016 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014017 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014018 int save_p_ws = p_ws;
14019 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014020 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014021 long lnum_stop = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014022 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014023 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014024
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014025 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014026 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014027 if (dir == 0)
14028 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014029 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014030 if (flags & SP_START)
14031 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014032 if (flags & SP_END)
14033 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014034
14035 /* Optional extra argument: line number to stop searching. */
14036 if (argvars[1].v_type != VAR_UNKNOWN
14037 && argvars[2].v_type != VAR_UNKNOWN)
14038 {
14039 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
14040 if (lnum_stop < 0)
14041 goto theend;
14042 }
14043
Bram Moolenaar231334e2005-07-25 20:46:57 +000014044 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014045 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000014046 * Check to make sure only those flags are set.
14047 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
14048 * flags cannot be set. Check for that condition also.
14049 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014050 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014051 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014052 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014053 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014054 goto theend;
14055 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014056
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014057 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014058 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
14059 options, RE_SEARCH, (linenr_T)lnum_stop);
14060 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014061 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014062 if (flags & SP_SUBPAT)
14063 retval = subpatnum;
14064 else
14065 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000014066 if (flags & SP_SETPCMARK)
14067 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014068 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014069 if (match_pos != NULL)
14070 {
14071 /* Store the match cursor position */
14072 match_pos->lnum = pos.lnum;
14073 match_pos->col = pos.col + 1;
14074 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014075 /* "/$" will put the cursor after the end of the line, may need to
14076 * correct that here */
14077 check_cursor();
14078 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014079
14080 /* If 'n' flag is used: restore cursor position. */
14081 if (flags & SP_NOMOVE)
14082 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000014083 else
14084 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014085theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000014086 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014087
14088 return retval;
14089}
14090
14091/*
14092 * "search()" function
14093 */
14094 static void
14095f_search(argvars, rettv)
14096 typval_T *argvars;
14097 typval_T *rettv;
14098{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014099 int flags = 0;
14100
14101 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014102}
14103
Bram Moolenaar071d4272004-06-13 20:20:40 +000014104/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014105 * "searchdecl()" function
14106 */
14107 static void
14108f_searchdecl(argvars, rettv)
14109 typval_T *argvars;
14110 typval_T *rettv;
14111{
14112 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000014113 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014114 int error = FALSE;
14115 char_u *name;
14116
14117 rettv->vval.v_number = 1; /* default: FAIL */
14118
14119 name = get_tv_string_chk(&argvars[0]);
14120 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000014121 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014122 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000014123 if (!error && argvars[2].v_type != VAR_UNKNOWN)
14124 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
14125 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014126 if (!error && name != NULL)
14127 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000014128 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014129}
14130
14131/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014132 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000014133 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014134 static int
14135searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000014136 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014137 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014138{
14139 char_u *spat, *mpat, *epat;
14140 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014141 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014142 int dir;
14143 int flags = 0;
14144 char_u nbuf1[NUMBUFLEN];
14145 char_u nbuf2[NUMBUFLEN];
14146 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014147 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014148 long lnum_stop = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014149
Bram Moolenaar071d4272004-06-13 20:20:40 +000014150 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014151 spat = get_tv_string_chk(&argvars[0]);
14152 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
14153 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
14154 if (spat == NULL || mpat == NULL || epat == NULL)
14155 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014156
Bram Moolenaar071d4272004-06-13 20:20:40 +000014157 /* Handle the optional fourth argument: flags */
14158 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014159 if (dir == 0)
14160 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014161
14162 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000014163 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
14164 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014165 if ((flags & (SP_END | SP_SUBPAT)) != 0
14166 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000014167 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014168 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000014169 goto theend;
14170 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014171
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014172 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014173 if (argvars[3].v_type == VAR_UNKNOWN
14174 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014175 skip = (char_u *)"";
14176 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014177 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014178 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014179 if (argvars[5].v_type != VAR_UNKNOWN)
14180 {
14181 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
14182 if (lnum_stop < 0)
14183 goto theend;
14184 }
14185 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014186 if (skip == NULL)
14187 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014188
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014189 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
14190 match_pos, lnum_stop);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014191
14192theend:
14193 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014194
14195 return retval;
14196}
14197
14198/*
14199 * "searchpair()" function
14200 */
14201 static void
14202f_searchpair(argvars, rettv)
14203 typval_T *argvars;
14204 typval_T *rettv;
14205{
14206 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
14207}
14208
14209/*
14210 * "searchpairpos()" function
14211 */
14212 static void
14213f_searchpairpos(argvars, rettv)
14214 typval_T *argvars;
14215 typval_T *rettv;
14216{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014217 pos_T match_pos;
14218 int lnum = 0;
14219 int col = 0;
14220
14221 rettv->vval.v_number = 0;
14222
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014223 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014224 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014225
14226 if (searchpair_cmn(argvars, &match_pos) > 0)
14227 {
14228 lnum = match_pos.lnum;
14229 col = match_pos.col;
14230 }
14231
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014232 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
14233 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014234}
14235
14236/*
14237 * Search for a start/middle/end thing.
14238 * Used by searchpair(), see its documentation for the details.
14239 * Returns 0 or -1 for no match,
14240 */
14241 long
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014242do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos, lnum_stop)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014243 char_u *spat; /* start pattern */
14244 char_u *mpat; /* middle pattern */
14245 char_u *epat; /* end pattern */
14246 int dir; /* BACKWARD or FORWARD */
14247 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014248 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014249 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014250 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014251{
14252 char_u *save_cpo;
14253 char_u *pat, *pat2 = NULL, *pat3 = NULL;
14254 long retval = 0;
14255 pos_T pos;
14256 pos_T firstpos;
14257 pos_T foundpos;
14258 pos_T save_cursor;
14259 pos_T save_pos;
14260 int n;
14261 int r;
14262 int nest = 1;
14263 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014264 int options = SEARCH_KEEP;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014265
14266 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14267 save_cpo = p_cpo;
14268 p_cpo = (char_u *)"";
14269
14270 /* Make two search patterns: start/end (pat2, for in nested pairs) and
14271 * start/middle/end (pat3, for the top pair). */
14272 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
14273 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
14274 if (pat2 == NULL || pat3 == NULL)
14275 goto theend;
14276 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
14277 if (*mpat == NUL)
14278 STRCPY(pat3, pat2);
14279 else
14280 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
14281 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014282 if (flags & SP_START)
14283 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014284
Bram Moolenaar071d4272004-06-13 20:20:40 +000014285 save_cursor = curwin->w_cursor;
14286 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000014287 clearpos(&firstpos);
14288 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014289 pat = pat3;
14290 for (;;)
14291 {
14292 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014293 options, RE_SEARCH, lnum_stop);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014294 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
14295 /* didn't find it or found the first match again: FAIL */
14296 break;
14297
14298 if (firstpos.lnum == 0)
14299 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000014300 if (equalpos(pos, foundpos))
14301 {
14302 /* Found the same position again. Can happen with a pattern that
14303 * has "\zs" at the end and searching backwards. Advance one
14304 * character and try again. */
14305 if (dir == BACKWARD)
14306 decl(&pos);
14307 else
14308 incl(&pos);
14309 }
14310 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014311
14312 /* If the skip pattern matches, ignore this match. */
14313 if (*skip != NUL)
14314 {
14315 save_pos = curwin->w_cursor;
14316 curwin->w_cursor = pos;
14317 r = eval_to_bool(skip, &err, NULL, FALSE);
14318 curwin->w_cursor = save_pos;
14319 if (err)
14320 {
14321 /* Evaluating {skip} caused an error, break here. */
14322 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014323 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014324 break;
14325 }
14326 if (r)
14327 continue;
14328 }
14329
14330 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
14331 {
14332 /* Found end when searching backwards or start when searching
14333 * forward: nested pair. */
14334 ++nest;
14335 pat = pat2; /* nested, don't search for middle */
14336 }
14337 else
14338 {
14339 /* Found end when searching forward or start when searching
14340 * backward: end of (nested) pair; or found middle in outer pair. */
14341 if (--nest == 1)
14342 pat = pat3; /* outer level, search for middle */
14343 }
14344
14345 if (nest == 0)
14346 {
14347 /* Found the match: return matchcount or line number. */
14348 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014349 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014350 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014351 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000014352 if (flags & SP_SETPCMARK)
14353 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014354 curwin->w_cursor = pos;
14355 if (!(flags & SP_REPEAT))
14356 break;
14357 nest = 1; /* search for next unmatched */
14358 }
14359 }
14360
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014361 if (match_pos != NULL)
14362 {
14363 /* Store the match cursor position */
14364 match_pos->lnum = curwin->w_cursor.lnum;
14365 match_pos->col = curwin->w_cursor.col + 1;
14366 }
14367
Bram Moolenaar071d4272004-06-13 20:20:40 +000014368 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014369 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014370 curwin->w_cursor = save_cursor;
14371
14372theend:
14373 vim_free(pat2);
14374 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014375 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014376
14377 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014378}
14379
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014380/*
14381 * "searchpos()" function
14382 */
14383 static void
14384f_searchpos(argvars, rettv)
14385 typval_T *argvars;
14386 typval_T *rettv;
14387{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014388 pos_T match_pos;
14389 int lnum = 0;
14390 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014391 int n;
14392 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014393
14394 rettv->vval.v_number = 0;
14395
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014396 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014397 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014398
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014399 n = search_cmn(argvars, &match_pos, &flags);
14400 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014401 {
14402 lnum = match_pos.lnum;
14403 col = match_pos.col;
14404 }
14405
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014406 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
14407 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014408 if (flags & SP_SUBPAT)
14409 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014410}
14411
14412
Bram Moolenaar0d660222005-01-07 21:51:51 +000014413/*ARGSUSED*/
14414 static void
14415f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014416 typval_T *argvars;
14417 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014418{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014419#ifdef FEAT_CLIENTSERVER
14420 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014421 char_u *server = get_tv_string_chk(&argvars[0]);
14422 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014423
Bram Moolenaar0d660222005-01-07 21:51:51 +000014424 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014425 if (server == NULL || reply == NULL)
14426 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014427 if (check_restricted() || check_secure())
14428 return;
14429# ifdef FEAT_X11
14430 if (check_connection() == FAIL)
14431 return;
14432# endif
14433
14434 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014435 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014436 EMSG(_("E258: Unable to send to client"));
14437 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014438 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014439 rettv->vval.v_number = 0;
14440#else
14441 rettv->vval.v_number = -1;
14442#endif
14443}
14444
14445/*ARGSUSED*/
14446 static void
14447f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014448 typval_T *argvars;
14449 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014450{
14451 char_u *r = NULL;
14452
14453#ifdef FEAT_CLIENTSERVER
14454# ifdef WIN32
14455 r = serverGetVimNames();
14456# else
14457 make_connection();
14458 if (X_DISPLAY != NULL)
14459 r = serverGetVimNames(X_DISPLAY);
14460# endif
14461#endif
14462 rettv->v_type = VAR_STRING;
14463 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014464}
14465
14466/*
14467 * "setbufvar()" function
14468 */
14469/*ARGSUSED*/
14470 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014471f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014472 typval_T *argvars;
14473 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014474{
14475 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014476 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014477 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014478 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014479 char_u nbuf[NUMBUFLEN];
14480
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014481 rettv->vval.v_number = 0;
14482
Bram Moolenaar071d4272004-06-13 20:20:40 +000014483 if (check_restricted() || check_secure())
14484 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014485 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
14486 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014487 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014488 varp = &argvars[2];
14489
14490 if (buf != NULL && varname != NULL && varp != NULL)
14491 {
14492 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014493 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014494
14495 if (*varname == '&')
14496 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014497 long numval;
14498 char_u *strval;
14499 int error = FALSE;
14500
Bram Moolenaar071d4272004-06-13 20:20:40 +000014501 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014502 numval = get_tv_number_chk(varp, &error);
14503 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014504 if (!error && strval != NULL)
14505 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014506 }
14507 else
14508 {
14509 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
14510 if (bufvarname != NULL)
14511 {
14512 STRCPY(bufvarname, "b:");
14513 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014514 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014515 vim_free(bufvarname);
14516 }
14517 }
14518
14519 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014520 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014522}
14523
14524/*
14525 * "setcmdpos()" function
14526 */
14527 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014528f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014529 typval_T *argvars;
14530 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014531{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014532 int pos = (int)get_tv_number(&argvars[0]) - 1;
14533
14534 if (pos >= 0)
14535 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014536}
14537
14538/*
14539 * "setline()" function
14540 */
14541 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014542f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014543 typval_T *argvars;
14544 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014545{
14546 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000014547 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014548 list_T *l = NULL;
14549 listitem_T *li = NULL;
14550 long added = 0;
14551 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014552
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014553 lnum = get_tv_lnum(&argvars[0]);
14554 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014555 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014556 l = argvars[1].vval.v_list;
14557 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014558 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014559 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014560 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014561
14562 rettv->vval.v_number = 0; /* OK */
14563 for (;;)
14564 {
14565 if (l != NULL)
14566 {
14567 /* list argument, get next string */
14568 if (li == NULL)
14569 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014570 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014571 li = li->li_next;
14572 }
14573
14574 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014575 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014576 break;
14577 if (lnum <= curbuf->b_ml.ml_line_count)
14578 {
14579 /* existing line, replace it */
14580 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
14581 {
14582 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000014583 if (lnum == curwin->w_cursor.lnum)
14584 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014585 rettv->vval.v_number = 0; /* OK */
14586 }
14587 }
14588 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
14589 {
14590 /* lnum is one past the last line, append the line */
14591 ++added;
14592 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
14593 rettv->vval.v_number = 0; /* OK */
14594 }
14595
14596 if (l == NULL) /* only one string argument */
14597 break;
14598 ++lnum;
14599 }
14600
14601 if (added > 0)
14602 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014603}
14604
14605/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014606 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000014607 */
14608/*ARGSUSED*/
14609 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014610set_qf_ll_list(wp, list_arg, action_arg, rettv)
14611 win_T *wp;
14612 typval_T *list_arg;
14613 typval_T *action_arg;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014614 typval_T *rettv;
14615{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014616#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014617 char_u *act;
14618 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014619#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014620
Bram Moolenaar2641f772005-03-25 21:58:17 +000014621 rettv->vval.v_number = -1;
14622
14623#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014624 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014625 EMSG(_(e_listreq));
14626 else
14627 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014628 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014629
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014630 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014631 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014632 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014633 if (act == NULL)
14634 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014635 if (*act == 'a' || *act == 'r')
14636 action = *act;
14637 }
14638
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014639 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014640 rettv->vval.v_number = 0;
14641 }
14642#endif
14643}
14644
14645/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014646 * "setloclist()" function
14647 */
14648/*ARGSUSED*/
14649 static void
14650f_setloclist(argvars, rettv)
14651 typval_T *argvars;
14652 typval_T *rettv;
14653{
14654 win_T *win;
14655
14656 rettv->vval.v_number = -1;
14657
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014658 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014659 if (win != NULL)
14660 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
14661}
14662
14663/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014664 * "setmatches()" function
14665 */
14666 static void
14667f_setmatches(argvars, rettv)
14668 typval_T *argvars;
14669 typval_T *rettv;
14670{
14671#ifdef FEAT_SEARCH_EXTRA
14672 list_T *l;
14673 listitem_T *li;
14674 dict_T *d;
14675
14676 rettv->vval.v_number = -1;
14677 if (argvars[0].v_type != VAR_LIST)
14678 {
14679 EMSG(_(e_listreq));
14680 return;
14681 }
14682 if ((l = argvars[0].vval.v_list) != NULL)
14683 {
14684
14685 /* To some extent make sure that we are dealing with a list from
14686 * "getmatches()". */
14687 li = l->lv_first;
14688 while (li != NULL)
14689 {
14690 if (li->li_tv.v_type != VAR_DICT
14691 || (d = li->li_tv.vval.v_dict) == NULL)
14692 {
14693 EMSG(_(e_invarg));
14694 return;
14695 }
14696 if (!(dict_find(d, (char_u *)"group", -1) != NULL
14697 && dict_find(d, (char_u *)"pattern", -1) != NULL
14698 && dict_find(d, (char_u *)"priority", -1) != NULL
14699 && dict_find(d, (char_u *)"id", -1) != NULL))
14700 {
14701 EMSG(_(e_invarg));
14702 return;
14703 }
14704 li = li->li_next;
14705 }
14706
14707 clear_matches(curwin);
14708 li = l->lv_first;
14709 while (li != NULL)
14710 {
14711 d = li->li_tv.vval.v_dict;
14712 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
14713 get_dict_string(d, (char_u *)"pattern", FALSE),
14714 (int)get_dict_number(d, (char_u *)"priority"),
14715 (int)get_dict_number(d, (char_u *)"id"));
14716 li = li->li_next;
14717 }
14718 rettv->vval.v_number = 0;
14719 }
14720#endif
14721}
14722
14723/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014724 * "setpos()" function
14725 */
14726/*ARGSUSED*/
14727 static void
14728f_setpos(argvars, rettv)
14729 typval_T *argvars;
14730 typval_T *rettv;
14731{
14732 pos_T pos;
14733 int fnum;
14734 char_u *name;
14735
14736 name = get_tv_string_chk(argvars);
14737 if (name != NULL)
14738 {
14739 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
14740 {
14741 --pos.col;
14742 if (name[0] == '.') /* cursor */
14743 {
14744 if (fnum == curbuf->b_fnum)
14745 {
14746 curwin->w_cursor = pos;
14747 check_cursor();
14748 }
14749 else
14750 EMSG(_(e_invarg));
14751 }
14752 else if (name[0] == '\'') /* mark */
14753 (void)setmark_pos(name[1], &pos, fnum);
14754 else
14755 EMSG(_(e_invarg));
14756 }
14757 }
14758}
14759
14760/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014761 * "setqflist()" function
14762 */
14763/*ARGSUSED*/
14764 static void
14765f_setqflist(argvars, rettv)
14766 typval_T *argvars;
14767 typval_T *rettv;
14768{
14769 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
14770}
14771
14772/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014773 * "setreg()" function
14774 */
14775 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014776f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014777 typval_T *argvars;
14778 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014779{
14780 int regname;
14781 char_u *strregname;
14782 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014783 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014784 int append;
14785 char_u yank_type;
14786 long block_len;
14787
14788 block_len = -1;
14789 yank_type = MAUTO;
14790 append = FALSE;
14791
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014792 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014793 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014794
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014795 if (strregname == NULL)
14796 return; /* type error; errmsg already given */
14797 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014798 if (regname == 0 || regname == '@')
14799 regname = '"';
14800 else if (regname == '=')
14801 return;
14802
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014803 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014804 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014805 stropt = get_tv_string_chk(&argvars[2]);
14806 if (stropt == NULL)
14807 return; /* type error */
14808 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014809 switch (*stropt)
14810 {
14811 case 'a': case 'A': /* append */
14812 append = TRUE;
14813 break;
14814 case 'v': case 'c': /* character-wise selection */
14815 yank_type = MCHAR;
14816 break;
14817 case 'V': case 'l': /* line-wise selection */
14818 yank_type = MLINE;
14819 break;
14820#ifdef FEAT_VISUAL
14821 case 'b': case Ctrl_V: /* block-wise selection */
14822 yank_type = MBLOCK;
14823 if (VIM_ISDIGIT(stropt[1]))
14824 {
14825 ++stropt;
14826 block_len = getdigits(&stropt) - 1;
14827 --stropt;
14828 }
14829 break;
14830#endif
14831 }
14832 }
14833
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014834 strval = get_tv_string_chk(&argvars[1]);
14835 if (strval != NULL)
14836 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000014837 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014838 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014839}
14840
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014841/*
14842 * "settabwinvar()" function
14843 */
14844 static void
14845f_settabwinvar(argvars, rettv)
14846 typval_T *argvars;
14847 typval_T *rettv;
14848{
14849 setwinvar(argvars, rettv, 1);
14850}
Bram Moolenaar071d4272004-06-13 20:20:40 +000014851
14852/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014853 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014854 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014855 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014856f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014857 typval_T *argvars;
14858 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014859{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014860 setwinvar(argvars, rettv, 0);
14861}
14862
14863/*
14864 * "setwinvar()" and "settabwinvar()" functions
14865 */
14866 static void
14867setwinvar(argvars, rettv, off)
14868 typval_T *argvars;
14869 typval_T *rettv;
14870 int off;
14871{
Bram Moolenaar071d4272004-06-13 20:20:40 +000014872 win_T *win;
14873#ifdef FEAT_WINDOWS
14874 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014875 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014876#endif
14877 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014878 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014879 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014880 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014881
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014882 rettv->vval.v_number = 0;
14883
Bram Moolenaar071d4272004-06-13 20:20:40 +000014884 if (check_restricted() || check_secure())
14885 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014886
14887#ifdef FEAT_WINDOWS
14888 if (off == 1)
14889 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
14890 else
14891 tp = curtab;
14892#endif
14893 win = find_win_by_nr(&argvars[off], tp);
14894 varname = get_tv_string_chk(&argvars[off + 1]);
14895 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014896
14897 if (win != NULL && varname != NULL && varp != NULL)
14898 {
14899#ifdef FEAT_WINDOWS
14900 /* set curwin to be our win, temporarily */
14901 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014902 save_curtab = curtab;
14903 goto_tabpage_tp(tp);
14904 if (!win_valid(win))
14905 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014906 curwin = win;
14907 curbuf = curwin->w_buffer;
14908#endif
14909
14910 if (*varname == '&')
14911 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014912 long numval;
14913 char_u *strval;
14914 int error = FALSE;
14915
Bram Moolenaar071d4272004-06-13 20:20:40 +000014916 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014917 numval = get_tv_number_chk(varp, &error);
14918 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014919 if (!error && strval != NULL)
14920 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014921 }
14922 else
14923 {
14924 winvarname = alloc((unsigned)STRLEN(varname) + 3);
14925 if (winvarname != NULL)
14926 {
14927 STRCPY(winvarname, "w:");
14928 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014929 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014930 vim_free(winvarname);
14931 }
14932 }
14933
14934#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014935 /* Restore current tabpage and window, if still valid (autocomands can
14936 * make them invalid). */
14937 if (valid_tabpage(save_curtab))
14938 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014939 if (win_valid(save_curwin))
14940 {
14941 curwin = save_curwin;
14942 curbuf = curwin->w_buffer;
14943 }
14944#endif
14945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014946}
14947
14948/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000014949 * "shellescape({string})" function
14950 */
14951 static void
14952f_shellescape(argvars, rettv)
14953 typval_T *argvars;
14954 typval_T *rettv;
14955{
14956 rettv->vval.v_string = vim_strsave_shellescape(get_tv_string(&argvars[0]));
14957 rettv->v_type = VAR_STRING;
14958}
14959
14960/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014961 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014962 */
14963 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014964f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014965 typval_T *argvars;
14966 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014967{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014968 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014969
Bram Moolenaar0d660222005-01-07 21:51:51 +000014970 p = get_tv_string(&argvars[0]);
14971 rettv->vval.v_string = vim_strsave(p);
14972 simplify_filename(rettv->vval.v_string); /* simplify in place */
14973 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014974}
14975
Bram Moolenaar0d660222005-01-07 21:51:51 +000014976static int
14977#ifdef __BORLANDC__
14978 _RTLENTRYF
14979#endif
14980 item_compare __ARGS((const void *s1, const void *s2));
14981static int
14982#ifdef __BORLANDC__
14983 _RTLENTRYF
14984#endif
14985 item_compare2 __ARGS((const void *s1, const void *s2));
14986
14987static int item_compare_ic;
14988static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014989static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014990#define ITEM_COMPARE_FAIL 999
14991
Bram Moolenaar071d4272004-06-13 20:20:40 +000014992/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014993 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014994 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014995 static int
14996#ifdef __BORLANDC__
14997_RTLENTRYF
14998#endif
14999item_compare(s1, s2)
15000 const void *s1;
15001 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015002{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015003 char_u *p1, *p2;
15004 char_u *tofree1, *tofree2;
15005 int res;
15006 char_u numbuf1[NUMBUFLEN];
15007 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015008
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015009 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
15010 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000015011 if (p1 == NULL)
15012 p1 = (char_u *)"";
15013 if (p2 == NULL)
15014 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000015015 if (item_compare_ic)
15016 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015017 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015018 res = STRCMP(p1, p2);
15019 vim_free(tofree1);
15020 vim_free(tofree2);
15021 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015022}
15023
15024 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000015025#ifdef __BORLANDC__
15026_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000015027#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000015028item_compare2(s1, s2)
15029 const void *s1;
15030 const void *s2;
15031{
15032 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000015033 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015034 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000015035 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015036
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015037 /* shortcut after failure in previous call; compare all items equal */
15038 if (item_compare_func_err)
15039 return 0;
15040
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015041 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
15042 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015043 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
15044 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015045
15046 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015047 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000015048 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015049 clear_tv(&argv[0]);
15050 clear_tv(&argv[1]);
15051
15052 if (res == FAIL)
15053 res = ITEM_COMPARE_FAIL;
15054 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015055 /* return value has wrong type */
15056 res = get_tv_number_chk(&rettv, &item_compare_func_err);
15057 if (item_compare_func_err)
15058 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015059 clear_tv(&rettv);
15060 return res;
15061}
15062
15063/*
15064 * "sort({list})" function
15065 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015066 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015067f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015068 typval_T *argvars;
15069 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015070{
Bram Moolenaar33570922005-01-25 22:26:29 +000015071 list_T *l;
15072 listitem_T *li;
15073 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015074 long len;
15075 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015076
Bram Moolenaar0d660222005-01-07 21:51:51 +000015077 rettv->vval.v_number = 0;
15078 if (argvars[0].v_type != VAR_LIST)
15079 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000015080 else
15081 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015082 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015083 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015084 return;
15085 rettv->vval.v_list = l;
15086 rettv->v_type = VAR_LIST;
15087 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015088
Bram Moolenaar0d660222005-01-07 21:51:51 +000015089 len = list_len(l);
15090 if (len <= 1)
15091 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015092
Bram Moolenaar0d660222005-01-07 21:51:51 +000015093 item_compare_ic = FALSE;
15094 item_compare_func = NULL;
15095 if (argvars[1].v_type != VAR_UNKNOWN)
15096 {
15097 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015098 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015099 else
15100 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015101 int error = FALSE;
15102
15103 i = get_tv_number_chk(&argvars[1], &error);
15104 if (error)
15105 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015106 if (i == 1)
15107 item_compare_ic = TRUE;
15108 else
15109 item_compare_func = get_tv_string(&argvars[1]);
15110 }
15111 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015112
Bram Moolenaar0d660222005-01-07 21:51:51 +000015113 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015114 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015115 if (ptrs == NULL)
15116 return;
15117 i = 0;
15118 for (li = l->lv_first; li != NULL; li = li->li_next)
15119 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015120
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015121 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015122 /* test the compare function */
15123 if (item_compare_func != NULL
15124 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
15125 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015126 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015127 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015128 {
15129 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015130 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000015131 item_compare_func == NULL ? item_compare : item_compare2);
15132
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015133 if (!item_compare_func_err)
15134 {
15135 /* Clear the List and append the items in the sorted order. */
15136 l->lv_first = l->lv_last = NULL;
15137 l->lv_len = 0;
15138 for (i = 0; i < len; ++i)
15139 list_append(l, ptrs[i]);
15140 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015141 }
15142
15143 vim_free(ptrs);
15144 }
15145}
15146
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015147/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000015148 * "soundfold({word})" function
15149 */
15150 static void
15151f_soundfold(argvars, rettv)
15152 typval_T *argvars;
15153 typval_T *rettv;
15154{
15155 char_u *s;
15156
15157 rettv->v_type = VAR_STRING;
15158 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015159#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000015160 rettv->vval.v_string = eval_soundfold(s);
15161#else
15162 rettv->vval.v_string = vim_strsave(s);
15163#endif
15164}
15165
15166/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015167 * "spellbadword()" function
15168 */
15169/* ARGSUSED */
15170 static void
15171f_spellbadword(argvars, rettv)
15172 typval_T *argvars;
15173 typval_T *rettv;
15174{
Bram Moolenaar4463f292005-09-25 22:20:24 +000015175 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000015176 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015177 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015178
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015179 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000015180 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015181
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015182#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000015183 if (argvars[0].v_type == VAR_UNKNOWN)
15184 {
15185 /* Find the start and length of the badly spelled word. */
15186 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
15187 if (len != 0)
15188 word = ml_get_cursor();
15189 }
15190 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
15191 {
15192 char_u *str = get_tv_string_chk(&argvars[0]);
15193 int capcol = -1;
15194
15195 if (str != NULL)
15196 {
15197 /* Check the argument for spelling. */
15198 while (*str != NUL)
15199 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000015200 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000015201 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000015202 {
15203 word = str;
15204 break;
15205 }
15206 str += len;
15207 }
15208 }
15209 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015210#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000015211
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015212 list_append_string(rettv->vval.v_list, word, len);
15213 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000015214 attr == HLF_SPB ? "bad" :
15215 attr == HLF_SPR ? "rare" :
15216 attr == HLF_SPL ? "local" :
15217 attr == HLF_SPC ? "caps" :
15218 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015219}
15220
15221/*
15222 * "spellsuggest()" function
15223 */
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015224/*ARGSUSED*/
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015225 static void
15226f_spellsuggest(argvars, rettv)
15227 typval_T *argvars;
15228 typval_T *rettv;
15229{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015230#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015231 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015232 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015233 int maxcount;
15234 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015235 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015236 listitem_T *li;
15237 int need_capital = FALSE;
15238#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015239
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015240 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015241 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015242
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015243#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015244 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
15245 {
15246 str = get_tv_string(&argvars[0]);
15247 if (argvars[1].v_type != VAR_UNKNOWN)
15248 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015249 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015250 if (maxcount <= 0)
15251 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015252 if (argvars[2].v_type != VAR_UNKNOWN)
15253 {
15254 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
15255 if (typeerr)
15256 return;
15257 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015258 }
15259 else
15260 maxcount = 25;
15261
Bram Moolenaar4770d092006-01-12 23:22:24 +000015262 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015263
15264 for (i = 0; i < ga.ga_len; ++i)
15265 {
15266 str = ((char_u **)ga.ga_data)[i];
15267
15268 li = listitem_alloc();
15269 if (li == NULL)
15270 vim_free(str);
15271 else
15272 {
15273 li->li_tv.v_type = VAR_STRING;
15274 li->li_tv.v_lock = 0;
15275 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015276 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015277 }
15278 }
15279 ga_clear(&ga);
15280 }
15281#endif
15282}
15283
Bram Moolenaar0d660222005-01-07 21:51:51 +000015284 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015285f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015286 typval_T *argvars;
15287 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015288{
15289 char_u *str;
15290 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015291 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015292 regmatch_T regmatch;
15293 char_u patbuf[NUMBUFLEN];
15294 char_u *save_cpo;
15295 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015296 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015297 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015298 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015299
15300 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15301 save_cpo = p_cpo;
15302 p_cpo = (char_u *)"";
15303
15304 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015305 if (argvars[1].v_type != VAR_UNKNOWN)
15306 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015307 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15308 if (pat == NULL)
15309 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015310 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015311 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015312 }
15313 if (pat == NULL || *pat == NUL)
15314 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000015315
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015316 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015317 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015318 if (typeerr)
15319 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015320
Bram Moolenaar0d660222005-01-07 21:51:51 +000015321 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15322 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015323 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015324 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015325 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015326 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015327 if (*str == NUL)
15328 match = FALSE; /* empty item at the end */
15329 else
15330 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015331 if (match)
15332 end = regmatch.startp[0];
15333 else
15334 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015335 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
15336 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015337 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015338 if (list_append_string(rettv->vval.v_list, str,
15339 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015340 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015341 }
15342 if (!match)
15343 break;
15344 /* Advance to just after the match. */
15345 if (regmatch.endp[0] > str)
15346 col = 0;
15347 else
15348 {
15349 /* Don't get stuck at the same match. */
15350#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015351 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015352#else
15353 col = 1;
15354#endif
15355 }
15356 str = regmatch.endp[0];
15357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015358
Bram Moolenaar0d660222005-01-07 21:51:51 +000015359 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015360 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015361
Bram Moolenaar0d660222005-01-07 21:51:51 +000015362 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015363}
15364
Bram Moolenaar2c932302006-03-18 21:42:09 +000015365/*
15366 * "str2nr()" function
15367 */
15368 static void
15369f_str2nr(argvars, rettv)
15370 typval_T *argvars;
15371 typval_T *rettv;
15372{
15373 int base = 10;
15374 char_u *p;
15375 long n;
15376
15377 if (argvars[1].v_type != VAR_UNKNOWN)
15378 {
15379 base = get_tv_number(&argvars[1]);
15380 if (base != 8 && base != 10 && base != 16)
15381 {
15382 EMSG(_(e_invarg));
15383 return;
15384 }
15385 }
15386
15387 p = skipwhite(get_tv_string(&argvars[0]));
15388 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
15389 rettv->vval.v_number = n;
15390}
15391
Bram Moolenaar071d4272004-06-13 20:20:40 +000015392#ifdef HAVE_STRFTIME
15393/*
15394 * "strftime({format}[, {time}])" function
15395 */
15396 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015397f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015398 typval_T *argvars;
15399 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015400{
15401 char_u result_buf[256];
15402 struct tm *curtime;
15403 time_t seconds;
15404 char_u *p;
15405
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015406 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015407
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015408 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015409 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015410 seconds = time(NULL);
15411 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015412 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015413 curtime = localtime(&seconds);
15414 /* MSVC returns NULL for an invalid value of seconds. */
15415 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015416 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015417 else
15418 {
15419# ifdef FEAT_MBYTE
15420 vimconv_T conv;
15421 char_u *enc;
15422
15423 conv.vc_type = CONV_NONE;
15424 enc = enc_locale();
15425 convert_setup(&conv, p_enc, enc);
15426 if (conv.vc_type != CONV_NONE)
15427 p = string_convert(&conv, p, NULL);
15428# endif
15429 if (p != NULL)
15430 (void)strftime((char *)result_buf, sizeof(result_buf),
15431 (char *)p, curtime);
15432 else
15433 result_buf[0] = NUL;
15434
15435# ifdef FEAT_MBYTE
15436 if (conv.vc_type != CONV_NONE)
15437 vim_free(p);
15438 convert_setup(&conv, enc, p_enc);
15439 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015440 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015441 else
15442# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015443 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015444
15445# ifdef FEAT_MBYTE
15446 /* Release conversion descriptors */
15447 convert_setup(&conv, NULL, NULL);
15448 vim_free(enc);
15449# endif
15450 }
15451}
15452#endif
15453
15454/*
15455 * "stridx()" function
15456 */
15457 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015458f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015459 typval_T *argvars;
15460 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015461{
15462 char_u buf[NUMBUFLEN];
15463 char_u *needle;
15464 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000015465 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015466 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000015467 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015468
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015469 needle = get_tv_string_chk(&argvars[1]);
15470 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000015471 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015472 if (needle == NULL || haystack == NULL)
15473 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015474
Bram Moolenaar33570922005-01-25 22:26:29 +000015475 if (argvars[2].v_type != VAR_UNKNOWN)
15476 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015477 int error = FALSE;
15478
15479 start_idx = get_tv_number_chk(&argvars[2], &error);
15480 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000015481 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015482 if (start_idx >= 0)
15483 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000015484 }
15485
15486 pos = (char_u *)strstr((char *)haystack, (char *)needle);
15487 if (pos != NULL)
15488 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015489}
15490
15491/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015492 * "string()" function
15493 */
15494 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015495f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015496 typval_T *argvars;
15497 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015498{
15499 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015500 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015501
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015502 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015503 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000015504 /* Make a copy if we have a value but it's not in allocate memory. */
15505 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015506 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015507}
15508
15509/*
15510 * "strlen()" function
15511 */
15512 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015513f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015514 typval_T *argvars;
15515 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015516{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015517 rettv->vval.v_number = (varnumber_T)(STRLEN(
15518 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015519}
15520
15521/*
15522 * "strpart()" function
15523 */
15524 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015525f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015526 typval_T *argvars;
15527 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015528{
15529 char_u *p;
15530 int n;
15531 int len;
15532 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015533 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015534
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015535 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015536 slen = (int)STRLEN(p);
15537
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015538 n = get_tv_number_chk(&argvars[1], &error);
15539 if (error)
15540 len = 0;
15541 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015542 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015543 else
15544 len = slen - n; /* default len: all bytes that are available. */
15545
15546 /*
15547 * Only return the overlap between the specified part and the actual
15548 * string.
15549 */
15550 if (n < 0)
15551 {
15552 len += n;
15553 n = 0;
15554 }
15555 else if (n > slen)
15556 n = slen;
15557 if (len < 0)
15558 len = 0;
15559 else if (n + len > slen)
15560 len = slen - n;
15561
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015562 rettv->v_type = VAR_STRING;
15563 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015564}
15565
15566/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015567 * "strridx()" function
15568 */
15569 static void
15570f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015571 typval_T *argvars;
15572 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015573{
15574 char_u buf[NUMBUFLEN];
15575 char_u *needle;
15576 char_u *haystack;
15577 char_u *rest;
15578 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015579 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015580
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015581 needle = get_tv_string_chk(&argvars[1]);
15582 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015583
15584 rettv->vval.v_number = -1;
15585 if (needle == NULL || haystack == NULL)
15586 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015587
15588 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000015589 if (argvars[2].v_type != VAR_UNKNOWN)
15590 {
15591 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015592 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000015593 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015594 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000015595 }
15596 else
15597 end_idx = haystack_len;
15598
Bram Moolenaar0d660222005-01-07 21:51:51 +000015599 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000015600 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015601 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000015602 lastmatch = haystack + end_idx;
15603 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015604 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000015605 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015606 for (rest = haystack; *rest != '\0'; ++rest)
15607 {
15608 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000015609 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015610 break;
15611 lastmatch = rest;
15612 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000015613 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015614
15615 if (lastmatch == NULL)
15616 rettv->vval.v_number = -1;
15617 else
15618 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
15619}
15620
15621/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015622 * "strtrans()" function
15623 */
15624 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015625f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015626 typval_T *argvars;
15627 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015628{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015629 rettv->v_type = VAR_STRING;
15630 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015631}
15632
15633/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015634 * "submatch()" function
15635 */
15636 static void
15637f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015638 typval_T *argvars;
15639 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015640{
15641 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015642 rettv->vval.v_string =
15643 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015644}
15645
15646/*
15647 * "substitute()" function
15648 */
15649 static void
15650f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015651 typval_T *argvars;
15652 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015653{
15654 char_u patbuf[NUMBUFLEN];
15655 char_u subbuf[NUMBUFLEN];
15656 char_u flagsbuf[NUMBUFLEN];
15657
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015658 char_u *str = get_tv_string_chk(&argvars[0]);
15659 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15660 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
15661 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
15662
Bram Moolenaar0d660222005-01-07 21:51:51 +000015663 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015664 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
15665 rettv->vval.v_string = NULL;
15666 else
15667 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015668}
15669
15670/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015671 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015672 */
15673/*ARGSUSED*/
15674 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015675f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015676 typval_T *argvars;
15677 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015678{
15679 int id = 0;
15680#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015681 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015682 long col;
15683 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000015684 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015685
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015686 lnum = get_tv_lnum(argvars); /* -1 on type error */
15687 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
15688 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015689
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015690 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015691 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +000015692 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015693#endif
15694
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015695 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015696}
15697
15698/*
15699 * "synIDattr(id, what [, mode])" function
15700 */
15701/*ARGSUSED*/
15702 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015703f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015704 typval_T *argvars;
15705 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015706{
15707 char_u *p = NULL;
15708#ifdef FEAT_SYN_HL
15709 int id;
15710 char_u *what;
15711 char_u *mode;
15712 char_u modebuf[NUMBUFLEN];
15713 int modec;
15714
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015715 id = get_tv_number(&argvars[0]);
15716 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015717 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015718 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015719 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015720 modec = TOLOWER_ASC(mode[0]);
15721 if (modec != 't' && modec != 'c'
15722#ifdef FEAT_GUI
15723 && modec != 'g'
15724#endif
15725 )
15726 modec = 0; /* replace invalid with current */
15727 }
15728 else
15729 {
15730#ifdef FEAT_GUI
15731 if (gui.in_use)
15732 modec = 'g';
15733 else
15734#endif
15735 if (t_colors > 1)
15736 modec = 'c';
15737 else
15738 modec = 't';
15739 }
15740
15741
15742 switch (TOLOWER_ASC(what[0]))
15743 {
15744 case 'b':
15745 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
15746 p = highlight_color(id, what, modec);
15747 else /* bold */
15748 p = highlight_has_attr(id, HL_BOLD, modec);
15749 break;
15750
15751 case 'f': /* fg[#] */
15752 p = highlight_color(id, what, modec);
15753 break;
15754
15755 case 'i':
15756 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
15757 p = highlight_has_attr(id, HL_INVERSE, modec);
15758 else /* italic */
15759 p = highlight_has_attr(id, HL_ITALIC, modec);
15760 break;
15761
15762 case 'n': /* name */
15763 p = get_highlight_name(NULL, id - 1);
15764 break;
15765
15766 case 'r': /* reverse */
15767 p = highlight_has_attr(id, HL_INVERSE, modec);
15768 break;
15769
15770 case 's': /* standout */
15771 p = highlight_has_attr(id, HL_STANDOUT, modec);
15772 break;
15773
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000015774 case 'u':
15775 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
15776 /* underline */
15777 p = highlight_has_attr(id, HL_UNDERLINE, modec);
15778 else
15779 /* undercurl */
15780 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015781 break;
15782 }
15783
15784 if (p != NULL)
15785 p = vim_strsave(p);
15786#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015787 rettv->v_type = VAR_STRING;
15788 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015789}
15790
15791/*
15792 * "synIDtrans(id)" function
15793 */
15794/*ARGSUSED*/
15795 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015796f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015797 typval_T *argvars;
15798 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015799{
15800 int id;
15801
15802#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015803 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015804
15805 if (id > 0)
15806 id = syn_get_final_id(id);
15807 else
15808#endif
15809 id = 0;
15810
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015811 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015812}
15813
15814/*
15815 * "system()" function
15816 */
15817 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015818f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015819 typval_T *argvars;
15820 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015821{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015822 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015823 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015824 char_u *infile = NULL;
15825 char_u buf[NUMBUFLEN];
15826 int err = FALSE;
15827 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015828
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000015829 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000015830 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000015831
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015832 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015833 {
15834 /*
15835 * Write the string to a temp file, to be used for input of the shell
15836 * command.
15837 */
15838 if ((infile = vim_tempname('i')) == NULL)
15839 {
15840 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000015841 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015842 }
15843
15844 fd = mch_fopen((char *)infile, WRITEBIN);
15845 if (fd == NULL)
15846 {
15847 EMSG2(_(e_notopen), infile);
15848 goto done;
15849 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015850 p = get_tv_string_buf_chk(&argvars[1], buf);
15851 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015852 {
15853 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015854 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015855 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015856 if (fwrite(p, STRLEN(p), 1, fd) != 1)
15857 err = TRUE;
15858 if (fclose(fd) != 0)
15859 err = TRUE;
15860 if (err)
15861 {
15862 EMSG(_("E677: Error writing temp file"));
15863 goto done;
15864 }
15865 }
15866
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015867 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
15868 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015869
Bram Moolenaar071d4272004-06-13 20:20:40 +000015870#ifdef USE_CR
15871 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015872 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015873 {
15874 char_u *s;
15875
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015876 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015877 {
15878 if (*s == CAR)
15879 *s = NL;
15880 }
15881 }
15882#else
15883# ifdef USE_CRNL
15884 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015885 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015886 {
15887 char_u *s, *d;
15888
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015889 d = res;
15890 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015891 {
15892 if (s[0] == CAR && s[1] == NL)
15893 ++s;
15894 *d++ = *s;
15895 }
15896 *d = NUL;
15897 }
15898# endif
15899#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015900
15901done:
15902 if (infile != NULL)
15903 {
15904 mch_remove(infile);
15905 vim_free(infile);
15906 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015907 rettv->v_type = VAR_STRING;
15908 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015909}
15910
15911/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015912 * "tabpagebuflist()" function
15913 */
15914/* ARGSUSED */
15915 static void
15916f_tabpagebuflist(argvars, rettv)
15917 typval_T *argvars;
15918 typval_T *rettv;
15919{
15920#ifndef FEAT_WINDOWS
15921 rettv->vval.v_number = 0;
15922#else
15923 tabpage_T *tp;
15924 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015925
15926 if (argvars[0].v_type == VAR_UNKNOWN)
15927 wp = firstwin;
15928 else
15929 {
15930 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15931 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000015932 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015933 }
15934 if (wp == NULL)
15935 rettv->vval.v_number = 0;
15936 else
15937 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015938 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015939 rettv->vval.v_number = 0;
15940 else
15941 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015942 for (; wp != NULL; wp = wp->w_next)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015943 if (list_append_number(rettv->vval.v_list,
15944 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015945 break;
15946 }
15947 }
15948#endif
15949}
15950
15951
15952/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015953 * "tabpagenr()" function
15954 */
15955/* ARGSUSED */
15956 static void
15957f_tabpagenr(argvars, rettv)
15958 typval_T *argvars;
15959 typval_T *rettv;
15960{
15961 int nr = 1;
15962#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015963 char_u *arg;
15964
15965 if (argvars[0].v_type != VAR_UNKNOWN)
15966 {
15967 arg = get_tv_string_chk(&argvars[0]);
15968 nr = 0;
15969 if (arg != NULL)
15970 {
15971 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000015972 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015973 else
15974 EMSG2(_(e_invexpr2), arg);
15975 }
15976 }
15977 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015978 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015979#endif
15980 rettv->vval.v_number = nr;
15981}
15982
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015983
15984#ifdef FEAT_WINDOWS
15985static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
15986
15987/*
15988 * Common code for tabpagewinnr() and winnr().
15989 */
15990 static int
15991get_winnr(tp, argvar)
15992 tabpage_T *tp;
15993 typval_T *argvar;
15994{
15995 win_T *twin;
15996 int nr = 1;
15997 win_T *wp;
15998 char_u *arg;
15999
16000 twin = (tp == curtab) ? curwin : tp->tp_curwin;
16001 if (argvar->v_type != VAR_UNKNOWN)
16002 {
16003 arg = get_tv_string_chk(argvar);
16004 if (arg == NULL)
16005 nr = 0; /* type error; errmsg already given */
16006 else if (STRCMP(arg, "$") == 0)
16007 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
16008 else if (STRCMP(arg, "#") == 0)
16009 {
16010 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
16011 if (twin == NULL)
16012 nr = 0;
16013 }
16014 else
16015 {
16016 EMSG2(_(e_invexpr2), arg);
16017 nr = 0;
16018 }
16019 }
16020
16021 if (nr > 0)
16022 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
16023 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000016024 {
16025 if (wp == NULL)
16026 {
16027 /* didn't find it in this tabpage */
16028 nr = 0;
16029 break;
16030 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016031 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000016032 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016033 return nr;
16034}
16035#endif
16036
16037/*
16038 * "tabpagewinnr()" function
16039 */
16040/* ARGSUSED */
16041 static void
16042f_tabpagewinnr(argvars, rettv)
16043 typval_T *argvars;
16044 typval_T *rettv;
16045{
16046 int nr = 1;
16047#ifdef FEAT_WINDOWS
16048 tabpage_T *tp;
16049
16050 tp = find_tabpage((int)get_tv_number(&argvars[0]));
16051 if (tp == NULL)
16052 nr = 0;
16053 else
16054 nr = get_winnr(tp, &argvars[1]);
16055#endif
16056 rettv->vval.v_number = nr;
16057}
16058
16059
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016060/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016061 * "tagfiles()" function
16062 */
16063/*ARGSUSED*/
16064 static void
16065f_tagfiles(argvars, rettv)
16066 typval_T *argvars;
16067 typval_T *rettv;
16068{
16069 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016070 tagname_T tn;
16071 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016072
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016073 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016074 {
16075 rettv->vval.v_number = 0;
16076 return;
16077 }
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016078
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016079 for (first = TRUE; ; first = FALSE)
16080 if (get_tagfname(&tn, first, fname) == FAIL
16081 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016082 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016083 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016084}
16085
16086/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000016087 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016088 */
16089 static void
16090f_taglist(argvars, rettv)
16091 typval_T *argvars;
16092 typval_T *rettv;
16093{
16094 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016095
16096 tag_pattern = get_tv_string(&argvars[0]);
16097
16098 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016099 if (*tag_pattern == NUL)
16100 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016101
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016102 if (rettv_list_alloc(rettv) == OK)
16103 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016104}
16105
16106/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016107 * "tempname()" function
16108 */
16109/*ARGSUSED*/
16110 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016111f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016112 typval_T *argvars;
16113 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016114{
16115 static int x = 'A';
16116
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016117 rettv->v_type = VAR_STRING;
16118 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016119
16120 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
16121 * names. Skip 'I' and 'O', they are used for shell redirection. */
16122 do
16123 {
16124 if (x == 'Z')
16125 x = '0';
16126 else if (x == '9')
16127 x = 'A';
16128 else
16129 {
16130#ifdef EBCDIC
16131 if (x == 'I')
16132 x = 'J';
16133 else if (x == 'R')
16134 x = 'S';
16135 else
16136#endif
16137 ++x;
16138 }
16139 } while (x == 'I' || x == 'O');
16140}
16141
16142/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000016143 * "test(list)" function: Just checking the walls...
16144 */
16145/*ARGSUSED*/
16146 static void
16147f_test(argvars, rettv)
16148 typval_T *argvars;
16149 typval_T *rettv;
16150{
16151 /* Used for unit testing. Change the code below to your liking. */
16152#if 0
16153 listitem_T *li;
16154 list_T *l;
16155 char_u *bad, *good;
16156
16157 if (argvars[0].v_type != VAR_LIST)
16158 return;
16159 l = argvars[0].vval.v_list;
16160 if (l == NULL)
16161 return;
16162 li = l->lv_first;
16163 if (li == NULL)
16164 return;
16165 bad = get_tv_string(&li->li_tv);
16166 li = li->li_next;
16167 if (li == NULL)
16168 return;
16169 good = get_tv_string(&li->li_tv);
16170 rettv->vval.v_number = test_edit_score(bad, good);
16171#endif
16172}
16173
16174/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016175 * "tolower(string)" function
16176 */
16177 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016178f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016179 typval_T *argvars;
16180 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016181{
16182 char_u *p;
16183
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016184 p = vim_strsave(get_tv_string(&argvars[0]));
16185 rettv->v_type = VAR_STRING;
16186 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016187
16188 if (p != NULL)
16189 while (*p != NUL)
16190 {
16191#ifdef FEAT_MBYTE
16192 int l;
16193
16194 if (enc_utf8)
16195 {
16196 int c, lc;
16197
16198 c = utf_ptr2char(p);
16199 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016200 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016201 /* TODO: reallocate string when byte count changes. */
16202 if (utf_char2len(lc) == l)
16203 utf_char2bytes(lc, p);
16204 p += l;
16205 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016206 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016207 p += l; /* skip multi-byte character */
16208 else
16209#endif
16210 {
16211 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
16212 ++p;
16213 }
16214 }
16215}
16216
16217/*
16218 * "toupper(string)" function
16219 */
16220 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016221f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016222 typval_T *argvars;
16223 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016224{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016225 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016226 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016227}
16228
16229/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000016230 * "tr(string, fromstr, tostr)" function
16231 */
16232 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016233f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016234 typval_T *argvars;
16235 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000016236{
16237 char_u *instr;
16238 char_u *fromstr;
16239 char_u *tostr;
16240 char_u *p;
16241#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000016242 int inlen;
16243 int fromlen;
16244 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000016245 int idx;
16246 char_u *cpstr;
16247 int cplen;
16248 int first = TRUE;
16249#endif
16250 char_u buf[NUMBUFLEN];
16251 char_u buf2[NUMBUFLEN];
16252 garray_T ga;
16253
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016254 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016255 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
16256 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016257
16258 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016259 rettv->v_type = VAR_STRING;
16260 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016261 if (fromstr == NULL || tostr == NULL)
16262 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000016263 ga_init2(&ga, (int)sizeof(char), 80);
16264
16265#ifdef FEAT_MBYTE
16266 if (!has_mbyte)
16267#endif
16268 /* not multi-byte: fromstr and tostr must be the same length */
16269 if (STRLEN(fromstr) != STRLEN(tostr))
16270 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016271#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000016272error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016273#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000016274 EMSG2(_(e_invarg2), fromstr);
16275 ga_clear(&ga);
16276 return;
16277 }
16278
16279 /* fromstr and tostr have to contain the same number of chars */
16280 while (*instr != NUL)
16281 {
16282#ifdef FEAT_MBYTE
16283 if (has_mbyte)
16284 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016285 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016286 cpstr = instr;
16287 cplen = inlen;
16288 idx = 0;
16289 for (p = fromstr; *p != NUL; p += fromlen)
16290 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016291 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016292 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
16293 {
16294 for (p = tostr; *p != NUL; p += tolen)
16295 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016296 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016297 if (idx-- == 0)
16298 {
16299 cplen = tolen;
16300 cpstr = p;
16301 break;
16302 }
16303 }
16304 if (*p == NUL) /* tostr is shorter than fromstr */
16305 goto error;
16306 break;
16307 }
16308 ++idx;
16309 }
16310
16311 if (first && cpstr == instr)
16312 {
16313 /* Check that fromstr and tostr have the same number of
16314 * (multi-byte) characters. Done only once when a character
16315 * of instr doesn't appear in fromstr. */
16316 first = FALSE;
16317 for (p = tostr; *p != NUL; p += tolen)
16318 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016319 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016320 --idx;
16321 }
16322 if (idx != 0)
16323 goto error;
16324 }
16325
16326 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000016327 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016328 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000016329
16330 instr += inlen;
16331 }
16332 else
16333#endif
16334 {
16335 /* When not using multi-byte chars we can do it faster. */
16336 p = vim_strchr(fromstr, *instr);
16337 if (p != NULL)
16338 ga_append(&ga, tostr[p - fromstr]);
16339 else
16340 ga_append(&ga, *instr);
16341 ++instr;
16342 }
16343 }
16344
Bram Moolenaar61b974b2006-12-05 09:32:29 +000016345 /* add a terminating NUL */
16346 ga_grow(&ga, 1);
16347 ga_append(&ga, NUL);
16348
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016349 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000016350}
16351
16352/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016353 * "type(expr)" function
16354 */
16355 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016356f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016357 typval_T *argvars;
16358 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016359{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016360 int n;
16361
16362 switch (argvars[0].v_type)
16363 {
16364 case VAR_NUMBER: n = 0; break;
16365 case VAR_STRING: n = 1; break;
16366 case VAR_FUNC: n = 2; break;
16367 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016368 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016369 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
16370 }
16371 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016372}
16373
16374/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016375 * "values(dict)" function
16376 */
16377 static void
16378f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016379 typval_T *argvars;
16380 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016381{
16382 dict_list(argvars, rettv, 1);
16383}
16384
16385/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016386 * "virtcol(string)" function
16387 */
16388 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016389f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016390 typval_T *argvars;
16391 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016392{
16393 colnr_T vcol = 0;
16394 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016395 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016396
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016397 fp = var2fpos(&argvars[0], FALSE, &fnum);
16398 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
16399 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016400 {
16401 getvvcol(curwin, fp, NULL, NULL, &vcol);
16402 ++vcol;
16403 }
16404
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016405 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016406}
16407
16408/*
16409 * "visualmode()" function
16410 */
16411/*ARGSUSED*/
16412 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016413f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016414 typval_T *argvars;
16415 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016416{
16417#ifdef FEAT_VISUAL
16418 char_u str[2];
16419
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016420 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016421 str[0] = curbuf->b_visual_mode_eval;
16422 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016423 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016424
16425 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016426 if ((argvars[0].v_type == VAR_NUMBER
16427 && argvars[0].vval.v_number != 0)
16428 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016429 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016430 curbuf->b_visual_mode_eval = NUL;
16431#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016432 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016433#endif
16434}
16435
16436/*
16437 * "winbufnr(nr)" function
16438 */
16439 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016440f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016441 typval_T *argvars;
16442 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016443{
16444 win_T *wp;
16445
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016446 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016447 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016448 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016449 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016450 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016451}
16452
16453/*
16454 * "wincol()" function
16455 */
16456/*ARGSUSED*/
16457 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016458f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016459 typval_T *argvars;
16460 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016461{
16462 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016463 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016464}
16465
16466/*
16467 * "winheight(nr)" function
16468 */
16469 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016470f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016471 typval_T *argvars;
16472 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016473{
16474 win_T *wp;
16475
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016476 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016477 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016478 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016479 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016480 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016481}
16482
16483/*
16484 * "winline()" function
16485 */
16486/*ARGSUSED*/
16487 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016488f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016489 typval_T *argvars;
16490 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016491{
16492 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016493 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016494}
16495
16496/*
16497 * "winnr()" function
16498 */
16499/* ARGSUSED */
16500 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016501f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016502 typval_T *argvars;
16503 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016504{
16505 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016506
Bram Moolenaar071d4272004-06-13 20:20:40 +000016507#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016508 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016509#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016510 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016511}
16512
16513/*
16514 * "winrestcmd()" function
16515 */
16516/* ARGSUSED */
16517 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016518f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016519 typval_T *argvars;
16520 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016521{
16522#ifdef FEAT_WINDOWS
16523 win_T *wp;
16524 int winnr = 1;
16525 garray_T ga;
16526 char_u buf[50];
16527
16528 ga_init2(&ga, (int)sizeof(char), 70);
16529 for (wp = firstwin; wp != NULL; wp = wp->w_next)
16530 {
16531 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
16532 ga_concat(&ga, buf);
16533# ifdef FEAT_VERTSPLIT
16534 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
16535 ga_concat(&ga, buf);
16536# endif
16537 ++winnr;
16538 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000016539 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016540
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016541 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016542#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016543 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016544#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016545 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016546}
16547
16548/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016549 * "winrestview()" function
16550 */
16551/* ARGSUSED */
16552 static void
16553f_winrestview(argvars, rettv)
16554 typval_T *argvars;
16555 typval_T *rettv;
16556{
16557 dict_T *dict;
16558
16559 if (argvars[0].v_type != VAR_DICT
16560 || (dict = argvars[0].vval.v_dict) == NULL)
16561 EMSG(_(e_invarg));
16562 else
16563 {
16564 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
16565 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
16566#ifdef FEAT_VIRTUALEDIT
16567 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
16568#endif
16569 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016570 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016571
Bram Moolenaar6f11a412006-09-06 20:16:42 +000016572 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016573#ifdef FEAT_DIFF
16574 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
16575#endif
16576 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
16577 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
16578
16579 check_cursor();
16580 changed_cline_bef_curs();
16581 invalidate_botline();
16582 redraw_later(VALID);
16583
16584 if (curwin->w_topline == 0)
16585 curwin->w_topline = 1;
16586 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
16587 curwin->w_topline = curbuf->b_ml.ml_line_count;
16588#ifdef FEAT_DIFF
16589 check_topfill(curwin, TRUE);
16590#endif
16591 }
16592}
16593
16594/*
16595 * "winsaveview()" function
16596 */
16597/* ARGSUSED */
16598 static void
16599f_winsaveview(argvars, rettv)
16600 typval_T *argvars;
16601 typval_T *rettv;
16602{
16603 dict_T *dict;
16604
16605 dict = dict_alloc();
16606 if (dict == NULL)
16607 return;
16608 rettv->v_type = VAR_DICT;
16609 rettv->vval.v_dict = dict;
16610 ++dict->dv_refcount;
16611
16612 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
16613 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
16614#ifdef FEAT_VIRTUALEDIT
16615 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
16616#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000016617 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016618 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
16619
16620 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
16621#ifdef FEAT_DIFF
16622 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
16623#endif
16624 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
16625 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
16626}
16627
16628/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016629 * "winwidth(nr)" function
16630 */
16631 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016632f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016633 typval_T *argvars;
16634 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016635{
16636 win_T *wp;
16637
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016638 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016639 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016640 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016641 else
16642#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016643 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016644#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016645 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016646#endif
16647}
16648
Bram Moolenaar071d4272004-06-13 20:20:40 +000016649/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016650 * "writefile()" function
16651 */
16652 static void
16653f_writefile(argvars, rettv)
16654 typval_T *argvars;
16655 typval_T *rettv;
16656{
16657 int binary = FALSE;
16658 char_u *fname;
16659 FILE *fd;
16660 listitem_T *li;
16661 char_u *s;
16662 int ret = 0;
16663 int c;
16664
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000016665 if (check_restricted() || check_secure())
16666 return;
16667
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016668 if (argvars[0].v_type != VAR_LIST)
16669 {
16670 EMSG2(_(e_listarg), "writefile()");
16671 return;
16672 }
16673 if (argvars[0].vval.v_list == NULL)
16674 return;
16675
16676 if (argvars[2].v_type != VAR_UNKNOWN
16677 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
16678 binary = TRUE;
16679
16680 /* Always open the file in binary mode, library functions have a mind of
16681 * their own about CR-LF conversion. */
16682 fname = get_tv_string(&argvars[1]);
16683 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
16684 {
16685 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
16686 ret = -1;
16687 }
16688 else
16689 {
16690 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
16691 li = li->li_next)
16692 {
16693 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
16694 {
16695 if (*s == '\n')
16696 c = putc(NUL, fd);
16697 else
16698 c = putc(*s, fd);
16699 if (c == EOF)
16700 {
16701 ret = -1;
16702 break;
16703 }
16704 }
16705 if (!binary || li->li_next != NULL)
16706 if (putc('\n', fd) == EOF)
16707 {
16708 ret = -1;
16709 break;
16710 }
16711 if (ret < 0)
16712 {
16713 EMSG(_(e_write));
16714 break;
16715 }
16716 }
16717 fclose(fd);
16718 }
16719
16720 rettv->vval.v_number = ret;
16721}
16722
16723/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016724 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016725 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016726 */
16727 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000016728var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000016729 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000016730 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016731 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016732{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016733 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016734 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016735 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016736
Bram Moolenaara5525202006-03-02 22:52:09 +000016737 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016738 if (varp->v_type == VAR_LIST)
16739 {
16740 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016741 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000016742 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000016743 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016744
16745 l = varp->vval.v_list;
16746 if (l == NULL)
16747 return NULL;
16748
16749 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016750 pos.lnum = list_find_nr(l, 0L, &error);
16751 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016752 return NULL; /* invalid line number */
16753
16754 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016755 pos.col = list_find_nr(l, 1L, &error);
16756 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016757 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016758 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000016759
16760 /* We accept "$" for the column number: last column. */
16761 li = list_find(l, 1L);
16762 if (li != NULL && li->li_tv.v_type == VAR_STRING
16763 && li->li_tv.vval.v_string != NULL
16764 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
16765 pos.col = len + 1;
16766
Bram Moolenaara5525202006-03-02 22:52:09 +000016767 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000016768 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016769 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016770 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016771
Bram Moolenaara5525202006-03-02 22:52:09 +000016772#ifdef FEAT_VIRTUALEDIT
16773 /* Get the virtual offset. Defaults to zero. */
16774 pos.coladd = list_find_nr(l, 2L, &error);
16775 if (error)
16776 pos.coladd = 0;
16777#endif
16778
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016779 return &pos;
16780 }
16781
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016782 name = get_tv_string_chk(varp);
16783 if (name == NULL)
16784 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016785 if (name[0] == '.') /* cursor */
16786 return &curwin->w_cursor;
16787 if (name[0] == '\'') /* mark */
16788 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016789 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016790 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
16791 return NULL;
16792 return pp;
16793 }
Bram Moolenaara5525202006-03-02 22:52:09 +000016794
16795#ifdef FEAT_VIRTUALEDIT
16796 pos.coladd = 0;
16797#endif
16798
Bram Moolenaar477933c2007-07-17 14:32:23 +000016799 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016800 {
16801 pos.col = 0;
16802 if (name[1] == '0') /* "w0": first visible line */
16803 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016804 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016805 pos.lnum = curwin->w_topline;
16806 return &pos;
16807 }
16808 else if (name[1] == '$') /* "w$": last visible line */
16809 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016810 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016811 pos.lnum = curwin->w_botline - 1;
16812 return &pos;
16813 }
16814 }
16815 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016816 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000016817 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016818 {
16819 pos.lnum = curbuf->b_ml.ml_line_count;
16820 pos.col = 0;
16821 }
16822 else
16823 {
16824 pos.lnum = curwin->w_cursor.lnum;
16825 pos.col = (colnr_T)STRLEN(ml_get_curline());
16826 }
16827 return &pos;
16828 }
16829 return NULL;
16830}
16831
16832/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016833 * Convert list in "arg" into a position and optional file number.
16834 * When "fnump" is NULL there is no file number, only 3 items.
16835 * Note that the column is passed on as-is, the caller may want to decrement
16836 * it to use 1 for the first column.
16837 * Return FAIL when conversion is not possible, doesn't check the position for
16838 * validity.
16839 */
16840 static int
16841list2fpos(arg, posp, fnump)
16842 typval_T *arg;
16843 pos_T *posp;
16844 int *fnump;
16845{
16846 list_T *l = arg->vval.v_list;
16847 long i = 0;
16848 long n;
16849
Bram Moolenaarbde35262006-07-23 20:12:24 +000016850 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
16851 * when "fnump" isn't NULL and "coladd" is optional. */
16852 if (arg->v_type != VAR_LIST
16853 || l == NULL
16854 || l->lv_len < (fnump == NULL ? 2 : 3)
16855 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016856 return FAIL;
16857
16858 if (fnump != NULL)
16859 {
16860 n = list_find_nr(l, i++, NULL); /* fnum */
16861 if (n < 0)
16862 return FAIL;
16863 if (n == 0)
16864 n = curbuf->b_fnum; /* current buffer */
16865 *fnump = n;
16866 }
16867
16868 n = list_find_nr(l, i++, NULL); /* lnum */
16869 if (n < 0)
16870 return FAIL;
16871 posp->lnum = n;
16872
16873 n = list_find_nr(l, i++, NULL); /* col */
16874 if (n < 0)
16875 return FAIL;
16876 posp->col = n;
16877
16878#ifdef FEAT_VIRTUALEDIT
16879 n = list_find_nr(l, i, NULL);
16880 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000016881 posp->coladd = 0;
16882 else
16883 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016884#endif
16885
16886 return OK;
16887}
16888
16889/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016890 * Get the length of an environment variable name.
16891 * Advance "arg" to the first character after the name.
16892 * Return 0 for error.
16893 */
16894 static int
16895get_env_len(arg)
16896 char_u **arg;
16897{
16898 char_u *p;
16899 int len;
16900
16901 for (p = *arg; vim_isIDc(*p); ++p)
16902 ;
16903 if (p == *arg) /* no name found */
16904 return 0;
16905
16906 len = (int)(p - *arg);
16907 *arg = p;
16908 return len;
16909}
16910
16911/*
16912 * Get the length of the name of a function or internal variable.
16913 * "arg" is advanced to the first non-white character after the name.
16914 * Return 0 if something is wrong.
16915 */
16916 static int
16917get_id_len(arg)
16918 char_u **arg;
16919{
16920 char_u *p;
16921 int len;
16922
16923 /* Find the end of the name. */
16924 for (p = *arg; eval_isnamec(*p); ++p)
16925 ;
16926 if (p == *arg) /* no name found */
16927 return 0;
16928
16929 len = (int)(p - *arg);
16930 *arg = skipwhite(p);
16931
16932 return len;
16933}
16934
16935/*
Bram Moolenaara7043832005-01-21 11:56:39 +000016936 * Get the length of the name of a variable or function.
16937 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000016938 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016939 * Return -1 if curly braces expansion failed.
16940 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016941 * If the name contains 'magic' {}'s, expand them and return the
16942 * expanded name in an allocated string via 'alias' - caller must free.
16943 */
16944 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016945get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016946 char_u **arg;
16947 char_u **alias;
16948 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016949 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016950{
16951 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016952 char_u *p;
16953 char_u *expr_start;
16954 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016955
16956 *alias = NULL; /* default to no alias */
16957
16958 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
16959 && (*arg)[2] == (int)KE_SNR)
16960 {
16961 /* hard coded <SNR>, already translated */
16962 *arg += 3;
16963 return get_id_len(arg) + 3;
16964 }
16965 len = eval_fname_script(*arg);
16966 if (len > 0)
16967 {
16968 /* literal "<SID>", "s:" or "<SNR>" */
16969 *arg += len;
16970 }
16971
Bram Moolenaar071d4272004-06-13 20:20:40 +000016972 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016973 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016974 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016975 p = find_name_end(*arg, &expr_start, &expr_end,
16976 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016977 if (expr_start != NULL)
16978 {
16979 char_u *temp_string;
16980
16981 if (!evaluate)
16982 {
16983 len += (int)(p - *arg);
16984 *arg = skipwhite(p);
16985 return len;
16986 }
16987
16988 /*
16989 * Include any <SID> etc in the expanded string:
16990 * Thus the -len here.
16991 */
16992 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
16993 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016994 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016995 *alias = temp_string;
16996 *arg = skipwhite(p);
16997 return (int)STRLEN(temp_string);
16998 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016999
17000 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017001 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017002 EMSG2(_(e_invexpr2), *arg);
17003
17004 return len;
17005}
17006
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017007/*
17008 * Find the end of a variable or function name, taking care of magic braces.
17009 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
17010 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017011 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017012 * Return a pointer to just after the name. Equal to "arg" if there is no
17013 * valid name.
17014 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017015 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017016find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017017 char_u *arg;
17018 char_u **expr_start;
17019 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017020 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017021{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017022 int mb_nest = 0;
17023 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017024 char_u *p;
17025
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017026 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017027 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017028 *expr_start = NULL;
17029 *expr_end = NULL;
17030 }
17031
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017032 /* Quick check for valid starting character. */
17033 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
17034 return arg;
17035
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017036 for (p = arg; *p != NUL
17037 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017038 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017039 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017040 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000017041 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017042 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000017043 if (*p == '\'')
17044 {
17045 /* skip over 'string' to avoid counting [ and ] inside it. */
17046 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
17047 ;
17048 if (*p == NUL)
17049 break;
17050 }
17051 else if (*p == '"')
17052 {
17053 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
17054 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
17055 if (*p == '\\' && p[1] != NUL)
17056 ++p;
17057 if (*p == NUL)
17058 break;
17059 }
17060
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017061 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017062 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017063 if (*p == '[')
17064 ++br_nest;
17065 else if (*p == ']')
17066 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017067 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000017068
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017069 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017070 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017071 if (*p == '{')
17072 {
17073 mb_nest++;
17074 if (expr_start != NULL && *expr_start == NULL)
17075 *expr_start = p;
17076 }
17077 else if (*p == '}')
17078 {
17079 mb_nest--;
17080 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
17081 *expr_end = p;
17082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017084 }
17085
17086 return p;
17087}
17088
17089/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017090 * Expands out the 'magic' {}'s in a variable/function name.
17091 * Note that this can call itself recursively, to deal with
17092 * constructs like foo{bar}{baz}{bam}
17093 * The four pointer arguments point to "foo{expre}ss{ion}bar"
17094 * "in_start" ^
17095 * "expr_start" ^
17096 * "expr_end" ^
17097 * "in_end" ^
17098 *
17099 * Returns a new allocated string, which the caller must free.
17100 * Returns NULL for failure.
17101 */
17102 static char_u *
17103make_expanded_name(in_start, expr_start, expr_end, in_end)
17104 char_u *in_start;
17105 char_u *expr_start;
17106 char_u *expr_end;
17107 char_u *in_end;
17108{
17109 char_u c1;
17110 char_u *retval = NULL;
17111 char_u *temp_result;
17112 char_u *nextcmd = NULL;
17113
17114 if (expr_end == NULL || in_end == NULL)
17115 return NULL;
17116 *expr_start = NUL;
17117 *expr_end = NUL;
17118 c1 = *in_end;
17119 *in_end = NUL;
17120
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017121 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017122 if (temp_result != NULL && nextcmd == NULL)
17123 {
17124 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
17125 + (in_end - expr_end) + 1));
17126 if (retval != NULL)
17127 {
17128 STRCPY(retval, in_start);
17129 STRCAT(retval, temp_result);
17130 STRCAT(retval, expr_end + 1);
17131 }
17132 }
17133 vim_free(temp_result);
17134
17135 *in_end = c1; /* put char back for error messages */
17136 *expr_start = '{';
17137 *expr_end = '}';
17138
17139 if (retval != NULL)
17140 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017141 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017142 if (expr_start != NULL)
17143 {
17144 /* Further expansion! */
17145 temp_result = make_expanded_name(retval, expr_start,
17146 expr_end, temp_result);
17147 vim_free(retval);
17148 retval = temp_result;
17149 }
17150 }
17151
17152 return retval;
17153}
17154
17155/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017156 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017157 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017158 */
17159 static int
17160eval_isnamec(c)
17161 int c;
17162{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017163 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
17164}
17165
17166/*
17167 * Return TRUE if character "c" can be used as the first character in a
17168 * variable or function name (excluding '{' and '}').
17169 */
17170 static int
17171eval_isnamec1(c)
17172 int c;
17173{
17174 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000017175}
17176
17177/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017178 * Set number v: variable to "val".
17179 */
17180 void
17181set_vim_var_nr(idx, val)
17182 int idx;
17183 long val;
17184{
Bram Moolenaare9a41262005-01-15 22:18:47 +000017185 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017186}
17187
17188/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017189 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017190 */
17191 long
17192get_vim_var_nr(idx)
17193 int idx;
17194{
Bram Moolenaare9a41262005-01-15 22:18:47 +000017195 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017196}
17197
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017198#if defined(FEAT_AUTOCMD) || defined(PROTO)
17199/*
17200 * Get string v: variable value. Uses a static buffer, can only be used once.
17201 */
17202 char_u *
17203get_vim_var_str(idx)
17204 int idx;
17205{
17206 return get_tv_string(&vimvars[idx].vv_tv);
17207}
17208#endif
17209
Bram Moolenaar071d4272004-06-13 20:20:40 +000017210/*
17211 * Set v:count, v:count1 and v:prevcount.
17212 */
17213 void
17214set_vcount(count, count1)
17215 long count;
17216 long count1;
17217{
Bram Moolenaare9a41262005-01-15 22:18:47 +000017218 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
17219 vimvars[VV_COUNT].vv_nr = count;
17220 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017221}
17222
17223/*
17224 * Set string v: variable to a copy of "val".
17225 */
17226 void
17227set_vim_var_string(idx, val, len)
17228 int idx;
17229 char_u *val;
17230 int len; /* length of "val" to use or -1 (whole string) */
17231{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017232 /* Need to do this (at least) once, since we can't initialize a union.
17233 * Will always be invoked when "v:progname" is set. */
17234 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
17235
Bram Moolenaare9a41262005-01-15 22:18:47 +000017236 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017237 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017238 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017239 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017240 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017241 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000017242 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017243}
17244
17245/*
17246 * Set v:register if needed.
17247 */
17248 void
17249set_reg_var(c)
17250 int c;
17251{
17252 char_u regname;
17253
17254 if (c == 0 || c == ' ')
17255 regname = '"';
17256 else
17257 regname = c;
17258 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000017259 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017260 set_vim_var_string(VV_REG, &regname, 1);
17261}
17262
17263/*
17264 * Get or set v:exception. If "oldval" == NULL, return the current value.
17265 * Otherwise, restore the value to "oldval" and return NULL.
17266 * Must always be called in pairs to save and restore v:exception! Does not
17267 * take care of memory allocations.
17268 */
17269 char_u *
17270v_exception(oldval)
17271 char_u *oldval;
17272{
17273 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017274 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017275
Bram Moolenaare9a41262005-01-15 22:18:47 +000017276 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017277 return NULL;
17278}
17279
17280/*
17281 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
17282 * Otherwise, restore the value to "oldval" and return NULL.
17283 * Must always be called in pairs to save and restore v:throwpoint! Does not
17284 * take care of memory allocations.
17285 */
17286 char_u *
17287v_throwpoint(oldval)
17288 char_u *oldval;
17289{
17290 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017291 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017292
Bram Moolenaare9a41262005-01-15 22:18:47 +000017293 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017294 return NULL;
17295}
17296
17297#if defined(FEAT_AUTOCMD) || defined(PROTO)
17298/*
17299 * Set v:cmdarg.
17300 * If "eap" != NULL, use "eap" to generate the value and return the old value.
17301 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
17302 * Must always be called in pairs!
17303 */
17304 char_u *
17305set_cmdarg(eap, oldarg)
17306 exarg_T *eap;
17307 char_u *oldarg;
17308{
17309 char_u *oldval;
17310 char_u *newval;
17311 unsigned len;
17312
Bram Moolenaare9a41262005-01-15 22:18:47 +000017313 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017314 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017315 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017316 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000017317 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017318 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017319 }
17320
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017321 if (eap->force_bin == FORCE_BIN)
17322 len = 6;
17323 else if (eap->force_bin == FORCE_NOBIN)
17324 len = 8;
17325 else
17326 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017327
17328 if (eap->read_edit)
17329 len += 7;
17330
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017331 if (eap->force_ff != 0)
17332 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
17333# ifdef FEAT_MBYTE
17334 if (eap->force_enc != 0)
17335 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000017336 if (eap->bad_char != 0)
17337 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017338# endif
17339
17340 newval = alloc(len + 1);
17341 if (newval == NULL)
17342 return NULL;
17343
17344 if (eap->force_bin == FORCE_BIN)
17345 sprintf((char *)newval, " ++bin");
17346 else if (eap->force_bin == FORCE_NOBIN)
17347 sprintf((char *)newval, " ++nobin");
17348 else
17349 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017350
17351 if (eap->read_edit)
17352 STRCAT(newval, " ++edit");
17353
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017354 if (eap->force_ff != 0)
17355 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
17356 eap->cmd + eap->force_ff);
17357# ifdef FEAT_MBYTE
17358 if (eap->force_enc != 0)
17359 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
17360 eap->cmd + eap->force_enc);
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000017361 if (eap->bad_char != 0)
17362 sprintf((char *)newval + STRLEN(newval), " ++bad=%s",
17363 eap->cmd + eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017364# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000017365 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017366 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017367}
17368#endif
17369
17370/*
17371 * Get the value of internal variable "name".
17372 * Return OK or FAIL.
17373 */
17374 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017375get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017376 char_u *name;
17377 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000017378 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017379 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017380{
17381 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000017382 typval_T *tv = NULL;
17383 typval_T atv;
17384 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017385 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017386
17387 /* truncate the name, so that we can use strcmp() */
17388 cc = name[len];
17389 name[len] = NUL;
17390
17391 /*
17392 * Check for "b:changedtick".
17393 */
17394 if (STRCMP(name, "b:changedtick") == 0)
17395 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000017396 atv.v_type = VAR_NUMBER;
17397 atv.vval.v_number = curbuf->b_changedtick;
17398 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017399 }
17400
17401 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017402 * Check for user-defined variables.
17403 */
17404 else
17405 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017406 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017407 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017408 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017409 }
17410
Bram Moolenaare9a41262005-01-15 22:18:47 +000017411 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017412 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017413 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017414 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017415 ret = FAIL;
17416 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017417 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017418 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017419
17420 name[len] = cc;
17421
17422 return ret;
17423}
17424
17425/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017426 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
17427 * Also handle function call with Funcref variable: func(expr)
17428 * Can all be combined: dict.func(expr)[idx]['func'](expr)
17429 */
17430 static int
17431handle_subscript(arg, rettv, evaluate, verbose)
17432 char_u **arg;
17433 typval_T *rettv;
17434 int evaluate; /* do more than finding the end */
17435 int verbose; /* give error messages */
17436{
17437 int ret = OK;
17438 dict_T *selfdict = NULL;
17439 char_u *s;
17440 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000017441 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017442
17443 while (ret == OK
17444 && (**arg == '['
17445 || (**arg == '.' && rettv->v_type == VAR_DICT)
17446 || (**arg == '(' && rettv->v_type == VAR_FUNC))
17447 && !vim_iswhite(*(*arg - 1)))
17448 {
17449 if (**arg == '(')
17450 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000017451 /* need to copy the funcref so that we can clear rettv */
17452 functv = *rettv;
17453 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017454
17455 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000017456 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017457 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000017458 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
17459 &len, evaluate, selfdict);
17460
17461 /* Clear the funcref afterwards, so that deleting it while
17462 * evaluating the arguments is possible (see test55). */
17463 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017464
17465 /* Stop the expression evaluation when immediately aborting on
17466 * error, or when an interrupt occurred or an exception was thrown
17467 * but not caught. */
17468 if (aborting())
17469 {
17470 if (ret == OK)
17471 clear_tv(rettv);
17472 ret = FAIL;
17473 }
17474 dict_unref(selfdict);
17475 selfdict = NULL;
17476 }
17477 else /* **arg == '[' || **arg == '.' */
17478 {
17479 dict_unref(selfdict);
17480 if (rettv->v_type == VAR_DICT)
17481 {
17482 selfdict = rettv->vval.v_dict;
17483 if (selfdict != NULL)
17484 ++selfdict->dv_refcount;
17485 }
17486 else
17487 selfdict = NULL;
17488 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
17489 {
17490 clear_tv(rettv);
17491 ret = FAIL;
17492 }
17493 }
17494 }
17495 dict_unref(selfdict);
17496 return ret;
17497}
17498
17499/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017500 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
17501 * value).
17502 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017503 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017504alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017505{
Bram Moolenaar33570922005-01-25 22:26:29 +000017506 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017507}
17508
17509/*
17510 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017511 * The string "s" must have been allocated, it is consumed.
17512 * Return NULL for out of memory, the variable otherwise.
17513 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017514 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017515alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017516 char_u *s;
17517{
Bram Moolenaar33570922005-01-25 22:26:29 +000017518 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017519
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017520 rettv = alloc_tv();
17521 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017522 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017523 rettv->v_type = VAR_STRING;
17524 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017525 }
17526 else
17527 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017528 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017529}
17530
17531/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017532 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017533 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000017534 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017535free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017536 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017537{
17538 if (varp != NULL)
17539 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017540 switch (varp->v_type)
17541 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017542 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017543 func_unref(varp->vval.v_string);
17544 /*FALLTHROUGH*/
17545 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017546 vim_free(varp->vval.v_string);
17547 break;
17548 case VAR_LIST:
17549 list_unref(varp->vval.v_list);
17550 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017551 case VAR_DICT:
17552 dict_unref(varp->vval.v_dict);
17553 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017554 case VAR_NUMBER:
17555 case VAR_UNKNOWN:
17556 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017557 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000017558 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017559 break;
17560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017561 vim_free(varp);
17562 }
17563}
17564
17565/*
17566 * Free the memory for a variable value and set the value to NULL or 0.
17567 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017568 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017569clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017570 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017571{
17572 if (varp != NULL)
17573 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017574 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017575 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017576 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017577 func_unref(varp->vval.v_string);
17578 /*FALLTHROUGH*/
17579 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017580 vim_free(varp->vval.v_string);
17581 varp->vval.v_string = NULL;
17582 break;
17583 case VAR_LIST:
17584 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017585 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017586 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017587 case VAR_DICT:
17588 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017589 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017590 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017591 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017592 varp->vval.v_number = 0;
17593 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017594 case VAR_UNKNOWN:
17595 break;
17596 default:
17597 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017598 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017599 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017600 }
17601}
17602
17603/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017604 * Set the value of a variable to NULL without freeing items.
17605 */
17606 static void
17607init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017608 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017609{
17610 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017611 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017612}
17613
17614/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017615 * Get the number value of a variable.
17616 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017617 * For incompatible types, return 0.
17618 * get_tv_number_chk() is similar to get_tv_number(), but informs the
17619 * caller of incompatible types: it sets *denote to TRUE if "denote"
17620 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017621 */
17622 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017623get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017624 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017625{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017626 int error = FALSE;
17627
17628 return get_tv_number_chk(varp, &error); /* return 0L on error */
17629}
17630
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017631 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017632get_tv_number_chk(varp, denote)
17633 typval_T *varp;
17634 int *denote;
17635{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017636 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017637
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017638 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017639 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017640 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017641 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017642 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017643 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017644 break;
17645 case VAR_STRING:
17646 if (varp->vval.v_string != NULL)
17647 vim_str2nr(varp->vval.v_string, NULL, NULL,
17648 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017649 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017650 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000017651 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017652 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017653 case VAR_DICT:
17654 EMSG(_("E728: Using a Dictionary as a number"));
17655 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017656 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017657 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017658 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017659 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017660 if (denote == NULL) /* useful for values that must be unsigned */
17661 n = -1;
17662 else
17663 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017664 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017665}
17666
17667/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000017668 * Get the lnum from the first argument.
17669 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017670 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017671 */
17672 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017673get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000017674 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017675{
Bram Moolenaar33570922005-01-25 22:26:29 +000017676 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017677 linenr_T lnum;
17678
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017679 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017680 if (lnum == 0) /* no valid number, try using line() */
17681 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017682 rettv.v_type = VAR_NUMBER;
17683 f_line(argvars, &rettv);
17684 lnum = rettv.vval.v_number;
17685 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017686 }
17687 return lnum;
17688}
17689
17690/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000017691 * Get the lnum from the first argument.
17692 * Also accepts "$", then "buf" is used.
17693 * Returns 0 on error.
17694 */
17695 static linenr_T
17696get_tv_lnum_buf(argvars, buf)
17697 typval_T *argvars;
17698 buf_T *buf;
17699{
17700 if (argvars[0].v_type == VAR_STRING
17701 && argvars[0].vval.v_string != NULL
17702 && argvars[0].vval.v_string[0] == '$'
17703 && buf != NULL)
17704 return buf->b_ml.ml_line_count;
17705 return get_tv_number_chk(&argvars[0], NULL);
17706}
17707
17708/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017709 * Get the string value of a variable.
17710 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000017711 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
17712 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017713 * If the String variable has never been set, return an empty string.
17714 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017715 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
17716 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017717 */
17718 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017719get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017720 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017721{
17722 static char_u mybuf[NUMBUFLEN];
17723
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017724 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017725}
17726
17727 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017728get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000017729 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017730 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017731{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017732 char_u *res = get_tv_string_buf_chk(varp, buf);
17733
17734 return res != NULL ? res : (char_u *)"";
17735}
17736
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017737 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017738get_tv_string_chk(varp)
17739 typval_T *varp;
17740{
17741 static char_u mybuf[NUMBUFLEN];
17742
17743 return get_tv_string_buf_chk(varp, mybuf);
17744}
17745
17746 static char_u *
17747get_tv_string_buf_chk(varp, buf)
17748 typval_T *varp;
17749 char_u *buf;
17750{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017751 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017752 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017753 case VAR_NUMBER:
17754 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
17755 return buf;
17756 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017757 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017758 break;
17759 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017760 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000017761 break;
17762 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017763 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017764 break;
17765 case VAR_STRING:
17766 if (varp->vval.v_string != NULL)
17767 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017768 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017769 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017770 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017771 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017772 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017773 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017774}
17775
17776/*
17777 * Find variable "name" in the list of variables.
17778 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017779 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017780 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000017781 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017782 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017783 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017784find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017785 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017786 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017787{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017788 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017789 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017790
Bram Moolenaara7043832005-01-21 11:56:39 +000017791 ht = find_var_ht(name, &varname);
17792 if (htp != NULL)
17793 *htp = ht;
17794 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017795 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017796 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017797}
17798
17799/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017800 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000017801 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017802 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017803 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017804find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000017805 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000017806 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017807 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000017808{
Bram Moolenaar33570922005-01-25 22:26:29 +000017809 hashitem_T *hi;
17810
17811 if (*varname == NUL)
17812 {
17813 /* Must be something like "s:", otherwise "ht" would be NULL. */
17814 switch (varname[-2])
17815 {
17816 case 's': return &SCRIPT_SV(current_SID).sv_var;
17817 case 'g': return &globvars_var;
17818 case 'v': return &vimvars_var;
17819 case 'b': return &curbuf->b_bufvar;
17820 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017821#ifdef FEAT_WINDOWS
17822 case 't': return &curtab->tp_winvar;
17823#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017824 case 'l': return current_funccal == NULL
17825 ? NULL : &current_funccal->l_vars_var;
17826 case 'a': return current_funccal == NULL
17827 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000017828 }
17829 return NULL;
17830 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017831
17832 hi = hash_find(ht, varname);
17833 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017834 {
17835 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017836 * worked find the variable again. Don't auto-load a script if it was
17837 * loaded already, otherwise it would be loaded every time when
17838 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017839 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017840 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017841 hi = hash_find(ht, varname);
17842 if (HASHITEM_EMPTY(hi))
17843 return NULL;
17844 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017845 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017846}
17847
17848/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017849 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017850 * Set "varname" to the start of name without ':'.
17851 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017852 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017853find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017854 char_u *name;
17855 char_u **varname;
17856{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017857 hashitem_T *hi;
17858
Bram Moolenaar071d4272004-06-13 20:20:40 +000017859 if (name[1] != ':')
17860 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017861 /* The name must not start with a colon or #. */
17862 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017863 return NULL;
17864 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017865
17866 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017867 hi = hash_find(&compat_hashtab, name);
17868 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000017869 return &compat_hashtab;
17870
Bram Moolenaar071d4272004-06-13 20:20:40 +000017871 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017872 return &globvarht; /* global variable */
17873 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017874 }
17875 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017876 if (*name == 'g') /* global variable */
17877 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017878 /* There must be no ':' or '#' in the rest of the name, unless g: is used
17879 */
17880 if (vim_strchr(name + 2, ':') != NULL
17881 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017882 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017883 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017884 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017885 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017886 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017887#ifdef FEAT_WINDOWS
17888 if (*name == 't') /* tab page variable */
17889 return &curtab->tp_vars.dv_hashtab;
17890#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000017891 if (*name == 'v') /* v: variable */
17892 return &vimvarht;
17893 if (*name == 'a' && current_funccal != NULL) /* function argument */
17894 return &current_funccal->l_avars.dv_hashtab;
17895 if (*name == 'l' && current_funccal != NULL) /* local function variable */
17896 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017897 if (*name == 's' /* script variable */
17898 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
17899 return &SCRIPT_VARS(current_SID);
17900 return NULL;
17901}
17902
17903/*
17904 * Get the string value of a (global/local) variable.
17905 * Returns NULL when it doesn't exist.
17906 */
17907 char_u *
17908get_var_value(name)
17909 char_u *name;
17910{
Bram Moolenaar33570922005-01-25 22:26:29 +000017911 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017912
Bram Moolenaara7043832005-01-21 11:56:39 +000017913 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017914 if (v == NULL)
17915 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000017916 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017917}
17918
17919/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017920 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000017921 * sourcing this script and when executing functions defined in the script.
17922 */
17923 void
17924new_script_vars(id)
17925 scid_T id;
17926{
Bram Moolenaara7043832005-01-21 11:56:39 +000017927 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000017928 hashtab_T *ht;
17929 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000017930
Bram Moolenaar071d4272004-06-13 20:20:40 +000017931 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
17932 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017933 /* Re-allocating ga_data means that an ht_array pointing to
17934 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000017935 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000017936 for (i = 1; i <= ga_scripts.ga_len; ++i)
17937 {
17938 ht = &SCRIPT_VARS(i);
17939 if (ht->ht_mask == HT_INIT_SIZE - 1)
17940 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000017941 sv = &SCRIPT_SV(i);
17942 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000017943 }
17944
Bram Moolenaar071d4272004-06-13 20:20:40 +000017945 while (ga_scripts.ga_len < id)
17946 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017947 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
17948 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017949 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017950 }
17951 }
17952}
17953
17954/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017955 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
17956 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017957 */
17958 void
Bram Moolenaar33570922005-01-25 22:26:29 +000017959init_var_dict(dict, dict_var)
17960 dict_T *dict;
17961 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017962{
Bram Moolenaar33570922005-01-25 22:26:29 +000017963 hash_init(&dict->dv_hashtab);
17964 dict->dv_refcount = 99999;
17965 dict_var->di_tv.vval.v_dict = dict;
17966 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017967 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017968 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
17969 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017970}
17971
17972/*
17973 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000017974 * Frees all allocated variables and the value they contain.
17975 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017976 */
17977 void
Bram Moolenaara7043832005-01-21 11:56:39 +000017978vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000017979 hashtab_T *ht;
17980{
17981 vars_clear_ext(ht, TRUE);
17982}
17983
17984/*
17985 * Like vars_clear(), but only free the value if "free_val" is TRUE.
17986 */
17987 static void
17988vars_clear_ext(ht, free_val)
17989 hashtab_T *ht;
17990 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017991{
Bram Moolenaara7043832005-01-21 11:56:39 +000017992 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000017993 hashitem_T *hi;
17994 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017995
Bram Moolenaar33570922005-01-25 22:26:29 +000017996 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017997 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000017998 for (hi = ht->ht_array; todo > 0; ++hi)
17999 {
18000 if (!HASHITEM_EMPTY(hi))
18001 {
18002 --todo;
18003
Bram Moolenaar33570922005-01-25 22:26:29 +000018004 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000018005 * ht_array might change then. hash_clear() takes care of it
18006 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000018007 v = HI2DI(hi);
18008 if (free_val)
18009 clear_tv(&v->di_tv);
18010 if ((v->di_flags & DI_FLAGS_FIX) == 0)
18011 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000018012 }
18013 }
18014 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018015 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018016}
18017
Bram Moolenaara7043832005-01-21 11:56:39 +000018018/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018019 * Delete a variable from hashtab "ht" at item "hi".
18020 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000018021 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018022 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000018023delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000018024 hashtab_T *ht;
18025 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018026{
Bram Moolenaar33570922005-01-25 22:26:29 +000018027 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000018028
18029 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000018030 clear_tv(&di->di_tv);
18031 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018032}
18033
18034/*
18035 * List the value of one internal variable.
18036 */
18037 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018038list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000018039 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018040 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018041 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018042{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018043 char_u *tofree;
18044 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018045 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018046
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018047 s = echo_string(&v->di_tv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000018048 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018049 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018050 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018051}
18052
Bram Moolenaar071d4272004-06-13 20:20:40 +000018053 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018054list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018055 char_u *prefix;
18056 char_u *name;
18057 int type;
18058 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018059 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018060{
Bram Moolenaar31859182007-08-14 20:41:13 +000018061 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
18062 msg_start();
18063 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018064 if (name != NULL) /* "a:" vars don't have a name stored */
18065 msg_puts(name);
18066 msg_putchar(' ');
18067 msg_advance(22);
18068 if (type == VAR_NUMBER)
18069 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018070 else if (type == VAR_FUNC)
18071 msg_putchar('*');
18072 else if (type == VAR_LIST)
18073 {
18074 msg_putchar('[');
18075 if (*string == '[')
18076 ++string;
18077 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000018078 else if (type == VAR_DICT)
18079 {
18080 msg_putchar('{');
18081 if (*string == '{')
18082 ++string;
18083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018084 else
18085 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018086
Bram Moolenaar071d4272004-06-13 20:20:40 +000018087 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018088
18089 if (type == VAR_FUNC)
18090 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018091 if (*first)
18092 {
18093 msg_clr_eos();
18094 *first = FALSE;
18095 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018096}
18097
18098/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018099 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000018100 * If the variable already exists, the value is updated.
18101 * Otherwise the variable is created.
18102 */
18103 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018104set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018105 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000018106 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018107 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018108{
Bram Moolenaar33570922005-01-25 22:26:29 +000018109 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018110 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018111 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000018112 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018113
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018114 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018115 {
18116 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
18117 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
18118 ? name[2] : name[0]))
18119 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000018120 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018121 return;
18122 }
18123 if (function_exists(name))
18124 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018125 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018126 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018127 return;
18128 }
18129 }
18130
Bram Moolenaara7043832005-01-21 11:56:39 +000018131 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000018132 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000018133 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000018134 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000018135 return;
18136 }
18137
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018138 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000018139 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018140 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018141 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018142 if (var_check_ro(v->di_flags, name)
18143 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000018144 return;
18145 if (v->di_tv.v_type != tv->v_type
18146 && !((v->di_tv.v_type == VAR_STRING
18147 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018148 && (tv->v_type == VAR_STRING
18149 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018150 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000018151 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018152 return;
18153 }
Bram Moolenaar33570922005-01-25 22:26:29 +000018154
18155 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000018156 * Handle setting internal v: variables separately: we don't change
18157 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000018158 */
18159 if (ht == &vimvarht)
18160 {
18161 if (v->di_tv.v_type == VAR_STRING)
18162 {
18163 vim_free(v->di_tv.vval.v_string);
18164 if (copy || tv->v_type != VAR_STRING)
18165 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
18166 else
18167 {
18168 /* Take over the string to avoid an extra alloc/free. */
18169 v->di_tv.vval.v_string = tv->vval.v_string;
18170 tv->vval.v_string = NULL;
18171 }
18172 }
18173 else if (v->di_tv.v_type != VAR_NUMBER)
18174 EMSG2(_(e_intern2), "set_var()");
18175 else
18176 v->di_tv.vval.v_number = get_tv_number(tv);
18177 return;
18178 }
18179
18180 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018181 }
18182 else /* add a new variable */
18183 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000018184 /* Can't add "v:" variable. */
18185 if (ht == &vimvarht)
18186 {
18187 EMSG2(_(e_illvar), name);
18188 return;
18189 }
18190
Bram Moolenaar92124a32005-06-17 22:03:40 +000018191 /* Make sure the variable name is valid. */
18192 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000018193 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
18194 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000018195 {
18196 EMSG2(_(e_illvar), varname);
18197 return;
18198 }
18199
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018200 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
18201 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000018202 if (v == NULL)
18203 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000018204 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000018205 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018206 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018207 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018208 return;
18209 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018210 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018211 }
Bram Moolenaara7043832005-01-21 11:56:39 +000018212
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018213 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000018214 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018215 else
18216 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018217 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018218 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018219 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018221}
18222
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018223/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000018224 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000018225 * Also give an error message.
18226 */
18227 static int
18228var_check_ro(flags, name)
18229 int flags;
18230 char_u *name;
18231{
18232 if (flags & DI_FLAGS_RO)
18233 {
18234 EMSG2(_(e_readonlyvar), name);
18235 return TRUE;
18236 }
18237 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
18238 {
18239 EMSG2(_(e_readonlysbx), name);
18240 return TRUE;
18241 }
18242 return FALSE;
18243}
18244
18245/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000018246 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
18247 * Also give an error message.
18248 */
18249 static int
18250var_check_fixed(flags, name)
18251 int flags;
18252 char_u *name;
18253{
18254 if (flags & DI_FLAGS_FIX)
18255 {
18256 EMSG2(_("E795: Cannot delete variable %s"), name);
18257 return TRUE;
18258 }
18259 return FALSE;
18260}
18261
18262/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018263 * Return TRUE if typeval "tv" is set to be locked (immutable).
18264 * Also give an error message, using "name".
18265 */
18266 static int
18267tv_check_lock(lock, name)
18268 int lock;
18269 char_u *name;
18270{
18271 if (lock & VAR_LOCKED)
18272 {
18273 EMSG2(_("E741: Value is locked: %s"),
18274 name == NULL ? (char_u *)_("Unknown") : name);
18275 return TRUE;
18276 }
18277 if (lock & VAR_FIXED)
18278 {
18279 EMSG2(_("E742: Cannot change value of %s"),
18280 name == NULL ? (char_u *)_("Unknown") : name);
18281 return TRUE;
18282 }
18283 return FALSE;
18284}
18285
18286/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018287 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018288 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018289 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018290 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018291 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018292copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000018293 typval_T *from;
18294 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018295{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018296 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018297 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018298 switch (from->v_type)
18299 {
18300 case VAR_NUMBER:
18301 to->vval.v_number = from->vval.v_number;
18302 break;
18303 case VAR_STRING:
18304 case VAR_FUNC:
18305 if (from->vval.v_string == NULL)
18306 to->vval.v_string = NULL;
18307 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018308 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018309 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018310 if (from->v_type == VAR_FUNC)
18311 func_ref(to->vval.v_string);
18312 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018313 break;
18314 case VAR_LIST:
18315 if (from->vval.v_list == NULL)
18316 to->vval.v_list = NULL;
18317 else
18318 {
18319 to->vval.v_list = from->vval.v_list;
18320 ++to->vval.v_list->lv_refcount;
18321 }
18322 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018323 case VAR_DICT:
18324 if (from->vval.v_dict == NULL)
18325 to->vval.v_dict = NULL;
18326 else
18327 {
18328 to->vval.v_dict = from->vval.v_dict;
18329 ++to->vval.v_dict->dv_refcount;
18330 }
18331 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018332 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018333 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018334 break;
18335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018336}
18337
18338/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000018339 * Make a copy of an item.
18340 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018341 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
18342 * reference to an already copied list/dict can be used.
18343 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018344 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018345 static int
18346item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000018347 typval_T *from;
18348 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018349 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018350 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018351{
18352 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018353 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018354
Bram Moolenaar33570922005-01-25 22:26:29 +000018355 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018356 {
18357 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018358 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018359 }
18360 ++recurse;
18361
18362 switch (from->v_type)
18363 {
18364 case VAR_NUMBER:
18365 case VAR_STRING:
18366 case VAR_FUNC:
18367 copy_tv(from, to);
18368 break;
18369 case VAR_LIST:
18370 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018371 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018372 if (from->vval.v_list == NULL)
18373 to->vval.v_list = NULL;
18374 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
18375 {
18376 /* use the copy made earlier */
18377 to->vval.v_list = from->vval.v_list->lv_copylist;
18378 ++to->vval.v_list->lv_refcount;
18379 }
18380 else
18381 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
18382 if (to->vval.v_list == NULL)
18383 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018384 break;
18385 case VAR_DICT:
18386 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018387 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018388 if (from->vval.v_dict == NULL)
18389 to->vval.v_dict = NULL;
18390 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
18391 {
18392 /* use the copy made earlier */
18393 to->vval.v_dict = from->vval.v_dict->dv_copydict;
18394 ++to->vval.v_dict->dv_refcount;
18395 }
18396 else
18397 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
18398 if (to->vval.v_dict == NULL)
18399 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018400 break;
18401 default:
18402 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018403 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018404 }
18405 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018406 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018407}
18408
18409/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018410 * ":echo expr1 ..." print each argument separated with a space, add a
18411 * newline at the end.
18412 * ":echon expr1 ..." print each argument plain.
18413 */
18414 void
18415ex_echo(eap)
18416 exarg_T *eap;
18417{
18418 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018419 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018420 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018421 char_u *p;
18422 int needclr = TRUE;
18423 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018424 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018425
18426 if (eap->skip)
18427 ++emsg_skip;
18428 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
18429 {
18430 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018431 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018432 {
18433 /*
18434 * Report the invalid expression unless the expression evaluation
18435 * has been cancelled due to an aborting error, an interrupt, or an
18436 * exception.
18437 */
18438 if (!aborting())
18439 EMSG2(_(e_invexpr2), p);
18440 break;
18441 }
18442 if (!eap->skip)
18443 {
18444 if (atstart)
18445 {
18446 atstart = FALSE;
18447 /* Call msg_start() after eval1(), evaluating the expression
18448 * may cause a message to appear. */
18449 if (eap->cmdidx == CMD_echo)
18450 msg_start();
18451 }
18452 else if (eap->cmdidx == CMD_echo)
18453 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018454 p = echo_string(&rettv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018455 if (p != NULL)
18456 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018457 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018458 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018459 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018460 if (*p != TAB && needclr)
18461 {
18462 /* remove any text still there from the command */
18463 msg_clr_eos();
18464 needclr = FALSE;
18465 }
18466 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018467 }
18468 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018469 {
18470#ifdef FEAT_MBYTE
18471 if (has_mbyte)
18472 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018473 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018474
18475 (void)msg_outtrans_len_attr(p, i, echo_attr);
18476 p += i - 1;
18477 }
18478 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000018479#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018480 (void)msg_outtrans_len_attr(p, 1, echo_attr);
18481 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018482 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018483 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018484 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018485 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018486 arg = skipwhite(arg);
18487 }
18488 eap->nextcmd = check_nextcmd(arg);
18489
18490 if (eap->skip)
18491 --emsg_skip;
18492 else
18493 {
18494 /* remove text that may still be there from the command */
18495 if (needclr)
18496 msg_clr_eos();
18497 if (eap->cmdidx == CMD_echo)
18498 msg_end();
18499 }
18500}
18501
18502/*
18503 * ":echohl {name}".
18504 */
18505 void
18506ex_echohl(eap)
18507 exarg_T *eap;
18508{
18509 int id;
18510
18511 id = syn_name2id(eap->arg);
18512 if (id == 0)
18513 echo_attr = 0;
18514 else
18515 echo_attr = syn_id2attr(id);
18516}
18517
18518/*
18519 * ":execute expr1 ..." execute the result of an expression.
18520 * ":echomsg expr1 ..." Print a message
18521 * ":echoerr expr1 ..." Print an error
18522 * Each gets spaces around each argument and a newline at the end for
18523 * echo commands
18524 */
18525 void
18526ex_execute(eap)
18527 exarg_T *eap;
18528{
18529 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018530 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018531 int ret = OK;
18532 char_u *p;
18533 garray_T ga;
18534 int len;
18535 int save_did_emsg;
18536
18537 ga_init2(&ga, 1, 80);
18538
18539 if (eap->skip)
18540 ++emsg_skip;
18541 while (*arg != NUL && *arg != '|' && *arg != '\n')
18542 {
18543 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018544 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018545 {
18546 /*
18547 * Report the invalid expression unless the expression evaluation
18548 * has been cancelled due to an aborting error, an interrupt, or an
18549 * exception.
18550 */
18551 if (!aborting())
18552 EMSG2(_(e_invexpr2), p);
18553 ret = FAIL;
18554 break;
18555 }
18556
18557 if (!eap->skip)
18558 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018559 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018560 len = (int)STRLEN(p);
18561 if (ga_grow(&ga, len + 2) == FAIL)
18562 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018563 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018564 ret = FAIL;
18565 break;
18566 }
18567 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018568 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018569 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018570 ga.ga_len += len;
18571 }
18572
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018573 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018574 arg = skipwhite(arg);
18575 }
18576
18577 if (ret != FAIL && ga.ga_data != NULL)
18578 {
18579 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000018580 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018581 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000018582 out_flush();
18583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018584 else if (eap->cmdidx == CMD_echoerr)
18585 {
18586 /* We don't want to abort following commands, restore did_emsg. */
18587 save_did_emsg = did_emsg;
18588 EMSG((char_u *)ga.ga_data);
18589 if (!force_abort)
18590 did_emsg = save_did_emsg;
18591 }
18592 else if (eap->cmdidx == CMD_execute)
18593 do_cmdline((char_u *)ga.ga_data,
18594 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
18595 }
18596
18597 ga_clear(&ga);
18598
18599 if (eap->skip)
18600 --emsg_skip;
18601
18602 eap->nextcmd = check_nextcmd(arg);
18603}
18604
18605/*
18606 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
18607 * "arg" points to the "&" or '+' when called, to "option" when returning.
18608 * Returns NULL when no option name found. Otherwise pointer to the char
18609 * after the option name.
18610 */
18611 static char_u *
18612find_option_end(arg, opt_flags)
18613 char_u **arg;
18614 int *opt_flags;
18615{
18616 char_u *p = *arg;
18617
18618 ++p;
18619 if (*p == 'g' && p[1] == ':')
18620 {
18621 *opt_flags = OPT_GLOBAL;
18622 p += 2;
18623 }
18624 else if (*p == 'l' && p[1] == ':')
18625 {
18626 *opt_flags = OPT_LOCAL;
18627 p += 2;
18628 }
18629 else
18630 *opt_flags = 0;
18631
18632 if (!ASCII_ISALPHA(*p))
18633 return NULL;
18634 *arg = p;
18635
18636 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
18637 p += 4; /* termcap option */
18638 else
18639 while (ASCII_ISALPHA(*p))
18640 ++p;
18641 return p;
18642}
18643
18644/*
18645 * ":function"
18646 */
18647 void
18648ex_function(eap)
18649 exarg_T *eap;
18650{
18651 char_u *theline;
18652 int j;
18653 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018654 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018655 char_u *name = NULL;
18656 char_u *p;
18657 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018658 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018659 garray_T newargs;
18660 garray_T newlines;
18661 int varargs = FALSE;
18662 int mustend = FALSE;
18663 int flags = 0;
18664 ufunc_T *fp;
18665 int indent;
18666 int nesting;
18667 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000018668 dictitem_T *v;
18669 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018670 static int func_nr = 0; /* number for nameless function */
18671 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018672 hashtab_T *ht;
18673 int todo;
18674 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018675 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018676
18677 /*
18678 * ":function" without argument: list functions.
18679 */
18680 if (ends_excmd(*eap->arg))
18681 {
18682 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018683 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018684 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000018685 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018686 {
18687 if (!HASHITEM_EMPTY(hi))
18688 {
18689 --todo;
18690 fp = HI2UF(hi);
18691 if (!isdigit(*fp->uf_name))
18692 list_func_head(fp, FALSE);
18693 }
18694 }
18695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018696 eap->nextcmd = check_nextcmd(eap->arg);
18697 return;
18698 }
18699
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018700 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018701 * ":function /pat": list functions matching pattern.
18702 */
18703 if (*eap->arg == '/')
18704 {
18705 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
18706 if (!eap->skip)
18707 {
18708 regmatch_T regmatch;
18709
18710 c = *p;
18711 *p = NUL;
18712 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
18713 *p = c;
18714 if (regmatch.regprog != NULL)
18715 {
18716 regmatch.rm_ic = p_ic;
18717
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018718 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018719 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
18720 {
18721 if (!HASHITEM_EMPTY(hi))
18722 {
18723 --todo;
18724 fp = HI2UF(hi);
18725 if (!isdigit(*fp->uf_name)
18726 && vim_regexec(&regmatch, fp->uf_name, 0))
18727 list_func_head(fp, FALSE);
18728 }
18729 }
18730 }
18731 }
18732 if (*p == '/')
18733 ++p;
18734 eap->nextcmd = check_nextcmd(p);
18735 return;
18736 }
18737
18738 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018739 * Get the function name. There are these situations:
18740 * func normal function name
18741 * "name" == func, "fudi.fd_dict" == NULL
18742 * dict.func new dictionary entry
18743 * "name" == NULL, "fudi.fd_dict" set,
18744 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
18745 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018746 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018747 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18748 * dict.func existing dict entry that's not a Funcref
18749 * "name" == NULL, "fudi.fd_dict" set,
18750 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18751 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018752 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018753 name = trans_function_name(&p, eap->skip, 0, &fudi);
18754 paren = (vim_strchr(p, '(') != NULL);
18755 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018756 {
18757 /*
18758 * Return on an invalid expression in braces, unless the expression
18759 * evaluation has been cancelled due to an aborting error, an
18760 * interrupt, or an exception.
18761 */
18762 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018763 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018764 if (!eap->skip && fudi.fd_newkey != NULL)
18765 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018766 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018767 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018769 else
18770 eap->skip = TRUE;
18771 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000018772
Bram Moolenaar071d4272004-06-13 20:20:40 +000018773 /* An error in a function call during evaluation of an expression in magic
18774 * braces should not cause the function not to be defined. */
18775 saved_did_emsg = did_emsg;
18776 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018777
18778 /*
18779 * ":function func" with only function name: list function.
18780 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018781 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018782 {
18783 if (!ends_excmd(*skipwhite(p)))
18784 {
18785 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018786 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018787 }
18788 eap->nextcmd = check_nextcmd(p);
18789 if (eap->nextcmd != NULL)
18790 *p = NUL;
18791 if (!eap->skip && !got_int)
18792 {
18793 fp = find_func(name);
18794 if (fp != NULL)
18795 {
18796 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018797 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018798 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018799 if (FUNCLINE(fp, j) == NULL)
18800 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018801 msg_putchar('\n');
18802 msg_outnum((long)(j + 1));
18803 if (j < 9)
18804 msg_putchar(' ');
18805 if (j < 99)
18806 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018807 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018808 out_flush(); /* show a line at a time */
18809 ui_breakcheck();
18810 }
18811 if (!got_int)
18812 {
18813 msg_putchar('\n');
18814 msg_puts((char_u *)" endfunction");
18815 }
18816 }
18817 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018818 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018819 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018820 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018821 }
18822
18823 /*
18824 * ":function name(arg1, arg2)" Define function.
18825 */
18826 p = skipwhite(p);
18827 if (*p != '(')
18828 {
18829 if (!eap->skip)
18830 {
18831 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018832 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018833 }
18834 /* attempt to continue by skipping some text */
18835 if (vim_strchr(p, '(') != NULL)
18836 p = vim_strchr(p, '(');
18837 }
18838 p = skipwhite(p + 1);
18839
18840 ga_init2(&newargs, (int)sizeof(char_u *), 3);
18841 ga_init2(&newlines, (int)sizeof(char_u *), 3);
18842
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018843 if (!eap->skip)
18844 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000018845 /* Check the name of the function. Unless it's a dictionary function
18846 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018847 if (name != NULL)
18848 arg = name;
18849 else
18850 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000018851 if (arg != NULL && (fudi.fd_di == NULL
18852 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018853 {
18854 if (*arg == K_SPECIAL)
18855 j = 3;
18856 else
18857 j = 0;
18858 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
18859 : eval_isnamec(arg[j])))
18860 ++j;
18861 if (arg[j] != NUL)
18862 emsg_funcname(_(e_invarg2), arg);
18863 }
18864 }
18865
Bram Moolenaar071d4272004-06-13 20:20:40 +000018866 /*
18867 * Isolate the arguments: "arg1, arg2, ...)"
18868 */
18869 while (*p != ')')
18870 {
18871 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
18872 {
18873 varargs = TRUE;
18874 p += 3;
18875 mustend = TRUE;
18876 }
18877 else
18878 {
18879 arg = p;
18880 while (ASCII_ISALNUM(*p) || *p == '_')
18881 ++p;
18882 if (arg == p || isdigit(*arg)
18883 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
18884 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
18885 {
18886 if (!eap->skip)
18887 EMSG2(_("E125: Illegal argument: %s"), arg);
18888 break;
18889 }
18890 if (ga_grow(&newargs, 1) == FAIL)
18891 goto erret;
18892 c = *p;
18893 *p = NUL;
18894 arg = vim_strsave(arg);
18895 if (arg == NULL)
18896 goto erret;
18897 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
18898 *p = c;
18899 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018900 if (*p == ',')
18901 ++p;
18902 else
18903 mustend = TRUE;
18904 }
18905 p = skipwhite(p);
18906 if (mustend && *p != ')')
18907 {
18908 if (!eap->skip)
18909 EMSG2(_(e_invarg2), eap->arg);
18910 break;
18911 }
18912 }
18913 ++p; /* skip the ')' */
18914
Bram Moolenaare9a41262005-01-15 22:18:47 +000018915 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018916 for (;;)
18917 {
18918 p = skipwhite(p);
18919 if (STRNCMP(p, "range", 5) == 0)
18920 {
18921 flags |= FC_RANGE;
18922 p += 5;
18923 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000018924 else if (STRNCMP(p, "dict", 4) == 0)
18925 {
18926 flags |= FC_DICT;
18927 p += 4;
18928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018929 else if (STRNCMP(p, "abort", 5) == 0)
18930 {
18931 flags |= FC_ABORT;
18932 p += 5;
18933 }
18934 else
18935 break;
18936 }
18937
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018938 /* When there is a line break use what follows for the function body.
18939 * Makes 'exe "func Test()\n...\nendfunc"' work. */
18940 if (*p == '\n')
18941 line_arg = p + 1;
18942 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018943 EMSG(_(e_trailing));
18944
18945 /*
18946 * Read the body of the function, until ":endfunction" is found.
18947 */
18948 if (KeyTyped)
18949 {
18950 /* Check if the function already exists, don't let the user type the
18951 * whole function before telling him it doesn't work! For a script we
18952 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018953 if (!eap->skip && !eap->forceit)
18954 {
18955 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
18956 EMSG(_(e_funcdict));
18957 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018958 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018959 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018960
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018961 if (!eap->skip && did_emsg)
18962 goto erret;
18963
Bram Moolenaar071d4272004-06-13 20:20:40 +000018964 msg_putchar('\n'); /* don't overwrite the function name */
18965 cmdline_row = msg_row;
18966 }
18967
18968 indent = 2;
18969 nesting = 0;
18970 for (;;)
18971 {
18972 msg_scroll = TRUE;
18973 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018974 sourcing_lnum_off = sourcing_lnum;
18975
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018976 if (line_arg != NULL)
18977 {
18978 /* Use eap->arg, split up in parts by line breaks. */
18979 theline = line_arg;
18980 p = vim_strchr(theline, '\n');
18981 if (p == NULL)
18982 line_arg += STRLEN(line_arg);
18983 else
18984 {
18985 *p = NUL;
18986 line_arg = p + 1;
18987 }
18988 }
18989 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018990 theline = getcmdline(':', 0L, indent);
18991 else
18992 theline = eap->getline(':', eap->cookie, indent);
18993 if (KeyTyped)
18994 lines_left = Rows - 1;
18995 if (theline == NULL)
18996 {
18997 EMSG(_("E126: Missing :endfunction"));
18998 goto erret;
18999 }
19000
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019001 /* Detect line continuation: sourcing_lnum increased more than one. */
19002 if (sourcing_lnum > sourcing_lnum_off + 1)
19003 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
19004 else
19005 sourcing_lnum_off = 0;
19006
Bram Moolenaar071d4272004-06-13 20:20:40 +000019007 if (skip_until != NULL)
19008 {
19009 /* between ":append" and "." and between ":python <<EOF" and "EOF"
19010 * don't check for ":endfunc". */
19011 if (STRCMP(theline, skip_until) == 0)
19012 {
19013 vim_free(skip_until);
19014 skip_until = NULL;
19015 }
19016 }
19017 else
19018 {
19019 /* skip ':' and blanks*/
19020 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
19021 ;
19022
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019023 /* Check for "endfunction". */
19024 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019025 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019026 if (line_arg == NULL)
19027 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019028 break;
19029 }
19030
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019031 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000019032 * at "end". */
19033 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
19034 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019035 else if (STRNCMP(p, "if", 2) == 0
19036 || STRNCMP(p, "wh", 2) == 0
19037 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000019038 || STRNCMP(p, "try", 3) == 0)
19039 indent += 2;
19040
19041 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019042 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019043 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019044 if (*p == '!')
19045 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019046 p += eval_fname_script(p);
19047 if (ASCII_ISALPHA(*p))
19048 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019049 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019050 if (*skipwhite(p) == '(')
19051 {
19052 ++nesting;
19053 indent += 2;
19054 }
19055 }
19056 }
19057
19058 /* Check for ":append" or ":insert". */
19059 p = skip_range(p, NULL);
19060 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
19061 || (p[0] == 'i'
19062 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
19063 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
19064 skip_until = vim_strsave((char_u *)".");
19065
19066 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
19067 arg = skipwhite(skiptowhite(p));
19068 if (arg[0] == '<' && arg[1] =='<'
19069 && ((p[0] == 'p' && p[1] == 'y'
19070 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
19071 || (p[0] == 'p' && p[1] == 'e'
19072 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
19073 || (p[0] == 't' && p[1] == 'c'
19074 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
19075 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
19076 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000019077 || (p[0] == 'm' && p[1] == 'z'
19078 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019079 ))
19080 {
19081 /* ":python <<" continues until a dot, like ":append" */
19082 p = skipwhite(arg + 2);
19083 if (*p == NUL)
19084 skip_until = vim_strsave((char_u *)".");
19085 else
19086 skip_until = vim_strsave(p);
19087 }
19088 }
19089
19090 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019091 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019092 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019093 if (line_arg == NULL)
19094 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019095 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019096 }
19097
19098 /* Copy the line to newly allocated memory. get_one_sourceline()
19099 * allocates 250 bytes per line, this saves 80% on average. The cost
19100 * is an extra alloc/free. */
19101 p = vim_strsave(theline);
19102 if (p != NULL)
19103 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019104 if (line_arg == NULL)
19105 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019106 theline = p;
19107 }
19108
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019109 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
19110
19111 /* Add NULL lines for continuation lines, so that the line count is
19112 * equal to the index in the growarray. */
19113 while (sourcing_lnum_off-- > 0)
19114 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019115
19116 /* Check for end of eap->arg. */
19117 if (line_arg != NULL && *line_arg == NUL)
19118 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019119 }
19120
19121 /* Don't define the function when skipping commands or when an error was
19122 * detected. */
19123 if (eap->skip || did_emsg)
19124 goto erret;
19125
19126 /*
19127 * If there are no errors, add the function
19128 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019129 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019130 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019131 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000019132 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019133 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019134 emsg_funcname("E707: Function name conflicts with variable: %s",
19135 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019136 goto erret;
19137 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019138
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019139 fp = find_func(name);
19140 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019141 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019142 if (!eap->forceit)
19143 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019144 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019145 goto erret;
19146 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019147 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019148 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019149 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019150 name);
19151 goto erret;
19152 }
19153 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019154 ga_clear_strings(&(fp->uf_args));
19155 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019156 vim_free(name);
19157 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019158 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019159 }
19160 else
19161 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019162 char numbuf[20];
19163
19164 fp = NULL;
19165 if (fudi.fd_newkey == NULL && !eap->forceit)
19166 {
19167 EMSG(_(e_funcdict));
19168 goto erret;
19169 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000019170 if (fudi.fd_di == NULL)
19171 {
19172 /* Can't add a function to a locked dictionary */
19173 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
19174 goto erret;
19175 }
19176 /* Can't change an existing function if it is locked */
19177 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
19178 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019179
19180 /* Give the function a sequential number. Can only be used with a
19181 * Funcref! */
19182 vim_free(name);
19183 sprintf(numbuf, "%d", ++func_nr);
19184 name = vim_strsave((char_u *)numbuf);
19185 if (name == NULL)
19186 goto erret;
19187 }
19188
19189 if (fp == NULL)
19190 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019191 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019192 {
19193 int slen, plen;
19194 char_u *scriptname;
19195
19196 /* Check that the autoload name matches the script name. */
19197 j = FAIL;
19198 if (sourcing_name != NULL)
19199 {
19200 scriptname = autoload_name(name);
19201 if (scriptname != NULL)
19202 {
19203 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019204 plen = (int)STRLEN(p);
19205 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019206 if (slen > plen && fnamecmp(p,
19207 sourcing_name + slen - plen) == 0)
19208 j = OK;
19209 vim_free(scriptname);
19210 }
19211 }
19212 if (j == FAIL)
19213 {
19214 EMSG2(_("E746: Function name does not match script file name: %s"), name);
19215 goto erret;
19216 }
19217 }
19218
19219 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019220 if (fp == NULL)
19221 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019222
19223 if (fudi.fd_dict != NULL)
19224 {
19225 if (fudi.fd_di == NULL)
19226 {
19227 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019228 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019229 if (fudi.fd_di == NULL)
19230 {
19231 vim_free(fp);
19232 goto erret;
19233 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019234 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
19235 {
19236 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000019237 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019238 goto erret;
19239 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019240 }
19241 else
19242 /* overwrite existing dict entry */
19243 clear_tv(&fudi.fd_di->di_tv);
19244 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019245 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019246 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019247 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019248
19249 /* behave like "dict" was used */
19250 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019251 }
19252
Bram Moolenaar071d4272004-06-13 20:20:40 +000019253 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019254 STRCPY(fp->uf_name, name);
19255 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019256 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019257 fp->uf_args = newargs;
19258 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019259#ifdef FEAT_PROFILE
19260 fp->uf_tml_count = NULL;
19261 fp->uf_tml_total = NULL;
19262 fp->uf_tml_self = NULL;
19263 fp->uf_profiling = FALSE;
19264 if (prof_def_func())
19265 func_do_profile(fp);
19266#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019267 fp->uf_varargs = varargs;
19268 fp->uf_flags = flags;
19269 fp->uf_calls = 0;
19270 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019271 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019272
19273erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000019274 ga_clear_strings(&newargs);
19275 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019276ret_free:
19277 vim_free(skip_until);
19278 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019279 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019280 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019281}
19282
19283/*
19284 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000019285 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019286 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019287 * flags:
19288 * TFN_INT: internal function name OK
19289 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000019290 * Advances "pp" to just after the function name (if no error).
19291 */
19292 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019293trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019294 char_u **pp;
19295 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019296 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000019297 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019298{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019299 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019300 char_u *start;
19301 char_u *end;
19302 int lead;
19303 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019304 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000019305 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019306
19307 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019308 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019309 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000019310
19311 /* Check for hard coded <SNR>: already translated function ID (from a user
19312 * command). */
19313 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
19314 && (*pp)[2] == (int)KE_SNR)
19315 {
19316 *pp += 3;
19317 len = get_id_len(pp) + 3;
19318 return vim_strnsave(start, len);
19319 }
19320
19321 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
19322 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019323 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000019324 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019325 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019326
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019327 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
19328 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019329 if (end == start)
19330 {
19331 if (!skip)
19332 EMSG(_("E129: Function name required"));
19333 goto theend;
19334 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019335 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019336 {
19337 /*
19338 * Report an invalid expression in braces, unless the expression
19339 * evaluation has been cancelled due to an aborting error, an
19340 * interrupt, or an exception.
19341 */
19342 if (!aborting())
19343 {
19344 if (end != NULL)
19345 EMSG2(_(e_invarg2), start);
19346 }
19347 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019348 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019349 goto theend;
19350 }
19351
19352 if (lv.ll_tv != NULL)
19353 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019354 if (fdp != NULL)
19355 {
19356 fdp->fd_dict = lv.ll_dict;
19357 fdp->fd_newkey = lv.ll_newkey;
19358 lv.ll_newkey = NULL;
19359 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019360 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019361 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
19362 {
19363 name = vim_strsave(lv.ll_tv->vval.v_string);
19364 *pp = end;
19365 }
19366 else
19367 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019368 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
19369 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019370 EMSG(_(e_funcref));
19371 else
19372 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019373 name = NULL;
19374 }
19375 goto theend;
19376 }
19377
19378 if (lv.ll_name == NULL)
19379 {
19380 /* Error found, but continue after the function name. */
19381 *pp = end;
19382 goto theend;
19383 }
19384
Bram Moolenaar33e1a802007-09-06 12:26:44 +000019385 /* Check if the name is a Funcref. If so, use the value. */
19386 if (lv.ll_exp_name != NULL)
19387 {
19388 len = (int)STRLEN(lv.ll_exp_name);
19389 name = deref_func_name(lv.ll_exp_name, &len);
19390 if (name == lv.ll_exp_name)
19391 name = NULL;
19392 }
19393 else
19394 {
19395 len = (int)(end - *pp);
19396 name = deref_func_name(*pp, &len);
19397 if (name == *pp)
19398 name = NULL;
19399 }
19400 if (name != NULL)
19401 {
19402 name = vim_strsave(name);
19403 *pp = end;
19404 goto theend;
19405 }
19406
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019407 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000019408 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019409 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000019410 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
19411 && STRNCMP(lv.ll_name, "s:", 2) == 0)
19412 {
19413 /* When there was "s:" already or the name expanded to get a
19414 * leading "s:" then remove it. */
19415 lv.ll_name += 2;
19416 len -= 2;
19417 lead = 2;
19418 }
19419 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019420 else
Bram Moolenaara7043832005-01-21 11:56:39 +000019421 {
19422 if (lead == 2) /* skip over "s:" */
19423 lv.ll_name += 2;
19424 len = (int)(end - lv.ll_name);
19425 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019426
19427 /*
19428 * Copy the function name to allocated memory.
19429 * Accept <SID>name() inside a script, translate into <SNR>123_name().
19430 * Accept <SNR>123_name() outside a script.
19431 */
19432 if (skip)
19433 lead = 0; /* do nothing */
19434 else if (lead > 0)
19435 {
19436 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000019437 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
19438 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019439 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000019440 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019441 if (current_SID <= 0)
19442 {
19443 EMSG(_(e_usingsid));
19444 goto theend;
19445 }
19446 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
19447 lead += (int)STRLEN(sid_buf);
19448 }
19449 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019450 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019451 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019452 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019453 goto theend;
19454 }
19455 name = alloc((unsigned)(len + lead + 1));
19456 if (name != NULL)
19457 {
19458 if (lead > 0)
19459 {
19460 name[0] = K_SPECIAL;
19461 name[1] = KS_EXTRA;
19462 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000019463 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019464 STRCPY(name + 3, sid_buf);
19465 }
19466 mch_memmove(name + lead, lv.ll_name, (size_t)len);
19467 name[len + lead] = NUL;
19468 }
19469 *pp = end;
19470
19471theend:
19472 clear_lval(&lv);
19473 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019474}
19475
19476/*
19477 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
19478 * Return 2 if "p" starts with "s:".
19479 * Return 0 otherwise.
19480 */
19481 static int
19482eval_fname_script(p)
19483 char_u *p;
19484{
19485 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
19486 || STRNICMP(p + 1, "SNR>", 4) == 0))
19487 return 5;
19488 if (p[0] == 's' && p[1] == ':')
19489 return 2;
19490 return 0;
19491}
19492
19493/*
19494 * Return TRUE if "p" starts with "<SID>" or "s:".
19495 * Only works if eval_fname_script() returned non-zero for "p"!
19496 */
19497 static int
19498eval_fname_sid(p)
19499 char_u *p;
19500{
19501 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
19502}
19503
19504/*
19505 * List the head of the function: "name(arg1, arg2)".
19506 */
19507 static void
19508list_func_head(fp, indent)
19509 ufunc_T *fp;
19510 int indent;
19511{
19512 int j;
19513
19514 msg_start();
19515 if (indent)
19516 MSG_PUTS(" ");
19517 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019518 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019519 {
19520 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019521 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019522 }
19523 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019524 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019525 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019526 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019527 {
19528 if (j)
19529 MSG_PUTS(", ");
19530 msg_puts(FUNCARG(fp, j));
19531 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019532 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019533 {
19534 if (j)
19535 MSG_PUTS(", ");
19536 MSG_PUTS("...");
19537 }
19538 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019539 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000019540 if (p_verbose > 0)
19541 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019542}
19543
19544/*
19545 * Find a function by name, return pointer to it in ufuncs.
19546 * Return NULL for unknown function.
19547 */
19548 static ufunc_T *
19549find_func(name)
19550 char_u *name;
19551{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019552 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019553
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019554 hi = hash_find(&func_hashtab, name);
19555 if (!HASHITEM_EMPTY(hi))
19556 return HI2UF(hi);
19557 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019558}
19559
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019560#if defined(EXITFREE) || defined(PROTO)
19561 void
19562free_all_functions()
19563{
19564 hashitem_T *hi;
19565
19566 /* Need to start all over every time, because func_free() may change the
19567 * hash table. */
19568 while (func_hashtab.ht_used > 0)
19569 for (hi = func_hashtab.ht_array; ; ++hi)
19570 if (!HASHITEM_EMPTY(hi))
19571 {
19572 func_free(HI2UF(hi));
19573 break;
19574 }
19575}
19576#endif
19577
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019578/*
19579 * Return TRUE if a function "name" exists.
19580 */
19581 static int
19582function_exists(name)
19583 char_u *name;
19584{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000019585 char_u *nm = name;
19586 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019587 int n = FALSE;
19588
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000019589 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000019590 nm = skipwhite(nm);
19591
19592 /* Only accept "funcname", "funcname ", "funcname (..." and
19593 * "funcname(...", not "funcname!...". */
19594 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019595 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019596 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019597 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019598 else
19599 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019600 }
Bram Moolenaar79783442006-05-05 21:18:03 +000019601 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019602 return n;
19603}
19604
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019605/*
19606 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019607 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019608 */
19609 static int
19610builtin_function(name)
19611 char_u *name;
19612{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019613 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
19614 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019615}
19616
Bram Moolenaar05159a02005-02-26 23:04:13 +000019617#if defined(FEAT_PROFILE) || defined(PROTO)
19618/*
19619 * Start profiling function "fp".
19620 */
19621 static void
19622func_do_profile(fp)
19623 ufunc_T *fp;
19624{
19625 fp->uf_tm_count = 0;
19626 profile_zero(&fp->uf_tm_self);
19627 profile_zero(&fp->uf_tm_total);
19628 if (fp->uf_tml_count == NULL)
19629 fp->uf_tml_count = (int *)alloc_clear((unsigned)
19630 (sizeof(int) * fp->uf_lines.ga_len));
19631 if (fp->uf_tml_total == NULL)
19632 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
19633 (sizeof(proftime_T) * fp->uf_lines.ga_len));
19634 if (fp->uf_tml_self == NULL)
19635 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
19636 (sizeof(proftime_T) * fp->uf_lines.ga_len));
19637 fp->uf_tml_idx = -1;
19638 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
19639 || fp->uf_tml_self == NULL)
19640 return; /* out of memory */
19641
19642 fp->uf_profiling = TRUE;
19643}
19644
19645/*
19646 * Dump the profiling results for all functions in file "fd".
19647 */
19648 void
19649func_dump_profile(fd)
19650 FILE *fd;
19651{
19652 hashitem_T *hi;
19653 int todo;
19654 ufunc_T *fp;
19655 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000019656 ufunc_T **sorttab;
19657 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019658
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019659 todo = (int)func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000019660 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
19661
Bram Moolenaar05159a02005-02-26 23:04:13 +000019662 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
19663 {
19664 if (!HASHITEM_EMPTY(hi))
19665 {
19666 --todo;
19667 fp = HI2UF(hi);
19668 if (fp->uf_profiling)
19669 {
Bram Moolenaar73830342005-02-28 22:48:19 +000019670 if (sorttab != NULL)
19671 sorttab[st_len++] = fp;
19672
Bram Moolenaar05159a02005-02-26 23:04:13 +000019673 if (fp->uf_name[0] == K_SPECIAL)
19674 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
19675 else
19676 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
19677 if (fp->uf_tm_count == 1)
19678 fprintf(fd, "Called 1 time\n");
19679 else
19680 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
19681 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
19682 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
19683 fprintf(fd, "\n");
19684 fprintf(fd, "count total (s) self (s)\n");
19685
19686 for (i = 0; i < fp->uf_lines.ga_len; ++i)
19687 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019688 if (FUNCLINE(fp, i) == NULL)
19689 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000019690 prof_func_line(fd, fp->uf_tml_count[i],
19691 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019692 fprintf(fd, "%s\n", FUNCLINE(fp, i));
19693 }
19694 fprintf(fd, "\n");
19695 }
19696 }
19697 }
Bram Moolenaar73830342005-02-28 22:48:19 +000019698
19699 if (sorttab != NULL && st_len > 0)
19700 {
19701 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
19702 prof_total_cmp);
19703 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
19704 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
19705 prof_self_cmp);
19706 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
19707 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019708}
Bram Moolenaar73830342005-02-28 22:48:19 +000019709
19710 static void
19711prof_sort_list(fd, sorttab, st_len, title, prefer_self)
19712 FILE *fd;
19713 ufunc_T **sorttab;
19714 int st_len;
19715 char *title;
19716 int prefer_self; /* when equal print only self time */
19717{
19718 int i;
19719 ufunc_T *fp;
19720
19721 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
19722 fprintf(fd, "count total (s) self (s) function\n");
19723 for (i = 0; i < 20 && i < st_len; ++i)
19724 {
19725 fp = sorttab[i];
19726 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
19727 prefer_self);
19728 if (fp->uf_name[0] == K_SPECIAL)
19729 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
19730 else
19731 fprintf(fd, " %s()\n", fp->uf_name);
19732 }
19733 fprintf(fd, "\n");
19734}
19735
19736/*
19737 * Print the count and times for one function or function line.
19738 */
19739 static void
19740prof_func_line(fd, count, total, self, prefer_self)
19741 FILE *fd;
19742 int count;
19743 proftime_T *total;
19744 proftime_T *self;
19745 int prefer_self; /* when equal print only self time */
19746{
19747 if (count > 0)
19748 {
19749 fprintf(fd, "%5d ", count);
19750 if (prefer_self && profile_equal(total, self))
19751 fprintf(fd, " ");
19752 else
19753 fprintf(fd, "%s ", profile_msg(total));
19754 if (!prefer_self && profile_equal(total, self))
19755 fprintf(fd, " ");
19756 else
19757 fprintf(fd, "%s ", profile_msg(self));
19758 }
19759 else
19760 fprintf(fd, " ");
19761}
19762
19763/*
19764 * Compare function for total time sorting.
19765 */
19766 static int
19767#ifdef __BORLANDC__
19768_RTLENTRYF
19769#endif
19770prof_total_cmp(s1, s2)
19771 const void *s1;
19772 const void *s2;
19773{
19774 ufunc_T *p1, *p2;
19775
19776 p1 = *(ufunc_T **)s1;
19777 p2 = *(ufunc_T **)s2;
19778 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
19779}
19780
19781/*
19782 * Compare function for self time sorting.
19783 */
19784 static int
19785#ifdef __BORLANDC__
19786_RTLENTRYF
19787#endif
19788prof_self_cmp(s1, s2)
19789 const void *s1;
19790 const void *s2;
19791{
19792 ufunc_T *p1, *p2;
19793
19794 p1 = *(ufunc_T **)s1;
19795 p2 = *(ufunc_T **)s2;
19796 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
19797}
19798
Bram Moolenaar05159a02005-02-26 23:04:13 +000019799#endif
19800
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019801/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019802 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019803 * Return TRUE if a package was loaded.
19804 */
19805 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019806script_autoload(name, reload)
19807 char_u *name;
19808 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019809{
19810 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019811 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019812 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019813 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019814
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019815 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019816 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019817 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019818 return FALSE;
19819
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019820 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019821
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019822 /* Find the name in the list of previously loaded package names. Skip
19823 * "autoload/", it's always the same. */
19824 for (i = 0; i < ga_loaded.ga_len; ++i)
19825 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
19826 break;
19827 if (!reload && i < ga_loaded.ga_len)
19828 ret = FALSE; /* was loaded already */
19829 else
19830 {
19831 /* Remember the name if it wasn't loaded already. */
19832 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
19833 {
19834 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
19835 tofree = NULL;
19836 }
19837
19838 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000019839 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019840 ret = TRUE;
19841 }
19842
19843 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019844 return ret;
19845}
19846
19847/*
19848 * Return the autoload script name for a function or variable name.
19849 * Returns NULL when out of memory.
19850 */
19851 static char_u *
19852autoload_name(name)
19853 char_u *name;
19854{
19855 char_u *p;
19856 char_u *scriptname;
19857
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019858 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019859 scriptname = alloc((unsigned)(STRLEN(name) + 14));
19860 if (scriptname == NULL)
19861 return FALSE;
19862 STRCPY(scriptname, "autoload/");
19863 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019864 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019865 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019866 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019867 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019868 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019869}
19870
Bram Moolenaar071d4272004-06-13 20:20:40 +000019871#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
19872
19873/*
19874 * Function given to ExpandGeneric() to obtain the list of user defined
19875 * function names.
19876 */
19877 char_u *
19878get_user_func_name(xp, idx)
19879 expand_T *xp;
19880 int idx;
19881{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019882 static long_u done;
19883 static hashitem_T *hi;
19884 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019885
19886 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019887 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019888 done = 0;
19889 hi = func_hashtab.ht_array;
19890 }
19891 if (done < func_hashtab.ht_used)
19892 {
19893 if (done++ > 0)
19894 ++hi;
19895 while (HASHITEM_EMPTY(hi))
19896 ++hi;
19897 fp = HI2UF(hi);
19898
19899 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
19900 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019901
19902 cat_func_name(IObuff, fp);
19903 if (xp->xp_context != EXPAND_USER_FUNC)
19904 {
19905 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019906 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019907 STRCAT(IObuff, ")");
19908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019909 return IObuff;
19910 }
19911 return NULL;
19912}
19913
19914#endif /* FEAT_CMDL_COMPL */
19915
19916/*
19917 * Copy the function name of "fp" to buffer "buf".
19918 * "buf" must be able to hold the function name plus three bytes.
19919 * Takes care of script-local function names.
19920 */
19921 static void
19922cat_func_name(buf, fp)
19923 char_u *buf;
19924 ufunc_T *fp;
19925{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019926 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019927 {
19928 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019929 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019930 }
19931 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019932 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019933}
19934
19935/*
19936 * ":delfunction {name}"
19937 */
19938 void
19939ex_delfunction(eap)
19940 exarg_T *eap;
19941{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019942 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019943 char_u *p;
19944 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019945 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019946
19947 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019948 name = trans_function_name(&p, eap->skip, 0, &fudi);
19949 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019950 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019951 {
19952 if (fudi.fd_dict != NULL && !eap->skip)
19953 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019954 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019955 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019956 if (!ends_excmd(*skipwhite(p)))
19957 {
19958 vim_free(name);
19959 EMSG(_(e_trailing));
19960 return;
19961 }
19962 eap->nextcmd = check_nextcmd(p);
19963 if (eap->nextcmd != NULL)
19964 *p = NUL;
19965
19966 if (!eap->skip)
19967 fp = find_func(name);
19968 vim_free(name);
19969
19970 if (!eap->skip)
19971 {
19972 if (fp == NULL)
19973 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019974 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019975 return;
19976 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019977 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019978 {
19979 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
19980 return;
19981 }
19982
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019983 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019984 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019985 /* Delete the dict item that refers to the function, it will
19986 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019987 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019988 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019989 else
19990 func_free(fp);
19991 }
19992}
19993
19994/*
19995 * Free a function and remove it from the list of functions.
19996 */
19997 static void
19998func_free(fp)
19999 ufunc_T *fp;
20000{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020001 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020002
20003 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020004 ga_clear_strings(&(fp->uf_args));
20005 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000020006#ifdef FEAT_PROFILE
20007 vim_free(fp->uf_tml_count);
20008 vim_free(fp->uf_tml_total);
20009 vim_free(fp->uf_tml_self);
20010#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020011
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020012 /* remove the function from the function hashtable */
20013 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
20014 if (HASHITEM_EMPTY(hi))
20015 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020016 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020017 hash_remove(&func_hashtab, hi);
20018
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020019 vim_free(fp);
20020}
20021
20022/*
20023 * Unreference a Function: decrement the reference count and free it when it
20024 * becomes zero. Only for numbered functions.
20025 */
20026 static void
20027func_unref(name)
20028 char_u *name;
20029{
20030 ufunc_T *fp;
20031
20032 if (name != NULL && isdigit(*name))
20033 {
20034 fp = find_func(name);
20035 if (fp == NULL)
20036 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020037 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020038 {
20039 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020040 * when "uf_calls" becomes zero. */
20041 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020042 func_free(fp);
20043 }
20044 }
20045}
20046
20047/*
20048 * Count a reference to a Function.
20049 */
20050 static void
20051func_ref(name)
20052 char_u *name;
20053{
20054 ufunc_T *fp;
20055
20056 if (name != NULL && isdigit(*name))
20057 {
20058 fp = find_func(name);
20059 if (fp == NULL)
20060 EMSG2(_(e_intern2), "func_ref()");
20061 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020062 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020063 }
20064}
20065
20066/*
20067 * Call a user function.
20068 */
20069 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000020070call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020071 ufunc_T *fp; /* pointer to function */
20072 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000020073 typval_T *argvars; /* arguments */
20074 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020075 linenr_T firstline; /* first line of range */
20076 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000020077 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020078{
Bram Moolenaar33570922005-01-25 22:26:29 +000020079 char_u *save_sourcing_name;
20080 linenr_T save_sourcing_lnum;
20081 scid_T save_current_SID;
20082 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000020083 int save_did_emsg;
20084 static int depth = 0;
20085 dictitem_T *v;
20086 int fixvar_idx = 0; /* index in fixvar[] */
20087 int i;
20088 int ai;
20089 char_u numbuf[NUMBUFLEN];
20090 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020091#ifdef FEAT_PROFILE
20092 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000020093 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020094#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020095
20096 /* If depth of calling is getting too high, don't execute the function */
20097 if (depth >= p_mfd)
20098 {
20099 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020100 rettv->v_type = VAR_NUMBER;
20101 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020102 return;
20103 }
20104 ++depth;
20105
20106 line_breakcheck(); /* check for CTRL-C hit */
20107
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020108 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000020109 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020110 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020111 fc.rettv = rettv;
20112 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020113 fc.linenr = 0;
20114 fc.returned = FALSE;
20115 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020116 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020117 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020118 fc.dbg_tick = debug_tick;
20119
Bram Moolenaar33570922005-01-25 22:26:29 +000020120 /*
20121 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
20122 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
20123 * each argument variable and saves a lot of time.
20124 */
20125 /*
20126 * Init l: variables.
20127 */
20128 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000020129 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020130 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000020131 /* Set l:self to "selfdict". Use "name" to avoid a warning from
20132 * some compiler that checks the destination size. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020133 v = &fc.fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000020134 name = v->di_key;
20135 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000020136 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
20137 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
20138 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020139 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000020140 v->di_tv.vval.v_dict = selfdict;
20141 ++selfdict->dv_refcount;
20142 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000020143
Bram Moolenaar33570922005-01-25 22:26:29 +000020144 /*
20145 * Init a: variables.
20146 * Set a:0 to "argcount".
20147 * Set a:000 to a list with room for the "..." arguments.
20148 */
20149 init_var_dict(&fc.l_avars, &fc.l_avars_var);
20150 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020151 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000020152 v = &fc.fixvar[fixvar_idx++].var;
20153 STRCPY(v->di_key, "000");
20154 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20155 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
20156 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020157 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020158 v->di_tv.vval.v_list = &fc.l_varlist;
20159 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
20160 fc.l_varlist.lv_refcount = 99999;
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000020161 fc.l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020162
20163 /*
20164 * Set a:firstline to "firstline" and a:lastline to "lastline".
20165 * Set a:name to named arguments.
20166 * Set a:N to the "..." arguments.
20167 */
20168 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
20169 (varnumber_T)firstline);
20170 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
20171 (varnumber_T)lastline);
20172 for (i = 0; i < argcount; ++i)
20173 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020174 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000020175 if (ai < 0)
20176 /* named argument a:name */
20177 name = FUNCARG(fp, i);
20178 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000020179 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020180 /* "..." argument a:1, a:2, etc. */
20181 sprintf((char *)numbuf, "%d", ai + 1);
20182 name = numbuf;
20183 }
20184 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
20185 {
20186 v = &fc.fixvar[fixvar_idx++].var;
20187 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20188 }
20189 else
20190 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020191 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
20192 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000020193 if (v == NULL)
20194 break;
20195 v->di_flags = DI_FLAGS_RO;
20196 }
20197 STRCPY(v->di_key, name);
20198 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
20199
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020200 /* Note: the values are copied directly to avoid alloc/free.
20201 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020202 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020203 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020204
20205 if (ai >= 0 && ai < MAX_FUNC_ARGS)
20206 {
20207 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
20208 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020209 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020210 }
20211 }
20212
Bram Moolenaar071d4272004-06-13 20:20:40 +000020213 /* Don't redraw while executing the function. */
20214 ++RedrawingDisabled;
20215 save_sourcing_name = sourcing_name;
20216 save_sourcing_lnum = sourcing_lnum;
20217 sourcing_lnum = 1;
20218 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020219 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020220 if (sourcing_name != NULL)
20221 {
20222 if (save_sourcing_name != NULL
20223 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
20224 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
20225 else
20226 STRCPY(sourcing_name, "function ");
20227 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
20228
20229 if (p_verbose >= 12)
20230 {
20231 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020232 verbose_enter_scroll();
20233
Bram Moolenaar555b2802005-05-19 21:08:39 +000020234 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020235 if (p_verbose >= 14)
20236 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020237 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000020238 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000020239 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000020240 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020241
20242 msg_puts((char_u *)"(");
20243 for (i = 0; i < argcount; ++i)
20244 {
20245 if (i > 0)
20246 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020247 if (argvars[i].v_type == VAR_NUMBER)
20248 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020249 else
20250 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000020251 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
20252 if (s != NULL)
20253 {
20254 trunc_string(s, buf, MSG_BUF_CLEN);
20255 msg_puts(buf);
20256 vim_free(tofree);
20257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020258 }
20259 }
20260 msg_puts((char_u *)")");
20261 }
20262 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020263
20264 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020265 --no_wait_return;
20266 }
20267 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000020268#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020269 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020270 {
20271 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
20272 func_do_profile(fp);
20273 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020274 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000020275 {
20276 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000020277 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020278 profile_zero(&fp->uf_tm_children);
20279 }
20280 script_prof_save(&wait_start);
20281 }
20282#endif
20283
Bram Moolenaar071d4272004-06-13 20:20:40 +000020284 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020285 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020286 save_did_emsg = did_emsg;
20287 did_emsg = FALSE;
20288
20289 /* call do_cmdline() to execute the lines */
20290 do_cmdline(NULL, get_func_line, (void *)&fc,
20291 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
20292
20293 --RedrawingDisabled;
20294
20295 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020296 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020297 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020298 clear_tv(rettv);
20299 rettv->v_type = VAR_NUMBER;
20300 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020301 }
20302
Bram Moolenaar05159a02005-02-26 23:04:13 +000020303#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020304 if (do_profiling == PROF_YES && (fp->uf_profiling
20305 || (fc.caller != NULL && &fc.caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000020306 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000020307 profile_end(&call_start);
20308 profile_sub_wait(&wait_start, &call_start);
20309 profile_add(&fp->uf_tm_total, &call_start);
20310 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020311 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020312 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000020313 profile_add(&fc.caller->func->uf_tm_children, &call_start);
20314 profile_add(&fc.caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020315 }
20316 }
20317#endif
20318
Bram Moolenaar071d4272004-06-13 20:20:40 +000020319 /* when being verbose, mention the return value */
20320 if (p_verbose >= 12)
20321 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020322 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020323 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020324
Bram Moolenaar071d4272004-06-13 20:20:40 +000020325 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000020326 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020327 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000020328 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
20329 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000020330 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020331 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000020332 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000020333 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000020334 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000020335 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020336
Bram Moolenaar555b2802005-05-19 21:08:39 +000020337 /* The value may be very long. Skip the middle part, so that we
20338 * have some idea how it starts and ends. smsg() would always
20339 * truncate it at the end. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000020340 s = tv2string(fc.rettv, &tofree, numbuf2, 0);
20341 if (s != NULL)
20342 {
20343 trunc_string(s, buf, MSG_BUF_CLEN);
20344 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
20345 vim_free(tofree);
20346 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020347 }
20348 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020349
20350 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020351 --no_wait_return;
20352 }
20353
20354 vim_free(sourcing_name);
20355 sourcing_name = save_sourcing_name;
20356 sourcing_lnum = save_sourcing_lnum;
20357 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020358#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020359 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020360 script_prof_restore(&wait_start);
20361#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020362
20363 if (p_verbose >= 12 && sourcing_name != NULL)
20364 {
20365 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020366 verbose_enter_scroll();
20367
Bram Moolenaar555b2802005-05-19 21:08:39 +000020368 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020369 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020370
20371 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020372 --no_wait_return;
20373 }
20374
20375 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020376 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020377
Bram Moolenaar33570922005-01-25 22:26:29 +000020378 /* The a: variables typevals were not alloced, only free the allocated
20379 * variables. */
20380 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
20381
20382 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020383 --depth;
20384}
20385
20386/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020387 * Add a number variable "name" to dict "dp" with value "nr".
20388 */
20389 static void
20390add_nr_var(dp, v, name, nr)
20391 dict_T *dp;
20392 dictitem_T *v;
20393 char *name;
20394 varnumber_T nr;
20395{
20396 STRCPY(v->di_key, name);
20397 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20398 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
20399 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020400 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020401 v->di_tv.vval.v_number = nr;
20402}
20403
20404/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020405 * ":return [expr]"
20406 */
20407 void
20408ex_return(eap)
20409 exarg_T *eap;
20410{
20411 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020412 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020413 int returning = FALSE;
20414
20415 if (current_funccal == NULL)
20416 {
20417 EMSG(_("E133: :return not inside a function"));
20418 return;
20419 }
20420
20421 if (eap->skip)
20422 ++emsg_skip;
20423
20424 eap->nextcmd = NULL;
20425 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020426 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020427 {
20428 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020429 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020430 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020431 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020432 }
20433 /* It's safer to return also on error. */
20434 else if (!eap->skip)
20435 {
20436 /*
20437 * Return unless the expression evaluation has been cancelled due to an
20438 * aborting error, an interrupt, or an exception.
20439 */
20440 if (!aborting())
20441 returning = do_return(eap, FALSE, TRUE, NULL);
20442 }
20443
20444 /* When skipping or the return gets pending, advance to the next command
20445 * in this line (!returning). Otherwise, ignore the rest of the line.
20446 * Following lines will be ignored by get_func_line(). */
20447 if (returning)
20448 eap->nextcmd = NULL;
20449 else if (eap->nextcmd == NULL) /* no argument */
20450 eap->nextcmd = check_nextcmd(arg);
20451
20452 if (eap->skip)
20453 --emsg_skip;
20454}
20455
20456/*
20457 * Return from a function. Possibly makes the return pending. Also called
20458 * for a pending return at the ":endtry" or after returning from an extra
20459 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000020460 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020461 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020462 * FALSE when the return gets pending.
20463 */
20464 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020465do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020466 exarg_T *eap;
20467 int reanimate;
20468 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020469 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020470{
20471 int idx;
20472 struct condstack *cstack = eap->cstack;
20473
20474 if (reanimate)
20475 /* Undo the return. */
20476 current_funccal->returned = FALSE;
20477
20478 /*
20479 * Cleanup (and inactivate) conditionals, but stop when a try conditional
20480 * not in its finally clause (which then is to be executed next) is found.
20481 * In this case, make the ":return" pending for execution at the ":endtry".
20482 * Otherwise, return normally.
20483 */
20484 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
20485 if (idx >= 0)
20486 {
20487 cstack->cs_pending[idx] = CSTP_RETURN;
20488
20489 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020490 /* A pending return again gets pending. "rettv" points to an
20491 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000020492 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020493 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020494 else
20495 {
20496 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020497 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020498 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020499 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020500
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020501 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020502 {
20503 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020504 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020505 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020506 else
20507 EMSG(_(e_outofmem));
20508 }
20509 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020510 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020511
20512 if (reanimate)
20513 {
20514 /* The pending return value could be overwritten by a ":return"
20515 * without argument in a finally clause; reset the default
20516 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020517 current_funccal->rettv->v_type = VAR_NUMBER;
20518 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020519 }
20520 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020521 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020522 }
20523 else
20524 {
20525 current_funccal->returned = TRUE;
20526
20527 /* If the return is carried out now, store the return value. For
20528 * a return immediately after reanimation, the value is already
20529 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020530 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020531 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020532 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000020533 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020534 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020535 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020536 }
20537 }
20538
20539 return idx < 0;
20540}
20541
20542/*
20543 * Free the variable with a pending return value.
20544 */
20545 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020546discard_pending_return(rettv)
20547 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020548{
Bram Moolenaar33570922005-01-25 22:26:29 +000020549 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020550}
20551
20552/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020553 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000020554 * is an allocated string. Used by report_pending() for verbose messages.
20555 */
20556 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020557get_return_cmd(rettv)
20558 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020559{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020560 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020561 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020562 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020563
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020564 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000020565 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020566 if (s == NULL)
20567 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020568
20569 STRCPY(IObuff, ":return ");
20570 STRNCPY(IObuff + 8, s, IOSIZE - 8);
20571 if (STRLEN(s) + 8 >= IOSIZE)
20572 STRCPY(IObuff + IOSIZE - 4, "...");
20573 vim_free(tofree);
20574 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020575}
20576
20577/*
20578 * Get next function line.
20579 * Called by do_cmdline() to get the next line.
20580 * Returns allocated string, or NULL for end of function.
20581 */
20582/* ARGSUSED */
20583 char_u *
20584get_func_line(c, cookie, indent)
20585 int c; /* not used */
20586 void *cookie;
20587 int indent; /* not used */
20588{
Bram Moolenaar33570922005-01-25 22:26:29 +000020589 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020590 ufunc_T *fp = fcp->func;
20591 char_u *retval;
20592 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020593
20594 /* If breakpoints have been added/deleted need to check for it. */
20595 if (fcp->dbg_tick != debug_tick)
20596 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020597 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020598 sourcing_lnum);
20599 fcp->dbg_tick = debug_tick;
20600 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000020601#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020602 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020603 func_line_end(cookie);
20604#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020605
Bram Moolenaar05159a02005-02-26 23:04:13 +000020606 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020607 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
20608 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020609 retval = NULL;
20610 else
20611 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020612 /* Skip NULL lines (continuation lines). */
20613 while (fcp->linenr < gap->ga_len
20614 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
20615 ++fcp->linenr;
20616 if (fcp->linenr >= gap->ga_len)
20617 retval = NULL;
20618 else
20619 {
20620 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
20621 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020622#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020623 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020624 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020625#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020627 }
20628
20629 /* Did we encounter a breakpoint? */
20630 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
20631 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020632 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020633 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020634 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020635 sourcing_lnum);
20636 fcp->dbg_tick = debug_tick;
20637 }
20638
20639 return retval;
20640}
20641
Bram Moolenaar05159a02005-02-26 23:04:13 +000020642#if defined(FEAT_PROFILE) || defined(PROTO)
20643/*
20644 * Called when starting to read a function line.
20645 * "sourcing_lnum" must be correct!
20646 * When skipping lines it may not actually be executed, but we won't find out
20647 * until later and we need to store the time now.
20648 */
20649 void
20650func_line_start(cookie)
20651 void *cookie;
20652{
20653 funccall_T *fcp = (funccall_T *)cookie;
20654 ufunc_T *fp = fcp->func;
20655
20656 if (fp->uf_profiling && sourcing_lnum >= 1
20657 && sourcing_lnum <= fp->uf_lines.ga_len)
20658 {
20659 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020660 /* Skip continuation lines. */
20661 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
20662 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020663 fp->uf_tml_execed = FALSE;
20664 profile_start(&fp->uf_tml_start);
20665 profile_zero(&fp->uf_tml_children);
20666 profile_get_wait(&fp->uf_tml_wait);
20667 }
20668}
20669
20670/*
20671 * Called when actually executing a function line.
20672 */
20673 void
20674func_line_exec(cookie)
20675 void *cookie;
20676{
20677 funccall_T *fcp = (funccall_T *)cookie;
20678 ufunc_T *fp = fcp->func;
20679
20680 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
20681 fp->uf_tml_execed = TRUE;
20682}
20683
20684/*
20685 * Called when done with a function line.
20686 */
20687 void
20688func_line_end(cookie)
20689 void *cookie;
20690{
20691 funccall_T *fcp = (funccall_T *)cookie;
20692 ufunc_T *fp = fcp->func;
20693
20694 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
20695 {
20696 if (fp->uf_tml_execed)
20697 {
20698 ++fp->uf_tml_count[fp->uf_tml_idx];
20699 profile_end(&fp->uf_tml_start);
20700 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020701 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000020702 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
20703 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020704 }
20705 fp->uf_tml_idx = -1;
20706 }
20707}
20708#endif
20709
Bram Moolenaar071d4272004-06-13 20:20:40 +000020710/*
20711 * Return TRUE if the currently active function should be ended, because a
20712 * return was encountered or an error occured. Used inside a ":while".
20713 */
20714 int
20715func_has_ended(cookie)
20716 void *cookie;
20717{
Bram Moolenaar33570922005-01-25 22:26:29 +000020718 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020719
20720 /* Ignore the "abort" flag if the abortion behavior has been changed due to
20721 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020722 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000020723 || fcp->returned);
20724}
20725
20726/*
20727 * return TRUE if cookie indicates a function which "abort"s on errors.
20728 */
20729 int
20730func_has_abort(cookie)
20731 void *cookie;
20732{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020733 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020734}
20735
20736#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
20737typedef enum
20738{
20739 VAR_FLAVOUR_DEFAULT,
20740 VAR_FLAVOUR_SESSION,
20741 VAR_FLAVOUR_VIMINFO
20742} var_flavour_T;
20743
20744static var_flavour_T var_flavour __ARGS((char_u *varname));
20745
20746 static var_flavour_T
20747var_flavour(varname)
20748 char_u *varname;
20749{
20750 char_u *p = varname;
20751
20752 if (ASCII_ISUPPER(*p))
20753 {
20754 while (*(++p))
20755 if (ASCII_ISLOWER(*p))
20756 return VAR_FLAVOUR_SESSION;
20757 return VAR_FLAVOUR_VIMINFO;
20758 }
20759 else
20760 return VAR_FLAVOUR_DEFAULT;
20761}
20762#endif
20763
20764#if defined(FEAT_VIMINFO) || defined(PROTO)
20765/*
20766 * Restore global vars that start with a capital from the viminfo file
20767 */
20768 int
20769read_viminfo_varlist(virp, writing)
20770 vir_T *virp;
20771 int writing;
20772{
20773 char_u *tab;
20774 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000020775 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020776
20777 if (!writing && (find_viminfo_parameter('!') != NULL))
20778 {
20779 tab = vim_strchr(virp->vir_line + 1, '\t');
20780 if (tab != NULL)
20781 {
20782 *tab++ = '\0'; /* isolate the variable name */
20783 if (*tab == 'S') /* string var */
20784 is_string = TRUE;
20785
20786 tab = vim_strchr(tab, '\t');
20787 if (tab != NULL)
20788 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020789 if (is_string)
20790 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020791 tv.v_type = VAR_STRING;
20792 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020793 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020794 }
20795 else
20796 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020797 tv.v_type = VAR_NUMBER;
20798 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020799 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020800 set_var(virp->vir_line + 1, &tv, FALSE);
20801 if (is_string)
20802 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020803 }
20804 }
20805 }
20806
20807 return viminfo_readline(virp);
20808}
20809
20810/*
20811 * Write global vars that start with a capital to the viminfo file
20812 */
20813 void
20814write_viminfo_varlist(fp)
20815 FILE *fp;
20816{
Bram Moolenaar33570922005-01-25 22:26:29 +000020817 hashitem_T *hi;
20818 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020819 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020820 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020821 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020822 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020823 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020824
20825 if (find_viminfo_parameter('!') == NULL)
20826 return;
20827
20828 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000020829
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020830 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000020831 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020832 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020833 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020834 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020835 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020836 this_var = HI2DI(hi);
20837 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020838 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020839 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000020840 {
20841 case VAR_STRING: s = "STR"; break;
20842 case VAR_NUMBER: s = "NUM"; break;
20843 default: continue;
20844 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020845 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000020846 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020847 if (p != NULL)
20848 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000020849 vim_free(tofree);
20850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020851 }
20852 }
20853}
20854#endif
20855
20856#if defined(FEAT_SESSION) || defined(PROTO)
20857 int
20858store_session_globals(fd)
20859 FILE *fd;
20860{
Bram Moolenaar33570922005-01-25 22:26:29 +000020861 hashitem_T *hi;
20862 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020863 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020864 char_u *p, *t;
20865
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020866 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000020867 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020868 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020869 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020870 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020871 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020872 this_var = HI2DI(hi);
20873 if ((this_var->di_tv.v_type == VAR_NUMBER
20874 || this_var->di_tv.v_type == VAR_STRING)
20875 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020876 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020877 /* Escape special characters with a backslash. Turn a LF and
20878 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020879 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000020880 (char_u *)"\\\"\n\r");
20881 if (p == NULL) /* out of memory */
20882 break;
20883 for (t = p; *t != NUL; ++t)
20884 if (*t == '\n')
20885 *t = 'n';
20886 else if (*t == '\r')
20887 *t = 'r';
20888 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000020889 this_var->di_key,
20890 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20891 : ' ',
20892 p,
20893 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20894 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000020895 || put_eol(fd) == FAIL)
20896 {
20897 vim_free(p);
20898 return FAIL;
20899 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020900 vim_free(p);
20901 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020902 }
20903 }
20904 return OK;
20905}
20906#endif
20907
Bram Moolenaar661b1822005-07-28 22:36:45 +000020908/*
20909 * Display script name where an item was last set.
20910 * Should only be invoked when 'verbose' is non-zero.
20911 */
20912 void
20913last_set_msg(scriptID)
20914 scid_T scriptID;
20915{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020916 char_u *p;
20917
Bram Moolenaar661b1822005-07-28 22:36:45 +000020918 if (scriptID != 0)
20919 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020920 p = home_replace_save(NULL, get_scriptname(scriptID));
20921 if (p != NULL)
20922 {
20923 verbose_enter();
20924 MSG_PUTS(_("\n\tLast set from "));
20925 MSG_PUTS(p);
20926 vim_free(p);
20927 verbose_leave();
20928 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000020929 }
20930}
20931
Bram Moolenaar071d4272004-06-13 20:20:40 +000020932#endif /* FEAT_EVAL */
20933
20934#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
20935
20936
20937#ifdef WIN3264
20938/*
20939 * Functions for ":8" filename modifier: get 8.3 version of a filename.
20940 */
20941static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
20942static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
20943static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
20944
20945/*
20946 * Get the short pathname of a file.
Bram Moolenaarbae0c162007-05-10 19:30:25 +000020947 * Returns 1 on success. *fnamelen is 0 for nonexistent path.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020948 */
20949 static int
20950get_short_pathname(fnamep, bufp, fnamelen)
20951 char_u **fnamep;
20952 char_u **bufp;
20953 int *fnamelen;
20954{
20955 int l,len;
20956 char_u *newbuf;
20957
20958 len = *fnamelen;
20959
20960 l = GetShortPathName(*fnamep, *fnamep, len);
20961 if (l > len - 1)
20962 {
20963 /* If that doesn't work (not enough space), then save the string
20964 * and try again with a new buffer big enough
20965 */
20966 newbuf = vim_strnsave(*fnamep, l);
20967 if (newbuf == NULL)
20968 return 0;
20969
20970 vim_free(*bufp);
20971 *fnamep = *bufp = newbuf;
20972
20973 l = GetShortPathName(*fnamep,*fnamep,l+1);
20974
20975 /* Really should always succeed, as the buffer is big enough */
20976 }
20977
20978 *fnamelen = l;
20979 return 1;
20980}
20981
20982/*
20983 * Create a short path name. Returns the length of the buffer it needs.
20984 * Doesn't copy over the end of the buffer passed in.
20985 */
20986 static int
20987shortpath_for_invalid_fname(fname, bufp, fnamelen)
20988 char_u **fname;
20989 char_u **bufp;
20990 int *fnamelen;
20991{
20992 char_u *s, *p, *pbuf2, *pbuf3;
20993 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020994 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020995
20996 /* Make a copy */
20997 len2 = *fnamelen;
20998 pbuf2 = vim_strnsave(*fname, len2);
20999 pbuf3 = NULL;
21000
21001 s = pbuf2 + len2 - 1; /* Find the end */
21002 slen = 1;
21003 plen = len2;
21004
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021005 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021006 {
21007 --s;
21008 ++slen;
21009 --plen;
21010 }
21011
21012 do
21013 {
Bram Moolenaarbae0c162007-05-10 19:30:25 +000021014 /* Go back one path-separator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021015 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021016 {
21017 --s;
21018 ++slen;
21019 --plen;
21020 }
21021 if (s <= pbuf2)
21022 break;
21023
Bram Moolenaarbae0c162007-05-10 19:30:25 +000021024 /* Remember the character that is about to be splatted */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021025 ch = *s;
21026 *s = 0; /* get_short_pathname requires a null-terminated string */
21027
21028 /* Try it in situ */
21029 p = pbuf2;
21030 if (!get_short_pathname(&p, &pbuf3, &plen))
21031 {
21032 vim_free(pbuf2);
21033 return -1;
21034 }
21035 *s = ch; /* Preserve the string */
21036 } while (plen == 0);
21037
21038 if (plen > 0)
21039 {
Bram Moolenaarbae0c162007-05-10 19:30:25 +000021040 /* Remember the length of the new string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021041 *fnamelen = len = plen + slen;
21042 vim_free(*bufp);
21043 if (len > len2)
21044 {
21045 /* If there's not enough space in the currently allocated string,
21046 * then copy it to a buffer big enough.
21047 */
21048 *fname= *bufp = vim_strnsave(p, len);
21049 if (*fname == NULL)
21050 return -1;
21051 }
21052 else
21053 {
21054 /* Transfer pbuf2 to being the main buffer (it's big enough) */
21055 *fname = *bufp = pbuf2;
21056 if (p != pbuf2)
21057 strncpy(*fname, p, plen);
21058 pbuf2 = NULL;
21059 }
21060 /* Concat the next bit */
21061 strncpy(*fname + plen, s, slen);
21062 (*fname)[len] = '\0';
21063 }
21064 vim_free(pbuf3);
21065 vim_free(pbuf2);
21066 return 0;
21067}
21068
21069/*
21070 * Get a pathname for a partial path.
21071 */
21072 static int
21073shortpath_for_partial(fnamep, bufp, fnamelen)
21074 char_u **fnamep;
21075 char_u **bufp;
21076 int *fnamelen;
21077{
21078 int sepcount, len, tflen;
21079 char_u *p;
21080 char_u *pbuf, *tfname;
21081 int hasTilde;
21082
21083 /* Count up the path seperators from the RHS.. so we know which part
21084 * of the path to return.
21085 */
21086 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021087 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021088 if (vim_ispathsep(*p))
21089 ++sepcount;
21090
21091 /* Need full path first (use expand_env() to remove a "~/") */
21092 hasTilde = (**fnamep == '~');
21093 if (hasTilde)
21094 pbuf = tfname = expand_env_save(*fnamep);
21095 else
21096 pbuf = tfname = FullName_save(*fnamep, FALSE);
21097
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021098 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021099
21100 if (!get_short_pathname(&tfname, &pbuf, &len))
21101 return -1;
21102
21103 if (len == 0)
21104 {
21105 /* Don't have a valid filename, so shorten the rest of the
21106 * path if we can. This CAN give us invalid 8.3 filenames, but
21107 * there's not a lot of point in guessing what it might be.
21108 */
21109 len = tflen;
21110 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
21111 return -1;
21112 }
21113
21114 /* Count the paths backward to find the beginning of the desired string. */
21115 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021116 {
21117#ifdef FEAT_MBYTE
21118 if (has_mbyte)
21119 p -= mb_head_off(tfname, p);
21120#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021121 if (vim_ispathsep(*p))
21122 {
21123 if (sepcount == 0 || (hasTilde && sepcount == 1))
21124 break;
21125 else
21126 sepcount --;
21127 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021128 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021129 if (hasTilde)
21130 {
21131 --p;
21132 if (p >= tfname)
21133 *p = '~';
21134 else
21135 return -1;
21136 }
21137 else
21138 ++p;
21139
21140 /* Copy in the string - p indexes into tfname - allocated at pbuf */
21141 vim_free(*bufp);
21142 *fnamelen = (int)STRLEN(p);
21143 *bufp = pbuf;
21144 *fnamep = p;
21145
21146 return 0;
21147}
21148#endif /* WIN3264 */
21149
21150/*
21151 * Adjust a filename, according to a string of modifiers.
21152 * *fnamep must be NUL terminated when called. When returning, the length is
21153 * determined by *fnamelen.
21154 * Returns valid flags.
21155 * When there is an error, *fnamep is set to NULL.
21156 */
21157 int
21158modify_fname(src, usedlen, fnamep, bufp, fnamelen)
21159 char_u *src; /* string with modifiers */
21160 int *usedlen; /* characters after src that are used */
21161 char_u **fnamep; /* file name so far */
21162 char_u **bufp; /* buffer for allocated file name or NULL */
21163 int *fnamelen; /* length of fnamep */
21164{
21165 int valid = 0;
21166 char_u *tail;
21167 char_u *s, *p, *pbuf;
21168 char_u dirname[MAXPATHL];
21169 int c;
21170 int has_fullname = 0;
21171#ifdef WIN3264
21172 int has_shortname = 0;
21173#endif
21174
21175repeat:
21176 /* ":p" - full path/file_name */
21177 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
21178 {
21179 has_fullname = 1;
21180
21181 valid |= VALID_PATH;
21182 *usedlen += 2;
21183
21184 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
21185 if ((*fnamep)[0] == '~'
21186#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
21187 && ((*fnamep)[1] == '/'
21188# ifdef BACKSLASH_IN_FILENAME
21189 || (*fnamep)[1] == '\\'
21190# endif
21191 || (*fnamep)[1] == NUL)
21192
21193#endif
21194 )
21195 {
21196 *fnamep = expand_env_save(*fnamep);
21197 vim_free(*bufp); /* free any allocated file name */
21198 *bufp = *fnamep;
21199 if (*fnamep == NULL)
21200 return -1;
21201 }
21202
21203 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021204 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021205 {
21206 if (vim_ispathsep(*p)
21207 && p[1] == '.'
21208 && (p[2] == NUL
21209 || vim_ispathsep(p[2])
21210 || (p[2] == '.'
21211 && (p[3] == NUL || vim_ispathsep(p[3])))))
21212 break;
21213 }
21214
21215 /* FullName_save() is slow, don't use it when not needed. */
21216 if (*p != NUL || !vim_isAbsName(*fnamep))
21217 {
21218 *fnamep = FullName_save(*fnamep, *p != NUL);
21219 vim_free(*bufp); /* free any allocated file name */
21220 *bufp = *fnamep;
21221 if (*fnamep == NULL)
21222 return -1;
21223 }
21224
21225 /* Append a path separator to a directory. */
21226 if (mch_isdir(*fnamep))
21227 {
21228 /* Make room for one or two extra characters. */
21229 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
21230 vim_free(*bufp); /* free any allocated file name */
21231 *bufp = *fnamep;
21232 if (*fnamep == NULL)
21233 return -1;
21234 add_pathsep(*fnamep);
21235 }
21236 }
21237
21238 /* ":." - path relative to the current directory */
21239 /* ":~" - path relative to the home directory */
21240 /* ":8" - shortname path - postponed till after */
21241 while (src[*usedlen] == ':'
21242 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
21243 {
21244 *usedlen += 2;
21245 if (c == '8')
21246 {
21247#ifdef WIN3264
21248 has_shortname = 1; /* Postpone this. */
21249#endif
21250 continue;
21251 }
21252 pbuf = NULL;
21253 /* Need full path first (use expand_env() to remove a "~/") */
21254 if (!has_fullname)
21255 {
21256 if (c == '.' && **fnamep == '~')
21257 p = pbuf = expand_env_save(*fnamep);
21258 else
21259 p = pbuf = FullName_save(*fnamep, FALSE);
21260 }
21261 else
21262 p = *fnamep;
21263
21264 has_fullname = 0;
21265
21266 if (p != NULL)
21267 {
21268 if (c == '.')
21269 {
21270 mch_dirname(dirname, MAXPATHL);
21271 s = shorten_fname(p, dirname);
21272 if (s != NULL)
21273 {
21274 *fnamep = s;
21275 if (pbuf != NULL)
21276 {
21277 vim_free(*bufp); /* free any allocated file name */
21278 *bufp = pbuf;
21279 pbuf = NULL;
21280 }
21281 }
21282 }
21283 else
21284 {
21285 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
21286 /* Only replace it when it starts with '~' */
21287 if (*dirname == '~')
21288 {
21289 s = vim_strsave(dirname);
21290 if (s != NULL)
21291 {
21292 *fnamep = s;
21293 vim_free(*bufp);
21294 *bufp = s;
21295 }
21296 }
21297 }
21298 vim_free(pbuf);
21299 }
21300 }
21301
21302 tail = gettail(*fnamep);
21303 *fnamelen = (int)STRLEN(*fnamep);
21304
21305 /* ":h" - head, remove "/file_name", can be repeated */
21306 /* Don't remove the first "/" or "c:\" */
21307 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
21308 {
21309 valid |= VALID_HEAD;
21310 *usedlen += 2;
21311 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021312 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000021313 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021314 *fnamelen = (int)(tail - *fnamep);
21315#ifdef VMS
21316 if (*fnamelen > 0)
21317 *fnamelen += 1; /* the path separator is part of the path */
21318#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000021319 if (*fnamelen == 0)
21320 {
21321 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
21322 p = vim_strsave((char_u *)".");
21323 if (p == NULL)
21324 return -1;
21325 vim_free(*bufp);
21326 *bufp = *fnamep = tail = p;
21327 *fnamelen = 1;
21328 }
21329 else
21330 {
21331 while (tail > s && !after_pathsep(s, tail))
21332 mb_ptr_back(*fnamep, tail);
21333 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021334 }
21335
21336 /* ":8" - shortname */
21337 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
21338 {
21339 *usedlen += 2;
21340#ifdef WIN3264
21341 has_shortname = 1;
21342#endif
21343 }
21344
21345#ifdef WIN3264
21346 /* Check shortname after we have done 'heads' and before we do 'tails'
21347 */
21348 if (has_shortname)
21349 {
21350 pbuf = NULL;
21351 /* Copy the string if it is shortened by :h */
21352 if (*fnamelen < (int)STRLEN(*fnamep))
21353 {
21354 p = vim_strnsave(*fnamep, *fnamelen);
21355 if (p == 0)
21356 return -1;
21357 vim_free(*bufp);
21358 *bufp = *fnamep = p;
21359 }
21360
21361 /* Split into two implementations - makes it easier. First is where
21362 * there isn't a full name already, second is where there is.
21363 */
21364 if (!has_fullname && !vim_isAbsName(*fnamep))
21365 {
21366 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
21367 return -1;
21368 }
21369 else
21370 {
21371 int l;
21372
21373 /* Simple case, already have the full-name
21374 * Nearly always shorter, so try first time. */
21375 l = *fnamelen;
21376 if (!get_short_pathname(fnamep, bufp, &l))
21377 return -1;
21378
21379 if (l == 0)
21380 {
21381 /* Couldn't find the filename.. search the paths.
21382 */
21383 l = *fnamelen;
21384 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
21385 return -1;
21386 }
21387 *fnamelen = l;
21388 }
21389 }
21390#endif /* WIN3264 */
21391
21392 /* ":t" - tail, just the basename */
21393 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
21394 {
21395 *usedlen += 2;
21396 *fnamelen -= (int)(tail - *fnamep);
21397 *fnamep = tail;
21398 }
21399
21400 /* ":e" - extension, can be repeated */
21401 /* ":r" - root, without extension, can be repeated */
21402 while (src[*usedlen] == ':'
21403 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
21404 {
21405 /* find a '.' in the tail:
21406 * - for second :e: before the current fname
21407 * - otherwise: The last '.'
21408 */
21409 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
21410 s = *fnamep - 2;
21411 else
21412 s = *fnamep + *fnamelen - 1;
21413 for ( ; s > tail; --s)
21414 if (s[0] == '.')
21415 break;
21416 if (src[*usedlen + 1] == 'e') /* :e */
21417 {
21418 if (s > tail)
21419 {
21420 *fnamelen += (int)(*fnamep - (s + 1));
21421 *fnamep = s + 1;
21422#ifdef VMS
21423 /* cut version from the extension */
21424 s = *fnamep + *fnamelen - 1;
21425 for ( ; s > *fnamep; --s)
21426 if (s[0] == ';')
21427 break;
21428 if (s > *fnamep)
21429 *fnamelen = s - *fnamep;
21430#endif
21431 }
21432 else if (*fnamep <= tail)
21433 *fnamelen = 0;
21434 }
21435 else /* :r */
21436 {
21437 if (s > tail) /* remove one extension */
21438 *fnamelen = (int)(s - *fnamep);
21439 }
21440 *usedlen += 2;
21441 }
21442
21443 /* ":s?pat?foo?" - substitute */
21444 /* ":gs?pat?foo?" - global substitute */
21445 if (src[*usedlen] == ':'
21446 && (src[*usedlen + 1] == 's'
21447 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
21448 {
21449 char_u *str;
21450 char_u *pat;
21451 char_u *sub;
21452 int sep;
21453 char_u *flags;
21454 int didit = FALSE;
21455
21456 flags = (char_u *)"";
21457 s = src + *usedlen + 2;
21458 if (src[*usedlen + 1] == 'g')
21459 {
21460 flags = (char_u *)"g";
21461 ++s;
21462 }
21463
21464 sep = *s++;
21465 if (sep)
21466 {
21467 /* find end of pattern */
21468 p = vim_strchr(s, sep);
21469 if (p != NULL)
21470 {
21471 pat = vim_strnsave(s, (int)(p - s));
21472 if (pat != NULL)
21473 {
21474 s = p + 1;
21475 /* find end of substitution */
21476 p = vim_strchr(s, sep);
21477 if (p != NULL)
21478 {
21479 sub = vim_strnsave(s, (int)(p - s));
21480 str = vim_strnsave(*fnamep, *fnamelen);
21481 if (sub != NULL && str != NULL)
21482 {
21483 *usedlen = (int)(p + 1 - src);
21484 s = do_string_sub(str, pat, sub, flags);
21485 if (s != NULL)
21486 {
21487 *fnamep = s;
21488 *fnamelen = (int)STRLEN(s);
21489 vim_free(*bufp);
21490 *bufp = s;
21491 didit = TRUE;
21492 }
21493 }
21494 vim_free(sub);
21495 vim_free(str);
21496 }
21497 vim_free(pat);
21498 }
21499 }
21500 /* after using ":s", repeat all the modifiers */
21501 if (didit)
21502 goto repeat;
21503 }
21504 }
21505
21506 return valid;
21507}
21508
21509/*
21510 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
21511 * "flags" can be "g" to do a global substitute.
21512 * Returns an allocated string, NULL for error.
21513 */
21514 char_u *
21515do_string_sub(str, pat, sub, flags)
21516 char_u *str;
21517 char_u *pat;
21518 char_u *sub;
21519 char_u *flags;
21520{
21521 int sublen;
21522 regmatch_T regmatch;
21523 int i;
21524 int do_all;
21525 char_u *tail;
21526 garray_T ga;
21527 char_u *ret;
21528 char_u *save_cpo;
21529
21530 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
21531 save_cpo = p_cpo;
21532 p_cpo = (char_u *)"";
21533
21534 ga_init2(&ga, 1, 200);
21535
21536 do_all = (flags[0] == 'g');
21537
21538 regmatch.rm_ic = p_ic;
21539 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
21540 if (regmatch.regprog != NULL)
21541 {
21542 tail = str;
21543 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
21544 {
21545 /*
21546 * Get some space for a temporary buffer to do the substitution
21547 * into. It will contain:
21548 * - The text up to where the match is.
21549 * - The substituted text.
21550 * - The text after the match.
21551 */
21552 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
21553 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
21554 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
21555 {
21556 ga_clear(&ga);
21557 break;
21558 }
21559
21560 /* copy the text up to where the match is */
21561 i = (int)(regmatch.startp[0] - tail);
21562 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
21563 /* add the substituted text */
21564 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
21565 + ga.ga_len + i, TRUE, TRUE, FALSE);
21566 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021567 /* avoid getting stuck on a match with an empty string */
21568 if (tail == regmatch.endp[0])
21569 {
21570 if (*tail == NUL)
21571 break;
21572 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
21573 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021574 }
21575 else
21576 {
21577 tail = regmatch.endp[0];
21578 if (*tail == NUL)
21579 break;
21580 }
21581 if (!do_all)
21582 break;
21583 }
21584
21585 if (ga.ga_data != NULL)
21586 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
21587
21588 vim_free(regmatch.regprog);
21589 }
21590
21591 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
21592 ga_clear(&ga);
21593 p_cpo = save_cpo;
21594
21595 return ret;
21596}
21597
21598#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */