blob: 5b8a1b6a7a0a857a19aaa963f7f988e9967beeec [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 */
169 proftime_T uf_tm_start; /* time at function call */
170 proftime_T uf_tm_children; /* time spent in children this call */
171 /* profiling the function per line */
172 int *uf_tml_count; /* nr of times line was executed */
173 proftime_T *uf_tml_total; /* time spend in a line + children */
174 proftime_T *uf_tml_self; /* time spend in a line itself */
175 proftime_T uf_tml_start; /* start time for current line */
176 proftime_T uf_tml_children; /* time spent in children for this line */
177 proftime_T uf_tml_wait; /* start wait time for current line */
178 int uf_tml_idx; /* index of line being timed; -1 if none */
179 int uf_tml_execed; /* line being timed was executed */
180#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000181 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000183 int uf_refcount; /* for numbered function: reference count */
184 char_u uf_name[1]; /* name of function (actually longer); can
185 start with <SNR>123_ (<SNR> is K_SPECIAL
186 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187};
188
189/* function flags */
190#define FC_ABORT 1 /* abort function on error */
191#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000192#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193
Bram Moolenaard9fba312005-06-26 22:34:35 +0000194#define DEL_REFCOUNT 999999 /* list/dict is being deleted */
195
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000197 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000199static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200
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 Moolenaar33570922005-01-25 22:26:29 +0000344};
345
346/* shorthand */
347#define vv_type vv_di.di_tv.v_type
348#define vv_nr vv_di.di_tv.vval.v_number
349#define vv_str vv_di.di_tv.vval.v_string
350#define vv_tv vv_di.di_tv
351
352/*
353 * The v: variables are stored in dictionary "vimvardict".
354 * "vimvars_var" is the variable that is used for the "l:" scope.
355 */
356static dict_T vimvardict;
357static dictitem_T vimvars_var;
358#define vimvarht vimvardict.dv_hashtab
359
Bram Moolenaara40058a2005-07-11 22:42:07 +0000360static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
361static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
362#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
363static int call_vim_function __ARGS((char_u *func, int argc, char_u **argv, int safe, typval_T *rettv));
364#endif
365static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
366static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
367static char_u *skip_var_one __ARGS((char_u *arg));
368static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty));
369static void list_glob_vars __ARGS((void));
370static void list_buf_vars __ARGS((void));
371static void list_win_vars __ARGS((void));
372static void list_vim_vars __ARGS((void));
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000373static void list_script_vars __ARGS((void));
374static void list_func_vars __ARGS((void));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000375static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg));
376static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
377static int check_changedtick __ARGS((char_u *arg));
378static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
379static void clear_lval __ARGS((lval_T *lp));
380static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
381static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
382static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
383static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
384static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
385static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
386static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
387static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
388static void item_lock __ARGS((typval_T *tv, int deep, int lock));
389static int tv_islocked __ARGS((typval_T *tv));
390
Bram Moolenaar33570922005-01-25 22:26:29 +0000391static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
392static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
393static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
394static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
395static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
396static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
397static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
398static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000399
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000400static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000401static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
402static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
403static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
404static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000405static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000406static listitem_T *listitem_alloc __ARGS((void));
407static void listitem_free __ARGS((listitem_T *item));
408static void listitem_remove __ARGS((list_T *l, listitem_T *item));
409static long list_len __ARGS((list_T *l));
410static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
411static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
412static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
Bram Moolenaar33570922005-01-25 22:26:29 +0000413static listitem_T *list_find __ARGS((list_T *l, long n));
Bram Moolenaara5525202006-03-02 22:52:09 +0000414static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000415static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000416static void list_append __ARGS((list_T *l, listitem_T *item));
417static int list_append_tv __ARGS((list_T *l, typval_T *tv));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000418static int list_append_string __ARGS((list_T *l, char_u *str, int len));
419static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000420static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
421static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
422static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000423static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000424static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000425static char_u *list2string __ARGS((typval_T *tv, int copyID));
426static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000427static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
428static void set_ref_in_list __ARGS((list_T *l, int copyID));
429static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000430static void dict_unref __ARGS((dict_T *d));
431static void dict_free __ARGS((dict_T *d));
432static dictitem_T *dictitem_alloc __ARGS((char_u *key));
433static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
434static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
435static void dictitem_free __ARGS((dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000436static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000437static int dict_add __ARGS((dict_T *d, dictitem_T *item));
438static long dict_len __ARGS((dict_T *d));
439static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000440static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000441static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000442static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
443static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000444static char_u *string_quote __ARGS((char_u *str, int function));
445static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
446static int find_internal_func __ARGS((char_u *name));
447static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
448static 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));
449static 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 Moolenaar81bf7082005-02-12 14:31:42 +0000450static void emsg_funcname __ARGS((char *msg, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000451
452static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
453static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
454static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
455static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
456static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
457static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
458static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
459static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
460static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
463static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
464static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
465static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
467static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000468static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000469static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
470static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000472#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000473static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000474static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
476#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000477static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
482static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
503static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000508static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000509static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000510static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000511static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000516static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000517static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000524static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000525static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000526static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
534static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000548static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000549static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000554static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000555static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000571static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000572static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000575#ifdef vim_mkdir
576static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
577#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000578static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000582static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000583static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000584static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000585static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000586static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000597static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000598static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000599static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000601static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000606static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000607static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000608static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000609static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000613static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000614static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000616static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
617#ifdef HAVE_STRFTIME
618static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
619#endif
620static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
624static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
625static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
626static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
627static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
628static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
629static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
630static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
631static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000632static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000633static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000634static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000635static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000636static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000637static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000638static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000639static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
640static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
641static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
642static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
643static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
644static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
645static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
646static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
647static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
648static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
649static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
650static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
651static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000652static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
653static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000654static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000655static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000656
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000657static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
658static pos_T *var2fpos __ARGS((typval_T *varp, int lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000659static int get_env_len __ARGS((char_u **arg));
660static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000661static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000662static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
663#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
664#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
665 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000666static 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 +0000667static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000668static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000669static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
670static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000671static typval_T *alloc_tv __ARGS((void));
672static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000673static void init_tv __ARGS((typval_T *varp));
674static long get_tv_number __ARGS((typval_T *varp));
675static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000676static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000677static char_u *get_tv_string __ARGS((typval_T *varp));
678static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000679static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000680static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000681static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000682static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
683static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
684static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
685static void list_one_var __ARGS((dictitem_T *v, char_u *prefix));
686static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
687static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
688static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000689static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000690static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000691static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000692static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
693static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
694static int eval_fname_script __ARGS((char_u *p));
695static int eval_fname_sid __ARGS((char_u *p));
696static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000697static ufunc_T *find_func __ARGS((char_u *name));
698static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000699static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000700#ifdef FEAT_PROFILE
701static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000702static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
703static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
704static int
705# ifdef __BORLANDC__
706 _RTLENTRYF
707# endif
708 prof_total_cmp __ARGS((const void *s1, const void *s2));
709static int
710# ifdef __BORLANDC__
711 _RTLENTRYF
712# endif
713 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000714#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000715static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000716static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000717static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000718static void func_free __ARGS((ufunc_T *fp));
719static void func_unref __ARGS((char_u *name));
720static void func_ref __ARGS((char_u *name));
721static 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));
722static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000723static win_T *find_win_by_nr __ARGS((typval_T *vp));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000724static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000725static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000726
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000727/* Character used as separated in autoload function/variable names. */
728#define AUTOLOAD_CHAR '#'
729
Bram Moolenaar33570922005-01-25 22:26:29 +0000730/*
731 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000732 */
733 void
734eval_init()
735{
Bram Moolenaar33570922005-01-25 22:26:29 +0000736 int i;
737 struct vimvar *p;
738
739 init_var_dict(&globvardict, &globvars_var);
740 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000741 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000742 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000743
744 for (i = 0; i < VV_LEN; ++i)
745 {
746 p = &vimvars[i];
747 STRCPY(p->vv_di.di_key, p->vv_name);
748 if (p->vv_flags & VV_RO)
749 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
750 else if (p->vv_flags & VV_RO_SBX)
751 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
752 else
753 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000754
755 /* add to v: scope dict, unless the value is not always available */
756 if (p->vv_type != VAR_UNKNOWN)
757 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000758 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000759 /* add to compat scope dict */
760 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000761 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000762}
763
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000764#if defined(EXITFREE) || defined(PROTO)
765 void
766eval_clear()
767{
768 int i;
769 struct vimvar *p;
770
771 for (i = 0; i < VV_LEN; ++i)
772 {
773 p = &vimvars[i];
774 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000775 {
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000776 vim_free(p->vv_di.di_tv.vval.v_string);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000777 p->vv_di.di_tv.vval.v_string = NULL;
778 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000779 }
780 hash_clear(&vimvarht);
781 hash_clear(&compat_hashtab);
782
783 /* script-local variables */
784 for (i = 1; i <= ga_scripts.ga_len; ++i)
785 vars_clear(&SCRIPT_VARS(i));
786 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000787 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000788
789 /* global variables */
790 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000791
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000792 /* functions */
Bram Moolenaard9fba312005-06-26 22:34:35 +0000793 free_all_functions();
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000794 hash_clear(&func_hashtab);
795
796 /* unreferenced lists and dicts */
797 (void)garbage_collect();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000798}
799#endif
800
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000801/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 * Return the name of the executed function.
803 */
804 char_u *
805func_name(cookie)
806 void *cookie;
807{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000808 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809}
810
811/*
812 * Return the address holding the next breakpoint line for a funccall cookie.
813 */
814 linenr_T *
815func_breakpoint(cookie)
816 void *cookie;
817{
Bram Moolenaar33570922005-01-25 22:26:29 +0000818 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819}
820
821/*
822 * Return the address holding the debug tick for a funccall cookie.
823 */
824 int *
825func_dbg_tick(cookie)
826 void *cookie;
827{
Bram Moolenaar33570922005-01-25 22:26:29 +0000828 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829}
830
831/*
832 * Return the nesting level for a funccall cookie.
833 */
834 int
835func_level(cookie)
836 void *cookie;
837{
Bram Moolenaar33570922005-01-25 22:26:29 +0000838 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839}
840
841/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000842funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843
844/*
845 * Return TRUE when a function was ended by a ":return" command.
846 */
847 int
848current_func_returned()
849{
850 return current_funccal->returned;
851}
852
853
854/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 * Set an internal variable to a string value. Creates the variable if it does
856 * not already exist.
857 */
858 void
859set_internal_string_var(name, value)
860 char_u *name;
861 char_u *value;
862{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000863 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000864 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865
866 val = vim_strsave(value);
867 if (val != NULL)
868 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000869 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000870 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000872 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000873 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 }
875 }
876}
877
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000878static lval_T *redir_lval = NULL;
879static char_u *redir_endp = NULL;
880static char_u *redir_varname = NULL;
881
882/*
883 * Start recording command output to a variable
884 * Returns OK if successfully completed the setup. FAIL otherwise.
885 */
886 int
887var_redir_start(name, append)
888 char_u *name;
889 int append; /* append to an existing variable */
890{
891 int save_emsg;
892 int err;
893 typval_T tv;
894
895 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000896 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000897 {
898 EMSG(_(e_invarg));
899 return FAIL;
900 }
901
902 redir_varname = vim_strsave(name);
903 if (redir_varname == NULL)
904 return FAIL;
905
906 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
907 if (redir_lval == NULL)
908 {
909 var_redir_stop();
910 return FAIL;
911 }
912
913 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000914 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
915 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000916 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
917 {
918 if (redir_endp != NULL && *redir_endp != NUL)
919 /* Trailing characters are present after the variable name */
920 EMSG(_(e_trailing));
921 else
922 EMSG(_(e_invarg));
923 var_redir_stop();
924 return FAIL;
925 }
926
927 /* check if we can write to the variable: set it to or append an empty
928 * string */
929 save_emsg = did_emsg;
930 did_emsg = FALSE;
931 tv.v_type = VAR_STRING;
932 tv.vval.v_string = (char_u *)"";
933 if (append)
934 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
935 else
936 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
937 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000938 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000939 if (err)
940 {
941 var_redir_stop();
942 return FAIL;
943 }
944 if (redir_lval->ll_newkey != NULL)
945 {
946 /* Dictionary item was created, don't do it again. */
947 vim_free(redir_lval->ll_newkey);
948 redir_lval->ll_newkey = NULL;
949 }
950
951 return OK;
952}
953
954/*
955 * Append "value[len]" to the variable set by var_redir_start().
956 */
957 void
958var_redir_str(value, len)
959 char_u *value;
960 int len;
961{
962 char_u *val;
963 typval_T tv;
964 int save_emsg;
965 int err;
966
967 if (redir_lval == NULL)
968 return;
969
970 if (len == -1)
971 /* Append the entire string */
972 val = vim_strsave(value);
973 else
974 /* Append only the specified number of characters */
975 val = vim_strnsave(value, len);
976 if (val == NULL)
977 return;
978
979 tv.v_type = VAR_STRING;
980 tv.vval.v_string = val;
981
982 save_emsg = did_emsg;
983 did_emsg = FALSE;
984 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
985 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000986 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000987 if (err)
988 var_redir_stop();
989
990 vim_free(tv.vval.v_string);
991}
992
993/*
994 * Stop redirecting command output to a variable.
995 */
996 void
997var_redir_stop()
998{
999 if (redir_lval != NULL)
1000 {
1001 clear_lval(redir_lval);
1002 vim_free(redir_lval);
1003 redir_lval = NULL;
1004 }
1005 vim_free(redir_varname);
1006 redir_varname = NULL;
1007}
1008
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009# if defined(FEAT_MBYTE) || defined(PROTO)
1010 int
1011eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1012 char_u *enc_from;
1013 char_u *enc_to;
1014 char_u *fname_from;
1015 char_u *fname_to;
1016{
1017 int err = FALSE;
1018
1019 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1020 set_vim_var_string(VV_CC_TO, enc_to, -1);
1021 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1022 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1023 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1024 err = TRUE;
1025 set_vim_var_string(VV_CC_FROM, NULL, -1);
1026 set_vim_var_string(VV_CC_TO, NULL, -1);
1027 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1028 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1029
1030 if (err)
1031 return FAIL;
1032 return OK;
1033}
1034# endif
1035
1036# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1037 int
1038eval_printexpr(fname, args)
1039 char_u *fname;
1040 char_u *args;
1041{
1042 int err = FALSE;
1043
1044 set_vim_var_string(VV_FNAME_IN, fname, -1);
1045 set_vim_var_string(VV_CMDARG, args, -1);
1046 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1047 err = TRUE;
1048 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1049 set_vim_var_string(VV_CMDARG, NULL, -1);
1050
1051 if (err)
1052 {
1053 mch_remove(fname);
1054 return FAIL;
1055 }
1056 return OK;
1057}
1058# endif
1059
1060# if defined(FEAT_DIFF) || defined(PROTO)
1061 void
1062eval_diff(origfile, newfile, outfile)
1063 char_u *origfile;
1064 char_u *newfile;
1065 char_u *outfile;
1066{
1067 int err = FALSE;
1068
1069 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1070 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1071 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1072 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1073 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1074 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1075 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1076}
1077
1078 void
1079eval_patch(origfile, difffile, outfile)
1080 char_u *origfile;
1081 char_u *difffile;
1082 char_u *outfile;
1083{
1084 int err;
1085
1086 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1087 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1088 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1089 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1090 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1091 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1092 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1093}
1094# endif
1095
1096/*
1097 * Top level evaluation function, returning a boolean.
1098 * Sets "error" to TRUE if there was an error.
1099 * Return TRUE or FALSE.
1100 */
1101 int
1102eval_to_bool(arg, error, nextcmd, skip)
1103 char_u *arg;
1104 int *error;
1105 char_u **nextcmd;
1106 int skip; /* only parse, don't execute */
1107{
Bram Moolenaar33570922005-01-25 22:26:29 +00001108 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 int retval = FALSE;
1110
1111 if (skip)
1112 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001113 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 else
1116 {
1117 *error = FALSE;
1118 if (!skip)
1119 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001120 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001121 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 }
1123 }
1124 if (skip)
1125 --emsg_skip;
1126
1127 return retval;
1128}
1129
1130/*
1131 * Top level evaluation function, returning a string. If "skip" is TRUE,
1132 * only parsing to "nextcmd" is done, without reporting errors. Return
1133 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1134 */
1135 char_u *
1136eval_to_string_skip(arg, nextcmd, skip)
1137 char_u *arg;
1138 char_u **nextcmd;
1139 int skip; /* only parse, don't execute */
1140{
Bram Moolenaar33570922005-01-25 22:26:29 +00001141 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 char_u *retval;
1143
1144 if (skip)
1145 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001146 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 retval = NULL;
1148 else
1149 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001150 retval = vim_strsave(get_tv_string(&tv));
1151 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 }
1153 if (skip)
1154 --emsg_skip;
1155
1156 return retval;
1157}
1158
1159/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001160 * Skip over an expression at "*pp".
1161 * Return FAIL for an error, OK otherwise.
1162 */
1163 int
1164skip_expr(pp)
1165 char_u **pp;
1166{
Bram Moolenaar33570922005-01-25 22:26:29 +00001167 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001168
1169 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001170 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001171}
1172
1173/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 * Top level evaluation function, returning a string.
1175 * Return pointer to allocated memory, or NULL for failure.
1176 */
1177 char_u *
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001178eval_to_string(arg, nextcmd, dolist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 char_u *arg;
1180 char_u **nextcmd;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001181 int dolist; /* turn List into sequence of lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182{
Bram Moolenaar33570922005-01-25 22:26:29 +00001183 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001185 garray_T ga;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001187 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 retval = NULL;
1189 else
1190 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001191 if (dolist && tv.v_type == VAR_LIST)
1192 {
1193 ga_init2(&ga, (int)sizeof(char), 80);
1194 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
1195 ga_append(&ga, NUL);
1196 retval = (char_u *)ga.ga_data;
1197 }
1198 else
1199 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001200 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 }
1202
1203 return retval;
1204}
1205
1206/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001207 * Call eval_to_string() without using current local variables and using
1208 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 */
1210 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001211eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 char_u *arg;
1213 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001214 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215{
1216 char_u *retval;
1217 void *save_funccalp;
1218
1219 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001220 if (use_sandbox)
1221 ++sandbox;
1222 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001223 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001224 if (use_sandbox)
1225 --sandbox;
1226 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 restore_funccal(save_funccalp);
1228 return retval;
1229}
1230
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231/*
1232 * Top level evaluation function, returning a number.
1233 * Evaluates "expr" silently.
1234 * Returns -1 for an error.
1235 */
1236 int
1237eval_to_number(expr)
1238 char_u *expr;
1239{
Bram Moolenaar33570922005-01-25 22:26:29 +00001240 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001242 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243
1244 ++emsg_off;
1245
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001246 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 retval = -1;
1248 else
1249 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001250 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001251 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 }
1253 --emsg_off;
1254
1255 return retval;
1256}
1257
Bram Moolenaara40058a2005-07-11 22:42:07 +00001258/*
1259 * Prepare v: variable "idx" to be used.
1260 * Save the current typeval in "save_tv".
1261 * When not used yet add the variable to the v: hashtable.
1262 */
1263 static void
1264prepare_vimvar(idx, save_tv)
1265 int idx;
1266 typval_T *save_tv;
1267{
1268 *save_tv = vimvars[idx].vv_tv;
1269 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1270 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1271}
1272
1273/*
1274 * Restore v: variable "idx" to typeval "save_tv".
1275 * When no longer defined, remove the variable from the v: hashtable.
1276 */
1277 static void
1278restore_vimvar(idx, save_tv)
1279 int idx;
1280 typval_T *save_tv;
1281{
1282 hashitem_T *hi;
1283
1284 clear_tv(&vimvars[idx].vv_tv);
1285 vimvars[idx].vv_tv = *save_tv;
1286 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1287 {
1288 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1289 if (HASHITEM_EMPTY(hi))
1290 EMSG2(_(e_intern2), "restore_vimvar()");
1291 else
1292 hash_remove(&vimvarht, hi);
1293 }
1294}
1295
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001296#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001297/*
1298 * Evaluate an expression to a list with suggestions.
1299 * For the "expr:" part of 'spellsuggest'.
1300 */
1301 list_T *
1302eval_spell_expr(badword, expr)
1303 char_u *badword;
1304 char_u *expr;
1305{
1306 typval_T save_val;
1307 typval_T rettv;
1308 list_T *list = NULL;
1309 char_u *p = skipwhite(expr);
1310
1311 /* Set "v:val" to the bad word. */
1312 prepare_vimvar(VV_VAL, &save_val);
1313 vimvars[VV_VAL].vv_type = VAR_STRING;
1314 vimvars[VV_VAL].vv_str = badword;
1315 if (p_verbose == 0)
1316 ++emsg_off;
1317
1318 if (eval1(&p, &rettv, TRUE) == OK)
1319 {
1320 if (rettv.v_type != VAR_LIST)
1321 clear_tv(&rettv);
1322 else
1323 list = rettv.vval.v_list;
1324 }
1325
1326 if (p_verbose == 0)
1327 --emsg_off;
1328 vimvars[VV_VAL].vv_str = NULL;
1329 restore_vimvar(VV_VAL, &save_val);
1330
1331 return list;
1332}
1333
1334/*
1335 * "list" is supposed to contain two items: a word and a number. Return the
1336 * word in "pp" and the number as the return value.
1337 * Return -1 if anything isn't right.
1338 * Used to get the good word and score from the eval_spell_expr() result.
1339 */
1340 int
1341get_spellword(list, pp)
1342 list_T *list;
1343 char_u **pp;
1344{
1345 listitem_T *li;
1346
1347 li = list->lv_first;
1348 if (li == NULL)
1349 return -1;
1350 *pp = get_tv_string(&li->li_tv);
1351
1352 li = li->li_next;
1353 if (li == NULL)
1354 return -1;
1355 return get_tv_number(&li->li_tv);
1356}
1357#endif
1358
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001359/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001360 * Top level evaluation function.
1361 * Returns an allocated typval_T with the result.
1362 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001363 */
1364 typval_T *
1365eval_expr(arg, nextcmd)
1366 char_u *arg;
1367 char_u **nextcmd;
1368{
1369 typval_T *tv;
1370
1371 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001372 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001373 {
1374 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001375 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001376 }
1377
1378 return tv;
1379}
1380
1381
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1383/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001384 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 * Uses argv[argc] for the function arguments.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001386 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001388 static int
1389call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 char_u *func;
1391 int argc;
1392 char_u **argv;
1393 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001394 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395{
Bram Moolenaar33570922005-01-25 22:26:29 +00001396 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 long n;
1398 int len;
1399 int i;
1400 int doesrange;
1401 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001402 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403
Bram Moolenaar33570922005-01-25 22:26:29 +00001404 argvars = (typval_T *)alloc((unsigned)(argc * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001406 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407
1408 for (i = 0; i < argc; i++)
1409 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001410 /* Pass a NULL or empty argument as an empty string */
1411 if (argv[i] == NULL || *argv[i] == NUL)
1412 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001413 argvars[i].v_type = VAR_STRING;
1414 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001415 continue;
1416 }
1417
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 /* Recognize a number argument, the others must be strings. */
1419 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1420 if (len != 0 && len == (int)STRLEN(argv[i]))
1421 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001422 argvars[i].v_type = VAR_NUMBER;
1423 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 }
1425 else
1426 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001427 argvars[i].v_type = VAR_STRING;
1428 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 }
1430 }
1431
1432 if (safe)
1433 {
1434 save_funccalp = save_funccal();
1435 ++sandbox;
1436 }
1437
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001438 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1439 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001441 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 if (safe)
1443 {
1444 --sandbox;
1445 restore_funccal(save_funccalp);
1446 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001447 vim_free(argvars);
1448
1449 if (ret == FAIL)
1450 clear_tv(rettv);
1451
1452 return ret;
1453}
1454
1455/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001456 * Call vimL function "func" and return the result as a string.
1457 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001458 * Uses argv[argc] for the function arguments.
1459 */
1460 void *
1461call_func_retstr(func, argc, argv, safe)
1462 char_u *func;
1463 int argc;
1464 char_u **argv;
1465 int safe; /* use the sandbox */
1466{
1467 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001468 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001469
1470 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1471 return NULL;
1472
1473 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001474 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 return retval;
1476}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001477
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001478#if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001479/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001480 * Call vimL function "func" and return the result as a number.
1481 * Returns -1 when calling the function fails.
1482 * Uses argv[argc] for the function arguments.
1483 */
1484 long
1485call_func_retnr(func, argc, argv, safe)
1486 char_u *func;
1487 int argc;
1488 char_u **argv;
1489 int safe; /* use the sandbox */
1490{
1491 typval_T rettv;
1492 long retval;
1493
1494 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1495 return -1;
1496
1497 retval = get_tv_number_chk(&rettv, NULL);
1498 clear_tv(&rettv);
1499 return retval;
1500}
1501#endif
1502
1503/*
1504 * Call vimL function "func" and return the result as a list
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001505 * Uses argv[argc] for the function arguments.
1506 */
1507 void *
1508call_func_retlist(func, argc, argv, safe)
1509 char_u *func;
1510 int argc;
1511 char_u **argv;
1512 int safe; /* use the sandbox */
1513{
1514 typval_T rettv;
1515
1516 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1517 return NULL;
1518
1519 if (rettv.v_type != VAR_LIST)
1520 {
1521 clear_tv(&rettv);
1522 return NULL;
1523 }
1524
1525 return rettv.vval.v_list;
1526}
1527
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528#endif
1529
1530/*
1531 * Save the current function call pointer, and set it to NULL.
1532 * Used when executing autocommands and for ":source".
1533 */
1534 void *
1535save_funccal()
1536{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001537 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 current_funccal = NULL;
1540 return (void *)fc;
1541}
1542
1543 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001544restore_funccal(vfc)
1545 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001547 funccall_T *fc = (funccall_T *)vfc;
1548
1549 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550}
1551
Bram Moolenaar05159a02005-02-26 23:04:13 +00001552#if defined(FEAT_PROFILE) || defined(PROTO)
1553/*
1554 * Prepare profiling for entering a child or something else that is not
1555 * counted for the script/function itself.
1556 * Should always be called in pair with prof_child_exit().
1557 */
1558 void
1559prof_child_enter(tm)
1560 proftime_T *tm; /* place to store waittime */
1561{
1562 funccall_T *fc = current_funccal;
1563
1564 if (fc != NULL && fc->func->uf_profiling)
1565 profile_start(&fc->prof_child);
1566 script_prof_save(tm);
1567}
1568
1569/*
1570 * Take care of time spent in a child.
1571 * Should always be called after prof_child_enter().
1572 */
1573 void
1574prof_child_exit(tm)
1575 proftime_T *tm; /* where waittime was stored */
1576{
1577 funccall_T *fc = current_funccal;
1578
1579 if (fc != NULL && fc->func->uf_profiling)
1580 {
1581 profile_end(&fc->prof_child);
1582 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1583 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1584 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1585 }
1586 script_prof_restore(tm);
1587}
1588#endif
1589
1590
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591#ifdef FEAT_FOLDING
1592/*
1593 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1594 * it in "*cp". Doesn't give error messages.
1595 */
1596 int
1597eval_foldexpr(arg, cp)
1598 char_u *arg;
1599 int *cp;
1600{
Bram Moolenaar33570922005-01-25 22:26:29 +00001601 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 int retval;
1603 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001604 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1605 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606
1607 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001608 if (use_sandbox)
1609 ++sandbox;
1610 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001612 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 retval = 0;
1614 else
1615 {
1616 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001617 if (tv.v_type == VAR_NUMBER)
1618 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001619 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 retval = 0;
1621 else
1622 {
1623 /* If the result is a string, check if there is a non-digit before
1624 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001625 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 if (!VIM_ISDIGIT(*s) && *s != '-')
1627 *cp = *s++;
1628 retval = atol((char *)s);
1629 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001630 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 }
1632 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001633 if (use_sandbox)
1634 --sandbox;
1635 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636
1637 return retval;
1638}
1639#endif
1640
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001642 * ":let" list all variable values
1643 * ":let var1 var2" list variable values
1644 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001645 * ":let var += expr" assignment command.
1646 * ":let var -= expr" assignment command.
1647 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001648 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 */
1650 void
1651ex_let(eap)
1652 exarg_T *eap;
1653{
1654 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001655 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001656 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001658 int var_count = 0;
1659 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001660 char_u op[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001662 expr = skip_var_list(arg, &var_count, &semicolon);
1663 if (expr == NULL)
1664 return;
1665 expr = vim_strchr(expr, '=');
1666 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001668 /*
1669 * ":let" without "=": list variables
1670 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001671 if (*arg == '[')
1672 EMSG(_(e_invarg));
1673 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001674 /* ":let var1 var2" */
1675 arg = list_arg_vars(eap, arg);
1676 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001677 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001678 /* ":let" */
Bram Moolenaara7043832005-01-21 11:56:39 +00001679 list_glob_vars();
1680 list_buf_vars();
1681 list_win_vars();
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001682 list_script_vars();
1683 list_func_vars();
Bram Moolenaara7043832005-01-21 11:56:39 +00001684 list_vim_vars();
1685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 eap->nextcmd = check_nextcmd(arg);
1687 }
1688 else
1689 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001690 op[0] = '=';
1691 op[1] = NUL;
1692 if (expr > arg)
1693 {
1694 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1695 op[0] = expr[-1]; /* +=, -= or .= */
1696 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001697 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001698
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 if (eap->skip)
1700 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001701 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 if (eap->skip)
1703 {
1704 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001705 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 --emsg_skip;
1707 }
1708 else if (i != FAIL)
1709 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001710 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001711 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001712 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 }
1714 }
1715}
1716
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001717/*
1718 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1719 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001720 * When "nextchars" is not NULL it points to a string with characters that
1721 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1722 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001723 * Returns OK or FAIL;
1724 */
1725 static int
1726ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1727 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001728 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001729 int copy; /* copy values from "tv", don't move */
1730 int semicolon; /* from skip_var_list() */
1731 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001732 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001733{
1734 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001735 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001736 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001737 listitem_T *item;
1738 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001739
1740 if (*arg != '[')
1741 {
1742 /*
1743 * ":let var = expr" or ":for var in list"
1744 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001745 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001746 return FAIL;
1747 return OK;
1748 }
1749
1750 /*
1751 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1752 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001753 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001754 {
1755 EMSG(_(e_listreq));
1756 return FAIL;
1757 }
1758
1759 i = list_len(l);
1760 if (semicolon == 0 && var_count < i)
1761 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001762 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001763 return FAIL;
1764 }
1765 if (var_count - semicolon > i)
1766 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001767 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001768 return FAIL;
1769 }
1770
1771 item = l->lv_first;
1772 while (*arg != ']')
1773 {
1774 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001775 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001776 item = item->li_next;
1777 if (arg == NULL)
1778 return FAIL;
1779
1780 arg = skipwhite(arg);
1781 if (*arg == ';')
1782 {
1783 /* Put the rest of the list (may be empty) in the var after ';'.
1784 * Create a new list for this. */
1785 l = list_alloc();
1786 if (l == NULL)
1787 return FAIL;
1788 while (item != NULL)
1789 {
1790 list_append_tv(l, &item->li_tv);
1791 item = item->li_next;
1792 }
1793
1794 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001795 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001796 ltv.vval.v_list = l;
1797 l->lv_refcount = 1;
1798
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001799 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1800 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001801 clear_tv(&ltv);
1802 if (arg == NULL)
1803 return FAIL;
1804 break;
1805 }
1806 else if (*arg != ',' && *arg != ']')
1807 {
1808 EMSG2(_(e_intern2), "ex_let_vars()");
1809 return FAIL;
1810 }
1811 }
1812
1813 return OK;
1814}
1815
1816/*
1817 * Skip over assignable variable "var" or list of variables "[var, var]".
1818 * Used for ":let varvar = expr" and ":for varvar in expr".
1819 * For "[var, var]" increment "*var_count" for each variable.
1820 * for "[var, var; var]" set "semicolon".
1821 * Return NULL for an error.
1822 */
1823 static char_u *
1824skip_var_list(arg, var_count, semicolon)
1825 char_u *arg;
1826 int *var_count;
1827 int *semicolon;
1828{
1829 char_u *p, *s;
1830
1831 if (*arg == '[')
1832 {
1833 /* "[var, var]": find the matching ']'. */
1834 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001835 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001836 {
1837 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1838 s = skip_var_one(p);
1839 if (s == p)
1840 {
1841 EMSG2(_(e_invarg2), p);
1842 return NULL;
1843 }
1844 ++*var_count;
1845
1846 p = skipwhite(s);
1847 if (*p == ']')
1848 break;
1849 else if (*p == ';')
1850 {
1851 if (*semicolon == 1)
1852 {
1853 EMSG(_("Double ; in list of variables"));
1854 return NULL;
1855 }
1856 *semicolon = 1;
1857 }
1858 else if (*p != ',')
1859 {
1860 EMSG2(_(e_invarg2), p);
1861 return NULL;
1862 }
1863 }
1864 return p + 1;
1865 }
1866 else
1867 return skip_var_one(arg);
1868}
1869
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001870/*
Bram Moolenaar92124a32005-06-17 22:03:40 +00001871 * Skip one (assignable) variable name, includig @r, $VAR, &option, d.key,
1872 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001873 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001874 static char_u *
1875skip_var_one(arg)
1876 char_u *arg;
1877{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001878 if (*arg == '@' && arg[1] != NUL)
1879 return arg + 2;
1880 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1881 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001882}
1883
Bram Moolenaara7043832005-01-21 11:56:39 +00001884/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001885 * List variables for hashtab "ht" with prefix "prefix".
1886 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001887 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001888 static void
Bram Moolenaar33570922005-01-25 22:26:29 +00001889list_hashtable_vars(ht, prefix, empty)
1890 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001891 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001892 int empty;
Bram Moolenaara7043832005-01-21 11:56:39 +00001893{
Bram Moolenaar33570922005-01-25 22:26:29 +00001894 hashitem_T *hi;
1895 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001896 int todo;
1897
1898 todo = ht->ht_used;
1899 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1900 {
1901 if (!HASHITEM_EMPTY(hi))
1902 {
1903 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001904 di = HI2DI(hi);
1905 if (empty || di->di_tv.v_type != VAR_STRING
1906 || di->di_tv.vval.v_string != NULL)
1907 list_one_var(di, prefix);
Bram Moolenaara7043832005-01-21 11:56:39 +00001908 }
1909 }
1910}
1911
1912/*
1913 * List global variables.
1914 */
1915 static void
1916list_glob_vars()
1917{
Bram Moolenaar33570922005-01-25 22:26:29 +00001918 list_hashtable_vars(&globvarht, (char_u *)"", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001919}
1920
1921/*
1922 * List buffer variables.
1923 */
1924 static void
1925list_buf_vars()
1926{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001927 char_u numbuf[NUMBUFLEN];
1928
Bram Moolenaar33570922005-01-25 22:26:29 +00001929 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001930
1931 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
1932 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER, numbuf);
Bram Moolenaara7043832005-01-21 11:56:39 +00001933}
1934
1935/*
1936 * List window variables.
1937 */
1938 static void
1939list_win_vars()
1940{
Bram Moolenaar33570922005-01-25 22:26:29 +00001941 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001942}
1943
1944/*
1945 * List Vim variables.
1946 */
1947 static void
1948list_vim_vars()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001949{
Bram Moolenaar33570922005-01-25 22:26:29 +00001950 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001951}
1952
1953/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001954 * List script-local variables, if there is a script.
1955 */
1956 static void
1957list_script_vars()
1958{
1959 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
1960 list_hashtable_vars(&SCRIPT_VARS(current_SID), (char_u *)"s:", FALSE);
1961}
1962
1963/*
1964 * List function variables, if there is a function.
1965 */
1966 static void
1967list_func_vars()
1968{
1969 if (current_funccal != NULL)
1970 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
1971 (char_u *)"l:", FALSE);
1972}
1973
1974/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001975 * List variables in "arg".
1976 */
1977 static char_u *
1978list_arg_vars(eap, arg)
1979 exarg_T *eap;
1980 char_u *arg;
1981{
1982 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001983 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001984 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001985 char_u *name_start;
1986 char_u *arg_subsc;
1987 char_u *tofree;
1988 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001989
1990 while (!ends_excmd(*arg) && !got_int)
1991 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001992 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001993 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001994 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001995 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
1996 {
1997 emsg_severe = TRUE;
1998 EMSG(_(e_trailing));
1999 break;
2000 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002001 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002002 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002003 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002004 /* get_name_len() takes care of expanding curly braces */
2005 name_start = name = arg;
2006 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2007 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002008 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002009 /* This is mainly to keep test 49 working: when expanding
2010 * curly braces fails overrule the exception error message. */
2011 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002012 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002013 emsg_severe = TRUE;
2014 EMSG2(_(e_invarg2), arg);
2015 break;
2016 }
2017 error = TRUE;
2018 }
2019 else
2020 {
2021 if (tofree != NULL)
2022 name = tofree;
2023 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002024 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002025 else
2026 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002027 /* handle d.key, l[idx], f(expr) */
2028 arg_subsc = arg;
2029 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002030 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002031 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002032 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002033 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002034 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002035 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002036 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002037 case 'g': list_glob_vars(); break;
2038 case 'b': list_buf_vars(); break;
2039 case 'w': list_win_vars(); break;
2040 case 'v': list_vim_vars(); break;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002041 case 's': list_script_vars(); break;
2042 case 'l': list_func_vars(); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002043 default:
2044 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002045 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002046 }
2047 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002048 {
2049 char_u numbuf[NUMBUFLEN];
2050 char_u *tf;
2051 int c;
2052 char_u *s;
2053
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002054 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002055 c = *arg;
2056 *arg = NUL;
2057 list_one_var_a((char_u *)"",
2058 arg == arg_subsc ? name : name_start,
2059 tv.v_type, s == NULL ? (char_u *)"" : s);
2060 *arg = c;
2061 vim_free(tf);
2062 }
2063 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002064 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002065 }
2066 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002067
2068 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002069 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002070
2071 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002072 }
2073
2074 return arg;
2075}
2076
2077/*
2078 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2079 * Returns a pointer to the char just after the var name.
2080 * Returns NULL if there is an error.
2081 */
2082 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002083ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002084 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002085 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002086 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002087 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002088 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002089{
2090 int c1;
2091 char_u *name;
2092 char_u *p;
2093 char_u *arg_end = NULL;
2094 int len;
2095 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002096 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002097
2098 /*
2099 * ":let $VAR = expr": Set environment variable.
2100 */
2101 if (*arg == '$')
2102 {
2103 /* Find the end of the name. */
2104 ++arg;
2105 name = arg;
2106 len = get_env_len(&arg);
2107 if (len == 0)
2108 EMSG2(_(e_invarg2), name - 1);
2109 else
2110 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002111 if (op != NULL && (*op == '+' || *op == '-'))
2112 EMSG2(_(e_letwrong), op);
2113 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002114 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002115 EMSG(_(e_letunexp));
2116 else
2117 {
2118 c1 = name[len];
2119 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002120 p = get_tv_string_chk(tv);
2121 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002122 {
2123 int mustfree = FALSE;
2124 char_u *s = vim_getenv(name, &mustfree);
2125
2126 if (s != NULL)
2127 {
2128 p = tofree = concat_str(s, p);
2129 if (mustfree)
2130 vim_free(s);
2131 }
2132 }
2133 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002134 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002135 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002136 if (STRICMP(name, "HOME") == 0)
2137 init_homedir();
2138 else if (didset_vim && STRICMP(name, "VIM") == 0)
2139 didset_vim = FALSE;
2140 else if (didset_vimruntime
2141 && STRICMP(name, "VIMRUNTIME") == 0)
2142 didset_vimruntime = FALSE;
2143 arg_end = arg;
2144 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002145 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002146 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002147 }
2148 }
2149 }
2150
2151 /*
2152 * ":let &option = expr": Set option value.
2153 * ":let &l:option = expr": Set local option value.
2154 * ":let &g:option = expr": Set global option value.
2155 */
2156 else if (*arg == '&')
2157 {
2158 /* Find the end of the name. */
2159 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002160 if (p == NULL || (endchars != NULL
2161 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002162 EMSG(_(e_letunexp));
2163 else
2164 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002165 long n;
2166 int opt_type;
2167 long numval;
2168 char_u *stringval = NULL;
2169 char_u *s;
2170
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002171 c1 = *p;
2172 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002173
2174 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002175 s = get_tv_string_chk(tv); /* != NULL if number or string */
2176 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002177 {
2178 opt_type = get_option_value(arg, &numval,
2179 &stringval, opt_flags);
2180 if ((opt_type == 1 && *op == '.')
2181 || (opt_type == 0 && *op != '.'))
2182 EMSG2(_(e_letwrong), op);
2183 else
2184 {
2185 if (opt_type == 1) /* number */
2186 {
2187 if (*op == '+')
2188 n = numval + n;
2189 else
2190 n = numval - n;
2191 }
2192 else if (opt_type == 0 && stringval != NULL) /* string */
2193 {
2194 s = concat_str(stringval, s);
2195 vim_free(stringval);
2196 stringval = s;
2197 }
2198 }
2199 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002200 if (s != NULL)
2201 {
2202 set_option_value(arg, n, s, opt_flags);
2203 arg_end = p;
2204 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002205 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002206 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002207 }
2208 }
2209
2210 /*
2211 * ":let @r = expr": Set register contents.
2212 */
2213 else if (*arg == '@')
2214 {
2215 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002216 if (op != NULL && (*op == '+' || *op == '-'))
2217 EMSG2(_(e_letwrong), op);
2218 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002219 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002220 EMSG(_(e_letunexp));
2221 else
2222 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002223 char_u *tofree = NULL;
2224 char_u *s;
2225
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002226 p = get_tv_string_chk(tv);
2227 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002228 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002229 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002230 if (s != NULL)
2231 {
2232 p = tofree = concat_str(s, p);
2233 vim_free(s);
2234 }
2235 }
2236 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002237 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002238 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002239 arg_end = arg + 1;
2240 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002241 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002242 }
2243 }
2244
2245 /*
2246 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002247 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002248 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002249 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002250 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002251 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002252
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002253 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002254 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002255 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002256 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2257 EMSG(_(e_letunexp));
2258 else
2259 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002260 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002261 arg_end = p;
2262 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002263 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002264 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002265 }
2266
2267 else
2268 EMSG2(_(e_invarg2), arg);
2269
2270 return arg_end;
2271}
2272
2273/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002274 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2275 */
2276 static int
2277check_changedtick(arg)
2278 char_u *arg;
2279{
2280 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2281 {
2282 EMSG2(_(e_readonlyvar), arg);
2283 return TRUE;
2284 }
2285 return FALSE;
2286}
2287
2288/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002289 * Get an lval: variable, Dict item or List item that can be assigned a value
2290 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2291 * "name.key", "name.key[expr]" etc.
2292 * Indexing only works if "name" is an existing List or Dictionary.
2293 * "name" points to the start of the name.
2294 * If "rettv" is not NULL it points to the value to be assigned.
2295 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2296 * wrong; must end in space or cmd separator.
2297 *
2298 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002299 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002300 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002301 */
2302 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002303get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002304 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002305 typval_T *rettv;
2306 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002307 int unlet;
2308 int skip;
2309 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002310 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002311{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002312 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002313 char_u *expr_start, *expr_end;
2314 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002315 dictitem_T *v;
2316 typval_T var1;
2317 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002318 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002319 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002320 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002321 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002322 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002323
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002324 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002325 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002326
2327 if (skip)
2328 {
2329 /* When skipping just find the end of the name. */
2330 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002331 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002332 }
2333
2334 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002335 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002336 if (expr_start != NULL)
2337 {
2338 /* Don't expand the name when we already know there is an error. */
2339 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2340 && *p != '[' && *p != '.')
2341 {
2342 EMSG(_(e_trailing));
2343 return NULL;
2344 }
2345
2346 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2347 if (lp->ll_exp_name == NULL)
2348 {
2349 /* Report an invalid expression in braces, unless the
2350 * expression evaluation has been cancelled due to an
2351 * aborting error, an interrupt, or an exception. */
2352 if (!aborting() && !quiet)
2353 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002354 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002355 EMSG2(_(e_invarg2), name);
2356 return NULL;
2357 }
2358 }
2359 lp->ll_name = lp->ll_exp_name;
2360 }
2361 else
2362 lp->ll_name = name;
2363
2364 /* Without [idx] or .key we are done. */
2365 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2366 return p;
2367
2368 cc = *p;
2369 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002370 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002371 if (v == NULL && !quiet)
2372 EMSG2(_(e_undefvar), lp->ll_name);
2373 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002374 if (v == NULL)
2375 return NULL;
2376
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002377 /*
2378 * Loop until no more [idx] or .key is following.
2379 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002380 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002381 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002382 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002383 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2384 && !(lp->ll_tv->v_type == VAR_DICT
2385 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002386 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002387 if (!quiet)
2388 EMSG(_("E689: Can only index a List or Dictionary"));
2389 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002390 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002391 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002392 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002393 if (!quiet)
2394 EMSG(_("E708: [:] must come last"));
2395 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002396 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002397
Bram Moolenaar8c711452005-01-14 21:53:12 +00002398 len = -1;
2399 if (*p == '.')
2400 {
2401 key = p + 1;
2402 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2403 ;
2404 if (len == 0)
2405 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002406 if (!quiet)
2407 EMSG(_(e_emptykey));
2408 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002409 }
2410 p = key + len;
2411 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002412 else
2413 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002414 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002415 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002416 if (*p == ':')
2417 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002418 else
2419 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002420 empty1 = FALSE;
2421 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002422 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002423 if (get_tv_string_chk(&var1) == NULL)
2424 {
2425 /* not a number or string */
2426 clear_tv(&var1);
2427 return NULL;
2428 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002429 }
2430
2431 /* Optionally get the second index [ :expr]. */
2432 if (*p == ':')
2433 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002434 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002435 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002436 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002437 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002438 if (!empty1)
2439 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002440 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002441 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002442 if (rettv != NULL && (rettv->v_type != VAR_LIST
2443 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002444 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002445 if (!quiet)
2446 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002447 if (!empty1)
2448 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002449 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002450 }
2451 p = skipwhite(p + 1);
2452 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002453 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002454 else
2455 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002456 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002457 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2458 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002459 if (!empty1)
2460 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002461 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002462 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002463 if (get_tv_string_chk(&var2) == NULL)
2464 {
2465 /* not a number or string */
2466 if (!empty1)
2467 clear_tv(&var1);
2468 clear_tv(&var2);
2469 return NULL;
2470 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002471 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002472 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002473 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002474 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002475 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002476
Bram Moolenaar8c711452005-01-14 21:53:12 +00002477 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002478 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002479 if (!quiet)
2480 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002481 if (!empty1)
2482 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002483 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002484 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002485 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002486 }
2487
2488 /* Skip to past ']'. */
2489 ++p;
2490 }
2491
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002492 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002493 {
2494 if (len == -1)
2495 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002496 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002497 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002498 if (*key == NUL)
2499 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002500 if (!quiet)
2501 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002502 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002503 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002504 }
2505 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002506 lp->ll_list = NULL;
2507 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002508 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002509 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002510 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002511 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002512 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002513 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002514 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002515 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002516 if (len == -1)
2517 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002518 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002519 }
2520 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002521 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002522 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002523 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002524 if (len == -1)
2525 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002526 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002527 p = NULL;
2528 break;
2529 }
2530 if (len == -1)
2531 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002532 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002533 }
2534 else
2535 {
2536 /*
2537 * Get the number and item for the only or first index of the List.
2538 */
2539 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002540 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002541 else
2542 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002543 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002544 clear_tv(&var1);
2545 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002546 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002547 lp->ll_list = lp->ll_tv->vval.v_list;
2548 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2549 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002550 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002551 if (!quiet)
2552 EMSGN(_(e_listidx), lp->ll_n1);
2553 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002554 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002555 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002556 }
2557
2558 /*
2559 * May need to find the item or absolute index for the second
2560 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002561 * When no index given: "lp->ll_empty2" is TRUE.
2562 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002563 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002564 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002565 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002566 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002567 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002568 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002569 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002570 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002571 if (ni == NULL)
2572 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002573 if (!quiet)
2574 EMSGN(_(e_listidx), lp->ll_n2);
2575 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002576 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002577 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002578 }
2579
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002580 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2581 if (lp->ll_n1 < 0)
2582 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2583 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002584 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002585 if (!quiet)
2586 EMSGN(_(e_listidx), lp->ll_n2);
2587 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002588 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002589 }
2590
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002591 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002592 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002593 }
2594
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002595 return p;
2596}
2597
2598/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002599 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002600 */
2601 static void
2602clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002603 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002604{
2605 vim_free(lp->ll_exp_name);
2606 vim_free(lp->ll_newkey);
2607}
2608
2609/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002610 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002611 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002612 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002613 */
2614 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002615set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002616 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002617 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002618 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002619 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002620 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002621{
2622 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002623 listitem_T *ri;
2624 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002625
2626 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002627 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002628 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002629 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002630 cc = *endp;
2631 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002632 if (op != NULL && *op != '=')
2633 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002634 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002635
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002636 /* handle +=, -= and .= */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002637 if (get_var_tv(lp->ll_name, STRLEN(lp->ll_name),
2638 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002639 {
2640 if (tv_op(&tv, rettv, op) == OK)
2641 set_var(lp->ll_name, &tv, FALSE);
2642 clear_tv(&tv);
2643 }
2644 }
2645 else
2646 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002647 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002648 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002649 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002650 else if (tv_check_lock(lp->ll_newkey == NULL
2651 ? lp->ll_tv->v_lock
2652 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2653 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002654 else if (lp->ll_range)
2655 {
2656 /*
2657 * Assign the List values to the list items.
2658 */
2659 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002660 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002661 if (op != NULL && *op != '=')
2662 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2663 else
2664 {
2665 clear_tv(&lp->ll_li->li_tv);
2666 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2667 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002668 ri = ri->li_next;
2669 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2670 break;
2671 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002672 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002673 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002674 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002675 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002676 ri = NULL;
2677 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002678 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002679 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002680 lp->ll_li = lp->ll_li->li_next;
2681 ++lp->ll_n1;
2682 }
2683 if (ri != NULL)
2684 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002685 else if (lp->ll_empty2
2686 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002687 : lp->ll_n1 != lp->ll_n2)
2688 EMSG(_("E711: List value has not enough items"));
2689 }
2690 else
2691 {
2692 /*
2693 * Assign to a List or Dictionary item.
2694 */
2695 if (lp->ll_newkey != NULL)
2696 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002697 if (op != NULL && *op != '=')
2698 {
2699 EMSG2(_(e_letwrong), op);
2700 return;
2701 }
2702
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002703 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002704 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002705 if (di == NULL)
2706 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002707 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2708 {
2709 vim_free(di);
2710 return;
2711 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002712 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002713 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002714 else if (op != NULL && *op != '=')
2715 {
2716 tv_op(lp->ll_tv, rettv, op);
2717 return;
2718 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002719 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002720 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002721
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002722 /*
2723 * Assign the value to the variable or list item.
2724 */
2725 if (copy)
2726 copy_tv(rettv, lp->ll_tv);
2727 else
2728 {
2729 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002730 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002731 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002732 }
2733 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002734}
2735
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002736/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002737 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2738 * Returns OK or FAIL.
2739 */
2740 static int
2741tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002742 typval_T *tv1;
2743 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002744 char_u *op;
2745{
2746 long n;
2747 char_u numbuf[NUMBUFLEN];
2748 char_u *s;
2749
2750 /* Can't do anything with a Funcref or a Dict on the right. */
2751 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2752 {
2753 switch (tv1->v_type)
2754 {
2755 case VAR_DICT:
2756 case VAR_FUNC:
2757 break;
2758
2759 case VAR_LIST:
2760 if (*op != '+' || tv2->v_type != VAR_LIST)
2761 break;
2762 /* List += List */
2763 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2764 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2765 return OK;
2766
2767 case VAR_NUMBER:
2768 case VAR_STRING:
2769 if (tv2->v_type == VAR_LIST)
2770 break;
2771 if (*op == '+' || *op == '-')
2772 {
2773 /* nr += nr or nr -= nr*/
2774 n = get_tv_number(tv1);
2775 if (*op == '+')
2776 n += get_tv_number(tv2);
2777 else
2778 n -= get_tv_number(tv2);
2779 clear_tv(tv1);
2780 tv1->v_type = VAR_NUMBER;
2781 tv1->vval.v_number = n;
2782 }
2783 else
2784 {
2785 /* str .= str */
2786 s = get_tv_string(tv1);
2787 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2788 clear_tv(tv1);
2789 tv1->v_type = VAR_STRING;
2790 tv1->vval.v_string = s;
2791 }
2792 return OK;
2793 }
2794 }
2795
2796 EMSG2(_(e_letwrong), op);
2797 return FAIL;
2798}
2799
2800/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002801 * Add a watcher to a list.
2802 */
2803 static void
2804list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002805 list_T *l;
2806 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002807{
2808 lw->lw_next = l->lv_watch;
2809 l->lv_watch = lw;
2810}
2811
2812/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002813 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002814 * No warning when it isn't found...
2815 */
2816 static void
2817list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002818 list_T *l;
2819 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002820{
Bram Moolenaar33570922005-01-25 22:26:29 +00002821 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002822
2823 lwp = &l->lv_watch;
2824 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2825 {
2826 if (lw == lwrem)
2827 {
2828 *lwp = lw->lw_next;
2829 break;
2830 }
2831 lwp = &lw->lw_next;
2832 }
2833}
2834
2835/*
2836 * Just before removing an item from a list: advance watchers to the next
2837 * item.
2838 */
2839 static void
2840list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002841 list_T *l;
2842 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002843{
Bram Moolenaar33570922005-01-25 22:26:29 +00002844 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002845
2846 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2847 if (lw->lw_item == item)
2848 lw->lw_item = item->li_next;
2849}
2850
2851/*
2852 * Evaluate the expression used in a ":for var in expr" command.
2853 * "arg" points to "var".
2854 * Set "*errp" to TRUE for an error, FALSE otherwise;
2855 * Return a pointer that holds the info. Null when there is an error.
2856 */
2857 void *
2858eval_for_line(arg, errp, nextcmdp, skip)
2859 char_u *arg;
2860 int *errp;
2861 char_u **nextcmdp;
2862 int skip;
2863{
Bram Moolenaar33570922005-01-25 22:26:29 +00002864 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002865 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002866 typval_T tv;
2867 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002868
2869 *errp = TRUE; /* default: there is an error */
2870
Bram Moolenaar33570922005-01-25 22:26:29 +00002871 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002872 if (fi == NULL)
2873 return NULL;
2874
2875 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2876 if (expr == NULL)
2877 return fi;
2878
2879 expr = skipwhite(expr);
2880 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2881 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002882 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002883 return fi;
2884 }
2885
2886 if (skip)
2887 ++emsg_skip;
2888 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2889 {
2890 *errp = FALSE;
2891 if (!skip)
2892 {
2893 l = tv.vval.v_list;
2894 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002895 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002896 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002897 clear_tv(&tv);
2898 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002899 else
2900 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002901 /* No need to increment the refcount, it's already set for the
2902 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002903 fi->fi_list = l;
2904 list_add_watch(l, &fi->fi_lw);
2905 fi->fi_lw.lw_item = l->lv_first;
2906 }
2907 }
2908 }
2909 if (skip)
2910 --emsg_skip;
2911
2912 return fi;
2913}
2914
2915/*
2916 * Use the first item in a ":for" list. Advance to the next.
2917 * Assign the values to the variable (list). "arg" points to the first one.
2918 * Return TRUE when a valid item was found, FALSE when at end of list or
2919 * something wrong.
2920 */
2921 int
2922next_for_item(fi_void, arg)
2923 void *fi_void;
2924 char_u *arg;
2925{
Bram Moolenaar33570922005-01-25 22:26:29 +00002926 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002927 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002928 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002929
2930 item = fi->fi_lw.lw_item;
2931 if (item == NULL)
2932 result = FALSE;
2933 else
2934 {
2935 fi->fi_lw.lw_item = item->li_next;
2936 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2937 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2938 }
2939 return result;
2940}
2941
2942/*
2943 * Free the structure used to store info used by ":for".
2944 */
2945 void
2946free_for_info(fi_void)
2947 void *fi_void;
2948{
Bram Moolenaar33570922005-01-25 22:26:29 +00002949 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002950
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002951 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002952 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002953 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002954 list_unref(fi->fi_list);
2955 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002956 vim_free(fi);
2957}
2958
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2960
2961 void
2962set_context_for_expression(xp, arg, cmdidx)
2963 expand_T *xp;
2964 char_u *arg;
2965 cmdidx_T cmdidx;
2966{
2967 int got_eq = FALSE;
2968 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002969 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002971 if (cmdidx == CMD_let)
2972 {
2973 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002974 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002975 {
2976 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002977 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002978 {
2979 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00002980 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002981 if (vim_iswhite(*p))
2982 break;
2983 }
2984 return;
2985 }
2986 }
2987 else
2988 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2989 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 while ((xp->xp_pattern = vim_strpbrk(arg,
2991 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2992 {
2993 c = *xp->xp_pattern;
2994 if (c == '&')
2995 {
2996 c = xp->xp_pattern[1];
2997 if (c == '&')
2998 {
2999 ++xp->xp_pattern;
3000 xp->xp_context = cmdidx != CMD_let || got_eq
3001 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3002 }
3003 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003004 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003006 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3007 xp->xp_pattern += 2;
3008
3009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 }
3011 else if (c == '$')
3012 {
3013 /* environment variable */
3014 xp->xp_context = EXPAND_ENV_VARS;
3015 }
3016 else if (c == '=')
3017 {
3018 got_eq = TRUE;
3019 xp->xp_context = EXPAND_EXPRESSION;
3020 }
3021 else if (c == '<'
3022 && xp->xp_context == EXPAND_FUNCTIONS
3023 && vim_strchr(xp->xp_pattern, '(') == NULL)
3024 {
3025 /* Function name can start with "<SNR>" */
3026 break;
3027 }
3028 else if (cmdidx != CMD_let || got_eq)
3029 {
3030 if (c == '"') /* string */
3031 {
3032 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3033 if (c == '\\' && xp->xp_pattern[1] != NUL)
3034 ++xp->xp_pattern;
3035 xp->xp_context = EXPAND_NOTHING;
3036 }
3037 else if (c == '\'') /* literal string */
3038 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003039 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3041 /* skip */ ;
3042 xp->xp_context = EXPAND_NOTHING;
3043 }
3044 else if (c == '|')
3045 {
3046 if (xp->xp_pattern[1] == '|')
3047 {
3048 ++xp->xp_pattern;
3049 xp->xp_context = EXPAND_EXPRESSION;
3050 }
3051 else
3052 xp->xp_context = EXPAND_COMMANDS;
3053 }
3054 else
3055 xp->xp_context = EXPAND_EXPRESSION;
3056 }
3057 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003058 /* Doesn't look like something valid, expand as an expression
3059 * anyway. */
3060 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 arg = xp->xp_pattern;
3062 if (*arg != NUL)
3063 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3064 /* skip */ ;
3065 }
3066 xp->xp_pattern = arg;
3067}
3068
3069#endif /* FEAT_CMDL_COMPL */
3070
3071/*
3072 * ":1,25call func(arg1, arg2)" function call.
3073 */
3074 void
3075ex_call(eap)
3076 exarg_T *eap;
3077{
3078 char_u *arg = eap->arg;
3079 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003081 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003083 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 linenr_T lnum;
3085 int doesrange;
3086 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003087 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003089 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
3090 vim_free(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003091 if (tofree == NULL)
3092 return;
3093
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003094 /* Increase refcount on dictionary, it could get deleted when evaluating
3095 * the arguments. */
3096 if (fudi.fd_dict != NULL)
3097 ++fudi.fd_dict->dv_refcount;
3098
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003099 /* If it is the name of a variable of type VAR_FUNC use its contents. */
3100 len = STRLEN(tofree);
3101 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102
Bram Moolenaar532c7802005-01-27 14:44:31 +00003103 /* Skip white space to allow ":call func ()". Not good, but required for
3104 * backward compatibility. */
3105 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003106 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107
3108 if (*startarg != '(')
3109 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003110 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 goto end;
3112 }
3113
3114 /*
3115 * When skipping, evaluate the function once, to find the end of the
3116 * arguments.
3117 * When the function takes a range, this is discovered after the first
3118 * call, and the loop is broken.
3119 */
3120 if (eap->skip)
3121 {
3122 ++emsg_skip;
3123 lnum = eap->line2; /* do it once, also with an invalid range */
3124 }
3125 else
3126 lnum = eap->line1;
3127 for ( ; lnum <= eap->line2; ++lnum)
3128 {
3129 if (!eap->skip && eap->addr_count > 0)
3130 {
3131 curwin->w_cursor.lnum = lnum;
3132 curwin->w_cursor.col = 0;
3133 }
3134 arg = startarg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003135 if (get_func_tv(name, STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003136 eap->line1, eap->line2, &doesrange,
3137 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 {
3139 failed = TRUE;
3140 break;
3141 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003142 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 if (doesrange || eap->skip)
3144 break;
3145 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003146 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003147 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003148 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 if (aborting())
3150 break;
3151 }
3152 if (eap->skip)
3153 --emsg_skip;
3154
3155 if (!failed)
3156 {
3157 /* Check for trailing illegal characters and a following command. */
3158 if (!ends_excmd(*arg))
3159 {
3160 emsg_severe = TRUE;
3161 EMSG(_(e_trailing));
3162 }
3163 else
3164 eap->nextcmd = check_nextcmd(arg);
3165 }
3166
3167end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003168 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003169 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170}
3171
3172/*
3173 * ":unlet[!] var1 ... " command.
3174 */
3175 void
3176ex_unlet(eap)
3177 exarg_T *eap;
3178{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003179 ex_unletlock(eap, eap->arg, 0);
3180}
3181
3182/*
3183 * ":lockvar" and ":unlockvar" commands
3184 */
3185 void
3186ex_lockvar(eap)
3187 exarg_T *eap;
3188{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003190 int deep = 2;
3191
3192 if (eap->forceit)
3193 deep = -1;
3194 else if (vim_isdigit(*arg))
3195 {
3196 deep = getdigits(&arg);
3197 arg = skipwhite(arg);
3198 }
3199
3200 ex_unletlock(eap, arg, deep);
3201}
3202
3203/*
3204 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3205 */
3206 static void
3207ex_unletlock(eap, argstart, deep)
3208 exarg_T *eap;
3209 char_u *argstart;
3210 int deep;
3211{
3212 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003215 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216
3217 do
3218 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003219 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003220 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3221 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003222 if (lv.ll_name == NULL)
3223 error = TRUE; /* error but continue parsing */
3224 if (name_end == NULL || (!vim_iswhite(*name_end)
3225 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003227 if (name_end != NULL)
3228 {
3229 emsg_severe = TRUE;
3230 EMSG(_(e_trailing));
3231 }
3232 if (!(eap->skip || error))
3233 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 break;
3235 }
3236
3237 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003238 {
3239 if (eap->cmdidx == CMD_unlet)
3240 {
3241 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3242 error = TRUE;
3243 }
3244 else
3245 {
3246 if (do_lock_var(&lv, name_end, deep,
3247 eap->cmdidx == CMD_lockvar) == FAIL)
3248 error = TRUE;
3249 }
3250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003252 if (!eap->skip)
3253 clear_lval(&lv);
3254
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 arg = skipwhite(name_end);
3256 } while (!ends_excmd(*arg));
3257
3258 eap->nextcmd = check_nextcmd(arg);
3259}
3260
Bram Moolenaar8c711452005-01-14 21:53:12 +00003261 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003262do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003263 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003264 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003265 int forceit;
3266{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003267 int ret = OK;
3268 int cc;
3269
3270 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003271 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003272 cc = *name_end;
3273 *name_end = NUL;
3274
3275 /* Normal name or expanded name. */
3276 if (check_changedtick(lp->ll_name))
3277 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003278 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003279 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003280 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003281 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003282 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3283 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003284 else if (lp->ll_range)
3285 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003286 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003287
3288 /* Delete a range of List items. */
3289 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3290 {
3291 li = lp->ll_li->li_next;
3292 listitem_remove(lp->ll_list, lp->ll_li);
3293 lp->ll_li = li;
3294 ++lp->ll_n1;
3295 }
3296 }
3297 else
3298 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003299 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003300 /* unlet a List item. */
3301 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003302 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003303 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003304 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003305 }
3306
3307 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003308}
3309
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310/*
3311 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003312 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 */
3314 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003315do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003317 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318{
Bram Moolenaar33570922005-01-25 22:26:29 +00003319 hashtab_T *ht;
3320 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003321 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322
Bram Moolenaar33570922005-01-25 22:26:29 +00003323 ht = find_var_ht(name, &varname);
3324 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003326 hi = hash_find(ht, varname);
3327 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003328 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003329 if (var_check_ro(HI2DI(hi)->di_flags, name))
3330 return FAIL;
3331 delete_var(ht, hi);
3332 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003333 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003335 if (forceit)
3336 return OK;
3337 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 return FAIL;
3339}
3340
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003341/*
3342 * Lock or unlock variable indicated by "lp".
3343 * "deep" is the levels to go (-1 for unlimited);
3344 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3345 */
3346 static int
3347do_lock_var(lp, name_end, deep, lock)
3348 lval_T *lp;
3349 char_u *name_end;
3350 int deep;
3351 int lock;
3352{
3353 int ret = OK;
3354 int cc;
3355 dictitem_T *di;
3356
3357 if (deep == 0) /* nothing to do */
3358 return OK;
3359
3360 if (lp->ll_tv == NULL)
3361 {
3362 cc = *name_end;
3363 *name_end = NUL;
3364
3365 /* Normal name or expanded name. */
3366 if (check_changedtick(lp->ll_name))
3367 ret = FAIL;
3368 else
3369 {
3370 di = find_var(lp->ll_name, NULL);
3371 if (di == NULL)
3372 ret = FAIL;
3373 else
3374 {
3375 if (lock)
3376 di->di_flags |= DI_FLAGS_LOCK;
3377 else
3378 di->di_flags &= ~DI_FLAGS_LOCK;
3379 item_lock(&di->di_tv, deep, lock);
3380 }
3381 }
3382 *name_end = cc;
3383 }
3384 else if (lp->ll_range)
3385 {
3386 listitem_T *li = lp->ll_li;
3387
3388 /* (un)lock a range of List items. */
3389 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3390 {
3391 item_lock(&li->li_tv, deep, lock);
3392 li = li->li_next;
3393 ++lp->ll_n1;
3394 }
3395 }
3396 else if (lp->ll_list != NULL)
3397 /* (un)lock a List item. */
3398 item_lock(&lp->ll_li->li_tv, deep, lock);
3399 else
3400 /* un(lock) a Dictionary item. */
3401 item_lock(&lp->ll_di->di_tv, deep, lock);
3402
3403 return ret;
3404}
3405
3406/*
3407 * Lock or unlock an item. "deep" is nr of levels to go.
3408 */
3409 static void
3410item_lock(tv, deep, lock)
3411 typval_T *tv;
3412 int deep;
3413 int lock;
3414{
3415 static int recurse = 0;
3416 list_T *l;
3417 listitem_T *li;
3418 dict_T *d;
3419 hashitem_T *hi;
3420 int todo;
3421
3422 if (recurse >= DICT_MAXNEST)
3423 {
3424 EMSG(_("E743: variable nested too deep for (un)lock"));
3425 return;
3426 }
3427 if (deep == 0)
3428 return;
3429 ++recurse;
3430
3431 /* lock/unlock the item itself */
3432 if (lock)
3433 tv->v_lock |= VAR_LOCKED;
3434 else
3435 tv->v_lock &= ~VAR_LOCKED;
3436
3437 switch (tv->v_type)
3438 {
3439 case VAR_LIST:
3440 if ((l = tv->vval.v_list) != NULL)
3441 {
3442 if (lock)
3443 l->lv_lock |= VAR_LOCKED;
3444 else
3445 l->lv_lock &= ~VAR_LOCKED;
3446 if (deep < 0 || deep > 1)
3447 /* recursive: lock/unlock the items the List contains */
3448 for (li = l->lv_first; li != NULL; li = li->li_next)
3449 item_lock(&li->li_tv, deep - 1, lock);
3450 }
3451 break;
3452 case VAR_DICT:
3453 if ((d = tv->vval.v_dict) != NULL)
3454 {
3455 if (lock)
3456 d->dv_lock |= VAR_LOCKED;
3457 else
3458 d->dv_lock &= ~VAR_LOCKED;
3459 if (deep < 0 || deep > 1)
3460 {
3461 /* recursive: lock/unlock the items the List contains */
3462 todo = d->dv_hashtab.ht_used;
3463 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3464 {
3465 if (!HASHITEM_EMPTY(hi))
3466 {
3467 --todo;
3468 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3469 }
3470 }
3471 }
3472 }
3473 }
3474 --recurse;
3475}
3476
Bram Moolenaara40058a2005-07-11 22:42:07 +00003477/*
3478 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3479 * it refers to a List or Dictionary that is locked.
3480 */
3481 static int
3482tv_islocked(tv)
3483 typval_T *tv;
3484{
3485 return (tv->v_lock & VAR_LOCKED)
3486 || (tv->v_type == VAR_LIST
3487 && tv->vval.v_list != NULL
3488 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3489 || (tv->v_type == VAR_DICT
3490 && tv->vval.v_dict != NULL
3491 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3492}
3493
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3495/*
3496 * Delete all "menutrans_" variables.
3497 */
3498 void
3499del_menutrans_vars()
3500{
Bram Moolenaar33570922005-01-25 22:26:29 +00003501 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003502 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503
Bram Moolenaar33570922005-01-25 22:26:29 +00003504 hash_lock(&globvarht);
3505 todo = globvarht.ht_used;
3506 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003507 {
3508 if (!HASHITEM_EMPTY(hi))
3509 {
3510 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003511 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3512 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003513 }
3514 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003515 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516}
3517#endif
3518
3519#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3520
3521/*
3522 * Local string buffer for the next two functions to store a variable name
3523 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3524 * get_user_var_name().
3525 */
3526
3527static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3528
3529static char_u *varnamebuf = NULL;
3530static int varnamebuflen = 0;
3531
3532/*
3533 * Function to concatenate a prefix and a variable name.
3534 */
3535 static char_u *
3536cat_prefix_varname(prefix, name)
3537 int prefix;
3538 char_u *name;
3539{
3540 int len;
3541
3542 len = (int)STRLEN(name) + 3;
3543 if (len > varnamebuflen)
3544 {
3545 vim_free(varnamebuf);
3546 len += 10; /* some additional space */
3547 varnamebuf = alloc(len);
3548 if (varnamebuf == NULL)
3549 {
3550 varnamebuflen = 0;
3551 return NULL;
3552 }
3553 varnamebuflen = len;
3554 }
3555 *varnamebuf = prefix;
3556 varnamebuf[1] = ':';
3557 STRCPY(varnamebuf + 2, name);
3558 return varnamebuf;
3559}
3560
3561/*
3562 * Function given to ExpandGeneric() to obtain the list of user defined
3563 * (global/buffer/window/built-in) variable names.
3564 */
3565/*ARGSUSED*/
3566 char_u *
3567get_user_var_name(xp, idx)
3568 expand_T *xp;
3569 int idx;
3570{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003571 static long_u gdone;
3572 static long_u bdone;
3573 static long_u wdone;
3574 static int vidx;
3575 static hashitem_T *hi;
3576 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577
3578 if (idx == 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00003579 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00003580
3581 /* Global variables */
3582 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003584 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003585 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003586 else
3587 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003588 while (HASHITEM_EMPTY(hi))
3589 ++hi;
3590 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3591 return cat_prefix_varname('g', hi->hi_key);
3592 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003594
3595 /* b: variables */
3596 ht = &curbuf->b_vars.dv_hashtab;
3597 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003599 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003600 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003601 else
3602 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003603 while (HASHITEM_EMPTY(hi))
3604 ++hi;
3605 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003607 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003609 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 return (char_u *)"b:changedtick";
3611 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003612
3613 /* w: variables */
3614 ht = &curwin->w_vars.dv_hashtab;
3615 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003617 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003618 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003619 else
3620 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003621 while (HASHITEM_EMPTY(hi))
3622 ++hi;
3623 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003625
3626 /* v: variables */
3627 if (vidx < VV_LEN)
3628 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629
3630 vim_free(varnamebuf);
3631 varnamebuf = NULL;
3632 varnamebuflen = 0;
3633 return NULL;
3634}
3635
3636#endif /* FEAT_CMDL_COMPL */
3637
3638/*
3639 * types for expressions.
3640 */
3641typedef enum
3642{
3643 TYPE_UNKNOWN = 0
3644 , TYPE_EQUAL /* == */
3645 , TYPE_NEQUAL /* != */
3646 , TYPE_GREATER /* > */
3647 , TYPE_GEQUAL /* >= */
3648 , TYPE_SMALLER /* < */
3649 , TYPE_SEQUAL /* <= */
3650 , TYPE_MATCH /* =~ */
3651 , TYPE_NOMATCH /* !~ */
3652} exptype_T;
3653
3654/*
3655 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003656 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3658 */
3659
3660/*
3661 * Handle zero level expression.
3662 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003663 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003664 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 * Return OK or FAIL.
3666 */
3667 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003668eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003670 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 char_u **nextcmd;
3672 int evaluate;
3673{
3674 int ret;
3675 char_u *p;
3676
3677 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003678 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 if (ret == FAIL || !ends_excmd(*p))
3680 {
3681 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003682 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 /*
3684 * Report the invalid expression unless the expression evaluation has
3685 * been cancelled due to an aborting error, an interrupt, or an
3686 * exception.
3687 */
3688 if (!aborting())
3689 EMSG2(_(e_invexpr2), arg);
3690 ret = FAIL;
3691 }
3692 if (nextcmd != NULL)
3693 *nextcmd = check_nextcmd(p);
3694
3695 return ret;
3696}
3697
3698/*
3699 * Handle top level expression:
3700 * expr1 ? expr0 : expr0
3701 *
3702 * "arg" must point to the first non-white of the expression.
3703 * "arg" is advanced to the next non-white after the recognized expression.
3704 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003705 * Note: "rettv.v_lock" is not set.
3706 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 * Return OK or FAIL.
3708 */
3709 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003710eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003712 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 int evaluate;
3714{
3715 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003716 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717
3718 /*
3719 * Get the first variable.
3720 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003721 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 return FAIL;
3723
3724 if ((*arg)[0] == '?')
3725 {
3726 result = FALSE;
3727 if (evaluate)
3728 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003729 int error = FALSE;
3730
3731 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003733 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003734 if (error)
3735 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 }
3737
3738 /*
3739 * Get the second variable.
3740 */
3741 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003742 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 return FAIL;
3744
3745 /*
3746 * Check for the ":".
3747 */
3748 if ((*arg)[0] != ':')
3749 {
3750 EMSG(_("E109: Missing ':' after '?'"));
3751 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003752 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 return FAIL;
3754 }
3755
3756 /*
3757 * Get the third variable.
3758 */
3759 *arg = skipwhite(*arg + 1);
3760 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3761 {
3762 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003763 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 return FAIL;
3765 }
3766 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003767 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 }
3769
3770 return OK;
3771}
3772
3773/*
3774 * Handle first level expression:
3775 * expr2 || expr2 || expr2 logical OR
3776 *
3777 * "arg" must point to the first non-white of the expression.
3778 * "arg" is advanced to the next non-white after the recognized expression.
3779 *
3780 * Return OK or FAIL.
3781 */
3782 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003783eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003785 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 int evaluate;
3787{
Bram Moolenaar33570922005-01-25 22:26:29 +00003788 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 long result;
3790 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003791 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792
3793 /*
3794 * Get the first variable.
3795 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003796 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 return FAIL;
3798
3799 /*
3800 * Repeat until there is no following "||".
3801 */
3802 first = TRUE;
3803 result = FALSE;
3804 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3805 {
3806 if (evaluate && first)
3807 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003808 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003810 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003811 if (error)
3812 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 first = FALSE;
3814 }
3815
3816 /*
3817 * Get the second variable.
3818 */
3819 *arg = skipwhite(*arg + 2);
3820 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3821 return FAIL;
3822
3823 /*
3824 * Compute the result.
3825 */
3826 if (evaluate && !result)
3827 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003828 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003830 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003831 if (error)
3832 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 }
3834 if (evaluate)
3835 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003836 rettv->v_type = VAR_NUMBER;
3837 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 }
3839 }
3840
3841 return OK;
3842}
3843
3844/*
3845 * Handle second level expression:
3846 * expr3 && expr3 && expr3 logical AND
3847 *
3848 * "arg" must point to the first non-white of the expression.
3849 * "arg" is advanced to the next non-white after the recognized expression.
3850 *
3851 * Return OK or FAIL.
3852 */
3853 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003854eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003856 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 int evaluate;
3858{
Bram Moolenaar33570922005-01-25 22:26:29 +00003859 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 long result;
3861 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003862 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863
3864 /*
3865 * Get the first variable.
3866 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003867 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 return FAIL;
3869
3870 /*
3871 * Repeat until there is no following "&&".
3872 */
3873 first = TRUE;
3874 result = TRUE;
3875 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3876 {
3877 if (evaluate && first)
3878 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003879 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003881 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003882 if (error)
3883 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 first = FALSE;
3885 }
3886
3887 /*
3888 * Get the second variable.
3889 */
3890 *arg = skipwhite(*arg + 2);
3891 if (eval4(arg, &var2, evaluate && result) == FAIL)
3892 return FAIL;
3893
3894 /*
3895 * Compute the result.
3896 */
3897 if (evaluate && result)
3898 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003899 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003901 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003902 if (error)
3903 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 }
3905 if (evaluate)
3906 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003907 rettv->v_type = VAR_NUMBER;
3908 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 }
3910 }
3911
3912 return OK;
3913}
3914
3915/*
3916 * Handle third level expression:
3917 * var1 == var2
3918 * var1 =~ var2
3919 * var1 != var2
3920 * var1 !~ var2
3921 * var1 > var2
3922 * var1 >= var2
3923 * var1 < var2
3924 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003925 * var1 is var2
3926 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 *
3928 * "arg" must point to the first non-white of the expression.
3929 * "arg" is advanced to the next non-white after the recognized expression.
3930 *
3931 * Return OK or FAIL.
3932 */
3933 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003934eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003936 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 int evaluate;
3938{
Bram Moolenaar33570922005-01-25 22:26:29 +00003939 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 char_u *p;
3941 int i;
3942 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003943 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 int len = 2;
3945 long n1, n2;
3946 char_u *s1, *s2;
3947 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3948 regmatch_T regmatch;
3949 int ic;
3950 char_u *save_cpo;
3951
3952 /*
3953 * Get the first variable.
3954 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003955 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 return FAIL;
3957
3958 p = *arg;
3959 switch (p[0])
3960 {
3961 case '=': if (p[1] == '=')
3962 type = TYPE_EQUAL;
3963 else if (p[1] == '~')
3964 type = TYPE_MATCH;
3965 break;
3966 case '!': if (p[1] == '=')
3967 type = TYPE_NEQUAL;
3968 else if (p[1] == '~')
3969 type = TYPE_NOMATCH;
3970 break;
3971 case '>': if (p[1] != '=')
3972 {
3973 type = TYPE_GREATER;
3974 len = 1;
3975 }
3976 else
3977 type = TYPE_GEQUAL;
3978 break;
3979 case '<': if (p[1] != '=')
3980 {
3981 type = TYPE_SMALLER;
3982 len = 1;
3983 }
3984 else
3985 type = TYPE_SEQUAL;
3986 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003987 case 'i': if (p[1] == 's')
3988 {
3989 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3990 len = 5;
3991 if (!vim_isIDc(p[len]))
3992 {
3993 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3994 type_is = TRUE;
3995 }
3996 }
3997 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 }
3999
4000 /*
4001 * If there is a comparitive operator, use it.
4002 */
4003 if (type != TYPE_UNKNOWN)
4004 {
4005 /* extra question mark appended: ignore case */
4006 if (p[len] == '?')
4007 {
4008 ic = TRUE;
4009 ++len;
4010 }
4011 /* extra '#' appended: match case */
4012 else if (p[len] == '#')
4013 {
4014 ic = FALSE;
4015 ++len;
4016 }
4017 /* nothing appened: use 'ignorecase' */
4018 else
4019 ic = p_ic;
4020
4021 /*
4022 * Get the second variable.
4023 */
4024 *arg = skipwhite(p + len);
4025 if (eval5(arg, &var2, evaluate) == FAIL)
4026 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004027 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 return FAIL;
4029 }
4030
4031 if (evaluate)
4032 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004033 if (type_is && rettv->v_type != var2.v_type)
4034 {
4035 /* For "is" a different type always means FALSE, for "notis"
4036 * it means TRUE. */
4037 n1 = (type == TYPE_NEQUAL);
4038 }
4039 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4040 {
4041 if (type_is)
4042 {
4043 n1 = (rettv->v_type == var2.v_type
4044 && rettv->vval.v_list == var2.vval.v_list);
4045 if (type == TYPE_NEQUAL)
4046 n1 = !n1;
4047 }
4048 else if (rettv->v_type != var2.v_type
4049 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4050 {
4051 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004052 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004053 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004054 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004055 clear_tv(rettv);
4056 clear_tv(&var2);
4057 return FAIL;
4058 }
4059 else
4060 {
4061 /* Compare two Lists for being equal or unequal. */
4062 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4063 if (type == TYPE_NEQUAL)
4064 n1 = !n1;
4065 }
4066 }
4067
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004068 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4069 {
4070 if (type_is)
4071 {
4072 n1 = (rettv->v_type == var2.v_type
4073 && rettv->vval.v_dict == var2.vval.v_dict);
4074 if (type == TYPE_NEQUAL)
4075 n1 = !n1;
4076 }
4077 else if (rettv->v_type != var2.v_type
4078 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4079 {
4080 if (rettv->v_type != var2.v_type)
4081 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4082 else
4083 EMSG(_("E736: Invalid operation for Dictionary"));
4084 clear_tv(rettv);
4085 clear_tv(&var2);
4086 return FAIL;
4087 }
4088 else
4089 {
4090 /* Compare two Dictionaries for being equal or unequal. */
4091 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4092 if (type == TYPE_NEQUAL)
4093 n1 = !n1;
4094 }
4095 }
4096
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004097 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4098 {
4099 if (rettv->v_type != var2.v_type
4100 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4101 {
4102 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004103 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004104 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004105 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004106 clear_tv(rettv);
4107 clear_tv(&var2);
4108 return FAIL;
4109 }
4110 else
4111 {
4112 /* Compare two Funcrefs for being equal or unequal. */
4113 if (rettv->vval.v_string == NULL
4114 || var2.vval.v_string == NULL)
4115 n1 = FALSE;
4116 else
4117 n1 = STRCMP(rettv->vval.v_string,
4118 var2.vval.v_string) == 0;
4119 if (type == TYPE_NEQUAL)
4120 n1 = !n1;
4121 }
4122 }
4123
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 /*
4125 * If one of the two variables is a number, compare as a number.
4126 * When using "=~" or "!~", always compare as string.
4127 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004128 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4130 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004131 n1 = get_tv_number(rettv);
4132 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 switch (type)
4134 {
4135 case TYPE_EQUAL: n1 = (n1 == n2); break;
4136 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4137 case TYPE_GREATER: n1 = (n1 > n2); break;
4138 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4139 case TYPE_SMALLER: n1 = (n1 < n2); break;
4140 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4141 case TYPE_UNKNOWN:
4142 case TYPE_MATCH:
4143 case TYPE_NOMATCH: break; /* avoid gcc warning */
4144 }
4145 }
4146 else
4147 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004148 s1 = get_tv_string_buf(rettv, buf1);
4149 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4151 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4152 else
4153 i = 0;
4154 n1 = FALSE;
4155 switch (type)
4156 {
4157 case TYPE_EQUAL: n1 = (i == 0); break;
4158 case TYPE_NEQUAL: n1 = (i != 0); break;
4159 case TYPE_GREATER: n1 = (i > 0); break;
4160 case TYPE_GEQUAL: n1 = (i >= 0); break;
4161 case TYPE_SMALLER: n1 = (i < 0); break;
4162 case TYPE_SEQUAL: n1 = (i <= 0); break;
4163
4164 case TYPE_MATCH:
4165 case TYPE_NOMATCH:
4166 /* avoid 'l' flag in 'cpoptions' */
4167 save_cpo = p_cpo;
4168 p_cpo = (char_u *)"";
4169 regmatch.regprog = vim_regcomp(s2,
4170 RE_MAGIC + RE_STRING);
4171 regmatch.rm_ic = ic;
4172 if (regmatch.regprog != NULL)
4173 {
4174 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4175 vim_free(regmatch.regprog);
4176 if (type == TYPE_NOMATCH)
4177 n1 = !n1;
4178 }
4179 p_cpo = save_cpo;
4180 break;
4181
4182 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4183 }
4184 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004185 clear_tv(rettv);
4186 clear_tv(&var2);
4187 rettv->v_type = VAR_NUMBER;
4188 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 }
4190 }
4191
4192 return OK;
4193}
4194
4195/*
4196 * Handle fourth level expression:
4197 * + number addition
4198 * - number subtraction
4199 * . string concatenation
4200 *
4201 * "arg" must point to the first non-white of the expression.
4202 * "arg" is advanced to the next non-white after the recognized expression.
4203 *
4204 * Return OK or FAIL.
4205 */
4206 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004207eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004209 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 int evaluate;
4211{
Bram Moolenaar33570922005-01-25 22:26:29 +00004212 typval_T var2;
4213 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 int op;
4215 long n1, n2;
4216 char_u *s1, *s2;
4217 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4218 char_u *p;
4219
4220 /*
4221 * Get the first variable.
4222 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004223 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 return FAIL;
4225
4226 /*
4227 * Repeat computing, until no '+', '-' or '.' is following.
4228 */
4229 for (;;)
4230 {
4231 op = **arg;
4232 if (op != '+' && op != '-' && op != '.')
4233 break;
4234
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004235 if (op != '+' || rettv->v_type != VAR_LIST)
4236 {
4237 /* For "list + ...", an illegal use of the first operand as
4238 * a number cannot be determined before evaluating the 2nd
4239 * operand: if this is also a list, all is ok.
4240 * For "something . ...", "something - ..." or "non-list + ...",
4241 * we know that the first operand needs to be a string or number
4242 * without evaluating the 2nd operand. So check before to avoid
4243 * side effects after an error. */
4244 if (evaluate && get_tv_string_chk(rettv) == NULL)
4245 {
4246 clear_tv(rettv);
4247 return FAIL;
4248 }
4249 }
4250
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 /*
4252 * Get the second variable.
4253 */
4254 *arg = skipwhite(*arg + 1);
4255 if (eval6(arg, &var2, evaluate) == FAIL)
4256 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004257 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 return FAIL;
4259 }
4260
4261 if (evaluate)
4262 {
4263 /*
4264 * Compute the result.
4265 */
4266 if (op == '.')
4267 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004268 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4269 s2 = get_tv_string_buf_chk(&var2, buf2);
4270 if (s2 == NULL) /* type error ? */
4271 {
4272 clear_tv(rettv);
4273 clear_tv(&var2);
4274 return FAIL;
4275 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004276 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004277 clear_tv(rettv);
4278 rettv->v_type = VAR_STRING;
4279 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004281 else if (op == '+' && rettv->v_type == VAR_LIST
4282 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004283 {
4284 /* concatenate Lists */
4285 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4286 &var3) == FAIL)
4287 {
4288 clear_tv(rettv);
4289 clear_tv(&var2);
4290 return FAIL;
4291 }
4292 clear_tv(rettv);
4293 *rettv = var3;
4294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 else
4296 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004297 int error = FALSE;
4298
4299 n1 = get_tv_number_chk(rettv, &error);
4300 if (error)
4301 {
4302 /* This can only happen for "list + non-list".
4303 * For "non-list + ..." or "something - ...", we returned
4304 * before evaluating the 2nd operand. */
4305 clear_tv(rettv);
4306 return FAIL;
4307 }
4308 n2 = get_tv_number_chk(&var2, &error);
4309 if (error)
4310 {
4311 clear_tv(rettv);
4312 clear_tv(&var2);
4313 return FAIL;
4314 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004315 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 if (op == '+')
4317 n1 = n1 + n2;
4318 else
4319 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004320 rettv->v_type = VAR_NUMBER;
4321 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004323 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 }
4325 }
4326 return OK;
4327}
4328
4329/*
4330 * Handle fifth level expression:
4331 * * number multiplication
4332 * / number division
4333 * % number modulo
4334 *
4335 * "arg" must point to the first non-white of the expression.
4336 * "arg" is advanced to the next non-white after the recognized expression.
4337 *
4338 * Return OK or FAIL.
4339 */
4340 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004341eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004343 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 int evaluate;
4345{
Bram Moolenaar33570922005-01-25 22:26:29 +00004346 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 int op;
4348 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004349 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350
4351 /*
4352 * Get the first variable.
4353 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004354 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 return FAIL;
4356
4357 /*
4358 * Repeat computing, until no '*', '/' or '%' is following.
4359 */
4360 for (;;)
4361 {
4362 op = **arg;
4363 if (op != '*' && op != '/' && op != '%')
4364 break;
4365
4366 if (evaluate)
4367 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004368 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004369 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004370 if (error)
4371 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 }
4373 else
4374 n1 = 0;
4375
4376 /*
4377 * Get the second variable.
4378 */
4379 *arg = skipwhite(*arg + 1);
4380 if (eval7(arg, &var2, evaluate) == FAIL)
4381 return FAIL;
4382
4383 if (evaluate)
4384 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004385 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004386 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004387 if (error)
4388 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389
4390 /*
4391 * Compute the result.
4392 */
4393 if (op == '*')
4394 n1 = n1 * n2;
4395 else if (op == '/')
4396 {
4397 if (n2 == 0) /* give an error message? */
4398 n1 = 0x7fffffffL;
4399 else
4400 n1 = n1 / n2;
4401 }
4402 else
4403 {
4404 if (n2 == 0) /* give an error message? */
4405 n1 = 0;
4406 else
4407 n1 = n1 % n2;
4408 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004409 rettv->v_type = VAR_NUMBER;
4410 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 }
4412 }
4413
4414 return OK;
4415}
4416
4417/*
4418 * Handle sixth level expression:
4419 * number number constant
4420 * "string" string contstant
4421 * 'string' literal string contstant
4422 * &option-name option value
4423 * @r register contents
4424 * identifier variable value
4425 * function() function call
4426 * $VAR environment variable
4427 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004428 * [expr, expr] List
4429 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 *
4431 * Also handle:
4432 * ! in front logical NOT
4433 * - in front unary minus
4434 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004435 * trailing [] subscript in String or List
4436 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 *
4438 * "arg" must point to the first non-white of the expression.
4439 * "arg" is advanced to the next non-white after the recognized expression.
4440 *
4441 * Return OK or FAIL.
4442 */
4443 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004444eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004446 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 int evaluate;
4448{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 long n;
4450 int len;
4451 char_u *s;
4452 int val;
4453 char_u *start_leader, *end_leader;
4454 int ret = OK;
4455 char_u *alias;
4456
4457 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004458 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004459 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004461 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462
4463 /*
4464 * Skip '!' and '-' characters. They are handled later.
4465 */
4466 start_leader = *arg;
4467 while (**arg == '!' || **arg == '-' || **arg == '+')
4468 *arg = skipwhite(*arg + 1);
4469 end_leader = *arg;
4470
4471 switch (**arg)
4472 {
4473 /*
4474 * Number constant.
4475 */
4476 case '0':
4477 case '1':
4478 case '2':
4479 case '3':
4480 case '4':
4481 case '5':
4482 case '6':
4483 case '7':
4484 case '8':
4485 case '9':
4486 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4487 *arg += len;
4488 if (evaluate)
4489 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004490 rettv->v_type = VAR_NUMBER;
4491 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 }
4493 break;
4494
4495 /*
4496 * String constant: "string".
4497 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004498 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 break;
4500
4501 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004502 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004504 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004505 break;
4506
4507 /*
4508 * List: [expr, expr]
4509 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004510 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 break;
4512
4513 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004514 * Dictionary: {key: val, key: val}
4515 */
4516 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4517 break;
4518
4519 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004520 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004522 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 break;
4524
4525 /*
4526 * Environment variable: $VAR.
4527 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004528 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 break;
4530
4531 /*
4532 * Register contents: @r.
4533 */
4534 case '@': ++*arg;
4535 if (evaluate)
4536 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004537 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004538 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 }
4540 if (**arg != NUL)
4541 ++*arg;
4542 break;
4543
4544 /*
4545 * nested expression: (expression).
4546 */
4547 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004548 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 if (**arg == ')')
4550 ++*arg;
4551 else if (ret == OK)
4552 {
4553 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004554 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 ret = FAIL;
4556 }
4557 break;
4558
Bram Moolenaar8c711452005-01-14 21:53:12 +00004559 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 break;
4561 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004562
4563 if (ret == NOTDONE)
4564 {
4565 /*
4566 * Must be a variable or function name.
4567 * Can also be a curly-braces kind of name: {expr}.
4568 */
4569 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004570 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004571 if (alias != NULL)
4572 s = alias;
4573
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004574 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004575 ret = FAIL;
4576 else
4577 {
4578 if (**arg == '(') /* recursive! */
4579 {
4580 /* If "s" is the name of a variable of type VAR_FUNC
4581 * use its contents. */
4582 s = deref_func_name(s, &len);
4583
4584 /* Invoke the function. */
4585 ret = get_func_tv(s, len, rettv, arg,
4586 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004587 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004588 /* Stop the expression evaluation when immediately
4589 * aborting on error, or when an interrupt occurred or
4590 * an exception was thrown but not caught. */
4591 if (aborting())
4592 {
4593 if (ret == OK)
4594 clear_tv(rettv);
4595 ret = FAIL;
4596 }
4597 }
4598 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004599 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004600 else
4601 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004602 }
4603
4604 if (alias != NULL)
4605 vim_free(alias);
4606 }
4607
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 *arg = skipwhite(*arg);
4609
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004610 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4611 * expr(expr). */
4612 if (ret == OK)
4613 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614
4615 /*
4616 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4617 */
4618 if (ret == OK && evaluate && end_leader > start_leader)
4619 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004620 int error = FALSE;
4621
4622 val = get_tv_number_chk(rettv, &error);
4623 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004625 clear_tv(rettv);
4626 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004628 else
4629 {
4630 while (end_leader > start_leader)
4631 {
4632 --end_leader;
4633 if (*end_leader == '!')
4634 val = !val;
4635 else if (*end_leader == '-')
4636 val = -val;
4637 }
4638 clear_tv(rettv);
4639 rettv->v_type = VAR_NUMBER;
4640 rettv->vval.v_number = val;
4641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 }
4643
4644 return ret;
4645}
4646
4647/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004648 * Evaluate an "[expr]" or "[expr:expr]" index.
4649 * "*arg" points to the '['.
4650 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4651 */
4652 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004653eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004654 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004655 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004656 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004657 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004658{
4659 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004660 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004661 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004662 long len = -1;
4663 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004664 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004665 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004666
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004667 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004668 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004669 if (verbose)
4670 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004671 return FAIL;
4672 }
4673
Bram Moolenaar8c711452005-01-14 21:53:12 +00004674 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004675 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004676 /*
4677 * dict.name
4678 */
4679 key = *arg + 1;
4680 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4681 ;
4682 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004683 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004684 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004685 }
4686 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004687 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004688 /*
4689 * something[idx]
4690 *
4691 * Get the (first) variable from inside the [].
4692 */
4693 *arg = skipwhite(*arg + 1);
4694 if (**arg == ':')
4695 empty1 = TRUE;
4696 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4697 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004698 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4699 {
4700 /* not a number or string */
4701 clear_tv(&var1);
4702 return FAIL;
4703 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004704
4705 /*
4706 * Get the second variable from inside the [:].
4707 */
4708 if (**arg == ':')
4709 {
4710 range = TRUE;
4711 *arg = skipwhite(*arg + 1);
4712 if (**arg == ']')
4713 empty2 = TRUE;
4714 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4715 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004716 if (!empty1)
4717 clear_tv(&var1);
4718 return FAIL;
4719 }
4720 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4721 {
4722 /* not a number or string */
4723 if (!empty1)
4724 clear_tv(&var1);
4725 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004726 return FAIL;
4727 }
4728 }
4729
4730 /* Check for the ']'. */
4731 if (**arg != ']')
4732 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004733 if (verbose)
4734 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004735 clear_tv(&var1);
4736 if (range)
4737 clear_tv(&var2);
4738 return FAIL;
4739 }
4740 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004741 }
4742
4743 if (evaluate)
4744 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004745 n1 = 0;
4746 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004747 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004748 n1 = get_tv_number(&var1);
4749 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004750 }
4751 if (range)
4752 {
4753 if (empty2)
4754 n2 = -1;
4755 else
4756 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004757 n2 = get_tv_number(&var2);
4758 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004759 }
4760 }
4761
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004762 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004763 {
4764 case VAR_NUMBER:
4765 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004766 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004767 len = (long)STRLEN(s);
4768 if (range)
4769 {
4770 /* The resulting variable is a substring. If the indexes
4771 * are out of range the result is empty. */
4772 if (n1 < 0)
4773 {
4774 n1 = len + n1;
4775 if (n1 < 0)
4776 n1 = 0;
4777 }
4778 if (n2 < 0)
4779 n2 = len + n2;
4780 else if (n2 >= len)
4781 n2 = len;
4782 if (n1 >= len || n2 < 0 || n1 > n2)
4783 s = NULL;
4784 else
4785 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4786 }
4787 else
4788 {
4789 /* The resulting variable is a string of a single
4790 * character. If the index is too big or negative the
4791 * result is empty. */
4792 if (n1 >= len || n1 < 0)
4793 s = NULL;
4794 else
4795 s = vim_strnsave(s + n1, 1);
4796 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004797 clear_tv(rettv);
4798 rettv->v_type = VAR_STRING;
4799 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004800 break;
4801
4802 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004803 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004804 if (n1 < 0)
4805 n1 = len + n1;
4806 if (!empty1 && (n1 < 0 || n1 >= len))
4807 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004808 if (verbose)
4809 EMSGN(_(e_listidx), n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004810 return FAIL;
4811 }
4812 if (range)
4813 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004814 list_T *l;
4815 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004816
4817 if (n2 < 0)
4818 n2 = len + n2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004819 if (!empty2 && (n2 < 0 || n2 >= len || n2 + 1 < n1))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004820 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004821 if (verbose)
4822 EMSGN(_(e_listidx), n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004823 return FAIL;
4824 }
4825 l = list_alloc();
4826 if (l == NULL)
4827 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004828 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004829 n1 <= n2; ++n1)
4830 {
4831 if (list_append_tv(l, &item->li_tv) == FAIL)
4832 {
4833 list_free(l);
4834 return FAIL;
4835 }
4836 item = item->li_next;
4837 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004838 clear_tv(rettv);
4839 rettv->v_type = VAR_LIST;
4840 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004841 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004842 }
4843 else
4844 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004845 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv,
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004846 &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004847 clear_tv(rettv);
4848 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004849 }
4850 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004851
4852 case VAR_DICT:
4853 if (range)
4854 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004855 if (verbose)
4856 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004857 if (len == -1)
4858 clear_tv(&var1);
4859 return FAIL;
4860 }
4861 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004862 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004863
4864 if (len == -1)
4865 {
4866 key = get_tv_string(&var1);
4867 if (*key == NUL)
4868 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004869 if (verbose)
4870 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004871 clear_tv(&var1);
4872 return FAIL;
4873 }
4874 }
4875
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004876 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004877
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004878 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004879 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004880 if (len == -1)
4881 clear_tv(&var1);
4882 if (item == NULL)
4883 return FAIL;
4884
4885 copy_tv(&item->di_tv, &var1);
4886 clear_tv(rettv);
4887 *rettv = var1;
4888 }
4889 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004890 }
4891 }
4892
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004893 return OK;
4894}
4895
4896/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 * Get an option value.
4898 * "arg" points to the '&' or '+' before the option name.
4899 * "arg" is advanced to character after the option name.
4900 * Return OK or FAIL.
4901 */
4902 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004903get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004905 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 int evaluate;
4907{
4908 char_u *option_end;
4909 long numval;
4910 char_u *stringval;
4911 int opt_type;
4912 int c;
4913 int working = (**arg == '+'); /* has("+option") */
4914 int ret = OK;
4915 int opt_flags;
4916
4917 /*
4918 * Isolate the option name and find its value.
4919 */
4920 option_end = find_option_end(arg, &opt_flags);
4921 if (option_end == NULL)
4922 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004923 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 EMSG2(_("E112: Option name missing: %s"), *arg);
4925 return FAIL;
4926 }
4927
4928 if (!evaluate)
4929 {
4930 *arg = option_end;
4931 return OK;
4932 }
4933
4934 c = *option_end;
4935 *option_end = NUL;
4936 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004937 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938
4939 if (opt_type == -3) /* invalid name */
4940 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004941 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 EMSG2(_("E113: Unknown option: %s"), *arg);
4943 ret = FAIL;
4944 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004945 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 {
4947 if (opt_type == -2) /* hidden string option */
4948 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004949 rettv->v_type = VAR_STRING;
4950 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 }
4952 else if (opt_type == -1) /* hidden number option */
4953 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004954 rettv->v_type = VAR_NUMBER;
4955 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 }
4957 else if (opt_type == 1) /* number option */
4958 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004959 rettv->v_type = VAR_NUMBER;
4960 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 }
4962 else /* string option */
4963 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004964 rettv->v_type = VAR_STRING;
4965 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 }
4967 }
4968 else if (working && (opt_type == -2 || opt_type == -1))
4969 ret = FAIL;
4970
4971 *option_end = c; /* put back for error messages */
4972 *arg = option_end;
4973
4974 return ret;
4975}
4976
4977/*
4978 * Allocate a variable for a string constant.
4979 * Return OK or FAIL.
4980 */
4981 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004982get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004984 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 int evaluate;
4986{
4987 char_u *p;
4988 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 int extra = 0;
4990
4991 /*
4992 * Find the end of the string, skipping backslashed characters.
4993 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004994 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 {
4996 if (*p == '\\' && p[1] != NUL)
4997 {
4998 ++p;
4999 /* A "\<x>" form occupies at least 4 characters, and produces up
5000 * to 6 characters: reserve space for 2 extra */
5001 if (*p == '<')
5002 extra += 2;
5003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 }
5005
5006 if (*p != '"')
5007 {
5008 EMSG2(_("E114: Missing quote: %s"), *arg);
5009 return FAIL;
5010 }
5011
5012 /* If only parsing, set *arg and return here */
5013 if (!evaluate)
5014 {
5015 *arg = p + 1;
5016 return OK;
5017 }
5018
5019 /*
5020 * Copy the string into allocated memory, handling backslashed
5021 * characters.
5022 */
5023 name = alloc((unsigned)(p - *arg + extra));
5024 if (name == NULL)
5025 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005026 rettv->v_type = VAR_STRING;
5027 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028
Bram Moolenaar8c711452005-01-14 21:53:12 +00005029 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 {
5031 if (*p == '\\')
5032 {
5033 switch (*++p)
5034 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005035 case 'b': *name++ = BS; ++p; break;
5036 case 'e': *name++ = ESC; ++p; break;
5037 case 'f': *name++ = FF; ++p; break;
5038 case 'n': *name++ = NL; ++p; break;
5039 case 'r': *name++ = CAR; ++p; break;
5040 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041
5042 case 'X': /* hex: "\x1", "\x12" */
5043 case 'x':
5044 case 'u': /* Unicode: "\u0023" */
5045 case 'U':
5046 if (vim_isxdigit(p[1]))
5047 {
5048 int n, nr;
5049 int c = toupper(*p);
5050
5051 if (c == 'X')
5052 n = 2;
5053 else
5054 n = 4;
5055 nr = 0;
5056 while (--n >= 0 && vim_isxdigit(p[1]))
5057 {
5058 ++p;
5059 nr = (nr << 4) + hex2nr(*p);
5060 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005061 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062#ifdef FEAT_MBYTE
5063 /* For "\u" store the number according to
5064 * 'encoding'. */
5065 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005066 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 else
5068#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005069 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 break;
5072
5073 /* octal: "\1", "\12", "\123" */
5074 case '0':
5075 case '1':
5076 case '2':
5077 case '3':
5078 case '4':
5079 case '5':
5080 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005081 case '7': *name = *p++ - '0';
5082 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005084 *name = (*name << 3) + *p++ - '0';
5085 if (*p >= '0' && *p <= '7')
5086 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005088 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 break;
5090
5091 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005092 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 if (extra != 0)
5094 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005095 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 break;
5097 }
5098 /* FALLTHROUGH */
5099
Bram Moolenaar8c711452005-01-14 21:53:12 +00005100 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 break;
5102 }
5103 }
5104 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005105 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005108 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 *arg = p + 1;
5110
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 return OK;
5112}
5113
5114/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005115 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 * Return OK or FAIL.
5117 */
5118 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005119get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005121 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 int evaluate;
5123{
5124 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005125 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005126 int reduce = 0;
5127
5128 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005129 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005130 */
5131 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5132 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005133 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005134 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005135 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005136 break;
5137 ++reduce;
5138 ++p;
5139 }
5140 }
5141
Bram Moolenaar8c711452005-01-14 21:53:12 +00005142 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005143 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005144 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005145 return FAIL;
5146 }
5147
Bram Moolenaar8c711452005-01-14 21:53:12 +00005148 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005149 if (!evaluate)
5150 {
5151 *arg = p + 1;
5152 return OK;
5153 }
5154
5155 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005156 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005157 */
5158 str = alloc((unsigned)((p - *arg) - reduce));
5159 if (str == NULL)
5160 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005161 rettv->v_type = VAR_STRING;
5162 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005163
Bram Moolenaar8c711452005-01-14 21:53:12 +00005164 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005165 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005166 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005167 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005168 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005169 break;
5170 ++p;
5171 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005172 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005173 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005174 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005175 *arg = p + 1;
5176
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005177 return OK;
5178}
5179
5180/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005181 * Allocate a variable for a List and fill it from "*arg".
5182 * Return OK or FAIL.
5183 */
5184 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005185get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005186 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005187 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005188 int evaluate;
5189{
Bram Moolenaar33570922005-01-25 22:26:29 +00005190 list_T *l = NULL;
5191 typval_T tv;
5192 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005193
5194 if (evaluate)
5195 {
5196 l = list_alloc();
5197 if (l == NULL)
5198 return FAIL;
5199 }
5200
5201 *arg = skipwhite(*arg + 1);
5202 while (**arg != ']' && **arg != NUL)
5203 {
5204 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5205 goto failret;
5206 if (evaluate)
5207 {
5208 item = listitem_alloc();
5209 if (item != NULL)
5210 {
5211 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005212 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005213 list_append(l, item);
5214 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005215 else
5216 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005217 }
5218
5219 if (**arg == ']')
5220 break;
5221 if (**arg != ',')
5222 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005223 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005224 goto failret;
5225 }
5226 *arg = skipwhite(*arg + 1);
5227 }
5228
5229 if (**arg != ']')
5230 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005231 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005232failret:
5233 if (evaluate)
5234 list_free(l);
5235 return FAIL;
5236 }
5237
5238 *arg = skipwhite(*arg + 1);
5239 if (evaluate)
5240 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005241 rettv->v_type = VAR_LIST;
5242 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005243 ++l->lv_refcount;
5244 }
5245
5246 return OK;
5247}
5248
5249/*
5250 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005251 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005252 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005253 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005254list_alloc()
5255{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005256 list_T *l;
5257
5258 l = (list_T *)alloc_clear(sizeof(list_T));
5259 if (l != NULL)
5260 {
5261 /* Prepend the list to the list of lists for garbage collection. */
5262 if (first_list != NULL)
5263 first_list->lv_used_prev = l;
5264 l->lv_used_prev = NULL;
5265 l->lv_used_next = first_list;
5266 first_list = l;
5267 }
5268 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005269}
5270
5271/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005272 * Allocate an empty list for a return value.
5273 * Returns OK or FAIL.
5274 */
5275 static int
5276rettv_list_alloc(rettv)
5277 typval_T *rettv;
5278{
5279 list_T *l = list_alloc();
5280
5281 if (l == NULL)
5282 return FAIL;
5283
5284 rettv->vval.v_list = l;
5285 rettv->v_type = VAR_LIST;
5286 ++l->lv_refcount;
5287 return OK;
5288}
5289
5290/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005291 * Unreference a list: decrement the reference count and free it when it
5292 * becomes zero.
5293 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005294 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005295list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005296 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005297{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005298 if (l != NULL && l->lv_refcount != DEL_REFCOUNT && --l->lv_refcount <= 0)
5299 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005300}
5301
5302/*
5303 * Free a list, including all items it points to.
5304 * Ignores the reference count.
5305 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005306 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005307list_free(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005308 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005309{
Bram Moolenaar33570922005-01-25 22:26:29 +00005310 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005311
Bram Moolenaard9fba312005-06-26 22:34:35 +00005312 /* Avoid that recursive reference to the list frees us again. */
5313 l->lv_refcount = DEL_REFCOUNT;
5314
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005315 /* Remove the list from the list of lists for garbage collection. */
5316 if (l->lv_used_prev == NULL)
5317 first_list = l->lv_used_next;
5318 else
5319 l->lv_used_prev->lv_used_next = l->lv_used_next;
5320 if (l->lv_used_next != NULL)
5321 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5322
Bram Moolenaard9fba312005-06-26 22:34:35 +00005323 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005324 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005325 /* Remove the item before deleting it. */
5326 l->lv_first = item->li_next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005327 listitem_free(item);
5328 }
5329 vim_free(l);
5330}
5331
5332/*
5333 * Allocate a list item.
5334 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005335 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005336listitem_alloc()
5337{
Bram Moolenaar33570922005-01-25 22:26:29 +00005338 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005339}
5340
5341/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005342 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005343 */
5344 static void
5345listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005346 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005347{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005348 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005349 vim_free(item);
5350}
5351
5352/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005353 * Remove a list item from a List and free it. Also clears the value.
5354 */
5355 static void
5356listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005357 list_T *l;
5358 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005359{
5360 list_remove(l, item, item);
5361 listitem_free(item);
5362}
5363
5364/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005365 * Get the number of items in a list.
5366 */
5367 static long
5368list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005369 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005370{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005371 if (l == NULL)
5372 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005373 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005374}
5375
5376/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005377 * Return TRUE when two lists have exactly the same values.
5378 */
5379 static int
5380list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005381 list_T *l1;
5382 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005383 int ic; /* ignore case for strings */
5384{
Bram Moolenaar33570922005-01-25 22:26:29 +00005385 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005386
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005387 if (list_len(l1) != list_len(l2))
5388 return FALSE;
5389
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005390 for (item1 = l1->lv_first, item2 = l2->lv_first;
5391 item1 != NULL && item2 != NULL;
5392 item1 = item1->li_next, item2 = item2->li_next)
5393 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5394 return FALSE;
5395 return item1 == NULL && item2 == NULL;
5396}
5397
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005398#if defined(FEAT_PYTHON) || defined(PROTO)
5399/*
5400 * Return the dictitem that an entry in a hashtable points to.
5401 */
5402 dictitem_T *
5403dict_lookup(hi)
5404 hashitem_T *hi;
5405{
5406 return HI2DI(hi);
5407}
5408#endif
5409
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005410/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005411 * Return TRUE when two dictionaries have exactly the same key/values.
5412 */
5413 static int
5414dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005415 dict_T *d1;
5416 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005417 int ic; /* ignore case for strings */
5418{
Bram Moolenaar33570922005-01-25 22:26:29 +00005419 hashitem_T *hi;
5420 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005421 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005422
5423 if (dict_len(d1) != dict_len(d2))
5424 return FALSE;
5425
Bram Moolenaar33570922005-01-25 22:26:29 +00005426 todo = d1->dv_hashtab.ht_used;
5427 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005428 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005429 if (!HASHITEM_EMPTY(hi))
5430 {
5431 item2 = dict_find(d2, hi->hi_key, -1);
5432 if (item2 == NULL)
5433 return FALSE;
5434 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5435 return FALSE;
5436 --todo;
5437 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005438 }
5439 return TRUE;
5440}
5441
5442/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005443 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005444 * Compares the items just like "==" would compare them, but strings and
5445 * numbers are different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005446 */
5447 static int
5448tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005449 typval_T *tv1;
5450 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005451 int ic; /* ignore case */
5452{
5453 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005454 char_u *s1, *s2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005455
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005456 if (tv1->v_type != tv2->v_type)
5457 return FALSE;
5458
5459 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005460 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005461 case VAR_LIST:
5462 /* recursive! */
5463 return list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5464
5465 case VAR_DICT:
5466 /* recursive! */
5467 return dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5468
5469 case VAR_FUNC:
5470 return (tv1->vval.v_string != NULL
5471 && tv2->vval.v_string != NULL
5472 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5473
5474 case VAR_NUMBER:
5475 return tv1->vval.v_number == tv2->vval.v_number;
5476
5477 case VAR_STRING:
5478 s1 = get_tv_string_buf(tv1, buf1);
5479 s2 = get_tv_string_buf(tv2, buf2);
5480 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005481 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005482
5483 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005484 return TRUE;
5485}
5486
5487/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005488 * Locate item with index "n" in list "l" and return it.
5489 * A negative index is counted from the end; -1 is the last item.
5490 * Returns NULL when "n" is out of range.
5491 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005492 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005493list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005494 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005495 long n;
5496{
Bram Moolenaar33570922005-01-25 22:26:29 +00005497 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005498 long idx;
5499
5500 if (l == NULL)
5501 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005502
5503 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005504 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005505 n = l->lv_len + n;
5506
5507 /* Check for index out of range. */
5508 if (n < 0 || n >= l->lv_len)
5509 return NULL;
5510
5511 /* When there is a cached index may start search from there. */
5512 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005513 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005514 if (n < l->lv_idx / 2)
5515 {
5516 /* closest to the start of the list */
5517 item = l->lv_first;
5518 idx = 0;
5519 }
5520 else if (n > (l->lv_idx + l->lv_len) / 2)
5521 {
5522 /* closest to the end of the list */
5523 item = l->lv_last;
5524 idx = l->lv_len - 1;
5525 }
5526 else
5527 {
5528 /* closest to the cached index */
5529 item = l->lv_idx_item;
5530 idx = l->lv_idx;
5531 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005532 }
5533 else
5534 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005535 if (n < l->lv_len / 2)
5536 {
5537 /* closest to the start of the list */
5538 item = l->lv_first;
5539 idx = 0;
5540 }
5541 else
5542 {
5543 /* closest to the end of the list */
5544 item = l->lv_last;
5545 idx = l->lv_len - 1;
5546 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005547 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005548
5549 while (n > idx)
5550 {
5551 /* search forward */
5552 item = item->li_next;
5553 ++idx;
5554 }
5555 while (n < idx)
5556 {
5557 /* search backward */
5558 item = item->li_prev;
5559 --idx;
5560 }
5561
5562 /* cache the used index */
5563 l->lv_idx = idx;
5564 l->lv_idx_item = item;
5565
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005566 return item;
5567}
5568
5569/*
Bram Moolenaara5525202006-03-02 22:52:09 +00005570 * Get list item "l[idx]" as a number.
5571 */
5572 static long
5573list_find_nr(l, idx, errorp)
5574 list_T *l;
5575 long idx;
5576 int *errorp; /* set to TRUE when something wrong */
5577{
5578 listitem_T *li;
5579
5580 li = list_find(l, idx);
5581 if (li == NULL)
5582 {
5583 if (errorp != NULL)
5584 *errorp = TRUE;
5585 return -1L;
5586 }
5587 return get_tv_number_chk(&li->li_tv, errorp);
5588}
5589
5590/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005591 * Locate "item" list "l" and return its index.
5592 * Returns -1 when "item" is not in the list.
5593 */
5594 static long
5595list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005596 list_T *l;
5597 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005598{
5599 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005600 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005601
5602 if (l == NULL)
5603 return -1;
5604 idx = 0;
5605 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5606 ++idx;
5607 if (li == NULL)
5608 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005609 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005610}
5611
5612/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005613 * Append item "item" to the end of list "l".
5614 */
5615 static void
5616list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005617 list_T *l;
5618 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005619{
5620 if (l->lv_last == NULL)
5621 {
5622 /* empty list */
5623 l->lv_first = item;
5624 l->lv_last = item;
5625 item->li_prev = NULL;
5626 }
5627 else
5628 {
5629 l->lv_last->li_next = item;
5630 item->li_prev = l->lv_last;
5631 l->lv_last = item;
5632 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005633 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005634 item->li_next = NULL;
5635}
5636
5637/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005638 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005639 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005640 */
5641 static int
5642list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005643 list_T *l;
5644 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005645{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005646 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005647
Bram Moolenaar05159a02005-02-26 23:04:13 +00005648 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005649 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005650 copy_tv(tv, &li->li_tv);
5651 list_append(l, li);
5652 return OK;
5653}
5654
5655/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005656 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005657 * Return FAIL when out of memory.
5658 */
5659 int
5660list_append_dict(list, dict)
5661 list_T *list;
5662 dict_T *dict;
5663{
5664 listitem_T *li = listitem_alloc();
5665
5666 if (li == NULL)
5667 return FAIL;
5668 li->li_tv.v_type = VAR_DICT;
5669 li->li_tv.v_lock = 0;
5670 li->li_tv.vval.v_dict = dict;
5671 list_append(list, li);
5672 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005673 return OK;
5674}
5675
5676/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005677 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00005678 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005679 * Returns FAIL when out of memory.
5680 */
5681 static int
Bram Moolenaar4463f292005-09-25 22:20:24 +00005682list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005683 list_T *l;
5684 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00005685 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005686{
5687 listitem_T *li = listitem_alloc();
5688
5689 if (li == NULL)
5690 return FAIL;
5691 list_append(l, li);
5692 li->li_tv.v_type = VAR_STRING;
5693 li->li_tv.v_lock = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005694 if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
5695 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005696 return FAIL;
5697 return OK;
5698}
5699
5700/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00005701 * Append "n" to list "l".
5702 * Returns FAIL when out of memory.
5703 */
5704 static int
5705list_append_number(l, n)
5706 list_T *l;
5707 varnumber_T n;
5708{
5709 listitem_T *li;
5710
5711 li = listitem_alloc();
5712 if (li == NULL)
5713 return FAIL;
5714 li->li_tv.v_type = VAR_NUMBER;
5715 li->li_tv.v_lock = 0;
5716 li->li_tv.vval.v_number = n;
5717 list_append(l, li);
5718 return OK;
5719}
5720
5721/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005722 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005723 * If "item" is NULL append at the end.
5724 * Return FAIL when out of memory.
5725 */
5726 static int
5727list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005728 list_T *l;
5729 typval_T *tv;
5730 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005731{
Bram Moolenaar33570922005-01-25 22:26:29 +00005732 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005733
5734 if (ni == NULL)
5735 return FAIL;
5736 copy_tv(tv, &ni->li_tv);
5737 if (item == NULL)
5738 /* Append new item at end of list. */
5739 list_append(l, ni);
5740 else
5741 {
5742 /* Insert new item before existing item. */
5743 ni->li_prev = item->li_prev;
5744 ni->li_next = item;
5745 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005746 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005747 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005748 ++l->lv_idx;
5749 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005750 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005751 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005752 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005753 l->lv_idx_item = NULL;
5754 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005755 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005756 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005757 }
5758 return OK;
5759}
5760
5761/*
5762 * Extend "l1" with "l2".
5763 * If "bef" is NULL append at the end, otherwise insert before this item.
5764 * Returns FAIL when out of memory.
5765 */
5766 static int
5767list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005768 list_T *l1;
5769 list_T *l2;
5770 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005771{
Bram Moolenaar33570922005-01-25 22:26:29 +00005772 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005773
5774 for (item = l2->lv_first; item != NULL; item = item->li_next)
5775 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5776 return FAIL;
5777 return OK;
5778}
5779
5780/*
5781 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5782 * Return FAIL when out of memory.
5783 */
5784 static int
5785list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005786 list_T *l1;
5787 list_T *l2;
5788 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005789{
Bram Moolenaar33570922005-01-25 22:26:29 +00005790 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005791
5792 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005793 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005794 if (l == NULL)
5795 return FAIL;
5796 tv->v_type = VAR_LIST;
5797 tv->vval.v_list = l;
5798
5799 /* append all items from the second list */
5800 return list_extend(l, l2, NULL);
5801}
5802
5803/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005804 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005805 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005806 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005807 * Returns NULL when out of memory.
5808 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005809 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005810list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005811 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005812 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005813 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005814{
Bram Moolenaar33570922005-01-25 22:26:29 +00005815 list_T *copy;
5816 listitem_T *item;
5817 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005818
5819 if (orig == NULL)
5820 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005821
5822 copy = list_alloc();
5823 if (copy != NULL)
5824 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005825 if (copyID != 0)
5826 {
5827 /* Do this before adding the items, because one of the items may
5828 * refer back to this list. */
5829 orig->lv_copyID = copyID;
5830 orig->lv_copylist = copy;
5831 }
5832 for (item = orig->lv_first; item != NULL && !got_int;
5833 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005834 {
5835 ni = listitem_alloc();
5836 if (ni == NULL)
5837 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005838 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005839 {
5840 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5841 {
5842 vim_free(ni);
5843 break;
5844 }
5845 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005846 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005847 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005848 list_append(copy, ni);
5849 }
5850 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005851 if (item != NULL)
5852 {
5853 list_unref(copy);
5854 copy = NULL;
5855 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005856 }
5857
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005858 return copy;
5859}
5860
5861/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005862 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005863 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005864 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005865 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005866list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005867 list_T *l;
5868 listitem_T *item;
5869 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005870{
Bram Moolenaar33570922005-01-25 22:26:29 +00005871 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005872
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005873 /* notify watchers */
5874 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005875 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005876 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005877 list_fix_watch(l, ip);
5878 if (ip == item2)
5879 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005880 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005881
5882 if (item2->li_next == NULL)
5883 l->lv_last = item->li_prev;
5884 else
5885 item2->li_next->li_prev = item->li_prev;
5886 if (item->li_prev == NULL)
5887 l->lv_first = item2->li_next;
5888 else
5889 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005890 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005891}
5892
5893/*
5894 * Return an allocated string with the string representation of a list.
5895 * May return NULL.
5896 */
5897 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005898list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005899 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005900 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005901{
5902 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005903
5904 if (tv->vval.v_list == NULL)
5905 return NULL;
5906 ga_init2(&ga, (int)sizeof(char), 80);
5907 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005908 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005909 {
5910 vim_free(ga.ga_data);
5911 return NULL;
5912 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005913 ga_append(&ga, ']');
5914 ga_append(&ga, NUL);
5915 return (char_u *)ga.ga_data;
5916}
5917
5918/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005919 * Join list "l" into a string in "*gap", using separator "sep".
5920 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005921 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005922 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005923 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005924list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005925 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00005926 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005927 char_u *sep;
5928 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005929 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005930{
5931 int first = TRUE;
5932 char_u *tofree;
5933 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00005934 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005935 char_u *s;
5936
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005937 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005938 {
5939 if (first)
5940 first = FALSE;
5941 else
5942 ga_concat(gap, sep);
5943
5944 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005945 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005946 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005947 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005948 if (s != NULL)
5949 ga_concat(gap, s);
5950 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005951 if (s == NULL)
5952 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005953 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005954 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005955}
5956
5957/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005958 * Garbage collection for lists and dictionaries.
5959 *
5960 * We use reference counts to be able to free most items right away when they
5961 * are no longer used. But for composite items it's possible that it becomes
5962 * unused while the reference count is > 0: When there is a recursive
5963 * reference. Example:
5964 * :let l = [1, 2, 3]
5965 * :let d = {9: l}
5966 * :let l[1] = d
5967 *
5968 * Since this is quite unusual we handle this with garbage collection: every
5969 * once in a while find out which lists and dicts are not referenced from any
5970 * variable.
5971 *
5972 * Here is a good reference text about garbage collection (refers to Python
5973 * but it applies to all reference-counting mechanisms):
5974 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005975 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005976
5977/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005978 * Do garbage collection for lists and dicts.
5979 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005980 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005981 int
5982garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00005983{
5984 dict_T *dd;
5985 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005986 int copyID = ++current_copyID;
5987 buf_T *buf;
5988 win_T *wp;
5989 int i;
5990 funccall_T *fc;
5991 int did_free = FALSE;
5992
5993 /*
5994 * 1. Go through all accessible variables and mark all lists and dicts
5995 * with copyID.
5996 */
5997 /* script-local variables */
5998 for (i = 1; i <= ga_scripts.ga_len; ++i)
5999 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6000
6001 /* buffer-local variables */
6002 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6003 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6004
6005 /* window-local variables */
6006 FOR_ALL_WINDOWS(wp)
6007 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6008
6009 /* global variables */
6010 set_ref_in_ht(&globvarht, copyID);
6011
6012 /* function-local variables */
6013 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6014 {
6015 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6016 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6017 }
6018
6019 /*
6020 * 2. Go through the list of dicts and free items without the copyID.
6021 */
6022 for (dd = first_dict; dd != NULL; )
6023 if (dd->dv_copyID != copyID)
6024 {
6025 dict_free(dd);
6026 did_free = TRUE;
6027
6028 /* restart, next dict may also have been freed */
6029 dd = first_dict;
6030 }
6031 else
6032 dd = dd->dv_used_next;
6033
6034 /*
6035 * 3. Go through the list of lists and free items without the copyID.
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006036 * But don't free a list that has a watcher (used in a for loop), these
6037 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006038 */
6039 for (ll = first_list; ll != NULL; )
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006040 if (ll->lv_copyID != copyID && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006041 {
6042 list_free(ll);
6043 did_free = TRUE;
6044
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006045 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006046 ll = first_list;
6047 }
6048 else
6049 ll = ll->lv_used_next;
6050
6051 return did_free;
6052}
6053
6054/*
6055 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6056 */
6057 static void
6058set_ref_in_ht(ht, copyID)
6059 hashtab_T *ht;
6060 int copyID;
6061{
6062 int todo;
6063 hashitem_T *hi;
6064
6065 todo = ht->ht_used;
6066 for (hi = ht->ht_array; todo > 0; ++hi)
6067 if (!HASHITEM_EMPTY(hi))
6068 {
6069 --todo;
6070 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6071 }
6072}
6073
6074/*
6075 * Mark all lists and dicts referenced through list "l" with "copyID".
6076 */
6077 static void
6078set_ref_in_list(l, copyID)
6079 list_T *l;
6080 int copyID;
6081{
6082 listitem_T *li;
6083
6084 for (li = l->lv_first; li != NULL; li = li->li_next)
6085 set_ref_in_item(&li->li_tv, copyID);
6086}
6087
6088/*
6089 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6090 */
6091 static void
6092set_ref_in_item(tv, copyID)
6093 typval_T *tv;
6094 int copyID;
6095{
6096 dict_T *dd;
6097 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006098
6099 switch (tv->v_type)
6100 {
6101 case VAR_DICT:
6102 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006103 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006104 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006105 /* Didn't see this dict yet. */
6106 dd->dv_copyID = copyID;
6107 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006108 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006109 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006110
6111 case VAR_LIST:
6112 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006113 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006114 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006115 /* Didn't see this list yet. */
6116 ll->lv_copyID = copyID;
6117 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006118 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006119 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006120 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006121 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006122}
6123
6124/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006125 * Allocate an empty header for a dictionary.
6126 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006127 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006128dict_alloc()
6129{
Bram Moolenaar33570922005-01-25 22:26:29 +00006130 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006131
Bram Moolenaar33570922005-01-25 22:26:29 +00006132 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006133 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006134 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006135 /* Add the list to the hashtable for garbage collection. */
6136 if (first_dict != NULL)
6137 first_dict->dv_used_prev = d;
6138 d->dv_used_next = first_dict;
6139 d->dv_used_prev = NULL;
6140
Bram Moolenaar33570922005-01-25 22:26:29 +00006141 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006142 d->dv_lock = 0;
6143 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006144 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006145 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006146 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006147}
6148
6149/*
6150 * Unreference a Dictionary: decrement the reference count and free it when it
6151 * becomes zero.
6152 */
6153 static void
6154dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006155 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006156{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006157 if (d != NULL && d->dv_refcount != DEL_REFCOUNT && --d->dv_refcount <= 0)
6158 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006159}
6160
6161/*
6162 * Free a Dictionary, including all items it contains.
6163 * Ignores the reference count.
6164 */
6165 static void
6166dict_free(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006167 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006168{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006169 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006170 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006171 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006172
Bram Moolenaard9fba312005-06-26 22:34:35 +00006173 /* Avoid that recursive reference to the dict frees us again. */
6174 d->dv_refcount = DEL_REFCOUNT;
6175
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006176 /* Remove the dict from the list of dicts for garbage collection. */
6177 if (d->dv_used_prev == NULL)
6178 first_dict = d->dv_used_next;
6179 else
6180 d->dv_used_prev->dv_used_next = d->dv_used_next;
6181 if (d->dv_used_next != NULL)
6182 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6183
6184 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006185 hash_lock(&d->dv_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +00006186 todo = d->dv_hashtab.ht_used;
6187 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006188 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006189 if (!HASHITEM_EMPTY(hi))
6190 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006191 /* Remove the item before deleting it, just in case there is
6192 * something recursive causing trouble. */
6193 di = HI2DI(hi);
6194 hash_remove(&d->dv_hashtab, hi);
6195 dictitem_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006196 --todo;
6197 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006198 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006199 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006200 vim_free(d);
6201}
6202
6203/*
6204 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006205 * The "key" is copied to the new item.
6206 * Note that the value of the item "di_tv" still needs to be initialized!
6207 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006208 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006209 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006210dictitem_alloc(key)
6211 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006212{
Bram Moolenaar33570922005-01-25 22:26:29 +00006213 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006214
Bram Moolenaar33570922005-01-25 22:26:29 +00006215 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(key));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006216 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006217 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006218 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006219 di->di_flags = 0;
6220 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006221 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006222}
6223
6224/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006225 * Make a copy of a Dictionary item.
6226 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006227 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006228dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006229 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006230{
Bram Moolenaar33570922005-01-25 22:26:29 +00006231 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006232
Bram Moolenaar33570922005-01-25 22:26:29 +00006233 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(org->di_key));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006234 if (di != NULL)
6235 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006236 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006237 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006238 copy_tv(&org->di_tv, &di->di_tv);
6239 }
6240 return di;
6241}
6242
6243/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006244 * Remove item "item" from Dictionary "dict" and free it.
6245 */
6246 static void
6247dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006248 dict_T *dict;
6249 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006250{
Bram Moolenaar33570922005-01-25 22:26:29 +00006251 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006252
Bram Moolenaar33570922005-01-25 22:26:29 +00006253 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006254 if (HASHITEM_EMPTY(hi))
6255 EMSG2(_(e_intern2), "dictitem_remove()");
6256 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006257 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006258 dictitem_free(item);
6259}
6260
6261/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006262 * Free a dict item. Also clears the value.
6263 */
6264 static void
6265dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006266 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006267{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006268 clear_tv(&item->di_tv);
6269 vim_free(item);
6270}
6271
6272/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006273 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6274 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006275 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006276 * Returns NULL when out of memory.
6277 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006278 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006279dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006280 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006281 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006282 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006283{
Bram Moolenaar33570922005-01-25 22:26:29 +00006284 dict_T *copy;
6285 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006286 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006287 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006288
6289 if (orig == NULL)
6290 return NULL;
6291
6292 copy = dict_alloc();
6293 if (copy != NULL)
6294 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006295 if (copyID != 0)
6296 {
6297 orig->dv_copyID = copyID;
6298 orig->dv_copydict = copy;
6299 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006300 todo = orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006301 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006302 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006303 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006304 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006305 --todo;
6306
6307 di = dictitem_alloc(hi->hi_key);
6308 if (di == NULL)
6309 break;
6310 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006311 {
6312 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6313 copyID) == FAIL)
6314 {
6315 vim_free(di);
6316 break;
6317 }
6318 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006319 else
6320 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6321 if (dict_add(copy, di) == FAIL)
6322 {
6323 dictitem_free(di);
6324 break;
6325 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006326 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006327 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006328
Bram Moolenaare9a41262005-01-15 22:18:47 +00006329 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006330 if (todo > 0)
6331 {
6332 dict_unref(copy);
6333 copy = NULL;
6334 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006335 }
6336
6337 return copy;
6338}
6339
6340/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006341 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006342 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006343 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006344 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006345dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006346 dict_T *d;
6347 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006348{
Bram Moolenaar33570922005-01-25 22:26:29 +00006349 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006350}
6351
Bram Moolenaar8c711452005-01-14 21:53:12 +00006352/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006353 * Add a number or string entry to dictionary "d".
6354 * When "str" is NULL use number "nr", otherwise use "str".
6355 * Returns FAIL when out of memory and when key already exists.
6356 */
6357 int
6358dict_add_nr_str(d, key, nr, str)
6359 dict_T *d;
6360 char *key;
6361 long nr;
6362 char_u *str;
6363{
6364 dictitem_T *item;
6365
6366 item = dictitem_alloc((char_u *)key);
6367 if (item == NULL)
6368 return FAIL;
6369 item->di_tv.v_lock = 0;
6370 if (str == NULL)
6371 {
6372 item->di_tv.v_type = VAR_NUMBER;
6373 item->di_tv.vval.v_number = nr;
6374 }
6375 else
6376 {
6377 item->di_tv.v_type = VAR_STRING;
6378 item->di_tv.vval.v_string = vim_strsave(str);
6379 }
6380 if (dict_add(d, item) == FAIL)
6381 {
6382 dictitem_free(item);
6383 return FAIL;
6384 }
6385 return OK;
6386}
6387
6388/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006389 * Get the number of items in a Dictionary.
6390 */
6391 static long
6392dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006393 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006394{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006395 if (d == NULL)
6396 return 0L;
Bram Moolenaar33570922005-01-25 22:26:29 +00006397 return d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006398}
6399
6400/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006401 * Find item "key[len]" in Dictionary "d".
6402 * If "len" is negative use strlen(key).
6403 * Returns NULL when not found.
6404 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006405 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006406dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006407 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006408 char_u *key;
6409 int len;
6410{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006411#define AKEYLEN 200
6412 char_u buf[AKEYLEN];
6413 char_u *akey;
6414 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006415 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006416
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006417 if (len < 0)
6418 akey = key;
6419 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006420 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006421 tofree = akey = vim_strnsave(key, len);
6422 if (akey == NULL)
6423 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006424 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006425 else
6426 {
6427 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006428 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006429 akey = buf;
6430 }
6431
Bram Moolenaar33570922005-01-25 22:26:29 +00006432 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006433 vim_free(tofree);
6434 if (HASHITEM_EMPTY(hi))
6435 return NULL;
6436 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006437}
6438
6439/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006440 * Get a string item from a dictionary.
6441 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00006442 * Returns NULL if the entry doesn't exist or out of memory.
6443 */
6444 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006445get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00006446 dict_T *d;
6447 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006448 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006449{
6450 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006451 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006452
6453 di = dict_find(d, key, -1);
6454 if (di == NULL)
6455 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006456 s = get_tv_string(&di->di_tv);
6457 if (save && s != NULL)
6458 s = vim_strsave(s);
6459 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006460}
6461
6462/*
6463 * Get a number item from a dictionary.
6464 * Returns 0 if the entry doesn't exist or out of memory.
6465 */
6466 long
6467get_dict_number(d, key)
6468 dict_T *d;
6469 char_u *key;
6470{
6471 dictitem_T *di;
6472
6473 di = dict_find(d, key, -1);
6474 if (di == NULL)
6475 return 0;
6476 return get_tv_number(&di->di_tv);
6477}
6478
6479/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006480 * Return an allocated string with the string representation of a Dictionary.
6481 * May return NULL.
6482 */
6483 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006484dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006485 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006486 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006487{
6488 garray_T ga;
6489 int first = TRUE;
6490 char_u *tofree;
6491 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006492 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006493 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006494 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006495 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006496
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006497 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006498 return NULL;
6499 ga_init2(&ga, (int)sizeof(char), 80);
6500 ga_append(&ga, '{');
6501
Bram Moolenaar33570922005-01-25 22:26:29 +00006502 todo = d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006503 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006504 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006505 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006506 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006507 --todo;
6508
6509 if (first)
6510 first = FALSE;
6511 else
6512 ga_concat(&ga, (char_u *)", ");
6513
6514 tofree = string_quote(hi->hi_key, FALSE);
6515 if (tofree != NULL)
6516 {
6517 ga_concat(&ga, tofree);
6518 vim_free(tofree);
6519 }
6520 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006521 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006522 if (s != NULL)
6523 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006524 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006525 if (s == NULL)
6526 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006527 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006528 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006529 if (todo > 0)
6530 {
6531 vim_free(ga.ga_data);
6532 return NULL;
6533 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006534
6535 ga_append(&ga, '}');
6536 ga_append(&ga, NUL);
6537 return (char_u *)ga.ga_data;
6538}
6539
6540/*
6541 * Allocate a variable for a Dictionary and fill it from "*arg".
6542 * Return OK or FAIL. Returns NOTDONE for {expr}.
6543 */
6544 static int
6545get_dict_tv(arg, rettv, evaluate)
6546 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006547 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006548 int evaluate;
6549{
Bram Moolenaar33570922005-01-25 22:26:29 +00006550 dict_T *d = NULL;
6551 typval_T tvkey;
6552 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006553 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00006554 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006555 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006556 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00006557
6558 /*
6559 * First check if it's not a curly-braces thing: {expr}.
6560 * Must do this without evaluating, otherwise a function may be called
6561 * twice. Unfortunately this means we need to call eval1() twice for the
6562 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006563 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006564 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006565 if (*start != '}')
6566 {
6567 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
6568 return FAIL;
6569 if (*start == '}')
6570 return NOTDONE;
6571 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006572
6573 if (evaluate)
6574 {
6575 d = dict_alloc();
6576 if (d == NULL)
6577 return FAIL;
6578 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006579 tvkey.v_type = VAR_UNKNOWN;
6580 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006581
6582 *arg = skipwhite(*arg + 1);
6583 while (**arg != '}' && **arg != NUL)
6584 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006585 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006586 goto failret;
6587 if (**arg != ':')
6588 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006589 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006590 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006591 goto failret;
6592 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006593 key = get_tv_string_buf_chk(&tvkey, buf);
6594 if (key == NULL || *key == NUL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006595 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006596 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6597 if (key != NULL)
6598 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006599 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006600 goto failret;
6601 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006602
6603 *arg = skipwhite(*arg + 1);
6604 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6605 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006606 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006607 goto failret;
6608 }
6609 if (evaluate)
6610 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006611 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006612 if (item != NULL)
6613 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00006614 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006615 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006616 clear_tv(&tv);
6617 goto failret;
6618 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006619 item = dictitem_alloc(key);
6620 clear_tv(&tvkey);
6621 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006622 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006623 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006624 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006625 if (dict_add(d, item) == FAIL)
6626 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006627 }
6628 }
6629
6630 if (**arg == '}')
6631 break;
6632 if (**arg != ',')
6633 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006634 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006635 goto failret;
6636 }
6637 *arg = skipwhite(*arg + 1);
6638 }
6639
6640 if (**arg != '}')
6641 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006642 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006643failret:
6644 if (evaluate)
6645 dict_free(d);
6646 return FAIL;
6647 }
6648
6649 *arg = skipwhite(*arg + 1);
6650 if (evaluate)
6651 {
6652 rettv->v_type = VAR_DICT;
6653 rettv->vval.v_dict = d;
6654 ++d->dv_refcount;
6655 }
6656
6657 return OK;
6658}
6659
Bram Moolenaar8c711452005-01-14 21:53:12 +00006660/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006661 * Return a string with the string representation of a variable.
6662 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006663 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006664 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006665 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006666 * May return NULL;
6667 */
6668 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006669echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006670 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006671 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006672 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006673 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006674{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006675 static int recurse = 0;
6676 char_u *r = NULL;
6677
Bram Moolenaar33570922005-01-25 22:26:29 +00006678 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006679 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006680 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006681 *tofree = NULL;
6682 return NULL;
6683 }
6684 ++recurse;
6685
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006686 switch (tv->v_type)
6687 {
6688 case VAR_FUNC:
6689 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006690 r = tv->vval.v_string;
6691 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006692
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006693 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006694 if (tv->vval.v_list == NULL)
6695 {
6696 *tofree = NULL;
6697 r = NULL;
6698 }
6699 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
6700 {
6701 *tofree = NULL;
6702 r = (char_u *)"[...]";
6703 }
6704 else
6705 {
6706 tv->vval.v_list->lv_copyID = copyID;
6707 *tofree = list2string(tv, copyID);
6708 r = *tofree;
6709 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006710 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006711
Bram Moolenaar8c711452005-01-14 21:53:12 +00006712 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006713 if (tv->vval.v_dict == NULL)
6714 {
6715 *tofree = NULL;
6716 r = NULL;
6717 }
6718 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
6719 {
6720 *tofree = NULL;
6721 r = (char_u *)"{...}";
6722 }
6723 else
6724 {
6725 tv->vval.v_dict->dv_copyID = copyID;
6726 *tofree = dict2string(tv, copyID);
6727 r = *tofree;
6728 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006729 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006730
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006731 case VAR_STRING:
6732 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006733 *tofree = NULL;
6734 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006735 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006736
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006737 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006738 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006739 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006740 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006741
6742 --recurse;
6743 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006744}
6745
6746/*
6747 * Return a string with the string representation of a variable.
6748 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6749 * "numbuf" is used for a number.
6750 * Puts quotes around strings, so that they can be parsed back by eval().
6751 * May return NULL;
6752 */
6753 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006754tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006755 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006756 char_u **tofree;
6757 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006758 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006759{
6760 switch (tv->v_type)
6761 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006762 case VAR_FUNC:
6763 *tofree = string_quote(tv->vval.v_string, TRUE);
6764 return *tofree;
6765 case VAR_STRING:
6766 *tofree = string_quote(tv->vval.v_string, FALSE);
6767 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006768 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006769 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006770 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006771 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006772 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006773 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006774 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006775 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006776}
6777
6778/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006779 * Return string "str" in ' quotes, doubling ' characters.
6780 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006781 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006782 */
6783 static char_u *
6784string_quote(str, function)
6785 char_u *str;
6786 int function;
6787{
Bram Moolenaar33570922005-01-25 22:26:29 +00006788 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006789 char_u *p, *r, *s;
6790
Bram Moolenaar33570922005-01-25 22:26:29 +00006791 len = (function ? 13 : 3);
6792 if (str != NULL)
6793 {
6794 len += STRLEN(str);
6795 for (p = str; *p != NUL; mb_ptr_adv(p))
6796 if (*p == '\'')
6797 ++len;
6798 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006799 s = r = alloc(len);
6800 if (r != NULL)
6801 {
6802 if (function)
6803 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006804 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006805 r += 10;
6806 }
6807 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006808 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006809 if (str != NULL)
6810 for (p = str; *p != NUL; )
6811 {
6812 if (*p == '\'')
6813 *r++ = '\'';
6814 MB_COPY_CHAR(p, r);
6815 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006816 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006817 if (function)
6818 *r++ = ')';
6819 *r++ = NUL;
6820 }
6821 return s;
6822}
6823
6824/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825 * Get the value of an environment variable.
6826 * "arg" is pointing to the '$'. It is advanced to after the name.
6827 * If the environment variable was not set, silently assume it is empty.
6828 * Always return OK.
6829 */
6830 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006831get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006833 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 int evaluate;
6835{
6836 char_u *string = NULL;
6837 int len;
6838 int cc;
6839 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006840 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841
6842 ++*arg;
6843 name = *arg;
6844 len = get_env_len(arg);
6845 if (evaluate)
6846 {
6847 if (len != 0)
6848 {
6849 cc = name[len];
6850 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006851 /* first try vim_getenv(), fast for normal environment vars */
6852 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006854 {
6855 if (!mustfree)
6856 string = vim_strsave(string);
6857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858 else
6859 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00006860 if (mustfree)
6861 vim_free(string);
6862
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 /* next try expanding things like $VIM and ${HOME} */
6864 string = expand_env_save(name - 1);
6865 if (string != NULL && *string == '$')
6866 {
6867 vim_free(string);
6868 string = NULL;
6869 }
6870 }
6871 name[len] = cc;
6872 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006873 rettv->v_type = VAR_STRING;
6874 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875 }
6876
6877 return OK;
6878}
6879
6880/*
6881 * Array with names and number of arguments of all internal functions
6882 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
6883 */
6884static struct fst
6885{
6886 char *f_name; /* function name */
6887 char f_min_argc; /* minimal number of arguments */
6888 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00006889 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006890 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891} functions[] =
6892{
Bram Moolenaar0d660222005-01-07 21:51:51 +00006893 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894 {"append", 2, 2, f_append},
6895 {"argc", 0, 0, f_argc},
6896 {"argidx", 0, 0, f_argidx},
6897 {"argv", 1, 1, f_argv},
6898 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006899 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900 {"bufexists", 1, 1, f_bufexists},
6901 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
6902 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
6903 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
6904 {"buflisted", 1, 1, f_buflisted},
6905 {"bufloaded", 1, 1, f_bufloaded},
6906 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006907 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 {"bufwinnr", 1, 1, f_bufwinnr},
6909 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006910 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006911 {"call", 2, 3, f_call},
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00006912 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913 {"char2nr", 1, 1, f_char2nr},
6914 {"cindent", 1, 1, f_cindent},
6915 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00006916#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00006917 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00006918 {"complete_add", 1, 1, f_complete_add},
6919 {"complete_check", 0, 0, f_complete_check},
6920#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006922 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006923 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00006925 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006926 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927 {"delete", 1, 1, f_delete},
6928 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00006929 {"diff_filler", 1, 1, f_diff_filler},
6930 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006931 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006933 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934 {"eventhandler", 0, 0, f_eventhandler},
6935 {"executable", 1, 1, f_executable},
6936 {"exists", 1, 1, f_exists},
6937 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006938 {"extend", 2, 3, f_extend},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
6940 {"filereadable", 1, 1, f_filereadable},
6941 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006942 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006943 {"finddir", 1, 3, f_finddir},
6944 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 {"fnamemodify", 2, 2, f_fnamemodify},
6946 {"foldclosed", 1, 1, f_foldclosed},
6947 {"foldclosedend", 1, 1, f_foldclosedend},
6948 {"foldlevel", 1, 1, f_foldlevel},
6949 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006950 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006952 {"function", 1, 1, f_function},
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006953 {"garbagecollect", 0, 0, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006954 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00006955 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 {"getbufvar", 2, 2, f_getbufvar},
6957 {"getchar", 0, 1, f_getchar},
6958 {"getcharmod", 0, 0, f_getcharmod},
6959 {"getcmdline", 0, 0, f_getcmdline},
6960 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006961 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006963 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006964 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 {"getfsize", 1, 1, f_getfsize},
6966 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006967 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006968 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00006969 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaara5525202006-03-02 22:52:09 +00006970 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00006971 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006972 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 {"getregtype", 0, 1, f_getregtype},
6974 {"getwinposx", 0, 0, f_getwinposx},
6975 {"getwinposy", 0, 0, f_getwinposy},
6976 {"getwinvar", 2, 2, f_getwinvar},
6977 {"glob", 1, 1, f_glob},
6978 {"globpath", 2, 2, f_globpath},
6979 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006980 {"has_key", 2, 2, f_has_key},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981 {"hasmapto", 1, 2, f_hasmapto},
6982 {"highlightID", 1, 1, f_hlID}, /* obsolete */
6983 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
6984 {"histadd", 2, 2, f_histadd},
6985 {"histdel", 1, 2, f_histdel},
6986 {"histget", 1, 2, f_histget},
6987 {"histnr", 1, 1, f_histnr},
6988 {"hlID", 1, 1, f_hlID},
6989 {"hlexists", 1, 1, f_hlexists},
6990 {"hostname", 0, 0, f_hostname},
6991 {"iconv", 3, 3, f_iconv},
6992 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006993 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006994 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00006996 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997 {"inputrestore", 0, 0, f_inputrestore},
6998 {"inputsave", 0, 0, f_inputsave},
6999 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007000 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007002 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007003 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007004 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007005 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007007 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 {"libcall", 3, 3, f_libcall},
7009 {"libcallnr", 3, 3, f_libcallnr},
7010 {"line", 1, 1, f_line},
7011 {"line2byte", 1, 1, f_line2byte},
7012 {"lispindent", 1, 1, f_lispindent},
7013 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007014 {"map", 2, 2, f_map},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015 {"maparg", 1, 2, f_maparg},
7016 {"mapcheck", 1, 2, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007017 {"match", 2, 4, f_match},
7018 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007019 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007020 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007021 {"max", 1, 1, f_max},
7022 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007023#ifdef vim_mkdir
7024 {"mkdir", 1, 3, f_mkdir},
7025#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 {"mode", 0, 0, f_mode},
7027 {"nextnonblank", 1, 1, f_nextnonblank},
7028 {"nr2char", 1, 1, f_nr2char},
7029 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007030 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007031 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007032 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007033 {"readfile", 1, 3, f_readfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034 {"remote_expr", 2, 3, f_remote_expr},
7035 {"remote_foreground", 1, 1, f_remote_foreground},
7036 {"remote_peek", 1, 2, f_remote_peek},
7037 {"remote_read", 1, 1, f_remote_read},
7038 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007039 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007041 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007043 {"reverse", 1, 1, f_reverse},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00007044 {"search", 1, 3, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007045 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00007046 {"searchpair", 3, 6, f_searchpair},
7047 {"searchpairpos", 3, 6, f_searchpairpos},
7048 {"searchpos", 1, 3, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049 {"server2client", 2, 2, f_server2client},
7050 {"serverlist", 0, 0, f_serverlist},
7051 {"setbufvar", 3, 3, f_setbufvar},
7052 {"setcmdpos", 1, 1, f_setcmdpos},
7053 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007054 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007055 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007056 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 {"setreg", 2, 3, f_setreg},
7058 {"setwinvar", 3, 3, f_setwinvar},
7059 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007060 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007061 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007062 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007063 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007064 {"split", 1, 3, f_split},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065#ifdef HAVE_STRFTIME
7066 {"strftime", 1, 2, f_strftime},
7067#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007068 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007069 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 {"strlen", 1, 1, f_strlen},
7071 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007072 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073 {"strtrans", 1, 1, f_strtrans},
7074 {"submatch", 1, 1, f_submatch},
7075 {"substitute", 4, 4, f_substitute},
7076 {"synID", 3, 3, f_synID},
7077 {"synIDattr", 2, 3, f_synIDattr},
7078 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007079 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007080 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007081 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007082 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007083 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007084 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007086 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087 {"tolower", 1, 1, f_tolower},
7088 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007089 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007091 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092 {"virtcol", 1, 1, f_virtcol},
7093 {"visualmode", 0, 1, f_visualmode},
7094 {"winbufnr", 1, 1, f_winbufnr},
7095 {"wincol", 0, 0, f_wincol},
7096 {"winheight", 1, 1, f_winheight},
7097 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007098 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007100 {"winrestview", 1, 1, f_winrestview},
7101 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007103 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104};
7105
7106#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7107
7108/*
7109 * Function given to ExpandGeneric() to obtain the list of internal
7110 * or user defined function names.
7111 */
7112 char_u *
7113get_function_name(xp, idx)
7114 expand_T *xp;
7115 int idx;
7116{
7117 static int intidx = -1;
7118 char_u *name;
7119
7120 if (idx == 0)
7121 intidx = -1;
7122 if (intidx < 0)
7123 {
7124 name = get_user_func_name(xp, idx);
7125 if (name != NULL)
7126 return name;
7127 }
7128 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7129 {
7130 STRCPY(IObuff, functions[intidx].f_name);
7131 STRCAT(IObuff, "(");
7132 if (functions[intidx].f_max_argc == 0)
7133 STRCAT(IObuff, ")");
7134 return IObuff;
7135 }
7136
7137 return NULL;
7138}
7139
7140/*
7141 * Function given to ExpandGeneric() to obtain the list of internal or
7142 * user defined variable or function names.
7143 */
7144/*ARGSUSED*/
7145 char_u *
7146get_expr_name(xp, idx)
7147 expand_T *xp;
7148 int idx;
7149{
7150 static int intidx = -1;
7151 char_u *name;
7152
7153 if (idx == 0)
7154 intidx = -1;
7155 if (intidx < 0)
7156 {
7157 name = get_function_name(xp, idx);
7158 if (name != NULL)
7159 return name;
7160 }
7161 return get_user_var_name(xp, ++intidx);
7162}
7163
7164#endif /* FEAT_CMDL_COMPL */
7165
7166/*
7167 * Find internal function in table above.
7168 * Return index, or -1 if not found
7169 */
7170 static int
7171find_internal_func(name)
7172 char_u *name; /* name of the function */
7173{
7174 int first = 0;
7175 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7176 int cmp;
7177 int x;
7178
7179 /*
7180 * Find the function name in the table. Binary search.
7181 */
7182 while (first <= last)
7183 {
7184 x = first + ((unsigned)(last - first) >> 1);
7185 cmp = STRCMP(name, functions[x].f_name);
7186 if (cmp < 0)
7187 last = x - 1;
7188 else if (cmp > 0)
7189 first = x + 1;
7190 else
7191 return x;
7192 }
7193 return -1;
7194}
7195
7196/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007197 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7198 * name it contains, otherwise return "name".
7199 */
7200 static char_u *
7201deref_func_name(name, lenp)
7202 char_u *name;
7203 int *lenp;
7204{
Bram Moolenaar33570922005-01-25 22:26:29 +00007205 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007206 int cc;
7207
7208 cc = name[*lenp];
7209 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007210 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007211 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007212 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007213 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007214 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007215 {
7216 *lenp = 0;
7217 return (char_u *)""; /* just in case */
7218 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007219 *lenp = STRLEN(v->di_tv.vval.v_string);
7220 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007221 }
7222
7223 return name;
7224}
7225
7226/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227 * Allocate a variable for the result of a function.
7228 * Return OK or FAIL.
7229 */
7230 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007231get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7232 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 char_u *name; /* name of the function */
7234 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007235 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236 char_u **arg; /* argument, pointing to the '(' */
7237 linenr_T firstline; /* first line of range */
7238 linenr_T lastline; /* last line of range */
7239 int *doesrange; /* return: function handled range */
7240 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007241 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242{
7243 char_u *argp;
7244 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00007245 typval_T argvars[MAX_FUNC_ARGS]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246 int argcount = 0; /* number of arguments found */
7247
7248 /*
7249 * Get the arguments.
7250 */
7251 argp = *arg;
7252 while (argcount < MAX_FUNC_ARGS)
7253 {
7254 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7255 if (*argp == ')' || *argp == ',' || *argp == NUL)
7256 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7258 {
7259 ret = FAIL;
7260 break;
7261 }
7262 ++argcount;
7263 if (*argp != ',')
7264 break;
7265 }
7266 if (*argp == ')')
7267 ++argp;
7268 else
7269 ret = FAIL;
7270
7271 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007272 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007273 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007275 {
7276 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007277 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007278 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007279 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281
7282 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007283 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284
7285 *arg = skipwhite(argp);
7286 return ret;
7287}
7288
7289
7290/*
7291 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00007292 * Return OK when the function can't be called, FAIL otherwise.
7293 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294 */
7295 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007296call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007297 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 char_u *name; /* name of the function */
7299 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007300 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301 int argcount; /* number of "argvars" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007302 typval_T *argvars; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303 linenr_T firstline; /* first line of range */
7304 linenr_T lastline; /* last line of range */
7305 int *doesrange; /* return: function handled range */
7306 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007307 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308{
7309 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310#define ERROR_UNKNOWN 0
7311#define ERROR_TOOMANY 1
7312#define ERROR_TOOFEW 2
7313#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007314#define ERROR_DICT 4
7315#define ERROR_NONE 5
7316#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317 int error = ERROR_NONE;
7318 int i;
7319 int llen;
7320 ufunc_T *fp;
7321 int cc;
7322#define FLEN_FIXED 40
7323 char_u fname_buf[FLEN_FIXED + 1];
7324 char_u *fname;
7325
7326 /*
7327 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7328 * Change <SNR>123_name() to K_SNR 123_name().
7329 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7330 */
7331 cc = name[len];
7332 name[len] = NUL;
7333 llen = eval_fname_script(name);
7334 if (llen > 0)
7335 {
7336 fname_buf[0] = K_SPECIAL;
7337 fname_buf[1] = KS_EXTRA;
7338 fname_buf[2] = (int)KE_SNR;
7339 i = 3;
7340 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7341 {
7342 if (current_SID <= 0)
7343 error = ERROR_SCRIPT;
7344 else
7345 {
7346 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7347 i = (int)STRLEN(fname_buf);
7348 }
7349 }
7350 if (i + STRLEN(name + llen) < FLEN_FIXED)
7351 {
7352 STRCPY(fname_buf + i, name + llen);
7353 fname = fname_buf;
7354 }
7355 else
7356 {
7357 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7358 if (fname == NULL)
7359 error = ERROR_OTHER;
7360 else
7361 {
7362 mch_memmove(fname, fname_buf, (size_t)i);
7363 STRCPY(fname + i, name + llen);
7364 }
7365 }
7366 }
7367 else
7368 fname = name;
7369
7370 *doesrange = FALSE;
7371
7372
7373 /* execute the function if no errors detected and executing */
7374 if (evaluate && error == ERROR_NONE)
7375 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007376 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377 error = ERROR_UNKNOWN;
7378
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007379 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 {
7381 /*
7382 * User defined function.
7383 */
7384 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007385
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007387 /* Trigger FuncUndefined event, may load the function. */
7388 if (fp == NULL
7389 && apply_autocmds(EVENT_FUNCUNDEFINED,
7390 fname, fname, TRUE, NULL)
7391 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007393 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394 fp = find_func(fname);
7395 }
7396#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007397 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007398 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007399 {
7400 /* loaded a package, search for the function again */
7401 fp = find_func(fname);
7402 }
7403
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 if (fp != NULL)
7405 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007406 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007408 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007410 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007412 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007413 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414 else
7415 {
7416 /*
7417 * Call the user function.
7418 * Save and restore search patterns, script variables and
7419 * redo buffer.
7420 */
7421 save_search_patterns();
7422 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007423 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007424 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007425 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007426 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7427 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7428 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007429 /* Function was unreferenced while being used, free it
7430 * now. */
7431 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432 restoreRedobuff();
7433 restore_search_patterns();
7434 error = ERROR_NONE;
7435 }
7436 }
7437 }
7438 else
7439 {
7440 /*
7441 * Find the function name in the table, call its implementation.
7442 */
7443 i = find_internal_func(fname);
7444 if (i >= 0)
7445 {
7446 if (argcount < functions[i].f_min_argc)
7447 error = ERROR_TOOFEW;
7448 else if (argcount > functions[i].f_max_argc)
7449 error = ERROR_TOOMANY;
7450 else
7451 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007452 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007453 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454 error = ERROR_NONE;
7455 }
7456 }
7457 }
7458 /*
7459 * The function call (or "FuncUndefined" autocommand sequence) might
7460 * have been aborted by an error, an interrupt, or an explicitly thrown
7461 * exception that has not been caught so far. This situation can be
7462 * tested for by calling aborting(). For an error in an internal
7463 * function or for the "E132" error in call_user_func(), however, the
7464 * throw point at which the "force_abort" flag (temporarily reset by
7465 * emsg()) is normally updated has not been reached yet. We need to
7466 * update that flag first to make aborting() reliable.
7467 */
7468 update_force_abort();
7469 }
7470 if (error == ERROR_NONE)
7471 ret = OK;
7472
7473 /*
7474 * Report an error unless the argument evaluation or function call has been
7475 * cancelled due to an aborting error, an interrupt, or an exception.
7476 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007477 if (!aborting())
7478 {
7479 switch (error)
7480 {
7481 case ERROR_UNKNOWN:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007482 emsg_funcname("E117: Unknown function: %s", name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007483 break;
7484 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007485 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007486 break;
7487 case ERROR_TOOFEW:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007488 emsg_funcname("E119: Not enough arguments for function: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007489 name);
7490 break;
7491 case ERROR_SCRIPT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007492 emsg_funcname("E120: Using <SID> not in a script context: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007493 name);
7494 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007495 case ERROR_DICT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007496 emsg_funcname("E725: Calling dict function without Dictionary: %s",
Bram Moolenaare9a41262005-01-15 22:18:47 +00007497 name);
7498 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007499 }
7500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501
7502 name[len] = cc;
7503 if (fname != name && fname != fname_buf)
7504 vim_free(fname);
7505
7506 return ret;
7507}
7508
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007509/*
7510 * Give an error message with a function name. Handle <SNR> things.
7511 */
7512 static void
7513emsg_funcname(msg, name)
7514 char *msg;
7515 char_u *name;
7516{
7517 char_u *p;
7518
7519 if (*name == K_SPECIAL)
7520 p = concat_str((char_u *)"<SNR>", name + 3);
7521 else
7522 p = name;
7523 EMSG2(_(msg), p);
7524 if (p != name)
7525 vim_free(p);
7526}
7527
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528/*********************************************
7529 * Implementation of the built-in functions
7530 */
7531
7532/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007533 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534 */
7535 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007536f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007537 typval_T *argvars;
7538 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539{
Bram Moolenaar33570922005-01-25 22:26:29 +00007540 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007542 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007543 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007545 if ((l = argvars[0].vval.v_list) != NULL
7546 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7547 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007548 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007549 }
7550 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007551 EMSG(_(e_listreq));
7552}
7553
7554/*
7555 * "append(lnum, string/list)" function
7556 */
7557 static void
7558f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007559 typval_T *argvars;
7560 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007561{
7562 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007563 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007564 list_T *l = NULL;
7565 listitem_T *li = NULL;
7566 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007567 long added = 0;
7568
Bram Moolenaar0d660222005-01-07 21:51:51 +00007569 lnum = get_tv_lnum(argvars);
7570 if (lnum >= 0
7571 && lnum <= curbuf->b_ml.ml_line_count
7572 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007573 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007574 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007575 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007576 l = argvars[1].vval.v_list;
7577 if (l == NULL)
7578 return;
7579 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007580 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007581 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007582 for (;;)
7583 {
7584 if (l == NULL)
7585 tv = &argvars[1]; /* append a string */
7586 else if (li == NULL)
7587 break; /* end of list */
7588 else
7589 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007590 line = get_tv_string_chk(tv);
7591 if (line == NULL) /* type error */
7592 {
7593 rettv->vval.v_number = 1; /* Failed */
7594 break;
7595 }
7596 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007597 ++added;
7598 if (l == NULL)
7599 break;
7600 li = li->li_next;
7601 }
7602
7603 appended_lines_mark(lnum, added);
7604 if (curwin->w_cursor.lnum > lnum)
7605 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007607 else
7608 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609}
7610
7611/*
7612 * "argc()" function
7613 */
7614/* ARGSUSED */
7615 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007616f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007617 typval_T *argvars;
7618 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007620 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621}
7622
7623/*
7624 * "argidx()" function
7625 */
7626/* ARGSUSED */
7627 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007628f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007629 typval_T *argvars;
7630 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007631{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007632 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633}
7634
7635/*
7636 * "argv(nr)" function
7637 */
7638 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007639f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007640 typval_T *argvars;
7641 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642{
7643 int idx;
7644
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007645 idx = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646 if (idx >= 0 && idx < ARGCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007647 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007649 rettv->vval.v_string = NULL;
7650 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651}
7652
7653/*
7654 * "browse(save, title, initdir, default)" function
7655 */
7656/* ARGSUSED */
7657 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007658f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007659 typval_T *argvars;
7660 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661{
7662#ifdef FEAT_BROWSE
7663 int save;
7664 char_u *title;
7665 char_u *initdir;
7666 char_u *defname;
7667 char_u buf[NUMBUFLEN];
7668 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007669 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007671 save = get_tv_number_chk(&argvars[0], &error);
7672 title = get_tv_string_chk(&argvars[1]);
7673 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7674 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007676 if (error || title == NULL || initdir == NULL || defname == NULL)
7677 rettv->vval.v_string = NULL;
7678 else
7679 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007680 do_browse(save ? BROWSE_SAVE : 0,
7681 title, defname, NULL, initdir, NULL, curbuf);
7682#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007683 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007684#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007685 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007686}
7687
7688/*
7689 * "browsedir(title, initdir)" function
7690 */
7691/* ARGSUSED */
7692 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007693f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007694 typval_T *argvars;
7695 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007696{
7697#ifdef FEAT_BROWSE
7698 char_u *title;
7699 char_u *initdir;
7700 char_u buf[NUMBUFLEN];
7701
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007702 title = get_tv_string_chk(&argvars[0]);
7703 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007704
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007705 if (title == NULL || initdir == NULL)
7706 rettv->vval.v_string = NULL;
7707 else
7708 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007709 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007711 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007713 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714}
7715
Bram Moolenaar33570922005-01-25 22:26:29 +00007716static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007717
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718/*
7719 * Find a buffer by number or exact name.
7720 */
7721 static buf_T *
7722find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007723 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724{
7725 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007727 if (avar->v_type == VAR_NUMBER)
7728 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007729 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007731 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007732 if (buf == NULL)
7733 {
7734 /* No full path name match, try a match with a URL or a "nofile"
7735 * buffer, these don't use the full path. */
7736 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7737 if (buf->b_fname != NULL
7738 && (path_with_url(buf->b_fname)
7739#ifdef FEAT_QUICKFIX
7740 || bt_nofile(buf)
7741#endif
7742 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007743 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007744 break;
7745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746 }
7747 return buf;
7748}
7749
7750/*
7751 * "bufexists(expr)" function
7752 */
7753 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007754f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007755 typval_T *argvars;
7756 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007758 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759}
7760
7761/*
7762 * "buflisted(expr)" function
7763 */
7764 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007765f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007766 typval_T *argvars;
7767 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768{
7769 buf_T *buf;
7770
7771 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007772 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773}
7774
7775/*
7776 * "bufloaded(expr)" function
7777 */
7778 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007779f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007780 typval_T *argvars;
7781 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782{
7783 buf_T *buf;
7784
7785 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007786 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787}
7788
Bram Moolenaar33570922005-01-25 22:26:29 +00007789static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007790
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791/*
7792 * Get buffer by number or pattern.
7793 */
7794 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007795get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007796 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007798 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799 int save_magic;
7800 char_u *save_cpo;
7801 buf_T *buf;
7802
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007803 if (tv->v_type == VAR_NUMBER)
7804 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007805 if (tv->v_type != VAR_STRING)
7806 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807 if (name == NULL || *name == NUL)
7808 return curbuf;
7809 if (name[0] == '$' && name[1] == NUL)
7810 return lastbuf;
7811
7812 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7813 save_magic = p_magic;
7814 p_magic = TRUE;
7815 save_cpo = p_cpo;
7816 p_cpo = (char_u *)"";
7817
7818 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
7819 TRUE, FALSE));
7820
7821 p_magic = save_magic;
7822 p_cpo = save_cpo;
7823
7824 /* If not found, try expanding the name, like done for bufexists(). */
7825 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007826 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827
7828 return buf;
7829}
7830
7831/*
7832 * "bufname(expr)" function
7833 */
7834 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007835f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007836 typval_T *argvars;
7837 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838{
7839 buf_T *buf;
7840
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007841 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007843 buf = get_buf_tv(&argvars[0]);
7844 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007846 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007848 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849 --emsg_off;
7850}
7851
7852/*
7853 * "bufnr(expr)" function
7854 */
7855 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007856f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007857 typval_T *argvars;
7858 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859{
7860 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007861 int error = FALSE;
7862 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007864 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007866 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007867 --emsg_off;
7868
7869 /* If the buffer isn't found and the second argument is not zero create a
7870 * new buffer. */
7871 if (buf == NULL
7872 && argvars[1].v_type != VAR_UNKNOWN
7873 && get_tv_number_chk(&argvars[1], &error) != 0
7874 && !error
7875 && (name = get_tv_string_chk(&argvars[0])) != NULL
7876 && !error)
7877 buf = buflist_new(name, NULL, (linenr_T)1, 0);
7878
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007880 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007882 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883}
7884
7885/*
7886 * "bufwinnr(nr)" function
7887 */
7888 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007889f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007890 typval_T *argvars;
7891 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892{
7893#ifdef FEAT_WINDOWS
7894 win_T *wp;
7895 int winnr = 0;
7896#endif
7897 buf_T *buf;
7898
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007899 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007901 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902#ifdef FEAT_WINDOWS
7903 for (wp = firstwin; wp; wp = wp->w_next)
7904 {
7905 ++winnr;
7906 if (wp->w_buffer == buf)
7907 break;
7908 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007909 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007911 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912#endif
7913 --emsg_off;
7914}
7915
7916/*
7917 * "byte2line(byte)" function
7918 */
7919/*ARGSUSED*/
7920 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007921f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007922 typval_T *argvars;
7923 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924{
7925#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007926 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927#else
7928 long boff = 0;
7929
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007930 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007932 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007934 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935 (linenr_T)0, &boff);
7936#endif
7937}
7938
7939/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007940 * "byteidx()" function
7941 */
7942/*ARGSUSED*/
7943 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007944f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007945 typval_T *argvars;
7946 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007947{
7948#ifdef FEAT_MBYTE
7949 char_u *t;
7950#endif
7951 char_u *str;
7952 long idx;
7953
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007954 str = get_tv_string_chk(&argvars[0]);
7955 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007956 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007957 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007958 return;
7959
7960#ifdef FEAT_MBYTE
7961 t = str;
7962 for ( ; idx > 0; idx--)
7963 {
7964 if (*t == NUL) /* EOL reached */
7965 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007966 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007967 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007968 rettv->vval.v_number = t - str;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007969#else
7970 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007971 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007972#endif
7973}
7974
7975/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007976 * "call(func, arglist)" function
7977 */
7978 static void
7979f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007980 typval_T *argvars;
7981 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007982{
7983 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00007984 typval_T argv[MAX_FUNC_ARGS];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007985 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007986 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007987 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00007988 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007989
7990 rettv->vval.v_number = 0;
7991 if (argvars[1].v_type != VAR_LIST)
7992 {
7993 EMSG(_(e_listreq));
7994 return;
7995 }
7996 if (argvars[1].vval.v_list == NULL)
7997 return;
7998
7999 if (argvars[0].v_type == VAR_FUNC)
8000 func = argvars[0].vval.v_string;
8001 else
8002 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008003 if (*func == NUL)
8004 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008005
Bram Moolenaare9a41262005-01-15 22:18:47 +00008006 if (argvars[2].v_type != VAR_UNKNOWN)
8007 {
8008 if (argvars[2].v_type != VAR_DICT)
8009 {
8010 EMSG(_(e_dictreq));
8011 return;
8012 }
8013 selfdict = argvars[2].vval.v_dict;
8014 }
8015
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008016 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
8017 item = item->li_next)
8018 {
8019 if (argc == MAX_FUNC_ARGS)
8020 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008021 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008022 break;
8023 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008024 /* Make a copy of each argument. This is needed to be able to set
8025 * v_lock to VAR_FIXED in the copy without changing the original list.
8026 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008027 copy_tv(&item->li_tv, &argv[argc++]);
8028 }
8029
8030 if (item == NULL)
8031 (void)call_func(func, STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008032 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
8033 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008034
8035 /* Free the arguments. */
8036 while (argc > 0)
8037 clear_tv(&argv[--argc]);
8038}
8039
8040/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008041 * "changenr()" function
8042 */
8043/*ARGSUSED*/
8044 static void
8045f_changenr(argvars, rettv)
8046 typval_T *argvars;
8047 typval_T *rettv;
8048{
8049 rettv->vval.v_number = curbuf->b_u_seq_cur;
8050}
8051
8052/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053 * "char2nr(string)" function
8054 */
8055 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008056f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008057 typval_T *argvars;
8058 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059{
8060#ifdef FEAT_MBYTE
8061 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008062 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063 else
8064#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008065 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066}
8067
8068/*
8069 * "cindent(lnum)" function
8070 */
8071 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008072f_cindent(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_CINDENT
8077 pos_T pos;
8078 linenr_T lnum;
8079
8080 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008081 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8083 {
8084 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008085 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086 curwin->w_cursor = pos;
8087 }
8088 else
8089#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008090 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091}
8092
8093/*
8094 * "col(string)" function
8095 */
8096 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008097f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008098 typval_T *argvars;
8099 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100{
8101 colnr_T col = 0;
8102 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008103 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008105 fp = var2fpos(&argvars[0], FALSE, &fnum);
8106 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107 {
8108 if (fp->col == MAXCOL)
8109 {
8110 /* '> can be MAXCOL, get the length of the line then */
8111 if (fp->lnum <= curbuf->b_ml.ml_line_count)
8112 col = STRLEN(ml_get(fp->lnum)) + 1;
8113 else
8114 col = MAXCOL;
8115 }
8116 else
8117 {
8118 col = fp->col + 1;
8119#ifdef FEAT_VIRTUALEDIT
8120 /* col(".") when the cursor is on the NUL at the end of the line
8121 * because of "coladd" can be seen as an extra column. */
8122 if (virtual_active() && fp == &curwin->w_cursor)
8123 {
8124 char_u *p = ml_get_cursor();
8125
8126 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
8127 curwin->w_virtcol - curwin->w_cursor.coladd))
8128 {
8129# ifdef FEAT_MBYTE
8130 int l;
8131
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008132 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133 col += l;
8134# else
8135 if (*p != NUL && p[1] == NUL)
8136 ++col;
8137# endif
8138 }
8139 }
8140#endif
8141 }
8142 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008143 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144}
8145
Bram Moolenaar572cb562005-08-05 21:35:02 +00008146#if defined(FEAT_INS_EXPAND)
8147/*
Bram Moolenaarade00832006-03-10 21:46:58 +00008148 * "complete()" function
8149 */
8150/*ARGSUSED*/
8151 static void
8152f_complete(argvars, rettv)
8153 typval_T *argvars;
8154 typval_T *rettv;
8155{
8156 int startcol;
8157
8158 if ((State & INSERT) == 0)
8159 {
8160 EMSG(_("E785: complete() can only be used in Insert mode"));
8161 return;
8162 }
8163 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
8164 {
8165 EMSG(_(e_invarg));
8166 return;
8167 }
8168
8169 startcol = get_tv_number_chk(&argvars[0], NULL);
8170 if (startcol <= 0)
8171 return;
8172
8173 set_completion(startcol - 1, argvars[1].vval.v_list);
8174}
8175
8176/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00008177 * "complete_add()" function
8178 */
8179/*ARGSUSED*/
8180 static void
8181f_complete_add(argvars, rettv)
8182 typval_T *argvars;
8183 typval_T *rettv;
8184{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008185 char_u *word;
Bram Moolenaare48ec1f2006-03-11 21:38:31 +00008186 char_u *kind = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008187 char_u *extra = NULL;
Bram Moolenaare48ec1f2006-03-11 21:38:31 +00008188 char_u *info = NULL;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008189 int icase = FALSE;
Bram Moolenaar572cb562005-08-05 21:35:02 +00008190
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008191 if (argvars[0].v_type == VAR_DICT && argvars[0].vval.v_dict != NULL)
8192 {
Bram Moolenaare48ec1f2006-03-11 21:38:31 +00008193 word = get_dict_string(argvars[0].vval.v_dict, (char_u *)"word", FALSE);
8194 kind = get_dict_string(argvars[0].vval.v_dict, (char_u *)"kind", FALSE);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008195 extra = get_dict_string(argvars[0].vval.v_dict,
8196 (char_u *)"menu", FALSE);
Bram Moolenaare48ec1f2006-03-11 21:38:31 +00008197 info = get_dict_string(argvars[0].vval.v_dict,
8198 (char_u *)"info", FALSE);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008199 icase = get_dict_number(argvars[0].vval.v_dict, (char_u *)"icase");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008200 }
8201 else
8202 word = get_tv_string_chk(&argvars[0]);
8203 if (word != NULL)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008204 rettv->vval.v_number = ins_compl_add(word, -1, icase,
Bram Moolenaare48ec1f2006-03-11 21:38:31 +00008205 NULL, kind, extra, info, 0, 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00008206}
8207
8208/*
8209 * "complete_check()" function
8210 */
8211/*ARGSUSED*/
8212 static void
8213f_complete_check(argvars, rettv)
8214 typval_T *argvars;
8215 typval_T *rettv;
8216{
8217 int saved = RedrawingDisabled;
8218
8219 RedrawingDisabled = 0;
8220 ins_compl_check_keys(0);
8221 rettv->vval.v_number = compl_interrupted;
8222 RedrawingDisabled = saved;
8223}
8224#endif
8225
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226/*
8227 * "confirm(message, buttons[, default [, type]])" function
8228 */
8229/*ARGSUSED*/
8230 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008231f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008232 typval_T *argvars;
8233 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234{
8235#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
8236 char_u *message;
8237 char_u *buttons = NULL;
8238 char_u buf[NUMBUFLEN];
8239 char_u buf2[NUMBUFLEN];
8240 int def = 1;
8241 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008242 char_u *typestr;
8243 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008245 message = get_tv_string_chk(&argvars[0]);
8246 if (message == NULL)
8247 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008248 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008250 buttons = get_tv_string_buf_chk(&argvars[1], buf);
8251 if (buttons == NULL)
8252 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008253 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008255 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008256 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008258 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
8259 if (typestr == NULL)
8260 error = TRUE;
8261 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008263 switch (TOUPPER_ASC(*typestr))
8264 {
8265 case 'E': type = VIM_ERROR; break;
8266 case 'Q': type = VIM_QUESTION; break;
8267 case 'I': type = VIM_INFO; break;
8268 case 'W': type = VIM_WARNING; break;
8269 case 'G': type = VIM_GENERIC; break;
8270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 }
8272 }
8273 }
8274 }
8275
8276 if (buttons == NULL || *buttons == NUL)
8277 buttons = (char_u *)_("&Ok");
8278
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008279 if (error)
8280 rettv->vval.v_number = 0;
8281 else
8282 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283 def, NULL);
8284#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008285 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286#endif
8287}
8288
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008289/*
8290 * "copy()" function
8291 */
8292 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008293f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008294 typval_T *argvars;
8295 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008296{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008297 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008298}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299
8300/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008301 * "count()" function
8302 */
8303 static void
8304f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008305 typval_T *argvars;
8306 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008307{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008308 long n = 0;
8309 int ic = FALSE;
8310
Bram Moolenaare9a41262005-01-15 22:18:47 +00008311 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008312 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008313 listitem_T *li;
8314 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008315 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008316
Bram Moolenaare9a41262005-01-15 22:18:47 +00008317 if ((l = argvars[0].vval.v_list) != NULL)
8318 {
8319 li = l->lv_first;
8320 if (argvars[2].v_type != VAR_UNKNOWN)
8321 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008322 int error = FALSE;
8323
8324 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008325 if (argvars[3].v_type != VAR_UNKNOWN)
8326 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008327 idx = get_tv_number_chk(&argvars[3], &error);
8328 if (!error)
8329 {
8330 li = list_find(l, idx);
8331 if (li == NULL)
8332 EMSGN(_(e_listidx), idx);
8333 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008334 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008335 if (error)
8336 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008337 }
8338
8339 for ( ; li != NULL; li = li->li_next)
8340 if (tv_equal(&li->li_tv, &argvars[1], ic))
8341 ++n;
8342 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008343 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008344 else if (argvars[0].v_type == VAR_DICT)
8345 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008346 int todo;
8347 dict_T *d;
8348 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008349
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008350 if ((d = argvars[0].vval.v_dict) != NULL)
8351 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008352 int error = FALSE;
8353
Bram Moolenaare9a41262005-01-15 22:18:47 +00008354 if (argvars[2].v_type != VAR_UNKNOWN)
8355 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008356 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008357 if (argvars[3].v_type != VAR_UNKNOWN)
8358 EMSG(_(e_invarg));
8359 }
8360
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008361 todo = error ? 0 : d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008362 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008363 {
8364 if (!HASHITEM_EMPTY(hi))
8365 {
8366 --todo;
8367 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
8368 ++n;
8369 }
8370 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008371 }
8372 }
8373 else
8374 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008375 rettv->vval.v_number = n;
8376}
8377
8378/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8380 *
8381 * Checks the existence of a cscope connection.
8382 */
8383/*ARGSUSED*/
8384 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008385f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008386 typval_T *argvars;
8387 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388{
8389#ifdef FEAT_CSCOPE
8390 int num = 0;
8391 char_u *dbpath = NULL;
8392 char_u *prepend = NULL;
8393 char_u buf[NUMBUFLEN];
8394
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008395 if (argvars[0].v_type != VAR_UNKNOWN
8396 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008398 num = (int)get_tv_number(&argvars[0]);
8399 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008400 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008401 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402 }
8403
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008404 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008406 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407#endif
8408}
8409
8410/*
8411 * "cursor(lnum, col)" function
8412 *
8413 * Moves the cursor to the specified line and column
8414 */
8415/*ARGSUSED*/
8416 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008417f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008418 typval_T *argvars;
8419 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420{
8421 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008422#ifdef FEAT_VIRTUALEDIT
8423 long coladd = 0;
8424#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008425
Bram Moolenaara5525202006-03-02 22:52:09 +00008426 if (argvars[1].v_type == VAR_UNKNOWN)
8427 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008428 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00008429
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008430 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00008431 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008432 line = pos.lnum;
8433 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008434#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008435 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00008436#endif
8437 }
8438 else
8439 {
8440 line = get_tv_lnum(argvars);
8441 col = get_tv_number_chk(&argvars[1], NULL);
8442#ifdef FEAT_VIRTUALEDIT
8443 if (argvars[2].v_type != VAR_UNKNOWN)
8444 coladd = get_tv_number_chk(&argvars[2], NULL);
8445#endif
8446 }
8447 if (line < 0 || col < 0
8448#ifdef FEAT_VIRTUALEDIT
8449 || coladd < 0
8450#endif
8451 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008452 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453 if (line > 0)
8454 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455 if (col > 0)
8456 curwin->w_cursor.col = col - 1;
8457#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00008458 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008459#endif
8460
8461 /* Make sure the cursor is in a valid position. */
8462 check_cursor();
8463#ifdef FEAT_MBYTE
8464 /* Correct cursor for multi-byte character. */
8465 if (has_mbyte)
8466 mb_adjust_cursor();
8467#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008468
8469 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470}
8471
8472/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008473 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008474 */
8475 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008476f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008477 typval_T *argvars;
8478 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008480 int noref = 0;
8481
8482 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008483 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008484 if (noref < 0 || noref > 1)
8485 EMSG(_(e_invarg));
8486 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008487 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488}
8489
8490/*
8491 * "delete()" function
8492 */
8493 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008494f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008495 typval_T *argvars;
8496 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497{
8498 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008499 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008501 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502}
8503
8504/*
8505 * "did_filetype()" function
8506 */
8507/*ARGSUSED*/
8508 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008509f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008510 typval_T *argvars;
8511 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512{
8513#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008514 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008516 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517#endif
8518}
8519
8520/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008521 * "diff_filler()" function
8522 */
8523/*ARGSUSED*/
8524 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008525f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008526 typval_T *argvars;
8527 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008528{
8529#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008530 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008531#endif
8532}
8533
8534/*
8535 * "diff_hlID()" function
8536 */
8537/*ARGSUSED*/
8538 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008539f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008540 typval_T *argvars;
8541 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008542{
8543#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008544 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008545 static linenr_T prev_lnum = 0;
8546 static int changedtick = 0;
8547 static int fnum = 0;
8548 static int change_start = 0;
8549 static int change_end = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008550 static hlf_T hlID = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008551 int filler_lines;
8552 int col;
8553
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008554 if (lnum < 0) /* ignore type error in {lnum} arg */
8555 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008556 if (lnum != prev_lnum
8557 || changedtick != curbuf->b_changedtick
8558 || fnum != curbuf->b_fnum)
8559 {
8560 /* New line, buffer, change: need to get the values. */
8561 filler_lines = diff_check(curwin, lnum);
8562 if (filler_lines < 0)
8563 {
8564 if (filler_lines == -1)
8565 {
8566 change_start = MAXCOL;
8567 change_end = -1;
8568 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8569 hlID = HLF_ADD; /* added line */
8570 else
8571 hlID = HLF_CHD; /* changed line */
8572 }
8573 else
8574 hlID = HLF_ADD; /* added line */
8575 }
8576 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008577 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008578 prev_lnum = lnum;
8579 changedtick = curbuf->b_changedtick;
8580 fnum = curbuf->b_fnum;
8581 }
8582
8583 if (hlID == HLF_CHD || hlID == HLF_TXD)
8584 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008585 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008586 if (col >= change_start && col <= change_end)
8587 hlID = HLF_TXD; /* changed text */
8588 else
8589 hlID = HLF_CHD; /* changed line */
8590 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008591 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008592#endif
8593}
8594
8595/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008596 * "empty({expr})" function
8597 */
8598 static void
8599f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008600 typval_T *argvars;
8601 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008602{
8603 int n;
8604
8605 switch (argvars[0].v_type)
8606 {
8607 case VAR_STRING:
8608 case VAR_FUNC:
8609 n = argvars[0].vval.v_string == NULL
8610 || *argvars[0].vval.v_string == NUL;
8611 break;
8612 case VAR_NUMBER:
8613 n = argvars[0].vval.v_number == 0;
8614 break;
8615 case VAR_LIST:
8616 n = argvars[0].vval.v_list == NULL
8617 || argvars[0].vval.v_list->lv_first == NULL;
8618 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008619 case VAR_DICT:
8620 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008621 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008622 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008623 default:
8624 EMSG2(_(e_intern2), "f_empty()");
8625 n = 0;
8626 }
8627
8628 rettv->vval.v_number = n;
8629}
8630
8631/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632 * "escape({string}, {chars})" function
8633 */
8634 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008635f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008636 typval_T *argvars;
8637 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638{
8639 char_u buf[NUMBUFLEN];
8640
Bram Moolenaar758711c2005-02-02 23:11:38 +00008641 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8642 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008643 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644}
8645
8646/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008647 * "eval()" function
8648 */
8649/*ARGSUSED*/
8650 static void
8651f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008652 typval_T *argvars;
8653 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008654{
8655 char_u *s;
8656
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008657 s = get_tv_string_chk(&argvars[0]);
8658 if (s != NULL)
8659 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008660
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008661 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8662 {
8663 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008664 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008665 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008666 else if (*s != NUL)
8667 EMSG(_(e_trailing));
8668}
8669
8670/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008671 * "eventhandler()" function
8672 */
8673/*ARGSUSED*/
8674 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008675f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008676 typval_T *argvars;
8677 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008679 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680}
8681
8682/*
8683 * "executable()" function
8684 */
8685 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008686f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008687 typval_T *argvars;
8688 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008690 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691}
8692
8693/*
8694 * "exists()" function
8695 */
8696 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008697f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008698 typval_T *argvars;
8699 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700{
8701 char_u *p;
8702 char_u *name;
8703 int n = FALSE;
8704 int len = 0;
8705
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008706 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008707 if (*p == '$') /* environment variable */
8708 {
8709 /* first try "normal" environment variables (fast) */
8710 if (mch_getenv(p + 1) != NULL)
8711 n = TRUE;
8712 else
8713 {
8714 /* try expanding things like $VIM and ${HOME} */
8715 p = expand_env_save(p);
8716 if (p != NULL && *p != '$')
8717 n = TRUE;
8718 vim_free(p);
8719 }
8720 }
8721 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008722 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723 else if (*p == '*') /* internal or user defined function */
8724 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008725 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008726 }
8727 else if (*p == ':')
8728 {
8729 n = cmd_exists(p + 1);
8730 }
8731 else if (*p == '#')
8732 {
8733#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00008734 if (p[1] == '#')
8735 n = autocmd_supported(p + 2);
8736 else
8737 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738#endif
8739 }
8740 else /* internal variable */
8741 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008742 char_u *tofree;
8743 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008745 /* get_name_len() takes care of expanding curly braces */
8746 name = p;
8747 len = get_name_len(&p, &tofree, TRUE, FALSE);
8748 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008750 if (tofree != NULL)
8751 name = tofree;
8752 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8753 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008755 /* handle d.key, l[idx], f(expr) */
8756 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8757 if (n)
8758 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008759 }
8760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008762 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008763 }
8764
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008765 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008766}
8767
8768/*
8769 * "expand()" function
8770 */
8771 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008772f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008773 typval_T *argvars;
8774 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775{
8776 char_u *s;
8777 int len;
8778 char_u *errormsg;
8779 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8780 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008781 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008783 rettv->v_type = VAR_STRING;
8784 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785 if (*s == '%' || *s == '#' || *s == '<')
8786 {
8787 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008788 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789 --emsg_off;
8790 }
8791 else
8792 {
8793 /* When the optional second argument is non-zero, don't remove matches
8794 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008795 if (argvars[1].v_type != VAR_UNKNOWN
8796 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008797 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008798 if (!error)
8799 {
8800 ExpandInit(&xpc);
8801 xpc.xp_context = EXPAND_FILES;
8802 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
8803 ExpandCleanup(&xpc);
8804 }
8805 else
8806 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807 }
8808}
8809
8810/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008811 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00008812 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008813 */
8814 static void
8815f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008816 typval_T *argvars;
8817 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008818{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008819 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008820 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008821 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008822 list_T *l1, *l2;
8823 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008824 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008825 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008826
Bram Moolenaare9a41262005-01-15 22:18:47 +00008827 l1 = argvars[0].vval.v_list;
8828 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008829 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
8830 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008831 {
8832 if (argvars[2].v_type != VAR_UNKNOWN)
8833 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008834 before = get_tv_number_chk(&argvars[2], &error);
8835 if (error)
8836 return; /* type error; errmsg already given */
8837
Bram Moolenaar758711c2005-02-02 23:11:38 +00008838 if (before == l1->lv_len)
8839 item = NULL;
8840 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008841 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00008842 item = list_find(l1, before);
8843 if (item == NULL)
8844 {
8845 EMSGN(_(e_listidx), before);
8846 return;
8847 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008848 }
8849 }
8850 else
8851 item = NULL;
8852 list_extend(l1, l2, item);
8853
Bram Moolenaare9a41262005-01-15 22:18:47 +00008854 copy_tv(&argvars[0], rettv);
8855 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008856 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008857 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
8858 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008859 dict_T *d1, *d2;
8860 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008861 char_u *action;
8862 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00008863 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008864 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008865
8866 d1 = argvars[0].vval.v_dict;
8867 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008868 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
8869 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008870 {
8871 /* Check the third argument. */
8872 if (argvars[2].v_type != VAR_UNKNOWN)
8873 {
8874 static char *(av[]) = {"keep", "force", "error"};
8875
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008876 action = get_tv_string_chk(&argvars[2]);
8877 if (action == NULL)
8878 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008879 for (i = 0; i < 3; ++i)
8880 if (STRCMP(action, av[i]) == 0)
8881 break;
8882 if (i == 3)
8883 {
8884 EMSGN(_(e_invarg2), action);
8885 return;
8886 }
8887 }
8888 else
8889 action = (char_u *)"force";
8890
8891 /* Go over all entries in the second dict and add them to the
8892 * first dict. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008893 todo = d2->dv_hashtab.ht_used;
8894 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008895 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008896 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008897 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008898 --todo;
8899 di1 = dict_find(d1, hi2->hi_key, -1);
8900 if (di1 == NULL)
8901 {
8902 di1 = dictitem_copy(HI2DI(hi2));
8903 if (di1 != NULL && dict_add(d1, di1) == FAIL)
8904 dictitem_free(di1);
8905 }
8906 else if (*action == 'e')
8907 {
8908 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
8909 break;
8910 }
8911 else if (*action == 'f')
8912 {
8913 clear_tv(&di1->di_tv);
8914 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
8915 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008916 }
8917 }
8918
Bram Moolenaare9a41262005-01-15 22:18:47 +00008919 copy_tv(&argvars[0], rettv);
8920 }
8921 }
8922 else
8923 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008924}
8925
8926/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927 * "filereadable()" function
8928 */
8929 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008930f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008931 typval_T *argvars;
8932 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008933{
8934 FILE *fd;
8935 char_u *p;
8936 int n;
8937
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008938 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
8940 {
8941 n = TRUE;
8942 fclose(fd);
8943 }
8944 else
8945 n = FALSE;
8946
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008947 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948}
8949
8950/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008951 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952 * rights to write into.
8953 */
8954 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008955f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008956 typval_T *argvars;
8957 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008958{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008959 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960}
8961
Bram Moolenaar33570922005-01-25 22:26:29 +00008962static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008963
8964 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008965findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00008966 typval_T *argvars;
8967 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008968 int dir;
8969{
8970#ifdef FEAT_SEARCHPATH
8971 char_u *fname;
8972 char_u *fresult = NULL;
8973 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
8974 char_u *p;
8975 char_u pathbuf[NUMBUFLEN];
8976 int count = 1;
8977 int first = TRUE;
8978
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008979 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008980
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008981 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008982 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008983 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
8984 if (p == NULL)
8985 count = -1; /* error */
8986 else
8987 {
8988 if (*p != NUL)
8989 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008990
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008991 if (argvars[2].v_type != VAR_UNKNOWN)
8992 count = get_tv_number_chk(&argvars[2], NULL); /* -1: error */
8993 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008994 }
8995
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008996 if (*fname != NUL && count >= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008997 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008998 do
8999 {
9000 vim_free(fresult);
9001 fresult = find_file_in_path_option(first ? fname : NULL,
9002 first ? (int)STRLEN(fname) : 0,
9003 0, first, path, dir, NULL);
9004 first = FALSE;
9005 } while (--count > 0 && fresult != NULL);
9006 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009007
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009008 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009009#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009010 rettv->vval.v_string = NULL;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009011#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009012 rettv->v_type = VAR_STRING;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009013}
9014
Bram Moolenaar33570922005-01-25 22:26:29 +00009015static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
9016static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009017
9018/*
9019 * Implementation of map() and filter().
9020 */
9021 static void
9022filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00009023 typval_T *argvars;
9024 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009025 int map;
9026{
9027 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00009028 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00009029 listitem_T *li, *nli;
9030 list_T *l = NULL;
9031 dictitem_T *di;
9032 hashtab_T *ht;
9033 hashitem_T *hi;
9034 dict_T *d = NULL;
9035 typval_T save_val;
9036 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009037 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009038 int todo;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009039 char_u *msg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009040 int save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009041
9042 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009043 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009044 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009045 if ((l = argvars[0].vval.v_list) == NULL
9046 || (map && tv_check_lock(l->lv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009047 return;
9048 }
9049 else if (argvars[0].v_type == VAR_DICT)
9050 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009051 if ((d = argvars[0].vval.v_dict) == NULL
9052 || (map && tv_check_lock(d->dv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009053 return;
9054 }
9055 else
9056 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009057 EMSG2(_(e_listdictarg), msg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009058 return;
9059 }
9060
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009061 expr = get_tv_string_buf_chk(&argvars[1], buf);
9062 /* On type errors, the preceding call has already displayed an error
9063 * message. Avoid a misleading error message for an empty string that
9064 * was not passed as argument. */
9065 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009066 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009067 prepare_vimvar(VV_VAL, &save_val);
9068 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009069
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009070 /* We reset "did_emsg" to be able to detect whether an error
9071 * occurred during evaluation of the expression. */
9072 save_did_emsg = did_emsg;
9073 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009074
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009075 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009076 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009077 prepare_vimvar(VV_KEY, &save_key);
9078 vimvars[VV_KEY].vv_type = VAR_STRING;
9079
9080 ht = &d->dv_hashtab;
9081 hash_lock(ht);
9082 todo = ht->ht_used;
9083 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009084 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009085 if (!HASHITEM_EMPTY(hi))
9086 {
9087 --todo;
9088 di = HI2DI(hi);
9089 if (tv_check_lock(di->di_tv.v_lock, msg))
9090 break;
9091 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009092 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009093 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009094 break;
9095 if (!map && rem)
9096 dictitem_remove(d, di);
9097 clear_tv(&vimvars[VV_KEY].vv_tv);
9098 }
9099 }
9100 hash_unlock(ht);
9101
9102 restore_vimvar(VV_KEY, &save_key);
9103 }
9104 else
9105 {
9106 for (li = l->lv_first; li != NULL; li = nli)
9107 {
9108 if (tv_check_lock(li->li_tv.v_lock, msg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009109 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009110 nli = li->li_next;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009111 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009112 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009113 break;
9114 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009115 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009116 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009117 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009118
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009119 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009120
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009121 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009122 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009123
9124 copy_tv(&argvars[0], rettv);
9125}
9126
9127 static int
9128filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009129 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009130 char_u *expr;
9131 int map;
9132 int *remp;
9133{
Bram Moolenaar33570922005-01-25 22:26:29 +00009134 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009135 char_u *s;
9136
Bram Moolenaar33570922005-01-25 22:26:29 +00009137 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009138 s = expr;
9139 if (eval1(&s, &rettv, TRUE) == FAIL)
9140 return FAIL;
9141 if (*s != NUL) /* check for trailing chars after expr */
9142 {
9143 EMSG2(_(e_invexpr2), s);
9144 return FAIL;
9145 }
9146 if (map)
9147 {
9148 /* map(): replace the list item value */
9149 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +00009150 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009151 *tv = rettv;
9152 }
9153 else
9154 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009155 int error = FALSE;
9156
Bram Moolenaare9a41262005-01-15 22:18:47 +00009157 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009158 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009159 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009160 /* On type error, nothing has been removed; return FAIL to stop the
9161 * loop. The error message was given by get_tv_number_chk(). */
9162 if (error)
9163 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009164 }
Bram Moolenaar33570922005-01-25 22:26:29 +00009165 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009166 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009167}
9168
9169/*
9170 * "filter()" function
9171 */
9172 static void
9173f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009174 typval_T *argvars;
9175 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009176{
9177 filter_map(argvars, rettv, FALSE);
9178}
9179
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009180/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009181 * "finddir({fname}[, {path}[, {count}]])" function
9182 */
9183 static void
9184f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009185 typval_T *argvars;
9186 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009187{
9188 findfilendir(argvars, rettv, TRUE);
9189}
9190
9191/*
9192 * "findfile({fname}[, {path}[, {count}]])" function
9193 */
9194 static void
9195f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009196 typval_T *argvars;
9197 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009198{
9199 findfilendir(argvars, rettv, FALSE);
9200}
9201
9202/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009203 * "fnamemodify({fname}, {mods})" function
9204 */
9205 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009206f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009207 typval_T *argvars;
9208 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009209{
9210 char_u *fname;
9211 char_u *mods;
9212 int usedlen = 0;
9213 int len;
9214 char_u *fbuf = NULL;
9215 char_u buf[NUMBUFLEN];
9216
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009217 fname = get_tv_string_chk(&argvars[0]);
9218 mods = get_tv_string_buf_chk(&argvars[1], buf);
9219 if (fname == NULL || mods == NULL)
9220 fname = NULL;
9221 else
9222 {
9223 len = (int)STRLEN(fname);
9224 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
9225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009226
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009227 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009228 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009229 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009230 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009231 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009232 vim_free(fbuf);
9233}
9234
Bram Moolenaar33570922005-01-25 22:26:29 +00009235static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009236
9237/*
9238 * "foldclosed()" function
9239 */
9240 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009241foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00009242 typval_T *argvars;
9243 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009244 int end;
9245{
9246#ifdef FEAT_FOLDING
9247 linenr_T lnum;
9248 linenr_T first, last;
9249
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009250 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009251 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9252 {
9253 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
9254 {
9255 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009256 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009257 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009258 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009259 return;
9260 }
9261 }
9262#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009263 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009264}
9265
9266/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009267 * "foldclosed()" function
9268 */
9269 static void
9270f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009271 typval_T *argvars;
9272 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009273{
9274 foldclosed_both(argvars, rettv, FALSE);
9275}
9276
9277/*
9278 * "foldclosedend()" function
9279 */
9280 static void
9281f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009282 typval_T *argvars;
9283 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009284{
9285 foldclosed_both(argvars, rettv, TRUE);
9286}
9287
9288/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009289 * "foldlevel()" function
9290 */
9291 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009292f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009293 typval_T *argvars;
9294 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009295{
9296#ifdef FEAT_FOLDING
9297 linenr_T lnum;
9298
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009299 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009300 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009301 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009302 else
9303#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009304 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009305}
9306
9307/*
9308 * "foldtext()" function
9309 */
9310/*ARGSUSED*/
9311 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009312f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009313 typval_T *argvars;
9314 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009315{
9316#ifdef FEAT_FOLDING
9317 linenr_T lnum;
9318 char_u *s;
9319 char_u *r;
9320 int len;
9321 char *txt;
9322#endif
9323
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009324 rettv->v_type = VAR_STRING;
9325 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009326#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00009327 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
9328 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
9329 <= curbuf->b_ml.ml_line_count
9330 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009331 {
9332 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009333 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
9334 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009335 {
9336 if (!linewhite(lnum))
9337 break;
9338 ++lnum;
9339 }
9340
9341 /* Find interesting text in this line. */
9342 s = skipwhite(ml_get(lnum));
9343 /* skip C comment-start */
9344 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009345 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009346 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009347 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00009348 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009349 {
9350 s = skipwhite(ml_get(lnum + 1));
9351 if (*s == '*')
9352 s = skipwhite(s + 1);
9353 }
9354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009355 txt = _("+-%s%3ld lines: ");
9356 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009357 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009358 + 20 /* for %3ld */
9359 + STRLEN(s))); /* concatenated */
9360 if (r != NULL)
9361 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009362 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
9363 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
9364 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009365 len = (int)STRLEN(r);
9366 STRCAT(r, s);
9367 /* remove 'foldmarker' and 'commentstring' */
9368 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009369 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009370 }
9371 }
9372#endif
9373}
9374
9375/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009376 * "foldtextresult(lnum)" function
9377 */
9378/*ARGSUSED*/
9379 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009380f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009381 typval_T *argvars;
9382 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009383{
9384#ifdef FEAT_FOLDING
9385 linenr_T lnum;
9386 char_u *text;
9387 char_u buf[51];
9388 foldinfo_T foldinfo;
9389 int fold_count;
9390#endif
9391
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009392 rettv->v_type = VAR_STRING;
9393 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009394#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009395 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009396 /* treat illegal types and illegal string values for {lnum} the same */
9397 if (lnum < 0)
9398 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009399 fold_count = foldedCount(curwin, lnum, &foldinfo);
9400 if (fold_count > 0)
9401 {
9402 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
9403 &foldinfo, buf);
9404 if (text == buf)
9405 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009406 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009407 }
9408#endif
9409}
9410
9411/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009412 * "foreground()" function
9413 */
9414/*ARGSUSED*/
9415 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009416f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009417 typval_T *argvars;
9418 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009419{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009420 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009421#ifdef FEAT_GUI
9422 if (gui.in_use)
9423 gui_mch_set_foreground();
9424#else
9425# ifdef WIN32
9426 win32_set_foreground();
9427# endif
9428#endif
9429}
9430
9431/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009432 * "function()" function
9433 */
9434/*ARGSUSED*/
9435 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009436f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009437 typval_T *argvars;
9438 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009439{
9440 char_u *s;
9441
Bram Moolenaara7043832005-01-21 11:56:39 +00009442 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009443 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009444 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009445 EMSG2(_(e_invarg2), s);
9446 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009447 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009448 else
9449 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009450 rettv->vval.v_string = vim_strsave(s);
9451 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009452 }
9453}
9454
9455/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009456 * "garbagecollect()" function
9457 */
9458/*ARGSUSED*/
9459 static void
9460f_garbagecollect(argvars, rettv)
9461 typval_T *argvars;
9462 typval_T *rettv;
9463{
9464 garbage_collect();
9465}
9466
9467/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009468 * "get()" function
9469 */
9470 static void
9471f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009472 typval_T *argvars;
9473 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009474{
Bram Moolenaar33570922005-01-25 22:26:29 +00009475 listitem_T *li;
9476 list_T *l;
9477 dictitem_T *di;
9478 dict_T *d;
9479 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009480
Bram Moolenaare9a41262005-01-15 22:18:47 +00009481 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009482 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009483 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009484 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009485 int error = FALSE;
9486
9487 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9488 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009489 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009490 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009491 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009492 else if (argvars[0].v_type == VAR_DICT)
9493 {
9494 if ((d = argvars[0].vval.v_dict) != NULL)
9495 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009496 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009497 if (di != NULL)
9498 tv = &di->di_tv;
9499 }
9500 }
9501 else
9502 EMSG2(_(e_listdictarg), "get()");
9503
9504 if (tv == NULL)
9505 {
9506 if (argvars[2].v_type == VAR_UNKNOWN)
9507 rettv->vval.v_number = 0;
9508 else
9509 copy_tv(&argvars[2], rettv);
9510 }
9511 else
9512 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009513}
9514
Bram Moolenaar342337a2005-07-21 21:11:17 +00009515static 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 +00009516
9517/*
9518 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +00009519 * Return a range (from start to end) of lines in rettv from the specified
9520 * buffer.
9521 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009522 */
9523 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +00009524get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009525 buf_T *buf;
9526 linenr_T start;
9527 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009528 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009529 typval_T *rettv;
9530{
9531 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009532
Bram Moolenaar342337a2005-07-21 21:11:17 +00009533 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009534 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009535 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009536 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009537 }
9538 else
9539 rettv->vval.v_number = 0;
9540
9541 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
9542 return;
9543
9544 if (!retlist)
9545 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009546 if (start >= 1 && start <= buf->b_ml.ml_line_count)
9547 p = ml_get_buf(buf, start, FALSE);
9548 else
9549 p = (char_u *)"";
9550
9551 rettv->v_type = VAR_STRING;
9552 rettv->vval.v_string = vim_strsave(p);
9553 }
9554 else
9555 {
9556 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009557 return;
9558
9559 if (start < 1)
9560 start = 1;
9561 if (end > buf->b_ml.ml_line_count)
9562 end = buf->b_ml.ml_line_count;
9563 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009564 if (list_append_string(rettv->vval.v_list,
9565 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009566 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009567 }
9568}
9569
9570/*
9571 * "getbufline()" function
9572 */
9573 static void
9574f_getbufline(argvars, rettv)
9575 typval_T *argvars;
9576 typval_T *rettv;
9577{
9578 linenr_T lnum;
9579 linenr_T end;
9580 buf_T *buf;
9581
9582 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9583 ++emsg_off;
9584 buf = get_buf_tv(&argvars[0]);
9585 --emsg_off;
9586
Bram Moolenaar661b1822005-07-28 22:36:45 +00009587 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009588 if (argvars[2].v_type == VAR_UNKNOWN)
9589 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009590 else
Bram Moolenaar661b1822005-07-28 22:36:45 +00009591 end = get_tv_lnum_buf(&argvars[2], buf);
9592
Bram Moolenaar342337a2005-07-21 21:11:17 +00009593 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009594}
9595
Bram Moolenaar0d660222005-01-07 21:51:51 +00009596/*
9597 * "getbufvar()" function
9598 */
9599 static void
9600f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009601 typval_T *argvars;
9602 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009603{
9604 buf_T *buf;
9605 buf_T *save_curbuf;
9606 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009607 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009608
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009609 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9610 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009611 ++emsg_off;
9612 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009613
9614 rettv->v_type = VAR_STRING;
9615 rettv->vval.v_string = NULL;
9616
9617 if (buf != NULL && varname != NULL)
9618 {
9619 if (*varname == '&') /* buffer-local-option */
9620 {
9621 /* set curbuf to be our buf, temporarily */
9622 save_curbuf = curbuf;
9623 curbuf = buf;
9624
9625 get_option_tv(&varname, rettv, TRUE);
9626
9627 /* restore previous notion of curbuf */
9628 curbuf = save_curbuf;
9629 }
9630 else
9631 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009632 if (*varname == NUL)
9633 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9634 * scope prefix before the NUL byte is required by
9635 * find_var_in_ht(). */
9636 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009637 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009638 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009639 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009640 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009641 }
9642 }
9643
9644 --emsg_off;
9645}
9646
9647/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009648 * "getchar()" function
9649 */
9650 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009651f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009652 typval_T *argvars;
9653 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009654{
9655 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009656 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009657
9658 ++no_mapping;
9659 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009660 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009661 /* getchar(): blocking wait. */
9662 n = safe_vgetc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009663 else if (get_tv_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664 /* getchar(1): only check if char avail */
9665 n = vpeekc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009666 else if (error || vpeekc() == NUL)
9667 /* illegal argument or getchar(0) and no char avail: return zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009668 n = 0;
9669 else
9670 /* getchar(0) and char avail: return char */
9671 n = safe_vgetc();
9672 --no_mapping;
9673 --allow_keys;
9674
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009675 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009676 if (IS_SPECIAL(n) || mod_mask != 0)
9677 {
9678 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9679 int i = 0;
9680
9681 /* Turn a special key into three bytes, plus modifier. */
9682 if (mod_mask != 0)
9683 {
9684 temp[i++] = K_SPECIAL;
9685 temp[i++] = KS_MODIFIER;
9686 temp[i++] = mod_mask;
9687 }
9688 if (IS_SPECIAL(n))
9689 {
9690 temp[i++] = K_SPECIAL;
9691 temp[i++] = K_SECOND(n);
9692 temp[i++] = K_THIRD(n);
9693 }
9694#ifdef FEAT_MBYTE
9695 else if (has_mbyte)
9696 i += (*mb_char2bytes)(n, temp + i);
9697#endif
9698 else
9699 temp[i++] = n;
9700 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009701 rettv->v_type = VAR_STRING;
9702 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009703 }
9704}
9705
9706/*
9707 * "getcharmod()" function
9708 */
9709/*ARGSUSED*/
9710 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009711f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009712 typval_T *argvars;
9713 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009714{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009715 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009716}
9717
9718/*
9719 * "getcmdline()" function
9720 */
9721/*ARGSUSED*/
9722 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009723f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009724 typval_T *argvars;
9725 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009726{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009727 rettv->v_type = VAR_STRING;
9728 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009729}
9730
9731/*
9732 * "getcmdpos()" function
9733 */
9734/*ARGSUSED*/
9735 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009736f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009737 typval_T *argvars;
9738 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009739{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009740 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009741}
9742
9743/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00009744 * "getcmdtype()" function
9745 */
9746/*ARGSUSED*/
9747 static void
9748f_getcmdtype(argvars, rettv)
9749 typval_T *argvars;
9750 typval_T *rettv;
9751{
9752 rettv->v_type = VAR_STRING;
9753 rettv->vval.v_string = alloc(2);
9754 if (rettv->vval.v_string != NULL)
9755 {
9756 rettv->vval.v_string[0] = get_cmdline_type();
9757 rettv->vval.v_string[1] = NUL;
9758 }
9759}
9760
9761/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009762 * "getcwd()" function
9763 */
9764/*ARGSUSED*/
9765 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009766f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009767 typval_T *argvars;
9768 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009769{
9770 char_u cwd[MAXPATHL];
9771
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009772 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009774 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775 else
9776 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009777 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009778#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +00009779 if (rettv->vval.v_string != NULL)
9780 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009781#endif
9782 }
9783}
9784
9785/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009786 * "getfontname()" function
9787 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009788/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009789 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009790f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009791 typval_T *argvars;
9792 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009793{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009794 rettv->v_type = VAR_STRING;
9795 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009796#ifdef FEAT_GUI
9797 if (gui.in_use)
9798 {
9799 GuiFont font;
9800 char_u *name = NULL;
9801
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009802 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009803 {
9804 /* Get the "Normal" font. Either the name saved by
9805 * hl_set_font_name() or from the font ID. */
9806 font = gui.norm_font;
9807 name = hl_get_font_name();
9808 }
9809 else
9810 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009811 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009812 if (STRCMP(name, "*") == 0) /* don't use font dialog */
9813 return;
9814 font = gui_mch_get_font(name, FALSE);
9815 if (font == NOFONT)
9816 return; /* Invalid font name, return empty string. */
9817 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009818 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009819 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009820 gui_mch_free_font(font);
9821 }
9822#endif
9823}
9824
9825/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009826 * "getfperm({fname})" function
9827 */
9828 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009829f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009830 typval_T *argvars;
9831 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009832{
9833 char_u *fname;
9834 struct stat st;
9835 char_u *perm = NULL;
9836 char_u flags[] = "rwx";
9837 int i;
9838
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009839 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009840
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009841 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009842 if (mch_stat((char *)fname, &st) >= 0)
9843 {
9844 perm = vim_strsave((char_u *)"---------");
9845 if (perm != NULL)
9846 {
9847 for (i = 0; i < 9; i++)
9848 {
9849 if (st.st_mode & (1 << (8 - i)))
9850 perm[i] = flags[i % 3];
9851 }
9852 }
9853 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009854 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009855}
9856
9857/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009858 * "getfsize({fname})" function
9859 */
9860 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009861f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009862 typval_T *argvars;
9863 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009864{
9865 char_u *fname;
9866 struct stat st;
9867
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009868 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009869
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009870 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009871
9872 if (mch_stat((char *)fname, &st) >= 0)
9873 {
9874 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009875 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009876 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009877 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009878 }
9879 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009880 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009881}
9882
9883/*
9884 * "getftime({fname})" function
9885 */
9886 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009887f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009888 typval_T *argvars;
9889 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009890{
9891 char_u *fname;
9892 struct stat st;
9893
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009894 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009895
9896 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009897 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009898 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009899 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009900}
9901
9902/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009903 * "getftype({fname})" function
9904 */
9905 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009906f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009907 typval_T *argvars;
9908 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009909{
9910 char_u *fname;
9911 struct stat st;
9912 char_u *type = NULL;
9913 char *t;
9914
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009915 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009916
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009917 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009918 if (mch_lstat((char *)fname, &st) >= 0)
9919 {
9920#ifdef S_ISREG
9921 if (S_ISREG(st.st_mode))
9922 t = "file";
9923 else if (S_ISDIR(st.st_mode))
9924 t = "dir";
9925# ifdef S_ISLNK
9926 else if (S_ISLNK(st.st_mode))
9927 t = "link";
9928# endif
9929# ifdef S_ISBLK
9930 else if (S_ISBLK(st.st_mode))
9931 t = "bdev";
9932# endif
9933# ifdef S_ISCHR
9934 else if (S_ISCHR(st.st_mode))
9935 t = "cdev";
9936# endif
9937# ifdef S_ISFIFO
9938 else if (S_ISFIFO(st.st_mode))
9939 t = "fifo";
9940# endif
9941# ifdef S_ISSOCK
9942 else if (S_ISSOCK(st.st_mode))
9943 t = "fifo";
9944# endif
9945 else
9946 t = "other";
9947#else
9948# ifdef S_IFMT
9949 switch (st.st_mode & S_IFMT)
9950 {
9951 case S_IFREG: t = "file"; break;
9952 case S_IFDIR: t = "dir"; break;
9953# ifdef S_IFLNK
9954 case S_IFLNK: t = "link"; break;
9955# endif
9956# ifdef S_IFBLK
9957 case S_IFBLK: t = "bdev"; break;
9958# endif
9959# ifdef S_IFCHR
9960 case S_IFCHR: t = "cdev"; break;
9961# endif
9962# ifdef S_IFIFO
9963 case S_IFIFO: t = "fifo"; break;
9964# endif
9965# ifdef S_IFSOCK
9966 case S_IFSOCK: t = "socket"; break;
9967# endif
9968 default: t = "other";
9969 }
9970# else
9971 if (mch_isdir(fname))
9972 t = "dir";
9973 else
9974 t = "file";
9975# endif
9976#endif
9977 type = vim_strsave((char_u *)t);
9978 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009979 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009980}
9981
9982/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009983 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +00009984 */
9985 static void
9986f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009987 typval_T *argvars;
9988 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009989{
9990 linenr_T lnum;
9991 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009992 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009993
9994 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009995 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009996 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009997 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009998 retlist = FALSE;
9999 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010000 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000010001 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000010002 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010003 retlist = TRUE;
10004 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010005
Bram Moolenaar342337a2005-07-21 21:11:17 +000010006 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010007}
10008
10009/*
Bram Moolenaara5525202006-03-02 22:52:09 +000010010 * "getpos(string)" function
10011 */
10012 static void
10013f_getpos(argvars, rettv)
10014 typval_T *argvars;
10015 typval_T *rettv;
10016{
10017 pos_T *fp;
10018 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010019 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010020
10021 if (rettv_list_alloc(rettv) == OK)
10022 {
10023 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010024 fp = var2fpos(&argvars[0], TRUE, &fnum);
10025 if (fnum != -1)
10026 list_append_number(l, (varnumber_T)fnum);
10027 else
10028 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000010029 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
10030 : (varnumber_T)0);
10031 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->col + 1
10032 : (varnumber_T)0);
10033 list_append_number(l,
10034#ifdef FEAT_VIRTUALEDIT
10035 (fp != NULL) ? (varnumber_T)fp->coladd :
10036#endif
10037 (varnumber_T)0);
10038 }
10039 else
10040 rettv->vval.v_number = FALSE;
10041}
10042
10043/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000010044 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000010045 */
Bram Moolenaar280f1262006-01-30 00:14:18 +000010046/*ARGSUSED*/
Bram Moolenaar2641f772005-03-25 21:58:17 +000010047 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000010048f_getqflist(argvars, rettv)
10049 typval_T *argvars;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010050 typval_T *rettv;
10051{
10052#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000010053 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010054#endif
10055
10056 rettv->vval.v_number = FALSE;
10057#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010058 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000010059 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000010060 wp = NULL;
10061 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
10062 {
10063 wp = find_win_by_nr(&argvars[0]);
10064 if (wp == NULL)
10065 return;
10066 }
10067
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010068 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000010069 }
10070#endif
10071}
10072
10073/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010074 * "getreg()" function
10075 */
10076 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010077f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010078 typval_T *argvars;
10079 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010080{
10081 char_u *strregname;
10082 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010083 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010084 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010085
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010086 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010087 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010088 strregname = get_tv_string_chk(&argvars[0]);
10089 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010090 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010091 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010092 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010093 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010094 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010095 regname = (strregname == NULL ? '"' : *strregname);
10096 if (regname == 0)
10097 regname = '"';
10098
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010099 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010100 rettv->vval.v_string = error ? NULL :
10101 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010102}
10103
10104/*
10105 * "getregtype()" function
10106 */
10107 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010108f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010109 typval_T *argvars;
10110 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010111{
10112 char_u *strregname;
10113 int regname;
10114 char_u buf[NUMBUFLEN + 2];
10115 long reglen = 0;
10116
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010117 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010118 {
10119 strregname = get_tv_string_chk(&argvars[0]);
10120 if (strregname == NULL) /* type error; errmsg already given */
10121 {
10122 rettv->v_type = VAR_STRING;
10123 rettv->vval.v_string = NULL;
10124 return;
10125 }
10126 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010127 else
10128 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010129 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010130
10131 regname = (strregname == NULL ? '"' : *strregname);
10132 if (regname == 0)
10133 regname = '"';
10134
10135 buf[0] = NUL;
10136 buf[1] = NUL;
10137 switch (get_reg_type(regname, &reglen))
10138 {
10139 case MLINE: buf[0] = 'V'; break;
10140 case MCHAR: buf[0] = 'v'; break;
10141#ifdef FEAT_VISUAL
10142 case MBLOCK:
10143 buf[0] = Ctrl_V;
10144 sprintf((char *)buf + 1, "%ld", reglen + 1);
10145 break;
10146#endif
10147 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010148 rettv->v_type = VAR_STRING;
10149 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010150}
10151
10152/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010153 * "getwinposx()" function
10154 */
10155/*ARGSUSED*/
10156 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010157f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010158 typval_T *argvars;
10159 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010160{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010161 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010162#ifdef FEAT_GUI
10163 if (gui.in_use)
10164 {
10165 int x, y;
10166
10167 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010168 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010169 }
10170#endif
10171}
10172
10173/*
10174 * "getwinposy()" function
10175 */
10176/*ARGSUSED*/
10177 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010178f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010179 typval_T *argvars;
10180 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010181{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010182 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010183#ifdef FEAT_GUI
10184 if (gui.in_use)
10185 {
10186 int x, y;
10187
10188 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010189 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010190 }
10191#endif
10192}
10193
Bram Moolenaara40058a2005-07-11 22:42:07 +000010194 static win_T *
10195find_win_by_nr(vp)
10196 typval_T *vp;
10197{
10198#ifdef FEAT_WINDOWS
10199 win_T *wp;
10200#endif
10201 int nr;
10202
10203 nr = get_tv_number_chk(vp, NULL);
10204
10205#ifdef FEAT_WINDOWS
10206 if (nr < 0)
10207 return NULL;
10208 if (nr == 0)
10209 return curwin;
10210
10211 for (wp = firstwin; wp != NULL; wp = wp->w_next)
10212 if (--nr <= 0)
10213 break;
10214 return wp;
10215#else
10216 if (nr == 0 || nr == 1)
10217 return curwin;
10218 return NULL;
10219#endif
10220}
10221
Bram Moolenaar071d4272004-06-13 20:20:40 +000010222/*
10223 * "getwinvar()" function
10224 */
10225 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010226f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010227 typval_T *argvars;
10228 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010229{
10230 win_T *win, *oldcurwin;
10231 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010232 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010233
Bram Moolenaar071d4272004-06-13 20:20:40 +000010234 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010235 varname = get_tv_string_chk(&argvars[1]);
10236 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010237
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010238 rettv->v_type = VAR_STRING;
10239 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010240
10241 if (win != NULL && varname != NULL)
10242 {
10243 if (*varname == '&') /* window-local-option */
10244 {
Bram Moolenaarc0761132005-03-18 20:30:32 +000010245 /* Set curwin to be our win, temporarily. Also set curbuf, so
10246 * that we can get buffer-local options. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010247 oldcurwin = curwin;
10248 curwin = win;
Bram Moolenaarc0761132005-03-18 20:30:32 +000010249 curbuf = win->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010250
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010251 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010252
10253 /* restore previous notion of curwin */
10254 curwin = oldcurwin;
Bram Moolenaarc0761132005-03-18 20:30:32 +000010255 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010256 }
10257 else
10258 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010259 if (*varname == NUL)
10260 /* let getwinvar({nr}, "") return the "w:" dictionary. The
10261 * scope prefix before the NUL byte is required by
10262 * find_var_in_ht(). */
10263 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010264 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010265 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010266 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010267 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010268 }
10269 }
10270
10271 --emsg_off;
10272}
10273
10274/*
10275 * "glob()" function
10276 */
10277 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010278f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010279 typval_T *argvars;
10280 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010281{
10282 expand_T xpc;
10283
10284 ExpandInit(&xpc);
10285 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010286 rettv->v_type = VAR_STRING;
10287 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010288 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
10289 ExpandCleanup(&xpc);
10290}
10291
10292/*
10293 * "globpath()" function
10294 */
10295 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010296f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010297 typval_T *argvars;
10298 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010299{
10300 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010301 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010302
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010303 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010304 if (file == NULL)
10305 rettv->vval.v_string = NULL;
10306 else
10307 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010308}
10309
10310/*
10311 * "has()" function
10312 */
10313 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010314f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010315 typval_T *argvars;
10316 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010317{
10318 int i;
10319 char_u *name;
10320 int n = FALSE;
10321 static char *(has_list[]) =
10322 {
10323#ifdef AMIGA
10324 "amiga",
10325# ifdef FEAT_ARP
10326 "arp",
10327# endif
10328#endif
10329#ifdef __BEOS__
10330 "beos",
10331#endif
10332#ifdef MSDOS
10333# ifdef DJGPP
10334 "dos32",
10335# else
10336 "dos16",
10337# endif
10338#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000010339#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010340 "mac",
10341#endif
10342#if defined(MACOS_X_UNIX)
10343 "macunix",
10344#endif
10345#ifdef OS2
10346 "os2",
10347#endif
10348#ifdef __QNX__
10349 "qnx",
10350#endif
10351#ifdef RISCOS
10352 "riscos",
10353#endif
10354#ifdef UNIX
10355 "unix",
10356#endif
10357#ifdef VMS
10358 "vms",
10359#endif
10360#ifdef WIN16
10361 "win16",
10362#endif
10363#ifdef WIN32
10364 "win32",
10365#endif
10366#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
10367 "win32unix",
10368#endif
10369#ifdef WIN64
10370 "win64",
10371#endif
10372#ifdef EBCDIC
10373 "ebcdic",
10374#endif
10375#ifndef CASE_INSENSITIVE_FILENAME
10376 "fname_case",
10377#endif
10378#ifdef FEAT_ARABIC
10379 "arabic",
10380#endif
10381#ifdef FEAT_AUTOCMD
10382 "autocmd",
10383#endif
10384#ifdef FEAT_BEVAL
10385 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000010386# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
10387 "balloon_multiline",
10388# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010389#endif
10390#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
10391 "builtin_terms",
10392# ifdef ALL_BUILTIN_TCAPS
10393 "all_builtin_terms",
10394# endif
10395#endif
10396#ifdef FEAT_BYTEOFF
10397 "byte_offset",
10398#endif
10399#ifdef FEAT_CINDENT
10400 "cindent",
10401#endif
10402#ifdef FEAT_CLIENTSERVER
10403 "clientserver",
10404#endif
10405#ifdef FEAT_CLIPBOARD
10406 "clipboard",
10407#endif
10408#ifdef FEAT_CMDL_COMPL
10409 "cmdline_compl",
10410#endif
10411#ifdef FEAT_CMDHIST
10412 "cmdline_hist",
10413#endif
10414#ifdef FEAT_COMMENTS
10415 "comments",
10416#endif
10417#ifdef FEAT_CRYPT
10418 "cryptv",
10419#endif
10420#ifdef FEAT_CSCOPE
10421 "cscope",
10422#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010423#ifdef CURSOR_SHAPE
10424 "cursorshape",
10425#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010426#ifdef DEBUG
10427 "debug",
10428#endif
10429#ifdef FEAT_CON_DIALOG
10430 "dialog_con",
10431#endif
10432#ifdef FEAT_GUI_DIALOG
10433 "dialog_gui",
10434#endif
10435#ifdef FEAT_DIFF
10436 "diff",
10437#endif
10438#ifdef FEAT_DIGRAPHS
10439 "digraphs",
10440#endif
10441#ifdef FEAT_DND
10442 "dnd",
10443#endif
10444#ifdef FEAT_EMACS_TAGS
10445 "emacs_tags",
10446#endif
10447 "eval", /* always present, of course! */
10448#ifdef FEAT_EX_EXTRA
10449 "ex_extra",
10450#endif
10451#ifdef FEAT_SEARCH_EXTRA
10452 "extra_search",
10453#endif
10454#ifdef FEAT_FKMAP
10455 "farsi",
10456#endif
10457#ifdef FEAT_SEARCHPATH
10458 "file_in_path",
10459#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010460#if defined(UNIX) && !defined(USE_SYSTEM)
10461 "filterpipe",
10462#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010463#ifdef FEAT_FIND_ID
10464 "find_in_path",
10465#endif
10466#ifdef FEAT_FOLDING
10467 "folding",
10468#endif
10469#ifdef FEAT_FOOTER
10470 "footer",
10471#endif
10472#if !defined(USE_SYSTEM) && defined(UNIX)
10473 "fork",
10474#endif
10475#ifdef FEAT_GETTEXT
10476 "gettext",
10477#endif
10478#ifdef FEAT_GUI
10479 "gui",
10480#endif
10481#ifdef FEAT_GUI_ATHENA
10482# ifdef FEAT_GUI_NEXTAW
10483 "gui_neXtaw",
10484# else
10485 "gui_athena",
10486# endif
10487#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010488#ifdef FEAT_GUI_GTK
10489 "gui_gtk",
10490# ifdef HAVE_GTK2
10491 "gui_gtk2",
10492# endif
10493#endif
10494#ifdef FEAT_GUI_MAC
10495 "gui_mac",
10496#endif
10497#ifdef FEAT_GUI_MOTIF
10498 "gui_motif",
10499#endif
10500#ifdef FEAT_GUI_PHOTON
10501 "gui_photon",
10502#endif
10503#ifdef FEAT_GUI_W16
10504 "gui_win16",
10505#endif
10506#ifdef FEAT_GUI_W32
10507 "gui_win32",
10508#endif
10509#ifdef FEAT_HANGULIN
10510 "hangul_input",
10511#endif
10512#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10513 "iconv",
10514#endif
10515#ifdef FEAT_INS_EXPAND
10516 "insert_expand",
10517#endif
10518#ifdef FEAT_JUMPLIST
10519 "jumplist",
10520#endif
10521#ifdef FEAT_KEYMAP
10522 "keymap",
10523#endif
10524#ifdef FEAT_LANGMAP
10525 "langmap",
10526#endif
10527#ifdef FEAT_LIBCALL
10528 "libcall",
10529#endif
10530#ifdef FEAT_LINEBREAK
10531 "linebreak",
10532#endif
10533#ifdef FEAT_LISP
10534 "lispindent",
10535#endif
10536#ifdef FEAT_LISTCMDS
10537 "listcmds",
10538#endif
10539#ifdef FEAT_LOCALMAP
10540 "localmap",
10541#endif
10542#ifdef FEAT_MENU
10543 "menu",
10544#endif
10545#ifdef FEAT_SESSION
10546 "mksession",
10547#endif
10548#ifdef FEAT_MODIFY_FNAME
10549 "modify_fname",
10550#endif
10551#ifdef FEAT_MOUSE
10552 "mouse",
10553#endif
10554#ifdef FEAT_MOUSESHAPE
10555 "mouseshape",
10556#endif
10557#if defined(UNIX) || defined(VMS)
10558# ifdef FEAT_MOUSE_DEC
10559 "mouse_dec",
10560# endif
10561# ifdef FEAT_MOUSE_GPM
10562 "mouse_gpm",
10563# endif
10564# ifdef FEAT_MOUSE_JSB
10565 "mouse_jsbterm",
10566# endif
10567# ifdef FEAT_MOUSE_NET
10568 "mouse_netterm",
10569# endif
10570# ifdef FEAT_MOUSE_PTERM
10571 "mouse_pterm",
10572# endif
10573# ifdef FEAT_MOUSE_XTERM
10574 "mouse_xterm",
10575# endif
10576#endif
10577#ifdef FEAT_MBYTE
10578 "multi_byte",
10579#endif
10580#ifdef FEAT_MBYTE_IME
10581 "multi_byte_ime",
10582#endif
10583#ifdef FEAT_MULTI_LANG
10584 "multi_lang",
10585#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010586#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010587#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010588 "mzscheme",
10589#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010590#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010591#ifdef FEAT_OLE
10592 "ole",
10593#endif
10594#ifdef FEAT_OSFILETYPE
10595 "osfiletype",
10596#endif
10597#ifdef FEAT_PATH_EXTRA
10598 "path_extra",
10599#endif
10600#ifdef FEAT_PERL
10601#ifndef DYNAMIC_PERL
10602 "perl",
10603#endif
10604#endif
10605#ifdef FEAT_PYTHON
10606#ifndef DYNAMIC_PYTHON
10607 "python",
10608#endif
10609#endif
10610#ifdef FEAT_POSTSCRIPT
10611 "postscript",
10612#endif
10613#ifdef FEAT_PRINTER
10614 "printer",
10615#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000010616#ifdef FEAT_PROFILE
10617 "profile",
10618#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010619#ifdef FEAT_QUICKFIX
10620 "quickfix",
10621#endif
10622#ifdef FEAT_RIGHTLEFT
10623 "rightleft",
10624#endif
10625#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
10626 "ruby",
10627#endif
10628#ifdef FEAT_SCROLLBIND
10629 "scrollbind",
10630#endif
10631#ifdef FEAT_CMDL_INFO
10632 "showcmd",
10633 "cmdline_info",
10634#endif
10635#ifdef FEAT_SIGNS
10636 "signs",
10637#endif
10638#ifdef FEAT_SMARTINDENT
10639 "smartindent",
10640#endif
10641#ifdef FEAT_SNIFF
10642 "sniff",
10643#endif
10644#ifdef FEAT_STL_OPT
10645 "statusline",
10646#endif
10647#ifdef FEAT_SUN_WORKSHOP
10648 "sun_workshop",
10649#endif
10650#ifdef FEAT_NETBEANS_INTG
10651 "netbeans_intg",
10652#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000010653#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010654 "spell",
10655#endif
10656#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010657 "syntax",
10658#endif
10659#if defined(USE_SYSTEM) || !defined(UNIX)
10660 "system",
10661#endif
10662#ifdef FEAT_TAG_BINS
10663 "tag_binary",
10664#endif
10665#ifdef FEAT_TAG_OLDSTATIC
10666 "tag_old_static",
10667#endif
10668#ifdef FEAT_TAG_ANYWHITE
10669 "tag_any_white",
10670#endif
10671#ifdef FEAT_TCL
10672# ifndef DYNAMIC_TCL
10673 "tcl",
10674# endif
10675#endif
10676#ifdef TERMINFO
10677 "terminfo",
10678#endif
10679#ifdef FEAT_TERMRESPONSE
10680 "termresponse",
10681#endif
10682#ifdef FEAT_TEXTOBJ
10683 "textobjects",
10684#endif
10685#ifdef HAVE_TGETENT
10686 "tgetent",
10687#endif
10688#ifdef FEAT_TITLE
10689 "title",
10690#endif
10691#ifdef FEAT_TOOLBAR
10692 "toolbar",
10693#endif
10694#ifdef FEAT_USR_CMDS
10695 "user-commands", /* was accidentally included in 5.4 */
10696 "user_commands",
10697#endif
10698#ifdef FEAT_VIMINFO
10699 "viminfo",
10700#endif
10701#ifdef FEAT_VERTSPLIT
10702 "vertsplit",
10703#endif
10704#ifdef FEAT_VIRTUALEDIT
10705 "virtualedit",
10706#endif
10707#ifdef FEAT_VISUAL
10708 "visual",
10709#endif
10710#ifdef FEAT_VISUALEXTRA
10711 "visualextra",
10712#endif
10713#ifdef FEAT_VREPLACE
10714 "vreplace",
10715#endif
10716#ifdef FEAT_WILDIGN
10717 "wildignore",
10718#endif
10719#ifdef FEAT_WILDMENU
10720 "wildmenu",
10721#endif
10722#ifdef FEAT_WINDOWS
10723 "windows",
10724#endif
10725#ifdef FEAT_WAK
10726 "winaltkeys",
10727#endif
10728#ifdef FEAT_WRITEBACKUP
10729 "writebackup",
10730#endif
10731#ifdef FEAT_XIM
10732 "xim",
10733#endif
10734#ifdef FEAT_XFONTSET
10735 "xfontset",
10736#endif
10737#ifdef USE_XSMP
10738 "xsmp",
10739#endif
10740#ifdef USE_XSMP_INTERACT
10741 "xsmp_interact",
10742#endif
10743#ifdef FEAT_XCLIPBOARD
10744 "xterm_clipboard",
10745#endif
10746#ifdef FEAT_XTERM_SAVE
10747 "xterm_save",
10748#endif
10749#if defined(UNIX) && defined(FEAT_X11)
10750 "X11",
10751#endif
10752 NULL
10753 };
10754
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010755 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010756 for (i = 0; has_list[i] != NULL; ++i)
10757 if (STRICMP(name, has_list[i]) == 0)
10758 {
10759 n = TRUE;
10760 break;
10761 }
10762
10763 if (n == FALSE)
10764 {
10765 if (STRNICMP(name, "patch", 5) == 0)
10766 n = has_patch(atoi((char *)name + 5));
10767 else if (STRICMP(name, "vim_starting") == 0)
10768 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010769#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
10770 else if (STRICMP(name, "balloon_multiline") == 0)
10771 n = multiline_balloon_available();
10772#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010773#ifdef DYNAMIC_TCL
10774 else if (STRICMP(name, "tcl") == 0)
10775 n = tcl_enabled(FALSE);
10776#endif
10777#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
10778 else if (STRICMP(name, "iconv") == 0)
10779 n = iconv_enabled(FALSE);
10780#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010781#ifdef DYNAMIC_MZSCHEME
10782 else if (STRICMP(name, "mzscheme") == 0)
10783 n = mzscheme_enabled(FALSE);
10784#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010785#ifdef DYNAMIC_RUBY
10786 else if (STRICMP(name, "ruby") == 0)
10787 n = ruby_enabled(FALSE);
10788#endif
10789#ifdef DYNAMIC_PYTHON
10790 else if (STRICMP(name, "python") == 0)
10791 n = python_enabled(FALSE);
10792#endif
10793#ifdef DYNAMIC_PERL
10794 else if (STRICMP(name, "perl") == 0)
10795 n = perl_enabled(FALSE);
10796#endif
10797#ifdef FEAT_GUI
10798 else if (STRICMP(name, "gui_running") == 0)
10799 n = (gui.in_use || gui.starting);
10800# ifdef FEAT_GUI_W32
10801 else if (STRICMP(name, "gui_win32s") == 0)
10802 n = gui_is_win32s();
10803# endif
10804# ifdef FEAT_BROWSE
10805 else if (STRICMP(name, "browse") == 0)
10806 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
10807# endif
10808#endif
10809#ifdef FEAT_SYN_HL
10810 else if (STRICMP(name, "syntax_items") == 0)
10811 n = syntax_present(curbuf);
10812#endif
10813#if defined(WIN3264)
10814 else if (STRICMP(name, "win95") == 0)
10815 n = mch_windows95();
10816#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000010817#ifdef FEAT_NETBEANS_INTG
10818 else if (STRICMP(name, "netbeans_enabled") == 0)
10819 n = usingNetbeans;
10820#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010821 }
10822
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010823 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010824}
10825
10826/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000010827 * "has_key()" function
10828 */
10829 static void
10830f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010831 typval_T *argvars;
10832 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010833{
10834 rettv->vval.v_number = 0;
10835 if (argvars[0].v_type != VAR_DICT)
10836 {
10837 EMSG(_(e_dictreq));
10838 return;
10839 }
10840 if (argvars[0].vval.v_dict == NULL)
10841 return;
10842
10843 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010844 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010845}
10846
10847/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010848 * "hasmapto()" function
10849 */
10850 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010851f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010852 typval_T *argvars;
10853 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010854{
10855 char_u *name;
10856 char_u *mode;
10857 char_u buf[NUMBUFLEN];
10858
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010859 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010860 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010861 mode = (char_u *)"nvo";
10862 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010863 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010864
10865 if (map_to_exists(name, mode))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010866 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010867 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010868 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010869}
10870
10871/*
10872 * "histadd()" function
10873 */
10874/*ARGSUSED*/
10875 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010876f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010877 typval_T *argvars;
10878 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010879{
10880#ifdef FEAT_CMDHIST
10881 int histype;
10882 char_u *str;
10883 char_u buf[NUMBUFLEN];
10884#endif
10885
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010886 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010887 if (check_restricted() || check_secure())
10888 return;
10889#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010890 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10891 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010892 if (histype >= 0)
10893 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010894 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010895 if (*str != NUL)
10896 {
10897 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010898 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010899 return;
10900 }
10901 }
10902#endif
10903}
10904
10905/*
10906 * "histdel()" function
10907 */
10908/*ARGSUSED*/
10909 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010910f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010911 typval_T *argvars;
10912 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010913{
10914#ifdef FEAT_CMDHIST
10915 int n;
10916 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010917 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010918
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010919 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10920 if (str == NULL)
10921 n = 0;
10922 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010923 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010924 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010925 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010926 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010927 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010928 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010929 else
10930 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010931 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010932 get_tv_string_buf(&argvars[1], buf));
10933 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010934#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010935 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010936#endif
10937}
10938
10939/*
10940 * "histget()" function
10941 */
10942/*ARGSUSED*/
10943 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010944f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010945 typval_T *argvars;
10946 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010947{
10948#ifdef FEAT_CMDHIST
10949 int type;
10950 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010951 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010952
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010953 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10954 if (str == NULL)
10955 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010956 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010957 {
10958 type = get_histtype(str);
10959 if (argvars[1].v_type == VAR_UNKNOWN)
10960 idx = get_history_idx(type);
10961 else
10962 idx = (int)get_tv_number_chk(&argvars[1], NULL);
10963 /* -1 on type error */
10964 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
10965 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010966#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010967 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010968#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010969 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010970}
10971
10972/*
10973 * "histnr()" function
10974 */
10975/*ARGSUSED*/
10976 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010977f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010978 typval_T *argvars;
10979 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010980{
10981 int i;
10982
10983#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010984 char_u *history = get_tv_string_chk(&argvars[0]);
10985
10986 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010987 if (i >= HIST_CMD && i < HIST_COUNT)
10988 i = get_history_idx(i);
10989 else
10990#endif
10991 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010992 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010993}
10994
10995/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010996 * "highlightID(name)" function
10997 */
10998 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010999f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011000 typval_T *argvars;
11001 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011002{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011003 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011004}
11005
11006/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011007 * "highlight_exists()" function
11008 */
11009 static void
11010f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011011 typval_T *argvars;
11012 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011013{
11014 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
11015}
11016
11017/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011018 * "hostname()" function
11019 */
11020/*ARGSUSED*/
11021 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011022f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011023 typval_T *argvars;
11024 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011025{
11026 char_u hostname[256];
11027
11028 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011029 rettv->v_type = VAR_STRING;
11030 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011031}
11032
11033/*
11034 * iconv() function
11035 */
11036/*ARGSUSED*/
11037 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011038f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011039 typval_T *argvars;
11040 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011041{
11042#ifdef FEAT_MBYTE
11043 char_u buf1[NUMBUFLEN];
11044 char_u buf2[NUMBUFLEN];
11045 char_u *from, *to, *str;
11046 vimconv_T vimconv;
11047#endif
11048
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011049 rettv->v_type = VAR_STRING;
11050 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011051
11052#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011053 str = get_tv_string(&argvars[0]);
11054 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
11055 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011056 vimconv.vc_type = CONV_NONE;
11057 convert_setup(&vimconv, from, to);
11058
11059 /* If the encodings are equal, no conversion needed. */
11060 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011061 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011062 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011063 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011064
11065 convert_setup(&vimconv, NULL, NULL);
11066 vim_free(from);
11067 vim_free(to);
11068#endif
11069}
11070
11071/*
11072 * "indent()" function
11073 */
11074 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011075f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011076 typval_T *argvars;
11077 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011078{
11079 linenr_T lnum;
11080
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011081 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011082 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011083 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011084 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011085 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011086}
11087
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011088/*
11089 * "index()" function
11090 */
11091 static void
11092f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011093 typval_T *argvars;
11094 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011095{
Bram Moolenaar33570922005-01-25 22:26:29 +000011096 list_T *l;
11097 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011098 long idx = 0;
11099 int ic = FALSE;
11100
11101 rettv->vval.v_number = -1;
11102 if (argvars[0].v_type != VAR_LIST)
11103 {
11104 EMSG(_(e_listreq));
11105 return;
11106 }
11107 l = argvars[0].vval.v_list;
11108 if (l != NULL)
11109 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011110 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011111 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011112 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011113 int error = FALSE;
11114
Bram Moolenaar758711c2005-02-02 23:11:38 +000011115 /* Start at specified item. Use the cached index that list_find()
11116 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011117 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000011118 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011119 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011120 ic = get_tv_number_chk(&argvars[3], &error);
11121 if (error)
11122 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011123 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011124
Bram Moolenaar758711c2005-02-02 23:11:38 +000011125 for ( ; item != NULL; item = item->li_next, ++idx)
11126 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011127 {
11128 rettv->vval.v_number = idx;
11129 break;
11130 }
11131 }
11132}
11133
Bram Moolenaar071d4272004-06-13 20:20:40 +000011134static int inputsecret_flag = 0;
11135
11136/*
11137 * "input()" function
11138 * Also handles inputsecret() when inputsecret is set.
11139 */
11140 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011141f_input(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011142 typval_T *argvars;
11143 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011144{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011145 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011146 char_u *p = NULL;
11147 int c;
11148 char_u buf[NUMBUFLEN];
11149 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011150 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011151 int xp_type = EXPAND_NOTHING;
11152 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011153
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011154 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011155
11156#ifdef NO_CONSOLE_INPUT
11157 /* While starting up, there is no place to enter text. */
11158 if (no_console_input())
11159 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011160 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011161 return;
11162 }
11163#endif
11164
11165 cmd_silent = FALSE; /* Want to see the prompt. */
11166 if (prompt != NULL)
11167 {
11168 /* Only the part of the message after the last NL is considered as
11169 * prompt for the command line */
11170 p = vim_strrchr(prompt, '\n');
11171 if (p == NULL)
11172 p = prompt;
11173 else
11174 {
11175 ++p;
11176 c = *p;
11177 *p = NUL;
11178 msg_start();
11179 msg_clr_eos();
11180 msg_puts_attr(prompt, echo_attr);
11181 msg_didout = FALSE;
11182 msg_starthere();
11183 *p = c;
11184 }
11185 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011186
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011187 if (argvars[1].v_type != VAR_UNKNOWN)
11188 {
11189 defstr = get_tv_string_buf_chk(&argvars[1], buf);
11190 if (defstr != NULL)
11191 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011192
Bram Moolenaar4463f292005-09-25 22:20:24 +000011193 if (argvars[2].v_type != VAR_UNKNOWN)
11194 {
11195 char_u *xp_name;
11196 int xp_namelen;
11197 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011198
Bram Moolenaar4463f292005-09-25 22:20:24 +000011199 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011200
Bram Moolenaar4463f292005-09-25 22:20:24 +000011201 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
11202 if (xp_name == NULL)
11203 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011204
Bram Moolenaar4463f292005-09-25 22:20:24 +000011205 xp_namelen = STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011206
Bram Moolenaar4463f292005-09-25 22:20:24 +000011207 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
11208 &xp_arg) == FAIL)
11209 return;
11210 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011211 }
11212
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011213 if (defstr != NULL)
11214 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011215 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
11216 xp_type, xp_arg);
11217
11218 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011219
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011220 /* since the user typed this, no need to wait for return */
11221 need_wait_return = FALSE;
11222 msg_didout = FALSE;
11223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011224 cmd_silent = cmd_silent_save;
11225}
11226
11227/*
11228 * "inputdialog()" function
11229 */
11230 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011231f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011232 typval_T *argvars;
11233 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011234{
11235#if defined(FEAT_GUI_TEXTDIALOG)
11236 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
11237 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
11238 {
11239 char_u *message;
11240 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011241 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000011242
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011243 message = get_tv_string_chk(&argvars[0]);
11244 if (argvars[1].v_type != VAR_UNKNOWN
11245 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000011246 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011247 else
11248 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011249 if (message != NULL && defstr != NULL
11250 && do_dialog(VIM_QUESTION, NULL, message,
11251 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011252 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011253 else
11254 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011255 if (message != NULL && defstr != NULL
11256 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011257 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011258 rettv->vval.v_string = vim_strsave(
11259 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011260 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011261 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011262 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011263 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011264 }
11265 else
11266#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011267 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011268}
11269
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011270/*
11271 * "inputlist()" function
11272 */
11273 static void
11274f_inputlist(argvars, rettv)
11275 typval_T *argvars;
11276 typval_T *rettv;
11277{
11278 listitem_T *li;
11279 int selected;
11280 int mouse_used;
11281
11282 rettv->vval.v_number = 0;
11283#ifdef NO_CONSOLE_INPUT
11284 /* While starting up, there is no place to enter text. */
11285 if (no_console_input())
11286 return;
11287#endif
11288 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
11289 {
11290 EMSG2(_(e_listarg), "inputlist()");
11291 return;
11292 }
11293
11294 msg_start();
11295 lines_left = Rows; /* avoid more prompt */
11296 msg_scroll = TRUE;
11297 msg_clr_eos();
11298
11299 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
11300 {
11301 msg_puts(get_tv_string(&li->li_tv));
11302 msg_putchar('\n');
11303 }
11304
11305 /* Ask for choice. */
11306 selected = prompt_for_number(&mouse_used);
11307 if (mouse_used)
11308 selected -= lines_left;
11309
11310 rettv->vval.v_number = selected;
11311}
11312
11313
Bram Moolenaar071d4272004-06-13 20:20:40 +000011314static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
11315
11316/*
11317 * "inputrestore()" function
11318 */
11319/*ARGSUSED*/
11320 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011321f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011322 typval_T *argvars;
11323 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011324{
11325 if (ga_userinput.ga_len > 0)
11326 {
11327 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011328 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
11329 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011330 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011331 }
11332 else if (p_verbose > 1)
11333 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000011334 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011335 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011336 }
11337}
11338
11339/*
11340 * "inputsave()" function
11341 */
11342/*ARGSUSED*/
11343 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011344f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011345 typval_T *argvars;
11346 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011347{
11348 /* Add an entry to the stack of typehead storage. */
11349 if (ga_grow(&ga_userinput, 1) == OK)
11350 {
11351 save_typeahead((tasave_T *)(ga_userinput.ga_data)
11352 + ga_userinput.ga_len);
11353 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011354 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011355 }
11356 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011357 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011358}
11359
11360/*
11361 * "inputsecret()" function
11362 */
11363 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011364f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011365 typval_T *argvars;
11366 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011367{
11368 ++cmdline_star;
11369 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011370 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011371 --cmdline_star;
11372 --inputsecret_flag;
11373}
11374
11375/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011376 * "insert()" function
11377 */
11378 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011379f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011380 typval_T *argvars;
11381 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011382{
11383 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011384 listitem_T *item;
11385 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011386 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011387
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011388 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011389 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011390 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011391 else if ((l = argvars[0].vval.v_list) != NULL
11392 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011393 {
11394 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011395 before = get_tv_number_chk(&argvars[2], &error);
11396 if (error)
11397 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011398
Bram Moolenaar758711c2005-02-02 23:11:38 +000011399 if (before == l->lv_len)
11400 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011401 else
11402 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011403 item = list_find(l, before);
11404 if (item == NULL)
11405 {
11406 EMSGN(_(e_listidx), before);
11407 l = NULL;
11408 }
11409 }
11410 if (l != NULL)
11411 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011412 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011413 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011414 }
11415 }
11416}
11417
11418/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011419 * "isdirectory()" function
11420 */
11421 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011422f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011423 typval_T *argvars;
11424 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011425{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011426 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011427}
11428
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011429/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011430 * "islocked()" function
11431 */
11432 static void
11433f_islocked(argvars, rettv)
11434 typval_T *argvars;
11435 typval_T *rettv;
11436{
11437 lval_T lv;
11438 char_u *end;
11439 dictitem_T *di;
11440
11441 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000011442 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
11443 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011444 if (end != NULL && lv.ll_name != NULL)
11445 {
11446 if (*end != NUL)
11447 EMSG(_(e_trailing));
11448 else
11449 {
11450 if (lv.ll_tv == NULL)
11451 {
11452 if (check_changedtick(lv.ll_name))
11453 rettv->vval.v_number = 1; /* always locked */
11454 else
11455 {
11456 di = find_var(lv.ll_name, NULL);
11457 if (di != NULL)
11458 {
11459 /* Consider a variable locked when:
11460 * 1. the variable itself is locked
11461 * 2. the value of the variable is locked.
11462 * 3. the List or Dict value is locked.
11463 */
11464 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
11465 || tv_islocked(&di->di_tv));
11466 }
11467 }
11468 }
11469 else if (lv.ll_range)
11470 EMSG(_("E745: Range not allowed"));
11471 else if (lv.ll_newkey != NULL)
11472 EMSG2(_(e_dictkey), lv.ll_newkey);
11473 else if (lv.ll_list != NULL)
11474 /* List item. */
11475 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
11476 else
11477 /* Dictionary item. */
11478 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
11479 }
11480 }
11481
11482 clear_lval(&lv);
11483}
11484
Bram Moolenaar33570922005-01-25 22:26:29 +000011485static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011486
11487/*
11488 * Turn a dict into a list:
11489 * "what" == 0: list of keys
11490 * "what" == 1: list of values
11491 * "what" == 2: list of items
11492 */
11493 static void
11494dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000011495 typval_T *argvars;
11496 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011497 int what;
11498{
Bram Moolenaar33570922005-01-25 22:26:29 +000011499 list_T *l2;
11500 dictitem_T *di;
11501 hashitem_T *hi;
11502 listitem_T *li;
11503 listitem_T *li2;
11504 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011505 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011506
11507 rettv->vval.v_number = 0;
11508 if (argvars[0].v_type != VAR_DICT)
11509 {
11510 EMSG(_(e_dictreq));
11511 return;
11512 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011513 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011514 return;
11515
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011516 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011517 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011518
Bram Moolenaar33570922005-01-25 22:26:29 +000011519 todo = d->dv_hashtab.ht_used;
11520 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011521 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011522 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011523 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011524 --todo;
11525 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011526
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011527 li = listitem_alloc();
11528 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011529 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011530 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011531
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011532 if (what == 0)
11533 {
11534 /* keys() */
11535 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011536 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011537 li->li_tv.vval.v_string = vim_strsave(di->di_key);
11538 }
11539 else if (what == 1)
11540 {
11541 /* values() */
11542 copy_tv(&di->di_tv, &li->li_tv);
11543 }
11544 else
11545 {
11546 /* items() */
11547 l2 = list_alloc();
11548 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011549 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011550 li->li_tv.vval.v_list = l2;
11551 if (l2 == NULL)
11552 break;
11553 ++l2->lv_refcount;
11554
11555 li2 = listitem_alloc();
11556 if (li2 == NULL)
11557 break;
11558 list_append(l2, li2);
11559 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011560 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011561 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
11562
11563 li2 = listitem_alloc();
11564 if (li2 == NULL)
11565 break;
11566 list_append(l2, li2);
11567 copy_tv(&di->di_tv, &li2->li_tv);
11568 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011569 }
11570 }
11571}
11572
11573/*
11574 * "items(dict)" function
11575 */
11576 static void
11577f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011578 typval_T *argvars;
11579 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011580{
11581 dict_list(argvars, rettv, 2);
11582}
11583
Bram Moolenaar071d4272004-06-13 20:20:40 +000011584/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011585 * "join()" function
11586 */
11587 static void
11588f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011589 typval_T *argvars;
11590 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011591{
11592 garray_T ga;
11593 char_u *sep;
11594
11595 rettv->vval.v_number = 0;
11596 if (argvars[0].v_type != VAR_LIST)
11597 {
11598 EMSG(_(e_listreq));
11599 return;
11600 }
11601 if (argvars[0].vval.v_list == NULL)
11602 return;
11603 if (argvars[1].v_type == VAR_UNKNOWN)
11604 sep = (char_u *)" ";
11605 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011606 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011607
11608 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011609
11610 if (sep != NULL)
11611 {
11612 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000011613 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011614 ga_append(&ga, NUL);
11615 rettv->vval.v_string = (char_u *)ga.ga_data;
11616 }
11617 else
11618 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011619}
11620
11621/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011622 * "keys()" function
11623 */
11624 static void
11625f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011626 typval_T *argvars;
11627 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011628{
11629 dict_list(argvars, rettv, 0);
11630}
11631
11632/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011633 * "last_buffer_nr()" function.
11634 */
11635/*ARGSUSED*/
11636 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011637f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011638 typval_T *argvars;
11639 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011640{
11641 int n = 0;
11642 buf_T *buf;
11643
11644 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
11645 if (n < buf->b_fnum)
11646 n = buf->b_fnum;
11647
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011648 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011649}
11650
11651/*
11652 * "len()" function
11653 */
11654 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011655f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011656 typval_T *argvars;
11657 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011658{
11659 switch (argvars[0].v_type)
11660 {
11661 case VAR_STRING:
11662 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011663 rettv->vval.v_number = (varnumber_T)STRLEN(
11664 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011665 break;
11666 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011667 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011668 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011669 case VAR_DICT:
11670 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
11671 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011672 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011673 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011674 break;
11675 }
11676}
11677
Bram Moolenaar33570922005-01-25 22:26:29 +000011678static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011679
11680 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011681libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011682 typval_T *argvars;
11683 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011684 int type;
11685{
11686#ifdef FEAT_LIBCALL
11687 char_u *string_in;
11688 char_u **string_result;
11689 int nr_result;
11690#endif
11691
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011692 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011693 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011694 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011695 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011696 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011697
11698 if (check_restricted() || check_secure())
11699 return;
11700
11701#ifdef FEAT_LIBCALL
11702 /* The first two args must be strings, otherwise its meaningless */
11703 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
11704 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011705 string_in = NULL;
11706 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011707 string_in = argvars[2].vval.v_string;
11708 if (type == VAR_NUMBER)
11709 string_result = NULL;
11710 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011711 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011712 if (mch_libcall(argvars[0].vval.v_string,
11713 argvars[1].vval.v_string,
11714 string_in,
11715 argvars[2].vval.v_number,
11716 string_result,
11717 &nr_result) == OK
11718 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011719 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011720 }
11721#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011722}
11723
11724/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011725 * "libcall()" function
11726 */
11727 static void
11728f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011729 typval_T *argvars;
11730 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011731{
11732 libcall_common(argvars, rettv, VAR_STRING);
11733}
11734
11735/*
11736 * "libcallnr()" function
11737 */
11738 static void
11739f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011740 typval_T *argvars;
11741 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011742{
11743 libcall_common(argvars, rettv, VAR_NUMBER);
11744}
11745
11746/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011747 * "line(string)" function
11748 */
11749 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011750f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011751 typval_T *argvars;
11752 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011753{
11754 linenr_T lnum = 0;
11755 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011756 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011757
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011758 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011759 if (fp != NULL)
11760 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011761 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011762}
11763
11764/*
11765 * "line2byte(lnum)" function
11766 */
11767/*ARGSUSED*/
11768 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011769f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011770 typval_T *argvars;
11771 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011772{
11773#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011774 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011775#else
11776 linenr_T lnum;
11777
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011778 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011779 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011780 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011781 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011782 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
11783 if (rettv->vval.v_number >= 0)
11784 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011785#endif
11786}
11787
11788/*
11789 * "lispindent(lnum)" function
11790 */
11791 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011792f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011793 typval_T *argvars;
11794 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011795{
11796#ifdef FEAT_LISP
11797 pos_T pos;
11798 linenr_T lnum;
11799
11800 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011801 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011802 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11803 {
11804 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011805 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011806 curwin->w_cursor = pos;
11807 }
11808 else
11809#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011810 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011811}
11812
11813/*
11814 * "localtime()" function
11815 */
11816/*ARGSUSED*/
11817 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011818f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011819 typval_T *argvars;
11820 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011821{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011822 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011823}
11824
Bram Moolenaar33570922005-01-25 22:26:29 +000011825static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011826
11827 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011828get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000011829 typval_T *argvars;
11830 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011831 int exact;
11832{
11833 char_u *keys;
11834 char_u *which;
11835 char_u buf[NUMBUFLEN];
11836 char_u *keys_buf = NULL;
11837 char_u *rhs;
11838 int mode;
11839 garray_T ga;
11840
11841 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011842 rettv->v_type = VAR_STRING;
11843 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011844
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011845 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011846 if (*keys == NUL)
11847 return;
11848
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011849 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011850 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011851 else
11852 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011853 if (which == NULL)
11854 return;
11855
Bram Moolenaar071d4272004-06-13 20:20:40 +000011856 mode = get_map_mode(&which, 0);
11857
11858 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000011859 rhs = check_map(keys, mode, exact, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011860 vim_free(keys_buf);
11861 if (rhs != NULL)
11862 {
11863 ga_init(&ga);
11864 ga.ga_itemsize = 1;
11865 ga.ga_growsize = 40;
11866
11867 while (*rhs != NUL)
11868 ga_concat(&ga, str2special(&rhs, FALSE));
11869
11870 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011871 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011872 }
11873}
11874
11875/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011876 * "map()" function
11877 */
11878 static void
11879f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011880 typval_T *argvars;
11881 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011882{
11883 filter_map(argvars, rettv, TRUE);
11884}
11885
11886/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011887 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011888 */
11889 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011890f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011891 typval_T *argvars;
11892 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011893{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011894 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011895}
11896
11897/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011898 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011899 */
11900 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011901f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011902 typval_T *argvars;
11903 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011904{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011905 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011906}
11907
Bram Moolenaar33570922005-01-25 22:26:29 +000011908static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011909
11910 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011911find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011912 typval_T *argvars;
11913 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011914 int type;
11915{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011916 char_u *str = NULL;
11917 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011918 char_u *pat;
11919 regmatch_T regmatch;
11920 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011921 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011922 char_u *save_cpo;
11923 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011924 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011925 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011926 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011927 list_T *l = NULL;
11928 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011929 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011930 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011931
11932 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11933 save_cpo = p_cpo;
11934 p_cpo = (char_u *)"";
11935
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011936 rettv->vval.v_number = -1;
11937 if (type == 3)
11938 {
11939 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011940 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011941 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011942 }
11943 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011944 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011945 rettv->v_type = VAR_STRING;
11946 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011948
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011949 if (argvars[0].v_type == VAR_LIST)
11950 {
11951 if ((l = argvars[0].vval.v_list) == NULL)
11952 goto theend;
11953 li = l->lv_first;
11954 }
11955 else
11956 expr = str = get_tv_string(&argvars[0]);
11957
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011958 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11959 if (pat == NULL)
11960 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011961
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011962 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011963 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011964 int error = FALSE;
11965
11966 start = get_tv_number_chk(&argvars[2], &error);
11967 if (error)
11968 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011969 if (l != NULL)
11970 {
11971 li = list_find(l, start);
11972 if (li == NULL)
11973 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000011974 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011975 }
11976 else
11977 {
11978 if (start < 0)
11979 start = 0;
11980 if (start > (long)STRLEN(str))
11981 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011982 /* When "count" argument is there ignore matches before "start",
11983 * otherwise skip part of the string. Differs when pattern is "^"
11984 * or "\<". */
11985 if (argvars[3].v_type != VAR_UNKNOWN)
11986 startcol = start;
11987 else
11988 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011989 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011990
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011991 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011992 nth = get_tv_number_chk(&argvars[3], &error);
11993 if (error)
11994 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011995 }
11996
11997 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11998 if (regmatch.regprog != NULL)
11999 {
12000 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012001
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012002 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012003 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012004 if (l != NULL)
12005 {
12006 if (li == NULL)
12007 {
12008 match = FALSE;
12009 break;
12010 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012011 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012012 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012013 if (str == NULL)
12014 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012015 }
12016
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012017 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012018
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012019 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012020 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012021 if (l == NULL && !match)
12022 break;
12023
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012024 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012025 if (l != NULL)
12026 {
12027 li = li->li_next;
12028 ++idx;
12029 }
12030 else
12031 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012032#ifdef FEAT_MBYTE
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012033 startcol = regmatch.startp[0]
12034 + (*mb_ptr2len)(regmatch.startp[0]) - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012035#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012036 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012037#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012038 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012039 }
12040
12041 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012042 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012043 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012044 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012045 int i;
12046
12047 /* return list with matched string and submatches */
12048 for (i = 0; i < NSUBEXP; ++i)
12049 {
12050 if (regmatch.endp[i] == NULL)
12051 break;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012052 if (list_append_string(rettv->vval.v_list,
12053 regmatch.startp[i],
12054 (int)(regmatch.endp[i] - regmatch.startp[i]))
12055 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012056 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012057 }
12058 }
12059 else if (type == 2)
12060 {
12061 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012062 if (l != NULL)
12063 copy_tv(&li->li_tv, rettv);
12064 else
12065 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000012066 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012067 }
12068 else if (l != NULL)
12069 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012070 else
12071 {
12072 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012073 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012074 (varnumber_T)(regmatch.startp[0] - str);
12075 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012076 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012077 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012078 rettv->vval.v_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012079 }
12080 }
12081 vim_free(regmatch.regprog);
12082 }
12083
12084theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012085 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012086 p_cpo = save_cpo;
12087}
12088
12089/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012090 * "match()" function
12091 */
12092 static void
12093f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012094 typval_T *argvars;
12095 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012096{
12097 find_some_match(argvars, rettv, 1);
12098}
12099
12100/*
12101 * "matchend()" function
12102 */
12103 static void
12104f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012105 typval_T *argvars;
12106 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012107{
12108 find_some_match(argvars, rettv, 0);
12109}
12110
12111/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012112 * "matchlist()" function
12113 */
12114 static void
12115f_matchlist(argvars, rettv)
12116 typval_T *argvars;
12117 typval_T *rettv;
12118{
12119 find_some_match(argvars, rettv, 3);
12120}
12121
12122/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012123 * "matchstr()" function
12124 */
12125 static void
12126f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012127 typval_T *argvars;
12128 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012129{
12130 find_some_match(argvars, rettv, 2);
12131}
12132
Bram Moolenaar33570922005-01-25 22:26:29 +000012133static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012134
12135 static void
12136max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000012137 typval_T *argvars;
12138 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012139 int domax;
12140{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012141 long n = 0;
12142 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012143 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012144
12145 if (argvars[0].v_type == VAR_LIST)
12146 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012147 list_T *l;
12148 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012149
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012150 l = argvars[0].vval.v_list;
12151 if (l != NULL)
12152 {
12153 li = l->lv_first;
12154 if (li != NULL)
12155 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012156 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012157 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012158 {
12159 li = li->li_next;
12160 if (li == NULL)
12161 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012162 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012163 if (domax ? i > n : i < n)
12164 n = i;
12165 }
12166 }
12167 }
12168 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012169 else if (argvars[0].v_type == VAR_DICT)
12170 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012171 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012172 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000012173 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012174 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012175
12176 d = argvars[0].vval.v_dict;
12177 if (d != NULL)
12178 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012179 todo = d->dv_hashtab.ht_used;
12180 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012181 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012182 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000012183 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012184 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012185 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012186 if (first)
12187 {
12188 n = i;
12189 first = FALSE;
12190 }
12191 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012192 n = i;
12193 }
12194 }
12195 }
12196 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012197 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000012198 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012199 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012200}
12201
12202/*
12203 * "max()" function
12204 */
12205 static void
12206f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012207 typval_T *argvars;
12208 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012209{
12210 max_min(argvars, rettv, TRUE);
12211}
12212
12213/*
12214 * "min()" function
12215 */
12216 static void
12217f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012218 typval_T *argvars;
12219 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012220{
12221 max_min(argvars, rettv, FALSE);
12222}
12223
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012224static int mkdir_recurse __ARGS((char_u *dir, int prot));
12225
12226/*
12227 * Create the directory in which "dir" is located, and higher levels when
12228 * needed.
12229 */
12230 static int
12231mkdir_recurse(dir, prot)
12232 char_u *dir;
12233 int prot;
12234{
12235 char_u *p;
12236 char_u *updir;
12237 int r = FAIL;
12238
12239 /* Get end of directory name in "dir".
12240 * We're done when it's "/" or "c:/". */
12241 p = gettail_sep(dir);
12242 if (p <= get_past_head(dir))
12243 return OK;
12244
12245 /* If the directory exists we're done. Otherwise: create it.*/
12246 updir = vim_strnsave(dir, (int)(p - dir));
12247 if (updir == NULL)
12248 return FAIL;
12249 if (mch_isdir(updir))
12250 r = OK;
12251 else if (mkdir_recurse(updir, prot) == OK)
12252 r = vim_mkdir_emsg(updir, prot);
12253 vim_free(updir);
12254 return r;
12255}
12256
12257#ifdef vim_mkdir
12258/*
12259 * "mkdir()" function
12260 */
12261 static void
12262f_mkdir(argvars, rettv)
12263 typval_T *argvars;
12264 typval_T *rettv;
12265{
12266 char_u *dir;
12267 char_u buf[NUMBUFLEN];
12268 int prot = 0755;
12269
12270 rettv->vval.v_number = FAIL;
12271 if (check_restricted() || check_secure())
12272 return;
12273
12274 dir = get_tv_string_buf(&argvars[0], buf);
12275 if (argvars[1].v_type != VAR_UNKNOWN)
12276 {
12277 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012278 prot = get_tv_number_chk(&argvars[2], NULL);
12279 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012280 mkdir_recurse(dir, prot);
12281 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012282 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012283}
12284#endif
12285
Bram Moolenaar0d660222005-01-07 21:51:51 +000012286/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012287 * "mode()" function
12288 */
12289/*ARGSUSED*/
12290 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012291f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012292 typval_T *argvars;
12293 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012294{
12295 char_u buf[2];
12296
12297#ifdef FEAT_VISUAL
12298 if (VIsual_active)
12299 {
12300 if (VIsual_select)
12301 buf[0] = VIsual_mode + 's' - 'v';
12302 else
12303 buf[0] = VIsual_mode;
12304 }
12305 else
12306#endif
12307 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
12308 buf[0] = 'r';
12309 else if (State & INSERT)
12310 {
12311 if (State & REPLACE_FLAG)
12312 buf[0] = 'R';
12313 else
12314 buf[0] = 'i';
12315 }
12316 else if (State & CMDLINE)
12317 buf[0] = 'c';
12318 else
12319 buf[0] = 'n';
12320
12321 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012322 rettv->vval.v_string = vim_strsave(buf);
12323 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012324}
12325
12326/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012327 * "nextnonblank()" function
12328 */
12329 static void
12330f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012331 typval_T *argvars;
12332 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012333{
12334 linenr_T lnum;
12335
12336 for (lnum = get_tv_lnum(argvars); ; ++lnum)
12337 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012338 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012339 {
12340 lnum = 0;
12341 break;
12342 }
12343 if (*skipwhite(ml_get(lnum)) != NUL)
12344 break;
12345 }
12346 rettv->vval.v_number = lnum;
12347}
12348
12349/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012350 * "nr2char()" function
12351 */
12352 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012353f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012354 typval_T *argvars;
12355 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012356{
12357 char_u buf[NUMBUFLEN];
12358
12359#ifdef FEAT_MBYTE
12360 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012361 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012362 else
12363#endif
12364 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012365 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012366 buf[1] = NUL;
12367 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012368 rettv->v_type = VAR_STRING;
12369 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012370}
12371
12372/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012373 * "prevnonblank()" function
12374 */
12375 static void
12376f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012377 typval_T *argvars;
12378 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012379{
12380 linenr_T lnum;
12381
12382 lnum = get_tv_lnum(argvars);
12383 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
12384 lnum = 0;
12385 else
12386 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
12387 --lnum;
12388 rettv->vval.v_number = lnum;
12389}
12390
Bram Moolenaara6c840d2005-08-22 22:59:46 +000012391#ifdef HAVE_STDARG_H
12392/* This dummy va_list is here because:
12393 * - passing a NULL pointer doesn't work when va_list isn't a pointer
12394 * - locally in the function results in a "used before set" warning
12395 * - using va_start() to initialize it gives "function with fixed args" error */
12396static va_list ap;
12397#endif
12398
Bram Moolenaar8c711452005-01-14 21:53:12 +000012399/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012400 * "printf()" function
12401 */
12402 static void
12403f_printf(argvars, rettv)
12404 typval_T *argvars;
12405 typval_T *rettv;
12406{
12407 rettv->v_type = VAR_STRING;
12408 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000012409#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012410 {
12411 char_u buf[NUMBUFLEN];
12412 int len;
12413 char_u *s;
12414 int saved_did_emsg = did_emsg;
12415 char *fmt;
12416
12417 /* Get the required length, allocate the buffer and do it for real. */
12418 did_emsg = FALSE;
12419 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012420 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012421 if (!did_emsg)
12422 {
12423 s = alloc(len + 1);
12424 if (s != NULL)
12425 {
12426 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012427 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012428 }
12429 }
12430 did_emsg |= saved_did_emsg;
12431 }
12432#endif
12433}
12434
12435/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000012436 * "pumvisible()" function
12437 */
12438/*ARGSUSED*/
12439 static void
12440f_pumvisible(argvars, rettv)
12441 typval_T *argvars;
12442 typval_T *rettv;
12443{
12444 rettv->vval.v_number = 0;
12445#ifdef FEAT_INS_EXPAND
12446 if (pum_visible())
12447 rettv->vval.v_number = 1;
12448#endif
12449}
12450
12451/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012452 * "range()" function
12453 */
12454 static void
12455f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012456 typval_T *argvars;
12457 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012458{
12459 long start;
12460 long end;
12461 long stride = 1;
12462 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012463 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012464
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012465 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012466 if (argvars[1].v_type == VAR_UNKNOWN)
12467 {
12468 end = start - 1;
12469 start = 0;
12470 }
12471 else
12472 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012473 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012474 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012475 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012476 }
12477
12478 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012479 if (error)
12480 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000012481 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012482 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000012483 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012484 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012485 else
12486 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012487 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012488 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012489 if (list_append_number(rettv->vval.v_list,
12490 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012491 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012492 }
12493}
12494
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012495/*
12496 * "readfile()" function
12497 */
12498 static void
12499f_readfile(argvars, rettv)
12500 typval_T *argvars;
12501 typval_T *rettv;
12502{
12503 int binary = FALSE;
12504 char_u *fname;
12505 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012506 listitem_T *li;
12507#define FREAD_SIZE 200 /* optimized for text lines */
12508 char_u buf[FREAD_SIZE];
12509 int readlen; /* size of last fread() */
12510 int buflen; /* nr of valid chars in buf[] */
12511 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
12512 int tolist; /* first byte in buf[] still to be put in list */
12513 int chop; /* how many CR to chop off */
12514 char_u *prev = NULL; /* previously read bytes, if any */
12515 int prevlen = 0; /* length of "prev" if not NULL */
12516 char_u *s;
12517 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012518 long maxline = MAXLNUM;
12519 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012520
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012521 if (argvars[1].v_type != VAR_UNKNOWN)
12522 {
12523 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
12524 binary = TRUE;
12525 if (argvars[2].v_type != VAR_UNKNOWN)
12526 maxline = get_tv_number(&argvars[2]);
12527 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012528
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012529 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012530 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012531
12532 /* Always open the file in binary mode, library functions have a mind of
12533 * their own about CR-LF conversion. */
12534 fname = get_tv_string(&argvars[0]);
12535 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
12536 {
12537 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
12538 return;
12539 }
12540
12541 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012542 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012543 {
12544 readlen = fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
12545 buflen = filtd + readlen;
12546 tolist = 0;
12547 for ( ; filtd < buflen || readlen <= 0; ++filtd)
12548 {
12549 if (buf[filtd] == '\n' || readlen <= 0)
12550 {
12551 /* Only when in binary mode add an empty list item when the
12552 * last line ends in a '\n'. */
12553 if (!binary && readlen == 0 && filtd == 0)
12554 break;
12555
12556 /* Found end-of-line or end-of-file: add a text line to the
12557 * list. */
12558 chop = 0;
12559 if (!binary)
12560 while (filtd - chop - 1 >= tolist
12561 && buf[filtd - chop - 1] == '\r')
12562 ++chop;
12563 len = filtd - tolist - chop;
12564 if (prev == NULL)
12565 s = vim_strnsave(buf + tolist, len);
12566 else
12567 {
12568 s = alloc((unsigned)(prevlen + len + 1));
12569 if (s != NULL)
12570 {
12571 mch_memmove(s, prev, prevlen);
12572 vim_free(prev);
12573 prev = NULL;
12574 mch_memmove(s + prevlen, buf + tolist, len);
12575 s[prevlen + len] = NUL;
12576 }
12577 }
12578 tolist = filtd + 1;
12579
12580 li = listitem_alloc();
12581 if (li == NULL)
12582 {
12583 vim_free(s);
12584 break;
12585 }
12586 li->li_tv.v_type = VAR_STRING;
12587 li->li_tv.v_lock = 0;
12588 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012589 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012590
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012591 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012592 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012593 if (readlen <= 0)
12594 break;
12595 }
12596 else if (buf[filtd] == NUL)
12597 buf[filtd] = '\n';
12598 }
12599 if (readlen <= 0)
12600 break;
12601
12602 if (tolist == 0)
12603 {
12604 /* "buf" is full, need to move text to an allocated buffer */
12605 if (prev == NULL)
12606 {
12607 prev = vim_strnsave(buf, buflen);
12608 prevlen = buflen;
12609 }
12610 else
12611 {
12612 s = alloc((unsigned)(prevlen + buflen));
12613 if (s != NULL)
12614 {
12615 mch_memmove(s, prev, prevlen);
12616 mch_memmove(s + prevlen, buf, buflen);
12617 vim_free(prev);
12618 prev = s;
12619 prevlen += buflen;
12620 }
12621 }
12622 filtd = 0;
12623 }
12624 else
12625 {
12626 mch_memmove(buf, buf + tolist, buflen - tolist);
12627 filtd -= tolist;
12628 }
12629 }
12630
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012631 /*
12632 * For a negative line count use only the lines at the end of the file,
12633 * free the rest.
12634 */
12635 if (maxline < 0)
12636 while (cnt > -maxline)
12637 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012638 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012639 --cnt;
12640 }
12641
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012642 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012643 fclose(fd);
12644}
12645
12646
Bram Moolenaar0d660222005-01-07 21:51:51 +000012647#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
12648static void make_connection __ARGS((void));
12649static int check_connection __ARGS((void));
12650
12651 static void
12652make_connection()
12653{
12654 if (X_DISPLAY == NULL
12655# ifdef FEAT_GUI
12656 && !gui.in_use
12657# endif
12658 )
12659 {
12660 x_force_connect = TRUE;
12661 setup_term_clip();
12662 x_force_connect = FALSE;
12663 }
12664}
12665
12666 static int
12667check_connection()
12668{
12669 make_connection();
12670 if (X_DISPLAY == NULL)
12671 {
12672 EMSG(_("E240: No connection to Vim server"));
12673 return FAIL;
12674 }
12675 return OK;
12676}
12677#endif
12678
12679#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012680static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012681
12682 static void
12683remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000012684 typval_T *argvars;
12685 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012686 int expr;
12687{
12688 char_u *server_name;
12689 char_u *keys;
12690 char_u *r = NULL;
12691 char_u buf[NUMBUFLEN];
12692# ifdef WIN32
12693 HWND w;
12694# else
12695 Window w;
12696# endif
12697
12698 if (check_restricted() || check_secure())
12699 return;
12700
12701# ifdef FEAT_X11
12702 if (check_connection() == FAIL)
12703 return;
12704# endif
12705
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012706 server_name = get_tv_string_chk(&argvars[0]);
12707 if (server_name == NULL)
12708 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012709 keys = get_tv_string_buf(&argvars[1], buf);
12710# ifdef WIN32
12711 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
12712# else
12713 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
12714 < 0)
12715# endif
12716 {
12717 if (r != NULL)
12718 EMSG(r); /* sending worked but evaluation failed */
12719 else
12720 EMSG2(_("E241: Unable to send to %s"), server_name);
12721 return;
12722 }
12723
12724 rettv->vval.v_string = r;
12725
12726 if (argvars[2].v_type != VAR_UNKNOWN)
12727 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012728 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000012729 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012730 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012731
12732 sprintf((char *)str, "0x%x", (unsigned int)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000012733 v.di_tv.v_type = VAR_STRING;
12734 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012735 idvar = get_tv_string_chk(&argvars[2]);
12736 if (idvar != NULL)
12737 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012738 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012739 }
12740}
12741#endif
12742
12743/*
12744 * "remote_expr()" function
12745 */
12746/*ARGSUSED*/
12747 static void
12748f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012749 typval_T *argvars;
12750 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012751{
12752 rettv->v_type = VAR_STRING;
12753 rettv->vval.v_string = NULL;
12754#ifdef FEAT_CLIENTSERVER
12755 remote_common(argvars, rettv, TRUE);
12756#endif
12757}
12758
12759/*
12760 * "remote_foreground()" function
12761 */
12762/*ARGSUSED*/
12763 static void
12764f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012765 typval_T *argvars;
12766 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012767{
12768 rettv->vval.v_number = 0;
12769#ifdef FEAT_CLIENTSERVER
12770# ifdef WIN32
12771 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012772 {
12773 char_u *server_name = get_tv_string_chk(&argvars[0]);
12774
12775 if (server_name != NULL)
12776 serverForeground(server_name);
12777 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012778# else
12779 /* Send a foreground() expression to the server. */
12780 argvars[1].v_type = VAR_STRING;
12781 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
12782 argvars[2].v_type = VAR_UNKNOWN;
12783 remote_common(argvars, rettv, TRUE);
12784 vim_free(argvars[1].vval.v_string);
12785# endif
12786#endif
12787}
12788
12789/*ARGSUSED*/
12790 static void
12791f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012792 typval_T *argvars;
12793 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012794{
12795#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012796 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012797 char_u *s = NULL;
12798# ifdef WIN32
12799 int n = 0;
12800# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012801 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012802
12803 if (check_restricted() || check_secure())
12804 {
12805 rettv->vval.v_number = -1;
12806 return;
12807 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012808 serverid = get_tv_string_chk(&argvars[0]);
12809 if (serverid == NULL)
12810 {
12811 rettv->vval.v_number = -1;
12812 return; /* type error; errmsg already given */
12813 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012814# ifdef WIN32
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012815 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012816 if (n == 0)
12817 rettv->vval.v_number = -1;
12818 else
12819 {
12820 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
12821 rettv->vval.v_number = (s != NULL);
12822 }
12823# else
12824 rettv->vval.v_number = 0;
12825 if (check_connection() == FAIL)
12826 return;
12827
12828 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012829 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012830# endif
12831
12832 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
12833 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012834 char_u *retvar;
12835
Bram Moolenaar33570922005-01-25 22:26:29 +000012836 v.di_tv.v_type = VAR_STRING;
12837 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012838 retvar = get_tv_string_chk(&argvars[1]);
12839 if (retvar != NULL)
12840 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012841 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012842 }
12843#else
12844 rettv->vval.v_number = -1;
12845#endif
12846}
12847
12848/*ARGSUSED*/
12849 static void
12850f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012851 typval_T *argvars;
12852 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012853{
12854 char_u *r = NULL;
12855
12856#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012857 char_u *serverid = get_tv_string_chk(&argvars[0]);
12858
12859 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000012860 {
12861# ifdef WIN32
12862 /* The server's HWND is encoded in the 'id' parameter */
12863 int n = 0;
12864
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012865 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012866 if (n != 0)
12867 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
12868 if (r == NULL)
12869# else
12870 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012871 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012872# endif
12873 EMSG(_("E277: Unable to read a server reply"));
12874 }
12875#endif
12876 rettv->v_type = VAR_STRING;
12877 rettv->vval.v_string = r;
12878}
12879
12880/*
12881 * "remote_send()" function
12882 */
12883/*ARGSUSED*/
12884 static void
12885f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012886 typval_T *argvars;
12887 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012888{
12889 rettv->v_type = VAR_STRING;
12890 rettv->vval.v_string = NULL;
12891#ifdef FEAT_CLIENTSERVER
12892 remote_common(argvars, rettv, FALSE);
12893#endif
12894}
12895
12896/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012897 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012898 */
12899 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012900f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012901 typval_T *argvars;
12902 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012903{
Bram Moolenaar33570922005-01-25 22:26:29 +000012904 list_T *l;
12905 listitem_T *item, *item2;
12906 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012907 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012908 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012909 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000012910 dict_T *d;
12911 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012912
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012913 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012914 if (argvars[0].v_type == VAR_DICT)
12915 {
12916 if (argvars[2].v_type != VAR_UNKNOWN)
12917 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012918 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar758711c2005-02-02 23:11:38 +000012919 && !tv_check_lock(d->dv_lock, (char_u *)"remove()"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000012920 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012921 key = get_tv_string_chk(&argvars[1]);
12922 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012923 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012924 di = dict_find(d, key, -1);
12925 if (di == NULL)
12926 EMSG2(_(e_dictkey), key);
12927 else
12928 {
12929 *rettv = di->di_tv;
12930 init_tv(&di->di_tv);
12931 dictitem_remove(d, di);
12932 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012933 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012934 }
12935 }
12936 else if (argvars[0].v_type != VAR_LIST)
12937 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012938 else if ((l = argvars[0].vval.v_list) != NULL
12939 && !tv_check_lock(l->lv_lock, (char_u *)"remove()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012940 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012941 int error = FALSE;
12942
12943 idx = get_tv_number_chk(&argvars[1], &error);
12944 if (error)
12945 ; /* type error: do nothing, errmsg already given */
12946 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012947 EMSGN(_(e_listidx), idx);
12948 else
12949 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012950 if (argvars[2].v_type == VAR_UNKNOWN)
12951 {
12952 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012953 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012954 *rettv = item->li_tv;
12955 vim_free(item);
12956 }
12957 else
12958 {
12959 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012960 end = get_tv_number_chk(&argvars[2], &error);
12961 if (error)
12962 ; /* type error: do nothing */
12963 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012964 EMSGN(_(e_listidx), end);
12965 else
12966 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012967 int cnt = 0;
12968
12969 for (li = item; li != NULL; li = li->li_next)
12970 {
12971 ++cnt;
12972 if (li == item2)
12973 break;
12974 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012975 if (li == NULL) /* didn't find "item2" after "item" */
12976 EMSG(_(e_invrange));
12977 else
12978 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012979 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012980 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012981 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012982 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012983 l->lv_first = item;
12984 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012985 item->li_prev = NULL;
12986 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012987 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012988 }
12989 }
12990 }
12991 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012992 }
12993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012994}
12995
12996/*
12997 * "rename({from}, {to})" function
12998 */
12999 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013000f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013001 typval_T *argvars;
13002 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013003{
13004 char_u buf[NUMBUFLEN];
13005
13006 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013007 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013008 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013009 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
13010 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013011}
13012
13013/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013014 * "repeat()" function
13015 */
13016/*ARGSUSED*/
13017 static void
13018f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013019 typval_T *argvars;
13020 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013021{
13022 char_u *p;
13023 int n;
13024 int slen;
13025 int len;
13026 char_u *r;
13027 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013028
13029 n = get_tv_number(&argvars[1]);
13030 if (argvars[0].v_type == VAR_LIST)
13031 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013032 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013033 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013034 if (list_extend(rettv->vval.v_list,
13035 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013036 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013037 }
13038 else
13039 {
13040 p = get_tv_string(&argvars[0]);
13041 rettv->v_type = VAR_STRING;
13042 rettv->vval.v_string = NULL;
13043
13044 slen = (int)STRLEN(p);
13045 len = slen * n;
13046 if (len <= 0)
13047 return;
13048
13049 r = alloc(len + 1);
13050 if (r != NULL)
13051 {
13052 for (i = 0; i < n; i++)
13053 mch_memmove(r + i * slen, p, (size_t)slen);
13054 r[len] = NUL;
13055 }
13056
13057 rettv->vval.v_string = r;
13058 }
13059}
13060
13061/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013062 * "resolve()" function
13063 */
13064 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013065f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013066 typval_T *argvars;
13067 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013068{
13069 char_u *p;
13070
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013071 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013072#ifdef FEAT_SHORTCUT
13073 {
13074 char_u *v = NULL;
13075
13076 v = mch_resolve_shortcut(p);
13077 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013078 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013079 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013080 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013081 }
13082#else
13083# ifdef HAVE_READLINK
13084 {
13085 char_u buf[MAXPATHL + 1];
13086 char_u *cpy;
13087 int len;
13088 char_u *remain = NULL;
13089 char_u *q;
13090 int is_relative_to_current = FALSE;
13091 int has_trailing_pathsep = FALSE;
13092 int limit = 100;
13093
13094 p = vim_strsave(p);
13095
13096 if (p[0] == '.' && (vim_ispathsep(p[1])
13097 || (p[1] == '.' && (vim_ispathsep(p[2])))))
13098 is_relative_to_current = TRUE;
13099
13100 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013101 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013102 has_trailing_pathsep = TRUE;
13103
13104 q = getnextcomp(p);
13105 if (*q != NUL)
13106 {
13107 /* Separate the first path component in "p", and keep the
13108 * remainder (beginning with the path separator). */
13109 remain = vim_strsave(q - 1);
13110 q[-1] = NUL;
13111 }
13112
13113 for (;;)
13114 {
13115 for (;;)
13116 {
13117 len = readlink((char *)p, (char *)buf, MAXPATHL);
13118 if (len <= 0)
13119 break;
13120 buf[len] = NUL;
13121
13122 if (limit-- == 0)
13123 {
13124 vim_free(p);
13125 vim_free(remain);
13126 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013127 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013128 goto fail;
13129 }
13130
13131 /* Ensure that the result will have a trailing path separator
13132 * if the argument has one. */
13133 if (remain == NULL && has_trailing_pathsep)
13134 add_pathsep(buf);
13135
13136 /* Separate the first path component in the link value and
13137 * concatenate the remainders. */
13138 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
13139 if (*q != NUL)
13140 {
13141 if (remain == NULL)
13142 remain = vim_strsave(q - 1);
13143 else
13144 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000013145 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013146 if (cpy != NULL)
13147 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013148 vim_free(remain);
13149 remain = cpy;
13150 }
13151 }
13152 q[-1] = NUL;
13153 }
13154
13155 q = gettail(p);
13156 if (q > p && *q == NUL)
13157 {
13158 /* Ignore trailing path separator. */
13159 q[-1] = NUL;
13160 q = gettail(p);
13161 }
13162 if (q > p && !mch_isFullName(buf))
13163 {
13164 /* symlink is relative to directory of argument */
13165 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
13166 if (cpy != NULL)
13167 {
13168 STRCPY(cpy, p);
13169 STRCPY(gettail(cpy), buf);
13170 vim_free(p);
13171 p = cpy;
13172 }
13173 }
13174 else
13175 {
13176 vim_free(p);
13177 p = vim_strsave(buf);
13178 }
13179 }
13180
13181 if (remain == NULL)
13182 break;
13183
13184 /* Append the first path component of "remain" to "p". */
13185 q = getnextcomp(remain + 1);
13186 len = q - remain - (*q != NUL);
13187 cpy = vim_strnsave(p, STRLEN(p) + len);
13188 if (cpy != NULL)
13189 {
13190 STRNCAT(cpy, remain, len);
13191 vim_free(p);
13192 p = cpy;
13193 }
13194 /* Shorten "remain". */
13195 if (*q != NUL)
13196 STRCPY(remain, q - 1);
13197 else
13198 {
13199 vim_free(remain);
13200 remain = NULL;
13201 }
13202 }
13203
13204 /* If the result is a relative path name, make it explicitly relative to
13205 * the current directory if and only if the argument had this form. */
13206 if (!vim_ispathsep(*p))
13207 {
13208 if (is_relative_to_current
13209 && *p != NUL
13210 && !(p[0] == '.'
13211 && (p[1] == NUL
13212 || vim_ispathsep(p[1])
13213 || (p[1] == '.'
13214 && (p[2] == NUL
13215 || vim_ispathsep(p[2]))))))
13216 {
13217 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013218 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013219 if (cpy != NULL)
13220 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013221 vim_free(p);
13222 p = cpy;
13223 }
13224 }
13225 else if (!is_relative_to_current)
13226 {
13227 /* Strip leading "./". */
13228 q = p;
13229 while (q[0] == '.' && vim_ispathsep(q[1]))
13230 q += 2;
13231 if (q > p)
13232 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
13233 }
13234 }
13235
13236 /* Ensure that the result will have no trailing path separator
13237 * if the argument had none. But keep "/" or "//". */
13238 if (!has_trailing_pathsep)
13239 {
13240 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013241 if (after_pathsep(p, q))
13242 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013243 }
13244
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013245 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013246 }
13247# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013248 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013249# endif
13250#endif
13251
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013252 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013253
13254#ifdef HAVE_READLINK
13255fail:
13256#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013257 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013258}
13259
13260/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013261 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013262 */
13263 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013264f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013265 typval_T *argvars;
13266 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013267{
Bram Moolenaar33570922005-01-25 22:26:29 +000013268 list_T *l;
13269 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013270
Bram Moolenaar0d660222005-01-07 21:51:51 +000013271 rettv->vval.v_number = 0;
13272 if (argvars[0].v_type != VAR_LIST)
13273 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013274 else if ((l = argvars[0].vval.v_list) != NULL
13275 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013276 {
13277 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013278 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013279 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013280 while (li != NULL)
13281 {
13282 ni = li->li_prev;
13283 list_append(l, li);
13284 li = ni;
13285 }
13286 rettv->vval.v_list = l;
13287 rettv->v_type = VAR_LIST;
13288 ++l->lv_refcount;
13289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013290}
13291
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013292#define SP_NOMOVE 0x01 /* don't move cursor */
13293#define SP_REPEAT 0x02 /* repeat to find outer pair */
13294#define SP_RETCOUNT 0x04 /* return matchcount */
13295#define SP_SETPCMARK 0x08 /* set previous context mark */
13296#define SP_START 0x10 /* accept match at start position */
13297#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
13298#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013299
Bram Moolenaar33570922005-01-25 22:26:29 +000013300static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013301
13302/*
13303 * Get flags for a search function.
13304 * Possibly sets "p_ws".
13305 * Returns BACKWARD, FORWARD or zero (for an error).
13306 */
13307 static int
13308get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013309 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013310 int *flagsp;
13311{
13312 int dir = FORWARD;
13313 char_u *flags;
13314 char_u nbuf[NUMBUFLEN];
13315 int mask;
13316
13317 if (varp->v_type != VAR_UNKNOWN)
13318 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013319 flags = get_tv_string_buf_chk(varp, nbuf);
13320 if (flags == NULL)
13321 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013322 while (*flags != NUL)
13323 {
13324 switch (*flags)
13325 {
13326 case 'b': dir = BACKWARD; break;
13327 case 'w': p_ws = TRUE; break;
13328 case 'W': p_ws = FALSE; break;
13329 default: mask = 0;
13330 if (flagsp != NULL)
13331 switch (*flags)
13332 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013333 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013334 case 'e': mask = SP_END; break;
13335 case 'm': mask = SP_RETCOUNT; break;
13336 case 'n': mask = SP_NOMOVE; break;
13337 case 'p': mask = SP_SUBPAT; break;
13338 case 'r': mask = SP_REPEAT; break;
13339 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013340 }
13341 if (mask == 0)
13342 {
13343 EMSG2(_(e_invarg2), flags);
13344 dir = 0;
13345 }
13346 else
13347 *flagsp |= mask;
13348 }
13349 if (dir == 0)
13350 break;
13351 ++flags;
13352 }
13353 }
13354 return dir;
13355}
13356
Bram Moolenaar071d4272004-06-13 20:20:40 +000013357/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013358 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000013359 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013360 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013361search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013362 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013363 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013364 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013365{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013366 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013367 char_u *pat;
13368 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013369 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013370 int save_p_ws = p_ws;
13371 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013372 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013373 long lnum_stop = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013374 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013375 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013376
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013377 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013378 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013379 if (dir == 0)
13380 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013381 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013382 if (flags & SP_START)
13383 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013384 if (flags & SP_END)
13385 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013386
13387 /* Optional extra argument: line number to stop searching. */
13388 if (argvars[1].v_type != VAR_UNKNOWN
13389 && argvars[2].v_type != VAR_UNKNOWN)
13390 {
13391 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
13392 if (lnum_stop < 0)
13393 goto theend;
13394 }
13395
Bram Moolenaar231334e2005-07-25 20:46:57 +000013396 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013397 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000013398 * Check to make sure only those flags are set.
13399 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
13400 * flags cannot be set. Check for that condition also.
13401 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013402 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013403 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013404 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013405 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013406 goto theend;
13407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013408
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013409 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013410 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
13411 options, RE_SEARCH, (linenr_T)lnum_stop);
13412 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013413 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013414 if (flags & SP_SUBPAT)
13415 retval = subpatnum;
13416 else
13417 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013418 if (flags & SP_SETPCMARK)
13419 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013420 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013421 if (match_pos != NULL)
13422 {
13423 /* Store the match cursor position */
13424 match_pos->lnum = pos.lnum;
13425 match_pos->col = pos.col + 1;
13426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013427 /* "/$" will put the cursor after the end of the line, may need to
13428 * correct that here */
13429 check_cursor();
13430 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013431
13432 /* If 'n' flag is used: restore cursor position. */
13433 if (flags & SP_NOMOVE)
13434 curwin->w_cursor = save_cursor;
13435theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000013436 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013437
13438 return retval;
13439}
13440
13441/*
13442 * "search()" function
13443 */
13444 static void
13445f_search(argvars, rettv)
13446 typval_T *argvars;
13447 typval_T *rettv;
13448{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013449 int flags = 0;
13450
13451 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013452}
13453
Bram Moolenaar071d4272004-06-13 20:20:40 +000013454/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013455 * "searchdecl()" function
13456 */
13457 static void
13458f_searchdecl(argvars, rettv)
13459 typval_T *argvars;
13460 typval_T *rettv;
13461{
13462 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013463 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013464 int error = FALSE;
13465 char_u *name;
13466
13467 rettv->vval.v_number = 1; /* default: FAIL */
13468
13469 name = get_tv_string_chk(&argvars[0]);
13470 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000013471 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013472 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013473 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13474 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
13475 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013476 if (!error && name != NULL)
13477 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000013478 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013479}
13480
13481/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013482 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000013483 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013484 static int
13485searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000013486 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013487 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013488{
13489 char_u *spat, *mpat, *epat;
13490 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013491 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013492 int dir;
13493 int flags = 0;
13494 char_u nbuf1[NUMBUFLEN];
13495 char_u nbuf2[NUMBUFLEN];
13496 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013497 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013498 long lnum_stop = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013499
Bram Moolenaar071d4272004-06-13 20:20:40 +000013500 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013501 spat = get_tv_string_chk(&argvars[0]);
13502 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
13503 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
13504 if (spat == NULL || mpat == NULL || epat == NULL)
13505 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013506
Bram Moolenaar071d4272004-06-13 20:20:40 +000013507 /* Handle the optional fourth argument: flags */
13508 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013509 if (dir == 0)
13510 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013511
13512 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000013513 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
13514 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013515 if ((flags & (SP_END | SP_SUBPAT)) != 0
13516 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000013517 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013518 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000013519 goto theend;
13520 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013521
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013522 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013523 if (argvars[3].v_type == VAR_UNKNOWN
13524 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013525 skip = (char_u *)"";
13526 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013527 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013528 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013529 if (argvars[5].v_type != VAR_UNKNOWN)
13530 {
13531 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
13532 if (lnum_stop < 0)
13533 goto theend;
13534 }
13535 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013536 if (skip == NULL)
13537 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013538
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013539 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
13540 match_pos, lnum_stop);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013541
13542theend:
13543 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013544
13545 return retval;
13546}
13547
13548/*
13549 * "searchpair()" function
13550 */
13551 static void
13552f_searchpair(argvars, rettv)
13553 typval_T *argvars;
13554 typval_T *rettv;
13555{
13556 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
13557}
13558
13559/*
13560 * "searchpairpos()" function
13561 */
13562 static void
13563f_searchpairpos(argvars, rettv)
13564 typval_T *argvars;
13565 typval_T *rettv;
13566{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013567 pos_T match_pos;
13568 int lnum = 0;
13569 int col = 0;
13570
13571 rettv->vval.v_number = 0;
13572
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013573 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013574 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013575
13576 if (searchpair_cmn(argvars, &match_pos) > 0)
13577 {
13578 lnum = match_pos.lnum;
13579 col = match_pos.col;
13580 }
13581
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013582 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
13583 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013584}
13585
13586/*
13587 * Search for a start/middle/end thing.
13588 * Used by searchpair(), see its documentation for the details.
13589 * Returns 0 or -1 for no match,
13590 */
13591 long
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013592do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos, lnum_stop)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013593 char_u *spat; /* start pattern */
13594 char_u *mpat; /* middle pattern */
13595 char_u *epat; /* end pattern */
13596 int dir; /* BACKWARD or FORWARD */
13597 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013598 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013599 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013600 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013601{
13602 char_u *save_cpo;
13603 char_u *pat, *pat2 = NULL, *pat3 = NULL;
13604 long retval = 0;
13605 pos_T pos;
13606 pos_T firstpos;
13607 pos_T foundpos;
13608 pos_T save_cursor;
13609 pos_T save_pos;
13610 int n;
13611 int r;
13612 int nest = 1;
13613 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013614 int options = SEARCH_KEEP;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013615
13616 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13617 save_cpo = p_cpo;
13618 p_cpo = (char_u *)"";
13619
13620 /* Make two search patterns: start/end (pat2, for in nested pairs) and
13621 * start/middle/end (pat3, for the top pair). */
13622 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
13623 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
13624 if (pat2 == NULL || pat3 == NULL)
13625 goto theend;
13626 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
13627 if (*mpat == NUL)
13628 STRCPY(pat3, pat2);
13629 else
13630 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
13631 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013632 if (flags & SP_START)
13633 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013634
Bram Moolenaar071d4272004-06-13 20:20:40 +000013635 save_cursor = curwin->w_cursor;
13636 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000013637 clearpos(&firstpos);
13638 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013639 pat = pat3;
13640 for (;;)
13641 {
13642 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013643 options, RE_SEARCH, lnum_stop);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013644 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
13645 /* didn't find it or found the first match again: FAIL */
13646 break;
13647
13648 if (firstpos.lnum == 0)
13649 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000013650 if (equalpos(pos, foundpos))
13651 {
13652 /* Found the same position again. Can happen with a pattern that
13653 * has "\zs" at the end and searching backwards. Advance one
13654 * character and try again. */
13655 if (dir == BACKWARD)
13656 decl(&pos);
13657 else
13658 incl(&pos);
13659 }
13660 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013661
13662 /* If the skip pattern matches, ignore this match. */
13663 if (*skip != NUL)
13664 {
13665 save_pos = curwin->w_cursor;
13666 curwin->w_cursor = pos;
13667 r = eval_to_bool(skip, &err, NULL, FALSE);
13668 curwin->w_cursor = save_pos;
13669 if (err)
13670 {
13671 /* Evaluating {skip} caused an error, break here. */
13672 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013673 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013674 break;
13675 }
13676 if (r)
13677 continue;
13678 }
13679
13680 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
13681 {
13682 /* Found end when searching backwards or start when searching
13683 * forward: nested pair. */
13684 ++nest;
13685 pat = pat2; /* nested, don't search for middle */
13686 }
13687 else
13688 {
13689 /* Found end when searching forward or start when searching
13690 * backward: end of (nested) pair; or found middle in outer pair. */
13691 if (--nest == 1)
13692 pat = pat3; /* outer level, search for middle */
13693 }
13694
13695 if (nest == 0)
13696 {
13697 /* Found the match: return matchcount or line number. */
13698 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013699 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013700 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013701 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013702 if (flags & SP_SETPCMARK)
13703 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013704 curwin->w_cursor = pos;
13705 if (!(flags & SP_REPEAT))
13706 break;
13707 nest = 1; /* search for next unmatched */
13708 }
13709 }
13710
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013711 if (match_pos != NULL)
13712 {
13713 /* Store the match cursor position */
13714 match_pos->lnum = curwin->w_cursor.lnum;
13715 match_pos->col = curwin->w_cursor.col + 1;
13716 }
13717
Bram Moolenaar071d4272004-06-13 20:20:40 +000013718 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013719 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013720 curwin->w_cursor = save_cursor;
13721
13722theend:
13723 vim_free(pat2);
13724 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013725 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013726
13727 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013728}
13729
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013730/*
13731 * "searchpos()" function
13732 */
13733 static void
13734f_searchpos(argvars, rettv)
13735 typval_T *argvars;
13736 typval_T *rettv;
13737{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013738 pos_T match_pos;
13739 int lnum = 0;
13740 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013741 int n;
13742 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013743
13744 rettv->vval.v_number = 0;
13745
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013746 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013747 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013748
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013749 n = search_cmn(argvars, &match_pos, &flags);
13750 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013751 {
13752 lnum = match_pos.lnum;
13753 col = match_pos.col;
13754 }
13755
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013756 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
13757 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013758 if (flags & SP_SUBPAT)
13759 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013760}
13761
13762
Bram Moolenaar0d660222005-01-07 21:51:51 +000013763/*ARGSUSED*/
13764 static void
13765f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013766 typval_T *argvars;
13767 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013768{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013769#ifdef FEAT_CLIENTSERVER
13770 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013771 char_u *server = get_tv_string_chk(&argvars[0]);
13772 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013773
Bram Moolenaar0d660222005-01-07 21:51:51 +000013774 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013775 if (server == NULL || reply == NULL)
13776 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013777 if (check_restricted() || check_secure())
13778 return;
13779# ifdef FEAT_X11
13780 if (check_connection() == FAIL)
13781 return;
13782# endif
13783
13784 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013785 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013786 EMSG(_("E258: Unable to send to client"));
13787 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013788 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013789 rettv->vval.v_number = 0;
13790#else
13791 rettv->vval.v_number = -1;
13792#endif
13793}
13794
13795/*ARGSUSED*/
13796 static void
13797f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013798 typval_T *argvars;
13799 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013800{
13801 char_u *r = NULL;
13802
13803#ifdef FEAT_CLIENTSERVER
13804# ifdef WIN32
13805 r = serverGetVimNames();
13806# else
13807 make_connection();
13808 if (X_DISPLAY != NULL)
13809 r = serverGetVimNames(X_DISPLAY);
13810# endif
13811#endif
13812 rettv->v_type = VAR_STRING;
13813 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013814}
13815
13816/*
13817 * "setbufvar()" function
13818 */
13819/*ARGSUSED*/
13820 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013821f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013822 typval_T *argvars;
13823 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013824{
13825 buf_T *buf;
13826#ifdef FEAT_AUTOCMD
13827 aco_save_T aco;
13828#else
13829 buf_T *save_curbuf;
13830#endif
13831 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013832 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013833 char_u nbuf[NUMBUFLEN];
13834
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013835 rettv->vval.v_number = 0;
13836
Bram Moolenaar071d4272004-06-13 20:20:40 +000013837 if (check_restricted() || check_secure())
13838 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013839 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
13840 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013841 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013842 varp = &argvars[2];
13843
13844 if (buf != NULL && varname != NULL && varp != NULL)
13845 {
13846 /* set curbuf to be our buf, temporarily */
13847#ifdef FEAT_AUTOCMD
13848 aucmd_prepbuf(&aco, buf);
13849#else
13850 save_curbuf = curbuf;
13851 curbuf = buf;
13852#endif
13853
13854 if (*varname == '&')
13855 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013856 long numval;
13857 char_u *strval;
13858 int error = FALSE;
13859
Bram Moolenaar071d4272004-06-13 20:20:40 +000013860 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013861 numval = get_tv_number_chk(varp, &error);
13862 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013863 if (!error && strval != NULL)
13864 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013865 }
13866 else
13867 {
13868 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
13869 if (bufvarname != NULL)
13870 {
13871 STRCPY(bufvarname, "b:");
13872 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013873 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013874 vim_free(bufvarname);
13875 }
13876 }
13877
13878 /* reset notion of buffer */
13879#ifdef FEAT_AUTOCMD
13880 aucmd_restbuf(&aco);
13881#else
13882 curbuf = save_curbuf;
13883#endif
13884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013885}
13886
13887/*
13888 * "setcmdpos()" function
13889 */
13890 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013891f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013892 typval_T *argvars;
13893 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013894{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013895 int pos = (int)get_tv_number(&argvars[0]) - 1;
13896
13897 if (pos >= 0)
13898 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013899}
13900
13901/*
13902 * "setline()" function
13903 */
13904 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013905f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013906 typval_T *argvars;
13907 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013908{
13909 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000013910 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013911 list_T *l = NULL;
13912 listitem_T *li = NULL;
13913 long added = 0;
13914 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013915
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013916 lnum = get_tv_lnum(&argvars[0]);
13917 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013918 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013919 l = argvars[1].vval.v_list;
13920 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013921 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013922 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013923 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013924
13925 rettv->vval.v_number = 0; /* OK */
13926 for (;;)
13927 {
13928 if (l != NULL)
13929 {
13930 /* list argument, get next string */
13931 if (li == NULL)
13932 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013933 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013934 li = li->li_next;
13935 }
13936
13937 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013938 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013939 break;
13940 if (lnum <= curbuf->b_ml.ml_line_count)
13941 {
13942 /* existing line, replace it */
13943 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
13944 {
13945 changed_bytes(lnum, 0);
13946 check_cursor_col();
13947 rettv->vval.v_number = 0; /* OK */
13948 }
13949 }
13950 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
13951 {
13952 /* lnum is one past the last line, append the line */
13953 ++added;
13954 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
13955 rettv->vval.v_number = 0; /* OK */
13956 }
13957
13958 if (l == NULL) /* only one string argument */
13959 break;
13960 ++lnum;
13961 }
13962
13963 if (added > 0)
13964 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013965}
13966
13967/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013968 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000013969 */
13970/*ARGSUSED*/
13971 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013972set_qf_ll_list(wp, list_arg, action_arg, rettv)
13973 win_T *wp;
13974 typval_T *list_arg;
13975 typval_T *action_arg;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013976 typval_T *rettv;
13977{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000013978#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013979 char_u *act;
13980 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000013981#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013982
Bram Moolenaar2641f772005-03-25 21:58:17 +000013983 rettv->vval.v_number = -1;
13984
13985#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013986 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013987 EMSG(_(e_listreq));
13988 else
13989 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013990 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013991
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013992 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013993 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013994 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013995 if (act == NULL)
13996 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013997 if (*act == 'a' || *act == 'r')
13998 action = *act;
13999 }
14000
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014001 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014002 rettv->vval.v_number = 0;
14003 }
14004#endif
14005}
14006
14007/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014008 * "setloclist()" function
14009 */
14010/*ARGSUSED*/
14011 static void
14012f_setloclist(argvars, rettv)
14013 typval_T *argvars;
14014 typval_T *rettv;
14015{
14016 win_T *win;
14017
14018 rettv->vval.v_number = -1;
14019
14020 win = find_win_by_nr(&argvars[0]);
14021 if (win != NULL)
14022 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
14023}
14024
14025/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014026 * "setpos()" function
14027 */
14028/*ARGSUSED*/
14029 static void
14030f_setpos(argvars, rettv)
14031 typval_T *argvars;
14032 typval_T *rettv;
14033{
14034 pos_T pos;
14035 int fnum;
14036 char_u *name;
14037
14038 name = get_tv_string_chk(argvars);
14039 if (name != NULL)
14040 {
14041 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
14042 {
14043 --pos.col;
14044 if (name[0] == '.') /* cursor */
14045 {
14046 if (fnum == curbuf->b_fnum)
14047 {
14048 curwin->w_cursor = pos;
14049 check_cursor();
14050 }
14051 else
14052 EMSG(_(e_invarg));
14053 }
14054 else if (name[0] == '\'') /* mark */
14055 (void)setmark_pos(name[1], &pos, fnum);
14056 else
14057 EMSG(_(e_invarg));
14058 }
14059 }
14060}
14061
14062/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014063 * "setqflist()" function
14064 */
14065/*ARGSUSED*/
14066 static void
14067f_setqflist(argvars, rettv)
14068 typval_T *argvars;
14069 typval_T *rettv;
14070{
14071 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
14072}
14073
14074/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014075 * "setreg()" function
14076 */
14077 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014078f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014079 typval_T *argvars;
14080 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014081{
14082 int regname;
14083 char_u *strregname;
14084 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014085 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014086 int append;
14087 char_u yank_type;
14088 long block_len;
14089
14090 block_len = -1;
14091 yank_type = MAUTO;
14092 append = FALSE;
14093
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014094 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014095 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014096
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014097 if (strregname == NULL)
14098 return; /* type error; errmsg already given */
14099 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014100 if (regname == 0 || regname == '@')
14101 regname = '"';
14102 else if (regname == '=')
14103 return;
14104
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014105 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014106 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014107 stropt = get_tv_string_chk(&argvars[2]);
14108 if (stropt == NULL)
14109 return; /* type error */
14110 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014111 switch (*stropt)
14112 {
14113 case 'a': case 'A': /* append */
14114 append = TRUE;
14115 break;
14116 case 'v': case 'c': /* character-wise selection */
14117 yank_type = MCHAR;
14118 break;
14119 case 'V': case 'l': /* line-wise selection */
14120 yank_type = MLINE;
14121 break;
14122#ifdef FEAT_VISUAL
14123 case 'b': case Ctrl_V: /* block-wise selection */
14124 yank_type = MBLOCK;
14125 if (VIM_ISDIGIT(stropt[1]))
14126 {
14127 ++stropt;
14128 block_len = getdigits(&stropt) - 1;
14129 --stropt;
14130 }
14131 break;
14132#endif
14133 }
14134 }
14135
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014136 strval = get_tv_string_chk(&argvars[1]);
14137 if (strval != NULL)
14138 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000014139 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014140 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014141}
14142
14143
14144/*
14145 * "setwinvar(expr)" function
14146 */
14147/*ARGSUSED*/
14148 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014149f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014150 typval_T *argvars;
14151 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014152{
14153 win_T *win;
14154#ifdef FEAT_WINDOWS
14155 win_T *save_curwin;
14156#endif
14157 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014158 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014159 char_u nbuf[NUMBUFLEN];
14160
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014161 rettv->vval.v_number = 0;
14162
Bram Moolenaar071d4272004-06-13 20:20:40 +000014163 if (check_restricted() || check_secure())
14164 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014165 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014166 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014167 varp = &argvars[2];
14168
14169 if (win != NULL && varname != NULL && varp != NULL)
14170 {
14171#ifdef FEAT_WINDOWS
14172 /* set curwin to be our win, temporarily */
14173 save_curwin = curwin;
14174 curwin = win;
14175 curbuf = curwin->w_buffer;
14176#endif
14177
14178 if (*varname == '&')
14179 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014180 long numval;
14181 char_u *strval;
14182 int error = FALSE;
14183
Bram Moolenaar071d4272004-06-13 20:20:40 +000014184 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014185 numval = get_tv_number_chk(varp, &error);
14186 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014187 if (!error && strval != NULL)
14188 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014189 }
14190 else
14191 {
14192 winvarname = alloc((unsigned)STRLEN(varname) + 3);
14193 if (winvarname != NULL)
14194 {
14195 STRCPY(winvarname, "w:");
14196 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014197 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014198 vim_free(winvarname);
14199 }
14200 }
14201
14202#ifdef FEAT_WINDOWS
14203 /* Restore current window, if it's still valid (autocomands can make
14204 * it invalid). */
14205 if (win_valid(save_curwin))
14206 {
14207 curwin = save_curwin;
14208 curbuf = curwin->w_buffer;
14209 }
14210#endif
14211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014212}
14213
14214/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014215 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014216 */
14217 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014218f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014219 typval_T *argvars;
14220 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014221{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014222 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014223
Bram Moolenaar0d660222005-01-07 21:51:51 +000014224 p = get_tv_string(&argvars[0]);
14225 rettv->vval.v_string = vim_strsave(p);
14226 simplify_filename(rettv->vval.v_string); /* simplify in place */
14227 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014228}
14229
Bram Moolenaar0d660222005-01-07 21:51:51 +000014230static int
14231#ifdef __BORLANDC__
14232 _RTLENTRYF
14233#endif
14234 item_compare __ARGS((const void *s1, const void *s2));
14235static int
14236#ifdef __BORLANDC__
14237 _RTLENTRYF
14238#endif
14239 item_compare2 __ARGS((const void *s1, const void *s2));
14240
14241static int item_compare_ic;
14242static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014243static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014244#define ITEM_COMPARE_FAIL 999
14245
Bram Moolenaar071d4272004-06-13 20:20:40 +000014246/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014247 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014248 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014249 static int
14250#ifdef __BORLANDC__
14251_RTLENTRYF
14252#endif
14253item_compare(s1, s2)
14254 const void *s1;
14255 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014256{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014257 char_u *p1, *p2;
14258 char_u *tofree1, *tofree2;
14259 int res;
14260 char_u numbuf1[NUMBUFLEN];
14261 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014262
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014263 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
14264 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014265 if (item_compare_ic)
14266 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014267 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014268 res = STRCMP(p1, p2);
14269 vim_free(tofree1);
14270 vim_free(tofree2);
14271 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014272}
14273
14274 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000014275#ifdef __BORLANDC__
14276_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014277#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000014278item_compare2(s1, s2)
14279 const void *s1;
14280 const void *s2;
14281{
14282 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000014283 typval_T rettv;
14284 typval_T argv[2];
Bram Moolenaar0d660222005-01-07 21:51:51 +000014285 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014286
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014287 /* shortcut after failure in previous call; compare all items equal */
14288 if (item_compare_func_err)
14289 return 0;
14290
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014291 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
14292 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014293 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
14294 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014295
14296 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
14297 res = call_func(item_compare_func, STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000014298 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014299 clear_tv(&argv[0]);
14300 clear_tv(&argv[1]);
14301
14302 if (res == FAIL)
14303 res = ITEM_COMPARE_FAIL;
14304 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014305 /* return value has wrong type */
14306 res = get_tv_number_chk(&rettv, &item_compare_func_err);
14307 if (item_compare_func_err)
14308 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014309 clear_tv(&rettv);
14310 return res;
14311}
14312
14313/*
14314 * "sort({list})" function
14315 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014316 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014317f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014318 typval_T *argvars;
14319 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014320{
Bram Moolenaar33570922005-01-25 22:26:29 +000014321 list_T *l;
14322 listitem_T *li;
14323 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014324 long len;
14325 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014326
Bram Moolenaar0d660222005-01-07 21:51:51 +000014327 rettv->vval.v_number = 0;
14328 if (argvars[0].v_type != VAR_LIST)
14329 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000014330 else
14331 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014332 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014333 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014334 return;
14335 rettv->vval.v_list = l;
14336 rettv->v_type = VAR_LIST;
14337 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014338
Bram Moolenaar0d660222005-01-07 21:51:51 +000014339 len = list_len(l);
14340 if (len <= 1)
14341 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014342
Bram Moolenaar0d660222005-01-07 21:51:51 +000014343 item_compare_ic = FALSE;
14344 item_compare_func = NULL;
14345 if (argvars[1].v_type != VAR_UNKNOWN)
14346 {
14347 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014348 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014349 else
14350 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014351 int error = FALSE;
14352
14353 i = get_tv_number_chk(&argvars[1], &error);
14354 if (error)
14355 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014356 if (i == 1)
14357 item_compare_ic = TRUE;
14358 else
14359 item_compare_func = get_tv_string(&argvars[1]);
14360 }
14361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014362
Bram Moolenaar0d660222005-01-07 21:51:51 +000014363 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014364 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014365 if (ptrs == NULL)
14366 return;
14367 i = 0;
14368 for (li = l->lv_first; li != NULL; li = li->li_next)
14369 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014370
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014371 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014372 /* test the compare function */
14373 if (item_compare_func != NULL
14374 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
14375 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014376 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014377 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014378 {
14379 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014380 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000014381 item_compare_func == NULL ? item_compare : item_compare2);
14382
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014383 if (!item_compare_func_err)
14384 {
14385 /* Clear the List and append the items in the sorted order. */
14386 l->lv_first = l->lv_last = NULL;
14387 l->lv_len = 0;
14388 for (i = 0; i < len; ++i)
14389 list_append(l, ptrs[i]);
14390 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014391 }
14392
14393 vim_free(ptrs);
14394 }
14395}
14396
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014397/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000014398 * "soundfold({word})" function
14399 */
14400 static void
14401f_soundfold(argvars, rettv)
14402 typval_T *argvars;
14403 typval_T *rettv;
14404{
14405 char_u *s;
14406
14407 rettv->v_type = VAR_STRING;
14408 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014409#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000014410 rettv->vval.v_string = eval_soundfold(s);
14411#else
14412 rettv->vval.v_string = vim_strsave(s);
14413#endif
14414}
14415
14416/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014417 * "spellbadword()" function
14418 */
14419/* ARGSUSED */
14420 static void
14421f_spellbadword(argvars, rettv)
14422 typval_T *argvars;
14423 typval_T *rettv;
14424{
Bram Moolenaar4463f292005-09-25 22:20:24 +000014425 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014426 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014427 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014428
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014429 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014430 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014431
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014432#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000014433 if (argvars[0].v_type == VAR_UNKNOWN)
14434 {
14435 /* Find the start and length of the badly spelled word. */
14436 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
14437 if (len != 0)
14438 word = ml_get_cursor();
14439 }
14440 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14441 {
14442 char_u *str = get_tv_string_chk(&argvars[0]);
14443 int capcol = -1;
14444
14445 if (str != NULL)
14446 {
14447 /* Check the argument for spelling. */
14448 while (*str != NUL)
14449 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014450 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014451 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014452 {
14453 word = str;
14454 break;
14455 }
14456 str += len;
14457 }
14458 }
14459 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014460#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000014461
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014462 list_append_string(rettv->vval.v_list, word, len);
14463 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014464 attr == HLF_SPB ? "bad" :
14465 attr == HLF_SPR ? "rare" :
14466 attr == HLF_SPL ? "local" :
14467 attr == HLF_SPC ? "caps" :
14468 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014469}
14470
14471/*
14472 * "spellsuggest()" function
14473 */
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014474/*ARGSUSED*/
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014475 static void
14476f_spellsuggest(argvars, rettv)
14477 typval_T *argvars;
14478 typval_T *rettv;
14479{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014480#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014481 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014482 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014483 int maxcount;
14484 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014485 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014486 listitem_T *li;
14487 int need_capital = FALSE;
14488#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014489
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014490 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014491 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014492
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014493#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014494 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14495 {
14496 str = get_tv_string(&argvars[0]);
14497 if (argvars[1].v_type != VAR_UNKNOWN)
14498 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014499 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014500 if (maxcount <= 0)
14501 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014502 if (argvars[2].v_type != VAR_UNKNOWN)
14503 {
14504 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
14505 if (typeerr)
14506 return;
14507 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014508 }
14509 else
14510 maxcount = 25;
14511
Bram Moolenaar4770d092006-01-12 23:22:24 +000014512 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014513
14514 for (i = 0; i < ga.ga_len; ++i)
14515 {
14516 str = ((char_u **)ga.ga_data)[i];
14517
14518 li = listitem_alloc();
14519 if (li == NULL)
14520 vim_free(str);
14521 else
14522 {
14523 li->li_tv.v_type = VAR_STRING;
14524 li->li_tv.v_lock = 0;
14525 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014526 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014527 }
14528 }
14529 ga_clear(&ga);
14530 }
14531#endif
14532}
14533
Bram Moolenaar0d660222005-01-07 21:51:51 +000014534 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014535f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014536 typval_T *argvars;
14537 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014538{
14539 char_u *str;
14540 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014541 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014542 regmatch_T regmatch;
14543 char_u patbuf[NUMBUFLEN];
14544 char_u *save_cpo;
14545 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014546 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014547 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014548 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014549
14550 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14551 save_cpo = p_cpo;
14552 p_cpo = (char_u *)"";
14553
14554 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014555 if (argvars[1].v_type != VAR_UNKNOWN)
14556 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014557 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14558 if (pat == NULL)
14559 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014560 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014561 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014562 }
14563 if (pat == NULL || *pat == NUL)
14564 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000014565
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014566 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014567 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014568 if (typeerr)
14569 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014570
Bram Moolenaar0d660222005-01-07 21:51:51 +000014571 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14572 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014573 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014574 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014575 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014576 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014577 if (*str == NUL)
14578 match = FALSE; /* empty item at the end */
14579 else
14580 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014581 if (match)
14582 end = regmatch.startp[0];
14583 else
14584 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014585 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
14586 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014587 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014588 if (list_append_string(rettv->vval.v_list, str,
14589 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014590 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014591 }
14592 if (!match)
14593 break;
14594 /* Advance to just after the match. */
14595 if (regmatch.endp[0] > str)
14596 col = 0;
14597 else
14598 {
14599 /* Don't get stuck at the same match. */
14600#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014601 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014602#else
14603 col = 1;
14604#endif
14605 }
14606 str = regmatch.endp[0];
14607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014608
Bram Moolenaar0d660222005-01-07 21:51:51 +000014609 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014611
Bram Moolenaar0d660222005-01-07 21:51:51 +000014612 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014613}
14614
14615#ifdef HAVE_STRFTIME
14616/*
14617 * "strftime({format}[, {time}])" function
14618 */
14619 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014620f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014621 typval_T *argvars;
14622 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014623{
14624 char_u result_buf[256];
14625 struct tm *curtime;
14626 time_t seconds;
14627 char_u *p;
14628
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014629 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014630
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014631 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014632 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014633 seconds = time(NULL);
14634 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014635 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014636 curtime = localtime(&seconds);
14637 /* MSVC returns NULL for an invalid value of seconds. */
14638 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014639 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014640 else
14641 {
14642# ifdef FEAT_MBYTE
14643 vimconv_T conv;
14644 char_u *enc;
14645
14646 conv.vc_type = CONV_NONE;
14647 enc = enc_locale();
14648 convert_setup(&conv, p_enc, enc);
14649 if (conv.vc_type != CONV_NONE)
14650 p = string_convert(&conv, p, NULL);
14651# endif
14652 if (p != NULL)
14653 (void)strftime((char *)result_buf, sizeof(result_buf),
14654 (char *)p, curtime);
14655 else
14656 result_buf[0] = NUL;
14657
14658# ifdef FEAT_MBYTE
14659 if (conv.vc_type != CONV_NONE)
14660 vim_free(p);
14661 convert_setup(&conv, enc, p_enc);
14662 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014663 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014664 else
14665# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014666 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014667
14668# ifdef FEAT_MBYTE
14669 /* Release conversion descriptors */
14670 convert_setup(&conv, NULL, NULL);
14671 vim_free(enc);
14672# endif
14673 }
14674}
14675#endif
14676
14677/*
14678 * "stridx()" function
14679 */
14680 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014681f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014682 typval_T *argvars;
14683 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014684{
14685 char_u buf[NUMBUFLEN];
14686 char_u *needle;
14687 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000014688 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014689 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000014690 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014691
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014692 needle = get_tv_string_chk(&argvars[1]);
14693 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000014694 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014695 if (needle == NULL || haystack == NULL)
14696 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014697
Bram Moolenaar33570922005-01-25 22:26:29 +000014698 if (argvars[2].v_type != VAR_UNKNOWN)
14699 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014700 int error = FALSE;
14701
14702 start_idx = get_tv_number_chk(&argvars[2], &error);
14703 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000014704 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014705 if (start_idx >= 0)
14706 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000014707 }
14708
14709 pos = (char_u *)strstr((char *)haystack, (char *)needle);
14710 if (pos != NULL)
14711 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014712}
14713
14714/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014715 * "string()" function
14716 */
14717 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014718f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014719 typval_T *argvars;
14720 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014721{
14722 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014723 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014724
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014725 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014726 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014727 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014728 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014729}
14730
14731/*
14732 * "strlen()" function
14733 */
14734 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014735f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014736 typval_T *argvars;
14737 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014738{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014739 rettv->vval.v_number = (varnumber_T)(STRLEN(
14740 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014741}
14742
14743/*
14744 * "strpart()" function
14745 */
14746 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014747f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014748 typval_T *argvars;
14749 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014750{
14751 char_u *p;
14752 int n;
14753 int len;
14754 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014755 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014756
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014757 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014758 slen = (int)STRLEN(p);
14759
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014760 n = get_tv_number_chk(&argvars[1], &error);
14761 if (error)
14762 len = 0;
14763 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014764 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014765 else
14766 len = slen - n; /* default len: all bytes that are available. */
14767
14768 /*
14769 * Only return the overlap between the specified part and the actual
14770 * string.
14771 */
14772 if (n < 0)
14773 {
14774 len += n;
14775 n = 0;
14776 }
14777 else if (n > slen)
14778 n = slen;
14779 if (len < 0)
14780 len = 0;
14781 else if (n + len > slen)
14782 len = slen - n;
14783
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014784 rettv->v_type = VAR_STRING;
14785 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014786}
14787
14788/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014789 * "strridx()" function
14790 */
14791 static void
14792f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014793 typval_T *argvars;
14794 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014795{
14796 char_u buf[NUMBUFLEN];
14797 char_u *needle;
14798 char_u *haystack;
14799 char_u *rest;
14800 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014801 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014802
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014803 needle = get_tv_string_chk(&argvars[1]);
14804 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar532c7802005-01-27 14:44:31 +000014805 haystack_len = STRLEN(haystack);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014806
14807 rettv->vval.v_number = -1;
14808 if (needle == NULL || haystack == NULL)
14809 return; /* type error; errmsg already given */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014810 if (argvars[2].v_type != VAR_UNKNOWN)
14811 {
14812 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014813 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000014814 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014815 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014816 }
14817 else
14818 end_idx = haystack_len;
14819
Bram Moolenaar0d660222005-01-07 21:51:51 +000014820 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000014821 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014822 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014823 lastmatch = haystack + end_idx;
14824 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014825 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000014826 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014827 for (rest = haystack; *rest != '\0'; ++rest)
14828 {
14829 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000014830 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014831 break;
14832 lastmatch = rest;
14833 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000014834 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014835
14836 if (lastmatch == NULL)
14837 rettv->vval.v_number = -1;
14838 else
14839 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
14840}
14841
14842/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014843 * "strtrans()" function
14844 */
14845 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014846f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014847 typval_T *argvars;
14848 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014849{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014850 rettv->v_type = VAR_STRING;
14851 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014852}
14853
14854/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014855 * "submatch()" function
14856 */
14857 static void
14858f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014859 typval_T *argvars;
14860 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014861{
14862 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014863 rettv->vval.v_string =
14864 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014865}
14866
14867/*
14868 * "substitute()" function
14869 */
14870 static void
14871f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014872 typval_T *argvars;
14873 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014874{
14875 char_u patbuf[NUMBUFLEN];
14876 char_u subbuf[NUMBUFLEN];
14877 char_u flagsbuf[NUMBUFLEN];
14878
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014879 char_u *str = get_tv_string_chk(&argvars[0]);
14880 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14881 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
14882 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
14883
Bram Moolenaar0d660222005-01-07 21:51:51 +000014884 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014885 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
14886 rettv->vval.v_string = NULL;
14887 else
14888 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014889}
14890
14891/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014892 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014893 */
14894/*ARGSUSED*/
14895 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014896f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014897 typval_T *argvars;
14898 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014899{
14900 int id = 0;
14901#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014902 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014903 long col;
14904 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000014905 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014906
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014907 lnum = get_tv_lnum(argvars); /* -1 on type error */
14908 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
14909 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014910
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014911 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014912 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +000014913 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014914#endif
14915
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014916 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014917}
14918
14919/*
14920 * "synIDattr(id, what [, mode])" function
14921 */
14922/*ARGSUSED*/
14923 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014924f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014925 typval_T *argvars;
14926 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014927{
14928 char_u *p = NULL;
14929#ifdef FEAT_SYN_HL
14930 int id;
14931 char_u *what;
14932 char_u *mode;
14933 char_u modebuf[NUMBUFLEN];
14934 int modec;
14935
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014936 id = get_tv_number(&argvars[0]);
14937 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014938 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014939 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014940 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014941 modec = TOLOWER_ASC(mode[0]);
14942 if (modec != 't' && modec != 'c'
14943#ifdef FEAT_GUI
14944 && modec != 'g'
14945#endif
14946 )
14947 modec = 0; /* replace invalid with current */
14948 }
14949 else
14950 {
14951#ifdef FEAT_GUI
14952 if (gui.in_use)
14953 modec = 'g';
14954 else
14955#endif
14956 if (t_colors > 1)
14957 modec = 'c';
14958 else
14959 modec = 't';
14960 }
14961
14962
14963 switch (TOLOWER_ASC(what[0]))
14964 {
14965 case 'b':
14966 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
14967 p = highlight_color(id, what, modec);
14968 else /* bold */
14969 p = highlight_has_attr(id, HL_BOLD, modec);
14970 break;
14971
14972 case 'f': /* fg[#] */
14973 p = highlight_color(id, what, modec);
14974 break;
14975
14976 case 'i':
14977 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
14978 p = highlight_has_attr(id, HL_INVERSE, modec);
14979 else /* italic */
14980 p = highlight_has_attr(id, HL_ITALIC, modec);
14981 break;
14982
14983 case 'n': /* name */
14984 p = get_highlight_name(NULL, id - 1);
14985 break;
14986
14987 case 'r': /* reverse */
14988 p = highlight_has_attr(id, HL_INVERSE, modec);
14989 break;
14990
14991 case 's': /* standout */
14992 p = highlight_has_attr(id, HL_STANDOUT, modec);
14993 break;
14994
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000014995 case 'u':
14996 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
14997 /* underline */
14998 p = highlight_has_attr(id, HL_UNDERLINE, modec);
14999 else
15000 /* undercurl */
15001 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015002 break;
15003 }
15004
15005 if (p != NULL)
15006 p = vim_strsave(p);
15007#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015008 rettv->v_type = VAR_STRING;
15009 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015010}
15011
15012/*
15013 * "synIDtrans(id)" function
15014 */
15015/*ARGSUSED*/
15016 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015017f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015018 typval_T *argvars;
15019 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015020{
15021 int id;
15022
15023#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015024 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015025
15026 if (id > 0)
15027 id = syn_get_final_id(id);
15028 else
15029#endif
15030 id = 0;
15031
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015032 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015033}
15034
15035/*
15036 * "system()" function
15037 */
15038 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015039f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015040 typval_T *argvars;
15041 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015042{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015043 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015044 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015045 char_u *infile = NULL;
15046 char_u buf[NUMBUFLEN];
15047 int err = FALSE;
15048 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015049
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015050 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015051 {
15052 /*
15053 * Write the string to a temp file, to be used for input of the shell
15054 * command.
15055 */
15056 if ((infile = vim_tempname('i')) == NULL)
15057 {
15058 EMSG(_(e_notmp));
15059 return;
15060 }
15061
15062 fd = mch_fopen((char *)infile, WRITEBIN);
15063 if (fd == NULL)
15064 {
15065 EMSG2(_(e_notopen), infile);
15066 goto done;
15067 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015068 p = get_tv_string_buf_chk(&argvars[1], buf);
15069 if (p == NULL)
15070 goto done; /* type error; errmsg already given */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015071 if (fwrite(p, STRLEN(p), 1, fd) != 1)
15072 err = TRUE;
15073 if (fclose(fd) != 0)
15074 err = TRUE;
15075 if (err)
15076 {
15077 EMSG(_("E677: Error writing temp file"));
15078 goto done;
15079 }
15080 }
15081
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015082 res = get_cmd_output(get_tv_string(&argvars[0]), infile, SHELL_SILENT);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015083
Bram Moolenaar071d4272004-06-13 20:20:40 +000015084#ifdef USE_CR
15085 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015086 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015087 {
15088 char_u *s;
15089
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015090 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015091 {
15092 if (*s == CAR)
15093 *s = NL;
15094 }
15095 }
15096#else
15097# ifdef USE_CRNL
15098 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015099 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015100 {
15101 char_u *s, *d;
15102
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015103 d = res;
15104 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015105 {
15106 if (s[0] == CAR && s[1] == NL)
15107 ++s;
15108 *d++ = *s;
15109 }
15110 *d = NUL;
15111 }
15112# endif
15113#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015114
15115done:
15116 if (infile != NULL)
15117 {
15118 mch_remove(infile);
15119 vim_free(infile);
15120 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015121 rettv->v_type = VAR_STRING;
15122 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015123}
15124
15125/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015126 * "tabpagebuflist()" function
15127 */
15128/* ARGSUSED */
15129 static void
15130f_tabpagebuflist(argvars, rettv)
15131 typval_T *argvars;
15132 typval_T *rettv;
15133{
15134#ifndef FEAT_WINDOWS
15135 rettv->vval.v_number = 0;
15136#else
15137 tabpage_T *tp;
15138 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015139
15140 if (argvars[0].v_type == VAR_UNKNOWN)
15141 wp = firstwin;
15142 else
15143 {
15144 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15145 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000015146 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015147 }
15148 if (wp == NULL)
15149 rettv->vval.v_number = 0;
15150 else
15151 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015152 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015153 rettv->vval.v_number = 0;
15154 else
15155 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015156 for (; wp != NULL; wp = wp->w_next)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015157 if (list_append_number(rettv->vval.v_list,
15158 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015159 break;
15160 }
15161 }
15162#endif
15163}
15164
15165
15166/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015167 * "tabpagenr()" function
15168 */
15169/* ARGSUSED */
15170 static void
15171f_tabpagenr(argvars, rettv)
15172 typval_T *argvars;
15173 typval_T *rettv;
15174{
15175 int nr = 1;
15176#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015177 char_u *arg;
15178
15179 if (argvars[0].v_type != VAR_UNKNOWN)
15180 {
15181 arg = get_tv_string_chk(&argvars[0]);
15182 nr = 0;
15183 if (arg != NULL)
15184 {
15185 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000015186 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015187 else
15188 EMSG2(_(e_invexpr2), arg);
15189 }
15190 }
15191 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015192 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015193#endif
15194 rettv->vval.v_number = nr;
15195}
15196
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015197
15198#ifdef FEAT_WINDOWS
15199static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
15200
15201/*
15202 * Common code for tabpagewinnr() and winnr().
15203 */
15204 static int
15205get_winnr(tp, argvar)
15206 tabpage_T *tp;
15207 typval_T *argvar;
15208{
15209 win_T *twin;
15210 int nr = 1;
15211 win_T *wp;
15212 char_u *arg;
15213
15214 twin = (tp == curtab) ? curwin : tp->tp_curwin;
15215 if (argvar->v_type != VAR_UNKNOWN)
15216 {
15217 arg = get_tv_string_chk(argvar);
15218 if (arg == NULL)
15219 nr = 0; /* type error; errmsg already given */
15220 else if (STRCMP(arg, "$") == 0)
15221 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
15222 else if (STRCMP(arg, "#") == 0)
15223 {
15224 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
15225 if (twin == NULL)
15226 nr = 0;
15227 }
15228 else
15229 {
15230 EMSG2(_(e_invexpr2), arg);
15231 nr = 0;
15232 }
15233 }
15234
15235 if (nr > 0)
15236 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
15237 wp != twin; wp = wp->w_next)
15238 ++nr;
15239 return nr;
15240}
15241#endif
15242
15243/*
15244 * "tabpagewinnr()" function
15245 */
15246/* ARGSUSED */
15247 static void
15248f_tabpagewinnr(argvars, rettv)
15249 typval_T *argvars;
15250 typval_T *rettv;
15251{
15252 int nr = 1;
15253#ifdef FEAT_WINDOWS
15254 tabpage_T *tp;
15255
15256 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15257 if (tp == NULL)
15258 nr = 0;
15259 else
15260 nr = get_winnr(tp, &argvars[1]);
15261#endif
15262 rettv->vval.v_number = nr;
15263}
15264
15265
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015266/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015267 * "tagfiles()" function
15268 */
15269/*ARGSUSED*/
15270 static void
15271f_tagfiles(argvars, rettv)
15272 typval_T *argvars;
15273 typval_T *rettv;
15274{
15275 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015276 tagname_T tn;
15277 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015278
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015279 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015280 {
15281 rettv->vval.v_number = 0;
15282 return;
15283 }
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015284
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015285 for (first = TRUE; ; first = FALSE)
15286 if (get_tagfname(&tn, first, fname) == FAIL
15287 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015288 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015289 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015290}
15291
15292/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000015293 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015294 */
15295 static void
15296f_taglist(argvars, rettv)
15297 typval_T *argvars;
15298 typval_T *rettv;
15299{
15300 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015301
15302 tag_pattern = get_tv_string(&argvars[0]);
15303
15304 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015305 if (*tag_pattern == NUL)
15306 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015307
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015308 if (rettv_list_alloc(rettv) == OK)
15309 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015310}
15311
15312/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015313 * "tempname()" function
15314 */
15315/*ARGSUSED*/
15316 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015317f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015318 typval_T *argvars;
15319 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015320{
15321 static int x = 'A';
15322
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015323 rettv->v_type = VAR_STRING;
15324 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015325
15326 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
15327 * names. Skip 'I' and 'O', they are used for shell redirection. */
15328 do
15329 {
15330 if (x == 'Z')
15331 x = '0';
15332 else if (x == '9')
15333 x = 'A';
15334 else
15335 {
15336#ifdef EBCDIC
15337 if (x == 'I')
15338 x = 'J';
15339 else if (x == 'R')
15340 x = 'S';
15341 else
15342#endif
15343 ++x;
15344 }
15345 } while (x == 'I' || x == 'O');
15346}
15347
15348/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000015349 * "test(list)" function: Just checking the walls...
15350 */
15351/*ARGSUSED*/
15352 static void
15353f_test(argvars, rettv)
15354 typval_T *argvars;
15355 typval_T *rettv;
15356{
15357 /* Used for unit testing. Change the code below to your liking. */
15358#if 0
15359 listitem_T *li;
15360 list_T *l;
15361 char_u *bad, *good;
15362
15363 if (argvars[0].v_type != VAR_LIST)
15364 return;
15365 l = argvars[0].vval.v_list;
15366 if (l == NULL)
15367 return;
15368 li = l->lv_first;
15369 if (li == NULL)
15370 return;
15371 bad = get_tv_string(&li->li_tv);
15372 li = li->li_next;
15373 if (li == NULL)
15374 return;
15375 good = get_tv_string(&li->li_tv);
15376 rettv->vval.v_number = test_edit_score(bad, good);
15377#endif
15378}
15379
15380/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015381 * "tolower(string)" function
15382 */
15383 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015384f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015385 typval_T *argvars;
15386 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015387{
15388 char_u *p;
15389
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015390 p = vim_strsave(get_tv_string(&argvars[0]));
15391 rettv->v_type = VAR_STRING;
15392 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015393
15394 if (p != NULL)
15395 while (*p != NUL)
15396 {
15397#ifdef FEAT_MBYTE
15398 int l;
15399
15400 if (enc_utf8)
15401 {
15402 int c, lc;
15403
15404 c = utf_ptr2char(p);
15405 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015406 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015407 /* TODO: reallocate string when byte count changes. */
15408 if (utf_char2len(lc) == l)
15409 utf_char2bytes(lc, p);
15410 p += l;
15411 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015412 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015413 p += l; /* skip multi-byte character */
15414 else
15415#endif
15416 {
15417 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
15418 ++p;
15419 }
15420 }
15421}
15422
15423/*
15424 * "toupper(string)" function
15425 */
15426 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015427f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015428 typval_T *argvars;
15429 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015430{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015431 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015432 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015433}
15434
15435/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000015436 * "tr(string, fromstr, tostr)" function
15437 */
15438 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015439f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015440 typval_T *argvars;
15441 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015442{
15443 char_u *instr;
15444 char_u *fromstr;
15445 char_u *tostr;
15446 char_u *p;
15447#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000015448 int inlen;
15449 int fromlen;
15450 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015451 int idx;
15452 char_u *cpstr;
15453 int cplen;
15454 int first = TRUE;
15455#endif
15456 char_u buf[NUMBUFLEN];
15457 char_u buf2[NUMBUFLEN];
15458 garray_T ga;
15459
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015460 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015461 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
15462 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015463
15464 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015465 rettv->v_type = VAR_STRING;
15466 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015467 if (fromstr == NULL || tostr == NULL)
15468 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000015469 ga_init2(&ga, (int)sizeof(char), 80);
15470
15471#ifdef FEAT_MBYTE
15472 if (!has_mbyte)
15473#endif
15474 /* not multi-byte: fromstr and tostr must be the same length */
15475 if (STRLEN(fromstr) != STRLEN(tostr))
15476 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015477#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000015478error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015479#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000015480 EMSG2(_(e_invarg2), fromstr);
15481 ga_clear(&ga);
15482 return;
15483 }
15484
15485 /* fromstr and tostr have to contain the same number of chars */
15486 while (*instr != NUL)
15487 {
15488#ifdef FEAT_MBYTE
15489 if (has_mbyte)
15490 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015491 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015492 cpstr = instr;
15493 cplen = inlen;
15494 idx = 0;
15495 for (p = fromstr; *p != NUL; p += fromlen)
15496 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015497 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015498 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
15499 {
15500 for (p = tostr; *p != NUL; p += tolen)
15501 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015502 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015503 if (idx-- == 0)
15504 {
15505 cplen = tolen;
15506 cpstr = p;
15507 break;
15508 }
15509 }
15510 if (*p == NUL) /* tostr is shorter than fromstr */
15511 goto error;
15512 break;
15513 }
15514 ++idx;
15515 }
15516
15517 if (first && cpstr == instr)
15518 {
15519 /* Check that fromstr and tostr have the same number of
15520 * (multi-byte) characters. Done only once when a character
15521 * of instr doesn't appear in fromstr. */
15522 first = FALSE;
15523 for (p = tostr; *p != NUL; p += tolen)
15524 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015525 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015526 --idx;
15527 }
15528 if (idx != 0)
15529 goto error;
15530 }
15531
15532 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000015533 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015534 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015535
15536 instr += inlen;
15537 }
15538 else
15539#endif
15540 {
15541 /* When not using multi-byte chars we can do it faster. */
15542 p = vim_strchr(fromstr, *instr);
15543 if (p != NULL)
15544 ga_append(&ga, tostr[p - fromstr]);
15545 else
15546 ga_append(&ga, *instr);
15547 ++instr;
15548 }
15549 }
15550
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015551 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015552}
15553
15554/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015555 * "type(expr)" function
15556 */
15557 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015558f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015559 typval_T *argvars;
15560 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015561{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015562 int n;
15563
15564 switch (argvars[0].v_type)
15565 {
15566 case VAR_NUMBER: n = 0; break;
15567 case VAR_STRING: n = 1; break;
15568 case VAR_FUNC: n = 2; break;
15569 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015570 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015571 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
15572 }
15573 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015574}
15575
15576/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015577 * "values(dict)" function
15578 */
15579 static void
15580f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015581 typval_T *argvars;
15582 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015583{
15584 dict_list(argvars, rettv, 1);
15585}
15586
15587/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015588 * "virtcol(string)" function
15589 */
15590 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015591f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015592 typval_T *argvars;
15593 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015594{
15595 colnr_T vcol = 0;
15596 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015597 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015598
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015599 fp = var2fpos(&argvars[0], FALSE, &fnum);
15600 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
15601 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015602 {
15603 getvvcol(curwin, fp, NULL, NULL, &vcol);
15604 ++vcol;
15605 }
15606
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015607 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015608}
15609
15610/*
15611 * "visualmode()" function
15612 */
15613/*ARGSUSED*/
15614 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015615f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015616 typval_T *argvars;
15617 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015618{
15619#ifdef FEAT_VISUAL
15620 char_u str[2];
15621
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015622 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015623 str[0] = curbuf->b_visual_mode_eval;
15624 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015625 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015626
15627 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015628 if ((argvars[0].v_type == VAR_NUMBER
15629 && argvars[0].vval.v_number != 0)
15630 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015631 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015632 curbuf->b_visual_mode_eval = NUL;
15633#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015634 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015635#endif
15636}
15637
15638/*
15639 * "winbufnr(nr)" function
15640 */
15641 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015642f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015643 typval_T *argvars;
15644 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015645{
15646 win_T *wp;
15647
15648 wp = find_win_by_nr(&argvars[0]);
15649 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015650 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015651 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015652 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015653}
15654
15655/*
15656 * "wincol()" function
15657 */
15658/*ARGSUSED*/
15659 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015660f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015661 typval_T *argvars;
15662 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015663{
15664 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015665 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015666}
15667
15668/*
15669 * "winheight(nr)" function
15670 */
15671 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015672f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015673 typval_T *argvars;
15674 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015675{
15676 win_T *wp;
15677
15678 wp = find_win_by_nr(&argvars[0]);
15679 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015680 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015681 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015682 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015683}
15684
15685/*
15686 * "winline()" function
15687 */
15688/*ARGSUSED*/
15689 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015690f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015691 typval_T *argvars;
15692 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015693{
15694 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015695 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015696}
15697
15698/*
15699 * "winnr()" function
15700 */
15701/* ARGSUSED */
15702 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015703f_winnr(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 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015708
Bram Moolenaar071d4272004-06-13 20:20:40 +000015709#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015710 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015711#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015712 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015713}
15714
15715/*
15716 * "winrestcmd()" function
15717 */
15718/* ARGSUSED */
15719 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015720f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015721 typval_T *argvars;
15722 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015723{
15724#ifdef FEAT_WINDOWS
15725 win_T *wp;
15726 int winnr = 1;
15727 garray_T ga;
15728 char_u buf[50];
15729
15730 ga_init2(&ga, (int)sizeof(char), 70);
15731 for (wp = firstwin; wp != NULL; wp = wp->w_next)
15732 {
15733 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
15734 ga_concat(&ga, buf);
15735# ifdef FEAT_VERTSPLIT
15736 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
15737 ga_concat(&ga, buf);
15738# endif
15739 ++winnr;
15740 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000015741 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015742
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015743 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015744#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015745 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015746#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015747 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015748}
15749
15750/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015751 * "winrestview()" function
15752 */
15753/* ARGSUSED */
15754 static void
15755f_winrestview(argvars, rettv)
15756 typval_T *argvars;
15757 typval_T *rettv;
15758{
15759 dict_T *dict;
15760
15761 if (argvars[0].v_type != VAR_DICT
15762 || (dict = argvars[0].vval.v_dict) == NULL)
15763 EMSG(_(e_invarg));
15764 else
15765 {
15766 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
15767 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
15768#ifdef FEAT_VIRTUALEDIT
15769 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
15770#endif
15771 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015772 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015773
15774 curwin->w_topline = get_dict_number(dict, (char_u *)"topline");
15775#ifdef FEAT_DIFF
15776 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
15777#endif
15778 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
15779 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
15780
15781 check_cursor();
15782 changed_cline_bef_curs();
15783 invalidate_botline();
15784 redraw_later(VALID);
15785
15786 if (curwin->w_topline == 0)
15787 curwin->w_topline = 1;
15788 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
15789 curwin->w_topline = curbuf->b_ml.ml_line_count;
15790#ifdef FEAT_DIFF
15791 check_topfill(curwin, TRUE);
15792#endif
15793 }
15794}
15795
15796/*
15797 * "winsaveview()" function
15798 */
15799/* ARGSUSED */
15800 static void
15801f_winsaveview(argvars, rettv)
15802 typval_T *argvars;
15803 typval_T *rettv;
15804{
15805 dict_T *dict;
15806
15807 dict = dict_alloc();
15808 if (dict == NULL)
15809 return;
15810 rettv->v_type = VAR_DICT;
15811 rettv->vval.v_dict = dict;
15812 ++dict->dv_refcount;
15813
15814 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
15815 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
15816#ifdef FEAT_VIRTUALEDIT
15817 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
15818#endif
15819 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
15820
15821 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
15822#ifdef FEAT_DIFF
15823 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
15824#endif
15825 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
15826 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
15827}
15828
15829/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015830 * "winwidth(nr)" function
15831 */
15832 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015833f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015834 typval_T *argvars;
15835 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015836{
15837 win_T *wp;
15838
15839 wp = find_win_by_nr(&argvars[0]);
15840 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015841 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015842 else
15843#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015844 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015845#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015846 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015847#endif
15848}
15849
Bram Moolenaar071d4272004-06-13 20:20:40 +000015850/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015851 * "writefile()" function
15852 */
15853 static void
15854f_writefile(argvars, rettv)
15855 typval_T *argvars;
15856 typval_T *rettv;
15857{
15858 int binary = FALSE;
15859 char_u *fname;
15860 FILE *fd;
15861 listitem_T *li;
15862 char_u *s;
15863 int ret = 0;
15864 int c;
15865
15866 if (argvars[0].v_type != VAR_LIST)
15867 {
15868 EMSG2(_(e_listarg), "writefile()");
15869 return;
15870 }
15871 if (argvars[0].vval.v_list == NULL)
15872 return;
15873
15874 if (argvars[2].v_type != VAR_UNKNOWN
15875 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
15876 binary = TRUE;
15877
15878 /* Always open the file in binary mode, library functions have a mind of
15879 * their own about CR-LF conversion. */
15880 fname = get_tv_string(&argvars[1]);
15881 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
15882 {
15883 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
15884 ret = -1;
15885 }
15886 else
15887 {
15888 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
15889 li = li->li_next)
15890 {
15891 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
15892 {
15893 if (*s == '\n')
15894 c = putc(NUL, fd);
15895 else
15896 c = putc(*s, fd);
15897 if (c == EOF)
15898 {
15899 ret = -1;
15900 break;
15901 }
15902 }
15903 if (!binary || li->li_next != NULL)
15904 if (putc('\n', fd) == EOF)
15905 {
15906 ret = -1;
15907 break;
15908 }
15909 if (ret < 0)
15910 {
15911 EMSG(_(e_write));
15912 break;
15913 }
15914 }
15915 fclose(fd);
15916 }
15917
15918 rettv->vval.v_number = ret;
15919}
15920
15921/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015922 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015923 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015924 */
15925 static pos_T *
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015926var2fpos(varp, lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000015927 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015928 int lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015929 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015930{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015931 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015932 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015933 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015934
Bram Moolenaara5525202006-03-02 22:52:09 +000015935 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015936 if (varp->v_type == VAR_LIST)
15937 {
15938 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015939 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000015940 int error = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015941
15942 l = varp->vval.v_list;
15943 if (l == NULL)
15944 return NULL;
15945
15946 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000015947 pos.lnum = list_find_nr(l, 0L, &error);
15948 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015949 return NULL; /* invalid line number */
15950
15951 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000015952 pos.col = list_find_nr(l, 1L, &error);
15953 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015954 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015955 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaara5525202006-03-02 22:52:09 +000015956 /* Accept a position up to the NUL after the line. */
15957 if (pos.col <= 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015958 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000015959 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015960
Bram Moolenaara5525202006-03-02 22:52:09 +000015961#ifdef FEAT_VIRTUALEDIT
15962 /* Get the virtual offset. Defaults to zero. */
15963 pos.coladd = list_find_nr(l, 2L, &error);
15964 if (error)
15965 pos.coladd = 0;
15966#endif
15967
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015968 return &pos;
15969 }
15970
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015971 name = get_tv_string_chk(varp);
15972 if (name == NULL)
15973 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015974 if (name[0] == '.') /* cursor */
15975 return &curwin->w_cursor;
15976 if (name[0] == '\'') /* mark */
15977 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015978 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015979 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
15980 return NULL;
15981 return pp;
15982 }
Bram Moolenaara5525202006-03-02 22:52:09 +000015983
15984#ifdef FEAT_VIRTUALEDIT
15985 pos.coladd = 0;
15986#endif
15987
Bram Moolenaarf52c7252006-02-10 23:23:57 +000015988 if (name[0] == 'w' && lnum)
15989 {
15990 pos.col = 0;
15991 if (name[1] == '0') /* "w0": first visible line */
15992 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000015993 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000015994 pos.lnum = curwin->w_topline;
15995 return &pos;
15996 }
15997 else if (name[1] == '$') /* "w$": last visible line */
15998 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000015999 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016000 pos.lnum = curwin->w_botline - 1;
16001 return &pos;
16002 }
16003 }
16004 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016005 {
16006 if (lnum)
16007 {
16008 pos.lnum = curbuf->b_ml.ml_line_count;
16009 pos.col = 0;
16010 }
16011 else
16012 {
16013 pos.lnum = curwin->w_cursor.lnum;
16014 pos.col = (colnr_T)STRLEN(ml_get_curline());
16015 }
16016 return &pos;
16017 }
16018 return NULL;
16019}
16020
16021/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016022 * Convert list in "arg" into a position and optional file number.
16023 * When "fnump" is NULL there is no file number, only 3 items.
16024 * Note that the column is passed on as-is, the caller may want to decrement
16025 * it to use 1 for the first column.
16026 * Return FAIL when conversion is not possible, doesn't check the position for
16027 * validity.
16028 */
16029 static int
16030list2fpos(arg, posp, fnump)
16031 typval_T *arg;
16032 pos_T *posp;
16033 int *fnump;
16034{
16035 list_T *l = arg->vval.v_list;
16036 long i = 0;
16037 long n;
16038
16039 /* List must be: [fnum, lnum, col, coladd] */
16040 if (arg->v_type != VAR_LIST || l == NULL
16041 || l->lv_len != (fnump == NULL ? 3 : 4))
16042 return FAIL;
16043
16044 if (fnump != NULL)
16045 {
16046 n = list_find_nr(l, i++, NULL); /* fnum */
16047 if (n < 0)
16048 return FAIL;
16049 if (n == 0)
16050 n = curbuf->b_fnum; /* current buffer */
16051 *fnump = n;
16052 }
16053
16054 n = list_find_nr(l, i++, NULL); /* lnum */
16055 if (n < 0)
16056 return FAIL;
16057 posp->lnum = n;
16058
16059 n = list_find_nr(l, i++, NULL); /* col */
16060 if (n < 0)
16061 return FAIL;
16062 posp->col = n;
16063
16064#ifdef FEAT_VIRTUALEDIT
16065 n = list_find_nr(l, i, NULL);
16066 if (n < 0)
16067 return FAIL;
16068 posp->coladd = n;
16069#endif
16070
16071 return OK;
16072}
16073
16074/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016075 * Get the length of an environment variable name.
16076 * Advance "arg" to the first character after the name.
16077 * Return 0 for error.
16078 */
16079 static int
16080get_env_len(arg)
16081 char_u **arg;
16082{
16083 char_u *p;
16084 int len;
16085
16086 for (p = *arg; vim_isIDc(*p); ++p)
16087 ;
16088 if (p == *arg) /* no name found */
16089 return 0;
16090
16091 len = (int)(p - *arg);
16092 *arg = p;
16093 return len;
16094}
16095
16096/*
16097 * Get the length of the name of a function or internal variable.
16098 * "arg" is advanced to the first non-white character after the name.
16099 * Return 0 if something is wrong.
16100 */
16101 static int
16102get_id_len(arg)
16103 char_u **arg;
16104{
16105 char_u *p;
16106 int len;
16107
16108 /* Find the end of the name. */
16109 for (p = *arg; eval_isnamec(*p); ++p)
16110 ;
16111 if (p == *arg) /* no name found */
16112 return 0;
16113
16114 len = (int)(p - *arg);
16115 *arg = skipwhite(p);
16116
16117 return len;
16118}
16119
16120/*
Bram Moolenaara7043832005-01-21 11:56:39 +000016121 * Get the length of the name of a variable or function.
16122 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000016123 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016124 * Return -1 if curly braces expansion failed.
16125 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016126 * If the name contains 'magic' {}'s, expand them and return the
16127 * expanded name in an allocated string via 'alias' - caller must free.
16128 */
16129 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016130get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016131 char_u **arg;
16132 char_u **alias;
16133 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016134 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016135{
16136 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016137 char_u *p;
16138 char_u *expr_start;
16139 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016140
16141 *alias = NULL; /* default to no alias */
16142
16143 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
16144 && (*arg)[2] == (int)KE_SNR)
16145 {
16146 /* hard coded <SNR>, already translated */
16147 *arg += 3;
16148 return get_id_len(arg) + 3;
16149 }
16150 len = eval_fname_script(*arg);
16151 if (len > 0)
16152 {
16153 /* literal "<SID>", "s:" or "<SNR>" */
16154 *arg += len;
16155 }
16156
Bram Moolenaar071d4272004-06-13 20:20:40 +000016157 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016158 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016159 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016160 p = find_name_end(*arg, &expr_start, &expr_end,
16161 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016162 if (expr_start != NULL)
16163 {
16164 char_u *temp_string;
16165
16166 if (!evaluate)
16167 {
16168 len += (int)(p - *arg);
16169 *arg = skipwhite(p);
16170 return len;
16171 }
16172
16173 /*
16174 * Include any <SID> etc in the expanded string:
16175 * Thus the -len here.
16176 */
16177 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
16178 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016179 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016180 *alias = temp_string;
16181 *arg = skipwhite(p);
16182 return (int)STRLEN(temp_string);
16183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016184
16185 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016186 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016187 EMSG2(_(e_invexpr2), *arg);
16188
16189 return len;
16190}
16191
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016192/*
16193 * Find the end of a variable or function name, taking care of magic braces.
16194 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
16195 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016196 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016197 * Return a pointer to just after the name. Equal to "arg" if there is no
16198 * valid name.
16199 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016200 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016201find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016202 char_u *arg;
16203 char_u **expr_start;
16204 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016205 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016206{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016207 int mb_nest = 0;
16208 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016209 char_u *p;
16210
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016211 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016212 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016213 *expr_start = NULL;
16214 *expr_end = NULL;
16215 }
16216
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016217 /* Quick check for valid starting character. */
16218 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
16219 return arg;
16220
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016221 for (p = arg; *p != NUL
16222 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016223 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016224 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016225 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000016226 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016227 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000016228 if (*p == '\'')
16229 {
16230 /* skip over 'string' to avoid counting [ and ] inside it. */
16231 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
16232 ;
16233 if (*p == NUL)
16234 break;
16235 }
16236 else if (*p == '"')
16237 {
16238 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
16239 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
16240 if (*p == '\\' && p[1] != NUL)
16241 ++p;
16242 if (*p == NUL)
16243 break;
16244 }
16245
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016246 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016247 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016248 if (*p == '[')
16249 ++br_nest;
16250 else if (*p == ']')
16251 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016252 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000016253
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016254 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016255 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016256 if (*p == '{')
16257 {
16258 mb_nest++;
16259 if (expr_start != NULL && *expr_start == NULL)
16260 *expr_start = p;
16261 }
16262 else if (*p == '}')
16263 {
16264 mb_nest--;
16265 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
16266 *expr_end = p;
16267 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016269 }
16270
16271 return p;
16272}
16273
16274/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016275 * Expands out the 'magic' {}'s in a variable/function name.
16276 * Note that this can call itself recursively, to deal with
16277 * constructs like foo{bar}{baz}{bam}
16278 * The four pointer arguments point to "foo{expre}ss{ion}bar"
16279 * "in_start" ^
16280 * "expr_start" ^
16281 * "expr_end" ^
16282 * "in_end" ^
16283 *
16284 * Returns a new allocated string, which the caller must free.
16285 * Returns NULL for failure.
16286 */
16287 static char_u *
16288make_expanded_name(in_start, expr_start, expr_end, in_end)
16289 char_u *in_start;
16290 char_u *expr_start;
16291 char_u *expr_end;
16292 char_u *in_end;
16293{
16294 char_u c1;
16295 char_u *retval = NULL;
16296 char_u *temp_result;
16297 char_u *nextcmd = NULL;
16298
16299 if (expr_end == NULL || in_end == NULL)
16300 return NULL;
16301 *expr_start = NUL;
16302 *expr_end = NUL;
16303 c1 = *in_end;
16304 *in_end = NUL;
16305
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016306 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016307 if (temp_result != NULL && nextcmd == NULL)
16308 {
16309 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
16310 + (in_end - expr_end) + 1));
16311 if (retval != NULL)
16312 {
16313 STRCPY(retval, in_start);
16314 STRCAT(retval, temp_result);
16315 STRCAT(retval, expr_end + 1);
16316 }
16317 }
16318 vim_free(temp_result);
16319
16320 *in_end = c1; /* put char back for error messages */
16321 *expr_start = '{';
16322 *expr_end = '}';
16323
16324 if (retval != NULL)
16325 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016326 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016327 if (expr_start != NULL)
16328 {
16329 /* Further expansion! */
16330 temp_result = make_expanded_name(retval, expr_start,
16331 expr_end, temp_result);
16332 vim_free(retval);
16333 retval = temp_result;
16334 }
16335 }
16336
16337 return retval;
16338}
16339
16340/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016341 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016342 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016343 */
16344 static int
16345eval_isnamec(c)
16346 int c;
16347{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016348 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
16349}
16350
16351/*
16352 * Return TRUE if character "c" can be used as the first character in a
16353 * variable or function name (excluding '{' and '}').
16354 */
16355 static int
16356eval_isnamec1(c)
16357 int c;
16358{
16359 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000016360}
16361
16362/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016363 * Set number v: variable to "val".
16364 */
16365 void
16366set_vim_var_nr(idx, val)
16367 int idx;
16368 long val;
16369{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016370 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016371}
16372
16373/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016374 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016375 */
16376 long
16377get_vim_var_nr(idx)
16378 int idx;
16379{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016380 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016381}
16382
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016383#if defined(FEAT_AUTOCMD) || defined(PROTO)
16384/*
16385 * Get string v: variable value. Uses a static buffer, can only be used once.
16386 */
16387 char_u *
16388get_vim_var_str(idx)
16389 int idx;
16390{
16391 return get_tv_string(&vimvars[idx].vv_tv);
16392}
16393#endif
16394
Bram Moolenaar071d4272004-06-13 20:20:40 +000016395/*
16396 * Set v:count, v:count1 and v:prevcount.
16397 */
16398 void
16399set_vcount(count, count1)
16400 long count;
16401 long count1;
16402{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016403 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
16404 vimvars[VV_COUNT].vv_nr = count;
16405 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016406}
16407
16408/*
16409 * Set string v: variable to a copy of "val".
16410 */
16411 void
16412set_vim_var_string(idx, val, len)
16413 int idx;
16414 char_u *val;
16415 int len; /* length of "val" to use or -1 (whole string) */
16416{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016417 /* Need to do this (at least) once, since we can't initialize a union.
16418 * Will always be invoked when "v:progname" is set. */
16419 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
16420
Bram Moolenaare9a41262005-01-15 22:18:47 +000016421 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016422 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016423 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016424 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016425 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016426 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000016427 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016428}
16429
16430/*
16431 * Set v:register if needed.
16432 */
16433 void
16434set_reg_var(c)
16435 int c;
16436{
16437 char_u regname;
16438
16439 if (c == 0 || c == ' ')
16440 regname = '"';
16441 else
16442 regname = c;
16443 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000016444 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016445 set_vim_var_string(VV_REG, &regname, 1);
16446}
16447
16448/*
16449 * Get or set v:exception. If "oldval" == NULL, return the current value.
16450 * Otherwise, restore the value to "oldval" and return NULL.
16451 * Must always be called in pairs to save and restore v:exception! Does not
16452 * take care of memory allocations.
16453 */
16454 char_u *
16455v_exception(oldval)
16456 char_u *oldval;
16457{
16458 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016459 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016460
Bram Moolenaare9a41262005-01-15 22:18:47 +000016461 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016462 return NULL;
16463}
16464
16465/*
16466 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
16467 * Otherwise, restore the value to "oldval" and return NULL.
16468 * Must always be called in pairs to save and restore v:throwpoint! Does not
16469 * take care of memory allocations.
16470 */
16471 char_u *
16472v_throwpoint(oldval)
16473 char_u *oldval;
16474{
16475 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016476 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016477
Bram Moolenaare9a41262005-01-15 22:18:47 +000016478 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016479 return NULL;
16480}
16481
16482#if defined(FEAT_AUTOCMD) || defined(PROTO)
16483/*
16484 * Set v:cmdarg.
16485 * If "eap" != NULL, use "eap" to generate the value and return the old value.
16486 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
16487 * Must always be called in pairs!
16488 */
16489 char_u *
16490set_cmdarg(eap, oldarg)
16491 exarg_T *eap;
16492 char_u *oldarg;
16493{
16494 char_u *oldval;
16495 char_u *newval;
16496 unsigned len;
16497
Bram Moolenaare9a41262005-01-15 22:18:47 +000016498 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016499 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016500 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016501 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000016502 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016503 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016504 }
16505
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016506 if (eap->force_bin == FORCE_BIN)
16507 len = 6;
16508 else if (eap->force_bin == FORCE_NOBIN)
16509 len = 8;
16510 else
16511 len = 0;
16512 if (eap->force_ff != 0)
16513 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
16514# ifdef FEAT_MBYTE
16515 if (eap->force_enc != 0)
16516 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000016517 if (eap->bad_char != 0)
16518 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016519# endif
16520
16521 newval = alloc(len + 1);
16522 if (newval == NULL)
16523 return NULL;
16524
16525 if (eap->force_bin == FORCE_BIN)
16526 sprintf((char *)newval, " ++bin");
16527 else if (eap->force_bin == FORCE_NOBIN)
16528 sprintf((char *)newval, " ++nobin");
16529 else
16530 *newval = NUL;
16531 if (eap->force_ff != 0)
16532 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
16533 eap->cmd + eap->force_ff);
16534# ifdef FEAT_MBYTE
16535 if (eap->force_enc != 0)
16536 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
16537 eap->cmd + eap->force_enc);
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000016538 if (eap->bad_char != 0)
16539 sprintf((char *)newval + STRLEN(newval), " ++bad=%s",
16540 eap->cmd + eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016541# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000016542 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016543 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016544}
16545#endif
16546
16547/*
16548 * Get the value of internal variable "name".
16549 * Return OK or FAIL.
16550 */
16551 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016552get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016553 char_u *name;
16554 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000016555 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016556 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016557{
16558 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000016559 typval_T *tv = NULL;
16560 typval_T atv;
16561 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016562 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016563
16564 /* truncate the name, so that we can use strcmp() */
16565 cc = name[len];
16566 name[len] = NUL;
16567
16568 /*
16569 * Check for "b:changedtick".
16570 */
16571 if (STRCMP(name, "b:changedtick") == 0)
16572 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000016573 atv.v_type = VAR_NUMBER;
16574 atv.vval.v_number = curbuf->b_changedtick;
16575 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016576 }
16577
16578 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016579 * Check for user-defined variables.
16580 */
16581 else
16582 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016583 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016584 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016585 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016586 }
16587
Bram Moolenaare9a41262005-01-15 22:18:47 +000016588 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016589 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016590 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016591 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016592 ret = FAIL;
16593 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016594 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016595 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016596
16597 name[len] = cc;
16598
16599 return ret;
16600}
16601
16602/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016603 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
16604 * Also handle function call with Funcref variable: func(expr)
16605 * Can all be combined: dict.func(expr)[idx]['func'](expr)
16606 */
16607 static int
16608handle_subscript(arg, rettv, evaluate, verbose)
16609 char_u **arg;
16610 typval_T *rettv;
16611 int evaluate; /* do more than finding the end */
16612 int verbose; /* give error messages */
16613{
16614 int ret = OK;
16615 dict_T *selfdict = NULL;
16616 char_u *s;
16617 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000016618 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016619
16620 while (ret == OK
16621 && (**arg == '['
16622 || (**arg == '.' && rettv->v_type == VAR_DICT)
16623 || (**arg == '(' && rettv->v_type == VAR_FUNC))
16624 && !vim_iswhite(*(*arg - 1)))
16625 {
16626 if (**arg == '(')
16627 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000016628 /* need to copy the funcref so that we can clear rettv */
16629 functv = *rettv;
16630 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016631
16632 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000016633 s = functv.vval.v_string;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016634 ret = get_func_tv(s, STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000016635 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
16636 &len, evaluate, selfdict);
16637
16638 /* Clear the funcref afterwards, so that deleting it while
16639 * evaluating the arguments is possible (see test55). */
16640 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016641
16642 /* Stop the expression evaluation when immediately aborting on
16643 * error, or when an interrupt occurred or an exception was thrown
16644 * but not caught. */
16645 if (aborting())
16646 {
16647 if (ret == OK)
16648 clear_tv(rettv);
16649 ret = FAIL;
16650 }
16651 dict_unref(selfdict);
16652 selfdict = NULL;
16653 }
16654 else /* **arg == '[' || **arg == '.' */
16655 {
16656 dict_unref(selfdict);
16657 if (rettv->v_type == VAR_DICT)
16658 {
16659 selfdict = rettv->vval.v_dict;
16660 if (selfdict != NULL)
16661 ++selfdict->dv_refcount;
16662 }
16663 else
16664 selfdict = NULL;
16665 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
16666 {
16667 clear_tv(rettv);
16668 ret = FAIL;
16669 }
16670 }
16671 }
16672 dict_unref(selfdict);
16673 return ret;
16674}
16675
16676/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016677 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
16678 * value).
16679 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016680 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016681alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016682{
Bram Moolenaar33570922005-01-25 22:26:29 +000016683 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016684}
16685
16686/*
16687 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016688 * The string "s" must have been allocated, it is consumed.
16689 * Return NULL for out of memory, the variable otherwise.
16690 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016691 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016692alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016693 char_u *s;
16694{
Bram Moolenaar33570922005-01-25 22:26:29 +000016695 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016696
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016697 rettv = alloc_tv();
16698 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016699 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016700 rettv->v_type = VAR_STRING;
16701 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016702 }
16703 else
16704 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016705 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016706}
16707
16708/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016709 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016710 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000016711 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016712free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016713 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016714{
16715 if (varp != NULL)
16716 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016717 switch (varp->v_type)
16718 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016719 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016720 func_unref(varp->vval.v_string);
16721 /*FALLTHROUGH*/
16722 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016723 vim_free(varp->vval.v_string);
16724 break;
16725 case VAR_LIST:
16726 list_unref(varp->vval.v_list);
16727 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016728 case VAR_DICT:
16729 dict_unref(varp->vval.v_dict);
16730 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016731 case VAR_NUMBER:
16732 case VAR_UNKNOWN:
16733 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016734 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000016735 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016736 break;
16737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016738 vim_free(varp);
16739 }
16740}
16741
16742/*
16743 * Free the memory for a variable value and set the value to NULL or 0.
16744 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000016745 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016746clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016747 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016748{
16749 if (varp != NULL)
16750 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016751 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016752 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016753 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016754 func_unref(varp->vval.v_string);
16755 /*FALLTHROUGH*/
16756 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016757 vim_free(varp->vval.v_string);
16758 varp->vval.v_string = NULL;
16759 break;
16760 case VAR_LIST:
16761 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016762 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016763 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016764 case VAR_DICT:
16765 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016766 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016767 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016768 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016769 varp->vval.v_number = 0;
16770 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016771 case VAR_UNKNOWN:
16772 break;
16773 default:
16774 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016775 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016776 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016777 }
16778}
16779
16780/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016781 * Set the value of a variable to NULL without freeing items.
16782 */
16783 static void
16784init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016785 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016786{
16787 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016788 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016789}
16790
16791/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016792 * Get the number value of a variable.
16793 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016794 * For incompatible types, return 0.
16795 * get_tv_number_chk() is similar to get_tv_number(), but informs the
16796 * caller of incompatible types: it sets *denote to TRUE if "denote"
16797 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016798 */
16799 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016800get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016801 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016802{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016803 int error = FALSE;
16804
16805 return get_tv_number_chk(varp, &error); /* return 0L on error */
16806}
16807
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016808 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016809get_tv_number_chk(varp, denote)
16810 typval_T *varp;
16811 int *denote;
16812{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016813 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016814
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016815 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016816 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016817 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016818 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016819 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016820 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016821 break;
16822 case VAR_STRING:
16823 if (varp->vval.v_string != NULL)
16824 vim_str2nr(varp->vval.v_string, NULL, NULL,
16825 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016826 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016827 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000016828 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016829 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016830 case VAR_DICT:
16831 EMSG(_("E728: Using a Dictionary as a number"));
16832 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016833 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016834 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016835 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016836 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016837 if (denote == NULL) /* useful for values that must be unsigned */
16838 n = -1;
16839 else
16840 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016841 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016842}
16843
16844/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000016845 * Get the lnum from the first argument.
16846 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016847 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016848 */
16849 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016850get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000016851 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016852{
Bram Moolenaar33570922005-01-25 22:26:29 +000016853 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016854 linenr_T lnum;
16855
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016856 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016857 if (lnum == 0) /* no valid number, try using line() */
16858 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016859 rettv.v_type = VAR_NUMBER;
16860 f_line(argvars, &rettv);
16861 lnum = rettv.vval.v_number;
16862 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016863 }
16864 return lnum;
16865}
16866
16867/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000016868 * Get the lnum from the first argument.
16869 * Also accepts "$", then "buf" is used.
16870 * Returns 0 on error.
16871 */
16872 static linenr_T
16873get_tv_lnum_buf(argvars, buf)
16874 typval_T *argvars;
16875 buf_T *buf;
16876{
16877 if (argvars[0].v_type == VAR_STRING
16878 && argvars[0].vval.v_string != NULL
16879 && argvars[0].vval.v_string[0] == '$'
16880 && buf != NULL)
16881 return buf->b_ml.ml_line_count;
16882 return get_tv_number_chk(&argvars[0], NULL);
16883}
16884
16885/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016886 * Get the string value of a variable.
16887 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000016888 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
16889 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016890 * If the String variable has never been set, return an empty string.
16891 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016892 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
16893 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016894 */
16895 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016896get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016897 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016898{
16899 static char_u mybuf[NUMBUFLEN];
16900
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016901 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016902}
16903
16904 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016905get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000016906 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016907 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016908{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016909 char_u *res = get_tv_string_buf_chk(varp, buf);
16910
16911 return res != NULL ? res : (char_u *)"";
16912}
16913
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016914 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016915get_tv_string_chk(varp)
16916 typval_T *varp;
16917{
16918 static char_u mybuf[NUMBUFLEN];
16919
16920 return get_tv_string_buf_chk(varp, mybuf);
16921}
16922
16923 static char_u *
16924get_tv_string_buf_chk(varp, buf)
16925 typval_T *varp;
16926 char_u *buf;
16927{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016928 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016929 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016930 case VAR_NUMBER:
16931 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
16932 return buf;
16933 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016934 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016935 break;
16936 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016937 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016938 break;
16939 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016940 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016941 break;
16942 case VAR_STRING:
16943 if (varp->vval.v_string != NULL)
16944 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016945 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016946 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016947 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016948 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016949 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016950 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016951}
16952
16953/*
16954 * Find variable "name" in the list of variables.
16955 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016956 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000016957 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000016958 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016959 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016960 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000016961find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016962 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000016963 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016964{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016965 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016966 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016967
Bram Moolenaara7043832005-01-21 11:56:39 +000016968 ht = find_var_ht(name, &varname);
16969 if (htp != NULL)
16970 *htp = ht;
16971 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016972 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016973 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016974}
16975
16976/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016977 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000016978 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016979 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016980 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016981find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000016982 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000016983 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016984 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000016985{
Bram Moolenaar33570922005-01-25 22:26:29 +000016986 hashitem_T *hi;
16987
16988 if (*varname == NUL)
16989 {
16990 /* Must be something like "s:", otherwise "ht" would be NULL. */
16991 switch (varname[-2])
16992 {
16993 case 's': return &SCRIPT_SV(current_SID).sv_var;
16994 case 'g': return &globvars_var;
16995 case 'v': return &vimvars_var;
16996 case 'b': return &curbuf->b_bufvar;
16997 case 'w': return &curwin->w_winvar;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016998 case 'l': return current_funccal == NULL
16999 ? NULL : &current_funccal->l_vars_var;
17000 case 'a': return current_funccal == NULL
17001 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000017002 }
17003 return NULL;
17004 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017005
17006 hi = hash_find(ht, varname);
17007 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017008 {
17009 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017010 * worked find the variable again. Don't auto-load a script if it was
17011 * loaded already, otherwise it would be loaded every time when
17012 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017013 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017014 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017015 hi = hash_find(ht, varname);
17016 if (HASHITEM_EMPTY(hi))
17017 return NULL;
17018 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017019 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017020}
17021
17022/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017023 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017024 * Set "varname" to the start of name without ':'.
17025 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017026 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017027find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017028 char_u *name;
17029 char_u **varname;
17030{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017031 hashitem_T *hi;
17032
Bram Moolenaar071d4272004-06-13 20:20:40 +000017033 if (name[1] != ':')
17034 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017035 /* The name must not start with a colon or #. */
17036 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017037 return NULL;
17038 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017039
17040 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017041 hi = hash_find(&compat_hashtab, name);
17042 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000017043 return &compat_hashtab;
17044
Bram Moolenaar071d4272004-06-13 20:20:40 +000017045 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017046 return &globvarht; /* global variable */
17047 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017048 }
17049 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017050 if (*name == 'g') /* global variable */
17051 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017052 /* There must be no ':' or '#' in the rest of the name, unless g: is used
17053 */
17054 if (vim_strchr(name + 2, ':') != NULL
17055 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017056 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017057 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017058 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017059 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017060 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000017061 if (*name == 'v') /* v: variable */
17062 return &vimvarht;
17063 if (*name == 'a' && current_funccal != NULL) /* function argument */
17064 return &current_funccal->l_avars.dv_hashtab;
17065 if (*name == 'l' && current_funccal != NULL) /* local function variable */
17066 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017067 if (*name == 's' /* script variable */
17068 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
17069 return &SCRIPT_VARS(current_SID);
17070 return NULL;
17071}
17072
17073/*
17074 * Get the string value of a (global/local) variable.
17075 * Returns NULL when it doesn't exist.
17076 */
17077 char_u *
17078get_var_value(name)
17079 char_u *name;
17080{
Bram Moolenaar33570922005-01-25 22:26:29 +000017081 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017082
Bram Moolenaara7043832005-01-21 11:56:39 +000017083 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017084 if (v == NULL)
17085 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000017086 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017087}
17088
17089/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017090 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000017091 * sourcing this script and when executing functions defined in the script.
17092 */
17093 void
17094new_script_vars(id)
17095 scid_T id;
17096{
Bram Moolenaara7043832005-01-21 11:56:39 +000017097 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000017098 hashtab_T *ht;
17099 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000017100
Bram Moolenaar071d4272004-06-13 20:20:40 +000017101 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
17102 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017103 /* Re-allocating ga_data means that an ht_array pointing to
17104 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000017105 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000017106 for (i = 1; i <= ga_scripts.ga_len; ++i)
17107 {
17108 ht = &SCRIPT_VARS(i);
17109 if (ht->ht_mask == HT_INIT_SIZE - 1)
17110 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000017111 sv = &SCRIPT_SV(i);
17112 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000017113 }
17114
Bram Moolenaar071d4272004-06-13 20:20:40 +000017115 while (ga_scripts.ga_len < id)
17116 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017117 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
17118 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017119 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017120 }
17121 }
17122}
17123
17124/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017125 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
17126 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017127 */
17128 void
Bram Moolenaar33570922005-01-25 22:26:29 +000017129init_var_dict(dict, dict_var)
17130 dict_T *dict;
17131 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017132{
Bram Moolenaar33570922005-01-25 22:26:29 +000017133 hash_init(&dict->dv_hashtab);
17134 dict->dv_refcount = 99999;
17135 dict_var->di_tv.vval.v_dict = dict;
17136 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017137 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017138 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
17139 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017140}
17141
17142/*
17143 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000017144 * Frees all allocated variables and the value they contain.
17145 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017146 */
17147 void
Bram Moolenaara7043832005-01-21 11:56:39 +000017148vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000017149 hashtab_T *ht;
17150{
17151 vars_clear_ext(ht, TRUE);
17152}
17153
17154/*
17155 * Like vars_clear(), but only free the value if "free_val" is TRUE.
17156 */
17157 static void
17158vars_clear_ext(ht, free_val)
17159 hashtab_T *ht;
17160 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017161{
Bram Moolenaara7043832005-01-21 11:56:39 +000017162 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000017163 hashitem_T *hi;
17164 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017165
Bram Moolenaar33570922005-01-25 22:26:29 +000017166 hash_lock(ht);
Bram Moolenaara7043832005-01-21 11:56:39 +000017167 todo = ht->ht_used;
17168 for (hi = ht->ht_array; todo > 0; ++hi)
17169 {
17170 if (!HASHITEM_EMPTY(hi))
17171 {
17172 --todo;
17173
Bram Moolenaar33570922005-01-25 22:26:29 +000017174 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000017175 * ht_array might change then. hash_clear() takes care of it
17176 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017177 v = HI2DI(hi);
17178 if (free_val)
17179 clear_tv(&v->di_tv);
17180 if ((v->di_flags & DI_FLAGS_FIX) == 0)
17181 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000017182 }
17183 }
17184 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017185 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017186}
17187
Bram Moolenaara7043832005-01-21 11:56:39 +000017188/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017189 * Delete a variable from hashtab "ht" at item "hi".
17190 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000017191 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017192 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000017193delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000017194 hashtab_T *ht;
17195 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017196{
Bram Moolenaar33570922005-01-25 22:26:29 +000017197 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017198
17199 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000017200 clear_tv(&di->di_tv);
17201 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017202}
17203
17204/*
17205 * List the value of one internal variable.
17206 */
17207 static void
17208list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000017209 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017210 char_u *prefix;
17211{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017212 char_u *tofree;
17213 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017214 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017215
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017216 s = echo_string(&v->di_tv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000017217 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017218 s == NULL ? (char_u *)"" : s);
17219 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017220}
17221
Bram Moolenaar071d4272004-06-13 20:20:40 +000017222 static void
17223list_one_var_a(prefix, name, type, string)
17224 char_u *prefix;
17225 char_u *name;
17226 int type;
17227 char_u *string;
17228{
17229 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
17230 if (name != NULL) /* "a:" vars don't have a name stored */
17231 msg_puts(name);
17232 msg_putchar(' ');
17233 msg_advance(22);
17234 if (type == VAR_NUMBER)
17235 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017236 else if (type == VAR_FUNC)
17237 msg_putchar('*');
17238 else if (type == VAR_LIST)
17239 {
17240 msg_putchar('[');
17241 if (*string == '[')
17242 ++string;
17243 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017244 else if (type == VAR_DICT)
17245 {
17246 msg_putchar('{');
17247 if (*string == '{')
17248 ++string;
17249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017250 else
17251 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017252
Bram Moolenaar071d4272004-06-13 20:20:40 +000017253 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017254
17255 if (type == VAR_FUNC)
17256 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017257}
17258
17259/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017260 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000017261 * If the variable already exists, the value is updated.
17262 * Otherwise the variable is created.
17263 */
17264 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017265set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017266 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017267 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017268 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017269{
Bram Moolenaar33570922005-01-25 22:26:29 +000017270 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017271 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017272 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017273 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017274
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017275 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017276 {
17277 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
17278 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
17279 ? name[2] : name[0]))
17280 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017281 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017282 return;
17283 }
17284 if (function_exists(name))
17285 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017286 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017287 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017288 return;
17289 }
17290 }
17291
Bram Moolenaara7043832005-01-21 11:56:39 +000017292 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000017293 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000017294 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000017295 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000017296 return;
17297 }
17298
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017299 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017300 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017301 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017302 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017303 if (var_check_ro(v->di_flags, name)
17304 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000017305 return;
17306 if (v->di_tv.v_type != tv->v_type
17307 && !((v->di_tv.v_type == VAR_STRING
17308 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017309 && (tv->v_type == VAR_STRING
17310 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017311 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017312 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017313 return;
17314 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017315
17316 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000017317 * Handle setting internal v: variables separately: we don't change
17318 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000017319 */
17320 if (ht == &vimvarht)
17321 {
17322 if (v->di_tv.v_type == VAR_STRING)
17323 {
17324 vim_free(v->di_tv.vval.v_string);
17325 if (copy || tv->v_type != VAR_STRING)
17326 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
17327 else
17328 {
17329 /* Take over the string to avoid an extra alloc/free. */
17330 v->di_tv.vval.v_string = tv->vval.v_string;
17331 tv->vval.v_string = NULL;
17332 }
17333 }
17334 else if (v->di_tv.v_type != VAR_NUMBER)
17335 EMSG2(_(e_intern2), "set_var()");
17336 else
17337 v->di_tv.vval.v_number = get_tv_number(tv);
17338 return;
17339 }
17340
17341 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017342 }
17343 else /* add a new variable */
17344 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000017345 /* Make sure the variable name is valid. */
17346 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000017347 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
17348 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000017349 {
17350 EMSG2(_(e_illvar), varname);
17351 return;
17352 }
17353
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017354 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
17355 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000017356 if (v == NULL)
17357 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000017358 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000017359 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017360 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017361 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017362 return;
17363 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017364 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017365 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017366
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017367 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000017368 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017369 else
17370 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017371 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017372 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017373 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017375}
17376
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017377/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017378 * Return TRUE if di_flags "flags" indicate read-only variable "name".
17379 * Also give an error message.
17380 */
17381 static int
17382var_check_ro(flags, name)
17383 int flags;
17384 char_u *name;
17385{
17386 if (flags & DI_FLAGS_RO)
17387 {
17388 EMSG2(_(e_readonlyvar), name);
17389 return TRUE;
17390 }
17391 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
17392 {
17393 EMSG2(_(e_readonlysbx), name);
17394 return TRUE;
17395 }
17396 return FALSE;
17397}
17398
17399/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017400 * Return TRUE if typeval "tv" is set to be locked (immutable).
17401 * Also give an error message, using "name".
17402 */
17403 static int
17404tv_check_lock(lock, name)
17405 int lock;
17406 char_u *name;
17407{
17408 if (lock & VAR_LOCKED)
17409 {
17410 EMSG2(_("E741: Value is locked: %s"),
17411 name == NULL ? (char_u *)_("Unknown") : name);
17412 return TRUE;
17413 }
17414 if (lock & VAR_FIXED)
17415 {
17416 EMSG2(_("E742: Cannot change value of %s"),
17417 name == NULL ? (char_u *)_("Unknown") : name);
17418 return TRUE;
17419 }
17420 return FALSE;
17421}
17422
17423/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017424 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017425 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017426 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017427 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017428 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017429copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000017430 typval_T *from;
17431 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017432{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017433 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017434 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017435 switch (from->v_type)
17436 {
17437 case VAR_NUMBER:
17438 to->vval.v_number = from->vval.v_number;
17439 break;
17440 case VAR_STRING:
17441 case VAR_FUNC:
17442 if (from->vval.v_string == NULL)
17443 to->vval.v_string = NULL;
17444 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017445 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017446 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017447 if (from->v_type == VAR_FUNC)
17448 func_ref(to->vval.v_string);
17449 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017450 break;
17451 case VAR_LIST:
17452 if (from->vval.v_list == NULL)
17453 to->vval.v_list = NULL;
17454 else
17455 {
17456 to->vval.v_list = from->vval.v_list;
17457 ++to->vval.v_list->lv_refcount;
17458 }
17459 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017460 case VAR_DICT:
17461 if (from->vval.v_dict == NULL)
17462 to->vval.v_dict = NULL;
17463 else
17464 {
17465 to->vval.v_dict = from->vval.v_dict;
17466 ++to->vval.v_dict->dv_refcount;
17467 }
17468 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017469 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017470 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017471 break;
17472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017473}
17474
17475/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000017476 * Make a copy of an item.
17477 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017478 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
17479 * reference to an already copied list/dict can be used.
17480 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017481 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017482 static int
17483item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000017484 typval_T *from;
17485 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017486 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017487 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017488{
17489 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017490 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017491
Bram Moolenaar33570922005-01-25 22:26:29 +000017492 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017493 {
17494 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017495 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017496 }
17497 ++recurse;
17498
17499 switch (from->v_type)
17500 {
17501 case VAR_NUMBER:
17502 case VAR_STRING:
17503 case VAR_FUNC:
17504 copy_tv(from, to);
17505 break;
17506 case VAR_LIST:
17507 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017508 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017509 if (from->vval.v_list == NULL)
17510 to->vval.v_list = NULL;
17511 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
17512 {
17513 /* use the copy made earlier */
17514 to->vval.v_list = from->vval.v_list->lv_copylist;
17515 ++to->vval.v_list->lv_refcount;
17516 }
17517 else
17518 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
17519 if (to->vval.v_list == NULL)
17520 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017521 break;
17522 case VAR_DICT:
17523 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017524 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017525 if (from->vval.v_dict == NULL)
17526 to->vval.v_dict = NULL;
17527 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
17528 {
17529 /* use the copy made earlier */
17530 to->vval.v_dict = from->vval.v_dict->dv_copydict;
17531 ++to->vval.v_dict->dv_refcount;
17532 }
17533 else
17534 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
17535 if (to->vval.v_dict == NULL)
17536 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017537 break;
17538 default:
17539 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017540 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017541 }
17542 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017543 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017544}
17545
17546/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017547 * ":echo expr1 ..." print each argument separated with a space, add a
17548 * newline at the end.
17549 * ":echon expr1 ..." print each argument plain.
17550 */
17551 void
17552ex_echo(eap)
17553 exarg_T *eap;
17554{
17555 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000017556 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017557 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017558 char_u *p;
17559 int needclr = TRUE;
17560 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017561 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017562
17563 if (eap->skip)
17564 ++emsg_skip;
17565 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
17566 {
17567 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017568 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017569 {
17570 /*
17571 * Report the invalid expression unless the expression evaluation
17572 * has been cancelled due to an aborting error, an interrupt, or an
17573 * exception.
17574 */
17575 if (!aborting())
17576 EMSG2(_(e_invexpr2), p);
17577 break;
17578 }
17579 if (!eap->skip)
17580 {
17581 if (atstart)
17582 {
17583 atstart = FALSE;
17584 /* Call msg_start() after eval1(), evaluating the expression
17585 * may cause a message to appear. */
17586 if (eap->cmdidx == CMD_echo)
17587 msg_start();
17588 }
17589 else if (eap->cmdidx == CMD_echo)
17590 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017591 p = echo_string(&rettv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017592 if (p != NULL)
17593 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017594 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017595 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017596 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017597 if (*p != TAB && needclr)
17598 {
17599 /* remove any text still there from the command */
17600 msg_clr_eos();
17601 needclr = FALSE;
17602 }
17603 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017604 }
17605 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017606 {
17607#ifdef FEAT_MBYTE
17608 if (has_mbyte)
17609 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017610 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017611
17612 (void)msg_outtrans_len_attr(p, i, echo_attr);
17613 p += i - 1;
17614 }
17615 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000017616#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017617 (void)msg_outtrans_len_attr(p, 1, echo_attr);
17618 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017619 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017620 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017621 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017622 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017623 arg = skipwhite(arg);
17624 }
17625 eap->nextcmd = check_nextcmd(arg);
17626
17627 if (eap->skip)
17628 --emsg_skip;
17629 else
17630 {
17631 /* remove text that may still be there from the command */
17632 if (needclr)
17633 msg_clr_eos();
17634 if (eap->cmdidx == CMD_echo)
17635 msg_end();
17636 }
17637}
17638
17639/*
17640 * ":echohl {name}".
17641 */
17642 void
17643ex_echohl(eap)
17644 exarg_T *eap;
17645{
17646 int id;
17647
17648 id = syn_name2id(eap->arg);
17649 if (id == 0)
17650 echo_attr = 0;
17651 else
17652 echo_attr = syn_id2attr(id);
17653}
17654
17655/*
17656 * ":execute expr1 ..." execute the result of an expression.
17657 * ":echomsg expr1 ..." Print a message
17658 * ":echoerr expr1 ..." Print an error
17659 * Each gets spaces around each argument and a newline at the end for
17660 * echo commands
17661 */
17662 void
17663ex_execute(eap)
17664 exarg_T *eap;
17665{
17666 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000017667 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017668 int ret = OK;
17669 char_u *p;
17670 garray_T ga;
17671 int len;
17672 int save_did_emsg;
17673
17674 ga_init2(&ga, 1, 80);
17675
17676 if (eap->skip)
17677 ++emsg_skip;
17678 while (*arg != NUL && *arg != '|' && *arg != '\n')
17679 {
17680 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017681 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017682 {
17683 /*
17684 * Report the invalid expression unless the expression evaluation
17685 * has been cancelled due to an aborting error, an interrupt, or an
17686 * exception.
17687 */
17688 if (!aborting())
17689 EMSG2(_(e_invexpr2), p);
17690 ret = FAIL;
17691 break;
17692 }
17693
17694 if (!eap->skip)
17695 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017696 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017697 len = (int)STRLEN(p);
17698 if (ga_grow(&ga, len + 2) == FAIL)
17699 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017700 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017701 ret = FAIL;
17702 break;
17703 }
17704 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017705 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017706 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017707 ga.ga_len += len;
17708 }
17709
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017710 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017711 arg = skipwhite(arg);
17712 }
17713
17714 if (ret != FAIL && ga.ga_data != NULL)
17715 {
17716 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000017717 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017718 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000017719 out_flush();
17720 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017721 else if (eap->cmdidx == CMD_echoerr)
17722 {
17723 /* We don't want to abort following commands, restore did_emsg. */
17724 save_did_emsg = did_emsg;
17725 EMSG((char_u *)ga.ga_data);
17726 if (!force_abort)
17727 did_emsg = save_did_emsg;
17728 }
17729 else if (eap->cmdidx == CMD_execute)
17730 do_cmdline((char_u *)ga.ga_data,
17731 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
17732 }
17733
17734 ga_clear(&ga);
17735
17736 if (eap->skip)
17737 --emsg_skip;
17738
17739 eap->nextcmd = check_nextcmd(arg);
17740}
17741
17742/*
17743 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
17744 * "arg" points to the "&" or '+' when called, to "option" when returning.
17745 * Returns NULL when no option name found. Otherwise pointer to the char
17746 * after the option name.
17747 */
17748 static char_u *
17749find_option_end(arg, opt_flags)
17750 char_u **arg;
17751 int *opt_flags;
17752{
17753 char_u *p = *arg;
17754
17755 ++p;
17756 if (*p == 'g' && p[1] == ':')
17757 {
17758 *opt_flags = OPT_GLOBAL;
17759 p += 2;
17760 }
17761 else if (*p == 'l' && p[1] == ':')
17762 {
17763 *opt_flags = OPT_LOCAL;
17764 p += 2;
17765 }
17766 else
17767 *opt_flags = 0;
17768
17769 if (!ASCII_ISALPHA(*p))
17770 return NULL;
17771 *arg = p;
17772
17773 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
17774 p += 4; /* termcap option */
17775 else
17776 while (ASCII_ISALPHA(*p))
17777 ++p;
17778 return p;
17779}
17780
17781/*
17782 * ":function"
17783 */
17784 void
17785ex_function(eap)
17786 exarg_T *eap;
17787{
17788 char_u *theline;
17789 int j;
17790 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017791 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017792 char_u *name = NULL;
17793 char_u *p;
17794 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000017795 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017796 garray_T newargs;
17797 garray_T newlines;
17798 int varargs = FALSE;
17799 int mustend = FALSE;
17800 int flags = 0;
17801 ufunc_T *fp;
17802 int indent;
17803 int nesting;
17804 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000017805 dictitem_T *v;
17806 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017807 static int func_nr = 0; /* number for nameless function */
17808 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017809 hashtab_T *ht;
17810 int todo;
17811 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000017812 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017813
17814 /*
17815 * ":function" without argument: list functions.
17816 */
17817 if (ends_excmd(*eap->arg))
17818 {
17819 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017820 {
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000017821 todo = func_hashtab.ht_used;
17822 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017823 {
17824 if (!HASHITEM_EMPTY(hi))
17825 {
17826 --todo;
17827 fp = HI2UF(hi);
17828 if (!isdigit(*fp->uf_name))
17829 list_func_head(fp, FALSE);
17830 }
17831 }
17832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017833 eap->nextcmd = check_nextcmd(eap->arg);
17834 return;
17835 }
17836
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017837 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017838 * ":function /pat": list functions matching pattern.
17839 */
17840 if (*eap->arg == '/')
17841 {
17842 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
17843 if (!eap->skip)
17844 {
17845 regmatch_T regmatch;
17846
17847 c = *p;
17848 *p = NUL;
17849 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
17850 *p = c;
17851 if (regmatch.regprog != NULL)
17852 {
17853 regmatch.rm_ic = p_ic;
17854
17855 todo = func_hashtab.ht_used;
17856 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
17857 {
17858 if (!HASHITEM_EMPTY(hi))
17859 {
17860 --todo;
17861 fp = HI2UF(hi);
17862 if (!isdigit(*fp->uf_name)
17863 && vim_regexec(&regmatch, fp->uf_name, 0))
17864 list_func_head(fp, FALSE);
17865 }
17866 }
17867 }
17868 }
17869 if (*p == '/')
17870 ++p;
17871 eap->nextcmd = check_nextcmd(p);
17872 return;
17873 }
17874
17875 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017876 * Get the function name. There are these situations:
17877 * func normal function name
17878 * "name" == func, "fudi.fd_dict" == NULL
17879 * dict.func new dictionary entry
17880 * "name" == NULL, "fudi.fd_dict" set,
17881 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
17882 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017883 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017884 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
17885 * dict.func existing dict entry that's not a Funcref
17886 * "name" == NULL, "fudi.fd_dict" set,
17887 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
17888 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017889 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017890 name = trans_function_name(&p, eap->skip, 0, &fudi);
17891 paren = (vim_strchr(p, '(') != NULL);
17892 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017893 {
17894 /*
17895 * Return on an invalid expression in braces, unless the expression
17896 * evaluation has been cancelled due to an aborting error, an
17897 * interrupt, or an exception.
17898 */
17899 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017900 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017901 if (!eap->skip && fudi.fd_newkey != NULL)
17902 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017903 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017904 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017906 else
17907 eap->skip = TRUE;
17908 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000017909
Bram Moolenaar071d4272004-06-13 20:20:40 +000017910 /* An error in a function call during evaluation of an expression in magic
17911 * braces should not cause the function not to be defined. */
17912 saved_did_emsg = did_emsg;
17913 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017914
17915 /*
17916 * ":function func" with only function name: list function.
17917 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017918 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017919 {
17920 if (!ends_excmd(*skipwhite(p)))
17921 {
17922 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017923 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017924 }
17925 eap->nextcmd = check_nextcmd(p);
17926 if (eap->nextcmd != NULL)
17927 *p = NUL;
17928 if (!eap->skip && !got_int)
17929 {
17930 fp = find_func(name);
17931 if (fp != NULL)
17932 {
17933 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017934 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017935 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000017936 if (FUNCLINE(fp, j) == NULL)
17937 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017938 msg_putchar('\n');
17939 msg_outnum((long)(j + 1));
17940 if (j < 9)
17941 msg_putchar(' ');
17942 if (j < 99)
17943 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017944 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017945 out_flush(); /* show a line at a time */
17946 ui_breakcheck();
17947 }
17948 if (!got_int)
17949 {
17950 msg_putchar('\n');
17951 msg_puts((char_u *)" endfunction");
17952 }
17953 }
17954 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017955 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017956 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017957 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017958 }
17959
17960 /*
17961 * ":function name(arg1, arg2)" Define function.
17962 */
17963 p = skipwhite(p);
17964 if (*p != '(')
17965 {
17966 if (!eap->skip)
17967 {
17968 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017969 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017970 }
17971 /* attempt to continue by skipping some text */
17972 if (vim_strchr(p, '(') != NULL)
17973 p = vim_strchr(p, '(');
17974 }
17975 p = skipwhite(p + 1);
17976
17977 ga_init2(&newargs, (int)sizeof(char_u *), 3);
17978 ga_init2(&newlines, (int)sizeof(char_u *), 3);
17979
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017980 if (!eap->skip)
17981 {
17982 /* Check the name of the function. */
17983 if (name != NULL)
17984 arg = name;
17985 else
17986 arg = fudi.fd_newkey;
17987 if (arg != NULL)
17988 {
17989 if (*arg == K_SPECIAL)
17990 j = 3;
17991 else
17992 j = 0;
17993 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
17994 : eval_isnamec(arg[j])))
17995 ++j;
17996 if (arg[j] != NUL)
17997 emsg_funcname(_(e_invarg2), arg);
17998 }
17999 }
18000
Bram Moolenaar071d4272004-06-13 20:20:40 +000018001 /*
18002 * Isolate the arguments: "arg1, arg2, ...)"
18003 */
18004 while (*p != ')')
18005 {
18006 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
18007 {
18008 varargs = TRUE;
18009 p += 3;
18010 mustend = TRUE;
18011 }
18012 else
18013 {
18014 arg = p;
18015 while (ASCII_ISALNUM(*p) || *p == '_')
18016 ++p;
18017 if (arg == p || isdigit(*arg)
18018 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
18019 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
18020 {
18021 if (!eap->skip)
18022 EMSG2(_("E125: Illegal argument: %s"), arg);
18023 break;
18024 }
18025 if (ga_grow(&newargs, 1) == FAIL)
18026 goto erret;
18027 c = *p;
18028 *p = NUL;
18029 arg = vim_strsave(arg);
18030 if (arg == NULL)
18031 goto erret;
18032 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
18033 *p = c;
18034 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018035 if (*p == ',')
18036 ++p;
18037 else
18038 mustend = TRUE;
18039 }
18040 p = skipwhite(p);
18041 if (mustend && *p != ')')
18042 {
18043 if (!eap->skip)
18044 EMSG2(_(e_invarg2), eap->arg);
18045 break;
18046 }
18047 }
18048 ++p; /* skip the ')' */
18049
Bram Moolenaare9a41262005-01-15 22:18:47 +000018050 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018051 for (;;)
18052 {
18053 p = skipwhite(p);
18054 if (STRNCMP(p, "range", 5) == 0)
18055 {
18056 flags |= FC_RANGE;
18057 p += 5;
18058 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000018059 else if (STRNCMP(p, "dict", 4) == 0)
18060 {
18061 flags |= FC_DICT;
18062 p += 4;
18063 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018064 else if (STRNCMP(p, "abort", 5) == 0)
18065 {
18066 flags |= FC_ABORT;
18067 p += 5;
18068 }
18069 else
18070 break;
18071 }
18072
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018073 /* When there is a line break use what follows for the function body.
18074 * Makes 'exe "func Test()\n...\nendfunc"' work. */
18075 if (*p == '\n')
18076 line_arg = p + 1;
18077 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018078 EMSG(_(e_trailing));
18079
18080 /*
18081 * Read the body of the function, until ":endfunction" is found.
18082 */
18083 if (KeyTyped)
18084 {
18085 /* Check if the function already exists, don't let the user type the
18086 * whole function before telling him it doesn't work! For a script we
18087 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018088 if (!eap->skip && !eap->forceit)
18089 {
18090 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
18091 EMSG(_(e_funcdict));
18092 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018093 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018094 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018095
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018096 if (!eap->skip && did_emsg)
18097 goto erret;
18098
Bram Moolenaar071d4272004-06-13 20:20:40 +000018099 msg_putchar('\n'); /* don't overwrite the function name */
18100 cmdline_row = msg_row;
18101 }
18102
18103 indent = 2;
18104 nesting = 0;
18105 for (;;)
18106 {
18107 msg_scroll = TRUE;
18108 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018109 sourcing_lnum_off = sourcing_lnum;
18110
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018111 if (line_arg != NULL)
18112 {
18113 /* Use eap->arg, split up in parts by line breaks. */
18114 theline = line_arg;
18115 p = vim_strchr(theline, '\n');
18116 if (p == NULL)
18117 line_arg += STRLEN(line_arg);
18118 else
18119 {
18120 *p = NUL;
18121 line_arg = p + 1;
18122 }
18123 }
18124 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018125 theline = getcmdline(':', 0L, indent);
18126 else
18127 theline = eap->getline(':', eap->cookie, indent);
18128 if (KeyTyped)
18129 lines_left = Rows - 1;
18130 if (theline == NULL)
18131 {
18132 EMSG(_("E126: Missing :endfunction"));
18133 goto erret;
18134 }
18135
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018136 /* Detect line continuation: sourcing_lnum increased more than one. */
18137 if (sourcing_lnum > sourcing_lnum_off + 1)
18138 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
18139 else
18140 sourcing_lnum_off = 0;
18141
Bram Moolenaar071d4272004-06-13 20:20:40 +000018142 if (skip_until != NULL)
18143 {
18144 /* between ":append" and "." and between ":python <<EOF" and "EOF"
18145 * don't check for ":endfunc". */
18146 if (STRCMP(theline, skip_until) == 0)
18147 {
18148 vim_free(skip_until);
18149 skip_until = NULL;
18150 }
18151 }
18152 else
18153 {
18154 /* skip ':' and blanks*/
18155 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
18156 ;
18157
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018158 /* Check for "endfunction". */
18159 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018160 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018161 if (line_arg == NULL)
18162 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018163 break;
18164 }
18165
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018166 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000018167 * at "end". */
18168 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
18169 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018170 else if (STRNCMP(p, "if", 2) == 0
18171 || STRNCMP(p, "wh", 2) == 0
18172 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000018173 || STRNCMP(p, "try", 3) == 0)
18174 indent += 2;
18175
18176 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018177 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018178 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018179 if (*p == '!')
18180 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018181 p += eval_fname_script(p);
18182 if (ASCII_ISALPHA(*p))
18183 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018184 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018185 if (*skipwhite(p) == '(')
18186 {
18187 ++nesting;
18188 indent += 2;
18189 }
18190 }
18191 }
18192
18193 /* Check for ":append" or ":insert". */
18194 p = skip_range(p, NULL);
18195 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
18196 || (p[0] == 'i'
18197 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
18198 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
18199 skip_until = vim_strsave((char_u *)".");
18200
18201 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
18202 arg = skipwhite(skiptowhite(p));
18203 if (arg[0] == '<' && arg[1] =='<'
18204 && ((p[0] == 'p' && p[1] == 'y'
18205 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
18206 || (p[0] == 'p' && p[1] == 'e'
18207 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
18208 || (p[0] == 't' && p[1] == 'c'
18209 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
18210 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
18211 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000018212 || (p[0] == 'm' && p[1] == 'z'
18213 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018214 ))
18215 {
18216 /* ":python <<" continues until a dot, like ":append" */
18217 p = skipwhite(arg + 2);
18218 if (*p == NUL)
18219 skip_until = vim_strsave((char_u *)".");
18220 else
18221 skip_until = vim_strsave(p);
18222 }
18223 }
18224
18225 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018226 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018227 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018228 if (line_arg == NULL)
18229 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018230 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018231 }
18232
18233 /* Copy the line to newly allocated memory. get_one_sourceline()
18234 * allocates 250 bytes per line, this saves 80% on average. The cost
18235 * is an extra alloc/free. */
18236 p = vim_strsave(theline);
18237 if (p != NULL)
18238 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018239 if (line_arg == NULL)
18240 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018241 theline = p;
18242 }
18243
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018244 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
18245
18246 /* Add NULL lines for continuation lines, so that the line count is
18247 * equal to the index in the growarray. */
18248 while (sourcing_lnum_off-- > 0)
18249 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018250
18251 /* Check for end of eap->arg. */
18252 if (line_arg != NULL && *line_arg == NUL)
18253 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018254 }
18255
18256 /* Don't define the function when skipping commands or when an error was
18257 * detected. */
18258 if (eap->skip || did_emsg)
18259 goto erret;
18260
18261 /*
18262 * If there are no errors, add the function
18263 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018264 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018265 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018266 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000018267 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018268 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018269 emsg_funcname("E707: Function name conflicts with variable: %s",
18270 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018271 goto erret;
18272 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018273
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018274 fp = find_func(name);
18275 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018276 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018277 if (!eap->forceit)
18278 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018279 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018280 goto erret;
18281 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018282 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018283 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018284 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018285 name);
18286 goto erret;
18287 }
18288 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018289 ga_clear_strings(&(fp->uf_args));
18290 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018291 vim_free(name);
18292 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018294 }
18295 else
18296 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018297 char numbuf[20];
18298
18299 fp = NULL;
18300 if (fudi.fd_newkey == NULL && !eap->forceit)
18301 {
18302 EMSG(_(e_funcdict));
18303 goto erret;
18304 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000018305 if (fudi.fd_di == NULL)
18306 {
18307 /* Can't add a function to a locked dictionary */
18308 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
18309 goto erret;
18310 }
18311 /* Can't change an existing function if it is locked */
18312 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
18313 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018314
18315 /* Give the function a sequential number. Can only be used with a
18316 * Funcref! */
18317 vim_free(name);
18318 sprintf(numbuf, "%d", ++func_nr);
18319 name = vim_strsave((char_u *)numbuf);
18320 if (name == NULL)
18321 goto erret;
18322 }
18323
18324 if (fp == NULL)
18325 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018326 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018327 {
18328 int slen, plen;
18329 char_u *scriptname;
18330
18331 /* Check that the autoload name matches the script name. */
18332 j = FAIL;
18333 if (sourcing_name != NULL)
18334 {
18335 scriptname = autoload_name(name);
18336 if (scriptname != NULL)
18337 {
18338 p = vim_strchr(scriptname, '/');
18339 plen = STRLEN(p);
18340 slen = STRLEN(sourcing_name);
18341 if (slen > plen && fnamecmp(p,
18342 sourcing_name + slen - plen) == 0)
18343 j = OK;
18344 vim_free(scriptname);
18345 }
18346 }
18347 if (j == FAIL)
18348 {
18349 EMSG2(_("E746: Function name does not match script file name: %s"), name);
18350 goto erret;
18351 }
18352 }
18353
18354 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018355 if (fp == NULL)
18356 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018357
18358 if (fudi.fd_dict != NULL)
18359 {
18360 if (fudi.fd_di == NULL)
18361 {
18362 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018363 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018364 if (fudi.fd_di == NULL)
18365 {
18366 vim_free(fp);
18367 goto erret;
18368 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018369 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
18370 {
18371 vim_free(fudi.fd_di);
18372 goto erret;
18373 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018374 }
18375 else
18376 /* overwrite existing dict entry */
18377 clear_tv(&fudi.fd_di->di_tv);
18378 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018379 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018380 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018381 fp->uf_refcount = 1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018382 }
18383
Bram Moolenaar071d4272004-06-13 20:20:40 +000018384 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018385 STRCPY(fp->uf_name, name);
18386 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018387 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018388 fp->uf_args = newargs;
18389 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018390#ifdef FEAT_PROFILE
18391 fp->uf_tml_count = NULL;
18392 fp->uf_tml_total = NULL;
18393 fp->uf_tml_self = NULL;
18394 fp->uf_profiling = FALSE;
18395 if (prof_def_func())
18396 func_do_profile(fp);
18397#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018398 fp->uf_varargs = varargs;
18399 fp->uf_flags = flags;
18400 fp->uf_calls = 0;
18401 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018402 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018403
18404erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000018405 ga_clear_strings(&newargs);
18406 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018407ret_free:
18408 vim_free(skip_until);
18409 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018410 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018411 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018412}
18413
18414/*
18415 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000018416 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018417 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018418 * flags:
18419 * TFN_INT: internal function name OK
18420 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000018421 * Advances "pp" to just after the function name (if no error).
18422 */
18423 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018424trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018425 char_u **pp;
18426 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018427 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000018428 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018429{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018430 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018431 char_u *start;
18432 char_u *end;
18433 int lead;
18434 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018435 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000018436 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018437
18438 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018439 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018440 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000018441
18442 /* Check for hard coded <SNR>: already translated function ID (from a user
18443 * command). */
18444 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
18445 && (*pp)[2] == (int)KE_SNR)
18446 {
18447 *pp += 3;
18448 len = get_id_len(pp) + 3;
18449 return vim_strnsave(start, len);
18450 }
18451
18452 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
18453 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018454 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000018455 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018456 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018457
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018458 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
18459 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018460 if (end == start)
18461 {
18462 if (!skip)
18463 EMSG(_("E129: Function name required"));
18464 goto theend;
18465 }
Bram Moolenaara7043832005-01-21 11:56:39 +000018466 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018467 {
18468 /*
18469 * Report an invalid expression in braces, unless the expression
18470 * evaluation has been cancelled due to an aborting error, an
18471 * interrupt, or an exception.
18472 */
18473 if (!aborting())
18474 {
18475 if (end != NULL)
18476 EMSG2(_(e_invarg2), start);
18477 }
18478 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018479 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018480 goto theend;
18481 }
18482
18483 if (lv.ll_tv != NULL)
18484 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018485 if (fdp != NULL)
18486 {
18487 fdp->fd_dict = lv.ll_dict;
18488 fdp->fd_newkey = lv.ll_newkey;
18489 lv.ll_newkey = NULL;
18490 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018491 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018492 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
18493 {
18494 name = vim_strsave(lv.ll_tv->vval.v_string);
18495 *pp = end;
18496 }
18497 else
18498 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018499 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
18500 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018501 EMSG(_(e_funcref));
18502 else
18503 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018504 name = NULL;
18505 }
18506 goto theend;
18507 }
18508
18509 if (lv.ll_name == NULL)
18510 {
18511 /* Error found, but continue after the function name. */
18512 *pp = end;
18513 goto theend;
18514 }
18515
18516 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000018517 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018518 len = STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000018519 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
18520 && STRNCMP(lv.ll_name, "s:", 2) == 0)
18521 {
18522 /* When there was "s:" already or the name expanded to get a
18523 * leading "s:" then remove it. */
18524 lv.ll_name += 2;
18525 len -= 2;
18526 lead = 2;
18527 }
18528 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018529 else
Bram Moolenaara7043832005-01-21 11:56:39 +000018530 {
18531 if (lead == 2) /* skip over "s:" */
18532 lv.ll_name += 2;
18533 len = (int)(end - lv.ll_name);
18534 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018535
18536 /*
18537 * Copy the function name to allocated memory.
18538 * Accept <SID>name() inside a script, translate into <SNR>123_name().
18539 * Accept <SNR>123_name() outside a script.
18540 */
18541 if (skip)
18542 lead = 0; /* do nothing */
18543 else if (lead > 0)
18544 {
18545 lead = 3;
18546 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
18547 {
18548 if (current_SID <= 0)
18549 {
18550 EMSG(_(e_usingsid));
18551 goto theend;
18552 }
18553 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
18554 lead += (int)STRLEN(sid_buf);
18555 }
18556 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018557 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018558 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018559 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018560 goto theend;
18561 }
18562 name = alloc((unsigned)(len + lead + 1));
18563 if (name != NULL)
18564 {
18565 if (lead > 0)
18566 {
18567 name[0] = K_SPECIAL;
18568 name[1] = KS_EXTRA;
18569 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000018570 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018571 STRCPY(name + 3, sid_buf);
18572 }
18573 mch_memmove(name + lead, lv.ll_name, (size_t)len);
18574 name[len + lead] = NUL;
18575 }
18576 *pp = end;
18577
18578theend:
18579 clear_lval(&lv);
18580 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018581}
18582
18583/*
18584 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
18585 * Return 2 if "p" starts with "s:".
18586 * Return 0 otherwise.
18587 */
18588 static int
18589eval_fname_script(p)
18590 char_u *p;
18591{
18592 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
18593 || STRNICMP(p + 1, "SNR>", 4) == 0))
18594 return 5;
18595 if (p[0] == 's' && p[1] == ':')
18596 return 2;
18597 return 0;
18598}
18599
18600/*
18601 * Return TRUE if "p" starts with "<SID>" or "s:".
18602 * Only works if eval_fname_script() returned non-zero for "p"!
18603 */
18604 static int
18605eval_fname_sid(p)
18606 char_u *p;
18607{
18608 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
18609}
18610
18611/*
18612 * List the head of the function: "name(arg1, arg2)".
18613 */
18614 static void
18615list_func_head(fp, indent)
18616 ufunc_T *fp;
18617 int indent;
18618{
18619 int j;
18620
18621 msg_start();
18622 if (indent)
18623 MSG_PUTS(" ");
18624 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018625 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018626 {
18627 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018628 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018629 }
18630 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018631 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018632 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018633 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018634 {
18635 if (j)
18636 MSG_PUTS(", ");
18637 msg_puts(FUNCARG(fp, j));
18638 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018639 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018640 {
18641 if (j)
18642 MSG_PUTS(", ");
18643 MSG_PUTS("...");
18644 }
18645 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000018646 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000018647 if (p_verbose > 0)
18648 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018649}
18650
18651/*
18652 * Find a function by name, return pointer to it in ufuncs.
18653 * Return NULL for unknown function.
18654 */
18655 static ufunc_T *
18656find_func(name)
18657 char_u *name;
18658{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018659 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018660
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018661 hi = hash_find(&func_hashtab, name);
18662 if (!HASHITEM_EMPTY(hi))
18663 return HI2UF(hi);
18664 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018665}
18666
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018667#if defined(EXITFREE) || defined(PROTO)
18668 void
18669free_all_functions()
18670{
18671 hashitem_T *hi;
18672
18673 /* Need to start all over every time, because func_free() may change the
18674 * hash table. */
18675 while (func_hashtab.ht_used > 0)
18676 for (hi = func_hashtab.ht_array; ; ++hi)
18677 if (!HASHITEM_EMPTY(hi))
18678 {
18679 func_free(HI2UF(hi));
18680 break;
18681 }
18682}
18683#endif
18684
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018685/*
18686 * Return TRUE if a function "name" exists.
18687 */
18688 static int
18689function_exists(name)
18690 char_u *name;
18691{
18692 char_u *p = name;
18693 int n = FALSE;
18694
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018695 p = trans_function_name(&p, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018696 if (p != NULL)
18697 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018698 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018699 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018700 else
18701 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018702 vim_free(p);
18703 }
18704 return n;
18705}
18706
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018707/*
18708 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018709 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018710 */
18711 static int
18712builtin_function(name)
18713 char_u *name;
18714{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018715 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
18716 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018717}
18718
Bram Moolenaar05159a02005-02-26 23:04:13 +000018719#if defined(FEAT_PROFILE) || defined(PROTO)
18720/*
18721 * Start profiling function "fp".
18722 */
18723 static void
18724func_do_profile(fp)
18725 ufunc_T *fp;
18726{
18727 fp->uf_tm_count = 0;
18728 profile_zero(&fp->uf_tm_self);
18729 profile_zero(&fp->uf_tm_total);
18730 if (fp->uf_tml_count == NULL)
18731 fp->uf_tml_count = (int *)alloc_clear((unsigned)
18732 (sizeof(int) * fp->uf_lines.ga_len));
18733 if (fp->uf_tml_total == NULL)
18734 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
18735 (sizeof(proftime_T) * fp->uf_lines.ga_len));
18736 if (fp->uf_tml_self == NULL)
18737 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
18738 (sizeof(proftime_T) * fp->uf_lines.ga_len));
18739 fp->uf_tml_idx = -1;
18740 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
18741 || fp->uf_tml_self == NULL)
18742 return; /* out of memory */
18743
18744 fp->uf_profiling = TRUE;
18745}
18746
18747/*
18748 * Dump the profiling results for all functions in file "fd".
18749 */
18750 void
18751func_dump_profile(fd)
18752 FILE *fd;
18753{
18754 hashitem_T *hi;
18755 int todo;
18756 ufunc_T *fp;
18757 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000018758 ufunc_T **sorttab;
18759 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018760
18761 todo = func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000018762 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
18763
Bram Moolenaar05159a02005-02-26 23:04:13 +000018764 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
18765 {
18766 if (!HASHITEM_EMPTY(hi))
18767 {
18768 --todo;
18769 fp = HI2UF(hi);
18770 if (fp->uf_profiling)
18771 {
Bram Moolenaar73830342005-02-28 22:48:19 +000018772 if (sorttab != NULL)
18773 sorttab[st_len++] = fp;
18774
Bram Moolenaar05159a02005-02-26 23:04:13 +000018775 if (fp->uf_name[0] == K_SPECIAL)
18776 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
18777 else
18778 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
18779 if (fp->uf_tm_count == 1)
18780 fprintf(fd, "Called 1 time\n");
18781 else
18782 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
18783 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
18784 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
18785 fprintf(fd, "\n");
18786 fprintf(fd, "count total (s) self (s)\n");
18787
18788 for (i = 0; i < fp->uf_lines.ga_len; ++i)
18789 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018790 if (FUNCLINE(fp, i) == NULL)
18791 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000018792 prof_func_line(fd, fp->uf_tml_count[i],
18793 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018794 fprintf(fd, "%s\n", FUNCLINE(fp, i));
18795 }
18796 fprintf(fd, "\n");
18797 }
18798 }
18799 }
Bram Moolenaar73830342005-02-28 22:48:19 +000018800
18801 if (sorttab != NULL && st_len > 0)
18802 {
18803 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
18804 prof_total_cmp);
18805 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
18806 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
18807 prof_self_cmp);
18808 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
18809 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000018810}
Bram Moolenaar73830342005-02-28 22:48:19 +000018811
18812 static void
18813prof_sort_list(fd, sorttab, st_len, title, prefer_self)
18814 FILE *fd;
18815 ufunc_T **sorttab;
18816 int st_len;
18817 char *title;
18818 int prefer_self; /* when equal print only self time */
18819{
18820 int i;
18821 ufunc_T *fp;
18822
18823 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
18824 fprintf(fd, "count total (s) self (s) function\n");
18825 for (i = 0; i < 20 && i < st_len; ++i)
18826 {
18827 fp = sorttab[i];
18828 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
18829 prefer_self);
18830 if (fp->uf_name[0] == K_SPECIAL)
18831 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
18832 else
18833 fprintf(fd, " %s()\n", fp->uf_name);
18834 }
18835 fprintf(fd, "\n");
18836}
18837
18838/*
18839 * Print the count and times for one function or function line.
18840 */
18841 static void
18842prof_func_line(fd, count, total, self, prefer_self)
18843 FILE *fd;
18844 int count;
18845 proftime_T *total;
18846 proftime_T *self;
18847 int prefer_self; /* when equal print only self time */
18848{
18849 if (count > 0)
18850 {
18851 fprintf(fd, "%5d ", count);
18852 if (prefer_self && profile_equal(total, self))
18853 fprintf(fd, " ");
18854 else
18855 fprintf(fd, "%s ", profile_msg(total));
18856 if (!prefer_self && profile_equal(total, self))
18857 fprintf(fd, " ");
18858 else
18859 fprintf(fd, "%s ", profile_msg(self));
18860 }
18861 else
18862 fprintf(fd, " ");
18863}
18864
18865/*
18866 * Compare function for total time sorting.
18867 */
18868 static int
18869#ifdef __BORLANDC__
18870_RTLENTRYF
18871#endif
18872prof_total_cmp(s1, s2)
18873 const void *s1;
18874 const void *s2;
18875{
18876 ufunc_T *p1, *p2;
18877
18878 p1 = *(ufunc_T **)s1;
18879 p2 = *(ufunc_T **)s2;
18880 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
18881}
18882
18883/*
18884 * Compare function for self time sorting.
18885 */
18886 static int
18887#ifdef __BORLANDC__
18888_RTLENTRYF
18889#endif
18890prof_self_cmp(s1, s2)
18891 const void *s1;
18892 const void *s2;
18893{
18894 ufunc_T *p1, *p2;
18895
18896 p1 = *(ufunc_T **)s1;
18897 p2 = *(ufunc_T **)s2;
18898 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
18899}
18900
Bram Moolenaar05159a02005-02-26 23:04:13 +000018901#endif
18902
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018903/* The names of packages that once were loaded is remembered. */
18904static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
18905
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018906/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018907 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018908 * Return TRUE if a package was loaded.
18909 */
18910 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018911script_autoload(name, reload)
18912 char_u *name;
18913 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018914{
18915 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018916 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018917 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018918 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018919
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018920 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018921 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018922 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018923 return FALSE;
18924
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018925 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018926
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018927 /* Find the name in the list of previously loaded package names. Skip
18928 * "autoload/", it's always the same. */
18929 for (i = 0; i < ga_loaded.ga_len; ++i)
18930 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
18931 break;
18932 if (!reload && i < ga_loaded.ga_len)
18933 ret = FALSE; /* was loaded already */
18934 else
18935 {
18936 /* Remember the name if it wasn't loaded already. */
18937 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
18938 {
18939 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
18940 tofree = NULL;
18941 }
18942
18943 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000018944 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018945 ret = TRUE;
18946 }
18947
18948 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018949 return ret;
18950}
18951
18952/*
18953 * Return the autoload script name for a function or variable name.
18954 * Returns NULL when out of memory.
18955 */
18956 static char_u *
18957autoload_name(name)
18958 char_u *name;
18959{
18960 char_u *p;
18961 char_u *scriptname;
18962
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018963 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018964 scriptname = alloc((unsigned)(STRLEN(name) + 14));
18965 if (scriptname == NULL)
18966 return FALSE;
18967 STRCPY(scriptname, "autoload/");
18968 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018969 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018970 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018971 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018972 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018973 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018974}
18975
Bram Moolenaar071d4272004-06-13 20:20:40 +000018976#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
18977
18978/*
18979 * Function given to ExpandGeneric() to obtain the list of user defined
18980 * function names.
18981 */
18982 char_u *
18983get_user_func_name(xp, idx)
18984 expand_T *xp;
18985 int idx;
18986{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018987 static long_u done;
18988 static hashitem_T *hi;
18989 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018990
18991 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018992 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018993 done = 0;
18994 hi = func_hashtab.ht_array;
18995 }
18996 if (done < func_hashtab.ht_used)
18997 {
18998 if (done++ > 0)
18999 ++hi;
19000 while (HASHITEM_EMPTY(hi))
19001 ++hi;
19002 fp = HI2UF(hi);
19003
19004 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
19005 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019006
19007 cat_func_name(IObuff, fp);
19008 if (xp->xp_context != EXPAND_USER_FUNC)
19009 {
19010 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019011 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019012 STRCAT(IObuff, ")");
19013 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019014 return IObuff;
19015 }
19016 return NULL;
19017}
19018
19019#endif /* FEAT_CMDL_COMPL */
19020
19021/*
19022 * Copy the function name of "fp" to buffer "buf".
19023 * "buf" must be able to hold the function name plus three bytes.
19024 * Takes care of script-local function names.
19025 */
19026 static void
19027cat_func_name(buf, fp)
19028 char_u *buf;
19029 ufunc_T *fp;
19030{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019031 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019032 {
19033 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019034 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019035 }
19036 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019037 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019038}
19039
19040/*
19041 * ":delfunction {name}"
19042 */
19043 void
19044ex_delfunction(eap)
19045 exarg_T *eap;
19046{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019047 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019048 char_u *p;
19049 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019050 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019051
19052 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019053 name = trans_function_name(&p, eap->skip, 0, &fudi);
19054 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019055 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019056 {
19057 if (fudi.fd_dict != NULL && !eap->skip)
19058 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019059 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019061 if (!ends_excmd(*skipwhite(p)))
19062 {
19063 vim_free(name);
19064 EMSG(_(e_trailing));
19065 return;
19066 }
19067 eap->nextcmd = check_nextcmd(p);
19068 if (eap->nextcmd != NULL)
19069 *p = NUL;
19070
19071 if (!eap->skip)
19072 fp = find_func(name);
19073 vim_free(name);
19074
19075 if (!eap->skip)
19076 {
19077 if (fp == NULL)
19078 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019079 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019080 return;
19081 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019082 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019083 {
19084 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
19085 return;
19086 }
19087
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019088 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019089 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019090 /* Delete the dict item that refers to the function, it will
19091 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019092 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019093 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019094 else
19095 func_free(fp);
19096 }
19097}
19098
19099/*
19100 * Free a function and remove it from the list of functions.
19101 */
19102 static void
19103func_free(fp)
19104 ufunc_T *fp;
19105{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019106 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019107
19108 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019109 ga_clear_strings(&(fp->uf_args));
19110 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000019111#ifdef FEAT_PROFILE
19112 vim_free(fp->uf_tml_count);
19113 vim_free(fp->uf_tml_total);
19114 vim_free(fp->uf_tml_self);
19115#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019116
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019117 /* remove the function from the function hashtable */
19118 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
19119 if (HASHITEM_EMPTY(hi))
19120 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019121 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019122 hash_remove(&func_hashtab, hi);
19123
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019124 vim_free(fp);
19125}
19126
19127/*
19128 * Unreference a Function: decrement the reference count and free it when it
19129 * becomes zero. Only for numbered functions.
19130 */
19131 static void
19132func_unref(name)
19133 char_u *name;
19134{
19135 ufunc_T *fp;
19136
19137 if (name != NULL && isdigit(*name))
19138 {
19139 fp = find_func(name);
19140 if (fp == NULL)
19141 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019142 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019143 {
19144 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019145 * when "uf_calls" becomes zero. */
19146 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019147 func_free(fp);
19148 }
19149 }
19150}
19151
19152/*
19153 * Count a reference to a Function.
19154 */
19155 static void
19156func_ref(name)
19157 char_u *name;
19158{
19159 ufunc_T *fp;
19160
19161 if (name != NULL && isdigit(*name))
19162 {
19163 fp = find_func(name);
19164 if (fp == NULL)
19165 EMSG2(_(e_intern2), "func_ref()");
19166 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019167 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019168 }
19169}
19170
19171/*
19172 * Call a user function.
19173 */
19174 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000019175call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019176 ufunc_T *fp; /* pointer to function */
19177 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000019178 typval_T *argvars; /* arguments */
19179 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019180 linenr_T firstline; /* first line of range */
19181 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000019182 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019183{
Bram Moolenaar33570922005-01-25 22:26:29 +000019184 char_u *save_sourcing_name;
19185 linenr_T save_sourcing_lnum;
19186 scid_T save_current_SID;
19187 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000019188 int save_did_emsg;
19189 static int depth = 0;
19190 dictitem_T *v;
19191 int fixvar_idx = 0; /* index in fixvar[] */
19192 int i;
19193 int ai;
19194 char_u numbuf[NUMBUFLEN];
19195 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019196#ifdef FEAT_PROFILE
19197 proftime_T wait_start;
19198#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019199
19200 /* If depth of calling is getting too high, don't execute the function */
19201 if (depth >= p_mfd)
19202 {
19203 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019204 rettv->v_type = VAR_NUMBER;
19205 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019206 return;
19207 }
19208 ++depth;
19209
19210 line_breakcheck(); /* check for CTRL-C hit */
19211
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019212 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000019213 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019214 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019215 fc.rettv = rettv;
19216 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019217 fc.linenr = 0;
19218 fc.returned = FALSE;
19219 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019220 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019221 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019222 fc.dbg_tick = debug_tick;
19223
Bram Moolenaar33570922005-01-25 22:26:29 +000019224 /*
19225 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
19226 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
19227 * each argument variable and saves a lot of time.
19228 */
19229 /*
19230 * Init l: variables.
19231 */
19232 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000019233 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019234 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019235 /* Set l:self to "selfdict". */
19236 v = &fc.fixvar[fixvar_idx++].var;
19237 STRCPY(v->di_key, "self");
19238 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
19239 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
19240 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019241 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000019242 v->di_tv.vval.v_dict = selfdict;
19243 ++selfdict->dv_refcount;
19244 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000019245
Bram Moolenaar33570922005-01-25 22:26:29 +000019246 /*
19247 * Init a: variables.
19248 * Set a:0 to "argcount".
19249 * Set a:000 to a list with room for the "..." arguments.
19250 */
19251 init_var_dict(&fc.l_avars, &fc.l_avars_var);
19252 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019253 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000019254 v = &fc.fixvar[fixvar_idx++].var;
19255 STRCPY(v->di_key, "000");
19256 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19257 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
19258 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019259 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019260 v->di_tv.vval.v_list = &fc.l_varlist;
19261 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
19262 fc.l_varlist.lv_refcount = 99999;
19263
19264 /*
19265 * Set a:firstline to "firstline" and a:lastline to "lastline".
19266 * Set a:name to named arguments.
19267 * Set a:N to the "..." arguments.
19268 */
19269 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
19270 (varnumber_T)firstline);
19271 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
19272 (varnumber_T)lastline);
19273 for (i = 0; i < argcount; ++i)
19274 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019275 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000019276 if (ai < 0)
19277 /* named argument a:name */
19278 name = FUNCARG(fp, i);
19279 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019280 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019281 /* "..." argument a:1, a:2, etc. */
19282 sprintf((char *)numbuf, "%d", ai + 1);
19283 name = numbuf;
19284 }
19285 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
19286 {
19287 v = &fc.fixvar[fixvar_idx++].var;
19288 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19289 }
19290 else
19291 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019292 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19293 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000019294 if (v == NULL)
19295 break;
19296 v->di_flags = DI_FLAGS_RO;
19297 }
19298 STRCPY(v->di_key, name);
19299 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
19300
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019301 /* Note: the values are copied directly to avoid alloc/free.
19302 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019303 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019304 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019305
19306 if (ai >= 0 && ai < MAX_FUNC_ARGS)
19307 {
19308 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
19309 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019310 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019311 }
19312 }
19313
Bram Moolenaar071d4272004-06-13 20:20:40 +000019314 /* Don't redraw while executing the function. */
19315 ++RedrawingDisabled;
19316 save_sourcing_name = sourcing_name;
19317 save_sourcing_lnum = sourcing_lnum;
19318 sourcing_lnum = 1;
19319 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019320 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019321 if (sourcing_name != NULL)
19322 {
19323 if (save_sourcing_name != NULL
19324 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
19325 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
19326 else
19327 STRCPY(sourcing_name, "function ");
19328 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
19329
19330 if (p_verbose >= 12)
19331 {
19332 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019333 verbose_enter_scroll();
19334
Bram Moolenaar555b2802005-05-19 21:08:39 +000019335 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019336 if (p_verbose >= 14)
19337 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019338 char_u buf[MSG_BUF_LEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000019339 char_u numbuf[NUMBUFLEN];
19340 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019341
19342 msg_puts((char_u *)"(");
19343 for (i = 0; i < argcount; ++i)
19344 {
19345 if (i > 0)
19346 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019347 if (argvars[i].v_type == VAR_NUMBER)
19348 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019349 else
19350 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019351 trunc_string(tv2string(&argvars[i], &tofree, numbuf, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019352 buf, MSG_BUF_CLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019353 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019354 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019355 }
19356 }
19357 msg_puts((char_u *)")");
19358 }
19359 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019360
19361 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019362 --no_wait_return;
19363 }
19364 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019365#ifdef FEAT_PROFILE
19366 if (do_profiling)
19367 {
19368 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
19369 func_do_profile(fp);
19370 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019371 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000019372 {
19373 ++fp->uf_tm_count;
19374 profile_start(&fp->uf_tm_start);
19375 profile_zero(&fp->uf_tm_children);
19376 }
19377 script_prof_save(&wait_start);
19378 }
19379#endif
19380
Bram Moolenaar071d4272004-06-13 20:20:40 +000019381 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019382 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019383 save_did_emsg = did_emsg;
19384 did_emsg = FALSE;
19385
19386 /* call do_cmdline() to execute the lines */
19387 do_cmdline(NULL, get_func_line, (void *)&fc,
19388 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
19389
19390 --RedrawingDisabled;
19391
19392 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019393 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019394 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019395 clear_tv(rettv);
19396 rettv->v_type = VAR_NUMBER;
19397 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019398 }
19399
Bram Moolenaar05159a02005-02-26 23:04:13 +000019400#ifdef FEAT_PROFILE
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019401 if (fp->uf_profiling || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000019402 {
19403 profile_end(&fp->uf_tm_start);
19404 profile_sub_wait(&wait_start, &fp->uf_tm_start);
19405 profile_add(&fp->uf_tm_total, &fp->uf_tm_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000019406 profile_self(&fp->uf_tm_self, &fp->uf_tm_start, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019407 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019408 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019409 profile_add(&fc.caller->func->uf_tm_children, &fp->uf_tm_start);
19410 profile_add(&fc.caller->func->uf_tml_children, &fp->uf_tm_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019411 }
19412 }
19413#endif
19414
Bram Moolenaar071d4272004-06-13 20:20:40 +000019415 /* when being verbose, mention the return value */
19416 if (p_verbose >= 12)
19417 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019418 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019419 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019420
Bram Moolenaar071d4272004-06-13 20:20:40 +000019421 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000019422 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019423 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000019424 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
19425 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019426 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000019427 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000019428 char_u buf[MSG_BUF_LEN];
19429 char_u numbuf[NUMBUFLEN];
19430 char_u *tofree;
19431
Bram Moolenaar555b2802005-05-19 21:08:39 +000019432 /* The value may be very long. Skip the middle part, so that we
19433 * have some idea how it starts and ends. smsg() would always
19434 * truncate it at the end. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019435 trunc_string(tv2string(fc.rettv, &tofree, numbuf, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019436 buf, MSG_BUF_CLEN);
Bram Moolenaar555b2802005-05-19 21:08:39 +000019437 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019438 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019439 }
19440 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019441
19442 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019443 --no_wait_return;
19444 }
19445
19446 vim_free(sourcing_name);
19447 sourcing_name = save_sourcing_name;
19448 sourcing_lnum = save_sourcing_lnum;
19449 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019450#ifdef FEAT_PROFILE
19451 if (do_profiling)
19452 script_prof_restore(&wait_start);
19453#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019454
19455 if (p_verbose >= 12 && sourcing_name != NULL)
19456 {
19457 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019458 verbose_enter_scroll();
19459
Bram Moolenaar555b2802005-05-19 21:08:39 +000019460 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019461 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019462
19463 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019464 --no_wait_return;
19465 }
19466
19467 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019468 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019469
Bram Moolenaar33570922005-01-25 22:26:29 +000019470 /* The a: variables typevals were not alloced, only free the allocated
19471 * variables. */
19472 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
19473
19474 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019475 --depth;
19476}
19477
19478/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019479 * Add a number variable "name" to dict "dp" with value "nr".
19480 */
19481 static void
19482add_nr_var(dp, v, name, nr)
19483 dict_T *dp;
19484 dictitem_T *v;
19485 char *name;
19486 varnumber_T nr;
19487{
19488 STRCPY(v->di_key, name);
19489 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19490 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
19491 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019492 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019493 v->di_tv.vval.v_number = nr;
19494}
19495
19496/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019497 * ":return [expr]"
19498 */
19499 void
19500ex_return(eap)
19501 exarg_T *eap;
19502{
19503 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019504 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019505 int returning = FALSE;
19506
19507 if (current_funccal == NULL)
19508 {
19509 EMSG(_("E133: :return not inside a function"));
19510 return;
19511 }
19512
19513 if (eap->skip)
19514 ++emsg_skip;
19515
19516 eap->nextcmd = NULL;
19517 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019518 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019519 {
19520 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019521 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019522 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019523 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019524 }
19525 /* It's safer to return also on error. */
19526 else if (!eap->skip)
19527 {
19528 /*
19529 * Return unless the expression evaluation has been cancelled due to an
19530 * aborting error, an interrupt, or an exception.
19531 */
19532 if (!aborting())
19533 returning = do_return(eap, FALSE, TRUE, NULL);
19534 }
19535
19536 /* When skipping or the return gets pending, advance to the next command
19537 * in this line (!returning). Otherwise, ignore the rest of the line.
19538 * Following lines will be ignored by get_func_line(). */
19539 if (returning)
19540 eap->nextcmd = NULL;
19541 else if (eap->nextcmd == NULL) /* no argument */
19542 eap->nextcmd = check_nextcmd(arg);
19543
19544 if (eap->skip)
19545 --emsg_skip;
19546}
19547
19548/*
19549 * Return from a function. Possibly makes the return pending. Also called
19550 * for a pending return at the ":endtry" or after returning from an extra
19551 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000019552 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019553 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019554 * FALSE when the return gets pending.
19555 */
19556 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019557do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019558 exarg_T *eap;
19559 int reanimate;
19560 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019561 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019562{
19563 int idx;
19564 struct condstack *cstack = eap->cstack;
19565
19566 if (reanimate)
19567 /* Undo the return. */
19568 current_funccal->returned = FALSE;
19569
19570 /*
19571 * Cleanup (and inactivate) conditionals, but stop when a try conditional
19572 * not in its finally clause (which then is to be executed next) is found.
19573 * In this case, make the ":return" pending for execution at the ":endtry".
19574 * Otherwise, return normally.
19575 */
19576 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
19577 if (idx >= 0)
19578 {
19579 cstack->cs_pending[idx] = CSTP_RETURN;
19580
19581 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019582 /* A pending return again gets pending. "rettv" points to an
19583 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000019584 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019585 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019586 else
19587 {
19588 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019589 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019590 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019591 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019592
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019593 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019594 {
19595 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019596 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019597 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019598 else
19599 EMSG(_(e_outofmem));
19600 }
19601 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019602 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019603
19604 if (reanimate)
19605 {
19606 /* The pending return value could be overwritten by a ":return"
19607 * without argument in a finally clause; reset the default
19608 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019609 current_funccal->rettv->v_type = VAR_NUMBER;
19610 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019611 }
19612 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019613 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019614 }
19615 else
19616 {
19617 current_funccal->returned = TRUE;
19618
19619 /* If the return is carried out now, store the return value. For
19620 * a return immediately after reanimation, the value is already
19621 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019622 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019623 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019624 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000019625 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019626 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019627 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019628 }
19629 }
19630
19631 return idx < 0;
19632}
19633
19634/*
19635 * Free the variable with a pending return value.
19636 */
19637 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019638discard_pending_return(rettv)
19639 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019640{
Bram Moolenaar33570922005-01-25 22:26:29 +000019641 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019642}
19643
19644/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019645 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000019646 * is an allocated string. Used by report_pending() for verbose messages.
19647 */
19648 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019649get_return_cmd(rettv)
19650 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019651{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019652 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019653 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019654 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019655
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019656 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019657 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019658 if (s == NULL)
19659 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019660
19661 STRCPY(IObuff, ":return ");
19662 STRNCPY(IObuff + 8, s, IOSIZE - 8);
19663 if (STRLEN(s) + 8 >= IOSIZE)
19664 STRCPY(IObuff + IOSIZE - 4, "...");
19665 vim_free(tofree);
19666 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019667}
19668
19669/*
19670 * Get next function line.
19671 * Called by do_cmdline() to get the next line.
19672 * Returns allocated string, or NULL for end of function.
19673 */
19674/* ARGSUSED */
19675 char_u *
19676get_func_line(c, cookie, indent)
19677 int c; /* not used */
19678 void *cookie;
19679 int indent; /* not used */
19680{
Bram Moolenaar33570922005-01-25 22:26:29 +000019681 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019682 ufunc_T *fp = fcp->func;
19683 char_u *retval;
19684 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019685
19686 /* If breakpoints have been added/deleted need to check for it. */
19687 if (fcp->dbg_tick != debug_tick)
19688 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019689 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019690 sourcing_lnum);
19691 fcp->dbg_tick = debug_tick;
19692 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019693#ifdef FEAT_PROFILE
19694 if (do_profiling)
19695 func_line_end(cookie);
19696#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019697
Bram Moolenaar05159a02005-02-26 23:04:13 +000019698 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019699 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
19700 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019701 retval = NULL;
19702 else
19703 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019704 /* Skip NULL lines (continuation lines). */
19705 while (fcp->linenr < gap->ga_len
19706 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
19707 ++fcp->linenr;
19708 if (fcp->linenr >= gap->ga_len)
19709 retval = NULL;
19710 else
19711 {
19712 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
19713 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019714#ifdef FEAT_PROFILE
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019715 if (do_profiling)
19716 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019717#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019719 }
19720
19721 /* Did we encounter a breakpoint? */
19722 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
19723 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019724 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019725 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019726 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019727 sourcing_lnum);
19728 fcp->dbg_tick = debug_tick;
19729 }
19730
19731 return retval;
19732}
19733
Bram Moolenaar05159a02005-02-26 23:04:13 +000019734#if defined(FEAT_PROFILE) || defined(PROTO)
19735/*
19736 * Called when starting to read a function line.
19737 * "sourcing_lnum" must be correct!
19738 * When skipping lines it may not actually be executed, but we won't find out
19739 * until later and we need to store the time now.
19740 */
19741 void
19742func_line_start(cookie)
19743 void *cookie;
19744{
19745 funccall_T *fcp = (funccall_T *)cookie;
19746 ufunc_T *fp = fcp->func;
19747
19748 if (fp->uf_profiling && sourcing_lnum >= 1
19749 && sourcing_lnum <= fp->uf_lines.ga_len)
19750 {
19751 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019752 /* Skip continuation lines. */
19753 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
19754 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019755 fp->uf_tml_execed = FALSE;
19756 profile_start(&fp->uf_tml_start);
19757 profile_zero(&fp->uf_tml_children);
19758 profile_get_wait(&fp->uf_tml_wait);
19759 }
19760}
19761
19762/*
19763 * Called when actually executing a function line.
19764 */
19765 void
19766func_line_exec(cookie)
19767 void *cookie;
19768{
19769 funccall_T *fcp = (funccall_T *)cookie;
19770 ufunc_T *fp = fcp->func;
19771
19772 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
19773 fp->uf_tml_execed = TRUE;
19774}
19775
19776/*
19777 * Called when done with a function line.
19778 */
19779 void
19780func_line_end(cookie)
19781 void *cookie;
19782{
19783 funccall_T *fcp = (funccall_T *)cookie;
19784 ufunc_T *fp = fcp->func;
19785
19786 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
19787 {
19788 if (fp->uf_tml_execed)
19789 {
19790 ++fp->uf_tml_count[fp->uf_tml_idx];
19791 profile_end(&fp->uf_tml_start);
19792 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019793 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000019794 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
19795 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019796 }
19797 fp->uf_tml_idx = -1;
19798 }
19799}
19800#endif
19801
Bram Moolenaar071d4272004-06-13 20:20:40 +000019802/*
19803 * Return TRUE if the currently active function should be ended, because a
19804 * return was encountered or an error occured. Used inside a ":while".
19805 */
19806 int
19807func_has_ended(cookie)
19808 void *cookie;
19809{
Bram Moolenaar33570922005-01-25 22:26:29 +000019810 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019811
19812 /* Ignore the "abort" flag if the abortion behavior has been changed due to
19813 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019814 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000019815 || fcp->returned);
19816}
19817
19818/*
19819 * return TRUE if cookie indicates a function which "abort"s on errors.
19820 */
19821 int
19822func_has_abort(cookie)
19823 void *cookie;
19824{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019825 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019826}
19827
19828#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
19829typedef enum
19830{
19831 VAR_FLAVOUR_DEFAULT,
19832 VAR_FLAVOUR_SESSION,
19833 VAR_FLAVOUR_VIMINFO
19834} var_flavour_T;
19835
19836static var_flavour_T var_flavour __ARGS((char_u *varname));
19837
19838 static var_flavour_T
19839var_flavour(varname)
19840 char_u *varname;
19841{
19842 char_u *p = varname;
19843
19844 if (ASCII_ISUPPER(*p))
19845 {
19846 while (*(++p))
19847 if (ASCII_ISLOWER(*p))
19848 return VAR_FLAVOUR_SESSION;
19849 return VAR_FLAVOUR_VIMINFO;
19850 }
19851 else
19852 return VAR_FLAVOUR_DEFAULT;
19853}
19854#endif
19855
19856#if defined(FEAT_VIMINFO) || defined(PROTO)
19857/*
19858 * Restore global vars that start with a capital from the viminfo file
19859 */
19860 int
19861read_viminfo_varlist(virp, writing)
19862 vir_T *virp;
19863 int writing;
19864{
19865 char_u *tab;
19866 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000019867 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019868
19869 if (!writing && (find_viminfo_parameter('!') != NULL))
19870 {
19871 tab = vim_strchr(virp->vir_line + 1, '\t');
19872 if (tab != NULL)
19873 {
19874 *tab++ = '\0'; /* isolate the variable name */
19875 if (*tab == 'S') /* string var */
19876 is_string = TRUE;
19877
19878 tab = vim_strchr(tab, '\t');
19879 if (tab != NULL)
19880 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019881 if (is_string)
19882 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019883 tv.v_type = VAR_STRING;
19884 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019885 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019886 }
19887 else
19888 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019889 tv.v_type = VAR_NUMBER;
19890 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019891 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019892 set_var(virp->vir_line + 1, &tv, FALSE);
19893 if (is_string)
19894 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019895 }
19896 }
19897 }
19898
19899 return viminfo_readline(virp);
19900}
19901
19902/*
19903 * Write global vars that start with a capital to the viminfo file
19904 */
19905 void
19906write_viminfo_varlist(fp)
19907 FILE *fp;
19908{
Bram Moolenaar33570922005-01-25 22:26:29 +000019909 hashitem_T *hi;
19910 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000019911 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019912 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019913 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019914 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019915 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019916
19917 if (find_viminfo_parameter('!') == NULL)
19918 return;
19919
19920 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000019921
Bram Moolenaar33570922005-01-25 22:26:29 +000019922 todo = globvarht.ht_used;
19923 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019924 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019925 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019926 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019927 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019928 this_var = HI2DI(hi);
19929 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019930 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019931 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000019932 {
19933 case VAR_STRING: s = "STR"; break;
19934 case VAR_NUMBER: s = "NUM"; break;
19935 default: continue;
19936 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019937 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019938 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019939 if (p != NULL)
19940 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000019941 vim_free(tofree);
19942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019943 }
19944 }
19945}
19946#endif
19947
19948#if defined(FEAT_SESSION) || defined(PROTO)
19949 int
19950store_session_globals(fd)
19951 FILE *fd;
19952{
Bram Moolenaar33570922005-01-25 22:26:29 +000019953 hashitem_T *hi;
19954 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000019955 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019956 char_u *p, *t;
19957
Bram Moolenaar33570922005-01-25 22:26:29 +000019958 todo = globvarht.ht_used;
19959 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019960 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019961 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019962 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019963 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019964 this_var = HI2DI(hi);
19965 if ((this_var->di_tv.v_type == VAR_NUMBER
19966 || this_var->di_tv.v_type == VAR_STRING)
19967 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019968 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019969 /* Escape special characters with a backslash. Turn a LF and
19970 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019971 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000019972 (char_u *)"\\\"\n\r");
19973 if (p == NULL) /* out of memory */
19974 break;
19975 for (t = p; *t != NUL; ++t)
19976 if (*t == '\n')
19977 *t = 'n';
19978 else if (*t == '\r')
19979 *t = 'r';
19980 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000019981 this_var->di_key,
19982 (this_var->di_tv.v_type == VAR_STRING) ? '"'
19983 : ' ',
19984 p,
19985 (this_var->di_tv.v_type == VAR_STRING) ? '"'
19986 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000019987 || put_eol(fd) == FAIL)
19988 {
19989 vim_free(p);
19990 return FAIL;
19991 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019992 vim_free(p);
19993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019994 }
19995 }
19996 return OK;
19997}
19998#endif
19999
Bram Moolenaar661b1822005-07-28 22:36:45 +000020000/*
20001 * Display script name where an item was last set.
20002 * Should only be invoked when 'verbose' is non-zero.
20003 */
20004 void
20005last_set_msg(scriptID)
20006 scid_T scriptID;
20007{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020008 char_u *p;
20009
Bram Moolenaar661b1822005-07-28 22:36:45 +000020010 if (scriptID != 0)
20011 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020012 p = home_replace_save(NULL, get_scriptname(scriptID));
20013 if (p != NULL)
20014 {
20015 verbose_enter();
20016 MSG_PUTS(_("\n\tLast set from "));
20017 MSG_PUTS(p);
20018 vim_free(p);
20019 verbose_leave();
20020 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000020021 }
20022}
20023
Bram Moolenaar071d4272004-06-13 20:20:40 +000020024#endif /* FEAT_EVAL */
20025
20026#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
20027
20028
20029#ifdef WIN3264
20030/*
20031 * Functions for ":8" filename modifier: get 8.3 version of a filename.
20032 */
20033static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
20034static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
20035static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
20036
20037/*
20038 * Get the short pathname of a file.
20039 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
20040 */
20041 static int
20042get_short_pathname(fnamep, bufp, fnamelen)
20043 char_u **fnamep;
20044 char_u **bufp;
20045 int *fnamelen;
20046{
20047 int l,len;
20048 char_u *newbuf;
20049
20050 len = *fnamelen;
20051
20052 l = GetShortPathName(*fnamep, *fnamep, len);
20053 if (l > len - 1)
20054 {
20055 /* If that doesn't work (not enough space), then save the string
20056 * and try again with a new buffer big enough
20057 */
20058 newbuf = vim_strnsave(*fnamep, l);
20059 if (newbuf == NULL)
20060 return 0;
20061
20062 vim_free(*bufp);
20063 *fnamep = *bufp = newbuf;
20064
20065 l = GetShortPathName(*fnamep,*fnamep,l+1);
20066
20067 /* Really should always succeed, as the buffer is big enough */
20068 }
20069
20070 *fnamelen = l;
20071 return 1;
20072}
20073
20074/*
20075 * Create a short path name. Returns the length of the buffer it needs.
20076 * Doesn't copy over the end of the buffer passed in.
20077 */
20078 static int
20079shortpath_for_invalid_fname(fname, bufp, fnamelen)
20080 char_u **fname;
20081 char_u **bufp;
20082 int *fnamelen;
20083{
20084 char_u *s, *p, *pbuf2, *pbuf3;
20085 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020086 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020087
20088 /* Make a copy */
20089 len2 = *fnamelen;
20090 pbuf2 = vim_strnsave(*fname, len2);
20091 pbuf3 = NULL;
20092
20093 s = pbuf2 + len2 - 1; /* Find the end */
20094 slen = 1;
20095 plen = len2;
20096
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020097 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020098 {
20099 --s;
20100 ++slen;
20101 --plen;
20102 }
20103
20104 do
20105 {
20106 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020107 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020108 {
20109 --s;
20110 ++slen;
20111 --plen;
20112 }
20113 if (s <= pbuf2)
20114 break;
20115
20116 /* Remeber the character that is about to be blatted */
20117 ch = *s;
20118 *s = 0; /* get_short_pathname requires a null-terminated string */
20119
20120 /* Try it in situ */
20121 p = pbuf2;
20122 if (!get_short_pathname(&p, &pbuf3, &plen))
20123 {
20124 vim_free(pbuf2);
20125 return -1;
20126 }
20127 *s = ch; /* Preserve the string */
20128 } while (plen == 0);
20129
20130 if (plen > 0)
20131 {
20132 /* Remeber the length of the new string. */
20133 *fnamelen = len = plen + slen;
20134 vim_free(*bufp);
20135 if (len > len2)
20136 {
20137 /* If there's not enough space in the currently allocated string,
20138 * then copy it to a buffer big enough.
20139 */
20140 *fname= *bufp = vim_strnsave(p, len);
20141 if (*fname == NULL)
20142 return -1;
20143 }
20144 else
20145 {
20146 /* Transfer pbuf2 to being the main buffer (it's big enough) */
20147 *fname = *bufp = pbuf2;
20148 if (p != pbuf2)
20149 strncpy(*fname, p, plen);
20150 pbuf2 = NULL;
20151 }
20152 /* Concat the next bit */
20153 strncpy(*fname + plen, s, slen);
20154 (*fname)[len] = '\0';
20155 }
20156 vim_free(pbuf3);
20157 vim_free(pbuf2);
20158 return 0;
20159}
20160
20161/*
20162 * Get a pathname for a partial path.
20163 */
20164 static int
20165shortpath_for_partial(fnamep, bufp, fnamelen)
20166 char_u **fnamep;
20167 char_u **bufp;
20168 int *fnamelen;
20169{
20170 int sepcount, len, tflen;
20171 char_u *p;
20172 char_u *pbuf, *tfname;
20173 int hasTilde;
20174
20175 /* Count up the path seperators from the RHS.. so we know which part
20176 * of the path to return.
20177 */
20178 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020179 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020180 if (vim_ispathsep(*p))
20181 ++sepcount;
20182
20183 /* Need full path first (use expand_env() to remove a "~/") */
20184 hasTilde = (**fnamep == '~');
20185 if (hasTilde)
20186 pbuf = tfname = expand_env_save(*fnamep);
20187 else
20188 pbuf = tfname = FullName_save(*fnamep, FALSE);
20189
20190 len = tflen = STRLEN(tfname);
20191
20192 if (!get_short_pathname(&tfname, &pbuf, &len))
20193 return -1;
20194
20195 if (len == 0)
20196 {
20197 /* Don't have a valid filename, so shorten the rest of the
20198 * path if we can. This CAN give us invalid 8.3 filenames, but
20199 * there's not a lot of point in guessing what it might be.
20200 */
20201 len = tflen;
20202 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
20203 return -1;
20204 }
20205
20206 /* Count the paths backward to find the beginning of the desired string. */
20207 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020208 {
20209#ifdef FEAT_MBYTE
20210 if (has_mbyte)
20211 p -= mb_head_off(tfname, p);
20212#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020213 if (vim_ispathsep(*p))
20214 {
20215 if (sepcount == 0 || (hasTilde && sepcount == 1))
20216 break;
20217 else
20218 sepcount --;
20219 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020221 if (hasTilde)
20222 {
20223 --p;
20224 if (p >= tfname)
20225 *p = '~';
20226 else
20227 return -1;
20228 }
20229 else
20230 ++p;
20231
20232 /* Copy in the string - p indexes into tfname - allocated at pbuf */
20233 vim_free(*bufp);
20234 *fnamelen = (int)STRLEN(p);
20235 *bufp = pbuf;
20236 *fnamep = p;
20237
20238 return 0;
20239}
20240#endif /* WIN3264 */
20241
20242/*
20243 * Adjust a filename, according to a string of modifiers.
20244 * *fnamep must be NUL terminated when called. When returning, the length is
20245 * determined by *fnamelen.
20246 * Returns valid flags.
20247 * When there is an error, *fnamep is set to NULL.
20248 */
20249 int
20250modify_fname(src, usedlen, fnamep, bufp, fnamelen)
20251 char_u *src; /* string with modifiers */
20252 int *usedlen; /* characters after src that are used */
20253 char_u **fnamep; /* file name so far */
20254 char_u **bufp; /* buffer for allocated file name or NULL */
20255 int *fnamelen; /* length of fnamep */
20256{
20257 int valid = 0;
20258 char_u *tail;
20259 char_u *s, *p, *pbuf;
20260 char_u dirname[MAXPATHL];
20261 int c;
20262 int has_fullname = 0;
20263#ifdef WIN3264
20264 int has_shortname = 0;
20265#endif
20266
20267repeat:
20268 /* ":p" - full path/file_name */
20269 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
20270 {
20271 has_fullname = 1;
20272
20273 valid |= VALID_PATH;
20274 *usedlen += 2;
20275
20276 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
20277 if ((*fnamep)[0] == '~'
20278#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
20279 && ((*fnamep)[1] == '/'
20280# ifdef BACKSLASH_IN_FILENAME
20281 || (*fnamep)[1] == '\\'
20282# endif
20283 || (*fnamep)[1] == NUL)
20284
20285#endif
20286 )
20287 {
20288 *fnamep = expand_env_save(*fnamep);
20289 vim_free(*bufp); /* free any allocated file name */
20290 *bufp = *fnamep;
20291 if (*fnamep == NULL)
20292 return -1;
20293 }
20294
20295 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020296 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020297 {
20298 if (vim_ispathsep(*p)
20299 && p[1] == '.'
20300 && (p[2] == NUL
20301 || vim_ispathsep(p[2])
20302 || (p[2] == '.'
20303 && (p[3] == NUL || vim_ispathsep(p[3])))))
20304 break;
20305 }
20306
20307 /* FullName_save() is slow, don't use it when not needed. */
20308 if (*p != NUL || !vim_isAbsName(*fnamep))
20309 {
20310 *fnamep = FullName_save(*fnamep, *p != NUL);
20311 vim_free(*bufp); /* free any allocated file name */
20312 *bufp = *fnamep;
20313 if (*fnamep == NULL)
20314 return -1;
20315 }
20316
20317 /* Append a path separator to a directory. */
20318 if (mch_isdir(*fnamep))
20319 {
20320 /* Make room for one or two extra characters. */
20321 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
20322 vim_free(*bufp); /* free any allocated file name */
20323 *bufp = *fnamep;
20324 if (*fnamep == NULL)
20325 return -1;
20326 add_pathsep(*fnamep);
20327 }
20328 }
20329
20330 /* ":." - path relative to the current directory */
20331 /* ":~" - path relative to the home directory */
20332 /* ":8" - shortname path - postponed till after */
20333 while (src[*usedlen] == ':'
20334 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
20335 {
20336 *usedlen += 2;
20337 if (c == '8')
20338 {
20339#ifdef WIN3264
20340 has_shortname = 1; /* Postpone this. */
20341#endif
20342 continue;
20343 }
20344 pbuf = NULL;
20345 /* Need full path first (use expand_env() to remove a "~/") */
20346 if (!has_fullname)
20347 {
20348 if (c == '.' && **fnamep == '~')
20349 p = pbuf = expand_env_save(*fnamep);
20350 else
20351 p = pbuf = FullName_save(*fnamep, FALSE);
20352 }
20353 else
20354 p = *fnamep;
20355
20356 has_fullname = 0;
20357
20358 if (p != NULL)
20359 {
20360 if (c == '.')
20361 {
20362 mch_dirname(dirname, MAXPATHL);
20363 s = shorten_fname(p, dirname);
20364 if (s != NULL)
20365 {
20366 *fnamep = s;
20367 if (pbuf != NULL)
20368 {
20369 vim_free(*bufp); /* free any allocated file name */
20370 *bufp = pbuf;
20371 pbuf = NULL;
20372 }
20373 }
20374 }
20375 else
20376 {
20377 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
20378 /* Only replace it when it starts with '~' */
20379 if (*dirname == '~')
20380 {
20381 s = vim_strsave(dirname);
20382 if (s != NULL)
20383 {
20384 *fnamep = s;
20385 vim_free(*bufp);
20386 *bufp = s;
20387 }
20388 }
20389 }
20390 vim_free(pbuf);
20391 }
20392 }
20393
20394 tail = gettail(*fnamep);
20395 *fnamelen = (int)STRLEN(*fnamep);
20396
20397 /* ":h" - head, remove "/file_name", can be repeated */
20398 /* Don't remove the first "/" or "c:\" */
20399 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
20400 {
20401 valid |= VALID_HEAD;
20402 *usedlen += 2;
20403 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020404 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020405 --tail;
20406 *fnamelen = (int)(tail - *fnamep);
20407#ifdef VMS
20408 if (*fnamelen > 0)
20409 *fnamelen += 1; /* the path separator is part of the path */
20410#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020411 while (tail > s && !after_pathsep(s, tail))
20412 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020413 }
20414
20415 /* ":8" - shortname */
20416 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
20417 {
20418 *usedlen += 2;
20419#ifdef WIN3264
20420 has_shortname = 1;
20421#endif
20422 }
20423
20424#ifdef WIN3264
20425 /* Check shortname after we have done 'heads' and before we do 'tails'
20426 */
20427 if (has_shortname)
20428 {
20429 pbuf = NULL;
20430 /* Copy the string if it is shortened by :h */
20431 if (*fnamelen < (int)STRLEN(*fnamep))
20432 {
20433 p = vim_strnsave(*fnamep, *fnamelen);
20434 if (p == 0)
20435 return -1;
20436 vim_free(*bufp);
20437 *bufp = *fnamep = p;
20438 }
20439
20440 /* Split into two implementations - makes it easier. First is where
20441 * there isn't a full name already, second is where there is.
20442 */
20443 if (!has_fullname && !vim_isAbsName(*fnamep))
20444 {
20445 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
20446 return -1;
20447 }
20448 else
20449 {
20450 int l;
20451
20452 /* Simple case, already have the full-name
20453 * Nearly always shorter, so try first time. */
20454 l = *fnamelen;
20455 if (!get_short_pathname(fnamep, bufp, &l))
20456 return -1;
20457
20458 if (l == 0)
20459 {
20460 /* Couldn't find the filename.. search the paths.
20461 */
20462 l = *fnamelen;
20463 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
20464 return -1;
20465 }
20466 *fnamelen = l;
20467 }
20468 }
20469#endif /* WIN3264 */
20470
20471 /* ":t" - tail, just the basename */
20472 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
20473 {
20474 *usedlen += 2;
20475 *fnamelen -= (int)(tail - *fnamep);
20476 *fnamep = tail;
20477 }
20478
20479 /* ":e" - extension, can be repeated */
20480 /* ":r" - root, without extension, can be repeated */
20481 while (src[*usedlen] == ':'
20482 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
20483 {
20484 /* find a '.' in the tail:
20485 * - for second :e: before the current fname
20486 * - otherwise: The last '.'
20487 */
20488 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
20489 s = *fnamep - 2;
20490 else
20491 s = *fnamep + *fnamelen - 1;
20492 for ( ; s > tail; --s)
20493 if (s[0] == '.')
20494 break;
20495 if (src[*usedlen + 1] == 'e') /* :e */
20496 {
20497 if (s > tail)
20498 {
20499 *fnamelen += (int)(*fnamep - (s + 1));
20500 *fnamep = s + 1;
20501#ifdef VMS
20502 /* cut version from the extension */
20503 s = *fnamep + *fnamelen - 1;
20504 for ( ; s > *fnamep; --s)
20505 if (s[0] == ';')
20506 break;
20507 if (s > *fnamep)
20508 *fnamelen = s - *fnamep;
20509#endif
20510 }
20511 else if (*fnamep <= tail)
20512 *fnamelen = 0;
20513 }
20514 else /* :r */
20515 {
20516 if (s > tail) /* remove one extension */
20517 *fnamelen = (int)(s - *fnamep);
20518 }
20519 *usedlen += 2;
20520 }
20521
20522 /* ":s?pat?foo?" - substitute */
20523 /* ":gs?pat?foo?" - global substitute */
20524 if (src[*usedlen] == ':'
20525 && (src[*usedlen + 1] == 's'
20526 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
20527 {
20528 char_u *str;
20529 char_u *pat;
20530 char_u *sub;
20531 int sep;
20532 char_u *flags;
20533 int didit = FALSE;
20534
20535 flags = (char_u *)"";
20536 s = src + *usedlen + 2;
20537 if (src[*usedlen + 1] == 'g')
20538 {
20539 flags = (char_u *)"g";
20540 ++s;
20541 }
20542
20543 sep = *s++;
20544 if (sep)
20545 {
20546 /* find end of pattern */
20547 p = vim_strchr(s, sep);
20548 if (p != NULL)
20549 {
20550 pat = vim_strnsave(s, (int)(p - s));
20551 if (pat != NULL)
20552 {
20553 s = p + 1;
20554 /* find end of substitution */
20555 p = vim_strchr(s, sep);
20556 if (p != NULL)
20557 {
20558 sub = vim_strnsave(s, (int)(p - s));
20559 str = vim_strnsave(*fnamep, *fnamelen);
20560 if (sub != NULL && str != NULL)
20561 {
20562 *usedlen = (int)(p + 1 - src);
20563 s = do_string_sub(str, pat, sub, flags);
20564 if (s != NULL)
20565 {
20566 *fnamep = s;
20567 *fnamelen = (int)STRLEN(s);
20568 vim_free(*bufp);
20569 *bufp = s;
20570 didit = TRUE;
20571 }
20572 }
20573 vim_free(sub);
20574 vim_free(str);
20575 }
20576 vim_free(pat);
20577 }
20578 }
20579 /* after using ":s", repeat all the modifiers */
20580 if (didit)
20581 goto repeat;
20582 }
20583 }
20584
20585 return valid;
20586}
20587
20588/*
20589 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
20590 * "flags" can be "g" to do a global substitute.
20591 * Returns an allocated string, NULL for error.
20592 */
20593 char_u *
20594do_string_sub(str, pat, sub, flags)
20595 char_u *str;
20596 char_u *pat;
20597 char_u *sub;
20598 char_u *flags;
20599{
20600 int sublen;
20601 regmatch_T regmatch;
20602 int i;
20603 int do_all;
20604 char_u *tail;
20605 garray_T ga;
20606 char_u *ret;
20607 char_u *save_cpo;
20608
20609 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
20610 save_cpo = p_cpo;
20611 p_cpo = (char_u *)"";
20612
20613 ga_init2(&ga, 1, 200);
20614
20615 do_all = (flags[0] == 'g');
20616
20617 regmatch.rm_ic = p_ic;
20618 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
20619 if (regmatch.regprog != NULL)
20620 {
20621 tail = str;
20622 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
20623 {
20624 /*
20625 * Get some space for a temporary buffer to do the substitution
20626 * into. It will contain:
20627 * - The text up to where the match is.
20628 * - The substituted text.
20629 * - The text after the match.
20630 */
20631 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
20632 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
20633 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
20634 {
20635 ga_clear(&ga);
20636 break;
20637 }
20638
20639 /* copy the text up to where the match is */
20640 i = (int)(regmatch.startp[0] - tail);
20641 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
20642 /* add the substituted text */
20643 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
20644 + ga.ga_len + i, TRUE, TRUE, FALSE);
20645 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020646 /* avoid getting stuck on a match with an empty string */
20647 if (tail == regmatch.endp[0])
20648 {
20649 if (*tail == NUL)
20650 break;
20651 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
20652 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020653 }
20654 else
20655 {
20656 tail = regmatch.endp[0];
20657 if (*tail == NUL)
20658 break;
20659 }
20660 if (!do_all)
20661 break;
20662 }
20663
20664 if (ga.ga_data != NULL)
20665 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
20666
20667 vim_free(regmatch.regprog);
20668 }
20669
20670 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
20671 ga_clear(&ga);
20672 p_cpo = save_cpo;
20673
20674 return ret;
20675}
20676
20677#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */