blob: 63abcbae24956698240333aa9413cad227eafc4a [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 Moolenaare580b0c2006-03-21 21:33:03 +0000586static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000588static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000599static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000600static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000601static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000603static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000608static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000609static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000610static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000611static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000615static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000616static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000618static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2c932302006-03-18 21:42:09 +0000619static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000620#ifdef HAVE_STRFTIME
621static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
622#endif
623static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
624static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
625static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
626static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
627static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
628static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
629static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
630static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
631static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
632static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
633static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
634static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000635static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000636static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000637static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000638static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000639static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000640static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000641static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000642static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
643static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
644static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
645static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
646static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
647static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
648static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
649static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
650static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
651static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
652static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
653static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
654static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000655static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
656static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000657static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000658static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000659
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000660static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
661static pos_T *var2fpos __ARGS((typval_T *varp, int lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000662static int get_env_len __ARGS((char_u **arg));
663static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000664static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000665static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
666#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
667#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
668 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000669static 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 +0000670static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000671static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000672static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
673static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000674static typval_T *alloc_tv __ARGS((void));
675static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000676static void init_tv __ARGS((typval_T *varp));
677static long get_tv_number __ARGS((typval_T *varp));
678static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000679static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000680static char_u *get_tv_string __ARGS((typval_T *varp));
681static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000682static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000683static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000684static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000685static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
686static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
687static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
688static void list_one_var __ARGS((dictitem_T *v, char_u *prefix));
689static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
690static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
691static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000692static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000693static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000694static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000695static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
696static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
697static int eval_fname_script __ARGS((char_u *p));
698static int eval_fname_sid __ARGS((char_u *p));
699static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000700static ufunc_T *find_func __ARGS((char_u *name));
701static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000702static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000703#ifdef FEAT_PROFILE
704static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000705static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
706static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
707static int
708# ifdef __BORLANDC__
709 _RTLENTRYF
710# endif
711 prof_total_cmp __ARGS((const void *s1, const void *s2));
712static int
713# ifdef __BORLANDC__
714 _RTLENTRYF
715# endif
716 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000717#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000718static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000719static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000720static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000721static void func_free __ARGS((ufunc_T *fp));
722static void func_unref __ARGS((char_u *name));
723static void func_ref __ARGS((char_u *name));
724static 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));
725static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000726static win_T *find_win_by_nr __ARGS((typval_T *vp));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000727static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000728static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000729
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000730/* Character used as separated in autoload function/variable names. */
731#define AUTOLOAD_CHAR '#'
732
Bram Moolenaar33570922005-01-25 22:26:29 +0000733/*
734 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000735 */
736 void
737eval_init()
738{
Bram Moolenaar33570922005-01-25 22:26:29 +0000739 int i;
740 struct vimvar *p;
741
742 init_var_dict(&globvardict, &globvars_var);
743 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000744 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000745 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000746
747 for (i = 0; i < VV_LEN; ++i)
748 {
749 p = &vimvars[i];
750 STRCPY(p->vv_di.di_key, p->vv_name);
751 if (p->vv_flags & VV_RO)
752 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
753 else if (p->vv_flags & VV_RO_SBX)
754 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
755 else
756 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000757
758 /* add to v: scope dict, unless the value is not always available */
759 if (p->vv_type != VAR_UNKNOWN)
760 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000761 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000762 /* add to compat scope dict */
763 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000764 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000765}
766
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000767#if defined(EXITFREE) || defined(PROTO)
768 void
769eval_clear()
770{
771 int i;
772 struct vimvar *p;
773
774 for (i = 0; i < VV_LEN; ++i)
775 {
776 p = &vimvars[i];
777 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000778 {
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000779 vim_free(p->vv_di.di_tv.vval.v_string);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000780 p->vv_di.di_tv.vval.v_string = NULL;
781 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000782 }
783 hash_clear(&vimvarht);
784 hash_clear(&compat_hashtab);
785
786 /* script-local variables */
787 for (i = 1; i <= ga_scripts.ga_len; ++i)
788 vars_clear(&SCRIPT_VARS(i));
789 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000790 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000791
792 /* global variables */
793 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000794
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000795 /* functions */
Bram Moolenaard9fba312005-06-26 22:34:35 +0000796 free_all_functions();
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000797 hash_clear(&func_hashtab);
798
799 /* unreferenced lists and dicts */
800 (void)garbage_collect();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000801}
802#endif
803
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000804/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 * Return the name of the executed function.
806 */
807 char_u *
808func_name(cookie)
809 void *cookie;
810{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000811 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812}
813
814/*
815 * Return the address holding the next breakpoint line for a funccall cookie.
816 */
817 linenr_T *
818func_breakpoint(cookie)
819 void *cookie;
820{
Bram Moolenaar33570922005-01-25 22:26:29 +0000821 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822}
823
824/*
825 * Return the address holding the debug tick for a funccall cookie.
826 */
827 int *
828func_dbg_tick(cookie)
829 void *cookie;
830{
Bram Moolenaar33570922005-01-25 22:26:29 +0000831 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832}
833
834/*
835 * Return the nesting level for a funccall cookie.
836 */
837 int
838func_level(cookie)
839 void *cookie;
840{
Bram Moolenaar33570922005-01-25 22:26:29 +0000841 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842}
843
844/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000845funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846
847/*
848 * Return TRUE when a function was ended by a ":return" command.
849 */
850 int
851current_func_returned()
852{
853 return current_funccal->returned;
854}
855
856
857/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 * Set an internal variable to a string value. Creates the variable if it does
859 * not already exist.
860 */
861 void
862set_internal_string_var(name, value)
863 char_u *name;
864 char_u *value;
865{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000866 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000867 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868
869 val = vim_strsave(value);
870 if (val != NULL)
871 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000872 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000873 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000875 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000876 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 }
878 }
879}
880
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000881static lval_T *redir_lval = NULL;
882static char_u *redir_endp = NULL;
883static char_u *redir_varname = NULL;
884
885/*
886 * Start recording command output to a variable
887 * Returns OK if successfully completed the setup. FAIL otherwise.
888 */
889 int
890var_redir_start(name, append)
891 char_u *name;
892 int append; /* append to an existing variable */
893{
894 int save_emsg;
895 int err;
896 typval_T tv;
897
898 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000899 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000900 {
901 EMSG(_(e_invarg));
902 return FAIL;
903 }
904
905 redir_varname = vim_strsave(name);
906 if (redir_varname == NULL)
907 return FAIL;
908
909 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
910 if (redir_lval == NULL)
911 {
912 var_redir_stop();
913 return FAIL;
914 }
915
916 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000917 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
918 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000919 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
920 {
921 if (redir_endp != NULL && *redir_endp != NUL)
922 /* Trailing characters are present after the variable name */
923 EMSG(_(e_trailing));
924 else
925 EMSG(_(e_invarg));
926 var_redir_stop();
927 return FAIL;
928 }
929
930 /* check if we can write to the variable: set it to or append an empty
931 * string */
932 save_emsg = did_emsg;
933 did_emsg = FALSE;
934 tv.v_type = VAR_STRING;
935 tv.vval.v_string = (char_u *)"";
936 if (append)
937 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
938 else
939 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
940 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000941 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000942 if (err)
943 {
944 var_redir_stop();
945 return FAIL;
946 }
947 if (redir_lval->ll_newkey != NULL)
948 {
949 /* Dictionary item was created, don't do it again. */
950 vim_free(redir_lval->ll_newkey);
951 redir_lval->ll_newkey = NULL;
952 }
953
954 return OK;
955}
956
957/*
958 * Append "value[len]" to the variable set by var_redir_start().
959 */
960 void
961var_redir_str(value, len)
962 char_u *value;
963 int len;
964{
965 char_u *val;
966 typval_T tv;
967 int save_emsg;
968 int err;
969
970 if (redir_lval == NULL)
971 return;
972
973 if (len == -1)
974 /* Append the entire string */
975 val = vim_strsave(value);
976 else
977 /* Append only the specified number of characters */
978 val = vim_strnsave(value, len);
979 if (val == NULL)
980 return;
981
982 tv.v_type = VAR_STRING;
983 tv.vval.v_string = val;
984
985 save_emsg = did_emsg;
986 did_emsg = FALSE;
987 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
988 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000989 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000990 if (err)
991 var_redir_stop();
992
993 vim_free(tv.vval.v_string);
994}
995
996/*
997 * Stop redirecting command output to a variable.
998 */
999 void
1000var_redir_stop()
1001{
1002 if (redir_lval != NULL)
1003 {
1004 clear_lval(redir_lval);
1005 vim_free(redir_lval);
1006 redir_lval = NULL;
1007 }
1008 vim_free(redir_varname);
1009 redir_varname = NULL;
1010}
1011
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012# if defined(FEAT_MBYTE) || defined(PROTO)
1013 int
1014eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1015 char_u *enc_from;
1016 char_u *enc_to;
1017 char_u *fname_from;
1018 char_u *fname_to;
1019{
1020 int err = FALSE;
1021
1022 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1023 set_vim_var_string(VV_CC_TO, enc_to, -1);
1024 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1025 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1026 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1027 err = TRUE;
1028 set_vim_var_string(VV_CC_FROM, NULL, -1);
1029 set_vim_var_string(VV_CC_TO, NULL, -1);
1030 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1031 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1032
1033 if (err)
1034 return FAIL;
1035 return OK;
1036}
1037# endif
1038
1039# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1040 int
1041eval_printexpr(fname, args)
1042 char_u *fname;
1043 char_u *args;
1044{
1045 int err = FALSE;
1046
1047 set_vim_var_string(VV_FNAME_IN, fname, -1);
1048 set_vim_var_string(VV_CMDARG, args, -1);
1049 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1050 err = TRUE;
1051 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1052 set_vim_var_string(VV_CMDARG, NULL, -1);
1053
1054 if (err)
1055 {
1056 mch_remove(fname);
1057 return FAIL;
1058 }
1059 return OK;
1060}
1061# endif
1062
1063# if defined(FEAT_DIFF) || defined(PROTO)
1064 void
1065eval_diff(origfile, newfile, outfile)
1066 char_u *origfile;
1067 char_u *newfile;
1068 char_u *outfile;
1069{
1070 int err = FALSE;
1071
1072 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1073 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1074 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1075 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1076 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1077 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1078 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1079}
1080
1081 void
1082eval_patch(origfile, difffile, outfile)
1083 char_u *origfile;
1084 char_u *difffile;
1085 char_u *outfile;
1086{
1087 int err;
1088
1089 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1090 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1091 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1092 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1093 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1094 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1095 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1096}
1097# endif
1098
1099/*
1100 * Top level evaluation function, returning a boolean.
1101 * Sets "error" to TRUE if there was an error.
1102 * Return TRUE or FALSE.
1103 */
1104 int
1105eval_to_bool(arg, error, nextcmd, skip)
1106 char_u *arg;
1107 int *error;
1108 char_u **nextcmd;
1109 int skip; /* only parse, don't execute */
1110{
Bram Moolenaar33570922005-01-25 22:26:29 +00001111 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 int retval = FALSE;
1113
1114 if (skip)
1115 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001116 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 else
1119 {
1120 *error = FALSE;
1121 if (!skip)
1122 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001123 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001124 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 }
1126 }
1127 if (skip)
1128 --emsg_skip;
1129
1130 return retval;
1131}
1132
1133/*
1134 * Top level evaluation function, returning a string. If "skip" is TRUE,
1135 * only parsing to "nextcmd" is done, without reporting errors. Return
1136 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1137 */
1138 char_u *
1139eval_to_string_skip(arg, nextcmd, skip)
1140 char_u *arg;
1141 char_u **nextcmd;
1142 int skip; /* only parse, don't execute */
1143{
Bram Moolenaar33570922005-01-25 22:26:29 +00001144 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 char_u *retval;
1146
1147 if (skip)
1148 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001149 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 retval = NULL;
1151 else
1152 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001153 retval = vim_strsave(get_tv_string(&tv));
1154 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 }
1156 if (skip)
1157 --emsg_skip;
1158
1159 return retval;
1160}
1161
1162/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001163 * Skip over an expression at "*pp".
1164 * Return FAIL for an error, OK otherwise.
1165 */
1166 int
1167skip_expr(pp)
1168 char_u **pp;
1169{
Bram Moolenaar33570922005-01-25 22:26:29 +00001170 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001171
1172 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001173 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001174}
1175
1176/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 * Top level evaluation function, returning a string.
1178 * Return pointer to allocated memory, or NULL for failure.
1179 */
1180 char_u *
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001181eval_to_string(arg, nextcmd, dolist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 char_u *arg;
1183 char_u **nextcmd;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001184 int dolist; /* turn List into sequence of lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185{
Bram Moolenaar33570922005-01-25 22:26:29 +00001186 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001188 garray_T ga;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001190 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 retval = NULL;
1192 else
1193 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001194 if (dolist && tv.v_type == VAR_LIST)
1195 {
1196 ga_init2(&ga, (int)sizeof(char), 80);
1197 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
1198 ga_append(&ga, NUL);
1199 retval = (char_u *)ga.ga_data;
1200 }
1201 else
1202 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001203 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 }
1205
1206 return retval;
1207}
1208
1209/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001210 * Call eval_to_string() without using current local variables and using
1211 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 */
1213 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001214eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 char_u *arg;
1216 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001217 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218{
1219 char_u *retval;
1220 void *save_funccalp;
1221
1222 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001223 if (use_sandbox)
1224 ++sandbox;
1225 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001226 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001227 if (use_sandbox)
1228 --sandbox;
1229 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 restore_funccal(save_funccalp);
1231 return retval;
1232}
1233
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234/*
1235 * Top level evaluation function, returning a number.
1236 * Evaluates "expr" silently.
1237 * Returns -1 for an error.
1238 */
1239 int
1240eval_to_number(expr)
1241 char_u *expr;
1242{
Bram Moolenaar33570922005-01-25 22:26:29 +00001243 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001245 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246
1247 ++emsg_off;
1248
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001249 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 retval = -1;
1251 else
1252 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001253 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001254 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 }
1256 --emsg_off;
1257
1258 return retval;
1259}
1260
Bram Moolenaara40058a2005-07-11 22:42:07 +00001261/*
1262 * Prepare v: variable "idx" to be used.
1263 * Save the current typeval in "save_tv".
1264 * When not used yet add the variable to the v: hashtable.
1265 */
1266 static void
1267prepare_vimvar(idx, save_tv)
1268 int idx;
1269 typval_T *save_tv;
1270{
1271 *save_tv = vimvars[idx].vv_tv;
1272 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1273 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1274}
1275
1276/*
1277 * Restore v: variable "idx" to typeval "save_tv".
1278 * When no longer defined, remove the variable from the v: hashtable.
1279 */
1280 static void
1281restore_vimvar(idx, save_tv)
1282 int idx;
1283 typval_T *save_tv;
1284{
1285 hashitem_T *hi;
1286
1287 clear_tv(&vimvars[idx].vv_tv);
1288 vimvars[idx].vv_tv = *save_tv;
1289 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1290 {
1291 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1292 if (HASHITEM_EMPTY(hi))
1293 EMSG2(_(e_intern2), "restore_vimvar()");
1294 else
1295 hash_remove(&vimvarht, hi);
1296 }
1297}
1298
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001299#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001300/*
1301 * Evaluate an expression to a list with suggestions.
1302 * For the "expr:" part of 'spellsuggest'.
1303 */
1304 list_T *
1305eval_spell_expr(badword, expr)
1306 char_u *badword;
1307 char_u *expr;
1308{
1309 typval_T save_val;
1310 typval_T rettv;
1311 list_T *list = NULL;
1312 char_u *p = skipwhite(expr);
1313
1314 /* Set "v:val" to the bad word. */
1315 prepare_vimvar(VV_VAL, &save_val);
1316 vimvars[VV_VAL].vv_type = VAR_STRING;
1317 vimvars[VV_VAL].vv_str = badword;
1318 if (p_verbose == 0)
1319 ++emsg_off;
1320
1321 if (eval1(&p, &rettv, TRUE) == OK)
1322 {
1323 if (rettv.v_type != VAR_LIST)
1324 clear_tv(&rettv);
1325 else
1326 list = rettv.vval.v_list;
1327 }
1328
1329 if (p_verbose == 0)
1330 --emsg_off;
1331 vimvars[VV_VAL].vv_str = NULL;
1332 restore_vimvar(VV_VAL, &save_val);
1333
1334 return list;
1335}
1336
1337/*
1338 * "list" is supposed to contain two items: a word and a number. Return the
1339 * word in "pp" and the number as the return value.
1340 * Return -1 if anything isn't right.
1341 * Used to get the good word and score from the eval_spell_expr() result.
1342 */
1343 int
1344get_spellword(list, pp)
1345 list_T *list;
1346 char_u **pp;
1347{
1348 listitem_T *li;
1349
1350 li = list->lv_first;
1351 if (li == NULL)
1352 return -1;
1353 *pp = get_tv_string(&li->li_tv);
1354
1355 li = li->li_next;
1356 if (li == NULL)
1357 return -1;
1358 return get_tv_number(&li->li_tv);
1359}
1360#endif
1361
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001362/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001363 * Top level evaluation function.
1364 * Returns an allocated typval_T with the result.
1365 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001366 */
1367 typval_T *
1368eval_expr(arg, nextcmd)
1369 char_u *arg;
1370 char_u **nextcmd;
1371{
1372 typval_T *tv;
1373
1374 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001375 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001376 {
1377 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001378 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001379 }
1380
1381 return tv;
1382}
1383
1384
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1386/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001387 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 * Uses argv[argc] for the function arguments.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001389 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001391 static int
1392call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 char_u *func;
1394 int argc;
1395 char_u **argv;
1396 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001397 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398{
Bram Moolenaar33570922005-01-25 22:26:29 +00001399 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 long n;
1401 int len;
1402 int i;
1403 int doesrange;
1404 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001405 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406
Bram Moolenaar33570922005-01-25 22:26:29 +00001407 argvars = (typval_T *)alloc((unsigned)(argc * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001409 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410
1411 for (i = 0; i < argc; i++)
1412 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001413 /* Pass a NULL or empty argument as an empty string */
1414 if (argv[i] == NULL || *argv[i] == NUL)
1415 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001416 argvars[i].v_type = VAR_STRING;
1417 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001418 continue;
1419 }
1420
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 /* Recognize a number argument, the others must be strings. */
1422 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1423 if (len != 0 && len == (int)STRLEN(argv[i]))
1424 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001425 argvars[i].v_type = VAR_NUMBER;
1426 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 }
1428 else
1429 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001430 argvars[i].v_type = VAR_STRING;
1431 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 }
1433 }
1434
1435 if (safe)
1436 {
1437 save_funccalp = save_funccal();
1438 ++sandbox;
1439 }
1440
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001441 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1442 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001444 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 if (safe)
1446 {
1447 --sandbox;
1448 restore_funccal(save_funccalp);
1449 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001450 vim_free(argvars);
1451
1452 if (ret == FAIL)
1453 clear_tv(rettv);
1454
1455 return ret;
1456}
1457
1458/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001459 * Call vimL function "func" and return the result as a string.
1460 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001461 * Uses argv[argc] for the function arguments.
1462 */
1463 void *
1464call_func_retstr(func, argc, argv, safe)
1465 char_u *func;
1466 int argc;
1467 char_u **argv;
1468 int safe; /* use the sandbox */
1469{
1470 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001471 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001472
1473 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1474 return NULL;
1475
1476 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001477 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 return retval;
1479}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001480
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001481#if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001482/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001483 * Call vimL function "func" and return the result as a number.
1484 * Returns -1 when calling the function fails.
1485 * Uses argv[argc] for the function arguments.
1486 */
1487 long
1488call_func_retnr(func, argc, argv, safe)
1489 char_u *func;
1490 int argc;
1491 char_u **argv;
1492 int safe; /* use the sandbox */
1493{
1494 typval_T rettv;
1495 long retval;
1496
1497 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1498 return -1;
1499
1500 retval = get_tv_number_chk(&rettv, NULL);
1501 clear_tv(&rettv);
1502 return retval;
1503}
1504#endif
1505
1506/*
1507 * Call vimL function "func" and return the result as a list
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001508 * Uses argv[argc] for the function arguments.
1509 */
1510 void *
1511call_func_retlist(func, argc, argv, safe)
1512 char_u *func;
1513 int argc;
1514 char_u **argv;
1515 int safe; /* use the sandbox */
1516{
1517 typval_T rettv;
1518
1519 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1520 return NULL;
1521
1522 if (rettv.v_type != VAR_LIST)
1523 {
1524 clear_tv(&rettv);
1525 return NULL;
1526 }
1527
1528 return rettv.vval.v_list;
1529}
1530
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531#endif
1532
1533/*
1534 * Save the current function call pointer, and set it to NULL.
1535 * Used when executing autocommands and for ":source".
1536 */
1537 void *
1538save_funccal()
1539{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001540 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 current_funccal = NULL;
1543 return (void *)fc;
1544}
1545
1546 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001547restore_funccal(vfc)
1548 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001550 funccall_T *fc = (funccall_T *)vfc;
1551
1552 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553}
1554
Bram Moolenaar05159a02005-02-26 23:04:13 +00001555#if defined(FEAT_PROFILE) || defined(PROTO)
1556/*
1557 * Prepare profiling for entering a child or something else that is not
1558 * counted for the script/function itself.
1559 * Should always be called in pair with prof_child_exit().
1560 */
1561 void
1562prof_child_enter(tm)
1563 proftime_T *tm; /* place to store waittime */
1564{
1565 funccall_T *fc = current_funccal;
1566
1567 if (fc != NULL && fc->func->uf_profiling)
1568 profile_start(&fc->prof_child);
1569 script_prof_save(tm);
1570}
1571
1572/*
1573 * Take care of time spent in a child.
1574 * Should always be called after prof_child_enter().
1575 */
1576 void
1577prof_child_exit(tm)
1578 proftime_T *tm; /* where waittime was stored */
1579{
1580 funccall_T *fc = current_funccal;
1581
1582 if (fc != NULL && fc->func->uf_profiling)
1583 {
1584 profile_end(&fc->prof_child);
1585 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1586 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1587 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1588 }
1589 script_prof_restore(tm);
1590}
1591#endif
1592
1593
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594#ifdef FEAT_FOLDING
1595/*
1596 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1597 * it in "*cp". Doesn't give error messages.
1598 */
1599 int
1600eval_foldexpr(arg, cp)
1601 char_u *arg;
1602 int *cp;
1603{
Bram Moolenaar33570922005-01-25 22:26:29 +00001604 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 int retval;
1606 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001607 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1608 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609
1610 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001611 if (use_sandbox)
1612 ++sandbox;
1613 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001615 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 retval = 0;
1617 else
1618 {
1619 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001620 if (tv.v_type == VAR_NUMBER)
1621 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001622 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 retval = 0;
1624 else
1625 {
1626 /* If the result is a string, check if there is a non-digit before
1627 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001628 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 if (!VIM_ISDIGIT(*s) && *s != '-')
1630 *cp = *s++;
1631 retval = atol((char *)s);
1632 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001633 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 }
1635 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001636 if (use_sandbox)
1637 --sandbox;
1638 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639
1640 return retval;
1641}
1642#endif
1643
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001645 * ":let" list all variable values
1646 * ":let var1 var2" list variable values
1647 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001648 * ":let var += expr" assignment command.
1649 * ":let var -= expr" assignment command.
1650 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001651 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 */
1653 void
1654ex_let(eap)
1655 exarg_T *eap;
1656{
1657 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001658 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001659 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001661 int var_count = 0;
1662 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001663 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001664 char_u *argend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665
Bram Moolenaardb552d602006-03-23 22:59:57 +00001666 argend = skip_var_list(arg, &var_count, &semicolon);
1667 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001668 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001669 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1670 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001671 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001672 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001674 /*
1675 * ":let" without "=": list variables
1676 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001677 if (*arg == '[')
1678 EMSG(_(e_invarg));
1679 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001680 /* ":let var1 var2" */
1681 arg = list_arg_vars(eap, arg);
1682 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001683 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001684 /* ":let" */
Bram Moolenaara7043832005-01-21 11:56:39 +00001685 list_glob_vars();
1686 list_buf_vars();
1687 list_win_vars();
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001688 list_script_vars();
1689 list_func_vars();
Bram Moolenaara7043832005-01-21 11:56:39 +00001690 list_vim_vars();
1691 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 eap->nextcmd = check_nextcmd(arg);
1693 }
1694 else
1695 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001696 op[0] = '=';
1697 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001698 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001699 {
1700 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1701 op[0] = expr[-1]; /* +=, -= or .= */
1702 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001703 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001704
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 if (eap->skip)
1706 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001707 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 if (eap->skip)
1709 {
1710 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001711 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 --emsg_skip;
1713 }
1714 else if (i != FAIL)
1715 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001716 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001717 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001718 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 }
1720 }
1721}
1722
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001723/*
1724 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1725 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001726 * When "nextchars" is not NULL it points to a string with characters that
1727 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1728 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001729 * Returns OK or FAIL;
1730 */
1731 static int
1732ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1733 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001734 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001735 int copy; /* copy values from "tv", don't move */
1736 int semicolon; /* from skip_var_list() */
1737 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001738 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001739{
1740 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001741 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001742 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001743 listitem_T *item;
1744 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001745
1746 if (*arg != '[')
1747 {
1748 /*
1749 * ":let var = expr" or ":for var in list"
1750 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001751 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001752 return FAIL;
1753 return OK;
1754 }
1755
1756 /*
1757 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1758 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001759 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001760 {
1761 EMSG(_(e_listreq));
1762 return FAIL;
1763 }
1764
1765 i = list_len(l);
1766 if (semicolon == 0 && var_count < i)
1767 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001768 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001769 return FAIL;
1770 }
1771 if (var_count - semicolon > i)
1772 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001773 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001774 return FAIL;
1775 }
1776
1777 item = l->lv_first;
1778 while (*arg != ']')
1779 {
1780 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001781 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001782 item = item->li_next;
1783 if (arg == NULL)
1784 return FAIL;
1785
1786 arg = skipwhite(arg);
1787 if (*arg == ';')
1788 {
1789 /* Put the rest of the list (may be empty) in the var after ';'.
1790 * Create a new list for this. */
1791 l = list_alloc();
1792 if (l == NULL)
1793 return FAIL;
1794 while (item != NULL)
1795 {
1796 list_append_tv(l, &item->li_tv);
1797 item = item->li_next;
1798 }
1799
1800 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001801 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001802 ltv.vval.v_list = l;
1803 l->lv_refcount = 1;
1804
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001805 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1806 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001807 clear_tv(&ltv);
1808 if (arg == NULL)
1809 return FAIL;
1810 break;
1811 }
1812 else if (*arg != ',' && *arg != ']')
1813 {
1814 EMSG2(_(e_intern2), "ex_let_vars()");
1815 return FAIL;
1816 }
1817 }
1818
1819 return OK;
1820}
1821
1822/*
1823 * Skip over assignable variable "var" or list of variables "[var, var]".
1824 * Used for ":let varvar = expr" and ":for varvar in expr".
1825 * For "[var, var]" increment "*var_count" for each variable.
1826 * for "[var, var; var]" set "semicolon".
1827 * Return NULL for an error.
1828 */
1829 static char_u *
1830skip_var_list(arg, var_count, semicolon)
1831 char_u *arg;
1832 int *var_count;
1833 int *semicolon;
1834{
1835 char_u *p, *s;
1836
1837 if (*arg == '[')
1838 {
1839 /* "[var, var]": find the matching ']'. */
1840 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001841 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001842 {
1843 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1844 s = skip_var_one(p);
1845 if (s == p)
1846 {
1847 EMSG2(_(e_invarg2), p);
1848 return NULL;
1849 }
1850 ++*var_count;
1851
1852 p = skipwhite(s);
1853 if (*p == ']')
1854 break;
1855 else if (*p == ';')
1856 {
1857 if (*semicolon == 1)
1858 {
1859 EMSG(_("Double ; in list of variables"));
1860 return NULL;
1861 }
1862 *semicolon = 1;
1863 }
1864 else if (*p != ',')
1865 {
1866 EMSG2(_(e_invarg2), p);
1867 return NULL;
1868 }
1869 }
1870 return p + 1;
1871 }
1872 else
1873 return skip_var_one(arg);
1874}
1875
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001876/*
Bram Moolenaar92124a32005-06-17 22:03:40 +00001877 * Skip one (assignable) variable name, includig @r, $VAR, &option, d.key,
1878 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001879 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001880 static char_u *
1881skip_var_one(arg)
1882 char_u *arg;
1883{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001884 if (*arg == '@' && arg[1] != NUL)
1885 return arg + 2;
1886 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1887 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001888}
1889
Bram Moolenaara7043832005-01-21 11:56:39 +00001890/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001891 * List variables for hashtab "ht" with prefix "prefix".
1892 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001893 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001894 static void
Bram Moolenaar33570922005-01-25 22:26:29 +00001895list_hashtable_vars(ht, prefix, empty)
1896 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001897 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001898 int empty;
Bram Moolenaara7043832005-01-21 11:56:39 +00001899{
Bram Moolenaar33570922005-01-25 22:26:29 +00001900 hashitem_T *hi;
1901 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001902 int todo;
1903
1904 todo = ht->ht_used;
1905 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1906 {
1907 if (!HASHITEM_EMPTY(hi))
1908 {
1909 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001910 di = HI2DI(hi);
1911 if (empty || di->di_tv.v_type != VAR_STRING
1912 || di->di_tv.vval.v_string != NULL)
1913 list_one_var(di, prefix);
Bram Moolenaara7043832005-01-21 11:56:39 +00001914 }
1915 }
1916}
1917
1918/*
1919 * List global variables.
1920 */
1921 static void
1922list_glob_vars()
1923{
Bram Moolenaar33570922005-01-25 22:26:29 +00001924 list_hashtable_vars(&globvarht, (char_u *)"", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001925}
1926
1927/*
1928 * List buffer variables.
1929 */
1930 static void
1931list_buf_vars()
1932{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001933 char_u numbuf[NUMBUFLEN];
1934
Bram Moolenaar33570922005-01-25 22:26:29 +00001935 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001936
1937 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
1938 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER, numbuf);
Bram Moolenaara7043832005-01-21 11:56:39 +00001939}
1940
1941/*
1942 * List window variables.
1943 */
1944 static void
1945list_win_vars()
1946{
Bram Moolenaar33570922005-01-25 22:26:29 +00001947 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001948}
1949
1950/*
1951 * List Vim variables.
1952 */
1953 static void
1954list_vim_vars()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001955{
Bram Moolenaar33570922005-01-25 22:26:29 +00001956 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001957}
1958
1959/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001960 * List script-local variables, if there is a script.
1961 */
1962 static void
1963list_script_vars()
1964{
1965 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
1966 list_hashtable_vars(&SCRIPT_VARS(current_SID), (char_u *)"s:", FALSE);
1967}
1968
1969/*
1970 * List function variables, if there is a function.
1971 */
1972 static void
1973list_func_vars()
1974{
1975 if (current_funccal != NULL)
1976 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
1977 (char_u *)"l:", FALSE);
1978}
1979
1980/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001981 * List variables in "arg".
1982 */
1983 static char_u *
1984list_arg_vars(eap, arg)
1985 exarg_T *eap;
1986 char_u *arg;
1987{
1988 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001989 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001990 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001991 char_u *name_start;
1992 char_u *arg_subsc;
1993 char_u *tofree;
1994 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001995
1996 while (!ends_excmd(*arg) && !got_int)
1997 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001998 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001999 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002000 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002001 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2002 {
2003 emsg_severe = TRUE;
2004 EMSG(_(e_trailing));
2005 break;
2006 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002007 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002008 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002009 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002010 /* get_name_len() takes care of expanding curly braces */
2011 name_start = name = arg;
2012 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2013 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002014 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002015 /* This is mainly to keep test 49 working: when expanding
2016 * curly braces fails overrule the exception error message. */
2017 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002018 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002019 emsg_severe = TRUE;
2020 EMSG2(_(e_invarg2), arg);
2021 break;
2022 }
2023 error = TRUE;
2024 }
2025 else
2026 {
2027 if (tofree != NULL)
2028 name = tofree;
2029 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002030 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002031 else
2032 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002033 /* handle d.key, l[idx], f(expr) */
2034 arg_subsc = arg;
2035 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002036 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002037 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002038 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002039 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002040 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002041 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002042 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002043 case 'g': list_glob_vars(); break;
2044 case 'b': list_buf_vars(); break;
2045 case 'w': list_win_vars(); break;
2046 case 'v': list_vim_vars(); break;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002047 case 's': list_script_vars(); break;
2048 case 'l': list_func_vars(); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002049 default:
2050 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002051 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002052 }
2053 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002054 {
2055 char_u numbuf[NUMBUFLEN];
2056 char_u *tf;
2057 int c;
2058 char_u *s;
2059
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002060 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002061 c = *arg;
2062 *arg = NUL;
2063 list_one_var_a((char_u *)"",
2064 arg == arg_subsc ? name : name_start,
2065 tv.v_type, s == NULL ? (char_u *)"" : s);
2066 *arg = c;
2067 vim_free(tf);
2068 }
2069 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002070 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002071 }
2072 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002073
2074 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002075 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002076
2077 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002078 }
2079
2080 return arg;
2081}
2082
2083/*
2084 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2085 * Returns a pointer to the char just after the var name.
2086 * Returns NULL if there is an error.
2087 */
2088 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002089ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002090 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002091 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002092 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002093 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002094 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002095{
2096 int c1;
2097 char_u *name;
2098 char_u *p;
2099 char_u *arg_end = NULL;
2100 int len;
2101 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002102 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002103
2104 /*
2105 * ":let $VAR = expr": Set environment variable.
2106 */
2107 if (*arg == '$')
2108 {
2109 /* Find the end of the name. */
2110 ++arg;
2111 name = arg;
2112 len = get_env_len(&arg);
2113 if (len == 0)
2114 EMSG2(_(e_invarg2), name - 1);
2115 else
2116 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002117 if (op != NULL && (*op == '+' || *op == '-'))
2118 EMSG2(_(e_letwrong), op);
2119 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002120 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002121 EMSG(_(e_letunexp));
2122 else
2123 {
2124 c1 = name[len];
2125 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002126 p = get_tv_string_chk(tv);
2127 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002128 {
2129 int mustfree = FALSE;
2130 char_u *s = vim_getenv(name, &mustfree);
2131
2132 if (s != NULL)
2133 {
2134 p = tofree = concat_str(s, p);
2135 if (mustfree)
2136 vim_free(s);
2137 }
2138 }
2139 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002140 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002141 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002142 if (STRICMP(name, "HOME") == 0)
2143 init_homedir();
2144 else if (didset_vim && STRICMP(name, "VIM") == 0)
2145 didset_vim = FALSE;
2146 else if (didset_vimruntime
2147 && STRICMP(name, "VIMRUNTIME") == 0)
2148 didset_vimruntime = FALSE;
2149 arg_end = arg;
2150 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002151 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002152 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002153 }
2154 }
2155 }
2156
2157 /*
2158 * ":let &option = expr": Set option value.
2159 * ":let &l:option = expr": Set local option value.
2160 * ":let &g:option = expr": Set global option value.
2161 */
2162 else if (*arg == '&')
2163 {
2164 /* Find the end of the name. */
2165 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002166 if (p == NULL || (endchars != NULL
2167 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002168 EMSG(_(e_letunexp));
2169 else
2170 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002171 long n;
2172 int opt_type;
2173 long numval;
2174 char_u *stringval = NULL;
2175 char_u *s;
2176
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002177 c1 = *p;
2178 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002179
2180 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002181 s = get_tv_string_chk(tv); /* != NULL if number or string */
2182 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002183 {
2184 opt_type = get_option_value(arg, &numval,
2185 &stringval, opt_flags);
2186 if ((opt_type == 1 && *op == '.')
2187 || (opt_type == 0 && *op != '.'))
2188 EMSG2(_(e_letwrong), op);
2189 else
2190 {
2191 if (opt_type == 1) /* number */
2192 {
2193 if (*op == '+')
2194 n = numval + n;
2195 else
2196 n = numval - n;
2197 }
2198 else if (opt_type == 0 && stringval != NULL) /* string */
2199 {
2200 s = concat_str(stringval, s);
2201 vim_free(stringval);
2202 stringval = s;
2203 }
2204 }
2205 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002206 if (s != NULL)
2207 {
2208 set_option_value(arg, n, s, opt_flags);
2209 arg_end = p;
2210 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002211 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002212 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002213 }
2214 }
2215
2216 /*
2217 * ":let @r = expr": Set register contents.
2218 */
2219 else if (*arg == '@')
2220 {
2221 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002222 if (op != NULL && (*op == '+' || *op == '-'))
2223 EMSG2(_(e_letwrong), op);
2224 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002225 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002226 EMSG(_(e_letunexp));
2227 else
2228 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002229 char_u *tofree = NULL;
2230 char_u *s;
2231
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002232 p = get_tv_string_chk(tv);
2233 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002234 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002235 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002236 if (s != NULL)
2237 {
2238 p = tofree = concat_str(s, p);
2239 vim_free(s);
2240 }
2241 }
2242 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002243 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002244 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002245 arg_end = arg + 1;
2246 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002247 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002248 }
2249 }
2250
2251 /*
2252 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002253 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002254 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002255 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002256 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002257 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002258
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002259 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002260 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002261 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002262 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2263 EMSG(_(e_letunexp));
2264 else
2265 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002266 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002267 arg_end = p;
2268 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002269 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002270 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002271 }
2272
2273 else
2274 EMSG2(_(e_invarg2), arg);
2275
2276 return arg_end;
2277}
2278
2279/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002280 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2281 */
2282 static int
2283check_changedtick(arg)
2284 char_u *arg;
2285{
2286 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2287 {
2288 EMSG2(_(e_readonlyvar), arg);
2289 return TRUE;
2290 }
2291 return FALSE;
2292}
2293
2294/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002295 * Get an lval: variable, Dict item or List item that can be assigned a value
2296 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2297 * "name.key", "name.key[expr]" etc.
2298 * Indexing only works if "name" is an existing List or Dictionary.
2299 * "name" points to the start of the name.
2300 * If "rettv" is not NULL it points to the value to be assigned.
2301 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2302 * wrong; must end in space or cmd separator.
2303 *
2304 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002305 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002306 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002307 */
2308 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002309get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002310 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002311 typval_T *rettv;
2312 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002313 int unlet;
2314 int skip;
2315 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002316 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002317{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002318 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002319 char_u *expr_start, *expr_end;
2320 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002321 dictitem_T *v;
2322 typval_T var1;
2323 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002324 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002325 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002326 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002327 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002328 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002329
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002330 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002331 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002332
2333 if (skip)
2334 {
2335 /* When skipping just find the end of the name. */
2336 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002337 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002338 }
2339
2340 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002341 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002342 if (expr_start != NULL)
2343 {
2344 /* Don't expand the name when we already know there is an error. */
2345 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2346 && *p != '[' && *p != '.')
2347 {
2348 EMSG(_(e_trailing));
2349 return NULL;
2350 }
2351
2352 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2353 if (lp->ll_exp_name == NULL)
2354 {
2355 /* Report an invalid expression in braces, unless the
2356 * expression evaluation has been cancelled due to an
2357 * aborting error, an interrupt, or an exception. */
2358 if (!aborting() && !quiet)
2359 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002360 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002361 EMSG2(_(e_invarg2), name);
2362 return NULL;
2363 }
2364 }
2365 lp->ll_name = lp->ll_exp_name;
2366 }
2367 else
2368 lp->ll_name = name;
2369
2370 /* Without [idx] or .key we are done. */
2371 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2372 return p;
2373
2374 cc = *p;
2375 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002376 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002377 if (v == NULL && !quiet)
2378 EMSG2(_(e_undefvar), lp->ll_name);
2379 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002380 if (v == NULL)
2381 return NULL;
2382
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002383 /*
2384 * Loop until no more [idx] or .key is following.
2385 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002386 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002387 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002388 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002389 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2390 && !(lp->ll_tv->v_type == VAR_DICT
2391 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002392 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002393 if (!quiet)
2394 EMSG(_("E689: Can only index a List or Dictionary"));
2395 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002396 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002397 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002398 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002399 if (!quiet)
2400 EMSG(_("E708: [:] must come last"));
2401 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002402 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002403
Bram Moolenaar8c711452005-01-14 21:53:12 +00002404 len = -1;
2405 if (*p == '.')
2406 {
2407 key = p + 1;
2408 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2409 ;
2410 if (len == 0)
2411 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002412 if (!quiet)
2413 EMSG(_(e_emptykey));
2414 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002415 }
2416 p = key + len;
2417 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002418 else
2419 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002420 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002421 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002422 if (*p == ':')
2423 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002424 else
2425 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002426 empty1 = FALSE;
2427 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002428 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002429 if (get_tv_string_chk(&var1) == NULL)
2430 {
2431 /* not a number or string */
2432 clear_tv(&var1);
2433 return NULL;
2434 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002435 }
2436
2437 /* Optionally get the second index [ :expr]. */
2438 if (*p == ':')
2439 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002440 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002441 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002442 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002443 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002444 if (!empty1)
2445 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002446 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002447 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002448 if (rettv != NULL && (rettv->v_type != VAR_LIST
2449 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002450 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002451 if (!quiet)
2452 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002453 if (!empty1)
2454 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002455 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002456 }
2457 p = skipwhite(p + 1);
2458 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002459 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002460 else
2461 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002462 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002463 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2464 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002465 if (!empty1)
2466 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002467 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002468 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002469 if (get_tv_string_chk(&var2) == NULL)
2470 {
2471 /* not a number or string */
2472 if (!empty1)
2473 clear_tv(&var1);
2474 clear_tv(&var2);
2475 return NULL;
2476 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002477 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002478 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002479 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002480 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002481 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002482
Bram Moolenaar8c711452005-01-14 21:53:12 +00002483 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002484 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002485 if (!quiet)
2486 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002487 if (!empty1)
2488 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002489 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002490 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002491 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002492 }
2493
2494 /* Skip to past ']'. */
2495 ++p;
2496 }
2497
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002498 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002499 {
2500 if (len == -1)
2501 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002502 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002503 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002504 if (*key == NUL)
2505 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002506 if (!quiet)
2507 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002508 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002509 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002510 }
2511 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002512 lp->ll_list = NULL;
2513 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002514 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002515 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002516 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002517 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002518 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002519 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002520 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002521 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002522 if (len == -1)
2523 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002524 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002525 }
2526 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002527 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002528 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002529 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002530 if (len == -1)
2531 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002532 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002533 p = NULL;
2534 break;
2535 }
2536 if (len == -1)
2537 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002538 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002539 }
2540 else
2541 {
2542 /*
2543 * Get the number and item for the only or first index of the List.
2544 */
2545 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002546 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002547 else
2548 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002549 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002550 clear_tv(&var1);
2551 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002552 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002553 lp->ll_list = lp->ll_tv->vval.v_list;
2554 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2555 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002556 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002557 if (!quiet)
2558 EMSGN(_(e_listidx), lp->ll_n1);
2559 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002560 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002561 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002562 }
2563
2564 /*
2565 * May need to find the item or absolute index for the second
2566 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002567 * When no index given: "lp->ll_empty2" is TRUE.
2568 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002569 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002570 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002571 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002572 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002573 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002574 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002575 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002576 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002577 if (ni == NULL)
2578 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002579 if (!quiet)
2580 EMSGN(_(e_listidx), lp->ll_n2);
2581 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002582 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002583 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002584 }
2585
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002586 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2587 if (lp->ll_n1 < 0)
2588 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2589 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002590 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002591 if (!quiet)
2592 EMSGN(_(e_listidx), lp->ll_n2);
2593 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002594 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002595 }
2596
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002597 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002598 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002599 }
2600
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002601 return p;
2602}
2603
2604/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002605 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002606 */
2607 static void
2608clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002609 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002610{
2611 vim_free(lp->ll_exp_name);
2612 vim_free(lp->ll_newkey);
2613}
2614
2615/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002616 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002617 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002618 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002619 */
2620 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002621set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002622 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002623 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002624 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002625 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002626 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002627{
2628 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002629 listitem_T *ri;
2630 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002631
2632 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002633 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002634 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002635 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002636 cc = *endp;
2637 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002638 if (op != NULL && *op != '=')
2639 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002640 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002641
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002642 /* handle +=, -= and .= */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002643 if (get_var_tv(lp->ll_name, STRLEN(lp->ll_name),
2644 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002645 {
2646 if (tv_op(&tv, rettv, op) == OK)
2647 set_var(lp->ll_name, &tv, FALSE);
2648 clear_tv(&tv);
2649 }
2650 }
2651 else
2652 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002654 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002655 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002656 else if (tv_check_lock(lp->ll_newkey == NULL
2657 ? lp->ll_tv->v_lock
2658 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2659 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002660 else if (lp->ll_range)
2661 {
2662 /*
2663 * Assign the List values to the list items.
2664 */
2665 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002666 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002667 if (op != NULL && *op != '=')
2668 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2669 else
2670 {
2671 clear_tv(&lp->ll_li->li_tv);
2672 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2673 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002674 ri = ri->li_next;
2675 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2676 break;
2677 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002678 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002679 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002680 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002681 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002682 ri = NULL;
2683 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002684 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002685 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002686 lp->ll_li = lp->ll_li->li_next;
2687 ++lp->ll_n1;
2688 }
2689 if (ri != NULL)
2690 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002691 else if (lp->ll_empty2
2692 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002693 : lp->ll_n1 != lp->ll_n2)
2694 EMSG(_("E711: List value has not enough items"));
2695 }
2696 else
2697 {
2698 /*
2699 * Assign to a List or Dictionary item.
2700 */
2701 if (lp->ll_newkey != NULL)
2702 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002703 if (op != NULL && *op != '=')
2704 {
2705 EMSG2(_(e_letwrong), op);
2706 return;
2707 }
2708
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002709 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002710 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002711 if (di == NULL)
2712 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002713 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2714 {
2715 vim_free(di);
2716 return;
2717 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002718 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002719 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002720 else if (op != NULL && *op != '=')
2721 {
2722 tv_op(lp->ll_tv, rettv, op);
2723 return;
2724 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002725 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002726 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002727
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002728 /*
2729 * Assign the value to the variable or list item.
2730 */
2731 if (copy)
2732 copy_tv(rettv, lp->ll_tv);
2733 else
2734 {
2735 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002736 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002737 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002738 }
2739 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002740}
2741
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002742/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002743 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2744 * Returns OK or FAIL.
2745 */
2746 static int
2747tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002748 typval_T *tv1;
2749 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002750 char_u *op;
2751{
2752 long n;
2753 char_u numbuf[NUMBUFLEN];
2754 char_u *s;
2755
2756 /* Can't do anything with a Funcref or a Dict on the right. */
2757 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2758 {
2759 switch (tv1->v_type)
2760 {
2761 case VAR_DICT:
2762 case VAR_FUNC:
2763 break;
2764
2765 case VAR_LIST:
2766 if (*op != '+' || tv2->v_type != VAR_LIST)
2767 break;
2768 /* List += List */
2769 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2770 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2771 return OK;
2772
2773 case VAR_NUMBER:
2774 case VAR_STRING:
2775 if (tv2->v_type == VAR_LIST)
2776 break;
2777 if (*op == '+' || *op == '-')
2778 {
2779 /* nr += nr or nr -= nr*/
2780 n = get_tv_number(tv1);
2781 if (*op == '+')
2782 n += get_tv_number(tv2);
2783 else
2784 n -= get_tv_number(tv2);
2785 clear_tv(tv1);
2786 tv1->v_type = VAR_NUMBER;
2787 tv1->vval.v_number = n;
2788 }
2789 else
2790 {
2791 /* str .= str */
2792 s = get_tv_string(tv1);
2793 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2794 clear_tv(tv1);
2795 tv1->v_type = VAR_STRING;
2796 tv1->vval.v_string = s;
2797 }
2798 return OK;
2799 }
2800 }
2801
2802 EMSG2(_(e_letwrong), op);
2803 return FAIL;
2804}
2805
2806/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002807 * Add a watcher to a list.
2808 */
2809 static void
2810list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002811 list_T *l;
2812 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002813{
2814 lw->lw_next = l->lv_watch;
2815 l->lv_watch = lw;
2816}
2817
2818/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002819 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002820 * No warning when it isn't found...
2821 */
2822 static void
2823list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002824 list_T *l;
2825 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002826{
Bram Moolenaar33570922005-01-25 22:26:29 +00002827 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002828
2829 lwp = &l->lv_watch;
2830 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2831 {
2832 if (lw == lwrem)
2833 {
2834 *lwp = lw->lw_next;
2835 break;
2836 }
2837 lwp = &lw->lw_next;
2838 }
2839}
2840
2841/*
2842 * Just before removing an item from a list: advance watchers to the next
2843 * item.
2844 */
2845 static void
2846list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002847 list_T *l;
2848 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002849{
Bram Moolenaar33570922005-01-25 22:26:29 +00002850 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002851
2852 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2853 if (lw->lw_item == item)
2854 lw->lw_item = item->li_next;
2855}
2856
2857/*
2858 * Evaluate the expression used in a ":for var in expr" command.
2859 * "arg" points to "var".
2860 * Set "*errp" to TRUE for an error, FALSE otherwise;
2861 * Return a pointer that holds the info. Null when there is an error.
2862 */
2863 void *
2864eval_for_line(arg, errp, nextcmdp, skip)
2865 char_u *arg;
2866 int *errp;
2867 char_u **nextcmdp;
2868 int skip;
2869{
Bram Moolenaar33570922005-01-25 22:26:29 +00002870 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002871 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002872 typval_T tv;
2873 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002874
2875 *errp = TRUE; /* default: there is an error */
2876
Bram Moolenaar33570922005-01-25 22:26:29 +00002877 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002878 if (fi == NULL)
2879 return NULL;
2880
2881 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2882 if (expr == NULL)
2883 return fi;
2884
2885 expr = skipwhite(expr);
2886 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2887 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002888 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002889 return fi;
2890 }
2891
2892 if (skip)
2893 ++emsg_skip;
2894 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2895 {
2896 *errp = FALSE;
2897 if (!skip)
2898 {
2899 l = tv.vval.v_list;
2900 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002901 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002902 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002903 clear_tv(&tv);
2904 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002905 else
2906 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002907 /* No need to increment the refcount, it's already set for the
2908 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002909 fi->fi_list = l;
2910 list_add_watch(l, &fi->fi_lw);
2911 fi->fi_lw.lw_item = l->lv_first;
2912 }
2913 }
2914 }
2915 if (skip)
2916 --emsg_skip;
2917
2918 return fi;
2919}
2920
2921/*
2922 * Use the first item in a ":for" list. Advance to the next.
2923 * Assign the values to the variable (list). "arg" points to the first one.
2924 * Return TRUE when a valid item was found, FALSE when at end of list or
2925 * something wrong.
2926 */
2927 int
2928next_for_item(fi_void, arg)
2929 void *fi_void;
2930 char_u *arg;
2931{
Bram Moolenaar33570922005-01-25 22:26:29 +00002932 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002933 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002934 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002935
2936 item = fi->fi_lw.lw_item;
2937 if (item == NULL)
2938 result = FALSE;
2939 else
2940 {
2941 fi->fi_lw.lw_item = item->li_next;
2942 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2943 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2944 }
2945 return result;
2946}
2947
2948/*
2949 * Free the structure used to store info used by ":for".
2950 */
2951 void
2952free_for_info(fi_void)
2953 void *fi_void;
2954{
Bram Moolenaar33570922005-01-25 22:26:29 +00002955 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002956
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002957 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002958 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002959 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002960 list_unref(fi->fi_list);
2961 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002962 vim_free(fi);
2963}
2964
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2966
2967 void
2968set_context_for_expression(xp, arg, cmdidx)
2969 expand_T *xp;
2970 char_u *arg;
2971 cmdidx_T cmdidx;
2972{
2973 int got_eq = FALSE;
2974 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002975 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002977 if (cmdidx == CMD_let)
2978 {
2979 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002980 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002981 {
2982 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002983 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002984 {
2985 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00002986 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002987 if (vim_iswhite(*p))
2988 break;
2989 }
2990 return;
2991 }
2992 }
2993 else
2994 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2995 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 while ((xp->xp_pattern = vim_strpbrk(arg,
2997 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2998 {
2999 c = *xp->xp_pattern;
3000 if (c == '&')
3001 {
3002 c = xp->xp_pattern[1];
3003 if (c == '&')
3004 {
3005 ++xp->xp_pattern;
3006 xp->xp_context = cmdidx != CMD_let || got_eq
3007 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3008 }
3009 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003010 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003012 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3013 xp->xp_pattern += 2;
3014
3015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 }
3017 else if (c == '$')
3018 {
3019 /* environment variable */
3020 xp->xp_context = EXPAND_ENV_VARS;
3021 }
3022 else if (c == '=')
3023 {
3024 got_eq = TRUE;
3025 xp->xp_context = EXPAND_EXPRESSION;
3026 }
3027 else if (c == '<'
3028 && xp->xp_context == EXPAND_FUNCTIONS
3029 && vim_strchr(xp->xp_pattern, '(') == NULL)
3030 {
3031 /* Function name can start with "<SNR>" */
3032 break;
3033 }
3034 else if (cmdidx != CMD_let || got_eq)
3035 {
3036 if (c == '"') /* string */
3037 {
3038 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3039 if (c == '\\' && xp->xp_pattern[1] != NUL)
3040 ++xp->xp_pattern;
3041 xp->xp_context = EXPAND_NOTHING;
3042 }
3043 else if (c == '\'') /* literal string */
3044 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003045 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3047 /* skip */ ;
3048 xp->xp_context = EXPAND_NOTHING;
3049 }
3050 else if (c == '|')
3051 {
3052 if (xp->xp_pattern[1] == '|')
3053 {
3054 ++xp->xp_pattern;
3055 xp->xp_context = EXPAND_EXPRESSION;
3056 }
3057 else
3058 xp->xp_context = EXPAND_COMMANDS;
3059 }
3060 else
3061 xp->xp_context = EXPAND_EXPRESSION;
3062 }
3063 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003064 /* Doesn't look like something valid, expand as an expression
3065 * anyway. */
3066 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 arg = xp->xp_pattern;
3068 if (*arg != NUL)
3069 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3070 /* skip */ ;
3071 }
3072 xp->xp_pattern = arg;
3073}
3074
3075#endif /* FEAT_CMDL_COMPL */
3076
3077/*
3078 * ":1,25call func(arg1, arg2)" function call.
3079 */
3080 void
3081ex_call(eap)
3082 exarg_T *eap;
3083{
3084 char_u *arg = eap->arg;
3085 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003087 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003089 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 linenr_T lnum;
3091 int doesrange;
3092 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003093 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003095 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
3096 vim_free(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003097 if (tofree == NULL)
3098 return;
3099
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003100 /* Increase refcount on dictionary, it could get deleted when evaluating
3101 * the arguments. */
3102 if (fudi.fd_dict != NULL)
3103 ++fudi.fd_dict->dv_refcount;
3104
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003105 /* If it is the name of a variable of type VAR_FUNC use its contents. */
3106 len = STRLEN(tofree);
3107 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108
Bram Moolenaar532c7802005-01-27 14:44:31 +00003109 /* Skip white space to allow ":call func ()". Not good, but required for
3110 * backward compatibility. */
3111 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003112 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113
3114 if (*startarg != '(')
3115 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003116 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 goto end;
3118 }
3119
3120 /*
3121 * When skipping, evaluate the function once, to find the end of the
3122 * arguments.
3123 * When the function takes a range, this is discovered after the first
3124 * call, and the loop is broken.
3125 */
3126 if (eap->skip)
3127 {
3128 ++emsg_skip;
3129 lnum = eap->line2; /* do it once, also with an invalid range */
3130 }
3131 else
3132 lnum = eap->line1;
3133 for ( ; lnum <= eap->line2; ++lnum)
3134 {
3135 if (!eap->skip && eap->addr_count > 0)
3136 {
3137 curwin->w_cursor.lnum = lnum;
3138 curwin->w_cursor.col = 0;
3139 }
3140 arg = startarg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003141 if (get_func_tv(name, STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003142 eap->line1, eap->line2, &doesrange,
3143 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 {
3145 failed = TRUE;
3146 break;
3147 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003148 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 if (doesrange || eap->skip)
3150 break;
3151 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003152 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003153 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003154 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 if (aborting())
3156 break;
3157 }
3158 if (eap->skip)
3159 --emsg_skip;
3160
3161 if (!failed)
3162 {
3163 /* Check for trailing illegal characters and a following command. */
3164 if (!ends_excmd(*arg))
3165 {
3166 emsg_severe = TRUE;
3167 EMSG(_(e_trailing));
3168 }
3169 else
3170 eap->nextcmd = check_nextcmd(arg);
3171 }
3172
3173end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003174 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003175 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176}
3177
3178/*
3179 * ":unlet[!] var1 ... " command.
3180 */
3181 void
3182ex_unlet(eap)
3183 exarg_T *eap;
3184{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003185 ex_unletlock(eap, eap->arg, 0);
3186}
3187
3188/*
3189 * ":lockvar" and ":unlockvar" commands
3190 */
3191 void
3192ex_lockvar(eap)
3193 exarg_T *eap;
3194{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003196 int deep = 2;
3197
3198 if (eap->forceit)
3199 deep = -1;
3200 else if (vim_isdigit(*arg))
3201 {
3202 deep = getdigits(&arg);
3203 arg = skipwhite(arg);
3204 }
3205
3206 ex_unletlock(eap, arg, deep);
3207}
3208
3209/*
3210 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3211 */
3212 static void
3213ex_unletlock(eap, argstart, deep)
3214 exarg_T *eap;
3215 char_u *argstart;
3216 int deep;
3217{
3218 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003221 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222
3223 do
3224 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003225 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003226 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3227 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003228 if (lv.ll_name == NULL)
3229 error = TRUE; /* error but continue parsing */
3230 if (name_end == NULL || (!vim_iswhite(*name_end)
3231 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003233 if (name_end != NULL)
3234 {
3235 emsg_severe = TRUE;
3236 EMSG(_(e_trailing));
3237 }
3238 if (!(eap->skip || error))
3239 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 break;
3241 }
3242
3243 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003244 {
3245 if (eap->cmdidx == CMD_unlet)
3246 {
3247 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3248 error = TRUE;
3249 }
3250 else
3251 {
3252 if (do_lock_var(&lv, name_end, deep,
3253 eap->cmdidx == CMD_lockvar) == FAIL)
3254 error = TRUE;
3255 }
3256 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003258 if (!eap->skip)
3259 clear_lval(&lv);
3260
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 arg = skipwhite(name_end);
3262 } while (!ends_excmd(*arg));
3263
3264 eap->nextcmd = check_nextcmd(arg);
3265}
3266
Bram Moolenaar8c711452005-01-14 21:53:12 +00003267 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003268do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003269 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003270 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003271 int forceit;
3272{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003273 int ret = OK;
3274 int cc;
3275
3276 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003277 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003278 cc = *name_end;
3279 *name_end = NUL;
3280
3281 /* Normal name or expanded name. */
3282 if (check_changedtick(lp->ll_name))
3283 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003284 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003285 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003286 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003287 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003288 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3289 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003290 else if (lp->ll_range)
3291 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003292 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003293
3294 /* Delete a range of List items. */
3295 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3296 {
3297 li = lp->ll_li->li_next;
3298 listitem_remove(lp->ll_list, lp->ll_li);
3299 lp->ll_li = li;
3300 ++lp->ll_n1;
3301 }
3302 }
3303 else
3304 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003305 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003306 /* unlet a List item. */
3307 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003308 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003309 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003310 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003311 }
3312
3313 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003314}
3315
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316/*
3317 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003318 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 */
3320 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003321do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003323 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324{
Bram Moolenaar33570922005-01-25 22:26:29 +00003325 hashtab_T *ht;
3326 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003327 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328
Bram Moolenaar33570922005-01-25 22:26:29 +00003329 ht = find_var_ht(name, &varname);
3330 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003332 hi = hash_find(ht, varname);
3333 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003334 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003335 if (var_check_ro(HI2DI(hi)->di_flags, name))
3336 return FAIL;
3337 delete_var(ht, hi);
3338 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003341 if (forceit)
3342 return OK;
3343 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 return FAIL;
3345}
3346
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003347/*
3348 * Lock or unlock variable indicated by "lp".
3349 * "deep" is the levels to go (-1 for unlimited);
3350 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3351 */
3352 static int
3353do_lock_var(lp, name_end, deep, lock)
3354 lval_T *lp;
3355 char_u *name_end;
3356 int deep;
3357 int lock;
3358{
3359 int ret = OK;
3360 int cc;
3361 dictitem_T *di;
3362
3363 if (deep == 0) /* nothing to do */
3364 return OK;
3365
3366 if (lp->ll_tv == NULL)
3367 {
3368 cc = *name_end;
3369 *name_end = NUL;
3370
3371 /* Normal name or expanded name. */
3372 if (check_changedtick(lp->ll_name))
3373 ret = FAIL;
3374 else
3375 {
3376 di = find_var(lp->ll_name, NULL);
3377 if (di == NULL)
3378 ret = FAIL;
3379 else
3380 {
3381 if (lock)
3382 di->di_flags |= DI_FLAGS_LOCK;
3383 else
3384 di->di_flags &= ~DI_FLAGS_LOCK;
3385 item_lock(&di->di_tv, deep, lock);
3386 }
3387 }
3388 *name_end = cc;
3389 }
3390 else if (lp->ll_range)
3391 {
3392 listitem_T *li = lp->ll_li;
3393
3394 /* (un)lock a range of List items. */
3395 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3396 {
3397 item_lock(&li->li_tv, deep, lock);
3398 li = li->li_next;
3399 ++lp->ll_n1;
3400 }
3401 }
3402 else if (lp->ll_list != NULL)
3403 /* (un)lock a List item. */
3404 item_lock(&lp->ll_li->li_tv, deep, lock);
3405 else
3406 /* un(lock) a Dictionary item. */
3407 item_lock(&lp->ll_di->di_tv, deep, lock);
3408
3409 return ret;
3410}
3411
3412/*
3413 * Lock or unlock an item. "deep" is nr of levels to go.
3414 */
3415 static void
3416item_lock(tv, deep, lock)
3417 typval_T *tv;
3418 int deep;
3419 int lock;
3420{
3421 static int recurse = 0;
3422 list_T *l;
3423 listitem_T *li;
3424 dict_T *d;
3425 hashitem_T *hi;
3426 int todo;
3427
3428 if (recurse >= DICT_MAXNEST)
3429 {
3430 EMSG(_("E743: variable nested too deep for (un)lock"));
3431 return;
3432 }
3433 if (deep == 0)
3434 return;
3435 ++recurse;
3436
3437 /* lock/unlock the item itself */
3438 if (lock)
3439 tv->v_lock |= VAR_LOCKED;
3440 else
3441 tv->v_lock &= ~VAR_LOCKED;
3442
3443 switch (tv->v_type)
3444 {
3445 case VAR_LIST:
3446 if ((l = tv->vval.v_list) != NULL)
3447 {
3448 if (lock)
3449 l->lv_lock |= VAR_LOCKED;
3450 else
3451 l->lv_lock &= ~VAR_LOCKED;
3452 if (deep < 0 || deep > 1)
3453 /* recursive: lock/unlock the items the List contains */
3454 for (li = l->lv_first; li != NULL; li = li->li_next)
3455 item_lock(&li->li_tv, deep - 1, lock);
3456 }
3457 break;
3458 case VAR_DICT:
3459 if ((d = tv->vval.v_dict) != NULL)
3460 {
3461 if (lock)
3462 d->dv_lock |= VAR_LOCKED;
3463 else
3464 d->dv_lock &= ~VAR_LOCKED;
3465 if (deep < 0 || deep > 1)
3466 {
3467 /* recursive: lock/unlock the items the List contains */
3468 todo = d->dv_hashtab.ht_used;
3469 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3470 {
3471 if (!HASHITEM_EMPTY(hi))
3472 {
3473 --todo;
3474 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3475 }
3476 }
3477 }
3478 }
3479 }
3480 --recurse;
3481}
3482
Bram Moolenaara40058a2005-07-11 22:42:07 +00003483/*
3484 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3485 * it refers to a List or Dictionary that is locked.
3486 */
3487 static int
3488tv_islocked(tv)
3489 typval_T *tv;
3490{
3491 return (tv->v_lock & VAR_LOCKED)
3492 || (tv->v_type == VAR_LIST
3493 && tv->vval.v_list != NULL
3494 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3495 || (tv->v_type == VAR_DICT
3496 && tv->vval.v_dict != NULL
3497 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3498}
3499
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3501/*
3502 * Delete all "menutrans_" variables.
3503 */
3504 void
3505del_menutrans_vars()
3506{
Bram Moolenaar33570922005-01-25 22:26:29 +00003507 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003508 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509
Bram Moolenaar33570922005-01-25 22:26:29 +00003510 hash_lock(&globvarht);
3511 todo = globvarht.ht_used;
3512 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003513 {
3514 if (!HASHITEM_EMPTY(hi))
3515 {
3516 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003517 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3518 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003519 }
3520 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003521 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522}
3523#endif
3524
3525#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3526
3527/*
3528 * Local string buffer for the next two functions to store a variable name
3529 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3530 * get_user_var_name().
3531 */
3532
3533static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3534
3535static char_u *varnamebuf = NULL;
3536static int varnamebuflen = 0;
3537
3538/*
3539 * Function to concatenate a prefix and a variable name.
3540 */
3541 static char_u *
3542cat_prefix_varname(prefix, name)
3543 int prefix;
3544 char_u *name;
3545{
3546 int len;
3547
3548 len = (int)STRLEN(name) + 3;
3549 if (len > varnamebuflen)
3550 {
3551 vim_free(varnamebuf);
3552 len += 10; /* some additional space */
3553 varnamebuf = alloc(len);
3554 if (varnamebuf == NULL)
3555 {
3556 varnamebuflen = 0;
3557 return NULL;
3558 }
3559 varnamebuflen = len;
3560 }
3561 *varnamebuf = prefix;
3562 varnamebuf[1] = ':';
3563 STRCPY(varnamebuf + 2, name);
3564 return varnamebuf;
3565}
3566
3567/*
3568 * Function given to ExpandGeneric() to obtain the list of user defined
3569 * (global/buffer/window/built-in) variable names.
3570 */
3571/*ARGSUSED*/
3572 char_u *
3573get_user_var_name(xp, idx)
3574 expand_T *xp;
3575 int idx;
3576{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003577 static long_u gdone;
3578 static long_u bdone;
3579 static long_u wdone;
3580 static int vidx;
3581 static hashitem_T *hi;
3582 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583
3584 if (idx == 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00003585 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00003586
3587 /* Global variables */
3588 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003590 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003591 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003592 else
3593 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003594 while (HASHITEM_EMPTY(hi))
3595 ++hi;
3596 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3597 return cat_prefix_varname('g', hi->hi_key);
3598 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003600
3601 /* b: variables */
3602 ht = &curbuf->b_vars.dv_hashtab;
3603 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003605 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003606 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003607 else
3608 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003609 while (HASHITEM_EMPTY(hi))
3610 ++hi;
3611 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003613 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003615 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 return (char_u *)"b:changedtick";
3617 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003618
3619 /* w: variables */
3620 ht = &curwin->w_vars.dv_hashtab;
3621 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003623 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003624 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003625 else
3626 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003627 while (HASHITEM_EMPTY(hi))
3628 ++hi;
3629 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003631
3632 /* v: variables */
3633 if (vidx < VV_LEN)
3634 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635
3636 vim_free(varnamebuf);
3637 varnamebuf = NULL;
3638 varnamebuflen = 0;
3639 return NULL;
3640}
3641
3642#endif /* FEAT_CMDL_COMPL */
3643
3644/*
3645 * types for expressions.
3646 */
3647typedef enum
3648{
3649 TYPE_UNKNOWN = 0
3650 , TYPE_EQUAL /* == */
3651 , TYPE_NEQUAL /* != */
3652 , TYPE_GREATER /* > */
3653 , TYPE_GEQUAL /* >= */
3654 , TYPE_SMALLER /* < */
3655 , TYPE_SEQUAL /* <= */
3656 , TYPE_MATCH /* =~ */
3657 , TYPE_NOMATCH /* !~ */
3658} exptype_T;
3659
3660/*
3661 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003662 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3664 */
3665
3666/*
3667 * Handle zero level expression.
3668 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003669 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003670 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 * Return OK or FAIL.
3672 */
3673 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003674eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003676 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 char_u **nextcmd;
3678 int evaluate;
3679{
3680 int ret;
3681 char_u *p;
3682
3683 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003684 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 if (ret == FAIL || !ends_excmd(*p))
3686 {
3687 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003688 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 /*
3690 * Report the invalid expression unless the expression evaluation has
3691 * been cancelled due to an aborting error, an interrupt, or an
3692 * exception.
3693 */
3694 if (!aborting())
3695 EMSG2(_(e_invexpr2), arg);
3696 ret = FAIL;
3697 }
3698 if (nextcmd != NULL)
3699 *nextcmd = check_nextcmd(p);
3700
3701 return ret;
3702}
3703
3704/*
3705 * Handle top level expression:
3706 * expr1 ? expr0 : expr0
3707 *
3708 * "arg" must point to the first non-white of the expression.
3709 * "arg" is advanced to the next non-white after the recognized expression.
3710 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003711 * Note: "rettv.v_lock" is not set.
3712 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 * Return OK or FAIL.
3714 */
3715 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003716eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003718 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 int evaluate;
3720{
3721 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003722 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723
3724 /*
3725 * Get the first variable.
3726 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003727 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 return FAIL;
3729
3730 if ((*arg)[0] == '?')
3731 {
3732 result = FALSE;
3733 if (evaluate)
3734 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003735 int error = FALSE;
3736
3737 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003739 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003740 if (error)
3741 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 }
3743
3744 /*
3745 * Get the second variable.
3746 */
3747 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003748 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 return FAIL;
3750
3751 /*
3752 * Check for the ":".
3753 */
3754 if ((*arg)[0] != ':')
3755 {
3756 EMSG(_("E109: Missing ':' after '?'"));
3757 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003758 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 return FAIL;
3760 }
3761
3762 /*
3763 * Get the third variable.
3764 */
3765 *arg = skipwhite(*arg + 1);
3766 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3767 {
3768 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003769 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 return FAIL;
3771 }
3772 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003773 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 }
3775
3776 return OK;
3777}
3778
3779/*
3780 * Handle first level expression:
3781 * expr2 || expr2 || expr2 logical OR
3782 *
3783 * "arg" must point to the first non-white of the expression.
3784 * "arg" is advanced to the next non-white after the recognized expression.
3785 *
3786 * Return OK or FAIL.
3787 */
3788 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003789eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003791 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 int evaluate;
3793{
Bram Moolenaar33570922005-01-25 22:26:29 +00003794 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 long result;
3796 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003797 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798
3799 /*
3800 * Get the first variable.
3801 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003802 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 return FAIL;
3804
3805 /*
3806 * Repeat until there is no following "||".
3807 */
3808 first = TRUE;
3809 result = FALSE;
3810 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3811 {
3812 if (evaluate && first)
3813 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003814 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003816 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003817 if (error)
3818 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 first = FALSE;
3820 }
3821
3822 /*
3823 * Get the second variable.
3824 */
3825 *arg = skipwhite(*arg + 2);
3826 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3827 return FAIL;
3828
3829 /*
3830 * Compute the result.
3831 */
3832 if (evaluate && !result)
3833 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003834 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003836 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003837 if (error)
3838 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 }
3840 if (evaluate)
3841 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003842 rettv->v_type = VAR_NUMBER;
3843 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 }
3845 }
3846
3847 return OK;
3848}
3849
3850/*
3851 * Handle second level expression:
3852 * expr3 && expr3 && expr3 logical AND
3853 *
3854 * "arg" must point to the first non-white of the expression.
3855 * "arg" is advanced to the next non-white after the recognized expression.
3856 *
3857 * Return OK or FAIL.
3858 */
3859 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003860eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003862 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 int evaluate;
3864{
Bram Moolenaar33570922005-01-25 22:26:29 +00003865 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 long result;
3867 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003868 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869
3870 /*
3871 * Get the first variable.
3872 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003873 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 return FAIL;
3875
3876 /*
3877 * Repeat until there is no following "&&".
3878 */
3879 first = TRUE;
3880 result = TRUE;
3881 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3882 {
3883 if (evaluate && first)
3884 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003885 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003887 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003888 if (error)
3889 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 first = FALSE;
3891 }
3892
3893 /*
3894 * Get the second variable.
3895 */
3896 *arg = skipwhite(*arg + 2);
3897 if (eval4(arg, &var2, evaluate && result) == FAIL)
3898 return FAIL;
3899
3900 /*
3901 * Compute the result.
3902 */
3903 if (evaluate && result)
3904 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003905 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003907 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003908 if (error)
3909 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 }
3911 if (evaluate)
3912 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003913 rettv->v_type = VAR_NUMBER;
3914 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 }
3916 }
3917
3918 return OK;
3919}
3920
3921/*
3922 * Handle third level expression:
3923 * var1 == var2
3924 * var1 =~ var2
3925 * var1 != var2
3926 * var1 !~ var2
3927 * var1 > var2
3928 * var1 >= var2
3929 * var1 < var2
3930 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003931 * var1 is var2
3932 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 *
3934 * "arg" must point to the first non-white of the expression.
3935 * "arg" is advanced to the next non-white after the recognized expression.
3936 *
3937 * Return OK or FAIL.
3938 */
3939 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003940eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003942 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 int evaluate;
3944{
Bram Moolenaar33570922005-01-25 22:26:29 +00003945 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 char_u *p;
3947 int i;
3948 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003949 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 int len = 2;
3951 long n1, n2;
3952 char_u *s1, *s2;
3953 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3954 regmatch_T regmatch;
3955 int ic;
3956 char_u *save_cpo;
3957
3958 /*
3959 * Get the first variable.
3960 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003961 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 return FAIL;
3963
3964 p = *arg;
3965 switch (p[0])
3966 {
3967 case '=': if (p[1] == '=')
3968 type = TYPE_EQUAL;
3969 else if (p[1] == '~')
3970 type = TYPE_MATCH;
3971 break;
3972 case '!': if (p[1] == '=')
3973 type = TYPE_NEQUAL;
3974 else if (p[1] == '~')
3975 type = TYPE_NOMATCH;
3976 break;
3977 case '>': if (p[1] != '=')
3978 {
3979 type = TYPE_GREATER;
3980 len = 1;
3981 }
3982 else
3983 type = TYPE_GEQUAL;
3984 break;
3985 case '<': if (p[1] != '=')
3986 {
3987 type = TYPE_SMALLER;
3988 len = 1;
3989 }
3990 else
3991 type = TYPE_SEQUAL;
3992 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003993 case 'i': if (p[1] == 's')
3994 {
3995 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3996 len = 5;
3997 if (!vim_isIDc(p[len]))
3998 {
3999 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4000 type_is = TRUE;
4001 }
4002 }
4003 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 }
4005
4006 /*
4007 * If there is a comparitive operator, use it.
4008 */
4009 if (type != TYPE_UNKNOWN)
4010 {
4011 /* extra question mark appended: ignore case */
4012 if (p[len] == '?')
4013 {
4014 ic = TRUE;
4015 ++len;
4016 }
4017 /* extra '#' appended: match case */
4018 else if (p[len] == '#')
4019 {
4020 ic = FALSE;
4021 ++len;
4022 }
4023 /* nothing appened: use 'ignorecase' */
4024 else
4025 ic = p_ic;
4026
4027 /*
4028 * Get the second variable.
4029 */
4030 *arg = skipwhite(p + len);
4031 if (eval5(arg, &var2, evaluate) == FAIL)
4032 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004033 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 return FAIL;
4035 }
4036
4037 if (evaluate)
4038 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004039 if (type_is && rettv->v_type != var2.v_type)
4040 {
4041 /* For "is" a different type always means FALSE, for "notis"
4042 * it means TRUE. */
4043 n1 = (type == TYPE_NEQUAL);
4044 }
4045 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4046 {
4047 if (type_is)
4048 {
4049 n1 = (rettv->v_type == var2.v_type
4050 && rettv->vval.v_list == var2.vval.v_list);
4051 if (type == TYPE_NEQUAL)
4052 n1 = !n1;
4053 }
4054 else if (rettv->v_type != var2.v_type
4055 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4056 {
4057 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004058 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004059 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004060 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004061 clear_tv(rettv);
4062 clear_tv(&var2);
4063 return FAIL;
4064 }
4065 else
4066 {
4067 /* Compare two Lists for being equal or unequal. */
4068 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4069 if (type == TYPE_NEQUAL)
4070 n1 = !n1;
4071 }
4072 }
4073
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004074 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4075 {
4076 if (type_is)
4077 {
4078 n1 = (rettv->v_type == var2.v_type
4079 && rettv->vval.v_dict == var2.vval.v_dict);
4080 if (type == TYPE_NEQUAL)
4081 n1 = !n1;
4082 }
4083 else if (rettv->v_type != var2.v_type
4084 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4085 {
4086 if (rettv->v_type != var2.v_type)
4087 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4088 else
4089 EMSG(_("E736: Invalid operation for Dictionary"));
4090 clear_tv(rettv);
4091 clear_tv(&var2);
4092 return FAIL;
4093 }
4094 else
4095 {
4096 /* Compare two Dictionaries for being equal or unequal. */
4097 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4098 if (type == TYPE_NEQUAL)
4099 n1 = !n1;
4100 }
4101 }
4102
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004103 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4104 {
4105 if (rettv->v_type != var2.v_type
4106 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4107 {
4108 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004109 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004110 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004111 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004112 clear_tv(rettv);
4113 clear_tv(&var2);
4114 return FAIL;
4115 }
4116 else
4117 {
4118 /* Compare two Funcrefs for being equal or unequal. */
4119 if (rettv->vval.v_string == NULL
4120 || var2.vval.v_string == NULL)
4121 n1 = FALSE;
4122 else
4123 n1 = STRCMP(rettv->vval.v_string,
4124 var2.vval.v_string) == 0;
4125 if (type == TYPE_NEQUAL)
4126 n1 = !n1;
4127 }
4128 }
4129
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 /*
4131 * If one of the two variables is a number, compare as a number.
4132 * When using "=~" or "!~", always compare as string.
4133 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004134 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4136 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004137 n1 = get_tv_number(rettv);
4138 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 switch (type)
4140 {
4141 case TYPE_EQUAL: n1 = (n1 == n2); break;
4142 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4143 case TYPE_GREATER: n1 = (n1 > n2); break;
4144 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4145 case TYPE_SMALLER: n1 = (n1 < n2); break;
4146 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4147 case TYPE_UNKNOWN:
4148 case TYPE_MATCH:
4149 case TYPE_NOMATCH: break; /* avoid gcc warning */
4150 }
4151 }
4152 else
4153 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004154 s1 = get_tv_string_buf(rettv, buf1);
4155 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4157 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4158 else
4159 i = 0;
4160 n1 = FALSE;
4161 switch (type)
4162 {
4163 case TYPE_EQUAL: n1 = (i == 0); break;
4164 case TYPE_NEQUAL: n1 = (i != 0); break;
4165 case TYPE_GREATER: n1 = (i > 0); break;
4166 case TYPE_GEQUAL: n1 = (i >= 0); break;
4167 case TYPE_SMALLER: n1 = (i < 0); break;
4168 case TYPE_SEQUAL: n1 = (i <= 0); break;
4169
4170 case TYPE_MATCH:
4171 case TYPE_NOMATCH:
4172 /* avoid 'l' flag in 'cpoptions' */
4173 save_cpo = p_cpo;
4174 p_cpo = (char_u *)"";
4175 regmatch.regprog = vim_regcomp(s2,
4176 RE_MAGIC + RE_STRING);
4177 regmatch.rm_ic = ic;
4178 if (regmatch.regprog != NULL)
4179 {
4180 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4181 vim_free(regmatch.regprog);
4182 if (type == TYPE_NOMATCH)
4183 n1 = !n1;
4184 }
4185 p_cpo = save_cpo;
4186 break;
4187
4188 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4189 }
4190 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004191 clear_tv(rettv);
4192 clear_tv(&var2);
4193 rettv->v_type = VAR_NUMBER;
4194 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 }
4196 }
4197
4198 return OK;
4199}
4200
4201/*
4202 * Handle fourth level expression:
4203 * + number addition
4204 * - number subtraction
4205 * . string concatenation
4206 *
4207 * "arg" must point to the first non-white of the expression.
4208 * "arg" is advanced to the next non-white after the recognized expression.
4209 *
4210 * Return OK or FAIL.
4211 */
4212 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004213eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004215 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 int evaluate;
4217{
Bram Moolenaar33570922005-01-25 22:26:29 +00004218 typval_T var2;
4219 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 int op;
4221 long n1, n2;
4222 char_u *s1, *s2;
4223 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4224 char_u *p;
4225
4226 /*
4227 * Get the first variable.
4228 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004229 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 return FAIL;
4231
4232 /*
4233 * Repeat computing, until no '+', '-' or '.' is following.
4234 */
4235 for (;;)
4236 {
4237 op = **arg;
4238 if (op != '+' && op != '-' && op != '.')
4239 break;
4240
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004241 if (op != '+' || rettv->v_type != VAR_LIST)
4242 {
4243 /* For "list + ...", an illegal use of the first operand as
4244 * a number cannot be determined before evaluating the 2nd
4245 * operand: if this is also a list, all is ok.
4246 * For "something . ...", "something - ..." or "non-list + ...",
4247 * we know that the first operand needs to be a string or number
4248 * without evaluating the 2nd operand. So check before to avoid
4249 * side effects after an error. */
4250 if (evaluate && get_tv_string_chk(rettv) == NULL)
4251 {
4252 clear_tv(rettv);
4253 return FAIL;
4254 }
4255 }
4256
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 /*
4258 * Get the second variable.
4259 */
4260 *arg = skipwhite(*arg + 1);
4261 if (eval6(arg, &var2, evaluate) == FAIL)
4262 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004263 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 return FAIL;
4265 }
4266
4267 if (evaluate)
4268 {
4269 /*
4270 * Compute the result.
4271 */
4272 if (op == '.')
4273 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004274 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4275 s2 = get_tv_string_buf_chk(&var2, buf2);
4276 if (s2 == NULL) /* type error ? */
4277 {
4278 clear_tv(rettv);
4279 clear_tv(&var2);
4280 return FAIL;
4281 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004282 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004283 clear_tv(rettv);
4284 rettv->v_type = VAR_STRING;
4285 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004287 else if (op == '+' && rettv->v_type == VAR_LIST
4288 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004289 {
4290 /* concatenate Lists */
4291 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4292 &var3) == FAIL)
4293 {
4294 clear_tv(rettv);
4295 clear_tv(&var2);
4296 return FAIL;
4297 }
4298 clear_tv(rettv);
4299 *rettv = var3;
4300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 else
4302 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004303 int error = FALSE;
4304
4305 n1 = get_tv_number_chk(rettv, &error);
4306 if (error)
4307 {
4308 /* This can only happen for "list + non-list".
4309 * For "non-list + ..." or "something - ...", we returned
4310 * before evaluating the 2nd operand. */
4311 clear_tv(rettv);
4312 return FAIL;
4313 }
4314 n2 = get_tv_number_chk(&var2, &error);
4315 if (error)
4316 {
4317 clear_tv(rettv);
4318 clear_tv(&var2);
4319 return FAIL;
4320 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004321 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 if (op == '+')
4323 n1 = n1 + n2;
4324 else
4325 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004326 rettv->v_type = VAR_NUMBER;
4327 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004329 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 }
4331 }
4332 return OK;
4333}
4334
4335/*
4336 * Handle fifth level expression:
4337 * * number multiplication
4338 * / number division
4339 * % number modulo
4340 *
4341 * "arg" must point to the first non-white of the expression.
4342 * "arg" is advanced to the next non-white after the recognized expression.
4343 *
4344 * Return OK or FAIL.
4345 */
4346 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004347eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004349 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 int evaluate;
4351{
Bram Moolenaar33570922005-01-25 22:26:29 +00004352 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 int op;
4354 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004355 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356
4357 /*
4358 * Get the first variable.
4359 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004360 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 return FAIL;
4362
4363 /*
4364 * Repeat computing, until no '*', '/' or '%' is following.
4365 */
4366 for (;;)
4367 {
4368 op = **arg;
4369 if (op != '*' && op != '/' && op != '%')
4370 break;
4371
4372 if (evaluate)
4373 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004374 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004375 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004376 if (error)
4377 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 }
4379 else
4380 n1 = 0;
4381
4382 /*
4383 * Get the second variable.
4384 */
4385 *arg = skipwhite(*arg + 1);
4386 if (eval7(arg, &var2, evaluate) == FAIL)
4387 return FAIL;
4388
4389 if (evaluate)
4390 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004391 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004392 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004393 if (error)
4394 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395
4396 /*
4397 * Compute the result.
4398 */
4399 if (op == '*')
4400 n1 = n1 * n2;
4401 else if (op == '/')
4402 {
4403 if (n2 == 0) /* give an error message? */
4404 n1 = 0x7fffffffL;
4405 else
4406 n1 = n1 / n2;
4407 }
4408 else
4409 {
4410 if (n2 == 0) /* give an error message? */
4411 n1 = 0;
4412 else
4413 n1 = n1 % n2;
4414 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004415 rettv->v_type = VAR_NUMBER;
4416 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 }
4418 }
4419
4420 return OK;
4421}
4422
4423/*
4424 * Handle sixth level expression:
4425 * number number constant
4426 * "string" string contstant
4427 * 'string' literal string contstant
4428 * &option-name option value
4429 * @r register contents
4430 * identifier variable value
4431 * function() function call
4432 * $VAR environment variable
4433 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004434 * [expr, expr] List
4435 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 *
4437 * Also handle:
4438 * ! in front logical NOT
4439 * - in front unary minus
4440 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004441 * trailing [] subscript in String or List
4442 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 *
4444 * "arg" must point to the first non-white of the expression.
4445 * "arg" is advanced to the next non-white after the recognized expression.
4446 *
4447 * Return OK or FAIL.
4448 */
4449 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004450eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004452 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 int evaluate;
4454{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 long n;
4456 int len;
4457 char_u *s;
4458 int val;
4459 char_u *start_leader, *end_leader;
4460 int ret = OK;
4461 char_u *alias;
4462
4463 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004464 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004465 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004467 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468
4469 /*
4470 * Skip '!' and '-' characters. They are handled later.
4471 */
4472 start_leader = *arg;
4473 while (**arg == '!' || **arg == '-' || **arg == '+')
4474 *arg = skipwhite(*arg + 1);
4475 end_leader = *arg;
4476
4477 switch (**arg)
4478 {
4479 /*
4480 * Number constant.
4481 */
4482 case '0':
4483 case '1':
4484 case '2':
4485 case '3':
4486 case '4':
4487 case '5':
4488 case '6':
4489 case '7':
4490 case '8':
4491 case '9':
4492 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4493 *arg += len;
4494 if (evaluate)
4495 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004496 rettv->v_type = VAR_NUMBER;
4497 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 }
4499 break;
4500
4501 /*
4502 * String constant: "string".
4503 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004504 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 break;
4506
4507 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004508 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004510 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004511 break;
4512
4513 /*
4514 * List: [expr, expr]
4515 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004516 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 break;
4518
4519 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004520 * Dictionary: {key: val, key: val}
4521 */
4522 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4523 break;
4524
4525 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004526 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004528 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 break;
4530
4531 /*
4532 * Environment variable: $VAR.
4533 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004534 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 break;
4536
4537 /*
4538 * Register contents: @r.
4539 */
4540 case '@': ++*arg;
4541 if (evaluate)
4542 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004543 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004544 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 }
4546 if (**arg != NUL)
4547 ++*arg;
4548 break;
4549
4550 /*
4551 * nested expression: (expression).
4552 */
4553 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004554 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 if (**arg == ')')
4556 ++*arg;
4557 else if (ret == OK)
4558 {
4559 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004560 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 ret = FAIL;
4562 }
4563 break;
4564
Bram Moolenaar8c711452005-01-14 21:53:12 +00004565 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 break;
4567 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004568
4569 if (ret == NOTDONE)
4570 {
4571 /*
4572 * Must be a variable or function name.
4573 * Can also be a curly-braces kind of name: {expr}.
4574 */
4575 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004576 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004577 if (alias != NULL)
4578 s = alias;
4579
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004580 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004581 ret = FAIL;
4582 else
4583 {
4584 if (**arg == '(') /* recursive! */
4585 {
4586 /* If "s" is the name of a variable of type VAR_FUNC
4587 * use its contents. */
4588 s = deref_func_name(s, &len);
4589
4590 /* Invoke the function. */
4591 ret = get_func_tv(s, len, rettv, arg,
4592 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004593 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004594 /* Stop the expression evaluation when immediately
4595 * aborting on error, or when an interrupt occurred or
4596 * an exception was thrown but not caught. */
4597 if (aborting())
4598 {
4599 if (ret == OK)
4600 clear_tv(rettv);
4601 ret = FAIL;
4602 }
4603 }
4604 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004605 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004606 else
4607 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004608 }
4609
4610 if (alias != NULL)
4611 vim_free(alias);
4612 }
4613
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 *arg = skipwhite(*arg);
4615
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004616 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4617 * expr(expr). */
4618 if (ret == OK)
4619 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620
4621 /*
4622 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4623 */
4624 if (ret == OK && evaluate && end_leader > start_leader)
4625 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004626 int error = FALSE;
4627
4628 val = get_tv_number_chk(rettv, &error);
4629 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004631 clear_tv(rettv);
4632 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004634 else
4635 {
4636 while (end_leader > start_leader)
4637 {
4638 --end_leader;
4639 if (*end_leader == '!')
4640 val = !val;
4641 else if (*end_leader == '-')
4642 val = -val;
4643 }
4644 clear_tv(rettv);
4645 rettv->v_type = VAR_NUMBER;
4646 rettv->vval.v_number = val;
4647 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 }
4649
4650 return ret;
4651}
4652
4653/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004654 * Evaluate an "[expr]" or "[expr:expr]" index.
4655 * "*arg" points to the '['.
4656 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4657 */
4658 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004659eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004660 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004661 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004662 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004663 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004664{
4665 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004666 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004667 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004668 long len = -1;
4669 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004670 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004671 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004672
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004673 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004674 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004675 if (verbose)
4676 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004677 return FAIL;
4678 }
4679
Bram Moolenaar8c711452005-01-14 21:53:12 +00004680 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004681 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004682 /*
4683 * dict.name
4684 */
4685 key = *arg + 1;
4686 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4687 ;
4688 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004689 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004690 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004691 }
4692 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004693 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004694 /*
4695 * something[idx]
4696 *
4697 * Get the (first) variable from inside the [].
4698 */
4699 *arg = skipwhite(*arg + 1);
4700 if (**arg == ':')
4701 empty1 = TRUE;
4702 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4703 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004704 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4705 {
4706 /* not a number or string */
4707 clear_tv(&var1);
4708 return FAIL;
4709 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004710
4711 /*
4712 * Get the second variable from inside the [:].
4713 */
4714 if (**arg == ':')
4715 {
4716 range = TRUE;
4717 *arg = skipwhite(*arg + 1);
4718 if (**arg == ']')
4719 empty2 = TRUE;
4720 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4721 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004722 if (!empty1)
4723 clear_tv(&var1);
4724 return FAIL;
4725 }
4726 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4727 {
4728 /* not a number or string */
4729 if (!empty1)
4730 clear_tv(&var1);
4731 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004732 return FAIL;
4733 }
4734 }
4735
4736 /* Check for the ']'. */
4737 if (**arg != ']')
4738 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004739 if (verbose)
4740 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004741 clear_tv(&var1);
4742 if (range)
4743 clear_tv(&var2);
4744 return FAIL;
4745 }
4746 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004747 }
4748
4749 if (evaluate)
4750 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004751 n1 = 0;
4752 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004753 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004754 n1 = get_tv_number(&var1);
4755 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004756 }
4757 if (range)
4758 {
4759 if (empty2)
4760 n2 = -1;
4761 else
4762 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004763 n2 = get_tv_number(&var2);
4764 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004765 }
4766 }
4767
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004768 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004769 {
4770 case VAR_NUMBER:
4771 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004772 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004773 len = (long)STRLEN(s);
4774 if (range)
4775 {
4776 /* The resulting variable is a substring. If the indexes
4777 * are out of range the result is empty. */
4778 if (n1 < 0)
4779 {
4780 n1 = len + n1;
4781 if (n1 < 0)
4782 n1 = 0;
4783 }
4784 if (n2 < 0)
4785 n2 = len + n2;
4786 else if (n2 >= len)
4787 n2 = len;
4788 if (n1 >= len || n2 < 0 || n1 > n2)
4789 s = NULL;
4790 else
4791 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4792 }
4793 else
4794 {
4795 /* The resulting variable is a string of a single
4796 * character. If the index is too big or negative the
4797 * result is empty. */
4798 if (n1 >= len || n1 < 0)
4799 s = NULL;
4800 else
4801 s = vim_strnsave(s + n1, 1);
4802 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004803 clear_tv(rettv);
4804 rettv->v_type = VAR_STRING;
4805 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004806 break;
4807
4808 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004809 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004810 if (n1 < 0)
4811 n1 = len + n1;
4812 if (!empty1 && (n1 < 0 || n1 >= len))
4813 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004814 if (verbose)
4815 EMSGN(_(e_listidx), n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004816 return FAIL;
4817 }
4818 if (range)
4819 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004820 list_T *l;
4821 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004822
4823 if (n2 < 0)
4824 n2 = len + n2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004825 if (!empty2 && (n2 < 0 || n2 >= len || n2 + 1 < n1))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004826 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004827 if (verbose)
4828 EMSGN(_(e_listidx), n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004829 return FAIL;
4830 }
4831 l = list_alloc();
4832 if (l == NULL)
4833 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004834 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004835 n1 <= n2; ++n1)
4836 {
4837 if (list_append_tv(l, &item->li_tv) == FAIL)
4838 {
4839 list_free(l);
4840 return FAIL;
4841 }
4842 item = item->li_next;
4843 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004844 clear_tv(rettv);
4845 rettv->v_type = VAR_LIST;
4846 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004847 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004848 }
4849 else
4850 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004851 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv,
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004852 &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004853 clear_tv(rettv);
4854 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004855 }
4856 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004857
4858 case VAR_DICT:
4859 if (range)
4860 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004861 if (verbose)
4862 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004863 if (len == -1)
4864 clear_tv(&var1);
4865 return FAIL;
4866 }
4867 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004868 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004869
4870 if (len == -1)
4871 {
4872 key = get_tv_string(&var1);
4873 if (*key == NUL)
4874 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004875 if (verbose)
4876 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004877 clear_tv(&var1);
4878 return FAIL;
4879 }
4880 }
4881
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004882 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004883
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004884 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004885 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004886 if (len == -1)
4887 clear_tv(&var1);
4888 if (item == NULL)
4889 return FAIL;
4890
4891 copy_tv(&item->di_tv, &var1);
4892 clear_tv(rettv);
4893 *rettv = var1;
4894 }
4895 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004896 }
4897 }
4898
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004899 return OK;
4900}
4901
4902/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 * Get an option value.
4904 * "arg" points to the '&' or '+' before the option name.
4905 * "arg" is advanced to character after the option name.
4906 * Return OK or FAIL.
4907 */
4908 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004909get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004911 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 int evaluate;
4913{
4914 char_u *option_end;
4915 long numval;
4916 char_u *stringval;
4917 int opt_type;
4918 int c;
4919 int working = (**arg == '+'); /* has("+option") */
4920 int ret = OK;
4921 int opt_flags;
4922
4923 /*
4924 * Isolate the option name and find its value.
4925 */
4926 option_end = find_option_end(arg, &opt_flags);
4927 if (option_end == NULL)
4928 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004929 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 EMSG2(_("E112: Option name missing: %s"), *arg);
4931 return FAIL;
4932 }
4933
4934 if (!evaluate)
4935 {
4936 *arg = option_end;
4937 return OK;
4938 }
4939
4940 c = *option_end;
4941 *option_end = NUL;
4942 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004943 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944
4945 if (opt_type == -3) /* invalid name */
4946 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004947 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 EMSG2(_("E113: Unknown option: %s"), *arg);
4949 ret = FAIL;
4950 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004951 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 {
4953 if (opt_type == -2) /* hidden string option */
4954 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004955 rettv->v_type = VAR_STRING;
4956 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 }
4958 else if (opt_type == -1) /* hidden number option */
4959 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004960 rettv->v_type = VAR_NUMBER;
4961 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 }
4963 else if (opt_type == 1) /* number option */
4964 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004965 rettv->v_type = VAR_NUMBER;
4966 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 }
4968 else /* string option */
4969 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004970 rettv->v_type = VAR_STRING;
4971 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 }
4973 }
4974 else if (working && (opt_type == -2 || opt_type == -1))
4975 ret = FAIL;
4976
4977 *option_end = c; /* put back for error messages */
4978 *arg = option_end;
4979
4980 return ret;
4981}
4982
4983/*
4984 * Allocate a variable for a string constant.
4985 * Return OK or FAIL.
4986 */
4987 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004988get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004990 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 int evaluate;
4992{
4993 char_u *p;
4994 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 int extra = 0;
4996
4997 /*
4998 * Find the end of the string, skipping backslashed characters.
4999 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005000 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 {
5002 if (*p == '\\' && p[1] != NUL)
5003 {
5004 ++p;
5005 /* A "\<x>" form occupies at least 4 characters, and produces up
5006 * to 6 characters: reserve space for 2 extra */
5007 if (*p == '<')
5008 extra += 2;
5009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 }
5011
5012 if (*p != '"')
5013 {
5014 EMSG2(_("E114: Missing quote: %s"), *arg);
5015 return FAIL;
5016 }
5017
5018 /* If only parsing, set *arg and return here */
5019 if (!evaluate)
5020 {
5021 *arg = p + 1;
5022 return OK;
5023 }
5024
5025 /*
5026 * Copy the string into allocated memory, handling backslashed
5027 * characters.
5028 */
5029 name = alloc((unsigned)(p - *arg + extra));
5030 if (name == NULL)
5031 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005032 rettv->v_type = VAR_STRING;
5033 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034
Bram Moolenaar8c711452005-01-14 21:53:12 +00005035 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 {
5037 if (*p == '\\')
5038 {
5039 switch (*++p)
5040 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005041 case 'b': *name++ = BS; ++p; break;
5042 case 'e': *name++ = ESC; ++p; break;
5043 case 'f': *name++ = FF; ++p; break;
5044 case 'n': *name++ = NL; ++p; break;
5045 case 'r': *name++ = CAR; ++p; break;
5046 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047
5048 case 'X': /* hex: "\x1", "\x12" */
5049 case 'x':
5050 case 'u': /* Unicode: "\u0023" */
5051 case 'U':
5052 if (vim_isxdigit(p[1]))
5053 {
5054 int n, nr;
5055 int c = toupper(*p);
5056
5057 if (c == 'X')
5058 n = 2;
5059 else
5060 n = 4;
5061 nr = 0;
5062 while (--n >= 0 && vim_isxdigit(p[1]))
5063 {
5064 ++p;
5065 nr = (nr << 4) + hex2nr(*p);
5066 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005067 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068#ifdef FEAT_MBYTE
5069 /* For "\u" store the number according to
5070 * 'encoding'. */
5071 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005072 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 else
5074#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005075 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 break;
5078
5079 /* octal: "\1", "\12", "\123" */
5080 case '0':
5081 case '1':
5082 case '2':
5083 case '3':
5084 case '4':
5085 case '5':
5086 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005087 case '7': *name = *p++ - '0';
5088 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005090 *name = (*name << 3) + *p++ - '0';
5091 if (*p >= '0' && *p <= '7')
5092 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005094 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 break;
5096
5097 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005098 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 if (extra != 0)
5100 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005101 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 break;
5103 }
5104 /* FALLTHROUGH */
5105
Bram Moolenaar8c711452005-01-14 21:53:12 +00005106 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 break;
5108 }
5109 }
5110 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005111 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005114 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 *arg = p + 1;
5116
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 return OK;
5118}
5119
5120/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005121 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 * Return OK or FAIL.
5123 */
5124 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005125get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005127 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 int evaluate;
5129{
5130 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005131 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005132 int reduce = 0;
5133
5134 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005135 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005136 */
5137 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5138 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005139 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005140 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005141 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005142 break;
5143 ++reduce;
5144 ++p;
5145 }
5146 }
5147
Bram Moolenaar8c711452005-01-14 21:53:12 +00005148 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005149 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005150 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005151 return FAIL;
5152 }
5153
Bram Moolenaar8c711452005-01-14 21:53:12 +00005154 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005155 if (!evaluate)
5156 {
5157 *arg = p + 1;
5158 return OK;
5159 }
5160
5161 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005162 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005163 */
5164 str = alloc((unsigned)((p - *arg) - reduce));
5165 if (str == NULL)
5166 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005167 rettv->v_type = VAR_STRING;
5168 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005169
Bram Moolenaar8c711452005-01-14 21:53:12 +00005170 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005171 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005172 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005173 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005174 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005175 break;
5176 ++p;
5177 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005178 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005179 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005180 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005181 *arg = p + 1;
5182
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005183 return OK;
5184}
5185
5186/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005187 * Allocate a variable for a List and fill it from "*arg".
5188 * Return OK or FAIL.
5189 */
5190 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005191get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005192 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005193 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005194 int evaluate;
5195{
Bram Moolenaar33570922005-01-25 22:26:29 +00005196 list_T *l = NULL;
5197 typval_T tv;
5198 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005199
5200 if (evaluate)
5201 {
5202 l = list_alloc();
5203 if (l == NULL)
5204 return FAIL;
5205 }
5206
5207 *arg = skipwhite(*arg + 1);
5208 while (**arg != ']' && **arg != NUL)
5209 {
5210 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5211 goto failret;
5212 if (evaluate)
5213 {
5214 item = listitem_alloc();
5215 if (item != NULL)
5216 {
5217 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005218 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005219 list_append(l, item);
5220 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005221 else
5222 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005223 }
5224
5225 if (**arg == ']')
5226 break;
5227 if (**arg != ',')
5228 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005229 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005230 goto failret;
5231 }
5232 *arg = skipwhite(*arg + 1);
5233 }
5234
5235 if (**arg != ']')
5236 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005237 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005238failret:
5239 if (evaluate)
5240 list_free(l);
5241 return FAIL;
5242 }
5243
5244 *arg = skipwhite(*arg + 1);
5245 if (evaluate)
5246 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005247 rettv->v_type = VAR_LIST;
5248 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005249 ++l->lv_refcount;
5250 }
5251
5252 return OK;
5253}
5254
5255/*
5256 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005257 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005258 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005259 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005260list_alloc()
5261{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005262 list_T *l;
5263
5264 l = (list_T *)alloc_clear(sizeof(list_T));
5265 if (l != NULL)
5266 {
5267 /* Prepend the list to the list of lists for garbage collection. */
5268 if (first_list != NULL)
5269 first_list->lv_used_prev = l;
5270 l->lv_used_prev = NULL;
5271 l->lv_used_next = first_list;
5272 first_list = l;
5273 }
5274 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005275}
5276
5277/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005278 * Allocate an empty list for a return value.
5279 * Returns OK or FAIL.
5280 */
5281 static int
5282rettv_list_alloc(rettv)
5283 typval_T *rettv;
5284{
5285 list_T *l = list_alloc();
5286
5287 if (l == NULL)
5288 return FAIL;
5289
5290 rettv->vval.v_list = l;
5291 rettv->v_type = VAR_LIST;
5292 ++l->lv_refcount;
5293 return OK;
5294}
5295
5296/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005297 * Unreference a list: decrement the reference count and free it when it
5298 * becomes zero.
5299 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005300 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005301list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005302 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005303{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005304 if (l != NULL && l->lv_refcount != DEL_REFCOUNT && --l->lv_refcount <= 0)
5305 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005306}
5307
5308/*
5309 * Free a list, including all items it points to.
5310 * Ignores the reference count.
5311 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005312 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005313list_free(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005314 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005315{
Bram Moolenaar33570922005-01-25 22:26:29 +00005316 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005317
Bram Moolenaard9fba312005-06-26 22:34:35 +00005318 /* Avoid that recursive reference to the list frees us again. */
5319 l->lv_refcount = DEL_REFCOUNT;
5320
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005321 /* Remove the list from the list of lists for garbage collection. */
5322 if (l->lv_used_prev == NULL)
5323 first_list = l->lv_used_next;
5324 else
5325 l->lv_used_prev->lv_used_next = l->lv_used_next;
5326 if (l->lv_used_next != NULL)
5327 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5328
Bram Moolenaard9fba312005-06-26 22:34:35 +00005329 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005330 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005331 /* Remove the item before deleting it. */
5332 l->lv_first = item->li_next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005333 listitem_free(item);
5334 }
5335 vim_free(l);
5336}
5337
5338/*
5339 * Allocate a list item.
5340 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005341 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005342listitem_alloc()
5343{
Bram Moolenaar33570922005-01-25 22:26:29 +00005344 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005345}
5346
5347/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005348 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005349 */
5350 static void
5351listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005352 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005353{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005354 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005355 vim_free(item);
5356}
5357
5358/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005359 * Remove a list item from a List and free it. Also clears the value.
5360 */
5361 static void
5362listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005363 list_T *l;
5364 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005365{
5366 list_remove(l, item, item);
5367 listitem_free(item);
5368}
5369
5370/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005371 * Get the number of items in a list.
5372 */
5373 static long
5374list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005375 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005376{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005377 if (l == NULL)
5378 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005379 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005380}
5381
5382/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005383 * Return TRUE when two lists have exactly the same values.
5384 */
5385 static int
5386list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005387 list_T *l1;
5388 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005389 int ic; /* ignore case for strings */
5390{
Bram Moolenaar33570922005-01-25 22:26:29 +00005391 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005392
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005393 if (list_len(l1) != list_len(l2))
5394 return FALSE;
5395
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005396 for (item1 = l1->lv_first, item2 = l2->lv_first;
5397 item1 != NULL && item2 != NULL;
5398 item1 = item1->li_next, item2 = item2->li_next)
5399 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5400 return FALSE;
5401 return item1 == NULL && item2 == NULL;
5402}
5403
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005404#if defined(FEAT_PYTHON) || defined(PROTO)
5405/*
5406 * Return the dictitem that an entry in a hashtable points to.
5407 */
5408 dictitem_T *
5409dict_lookup(hi)
5410 hashitem_T *hi;
5411{
5412 return HI2DI(hi);
5413}
5414#endif
5415
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005416/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005417 * Return TRUE when two dictionaries have exactly the same key/values.
5418 */
5419 static int
5420dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005421 dict_T *d1;
5422 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005423 int ic; /* ignore case for strings */
5424{
Bram Moolenaar33570922005-01-25 22:26:29 +00005425 hashitem_T *hi;
5426 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005427 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005428
5429 if (dict_len(d1) != dict_len(d2))
5430 return FALSE;
5431
Bram Moolenaar33570922005-01-25 22:26:29 +00005432 todo = d1->dv_hashtab.ht_used;
5433 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005434 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005435 if (!HASHITEM_EMPTY(hi))
5436 {
5437 item2 = dict_find(d2, hi->hi_key, -1);
5438 if (item2 == NULL)
5439 return FALSE;
5440 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5441 return FALSE;
5442 --todo;
5443 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005444 }
5445 return TRUE;
5446}
5447
5448/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005449 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005450 * Compares the items just like "==" would compare them, but strings and
5451 * numbers are different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005452 */
5453 static int
5454tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005455 typval_T *tv1;
5456 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005457 int ic; /* ignore case */
5458{
5459 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005460 char_u *s1, *s2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005461
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005462 if (tv1->v_type != tv2->v_type)
5463 return FALSE;
5464
5465 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005466 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005467 case VAR_LIST:
5468 /* recursive! */
5469 return list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5470
5471 case VAR_DICT:
5472 /* recursive! */
5473 return dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5474
5475 case VAR_FUNC:
5476 return (tv1->vval.v_string != NULL
5477 && tv2->vval.v_string != NULL
5478 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5479
5480 case VAR_NUMBER:
5481 return tv1->vval.v_number == tv2->vval.v_number;
5482
5483 case VAR_STRING:
5484 s1 = get_tv_string_buf(tv1, buf1);
5485 s2 = get_tv_string_buf(tv2, buf2);
5486 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005487 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005488
5489 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005490 return TRUE;
5491}
5492
5493/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005494 * Locate item with index "n" in list "l" and return it.
5495 * A negative index is counted from the end; -1 is the last item.
5496 * Returns NULL when "n" is out of range.
5497 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005498 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005499list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005500 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005501 long n;
5502{
Bram Moolenaar33570922005-01-25 22:26:29 +00005503 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005504 long idx;
5505
5506 if (l == NULL)
5507 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005508
5509 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005510 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005511 n = l->lv_len + n;
5512
5513 /* Check for index out of range. */
5514 if (n < 0 || n >= l->lv_len)
5515 return NULL;
5516
5517 /* When there is a cached index may start search from there. */
5518 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005519 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005520 if (n < l->lv_idx / 2)
5521 {
5522 /* closest to the start of the list */
5523 item = l->lv_first;
5524 idx = 0;
5525 }
5526 else if (n > (l->lv_idx + l->lv_len) / 2)
5527 {
5528 /* closest to the end of the list */
5529 item = l->lv_last;
5530 idx = l->lv_len - 1;
5531 }
5532 else
5533 {
5534 /* closest to the cached index */
5535 item = l->lv_idx_item;
5536 idx = l->lv_idx;
5537 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005538 }
5539 else
5540 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005541 if (n < l->lv_len / 2)
5542 {
5543 /* closest to the start of the list */
5544 item = l->lv_first;
5545 idx = 0;
5546 }
5547 else
5548 {
5549 /* closest to the end of the list */
5550 item = l->lv_last;
5551 idx = l->lv_len - 1;
5552 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005553 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005554
5555 while (n > idx)
5556 {
5557 /* search forward */
5558 item = item->li_next;
5559 ++idx;
5560 }
5561 while (n < idx)
5562 {
5563 /* search backward */
5564 item = item->li_prev;
5565 --idx;
5566 }
5567
5568 /* cache the used index */
5569 l->lv_idx = idx;
5570 l->lv_idx_item = item;
5571
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005572 return item;
5573}
5574
5575/*
Bram Moolenaara5525202006-03-02 22:52:09 +00005576 * Get list item "l[idx]" as a number.
5577 */
5578 static long
5579list_find_nr(l, idx, errorp)
5580 list_T *l;
5581 long idx;
5582 int *errorp; /* set to TRUE when something wrong */
5583{
5584 listitem_T *li;
5585
5586 li = list_find(l, idx);
5587 if (li == NULL)
5588 {
5589 if (errorp != NULL)
5590 *errorp = TRUE;
5591 return -1L;
5592 }
5593 return get_tv_number_chk(&li->li_tv, errorp);
5594}
5595
5596/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005597 * Locate "item" list "l" and return its index.
5598 * Returns -1 when "item" is not in the list.
5599 */
5600 static long
5601list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005602 list_T *l;
5603 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005604{
5605 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005606 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005607
5608 if (l == NULL)
5609 return -1;
5610 idx = 0;
5611 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5612 ++idx;
5613 if (li == NULL)
5614 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005615 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005616}
5617
5618/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005619 * Append item "item" to the end of list "l".
5620 */
5621 static void
5622list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005623 list_T *l;
5624 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005625{
5626 if (l->lv_last == NULL)
5627 {
5628 /* empty list */
5629 l->lv_first = item;
5630 l->lv_last = item;
5631 item->li_prev = NULL;
5632 }
5633 else
5634 {
5635 l->lv_last->li_next = item;
5636 item->li_prev = l->lv_last;
5637 l->lv_last = item;
5638 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005639 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005640 item->li_next = NULL;
5641}
5642
5643/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005644 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005645 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005646 */
5647 static int
5648list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005649 list_T *l;
5650 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005651{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005652 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005653
Bram Moolenaar05159a02005-02-26 23:04:13 +00005654 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005655 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005656 copy_tv(tv, &li->li_tv);
5657 list_append(l, li);
5658 return OK;
5659}
5660
5661/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005662 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005663 * Return FAIL when out of memory.
5664 */
5665 int
5666list_append_dict(list, dict)
5667 list_T *list;
5668 dict_T *dict;
5669{
5670 listitem_T *li = listitem_alloc();
5671
5672 if (li == NULL)
5673 return FAIL;
5674 li->li_tv.v_type = VAR_DICT;
5675 li->li_tv.v_lock = 0;
5676 li->li_tv.vval.v_dict = dict;
5677 list_append(list, li);
5678 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005679 return OK;
5680}
5681
5682/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005683 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00005684 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005685 * Returns FAIL when out of memory.
5686 */
5687 static int
Bram Moolenaar4463f292005-09-25 22:20:24 +00005688list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005689 list_T *l;
5690 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00005691 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005692{
5693 listitem_T *li = listitem_alloc();
5694
5695 if (li == NULL)
5696 return FAIL;
5697 list_append(l, li);
5698 li->li_tv.v_type = VAR_STRING;
5699 li->li_tv.v_lock = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005700 if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
5701 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005702 return FAIL;
5703 return OK;
5704}
5705
5706/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00005707 * Append "n" to list "l".
5708 * Returns FAIL when out of memory.
5709 */
5710 static int
5711list_append_number(l, n)
5712 list_T *l;
5713 varnumber_T n;
5714{
5715 listitem_T *li;
5716
5717 li = listitem_alloc();
5718 if (li == NULL)
5719 return FAIL;
5720 li->li_tv.v_type = VAR_NUMBER;
5721 li->li_tv.v_lock = 0;
5722 li->li_tv.vval.v_number = n;
5723 list_append(l, li);
5724 return OK;
5725}
5726
5727/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005728 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005729 * If "item" is NULL append at the end.
5730 * Return FAIL when out of memory.
5731 */
5732 static int
5733list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005734 list_T *l;
5735 typval_T *tv;
5736 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005737{
Bram Moolenaar33570922005-01-25 22:26:29 +00005738 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005739
5740 if (ni == NULL)
5741 return FAIL;
5742 copy_tv(tv, &ni->li_tv);
5743 if (item == NULL)
5744 /* Append new item at end of list. */
5745 list_append(l, ni);
5746 else
5747 {
5748 /* Insert new item before existing item. */
5749 ni->li_prev = item->li_prev;
5750 ni->li_next = item;
5751 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005752 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005753 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005754 ++l->lv_idx;
5755 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005756 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005757 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005758 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005759 l->lv_idx_item = NULL;
5760 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005761 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005762 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005763 }
5764 return OK;
5765}
5766
5767/*
5768 * Extend "l1" with "l2".
5769 * If "bef" is NULL append at the end, otherwise insert before this item.
5770 * Returns FAIL when out of memory.
5771 */
5772 static int
5773list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005774 list_T *l1;
5775 list_T *l2;
5776 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005777{
Bram Moolenaar33570922005-01-25 22:26:29 +00005778 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005779
5780 for (item = l2->lv_first; item != NULL; item = item->li_next)
5781 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5782 return FAIL;
5783 return OK;
5784}
5785
5786/*
5787 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5788 * Return FAIL when out of memory.
5789 */
5790 static int
5791list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005792 list_T *l1;
5793 list_T *l2;
5794 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005795{
Bram Moolenaar33570922005-01-25 22:26:29 +00005796 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005797
5798 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005799 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005800 if (l == NULL)
5801 return FAIL;
5802 tv->v_type = VAR_LIST;
5803 tv->vval.v_list = l;
5804
5805 /* append all items from the second list */
5806 return list_extend(l, l2, NULL);
5807}
5808
5809/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005810 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005811 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005812 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005813 * Returns NULL when out of memory.
5814 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005815 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005816list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005817 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005818 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005819 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005820{
Bram Moolenaar33570922005-01-25 22:26:29 +00005821 list_T *copy;
5822 listitem_T *item;
5823 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005824
5825 if (orig == NULL)
5826 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005827
5828 copy = list_alloc();
5829 if (copy != NULL)
5830 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005831 if (copyID != 0)
5832 {
5833 /* Do this before adding the items, because one of the items may
5834 * refer back to this list. */
5835 orig->lv_copyID = copyID;
5836 orig->lv_copylist = copy;
5837 }
5838 for (item = orig->lv_first; item != NULL && !got_int;
5839 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005840 {
5841 ni = listitem_alloc();
5842 if (ni == NULL)
5843 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005844 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005845 {
5846 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5847 {
5848 vim_free(ni);
5849 break;
5850 }
5851 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005852 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005853 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005854 list_append(copy, ni);
5855 }
5856 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005857 if (item != NULL)
5858 {
5859 list_unref(copy);
5860 copy = NULL;
5861 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005862 }
5863
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005864 return copy;
5865}
5866
5867/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005868 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005869 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005870 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005871 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005872list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005873 list_T *l;
5874 listitem_T *item;
5875 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005876{
Bram Moolenaar33570922005-01-25 22:26:29 +00005877 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005878
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005879 /* notify watchers */
5880 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005881 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005882 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005883 list_fix_watch(l, ip);
5884 if (ip == item2)
5885 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005886 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005887
5888 if (item2->li_next == NULL)
5889 l->lv_last = item->li_prev;
5890 else
5891 item2->li_next->li_prev = item->li_prev;
5892 if (item->li_prev == NULL)
5893 l->lv_first = item2->li_next;
5894 else
5895 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005896 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005897}
5898
5899/*
5900 * Return an allocated string with the string representation of a list.
5901 * May return NULL.
5902 */
5903 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005904list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005905 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005906 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005907{
5908 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005909
5910 if (tv->vval.v_list == NULL)
5911 return NULL;
5912 ga_init2(&ga, (int)sizeof(char), 80);
5913 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005914 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005915 {
5916 vim_free(ga.ga_data);
5917 return NULL;
5918 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005919 ga_append(&ga, ']');
5920 ga_append(&ga, NUL);
5921 return (char_u *)ga.ga_data;
5922}
5923
5924/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005925 * Join list "l" into a string in "*gap", using separator "sep".
5926 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005927 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005928 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005929 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005930list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005931 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00005932 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005933 char_u *sep;
5934 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005935 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005936{
5937 int first = TRUE;
5938 char_u *tofree;
5939 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00005940 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005941 char_u *s;
5942
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005943 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005944 {
5945 if (first)
5946 first = FALSE;
5947 else
5948 ga_concat(gap, sep);
5949
5950 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005951 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005952 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005953 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005954 if (s != NULL)
5955 ga_concat(gap, s);
5956 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005957 if (s == NULL)
5958 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005959 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005960 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005961}
5962
5963/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005964 * Garbage collection for lists and dictionaries.
5965 *
5966 * We use reference counts to be able to free most items right away when they
5967 * are no longer used. But for composite items it's possible that it becomes
5968 * unused while the reference count is > 0: When there is a recursive
5969 * reference. Example:
5970 * :let l = [1, 2, 3]
5971 * :let d = {9: l}
5972 * :let l[1] = d
5973 *
5974 * Since this is quite unusual we handle this with garbage collection: every
5975 * once in a while find out which lists and dicts are not referenced from any
5976 * variable.
5977 *
5978 * Here is a good reference text about garbage collection (refers to Python
5979 * but it applies to all reference-counting mechanisms):
5980 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005981 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005982
5983/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005984 * Do garbage collection for lists and dicts.
5985 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005986 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005987 int
5988garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00005989{
5990 dict_T *dd;
5991 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005992 int copyID = ++current_copyID;
5993 buf_T *buf;
5994 win_T *wp;
5995 int i;
5996 funccall_T *fc;
5997 int did_free = FALSE;
5998
5999 /*
6000 * 1. Go through all accessible variables and mark all lists and dicts
6001 * with copyID.
6002 */
6003 /* script-local variables */
6004 for (i = 1; i <= ga_scripts.ga_len; ++i)
6005 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6006
6007 /* buffer-local variables */
6008 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6009 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6010
6011 /* window-local variables */
6012 FOR_ALL_WINDOWS(wp)
6013 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6014
6015 /* global variables */
6016 set_ref_in_ht(&globvarht, copyID);
6017
6018 /* function-local variables */
6019 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6020 {
6021 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6022 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6023 }
6024
6025 /*
6026 * 2. Go through the list of dicts and free items without the copyID.
6027 */
6028 for (dd = first_dict; dd != NULL; )
6029 if (dd->dv_copyID != copyID)
6030 {
6031 dict_free(dd);
6032 did_free = TRUE;
6033
6034 /* restart, next dict may also have been freed */
6035 dd = first_dict;
6036 }
6037 else
6038 dd = dd->dv_used_next;
6039
6040 /*
6041 * 3. Go through the list of lists and free items without the copyID.
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006042 * But don't free a list that has a watcher (used in a for loop), these
6043 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006044 */
6045 for (ll = first_list; ll != NULL; )
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006046 if (ll->lv_copyID != copyID && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006047 {
6048 list_free(ll);
6049 did_free = TRUE;
6050
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006051 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006052 ll = first_list;
6053 }
6054 else
6055 ll = ll->lv_used_next;
6056
6057 return did_free;
6058}
6059
6060/*
6061 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6062 */
6063 static void
6064set_ref_in_ht(ht, copyID)
6065 hashtab_T *ht;
6066 int copyID;
6067{
6068 int todo;
6069 hashitem_T *hi;
6070
6071 todo = ht->ht_used;
6072 for (hi = ht->ht_array; todo > 0; ++hi)
6073 if (!HASHITEM_EMPTY(hi))
6074 {
6075 --todo;
6076 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6077 }
6078}
6079
6080/*
6081 * Mark all lists and dicts referenced through list "l" with "copyID".
6082 */
6083 static void
6084set_ref_in_list(l, copyID)
6085 list_T *l;
6086 int copyID;
6087{
6088 listitem_T *li;
6089
6090 for (li = l->lv_first; li != NULL; li = li->li_next)
6091 set_ref_in_item(&li->li_tv, copyID);
6092}
6093
6094/*
6095 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6096 */
6097 static void
6098set_ref_in_item(tv, copyID)
6099 typval_T *tv;
6100 int copyID;
6101{
6102 dict_T *dd;
6103 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006104
6105 switch (tv->v_type)
6106 {
6107 case VAR_DICT:
6108 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006109 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006110 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006111 /* Didn't see this dict yet. */
6112 dd->dv_copyID = copyID;
6113 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006114 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006115 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006116
6117 case VAR_LIST:
6118 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006119 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006120 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006121 /* Didn't see this list yet. */
6122 ll->lv_copyID = copyID;
6123 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006124 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006125 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006126 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006127 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006128}
6129
6130/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006131 * Allocate an empty header for a dictionary.
6132 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006133 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006134dict_alloc()
6135{
Bram Moolenaar33570922005-01-25 22:26:29 +00006136 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006137
Bram Moolenaar33570922005-01-25 22:26:29 +00006138 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006139 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006140 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006141 /* Add the list to the hashtable for garbage collection. */
6142 if (first_dict != NULL)
6143 first_dict->dv_used_prev = d;
6144 d->dv_used_next = first_dict;
6145 d->dv_used_prev = NULL;
6146
Bram Moolenaar33570922005-01-25 22:26:29 +00006147 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006148 d->dv_lock = 0;
6149 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006150 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006151 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006152 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006153}
6154
6155/*
6156 * Unreference a Dictionary: decrement the reference count and free it when it
6157 * becomes zero.
6158 */
6159 static void
6160dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006161 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006162{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006163 if (d != NULL && d->dv_refcount != DEL_REFCOUNT && --d->dv_refcount <= 0)
6164 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006165}
6166
6167/*
6168 * Free a Dictionary, including all items it contains.
6169 * Ignores the reference count.
6170 */
6171 static void
6172dict_free(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006173 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006174{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006175 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006176 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006177 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006178
Bram Moolenaard9fba312005-06-26 22:34:35 +00006179 /* Avoid that recursive reference to the dict frees us again. */
6180 d->dv_refcount = DEL_REFCOUNT;
6181
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006182 /* Remove the dict from the list of dicts for garbage collection. */
6183 if (d->dv_used_prev == NULL)
6184 first_dict = d->dv_used_next;
6185 else
6186 d->dv_used_prev->dv_used_next = d->dv_used_next;
6187 if (d->dv_used_next != NULL)
6188 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6189
6190 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006191 hash_lock(&d->dv_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +00006192 todo = d->dv_hashtab.ht_used;
6193 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006194 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006195 if (!HASHITEM_EMPTY(hi))
6196 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006197 /* Remove the item before deleting it, just in case there is
6198 * something recursive causing trouble. */
6199 di = HI2DI(hi);
6200 hash_remove(&d->dv_hashtab, hi);
6201 dictitem_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006202 --todo;
6203 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006204 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006205 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006206 vim_free(d);
6207}
6208
6209/*
6210 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006211 * The "key" is copied to the new item.
6212 * Note that the value of the item "di_tv" still needs to be initialized!
6213 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006214 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006215 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006216dictitem_alloc(key)
6217 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006218{
Bram Moolenaar33570922005-01-25 22:26:29 +00006219 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006220
Bram Moolenaar33570922005-01-25 22:26:29 +00006221 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(key));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006222 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006223 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006224 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006225 di->di_flags = 0;
6226 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006227 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006228}
6229
6230/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006231 * Make a copy of a Dictionary item.
6232 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006233 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006234dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006235 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006236{
Bram Moolenaar33570922005-01-25 22:26:29 +00006237 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006238
Bram Moolenaar33570922005-01-25 22:26:29 +00006239 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(org->di_key));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006240 if (di != NULL)
6241 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006242 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006243 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006244 copy_tv(&org->di_tv, &di->di_tv);
6245 }
6246 return di;
6247}
6248
6249/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006250 * Remove item "item" from Dictionary "dict" and free it.
6251 */
6252 static void
6253dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006254 dict_T *dict;
6255 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006256{
Bram Moolenaar33570922005-01-25 22:26:29 +00006257 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006258
Bram Moolenaar33570922005-01-25 22:26:29 +00006259 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006260 if (HASHITEM_EMPTY(hi))
6261 EMSG2(_(e_intern2), "dictitem_remove()");
6262 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006263 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006264 dictitem_free(item);
6265}
6266
6267/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006268 * Free a dict item. Also clears the value.
6269 */
6270 static void
6271dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006272 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006273{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006274 clear_tv(&item->di_tv);
6275 vim_free(item);
6276}
6277
6278/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006279 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6280 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006281 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006282 * Returns NULL when out of memory.
6283 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006284 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006285dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006286 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006287 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006288 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006289{
Bram Moolenaar33570922005-01-25 22:26:29 +00006290 dict_T *copy;
6291 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006292 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006293 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006294
6295 if (orig == NULL)
6296 return NULL;
6297
6298 copy = dict_alloc();
6299 if (copy != NULL)
6300 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006301 if (copyID != 0)
6302 {
6303 orig->dv_copyID = copyID;
6304 orig->dv_copydict = copy;
6305 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006306 todo = orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006307 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006308 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006309 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006310 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006311 --todo;
6312
6313 di = dictitem_alloc(hi->hi_key);
6314 if (di == NULL)
6315 break;
6316 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006317 {
6318 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6319 copyID) == FAIL)
6320 {
6321 vim_free(di);
6322 break;
6323 }
6324 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006325 else
6326 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6327 if (dict_add(copy, di) == FAIL)
6328 {
6329 dictitem_free(di);
6330 break;
6331 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006332 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006333 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006334
Bram Moolenaare9a41262005-01-15 22:18:47 +00006335 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006336 if (todo > 0)
6337 {
6338 dict_unref(copy);
6339 copy = NULL;
6340 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006341 }
6342
6343 return copy;
6344}
6345
6346/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006347 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006348 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006349 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006350 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006351dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006352 dict_T *d;
6353 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006354{
Bram Moolenaar33570922005-01-25 22:26:29 +00006355 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006356}
6357
Bram Moolenaar8c711452005-01-14 21:53:12 +00006358/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006359 * Add a number or string entry to dictionary "d".
6360 * When "str" is NULL use number "nr", otherwise use "str".
6361 * Returns FAIL when out of memory and when key already exists.
6362 */
6363 int
6364dict_add_nr_str(d, key, nr, str)
6365 dict_T *d;
6366 char *key;
6367 long nr;
6368 char_u *str;
6369{
6370 dictitem_T *item;
6371
6372 item = dictitem_alloc((char_u *)key);
6373 if (item == NULL)
6374 return FAIL;
6375 item->di_tv.v_lock = 0;
6376 if (str == NULL)
6377 {
6378 item->di_tv.v_type = VAR_NUMBER;
6379 item->di_tv.vval.v_number = nr;
6380 }
6381 else
6382 {
6383 item->di_tv.v_type = VAR_STRING;
6384 item->di_tv.vval.v_string = vim_strsave(str);
6385 }
6386 if (dict_add(d, item) == FAIL)
6387 {
6388 dictitem_free(item);
6389 return FAIL;
6390 }
6391 return OK;
6392}
6393
6394/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006395 * Get the number of items in a Dictionary.
6396 */
6397 static long
6398dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006399 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006400{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006401 if (d == NULL)
6402 return 0L;
Bram Moolenaar33570922005-01-25 22:26:29 +00006403 return d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006404}
6405
6406/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006407 * Find item "key[len]" in Dictionary "d".
6408 * If "len" is negative use strlen(key).
6409 * Returns NULL when not found.
6410 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006411 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006412dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006413 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006414 char_u *key;
6415 int len;
6416{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006417#define AKEYLEN 200
6418 char_u buf[AKEYLEN];
6419 char_u *akey;
6420 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006421 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006422
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006423 if (len < 0)
6424 akey = key;
6425 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006426 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006427 tofree = akey = vim_strnsave(key, len);
6428 if (akey == NULL)
6429 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006430 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006431 else
6432 {
6433 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006434 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006435 akey = buf;
6436 }
6437
Bram Moolenaar33570922005-01-25 22:26:29 +00006438 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006439 vim_free(tofree);
6440 if (HASHITEM_EMPTY(hi))
6441 return NULL;
6442 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006443}
6444
6445/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006446 * Get a string item from a dictionary.
6447 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00006448 * Returns NULL if the entry doesn't exist or out of memory.
6449 */
6450 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006451get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00006452 dict_T *d;
6453 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006454 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006455{
6456 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006457 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006458
6459 di = dict_find(d, key, -1);
6460 if (di == NULL)
6461 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006462 s = get_tv_string(&di->di_tv);
6463 if (save && s != NULL)
6464 s = vim_strsave(s);
6465 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006466}
6467
6468/*
6469 * Get a number item from a dictionary.
6470 * Returns 0 if the entry doesn't exist or out of memory.
6471 */
6472 long
6473get_dict_number(d, key)
6474 dict_T *d;
6475 char_u *key;
6476{
6477 dictitem_T *di;
6478
6479 di = dict_find(d, key, -1);
6480 if (di == NULL)
6481 return 0;
6482 return get_tv_number(&di->di_tv);
6483}
6484
6485/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006486 * Return an allocated string with the string representation of a Dictionary.
6487 * May return NULL.
6488 */
6489 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006490dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006491 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006492 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006493{
6494 garray_T ga;
6495 int first = TRUE;
6496 char_u *tofree;
6497 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006498 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006499 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006500 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006501 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006502
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006503 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006504 return NULL;
6505 ga_init2(&ga, (int)sizeof(char), 80);
6506 ga_append(&ga, '{');
6507
Bram Moolenaar33570922005-01-25 22:26:29 +00006508 todo = d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006509 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006510 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006511 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006512 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006513 --todo;
6514
6515 if (first)
6516 first = FALSE;
6517 else
6518 ga_concat(&ga, (char_u *)", ");
6519
6520 tofree = string_quote(hi->hi_key, FALSE);
6521 if (tofree != NULL)
6522 {
6523 ga_concat(&ga, tofree);
6524 vim_free(tofree);
6525 }
6526 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006527 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006528 if (s != NULL)
6529 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006530 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006531 if (s == NULL)
6532 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006533 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006534 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006535 if (todo > 0)
6536 {
6537 vim_free(ga.ga_data);
6538 return NULL;
6539 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006540
6541 ga_append(&ga, '}');
6542 ga_append(&ga, NUL);
6543 return (char_u *)ga.ga_data;
6544}
6545
6546/*
6547 * Allocate a variable for a Dictionary and fill it from "*arg".
6548 * Return OK or FAIL. Returns NOTDONE for {expr}.
6549 */
6550 static int
6551get_dict_tv(arg, rettv, evaluate)
6552 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006553 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006554 int evaluate;
6555{
Bram Moolenaar33570922005-01-25 22:26:29 +00006556 dict_T *d = NULL;
6557 typval_T tvkey;
6558 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006559 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00006560 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006561 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006562 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00006563
6564 /*
6565 * First check if it's not a curly-braces thing: {expr}.
6566 * Must do this without evaluating, otherwise a function may be called
6567 * twice. Unfortunately this means we need to call eval1() twice for the
6568 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006569 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006570 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006571 if (*start != '}')
6572 {
6573 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
6574 return FAIL;
6575 if (*start == '}')
6576 return NOTDONE;
6577 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006578
6579 if (evaluate)
6580 {
6581 d = dict_alloc();
6582 if (d == NULL)
6583 return FAIL;
6584 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006585 tvkey.v_type = VAR_UNKNOWN;
6586 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006587
6588 *arg = skipwhite(*arg + 1);
6589 while (**arg != '}' && **arg != NUL)
6590 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006591 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006592 goto failret;
6593 if (**arg != ':')
6594 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006595 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006596 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006597 goto failret;
6598 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006599 key = get_tv_string_buf_chk(&tvkey, buf);
6600 if (key == NULL || *key == NUL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006601 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006602 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6603 if (key != NULL)
6604 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006605 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006606 goto failret;
6607 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006608
6609 *arg = skipwhite(*arg + 1);
6610 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6611 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006612 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006613 goto failret;
6614 }
6615 if (evaluate)
6616 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006617 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006618 if (item != NULL)
6619 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00006620 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006621 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006622 clear_tv(&tv);
6623 goto failret;
6624 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006625 item = dictitem_alloc(key);
6626 clear_tv(&tvkey);
6627 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006628 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006629 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006630 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006631 if (dict_add(d, item) == FAIL)
6632 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006633 }
6634 }
6635
6636 if (**arg == '}')
6637 break;
6638 if (**arg != ',')
6639 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006640 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006641 goto failret;
6642 }
6643 *arg = skipwhite(*arg + 1);
6644 }
6645
6646 if (**arg != '}')
6647 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006648 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006649failret:
6650 if (evaluate)
6651 dict_free(d);
6652 return FAIL;
6653 }
6654
6655 *arg = skipwhite(*arg + 1);
6656 if (evaluate)
6657 {
6658 rettv->v_type = VAR_DICT;
6659 rettv->vval.v_dict = d;
6660 ++d->dv_refcount;
6661 }
6662
6663 return OK;
6664}
6665
Bram Moolenaar8c711452005-01-14 21:53:12 +00006666/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006667 * Return a string with the string representation of a variable.
6668 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006669 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006670 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006671 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006672 * May return NULL;
6673 */
6674 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006675echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006676 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006677 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006678 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006679 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006680{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006681 static int recurse = 0;
6682 char_u *r = NULL;
6683
Bram Moolenaar33570922005-01-25 22:26:29 +00006684 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006685 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006686 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006687 *tofree = NULL;
6688 return NULL;
6689 }
6690 ++recurse;
6691
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006692 switch (tv->v_type)
6693 {
6694 case VAR_FUNC:
6695 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006696 r = tv->vval.v_string;
6697 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006698
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006699 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006700 if (tv->vval.v_list == NULL)
6701 {
6702 *tofree = NULL;
6703 r = NULL;
6704 }
6705 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
6706 {
6707 *tofree = NULL;
6708 r = (char_u *)"[...]";
6709 }
6710 else
6711 {
6712 tv->vval.v_list->lv_copyID = copyID;
6713 *tofree = list2string(tv, copyID);
6714 r = *tofree;
6715 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006716 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006717
Bram Moolenaar8c711452005-01-14 21:53:12 +00006718 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006719 if (tv->vval.v_dict == NULL)
6720 {
6721 *tofree = NULL;
6722 r = NULL;
6723 }
6724 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
6725 {
6726 *tofree = NULL;
6727 r = (char_u *)"{...}";
6728 }
6729 else
6730 {
6731 tv->vval.v_dict->dv_copyID = copyID;
6732 *tofree = dict2string(tv, copyID);
6733 r = *tofree;
6734 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006735 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006736
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006737 case VAR_STRING:
6738 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006739 *tofree = NULL;
6740 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006741 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006742
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006743 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006744 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006745 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006746 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006747
6748 --recurse;
6749 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006750}
6751
6752/*
6753 * Return a string with the string representation of a variable.
6754 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6755 * "numbuf" is used for a number.
6756 * Puts quotes around strings, so that they can be parsed back by eval().
6757 * May return NULL;
6758 */
6759 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006760tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006761 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006762 char_u **tofree;
6763 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006764 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006765{
6766 switch (tv->v_type)
6767 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006768 case VAR_FUNC:
6769 *tofree = string_quote(tv->vval.v_string, TRUE);
6770 return *tofree;
6771 case VAR_STRING:
6772 *tofree = string_quote(tv->vval.v_string, FALSE);
6773 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006774 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006775 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006776 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006777 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006778 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006779 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006780 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006781 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006782}
6783
6784/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006785 * Return string "str" in ' quotes, doubling ' characters.
6786 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006787 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006788 */
6789 static char_u *
6790string_quote(str, function)
6791 char_u *str;
6792 int function;
6793{
Bram Moolenaar33570922005-01-25 22:26:29 +00006794 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006795 char_u *p, *r, *s;
6796
Bram Moolenaar33570922005-01-25 22:26:29 +00006797 len = (function ? 13 : 3);
6798 if (str != NULL)
6799 {
6800 len += STRLEN(str);
6801 for (p = str; *p != NUL; mb_ptr_adv(p))
6802 if (*p == '\'')
6803 ++len;
6804 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006805 s = r = alloc(len);
6806 if (r != NULL)
6807 {
6808 if (function)
6809 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006810 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006811 r += 10;
6812 }
6813 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006814 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006815 if (str != NULL)
6816 for (p = str; *p != NUL; )
6817 {
6818 if (*p == '\'')
6819 *r++ = '\'';
6820 MB_COPY_CHAR(p, r);
6821 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006822 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006823 if (function)
6824 *r++ = ')';
6825 *r++ = NUL;
6826 }
6827 return s;
6828}
6829
6830/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 * Get the value of an environment variable.
6832 * "arg" is pointing to the '$'. It is advanced to after the name.
6833 * If the environment variable was not set, silently assume it is empty.
6834 * Always return OK.
6835 */
6836 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006837get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006839 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 int evaluate;
6841{
6842 char_u *string = NULL;
6843 int len;
6844 int cc;
6845 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006846 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847
6848 ++*arg;
6849 name = *arg;
6850 len = get_env_len(arg);
6851 if (evaluate)
6852 {
6853 if (len != 0)
6854 {
6855 cc = name[len];
6856 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006857 /* first try vim_getenv(), fast for normal environment vars */
6858 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006860 {
6861 if (!mustfree)
6862 string = vim_strsave(string);
6863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864 else
6865 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00006866 if (mustfree)
6867 vim_free(string);
6868
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 /* next try expanding things like $VIM and ${HOME} */
6870 string = expand_env_save(name - 1);
6871 if (string != NULL && *string == '$')
6872 {
6873 vim_free(string);
6874 string = NULL;
6875 }
6876 }
6877 name[len] = cc;
6878 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006879 rettv->v_type = VAR_STRING;
6880 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881 }
6882
6883 return OK;
6884}
6885
6886/*
6887 * Array with names and number of arguments of all internal functions
6888 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
6889 */
6890static struct fst
6891{
6892 char *f_name; /* function name */
6893 char f_min_argc; /* minimal number of arguments */
6894 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00006895 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006896 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897} functions[] =
6898{
Bram Moolenaar0d660222005-01-07 21:51:51 +00006899 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900 {"append", 2, 2, f_append},
6901 {"argc", 0, 0, f_argc},
6902 {"argidx", 0, 0, f_argidx},
6903 {"argv", 1, 1, f_argv},
6904 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006905 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906 {"bufexists", 1, 1, f_bufexists},
6907 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
6908 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
6909 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
6910 {"buflisted", 1, 1, f_buflisted},
6911 {"bufloaded", 1, 1, f_bufloaded},
6912 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006913 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914 {"bufwinnr", 1, 1, f_bufwinnr},
6915 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006916 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006917 {"call", 2, 3, f_call},
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00006918 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 {"char2nr", 1, 1, f_char2nr},
6920 {"cindent", 1, 1, f_cindent},
6921 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00006922#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00006923 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00006924 {"complete_add", 1, 1, f_complete_add},
6925 {"complete_check", 0, 0, f_complete_check},
6926#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006928 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006929 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00006931 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006932 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933 {"delete", 1, 1, f_delete},
6934 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00006935 {"diff_filler", 1, 1, f_diff_filler},
6936 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006937 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006939 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940 {"eventhandler", 0, 0, f_eventhandler},
6941 {"executable", 1, 1, f_executable},
6942 {"exists", 1, 1, f_exists},
6943 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006944 {"extend", 2, 3, f_extend},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
6946 {"filereadable", 1, 1, f_filereadable},
6947 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006948 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006949 {"finddir", 1, 3, f_finddir},
6950 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 {"fnamemodify", 2, 2, f_fnamemodify},
6952 {"foldclosed", 1, 1, f_foldclosed},
6953 {"foldclosedend", 1, 1, f_foldclosedend},
6954 {"foldlevel", 1, 1, f_foldlevel},
6955 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006956 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006958 {"function", 1, 1, f_function},
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006959 {"garbagecollect", 0, 0, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006960 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00006961 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962 {"getbufvar", 2, 2, f_getbufvar},
6963 {"getchar", 0, 1, f_getchar},
6964 {"getcharmod", 0, 0, f_getcharmod},
6965 {"getcmdline", 0, 0, f_getcmdline},
6966 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006967 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006969 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006970 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 {"getfsize", 1, 1, f_getfsize},
6972 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006973 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006974 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00006975 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaara5525202006-03-02 22:52:09 +00006976 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00006977 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006978 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 {"getregtype", 0, 1, f_getregtype},
6980 {"getwinposx", 0, 0, f_getwinposx},
6981 {"getwinposy", 0, 0, f_getwinposy},
6982 {"getwinvar", 2, 2, f_getwinvar},
6983 {"glob", 1, 1, f_glob},
6984 {"globpath", 2, 2, f_globpath},
6985 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006986 {"has_key", 2, 2, f_has_key},
Bram Moolenaar2c932302006-03-18 21:42:09 +00006987 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 {"highlightID", 1, 1, f_hlID}, /* obsolete */
6989 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
6990 {"histadd", 2, 2, f_histadd},
6991 {"histdel", 1, 2, f_histdel},
6992 {"histget", 1, 2, f_histget},
6993 {"histnr", 1, 1, f_histnr},
6994 {"hlID", 1, 1, f_hlID},
6995 {"hlexists", 1, 1, f_hlexists},
6996 {"hostname", 0, 0, f_hostname},
6997 {"iconv", 3, 3, f_iconv},
6998 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006999 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007000 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007002 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 {"inputrestore", 0, 0, f_inputrestore},
7004 {"inputsave", 0, 0, f_inputsave},
7005 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007006 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007008 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007009 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007010 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007011 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007013 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 {"libcall", 3, 3, f_libcall},
7015 {"libcallnr", 3, 3, f_libcallnr},
7016 {"line", 1, 1, f_line},
7017 {"line2byte", 1, 1, f_line2byte},
7018 {"lispindent", 1, 1, f_lispindent},
7019 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007020 {"map", 2, 2, f_map},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007021 {"maparg", 1, 3, f_maparg},
7022 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007023 {"match", 2, 4, f_match},
7024 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007025 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007026 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007027 {"max", 1, 1, f_max},
7028 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007029#ifdef vim_mkdir
7030 {"mkdir", 1, 3, f_mkdir},
7031#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 {"mode", 0, 0, f_mode},
7033 {"nextnonblank", 1, 1, f_nextnonblank},
7034 {"nr2char", 1, 1, f_nr2char},
7035 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007036 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007037 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007038 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007039 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007040 {"reltime", 0, 2, f_reltime},
7041 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 {"remote_expr", 2, 3, f_remote_expr},
7043 {"remote_foreground", 1, 1, f_remote_foreground},
7044 {"remote_peek", 1, 2, f_remote_peek},
7045 {"remote_read", 1, 1, f_remote_read},
7046 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007047 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007049 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007051 {"reverse", 1, 1, f_reverse},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00007052 {"search", 1, 3, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007053 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00007054 {"searchpair", 3, 6, f_searchpair},
7055 {"searchpairpos", 3, 6, f_searchpairpos},
7056 {"searchpos", 1, 3, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 {"server2client", 2, 2, f_server2client},
7058 {"serverlist", 0, 0, f_serverlist},
7059 {"setbufvar", 3, 3, f_setbufvar},
7060 {"setcmdpos", 1, 1, f_setcmdpos},
7061 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007062 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007063 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007064 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 {"setreg", 2, 3, f_setreg},
7066 {"setwinvar", 3, 3, f_setwinvar},
7067 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007068 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007069 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007070 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007071 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007072 {"split", 1, 3, f_split},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007073 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074#ifdef HAVE_STRFTIME
7075 {"strftime", 1, 2, f_strftime},
7076#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007077 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007078 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 {"strlen", 1, 1, f_strlen},
7080 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007081 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082 {"strtrans", 1, 1, f_strtrans},
7083 {"submatch", 1, 1, f_submatch},
7084 {"substitute", 4, 4, f_substitute},
7085 {"synID", 3, 3, f_synID},
7086 {"synIDattr", 2, 3, f_synIDattr},
7087 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007088 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007089 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007090 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007091 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007092 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007093 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007095 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 {"tolower", 1, 1, f_tolower},
7097 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007098 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007100 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 {"virtcol", 1, 1, f_virtcol},
7102 {"visualmode", 0, 1, f_visualmode},
7103 {"winbufnr", 1, 1, f_winbufnr},
7104 {"wincol", 0, 0, f_wincol},
7105 {"winheight", 1, 1, f_winheight},
7106 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007107 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007109 {"winrestview", 1, 1, f_winrestview},
7110 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007112 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113};
7114
7115#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7116
7117/*
7118 * Function given to ExpandGeneric() to obtain the list of internal
7119 * or user defined function names.
7120 */
7121 char_u *
7122get_function_name(xp, idx)
7123 expand_T *xp;
7124 int idx;
7125{
7126 static int intidx = -1;
7127 char_u *name;
7128
7129 if (idx == 0)
7130 intidx = -1;
7131 if (intidx < 0)
7132 {
7133 name = get_user_func_name(xp, idx);
7134 if (name != NULL)
7135 return name;
7136 }
7137 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7138 {
7139 STRCPY(IObuff, functions[intidx].f_name);
7140 STRCAT(IObuff, "(");
7141 if (functions[intidx].f_max_argc == 0)
7142 STRCAT(IObuff, ")");
7143 return IObuff;
7144 }
7145
7146 return NULL;
7147}
7148
7149/*
7150 * Function given to ExpandGeneric() to obtain the list of internal or
7151 * user defined variable or function names.
7152 */
7153/*ARGSUSED*/
7154 char_u *
7155get_expr_name(xp, idx)
7156 expand_T *xp;
7157 int idx;
7158{
7159 static int intidx = -1;
7160 char_u *name;
7161
7162 if (idx == 0)
7163 intidx = -1;
7164 if (intidx < 0)
7165 {
7166 name = get_function_name(xp, idx);
7167 if (name != NULL)
7168 return name;
7169 }
7170 return get_user_var_name(xp, ++intidx);
7171}
7172
7173#endif /* FEAT_CMDL_COMPL */
7174
7175/*
7176 * Find internal function in table above.
7177 * Return index, or -1 if not found
7178 */
7179 static int
7180find_internal_func(name)
7181 char_u *name; /* name of the function */
7182{
7183 int first = 0;
7184 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7185 int cmp;
7186 int x;
7187
7188 /*
7189 * Find the function name in the table. Binary search.
7190 */
7191 while (first <= last)
7192 {
7193 x = first + ((unsigned)(last - first) >> 1);
7194 cmp = STRCMP(name, functions[x].f_name);
7195 if (cmp < 0)
7196 last = x - 1;
7197 else if (cmp > 0)
7198 first = x + 1;
7199 else
7200 return x;
7201 }
7202 return -1;
7203}
7204
7205/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007206 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7207 * name it contains, otherwise return "name".
7208 */
7209 static char_u *
7210deref_func_name(name, lenp)
7211 char_u *name;
7212 int *lenp;
7213{
Bram Moolenaar33570922005-01-25 22:26:29 +00007214 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007215 int cc;
7216
7217 cc = name[*lenp];
7218 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007219 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007220 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007221 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007222 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007223 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007224 {
7225 *lenp = 0;
7226 return (char_u *)""; /* just in case */
7227 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007228 *lenp = STRLEN(v->di_tv.vval.v_string);
7229 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007230 }
7231
7232 return name;
7233}
7234
7235/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236 * Allocate a variable for the result of a function.
7237 * Return OK or FAIL.
7238 */
7239 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007240get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7241 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242 char_u *name; /* name of the function */
7243 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007244 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245 char_u **arg; /* argument, pointing to the '(' */
7246 linenr_T firstline; /* first line of range */
7247 linenr_T lastline; /* last line of range */
7248 int *doesrange; /* return: function handled range */
7249 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007250 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251{
7252 char_u *argp;
7253 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00007254 typval_T argvars[MAX_FUNC_ARGS]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255 int argcount = 0; /* number of arguments found */
7256
7257 /*
7258 * Get the arguments.
7259 */
7260 argp = *arg;
7261 while (argcount < MAX_FUNC_ARGS)
7262 {
7263 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7264 if (*argp == ')' || *argp == ',' || *argp == NUL)
7265 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7267 {
7268 ret = FAIL;
7269 break;
7270 }
7271 ++argcount;
7272 if (*argp != ',')
7273 break;
7274 }
7275 if (*argp == ')')
7276 ++argp;
7277 else
7278 ret = FAIL;
7279
7280 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007281 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007282 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007284 {
7285 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007286 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007287 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007288 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290
7291 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007292 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293
7294 *arg = skipwhite(argp);
7295 return ret;
7296}
7297
7298
7299/*
7300 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00007301 * Return OK when the function can't be called, FAIL otherwise.
7302 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303 */
7304 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007305call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007306 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307 char_u *name; /* name of the function */
7308 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007309 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310 int argcount; /* number of "argvars" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007311 typval_T *argvars; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312 linenr_T firstline; /* first line of range */
7313 linenr_T lastline; /* last line of range */
7314 int *doesrange; /* return: function handled range */
7315 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007316 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317{
7318 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319#define ERROR_UNKNOWN 0
7320#define ERROR_TOOMANY 1
7321#define ERROR_TOOFEW 2
7322#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007323#define ERROR_DICT 4
7324#define ERROR_NONE 5
7325#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326 int error = ERROR_NONE;
7327 int i;
7328 int llen;
7329 ufunc_T *fp;
7330 int cc;
7331#define FLEN_FIXED 40
7332 char_u fname_buf[FLEN_FIXED + 1];
7333 char_u *fname;
7334
7335 /*
7336 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7337 * Change <SNR>123_name() to K_SNR 123_name().
7338 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7339 */
7340 cc = name[len];
7341 name[len] = NUL;
7342 llen = eval_fname_script(name);
7343 if (llen > 0)
7344 {
7345 fname_buf[0] = K_SPECIAL;
7346 fname_buf[1] = KS_EXTRA;
7347 fname_buf[2] = (int)KE_SNR;
7348 i = 3;
7349 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7350 {
7351 if (current_SID <= 0)
7352 error = ERROR_SCRIPT;
7353 else
7354 {
7355 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7356 i = (int)STRLEN(fname_buf);
7357 }
7358 }
7359 if (i + STRLEN(name + llen) < FLEN_FIXED)
7360 {
7361 STRCPY(fname_buf + i, name + llen);
7362 fname = fname_buf;
7363 }
7364 else
7365 {
7366 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7367 if (fname == NULL)
7368 error = ERROR_OTHER;
7369 else
7370 {
7371 mch_memmove(fname, fname_buf, (size_t)i);
7372 STRCPY(fname + i, name + llen);
7373 }
7374 }
7375 }
7376 else
7377 fname = name;
7378
7379 *doesrange = FALSE;
7380
7381
7382 /* execute the function if no errors detected and executing */
7383 if (evaluate && error == ERROR_NONE)
7384 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007385 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386 error = ERROR_UNKNOWN;
7387
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007388 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389 {
7390 /*
7391 * User defined function.
7392 */
7393 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007394
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007396 /* Trigger FuncUndefined event, may load the function. */
7397 if (fp == NULL
7398 && apply_autocmds(EVENT_FUNCUNDEFINED,
7399 fname, fname, TRUE, NULL)
7400 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007402 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403 fp = find_func(fname);
7404 }
7405#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007406 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007407 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007408 {
7409 /* loaded a package, search for the function again */
7410 fp = find_func(fname);
7411 }
7412
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413 if (fp != NULL)
7414 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007415 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007417 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007419 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007421 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007422 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423 else
7424 {
7425 /*
7426 * Call the user function.
7427 * Save and restore search patterns, script variables and
7428 * redo buffer.
7429 */
7430 save_search_patterns();
7431 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007432 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007433 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007434 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007435 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7436 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7437 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007438 /* Function was unreferenced while being used, free it
7439 * now. */
7440 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 restoreRedobuff();
7442 restore_search_patterns();
7443 error = ERROR_NONE;
7444 }
7445 }
7446 }
7447 else
7448 {
7449 /*
7450 * Find the function name in the table, call its implementation.
7451 */
7452 i = find_internal_func(fname);
7453 if (i >= 0)
7454 {
7455 if (argcount < functions[i].f_min_argc)
7456 error = ERROR_TOOFEW;
7457 else if (argcount > functions[i].f_max_argc)
7458 error = ERROR_TOOMANY;
7459 else
7460 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007461 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007462 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 error = ERROR_NONE;
7464 }
7465 }
7466 }
7467 /*
7468 * The function call (or "FuncUndefined" autocommand sequence) might
7469 * have been aborted by an error, an interrupt, or an explicitly thrown
7470 * exception that has not been caught so far. This situation can be
7471 * tested for by calling aborting(). For an error in an internal
7472 * function or for the "E132" error in call_user_func(), however, the
7473 * throw point at which the "force_abort" flag (temporarily reset by
7474 * emsg()) is normally updated has not been reached yet. We need to
7475 * update that flag first to make aborting() reliable.
7476 */
7477 update_force_abort();
7478 }
7479 if (error == ERROR_NONE)
7480 ret = OK;
7481
7482 /*
7483 * Report an error unless the argument evaluation or function call has been
7484 * cancelled due to an aborting error, an interrupt, or an exception.
7485 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007486 if (!aborting())
7487 {
7488 switch (error)
7489 {
7490 case ERROR_UNKNOWN:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007491 emsg_funcname("E117: Unknown function: %s", name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007492 break;
7493 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007494 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007495 break;
7496 case ERROR_TOOFEW:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007497 emsg_funcname("E119: Not enough arguments for function: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007498 name);
7499 break;
7500 case ERROR_SCRIPT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007501 emsg_funcname("E120: Using <SID> not in a script context: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007502 name);
7503 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007504 case ERROR_DICT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007505 emsg_funcname("E725: Calling dict function without Dictionary: %s",
Bram Moolenaare9a41262005-01-15 22:18:47 +00007506 name);
7507 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007508 }
7509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510
7511 name[len] = cc;
7512 if (fname != name && fname != fname_buf)
7513 vim_free(fname);
7514
7515 return ret;
7516}
7517
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007518/*
7519 * Give an error message with a function name. Handle <SNR> things.
7520 */
7521 static void
7522emsg_funcname(msg, name)
7523 char *msg;
7524 char_u *name;
7525{
7526 char_u *p;
7527
7528 if (*name == K_SPECIAL)
7529 p = concat_str((char_u *)"<SNR>", name + 3);
7530 else
7531 p = name;
7532 EMSG2(_(msg), p);
7533 if (p != name)
7534 vim_free(p);
7535}
7536
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537/*********************************************
7538 * Implementation of the built-in functions
7539 */
7540
7541/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007542 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543 */
7544 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007545f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007546 typval_T *argvars;
7547 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548{
Bram Moolenaar33570922005-01-25 22:26:29 +00007549 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007550
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007551 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007552 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007554 if ((l = argvars[0].vval.v_list) != NULL
7555 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7556 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007557 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007558 }
7559 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007560 EMSG(_(e_listreq));
7561}
7562
7563/*
7564 * "append(lnum, string/list)" function
7565 */
7566 static void
7567f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007568 typval_T *argvars;
7569 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007570{
7571 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007572 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007573 list_T *l = NULL;
7574 listitem_T *li = NULL;
7575 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007576 long added = 0;
7577
Bram Moolenaar0d660222005-01-07 21:51:51 +00007578 lnum = get_tv_lnum(argvars);
7579 if (lnum >= 0
7580 && lnum <= curbuf->b_ml.ml_line_count
7581 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007582 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007583 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007584 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007585 l = argvars[1].vval.v_list;
7586 if (l == NULL)
7587 return;
7588 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007589 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007590 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007591 for (;;)
7592 {
7593 if (l == NULL)
7594 tv = &argvars[1]; /* append a string */
7595 else if (li == NULL)
7596 break; /* end of list */
7597 else
7598 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007599 line = get_tv_string_chk(tv);
7600 if (line == NULL) /* type error */
7601 {
7602 rettv->vval.v_number = 1; /* Failed */
7603 break;
7604 }
7605 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007606 ++added;
7607 if (l == NULL)
7608 break;
7609 li = li->li_next;
7610 }
7611
7612 appended_lines_mark(lnum, added);
7613 if (curwin->w_cursor.lnum > lnum)
7614 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007616 else
7617 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007618}
7619
7620/*
7621 * "argc()" function
7622 */
7623/* ARGSUSED */
7624 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007625f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007626 typval_T *argvars;
7627 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007629 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630}
7631
7632/*
7633 * "argidx()" function
7634 */
7635/* ARGSUSED */
7636 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007637f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007638 typval_T *argvars;
7639 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007641 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642}
7643
7644/*
7645 * "argv(nr)" function
7646 */
7647 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007648f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007649 typval_T *argvars;
7650 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651{
7652 int idx;
7653
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007654 idx = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655 if (idx >= 0 && idx < ARGCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007656 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007658 rettv->vval.v_string = NULL;
7659 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660}
7661
7662/*
7663 * "browse(save, title, initdir, default)" function
7664 */
7665/* ARGSUSED */
7666 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007667f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007668 typval_T *argvars;
7669 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670{
7671#ifdef FEAT_BROWSE
7672 int save;
7673 char_u *title;
7674 char_u *initdir;
7675 char_u *defname;
7676 char_u buf[NUMBUFLEN];
7677 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007678 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007680 save = get_tv_number_chk(&argvars[0], &error);
7681 title = get_tv_string_chk(&argvars[1]);
7682 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7683 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007685 if (error || title == NULL || initdir == NULL || defname == NULL)
7686 rettv->vval.v_string = NULL;
7687 else
7688 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007689 do_browse(save ? BROWSE_SAVE : 0,
7690 title, defname, NULL, initdir, NULL, curbuf);
7691#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007692 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007693#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007694 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007695}
7696
7697/*
7698 * "browsedir(title, initdir)" function
7699 */
7700/* ARGSUSED */
7701 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007702f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007703 typval_T *argvars;
7704 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007705{
7706#ifdef FEAT_BROWSE
7707 char_u *title;
7708 char_u *initdir;
7709 char_u buf[NUMBUFLEN];
7710
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007711 title = get_tv_string_chk(&argvars[0]);
7712 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007713
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007714 if (title == NULL || initdir == NULL)
7715 rettv->vval.v_string = NULL;
7716 else
7717 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007718 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007720 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007722 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723}
7724
Bram Moolenaar33570922005-01-25 22:26:29 +00007725static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007726
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727/*
7728 * Find a buffer by number or exact name.
7729 */
7730 static buf_T *
7731find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007732 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733{
7734 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007736 if (avar->v_type == VAR_NUMBER)
7737 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007738 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007740 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007741 if (buf == NULL)
7742 {
7743 /* No full path name match, try a match with a URL or a "nofile"
7744 * buffer, these don't use the full path. */
7745 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7746 if (buf->b_fname != NULL
7747 && (path_with_url(buf->b_fname)
7748#ifdef FEAT_QUICKFIX
7749 || bt_nofile(buf)
7750#endif
7751 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007752 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007753 break;
7754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755 }
7756 return buf;
7757}
7758
7759/*
7760 * "bufexists(expr)" function
7761 */
7762 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007763f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007764 typval_T *argvars;
7765 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007767 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768}
7769
7770/*
7771 * "buflisted(expr)" function
7772 */
7773 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007774f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007775 typval_T *argvars;
7776 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777{
7778 buf_T *buf;
7779
7780 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007781 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782}
7783
7784/*
7785 * "bufloaded(expr)" function
7786 */
7787 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007788f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007789 typval_T *argvars;
7790 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791{
7792 buf_T *buf;
7793
7794 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007795 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796}
7797
Bram Moolenaar33570922005-01-25 22:26:29 +00007798static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007799
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800/*
7801 * Get buffer by number or pattern.
7802 */
7803 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007804get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007805 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007807 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808 int save_magic;
7809 char_u *save_cpo;
7810 buf_T *buf;
7811
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007812 if (tv->v_type == VAR_NUMBER)
7813 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007814 if (tv->v_type != VAR_STRING)
7815 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816 if (name == NULL || *name == NUL)
7817 return curbuf;
7818 if (name[0] == '$' && name[1] == NUL)
7819 return lastbuf;
7820
7821 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7822 save_magic = p_magic;
7823 p_magic = TRUE;
7824 save_cpo = p_cpo;
7825 p_cpo = (char_u *)"";
7826
7827 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
7828 TRUE, FALSE));
7829
7830 p_magic = save_magic;
7831 p_cpo = save_cpo;
7832
7833 /* If not found, try expanding the name, like done for bufexists(). */
7834 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007835 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836
7837 return buf;
7838}
7839
7840/*
7841 * "bufname(expr)" function
7842 */
7843 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007844f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007845 typval_T *argvars;
7846 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847{
7848 buf_T *buf;
7849
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007850 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007852 buf = get_buf_tv(&argvars[0]);
7853 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007855 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007857 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858 --emsg_off;
7859}
7860
7861/*
7862 * "bufnr(expr)" function
7863 */
7864 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007865f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007866 typval_T *argvars;
7867 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868{
7869 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007870 int error = FALSE;
7871 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007873 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007875 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007876 --emsg_off;
7877
7878 /* If the buffer isn't found and the second argument is not zero create a
7879 * new buffer. */
7880 if (buf == NULL
7881 && argvars[1].v_type != VAR_UNKNOWN
7882 && get_tv_number_chk(&argvars[1], &error) != 0
7883 && !error
7884 && (name = get_tv_string_chk(&argvars[0])) != NULL
7885 && !error)
7886 buf = buflist_new(name, NULL, (linenr_T)1, 0);
7887
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007889 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007891 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892}
7893
7894/*
7895 * "bufwinnr(nr)" function
7896 */
7897 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007898f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007899 typval_T *argvars;
7900 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901{
7902#ifdef FEAT_WINDOWS
7903 win_T *wp;
7904 int winnr = 0;
7905#endif
7906 buf_T *buf;
7907
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007908 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007910 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911#ifdef FEAT_WINDOWS
7912 for (wp = firstwin; wp; wp = wp->w_next)
7913 {
7914 ++winnr;
7915 if (wp->w_buffer == buf)
7916 break;
7917 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007918 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007920 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921#endif
7922 --emsg_off;
7923}
7924
7925/*
7926 * "byte2line(byte)" function
7927 */
7928/*ARGSUSED*/
7929 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007930f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007931 typval_T *argvars;
7932 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933{
7934#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007935 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936#else
7937 long boff = 0;
7938
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007939 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007941 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007943 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944 (linenr_T)0, &boff);
7945#endif
7946}
7947
7948/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007949 * "byteidx()" function
7950 */
7951/*ARGSUSED*/
7952 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007953f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007954 typval_T *argvars;
7955 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007956{
7957#ifdef FEAT_MBYTE
7958 char_u *t;
7959#endif
7960 char_u *str;
7961 long idx;
7962
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007963 str = get_tv_string_chk(&argvars[0]);
7964 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007965 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007966 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007967 return;
7968
7969#ifdef FEAT_MBYTE
7970 t = str;
7971 for ( ; idx > 0; idx--)
7972 {
7973 if (*t == NUL) /* EOL reached */
7974 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007975 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007976 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007977 rettv->vval.v_number = t - str;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007978#else
7979 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007980 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007981#endif
7982}
7983
7984/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007985 * "call(func, arglist)" function
7986 */
7987 static void
7988f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007989 typval_T *argvars;
7990 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007991{
7992 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00007993 typval_T argv[MAX_FUNC_ARGS];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007994 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007995 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007996 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00007997 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007998
7999 rettv->vval.v_number = 0;
8000 if (argvars[1].v_type != VAR_LIST)
8001 {
8002 EMSG(_(e_listreq));
8003 return;
8004 }
8005 if (argvars[1].vval.v_list == NULL)
8006 return;
8007
8008 if (argvars[0].v_type == VAR_FUNC)
8009 func = argvars[0].vval.v_string;
8010 else
8011 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008012 if (*func == NUL)
8013 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008014
Bram Moolenaare9a41262005-01-15 22:18:47 +00008015 if (argvars[2].v_type != VAR_UNKNOWN)
8016 {
8017 if (argvars[2].v_type != VAR_DICT)
8018 {
8019 EMSG(_(e_dictreq));
8020 return;
8021 }
8022 selfdict = argvars[2].vval.v_dict;
8023 }
8024
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008025 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
8026 item = item->li_next)
8027 {
8028 if (argc == MAX_FUNC_ARGS)
8029 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008030 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008031 break;
8032 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008033 /* Make a copy of each argument. This is needed to be able to set
8034 * v_lock to VAR_FIXED in the copy without changing the original list.
8035 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008036 copy_tv(&item->li_tv, &argv[argc++]);
8037 }
8038
8039 if (item == NULL)
8040 (void)call_func(func, STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008041 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
8042 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008043
8044 /* Free the arguments. */
8045 while (argc > 0)
8046 clear_tv(&argv[--argc]);
8047}
8048
8049/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008050 * "changenr()" function
8051 */
8052/*ARGSUSED*/
8053 static void
8054f_changenr(argvars, rettv)
8055 typval_T *argvars;
8056 typval_T *rettv;
8057{
8058 rettv->vval.v_number = curbuf->b_u_seq_cur;
8059}
8060
8061/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062 * "char2nr(string)" function
8063 */
8064 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008065f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008066 typval_T *argvars;
8067 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068{
8069#ifdef FEAT_MBYTE
8070 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008071 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072 else
8073#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008074 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075}
8076
8077/*
8078 * "cindent(lnum)" function
8079 */
8080 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008081f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008082 typval_T *argvars;
8083 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084{
8085#ifdef FEAT_CINDENT
8086 pos_T pos;
8087 linenr_T lnum;
8088
8089 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008090 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8092 {
8093 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008094 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095 curwin->w_cursor = pos;
8096 }
8097 else
8098#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008099 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100}
8101
8102/*
8103 * "col(string)" function
8104 */
8105 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008106f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008107 typval_T *argvars;
8108 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109{
8110 colnr_T col = 0;
8111 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008112 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008114 fp = var2fpos(&argvars[0], FALSE, &fnum);
8115 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116 {
8117 if (fp->col == MAXCOL)
8118 {
8119 /* '> can be MAXCOL, get the length of the line then */
8120 if (fp->lnum <= curbuf->b_ml.ml_line_count)
8121 col = STRLEN(ml_get(fp->lnum)) + 1;
8122 else
8123 col = MAXCOL;
8124 }
8125 else
8126 {
8127 col = fp->col + 1;
8128#ifdef FEAT_VIRTUALEDIT
8129 /* col(".") when the cursor is on the NUL at the end of the line
8130 * because of "coladd" can be seen as an extra column. */
8131 if (virtual_active() && fp == &curwin->w_cursor)
8132 {
8133 char_u *p = ml_get_cursor();
8134
8135 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
8136 curwin->w_virtcol - curwin->w_cursor.coladd))
8137 {
8138# ifdef FEAT_MBYTE
8139 int l;
8140
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008141 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142 col += l;
8143# else
8144 if (*p != NUL && p[1] == NUL)
8145 ++col;
8146# endif
8147 }
8148 }
8149#endif
8150 }
8151 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008152 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153}
8154
Bram Moolenaar572cb562005-08-05 21:35:02 +00008155#if defined(FEAT_INS_EXPAND)
8156/*
Bram Moolenaarade00832006-03-10 21:46:58 +00008157 * "complete()" function
8158 */
8159/*ARGSUSED*/
8160 static void
8161f_complete(argvars, rettv)
8162 typval_T *argvars;
8163 typval_T *rettv;
8164{
8165 int startcol;
8166
8167 if ((State & INSERT) == 0)
8168 {
8169 EMSG(_("E785: complete() can only be used in Insert mode"));
8170 return;
8171 }
8172 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
8173 {
8174 EMSG(_(e_invarg));
8175 return;
8176 }
8177
8178 startcol = get_tv_number_chk(&argvars[0], NULL);
8179 if (startcol <= 0)
8180 return;
8181
8182 set_completion(startcol - 1, argvars[1].vval.v_list);
8183}
8184
8185/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00008186 * "complete_add()" function
8187 */
8188/*ARGSUSED*/
8189 static void
8190f_complete_add(argvars, rettv)
8191 typval_T *argvars;
8192 typval_T *rettv;
8193{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00008194 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00008195}
8196
8197/*
8198 * "complete_check()" function
8199 */
8200/*ARGSUSED*/
8201 static void
8202f_complete_check(argvars, rettv)
8203 typval_T *argvars;
8204 typval_T *rettv;
8205{
8206 int saved = RedrawingDisabled;
8207
8208 RedrawingDisabled = 0;
8209 ins_compl_check_keys(0);
8210 rettv->vval.v_number = compl_interrupted;
8211 RedrawingDisabled = saved;
8212}
8213#endif
8214
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215/*
8216 * "confirm(message, buttons[, default [, type]])" function
8217 */
8218/*ARGSUSED*/
8219 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008220f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008221 typval_T *argvars;
8222 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223{
8224#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
8225 char_u *message;
8226 char_u *buttons = NULL;
8227 char_u buf[NUMBUFLEN];
8228 char_u buf2[NUMBUFLEN];
8229 int def = 1;
8230 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008231 char_u *typestr;
8232 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008234 message = get_tv_string_chk(&argvars[0]);
8235 if (message == NULL)
8236 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008237 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008239 buttons = get_tv_string_buf_chk(&argvars[1], buf);
8240 if (buttons == NULL)
8241 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008242 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008244 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008245 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008247 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
8248 if (typestr == NULL)
8249 error = TRUE;
8250 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008252 switch (TOUPPER_ASC(*typestr))
8253 {
8254 case 'E': type = VIM_ERROR; break;
8255 case 'Q': type = VIM_QUESTION; break;
8256 case 'I': type = VIM_INFO; break;
8257 case 'W': type = VIM_WARNING; break;
8258 case 'G': type = VIM_GENERIC; break;
8259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260 }
8261 }
8262 }
8263 }
8264
8265 if (buttons == NULL || *buttons == NUL)
8266 buttons = (char_u *)_("&Ok");
8267
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008268 if (error)
8269 rettv->vval.v_number = 0;
8270 else
8271 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272 def, NULL);
8273#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008274 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275#endif
8276}
8277
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008278/*
8279 * "copy()" function
8280 */
8281 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008282f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008283 typval_T *argvars;
8284 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008285{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008286 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008287}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288
8289/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008290 * "count()" function
8291 */
8292 static void
8293f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008294 typval_T *argvars;
8295 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008296{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008297 long n = 0;
8298 int ic = FALSE;
8299
Bram Moolenaare9a41262005-01-15 22:18:47 +00008300 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008301 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008302 listitem_T *li;
8303 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008304 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008305
Bram Moolenaare9a41262005-01-15 22:18:47 +00008306 if ((l = argvars[0].vval.v_list) != NULL)
8307 {
8308 li = l->lv_first;
8309 if (argvars[2].v_type != VAR_UNKNOWN)
8310 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008311 int error = FALSE;
8312
8313 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008314 if (argvars[3].v_type != VAR_UNKNOWN)
8315 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008316 idx = get_tv_number_chk(&argvars[3], &error);
8317 if (!error)
8318 {
8319 li = list_find(l, idx);
8320 if (li == NULL)
8321 EMSGN(_(e_listidx), idx);
8322 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008323 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008324 if (error)
8325 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008326 }
8327
8328 for ( ; li != NULL; li = li->li_next)
8329 if (tv_equal(&li->li_tv, &argvars[1], ic))
8330 ++n;
8331 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008332 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008333 else if (argvars[0].v_type == VAR_DICT)
8334 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008335 int todo;
8336 dict_T *d;
8337 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008338
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008339 if ((d = argvars[0].vval.v_dict) != NULL)
8340 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008341 int error = FALSE;
8342
Bram Moolenaare9a41262005-01-15 22:18:47 +00008343 if (argvars[2].v_type != VAR_UNKNOWN)
8344 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008345 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008346 if (argvars[3].v_type != VAR_UNKNOWN)
8347 EMSG(_(e_invarg));
8348 }
8349
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008350 todo = error ? 0 : d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008351 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008352 {
8353 if (!HASHITEM_EMPTY(hi))
8354 {
8355 --todo;
8356 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
8357 ++n;
8358 }
8359 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008360 }
8361 }
8362 else
8363 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008364 rettv->vval.v_number = n;
8365}
8366
8367/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8369 *
8370 * Checks the existence of a cscope connection.
8371 */
8372/*ARGSUSED*/
8373 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008374f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008375 typval_T *argvars;
8376 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377{
8378#ifdef FEAT_CSCOPE
8379 int num = 0;
8380 char_u *dbpath = NULL;
8381 char_u *prepend = NULL;
8382 char_u buf[NUMBUFLEN];
8383
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008384 if (argvars[0].v_type != VAR_UNKNOWN
8385 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008387 num = (int)get_tv_number(&argvars[0]);
8388 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008389 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008390 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391 }
8392
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008393 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008395 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396#endif
8397}
8398
8399/*
8400 * "cursor(lnum, col)" function
8401 *
8402 * Moves the cursor to the specified line and column
8403 */
8404/*ARGSUSED*/
8405 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008406f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008407 typval_T *argvars;
8408 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409{
8410 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008411#ifdef FEAT_VIRTUALEDIT
8412 long coladd = 0;
8413#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414
Bram Moolenaara5525202006-03-02 22:52:09 +00008415 if (argvars[1].v_type == VAR_UNKNOWN)
8416 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008417 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00008418
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008419 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00008420 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008421 line = pos.lnum;
8422 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008423#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008424 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00008425#endif
8426 }
8427 else
8428 {
8429 line = get_tv_lnum(argvars);
8430 col = get_tv_number_chk(&argvars[1], NULL);
8431#ifdef FEAT_VIRTUALEDIT
8432 if (argvars[2].v_type != VAR_UNKNOWN)
8433 coladd = get_tv_number_chk(&argvars[2], NULL);
8434#endif
8435 }
8436 if (line < 0 || col < 0
8437#ifdef FEAT_VIRTUALEDIT
8438 || coladd < 0
8439#endif
8440 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008441 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442 if (line > 0)
8443 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444 if (col > 0)
8445 curwin->w_cursor.col = col - 1;
8446#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00008447 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448#endif
8449
8450 /* Make sure the cursor is in a valid position. */
8451 check_cursor();
8452#ifdef FEAT_MBYTE
8453 /* Correct cursor for multi-byte character. */
8454 if (has_mbyte)
8455 mb_adjust_cursor();
8456#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008457
8458 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008459}
8460
8461/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008462 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463 */
8464 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008465f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008466 typval_T *argvars;
8467 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008469 int noref = 0;
8470
8471 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008472 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008473 if (noref < 0 || noref > 1)
8474 EMSG(_(e_invarg));
8475 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008476 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477}
8478
8479/*
8480 * "delete()" function
8481 */
8482 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008483f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008484 typval_T *argvars;
8485 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486{
8487 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008488 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008490 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491}
8492
8493/*
8494 * "did_filetype()" function
8495 */
8496/*ARGSUSED*/
8497 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008498f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008499 typval_T *argvars;
8500 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501{
8502#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008503 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008505 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506#endif
8507}
8508
8509/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008510 * "diff_filler()" function
8511 */
8512/*ARGSUSED*/
8513 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008514f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008515 typval_T *argvars;
8516 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008517{
8518#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008519 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008520#endif
8521}
8522
8523/*
8524 * "diff_hlID()" function
8525 */
8526/*ARGSUSED*/
8527 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008528f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008529 typval_T *argvars;
8530 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008531{
8532#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008533 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008534 static linenr_T prev_lnum = 0;
8535 static int changedtick = 0;
8536 static int fnum = 0;
8537 static int change_start = 0;
8538 static int change_end = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008539 static hlf_T hlID = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008540 int filler_lines;
8541 int col;
8542
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008543 if (lnum < 0) /* ignore type error in {lnum} arg */
8544 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008545 if (lnum != prev_lnum
8546 || changedtick != curbuf->b_changedtick
8547 || fnum != curbuf->b_fnum)
8548 {
8549 /* New line, buffer, change: need to get the values. */
8550 filler_lines = diff_check(curwin, lnum);
8551 if (filler_lines < 0)
8552 {
8553 if (filler_lines == -1)
8554 {
8555 change_start = MAXCOL;
8556 change_end = -1;
8557 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8558 hlID = HLF_ADD; /* added line */
8559 else
8560 hlID = HLF_CHD; /* changed line */
8561 }
8562 else
8563 hlID = HLF_ADD; /* added line */
8564 }
8565 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008566 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008567 prev_lnum = lnum;
8568 changedtick = curbuf->b_changedtick;
8569 fnum = curbuf->b_fnum;
8570 }
8571
8572 if (hlID == HLF_CHD || hlID == HLF_TXD)
8573 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008574 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008575 if (col >= change_start && col <= change_end)
8576 hlID = HLF_TXD; /* changed text */
8577 else
8578 hlID = HLF_CHD; /* changed line */
8579 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008580 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008581#endif
8582}
8583
8584/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008585 * "empty({expr})" function
8586 */
8587 static void
8588f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008589 typval_T *argvars;
8590 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008591{
8592 int n;
8593
8594 switch (argvars[0].v_type)
8595 {
8596 case VAR_STRING:
8597 case VAR_FUNC:
8598 n = argvars[0].vval.v_string == NULL
8599 || *argvars[0].vval.v_string == NUL;
8600 break;
8601 case VAR_NUMBER:
8602 n = argvars[0].vval.v_number == 0;
8603 break;
8604 case VAR_LIST:
8605 n = argvars[0].vval.v_list == NULL
8606 || argvars[0].vval.v_list->lv_first == NULL;
8607 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008608 case VAR_DICT:
8609 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008610 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008611 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008612 default:
8613 EMSG2(_(e_intern2), "f_empty()");
8614 n = 0;
8615 }
8616
8617 rettv->vval.v_number = n;
8618}
8619
8620/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621 * "escape({string}, {chars})" function
8622 */
8623 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008624f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008625 typval_T *argvars;
8626 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627{
8628 char_u buf[NUMBUFLEN];
8629
Bram Moolenaar758711c2005-02-02 23:11:38 +00008630 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8631 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008632 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633}
8634
8635/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008636 * "eval()" function
8637 */
8638/*ARGSUSED*/
8639 static void
8640f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008641 typval_T *argvars;
8642 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008643{
8644 char_u *s;
8645
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008646 s = get_tv_string_chk(&argvars[0]);
8647 if (s != NULL)
8648 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008649
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008650 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8651 {
8652 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008653 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008654 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008655 else if (*s != NUL)
8656 EMSG(_(e_trailing));
8657}
8658
8659/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660 * "eventhandler()" function
8661 */
8662/*ARGSUSED*/
8663 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008664f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008665 typval_T *argvars;
8666 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008668 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008669}
8670
8671/*
8672 * "executable()" function
8673 */
8674 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008675f_executable(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 = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680}
8681
8682/*
8683 * "exists()" function
8684 */
8685 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008686f_exists(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{
8690 char_u *p;
8691 char_u *name;
8692 int n = FALSE;
8693 int len = 0;
8694
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008695 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696 if (*p == '$') /* environment variable */
8697 {
8698 /* first try "normal" environment variables (fast) */
8699 if (mch_getenv(p + 1) != NULL)
8700 n = TRUE;
8701 else
8702 {
8703 /* try expanding things like $VIM and ${HOME} */
8704 p = expand_env_save(p);
8705 if (p != NULL && *p != '$')
8706 n = TRUE;
8707 vim_free(p);
8708 }
8709 }
8710 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008711 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712 else if (*p == '*') /* internal or user defined function */
8713 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008714 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715 }
8716 else if (*p == ':')
8717 {
8718 n = cmd_exists(p + 1);
8719 }
8720 else if (*p == '#')
8721 {
8722#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00008723 if (p[1] == '#')
8724 n = autocmd_supported(p + 2);
8725 else
8726 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727#endif
8728 }
8729 else /* internal variable */
8730 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008731 char_u *tofree;
8732 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008734 /* get_name_len() takes care of expanding curly braces */
8735 name = p;
8736 len = get_name_len(&p, &tofree, TRUE, FALSE);
8737 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008739 if (tofree != NULL)
8740 name = tofree;
8741 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8742 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008744 /* handle d.key, l[idx], f(expr) */
8745 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8746 if (n)
8747 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748 }
8749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008751 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752 }
8753
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008754 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755}
8756
8757/*
8758 * "expand()" function
8759 */
8760 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008761f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008762 typval_T *argvars;
8763 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764{
8765 char_u *s;
8766 int len;
8767 char_u *errormsg;
8768 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8769 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008770 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008772 rettv->v_type = VAR_STRING;
8773 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774 if (*s == '%' || *s == '#' || *s == '<')
8775 {
8776 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008777 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778 --emsg_off;
8779 }
8780 else
8781 {
8782 /* When the optional second argument is non-zero, don't remove matches
8783 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008784 if (argvars[1].v_type != VAR_UNKNOWN
8785 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008786 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008787 if (!error)
8788 {
8789 ExpandInit(&xpc);
8790 xpc.xp_context = EXPAND_FILES;
8791 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
8792 ExpandCleanup(&xpc);
8793 }
8794 else
8795 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008796 }
8797}
8798
8799/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008800 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00008801 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008802 */
8803 static void
8804f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008805 typval_T *argvars;
8806 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008807{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008808 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008809 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008810 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008811 list_T *l1, *l2;
8812 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008813 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008814 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008815
Bram Moolenaare9a41262005-01-15 22:18:47 +00008816 l1 = argvars[0].vval.v_list;
8817 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008818 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
8819 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008820 {
8821 if (argvars[2].v_type != VAR_UNKNOWN)
8822 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008823 before = get_tv_number_chk(&argvars[2], &error);
8824 if (error)
8825 return; /* type error; errmsg already given */
8826
Bram Moolenaar758711c2005-02-02 23:11:38 +00008827 if (before == l1->lv_len)
8828 item = NULL;
8829 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008830 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00008831 item = list_find(l1, before);
8832 if (item == NULL)
8833 {
8834 EMSGN(_(e_listidx), before);
8835 return;
8836 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008837 }
8838 }
8839 else
8840 item = NULL;
8841 list_extend(l1, l2, item);
8842
Bram Moolenaare9a41262005-01-15 22:18:47 +00008843 copy_tv(&argvars[0], rettv);
8844 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008845 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008846 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
8847 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008848 dict_T *d1, *d2;
8849 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008850 char_u *action;
8851 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00008852 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008853 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008854
8855 d1 = argvars[0].vval.v_dict;
8856 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008857 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
8858 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008859 {
8860 /* Check the third argument. */
8861 if (argvars[2].v_type != VAR_UNKNOWN)
8862 {
8863 static char *(av[]) = {"keep", "force", "error"};
8864
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008865 action = get_tv_string_chk(&argvars[2]);
8866 if (action == NULL)
8867 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008868 for (i = 0; i < 3; ++i)
8869 if (STRCMP(action, av[i]) == 0)
8870 break;
8871 if (i == 3)
8872 {
8873 EMSGN(_(e_invarg2), action);
8874 return;
8875 }
8876 }
8877 else
8878 action = (char_u *)"force";
8879
8880 /* Go over all entries in the second dict and add them to the
8881 * first dict. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008882 todo = d2->dv_hashtab.ht_used;
8883 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008884 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008885 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008886 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008887 --todo;
8888 di1 = dict_find(d1, hi2->hi_key, -1);
8889 if (di1 == NULL)
8890 {
8891 di1 = dictitem_copy(HI2DI(hi2));
8892 if (di1 != NULL && dict_add(d1, di1) == FAIL)
8893 dictitem_free(di1);
8894 }
8895 else if (*action == 'e')
8896 {
8897 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
8898 break;
8899 }
8900 else if (*action == 'f')
8901 {
8902 clear_tv(&di1->di_tv);
8903 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
8904 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008905 }
8906 }
8907
Bram Moolenaare9a41262005-01-15 22:18:47 +00008908 copy_tv(&argvars[0], rettv);
8909 }
8910 }
8911 else
8912 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008913}
8914
8915/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916 * "filereadable()" function
8917 */
8918 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008919f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008920 typval_T *argvars;
8921 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922{
8923 FILE *fd;
8924 char_u *p;
8925 int n;
8926
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008927 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
8929 {
8930 n = TRUE;
8931 fclose(fd);
8932 }
8933 else
8934 n = FALSE;
8935
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008936 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937}
8938
8939/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008940 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00008941 * rights to write into.
8942 */
8943 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008944f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008945 typval_T *argvars;
8946 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008948 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949}
8950
Bram Moolenaar33570922005-01-25 22:26:29 +00008951static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008952
8953 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008954findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00008955 typval_T *argvars;
8956 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008957 int dir;
8958{
8959#ifdef FEAT_SEARCHPATH
8960 char_u *fname;
8961 char_u *fresult = NULL;
8962 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
8963 char_u *p;
8964 char_u pathbuf[NUMBUFLEN];
8965 int count = 1;
8966 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008967 int error = FALSE;
8968#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008969
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008970 rettv->vval.v_string = NULL;
8971 rettv->v_type = VAR_STRING;
8972
8973#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008974 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008975
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008976 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008977 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008978 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
8979 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008980 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008981 else
8982 {
8983 if (*p != NUL)
8984 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008985
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008986 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008987 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008988 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008989 }
8990
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008991 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
8992 error = TRUE;
8993
8994 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008995 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008996 do
8997 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008998 if (rettv->v_type == VAR_STRING)
8999 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009000 fresult = find_file_in_path_option(first ? fname : NULL,
9001 first ? (int)STRLEN(fname) : 0,
Bram Moolenaare580b0c2006-03-21 21:33:03 +00009002 0, first, path, dir, NULL,
9003 dir ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009004 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009005
9006 if (fresult != NULL && rettv->v_type == VAR_LIST)
9007 list_append_string(rettv->vval.v_list, fresult, -1);
9008
9009 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009010 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009011
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009012 if (rettv->v_type == VAR_STRING)
9013 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009014#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009015}
9016
Bram Moolenaar33570922005-01-25 22:26:29 +00009017static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
9018static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009019
9020/*
9021 * Implementation of map() and filter().
9022 */
9023 static void
9024filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00009025 typval_T *argvars;
9026 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009027 int map;
9028{
9029 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00009030 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00009031 listitem_T *li, *nli;
9032 list_T *l = NULL;
9033 dictitem_T *di;
9034 hashtab_T *ht;
9035 hashitem_T *hi;
9036 dict_T *d = NULL;
9037 typval_T save_val;
9038 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009039 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009040 int todo;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009041 char_u *msg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009042 int save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009043
9044 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009045 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009046 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009047 if ((l = argvars[0].vval.v_list) == NULL
9048 || (map && tv_check_lock(l->lv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009049 return;
9050 }
9051 else if (argvars[0].v_type == VAR_DICT)
9052 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009053 if ((d = argvars[0].vval.v_dict) == NULL
9054 || (map && tv_check_lock(d->dv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009055 return;
9056 }
9057 else
9058 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009059 EMSG2(_(e_listdictarg), msg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009060 return;
9061 }
9062
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009063 expr = get_tv_string_buf_chk(&argvars[1], buf);
9064 /* On type errors, the preceding call has already displayed an error
9065 * message. Avoid a misleading error message for an empty string that
9066 * was not passed as argument. */
9067 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009068 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009069 prepare_vimvar(VV_VAL, &save_val);
9070 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009071
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009072 /* We reset "did_emsg" to be able to detect whether an error
9073 * occurred during evaluation of the expression. */
9074 save_did_emsg = did_emsg;
9075 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009076
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009077 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009078 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009079 prepare_vimvar(VV_KEY, &save_key);
9080 vimvars[VV_KEY].vv_type = VAR_STRING;
9081
9082 ht = &d->dv_hashtab;
9083 hash_lock(ht);
9084 todo = ht->ht_used;
9085 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009086 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009087 if (!HASHITEM_EMPTY(hi))
9088 {
9089 --todo;
9090 di = HI2DI(hi);
9091 if (tv_check_lock(di->di_tv.v_lock, msg))
9092 break;
9093 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009094 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009095 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009096 break;
9097 if (!map && rem)
9098 dictitem_remove(d, di);
9099 clear_tv(&vimvars[VV_KEY].vv_tv);
9100 }
9101 }
9102 hash_unlock(ht);
9103
9104 restore_vimvar(VV_KEY, &save_key);
9105 }
9106 else
9107 {
9108 for (li = l->lv_first; li != NULL; li = nli)
9109 {
9110 if (tv_check_lock(li->li_tv.v_lock, msg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009111 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009112 nli = li->li_next;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009113 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009114 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009115 break;
9116 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009117 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009118 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009119 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009120
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009121 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009122
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009123 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009124 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009125
9126 copy_tv(&argvars[0], rettv);
9127}
9128
9129 static int
9130filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009131 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009132 char_u *expr;
9133 int map;
9134 int *remp;
9135{
Bram Moolenaar33570922005-01-25 22:26:29 +00009136 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009137 char_u *s;
9138
Bram Moolenaar33570922005-01-25 22:26:29 +00009139 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009140 s = expr;
9141 if (eval1(&s, &rettv, TRUE) == FAIL)
9142 return FAIL;
9143 if (*s != NUL) /* check for trailing chars after expr */
9144 {
9145 EMSG2(_(e_invexpr2), s);
9146 return FAIL;
9147 }
9148 if (map)
9149 {
9150 /* map(): replace the list item value */
9151 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +00009152 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009153 *tv = rettv;
9154 }
9155 else
9156 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009157 int error = FALSE;
9158
Bram Moolenaare9a41262005-01-15 22:18:47 +00009159 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009160 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009161 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009162 /* On type error, nothing has been removed; return FAIL to stop the
9163 * loop. The error message was given by get_tv_number_chk(). */
9164 if (error)
9165 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009166 }
Bram Moolenaar33570922005-01-25 22:26:29 +00009167 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009168 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009169}
9170
9171/*
9172 * "filter()" function
9173 */
9174 static void
9175f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009176 typval_T *argvars;
9177 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009178{
9179 filter_map(argvars, rettv, FALSE);
9180}
9181
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009182/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009183 * "finddir({fname}[, {path}[, {count}]])" function
9184 */
9185 static void
9186f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009187 typval_T *argvars;
9188 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009189{
9190 findfilendir(argvars, rettv, TRUE);
9191}
9192
9193/*
9194 * "findfile({fname}[, {path}[, {count}]])" function
9195 */
9196 static void
9197f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009198 typval_T *argvars;
9199 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009200{
9201 findfilendir(argvars, rettv, FALSE);
9202}
9203
9204/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009205 * "fnamemodify({fname}, {mods})" function
9206 */
9207 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009208f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009209 typval_T *argvars;
9210 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009211{
9212 char_u *fname;
9213 char_u *mods;
9214 int usedlen = 0;
9215 int len;
9216 char_u *fbuf = NULL;
9217 char_u buf[NUMBUFLEN];
9218
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009219 fname = get_tv_string_chk(&argvars[0]);
9220 mods = get_tv_string_buf_chk(&argvars[1], buf);
9221 if (fname == NULL || mods == NULL)
9222 fname = NULL;
9223 else
9224 {
9225 len = (int)STRLEN(fname);
9226 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
9227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009228
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009229 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009230 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009231 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009232 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009233 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009234 vim_free(fbuf);
9235}
9236
Bram Moolenaar33570922005-01-25 22:26:29 +00009237static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009238
9239/*
9240 * "foldclosed()" function
9241 */
9242 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009243foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00009244 typval_T *argvars;
9245 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246 int end;
9247{
9248#ifdef FEAT_FOLDING
9249 linenr_T lnum;
9250 linenr_T first, last;
9251
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009252 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009253 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9254 {
9255 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
9256 {
9257 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009258 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009259 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009260 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009261 return;
9262 }
9263 }
9264#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009265 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009266}
9267
9268/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009269 * "foldclosed()" function
9270 */
9271 static void
9272f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009273 typval_T *argvars;
9274 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009275{
9276 foldclosed_both(argvars, rettv, FALSE);
9277}
9278
9279/*
9280 * "foldclosedend()" function
9281 */
9282 static void
9283f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009284 typval_T *argvars;
9285 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009286{
9287 foldclosed_both(argvars, rettv, TRUE);
9288}
9289
9290/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009291 * "foldlevel()" function
9292 */
9293 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009294f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009295 typval_T *argvars;
9296 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009297{
9298#ifdef FEAT_FOLDING
9299 linenr_T lnum;
9300
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009301 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009302 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009303 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009304 else
9305#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009306 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009307}
9308
9309/*
9310 * "foldtext()" function
9311 */
9312/*ARGSUSED*/
9313 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009314f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009315 typval_T *argvars;
9316 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009317{
9318#ifdef FEAT_FOLDING
9319 linenr_T lnum;
9320 char_u *s;
9321 char_u *r;
9322 int len;
9323 char *txt;
9324#endif
9325
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009326 rettv->v_type = VAR_STRING;
9327 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009328#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00009329 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
9330 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
9331 <= curbuf->b_ml.ml_line_count
9332 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009333 {
9334 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009335 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
9336 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009337 {
9338 if (!linewhite(lnum))
9339 break;
9340 ++lnum;
9341 }
9342
9343 /* Find interesting text in this line. */
9344 s = skipwhite(ml_get(lnum));
9345 /* skip C comment-start */
9346 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009347 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009348 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009349 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00009350 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009351 {
9352 s = skipwhite(ml_get(lnum + 1));
9353 if (*s == '*')
9354 s = skipwhite(s + 1);
9355 }
9356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009357 txt = _("+-%s%3ld lines: ");
9358 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009359 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009360 + 20 /* for %3ld */
9361 + STRLEN(s))); /* concatenated */
9362 if (r != NULL)
9363 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009364 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
9365 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
9366 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009367 len = (int)STRLEN(r);
9368 STRCAT(r, s);
9369 /* remove 'foldmarker' and 'commentstring' */
9370 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009371 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009372 }
9373 }
9374#endif
9375}
9376
9377/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009378 * "foldtextresult(lnum)" function
9379 */
9380/*ARGSUSED*/
9381 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009382f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009383 typval_T *argvars;
9384 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009385{
9386#ifdef FEAT_FOLDING
9387 linenr_T lnum;
9388 char_u *text;
9389 char_u buf[51];
9390 foldinfo_T foldinfo;
9391 int fold_count;
9392#endif
9393
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009394 rettv->v_type = VAR_STRING;
9395 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009396#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009397 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009398 /* treat illegal types and illegal string values for {lnum} the same */
9399 if (lnum < 0)
9400 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009401 fold_count = foldedCount(curwin, lnum, &foldinfo);
9402 if (fold_count > 0)
9403 {
9404 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
9405 &foldinfo, buf);
9406 if (text == buf)
9407 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009408 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009409 }
9410#endif
9411}
9412
9413/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009414 * "foreground()" function
9415 */
9416/*ARGSUSED*/
9417 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009418f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009419 typval_T *argvars;
9420 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009421{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009422 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009423#ifdef FEAT_GUI
9424 if (gui.in_use)
9425 gui_mch_set_foreground();
9426#else
9427# ifdef WIN32
9428 win32_set_foreground();
9429# endif
9430#endif
9431}
9432
9433/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009434 * "function()" function
9435 */
9436/*ARGSUSED*/
9437 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009438f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009439 typval_T *argvars;
9440 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009441{
9442 char_u *s;
9443
Bram Moolenaara7043832005-01-21 11:56:39 +00009444 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009445 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009446 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009447 EMSG2(_(e_invarg2), s);
9448 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009449 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009450 else
9451 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009452 rettv->vval.v_string = vim_strsave(s);
9453 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009454 }
9455}
9456
9457/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009458 * "garbagecollect()" function
9459 */
9460/*ARGSUSED*/
9461 static void
9462f_garbagecollect(argvars, rettv)
9463 typval_T *argvars;
9464 typval_T *rettv;
9465{
9466 garbage_collect();
9467}
9468
9469/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009470 * "get()" function
9471 */
9472 static void
9473f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009474 typval_T *argvars;
9475 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009476{
Bram Moolenaar33570922005-01-25 22:26:29 +00009477 listitem_T *li;
9478 list_T *l;
9479 dictitem_T *di;
9480 dict_T *d;
9481 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009482
Bram Moolenaare9a41262005-01-15 22:18:47 +00009483 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009484 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009485 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009486 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009487 int error = FALSE;
9488
9489 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9490 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009491 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009492 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009493 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009494 else if (argvars[0].v_type == VAR_DICT)
9495 {
9496 if ((d = argvars[0].vval.v_dict) != NULL)
9497 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009498 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009499 if (di != NULL)
9500 tv = &di->di_tv;
9501 }
9502 }
9503 else
9504 EMSG2(_(e_listdictarg), "get()");
9505
9506 if (tv == NULL)
9507 {
9508 if (argvars[2].v_type == VAR_UNKNOWN)
9509 rettv->vval.v_number = 0;
9510 else
9511 copy_tv(&argvars[2], rettv);
9512 }
9513 else
9514 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009515}
9516
Bram Moolenaar342337a2005-07-21 21:11:17 +00009517static 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 +00009518
9519/*
9520 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +00009521 * Return a range (from start to end) of lines in rettv from the specified
9522 * buffer.
9523 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009524 */
9525 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +00009526get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009527 buf_T *buf;
9528 linenr_T start;
9529 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009530 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009531 typval_T *rettv;
9532{
9533 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009534
Bram Moolenaar342337a2005-07-21 21:11:17 +00009535 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009536 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009537 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009538 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009539 }
9540 else
9541 rettv->vval.v_number = 0;
9542
9543 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
9544 return;
9545
9546 if (!retlist)
9547 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009548 if (start >= 1 && start <= buf->b_ml.ml_line_count)
9549 p = ml_get_buf(buf, start, FALSE);
9550 else
9551 p = (char_u *)"";
9552
9553 rettv->v_type = VAR_STRING;
9554 rettv->vval.v_string = vim_strsave(p);
9555 }
9556 else
9557 {
9558 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009559 return;
9560
9561 if (start < 1)
9562 start = 1;
9563 if (end > buf->b_ml.ml_line_count)
9564 end = buf->b_ml.ml_line_count;
9565 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009566 if (list_append_string(rettv->vval.v_list,
9567 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009568 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009569 }
9570}
9571
9572/*
9573 * "getbufline()" function
9574 */
9575 static void
9576f_getbufline(argvars, rettv)
9577 typval_T *argvars;
9578 typval_T *rettv;
9579{
9580 linenr_T lnum;
9581 linenr_T end;
9582 buf_T *buf;
9583
9584 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9585 ++emsg_off;
9586 buf = get_buf_tv(&argvars[0]);
9587 --emsg_off;
9588
Bram Moolenaar661b1822005-07-28 22:36:45 +00009589 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009590 if (argvars[2].v_type == VAR_UNKNOWN)
9591 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009592 else
Bram Moolenaar661b1822005-07-28 22:36:45 +00009593 end = get_tv_lnum_buf(&argvars[2], buf);
9594
Bram Moolenaar342337a2005-07-21 21:11:17 +00009595 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009596}
9597
Bram Moolenaar0d660222005-01-07 21:51:51 +00009598/*
9599 * "getbufvar()" function
9600 */
9601 static void
9602f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009603 typval_T *argvars;
9604 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009605{
9606 buf_T *buf;
9607 buf_T *save_curbuf;
9608 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009609 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009610
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009611 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9612 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009613 ++emsg_off;
9614 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009615
9616 rettv->v_type = VAR_STRING;
9617 rettv->vval.v_string = NULL;
9618
9619 if (buf != NULL && varname != NULL)
9620 {
9621 if (*varname == '&') /* buffer-local-option */
9622 {
9623 /* set curbuf to be our buf, temporarily */
9624 save_curbuf = curbuf;
9625 curbuf = buf;
9626
9627 get_option_tv(&varname, rettv, TRUE);
9628
9629 /* restore previous notion of curbuf */
9630 curbuf = save_curbuf;
9631 }
9632 else
9633 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009634 if (*varname == NUL)
9635 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9636 * scope prefix before the NUL byte is required by
9637 * find_var_in_ht(). */
9638 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009639 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009640 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009641 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009642 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009643 }
9644 }
9645
9646 --emsg_off;
9647}
9648
9649/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009650 * "getchar()" function
9651 */
9652 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009653f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009654 typval_T *argvars;
9655 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009656{
9657 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009658 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009659
9660 ++no_mapping;
9661 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009662 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009663 /* getchar(): blocking wait. */
9664 n = safe_vgetc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009665 else if (get_tv_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009666 /* getchar(1): only check if char avail */
9667 n = vpeekc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009668 else if (error || vpeekc() == NUL)
9669 /* illegal argument or getchar(0) and no char avail: return zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009670 n = 0;
9671 else
9672 /* getchar(0) and char avail: return char */
9673 n = safe_vgetc();
9674 --no_mapping;
9675 --allow_keys;
9676
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009677 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009678 if (IS_SPECIAL(n) || mod_mask != 0)
9679 {
9680 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9681 int i = 0;
9682
9683 /* Turn a special key into three bytes, plus modifier. */
9684 if (mod_mask != 0)
9685 {
9686 temp[i++] = K_SPECIAL;
9687 temp[i++] = KS_MODIFIER;
9688 temp[i++] = mod_mask;
9689 }
9690 if (IS_SPECIAL(n))
9691 {
9692 temp[i++] = K_SPECIAL;
9693 temp[i++] = K_SECOND(n);
9694 temp[i++] = K_THIRD(n);
9695 }
9696#ifdef FEAT_MBYTE
9697 else if (has_mbyte)
9698 i += (*mb_char2bytes)(n, temp + i);
9699#endif
9700 else
9701 temp[i++] = n;
9702 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009703 rettv->v_type = VAR_STRING;
9704 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705 }
9706}
9707
9708/*
9709 * "getcharmod()" function
9710 */
9711/*ARGSUSED*/
9712 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009713f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009714 typval_T *argvars;
9715 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009716{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009717 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009718}
9719
9720/*
9721 * "getcmdline()" function
9722 */
9723/*ARGSUSED*/
9724 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009725f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009726 typval_T *argvars;
9727 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009728{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009729 rettv->v_type = VAR_STRING;
9730 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009731}
9732
9733/*
9734 * "getcmdpos()" function
9735 */
9736/*ARGSUSED*/
9737 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009738f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009739 typval_T *argvars;
9740 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009741{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009742 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009743}
9744
9745/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00009746 * "getcmdtype()" function
9747 */
9748/*ARGSUSED*/
9749 static void
9750f_getcmdtype(argvars, rettv)
9751 typval_T *argvars;
9752 typval_T *rettv;
9753{
9754 rettv->v_type = VAR_STRING;
9755 rettv->vval.v_string = alloc(2);
9756 if (rettv->vval.v_string != NULL)
9757 {
9758 rettv->vval.v_string[0] = get_cmdline_type();
9759 rettv->vval.v_string[1] = NUL;
9760 }
9761}
9762
9763/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009764 * "getcwd()" function
9765 */
9766/*ARGSUSED*/
9767 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009768f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009769 typval_T *argvars;
9770 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009771{
9772 char_u cwd[MAXPATHL];
9773
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009774 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009776 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009777 else
9778 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009779 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +00009781 if (rettv->vval.v_string != NULL)
9782 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009783#endif
9784 }
9785}
9786
9787/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009788 * "getfontname()" function
9789 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009790/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009791 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009792f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009793 typval_T *argvars;
9794 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009795{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009796 rettv->v_type = VAR_STRING;
9797 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009798#ifdef FEAT_GUI
9799 if (gui.in_use)
9800 {
9801 GuiFont font;
9802 char_u *name = NULL;
9803
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009804 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009805 {
9806 /* Get the "Normal" font. Either the name saved by
9807 * hl_set_font_name() or from the font ID. */
9808 font = gui.norm_font;
9809 name = hl_get_font_name();
9810 }
9811 else
9812 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009813 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009814 if (STRCMP(name, "*") == 0) /* don't use font dialog */
9815 return;
9816 font = gui_mch_get_font(name, FALSE);
9817 if (font == NOFONT)
9818 return; /* Invalid font name, return empty string. */
9819 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009820 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009821 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009822 gui_mch_free_font(font);
9823 }
9824#endif
9825}
9826
9827/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009828 * "getfperm({fname})" function
9829 */
9830 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009831f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009832 typval_T *argvars;
9833 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009834{
9835 char_u *fname;
9836 struct stat st;
9837 char_u *perm = NULL;
9838 char_u flags[] = "rwx";
9839 int i;
9840
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009841 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009842
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009843 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009844 if (mch_stat((char *)fname, &st) >= 0)
9845 {
9846 perm = vim_strsave((char_u *)"---------");
9847 if (perm != NULL)
9848 {
9849 for (i = 0; i < 9; i++)
9850 {
9851 if (st.st_mode & (1 << (8 - i)))
9852 perm[i] = flags[i % 3];
9853 }
9854 }
9855 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009856 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009857}
9858
9859/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009860 * "getfsize({fname})" function
9861 */
9862 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009863f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009864 typval_T *argvars;
9865 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866{
9867 char_u *fname;
9868 struct stat st;
9869
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009870 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009871
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009872 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009873
9874 if (mch_stat((char *)fname, &st) >= 0)
9875 {
9876 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009877 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009878 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009879 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009880 }
9881 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009882 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009883}
9884
9885/*
9886 * "getftime({fname})" function
9887 */
9888 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009889f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009890 typval_T *argvars;
9891 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009892{
9893 char_u *fname;
9894 struct stat st;
9895
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009896 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009897
9898 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009899 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009900 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009901 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009902}
9903
9904/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009905 * "getftype({fname})" function
9906 */
9907 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009908f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009909 typval_T *argvars;
9910 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009911{
9912 char_u *fname;
9913 struct stat st;
9914 char_u *type = NULL;
9915 char *t;
9916
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009917 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009918
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009919 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009920 if (mch_lstat((char *)fname, &st) >= 0)
9921 {
9922#ifdef S_ISREG
9923 if (S_ISREG(st.st_mode))
9924 t = "file";
9925 else if (S_ISDIR(st.st_mode))
9926 t = "dir";
9927# ifdef S_ISLNK
9928 else if (S_ISLNK(st.st_mode))
9929 t = "link";
9930# endif
9931# ifdef S_ISBLK
9932 else if (S_ISBLK(st.st_mode))
9933 t = "bdev";
9934# endif
9935# ifdef S_ISCHR
9936 else if (S_ISCHR(st.st_mode))
9937 t = "cdev";
9938# endif
9939# ifdef S_ISFIFO
9940 else if (S_ISFIFO(st.st_mode))
9941 t = "fifo";
9942# endif
9943# ifdef S_ISSOCK
9944 else if (S_ISSOCK(st.st_mode))
9945 t = "fifo";
9946# endif
9947 else
9948 t = "other";
9949#else
9950# ifdef S_IFMT
9951 switch (st.st_mode & S_IFMT)
9952 {
9953 case S_IFREG: t = "file"; break;
9954 case S_IFDIR: t = "dir"; break;
9955# ifdef S_IFLNK
9956 case S_IFLNK: t = "link"; break;
9957# endif
9958# ifdef S_IFBLK
9959 case S_IFBLK: t = "bdev"; break;
9960# endif
9961# ifdef S_IFCHR
9962 case S_IFCHR: t = "cdev"; break;
9963# endif
9964# ifdef S_IFIFO
9965 case S_IFIFO: t = "fifo"; break;
9966# endif
9967# ifdef S_IFSOCK
9968 case S_IFSOCK: t = "socket"; break;
9969# endif
9970 default: t = "other";
9971 }
9972# else
9973 if (mch_isdir(fname))
9974 t = "dir";
9975 else
9976 t = "file";
9977# endif
9978#endif
9979 type = vim_strsave((char_u *)t);
9980 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009981 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009982}
9983
9984/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009985 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +00009986 */
9987 static void
9988f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009989 typval_T *argvars;
9990 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009991{
9992 linenr_T lnum;
9993 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009994 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009995
9996 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009997 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009998 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009999 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010000 retlist = FALSE;
10001 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010002 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000010003 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000010004 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010005 retlist = TRUE;
10006 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010007
Bram Moolenaar342337a2005-07-21 21:11:17 +000010008 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010009}
10010
10011/*
Bram Moolenaara5525202006-03-02 22:52:09 +000010012 * "getpos(string)" function
10013 */
10014 static void
10015f_getpos(argvars, rettv)
10016 typval_T *argvars;
10017 typval_T *rettv;
10018{
10019 pos_T *fp;
10020 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010021 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010022
10023 if (rettv_list_alloc(rettv) == OK)
10024 {
10025 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010026 fp = var2fpos(&argvars[0], TRUE, &fnum);
10027 if (fnum != -1)
10028 list_append_number(l, (varnumber_T)fnum);
10029 else
10030 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000010031 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
10032 : (varnumber_T)0);
10033 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->col + 1
10034 : (varnumber_T)0);
10035 list_append_number(l,
10036#ifdef FEAT_VIRTUALEDIT
10037 (fp != NULL) ? (varnumber_T)fp->coladd :
10038#endif
10039 (varnumber_T)0);
10040 }
10041 else
10042 rettv->vval.v_number = FALSE;
10043}
10044
10045/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000010046 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000010047 */
Bram Moolenaar280f1262006-01-30 00:14:18 +000010048/*ARGSUSED*/
Bram Moolenaar2641f772005-03-25 21:58:17 +000010049 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000010050f_getqflist(argvars, rettv)
10051 typval_T *argvars;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010052 typval_T *rettv;
10053{
10054#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000010055 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010056#endif
10057
10058 rettv->vval.v_number = FALSE;
10059#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010060 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000010061 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000010062 wp = NULL;
10063 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
10064 {
10065 wp = find_win_by_nr(&argvars[0]);
10066 if (wp == NULL)
10067 return;
10068 }
10069
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010070 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000010071 }
10072#endif
10073}
10074
10075/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010076 * "getreg()" function
10077 */
10078 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010079f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010080 typval_T *argvars;
10081 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010082{
10083 char_u *strregname;
10084 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010085 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010086 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010087
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010088 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010089 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010090 strregname = get_tv_string_chk(&argvars[0]);
10091 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010092 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010093 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010094 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010095 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010096 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010097 regname = (strregname == NULL ? '"' : *strregname);
10098 if (regname == 0)
10099 regname = '"';
10100
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010101 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010102 rettv->vval.v_string = error ? NULL :
10103 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010104}
10105
10106/*
10107 * "getregtype()" function
10108 */
10109 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010110f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010111 typval_T *argvars;
10112 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010113{
10114 char_u *strregname;
10115 int regname;
10116 char_u buf[NUMBUFLEN + 2];
10117 long reglen = 0;
10118
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010119 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010120 {
10121 strregname = get_tv_string_chk(&argvars[0]);
10122 if (strregname == NULL) /* type error; errmsg already given */
10123 {
10124 rettv->v_type = VAR_STRING;
10125 rettv->vval.v_string = NULL;
10126 return;
10127 }
10128 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010129 else
10130 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010131 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010132
10133 regname = (strregname == NULL ? '"' : *strregname);
10134 if (regname == 0)
10135 regname = '"';
10136
10137 buf[0] = NUL;
10138 buf[1] = NUL;
10139 switch (get_reg_type(regname, &reglen))
10140 {
10141 case MLINE: buf[0] = 'V'; break;
10142 case MCHAR: buf[0] = 'v'; break;
10143#ifdef FEAT_VISUAL
10144 case MBLOCK:
10145 buf[0] = Ctrl_V;
10146 sprintf((char *)buf + 1, "%ld", reglen + 1);
10147 break;
10148#endif
10149 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010150 rettv->v_type = VAR_STRING;
10151 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010152}
10153
10154/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010155 * "getwinposx()" function
10156 */
10157/*ARGSUSED*/
10158 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010159f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010160 typval_T *argvars;
10161 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010162{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010163 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010164#ifdef FEAT_GUI
10165 if (gui.in_use)
10166 {
10167 int x, y;
10168
10169 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010170 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010171 }
10172#endif
10173}
10174
10175/*
10176 * "getwinposy()" function
10177 */
10178/*ARGSUSED*/
10179 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010180f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010181 typval_T *argvars;
10182 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010183{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010184 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010185#ifdef FEAT_GUI
10186 if (gui.in_use)
10187 {
10188 int x, y;
10189
10190 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010191 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010192 }
10193#endif
10194}
10195
Bram Moolenaara40058a2005-07-11 22:42:07 +000010196 static win_T *
10197find_win_by_nr(vp)
10198 typval_T *vp;
10199{
10200#ifdef FEAT_WINDOWS
10201 win_T *wp;
10202#endif
10203 int nr;
10204
10205 nr = get_tv_number_chk(vp, NULL);
10206
10207#ifdef FEAT_WINDOWS
10208 if (nr < 0)
10209 return NULL;
10210 if (nr == 0)
10211 return curwin;
10212
10213 for (wp = firstwin; wp != NULL; wp = wp->w_next)
10214 if (--nr <= 0)
10215 break;
10216 return wp;
10217#else
10218 if (nr == 0 || nr == 1)
10219 return curwin;
10220 return NULL;
10221#endif
10222}
10223
Bram Moolenaar071d4272004-06-13 20:20:40 +000010224/*
10225 * "getwinvar()" function
10226 */
10227 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010228f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010229 typval_T *argvars;
10230 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010231{
10232 win_T *win, *oldcurwin;
10233 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010234 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010235
Bram Moolenaar071d4272004-06-13 20:20:40 +000010236 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010237 varname = get_tv_string_chk(&argvars[1]);
10238 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010239
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010240 rettv->v_type = VAR_STRING;
10241 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010242
10243 if (win != NULL && varname != NULL)
10244 {
10245 if (*varname == '&') /* window-local-option */
10246 {
Bram Moolenaarc0761132005-03-18 20:30:32 +000010247 /* Set curwin to be our win, temporarily. Also set curbuf, so
10248 * that we can get buffer-local options. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010249 oldcurwin = curwin;
10250 curwin = win;
Bram Moolenaarc0761132005-03-18 20:30:32 +000010251 curbuf = win->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010252
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010253 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010254
10255 /* restore previous notion of curwin */
10256 curwin = oldcurwin;
Bram Moolenaarc0761132005-03-18 20:30:32 +000010257 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010258 }
10259 else
10260 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010261 if (*varname == NUL)
10262 /* let getwinvar({nr}, "") return the "w:" dictionary. The
10263 * scope prefix before the NUL byte is required by
10264 * find_var_in_ht(). */
10265 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010266 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010267 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010268 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010269 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010270 }
10271 }
10272
10273 --emsg_off;
10274}
10275
10276/*
10277 * "glob()" function
10278 */
10279 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010280f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010281 typval_T *argvars;
10282 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010283{
10284 expand_T xpc;
10285
10286 ExpandInit(&xpc);
10287 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010288 rettv->v_type = VAR_STRING;
10289 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010290 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
10291 ExpandCleanup(&xpc);
10292}
10293
10294/*
10295 * "globpath()" function
10296 */
10297 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010298f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010299 typval_T *argvars;
10300 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010301{
10302 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010303 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010304
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010305 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010306 if (file == NULL)
10307 rettv->vval.v_string = NULL;
10308 else
10309 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010310}
10311
10312/*
10313 * "has()" function
10314 */
10315 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010316f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010317 typval_T *argvars;
10318 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010319{
10320 int i;
10321 char_u *name;
10322 int n = FALSE;
10323 static char *(has_list[]) =
10324 {
10325#ifdef AMIGA
10326 "amiga",
10327# ifdef FEAT_ARP
10328 "arp",
10329# endif
10330#endif
10331#ifdef __BEOS__
10332 "beos",
10333#endif
10334#ifdef MSDOS
10335# ifdef DJGPP
10336 "dos32",
10337# else
10338 "dos16",
10339# endif
10340#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000010341#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010342 "mac",
10343#endif
10344#if defined(MACOS_X_UNIX)
10345 "macunix",
10346#endif
10347#ifdef OS2
10348 "os2",
10349#endif
10350#ifdef __QNX__
10351 "qnx",
10352#endif
10353#ifdef RISCOS
10354 "riscos",
10355#endif
10356#ifdef UNIX
10357 "unix",
10358#endif
10359#ifdef VMS
10360 "vms",
10361#endif
10362#ifdef WIN16
10363 "win16",
10364#endif
10365#ifdef WIN32
10366 "win32",
10367#endif
10368#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
10369 "win32unix",
10370#endif
10371#ifdef WIN64
10372 "win64",
10373#endif
10374#ifdef EBCDIC
10375 "ebcdic",
10376#endif
10377#ifndef CASE_INSENSITIVE_FILENAME
10378 "fname_case",
10379#endif
10380#ifdef FEAT_ARABIC
10381 "arabic",
10382#endif
10383#ifdef FEAT_AUTOCMD
10384 "autocmd",
10385#endif
10386#ifdef FEAT_BEVAL
10387 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000010388# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
10389 "balloon_multiline",
10390# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010391#endif
10392#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
10393 "builtin_terms",
10394# ifdef ALL_BUILTIN_TCAPS
10395 "all_builtin_terms",
10396# endif
10397#endif
10398#ifdef FEAT_BYTEOFF
10399 "byte_offset",
10400#endif
10401#ifdef FEAT_CINDENT
10402 "cindent",
10403#endif
10404#ifdef FEAT_CLIENTSERVER
10405 "clientserver",
10406#endif
10407#ifdef FEAT_CLIPBOARD
10408 "clipboard",
10409#endif
10410#ifdef FEAT_CMDL_COMPL
10411 "cmdline_compl",
10412#endif
10413#ifdef FEAT_CMDHIST
10414 "cmdline_hist",
10415#endif
10416#ifdef FEAT_COMMENTS
10417 "comments",
10418#endif
10419#ifdef FEAT_CRYPT
10420 "cryptv",
10421#endif
10422#ifdef FEAT_CSCOPE
10423 "cscope",
10424#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010425#ifdef CURSOR_SHAPE
10426 "cursorshape",
10427#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010428#ifdef DEBUG
10429 "debug",
10430#endif
10431#ifdef FEAT_CON_DIALOG
10432 "dialog_con",
10433#endif
10434#ifdef FEAT_GUI_DIALOG
10435 "dialog_gui",
10436#endif
10437#ifdef FEAT_DIFF
10438 "diff",
10439#endif
10440#ifdef FEAT_DIGRAPHS
10441 "digraphs",
10442#endif
10443#ifdef FEAT_DND
10444 "dnd",
10445#endif
10446#ifdef FEAT_EMACS_TAGS
10447 "emacs_tags",
10448#endif
10449 "eval", /* always present, of course! */
10450#ifdef FEAT_EX_EXTRA
10451 "ex_extra",
10452#endif
10453#ifdef FEAT_SEARCH_EXTRA
10454 "extra_search",
10455#endif
10456#ifdef FEAT_FKMAP
10457 "farsi",
10458#endif
10459#ifdef FEAT_SEARCHPATH
10460 "file_in_path",
10461#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010462#if defined(UNIX) && !defined(USE_SYSTEM)
10463 "filterpipe",
10464#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010465#ifdef FEAT_FIND_ID
10466 "find_in_path",
10467#endif
10468#ifdef FEAT_FOLDING
10469 "folding",
10470#endif
10471#ifdef FEAT_FOOTER
10472 "footer",
10473#endif
10474#if !defined(USE_SYSTEM) && defined(UNIX)
10475 "fork",
10476#endif
10477#ifdef FEAT_GETTEXT
10478 "gettext",
10479#endif
10480#ifdef FEAT_GUI
10481 "gui",
10482#endif
10483#ifdef FEAT_GUI_ATHENA
10484# ifdef FEAT_GUI_NEXTAW
10485 "gui_neXtaw",
10486# else
10487 "gui_athena",
10488# endif
10489#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010490#ifdef FEAT_GUI_GTK
10491 "gui_gtk",
10492# ifdef HAVE_GTK2
10493 "gui_gtk2",
10494# endif
10495#endif
10496#ifdef FEAT_GUI_MAC
10497 "gui_mac",
10498#endif
10499#ifdef FEAT_GUI_MOTIF
10500 "gui_motif",
10501#endif
10502#ifdef FEAT_GUI_PHOTON
10503 "gui_photon",
10504#endif
10505#ifdef FEAT_GUI_W16
10506 "gui_win16",
10507#endif
10508#ifdef FEAT_GUI_W32
10509 "gui_win32",
10510#endif
10511#ifdef FEAT_HANGULIN
10512 "hangul_input",
10513#endif
10514#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10515 "iconv",
10516#endif
10517#ifdef FEAT_INS_EXPAND
10518 "insert_expand",
10519#endif
10520#ifdef FEAT_JUMPLIST
10521 "jumplist",
10522#endif
10523#ifdef FEAT_KEYMAP
10524 "keymap",
10525#endif
10526#ifdef FEAT_LANGMAP
10527 "langmap",
10528#endif
10529#ifdef FEAT_LIBCALL
10530 "libcall",
10531#endif
10532#ifdef FEAT_LINEBREAK
10533 "linebreak",
10534#endif
10535#ifdef FEAT_LISP
10536 "lispindent",
10537#endif
10538#ifdef FEAT_LISTCMDS
10539 "listcmds",
10540#endif
10541#ifdef FEAT_LOCALMAP
10542 "localmap",
10543#endif
10544#ifdef FEAT_MENU
10545 "menu",
10546#endif
10547#ifdef FEAT_SESSION
10548 "mksession",
10549#endif
10550#ifdef FEAT_MODIFY_FNAME
10551 "modify_fname",
10552#endif
10553#ifdef FEAT_MOUSE
10554 "mouse",
10555#endif
10556#ifdef FEAT_MOUSESHAPE
10557 "mouseshape",
10558#endif
10559#if defined(UNIX) || defined(VMS)
10560# ifdef FEAT_MOUSE_DEC
10561 "mouse_dec",
10562# endif
10563# ifdef FEAT_MOUSE_GPM
10564 "mouse_gpm",
10565# endif
10566# ifdef FEAT_MOUSE_JSB
10567 "mouse_jsbterm",
10568# endif
10569# ifdef FEAT_MOUSE_NET
10570 "mouse_netterm",
10571# endif
10572# ifdef FEAT_MOUSE_PTERM
10573 "mouse_pterm",
10574# endif
10575# ifdef FEAT_MOUSE_XTERM
10576 "mouse_xterm",
10577# endif
10578#endif
10579#ifdef FEAT_MBYTE
10580 "multi_byte",
10581#endif
10582#ifdef FEAT_MBYTE_IME
10583 "multi_byte_ime",
10584#endif
10585#ifdef FEAT_MULTI_LANG
10586 "multi_lang",
10587#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010588#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010589#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010590 "mzscheme",
10591#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010592#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010593#ifdef FEAT_OLE
10594 "ole",
10595#endif
10596#ifdef FEAT_OSFILETYPE
10597 "osfiletype",
10598#endif
10599#ifdef FEAT_PATH_EXTRA
10600 "path_extra",
10601#endif
10602#ifdef FEAT_PERL
10603#ifndef DYNAMIC_PERL
10604 "perl",
10605#endif
10606#endif
10607#ifdef FEAT_PYTHON
10608#ifndef DYNAMIC_PYTHON
10609 "python",
10610#endif
10611#endif
10612#ifdef FEAT_POSTSCRIPT
10613 "postscript",
10614#endif
10615#ifdef FEAT_PRINTER
10616 "printer",
10617#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000010618#ifdef FEAT_PROFILE
10619 "profile",
10620#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000010621#ifdef FEAT_RELTIME
10622 "reltime",
10623#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010624#ifdef FEAT_QUICKFIX
10625 "quickfix",
10626#endif
10627#ifdef FEAT_RIGHTLEFT
10628 "rightleft",
10629#endif
10630#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
10631 "ruby",
10632#endif
10633#ifdef FEAT_SCROLLBIND
10634 "scrollbind",
10635#endif
10636#ifdef FEAT_CMDL_INFO
10637 "showcmd",
10638 "cmdline_info",
10639#endif
10640#ifdef FEAT_SIGNS
10641 "signs",
10642#endif
10643#ifdef FEAT_SMARTINDENT
10644 "smartindent",
10645#endif
10646#ifdef FEAT_SNIFF
10647 "sniff",
10648#endif
10649#ifdef FEAT_STL_OPT
10650 "statusline",
10651#endif
10652#ifdef FEAT_SUN_WORKSHOP
10653 "sun_workshop",
10654#endif
10655#ifdef FEAT_NETBEANS_INTG
10656 "netbeans_intg",
10657#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000010658#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010659 "spell",
10660#endif
10661#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010662 "syntax",
10663#endif
10664#if defined(USE_SYSTEM) || !defined(UNIX)
10665 "system",
10666#endif
10667#ifdef FEAT_TAG_BINS
10668 "tag_binary",
10669#endif
10670#ifdef FEAT_TAG_OLDSTATIC
10671 "tag_old_static",
10672#endif
10673#ifdef FEAT_TAG_ANYWHITE
10674 "tag_any_white",
10675#endif
10676#ifdef FEAT_TCL
10677# ifndef DYNAMIC_TCL
10678 "tcl",
10679# endif
10680#endif
10681#ifdef TERMINFO
10682 "terminfo",
10683#endif
10684#ifdef FEAT_TERMRESPONSE
10685 "termresponse",
10686#endif
10687#ifdef FEAT_TEXTOBJ
10688 "textobjects",
10689#endif
10690#ifdef HAVE_TGETENT
10691 "tgetent",
10692#endif
10693#ifdef FEAT_TITLE
10694 "title",
10695#endif
10696#ifdef FEAT_TOOLBAR
10697 "toolbar",
10698#endif
10699#ifdef FEAT_USR_CMDS
10700 "user-commands", /* was accidentally included in 5.4 */
10701 "user_commands",
10702#endif
10703#ifdef FEAT_VIMINFO
10704 "viminfo",
10705#endif
10706#ifdef FEAT_VERTSPLIT
10707 "vertsplit",
10708#endif
10709#ifdef FEAT_VIRTUALEDIT
10710 "virtualedit",
10711#endif
10712#ifdef FEAT_VISUAL
10713 "visual",
10714#endif
10715#ifdef FEAT_VISUALEXTRA
10716 "visualextra",
10717#endif
10718#ifdef FEAT_VREPLACE
10719 "vreplace",
10720#endif
10721#ifdef FEAT_WILDIGN
10722 "wildignore",
10723#endif
10724#ifdef FEAT_WILDMENU
10725 "wildmenu",
10726#endif
10727#ifdef FEAT_WINDOWS
10728 "windows",
10729#endif
10730#ifdef FEAT_WAK
10731 "winaltkeys",
10732#endif
10733#ifdef FEAT_WRITEBACKUP
10734 "writebackup",
10735#endif
10736#ifdef FEAT_XIM
10737 "xim",
10738#endif
10739#ifdef FEAT_XFONTSET
10740 "xfontset",
10741#endif
10742#ifdef USE_XSMP
10743 "xsmp",
10744#endif
10745#ifdef USE_XSMP_INTERACT
10746 "xsmp_interact",
10747#endif
10748#ifdef FEAT_XCLIPBOARD
10749 "xterm_clipboard",
10750#endif
10751#ifdef FEAT_XTERM_SAVE
10752 "xterm_save",
10753#endif
10754#if defined(UNIX) && defined(FEAT_X11)
10755 "X11",
10756#endif
10757 NULL
10758 };
10759
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010760 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010761 for (i = 0; has_list[i] != NULL; ++i)
10762 if (STRICMP(name, has_list[i]) == 0)
10763 {
10764 n = TRUE;
10765 break;
10766 }
10767
10768 if (n == FALSE)
10769 {
10770 if (STRNICMP(name, "patch", 5) == 0)
10771 n = has_patch(atoi((char *)name + 5));
10772 else if (STRICMP(name, "vim_starting") == 0)
10773 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010774#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
10775 else if (STRICMP(name, "balloon_multiline") == 0)
10776 n = multiline_balloon_available();
10777#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010778#ifdef DYNAMIC_TCL
10779 else if (STRICMP(name, "tcl") == 0)
10780 n = tcl_enabled(FALSE);
10781#endif
10782#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
10783 else if (STRICMP(name, "iconv") == 0)
10784 n = iconv_enabled(FALSE);
10785#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010786#ifdef DYNAMIC_MZSCHEME
10787 else if (STRICMP(name, "mzscheme") == 0)
10788 n = mzscheme_enabled(FALSE);
10789#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010790#ifdef DYNAMIC_RUBY
10791 else if (STRICMP(name, "ruby") == 0)
10792 n = ruby_enabled(FALSE);
10793#endif
10794#ifdef DYNAMIC_PYTHON
10795 else if (STRICMP(name, "python") == 0)
10796 n = python_enabled(FALSE);
10797#endif
10798#ifdef DYNAMIC_PERL
10799 else if (STRICMP(name, "perl") == 0)
10800 n = perl_enabled(FALSE);
10801#endif
10802#ifdef FEAT_GUI
10803 else if (STRICMP(name, "gui_running") == 0)
10804 n = (gui.in_use || gui.starting);
10805# ifdef FEAT_GUI_W32
10806 else if (STRICMP(name, "gui_win32s") == 0)
10807 n = gui_is_win32s();
10808# endif
10809# ifdef FEAT_BROWSE
10810 else if (STRICMP(name, "browse") == 0)
10811 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
10812# endif
10813#endif
10814#ifdef FEAT_SYN_HL
10815 else if (STRICMP(name, "syntax_items") == 0)
10816 n = syntax_present(curbuf);
10817#endif
10818#if defined(WIN3264)
10819 else if (STRICMP(name, "win95") == 0)
10820 n = mch_windows95();
10821#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000010822#ifdef FEAT_NETBEANS_INTG
10823 else if (STRICMP(name, "netbeans_enabled") == 0)
10824 n = usingNetbeans;
10825#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010826 }
10827
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010828 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010829}
10830
10831/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000010832 * "has_key()" function
10833 */
10834 static void
10835f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010836 typval_T *argvars;
10837 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010838{
10839 rettv->vval.v_number = 0;
10840 if (argvars[0].v_type != VAR_DICT)
10841 {
10842 EMSG(_(e_dictreq));
10843 return;
10844 }
10845 if (argvars[0].vval.v_dict == NULL)
10846 return;
10847
10848 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010849 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010850}
10851
10852/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010853 * "hasmapto()" function
10854 */
10855 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010856f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010857 typval_T *argvars;
10858 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010859{
10860 char_u *name;
10861 char_u *mode;
10862 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000010863 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010864
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010865 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010866 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010867 mode = (char_u *)"nvo";
10868 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000010869 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010870 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000010871 if (argvars[2].v_type != VAR_UNKNOWN)
10872 abbr = get_tv_number(&argvars[2]);
10873 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010874
Bram Moolenaar2c932302006-03-18 21:42:09 +000010875 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010876 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010877 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010878 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010879}
10880
10881/*
10882 * "histadd()" function
10883 */
10884/*ARGSUSED*/
10885 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010886f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010887 typval_T *argvars;
10888 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010889{
10890#ifdef FEAT_CMDHIST
10891 int histype;
10892 char_u *str;
10893 char_u buf[NUMBUFLEN];
10894#endif
10895
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010896 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010897 if (check_restricted() || check_secure())
10898 return;
10899#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010900 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10901 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010902 if (histype >= 0)
10903 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010904 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010905 if (*str != NUL)
10906 {
10907 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010908 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010909 return;
10910 }
10911 }
10912#endif
10913}
10914
10915/*
10916 * "histdel()" function
10917 */
10918/*ARGSUSED*/
10919 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010920f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010921 typval_T *argvars;
10922 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010923{
10924#ifdef FEAT_CMDHIST
10925 int n;
10926 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010927 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010928
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010929 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10930 if (str == NULL)
10931 n = 0;
10932 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010933 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010934 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010935 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010936 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010937 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010938 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010939 else
10940 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010941 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010942 get_tv_string_buf(&argvars[1], buf));
10943 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010944#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010945 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010946#endif
10947}
10948
10949/*
10950 * "histget()" function
10951 */
10952/*ARGSUSED*/
10953 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010954f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010955 typval_T *argvars;
10956 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010957{
10958#ifdef FEAT_CMDHIST
10959 int type;
10960 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010961 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010962
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010963 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10964 if (str == NULL)
10965 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010966 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010967 {
10968 type = get_histtype(str);
10969 if (argvars[1].v_type == VAR_UNKNOWN)
10970 idx = get_history_idx(type);
10971 else
10972 idx = (int)get_tv_number_chk(&argvars[1], NULL);
10973 /* -1 on type error */
10974 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
10975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010976#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010977 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010978#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010979 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010980}
10981
10982/*
10983 * "histnr()" function
10984 */
10985/*ARGSUSED*/
10986 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010987f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010988 typval_T *argvars;
10989 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010990{
10991 int i;
10992
10993#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010994 char_u *history = get_tv_string_chk(&argvars[0]);
10995
10996 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010997 if (i >= HIST_CMD && i < HIST_COUNT)
10998 i = get_history_idx(i);
10999 else
11000#endif
11001 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011002 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011003}
11004
11005/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011006 * "highlightID(name)" function
11007 */
11008 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011009f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011010 typval_T *argvars;
11011 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011012{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011013 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011014}
11015
11016/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011017 * "highlight_exists()" function
11018 */
11019 static void
11020f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011021 typval_T *argvars;
11022 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011023{
11024 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
11025}
11026
11027/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011028 * "hostname()" function
11029 */
11030/*ARGSUSED*/
11031 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011032f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011033 typval_T *argvars;
11034 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011035{
11036 char_u hostname[256];
11037
11038 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011039 rettv->v_type = VAR_STRING;
11040 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011041}
11042
11043/*
11044 * iconv() function
11045 */
11046/*ARGSUSED*/
11047 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011048f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011049 typval_T *argvars;
11050 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011051{
11052#ifdef FEAT_MBYTE
11053 char_u buf1[NUMBUFLEN];
11054 char_u buf2[NUMBUFLEN];
11055 char_u *from, *to, *str;
11056 vimconv_T vimconv;
11057#endif
11058
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011059 rettv->v_type = VAR_STRING;
11060 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011061
11062#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011063 str = get_tv_string(&argvars[0]);
11064 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
11065 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011066 vimconv.vc_type = CONV_NONE;
11067 convert_setup(&vimconv, from, to);
11068
11069 /* If the encodings are equal, no conversion needed. */
11070 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011071 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011072 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011073 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011074
11075 convert_setup(&vimconv, NULL, NULL);
11076 vim_free(from);
11077 vim_free(to);
11078#endif
11079}
11080
11081/*
11082 * "indent()" function
11083 */
11084 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011085f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011086 typval_T *argvars;
11087 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011088{
11089 linenr_T lnum;
11090
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011091 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011092 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011093 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011094 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011095 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011096}
11097
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011098/*
11099 * "index()" function
11100 */
11101 static void
11102f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011103 typval_T *argvars;
11104 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011105{
Bram Moolenaar33570922005-01-25 22:26:29 +000011106 list_T *l;
11107 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011108 long idx = 0;
11109 int ic = FALSE;
11110
11111 rettv->vval.v_number = -1;
11112 if (argvars[0].v_type != VAR_LIST)
11113 {
11114 EMSG(_(e_listreq));
11115 return;
11116 }
11117 l = argvars[0].vval.v_list;
11118 if (l != NULL)
11119 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011120 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011121 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011122 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011123 int error = FALSE;
11124
Bram Moolenaar758711c2005-02-02 23:11:38 +000011125 /* Start at specified item. Use the cached index that list_find()
11126 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011127 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000011128 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011129 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011130 ic = get_tv_number_chk(&argvars[3], &error);
11131 if (error)
11132 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011133 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011134
Bram Moolenaar758711c2005-02-02 23:11:38 +000011135 for ( ; item != NULL; item = item->li_next, ++idx)
11136 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011137 {
11138 rettv->vval.v_number = idx;
11139 break;
11140 }
11141 }
11142}
11143
Bram Moolenaar071d4272004-06-13 20:20:40 +000011144static int inputsecret_flag = 0;
11145
11146/*
11147 * "input()" function
11148 * Also handles inputsecret() when inputsecret is set.
11149 */
11150 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011151f_input(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011152 typval_T *argvars;
11153 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011154{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011155 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011156 char_u *p = NULL;
11157 int c;
11158 char_u buf[NUMBUFLEN];
11159 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011160 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011161 int xp_type = EXPAND_NOTHING;
11162 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011163
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011164 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011165
11166#ifdef NO_CONSOLE_INPUT
11167 /* While starting up, there is no place to enter text. */
11168 if (no_console_input())
11169 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011170 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011171 return;
11172 }
11173#endif
11174
11175 cmd_silent = FALSE; /* Want to see the prompt. */
11176 if (prompt != NULL)
11177 {
11178 /* Only the part of the message after the last NL is considered as
11179 * prompt for the command line */
11180 p = vim_strrchr(prompt, '\n');
11181 if (p == NULL)
11182 p = prompt;
11183 else
11184 {
11185 ++p;
11186 c = *p;
11187 *p = NUL;
11188 msg_start();
11189 msg_clr_eos();
11190 msg_puts_attr(prompt, echo_attr);
11191 msg_didout = FALSE;
11192 msg_starthere();
11193 *p = c;
11194 }
11195 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011196
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011197 if (argvars[1].v_type != VAR_UNKNOWN)
11198 {
11199 defstr = get_tv_string_buf_chk(&argvars[1], buf);
11200 if (defstr != NULL)
11201 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011202
Bram Moolenaar4463f292005-09-25 22:20:24 +000011203 if (argvars[2].v_type != VAR_UNKNOWN)
11204 {
11205 char_u *xp_name;
11206 int xp_namelen;
11207 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011208
Bram Moolenaar4463f292005-09-25 22:20:24 +000011209 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011210
Bram Moolenaar4463f292005-09-25 22:20:24 +000011211 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
11212 if (xp_name == NULL)
11213 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011214
Bram Moolenaar4463f292005-09-25 22:20:24 +000011215 xp_namelen = STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011216
Bram Moolenaar4463f292005-09-25 22:20:24 +000011217 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
11218 &xp_arg) == FAIL)
11219 return;
11220 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011221 }
11222
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011223 if (defstr != NULL)
11224 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011225 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
11226 xp_type, xp_arg);
11227
11228 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011229
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011230 /* since the user typed this, no need to wait for return */
11231 need_wait_return = FALSE;
11232 msg_didout = FALSE;
11233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011234 cmd_silent = cmd_silent_save;
11235}
11236
11237/*
11238 * "inputdialog()" function
11239 */
11240 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011241f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011242 typval_T *argvars;
11243 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011244{
11245#if defined(FEAT_GUI_TEXTDIALOG)
11246 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
11247 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
11248 {
11249 char_u *message;
11250 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011251 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000011252
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011253 message = get_tv_string_chk(&argvars[0]);
11254 if (argvars[1].v_type != VAR_UNKNOWN
11255 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000011256 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011257 else
11258 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011259 if (message != NULL && defstr != NULL
11260 && do_dialog(VIM_QUESTION, NULL, message,
11261 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011262 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011263 else
11264 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011265 if (message != NULL && defstr != NULL
11266 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011267 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011268 rettv->vval.v_string = vim_strsave(
11269 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011270 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011271 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011272 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011273 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011274 }
11275 else
11276#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011277 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011278}
11279
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011280/*
11281 * "inputlist()" function
11282 */
11283 static void
11284f_inputlist(argvars, rettv)
11285 typval_T *argvars;
11286 typval_T *rettv;
11287{
11288 listitem_T *li;
11289 int selected;
11290 int mouse_used;
11291
11292 rettv->vval.v_number = 0;
11293#ifdef NO_CONSOLE_INPUT
11294 /* While starting up, there is no place to enter text. */
11295 if (no_console_input())
11296 return;
11297#endif
11298 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
11299 {
11300 EMSG2(_(e_listarg), "inputlist()");
11301 return;
11302 }
11303
11304 msg_start();
11305 lines_left = Rows; /* avoid more prompt */
11306 msg_scroll = TRUE;
11307 msg_clr_eos();
11308
11309 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
11310 {
11311 msg_puts(get_tv_string(&li->li_tv));
11312 msg_putchar('\n');
11313 }
11314
11315 /* Ask for choice. */
11316 selected = prompt_for_number(&mouse_used);
11317 if (mouse_used)
11318 selected -= lines_left;
11319
11320 rettv->vval.v_number = selected;
11321}
11322
11323
Bram Moolenaar071d4272004-06-13 20:20:40 +000011324static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
11325
11326/*
11327 * "inputrestore()" function
11328 */
11329/*ARGSUSED*/
11330 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011331f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011332 typval_T *argvars;
11333 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011334{
11335 if (ga_userinput.ga_len > 0)
11336 {
11337 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011338 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
11339 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011340 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011341 }
11342 else if (p_verbose > 1)
11343 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000011344 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011345 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011346 }
11347}
11348
11349/*
11350 * "inputsave()" function
11351 */
11352/*ARGSUSED*/
11353 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011354f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011355 typval_T *argvars;
11356 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011357{
11358 /* Add an entry to the stack of typehead storage. */
11359 if (ga_grow(&ga_userinput, 1) == OK)
11360 {
11361 save_typeahead((tasave_T *)(ga_userinput.ga_data)
11362 + ga_userinput.ga_len);
11363 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011364 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011365 }
11366 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011367 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011368}
11369
11370/*
11371 * "inputsecret()" function
11372 */
11373 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011374f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011375 typval_T *argvars;
11376 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011377{
11378 ++cmdline_star;
11379 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011380 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011381 --cmdline_star;
11382 --inputsecret_flag;
11383}
11384
11385/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011386 * "insert()" function
11387 */
11388 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011389f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011390 typval_T *argvars;
11391 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011392{
11393 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011394 listitem_T *item;
11395 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011396 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011397
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011398 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011399 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011400 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011401 else if ((l = argvars[0].vval.v_list) != NULL
11402 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011403 {
11404 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011405 before = get_tv_number_chk(&argvars[2], &error);
11406 if (error)
11407 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011408
Bram Moolenaar758711c2005-02-02 23:11:38 +000011409 if (before == l->lv_len)
11410 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011411 else
11412 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011413 item = list_find(l, before);
11414 if (item == NULL)
11415 {
11416 EMSGN(_(e_listidx), before);
11417 l = NULL;
11418 }
11419 }
11420 if (l != NULL)
11421 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011422 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011423 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011424 }
11425 }
11426}
11427
11428/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011429 * "isdirectory()" function
11430 */
11431 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011432f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011433 typval_T *argvars;
11434 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011435{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011436 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011437}
11438
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011439/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011440 * "islocked()" function
11441 */
11442 static void
11443f_islocked(argvars, rettv)
11444 typval_T *argvars;
11445 typval_T *rettv;
11446{
11447 lval_T lv;
11448 char_u *end;
11449 dictitem_T *di;
11450
11451 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000011452 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
11453 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011454 if (end != NULL && lv.ll_name != NULL)
11455 {
11456 if (*end != NUL)
11457 EMSG(_(e_trailing));
11458 else
11459 {
11460 if (lv.ll_tv == NULL)
11461 {
11462 if (check_changedtick(lv.ll_name))
11463 rettv->vval.v_number = 1; /* always locked */
11464 else
11465 {
11466 di = find_var(lv.ll_name, NULL);
11467 if (di != NULL)
11468 {
11469 /* Consider a variable locked when:
11470 * 1. the variable itself is locked
11471 * 2. the value of the variable is locked.
11472 * 3. the List or Dict value is locked.
11473 */
11474 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
11475 || tv_islocked(&di->di_tv));
11476 }
11477 }
11478 }
11479 else if (lv.ll_range)
11480 EMSG(_("E745: Range not allowed"));
11481 else if (lv.ll_newkey != NULL)
11482 EMSG2(_(e_dictkey), lv.ll_newkey);
11483 else if (lv.ll_list != NULL)
11484 /* List item. */
11485 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
11486 else
11487 /* Dictionary item. */
11488 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
11489 }
11490 }
11491
11492 clear_lval(&lv);
11493}
11494
Bram Moolenaar33570922005-01-25 22:26:29 +000011495static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011496
11497/*
11498 * Turn a dict into a list:
11499 * "what" == 0: list of keys
11500 * "what" == 1: list of values
11501 * "what" == 2: list of items
11502 */
11503 static void
11504dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000011505 typval_T *argvars;
11506 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011507 int what;
11508{
Bram Moolenaar33570922005-01-25 22:26:29 +000011509 list_T *l2;
11510 dictitem_T *di;
11511 hashitem_T *hi;
11512 listitem_T *li;
11513 listitem_T *li2;
11514 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011515 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011516
11517 rettv->vval.v_number = 0;
11518 if (argvars[0].v_type != VAR_DICT)
11519 {
11520 EMSG(_(e_dictreq));
11521 return;
11522 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011523 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011524 return;
11525
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011526 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011527 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011528
Bram Moolenaar33570922005-01-25 22:26:29 +000011529 todo = d->dv_hashtab.ht_used;
11530 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011531 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011532 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011533 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011534 --todo;
11535 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011536
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011537 li = listitem_alloc();
11538 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011539 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011540 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011541
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011542 if (what == 0)
11543 {
11544 /* keys() */
11545 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011546 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011547 li->li_tv.vval.v_string = vim_strsave(di->di_key);
11548 }
11549 else if (what == 1)
11550 {
11551 /* values() */
11552 copy_tv(&di->di_tv, &li->li_tv);
11553 }
11554 else
11555 {
11556 /* items() */
11557 l2 = list_alloc();
11558 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011559 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011560 li->li_tv.vval.v_list = l2;
11561 if (l2 == NULL)
11562 break;
11563 ++l2->lv_refcount;
11564
11565 li2 = listitem_alloc();
11566 if (li2 == NULL)
11567 break;
11568 list_append(l2, li2);
11569 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011570 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011571 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
11572
11573 li2 = listitem_alloc();
11574 if (li2 == NULL)
11575 break;
11576 list_append(l2, li2);
11577 copy_tv(&di->di_tv, &li2->li_tv);
11578 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011579 }
11580 }
11581}
11582
11583/*
11584 * "items(dict)" function
11585 */
11586 static void
11587f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011588 typval_T *argvars;
11589 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011590{
11591 dict_list(argvars, rettv, 2);
11592}
11593
Bram Moolenaar071d4272004-06-13 20:20:40 +000011594/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011595 * "join()" function
11596 */
11597 static void
11598f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011599 typval_T *argvars;
11600 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011601{
11602 garray_T ga;
11603 char_u *sep;
11604
11605 rettv->vval.v_number = 0;
11606 if (argvars[0].v_type != VAR_LIST)
11607 {
11608 EMSG(_(e_listreq));
11609 return;
11610 }
11611 if (argvars[0].vval.v_list == NULL)
11612 return;
11613 if (argvars[1].v_type == VAR_UNKNOWN)
11614 sep = (char_u *)" ";
11615 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011616 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011617
11618 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011619
11620 if (sep != NULL)
11621 {
11622 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000011623 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011624 ga_append(&ga, NUL);
11625 rettv->vval.v_string = (char_u *)ga.ga_data;
11626 }
11627 else
11628 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011629}
11630
11631/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011632 * "keys()" function
11633 */
11634 static void
11635f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011636 typval_T *argvars;
11637 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011638{
11639 dict_list(argvars, rettv, 0);
11640}
11641
11642/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011643 * "last_buffer_nr()" function.
11644 */
11645/*ARGSUSED*/
11646 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011647f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011648 typval_T *argvars;
11649 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011650{
11651 int n = 0;
11652 buf_T *buf;
11653
11654 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
11655 if (n < buf->b_fnum)
11656 n = buf->b_fnum;
11657
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011658 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011659}
11660
11661/*
11662 * "len()" function
11663 */
11664 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011665f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011666 typval_T *argvars;
11667 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011668{
11669 switch (argvars[0].v_type)
11670 {
11671 case VAR_STRING:
11672 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011673 rettv->vval.v_number = (varnumber_T)STRLEN(
11674 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011675 break;
11676 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011677 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011678 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011679 case VAR_DICT:
11680 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
11681 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011682 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011683 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011684 break;
11685 }
11686}
11687
Bram Moolenaar33570922005-01-25 22:26:29 +000011688static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011689
11690 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011691libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011692 typval_T *argvars;
11693 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011694 int type;
11695{
11696#ifdef FEAT_LIBCALL
11697 char_u *string_in;
11698 char_u **string_result;
11699 int nr_result;
11700#endif
11701
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011702 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011703 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011704 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011705 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011706 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011707
11708 if (check_restricted() || check_secure())
11709 return;
11710
11711#ifdef FEAT_LIBCALL
11712 /* The first two args must be strings, otherwise its meaningless */
11713 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
11714 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011715 string_in = NULL;
11716 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011717 string_in = argvars[2].vval.v_string;
11718 if (type == VAR_NUMBER)
11719 string_result = NULL;
11720 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011721 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011722 if (mch_libcall(argvars[0].vval.v_string,
11723 argvars[1].vval.v_string,
11724 string_in,
11725 argvars[2].vval.v_number,
11726 string_result,
11727 &nr_result) == OK
11728 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011729 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011730 }
11731#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011732}
11733
11734/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011735 * "libcall()" function
11736 */
11737 static void
11738f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011739 typval_T *argvars;
11740 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011741{
11742 libcall_common(argvars, rettv, VAR_STRING);
11743}
11744
11745/*
11746 * "libcallnr()" function
11747 */
11748 static void
11749f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011750 typval_T *argvars;
11751 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011752{
11753 libcall_common(argvars, rettv, VAR_NUMBER);
11754}
11755
11756/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011757 * "line(string)" function
11758 */
11759 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011760f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011761 typval_T *argvars;
11762 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011763{
11764 linenr_T lnum = 0;
11765 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011766 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011767
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011768 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011769 if (fp != NULL)
11770 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011771 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011772}
11773
11774/*
11775 * "line2byte(lnum)" function
11776 */
11777/*ARGSUSED*/
11778 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011779f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011780 typval_T *argvars;
11781 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011782{
11783#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011784 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011785#else
11786 linenr_T lnum;
11787
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011788 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011789 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011790 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011791 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011792 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
11793 if (rettv->vval.v_number >= 0)
11794 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011795#endif
11796}
11797
11798/*
11799 * "lispindent(lnum)" function
11800 */
11801 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011802f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011803 typval_T *argvars;
11804 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011805{
11806#ifdef FEAT_LISP
11807 pos_T pos;
11808 linenr_T lnum;
11809
11810 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011811 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011812 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11813 {
11814 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011815 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011816 curwin->w_cursor = pos;
11817 }
11818 else
11819#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011820 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011821}
11822
11823/*
11824 * "localtime()" function
11825 */
11826/*ARGSUSED*/
11827 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011828f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011829 typval_T *argvars;
11830 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011831{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011832 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011833}
11834
Bram Moolenaar33570922005-01-25 22:26:29 +000011835static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011836
11837 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011838get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000011839 typval_T *argvars;
11840 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011841 int exact;
11842{
11843 char_u *keys;
11844 char_u *which;
11845 char_u buf[NUMBUFLEN];
11846 char_u *keys_buf = NULL;
11847 char_u *rhs;
11848 int mode;
11849 garray_T ga;
Bram Moolenaar2c932302006-03-18 21:42:09 +000011850 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011851
11852 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011853 rettv->v_type = VAR_STRING;
11854 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011855
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011856 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011857 if (*keys == NUL)
11858 return;
11859
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011860 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000011861 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011862 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000011863 if (argvars[2].v_type != VAR_UNKNOWN)
11864 abbr = get_tv_number(&argvars[2]);
11865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011866 else
11867 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011868 if (which == NULL)
11869 return;
11870
Bram Moolenaar071d4272004-06-13 20:20:40 +000011871 mode = get_map_mode(&which, 0);
11872
11873 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
Bram Moolenaar2c932302006-03-18 21:42:09 +000011874 rhs = check_map(keys, mode, exact, FALSE, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011875 vim_free(keys_buf);
11876 if (rhs != NULL)
11877 {
11878 ga_init(&ga);
11879 ga.ga_itemsize = 1;
11880 ga.ga_growsize = 40;
11881
11882 while (*rhs != NUL)
11883 ga_concat(&ga, str2special(&rhs, FALSE));
11884
11885 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011886 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011887 }
11888}
11889
11890/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011891 * "map()" function
11892 */
11893 static void
11894f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011895 typval_T *argvars;
11896 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011897{
11898 filter_map(argvars, rettv, TRUE);
11899}
11900
11901/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011902 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011903 */
11904 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011905f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011906 typval_T *argvars;
11907 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011908{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011909 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011910}
11911
11912/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011913 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011914 */
11915 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011916f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011917 typval_T *argvars;
11918 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011919{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011920 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011921}
11922
Bram Moolenaar33570922005-01-25 22:26:29 +000011923static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011924
11925 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011926find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011927 typval_T *argvars;
11928 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011929 int type;
11930{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011931 char_u *str = NULL;
11932 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011933 char_u *pat;
11934 regmatch_T regmatch;
11935 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011936 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011937 char_u *save_cpo;
11938 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011939 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011940 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011941 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011942 list_T *l = NULL;
11943 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011944 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011945 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011946
11947 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11948 save_cpo = p_cpo;
11949 p_cpo = (char_u *)"";
11950
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011951 rettv->vval.v_number = -1;
11952 if (type == 3)
11953 {
11954 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011955 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011956 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011957 }
11958 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011959 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011960 rettv->v_type = VAR_STRING;
11961 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011962 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011963
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011964 if (argvars[0].v_type == VAR_LIST)
11965 {
11966 if ((l = argvars[0].vval.v_list) == NULL)
11967 goto theend;
11968 li = l->lv_first;
11969 }
11970 else
11971 expr = str = get_tv_string(&argvars[0]);
11972
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011973 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11974 if (pat == NULL)
11975 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011976
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011977 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011978 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011979 int error = FALSE;
11980
11981 start = get_tv_number_chk(&argvars[2], &error);
11982 if (error)
11983 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011984 if (l != NULL)
11985 {
11986 li = list_find(l, start);
11987 if (li == NULL)
11988 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000011989 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011990 }
11991 else
11992 {
11993 if (start < 0)
11994 start = 0;
11995 if (start > (long)STRLEN(str))
11996 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011997 /* When "count" argument is there ignore matches before "start",
11998 * otherwise skip part of the string. Differs when pattern is "^"
11999 * or "\<". */
12000 if (argvars[3].v_type != VAR_UNKNOWN)
12001 startcol = start;
12002 else
12003 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012004 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012005
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012006 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012007 nth = get_tv_number_chk(&argvars[3], &error);
12008 if (error)
12009 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012010 }
12011
12012 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12013 if (regmatch.regprog != NULL)
12014 {
12015 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012016
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012017 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012018 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012019 if (l != NULL)
12020 {
12021 if (li == NULL)
12022 {
12023 match = FALSE;
12024 break;
12025 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012026 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012027 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012028 if (str == NULL)
12029 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012030 }
12031
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012032 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012033
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012034 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012035 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012036 if (l == NULL && !match)
12037 break;
12038
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012039 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012040 if (l != NULL)
12041 {
12042 li = li->li_next;
12043 ++idx;
12044 }
12045 else
12046 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012047#ifdef FEAT_MBYTE
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012048 startcol = regmatch.startp[0]
12049 + (*mb_ptr2len)(regmatch.startp[0]) - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012050#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012051 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012052#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012053 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012054 }
12055
12056 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012057 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012058 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012059 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012060 int i;
12061
12062 /* return list with matched string and submatches */
12063 for (i = 0; i < NSUBEXP; ++i)
12064 {
12065 if (regmatch.endp[i] == NULL)
12066 break;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012067 if (list_append_string(rettv->vval.v_list,
12068 regmatch.startp[i],
12069 (int)(regmatch.endp[i] - regmatch.startp[i]))
12070 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012071 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012072 }
12073 }
12074 else if (type == 2)
12075 {
12076 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012077 if (l != NULL)
12078 copy_tv(&li->li_tv, rettv);
12079 else
12080 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000012081 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012082 }
12083 else if (l != NULL)
12084 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012085 else
12086 {
12087 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012088 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012089 (varnumber_T)(regmatch.startp[0] - str);
12090 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012091 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012092 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012093 rettv->vval.v_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012094 }
12095 }
12096 vim_free(regmatch.regprog);
12097 }
12098
12099theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012100 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012101 p_cpo = save_cpo;
12102}
12103
12104/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012105 * "match()" function
12106 */
12107 static void
12108f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012109 typval_T *argvars;
12110 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012111{
12112 find_some_match(argvars, rettv, 1);
12113}
12114
12115/*
12116 * "matchend()" function
12117 */
12118 static void
12119f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012120 typval_T *argvars;
12121 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012122{
12123 find_some_match(argvars, rettv, 0);
12124}
12125
12126/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012127 * "matchlist()" function
12128 */
12129 static void
12130f_matchlist(argvars, rettv)
12131 typval_T *argvars;
12132 typval_T *rettv;
12133{
12134 find_some_match(argvars, rettv, 3);
12135}
12136
12137/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012138 * "matchstr()" function
12139 */
12140 static void
12141f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012142 typval_T *argvars;
12143 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012144{
12145 find_some_match(argvars, rettv, 2);
12146}
12147
Bram Moolenaar33570922005-01-25 22:26:29 +000012148static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012149
12150 static void
12151max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000012152 typval_T *argvars;
12153 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012154 int domax;
12155{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012156 long n = 0;
12157 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012158 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012159
12160 if (argvars[0].v_type == VAR_LIST)
12161 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012162 list_T *l;
12163 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012164
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012165 l = argvars[0].vval.v_list;
12166 if (l != NULL)
12167 {
12168 li = l->lv_first;
12169 if (li != NULL)
12170 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012171 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012172 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012173 {
12174 li = li->li_next;
12175 if (li == NULL)
12176 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012177 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012178 if (domax ? i > n : i < n)
12179 n = i;
12180 }
12181 }
12182 }
12183 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012184 else if (argvars[0].v_type == VAR_DICT)
12185 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012186 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012187 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000012188 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012189 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012190
12191 d = argvars[0].vval.v_dict;
12192 if (d != NULL)
12193 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012194 todo = d->dv_hashtab.ht_used;
12195 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012196 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012197 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000012198 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012199 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012200 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012201 if (first)
12202 {
12203 n = i;
12204 first = FALSE;
12205 }
12206 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012207 n = i;
12208 }
12209 }
12210 }
12211 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012212 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000012213 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012214 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012215}
12216
12217/*
12218 * "max()" function
12219 */
12220 static void
12221f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012222 typval_T *argvars;
12223 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012224{
12225 max_min(argvars, rettv, TRUE);
12226}
12227
12228/*
12229 * "min()" function
12230 */
12231 static void
12232f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012233 typval_T *argvars;
12234 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012235{
12236 max_min(argvars, rettv, FALSE);
12237}
12238
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012239static int mkdir_recurse __ARGS((char_u *dir, int prot));
12240
12241/*
12242 * Create the directory in which "dir" is located, and higher levels when
12243 * needed.
12244 */
12245 static int
12246mkdir_recurse(dir, prot)
12247 char_u *dir;
12248 int prot;
12249{
12250 char_u *p;
12251 char_u *updir;
12252 int r = FAIL;
12253
12254 /* Get end of directory name in "dir".
12255 * We're done when it's "/" or "c:/". */
12256 p = gettail_sep(dir);
12257 if (p <= get_past_head(dir))
12258 return OK;
12259
12260 /* If the directory exists we're done. Otherwise: create it.*/
12261 updir = vim_strnsave(dir, (int)(p - dir));
12262 if (updir == NULL)
12263 return FAIL;
12264 if (mch_isdir(updir))
12265 r = OK;
12266 else if (mkdir_recurse(updir, prot) == OK)
12267 r = vim_mkdir_emsg(updir, prot);
12268 vim_free(updir);
12269 return r;
12270}
12271
12272#ifdef vim_mkdir
12273/*
12274 * "mkdir()" function
12275 */
12276 static void
12277f_mkdir(argvars, rettv)
12278 typval_T *argvars;
12279 typval_T *rettv;
12280{
12281 char_u *dir;
12282 char_u buf[NUMBUFLEN];
12283 int prot = 0755;
12284
12285 rettv->vval.v_number = FAIL;
12286 if (check_restricted() || check_secure())
12287 return;
12288
12289 dir = get_tv_string_buf(&argvars[0], buf);
12290 if (argvars[1].v_type != VAR_UNKNOWN)
12291 {
12292 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012293 prot = get_tv_number_chk(&argvars[2], NULL);
12294 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012295 mkdir_recurse(dir, prot);
12296 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012297 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012298}
12299#endif
12300
Bram Moolenaar0d660222005-01-07 21:51:51 +000012301/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012302 * "mode()" function
12303 */
12304/*ARGSUSED*/
12305 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012306f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012307 typval_T *argvars;
12308 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012309{
12310 char_u buf[2];
12311
12312#ifdef FEAT_VISUAL
12313 if (VIsual_active)
12314 {
12315 if (VIsual_select)
12316 buf[0] = VIsual_mode + 's' - 'v';
12317 else
12318 buf[0] = VIsual_mode;
12319 }
12320 else
12321#endif
12322 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
12323 buf[0] = 'r';
12324 else if (State & INSERT)
12325 {
12326 if (State & REPLACE_FLAG)
12327 buf[0] = 'R';
12328 else
12329 buf[0] = 'i';
12330 }
12331 else if (State & CMDLINE)
12332 buf[0] = 'c';
12333 else
12334 buf[0] = 'n';
12335
12336 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012337 rettv->vval.v_string = vim_strsave(buf);
12338 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012339}
12340
12341/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012342 * "nextnonblank()" function
12343 */
12344 static void
12345f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012346 typval_T *argvars;
12347 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012348{
12349 linenr_T lnum;
12350
12351 for (lnum = get_tv_lnum(argvars); ; ++lnum)
12352 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012353 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012354 {
12355 lnum = 0;
12356 break;
12357 }
12358 if (*skipwhite(ml_get(lnum)) != NUL)
12359 break;
12360 }
12361 rettv->vval.v_number = lnum;
12362}
12363
12364/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012365 * "nr2char()" function
12366 */
12367 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012368f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012369 typval_T *argvars;
12370 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012371{
12372 char_u buf[NUMBUFLEN];
12373
12374#ifdef FEAT_MBYTE
12375 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012376 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012377 else
12378#endif
12379 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012380 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012381 buf[1] = NUL;
12382 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012383 rettv->v_type = VAR_STRING;
12384 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012385}
12386
12387/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012388 * "prevnonblank()" function
12389 */
12390 static void
12391f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012392 typval_T *argvars;
12393 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012394{
12395 linenr_T lnum;
12396
12397 lnum = get_tv_lnum(argvars);
12398 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
12399 lnum = 0;
12400 else
12401 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
12402 --lnum;
12403 rettv->vval.v_number = lnum;
12404}
12405
Bram Moolenaara6c840d2005-08-22 22:59:46 +000012406#ifdef HAVE_STDARG_H
12407/* This dummy va_list is here because:
12408 * - passing a NULL pointer doesn't work when va_list isn't a pointer
12409 * - locally in the function results in a "used before set" warning
12410 * - using va_start() to initialize it gives "function with fixed args" error */
12411static va_list ap;
12412#endif
12413
Bram Moolenaar8c711452005-01-14 21:53:12 +000012414/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012415 * "printf()" function
12416 */
12417 static void
12418f_printf(argvars, rettv)
12419 typval_T *argvars;
12420 typval_T *rettv;
12421{
12422 rettv->v_type = VAR_STRING;
12423 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000012424#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012425 {
12426 char_u buf[NUMBUFLEN];
12427 int len;
12428 char_u *s;
12429 int saved_did_emsg = did_emsg;
12430 char *fmt;
12431
12432 /* Get the required length, allocate the buffer and do it for real. */
12433 did_emsg = FALSE;
12434 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012435 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012436 if (!did_emsg)
12437 {
12438 s = alloc(len + 1);
12439 if (s != NULL)
12440 {
12441 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012442 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012443 }
12444 }
12445 did_emsg |= saved_did_emsg;
12446 }
12447#endif
12448}
12449
12450/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000012451 * "pumvisible()" function
12452 */
12453/*ARGSUSED*/
12454 static void
12455f_pumvisible(argvars, rettv)
12456 typval_T *argvars;
12457 typval_T *rettv;
12458{
12459 rettv->vval.v_number = 0;
12460#ifdef FEAT_INS_EXPAND
12461 if (pum_visible())
12462 rettv->vval.v_number = 1;
12463#endif
12464}
12465
12466/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012467 * "range()" function
12468 */
12469 static void
12470f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012471 typval_T *argvars;
12472 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012473{
12474 long start;
12475 long end;
12476 long stride = 1;
12477 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012478 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012479
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012480 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012481 if (argvars[1].v_type == VAR_UNKNOWN)
12482 {
12483 end = start - 1;
12484 start = 0;
12485 }
12486 else
12487 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012488 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012489 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012490 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012491 }
12492
12493 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012494 if (error)
12495 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000012496 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012497 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000012498 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012499 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012500 else
12501 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012502 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012503 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012504 if (list_append_number(rettv->vval.v_list,
12505 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012506 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012507 }
12508}
12509
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012510/*
12511 * "readfile()" function
12512 */
12513 static void
12514f_readfile(argvars, rettv)
12515 typval_T *argvars;
12516 typval_T *rettv;
12517{
12518 int binary = FALSE;
12519 char_u *fname;
12520 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012521 listitem_T *li;
12522#define FREAD_SIZE 200 /* optimized for text lines */
12523 char_u buf[FREAD_SIZE];
12524 int readlen; /* size of last fread() */
12525 int buflen; /* nr of valid chars in buf[] */
12526 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
12527 int tolist; /* first byte in buf[] still to be put in list */
12528 int chop; /* how many CR to chop off */
12529 char_u *prev = NULL; /* previously read bytes, if any */
12530 int prevlen = 0; /* length of "prev" if not NULL */
12531 char_u *s;
12532 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012533 long maxline = MAXLNUM;
12534 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012535
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012536 if (argvars[1].v_type != VAR_UNKNOWN)
12537 {
12538 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
12539 binary = TRUE;
12540 if (argvars[2].v_type != VAR_UNKNOWN)
12541 maxline = get_tv_number(&argvars[2]);
12542 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012543
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012544 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012545 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012546
12547 /* Always open the file in binary mode, library functions have a mind of
12548 * their own about CR-LF conversion. */
12549 fname = get_tv_string(&argvars[0]);
12550 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
12551 {
12552 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
12553 return;
12554 }
12555
12556 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012557 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012558 {
12559 readlen = fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
12560 buflen = filtd + readlen;
12561 tolist = 0;
12562 for ( ; filtd < buflen || readlen <= 0; ++filtd)
12563 {
12564 if (buf[filtd] == '\n' || readlen <= 0)
12565 {
12566 /* Only when in binary mode add an empty list item when the
12567 * last line ends in a '\n'. */
12568 if (!binary && readlen == 0 && filtd == 0)
12569 break;
12570
12571 /* Found end-of-line or end-of-file: add a text line to the
12572 * list. */
12573 chop = 0;
12574 if (!binary)
12575 while (filtd - chop - 1 >= tolist
12576 && buf[filtd - chop - 1] == '\r')
12577 ++chop;
12578 len = filtd - tolist - chop;
12579 if (prev == NULL)
12580 s = vim_strnsave(buf + tolist, len);
12581 else
12582 {
12583 s = alloc((unsigned)(prevlen + len + 1));
12584 if (s != NULL)
12585 {
12586 mch_memmove(s, prev, prevlen);
12587 vim_free(prev);
12588 prev = NULL;
12589 mch_memmove(s + prevlen, buf + tolist, len);
12590 s[prevlen + len] = NUL;
12591 }
12592 }
12593 tolist = filtd + 1;
12594
12595 li = listitem_alloc();
12596 if (li == NULL)
12597 {
12598 vim_free(s);
12599 break;
12600 }
12601 li->li_tv.v_type = VAR_STRING;
12602 li->li_tv.v_lock = 0;
12603 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012604 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012605
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012606 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012607 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012608 if (readlen <= 0)
12609 break;
12610 }
12611 else if (buf[filtd] == NUL)
12612 buf[filtd] = '\n';
12613 }
12614 if (readlen <= 0)
12615 break;
12616
12617 if (tolist == 0)
12618 {
12619 /* "buf" is full, need to move text to an allocated buffer */
12620 if (prev == NULL)
12621 {
12622 prev = vim_strnsave(buf, buflen);
12623 prevlen = buflen;
12624 }
12625 else
12626 {
12627 s = alloc((unsigned)(prevlen + buflen));
12628 if (s != NULL)
12629 {
12630 mch_memmove(s, prev, prevlen);
12631 mch_memmove(s + prevlen, buf, buflen);
12632 vim_free(prev);
12633 prev = s;
12634 prevlen += buflen;
12635 }
12636 }
12637 filtd = 0;
12638 }
12639 else
12640 {
12641 mch_memmove(buf, buf + tolist, buflen - tolist);
12642 filtd -= tolist;
12643 }
12644 }
12645
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012646 /*
12647 * For a negative line count use only the lines at the end of the file,
12648 * free the rest.
12649 */
12650 if (maxline < 0)
12651 while (cnt > -maxline)
12652 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012653 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012654 --cnt;
12655 }
12656
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012657 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012658 fclose(fd);
12659}
12660
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012661#if defined(FEAT_RELTIME)
12662static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
12663
12664/*
12665 * Convert a List to proftime_T.
12666 * Return FAIL when there is something wrong.
12667 */
12668 static int
12669list2proftime(arg, tm)
12670 typval_T *arg;
12671 proftime_T *tm;
12672{
12673 long n1, n2;
12674 int error = FALSE;
12675
12676 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
12677 || arg->vval.v_list->lv_len != 2)
12678 return FAIL;
12679 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
12680 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
12681# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000012682 tm->HighPart = n1;
12683 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012684# else
12685 tm->tv_sec = n1;
12686 tm->tv_usec = n2;
12687# endif
12688 return error ? FAIL : OK;
12689}
12690#endif /* FEAT_RELTIME */
12691
12692/*
12693 * "reltime()" function
12694 */
12695 static void
12696f_reltime(argvars, rettv)
12697 typval_T *argvars;
12698 typval_T *rettv;
12699{
12700#ifdef FEAT_RELTIME
12701 proftime_T res;
12702 proftime_T start;
12703
12704 if (argvars[0].v_type == VAR_UNKNOWN)
12705 {
12706 /* No arguments: get current time. */
12707 profile_start(&res);
12708 }
12709 else if (argvars[1].v_type == VAR_UNKNOWN)
12710 {
12711 if (list2proftime(&argvars[0], &res) == FAIL)
12712 return;
12713 profile_end(&res);
12714 }
12715 else
12716 {
12717 /* Two arguments: compute the difference. */
12718 if (list2proftime(&argvars[0], &start) == FAIL
12719 || list2proftime(&argvars[1], &res) == FAIL)
12720 return;
12721 profile_sub(&res, &start);
12722 }
12723
12724 if (rettv_list_alloc(rettv) == OK)
12725 {
12726 long n1, n2;
12727
12728# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000012729 n1 = res.HighPart;
12730 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012731# else
12732 n1 = res.tv_sec;
12733 n2 = res.tv_usec;
12734# endif
12735 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
12736 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
12737 }
12738#endif
12739}
12740
12741/*
12742 * "reltimestr()" function
12743 */
12744 static void
12745f_reltimestr(argvars, rettv)
12746 typval_T *argvars;
12747 typval_T *rettv;
12748{
12749#ifdef FEAT_RELTIME
12750 proftime_T tm;
12751#endif
12752
12753 rettv->v_type = VAR_STRING;
12754 rettv->vval.v_string = NULL;
12755#ifdef FEAT_RELTIME
12756 if (list2proftime(&argvars[0], &tm) == OK)
12757 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
12758#endif
12759}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012760
Bram Moolenaar0d660222005-01-07 21:51:51 +000012761#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
12762static void make_connection __ARGS((void));
12763static int check_connection __ARGS((void));
12764
12765 static void
12766make_connection()
12767{
12768 if (X_DISPLAY == NULL
12769# ifdef FEAT_GUI
12770 && !gui.in_use
12771# endif
12772 )
12773 {
12774 x_force_connect = TRUE;
12775 setup_term_clip();
12776 x_force_connect = FALSE;
12777 }
12778}
12779
12780 static int
12781check_connection()
12782{
12783 make_connection();
12784 if (X_DISPLAY == NULL)
12785 {
12786 EMSG(_("E240: No connection to Vim server"));
12787 return FAIL;
12788 }
12789 return OK;
12790}
12791#endif
12792
12793#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012794static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012795
12796 static void
12797remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000012798 typval_T *argvars;
12799 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012800 int expr;
12801{
12802 char_u *server_name;
12803 char_u *keys;
12804 char_u *r = NULL;
12805 char_u buf[NUMBUFLEN];
12806# ifdef WIN32
12807 HWND w;
12808# else
12809 Window w;
12810# endif
12811
12812 if (check_restricted() || check_secure())
12813 return;
12814
12815# ifdef FEAT_X11
12816 if (check_connection() == FAIL)
12817 return;
12818# endif
12819
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012820 server_name = get_tv_string_chk(&argvars[0]);
12821 if (server_name == NULL)
12822 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012823 keys = get_tv_string_buf(&argvars[1], buf);
12824# ifdef WIN32
12825 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
12826# else
12827 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
12828 < 0)
12829# endif
12830 {
12831 if (r != NULL)
12832 EMSG(r); /* sending worked but evaluation failed */
12833 else
12834 EMSG2(_("E241: Unable to send to %s"), server_name);
12835 return;
12836 }
12837
12838 rettv->vval.v_string = r;
12839
12840 if (argvars[2].v_type != VAR_UNKNOWN)
12841 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012842 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000012843 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012844 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012845
12846 sprintf((char *)str, "0x%x", (unsigned int)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000012847 v.di_tv.v_type = VAR_STRING;
12848 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012849 idvar = get_tv_string_chk(&argvars[2]);
12850 if (idvar != NULL)
12851 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012852 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012853 }
12854}
12855#endif
12856
12857/*
12858 * "remote_expr()" function
12859 */
12860/*ARGSUSED*/
12861 static void
12862f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012863 typval_T *argvars;
12864 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012865{
12866 rettv->v_type = VAR_STRING;
12867 rettv->vval.v_string = NULL;
12868#ifdef FEAT_CLIENTSERVER
12869 remote_common(argvars, rettv, TRUE);
12870#endif
12871}
12872
12873/*
12874 * "remote_foreground()" function
12875 */
12876/*ARGSUSED*/
12877 static void
12878f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012879 typval_T *argvars;
12880 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012881{
12882 rettv->vval.v_number = 0;
12883#ifdef FEAT_CLIENTSERVER
12884# ifdef WIN32
12885 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012886 {
12887 char_u *server_name = get_tv_string_chk(&argvars[0]);
12888
12889 if (server_name != NULL)
12890 serverForeground(server_name);
12891 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012892# else
12893 /* Send a foreground() expression to the server. */
12894 argvars[1].v_type = VAR_STRING;
12895 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
12896 argvars[2].v_type = VAR_UNKNOWN;
12897 remote_common(argvars, rettv, TRUE);
12898 vim_free(argvars[1].vval.v_string);
12899# endif
12900#endif
12901}
12902
12903/*ARGSUSED*/
12904 static void
12905f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012906 typval_T *argvars;
12907 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012908{
12909#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012910 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012911 char_u *s = NULL;
12912# ifdef WIN32
12913 int n = 0;
12914# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012915 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012916
12917 if (check_restricted() || check_secure())
12918 {
12919 rettv->vval.v_number = -1;
12920 return;
12921 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012922 serverid = get_tv_string_chk(&argvars[0]);
12923 if (serverid == NULL)
12924 {
12925 rettv->vval.v_number = -1;
12926 return; /* type error; errmsg already given */
12927 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012928# ifdef WIN32
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012929 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012930 if (n == 0)
12931 rettv->vval.v_number = -1;
12932 else
12933 {
12934 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
12935 rettv->vval.v_number = (s != NULL);
12936 }
12937# else
12938 rettv->vval.v_number = 0;
12939 if (check_connection() == FAIL)
12940 return;
12941
12942 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012943 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012944# endif
12945
12946 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
12947 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012948 char_u *retvar;
12949
Bram Moolenaar33570922005-01-25 22:26:29 +000012950 v.di_tv.v_type = VAR_STRING;
12951 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012952 retvar = get_tv_string_chk(&argvars[1]);
12953 if (retvar != NULL)
12954 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012955 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012956 }
12957#else
12958 rettv->vval.v_number = -1;
12959#endif
12960}
12961
12962/*ARGSUSED*/
12963 static void
12964f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012965 typval_T *argvars;
12966 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012967{
12968 char_u *r = NULL;
12969
12970#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012971 char_u *serverid = get_tv_string_chk(&argvars[0]);
12972
12973 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000012974 {
12975# ifdef WIN32
12976 /* The server's HWND is encoded in the 'id' parameter */
12977 int n = 0;
12978
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012979 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012980 if (n != 0)
12981 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
12982 if (r == NULL)
12983# else
12984 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012985 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012986# endif
12987 EMSG(_("E277: Unable to read a server reply"));
12988 }
12989#endif
12990 rettv->v_type = VAR_STRING;
12991 rettv->vval.v_string = r;
12992}
12993
12994/*
12995 * "remote_send()" function
12996 */
12997/*ARGSUSED*/
12998 static void
12999f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013000 typval_T *argvars;
13001 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013002{
13003 rettv->v_type = VAR_STRING;
13004 rettv->vval.v_string = NULL;
13005#ifdef FEAT_CLIENTSERVER
13006 remote_common(argvars, rettv, FALSE);
13007#endif
13008}
13009
13010/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013011 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013012 */
13013 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013014f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013015 typval_T *argvars;
13016 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013017{
Bram Moolenaar33570922005-01-25 22:26:29 +000013018 list_T *l;
13019 listitem_T *item, *item2;
13020 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013021 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013022 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013023 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000013024 dict_T *d;
13025 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013026
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013027 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013028 if (argvars[0].v_type == VAR_DICT)
13029 {
13030 if (argvars[2].v_type != VAR_UNKNOWN)
13031 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013032 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar758711c2005-02-02 23:11:38 +000013033 && !tv_check_lock(d->dv_lock, (char_u *)"remove()"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013034 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013035 key = get_tv_string_chk(&argvars[1]);
13036 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013037 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013038 di = dict_find(d, key, -1);
13039 if (di == NULL)
13040 EMSG2(_(e_dictkey), key);
13041 else
13042 {
13043 *rettv = di->di_tv;
13044 init_tv(&di->di_tv);
13045 dictitem_remove(d, di);
13046 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013047 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013048 }
13049 }
13050 else if (argvars[0].v_type != VAR_LIST)
13051 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013052 else if ((l = argvars[0].vval.v_list) != NULL
13053 && !tv_check_lock(l->lv_lock, (char_u *)"remove()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013054 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013055 int error = FALSE;
13056
13057 idx = get_tv_number_chk(&argvars[1], &error);
13058 if (error)
13059 ; /* type error: do nothing, errmsg already given */
13060 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013061 EMSGN(_(e_listidx), idx);
13062 else
13063 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013064 if (argvars[2].v_type == VAR_UNKNOWN)
13065 {
13066 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013067 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013068 *rettv = item->li_tv;
13069 vim_free(item);
13070 }
13071 else
13072 {
13073 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013074 end = get_tv_number_chk(&argvars[2], &error);
13075 if (error)
13076 ; /* type error: do nothing */
13077 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013078 EMSGN(_(e_listidx), end);
13079 else
13080 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013081 int cnt = 0;
13082
13083 for (li = item; li != NULL; li = li->li_next)
13084 {
13085 ++cnt;
13086 if (li == item2)
13087 break;
13088 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013089 if (li == NULL) /* didn't find "item2" after "item" */
13090 EMSG(_(e_invrange));
13091 else
13092 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013093 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013094 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013095 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013096 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013097 l->lv_first = item;
13098 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013099 item->li_prev = NULL;
13100 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013101 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013102 }
13103 }
13104 }
13105 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013106 }
13107 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013108}
13109
13110/*
13111 * "rename({from}, {to})" function
13112 */
13113 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013114f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013115 typval_T *argvars;
13116 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013117{
13118 char_u buf[NUMBUFLEN];
13119
13120 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013121 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013122 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013123 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
13124 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013125}
13126
13127/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013128 * "repeat()" function
13129 */
13130/*ARGSUSED*/
13131 static void
13132f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013133 typval_T *argvars;
13134 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013135{
13136 char_u *p;
13137 int n;
13138 int slen;
13139 int len;
13140 char_u *r;
13141 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013142
13143 n = get_tv_number(&argvars[1]);
13144 if (argvars[0].v_type == VAR_LIST)
13145 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013146 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013147 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013148 if (list_extend(rettv->vval.v_list,
13149 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013150 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013151 }
13152 else
13153 {
13154 p = get_tv_string(&argvars[0]);
13155 rettv->v_type = VAR_STRING;
13156 rettv->vval.v_string = NULL;
13157
13158 slen = (int)STRLEN(p);
13159 len = slen * n;
13160 if (len <= 0)
13161 return;
13162
13163 r = alloc(len + 1);
13164 if (r != NULL)
13165 {
13166 for (i = 0; i < n; i++)
13167 mch_memmove(r + i * slen, p, (size_t)slen);
13168 r[len] = NUL;
13169 }
13170
13171 rettv->vval.v_string = r;
13172 }
13173}
13174
13175/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013176 * "resolve()" function
13177 */
13178 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013179f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013180 typval_T *argvars;
13181 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013182{
13183 char_u *p;
13184
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013185 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013186#ifdef FEAT_SHORTCUT
13187 {
13188 char_u *v = NULL;
13189
13190 v = mch_resolve_shortcut(p);
13191 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013192 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013193 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013194 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013195 }
13196#else
13197# ifdef HAVE_READLINK
13198 {
13199 char_u buf[MAXPATHL + 1];
13200 char_u *cpy;
13201 int len;
13202 char_u *remain = NULL;
13203 char_u *q;
13204 int is_relative_to_current = FALSE;
13205 int has_trailing_pathsep = FALSE;
13206 int limit = 100;
13207
13208 p = vim_strsave(p);
13209
13210 if (p[0] == '.' && (vim_ispathsep(p[1])
13211 || (p[1] == '.' && (vim_ispathsep(p[2])))))
13212 is_relative_to_current = TRUE;
13213
13214 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013215 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013216 has_trailing_pathsep = TRUE;
13217
13218 q = getnextcomp(p);
13219 if (*q != NUL)
13220 {
13221 /* Separate the first path component in "p", and keep the
13222 * remainder (beginning with the path separator). */
13223 remain = vim_strsave(q - 1);
13224 q[-1] = NUL;
13225 }
13226
13227 for (;;)
13228 {
13229 for (;;)
13230 {
13231 len = readlink((char *)p, (char *)buf, MAXPATHL);
13232 if (len <= 0)
13233 break;
13234 buf[len] = NUL;
13235
13236 if (limit-- == 0)
13237 {
13238 vim_free(p);
13239 vim_free(remain);
13240 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013241 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013242 goto fail;
13243 }
13244
13245 /* Ensure that the result will have a trailing path separator
13246 * if the argument has one. */
13247 if (remain == NULL && has_trailing_pathsep)
13248 add_pathsep(buf);
13249
13250 /* Separate the first path component in the link value and
13251 * concatenate the remainders. */
13252 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
13253 if (*q != NUL)
13254 {
13255 if (remain == NULL)
13256 remain = vim_strsave(q - 1);
13257 else
13258 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000013259 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013260 if (cpy != NULL)
13261 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013262 vim_free(remain);
13263 remain = cpy;
13264 }
13265 }
13266 q[-1] = NUL;
13267 }
13268
13269 q = gettail(p);
13270 if (q > p && *q == NUL)
13271 {
13272 /* Ignore trailing path separator. */
13273 q[-1] = NUL;
13274 q = gettail(p);
13275 }
13276 if (q > p && !mch_isFullName(buf))
13277 {
13278 /* symlink is relative to directory of argument */
13279 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
13280 if (cpy != NULL)
13281 {
13282 STRCPY(cpy, p);
13283 STRCPY(gettail(cpy), buf);
13284 vim_free(p);
13285 p = cpy;
13286 }
13287 }
13288 else
13289 {
13290 vim_free(p);
13291 p = vim_strsave(buf);
13292 }
13293 }
13294
13295 if (remain == NULL)
13296 break;
13297
13298 /* Append the first path component of "remain" to "p". */
13299 q = getnextcomp(remain + 1);
13300 len = q - remain - (*q != NUL);
13301 cpy = vim_strnsave(p, STRLEN(p) + len);
13302 if (cpy != NULL)
13303 {
13304 STRNCAT(cpy, remain, len);
13305 vim_free(p);
13306 p = cpy;
13307 }
13308 /* Shorten "remain". */
13309 if (*q != NUL)
13310 STRCPY(remain, q - 1);
13311 else
13312 {
13313 vim_free(remain);
13314 remain = NULL;
13315 }
13316 }
13317
13318 /* If the result is a relative path name, make it explicitly relative to
13319 * the current directory if and only if the argument had this form. */
13320 if (!vim_ispathsep(*p))
13321 {
13322 if (is_relative_to_current
13323 && *p != NUL
13324 && !(p[0] == '.'
13325 && (p[1] == NUL
13326 || vim_ispathsep(p[1])
13327 || (p[1] == '.'
13328 && (p[2] == NUL
13329 || vim_ispathsep(p[2]))))))
13330 {
13331 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013332 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013333 if (cpy != NULL)
13334 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013335 vim_free(p);
13336 p = cpy;
13337 }
13338 }
13339 else if (!is_relative_to_current)
13340 {
13341 /* Strip leading "./". */
13342 q = p;
13343 while (q[0] == '.' && vim_ispathsep(q[1]))
13344 q += 2;
13345 if (q > p)
13346 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
13347 }
13348 }
13349
13350 /* Ensure that the result will have no trailing path separator
13351 * if the argument had none. But keep "/" or "//". */
13352 if (!has_trailing_pathsep)
13353 {
13354 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013355 if (after_pathsep(p, q))
13356 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013357 }
13358
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013359 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013360 }
13361# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013362 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013363# endif
13364#endif
13365
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013366 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013367
13368#ifdef HAVE_READLINK
13369fail:
13370#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013371 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013372}
13373
13374/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013375 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013376 */
13377 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013378f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013379 typval_T *argvars;
13380 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013381{
Bram Moolenaar33570922005-01-25 22:26:29 +000013382 list_T *l;
13383 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013384
Bram Moolenaar0d660222005-01-07 21:51:51 +000013385 rettv->vval.v_number = 0;
13386 if (argvars[0].v_type != VAR_LIST)
13387 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013388 else if ((l = argvars[0].vval.v_list) != NULL
13389 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013390 {
13391 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013392 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013393 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013394 while (li != NULL)
13395 {
13396 ni = li->li_prev;
13397 list_append(l, li);
13398 li = ni;
13399 }
13400 rettv->vval.v_list = l;
13401 rettv->v_type = VAR_LIST;
13402 ++l->lv_refcount;
13403 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013404}
13405
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013406#define SP_NOMOVE 0x01 /* don't move cursor */
13407#define SP_REPEAT 0x02 /* repeat to find outer pair */
13408#define SP_RETCOUNT 0x04 /* return matchcount */
13409#define SP_SETPCMARK 0x08 /* set previous context mark */
13410#define SP_START 0x10 /* accept match at start position */
13411#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
13412#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013413
Bram Moolenaar33570922005-01-25 22:26:29 +000013414static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013415
13416/*
13417 * Get flags for a search function.
13418 * Possibly sets "p_ws".
13419 * Returns BACKWARD, FORWARD or zero (for an error).
13420 */
13421 static int
13422get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013423 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013424 int *flagsp;
13425{
13426 int dir = FORWARD;
13427 char_u *flags;
13428 char_u nbuf[NUMBUFLEN];
13429 int mask;
13430
13431 if (varp->v_type != VAR_UNKNOWN)
13432 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013433 flags = get_tv_string_buf_chk(varp, nbuf);
13434 if (flags == NULL)
13435 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013436 while (*flags != NUL)
13437 {
13438 switch (*flags)
13439 {
13440 case 'b': dir = BACKWARD; break;
13441 case 'w': p_ws = TRUE; break;
13442 case 'W': p_ws = FALSE; break;
13443 default: mask = 0;
13444 if (flagsp != NULL)
13445 switch (*flags)
13446 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013447 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013448 case 'e': mask = SP_END; break;
13449 case 'm': mask = SP_RETCOUNT; break;
13450 case 'n': mask = SP_NOMOVE; break;
13451 case 'p': mask = SP_SUBPAT; break;
13452 case 'r': mask = SP_REPEAT; break;
13453 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013454 }
13455 if (mask == 0)
13456 {
13457 EMSG2(_(e_invarg2), flags);
13458 dir = 0;
13459 }
13460 else
13461 *flagsp |= mask;
13462 }
13463 if (dir == 0)
13464 break;
13465 ++flags;
13466 }
13467 }
13468 return dir;
13469}
13470
Bram Moolenaar071d4272004-06-13 20:20:40 +000013471/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013472 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000013473 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013474 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013475search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013476 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013477 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013478 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013479{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013480 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013481 char_u *pat;
13482 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013483 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013484 int save_p_ws = p_ws;
13485 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013486 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013487 long lnum_stop = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013488 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013489 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013490
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013491 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013492 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013493 if (dir == 0)
13494 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013495 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013496 if (flags & SP_START)
13497 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013498 if (flags & SP_END)
13499 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013500
13501 /* Optional extra argument: line number to stop searching. */
13502 if (argvars[1].v_type != VAR_UNKNOWN
13503 && argvars[2].v_type != VAR_UNKNOWN)
13504 {
13505 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
13506 if (lnum_stop < 0)
13507 goto theend;
13508 }
13509
Bram Moolenaar231334e2005-07-25 20:46:57 +000013510 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013511 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000013512 * Check to make sure only those flags are set.
13513 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
13514 * flags cannot be set. Check for that condition also.
13515 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013516 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013517 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013518 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013519 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013520 goto theend;
13521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013522
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013523 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013524 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
13525 options, RE_SEARCH, (linenr_T)lnum_stop);
13526 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013527 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013528 if (flags & SP_SUBPAT)
13529 retval = subpatnum;
13530 else
13531 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013532 if (flags & SP_SETPCMARK)
13533 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013534 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013535 if (match_pos != NULL)
13536 {
13537 /* Store the match cursor position */
13538 match_pos->lnum = pos.lnum;
13539 match_pos->col = pos.col + 1;
13540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013541 /* "/$" will put the cursor after the end of the line, may need to
13542 * correct that here */
13543 check_cursor();
13544 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013545
13546 /* If 'n' flag is used: restore cursor position. */
13547 if (flags & SP_NOMOVE)
13548 curwin->w_cursor = save_cursor;
13549theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000013550 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013551
13552 return retval;
13553}
13554
13555/*
13556 * "search()" function
13557 */
13558 static void
13559f_search(argvars, rettv)
13560 typval_T *argvars;
13561 typval_T *rettv;
13562{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013563 int flags = 0;
13564
13565 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013566}
13567
Bram Moolenaar071d4272004-06-13 20:20:40 +000013568/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013569 * "searchdecl()" function
13570 */
13571 static void
13572f_searchdecl(argvars, rettv)
13573 typval_T *argvars;
13574 typval_T *rettv;
13575{
13576 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013577 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013578 int error = FALSE;
13579 char_u *name;
13580
13581 rettv->vval.v_number = 1; /* default: FAIL */
13582
13583 name = get_tv_string_chk(&argvars[0]);
13584 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000013585 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013586 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013587 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13588 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
13589 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013590 if (!error && name != NULL)
13591 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000013592 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013593}
13594
13595/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013596 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000013597 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013598 static int
13599searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000013600 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013601 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013602{
13603 char_u *spat, *mpat, *epat;
13604 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013605 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013606 int dir;
13607 int flags = 0;
13608 char_u nbuf1[NUMBUFLEN];
13609 char_u nbuf2[NUMBUFLEN];
13610 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013611 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013612 long lnum_stop = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013613
Bram Moolenaar071d4272004-06-13 20:20:40 +000013614 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013615 spat = get_tv_string_chk(&argvars[0]);
13616 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
13617 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
13618 if (spat == NULL || mpat == NULL || epat == NULL)
13619 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013620
Bram Moolenaar071d4272004-06-13 20:20:40 +000013621 /* Handle the optional fourth argument: flags */
13622 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013623 if (dir == 0)
13624 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013625
13626 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000013627 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
13628 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013629 if ((flags & (SP_END | SP_SUBPAT)) != 0
13630 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000013631 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013632 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000013633 goto theend;
13634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013635
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013636 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013637 if (argvars[3].v_type == VAR_UNKNOWN
13638 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013639 skip = (char_u *)"";
13640 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013641 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013642 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013643 if (argvars[5].v_type != VAR_UNKNOWN)
13644 {
13645 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
13646 if (lnum_stop < 0)
13647 goto theend;
13648 }
13649 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013650 if (skip == NULL)
13651 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013652
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013653 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
13654 match_pos, lnum_stop);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013655
13656theend:
13657 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013658
13659 return retval;
13660}
13661
13662/*
13663 * "searchpair()" function
13664 */
13665 static void
13666f_searchpair(argvars, rettv)
13667 typval_T *argvars;
13668 typval_T *rettv;
13669{
13670 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
13671}
13672
13673/*
13674 * "searchpairpos()" function
13675 */
13676 static void
13677f_searchpairpos(argvars, rettv)
13678 typval_T *argvars;
13679 typval_T *rettv;
13680{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013681 pos_T match_pos;
13682 int lnum = 0;
13683 int col = 0;
13684
13685 rettv->vval.v_number = 0;
13686
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013687 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013688 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013689
13690 if (searchpair_cmn(argvars, &match_pos) > 0)
13691 {
13692 lnum = match_pos.lnum;
13693 col = match_pos.col;
13694 }
13695
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013696 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
13697 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013698}
13699
13700/*
13701 * Search for a start/middle/end thing.
13702 * Used by searchpair(), see its documentation for the details.
13703 * Returns 0 or -1 for no match,
13704 */
13705 long
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013706do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos, lnum_stop)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013707 char_u *spat; /* start pattern */
13708 char_u *mpat; /* middle pattern */
13709 char_u *epat; /* end pattern */
13710 int dir; /* BACKWARD or FORWARD */
13711 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013712 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013713 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013714 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013715{
13716 char_u *save_cpo;
13717 char_u *pat, *pat2 = NULL, *pat3 = NULL;
13718 long retval = 0;
13719 pos_T pos;
13720 pos_T firstpos;
13721 pos_T foundpos;
13722 pos_T save_cursor;
13723 pos_T save_pos;
13724 int n;
13725 int r;
13726 int nest = 1;
13727 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013728 int options = SEARCH_KEEP;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013729
13730 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13731 save_cpo = p_cpo;
13732 p_cpo = (char_u *)"";
13733
13734 /* Make two search patterns: start/end (pat2, for in nested pairs) and
13735 * start/middle/end (pat3, for the top pair). */
13736 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
13737 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
13738 if (pat2 == NULL || pat3 == NULL)
13739 goto theend;
13740 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
13741 if (*mpat == NUL)
13742 STRCPY(pat3, pat2);
13743 else
13744 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
13745 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013746 if (flags & SP_START)
13747 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013748
Bram Moolenaar071d4272004-06-13 20:20:40 +000013749 save_cursor = curwin->w_cursor;
13750 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000013751 clearpos(&firstpos);
13752 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013753 pat = pat3;
13754 for (;;)
13755 {
13756 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013757 options, RE_SEARCH, lnum_stop);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013758 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
13759 /* didn't find it or found the first match again: FAIL */
13760 break;
13761
13762 if (firstpos.lnum == 0)
13763 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000013764 if (equalpos(pos, foundpos))
13765 {
13766 /* Found the same position again. Can happen with a pattern that
13767 * has "\zs" at the end and searching backwards. Advance one
13768 * character and try again. */
13769 if (dir == BACKWARD)
13770 decl(&pos);
13771 else
13772 incl(&pos);
13773 }
13774 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013775
13776 /* If the skip pattern matches, ignore this match. */
13777 if (*skip != NUL)
13778 {
13779 save_pos = curwin->w_cursor;
13780 curwin->w_cursor = pos;
13781 r = eval_to_bool(skip, &err, NULL, FALSE);
13782 curwin->w_cursor = save_pos;
13783 if (err)
13784 {
13785 /* Evaluating {skip} caused an error, break here. */
13786 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013787 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013788 break;
13789 }
13790 if (r)
13791 continue;
13792 }
13793
13794 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
13795 {
13796 /* Found end when searching backwards or start when searching
13797 * forward: nested pair. */
13798 ++nest;
13799 pat = pat2; /* nested, don't search for middle */
13800 }
13801 else
13802 {
13803 /* Found end when searching forward or start when searching
13804 * backward: end of (nested) pair; or found middle in outer pair. */
13805 if (--nest == 1)
13806 pat = pat3; /* outer level, search for middle */
13807 }
13808
13809 if (nest == 0)
13810 {
13811 /* Found the match: return matchcount or line number. */
13812 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013813 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013814 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013815 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013816 if (flags & SP_SETPCMARK)
13817 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013818 curwin->w_cursor = pos;
13819 if (!(flags & SP_REPEAT))
13820 break;
13821 nest = 1; /* search for next unmatched */
13822 }
13823 }
13824
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013825 if (match_pos != NULL)
13826 {
13827 /* Store the match cursor position */
13828 match_pos->lnum = curwin->w_cursor.lnum;
13829 match_pos->col = curwin->w_cursor.col + 1;
13830 }
13831
Bram Moolenaar071d4272004-06-13 20:20:40 +000013832 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013833 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013834 curwin->w_cursor = save_cursor;
13835
13836theend:
13837 vim_free(pat2);
13838 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013839 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013840
13841 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013842}
13843
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013844/*
13845 * "searchpos()" function
13846 */
13847 static void
13848f_searchpos(argvars, rettv)
13849 typval_T *argvars;
13850 typval_T *rettv;
13851{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013852 pos_T match_pos;
13853 int lnum = 0;
13854 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013855 int n;
13856 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013857
13858 rettv->vval.v_number = 0;
13859
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013860 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013861 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013862
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013863 n = search_cmn(argvars, &match_pos, &flags);
13864 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013865 {
13866 lnum = match_pos.lnum;
13867 col = match_pos.col;
13868 }
13869
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013870 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
13871 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013872 if (flags & SP_SUBPAT)
13873 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013874}
13875
13876
Bram Moolenaar0d660222005-01-07 21:51:51 +000013877/*ARGSUSED*/
13878 static void
13879f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013880 typval_T *argvars;
13881 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013882{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013883#ifdef FEAT_CLIENTSERVER
13884 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013885 char_u *server = get_tv_string_chk(&argvars[0]);
13886 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013887
Bram Moolenaar0d660222005-01-07 21:51:51 +000013888 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013889 if (server == NULL || reply == NULL)
13890 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013891 if (check_restricted() || check_secure())
13892 return;
13893# ifdef FEAT_X11
13894 if (check_connection() == FAIL)
13895 return;
13896# endif
13897
13898 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013899 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013900 EMSG(_("E258: Unable to send to client"));
13901 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013902 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013903 rettv->vval.v_number = 0;
13904#else
13905 rettv->vval.v_number = -1;
13906#endif
13907}
13908
13909/*ARGSUSED*/
13910 static void
13911f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013912 typval_T *argvars;
13913 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013914{
13915 char_u *r = NULL;
13916
13917#ifdef FEAT_CLIENTSERVER
13918# ifdef WIN32
13919 r = serverGetVimNames();
13920# else
13921 make_connection();
13922 if (X_DISPLAY != NULL)
13923 r = serverGetVimNames(X_DISPLAY);
13924# endif
13925#endif
13926 rettv->v_type = VAR_STRING;
13927 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013928}
13929
13930/*
13931 * "setbufvar()" function
13932 */
13933/*ARGSUSED*/
13934 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013935f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013936 typval_T *argvars;
13937 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013938{
13939 buf_T *buf;
13940#ifdef FEAT_AUTOCMD
13941 aco_save_T aco;
13942#else
13943 buf_T *save_curbuf;
13944#endif
13945 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013946 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013947 char_u nbuf[NUMBUFLEN];
13948
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013949 rettv->vval.v_number = 0;
13950
Bram Moolenaar071d4272004-06-13 20:20:40 +000013951 if (check_restricted() || check_secure())
13952 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013953 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
13954 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013955 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013956 varp = &argvars[2];
13957
13958 if (buf != NULL && varname != NULL && varp != NULL)
13959 {
13960 /* set curbuf to be our buf, temporarily */
13961#ifdef FEAT_AUTOCMD
13962 aucmd_prepbuf(&aco, buf);
13963#else
13964 save_curbuf = curbuf;
13965 curbuf = buf;
13966#endif
13967
13968 if (*varname == '&')
13969 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013970 long numval;
13971 char_u *strval;
13972 int error = FALSE;
13973
Bram Moolenaar071d4272004-06-13 20:20:40 +000013974 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013975 numval = get_tv_number_chk(varp, &error);
13976 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013977 if (!error && strval != NULL)
13978 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013979 }
13980 else
13981 {
13982 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
13983 if (bufvarname != NULL)
13984 {
13985 STRCPY(bufvarname, "b:");
13986 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013987 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013988 vim_free(bufvarname);
13989 }
13990 }
13991
13992 /* reset notion of buffer */
13993#ifdef FEAT_AUTOCMD
13994 aucmd_restbuf(&aco);
13995#else
13996 curbuf = save_curbuf;
13997#endif
13998 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013999}
14000
14001/*
14002 * "setcmdpos()" function
14003 */
14004 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014005f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014006 typval_T *argvars;
14007 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014008{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014009 int pos = (int)get_tv_number(&argvars[0]) - 1;
14010
14011 if (pos >= 0)
14012 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014013}
14014
14015/*
14016 * "setline()" function
14017 */
14018 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014019f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014020 typval_T *argvars;
14021 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014022{
14023 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000014024 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014025 list_T *l = NULL;
14026 listitem_T *li = NULL;
14027 long added = 0;
14028 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014029
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014030 lnum = get_tv_lnum(&argvars[0]);
14031 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014032 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014033 l = argvars[1].vval.v_list;
14034 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014035 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014036 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014037 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014038
14039 rettv->vval.v_number = 0; /* OK */
14040 for (;;)
14041 {
14042 if (l != NULL)
14043 {
14044 /* list argument, get next string */
14045 if (li == NULL)
14046 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014047 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014048 li = li->li_next;
14049 }
14050
14051 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014052 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014053 break;
14054 if (lnum <= curbuf->b_ml.ml_line_count)
14055 {
14056 /* existing line, replace it */
14057 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
14058 {
14059 changed_bytes(lnum, 0);
14060 check_cursor_col();
14061 rettv->vval.v_number = 0; /* OK */
14062 }
14063 }
14064 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
14065 {
14066 /* lnum is one past the last line, append the line */
14067 ++added;
14068 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
14069 rettv->vval.v_number = 0; /* OK */
14070 }
14071
14072 if (l == NULL) /* only one string argument */
14073 break;
14074 ++lnum;
14075 }
14076
14077 if (added > 0)
14078 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014079}
14080
14081/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014082 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000014083 */
14084/*ARGSUSED*/
14085 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014086set_qf_ll_list(wp, list_arg, action_arg, rettv)
14087 win_T *wp;
14088 typval_T *list_arg;
14089 typval_T *action_arg;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014090 typval_T *rettv;
14091{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014092#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014093 char_u *act;
14094 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014095#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014096
Bram Moolenaar2641f772005-03-25 21:58:17 +000014097 rettv->vval.v_number = -1;
14098
14099#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014100 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014101 EMSG(_(e_listreq));
14102 else
14103 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014104 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014105
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014106 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014107 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014108 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014109 if (act == NULL)
14110 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014111 if (*act == 'a' || *act == 'r')
14112 action = *act;
14113 }
14114
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014115 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014116 rettv->vval.v_number = 0;
14117 }
14118#endif
14119}
14120
14121/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014122 * "setloclist()" function
14123 */
14124/*ARGSUSED*/
14125 static void
14126f_setloclist(argvars, rettv)
14127 typval_T *argvars;
14128 typval_T *rettv;
14129{
14130 win_T *win;
14131
14132 rettv->vval.v_number = -1;
14133
14134 win = find_win_by_nr(&argvars[0]);
14135 if (win != NULL)
14136 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
14137}
14138
14139/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014140 * "setpos()" function
14141 */
14142/*ARGSUSED*/
14143 static void
14144f_setpos(argvars, rettv)
14145 typval_T *argvars;
14146 typval_T *rettv;
14147{
14148 pos_T pos;
14149 int fnum;
14150 char_u *name;
14151
14152 name = get_tv_string_chk(argvars);
14153 if (name != NULL)
14154 {
14155 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
14156 {
14157 --pos.col;
14158 if (name[0] == '.') /* cursor */
14159 {
14160 if (fnum == curbuf->b_fnum)
14161 {
14162 curwin->w_cursor = pos;
14163 check_cursor();
14164 }
14165 else
14166 EMSG(_(e_invarg));
14167 }
14168 else if (name[0] == '\'') /* mark */
14169 (void)setmark_pos(name[1], &pos, fnum);
14170 else
14171 EMSG(_(e_invarg));
14172 }
14173 }
14174}
14175
14176/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014177 * "setqflist()" function
14178 */
14179/*ARGSUSED*/
14180 static void
14181f_setqflist(argvars, rettv)
14182 typval_T *argvars;
14183 typval_T *rettv;
14184{
14185 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
14186}
14187
14188/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014189 * "setreg()" function
14190 */
14191 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014192f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014193 typval_T *argvars;
14194 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014195{
14196 int regname;
14197 char_u *strregname;
14198 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014199 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014200 int append;
14201 char_u yank_type;
14202 long block_len;
14203
14204 block_len = -1;
14205 yank_type = MAUTO;
14206 append = FALSE;
14207
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014208 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014209 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014210
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014211 if (strregname == NULL)
14212 return; /* type error; errmsg already given */
14213 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014214 if (regname == 0 || regname == '@')
14215 regname = '"';
14216 else if (regname == '=')
14217 return;
14218
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014219 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014220 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014221 stropt = get_tv_string_chk(&argvars[2]);
14222 if (stropt == NULL)
14223 return; /* type error */
14224 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014225 switch (*stropt)
14226 {
14227 case 'a': case 'A': /* append */
14228 append = TRUE;
14229 break;
14230 case 'v': case 'c': /* character-wise selection */
14231 yank_type = MCHAR;
14232 break;
14233 case 'V': case 'l': /* line-wise selection */
14234 yank_type = MLINE;
14235 break;
14236#ifdef FEAT_VISUAL
14237 case 'b': case Ctrl_V: /* block-wise selection */
14238 yank_type = MBLOCK;
14239 if (VIM_ISDIGIT(stropt[1]))
14240 {
14241 ++stropt;
14242 block_len = getdigits(&stropt) - 1;
14243 --stropt;
14244 }
14245 break;
14246#endif
14247 }
14248 }
14249
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014250 strval = get_tv_string_chk(&argvars[1]);
14251 if (strval != NULL)
14252 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000014253 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014254 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014255}
14256
14257
14258/*
14259 * "setwinvar(expr)" function
14260 */
14261/*ARGSUSED*/
14262 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014263f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014264 typval_T *argvars;
14265 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014266{
14267 win_T *win;
14268#ifdef FEAT_WINDOWS
14269 win_T *save_curwin;
14270#endif
14271 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014272 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014273 char_u nbuf[NUMBUFLEN];
14274
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014275 rettv->vval.v_number = 0;
14276
Bram Moolenaar071d4272004-06-13 20:20:40 +000014277 if (check_restricted() || check_secure())
14278 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014279 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014280 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014281 varp = &argvars[2];
14282
14283 if (win != NULL && varname != NULL && varp != NULL)
14284 {
14285#ifdef FEAT_WINDOWS
14286 /* set curwin to be our win, temporarily */
14287 save_curwin = curwin;
14288 curwin = win;
14289 curbuf = curwin->w_buffer;
14290#endif
14291
14292 if (*varname == '&')
14293 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014294 long numval;
14295 char_u *strval;
14296 int error = FALSE;
14297
Bram Moolenaar071d4272004-06-13 20:20:40 +000014298 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014299 numval = get_tv_number_chk(varp, &error);
14300 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014301 if (!error && strval != NULL)
14302 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014303 }
14304 else
14305 {
14306 winvarname = alloc((unsigned)STRLEN(varname) + 3);
14307 if (winvarname != NULL)
14308 {
14309 STRCPY(winvarname, "w:");
14310 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014311 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014312 vim_free(winvarname);
14313 }
14314 }
14315
14316#ifdef FEAT_WINDOWS
14317 /* Restore current window, if it's still valid (autocomands can make
14318 * it invalid). */
14319 if (win_valid(save_curwin))
14320 {
14321 curwin = save_curwin;
14322 curbuf = curwin->w_buffer;
14323 }
14324#endif
14325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014326}
14327
14328/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014329 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014330 */
14331 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014332f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014333 typval_T *argvars;
14334 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014335{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014336 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014337
Bram Moolenaar0d660222005-01-07 21:51:51 +000014338 p = get_tv_string(&argvars[0]);
14339 rettv->vval.v_string = vim_strsave(p);
14340 simplify_filename(rettv->vval.v_string); /* simplify in place */
14341 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014342}
14343
Bram Moolenaar0d660222005-01-07 21:51:51 +000014344static int
14345#ifdef __BORLANDC__
14346 _RTLENTRYF
14347#endif
14348 item_compare __ARGS((const void *s1, const void *s2));
14349static int
14350#ifdef __BORLANDC__
14351 _RTLENTRYF
14352#endif
14353 item_compare2 __ARGS((const void *s1, const void *s2));
14354
14355static int item_compare_ic;
14356static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014357static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014358#define ITEM_COMPARE_FAIL 999
14359
Bram Moolenaar071d4272004-06-13 20:20:40 +000014360/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014361 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014362 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014363 static int
14364#ifdef __BORLANDC__
14365_RTLENTRYF
14366#endif
14367item_compare(s1, s2)
14368 const void *s1;
14369 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014370{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014371 char_u *p1, *p2;
14372 char_u *tofree1, *tofree2;
14373 int res;
14374 char_u numbuf1[NUMBUFLEN];
14375 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014376
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014377 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
14378 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014379 if (item_compare_ic)
14380 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014381 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014382 res = STRCMP(p1, p2);
14383 vim_free(tofree1);
14384 vim_free(tofree2);
14385 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014386}
14387
14388 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000014389#ifdef __BORLANDC__
14390_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014391#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000014392item_compare2(s1, s2)
14393 const void *s1;
14394 const void *s2;
14395{
14396 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000014397 typval_T rettv;
14398 typval_T argv[2];
Bram Moolenaar0d660222005-01-07 21:51:51 +000014399 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014400
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014401 /* shortcut after failure in previous call; compare all items equal */
14402 if (item_compare_func_err)
14403 return 0;
14404
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014405 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
14406 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014407 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
14408 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014409
14410 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
14411 res = call_func(item_compare_func, STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000014412 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014413 clear_tv(&argv[0]);
14414 clear_tv(&argv[1]);
14415
14416 if (res == FAIL)
14417 res = ITEM_COMPARE_FAIL;
14418 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014419 /* return value has wrong type */
14420 res = get_tv_number_chk(&rettv, &item_compare_func_err);
14421 if (item_compare_func_err)
14422 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014423 clear_tv(&rettv);
14424 return res;
14425}
14426
14427/*
14428 * "sort({list})" function
14429 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014430 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014431f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014432 typval_T *argvars;
14433 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014434{
Bram Moolenaar33570922005-01-25 22:26:29 +000014435 list_T *l;
14436 listitem_T *li;
14437 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014438 long len;
14439 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014440
Bram Moolenaar0d660222005-01-07 21:51:51 +000014441 rettv->vval.v_number = 0;
14442 if (argvars[0].v_type != VAR_LIST)
14443 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000014444 else
14445 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014446 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014447 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014448 return;
14449 rettv->vval.v_list = l;
14450 rettv->v_type = VAR_LIST;
14451 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014452
Bram Moolenaar0d660222005-01-07 21:51:51 +000014453 len = list_len(l);
14454 if (len <= 1)
14455 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014456
Bram Moolenaar0d660222005-01-07 21:51:51 +000014457 item_compare_ic = FALSE;
14458 item_compare_func = NULL;
14459 if (argvars[1].v_type != VAR_UNKNOWN)
14460 {
14461 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014462 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014463 else
14464 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014465 int error = FALSE;
14466
14467 i = get_tv_number_chk(&argvars[1], &error);
14468 if (error)
14469 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014470 if (i == 1)
14471 item_compare_ic = TRUE;
14472 else
14473 item_compare_func = get_tv_string(&argvars[1]);
14474 }
14475 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014476
Bram Moolenaar0d660222005-01-07 21:51:51 +000014477 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014478 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014479 if (ptrs == NULL)
14480 return;
14481 i = 0;
14482 for (li = l->lv_first; li != NULL; li = li->li_next)
14483 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014484
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014485 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014486 /* test the compare function */
14487 if (item_compare_func != NULL
14488 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
14489 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014490 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014491 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014492 {
14493 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014494 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000014495 item_compare_func == NULL ? item_compare : item_compare2);
14496
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014497 if (!item_compare_func_err)
14498 {
14499 /* Clear the List and append the items in the sorted order. */
14500 l->lv_first = l->lv_last = NULL;
14501 l->lv_len = 0;
14502 for (i = 0; i < len; ++i)
14503 list_append(l, ptrs[i]);
14504 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014505 }
14506
14507 vim_free(ptrs);
14508 }
14509}
14510
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014511/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000014512 * "soundfold({word})" function
14513 */
14514 static void
14515f_soundfold(argvars, rettv)
14516 typval_T *argvars;
14517 typval_T *rettv;
14518{
14519 char_u *s;
14520
14521 rettv->v_type = VAR_STRING;
14522 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014523#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000014524 rettv->vval.v_string = eval_soundfold(s);
14525#else
14526 rettv->vval.v_string = vim_strsave(s);
14527#endif
14528}
14529
14530/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014531 * "spellbadword()" function
14532 */
14533/* ARGSUSED */
14534 static void
14535f_spellbadword(argvars, rettv)
14536 typval_T *argvars;
14537 typval_T *rettv;
14538{
Bram Moolenaar4463f292005-09-25 22:20:24 +000014539 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014540 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014541 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014542
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014543 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014544 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014545
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014546#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000014547 if (argvars[0].v_type == VAR_UNKNOWN)
14548 {
14549 /* Find the start and length of the badly spelled word. */
14550 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
14551 if (len != 0)
14552 word = ml_get_cursor();
14553 }
14554 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14555 {
14556 char_u *str = get_tv_string_chk(&argvars[0]);
14557 int capcol = -1;
14558
14559 if (str != NULL)
14560 {
14561 /* Check the argument for spelling. */
14562 while (*str != NUL)
14563 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014564 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014565 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014566 {
14567 word = str;
14568 break;
14569 }
14570 str += len;
14571 }
14572 }
14573 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014574#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000014575
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014576 list_append_string(rettv->vval.v_list, word, len);
14577 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014578 attr == HLF_SPB ? "bad" :
14579 attr == HLF_SPR ? "rare" :
14580 attr == HLF_SPL ? "local" :
14581 attr == HLF_SPC ? "caps" :
14582 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014583}
14584
14585/*
14586 * "spellsuggest()" function
14587 */
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014588/*ARGSUSED*/
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014589 static void
14590f_spellsuggest(argvars, rettv)
14591 typval_T *argvars;
14592 typval_T *rettv;
14593{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014594#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014595 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014596 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014597 int maxcount;
14598 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014599 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014600 listitem_T *li;
14601 int need_capital = FALSE;
14602#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014603
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014604 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014605 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014606
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014607#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014608 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14609 {
14610 str = get_tv_string(&argvars[0]);
14611 if (argvars[1].v_type != VAR_UNKNOWN)
14612 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014613 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014614 if (maxcount <= 0)
14615 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014616 if (argvars[2].v_type != VAR_UNKNOWN)
14617 {
14618 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
14619 if (typeerr)
14620 return;
14621 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014622 }
14623 else
14624 maxcount = 25;
14625
Bram Moolenaar4770d092006-01-12 23:22:24 +000014626 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014627
14628 for (i = 0; i < ga.ga_len; ++i)
14629 {
14630 str = ((char_u **)ga.ga_data)[i];
14631
14632 li = listitem_alloc();
14633 if (li == NULL)
14634 vim_free(str);
14635 else
14636 {
14637 li->li_tv.v_type = VAR_STRING;
14638 li->li_tv.v_lock = 0;
14639 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014640 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014641 }
14642 }
14643 ga_clear(&ga);
14644 }
14645#endif
14646}
14647
Bram Moolenaar0d660222005-01-07 21:51:51 +000014648 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014649f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014650 typval_T *argvars;
14651 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014652{
14653 char_u *str;
14654 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014655 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014656 regmatch_T regmatch;
14657 char_u patbuf[NUMBUFLEN];
14658 char_u *save_cpo;
14659 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014660 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014661 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014662 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014663
14664 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14665 save_cpo = p_cpo;
14666 p_cpo = (char_u *)"";
14667
14668 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014669 if (argvars[1].v_type != VAR_UNKNOWN)
14670 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014671 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14672 if (pat == NULL)
14673 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014674 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014675 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014676 }
14677 if (pat == NULL || *pat == NUL)
14678 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000014679
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014680 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014681 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014682 if (typeerr)
14683 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014684
Bram Moolenaar0d660222005-01-07 21:51:51 +000014685 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14686 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014687 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014688 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014689 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014690 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014691 if (*str == NUL)
14692 match = FALSE; /* empty item at the end */
14693 else
14694 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014695 if (match)
14696 end = regmatch.startp[0];
14697 else
14698 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014699 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
14700 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014701 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014702 if (list_append_string(rettv->vval.v_list, str,
14703 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014704 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014705 }
14706 if (!match)
14707 break;
14708 /* Advance to just after the match. */
14709 if (regmatch.endp[0] > str)
14710 col = 0;
14711 else
14712 {
14713 /* Don't get stuck at the same match. */
14714#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014715 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014716#else
14717 col = 1;
14718#endif
14719 }
14720 str = regmatch.endp[0];
14721 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014722
Bram Moolenaar0d660222005-01-07 21:51:51 +000014723 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014725
Bram Moolenaar0d660222005-01-07 21:51:51 +000014726 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014727}
14728
Bram Moolenaar2c932302006-03-18 21:42:09 +000014729/*
14730 * "str2nr()" function
14731 */
14732 static void
14733f_str2nr(argvars, rettv)
14734 typval_T *argvars;
14735 typval_T *rettv;
14736{
14737 int base = 10;
14738 char_u *p;
14739 long n;
14740
14741 if (argvars[1].v_type != VAR_UNKNOWN)
14742 {
14743 base = get_tv_number(&argvars[1]);
14744 if (base != 8 && base != 10 && base != 16)
14745 {
14746 EMSG(_(e_invarg));
14747 return;
14748 }
14749 }
14750
14751 p = skipwhite(get_tv_string(&argvars[0]));
14752 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
14753 rettv->vval.v_number = n;
14754}
14755
Bram Moolenaar071d4272004-06-13 20:20:40 +000014756#ifdef HAVE_STRFTIME
14757/*
14758 * "strftime({format}[, {time}])" function
14759 */
14760 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014761f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014762 typval_T *argvars;
14763 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014764{
14765 char_u result_buf[256];
14766 struct tm *curtime;
14767 time_t seconds;
14768 char_u *p;
14769
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014770 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014771
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014772 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014773 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014774 seconds = time(NULL);
14775 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014776 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014777 curtime = localtime(&seconds);
14778 /* MSVC returns NULL for an invalid value of seconds. */
14779 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014780 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014781 else
14782 {
14783# ifdef FEAT_MBYTE
14784 vimconv_T conv;
14785 char_u *enc;
14786
14787 conv.vc_type = CONV_NONE;
14788 enc = enc_locale();
14789 convert_setup(&conv, p_enc, enc);
14790 if (conv.vc_type != CONV_NONE)
14791 p = string_convert(&conv, p, NULL);
14792# endif
14793 if (p != NULL)
14794 (void)strftime((char *)result_buf, sizeof(result_buf),
14795 (char *)p, curtime);
14796 else
14797 result_buf[0] = NUL;
14798
14799# ifdef FEAT_MBYTE
14800 if (conv.vc_type != CONV_NONE)
14801 vim_free(p);
14802 convert_setup(&conv, enc, p_enc);
14803 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014804 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014805 else
14806# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014807 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014808
14809# ifdef FEAT_MBYTE
14810 /* Release conversion descriptors */
14811 convert_setup(&conv, NULL, NULL);
14812 vim_free(enc);
14813# endif
14814 }
14815}
14816#endif
14817
14818/*
14819 * "stridx()" function
14820 */
14821 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014822f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014823 typval_T *argvars;
14824 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014825{
14826 char_u buf[NUMBUFLEN];
14827 char_u *needle;
14828 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000014829 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014830 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000014831 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014832
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014833 needle = get_tv_string_chk(&argvars[1]);
14834 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000014835 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014836 if (needle == NULL || haystack == NULL)
14837 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014838
Bram Moolenaar33570922005-01-25 22:26:29 +000014839 if (argvars[2].v_type != VAR_UNKNOWN)
14840 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014841 int error = FALSE;
14842
14843 start_idx = get_tv_number_chk(&argvars[2], &error);
14844 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000014845 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014846 if (start_idx >= 0)
14847 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000014848 }
14849
14850 pos = (char_u *)strstr((char *)haystack, (char *)needle);
14851 if (pos != NULL)
14852 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014853}
14854
14855/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014856 * "string()" function
14857 */
14858 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014859f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014860 typval_T *argvars;
14861 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014862{
14863 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014864 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014865
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014866 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014867 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014868 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014869 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014870}
14871
14872/*
14873 * "strlen()" function
14874 */
14875 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014876f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014877 typval_T *argvars;
14878 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014879{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014880 rettv->vval.v_number = (varnumber_T)(STRLEN(
14881 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014882}
14883
14884/*
14885 * "strpart()" function
14886 */
14887 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014888f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014889 typval_T *argvars;
14890 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014891{
14892 char_u *p;
14893 int n;
14894 int len;
14895 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014896 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014897
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014898 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014899 slen = (int)STRLEN(p);
14900
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014901 n = get_tv_number_chk(&argvars[1], &error);
14902 if (error)
14903 len = 0;
14904 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014905 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014906 else
14907 len = slen - n; /* default len: all bytes that are available. */
14908
14909 /*
14910 * Only return the overlap between the specified part and the actual
14911 * string.
14912 */
14913 if (n < 0)
14914 {
14915 len += n;
14916 n = 0;
14917 }
14918 else if (n > slen)
14919 n = slen;
14920 if (len < 0)
14921 len = 0;
14922 else if (n + len > slen)
14923 len = slen - n;
14924
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014925 rettv->v_type = VAR_STRING;
14926 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014927}
14928
14929/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014930 * "strridx()" function
14931 */
14932 static void
14933f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014934 typval_T *argvars;
14935 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014936{
14937 char_u buf[NUMBUFLEN];
14938 char_u *needle;
14939 char_u *haystack;
14940 char_u *rest;
14941 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014942 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014943
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014944 needle = get_tv_string_chk(&argvars[1]);
14945 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar532c7802005-01-27 14:44:31 +000014946 haystack_len = STRLEN(haystack);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014947
14948 rettv->vval.v_number = -1;
14949 if (needle == NULL || haystack == NULL)
14950 return; /* type error; errmsg already given */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014951 if (argvars[2].v_type != VAR_UNKNOWN)
14952 {
14953 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014954 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000014955 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014956 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014957 }
14958 else
14959 end_idx = haystack_len;
14960
Bram Moolenaar0d660222005-01-07 21:51:51 +000014961 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000014962 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014963 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014964 lastmatch = haystack + end_idx;
14965 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014966 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000014967 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014968 for (rest = haystack; *rest != '\0'; ++rest)
14969 {
14970 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000014971 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014972 break;
14973 lastmatch = rest;
14974 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000014975 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014976
14977 if (lastmatch == NULL)
14978 rettv->vval.v_number = -1;
14979 else
14980 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
14981}
14982
14983/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014984 * "strtrans()" function
14985 */
14986 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014987f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014988 typval_T *argvars;
14989 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014990{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014991 rettv->v_type = VAR_STRING;
14992 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014993}
14994
14995/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014996 * "submatch()" function
14997 */
14998 static void
14999f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015000 typval_T *argvars;
15001 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015002{
15003 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015004 rettv->vval.v_string =
15005 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015006}
15007
15008/*
15009 * "substitute()" function
15010 */
15011 static void
15012f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015013 typval_T *argvars;
15014 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015015{
15016 char_u patbuf[NUMBUFLEN];
15017 char_u subbuf[NUMBUFLEN];
15018 char_u flagsbuf[NUMBUFLEN];
15019
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015020 char_u *str = get_tv_string_chk(&argvars[0]);
15021 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15022 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
15023 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
15024
Bram Moolenaar0d660222005-01-07 21:51:51 +000015025 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015026 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
15027 rettv->vval.v_string = NULL;
15028 else
15029 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015030}
15031
15032/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015033 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015034 */
15035/*ARGSUSED*/
15036 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015037f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015038 typval_T *argvars;
15039 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015040{
15041 int id = 0;
15042#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015043 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015044 long col;
15045 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000015046 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015047
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015048 lnum = get_tv_lnum(argvars); /* -1 on type error */
15049 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
15050 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015051
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015052 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015053 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +000015054 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015055#endif
15056
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015057 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015058}
15059
15060/*
15061 * "synIDattr(id, what [, mode])" function
15062 */
15063/*ARGSUSED*/
15064 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015065f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015066 typval_T *argvars;
15067 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015068{
15069 char_u *p = NULL;
15070#ifdef FEAT_SYN_HL
15071 int id;
15072 char_u *what;
15073 char_u *mode;
15074 char_u modebuf[NUMBUFLEN];
15075 int modec;
15076
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015077 id = get_tv_number(&argvars[0]);
15078 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015079 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015080 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015081 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015082 modec = TOLOWER_ASC(mode[0]);
15083 if (modec != 't' && modec != 'c'
15084#ifdef FEAT_GUI
15085 && modec != 'g'
15086#endif
15087 )
15088 modec = 0; /* replace invalid with current */
15089 }
15090 else
15091 {
15092#ifdef FEAT_GUI
15093 if (gui.in_use)
15094 modec = 'g';
15095 else
15096#endif
15097 if (t_colors > 1)
15098 modec = 'c';
15099 else
15100 modec = 't';
15101 }
15102
15103
15104 switch (TOLOWER_ASC(what[0]))
15105 {
15106 case 'b':
15107 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
15108 p = highlight_color(id, what, modec);
15109 else /* bold */
15110 p = highlight_has_attr(id, HL_BOLD, modec);
15111 break;
15112
15113 case 'f': /* fg[#] */
15114 p = highlight_color(id, what, modec);
15115 break;
15116
15117 case 'i':
15118 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
15119 p = highlight_has_attr(id, HL_INVERSE, modec);
15120 else /* italic */
15121 p = highlight_has_attr(id, HL_ITALIC, modec);
15122 break;
15123
15124 case 'n': /* name */
15125 p = get_highlight_name(NULL, id - 1);
15126 break;
15127
15128 case 'r': /* reverse */
15129 p = highlight_has_attr(id, HL_INVERSE, modec);
15130 break;
15131
15132 case 's': /* standout */
15133 p = highlight_has_attr(id, HL_STANDOUT, modec);
15134 break;
15135
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000015136 case 'u':
15137 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
15138 /* underline */
15139 p = highlight_has_attr(id, HL_UNDERLINE, modec);
15140 else
15141 /* undercurl */
15142 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015143 break;
15144 }
15145
15146 if (p != NULL)
15147 p = vim_strsave(p);
15148#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015149 rettv->v_type = VAR_STRING;
15150 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015151}
15152
15153/*
15154 * "synIDtrans(id)" function
15155 */
15156/*ARGSUSED*/
15157 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015158f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015159 typval_T *argvars;
15160 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015161{
15162 int id;
15163
15164#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015165 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015166
15167 if (id > 0)
15168 id = syn_get_final_id(id);
15169 else
15170#endif
15171 id = 0;
15172
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015173 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015174}
15175
15176/*
15177 * "system()" function
15178 */
15179 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015180f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015181 typval_T *argvars;
15182 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015183{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015184 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015185 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015186 char_u *infile = NULL;
15187 char_u buf[NUMBUFLEN];
15188 int err = FALSE;
15189 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015190
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015191 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015192 {
15193 /*
15194 * Write the string to a temp file, to be used for input of the shell
15195 * command.
15196 */
15197 if ((infile = vim_tempname('i')) == NULL)
15198 {
15199 EMSG(_(e_notmp));
15200 return;
15201 }
15202
15203 fd = mch_fopen((char *)infile, WRITEBIN);
15204 if (fd == NULL)
15205 {
15206 EMSG2(_(e_notopen), infile);
15207 goto done;
15208 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015209 p = get_tv_string_buf_chk(&argvars[1], buf);
15210 if (p == NULL)
15211 goto done; /* type error; errmsg already given */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015212 if (fwrite(p, STRLEN(p), 1, fd) != 1)
15213 err = TRUE;
15214 if (fclose(fd) != 0)
15215 err = TRUE;
15216 if (err)
15217 {
15218 EMSG(_("E677: Error writing temp file"));
15219 goto done;
15220 }
15221 }
15222
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015223 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
15224 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015225
Bram Moolenaar071d4272004-06-13 20:20:40 +000015226#ifdef USE_CR
15227 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015228 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015229 {
15230 char_u *s;
15231
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015232 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015233 {
15234 if (*s == CAR)
15235 *s = NL;
15236 }
15237 }
15238#else
15239# ifdef USE_CRNL
15240 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015241 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015242 {
15243 char_u *s, *d;
15244
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015245 d = res;
15246 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015247 {
15248 if (s[0] == CAR && s[1] == NL)
15249 ++s;
15250 *d++ = *s;
15251 }
15252 *d = NUL;
15253 }
15254# endif
15255#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015256
15257done:
15258 if (infile != NULL)
15259 {
15260 mch_remove(infile);
15261 vim_free(infile);
15262 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015263 rettv->v_type = VAR_STRING;
15264 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015265}
15266
15267/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015268 * "tabpagebuflist()" function
15269 */
15270/* ARGSUSED */
15271 static void
15272f_tabpagebuflist(argvars, rettv)
15273 typval_T *argvars;
15274 typval_T *rettv;
15275{
15276#ifndef FEAT_WINDOWS
15277 rettv->vval.v_number = 0;
15278#else
15279 tabpage_T *tp;
15280 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015281
15282 if (argvars[0].v_type == VAR_UNKNOWN)
15283 wp = firstwin;
15284 else
15285 {
15286 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15287 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000015288 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015289 }
15290 if (wp == NULL)
15291 rettv->vval.v_number = 0;
15292 else
15293 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015294 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015295 rettv->vval.v_number = 0;
15296 else
15297 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015298 for (; wp != NULL; wp = wp->w_next)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015299 if (list_append_number(rettv->vval.v_list,
15300 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015301 break;
15302 }
15303 }
15304#endif
15305}
15306
15307
15308/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015309 * "tabpagenr()" function
15310 */
15311/* ARGSUSED */
15312 static void
15313f_tabpagenr(argvars, rettv)
15314 typval_T *argvars;
15315 typval_T *rettv;
15316{
15317 int nr = 1;
15318#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015319 char_u *arg;
15320
15321 if (argvars[0].v_type != VAR_UNKNOWN)
15322 {
15323 arg = get_tv_string_chk(&argvars[0]);
15324 nr = 0;
15325 if (arg != NULL)
15326 {
15327 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000015328 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015329 else
15330 EMSG2(_(e_invexpr2), arg);
15331 }
15332 }
15333 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015334 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015335#endif
15336 rettv->vval.v_number = nr;
15337}
15338
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015339
15340#ifdef FEAT_WINDOWS
15341static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
15342
15343/*
15344 * Common code for tabpagewinnr() and winnr().
15345 */
15346 static int
15347get_winnr(tp, argvar)
15348 tabpage_T *tp;
15349 typval_T *argvar;
15350{
15351 win_T *twin;
15352 int nr = 1;
15353 win_T *wp;
15354 char_u *arg;
15355
15356 twin = (tp == curtab) ? curwin : tp->tp_curwin;
15357 if (argvar->v_type != VAR_UNKNOWN)
15358 {
15359 arg = get_tv_string_chk(argvar);
15360 if (arg == NULL)
15361 nr = 0; /* type error; errmsg already given */
15362 else if (STRCMP(arg, "$") == 0)
15363 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
15364 else if (STRCMP(arg, "#") == 0)
15365 {
15366 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
15367 if (twin == NULL)
15368 nr = 0;
15369 }
15370 else
15371 {
15372 EMSG2(_(e_invexpr2), arg);
15373 nr = 0;
15374 }
15375 }
15376
15377 if (nr > 0)
15378 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
15379 wp != twin; wp = wp->w_next)
15380 ++nr;
15381 return nr;
15382}
15383#endif
15384
15385/*
15386 * "tabpagewinnr()" function
15387 */
15388/* ARGSUSED */
15389 static void
15390f_tabpagewinnr(argvars, rettv)
15391 typval_T *argvars;
15392 typval_T *rettv;
15393{
15394 int nr = 1;
15395#ifdef FEAT_WINDOWS
15396 tabpage_T *tp;
15397
15398 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15399 if (tp == NULL)
15400 nr = 0;
15401 else
15402 nr = get_winnr(tp, &argvars[1]);
15403#endif
15404 rettv->vval.v_number = nr;
15405}
15406
15407
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015408/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015409 * "tagfiles()" function
15410 */
15411/*ARGSUSED*/
15412 static void
15413f_tagfiles(argvars, rettv)
15414 typval_T *argvars;
15415 typval_T *rettv;
15416{
15417 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015418 tagname_T tn;
15419 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015420
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015421 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015422 {
15423 rettv->vval.v_number = 0;
15424 return;
15425 }
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015426
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015427 for (first = TRUE; ; first = FALSE)
15428 if (get_tagfname(&tn, first, fname) == FAIL
15429 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015430 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015431 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015432}
15433
15434/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000015435 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015436 */
15437 static void
15438f_taglist(argvars, rettv)
15439 typval_T *argvars;
15440 typval_T *rettv;
15441{
15442 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015443
15444 tag_pattern = get_tv_string(&argvars[0]);
15445
15446 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015447 if (*tag_pattern == NUL)
15448 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015449
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015450 if (rettv_list_alloc(rettv) == OK)
15451 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015452}
15453
15454/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015455 * "tempname()" function
15456 */
15457/*ARGSUSED*/
15458 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015459f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015460 typval_T *argvars;
15461 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015462{
15463 static int x = 'A';
15464
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015465 rettv->v_type = VAR_STRING;
15466 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015467
15468 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
15469 * names. Skip 'I' and 'O', they are used for shell redirection. */
15470 do
15471 {
15472 if (x == 'Z')
15473 x = '0';
15474 else if (x == '9')
15475 x = 'A';
15476 else
15477 {
15478#ifdef EBCDIC
15479 if (x == 'I')
15480 x = 'J';
15481 else if (x == 'R')
15482 x = 'S';
15483 else
15484#endif
15485 ++x;
15486 }
15487 } while (x == 'I' || x == 'O');
15488}
15489
15490/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000015491 * "test(list)" function: Just checking the walls...
15492 */
15493/*ARGSUSED*/
15494 static void
15495f_test(argvars, rettv)
15496 typval_T *argvars;
15497 typval_T *rettv;
15498{
15499 /* Used for unit testing. Change the code below to your liking. */
15500#if 0
15501 listitem_T *li;
15502 list_T *l;
15503 char_u *bad, *good;
15504
15505 if (argvars[0].v_type != VAR_LIST)
15506 return;
15507 l = argvars[0].vval.v_list;
15508 if (l == NULL)
15509 return;
15510 li = l->lv_first;
15511 if (li == NULL)
15512 return;
15513 bad = get_tv_string(&li->li_tv);
15514 li = li->li_next;
15515 if (li == NULL)
15516 return;
15517 good = get_tv_string(&li->li_tv);
15518 rettv->vval.v_number = test_edit_score(bad, good);
15519#endif
15520}
15521
15522/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015523 * "tolower(string)" function
15524 */
15525 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015526f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015527 typval_T *argvars;
15528 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015529{
15530 char_u *p;
15531
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015532 p = vim_strsave(get_tv_string(&argvars[0]));
15533 rettv->v_type = VAR_STRING;
15534 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015535
15536 if (p != NULL)
15537 while (*p != NUL)
15538 {
15539#ifdef FEAT_MBYTE
15540 int l;
15541
15542 if (enc_utf8)
15543 {
15544 int c, lc;
15545
15546 c = utf_ptr2char(p);
15547 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015548 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015549 /* TODO: reallocate string when byte count changes. */
15550 if (utf_char2len(lc) == l)
15551 utf_char2bytes(lc, p);
15552 p += l;
15553 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015554 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015555 p += l; /* skip multi-byte character */
15556 else
15557#endif
15558 {
15559 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
15560 ++p;
15561 }
15562 }
15563}
15564
15565/*
15566 * "toupper(string)" function
15567 */
15568 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015569f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015570 typval_T *argvars;
15571 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015572{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015573 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015574 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015575}
15576
15577/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000015578 * "tr(string, fromstr, tostr)" function
15579 */
15580 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015581f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015582 typval_T *argvars;
15583 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015584{
15585 char_u *instr;
15586 char_u *fromstr;
15587 char_u *tostr;
15588 char_u *p;
15589#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000015590 int inlen;
15591 int fromlen;
15592 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015593 int idx;
15594 char_u *cpstr;
15595 int cplen;
15596 int first = TRUE;
15597#endif
15598 char_u buf[NUMBUFLEN];
15599 char_u buf2[NUMBUFLEN];
15600 garray_T ga;
15601
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015602 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015603 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
15604 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015605
15606 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015607 rettv->v_type = VAR_STRING;
15608 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015609 if (fromstr == NULL || tostr == NULL)
15610 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000015611 ga_init2(&ga, (int)sizeof(char), 80);
15612
15613#ifdef FEAT_MBYTE
15614 if (!has_mbyte)
15615#endif
15616 /* not multi-byte: fromstr and tostr must be the same length */
15617 if (STRLEN(fromstr) != STRLEN(tostr))
15618 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015619#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000015620error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015621#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000015622 EMSG2(_(e_invarg2), fromstr);
15623 ga_clear(&ga);
15624 return;
15625 }
15626
15627 /* fromstr and tostr have to contain the same number of chars */
15628 while (*instr != NUL)
15629 {
15630#ifdef FEAT_MBYTE
15631 if (has_mbyte)
15632 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015633 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015634 cpstr = instr;
15635 cplen = inlen;
15636 idx = 0;
15637 for (p = fromstr; *p != NUL; p += fromlen)
15638 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015639 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015640 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
15641 {
15642 for (p = tostr; *p != NUL; p += tolen)
15643 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015644 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015645 if (idx-- == 0)
15646 {
15647 cplen = tolen;
15648 cpstr = p;
15649 break;
15650 }
15651 }
15652 if (*p == NUL) /* tostr is shorter than fromstr */
15653 goto error;
15654 break;
15655 }
15656 ++idx;
15657 }
15658
15659 if (first && cpstr == instr)
15660 {
15661 /* Check that fromstr and tostr have the same number of
15662 * (multi-byte) characters. Done only once when a character
15663 * of instr doesn't appear in fromstr. */
15664 first = FALSE;
15665 for (p = tostr; *p != NUL; p += tolen)
15666 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015667 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015668 --idx;
15669 }
15670 if (idx != 0)
15671 goto error;
15672 }
15673
15674 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000015675 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015676 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015677
15678 instr += inlen;
15679 }
15680 else
15681#endif
15682 {
15683 /* When not using multi-byte chars we can do it faster. */
15684 p = vim_strchr(fromstr, *instr);
15685 if (p != NULL)
15686 ga_append(&ga, tostr[p - fromstr]);
15687 else
15688 ga_append(&ga, *instr);
15689 ++instr;
15690 }
15691 }
15692
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015693 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015694}
15695
15696/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015697 * "type(expr)" function
15698 */
15699 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015700f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015701 typval_T *argvars;
15702 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015703{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015704 int n;
15705
15706 switch (argvars[0].v_type)
15707 {
15708 case VAR_NUMBER: n = 0; break;
15709 case VAR_STRING: n = 1; break;
15710 case VAR_FUNC: n = 2; break;
15711 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015712 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015713 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
15714 }
15715 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015716}
15717
15718/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015719 * "values(dict)" function
15720 */
15721 static void
15722f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015723 typval_T *argvars;
15724 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015725{
15726 dict_list(argvars, rettv, 1);
15727}
15728
15729/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015730 * "virtcol(string)" function
15731 */
15732 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015733f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015734 typval_T *argvars;
15735 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015736{
15737 colnr_T vcol = 0;
15738 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015739 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015740
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015741 fp = var2fpos(&argvars[0], FALSE, &fnum);
15742 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
15743 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015744 {
15745 getvvcol(curwin, fp, NULL, NULL, &vcol);
15746 ++vcol;
15747 }
15748
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015749 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015750}
15751
15752/*
15753 * "visualmode()" function
15754 */
15755/*ARGSUSED*/
15756 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015757f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015758 typval_T *argvars;
15759 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015760{
15761#ifdef FEAT_VISUAL
15762 char_u str[2];
15763
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015764 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015765 str[0] = curbuf->b_visual_mode_eval;
15766 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015767 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015768
15769 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015770 if ((argvars[0].v_type == VAR_NUMBER
15771 && argvars[0].vval.v_number != 0)
15772 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015773 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015774 curbuf->b_visual_mode_eval = NUL;
15775#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015776 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015777#endif
15778}
15779
15780/*
15781 * "winbufnr(nr)" function
15782 */
15783 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015784f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015785 typval_T *argvars;
15786 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015787{
15788 win_T *wp;
15789
15790 wp = find_win_by_nr(&argvars[0]);
15791 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015792 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015793 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015794 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015795}
15796
15797/*
15798 * "wincol()" function
15799 */
15800/*ARGSUSED*/
15801 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015802f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015803 typval_T *argvars;
15804 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015805{
15806 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015807 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015808}
15809
15810/*
15811 * "winheight(nr)" function
15812 */
15813 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015814f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015815 typval_T *argvars;
15816 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015817{
15818 win_T *wp;
15819
15820 wp = find_win_by_nr(&argvars[0]);
15821 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015822 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015823 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015824 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015825}
15826
15827/*
15828 * "winline()" function
15829 */
15830/*ARGSUSED*/
15831 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015832f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015833 typval_T *argvars;
15834 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015835{
15836 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015837 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015838}
15839
15840/*
15841 * "winnr()" function
15842 */
15843/* ARGSUSED */
15844 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015845f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015846 typval_T *argvars;
15847 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015848{
15849 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015850
Bram Moolenaar071d4272004-06-13 20:20:40 +000015851#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015852 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015853#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015854 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015855}
15856
15857/*
15858 * "winrestcmd()" function
15859 */
15860/* ARGSUSED */
15861 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015862f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015863 typval_T *argvars;
15864 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015865{
15866#ifdef FEAT_WINDOWS
15867 win_T *wp;
15868 int winnr = 1;
15869 garray_T ga;
15870 char_u buf[50];
15871
15872 ga_init2(&ga, (int)sizeof(char), 70);
15873 for (wp = firstwin; wp != NULL; wp = wp->w_next)
15874 {
15875 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
15876 ga_concat(&ga, buf);
15877# ifdef FEAT_VERTSPLIT
15878 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
15879 ga_concat(&ga, buf);
15880# endif
15881 ++winnr;
15882 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000015883 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015884
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015885 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015886#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015887 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015888#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015889 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015890}
15891
15892/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015893 * "winrestview()" function
15894 */
15895/* ARGSUSED */
15896 static void
15897f_winrestview(argvars, rettv)
15898 typval_T *argvars;
15899 typval_T *rettv;
15900{
15901 dict_T *dict;
15902
15903 if (argvars[0].v_type != VAR_DICT
15904 || (dict = argvars[0].vval.v_dict) == NULL)
15905 EMSG(_(e_invarg));
15906 else
15907 {
15908 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
15909 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
15910#ifdef FEAT_VIRTUALEDIT
15911 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
15912#endif
15913 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015914 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015915
15916 curwin->w_topline = get_dict_number(dict, (char_u *)"topline");
15917#ifdef FEAT_DIFF
15918 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
15919#endif
15920 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
15921 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
15922
15923 check_cursor();
15924 changed_cline_bef_curs();
15925 invalidate_botline();
15926 redraw_later(VALID);
15927
15928 if (curwin->w_topline == 0)
15929 curwin->w_topline = 1;
15930 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
15931 curwin->w_topline = curbuf->b_ml.ml_line_count;
15932#ifdef FEAT_DIFF
15933 check_topfill(curwin, TRUE);
15934#endif
15935 }
15936}
15937
15938/*
15939 * "winsaveview()" function
15940 */
15941/* ARGSUSED */
15942 static void
15943f_winsaveview(argvars, rettv)
15944 typval_T *argvars;
15945 typval_T *rettv;
15946{
15947 dict_T *dict;
15948
15949 dict = dict_alloc();
15950 if (dict == NULL)
15951 return;
15952 rettv->v_type = VAR_DICT;
15953 rettv->vval.v_dict = dict;
15954 ++dict->dv_refcount;
15955
15956 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
15957 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
15958#ifdef FEAT_VIRTUALEDIT
15959 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
15960#endif
15961 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
15962
15963 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
15964#ifdef FEAT_DIFF
15965 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
15966#endif
15967 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
15968 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
15969}
15970
15971/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015972 * "winwidth(nr)" function
15973 */
15974 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015975f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015976 typval_T *argvars;
15977 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015978{
15979 win_T *wp;
15980
15981 wp = find_win_by_nr(&argvars[0]);
15982 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015983 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015984 else
15985#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015986 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015987#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015988 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015989#endif
15990}
15991
Bram Moolenaar071d4272004-06-13 20:20:40 +000015992/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015993 * "writefile()" function
15994 */
15995 static void
15996f_writefile(argvars, rettv)
15997 typval_T *argvars;
15998 typval_T *rettv;
15999{
16000 int binary = FALSE;
16001 char_u *fname;
16002 FILE *fd;
16003 listitem_T *li;
16004 char_u *s;
16005 int ret = 0;
16006 int c;
16007
16008 if (argvars[0].v_type != VAR_LIST)
16009 {
16010 EMSG2(_(e_listarg), "writefile()");
16011 return;
16012 }
16013 if (argvars[0].vval.v_list == NULL)
16014 return;
16015
16016 if (argvars[2].v_type != VAR_UNKNOWN
16017 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
16018 binary = TRUE;
16019
16020 /* Always open the file in binary mode, library functions have a mind of
16021 * their own about CR-LF conversion. */
16022 fname = get_tv_string(&argvars[1]);
16023 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
16024 {
16025 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
16026 ret = -1;
16027 }
16028 else
16029 {
16030 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
16031 li = li->li_next)
16032 {
16033 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
16034 {
16035 if (*s == '\n')
16036 c = putc(NUL, fd);
16037 else
16038 c = putc(*s, fd);
16039 if (c == EOF)
16040 {
16041 ret = -1;
16042 break;
16043 }
16044 }
16045 if (!binary || li->li_next != NULL)
16046 if (putc('\n', fd) == EOF)
16047 {
16048 ret = -1;
16049 break;
16050 }
16051 if (ret < 0)
16052 {
16053 EMSG(_(e_write));
16054 break;
16055 }
16056 }
16057 fclose(fd);
16058 }
16059
16060 rettv->vval.v_number = ret;
16061}
16062
16063/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016064 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016065 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016066 */
16067 static pos_T *
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016068var2fpos(varp, lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000016069 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016070 int lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016071 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016072{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016073 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016074 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016075 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016076
Bram Moolenaara5525202006-03-02 22:52:09 +000016077 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016078 if (varp->v_type == VAR_LIST)
16079 {
16080 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016081 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000016082 int error = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016083
16084 l = varp->vval.v_list;
16085 if (l == NULL)
16086 return NULL;
16087
16088 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016089 pos.lnum = list_find_nr(l, 0L, &error);
16090 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016091 return NULL; /* invalid line number */
16092
16093 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016094 pos.col = list_find_nr(l, 1L, &error);
16095 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016096 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016097 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaara5525202006-03-02 22:52:09 +000016098 /* Accept a position up to the NUL after the line. */
16099 if (pos.col <= 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016100 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016101 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016102
Bram Moolenaara5525202006-03-02 22:52:09 +000016103#ifdef FEAT_VIRTUALEDIT
16104 /* Get the virtual offset. Defaults to zero. */
16105 pos.coladd = list_find_nr(l, 2L, &error);
16106 if (error)
16107 pos.coladd = 0;
16108#endif
16109
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016110 return &pos;
16111 }
16112
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016113 name = get_tv_string_chk(varp);
16114 if (name == NULL)
16115 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016116 if (name[0] == '.') /* cursor */
16117 return &curwin->w_cursor;
16118 if (name[0] == '\'') /* mark */
16119 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016120 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016121 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
16122 return NULL;
16123 return pp;
16124 }
Bram Moolenaara5525202006-03-02 22:52:09 +000016125
16126#ifdef FEAT_VIRTUALEDIT
16127 pos.coladd = 0;
16128#endif
16129
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016130 if (name[0] == 'w' && lnum)
16131 {
16132 pos.col = 0;
16133 if (name[1] == '0') /* "w0": first visible line */
16134 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016135 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016136 pos.lnum = curwin->w_topline;
16137 return &pos;
16138 }
16139 else if (name[1] == '$') /* "w$": last visible line */
16140 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016141 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016142 pos.lnum = curwin->w_botline - 1;
16143 return &pos;
16144 }
16145 }
16146 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016147 {
16148 if (lnum)
16149 {
16150 pos.lnum = curbuf->b_ml.ml_line_count;
16151 pos.col = 0;
16152 }
16153 else
16154 {
16155 pos.lnum = curwin->w_cursor.lnum;
16156 pos.col = (colnr_T)STRLEN(ml_get_curline());
16157 }
16158 return &pos;
16159 }
16160 return NULL;
16161}
16162
16163/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016164 * Convert list in "arg" into a position and optional file number.
16165 * When "fnump" is NULL there is no file number, only 3 items.
16166 * Note that the column is passed on as-is, the caller may want to decrement
16167 * it to use 1 for the first column.
16168 * Return FAIL when conversion is not possible, doesn't check the position for
16169 * validity.
16170 */
16171 static int
16172list2fpos(arg, posp, fnump)
16173 typval_T *arg;
16174 pos_T *posp;
16175 int *fnump;
16176{
16177 list_T *l = arg->vval.v_list;
16178 long i = 0;
16179 long n;
16180
16181 /* List must be: [fnum, lnum, col, coladd] */
16182 if (arg->v_type != VAR_LIST || l == NULL
16183 || l->lv_len != (fnump == NULL ? 3 : 4))
16184 return FAIL;
16185
16186 if (fnump != NULL)
16187 {
16188 n = list_find_nr(l, i++, NULL); /* fnum */
16189 if (n < 0)
16190 return FAIL;
16191 if (n == 0)
16192 n = curbuf->b_fnum; /* current buffer */
16193 *fnump = n;
16194 }
16195
16196 n = list_find_nr(l, i++, NULL); /* lnum */
16197 if (n < 0)
16198 return FAIL;
16199 posp->lnum = n;
16200
16201 n = list_find_nr(l, i++, NULL); /* col */
16202 if (n < 0)
16203 return FAIL;
16204 posp->col = n;
16205
16206#ifdef FEAT_VIRTUALEDIT
16207 n = list_find_nr(l, i, NULL);
16208 if (n < 0)
16209 return FAIL;
16210 posp->coladd = n;
16211#endif
16212
16213 return OK;
16214}
16215
16216/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016217 * Get the length of an environment variable name.
16218 * Advance "arg" to the first character after the name.
16219 * Return 0 for error.
16220 */
16221 static int
16222get_env_len(arg)
16223 char_u **arg;
16224{
16225 char_u *p;
16226 int len;
16227
16228 for (p = *arg; vim_isIDc(*p); ++p)
16229 ;
16230 if (p == *arg) /* no name found */
16231 return 0;
16232
16233 len = (int)(p - *arg);
16234 *arg = p;
16235 return len;
16236}
16237
16238/*
16239 * Get the length of the name of a function or internal variable.
16240 * "arg" is advanced to the first non-white character after the name.
16241 * Return 0 if something is wrong.
16242 */
16243 static int
16244get_id_len(arg)
16245 char_u **arg;
16246{
16247 char_u *p;
16248 int len;
16249
16250 /* Find the end of the name. */
16251 for (p = *arg; eval_isnamec(*p); ++p)
16252 ;
16253 if (p == *arg) /* no name found */
16254 return 0;
16255
16256 len = (int)(p - *arg);
16257 *arg = skipwhite(p);
16258
16259 return len;
16260}
16261
16262/*
Bram Moolenaara7043832005-01-21 11:56:39 +000016263 * Get the length of the name of a variable or function.
16264 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000016265 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016266 * Return -1 if curly braces expansion failed.
16267 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016268 * If the name contains 'magic' {}'s, expand them and return the
16269 * expanded name in an allocated string via 'alias' - caller must free.
16270 */
16271 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016272get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016273 char_u **arg;
16274 char_u **alias;
16275 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016276 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016277{
16278 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016279 char_u *p;
16280 char_u *expr_start;
16281 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016282
16283 *alias = NULL; /* default to no alias */
16284
16285 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
16286 && (*arg)[2] == (int)KE_SNR)
16287 {
16288 /* hard coded <SNR>, already translated */
16289 *arg += 3;
16290 return get_id_len(arg) + 3;
16291 }
16292 len = eval_fname_script(*arg);
16293 if (len > 0)
16294 {
16295 /* literal "<SID>", "s:" or "<SNR>" */
16296 *arg += len;
16297 }
16298
Bram Moolenaar071d4272004-06-13 20:20:40 +000016299 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016300 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016301 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016302 p = find_name_end(*arg, &expr_start, &expr_end,
16303 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016304 if (expr_start != NULL)
16305 {
16306 char_u *temp_string;
16307
16308 if (!evaluate)
16309 {
16310 len += (int)(p - *arg);
16311 *arg = skipwhite(p);
16312 return len;
16313 }
16314
16315 /*
16316 * Include any <SID> etc in the expanded string:
16317 * Thus the -len here.
16318 */
16319 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
16320 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016321 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016322 *alias = temp_string;
16323 *arg = skipwhite(p);
16324 return (int)STRLEN(temp_string);
16325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016326
16327 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016328 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016329 EMSG2(_(e_invexpr2), *arg);
16330
16331 return len;
16332}
16333
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016334/*
16335 * Find the end of a variable or function name, taking care of magic braces.
16336 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
16337 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016338 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016339 * Return a pointer to just after the name. Equal to "arg" if there is no
16340 * valid name.
16341 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016342 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016343find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016344 char_u *arg;
16345 char_u **expr_start;
16346 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016347 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016348{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016349 int mb_nest = 0;
16350 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016351 char_u *p;
16352
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016353 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016354 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016355 *expr_start = NULL;
16356 *expr_end = NULL;
16357 }
16358
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016359 /* Quick check for valid starting character. */
16360 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
16361 return arg;
16362
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016363 for (p = arg; *p != NUL
16364 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016365 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016366 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016367 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000016368 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016369 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000016370 if (*p == '\'')
16371 {
16372 /* skip over 'string' to avoid counting [ and ] inside it. */
16373 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
16374 ;
16375 if (*p == NUL)
16376 break;
16377 }
16378 else if (*p == '"')
16379 {
16380 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
16381 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
16382 if (*p == '\\' && p[1] != NUL)
16383 ++p;
16384 if (*p == NUL)
16385 break;
16386 }
16387
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016388 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016389 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016390 if (*p == '[')
16391 ++br_nest;
16392 else if (*p == ']')
16393 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016394 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000016395
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016396 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016397 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016398 if (*p == '{')
16399 {
16400 mb_nest++;
16401 if (expr_start != NULL && *expr_start == NULL)
16402 *expr_start = p;
16403 }
16404 else if (*p == '}')
16405 {
16406 mb_nest--;
16407 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
16408 *expr_end = p;
16409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016411 }
16412
16413 return p;
16414}
16415
16416/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016417 * Expands out the 'magic' {}'s in a variable/function name.
16418 * Note that this can call itself recursively, to deal with
16419 * constructs like foo{bar}{baz}{bam}
16420 * The four pointer arguments point to "foo{expre}ss{ion}bar"
16421 * "in_start" ^
16422 * "expr_start" ^
16423 * "expr_end" ^
16424 * "in_end" ^
16425 *
16426 * Returns a new allocated string, which the caller must free.
16427 * Returns NULL for failure.
16428 */
16429 static char_u *
16430make_expanded_name(in_start, expr_start, expr_end, in_end)
16431 char_u *in_start;
16432 char_u *expr_start;
16433 char_u *expr_end;
16434 char_u *in_end;
16435{
16436 char_u c1;
16437 char_u *retval = NULL;
16438 char_u *temp_result;
16439 char_u *nextcmd = NULL;
16440
16441 if (expr_end == NULL || in_end == NULL)
16442 return NULL;
16443 *expr_start = NUL;
16444 *expr_end = NUL;
16445 c1 = *in_end;
16446 *in_end = NUL;
16447
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016448 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016449 if (temp_result != NULL && nextcmd == NULL)
16450 {
16451 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
16452 + (in_end - expr_end) + 1));
16453 if (retval != NULL)
16454 {
16455 STRCPY(retval, in_start);
16456 STRCAT(retval, temp_result);
16457 STRCAT(retval, expr_end + 1);
16458 }
16459 }
16460 vim_free(temp_result);
16461
16462 *in_end = c1; /* put char back for error messages */
16463 *expr_start = '{';
16464 *expr_end = '}';
16465
16466 if (retval != NULL)
16467 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016468 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016469 if (expr_start != NULL)
16470 {
16471 /* Further expansion! */
16472 temp_result = make_expanded_name(retval, expr_start,
16473 expr_end, temp_result);
16474 vim_free(retval);
16475 retval = temp_result;
16476 }
16477 }
16478
16479 return retval;
16480}
16481
16482/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016483 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016484 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016485 */
16486 static int
16487eval_isnamec(c)
16488 int c;
16489{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016490 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
16491}
16492
16493/*
16494 * Return TRUE if character "c" can be used as the first character in a
16495 * variable or function name (excluding '{' and '}').
16496 */
16497 static int
16498eval_isnamec1(c)
16499 int c;
16500{
16501 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000016502}
16503
16504/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016505 * Set number v: variable to "val".
16506 */
16507 void
16508set_vim_var_nr(idx, val)
16509 int idx;
16510 long val;
16511{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016512 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016513}
16514
16515/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016516 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016517 */
16518 long
16519get_vim_var_nr(idx)
16520 int idx;
16521{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016522 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016523}
16524
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016525#if defined(FEAT_AUTOCMD) || defined(PROTO)
16526/*
16527 * Get string v: variable value. Uses a static buffer, can only be used once.
16528 */
16529 char_u *
16530get_vim_var_str(idx)
16531 int idx;
16532{
16533 return get_tv_string(&vimvars[idx].vv_tv);
16534}
16535#endif
16536
Bram Moolenaar071d4272004-06-13 20:20:40 +000016537/*
16538 * Set v:count, v:count1 and v:prevcount.
16539 */
16540 void
16541set_vcount(count, count1)
16542 long count;
16543 long count1;
16544{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016545 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
16546 vimvars[VV_COUNT].vv_nr = count;
16547 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016548}
16549
16550/*
16551 * Set string v: variable to a copy of "val".
16552 */
16553 void
16554set_vim_var_string(idx, val, len)
16555 int idx;
16556 char_u *val;
16557 int len; /* length of "val" to use or -1 (whole string) */
16558{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016559 /* Need to do this (at least) once, since we can't initialize a union.
16560 * Will always be invoked when "v:progname" is set. */
16561 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
16562
Bram Moolenaare9a41262005-01-15 22:18:47 +000016563 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016564 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016565 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016566 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016567 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016568 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000016569 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016570}
16571
16572/*
16573 * Set v:register if needed.
16574 */
16575 void
16576set_reg_var(c)
16577 int c;
16578{
16579 char_u regname;
16580
16581 if (c == 0 || c == ' ')
16582 regname = '"';
16583 else
16584 regname = c;
16585 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000016586 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016587 set_vim_var_string(VV_REG, &regname, 1);
16588}
16589
16590/*
16591 * Get or set v:exception. If "oldval" == NULL, return the current value.
16592 * Otherwise, restore the value to "oldval" and return NULL.
16593 * Must always be called in pairs to save and restore v:exception! Does not
16594 * take care of memory allocations.
16595 */
16596 char_u *
16597v_exception(oldval)
16598 char_u *oldval;
16599{
16600 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016601 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016602
Bram Moolenaare9a41262005-01-15 22:18:47 +000016603 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016604 return NULL;
16605}
16606
16607/*
16608 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
16609 * Otherwise, restore the value to "oldval" and return NULL.
16610 * Must always be called in pairs to save and restore v:throwpoint! Does not
16611 * take care of memory allocations.
16612 */
16613 char_u *
16614v_throwpoint(oldval)
16615 char_u *oldval;
16616{
16617 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016618 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016619
Bram Moolenaare9a41262005-01-15 22:18:47 +000016620 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016621 return NULL;
16622}
16623
16624#if defined(FEAT_AUTOCMD) || defined(PROTO)
16625/*
16626 * Set v:cmdarg.
16627 * If "eap" != NULL, use "eap" to generate the value and return the old value.
16628 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
16629 * Must always be called in pairs!
16630 */
16631 char_u *
16632set_cmdarg(eap, oldarg)
16633 exarg_T *eap;
16634 char_u *oldarg;
16635{
16636 char_u *oldval;
16637 char_u *newval;
16638 unsigned len;
16639
Bram Moolenaare9a41262005-01-15 22:18:47 +000016640 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016641 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016642 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016643 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000016644 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016645 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016646 }
16647
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016648 if (eap->force_bin == FORCE_BIN)
16649 len = 6;
16650 else if (eap->force_bin == FORCE_NOBIN)
16651 len = 8;
16652 else
16653 len = 0;
16654 if (eap->force_ff != 0)
16655 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
16656# ifdef FEAT_MBYTE
16657 if (eap->force_enc != 0)
16658 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000016659 if (eap->bad_char != 0)
16660 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016661# endif
16662
16663 newval = alloc(len + 1);
16664 if (newval == NULL)
16665 return NULL;
16666
16667 if (eap->force_bin == FORCE_BIN)
16668 sprintf((char *)newval, " ++bin");
16669 else if (eap->force_bin == FORCE_NOBIN)
16670 sprintf((char *)newval, " ++nobin");
16671 else
16672 *newval = NUL;
16673 if (eap->force_ff != 0)
16674 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
16675 eap->cmd + eap->force_ff);
16676# ifdef FEAT_MBYTE
16677 if (eap->force_enc != 0)
16678 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
16679 eap->cmd + eap->force_enc);
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000016680 if (eap->bad_char != 0)
16681 sprintf((char *)newval + STRLEN(newval), " ++bad=%s",
16682 eap->cmd + eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016683# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000016684 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016685 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016686}
16687#endif
16688
16689/*
16690 * Get the value of internal variable "name".
16691 * Return OK or FAIL.
16692 */
16693 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016694get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016695 char_u *name;
16696 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000016697 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016698 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016699{
16700 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000016701 typval_T *tv = NULL;
16702 typval_T atv;
16703 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016704 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016705
16706 /* truncate the name, so that we can use strcmp() */
16707 cc = name[len];
16708 name[len] = NUL;
16709
16710 /*
16711 * Check for "b:changedtick".
16712 */
16713 if (STRCMP(name, "b:changedtick") == 0)
16714 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000016715 atv.v_type = VAR_NUMBER;
16716 atv.vval.v_number = curbuf->b_changedtick;
16717 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016718 }
16719
16720 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016721 * Check for user-defined variables.
16722 */
16723 else
16724 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016725 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016726 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016727 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016728 }
16729
Bram Moolenaare9a41262005-01-15 22:18:47 +000016730 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016731 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016732 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016733 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016734 ret = FAIL;
16735 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016736 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016737 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016738
16739 name[len] = cc;
16740
16741 return ret;
16742}
16743
16744/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016745 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
16746 * Also handle function call with Funcref variable: func(expr)
16747 * Can all be combined: dict.func(expr)[idx]['func'](expr)
16748 */
16749 static int
16750handle_subscript(arg, rettv, evaluate, verbose)
16751 char_u **arg;
16752 typval_T *rettv;
16753 int evaluate; /* do more than finding the end */
16754 int verbose; /* give error messages */
16755{
16756 int ret = OK;
16757 dict_T *selfdict = NULL;
16758 char_u *s;
16759 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000016760 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016761
16762 while (ret == OK
16763 && (**arg == '['
16764 || (**arg == '.' && rettv->v_type == VAR_DICT)
16765 || (**arg == '(' && rettv->v_type == VAR_FUNC))
16766 && !vim_iswhite(*(*arg - 1)))
16767 {
16768 if (**arg == '(')
16769 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000016770 /* need to copy the funcref so that we can clear rettv */
16771 functv = *rettv;
16772 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016773
16774 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000016775 s = functv.vval.v_string;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016776 ret = get_func_tv(s, STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000016777 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
16778 &len, evaluate, selfdict);
16779
16780 /* Clear the funcref afterwards, so that deleting it while
16781 * evaluating the arguments is possible (see test55). */
16782 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016783
16784 /* Stop the expression evaluation when immediately aborting on
16785 * error, or when an interrupt occurred or an exception was thrown
16786 * but not caught. */
16787 if (aborting())
16788 {
16789 if (ret == OK)
16790 clear_tv(rettv);
16791 ret = FAIL;
16792 }
16793 dict_unref(selfdict);
16794 selfdict = NULL;
16795 }
16796 else /* **arg == '[' || **arg == '.' */
16797 {
16798 dict_unref(selfdict);
16799 if (rettv->v_type == VAR_DICT)
16800 {
16801 selfdict = rettv->vval.v_dict;
16802 if (selfdict != NULL)
16803 ++selfdict->dv_refcount;
16804 }
16805 else
16806 selfdict = NULL;
16807 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
16808 {
16809 clear_tv(rettv);
16810 ret = FAIL;
16811 }
16812 }
16813 }
16814 dict_unref(selfdict);
16815 return ret;
16816}
16817
16818/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016819 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
16820 * value).
16821 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016822 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016823alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016824{
Bram Moolenaar33570922005-01-25 22:26:29 +000016825 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016826}
16827
16828/*
16829 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016830 * The string "s" must have been allocated, it is consumed.
16831 * Return NULL for out of memory, the variable otherwise.
16832 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016833 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016834alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016835 char_u *s;
16836{
Bram Moolenaar33570922005-01-25 22:26:29 +000016837 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016838
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016839 rettv = alloc_tv();
16840 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016841 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016842 rettv->v_type = VAR_STRING;
16843 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016844 }
16845 else
16846 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016847 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016848}
16849
16850/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016851 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016852 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000016853 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016854free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016855 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016856{
16857 if (varp != NULL)
16858 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016859 switch (varp->v_type)
16860 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016861 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016862 func_unref(varp->vval.v_string);
16863 /*FALLTHROUGH*/
16864 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016865 vim_free(varp->vval.v_string);
16866 break;
16867 case VAR_LIST:
16868 list_unref(varp->vval.v_list);
16869 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016870 case VAR_DICT:
16871 dict_unref(varp->vval.v_dict);
16872 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016873 case VAR_NUMBER:
16874 case VAR_UNKNOWN:
16875 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016876 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000016877 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016878 break;
16879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016880 vim_free(varp);
16881 }
16882}
16883
16884/*
16885 * Free the memory for a variable value and set the value to NULL or 0.
16886 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000016887 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016888clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016889 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016890{
16891 if (varp != NULL)
16892 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016893 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016894 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016895 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016896 func_unref(varp->vval.v_string);
16897 /*FALLTHROUGH*/
16898 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016899 vim_free(varp->vval.v_string);
16900 varp->vval.v_string = NULL;
16901 break;
16902 case VAR_LIST:
16903 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016904 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016905 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016906 case VAR_DICT:
16907 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016908 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016909 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016910 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016911 varp->vval.v_number = 0;
16912 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016913 case VAR_UNKNOWN:
16914 break;
16915 default:
16916 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016917 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016918 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016919 }
16920}
16921
16922/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016923 * Set the value of a variable to NULL without freeing items.
16924 */
16925 static void
16926init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016927 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016928{
16929 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016930 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016931}
16932
16933/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016934 * Get the number value of a variable.
16935 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016936 * For incompatible types, return 0.
16937 * get_tv_number_chk() is similar to get_tv_number(), but informs the
16938 * caller of incompatible types: it sets *denote to TRUE if "denote"
16939 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016940 */
16941 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016942get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016943 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016944{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016945 int error = FALSE;
16946
16947 return get_tv_number_chk(varp, &error); /* return 0L on error */
16948}
16949
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016950 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016951get_tv_number_chk(varp, denote)
16952 typval_T *varp;
16953 int *denote;
16954{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016955 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016956
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016957 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016958 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016959 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016960 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016961 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016962 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016963 break;
16964 case VAR_STRING:
16965 if (varp->vval.v_string != NULL)
16966 vim_str2nr(varp->vval.v_string, NULL, NULL,
16967 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016968 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016969 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000016970 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016971 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016972 case VAR_DICT:
16973 EMSG(_("E728: Using a Dictionary as a number"));
16974 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016975 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016976 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016977 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016978 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016979 if (denote == NULL) /* useful for values that must be unsigned */
16980 n = -1;
16981 else
16982 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016983 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016984}
16985
16986/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000016987 * Get the lnum from the first argument.
16988 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016989 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016990 */
16991 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016992get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000016993 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016994{
Bram Moolenaar33570922005-01-25 22:26:29 +000016995 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016996 linenr_T lnum;
16997
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016998 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016999 if (lnum == 0) /* no valid number, try using line() */
17000 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017001 rettv.v_type = VAR_NUMBER;
17002 f_line(argvars, &rettv);
17003 lnum = rettv.vval.v_number;
17004 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017005 }
17006 return lnum;
17007}
17008
17009/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000017010 * Get the lnum from the first argument.
17011 * Also accepts "$", then "buf" is used.
17012 * Returns 0 on error.
17013 */
17014 static linenr_T
17015get_tv_lnum_buf(argvars, buf)
17016 typval_T *argvars;
17017 buf_T *buf;
17018{
17019 if (argvars[0].v_type == VAR_STRING
17020 && argvars[0].vval.v_string != NULL
17021 && argvars[0].vval.v_string[0] == '$'
17022 && buf != NULL)
17023 return buf->b_ml.ml_line_count;
17024 return get_tv_number_chk(&argvars[0], NULL);
17025}
17026
17027/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017028 * Get the string value of a variable.
17029 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000017030 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
17031 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017032 * If the String variable has never been set, return an empty string.
17033 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017034 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
17035 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017036 */
17037 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017038get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017039 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017040{
17041 static char_u mybuf[NUMBUFLEN];
17042
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017043 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017044}
17045
17046 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017047get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000017048 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017049 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017050{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017051 char_u *res = get_tv_string_buf_chk(varp, buf);
17052
17053 return res != NULL ? res : (char_u *)"";
17054}
17055
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017056 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017057get_tv_string_chk(varp)
17058 typval_T *varp;
17059{
17060 static char_u mybuf[NUMBUFLEN];
17061
17062 return get_tv_string_buf_chk(varp, mybuf);
17063}
17064
17065 static char_u *
17066get_tv_string_buf_chk(varp, buf)
17067 typval_T *varp;
17068 char_u *buf;
17069{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017070 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017071 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017072 case VAR_NUMBER:
17073 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
17074 return buf;
17075 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017076 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017077 break;
17078 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017079 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000017080 break;
17081 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017082 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017083 break;
17084 case VAR_STRING:
17085 if (varp->vval.v_string != NULL)
17086 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017087 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017088 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017089 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017090 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017091 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017092 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017093}
17094
17095/*
17096 * Find variable "name" in the list of variables.
17097 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017098 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017099 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000017100 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017101 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017102 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017103find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017104 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017105 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017106{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017107 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017108 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017109
Bram Moolenaara7043832005-01-21 11:56:39 +000017110 ht = find_var_ht(name, &varname);
17111 if (htp != NULL)
17112 *htp = ht;
17113 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017114 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017115 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017116}
17117
17118/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017119 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000017120 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017121 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017122 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017123find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000017124 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000017125 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017126 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000017127{
Bram Moolenaar33570922005-01-25 22:26:29 +000017128 hashitem_T *hi;
17129
17130 if (*varname == NUL)
17131 {
17132 /* Must be something like "s:", otherwise "ht" would be NULL. */
17133 switch (varname[-2])
17134 {
17135 case 's': return &SCRIPT_SV(current_SID).sv_var;
17136 case 'g': return &globvars_var;
17137 case 'v': return &vimvars_var;
17138 case 'b': return &curbuf->b_bufvar;
17139 case 'w': return &curwin->w_winvar;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017140 case 'l': return current_funccal == NULL
17141 ? NULL : &current_funccal->l_vars_var;
17142 case 'a': return current_funccal == NULL
17143 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000017144 }
17145 return NULL;
17146 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017147
17148 hi = hash_find(ht, varname);
17149 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017150 {
17151 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017152 * worked find the variable again. Don't auto-load a script if it was
17153 * loaded already, otherwise it would be loaded every time when
17154 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017155 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017156 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017157 hi = hash_find(ht, varname);
17158 if (HASHITEM_EMPTY(hi))
17159 return NULL;
17160 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017161 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017162}
17163
17164/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017165 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017166 * Set "varname" to the start of name without ':'.
17167 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017168 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017169find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017170 char_u *name;
17171 char_u **varname;
17172{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017173 hashitem_T *hi;
17174
Bram Moolenaar071d4272004-06-13 20:20:40 +000017175 if (name[1] != ':')
17176 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017177 /* The name must not start with a colon or #. */
17178 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017179 return NULL;
17180 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017181
17182 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017183 hi = hash_find(&compat_hashtab, name);
17184 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000017185 return &compat_hashtab;
17186
Bram Moolenaar071d4272004-06-13 20:20:40 +000017187 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017188 return &globvarht; /* global variable */
17189 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017190 }
17191 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017192 if (*name == 'g') /* global variable */
17193 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017194 /* There must be no ':' or '#' in the rest of the name, unless g: is used
17195 */
17196 if (vim_strchr(name + 2, ':') != NULL
17197 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017198 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017199 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017200 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017201 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017202 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000017203 if (*name == 'v') /* v: variable */
17204 return &vimvarht;
17205 if (*name == 'a' && current_funccal != NULL) /* function argument */
17206 return &current_funccal->l_avars.dv_hashtab;
17207 if (*name == 'l' && current_funccal != NULL) /* local function variable */
17208 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017209 if (*name == 's' /* script variable */
17210 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
17211 return &SCRIPT_VARS(current_SID);
17212 return NULL;
17213}
17214
17215/*
17216 * Get the string value of a (global/local) variable.
17217 * Returns NULL when it doesn't exist.
17218 */
17219 char_u *
17220get_var_value(name)
17221 char_u *name;
17222{
Bram Moolenaar33570922005-01-25 22:26:29 +000017223 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017224
Bram Moolenaara7043832005-01-21 11:56:39 +000017225 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017226 if (v == NULL)
17227 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000017228 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017229}
17230
17231/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017232 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000017233 * sourcing this script and when executing functions defined in the script.
17234 */
17235 void
17236new_script_vars(id)
17237 scid_T id;
17238{
Bram Moolenaara7043832005-01-21 11:56:39 +000017239 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000017240 hashtab_T *ht;
17241 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000017242
Bram Moolenaar071d4272004-06-13 20:20:40 +000017243 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
17244 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017245 /* Re-allocating ga_data means that an ht_array pointing to
17246 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000017247 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000017248 for (i = 1; i <= ga_scripts.ga_len; ++i)
17249 {
17250 ht = &SCRIPT_VARS(i);
17251 if (ht->ht_mask == HT_INIT_SIZE - 1)
17252 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000017253 sv = &SCRIPT_SV(i);
17254 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000017255 }
17256
Bram Moolenaar071d4272004-06-13 20:20:40 +000017257 while (ga_scripts.ga_len < id)
17258 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017259 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
17260 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017261 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017262 }
17263 }
17264}
17265
17266/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017267 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
17268 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017269 */
17270 void
Bram Moolenaar33570922005-01-25 22:26:29 +000017271init_var_dict(dict, dict_var)
17272 dict_T *dict;
17273 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017274{
Bram Moolenaar33570922005-01-25 22:26:29 +000017275 hash_init(&dict->dv_hashtab);
17276 dict->dv_refcount = 99999;
17277 dict_var->di_tv.vval.v_dict = dict;
17278 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017279 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017280 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
17281 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017282}
17283
17284/*
17285 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000017286 * Frees all allocated variables and the value they contain.
17287 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017288 */
17289 void
Bram Moolenaara7043832005-01-21 11:56:39 +000017290vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000017291 hashtab_T *ht;
17292{
17293 vars_clear_ext(ht, TRUE);
17294}
17295
17296/*
17297 * Like vars_clear(), but only free the value if "free_val" is TRUE.
17298 */
17299 static void
17300vars_clear_ext(ht, free_val)
17301 hashtab_T *ht;
17302 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017303{
Bram Moolenaara7043832005-01-21 11:56:39 +000017304 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000017305 hashitem_T *hi;
17306 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017307
Bram Moolenaar33570922005-01-25 22:26:29 +000017308 hash_lock(ht);
Bram Moolenaara7043832005-01-21 11:56:39 +000017309 todo = ht->ht_used;
17310 for (hi = ht->ht_array; todo > 0; ++hi)
17311 {
17312 if (!HASHITEM_EMPTY(hi))
17313 {
17314 --todo;
17315
Bram Moolenaar33570922005-01-25 22:26:29 +000017316 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000017317 * ht_array might change then. hash_clear() takes care of it
17318 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017319 v = HI2DI(hi);
17320 if (free_val)
17321 clear_tv(&v->di_tv);
17322 if ((v->di_flags & DI_FLAGS_FIX) == 0)
17323 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000017324 }
17325 }
17326 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017327 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017328}
17329
Bram Moolenaara7043832005-01-21 11:56:39 +000017330/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017331 * Delete a variable from hashtab "ht" at item "hi".
17332 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000017333 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017334 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000017335delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000017336 hashtab_T *ht;
17337 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017338{
Bram Moolenaar33570922005-01-25 22:26:29 +000017339 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017340
17341 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000017342 clear_tv(&di->di_tv);
17343 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017344}
17345
17346/*
17347 * List the value of one internal variable.
17348 */
17349 static void
17350list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000017351 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017352 char_u *prefix;
17353{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017354 char_u *tofree;
17355 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017356 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017357
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017358 s = echo_string(&v->di_tv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000017359 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017360 s == NULL ? (char_u *)"" : s);
17361 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017362}
17363
Bram Moolenaar071d4272004-06-13 20:20:40 +000017364 static void
17365list_one_var_a(prefix, name, type, string)
17366 char_u *prefix;
17367 char_u *name;
17368 int type;
17369 char_u *string;
17370{
17371 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
17372 if (name != NULL) /* "a:" vars don't have a name stored */
17373 msg_puts(name);
17374 msg_putchar(' ');
17375 msg_advance(22);
17376 if (type == VAR_NUMBER)
17377 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017378 else if (type == VAR_FUNC)
17379 msg_putchar('*');
17380 else if (type == VAR_LIST)
17381 {
17382 msg_putchar('[');
17383 if (*string == '[')
17384 ++string;
17385 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017386 else if (type == VAR_DICT)
17387 {
17388 msg_putchar('{');
17389 if (*string == '{')
17390 ++string;
17391 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017392 else
17393 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017394
Bram Moolenaar071d4272004-06-13 20:20:40 +000017395 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017396
17397 if (type == VAR_FUNC)
17398 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017399}
17400
17401/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017402 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000017403 * If the variable already exists, the value is updated.
17404 * Otherwise the variable is created.
17405 */
17406 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017407set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017408 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017409 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017410 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017411{
Bram Moolenaar33570922005-01-25 22:26:29 +000017412 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017413 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017414 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017415 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017416
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017417 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017418 {
17419 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
17420 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
17421 ? name[2] : name[0]))
17422 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017423 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017424 return;
17425 }
17426 if (function_exists(name))
17427 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017428 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017429 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017430 return;
17431 }
17432 }
17433
Bram Moolenaara7043832005-01-21 11:56:39 +000017434 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000017435 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000017436 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000017437 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000017438 return;
17439 }
17440
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017441 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017442 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017443 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017444 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017445 if (var_check_ro(v->di_flags, name)
17446 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000017447 return;
17448 if (v->di_tv.v_type != tv->v_type
17449 && !((v->di_tv.v_type == VAR_STRING
17450 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017451 && (tv->v_type == VAR_STRING
17452 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017453 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017454 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017455 return;
17456 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017457
17458 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000017459 * Handle setting internal v: variables separately: we don't change
17460 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000017461 */
17462 if (ht == &vimvarht)
17463 {
17464 if (v->di_tv.v_type == VAR_STRING)
17465 {
17466 vim_free(v->di_tv.vval.v_string);
17467 if (copy || tv->v_type != VAR_STRING)
17468 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
17469 else
17470 {
17471 /* Take over the string to avoid an extra alloc/free. */
17472 v->di_tv.vval.v_string = tv->vval.v_string;
17473 tv->vval.v_string = NULL;
17474 }
17475 }
17476 else if (v->di_tv.v_type != VAR_NUMBER)
17477 EMSG2(_(e_intern2), "set_var()");
17478 else
17479 v->di_tv.vval.v_number = get_tv_number(tv);
17480 return;
17481 }
17482
17483 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017484 }
17485 else /* add a new variable */
17486 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000017487 /* Make sure the variable name is valid. */
17488 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000017489 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
17490 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000017491 {
17492 EMSG2(_(e_illvar), varname);
17493 return;
17494 }
17495
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017496 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
17497 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000017498 if (v == NULL)
17499 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000017500 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000017501 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017502 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017503 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017504 return;
17505 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017506 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017507 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017508
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017509 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000017510 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017511 else
17512 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017513 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017514 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017515 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017517}
17518
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017519/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017520 * Return TRUE if di_flags "flags" indicate read-only variable "name".
17521 * Also give an error message.
17522 */
17523 static int
17524var_check_ro(flags, name)
17525 int flags;
17526 char_u *name;
17527{
17528 if (flags & DI_FLAGS_RO)
17529 {
17530 EMSG2(_(e_readonlyvar), name);
17531 return TRUE;
17532 }
17533 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
17534 {
17535 EMSG2(_(e_readonlysbx), name);
17536 return TRUE;
17537 }
17538 return FALSE;
17539}
17540
17541/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017542 * Return TRUE if typeval "tv" is set to be locked (immutable).
17543 * Also give an error message, using "name".
17544 */
17545 static int
17546tv_check_lock(lock, name)
17547 int lock;
17548 char_u *name;
17549{
17550 if (lock & VAR_LOCKED)
17551 {
17552 EMSG2(_("E741: Value is locked: %s"),
17553 name == NULL ? (char_u *)_("Unknown") : name);
17554 return TRUE;
17555 }
17556 if (lock & VAR_FIXED)
17557 {
17558 EMSG2(_("E742: Cannot change value of %s"),
17559 name == NULL ? (char_u *)_("Unknown") : name);
17560 return TRUE;
17561 }
17562 return FALSE;
17563}
17564
17565/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017566 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017567 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017568 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017569 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017570 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017571copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000017572 typval_T *from;
17573 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017574{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017575 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017576 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017577 switch (from->v_type)
17578 {
17579 case VAR_NUMBER:
17580 to->vval.v_number = from->vval.v_number;
17581 break;
17582 case VAR_STRING:
17583 case VAR_FUNC:
17584 if (from->vval.v_string == NULL)
17585 to->vval.v_string = NULL;
17586 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017587 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017588 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017589 if (from->v_type == VAR_FUNC)
17590 func_ref(to->vval.v_string);
17591 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017592 break;
17593 case VAR_LIST:
17594 if (from->vval.v_list == NULL)
17595 to->vval.v_list = NULL;
17596 else
17597 {
17598 to->vval.v_list = from->vval.v_list;
17599 ++to->vval.v_list->lv_refcount;
17600 }
17601 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017602 case VAR_DICT:
17603 if (from->vval.v_dict == NULL)
17604 to->vval.v_dict = NULL;
17605 else
17606 {
17607 to->vval.v_dict = from->vval.v_dict;
17608 ++to->vval.v_dict->dv_refcount;
17609 }
17610 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017611 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017612 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017613 break;
17614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017615}
17616
17617/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000017618 * Make a copy of an item.
17619 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017620 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
17621 * reference to an already copied list/dict can be used.
17622 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017623 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017624 static int
17625item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000017626 typval_T *from;
17627 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017628 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017629 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017630{
17631 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017632 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017633
Bram Moolenaar33570922005-01-25 22:26:29 +000017634 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017635 {
17636 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017637 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017638 }
17639 ++recurse;
17640
17641 switch (from->v_type)
17642 {
17643 case VAR_NUMBER:
17644 case VAR_STRING:
17645 case VAR_FUNC:
17646 copy_tv(from, to);
17647 break;
17648 case VAR_LIST:
17649 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017650 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017651 if (from->vval.v_list == NULL)
17652 to->vval.v_list = NULL;
17653 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
17654 {
17655 /* use the copy made earlier */
17656 to->vval.v_list = from->vval.v_list->lv_copylist;
17657 ++to->vval.v_list->lv_refcount;
17658 }
17659 else
17660 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
17661 if (to->vval.v_list == NULL)
17662 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017663 break;
17664 case VAR_DICT:
17665 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017666 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017667 if (from->vval.v_dict == NULL)
17668 to->vval.v_dict = NULL;
17669 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
17670 {
17671 /* use the copy made earlier */
17672 to->vval.v_dict = from->vval.v_dict->dv_copydict;
17673 ++to->vval.v_dict->dv_refcount;
17674 }
17675 else
17676 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
17677 if (to->vval.v_dict == NULL)
17678 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017679 break;
17680 default:
17681 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017682 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017683 }
17684 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017685 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017686}
17687
17688/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017689 * ":echo expr1 ..." print each argument separated with a space, add a
17690 * newline at the end.
17691 * ":echon expr1 ..." print each argument plain.
17692 */
17693 void
17694ex_echo(eap)
17695 exarg_T *eap;
17696{
17697 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000017698 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017699 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017700 char_u *p;
17701 int needclr = TRUE;
17702 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017703 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017704
17705 if (eap->skip)
17706 ++emsg_skip;
17707 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
17708 {
17709 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017710 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017711 {
17712 /*
17713 * Report the invalid expression unless the expression evaluation
17714 * has been cancelled due to an aborting error, an interrupt, or an
17715 * exception.
17716 */
17717 if (!aborting())
17718 EMSG2(_(e_invexpr2), p);
17719 break;
17720 }
17721 if (!eap->skip)
17722 {
17723 if (atstart)
17724 {
17725 atstart = FALSE;
17726 /* Call msg_start() after eval1(), evaluating the expression
17727 * may cause a message to appear. */
17728 if (eap->cmdidx == CMD_echo)
17729 msg_start();
17730 }
17731 else if (eap->cmdidx == CMD_echo)
17732 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017733 p = echo_string(&rettv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017734 if (p != NULL)
17735 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017736 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017737 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017738 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017739 if (*p != TAB && needclr)
17740 {
17741 /* remove any text still there from the command */
17742 msg_clr_eos();
17743 needclr = FALSE;
17744 }
17745 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017746 }
17747 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017748 {
17749#ifdef FEAT_MBYTE
17750 if (has_mbyte)
17751 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017752 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017753
17754 (void)msg_outtrans_len_attr(p, i, echo_attr);
17755 p += i - 1;
17756 }
17757 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000017758#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017759 (void)msg_outtrans_len_attr(p, 1, echo_attr);
17760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017761 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017762 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017763 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017764 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017765 arg = skipwhite(arg);
17766 }
17767 eap->nextcmd = check_nextcmd(arg);
17768
17769 if (eap->skip)
17770 --emsg_skip;
17771 else
17772 {
17773 /* remove text that may still be there from the command */
17774 if (needclr)
17775 msg_clr_eos();
17776 if (eap->cmdidx == CMD_echo)
17777 msg_end();
17778 }
17779}
17780
17781/*
17782 * ":echohl {name}".
17783 */
17784 void
17785ex_echohl(eap)
17786 exarg_T *eap;
17787{
17788 int id;
17789
17790 id = syn_name2id(eap->arg);
17791 if (id == 0)
17792 echo_attr = 0;
17793 else
17794 echo_attr = syn_id2attr(id);
17795}
17796
17797/*
17798 * ":execute expr1 ..." execute the result of an expression.
17799 * ":echomsg expr1 ..." Print a message
17800 * ":echoerr expr1 ..." Print an error
17801 * Each gets spaces around each argument and a newline at the end for
17802 * echo commands
17803 */
17804 void
17805ex_execute(eap)
17806 exarg_T *eap;
17807{
17808 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000017809 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017810 int ret = OK;
17811 char_u *p;
17812 garray_T ga;
17813 int len;
17814 int save_did_emsg;
17815
17816 ga_init2(&ga, 1, 80);
17817
17818 if (eap->skip)
17819 ++emsg_skip;
17820 while (*arg != NUL && *arg != '|' && *arg != '\n')
17821 {
17822 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017823 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017824 {
17825 /*
17826 * Report the invalid expression unless the expression evaluation
17827 * has been cancelled due to an aborting error, an interrupt, or an
17828 * exception.
17829 */
17830 if (!aborting())
17831 EMSG2(_(e_invexpr2), p);
17832 ret = FAIL;
17833 break;
17834 }
17835
17836 if (!eap->skip)
17837 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017838 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017839 len = (int)STRLEN(p);
17840 if (ga_grow(&ga, len + 2) == FAIL)
17841 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017842 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017843 ret = FAIL;
17844 break;
17845 }
17846 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017847 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017848 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017849 ga.ga_len += len;
17850 }
17851
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017852 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017853 arg = skipwhite(arg);
17854 }
17855
17856 if (ret != FAIL && ga.ga_data != NULL)
17857 {
17858 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000017859 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017860 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000017861 out_flush();
17862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017863 else if (eap->cmdidx == CMD_echoerr)
17864 {
17865 /* We don't want to abort following commands, restore did_emsg. */
17866 save_did_emsg = did_emsg;
17867 EMSG((char_u *)ga.ga_data);
17868 if (!force_abort)
17869 did_emsg = save_did_emsg;
17870 }
17871 else if (eap->cmdidx == CMD_execute)
17872 do_cmdline((char_u *)ga.ga_data,
17873 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
17874 }
17875
17876 ga_clear(&ga);
17877
17878 if (eap->skip)
17879 --emsg_skip;
17880
17881 eap->nextcmd = check_nextcmd(arg);
17882}
17883
17884/*
17885 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
17886 * "arg" points to the "&" or '+' when called, to "option" when returning.
17887 * Returns NULL when no option name found. Otherwise pointer to the char
17888 * after the option name.
17889 */
17890 static char_u *
17891find_option_end(arg, opt_flags)
17892 char_u **arg;
17893 int *opt_flags;
17894{
17895 char_u *p = *arg;
17896
17897 ++p;
17898 if (*p == 'g' && p[1] == ':')
17899 {
17900 *opt_flags = OPT_GLOBAL;
17901 p += 2;
17902 }
17903 else if (*p == 'l' && p[1] == ':')
17904 {
17905 *opt_flags = OPT_LOCAL;
17906 p += 2;
17907 }
17908 else
17909 *opt_flags = 0;
17910
17911 if (!ASCII_ISALPHA(*p))
17912 return NULL;
17913 *arg = p;
17914
17915 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
17916 p += 4; /* termcap option */
17917 else
17918 while (ASCII_ISALPHA(*p))
17919 ++p;
17920 return p;
17921}
17922
17923/*
17924 * ":function"
17925 */
17926 void
17927ex_function(eap)
17928 exarg_T *eap;
17929{
17930 char_u *theline;
17931 int j;
17932 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017933 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017934 char_u *name = NULL;
17935 char_u *p;
17936 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000017937 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017938 garray_T newargs;
17939 garray_T newlines;
17940 int varargs = FALSE;
17941 int mustend = FALSE;
17942 int flags = 0;
17943 ufunc_T *fp;
17944 int indent;
17945 int nesting;
17946 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000017947 dictitem_T *v;
17948 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017949 static int func_nr = 0; /* number for nameless function */
17950 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017951 hashtab_T *ht;
17952 int todo;
17953 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000017954 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017955
17956 /*
17957 * ":function" without argument: list functions.
17958 */
17959 if (ends_excmd(*eap->arg))
17960 {
17961 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017962 {
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000017963 todo = func_hashtab.ht_used;
17964 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017965 {
17966 if (!HASHITEM_EMPTY(hi))
17967 {
17968 --todo;
17969 fp = HI2UF(hi);
17970 if (!isdigit(*fp->uf_name))
17971 list_func_head(fp, FALSE);
17972 }
17973 }
17974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017975 eap->nextcmd = check_nextcmd(eap->arg);
17976 return;
17977 }
17978
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017979 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017980 * ":function /pat": list functions matching pattern.
17981 */
17982 if (*eap->arg == '/')
17983 {
17984 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
17985 if (!eap->skip)
17986 {
17987 regmatch_T regmatch;
17988
17989 c = *p;
17990 *p = NUL;
17991 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
17992 *p = c;
17993 if (regmatch.regprog != NULL)
17994 {
17995 regmatch.rm_ic = p_ic;
17996
17997 todo = func_hashtab.ht_used;
17998 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
17999 {
18000 if (!HASHITEM_EMPTY(hi))
18001 {
18002 --todo;
18003 fp = HI2UF(hi);
18004 if (!isdigit(*fp->uf_name)
18005 && vim_regexec(&regmatch, fp->uf_name, 0))
18006 list_func_head(fp, FALSE);
18007 }
18008 }
18009 }
18010 }
18011 if (*p == '/')
18012 ++p;
18013 eap->nextcmd = check_nextcmd(p);
18014 return;
18015 }
18016
18017 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018018 * Get the function name. There are these situations:
18019 * func normal function name
18020 * "name" == func, "fudi.fd_dict" == NULL
18021 * dict.func new dictionary entry
18022 * "name" == NULL, "fudi.fd_dict" set,
18023 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
18024 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018025 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018026 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18027 * dict.func existing dict entry that's not a Funcref
18028 * "name" == NULL, "fudi.fd_dict" set,
18029 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18030 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018031 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018032 name = trans_function_name(&p, eap->skip, 0, &fudi);
18033 paren = (vim_strchr(p, '(') != NULL);
18034 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018035 {
18036 /*
18037 * Return on an invalid expression in braces, unless the expression
18038 * evaluation has been cancelled due to an aborting error, an
18039 * interrupt, or an exception.
18040 */
18041 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018042 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018043 if (!eap->skip && fudi.fd_newkey != NULL)
18044 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018045 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018046 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018047 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018048 else
18049 eap->skip = TRUE;
18050 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000018051
Bram Moolenaar071d4272004-06-13 20:20:40 +000018052 /* An error in a function call during evaluation of an expression in magic
18053 * braces should not cause the function not to be defined. */
18054 saved_did_emsg = did_emsg;
18055 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018056
18057 /*
18058 * ":function func" with only function name: list function.
18059 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018060 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018061 {
18062 if (!ends_excmd(*skipwhite(p)))
18063 {
18064 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018065 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018066 }
18067 eap->nextcmd = check_nextcmd(p);
18068 if (eap->nextcmd != NULL)
18069 *p = NUL;
18070 if (!eap->skip && !got_int)
18071 {
18072 fp = find_func(name);
18073 if (fp != NULL)
18074 {
18075 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018076 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018077 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018078 if (FUNCLINE(fp, j) == NULL)
18079 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018080 msg_putchar('\n');
18081 msg_outnum((long)(j + 1));
18082 if (j < 9)
18083 msg_putchar(' ');
18084 if (j < 99)
18085 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018086 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018087 out_flush(); /* show a line at a time */
18088 ui_breakcheck();
18089 }
18090 if (!got_int)
18091 {
18092 msg_putchar('\n');
18093 msg_puts((char_u *)" endfunction");
18094 }
18095 }
18096 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018097 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018098 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018099 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018100 }
18101
18102 /*
18103 * ":function name(arg1, arg2)" Define function.
18104 */
18105 p = skipwhite(p);
18106 if (*p != '(')
18107 {
18108 if (!eap->skip)
18109 {
18110 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018111 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018112 }
18113 /* attempt to continue by skipping some text */
18114 if (vim_strchr(p, '(') != NULL)
18115 p = vim_strchr(p, '(');
18116 }
18117 p = skipwhite(p + 1);
18118
18119 ga_init2(&newargs, (int)sizeof(char_u *), 3);
18120 ga_init2(&newlines, (int)sizeof(char_u *), 3);
18121
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018122 if (!eap->skip)
18123 {
18124 /* Check the name of the function. */
18125 if (name != NULL)
18126 arg = name;
18127 else
18128 arg = fudi.fd_newkey;
18129 if (arg != NULL)
18130 {
18131 if (*arg == K_SPECIAL)
18132 j = 3;
18133 else
18134 j = 0;
18135 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
18136 : eval_isnamec(arg[j])))
18137 ++j;
18138 if (arg[j] != NUL)
18139 emsg_funcname(_(e_invarg2), arg);
18140 }
18141 }
18142
Bram Moolenaar071d4272004-06-13 20:20:40 +000018143 /*
18144 * Isolate the arguments: "arg1, arg2, ...)"
18145 */
18146 while (*p != ')')
18147 {
18148 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
18149 {
18150 varargs = TRUE;
18151 p += 3;
18152 mustend = TRUE;
18153 }
18154 else
18155 {
18156 arg = p;
18157 while (ASCII_ISALNUM(*p) || *p == '_')
18158 ++p;
18159 if (arg == p || isdigit(*arg)
18160 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
18161 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
18162 {
18163 if (!eap->skip)
18164 EMSG2(_("E125: Illegal argument: %s"), arg);
18165 break;
18166 }
18167 if (ga_grow(&newargs, 1) == FAIL)
18168 goto erret;
18169 c = *p;
18170 *p = NUL;
18171 arg = vim_strsave(arg);
18172 if (arg == NULL)
18173 goto erret;
18174 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
18175 *p = c;
18176 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018177 if (*p == ',')
18178 ++p;
18179 else
18180 mustend = TRUE;
18181 }
18182 p = skipwhite(p);
18183 if (mustend && *p != ')')
18184 {
18185 if (!eap->skip)
18186 EMSG2(_(e_invarg2), eap->arg);
18187 break;
18188 }
18189 }
18190 ++p; /* skip the ')' */
18191
Bram Moolenaare9a41262005-01-15 22:18:47 +000018192 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018193 for (;;)
18194 {
18195 p = skipwhite(p);
18196 if (STRNCMP(p, "range", 5) == 0)
18197 {
18198 flags |= FC_RANGE;
18199 p += 5;
18200 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000018201 else if (STRNCMP(p, "dict", 4) == 0)
18202 {
18203 flags |= FC_DICT;
18204 p += 4;
18205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018206 else if (STRNCMP(p, "abort", 5) == 0)
18207 {
18208 flags |= FC_ABORT;
18209 p += 5;
18210 }
18211 else
18212 break;
18213 }
18214
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018215 /* When there is a line break use what follows for the function body.
18216 * Makes 'exe "func Test()\n...\nendfunc"' work. */
18217 if (*p == '\n')
18218 line_arg = p + 1;
18219 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018220 EMSG(_(e_trailing));
18221
18222 /*
18223 * Read the body of the function, until ":endfunction" is found.
18224 */
18225 if (KeyTyped)
18226 {
18227 /* Check if the function already exists, don't let the user type the
18228 * whole function before telling him it doesn't work! For a script we
18229 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018230 if (!eap->skip && !eap->forceit)
18231 {
18232 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
18233 EMSG(_(e_funcdict));
18234 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018235 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018237
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018238 if (!eap->skip && did_emsg)
18239 goto erret;
18240
Bram Moolenaar071d4272004-06-13 20:20:40 +000018241 msg_putchar('\n'); /* don't overwrite the function name */
18242 cmdline_row = msg_row;
18243 }
18244
18245 indent = 2;
18246 nesting = 0;
18247 for (;;)
18248 {
18249 msg_scroll = TRUE;
18250 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018251 sourcing_lnum_off = sourcing_lnum;
18252
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018253 if (line_arg != NULL)
18254 {
18255 /* Use eap->arg, split up in parts by line breaks. */
18256 theline = line_arg;
18257 p = vim_strchr(theline, '\n');
18258 if (p == NULL)
18259 line_arg += STRLEN(line_arg);
18260 else
18261 {
18262 *p = NUL;
18263 line_arg = p + 1;
18264 }
18265 }
18266 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018267 theline = getcmdline(':', 0L, indent);
18268 else
18269 theline = eap->getline(':', eap->cookie, indent);
18270 if (KeyTyped)
18271 lines_left = Rows - 1;
18272 if (theline == NULL)
18273 {
18274 EMSG(_("E126: Missing :endfunction"));
18275 goto erret;
18276 }
18277
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018278 /* Detect line continuation: sourcing_lnum increased more than one. */
18279 if (sourcing_lnum > sourcing_lnum_off + 1)
18280 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
18281 else
18282 sourcing_lnum_off = 0;
18283
Bram Moolenaar071d4272004-06-13 20:20:40 +000018284 if (skip_until != NULL)
18285 {
18286 /* between ":append" and "." and between ":python <<EOF" and "EOF"
18287 * don't check for ":endfunc". */
18288 if (STRCMP(theline, skip_until) == 0)
18289 {
18290 vim_free(skip_until);
18291 skip_until = NULL;
18292 }
18293 }
18294 else
18295 {
18296 /* skip ':' and blanks*/
18297 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
18298 ;
18299
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018300 /* Check for "endfunction". */
18301 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018302 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018303 if (line_arg == NULL)
18304 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018305 break;
18306 }
18307
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018308 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000018309 * at "end". */
18310 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
18311 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018312 else if (STRNCMP(p, "if", 2) == 0
18313 || STRNCMP(p, "wh", 2) == 0
18314 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000018315 || STRNCMP(p, "try", 3) == 0)
18316 indent += 2;
18317
18318 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018319 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018320 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018321 if (*p == '!')
18322 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018323 p += eval_fname_script(p);
18324 if (ASCII_ISALPHA(*p))
18325 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018326 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018327 if (*skipwhite(p) == '(')
18328 {
18329 ++nesting;
18330 indent += 2;
18331 }
18332 }
18333 }
18334
18335 /* Check for ":append" or ":insert". */
18336 p = skip_range(p, NULL);
18337 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
18338 || (p[0] == 'i'
18339 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
18340 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
18341 skip_until = vim_strsave((char_u *)".");
18342
18343 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
18344 arg = skipwhite(skiptowhite(p));
18345 if (arg[0] == '<' && arg[1] =='<'
18346 && ((p[0] == 'p' && p[1] == 'y'
18347 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
18348 || (p[0] == 'p' && p[1] == 'e'
18349 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
18350 || (p[0] == 't' && p[1] == 'c'
18351 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
18352 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
18353 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000018354 || (p[0] == 'm' && p[1] == 'z'
18355 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018356 ))
18357 {
18358 /* ":python <<" continues until a dot, like ":append" */
18359 p = skipwhite(arg + 2);
18360 if (*p == NUL)
18361 skip_until = vim_strsave((char_u *)".");
18362 else
18363 skip_until = vim_strsave(p);
18364 }
18365 }
18366
18367 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018368 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018369 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018370 if (line_arg == NULL)
18371 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018372 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018373 }
18374
18375 /* Copy the line to newly allocated memory. get_one_sourceline()
18376 * allocates 250 bytes per line, this saves 80% on average. The cost
18377 * is an extra alloc/free. */
18378 p = vim_strsave(theline);
18379 if (p != NULL)
18380 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018381 if (line_arg == NULL)
18382 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018383 theline = p;
18384 }
18385
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018386 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
18387
18388 /* Add NULL lines for continuation lines, so that the line count is
18389 * equal to the index in the growarray. */
18390 while (sourcing_lnum_off-- > 0)
18391 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018392
18393 /* Check for end of eap->arg. */
18394 if (line_arg != NULL && *line_arg == NUL)
18395 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018396 }
18397
18398 /* Don't define the function when skipping commands or when an error was
18399 * detected. */
18400 if (eap->skip || did_emsg)
18401 goto erret;
18402
18403 /*
18404 * If there are no errors, add the function
18405 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018406 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018407 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018408 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000018409 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018410 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018411 emsg_funcname("E707: Function name conflicts with variable: %s",
18412 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018413 goto erret;
18414 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018415
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018416 fp = find_func(name);
18417 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018418 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018419 if (!eap->forceit)
18420 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018421 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018422 goto erret;
18423 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018424 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018425 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018426 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018427 name);
18428 goto erret;
18429 }
18430 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018431 ga_clear_strings(&(fp->uf_args));
18432 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018433 vim_free(name);
18434 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018436 }
18437 else
18438 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018439 char numbuf[20];
18440
18441 fp = NULL;
18442 if (fudi.fd_newkey == NULL && !eap->forceit)
18443 {
18444 EMSG(_(e_funcdict));
18445 goto erret;
18446 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000018447 if (fudi.fd_di == NULL)
18448 {
18449 /* Can't add a function to a locked dictionary */
18450 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
18451 goto erret;
18452 }
18453 /* Can't change an existing function if it is locked */
18454 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
18455 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018456
18457 /* Give the function a sequential number. Can only be used with a
18458 * Funcref! */
18459 vim_free(name);
18460 sprintf(numbuf, "%d", ++func_nr);
18461 name = vim_strsave((char_u *)numbuf);
18462 if (name == NULL)
18463 goto erret;
18464 }
18465
18466 if (fp == NULL)
18467 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018468 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018469 {
18470 int slen, plen;
18471 char_u *scriptname;
18472
18473 /* Check that the autoload name matches the script name. */
18474 j = FAIL;
18475 if (sourcing_name != NULL)
18476 {
18477 scriptname = autoload_name(name);
18478 if (scriptname != NULL)
18479 {
18480 p = vim_strchr(scriptname, '/');
18481 plen = STRLEN(p);
18482 slen = STRLEN(sourcing_name);
18483 if (slen > plen && fnamecmp(p,
18484 sourcing_name + slen - plen) == 0)
18485 j = OK;
18486 vim_free(scriptname);
18487 }
18488 }
18489 if (j == FAIL)
18490 {
18491 EMSG2(_("E746: Function name does not match script file name: %s"), name);
18492 goto erret;
18493 }
18494 }
18495
18496 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018497 if (fp == NULL)
18498 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018499
18500 if (fudi.fd_dict != NULL)
18501 {
18502 if (fudi.fd_di == NULL)
18503 {
18504 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018505 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018506 if (fudi.fd_di == NULL)
18507 {
18508 vim_free(fp);
18509 goto erret;
18510 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018511 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
18512 {
18513 vim_free(fudi.fd_di);
18514 goto erret;
18515 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018516 }
18517 else
18518 /* overwrite existing dict entry */
18519 clear_tv(&fudi.fd_di->di_tv);
18520 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018521 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018522 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018523 fp->uf_refcount = 1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018524 }
18525
Bram Moolenaar071d4272004-06-13 20:20:40 +000018526 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018527 STRCPY(fp->uf_name, name);
18528 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018529 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018530 fp->uf_args = newargs;
18531 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018532#ifdef FEAT_PROFILE
18533 fp->uf_tml_count = NULL;
18534 fp->uf_tml_total = NULL;
18535 fp->uf_tml_self = NULL;
18536 fp->uf_profiling = FALSE;
18537 if (prof_def_func())
18538 func_do_profile(fp);
18539#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018540 fp->uf_varargs = varargs;
18541 fp->uf_flags = flags;
18542 fp->uf_calls = 0;
18543 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018544 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018545
18546erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000018547 ga_clear_strings(&newargs);
18548 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018549ret_free:
18550 vim_free(skip_until);
18551 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018552 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018553 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018554}
18555
18556/*
18557 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000018558 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018559 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018560 * flags:
18561 * TFN_INT: internal function name OK
18562 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000018563 * Advances "pp" to just after the function name (if no error).
18564 */
18565 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018566trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018567 char_u **pp;
18568 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018569 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000018570 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018571{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018572 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018573 char_u *start;
18574 char_u *end;
18575 int lead;
18576 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018577 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000018578 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018579
18580 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018581 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018582 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000018583
18584 /* Check for hard coded <SNR>: already translated function ID (from a user
18585 * command). */
18586 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
18587 && (*pp)[2] == (int)KE_SNR)
18588 {
18589 *pp += 3;
18590 len = get_id_len(pp) + 3;
18591 return vim_strnsave(start, len);
18592 }
18593
18594 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
18595 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018596 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000018597 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018598 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018599
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018600 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
18601 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018602 if (end == start)
18603 {
18604 if (!skip)
18605 EMSG(_("E129: Function name required"));
18606 goto theend;
18607 }
Bram Moolenaara7043832005-01-21 11:56:39 +000018608 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018609 {
18610 /*
18611 * Report an invalid expression in braces, unless the expression
18612 * evaluation has been cancelled due to an aborting error, an
18613 * interrupt, or an exception.
18614 */
18615 if (!aborting())
18616 {
18617 if (end != NULL)
18618 EMSG2(_(e_invarg2), start);
18619 }
18620 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018621 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018622 goto theend;
18623 }
18624
18625 if (lv.ll_tv != NULL)
18626 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018627 if (fdp != NULL)
18628 {
18629 fdp->fd_dict = lv.ll_dict;
18630 fdp->fd_newkey = lv.ll_newkey;
18631 lv.ll_newkey = NULL;
18632 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018633 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018634 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
18635 {
18636 name = vim_strsave(lv.ll_tv->vval.v_string);
18637 *pp = end;
18638 }
18639 else
18640 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018641 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
18642 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018643 EMSG(_(e_funcref));
18644 else
18645 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018646 name = NULL;
18647 }
18648 goto theend;
18649 }
18650
18651 if (lv.ll_name == NULL)
18652 {
18653 /* Error found, but continue after the function name. */
18654 *pp = end;
18655 goto theend;
18656 }
18657
18658 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000018659 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018660 len = STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000018661 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
18662 && STRNCMP(lv.ll_name, "s:", 2) == 0)
18663 {
18664 /* When there was "s:" already or the name expanded to get a
18665 * leading "s:" then remove it. */
18666 lv.ll_name += 2;
18667 len -= 2;
18668 lead = 2;
18669 }
18670 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018671 else
Bram Moolenaara7043832005-01-21 11:56:39 +000018672 {
18673 if (lead == 2) /* skip over "s:" */
18674 lv.ll_name += 2;
18675 len = (int)(end - lv.ll_name);
18676 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018677
18678 /*
18679 * Copy the function name to allocated memory.
18680 * Accept <SID>name() inside a script, translate into <SNR>123_name().
18681 * Accept <SNR>123_name() outside a script.
18682 */
18683 if (skip)
18684 lead = 0; /* do nothing */
18685 else if (lead > 0)
18686 {
18687 lead = 3;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000018688 if (eval_fname_sid(lv.ll_exp_name != NULL ? lv.ll_exp_name : *pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018689 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000018690 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018691 if (current_SID <= 0)
18692 {
18693 EMSG(_(e_usingsid));
18694 goto theend;
18695 }
18696 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
18697 lead += (int)STRLEN(sid_buf);
18698 }
18699 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018700 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018701 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018702 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018703 goto theend;
18704 }
18705 name = alloc((unsigned)(len + lead + 1));
18706 if (name != NULL)
18707 {
18708 if (lead > 0)
18709 {
18710 name[0] = K_SPECIAL;
18711 name[1] = KS_EXTRA;
18712 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000018713 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018714 STRCPY(name + 3, sid_buf);
18715 }
18716 mch_memmove(name + lead, lv.ll_name, (size_t)len);
18717 name[len + lead] = NUL;
18718 }
18719 *pp = end;
18720
18721theend:
18722 clear_lval(&lv);
18723 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018724}
18725
18726/*
18727 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
18728 * Return 2 if "p" starts with "s:".
18729 * Return 0 otherwise.
18730 */
18731 static int
18732eval_fname_script(p)
18733 char_u *p;
18734{
18735 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
18736 || STRNICMP(p + 1, "SNR>", 4) == 0))
18737 return 5;
18738 if (p[0] == 's' && p[1] == ':')
18739 return 2;
18740 return 0;
18741}
18742
18743/*
18744 * Return TRUE if "p" starts with "<SID>" or "s:".
18745 * Only works if eval_fname_script() returned non-zero for "p"!
18746 */
18747 static int
18748eval_fname_sid(p)
18749 char_u *p;
18750{
18751 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
18752}
18753
18754/*
18755 * List the head of the function: "name(arg1, arg2)".
18756 */
18757 static void
18758list_func_head(fp, indent)
18759 ufunc_T *fp;
18760 int indent;
18761{
18762 int j;
18763
18764 msg_start();
18765 if (indent)
18766 MSG_PUTS(" ");
18767 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018768 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018769 {
18770 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018771 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018772 }
18773 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018774 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018775 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018776 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018777 {
18778 if (j)
18779 MSG_PUTS(", ");
18780 msg_puts(FUNCARG(fp, j));
18781 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018782 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018783 {
18784 if (j)
18785 MSG_PUTS(", ");
18786 MSG_PUTS("...");
18787 }
18788 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000018789 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000018790 if (p_verbose > 0)
18791 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018792}
18793
18794/*
18795 * Find a function by name, return pointer to it in ufuncs.
18796 * Return NULL for unknown function.
18797 */
18798 static ufunc_T *
18799find_func(name)
18800 char_u *name;
18801{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018802 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018803
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018804 hi = hash_find(&func_hashtab, name);
18805 if (!HASHITEM_EMPTY(hi))
18806 return HI2UF(hi);
18807 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018808}
18809
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018810#if defined(EXITFREE) || defined(PROTO)
18811 void
18812free_all_functions()
18813{
18814 hashitem_T *hi;
18815
18816 /* Need to start all over every time, because func_free() may change the
18817 * hash table. */
18818 while (func_hashtab.ht_used > 0)
18819 for (hi = func_hashtab.ht_array; ; ++hi)
18820 if (!HASHITEM_EMPTY(hi))
18821 {
18822 func_free(HI2UF(hi));
18823 break;
18824 }
18825}
18826#endif
18827
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018828/*
18829 * Return TRUE if a function "name" exists.
18830 */
18831 static int
18832function_exists(name)
18833 char_u *name;
18834{
18835 char_u *p = name;
18836 int n = FALSE;
18837
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018838 p = trans_function_name(&p, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018839 if (p != NULL)
18840 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018841 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018842 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018843 else
18844 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018845 vim_free(p);
18846 }
18847 return n;
18848}
18849
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018850/*
18851 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018852 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018853 */
18854 static int
18855builtin_function(name)
18856 char_u *name;
18857{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018858 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
18859 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018860}
18861
Bram Moolenaar05159a02005-02-26 23:04:13 +000018862#if defined(FEAT_PROFILE) || defined(PROTO)
18863/*
18864 * Start profiling function "fp".
18865 */
18866 static void
18867func_do_profile(fp)
18868 ufunc_T *fp;
18869{
18870 fp->uf_tm_count = 0;
18871 profile_zero(&fp->uf_tm_self);
18872 profile_zero(&fp->uf_tm_total);
18873 if (fp->uf_tml_count == NULL)
18874 fp->uf_tml_count = (int *)alloc_clear((unsigned)
18875 (sizeof(int) * fp->uf_lines.ga_len));
18876 if (fp->uf_tml_total == NULL)
18877 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
18878 (sizeof(proftime_T) * fp->uf_lines.ga_len));
18879 if (fp->uf_tml_self == NULL)
18880 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
18881 (sizeof(proftime_T) * fp->uf_lines.ga_len));
18882 fp->uf_tml_idx = -1;
18883 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
18884 || fp->uf_tml_self == NULL)
18885 return; /* out of memory */
18886
18887 fp->uf_profiling = TRUE;
18888}
18889
18890/*
18891 * Dump the profiling results for all functions in file "fd".
18892 */
18893 void
18894func_dump_profile(fd)
18895 FILE *fd;
18896{
18897 hashitem_T *hi;
18898 int todo;
18899 ufunc_T *fp;
18900 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000018901 ufunc_T **sorttab;
18902 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018903
18904 todo = func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000018905 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
18906
Bram Moolenaar05159a02005-02-26 23:04:13 +000018907 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
18908 {
18909 if (!HASHITEM_EMPTY(hi))
18910 {
18911 --todo;
18912 fp = HI2UF(hi);
18913 if (fp->uf_profiling)
18914 {
Bram Moolenaar73830342005-02-28 22:48:19 +000018915 if (sorttab != NULL)
18916 sorttab[st_len++] = fp;
18917
Bram Moolenaar05159a02005-02-26 23:04:13 +000018918 if (fp->uf_name[0] == K_SPECIAL)
18919 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
18920 else
18921 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
18922 if (fp->uf_tm_count == 1)
18923 fprintf(fd, "Called 1 time\n");
18924 else
18925 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
18926 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
18927 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
18928 fprintf(fd, "\n");
18929 fprintf(fd, "count total (s) self (s)\n");
18930
18931 for (i = 0; i < fp->uf_lines.ga_len; ++i)
18932 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018933 if (FUNCLINE(fp, i) == NULL)
18934 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000018935 prof_func_line(fd, fp->uf_tml_count[i],
18936 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018937 fprintf(fd, "%s\n", FUNCLINE(fp, i));
18938 }
18939 fprintf(fd, "\n");
18940 }
18941 }
18942 }
Bram Moolenaar73830342005-02-28 22:48:19 +000018943
18944 if (sorttab != NULL && st_len > 0)
18945 {
18946 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
18947 prof_total_cmp);
18948 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
18949 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
18950 prof_self_cmp);
18951 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
18952 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000018953}
Bram Moolenaar73830342005-02-28 22:48:19 +000018954
18955 static void
18956prof_sort_list(fd, sorttab, st_len, title, prefer_self)
18957 FILE *fd;
18958 ufunc_T **sorttab;
18959 int st_len;
18960 char *title;
18961 int prefer_self; /* when equal print only self time */
18962{
18963 int i;
18964 ufunc_T *fp;
18965
18966 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
18967 fprintf(fd, "count total (s) self (s) function\n");
18968 for (i = 0; i < 20 && i < st_len; ++i)
18969 {
18970 fp = sorttab[i];
18971 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
18972 prefer_self);
18973 if (fp->uf_name[0] == K_SPECIAL)
18974 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
18975 else
18976 fprintf(fd, " %s()\n", fp->uf_name);
18977 }
18978 fprintf(fd, "\n");
18979}
18980
18981/*
18982 * Print the count and times for one function or function line.
18983 */
18984 static void
18985prof_func_line(fd, count, total, self, prefer_self)
18986 FILE *fd;
18987 int count;
18988 proftime_T *total;
18989 proftime_T *self;
18990 int prefer_self; /* when equal print only self time */
18991{
18992 if (count > 0)
18993 {
18994 fprintf(fd, "%5d ", count);
18995 if (prefer_self && profile_equal(total, self))
18996 fprintf(fd, " ");
18997 else
18998 fprintf(fd, "%s ", profile_msg(total));
18999 if (!prefer_self && profile_equal(total, self))
19000 fprintf(fd, " ");
19001 else
19002 fprintf(fd, "%s ", profile_msg(self));
19003 }
19004 else
19005 fprintf(fd, " ");
19006}
19007
19008/*
19009 * Compare function for total time sorting.
19010 */
19011 static int
19012#ifdef __BORLANDC__
19013_RTLENTRYF
19014#endif
19015prof_total_cmp(s1, s2)
19016 const void *s1;
19017 const void *s2;
19018{
19019 ufunc_T *p1, *p2;
19020
19021 p1 = *(ufunc_T **)s1;
19022 p2 = *(ufunc_T **)s2;
19023 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
19024}
19025
19026/*
19027 * Compare function for self time sorting.
19028 */
19029 static int
19030#ifdef __BORLANDC__
19031_RTLENTRYF
19032#endif
19033prof_self_cmp(s1, s2)
19034 const void *s1;
19035 const void *s2;
19036{
19037 ufunc_T *p1, *p2;
19038
19039 p1 = *(ufunc_T **)s1;
19040 p2 = *(ufunc_T **)s2;
19041 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
19042}
19043
Bram Moolenaar05159a02005-02-26 23:04:13 +000019044#endif
19045
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019046/* The names of packages that once were loaded is remembered. */
19047static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
19048
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019049/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019050 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019051 * Return TRUE if a package was loaded.
19052 */
19053 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019054script_autoload(name, reload)
19055 char_u *name;
19056 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019057{
19058 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019059 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019060 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019061 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019062
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019063 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019064 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019065 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019066 return FALSE;
19067
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019068 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019069
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019070 /* Find the name in the list of previously loaded package names. Skip
19071 * "autoload/", it's always the same. */
19072 for (i = 0; i < ga_loaded.ga_len; ++i)
19073 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
19074 break;
19075 if (!reload && i < ga_loaded.ga_len)
19076 ret = FALSE; /* was loaded already */
19077 else
19078 {
19079 /* Remember the name if it wasn't loaded already. */
19080 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
19081 {
19082 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
19083 tofree = NULL;
19084 }
19085
19086 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000019087 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019088 ret = TRUE;
19089 }
19090
19091 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019092 return ret;
19093}
19094
19095/*
19096 * Return the autoload script name for a function or variable name.
19097 * Returns NULL when out of memory.
19098 */
19099 static char_u *
19100autoload_name(name)
19101 char_u *name;
19102{
19103 char_u *p;
19104 char_u *scriptname;
19105
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019106 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019107 scriptname = alloc((unsigned)(STRLEN(name) + 14));
19108 if (scriptname == NULL)
19109 return FALSE;
19110 STRCPY(scriptname, "autoload/");
19111 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019112 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019113 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019114 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019115 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019116 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019117}
19118
Bram Moolenaar071d4272004-06-13 20:20:40 +000019119#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
19120
19121/*
19122 * Function given to ExpandGeneric() to obtain the list of user defined
19123 * function names.
19124 */
19125 char_u *
19126get_user_func_name(xp, idx)
19127 expand_T *xp;
19128 int idx;
19129{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019130 static long_u done;
19131 static hashitem_T *hi;
19132 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019133
19134 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019135 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019136 done = 0;
19137 hi = func_hashtab.ht_array;
19138 }
19139 if (done < func_hashtab.ht_used)
19140 {
19141 if (done++ > 0)
19142 ++hi;
19143 while (HASHITEM_EMPTY(hi))
19144 ++hi;
19145 fp = HI2UF(hi);
19146
19147 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
19148 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019149
19150 cat_func_name(IObuff, fp);
19151 if (xp->xp_context != EXPAND_USER_FUNC)
19152 {
19153 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019154 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019155 STRCAT(IObuff, ")");
19156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019157 return IObuff;
19158 }
19159 return NULL;
19160}
19161
19162#endif /* FEAT_CMDL_COMPL */
19163
19164/*
19165 * Copy the function name of "fp" to buffer "buf".
19166 * "buf" must be able to hold the function name plus three bytes.
19167 * Takes care of script-local function names.
19168 */
19169 static void
19170cat_func_name(buf, fp)
19171 char_u *buf;
19172 ufunc_T *fp;
19173{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019174 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019175 {
19176 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019177 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019178 }
19179 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019180 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019181}
19182
19183/*
19184 * ":delfunction {name}"
19185 */
19186 void
19187ex_delfunction(eap)
19188 exarg_T *eap;
19189{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019190 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019191 char_u *p;
19192 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019193 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019194
19195 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019196 name = trans_function_name(&p, eap->skip, 0, &fudi);
19197 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019198 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019199 {
19200 if (fudi.fd_dict != NULL && !eap->skip)
19201 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019202 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019204 if (!ends_excmd(*skipwhite(p)))
19205 {
19206 vim_free(name);
19207 EMSG(_(e_trailing));
19208 return;
19209 }
19210 eap->nextcmd = check_nextcmd(p);
19211 if (eap->nextcmd != NULL)
19212 *p = NUL;
19213
19214 if (!eap->skip)
19215 fp = find_func(name);
19216 vim_free(name);
19217
19218 if (!eap->skip)
19219 {
19220 if (fp == NULL)
19221 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019222 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019223 return;
19224 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019225 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019226 {
19227 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
19228 return;
19229 }
19230
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019231 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019232 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019233 /* Delete the dict item that refers to the function, it will
19234 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019235 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019236 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019237 else
19238 func_free(fp);
19239 }
19240}
19241
19242/*
19243 * Free a function and remove it from the list of functions.
19244 */
19245 static void
19246func_free(fp)
19247 ufunc_T *fp;
19248{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019249 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019250
19251 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019252 ga_clear_strings(&(fp->uf_args));
19253 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000019254#ifdef FEAT_PROFILE
19255 vim_free(fp->uf_tml_count);
19256 vim_free(fp->uf_tml_total);
19257 vim_free(fp->uf_tml_self);
19258#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019259
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019260 /* remove the function from the function hashtable */
19261 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
19262 if (HASHITEM_EMPTY(hi))
19263 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019264 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019265 hash_remove(&func_hashtab, hi);
19266
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019267 vim_free(fp);
19268}
19269
19270/*
19271 * Unreference a Function: decrement the reference count and free it when it
19272 * becomes zero. Only for numbered functions.
19273 */
19274 static void
19275func_unref(name)
19276 char_u *name;
19277{
19278 ufunc_T *fp;
19279
19280 if (name != NULL && isdigit(*name))
19281 {
19282 fp = find_func(name);
19283 if (fp == NULL)
19284 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019285 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019286 {
19287 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019288 * when "uf_calls" becomes zero. */
19289 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019290 func_free(fp);
19291 }
19292 }
19293}
19294
19295/*
19296 * Count a reference to a Function.
19297 */
19298 static void
19299func_ref(name)
19300 char_u *name;
19301{
19302 ufunc_T *fp;
19303
19304 if (name != NULL && isdigit(*name))
19305 {
19306 fp = find_func(name);
19307 if (fp == NULL)
19308 EMSG2(_(e_intern2), "func_ref()");
19309 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019310 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019311 }
19312}
19313
19314/*
19315 * Call a user function.
19316 */
19317 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000019318call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019319 ufunc_T *fp; /* pointer to function */
19320 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000019321 typval_T *argvars; /* arguments */
19322 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019323 linenr_T firstline; /* first line of range */
19324 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000019325 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019326{
Bram Moolenaar33570922005-01-25 22:26:29 +000019327 char_u *save_sourcing_name;
19328 linenr_T save_sourcing_lnum;
19329 scid_T save_current_SID;
19330 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000019331 int save_did_emsg;
19332 static int depth = 0;
19333 dictitem_T *v;
19334 int fixvar_idx = 0; /* index in fixvar[] */
19335 int i;
19336 int ai;
19337 char_u numbuf[NUMBUFLEN];
19338 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019339#ifdef FEAT_PROFILE
19340 proftime_T wait_start;
19341#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019342
19343 /* If depth of calling is getting too high, don't execute the function */
19344 if (depth >= p_mfd)
19345 {
19346 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019347 rettv->v_type = VAR_NUMBER;
19348 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019349 return;
19350 }
19351 ++depth;
19352
19353 line_breakcheck(); /* check for CTRL-C hit */
19354
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019355 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000019356 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019357 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019358 fc.rettv = rettv;
19359 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019360 fc.linenr = 0;
19361 fc.returned = FALSE;
19362 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019363 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019364 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019365 fc.dbg_tick = debug_tick;
19366
Bram Moolenaar33570922005-01-25 22:26:29 +000019367 /*
19368 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
19369 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
19370 * each argument variable and saves a lot of time.
19371 */
19372 /*
19373 * Init l: variables.
19374 */
19375 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000019376 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019377 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000019378 /* Set l:self to "selfdict". Use "name" to avoid a warning from
19379 * some compiler that checks the destination size. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019380 v = &fc.fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000019381 name = v->di_key;
19382 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000019383 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
19384 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
19385 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019386 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000019387 v->di_tv.vval.v_dict = selfdict;
19388 ++selfdict->dv_refcount;
19389 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000019390
Bram Moolenaar33570922005-01-25 22:26:29 +000019391 /*
19392 * Init a: variables.
19393 * Set a:0 to "argcount".
19394 * Set a:000 to a list with room for the "..." arguments.
19395 */
19396 init_var_dict(&fc.l_avars, &fc.l_avars_var);
19397 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019398 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000019399 v = &fc.fixvar[fixvar_idx++].var;
19400 STRCPY(v->di_key, "000");
19401 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19402 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
19403 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019404 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019405 v->di_tv.vval.v_list = &fc.l_varlist;
19406 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
19407 fc.l_varlist.lv_refcount = 99999;
19408
19409 /*
19410 * Set a:firstline to "firstline" and a:lastline to "lastline".
19411 * Set a:name to named arguments.
19412 * Set a:N to the "..." arguments.
19413 */
19414 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
19415 (varnumber_T)firstline);
19416 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
19417 (varnumber_T)lastline);
19418 for (i = 0; i < argcount; ++i)
19419 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019420 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000019421 if (ai < 0)
19422 /* named argument a:name */
19423 name = FUNCARG(fp, i);
19424 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019425 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019426 /* "..." argument a:1, a:2, etc. */
19427 sprintf((char *)numbuf, "%d", ai + 1);
19428 name = numbuf;
19429 }
19430 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
19431 {
19432 v = &fc.fixvar[fixvar_idx++].var;
19433 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19434 }
19435 else
19436 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019437 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19438 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000019439 if (v == NULL)
19440 break;
19441 v->di_flags = DI_FLAGS_RO;
19442 }
19443 STRCPY(v->di_key, name);
19444 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
19445
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019446 /* Note: the values are copied directly to avoid alloc/free.
19447 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019448 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019449 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019450
19451 if (ai >= 0 && ai < MAX_FUNC_ARGS)
19452 {
19453 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
19454 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019455 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019456 }
19457 }
19458
Bram Moolenaar071d4272004-06-13 20:20:40 +000019459 /* Don't redraw while executing the function. */
19460 ++RedrawingDisabled;
19461 save_sourcing_name = sourcing_name;
19462 save_sourcing_lnum = sourcing_lnum;
19463 sourcing_lnum = 1;
19464 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019465 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019466 if (sourcing_name != NULL)
19467 {
19468 if (save_sourcing_name != NULL
19469 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
19470 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
19471 else
19472 STRCPY(sourcing_name, "function ");
19473 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
19474
19475 if (p_verbose >= 12)
19476 {
19477 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019478 verbose_enter_scroll();
19479
Bram Moolenaar555b2802005-05-19 21:08:39 +000019480 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019481 if (p_verbose >= 14)
19482 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019483 char_u buf[MSG_BUF_LEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000019484 char_u numbuf[NUMBUFLEN];
19485 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019486
19487 msg_puts((char_u *)"(");
19488 for (i = 0; i < argcount; ++i)
19489 {
19490 if (i > 0)
19491 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019492 if (argvars[i].v_type == VAR_NUMBER)
19493 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019494 else
19495 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019496 trunc_string(tv2string(&argvars[i], &tofree, numbuf, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019497 buf, MSG_BUF_CLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019498 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019499 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019500 }
19501 }
19502 msg_puts((char_u *)")");
19503 }
19504 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019505
19506 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019507 --no_wait_return;
19508 }
19509 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019510#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000019511 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019512 {
19513 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
19514 func_do_profile(fp);
19515 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019516 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000019517 {
19518 ++fp->uf_tm_count;
19519 profile_start(&fp->uf_tm_start);
19520 profile_zero(&fp->uf_tm_children);
19521 }
19522 script_prof_save(&wait_start);
19523 }
19524#endif
19525
Bram Moolenaar071d4272004-06-13 20:20:40 +000019526 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019527 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019528 save_did_emsg = did_emsg;
19529 did_emsg = FALSE;
19530
19531 /* call do_cmdline() to execute the lines */
19532 do_cmdline(NULL, get_func_line, (void *)&fc,
19533 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
19534
19535 --RedrawingDisabled;
19536
19537 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019538 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019539 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019540 clear_tv(rettv);
19541 rettv->v_type = VAR_NUMBER;
19542 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019543 }
19544
Bram Moolenaar05159a02005-02-26 23:04:13 +000019545#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000019546 if (do_profiling == PROF_YES && (fp->uf_profiling
19547 || (fc.caller != NULL && &fc.caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000019548 {
19549 profile_end(&fp->uf_tm_start);
19550 profile_sub_wait(&wait_start, &fp->uf_tm_start);
19551 profile_add(&fp->uf_tm_total, &fp->uf_tm_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000019552 profile_self(&fp->uf_tm_self, &fp->uf_tm_start, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019553 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019554 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019555 profile_add(&fc.caller->func->uf_tm_children, &fp->uf_tm_start);
19556 profile_add(&fc.caller->func->uf_tml_children, &fp->uf_tm_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019557 }
19558 }
19559#endif
19560
Bram Moolenaar071d4272004-06-13 20:20:40 +000019561 /* when being verbose, mention the return value */
19562 if (p_verbose >= 12)
19563 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019564 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019565 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019566
Bram Moolenaar071d4272004-06-13 20:20:40 +000019567 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000019568 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019569 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000019570 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
19571 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019572 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000019573 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000019574 char_u buf[MSG_BUF_LEN];
19575 char_u numbuf[NUMBUFLEN];
19576 char_u *tofree;
19577
Bram Moolenaar555b2802005-05-19 21:08:39 +000019578 /* The value may be very long. Skip the middle part, so that we
19579 * have some idea how it starts and ends. smsg() would always
19580 * truncate it at the end. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019581 trunc_string(tv2string(fc.rettv, &tofree, numbuf, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019582 buf, MSG_BUF_CLEN);
Bram Moolenaar555b2802005-05-19 21:08:39 +000019583 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019584 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019585 }
19586 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019587
19588 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019589 --no_wait_return;
19590 }
19591
19592 vim_free(sourcing_name);
19593 sourcing_name = save_sourcing_name;
19594 sourcing_lnum = save_sourcing_lnum;
19595 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019596#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000019597 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019598 script_prof_restore(&wait_start);
19599#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019600
19601 if (p_verbose >= 12 && sourcing_name != NULL)
19602 {
19603 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019604 verbose_enter_scroll();
19605
Bram Moolenaar555b2802005-05-19 21:08:39 +000019606 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019607 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019608
19609 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019610 --no_wait_return;
19611 }
19612
19613 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019614 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019615
Bram Moolenaar33570922005-01-25 22:26:29 +000019616 /* The a: variables typevals were not alloced, only free the allocated
19617 * variables. */
19618 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
19619
19620 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019621 --depth;
19622}
19623
19624/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019625 * Add a number variable "name" to dict "dp" with value "nr".
19626 */
19627 static void
19628add_nr_var(dp, v, name, nr)
19629 dict_T *dp;
19630 dictitem_T *v;
19631 char *name;
19632 varnumber_T nr;
19633{
19634 STRCPY(v->di_key, name);
19635 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19636 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
19637 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019638 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019639 v->di_tv.vval.v_number = nr;
19640}
19641
19642/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019643 * ":return [expr]"
19644 */
19645 void
19646ex_return(eap)
19647 exarg_T *eap;
19648{
19649 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019650 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019651 int returning = FALSE;
19652
19653 if (current_funccal == NULL)
19654 {
19655 EMSG(_("E133: :return not inside a function"));
19656 return;
19657 }
19658
19659 if (eap->skip)
19660 ++emsg_skip;
19661
19662 eap->nextcmd = NULL;
19663 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019664 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019665 {
19666 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019667 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019668 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019669 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019670 }
19671 /* It's safer to return also on error. */
19672 else if (!eap->skip)
19673 {
19674 /*
19675 * Return unless the expression evaluation has been cancelled due to an
19676 * aborting error, an interrupt, or an exception.
19677 */
19678 if (!aborting())
19679 returning = do_return(eap, FALSE, TRUE, NULL);
19680 }
19681
19682 /* When skipping or the return gets pending, advance to the next command
19683 * in this line (!returning). Otherwise, ignore the rest of the line.
19684 * Following lines will be ignored by get_func_line(). */
19685 if (returning)
19686 eap->nextcmd = NULL;
19687 else if (eap->nextcmd == NULL) /* no argument */
19688 eap->nextcmd = check_nextcmd(arg);
19689
19690 if (eap->skip)
19691 --emsg_skip;
19692}
19693
19694/*
19695 * Return from a function. Possibly makes the return pending. Also called
19696 * for a pending return at the ":endtry" or after returning from an extra
19697 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000019698 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019699 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019700 * FALSE when the return gets pending.
19701 */
19702 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019703do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019704 exarg_T *eap;
19705 int reanimate;
19706 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019707 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019708{
19709 int idx;
19710 struct condstack *cstack = eap->cstack;
19711
19712 if (reanimate)
19713 /* Undo the return. */
19714 current_funccal->returned = FALSE;
19715
19716 /*
19717 * Cleanup (and inactivate) conditionals, but stop when a try conditional
19718 * not in its finally clause (which then is to be executed next) is found.
19719 * In this case, make the ":return" pending for execution at the ":endtry".
19720 * Otherwise, return normally.
19721 */
19722 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
19723 if (idx >= 0)
19724 {
19725 cstack->cs_pending[idx] = CSTP_RETURN;
19726
19727 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019728 /* A pending return again gets pending. "rettv" points to an
19729 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000019730 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019731 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019732 else
19733 {
19734 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019735 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019736 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019737 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019738
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019739 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019740 {
19741 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019742 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019743 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019744 else
19745 EMSG(_(e_outofmem));
19746 }
19747 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019748 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019749
19750 if (reanimate)
19751 {
19752 /* The pending return value could be overwritten by a ":return"
19753 * without argument in a finally clause; reset the default
19754 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019755 current_funccal->rettv->v_type = VAR_NUMBER;
19756 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019757 }
19758 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019759 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019760 }
19761 else
19762 {
19763 current_funccal->returned = TRUE;
19764
19765 /* If the return is carried out now, store the return value. For
19766 * a return immediately after reanimation, the value is already
19767 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019768 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019769 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019770 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000019771 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019772 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019773 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019774 }
19775 }
19776
19777 return idx < 0;
19778}
19779
19780/*
19781 * Free the variable with a pending return value.
19782 */
19783 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019784discard_pending_return(rettv)
19785 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019786{
Bram Moolenaar33570922005-01-25 22:26:29 +000019787 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019788}
19789
19790/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019791 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000019792 * is an allocated string. Used by report_pending() for verbose messages.
19793 */
19794 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019795get_return_cmd(rettv)
19796 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019797{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019798 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019799 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019800 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019801
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019802 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019803 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019804 if (s == NULL)
19805 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019806
19807 STRCPY(IObuff, ":return ");
19808 STRNCPY(IObuff + 8, s, IOSIZE - 8);
19809 if (STRLEN(s) + 8 >= IOSIZE)
19810 STRCPY(IObuff + IOSIZE - 4, "...");
19811 vim_free(tofree);
19812 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019813}
19814
19815/*
19816 * Get next function line.
19817 * Called by do_cmdline() to get the next line.
19818 * Returns allocated string, or NULL for end of function.
19819 */
19820/* ARGSUSED */
19821 char_u *
19822get_func_line(c, cookie, indent)
19823 int c; /* not used */
19824 void *cookie;
19825 int indent; /* not used */
19826{
Bram Moolenaar33570922005-01-25 22:26:29 +000019827 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019828 ufunc_T *fp = fcp->func;
19829 char_u *retval;
19830 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019831
19832 /* If breakpoints have been added/deleted need to check for it. */
19833 if (fcp->dbg_tick != debug_tick)
19834 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019835 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019836 sourcing_lnum);
19837 fcp->dbg_tick = debug_tick;
19838 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019839#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000019840 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019841 func_line_end(cookie);
19842#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019843
Bram Moolenaar05159a02005-02-26 23:04:13 +000019844 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019845 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
19846 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019847 retval = NULL;
19848 else
19849 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019850 /* Skip NULL lines (continuation lines). */
19851 while (fcp->linenr < gap->ga_len
19852 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
19853 ++fcp->linenr;
19854 if (fcp->linenr >= gap->ga_len)
19855 retval = NULL;
19856 else
19857 {
19858 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
19859 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019860#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000019861 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019862 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019863#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019865 }
19866
19867 /* Did we encounter a breakpoint? */
19868 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
19869 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019870 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019871 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019872 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019873 sourcing_lnum);
19874 fcp->dbg_tick = debug_tick;
19875 }
19876
19877 return retval;
19878}
19879
Bram Moolenaar05159a02005-02-26 23:04:13 +000019880#if defined(FEAT_PROFILE) || defined(PROTO)
19881/*
19882 * Called when starting to read a function line.
19883 * "sourcing_lnum" must be correct!
19884 * When skipping lines it may not actually be executed, but we won't find out
19885 * until later and we need to store the time now.
19886 */
19887 void
19888func_line_start(cookie)
19889 void *cookie;
19890{
19891 funccall_T *fcp = (funccall_T *)cookie;
19892 ufunc_T *fp = fcp->func;
19893
19894 if (fp->uf_profiling && sourcing_lnum >= 1
19895 && sourcing_lnum <= fp->uf_lines.ga_len)
19896 {
19897 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019898 /* Skip continuation lines. */
19899 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
19900 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019901 fp->uf_tml_execed = FALSE;
19902 profile_start(&fp->uf_tml_start);
19903 profile_zero(&fp->uf_tml_children);
19904 profile_get_wait(&fp->uf_tml_wait);
19905 }
19906}
19907
19908/*
19909 * Called when actually executing a function line.
19910 */
19911 void
19912func_line_exec(cookie)
19913 void *cookie;
19914{
19915 funccall_T *fcp = (funccall_T *)cookie;
19916 ufunc_T *fp = fcp->func;
19917
19918 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
19919 fp->uf_tml_execed = TRUE;
19920}
19921
19922/*
19923 * Called when done with a function line.
19924 */
19925 void
19926func_line_end(cookie)
19927 void *cookie;
19928{
19929 funccall_T *fcp = (funccall_T *)cookie;
19930 ufunc_T *fp = fcp->func;
19931
19932 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
19933 {
19934 if (fp->uf_tml_execed)
19935 {
19936 ++fp->uf_tml_count[fp->uf_tml_idx];
19937 profile_end(&fp->uf_tml_start);
19938 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019939 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000019940 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
19941 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019942 }
19943 fp->uf_tml_idx = -1;
19944 }
19945}
19946#endif
19947
Bram Moolenaar071d4272004-06-13 20:20:40 +000019948/*
19949 * Return TRUE if the currently active function should be ended, because a
19950 * return was encountered or an error occured. Used inside a ":while".
19951 */
19952 int
19953func_has_ended(cookie)
19954 void *cookie;
19955{
Bram Moolenaar33570922005-01-25 22:26:29 +000019956 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019957
19958 /* Ignore the "abort" flag if the abortion behavior has been changed due to
19959 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019960 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000019961 || fcp->returned);
19962}
19963
19964/*
19965 * return TRUE if cookie indicates a function which "abort"s on errors.
19966 */
19967 int
19968func_has_abort(cookie)
19969 void *cookie;
19970{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019971 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019972}
19973
19974#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
19975typedef enum
19976{
19977 VAR_FLAVOUR_DEFAULT,
19978 VAR_FLAVOUR_SESSION,
19979 VAR_FLAVOUR_VIMINFO
19980} var_flavour_T;
19981
19982static var_flavour_T var_flavour __ARGS((char_u *varname));
19983
19984 static var_flavour_T
19985var_flavour(varname)
19986 char_u *varname;
19987{
19988 char_u *p = varname;
19989
19990 if (ASCII_ISUPPER(*p))
19991 {
19992 while (*(++p))
19993 if (ASCII_ISLOWER(*p))
19994 return VAR_FLAVOUR_SESSION;
19995 return VAR_FLAVOUR_VIMINFO;
19996 }
19997 else
19998 return VAR_FLAVOUR_DEFAULT;
19999}
20000#endif
20001
20002#if defined(FEAT_VIMINFO) || defined(PROTO)
20003/*
20004 * Restore global vars that start with a capital from the viminfo file
20005 */
20006 int
20007read_viminfo_varlist(virp, writing)
20008 vir_T *virp;
20009 int writing;
20010{
20011 char_u *tab;
20012 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000020013 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020014
20015 if (!writing && (find_viminfo_parameter('!') != NULL))
20016 {
20017 tab = vim_strchr(virp->vir_line + 1, '\t');
20018 if (tab != NULL)
20019 {
20020 *tab++ = '\0'; /* isolate the variable name */
20021 if (*tab == 'S') /* string var */
20022 is_string = TRUE;
20023
20024 tab = vim_strchr(tab, '\t');
20025 if (tab != NULL)
20026 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020027 if (is_string)
20028 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020029 tv.v_type = VAR_STRING;
20030 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020031 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020032 }
20033 else
20034 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020035 tv.v_type = VAR_NUMBER;
20036 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020037 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020038 set_var(virp->vir_line + 1, &tv, FALSE);
20039 if (is_string)
20040 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020041 }
20042 }
20043 }
20044
20045 return viminfo_readline(virp);
20046}
20047
20048/*
20049 * Write global vars that start with a capital to the viminfo file
20050 */
20051 void
20052write_viminfo_varlist(fp)
20053 FILE *fp;
20054{
Bram Moolenaar33570922005-01-25 22:26:29 +000020055 hashitem_T *hi;
20056 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020057 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020058 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020059 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020060 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020061 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020062
20063 if (find_viminfo_parameter('!') == NULL)
20064 return;
20065
20066 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000020067
Bram Moolenaar33570922005-01-25 22:26:29 +000020068 todo = globvarht.ht_used;
20069 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020070 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020071 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020072 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020073 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020074 this_var = HI2DI(hi);
20075 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020076 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020077 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000020078 {
20079 case VAR_STRING: s = "STR"; break;
20080 case VAR_NUMBER: s = "NUM"; break;
20081 default: continue;
20082 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020083 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000020084 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020085 if (p != NULL)
20086 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000020087 vim_free(tofree);
20088 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020089 }
20090 }
20091}
20092#endif
20093
20094#if defined(FEAT_SESSION) || defined(PROTO)
20095 int
20096store_session_globals(fd)
20097 FILE *fd;
20098{
Bram Moolenaar33570922005-01-25 22:26:29 +000020099 hashitem_T *hi;
20100 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020101 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020102 char_u *p, *t;
20103
Bram Moolenaar33570922005-01-25 22:26:29 +000020104 todo = globvarht.ht_used;
20105 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020106 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020107 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020108 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020109 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020110 this_var = HI2DI(hi);
20111 if ((this_var->di_tv.v_type == VAR_NUMBER
20112 || this_var->di_tv.v_type == VAR_STRING)
20113 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020114 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020115 /* Escape special characters with a backslash. Turn a LF and
20116 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020117 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000020118 (char_u *)"\\\"\n\r");
20119 if (p == NULL) /* out of memory */
20120 break;
20121 for (t = p; *t != NUL; ++t)
20122 if (*t == '\n')
20123 *t = 'n';
20124 else if (*t == '\r')
20125 *t = 'r';
20126 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000020127 this_var->di_key,
20128 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20129 : ' ',
20130 p,
20131 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20132 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000020133 || put_eol(fd) == FAIL)
20134 {
20135 vim_free(p);
20136 return FAIL;
20137 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020138 vim_free(p);
20139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020140 }
20141 }
20142 return OK;
20143}
20144#endif
20145
Bram Moolenaar661b1822005-07-28 22:36:45 +000020146/*
20147 * Display script name where an item was last set.
20148 * Should only be invoked when 'verbose' is non-zero.
20149 */
20150 void
20151last_set_msg(scriptID)
20152 scid_T scriptID;
20153{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020154 char_u *p;
20155
Bram Moolenaar661b1822005-07-28 22:36:45 +000020156 if (scriptID != 0)
20157 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020158 p = home_replace_save(NULL, get_scriptname(scriptID));
20159 if (p != NULL)
20160 {
20161 verbose_enter();
20162 MSG_PUTS(_("\n\tLast set from "));
20163 MSG_PUTS(p);
20164 vim_free(p);
20165 verbose_leave();
20166 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000020167 }
20168}
20169
Bram Moolenaar071d4272004-06-13 20:20:40 +000020170#endif /* FEAT_EVAL */
20171
20172#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
20173
20174
20175#ifdef WIN3264
20176/*
20177 * Functions for ":8" filename modifier: get 8.3 version of a filename.
20178 */
20179static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
20180static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
20181static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
20182
20183/*
20184 * Get the short pathname of a file.
20185 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
20186 */
20187 static int
20188get_short_pathname(fnamep, bufp, fnamelen)
20189 char_u **fnamep;
20190 char_u **bufp;
20191 int *fnamelen;
20192{
20193 int l,len;
20194 char_u *newbuf;
20195
20196 len = *fnamelen;
20197
20198 l = GetShortPathName(*fnamep, *fnamep, len);
20199 if (l > len - 1)
20200 {
20201 /* If that doesn't work (not enough space), then save the string
20202 * and try again with a new buffer big enough
20203 */
20204 newbuf = vim_strnsave(*fnamep, l);
20205 if (newbuf == NULL)
20206 return 0;
20207
20208 vim_free(*bufp);
20209 *fnamep = *bufp = newbuf;
20210
20211 l = GetShortPathName(*fnamep,*fnamep,l+1);
20212
20213 /* Really should always succeed, as the buffer is big enough */
20214 }
20215
20216 *fnamelen = l;
20217 return 1;
20218}
20219
20220/*
20221 * Create a short path name. Returns the length of the buffer it needs.
20222 * Doesn't copy over the end of the buffer passed in.
20223 */
20224 static int
20225shortpath_for_invalid_fname(fname, bufp, fnamelen)
20226 char_u **fname;
20227 char_u **bufp;
20228 int *fnamelen;
20229{
20230 char_u *s, *p, *pbuf2, *pbuf3;
20231 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020232 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020233
20234 /* Make a copy */
20235 len2 = *fnamelen;
20236 pbuf2 = vim_strnsave(*fname, len2);
20237 pbuf3 = NULL;
20238
20239 s = pbuf2 + len2 - 1; /* Find the end */
20240 slen = 1;
20241 plen = len2;
20242
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020243 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020244 {
20245 --s;
20246 ++slen;
20247 --plen;
20248 }
20249
20250 do
20251 {
20252 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020253 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020254 {
20255 --s;
20256 ++slen;
20257 --plen;
20258 }
20259 if (s <= pbuf2)
20260 break;
20261
20262 /* Remeber the character that is about to be blatted */
20263 ch = *s;
20264 *s = 0; /* get_short_pathname requires a null-terminated string */
20265
20266 /* Try it in situ */
20267 p = pbuf2;
20268 if (!get_short_pathname(&p, &pbuf3, &plen))
20269 {
20270 vim_free(pbuf2);
20271 return -1;
20272 }
20273 *s = ch; /* Preserve the string */
20274 } while (plen == 0);
20275
20276 if (plen > 0)
20277 {
20278 /* Remeber the length of the new string. */
20279 *fnamelen = len = plen + slen;
20280 vim_free(*bufp);
20281 if (len > len2)
20282 {
20283 /* If there's not enough space in the currently allocated string,
20284 * then copy it to a buffer big enough.
20285 */
20286 *fname= *bufp = vim_strnsave(p, len);
20287 if (*fname == NULL)
20288 return -1;
20289 }
20290 else
20291 {
20292 /* Transfer pbuf2 to being the main buffer (it's big enough) */
20293 *fname = *bufp = pbuf2;
20294 if (p != pbuf2)
20295 strncpy(*fname, p, plen);
20296 pbuf2 = NULL;
20297 }
20298 /* Concat the next bit */
20299 strncpy(*fname + plen, s, slen);
20300 (*fname)[len] = '\0';
20301 }
20302 vim_free(pbuf3);
20303 vim_free(pbuf2);
20304 return 0;
20305}
20306
20307/*
20308 * Get a pathname for a partial path.
20309 */
20310 static int
20311shortpath_for_partial(fnamep, bufp, fnamelen)
20312 char_u **fnamep;
20313 char_u **bufp;
20314 int *fnamelen;
20315{
20316 int sepcount, len, tflen;
20317 char_u *p;
20318 char_u *pbuf, *tfname;
20319 int hasTilde;
20320
20321 /* Count up the path seperators from the RHS.. so we know which part
20322 * of the path to return.
20323 */
20324 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020325 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020326 if (vim_ispathsep(*p))
20327 ++sepcount;
20328
20329 /* Need full path first (use expand_env() to remove a "~/") */
20330 hasTilde = (**fnamep == '~');
20331 if (hasTilde)
20332 pbuf = tfname = expand_env_save(*fnamep);
20333 else
20334 pbuf = tfname = FullName_save(*fnamep, FALSE);
20335
20336 len = tflen = STRLEN(tfname);
20337
20338 if (!get_short_pathname(&tfname, &pbuf, &len))
20339 return -1;
20340
20341 if (len == 0)
20342 {
20343 /* Don't have a valid filename, so shorten the rest of the
20344 * path if we can. This CAN give us invalid 8.3 filenames, but
20345 * there's not a lot of point in guessing what it might be.
20346 */
20347 len = tflen;
20348 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
20349 return -1;
20350 }
20351
20352 /* Count the paths backward to find the beginning of the desired string. */
20353 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020354 {
20355#ifdef FEAT_MBYTE
20356 if (has_mbyte)
20357 p -= mb_head_off(tfname, p);
20358#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020359 if (vim_ispathsep(*p))
20360 {
20361 if (sepcount == 0 || (hasTilde && sepcount == 1))
20362 break;
20363 else
20364 sepcount --;
20365 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020367 if (hasTilde)
20368 {
20369 --p;
20370 if (p >= tfname)
20371 *p = '~';
20372 else
20373 return -1;
20374 }
20375 else
20376 ++p;
20377
20378 /* Copy in the string - p indexes into tfname - allocated at pbuf */
20379 vim_free(*bufp);
20380 *fnamelen = (int)STRLEN(p);
20381 *bufp = pbuf;
20382 *fnamep = p;
20383
20384 return 0;
20385}
20386#endif /* WIN3264 */
20387
20388/*
20389 * Adjust a filename, according to a string of modifiers.
20390 * *fnamep must be NUL terminated when called. When returning, the length is
20391 * determined by *fnamelen.
20392 * Returns valid flags.
20393 * When there is an error, *fnamep is set to NULL.
20394 */
20395 int
20396modify_fname(src, usedlen, fnamep, bufp, fnamelen)
20397 char_u *src; /* string with modifiers */
20398 int *usedlen; /* characters after src that are used */
20399 char_u **fnamep; /* file name so far */
20400 char_u **bufp; /* buffer for allocated file name or NULL */
20401 int *fnamelen; /* length of fnamep */
20402{
20403 int valid = 0;
20404 char_u *tail;
20405 char_u *s, *p, *pbuf;
20406 char_u dirname[MAXPATHL];
20407 int c;
20408 int has_fullname = 0;
20409#ifdef WIN3264
20410 int has_shortname = 0;
20411#endif
20412
20413repeat:
20414 /* ":p" - full path/file_name */
20415 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
20416 {
20417 has_fullname = 1;
20418
20419 valid |= VALID_PATH;
20420 *usedlen += 2;
20421
20422 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
20423 if ((*fnamep)[0] == '~'
20424#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
20425 && ((*fnamep)[1] == '/'
20426# ifdef BACKSLASH_IN_FILENAME
20427 || (*fnamep)[1] == '\\'
20428# endif
20429 || (*fnamep)[1] == NUL)
20430
20431#endif
20432 )
20433 {
20434 *fnamep = expand_env_save(*fnamep);
20435 vim_free(*bufp); /* free any allocated file name */
20436 *bufp = *fnamep;
20437 if (*fnamep == NULL)
20438 return -1;
20439 }
20440
20441 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020442 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020443 {
20444 if (vim_ispathsep(*p)
20445 && p[1] == '.'
20446 && (p[2] == NUL
20447 || vim_ispathsep(p[2])
20448 || (p[2] == '.'
20449 && (p[3] == NUL || vim_ispathsep(p[3])))))
20450 break;
20451 }
20452
20453 /* FullName_save() is slow, don't use it when not needed. */
20454 if (*p != NUL || !vim_isAbsName(*fnamep))
20455 {
20456 *fnamep = FullName_save(*fnamep, *p != NUL);
20457 vim_free(*bufp); /* free any allocated file name */
20458 *bufp = *fnamep;
20459 if (*fnamep == NULL)
20460 return -1;
20461 }
20462
20463 /* Append a path separator to a directory. */
20464 if (mch_isdir(*fnamep))
20465 {
20466 /* Make room for one or two extra characters. */
20467 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
20468 vim_free(*bufp); /* free any allocated file name */
20469 *bufp = *fnamep;
20470 if (*fnamep == NULL)
20471 return -1;
20472 add_pathsep(*fnamep);
20473 }
20474 }
20475
20476 /* ":." - path relative to the current directory */
20477 /* ":~" - path relative to the home directory */
20478 /* ":8" - shortname path - postponed till after */
20479 while (src[*usedlen] == ':'
20480 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
20481 {
20482 *usedlen += 2;
20483 if (c == '8')
20484 {
20485#ifdef WIN3264
20486 has_shortname = 1; /* Postpone this. */
20487#endif
20488 continue;
20489 }
20490 pbuf = NULL;
20491 /* Need full path first (use expand_env() to remove a "~/") */
20492 if (!has_fullname)
20493 {
20494 if (c == '.' && **fnamep == '~')
20495 p = pbuf = expand_env_save(*fnamep);
20496 else
20497 p = pbuf = FullName_save(*fnamep, FALSE);
20498 }
20499 else
20500 p = *fnamep;
20501
20502 has_fullname = 0;
20503
20504 if (p != NULL)
20505 {
20506 if (c == '.')
20507 {
20508 mch_dirname(dirname, MAXPATHL);
20509 s = shorten_fname(p, dirname);
20510 if (s != NULL)
20511 {
20512 *fnamep = s;
20513 if (pbuf != NULL)
20514 {
20515 vim_free(*bufp); /* free any allocated file name */
20516 *bufp = pbuf;
20517 pbuf = NULL;
20518 }
20519 }
20520 }
20521 else
20522 {
20523 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
20524 /* Only replace it when it starts with '~' */
20525 if (*dirname == '~')
20526 {
20527 s = vim_strsave(dirname);
20528 if (s != NULL)
20529 {
20530 *fnamep = s;
20531 vim_free(*bufp);
20532 *bufp = s;
20533 }
20534 }
20535 }
20536 vim_free(pbuf);
20537 }
20538 }
20539
20540 tail = gettail(*fnamep);
20541 *fnamelen = (int)STRLEN(*fnamep);
20542
20543 /* ":h" - head, remove "/file_name", can be repeated */
20544 /* Don't remove the first "/" or "c:\" */
20545 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
20546 {
20547 valid |= VALID_HEAD;
20548 *usedlen += 2;
20549 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020550 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020551 --tail;
20552 *fnamelen = (int)(tail - *fnamep);
20553#ifdef VMS
20554 if (*fnamelen > 0)
20555 *fnamelen += 1; /* the path separator is part of the path */
20556#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020557 while (tail > s && !after_pathsep(s, tail))
20558 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020559 }
20560
20561 /* ":8" - shortname */
20562 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
20563 {
20564 *usedlen += 2;
20565#ifdef WIN3264
20566 has_shortname = 1;
20567#endif
20568 }
20569
20570#ifdef WIN3264
20571 /* Check shortname after we have done 'heads' and before we do 'tails'
20572 */
20573 if (has_shortname)
20574 {
20575 pbuf = NULL;
20576 /* Copy the string if it is shortened by :h */
20577 if (*fnamelen < (int)STRLEN(*fnamep))
20578 {
20579 p = vim_strnsave(*fnamep, *fnamelen);
20580 if (p == 0)
20581 return -1;
20582 vim_free(*bufp);
20583 *bufp = *fnamep = p;
20584 }
20585
20586 /* Split into two implementations - makes it easier. First is where
20587 * there isn't a full name already, second is where there is.
20588 */
20589 if (!has_fullname && !vim_isAbsName(*fnamep))
20590 {
20591 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
20592 return -1;
20593 }
20594 else
20595 {
20596 int l;
20597
20598 /* Simple case, already have the full-name
20599 * Nearly always shorter, so try first time. */
20600 l = *fnamelen;
20601 if (!get_short_pathname(fnamep, bufp, &l))
20602 return -1;
20603
20604 if (l == 0)
20605 {
20606 /* Couldn't find the filename.. search the paths.
20607 */
20608 l = *fnamelen;
20609 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
20610 return -1;
20611 }
20612 *fnamelen = l;
20613 }
20614 }
20615#endif /* WIN3264 */
20616
20617 /* ":t" - tail, just the basename */
20618 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
20619 {
20620 *usedlen += 2;
20621 *fnamelen -= (int)(tail - *fnamep);
20622 *fnamep = tail;
20623 }
20624
20625 /* ":e" - extension, can be repeated */
20626 /* ":r" - root, without extension, can be repeated */
20627 while (src[*usedlen] == ':'
20628 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
20629 {
20630 /* find a '.' in the tail:
20631 * - for second :e: before the current fname
20632 * - otherwise: The last '.'
20633 */
20634 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
20635 s = *fnamep - 2;
20636 else
20637 s = *fnamep + *fnamelen - 1;
20638 for ( ; s > tail; --s)
20639 if (s[0] == '.')
20640 break;
20641 if (src[*usedlen + 1] == 'e') /* :e */
20642 {
20643 if (s > tail)
20644 {
20645 *fnamelen += (int)(*fnamep - (s + 1));
20646 *fnamep = s + 1;
20647#ifdef VMS
20648 /* cut version from the extension */
20649 s = *fnamep + *fnamelen - 1;
20650 for ( ; s > *fnamep; --s)
20651 if (s[0] == ';')
20652 break;
20653 if (s > *fnamep)
20654 *fnamelen = s - *fnamep;
20655#endif
20656 }
20657 else if (*fnamep <= tail)
20658 *fnamelen = 0;
20659 }
20660 else /* :r */
20661 {
20662 if (s > tail) /* remove one extension */
20663 *fnamelen = (int)(s - *fnamep);
20664 }
20665 *usedlen += 2;
20666 }
20667
20668 /* ":s?pat?foo?" - substitute */
20669 /* ":gs?pat?foo?" - global substitute */
20670 if (src[*usedlen] == ':'
20671 && (src[*usedlen + 1] == 's'
20672 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
20673 {
20674 char_u *str;
20675 char_u *pat;
20676 char_u *sub;
20677 int sep;
20678 char_u *flags;
20679 int didit = FALSE;
20680
20681 flags = (char_u *)"";
20682 s = src + *usedlen + 2;
20683 if (src[*usedlen + 1] == 'g')
20684 {
20685 flags = (char_u *)"g";
20686 ++s;
20687 }
20688
20689 sep = *s++;
20690 if (sep)
20691 {
20692 /* find end of pattern */
20693 p = vim_strchr(s, sep);
20694 if (p != NULL)
20695 {
20696 pat = vim_strnsave(s, (int)(p - s));
20697 if (pat != NULL)
20698 {
20699 s = p + 1;
20700 /* find end of substitution */
20701 p = vim_strchr(s, sep);
20702 if (p != NULL)
20703 {
20704 sub = vim_strnsave(s, (int)(p - s));
20705 str = vim_strnsave(*fnamep, *fnamelen);
20706 if (sub != NULL && str != NULL)
20707 {
20708 *usedlen = (int)(p + 1 - src);
20709 s = do_string_sub(str, pat, sub, flags);
20710 if (s != NULL)
20711 {
20712 *fnamep = s;
20713 *fnamelen = (int)STRLEN(s);
20714 vim_free(*bufp);
20715 *bufp = s;
20716 didit = TRUE;
20717 }
20718 }
20719 vim_free(sub);
20720 vim_free(str);
20721 }
20722 vim_free(pat);
20723 }
20724 }
20725 /* after using ":s", repeat all the modifiers */
20726 if (didit)
20727 goto repeat;
20728 }
20729 }
20730
20731 return valid;
20732}
20733
20734/*
20735 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
20736 * "flags" can be "g" to do a global substitute.
20737 * Returns an allocated string, NULL for error.
20738 */
20739 char_u *
20740do_string_sub(str, pat, sub, flags)
20741 char_u *str;
20742 char_u *pat;
20743 char_u *sub;
20744 char_u *flags;
20745{
20746 int sublen;
20747 regmatch_T regmatch;
20748 int i;
20749 int do_all;
20750 char_u *tail;
20751 garray_T ga;
20752 char_u *ret;
20753 char_u *save_cpo;
20754
20755 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
20756 save_cpo = p_cpo;
20757 p_cpo = (char_u *)"";
20758
20759 ga_init2(&ga, 1, 200);
20760
20761 do_all = (flags[0] == 'g');
20762
20763 regmatch.rm_ic = p_ic;
20764 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
20765 if (regmatch.regprog != NULL)
20766 {
20767 tail = str;
20768 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
20769 {
20770 /*
20771 * Get some space for a temporary buffer to do the substitution
20772 * into. It will contain:
20773 * - The text up to where the match is.
20774 * - The substituted text.
20775 * - The text after the match.
20776 */
20777 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
20778 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
20779 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
20780 {
20781 ga_clear(&ga);
20782 break;
20783 }
20784
20785 /* copy the text up to where the match is */
20786 i = (int)(regmatch.startp[0] - tail);
20787 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
20788 /* add the substituted text */
20789 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
20790 + ga.ga_len + i, TRUE, TRUE, FALSE);
20791 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020792 /* avoid getting stuck on a match with an empty string */
20793 if (tail == regmatch.endp[0])
20794 {
20795 if (*tail == NUL)
20796 break;
20797 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
20798 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020799 }
20800 else
20801 {
20802 tail = regmatch.endp[0];
20803 if (*tail == NUL)
20804 break;
20805 }
20806 if (!do_all)
20807 break;
20808 }
20809
20810 if (ga.ga_data != NULL)
20811 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
20812
20813 vim_free(regmatch.regprog);
20814 }
20815
20816 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
20817 ga_clear(&ga);
20818 p_cpo = save_cpo;
20819
20820 return ret;
20821}
20822
20823#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */