blob: 4e202f40444f0cc96ab7ad798627067af2a45ca5 [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)
14# include <io.h> /* for mch_open(), must be before vim.h */
15#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 Moolenaar33570922005-01-25 22:26:29 +0000340};
341
342/* shorthand */
343#define vv_type vv_di.di_tv.v_type
344#define vv_nr vv_di.di_tv.vval.v_number
345#define vv_str vv_di.di_tv.vval.v_string
346#define vv_tv vv_di.di_tv
347
348/*
349 * The v: variables are stored in dictionary "vimvardict".
350 * "vimvars_var" is the variable that is used for the "l:" scope.
351 */
352static dict_T vimvardict;
353static dictitem_T vimvars_var;
354#define vimvarht vimvardict.dv_hashtab
355
Bram Moolenaara40058a2005-07-11 22:42:07 +0000356static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
357static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
358#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
359static int call_vim_function __ARGS((char_u *func, int argc, char_u **argv, int safe, typval_T *rettv));
360#endif
361static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
362static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
363static char_u *skip_var_one __ARGS((char_u *arg));
364static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty));
365static void list_glob_vars __ARGS((void));
366static void list_buf_vars __ARGS((void));
367static void list_win_vars __ARGS((void));
368static void list_vim_vars __ARGS((void));
369static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg));
370static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
371static int check_changedtick __ARGS((char_u *arg));
372static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
373static void clear_lval __ARGS((lval_T *lp));
374static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
375static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
376static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
377static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
378static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
379static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
380static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
381static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
382static void item_lock __ARGS((typval_T *tv, int deep, int lock));
383static int tv_islocked __ARGS((typval_T *tv));
384
Bram Moolenaar33570922005-01-25 22:26:29 +0000385static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
386static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
387static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
388static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
389static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
390static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
391static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
392static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000393
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000394static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000395static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
396static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
397static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
398static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
399static list_T *list_alloc __ARGS((void));
Bram Moolenaar33570922005-01-25 22:26:29 +0000400static void list_free __ARGS((list_T *l));
401static listitem_T *listitem_alloc __ARGS((void));
402static void listitem_free __ARGS((listitem_T *item));
403static void listitem_remove __ARGS((list_T *l, listitem_T *item));
404static long list_len __ARGS((list_T *l));
405static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
406static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
407static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
Bram Moolenaar33570922005-01-25 22:26:29 +0000408static listitem_T *list_find __ARGS((list_T *l, long n));
409static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000410static void list_append __ARGS((list_T *l, listitem_T *item));
411static int list_append_tv __ARGS((list_T *l, typval_T *tv));
412static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
413static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
414static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000415static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000416static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
417static char_u *list2string __ARGS((typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000418static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000419static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
420static void set_ref_in_list __ARGS((list_T *l, int copyID));
421static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000422static void dict_unref __ARGS((dict_T *d));
423static void dict_free __ARGS((dict_T *d));
424static dictitem_T *dictitem_alloc __ARGS((char_u *key));
425static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
426static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
427static void dictitem_free __ARGS((dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000428static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000429static int dict_add __ARGS((dict_T *d, dictitem_T *item));
430static long dict_len __ARGS((dict_T *d));
431static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
432static char_u *dict2string __ARGS((typval_T *tv));
433static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar33570922005-01-25 22:26:29 +0000434static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf));
435static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf));
436static char_u *string_quote __ARGS((char_u *str, int function));
437static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
438static int find_internal_func __ARGS((char_u *name));
439static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
440static 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));
441static 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 +0000442static void emsg_funcname __ARGS((char *msg, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000443
444static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
445static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
446static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
447static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
448static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
449static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
450static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
451static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
452static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
453static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
454static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
455static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
456static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
457static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
458static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
459static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
460static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
463static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
464static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
465static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
467static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
468static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
469static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
470static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000494static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000495static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000496static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000497static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
503static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000509static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000510static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
534static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000537static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000538static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000554static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000555static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000558#ifdef vim_mkdir
559static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
560#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000561static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000565static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000566static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000567static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000568static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000585static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000586static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000590static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000591static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000593static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
594#ifdef HAVE_STRFTIME
595static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
596#endif
597static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000609static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000610static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
619static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
620static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
624static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000625static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000626
Bram Moolenaar33570922005-01-25 22:26:29 +0000627static pos_T *var2fpos __ARGS((typval_T *varp, int lnum));
628static int get_env_len __ARGS((char_u **arg));
629static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000630static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000631static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
632#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
633#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
634 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000635static 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 +0000636static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000637static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000638static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
639static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000640static typval_T *alloc_tv __ARGS((void));
641static typval_T *alloc_string_tv __ARGS((char_u *string));
642static void free_tv __ARGS((typval_T *varp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000643static void init_tv __ARGS((typval_T *varp));
644static long get_tv_number __ARGS((typval_T *varp));
645static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000646static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000647static char_u *get_tv_string __ARGS((typval_T *varp));
648static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000649static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000650static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000651static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000652static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
653static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
654static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
655static void list_one_var __ARGS((dictitem_T *v, char_u *prefix));
656static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
657static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
658static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000659static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000660static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000661static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000662static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
663static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
664static int eval_fname_script __ARGS((char_u *p));
665static int eval_fname_sid __ARGS((char_u *p));
666static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000667static ufunc_T *find_func __ARGS((char_u *name));
668static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000669static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000670#ifdef FEAT_PROFILE
671static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000672static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
673static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
674static int
675# ifdef __BORLANDC__
676 _RTLENTRYF
677# endif
678 prof_total_cmp __ARGS((const void *s1, const void *s2));
679static int
680# ifdef __BORLANDC__
681 _RTLENTRYF
682# endif
683 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000684#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000685static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000686static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000687static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000688static void func_free __ARGS((ufunc_T *fp));
689static void func_unref __ARGS((char_u *name));
690static void func_ref __ARGS((char_u *name));
691static 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));
692static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
693
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000694/* Character used as separated in autoload function/variable names. */
695#define AUTOLOAD_CHAR '#'
696
Bram Moolenaar33570922005-01-25 22:26:29 +0000697/*
698 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000699 */
700 void
701eval_init()
702{
Bram Moolenaar33570922005-01-25 22:26:29 +0000703 int i;
704 struct vimvar *p;
705
706 init_var_dict(&globvardict, &globvars_var);
707 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000708 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000709 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000710
711 for (i = 0; i < VV_LEN; ++i)
712 {
713 p = &vimvars[i];
714 STRCPY(p->vv_di.di_key, p->vv_name);
715 if (p->vv_flags & VV_RO)
716 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
717 else if (p->vv_flags & VV_RO_SBX)
718 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
719 else
720 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000721
722 /* add to v: scope dict, unless the value is not always available */
723 if (p->vv_type != VAR_UNKNOWN)
724 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000725 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000726 /* add to compat scope dict */
727 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000728 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000729}
730
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000731#if defined(EXITFREE) || defined(PROTO)
732 void
733eval_clear()
734{
735 int i;
736 struct vimvar *p;
737
738 for (i = 0; i < VV_LEN; ++i)
739 {
740 p = &vimvars[i];
741 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000742 {
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000743 vim_free(p->vv_di.di_tv.vval.v_string);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000744 p->vv_di.di_tv.vval.v_string = NULL;
745 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000746 }
747 hash_clear(&vimvarht);
748 hash_clear(&compat_hashtab);
749
750 /* script-local variables */
751 for (i = 1; i <= ga_scripts.ga_len; ++i)
752 vars_clear(&SCRIPT_VARS(i));
753 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000754 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000755
756 /* global variables */
757 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000758
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000759 /* functions */
Bram Moolenaard9fba312005-06-26 22:34:35 +0000760 free_all_functions();
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000761 hash_clear(&func_hashtab);
762
763 /* unreferenced lists and dicts */
764 (void)garbage_collect();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000765}
766#endif
767
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000768/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 * Return the name of the executed function.
770 */
771 char_u *
772func_name(cookie)
773 void *cookie;
774{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000775 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776}
777
778/*
779 * Return the address holding the next breakpoint line for a funccall cookie.
780 */
781 linenr_T *
782func_breakpoint(cookie)
783 void *cookie;
784{
Bram Moolenaar33570922005-01-25 22:26:29 +0000785 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786}
787
788/*
789 * Return the address holding the debug tick for a funccall cookie.
790 */
791 int *
792func_dbg_tick(cookie)
793 void *cookie;
794{
Bram Moolenaar33570922005-01-25 22:26:29 +0000795 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796}
797
798/*
799 * Return the nesting level for a funccall cookie.
800 */
801 int
802func_level(cookie)
803 void *cookie;
804{
Bram Moolenaar33570922005-01-25 22:26:29 +0000805 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806}
807
808/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000809funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810
811/*
812 * Return TRUE when a function was ended by a ":return" command.
813 */
814 int
815current_func_returned()
816{
817 return current_funccal->returned;
818}
819
820
821/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 * Set an internal variable to a string value. Creates the variable if it does
823 * not already exist.
824 */
825 void
826set_internal_string_var(name, value)
827 char_u *name;
828 char_u *value;
829{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000830 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000831 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832
833 val = vim_strsave(value);
834 if (val != NULL)
835 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000836 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000837 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000839 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000840 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 }
842 }
843}
844
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000845static lval_T *redir_lval = NULL;
846static char_u *redir_endp = NULL;
847static char_u *redir_varname = NULL;
848
849/*
850 * Start recording command output to a variable
851 * Returns OK if successfully completed the setup. FAIL otherwise.
852 */
853 int
854var_redir_start(name, append)
855 char_u *name;
856 int append; /* append to an existing variable */
857{
858 int save_emsg;
859 int err;
860 typval_T tv;
861
862 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000863 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000864 {
865 EMSG(_(e_invarg));
866 return FAIL;
867 }
868
869 redir_varname = vim_strsave(name);
870 if (redir_varname == NULL)
871 return FAIL;
872
873 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
874 if (redir_lval == NULL)
875 {
876 var_redir_stop();
877 return FAIL;
878 }
879
880 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000881 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
882 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000883 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
884 {
885 if (redir_endp != NULL && *redir_endp != NUL)
886 /* Trailing characters are present after the variable name */
887 EMSG(_(e_trailing));
888 else
889 EMSG(_(e_invarg));
890 var_redir_stop();
891 return FAIL;
892 }
893
894 /* check if we can write to the variable: set it to or append an empty
895 * string */
896 save_emsg = did_emsg;
897 did_emsg = FALSE;
898 tv.v_type = VAR_STRING;
899 tv.vval.v_string = (char_u *)"";
900 if (append)
901 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
902 else
903 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
904 err = did_emsg;
905 did_emsg += save_emsg;
906 if (err)
907 {
908 var_redir_stop();
909 return FAIL;
910 }
911 if (redir_lval->ll_newkey != NULL)
912 {
913 /* Dictionary item was created, don't do it again. */
914 vim_free(redir_lval->ll_newkey);
915 redir_lval->ll_newkey = NULL;
916 }
917
918 return OK;
919}
920
921/*
922 * Append "value[len]" to the variable set by var_redir_start().
923 */
924 void
925var_redir_str(value, len)
926 char_u *value;
927 int len;
928{
929 char_u *val;
930 typval_T tv;
931 int save_emsg;
932 int err;
933
934 if (redir_lval == NULL)
935 return;
936
937 if (len == -1)
938 /* Append the entire string */
939 val = vim_strsave(value);
940 else
941 /* Append only the specified number of characters */
942 val = vim_strnsave(value, len);
943 if (val == NULL)
944 return;
945
946 tv.v_type = VAR_STRING;
947 tv.vval.v_string = val;
948
949 save_emsg = did_emsg;
950 did_emsg = FALSE;
951 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
952 err = did_emsg;
953 did_emsg += save_emsg;
954 if (err)
955 var_redir_stop();
956
957 vim_free(tv.vval.v_string);
958}
959
960/*
961 * Stop redirecting command output to a variable.
962 */
963 void
964var_redir_stop()
965{
966 if (redir_lval != NULL)
967 {
968 clear_lval(redir_lval);
969 vim_free(redir_lval);
970 redir_lval = NULL;
971 }
972 vim_free(redir_varname);
973 redir_varname = NULL;
974}
975
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976# if defined(FEAT_MBYTE) || defined(PROTO)
977 int
978eval_charconvert(enc_from, enc_to, fname_from, fname_to)
979 char_u *enc_from;
980 char_u *enc_to;
981 char_u *fname_from;
982 char_u *fname_to;
983{
984 int err = FALSE;
985
986 set_vim_var_string(VV_CC_FROM, enc_from, -1);
987 set_vim_var_string(VV_CC_TO, enc_to, -1);
988 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
989 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
990 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
991 err = TRUE;
992 set_vim_var_string(VV_CC_FROM, NULL, -1);
993 set_vim_var_string(VV_CC_TO, NULL, -1);
994 set_vim_var_string(VV_FNAME_IN, NULL, -1);
995 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
996
997 if (err)
998 return FAIL;
999 return OK;
1000}
1001# endif
1002
1003# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1004 int
1005eval_printexpr(fname, args)
1006 char_u *fname;
1007 char_u *args;
1008{
1009 int err = FALSE;
1010
1011 set_vim_var_string(VV_FNAME_IN, fname, -1);
1012 set_vim_var_string(VV_CMDARG, args, -1);
1013 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1014 err = TRUE;
1015 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1016 set_vim_var_string(VV_CMDARG, NULL, -1);
1017
1018 if (err)
1019 {
1020 mch_remove(fname);
1021 return FAIL;
1022 }
1023 return OK;
1024}
1025# endif
1026
1027# if defined(FEAT_DIFF) || defined(PROTO)
1028 void
1029eval_diff(origfile, newfile, outfile)
1030 char_u *origfile;
1031 char_u *newfile;
1032 char_u *outfile;
1033{
1034 int err = FALSE;
1035
1036 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1037 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1038 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1039 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1040 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1041 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1042 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1043}
1044
1045 void
1046eval_patch(origfile, difffile, outfile)
1047 char_u *origfile;
1048 char_u *difffile;
1049 char_u *outfile;
1050{
1051 int err;
1052
1053 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1054 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1055 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1056 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1057 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1058 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1059 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1060}
1061# endif
1062
1063/*
1064 * Top level evaluation function, returning a boolean.
1065 * Sets "error" to TRUE if there was an error.
1066 * Return TRUE or FALSE.
1067 */
1068 int
1069eval_to_bool(arg, error, nextcmd, skip)
1070 char_u *arg;
1071 int *error;
1072 char_u **nextcmd;
1073 int skip; /* only parse, don't execute */
1074{
Bram Moolenaar33570922005-01-25 22:26:29 +00001075 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 int retval = FALSE;
1077
1078 if (skip)
1079 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001080 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 else
1083 {
1084 *error = FALSE;
1085 if (!skip)
1086 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001087 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001088 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 }
1090 }
1091 if (skip)
1092 --emsg_skip;
1093
1094 return retval;
1095}
1096
1097/*
1098 * Top level evaluation function, returning a string. If "skip" is TRUE,
1099 * only parsing to "nextcmd" is done, without reporting errors. Return
1100 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1101 */
1102 char_u *
1103eval_to_string_skip(arg, nextcmd, skip)
1104 char_u *arg;
1105 char_u **nextcmd;
1106 int skip; /* only parse, don't execute */
1107{
Bram Moolenaar33570922005-01-25 22:26:29 +00001108 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 char_u *retval;
1110
1111 if (skip)
1112 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001113 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 retval = NULL;
1115 else
1116 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001117 retval = vim_strsave(get_tv_string(&tv));
1118 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 }
1120 if (skip)
1121 --emsg_skip;
1122
1123 return retval;
1124}
1125
1126/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001127 * Skip over an expression at "*pp".
1128 * Return FAIL for an error, OK otherwise.
1129 */
1130 int
1131skip_expr(pp)
1132 char_u **pp;
1133{
Bram Moolenaar33570922005-01-25 22:26:29 +00001134 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001135
1136 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001137 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001138}
1139
1140/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 * Top level evaluation function, returning a string.
1142 * Return pointer to allocated memory, or NULL for failure.
1143 */
1144 char_u *
1145eval_to_string(arg, nextcmd)
1146 char_u *arg;
1147 char_u **nextcmd;
1148{
Bram Moolenaar33570922005-01-25 22:26:29 +00001149 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 char_u *retval;
1151
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001152 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 retval = NULL;
1154 else
1155 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001156 retval = vim_strsave(get_tv_string(&tv));
1157 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 }
1159
1160 return retval;
1161}
1162
1163/*
1164 * Call eval_to_string() with "sandbox" set and not using local variables.
1165 */
1166 char_u *
1167eval_to_string_safe(arg, nextcmd)
1168 char_u *arg;
1169 char_u **nextcmd;
1170{
1171 char_u *retval;
1172 void *save_funccalp;
1173
1174 save_funccalp = save_funccal();
1175 ++sandbox;
1176 retval = eval_to_string(arg, nextcmd);
1177 --sandbox;
1178 restore_funccal(save_funccalp);
1179 return retval;
1180}
1181
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182/*
1183 * Top level evaluation function, returning a number.
1184 * Evaluates "expr" silently.
1185 * Returns -1 for an error.
1186 */
1187 int
1188eval_to_number(expr)
1189 char_u *expr;
1190{
Bram Moolenaar33570922005-01-25 22:26:29 +00001191 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001193 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194
1195 ++emsg_off;
1196
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001197 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 retval = -1;
1199 else
1200 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001201 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001202 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 }
1204 --emsg_off;
1205
1206 return retval;
1207}
1208
Bram Moolenaara40058a2005-07-11 22:42:07 +00001209/*
1210 * Prepare v: variable "idx" to be used.
1211 * Save the current typeval in "save_tv".
1212 * When not used yet add the variable to the v: hashtable.
1213 */
1214 static void
1215prepare_vimvar(idx, save_tv)
1216 int idx;
1217 typval_T *save_tv;
1218{
1219 *save_tv = vimvars[idx].vv_tv;
1220 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1221 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1222}
1223
1224/*
1225 * Restore v: variable "idx" to typeval "save_tv".
1226 * When no longer defined, remove the variable from the v: hashtable.
1227 */
1228 static void
1229restore_vimvar(idx, save_tv)
1230 int idx;
1231 typval_T *save_tv;
1232{
1233 hashitem_T *hi;
1234
1235 clear_tv(&vimvars[idx].vv_tv);
1236 vimvars[idx].vv_tv = *save_tv;
1237 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1238 {
1239 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1240 if (HASHITEM_EMPTY(hi))
1241 EMSG2(_(e_intern2), "restore_vimvar()");
1242 else
1243 hash_remove(&vimvarht, hi);
1244 }
1245}
1246
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001247#if defined(FEAT_SYN_HL) || defined(PROTO)
1248/*
1249 * Evaluate an expression to a list with suggestions.
1250 * For the "expr:" part of 'spellsuggest'.
1251 */
1252 list_T *
1253eval_spell_expr(badword, expr)
1254 char_u *badword;
1255 char_u *expr;
1256{
1257 typval_T save_val;
1258 typval_T rettv;
1259 list_T *list = NULL;
1260 char_u *p = skipwhite(expr);
1261
1262 /* Set "v:val" to the bad word. */
1263 prepare_vimvar(VV_VAL, &save_val);
1264 vimvars[VV_VAL].vv_type = VAR_STRING;
1265 vimvars[VV_VAL].vv_str = badword;
1266 if (p_verbose == 0)
1267 ++emsg_off;
1268
1269 if (eval1(&p, &rettv, TRUE) == OK)
1270 {
1271 if (rettv.v_type != VAR_LIST)
1272 clear_tv(&rettv);
1273 else
1274 list = rettv.vval.v_list;
1275 }
1276
1277 if (p_verbose == 0)
1278 --emsg_off;
1279 vimvars[VV_VAL].vv_str = NULL;
1280 restore_vimvar(VV_VAL, &save_val);
1281
1282 return list;
1283}
1284
1285/*
1286 * "list" is supposed to contain two items: a word and a number. Return the
1287 * word in "pp" and the number as the return value.
1288 * Return -1 if anything isn't right.
1289 * Used to get the good word and score from the eval_spell_expr() result.
1290 */
1291 int
1292get_spellword(list, pp)
1293 list_T *list;
1294 char_u **pp;
1295{
1296 listitem_T *li;
1297
1298 li = list->lv_first;
1299 if (li == NULL)
1300 return -1;
1301 *pp = get_tv_string(&li->li_tv);
1302
1303 li = li->li_next;
1304 if (li == NULL)
1305 return -1;
1306 return get_tv_number(&li->li_tv);
1307}
1308#endif
1309
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001310/*
1311 * Top level evaluation function,
1312 */
1313 typval_T *
1314eval_expr(arg, nextcmd)
1315 char_u *arg;
1316 char_u **nextcmd;
1317{
1318 typval_T *tv;
1319
1320 tv = (typval_T *)alloc(sizeof(typval_T));
1321 if (!tv)
1322 return NULL;
1323
1324 if (eval0(arg, tv, nextcmd, TRUE) == FAIL)
1325 {
1326 vim_free(tv);
1327 return NULL;
1328 }
1329
1330 return tv;
1331}
1332
1333
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1335/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001336 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 * Uses argv[argc] for the function arguments.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001338 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001340 static int
1341call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 char_u *func;
1343 int argc;
1344 char_u **argv;
1345 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001346 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347{
Bram Moolenaar33570922005-01-25 22:26:29 +00001348 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 long n;
1350 int len;
1351 int i;
1352 int doesrange;
1353 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001354 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355
Bram Moolenaar33570922005-01-25 22:26:29 +00001356 argvars = (typval_T *)alloc((unsigned)(argc * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001358 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359
1360 for (i = 0; i < argc; i++)
1361 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001362 /* Pass a NULL or empty argument as an empty string */
1363 if (argv[i] == NULL || *argv[i] == NUL)
1364 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001365 argvars[i].v_type = VAR_STRING;
1366 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001367 continue;
1368 }
1369
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 /* Recognize a number argument, the others must be strings. */
1371 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1372 if (len != 0 && len == (int)STRLEN(argv[i]))
1373 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001374 argvars[i].v_type = VAR_NUMBER;
1375 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 }
1377 else
1378 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001379 argvars[i].v_type = VAR_STRING;
1380 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 }
1382 }
1383
1384 if (safe)
1385 {
1386 save_funccalp = save_funccal();
1387 ++sandbox;
1388 }
1389
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001390 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1391 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001393 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 if (safe)
1395 {
1396 --sandbox;
1397 restore_funccal(save_funccalp);
1398 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001399 vim_free(argvars);
1400
1401 if (ret == FAIL)
1402 clear_tv(rettv);
1403
1404 return ret;
1405}
1406
1407/*
1408 * Call some vimL function and return the result as a string
1409 * Uses argv[argc] for the function arguments.
1410 */
1411 void *
1412call_func_retstr(func, argc, argv, safe)
1413 char_u *func;
1414 int argc;
1415 char_u **argv;
1416 int safe; /* use the sandbox */
1417{
1418 typval_T rettv;
1419 char_u *retval = NULL;
1420
1421 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1422 return NULL;
1423
1424 retval = vim_strsave(get_tv_string(&rettv));
1425
1426 clear_tv(&rettv);
1427
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 return retval;
1429}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001430
1431/*
1432 * Call some vimL function and return the result as a list
1433 * Uses argv[argc] for the function arguments.
1434 */
1435 void *
1436call_func_retlist(func, argc, argv, safe)
1437 char_u *func;
1438 int argc;
1439 char_u **argv;
1440 int safe; /* use the sandbox */
1441{
1442 typval_T rettv;
1443
1444 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1445 return NULL;
1446
1447 if (rettv.v_type != VAR_LIST)
1448 {
1449 clear_tv(&rettv);
1450 return NULL;
1451 }
1452
1453 return rettv.vval.v_list;
1454}
1455
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456#endif
1457
1458/*
1459 * Save the current function call pointer, and set it to NULL.
1460 * Used when executing autocommands and for ":source".
1461 */
1462 void *
1463save_funccal()
1464{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001465 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 current_funccal = NULL;
1468 return (void *)fc;
1469}
1470
1471 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001472restore_funccal(vfc)
1473 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001475 funccall_T *fc = (funccall_T *)vfc;
1476
1477 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478}
1479
Bram Moolenaar05159a02005-02-26 23:04:13 +00001480#if defined(FEAT_PROFILE) || defined(PROTO)
1481/*
1482 * Prepare profiling for entering a child or something else that is not
1483 * counted for the script/function itself.
1484 * Should always be called in pair with prof_child_exit().
1485 */
1486 void
1487prof_child_enter(tm)
1488 proftime_T *tm; /* place to store waittime */
1489{
1490 funccall_T *fc = current_funccal;
1491
1492 if (fc != NULL && fc->func->uf_profiling)
1493 profile_start(&fc->prof_child);
1494 script_prof_save(tm);
1495}
1496
1497/*
1498 * Take care of time spent in a child.
1499 * Should always be called after prof_child_enter().
1500 */
1501 void
1502prof_child_exit(tm)
1503 proftime_T *tm; /* where waittime was stored */
1504{
1505 funccall_T *fc = current_funccal;
1506
1507 if (fc != NULL && fc->func->uf_profiling)
1508 {
1509 profile_end(&fc->prof_child);
1510 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1511 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1512 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1513 }
1514 script_prof_restore(tm);
1515}
1516#endif
1517
1518
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519#ifdef FEAT_FOLDING
1520/*
1521 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1522 * it in "*cp". Doesn't give error messages.
1523 */
1524 int
1525eval_foldexpr(arg, cp)
1526 char_u *arg;
1527 int *cp;
1528{
Bram Moolenaar33570922005-01-25 22:26:29 +00001529 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 int retval;
1531 char_u *s;
1532
1533 ++emsg_off;
1534 ++sandbox;
1535 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001536 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 retval = 0;
1538 else
1539 {
1540 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001541 if (tv.v_type == VAR_NUMBER)
1542 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001543 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 retval = 0;
1545 else
1546 {
1547 /* If the result is a string, check if there is a non-digit before
1548 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001549 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 if (!VIM_ISDIGIT(*s) && *s != '-')
1551 *cp = *s++;
1552 retval = atol((char *)s);
1553 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001554 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 }
1556 --emsg_off;
1557 --sandbox;
1558
1559 return retval;
1560}
1561#endif
1562
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001564 * ":let" list all variable values
1565 * ":let var1 var2" list variable values
1566 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001567 * ":let var += expr" assignment command.
1568 * ":let var -= expr" assignment command.
1569 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001570 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 */
1572 void
1573ex_let(eap)
1574 exarg_T *eap;
1575{
1576 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001577 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001578 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001580 int var_count = 0;
1581 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001582 char_u op[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001584 expr = skip_var_list(arg, &var_count, &semicolon);
1585 if (expr == NULL)
1586 return;
1587 expr = vim_strchr(expr, '=');
1588 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001590 /*
1591 * ":let" without "=": list variables
1592 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001593 if (*arg == '[')
1594 EMSG(_(e_invarg));
1595 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001596 /* ":let var1 var2" */
1597 arg = list_arg_vars(eap, arg);
1598 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001599 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001600 /* ":let" */
Bram Moolenaara7043832005-01-21 11:56:39 +00001601 list_glob_vars();
1602 list_buf_vars();
1603 list_win_vars();
1604 list_vim_vars();
1605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 eap->nextcmd = check_nextcmd(arg);
1607 }
1608 else
1609 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001610 op[0] = '=';
1611 op[1] = NUL;
1612 if (expr > arg)
1613 {
1614 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1615 op[0] = expr[-1]; /* +=, -= or .= */
1616 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001617 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001618
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 if (eap->skip)
1620 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001621 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 if (eap->skip)
1623 {
1624 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001625 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 --emsg_skip;
1627 }
1628 else if (i != FAIL)
1629 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001630 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001631 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001632 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 }
1634 }
1635}
1636
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001637/*
1638 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1639 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001640 * When "nextchars" is not NULL it points to a string with characters that
1641 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1642 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001643 * Returns OK or FAIL;
1644 */
1645 static int
1646ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1647 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001648 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001649 int copy; /* copy values from "tv", don't move */
1650 int semicolon; /* from skip_var_list() */
1651 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001652 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001653{
1654 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001655 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001656 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001657 listitem_T *item;
1658 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001659
1660 if (*arg != '[')
1661 {
1662 /*
1663 * ":let var = expr" or ":for var in list"
1664 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001665 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001666 return FAIL;
1667 return OK;
1668 }
1669
1670 /*
1671 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1672 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001673 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001674 {
1675 EMSG(_(e_listreq));
1676 return FAIL;
1677 }
1678
1679 i = list_len(l);
1680 if (semicolon == 0 && var_count < i)
1681 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001682 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001683 return FAIL;
1684 }
1685 if (var_count - semicolon > i)
1686 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001687 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001688 return FAIL;
1689 }
1690
1691 item = l->lv_first;
1692 while (*arg != ']')
1693 {
1694 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001695 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001696 item = item->li_next;
1697 if (arg == NULL)
1698 return FAIL;
1699
1700 arg = skipwhite(arg);
1701 if (*arg == ';')
1702 {
1703 /* Put the rest of the list (may be empty) in the var after ';'.
1704 * Create a new list for this. */
1705 l = list_alloc();
1706 if (l == NULL)
1707 return FAIL;
1708 while (item != NULL)
1709 {
1710 list_append_tv(l, &item->li_tv);
1711 item = item->li_next;
1712 }
1713
1714 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001715 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001716 ltv.vval.v_list = l;
1717 l->lv_refcount = 1;
1718
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001719 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1720 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001721 clear_tv(&ltv);
1722 if (arg == NULL)
1723 return FAIL;
1724 break;
1725 }
1726 else if (*arg != ',' && *arg != ']')
1727 {
1728 EMSG2(_(e_intern2), "ex_let_vars()");
1729 return FAIL;
1730 }
1731 }
1732
1733 return OK;
1734}
1735
1736/*
1737 * Skip over assignable variable "var" or list of variables "[var, var]".
1738 * Used for ":let varvar = expr" and ":for varvar in expr".
1739 * For "[var, var]" increment "*var_count" for each variable.
1740 * for "[var, var; var]" set "semicolon".
1741 * Return NULL for an error.
1742 */
1743 static char_u *
1744skip_var_list(arg, var_count, semicolon)
1745 char_u *arg;
1746 int *var_count;
1747 int *semicolon;
1748{
1749 char_u *p, *s;
1750
1751 if (*arg == '[')
1752 {
1753 /* "[var, var]": find the matching ']'. */
1754 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001755 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001756 {
1757 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1758 s = skip_var_one(p);
1759 if (s == p)
1760 {
1761 EMSG2(_(e_invarg2), p);
1762 return NULL;
1763 }
1764 ++*var_count;
1765
1766 p = skipwhite(s);
1767 if (*p == ']')
1768 break;
1769 else if (*p == ';')
1770 {
1771 if (*semicolon == 1)
1772 {
1773 EMSG(_("Double ; in list of variables"));
1774 return NULL;
1775 }
1776 *semicolon = 1;
1777 }
1778 else if (*p != ',')
1779 {
1780 EMSG2(_(e_invarg2), p);
1781 return NULL;
1782 }
1783 }
1784 return p + 1;
1785 }
1786 else
1787 return skip_var_one(arg);
1788}
1789
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001790/*
Bram Moolenaar92124a32005-06-17 22:03:40 +00001791 * Skip one (assignable) variable name, includig @r, $VAR, &option, d.key,
1792 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001793 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001794 static char_u *
1795skip_var_one(arg)
1796 char_u *arg;
1797{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001798 if (*arg == '@' && arg[1] != NUL)
1799 return arg + 2;
1800 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1801 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001802}
1803
Bram Moolenaara7043832005-01-21 11:56:39 +00001804/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001805 * List variables for hashtab "ht" with prefix "prefix".
1806 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001807 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001808 static void
Bram Moolenaar33570922005-01-25 22:26:29 +00001809list_hashtable_vars(ht, prefix, empty)
1810 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001811 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001812 int empty;
Bram Moolenaara7043832005-01-21 11:56:39 +00001813{
Bram Moolenaar33570922005-01-25 22:26:29 +00001814 hashitem_T *hi;
1815 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001816 int todo;
1817
1818 todo = ht->ht_used;
1819 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1820 {
1821 if (!HASHITEM_EMPTY(hi))
1822 {
1823 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001824 di = HI2DI(hi);
1825 if (empty || di->di_tv.v_type != VAR_STRING
1826 || di->di_tv.vval.v_string != NULL)
1827 list_one_var(di, prefix);
Bram Moolenaara7043832005-01-21 11:56:39 +00001828 }
1829 }
1830}
1831
1832/*
1833 * List global variables.
1834 */
1835 static void
1836list_glob_vars()
1837{
Bram Moolenaar33570922005-01-25 22:26:29 +00001838 list_hashtable_vars(&globvarht, (char_u *)"", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001839}
1840
1841/*
1842 * List buffer variables.
1843 */
1844 static void
1845list_buf_vars()
1846{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001847 char_u numbuf[NUMBUFLEN];
1848
Bram Moolenaar33570922005-01-25 22:26:29 +00001849 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001850
1851 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
1852 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER, numbuf);
Bram Moolenaara7043832005-01-21 11:56:39 +00001853}
1854
1855/*
1856 * List window variables.
1857 */
1858 static void
1859list_win_vars()
1860{
Bram Moolenaar33570922005-01-25 22:26:29 +00001861 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001862}
1863
1864/*
1865 * List Vim variables.
1866 */
1867 static void
1868list_vim_vars()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001869{
Bram Moolenaar33570922005-01-25 22:26:29 +00001870 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001871}
1872
1873/*
1874 * List variables in "arg".
1875 */
1876 static char_u *
1877list_arg_vars(eap, arg)
1878 exarg_T *eap;
1879 char_u *arg;
1880{
1881 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001882 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001883 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001884 char_u *name_start;
1885 char_u *arg_subsc;
1886 char_u *tofree;
1887 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001888
1889 while (!ends_excmd(*arg) && !got_int)
1890 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001891 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001892 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001893 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001894 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
1895 {
1896 emsg_severe = TRUE;
1897 EMSG(_(e_trailing));
1898 break;
1899 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001900 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001901 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001902 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001903 /* get_name_len() takes care of expanding curly braces */
1904 name_start = name = arg;
1905 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1906 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001907 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001908 /* This is mainly to keep test 49 working: when expanding
1909 * curly braces fails overrule the exception error message. */
1910 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001911 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001912 emsg_severe = TRUE;
1913 EMSG2(_(e_invarg2), arg);
1914 break;
1915 }
1916 error = TRUE;
1917 }
1918 else
1919 {
1920 if (tofree != NULL)
1921 name = tofree;
1922 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001923 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001924 else
1925 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001926 /* handle d.key, l[idx], f(expr) */
1927 arg_subsc = arg;
1928 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001929 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001930 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001931 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001932 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001933 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001934 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001935 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001936 case 'g': list_glob_vars(); break;
1937 case 'b': list_buf_vars(); break;
1938 case 'w': list_win_vars(); break;
1939 case 'v': list_vim_vars(); break;
1940 default:
1941 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001942 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001943 }
1944 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001945 {
1946 char_u numbuf[NUMBUFLEN];
1947 char_u *tf;
1948 int c;
1949 char_u *s;
1950
1951 s = echo_string(&tv, &tf, numbuf);
1952 c = *arg;
1953 *arg = NUL;
1954 list_one_var_a((char_u *)"",
1955 arg == arg_subsc ? name : name_start,
1956 tv.v_type, s == NULL ? (char_u *)"" : s);
1957 *arg = c;
1958 vim_free(tf);
1959 }
1960 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001961 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001962 }
1963 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001964
1965 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001966 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001967
1968 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001969 }
1970
1971 return arg;
1972}
1973
1974/*
1975 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1976 * Returns a pointer to the char just after the var name.
1977 * Returns NULL if there is an error.
1978 */
1979 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001980ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001981 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00001982 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001983 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001984 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001985 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001986{
1987 int c1;
1988 char_u *name;
1989 char_u *p;
1990 char_u *arg_end = NULL;
1991 int len;
1992 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001993 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001994
1995 /*
1996 * ":let $VAR = expr": Set environment variable.
1997 */
1998 if (*arg == '$')
1999 {
2000 /* Find the end of the name. */
2001 ++arg;
2002 name = arg;
2003 len = get_env_len(&arg);
2004 if (len == 0)
2005 EMSG2(_(e_invarg2), name - 1);
2006 else
2007 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002008 if (op != NULL && (*op == '+' || *op == '-'))
2009 EMSG2(_(e_letwrong), op);
2010 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002011 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002012 EMSG(_(e_letunexp));
2013 else
2014 {
2015 c1 = name[len];
2016 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002017 p = get_tv_string_chk(tv);
2018 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002019 {
2020 int mustfree = FALSE;
2021 char_u *s = vim_getenv(name, &mustfree);
2022
2023 if (s != NULL)
2024 {
2025 p = tofree = concat_str(s, p);
2026 if (mustfree)
2027 vim_free(s);
2028 }
2029 }
2030 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002031 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002032 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002033 if (STRICMP(name, "HOME") == 0)
2034 init_homedir();
2035 else if (didset_vim && STRICMP(name, "VIM") == 0)
2036 didset_vim = FALSE;
2037 else if (didset_vimruntime
2038 && STRICMP(name, "VIMRUNTIME") == 0)
2039 didset_vimruntime = FALSE;
2040 arg_end = arg;
2041 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002042 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002043 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002044 }
2045 }
2046 }
2047
2048 /*
2049 * ":let &option = expr": Set option value.
2050 * ":let &l:option = expr": Set local option value.
2051 * ":let &g:option = expr": Set global option value.
2052 */
2053 else if (*arg == '&')
2054 {
2055 /* Find the end of the name. */
2056 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002057 if (p == NULL || (endchars != NULL
2058 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002059 EMSG(_(e_letunexp));
2060 else
2061 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002062 long n;
2063 int opt_type;
2064 long numval;
2065 char_u *stringval = NULL;
2066 char_u *s;
2067
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002068 c1 = *p;
2069 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002070
2071 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002072 s = get_tv_string_chk(tv); /* != NULL if number or string */
2073 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002074 {
2075 opt_type = get_option_value(arg, &numval,
2076 &stringval, opt_flags);
2077 if ((opt_type == 1 && *op == '.')
2078 || (opt_type == 0 && *op != '.'))
2079 EMSG2(_(e_letwrong), op);
2080 else
2081 {
2082 if (opt_type == 1) /* number */
2083 {
2084 if (*op == '+')
2085 n = numval + n;
2086 else
2087 n = numval - n;
2088 }
2089 else if (opt_type == 0 && stringval != NULL) /* string */
2090 {
2091 s = concat_str(stringval, s);
2092 vim_free(stringval);
2093 stringval = s;
2094 }
2095 }
2096 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002097 if (s != NULL)
2098 {
2099 set_option_value(arg, n, s, opt_flags);
2100 arg_end = p;
2101 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002102 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002103 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002104 }
2105 }
2106
2107 /*
2108 * ":let @r = expr": Set register contents.
2109 */
2110 else if (*arg == '@')
2111 {
2112 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002113 if (op != NULL && (*op == '+' || *op == '-'))
2114 EMSG2(_(e_letwrong), op);
2115 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002116 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002117 EMSG(_(e_letunexp));
2118 else
2119 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002120 char_u *tofree = NULL;
2121 char_u *s;
2122
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002123 p = get_tv_string_chk(tv);
2124 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002125 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002126 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002127 if (s != NULL)
2128 {
2129 p = tofree = concat_str(s, p);
2130 vim_free(s);
2131 }
2132 }
2133 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002134 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002135 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002136 arg_end = arg + 1;
2137 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002138 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002139 }
2140 }
2141
2142 /*
2143 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002144 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002145 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002146 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002147 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002148 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002149
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002150 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002151 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002152 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002153 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2154 EMSG(_(e_letunexp));
2155 else
2156 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002157 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002158 arg_end = p;
2159 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002160 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002161 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002162 }
2163
2164 else
2165 EMSG2(_(e_invarg2), arg);
2166
2167 return arg_end;
2168}
2169
2170/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002171 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2172 */
2173 static int
2174check_changedtick(arg)
2175 char_u *arg;
2176{
2177 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2178 {
2179 EMSG2(_(e_readonlyvar), arg);
2180 return TRUE;
2181 }
2182 return FALSE;
2183}
2184
2185/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002186 * Get an lval: variable, Dict item or List item that can be assigned a value
2187 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2188 * "name.key", "name.key[expr]" etc.
2189 * Indexing only works if "name" is an existing List or Dictionary.
2190 * "name" points to the start of the name.
2191 * If "rettv" is not NULL it points to the value to be assigned.
2192 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2193 * wrong; must end in space or cmd separator.
2194 *
2195 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002196 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002197 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002198 */
2199 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002200get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002201 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002202 typval_T *rettv;
2203 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002204 int unlet;
2205 int skip;
2206 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002207 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002208{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002209 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002210 char_u *expr_start, *expr_end;
2211 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002212 dictitem_T *v;
2213 typval_T var1;
2214 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002215 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002216 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002217 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002218 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002219 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002220
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002221 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002222 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002223
2224 if (skip)
2225 {
2226 /* When skipping just find the end of the name. */
2227 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002228 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002229 }
2230
2231 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002232 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002233 if (expr_start != NULL)
2234 {
2235 /* Don't expand the name when we already know there is an error. */
2236 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2237 && *p != '[' && *p != '.')
2238 {
2239 EMSG(_(e_trailing));
2240 return NULL;
2241 }
2242
2243 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2244 if (lp->ll_exp_name == NULL)
2245 {
2246 /* Report an invalid expression in braces, unless the
2247 * expression evaluation has been cancelled due to an
2248 * aborting error, an interrupt, or an exception. */
2249 if (!aborting() && !quiet)
2250 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002251 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002252 EMSG2(_(e_invarg2), name);
2253 return NULL;
2254 }
2255 }
2256 lp->ll_name = lp->ll_exp_name;
2257 }
2258 else
2259 lp->ll_name = name;
2260
2261 /* Without [idx] or .key we are done. */
2262 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2263 return p;
2264
2265 cc = *p;
2266 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002267 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002268 if (v == NULL && !quiet)
2269 EMSG2(_(e_undefvar), lp->ll_name);
2270 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002271 if (v == NULL)
2272 return NULL;
2273
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002274 /*
2275 * Loop until no more [idx] or .key is following.
2276 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002277 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002278 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002279 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002280 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2281 && !(lp->ll_tv->v_type == VAR_DICT
2282 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002283 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002284 if (!quiet)
2285 EMSG(_("E689: Can only index a List or Dictionary"));
2286 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002287 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002288 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002289 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002290 if (!quiet)
2291 EMSG(_("E708: [:] must come last"));
2292 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002293 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002294
Bram Moolenaar8c711452005-01-14 21:53:12 +00002295 len = -1;
2296 if (*p == '.')
2297 {
2298 key = p + 1;
2299 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2300 ;
2301 if (len == 0)
2302 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002303 if (!quiet)
2304 EMSG(_(e_emptykey));
2305 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002306 }
2307 p = key + len;
2308 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002309 else
2310 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002311 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002312 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002313 if (*p == ':')
2314 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002315 else
2316 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002317 empty1 = FALSE;
2318 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002319 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002320 if (get_tv_string_chk(&var1) == NULL)
2321 {
2322 /* not a number or string */
2323 clear_tv(&var1);
2324 return NULL;
2325 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002326 }
2327
2328 /* Optionally get the second index [ :expr]. */
2329 if (*p == ':')
2330 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002331 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002332 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002333 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002334 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002335 if (!empty1)
2336 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002337 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002338 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002339 if (rettv != NULL && (rettv->v_type != VAR_LIST
2340 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002341 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002342 if (!quiet)
2343 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002344 if (!empty1)
2345 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002346 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002347 }
2348 p = skipwhite(p + 1);
2349 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002350 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002351 else
2352 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002353 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002354 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2355 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002356 if (!empty1)
2357 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002358 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002359 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002360 if (get_tv_string_chk(&var2) == NULL)
2361 {
2362 /* not a number or string */
2363 if (!empty1)
2364 clear_tv(&var1);
2365 clear_tv(&var2);
2366 return NULL;
2367 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002368 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002369 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002370 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002371 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002372 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002373
Bram Moolenaar8c711452005-01-14 21:53:12 +00002374 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002375 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002376 if (!quiet)
2377 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002378 if (!empty1)
2379 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002380 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002381 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002382 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002383 }
2384
2385 /* Skip to past ']'. */
2386 ++p;
2387 }
2388
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002389 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002390 {
2391 if (len == -1)
2392 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002393 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002394 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002395 if (*key == NUL)
2396 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002397 if (!quiet)
2398 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002399 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002400 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002401 }
2402 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002403 lp->ll_list = NULL;
2404 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002405 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002406 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002407 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002408 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002409 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002410 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002411 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002412 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002413 if (len == -1)
2414 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002415 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002416 }
2417 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002418 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002419 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002420 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002421 if (len == -1)
2422 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002423 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002424 p = NULL;
2425 break;
2426 }
2427 if (len == -1)
2428 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002429 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002430 }
2431 else
2432 {
2433 /*
2434 * Get the number and item for the only or first index of the List.
2435 */
2436 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002437 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002438 else
2439 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002440 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002441 clear_tv(&var1);
2442 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002443 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002444 lp->ll_list = lp->ll_tv->vval.v_list;
2445 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2446 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002447 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002448 if (!quiet)
2449 EMSGN(_(e_listidx), lp->ll_n1);
2450 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002451 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002452 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002453 }
2454
2455 /*
2456 * May need to find the item or absolute index for the second
2457 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002458 * When no index given: "lp->ll_empty2" is TRUE.
2459 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002460 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002461 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002462 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002463 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002464 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002465 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002466 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002467 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002468 if (ni == NULL)
2469 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002470 if (!quiet)
2471 EMSGN(_(e_listidx), lp->ll_n2);
2472 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002473 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002474 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002475 }
2476
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002477 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2478 if (lp->ll_n1 < 0)
2479 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2480 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002481 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002482 if (!quiet)
2483 EMSGN(_(e_listidx), lp->ll_n2);
2484 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002485 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002486 }
2487
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002488 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002489 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002490 }
2491
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002492 return p;
2493}
2494
2495/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002496 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002497 */
2498 static void
2499clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002500 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002501{
2502 vim_free(lp->ll_exp_name);
2503 vim_free(lp->ll_newkey);
2504}
2505
2506/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002507 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002508 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002509 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002510 */
2511 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002512set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002513 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002514 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002515 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002516 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002517 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002518{
2519 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002520 listitem_T *ni;
2521 listitem_T *ri;
2522 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002523
2524 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002525 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002526 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002527 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002528 cc = *endp;
2529 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002530 if (op != NULL && *op != '=')
2531 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002532 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002533
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002534 /* handle +=, -= and .= */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002535 if (get_var_tv(lp->ll_name, STRLEN(lp->ll_name),
2536 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002537 {
2538 if (tv_op(&tv, rettv, op) == OK)
2539 set_var(lp->ll_name, &tv, FALSE);
2540 clear_tv(&tv);
2541 }
2542 }
2543 else
2544 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002545 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002546 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002547 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002548 else if (tv_check_lock(lp->ll_newkey == NULL
2549 ? lp->ll_tv->v_lock
2550 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2551 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002552 else if (lp->ll_range)
2553 {
2554 /*
2555 * Assign the List values to the list items.
2556 */
2557 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002558 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002559 if (op != NULL && *op != '=')
2560 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2561 else
2562 {
2563 clear_tv(&lp->ll_li->li_tv);
2564 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2565 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002566 ri = ri->li_next;
2567 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2568 break;
2569 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002570 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002571 /* Need to add an empty item. */
2572 ni = listitem_alloc();
2573 if (ni == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002574 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002575 ri = NULL;
2576 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002577 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002578 ni->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002579 ni->li_tv.v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002580 ni->li_tv.vval.v_number = 0;
2581 list_append(lp->ll_list, ni);
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002582 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002583 lp->ll_li = lp->ll_li->li_next;
2584 ++lp->ll_n1;
2585 }
2586 if (ri != NULL)
2587 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002588 else if (lp->ll_empty2
2589 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002590 : lp->ll_n1 != lp->ll_n2)
2591 EMSG(_("E711: List value has not enough items"));
2592 }
2593 else
2594 {
2595 /*
2596 * Assign to a List or Dictionary item.
2597 */
2598 if (lp->ll_newkey != NULL)
2599 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002600 if (op != NULL && *op != '=')
2601 {
2602 EMSG2(_(e_letwrong), op);
2603 return;
2604 }
2605
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002606 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002607 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002608 if (di == NULL)
2609 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002610 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2611 {
2612 vim_free(di);
2613 return;
2614 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002615 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002616 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002617 else if (op != NULL && *op != '=')
2618 {
2619 tv_op(lp->ll_tv, rettv, op);
2620 return;
2621 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002622 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002623 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002624
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002625 /*
2626 * Assign the value to the variable or list item.
2627 */
2628 if (copy)
2629 copy_tv(rettv, lp->ll_tv);
2630 else
2631 {
2632 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002633 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002634 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002635 }
2636 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002637}
2638
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002639/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002640 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2641 * Returns OK or FAIL.
2642 */
2643 static int
2644tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002645 typval_T *tv1;
2646 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002647 char_u *op;
2648{
2649 long n;
2650 char_u numbuf[NUMBUFLEN];
2651 char_u *s;
2652
2653 /* Can't do anything with a Funcref or a Dict on the right. */
2654 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2655 {
2656 switch (tv1->v_type)
2657 {
2658 case VAR_DICT:
2659 case VAR_FUNC:
2660 break;
2661
2662 case VAR_LIST:
2663 if (*op != '+' || tv2->v_type != VAR_LIST)
2664 break;
2665 /* List += List */
2666 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2667 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2668 return OK;
2669
2670 case VAR_NUMBER:
2671 case VAR_STRING:
2672 if (tv2->v_type == VAR_LIST)
2673 break;
2674 if (*op == '+' || *op == '-')
2675 {
2676 /* nr += nr or nr -= nr*/
2677 n = get_tv_number(tv1);
2678 if (*op == '+')
2679 n += get_tv_number(tv2);
2680 else
2681 n -= get_tv_number(tv2);
2682 clear_tv(tv1);
2683 tv1->v_type = VAR_NUMBER;
2684 tv1->vval.v_number = n;
2685 }
2686 else
2687 {
2688 /* str .= str */
2689 s = get_tv_string(tv1);
2690 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2691 clear_tv(tv1);
2692 tv1->v_type = VAR_STRING;
2693 tv1->vval.v_string = s;
2694 }
2695 return OK;
2696 }
2697 }
2698
2699 EMSG2(_(e_letwrong), op);
2700 return FAIL;
2701}
2702
2703/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002704 * Add a watcher to a list.
2705 */
2706 static void
2707list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002708 list_T *l;
2709 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002710{
2711 lw->lw_next = l->lv_watch;
2712 l->lv_watch = lw;
2713}
2714
2715/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002716 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002717 * No warning when it isn't found...
2718 */
2719 static void
2720list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002721 list_T *l;
2722 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002723{
Bram Moolenaar33570922005-01-25 22:26:29 +00002724 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002725
2726 lwp = &l->lv_watch;
2727 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2728 {
2729 if (lw == lwrem)
2730 {
2731 *lwp = lw->lw_next;
2732 break;
2733 }
2734 lwp = &lw->lw_next;
2735 }
2736}
2737
2738/*
2739 * Just before removing an item from a list: advance watchers to the next
2740 * item.
2741 */
2742 static void
2743list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002744 list_T *l;
2745 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002746{
Bram Moolenaar33570922005-01-25 22:26:29 +00002747 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002748
2749 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2750 if (lw->lw_item == item)
2751 lw->lw_item = item->li_next;
2752}
2753
2754/*
2755 * Evaluate the expression used in a ":for var in expr" command.
2756 * "arg" points to "var".
2757 * Set "*errp" to TRUE for an error, FALSE otherwise;
2758 * Return a pointer that holds the info. Null when there is an error.
2759 */
2760 void *
2761eval_for_line(arg, errp, nextcmdp, skip)
2762 char_u *arg;
2763 int *errp;
2764 char_u **nextcmdp;
2765 int skip;
2766{
Bram Moolenaar33570922005-01-25 22:26:29 +00002767 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002768 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002769 typval_T tv;
2770 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002771
2772 *errp = TRUE; /* default: there is an error */
2773
Bram Moolenaar33570922005-01-25 22:26:29 +00002774 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002775 if (fi == NULL)
2776 return NULL;
2777
2778 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2779 if (expr == NULL)
2780 return fi;
2781
2782 expr = skipwhite(expr);
2783 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2784 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002785 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002786 return fi;
2787 }
2788
2789 if (skip)
2790 ++emsg_skip;
2791 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2792 {
2793 *errp = FALSE;
2794 if (!skip)
2795 {
2796 l = tv.vval.v_list;
2797 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002798 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002799 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002800 clear_tv(&tv);
2801 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002802 else
2803 {
2804 fi->fi_list = l;
2805 list_add_watch(l, &fi->fi_lw);
2806 fi->fi_lw.lw_item = l->lv_first;
2807 }
2808 }
2809 }
2810 if (skip)
2811 --emsg_skip;
2812
2813 return fi;
2814}
2815
2816/*
2817 * Use the first item in a ":for" list. Advance to the next.
2818 * Assign the values to the variable (list). "arg" points to the first one.
2819 * Return TRUE when a valid item was found, FALSE when at end of list or
2820 * something wrong.
2821 */
2822 int
2823next_for_item(fi_void, arg)
2824 void *fi_void;
2825 char_u *arg;
2826{
Bram Moolenaar33570922005-01-25 22:26:29 +00002827 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002828 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002829 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002830
2831 item = fi->fi_lw.lw_item;
2832 if (item == NULL)
2833 result = FALSE;
2834 else
2835 {
2836 fi->fi_lw.lw_item = item->li_next;
2837 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2838 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2839 }
2840 return result;
2841}
2842
2843/*
2844 * Free the structure used to store info used by ":for".
2845 */
2846 void
2847free_for_info(fi_void)
2848 void *fi_void;
2849{
Bram Moolenaar33570922005-01-25 22:26:29 +00002850 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002851
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002852 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002853 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002854 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002855 list_unref(fi->fi_list);
2856 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002857 vim_free(fi);
2858}
2859
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2861
2862 void
2863set_context_for_expression(xp, arg, cmdidx)
2864 expand_T *xp;
2865 char_u *arg;
2866 cmdidx_T cmdidx;
2867{
2868 int got_eq = FALSE;
2869 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002870 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002872 if (cmdidx == CMD_let)
2873 {
2874 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002875 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002876 {
2877 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002878 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002879 {
2880 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00002881 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002882 if (vim_iswhite(*p))
2883 break;
2884 }
2885 return;
2886 }
2887 }
2888 else
2889 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2890 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 while ((xp->xp_pattern = vim_strpbrk(arg,
2892 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2893 {
2894 c = *xp->xp_pattern;
2895 if (c == '&')
2896 {
2897 c = xp->xp_pattern[1];
2898 if (c == '&')
2899 {
2900 ++xp->xp_pattern;
2901 xp->xp_context = cmdidx != CMD_let || got_eq
2902 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2903 }
2904 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002905 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002907 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2908 xp->xp_pattern += 2;
2909
2910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 }
2912 else if (c == '$')
2913 {
2914 /* environment variable */
2915 xp->xp_context = EXPAND_ENV_VARS;
2916 }
2917 else if (c == '=')
2918 {
2919 got_eq = TRUE;
2920 xp->xp_context = EXPAND_EXPRESSION;
2921 }
2922 else if (c == '<'
2923 && xp->xp_context == EXPAND_FUNCTIONS
2924 && vim_strchr(xp->xp_pattern, '(') == NULL)
2925 {
2926 /* Function name can start with "<SNR>" */
2927 break;
2928 }
2929 else if (cmdidx != CMD_let || got_eq)
2930 {
2931 if (c == '"') /* string */
2932 {
2933 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2934 if (c == '\\' && xp->xp_pattern[1] != NUL)
2935 ++xp->xp_pattern;
2936 xp->xp_context = EXPAND_NOTHING;
2937 }
2938 else if (c == '\'') /* literal string */
2939 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002940 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2942 /* skip */ ;
2943 xp->xp_context = EXPAND_NOTHING;
2944 }
2945 else if (c == '|')
2946 {
2947 if (xp->xp_pattern[1] == '|')
2948 {
2949 ++xp->xp_pattern;
2950 xp->xp_context = EXPAND_EXPRESSION;
2951 }
2952 else
2953 xp->xp_context = EXPAND_COMMANDS;
2954 }
2955 else
2956 xp->xp_context = EXPAND_EXPRESSION;
2957 }
2958 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002959 /* Doesn't look like something valid, expand as an expression
2960 * anyway. */
2961 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 arg = xp->xp_pattern;
2963 if (*arg != NUL)
2964 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2965 /* skip */ ;
2966 }
2967 xp->xp_pattern = arg;
2968}
2969
2970#endif /* FEAT_CMDL_COMPL */
2971
2972/*
2973 * ":1,25call func(arg1, arg2)" function call.
2974 */
2975 void
2976ex_call(eap)
2977 exarg_T *eap;
2978{
2979 char_u *arg = eap->arg;
2980 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002982 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002984 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 linenr_T lnum;
2986 int doesrange;
2987 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002988 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002990 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
2991 vim_free(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002992 if (tofree == NULL)
2993 return;
2994
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002995 /* Increase refcount on dictionary, it could get deleted when evaluating
2996 * the arguments. */
2997 if (fudi.fd_dict != NULL)
2998 ++fudi.fd_dict->dv_refcount;
2999
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003000 /* If it is the name of a variable of type VAR_FUNC use its contents. */
3001 len = STRLEN(tofree);
3002 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003
Bram Moolenaar532c7802005-01-27 14:44:31 +00003004 /* Skip white space to allow ":call func ()". Not good, but required for
3005 * backward compatibility. */
3006 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003007 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008
3009 if (*startarg != '(')
3010 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003011 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 goto end;
3013 }
3014
3015 /*
3016 * When skipping, evaluate the function once, to find the end of the
3017 * arguments.
3018 * When the function takes a range, this is discovered after the first
3019 * call, and the loop is broken.
3020 */
3021 if (eap->skip)
3022 {
3023 ++emsg_skip;
3024 lnum = eap->line2; /* do it once, also with an invalid range */
3025 }
3026 else
3027 lnum = eap->line1;
3028 for ( ; lnum <= eap->line2; ++lnum)
3029 {
3030 if (!eap->skip && eap->addr_count > 0)
3031 {
3032 curwin->w_cursor.lnum = lnum;
3033 curwin->w_cursor.col = 0;
3034 }
3035 arg = startarg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003036 if (get_func_tv(name, STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003037 eap->line1, eap->line2, &doesrange,
3038 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 {
3040 failed = TRUE;
3041 break;
3042 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003043 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 if (doesrange || eap->skip)
3045 break;
3046 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003047 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003048 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003049 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 if (aborting())
3051 break;
3052 }
3053 if (eap->skip)
3054 --emsg_skip;
3055
3056 if (!failed)
3057 {
3058 /* Check for trailing illegal characters and a following command. */
3059 if (!ends_excmd(*arg))
3060 {
3061 emsg_severe = TRUE;
3062 EMSG(_(e_trailing));
3063 }
3064 else
3065 eap->nextcmd = check_nextcmd(arg);
3066 }
3067
3068end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003069 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003070 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071}
3072
3073/*
3074 * ":unlet[!] var1 ... " command.
3075 */
3076 void
3077ex_unlet(eap)
3078 exarg_T *eap;
3079{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003080 ex_unletlock(eap, eap->arg, 0);
3081}
3082
3083/*
3084 * ":lockvar" and ":unlockvar" commands
3085 */
3086 void
3087ex_lockvar(eap)
3088 exarg_T *eap;
3089{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003091 int deep = 2;
3092
3093 if (eap->forceit)
3094 deep = -1;
3095 else if (vim_isdigit(*arg))
3096 {
3097 deep = getdigits(&arg);
3098 arg = skipwhite(arg);
3099 }
3100
3101 ex_unletlock(eap, arg, deep);
3102}
3103
3104/*
3105 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3106 */
3107 static void
3108ex_unletlock(eap, argstart, deep)
3109 exarg_T *eap;
3110 char_u *argstart;
3111 int deep;
3112{
3113 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003116 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117
3118 do
3119 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003120 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003121 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3122 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003123 if (lv.ll_name == NULL)
3124 error = TRUE; /* error but continue parsing */
3125 if (name_end == NULL || (!vim_iswhite(*name_end)
3126 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003128 if (name_end != NULL)
3129 {
3130 emsg_severe = TRUE;
3131 EMSG(_(e_trailing));
3132 }
3133 if (!(eap->skip || error))
3134 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 break;
3136 }
3137
3138 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003139 {
3140 if (eap->cmdidx == CMD_unlet)
3141 {
3142 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3143 error = TRUE;
3144 }
3145 else
3146 {
3147 if (do_lock_var(&lv, name_end, deep,
3148 eap->cmdidx == CMD_lockvar) == FAIL)
3149 error = TRUE;
3150 }
3151 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003153 if (!eap->skip)
3154 clear_lval(&lv);
3155
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 arg = skipwhite(name_end);
3157 } while (!ends_excmd(*arg));
3158
3159 eap->nextcmd = check_nextcmd(arg);
3160}
3161
Bram Moolenaar8c711452005-01-14 21:53:12 +00003162 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003163do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003164 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003165 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003166 int forceit;
3167{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003168 int ret = OK;
3169 int cc;
3170
3171 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003172 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003173 cc = *name_end;
3174 *name_end = NUL;
3175
3176 /* Normal name or expanded name. */
3177 if (check_changedtick(lp->ll_name))
3178 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003179 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003180 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003181 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003182 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003183 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3184 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003185 else if (lp->ll_range)
3186 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003187 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003188
3189 /* Delete a range of List items. */
3190 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3191 {
3192 li = lp->ll_li->li_next;
3193 listitem_remove(lp->ll_list, lp->ll_li);
3194 lp->ll_li = li;
3195 ++lp->ll_n1;
3196 }
3197 }
3198 else
3199 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003200 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003201 /* unlet a List item. */
3202 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003203 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003204 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003205 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003206 }
3207
3208 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003209}
3210
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211/*
3212 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003213 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 */
3215 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003216do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003218 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219{
Bram Moolenaar33570922005-01-25 22:26:29 +00003220 hashtab_T *ht;
3221 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003222 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223
Bram Moolenaar33570922005-01-25 22:26:29 +00003224 ht = find_var_ht(name, &varname);
3225 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003227 hi = hash_find(ht, varname);
3228 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003229 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003230 if (var_check_ro(HI2DI(hi)->di_flags, name))
3231 return FAIL;
3232 delete_var(ht, hi);
3233 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003234 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003236 if (forceit)
3237 return OK;
3238 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 return FAIL;
3240}
3241
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003242/*
3243 * Lock or unlock variable indicated by "lp".
3244 * "deep" is the levels to go (-1 for unlimited);
3245 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3246 */
3247 static int
3248do_lock_var(lp, name_end, deep, lock)
3249 lval_T *lp;
3250 char_u *name_end;
3251 int deep;
3252 int lock;
3253{
3254 int ret = OK;
3255 int cc;
3256 dictitem_T *di;
3257
3258 if (deep == 0) /* nothing to do */
3259 return OK;
3260
3261 if (lp->ll_tv == NULL)
3262 {
3263 cc = *name_end;
3264 *name_end = NUL;
3265
3266 /* Normal name or expanded name. */
3267 if (check_changedtick(lp->ll_name))
3268 ret = FAIL;
3269 else
3270 {
3271 di = find_var(lp->ll_name, NULL);
3272 if (di == NULL)
3273 ret = FAIL;
3274 else
3275 {
3276 if (lock)
3277 di->di_flags |= DI_FLAGS_LOCK;
3278 else
3279 di->di_flags &= ~DI_FLAGS_LOCK;
3280 item_lock(&di->di_tv, deep, lock);
3281 }
3282 }
3283 *name_end = cc;
3284 }
3285 else if (lp->ll_range)
3286 {
3287 listitem_T *li = lp->ll_li;
3288
3289 /* (un)lock a range of List items. */
3290 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3291 {
3292 item_lock(&li->li_tv, deep, lock);
3293 li = li->li_next;
3294 ++lp->ll_n1;
3295 }
3296 }
3297 else if (lp->ll_list != NULL)
3298 /* (un)lock a List item. */
3299 item_lock(&lp->ll_li->li_tv, deep, lock);
3300 else
3301 /* un(lock) a Dictionary item. */
3302 item_lock(&lp->ll_di->di_tv, deep, lock);
3303
3304 return ret;
3305}
3306
3307/*
3308 * Lock or unlock an item. "deep" is nr of levels to go.
3309 */
3310 static void
3311item_lock(tv, deep, lock)
3312 typval_T *tv;
3313 int deep;
3314 int lock;
3315{
3316 static int recurse = 0;
3317 list_T *l;
3318 listitem_T *li;
3319 dict_T *d;
3320 hashitem_T *hi;
3321 int todo;
3322
3323 if (recurse >= DICT_MAXNEST)
3324 {
3325 EMSG(_("E743: variable nested too deep for (un)lock"));
3326 return;
3327 }
3328 if (deep == 0)
3329 return;
3330 ++recurse;
3331
3332 /* lock/unlock the item itself */
3333 if (lock)
3334 tv->v_lock |= VAR_LOCKED;
3335 else
3336 tv->v_lock &= ~VAR_LOCKED;
3337
3338 switch (tv->v_type)
3339 {
3340 case VAR_LIST:
3341 if ((l = tv->vval.v_list) != NULL)
3342 {
3343 if (lock)
3344 l->lv_lock |= VAR_LOCKED;
3345 else
3346 l->lv_lock &= ~VAR_LOCKED;
3347 if (deep < 0 || deep > 1)
3348 /* recursive: lock/unlock the items the List contains */
3349 for (li = l->lv_first; li != NULL; li = li->li_next)
3350 item_lock(&li->li_tv, deep - 1, lock);
3351 }
3352 break;
3353 case VAR_DICT:
3354 if ((d = tv->vval.v_dict) != NULL)
3355 {
3356 if (lock)
3357 d->dv_lock |= VAR_LOCKED;
3358 else
3359 d->dv_lock &= ~VAR_LOCKED;
3360 if (deep < 0 || deep > 1)
3361 {
3362 /* recursive: lock/unlock the items the List contains */
3363 todo = d->dv_hashtab.ht_used;
3364 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3365 {
3366 if (!HASHITEM_EMPTY(hi))
3367 {
3368 --todo;
3369 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3370 }
3371 }
3372 }
3373 }
3374 }
3375 --recurse;
3376}
3377
Bram Moolenaara40058a2005-07-11 22:42:07 +00003378/*
3379 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3380 * it refers to a List or Dictionary that is locked.
3381 */
3382 static int
3383tv_islocked(tv)
3384 typval_T *tv;
3385{
3386 return (tv->v_lock & VAR_LOCKED)
3387 || (tv->v_type == VAR_LIST
3388 && tv->vval.v_list != NULL
3389 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3390 || (tv->v_type == VAR_DICT
3391 && tv->vval.v_dict != NULL
3392 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3393}
3394
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3396/*
3397 * Delete all "menutrans_" variables.
3398 */
3399 void
3400del_menutrans_vars()
3401{
Bram Moolenaar33570922005-01-25 22:26:29 +00003402 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003403 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404
Bram Moolenaar33570922005-01-25 22:26:29 +00003405 hash_lock(&globvarht);
3406 todo = globvarht.ht_used;
3407 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003408 {
3409 if (!HASHITEM_EMPTY(hi))
3410 {
3411 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003412 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3413 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003414 }
3415 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003416 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417}
3418#endif
3419
3420#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3421
3422/*
3423 * Local string buffer for the next two functions to store a variable name
3424 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3425 * get_user_var_name().
3426 */
3427
3428static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3429
3430static char_u *varnamebuf = NULL;
3431static int varnamebuflen = 0;
3432
3433/*
3434 * Function to concatenate a prefix and a variable name.
3435 */
3436 static char_u *
3437cat_prefix_varname(prefix, name)
3438 int prefix;
3439 char_u *name;
3440{
3441 int len;
3442
3443 len = (int)STRLEN(name) + 3;
3444 if (len > varnamebuflen)
3445 {
3446 vim_free(varnamebuf);
3447 len += 10; /* some additional space */
3448 varnamebuf = alloc(len);
3449 if (varnamebuf == NULL)
3450 {
3451 varnamebuflen = 0;
3452 return NULL;
3453 }
3454 varnamebuflen = len;
3455 }
3456 *varnamebuf = prefix;
3457 varnamebuf[1] = ':';
3458 STRCPY(varnamebuf + 2, name);
3459 return varnamebuf;
3460}
3461
3462/*
3463 * Function given to ExpandGeneric() to obtain the list of user defined
3464 * (global/buffer/window/built-in) variable names.
3465 */
3466/*ARGSUSED*/
3467 char_u *
3468get_user_var_name(xp, idx)
3469 expand_T *xp;
3470 int idx;
3471{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003472 static long_u gdone;
3473 static long_u bdone;
3474 static long_u wdone;
3475 static int vidx;
3476 static hashitem_T *hi;
3477 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478
3479 if (idx == 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00003480 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00003481
3482 /* Global variables */
3483 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003485 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003486 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003487 else
3488 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003489 while (HASHITEM_EMPTY(hi))
3490 ++hi;
3491 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3492 return cat_prefix_varname('g', hi->hi_key);
3493 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003495
3496 /* b: variables */
3497 ht = &curbuf->b_vars.dv_hashtab;
3498 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003500 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003501 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003502 else
3503 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003504 while (HASHITEM_EMPTY(hi))
3505 ++hi;
3506 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003508 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003510 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 return (char_u *)"b:changedtick";
3512 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003513
3514 /* w: variables */
3515 ht = &curwin->w_vars.dv_hashtab;
3516 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003518 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003519 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003520 else
3521 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003522 while (HASHITEM_EMPTY(hi))
3523 ++hi;
3524 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003526
3527 /* v: variables */
3528 if (vidx < VV_LEN)
3529 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530
3531 vim_free(varnamebuf);
3532 varnamebuf = NULL;
3533 varnamebuflen = 0;
3534 return NULL;
3535}
3536
3537#endif /* FEAT_CMDL_COMPL */
3538
3539/*
3540 * types for expressions.
3541 */
3542typedef enum
3543{
3544 TYPE_UNKNOWN = 0
3545 , TYPE_EQUAL /* == */
3546 , TYPE_NEQUAL /* != */
3547 , TYPE_GREATER /* > */
3548 , TYPE_GEQUAL /* >= */
3549 , TYPE_SMALLER /* < */
3550 , TYPE_SEQUAL /* <= */
3551 , TYPE_MATCH /* =~ */
3552 , TYPE_NOMATCH /* !~ */
3553} exptype_T;
3554
3555/*
3556 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003557 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3559 */
3560
3561/*
3562 * Handle zero level expression.
3563 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003564 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 * Return OK or FAIL.
3566 */
3567 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003568eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003570 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 char_u **nextcmd;
3572 int evaluate;
3573{
3574 int ret;
3575 char_u *p;
3576
3577 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003578 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 if (ret == FAIL || !ends_excmd(*p))
3580 {
3581 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003582 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 /*
3584 * Report the invalid expression unless the expression evaluation has
3585 * been cancelled due to an aborting error, an interrupt, or an
3586 * exception.
3587 */
3588 if (!aborting())
3589 EMSG2(_(e_invexpr2), arg);
3590 ret = FAIL;
3591 }
3592 if (nextcmd != NULL)
3593 *nextcmd = check_nextcmd(p);
3594
3595 return ret;
3596}
3597
3598/*
3599 * Handle top level expression:
3600 * expr1 ? expr0 : expr0
3601 *
3602 * "arg" must point to the first non-white of the expression.
3603 * "arg" is advanced to the next non-white after the recognized expression.
3604 *
3605 * Return OK or FAIL.
3606 */
3607 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003608eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003610 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 int evaluate;
3612{
3613 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003614 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615
3616 /*
3617 * Get the first variable.
3618 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003619 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 return FAIL;
3621
3622 if ((*arg)[0] == '?')
3623 {
3624 result = FALSE;
3625 if (evaluate)
3626 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003627 int error = FALSE;
3628
3629 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003631 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003632 if (error)
3633 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 }
3635
3636 /*
3637 * Get the second variable.
3638 */
3639 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003640 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 return FAIL;
3642
3643 /*
3644 * Check for the ":".
3645 */
3646 if ((*arg)[0] != ':')
3647 {
3648 EMSG(_("E109: Missing ':' after '?'"));
3649 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003650 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 return FAIL;
3652 }
3653
3654 /*
3655 * Get the third variable.
3656 */
3657 *arg = skipwhite(*arg + 1);
3658 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3659 {
3660 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003661 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 return FAIL;
3663 }
3664 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003665 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 }
3667
3668 return OK;
3669}
3670
3671/*
3672 * Handle first level expression:
3673 * expr2 || expr2 || expr2 logical OR
3674 *
3675 * "arg" must point to the first non-white of the expression.
3676 * "arg" is advanced to the next non-white after the recognized expression.
3677 *
3678 * Return OK or FAIL.
3679 */
3680 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003681eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003683 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 int evaluate;
3685{
Bram Moolenaar33570922005-01-25 22:26:29 +00003686 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 long result;
3688 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003689 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690
3691 /*
3692 * Get the first variable.
3693 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003694 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 return FAIL;
3696
3697 /*
3698 * Repeat until there is no following "||".
3699 */
3700 first = TRUE;
3701 result = FALSE;
3702 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3703 {
3704 if (evaluate && first)
3705 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003706 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003708 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003709 if (error)
3710 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 first = FALSE;
3712 }
3713
3714 /*
3715 * Get the second variable.
3716 */
3717 *arg = skipwhite(*arg + 2);
3718 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3719 return FAIL;
3720
3721 /*
3722 * Compute the result.
3723 */
3724 if (evaluate && !result)
3725 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003726 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003728 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003729 if (error)
3730 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 }
3732 if (evaluate)
3733 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003734 rettv->v_type = VAR_NUMBER;
3735 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 }
3737 }
3738
3739 return OK;
3740}
3741
3742/*
3743 * Handle second level expression:
3744 * expr3 && expr3 && expr3 logical AND
3745 *
3746 * "arg" must point to the first non-white of the expression.
3747 * "arg" is advanced to the next non-white after the recognized expression.
3748 *
3749 * Return OK or FAIL.
3750 */
3751 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003752eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003754 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 int evaluate;
3756{
Bram Moolenaar33570922005-01-25 22:26:29 +00003757 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 long result;
3759 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003760 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761
3762 /*
3763 * Get the first variable.
3764 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003765 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 return FAIL;
3767
3768 /*
3769 * Repeat until there is no following "&&".
3770 */
3771 first = TRUE;
3772 result = TRUE;
3773 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3774 {
3775 if (evaluate && first)
3776 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003777 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003779 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003780 if (error)
3781 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 first = FALSE;
3783 }
3784
3785 /*
3786 * Get the second variable.
3787 */
3788 *arg = skipwhite(*arg + 2);
3789 if (eval4(arg, &var2, evaluate && result) == FAIL)
3790 return FAIL;
3791
3792 /*
3793 * Compute the result.
3794 */
3795 if (evaluate && result)
3796 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003797 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003799 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003800 if (error)
3801 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 }
3803 if (evaluate)
3804 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003805 rettv->v_type = VAR_NUMBER;
3806 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 }
3808 }
3809
3810 return OK;
3811}
3812
3813/*
3814 * Handle third level expression:
3815 * var1 == var2
3816 * var1 =~ var2
3817 * var1 != var2
3818 * var1 !~ var2
3819 * var1 > var2
3820 * var1 >= var2
3821 * var1 < var2
3822 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003823 * var1 is var2
3824 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 *
3826 * "arg" must point to the first non-white of the expression.
3827 * "arg" is advanced to the next non-white after the recognized expression.
3828 *
3829 * Return OK or FAIL.
3830 */
3831 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003832eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003834 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 int evaluate;
3836{
Bram Moolenaar33570922005-01-25 22:26:29 +00003837 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 char_u *p;
3839 int i;
3840 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003841 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 int len = 2;
3843 long n1, n2;
3844 char_u *s1, *s2;
3845 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3846 regmatch_T regmatch;
3847 int ic;
3848 char_u *save_cpo;
3849
3850 /*
3851 * Get the first variable.
3852 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003853 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 return FAIL;
3855
3856 p = *arg;
3857 switch (p[0])
3858 {
3859 case '=': if (p[1] == '=')
3860 type = TYPE_EQUAL;
3861 else if (p[1] == '~')
3862 type = TYPE_MATCH;
3863 break;
3864 case '!': if (p[1] == '=')
3865 type = TYPE_NEQUAL;
3866 else if (p[1] == '~')
3867 type = TYPE_NOMATCH;
3868 break;
3869 case '>': if (p[1] != '=')
3870 {
3871 type = TYPE_GREATER;
3872 len = 1;
3873 }
3874 else
3875 type = TYPE_GEQUAL;
3876 break;
3877 case '<': if (p[1] != '=')
3878 {
3879 type = TYPE_SMALLER;
3880 len = 1;
3881 }
3882 else
3883 type = TYPE_SEQUAL;
3884 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003885 case 'i': if (p[1] == 's')
3886 {
3887 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3888 len = 5;
3889 if (!vim_isIDc(p[len]))
3890 {
3891 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3892 type_is = TRUE;
3893 }
3894 }
3895 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 }
3897
3898 /*
3899 * If there is a comparitive operator, use it.
3900 */
3901 if (type != TYPE_UNKNOWN)
3902 {
3903 /* extra question mark appended: ignore case */
3904 if (p[len] == '?')
3905 {
3906 ic = TRUE;
3907 ++len;
3908 }
3909 /* extra '#' appended: match case */
3910 else if (p[len] == '#')
3911 {
3912 ic = FALSE;
3913 ++len;
3914 }
3915 /* nothing appened: use 'ignorecase' */
3916 else
3917 ic = p_ic;
3918
3919 /*
3920 * Get the second variable.
3921 */
3922 *arg = skipwhite(p + len);
3923 if (eval5(arg, &var2, evaluate) == FAIL)
3924 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003925 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 return FAIL;
3927 }
3928
3929 if (evaluate)
3930 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003931 if (type_is && rettv->v_type != var2.v_type)
3932 {
3933 /* For "is" a different type always means FALSE, for "notis"
3934 * it means TRUE. */
3935 n1 = (type == TYPE_NEQUAL);
3936 }
3937 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
3938 {
3939 if (type_is)
3940 {
3941 n1 = (rettv->v_type == var2.v_type
3942 && rettv->vval.v_list == var2.vval.v_list);
3943 if (type == TYPE_NEQUAL)
3944 n1 = !n1;
3945 }
3946 else if (rettv->v_type != var2.v_type
3947 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3948 {
3949 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003950 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003951 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003952 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003953 clear_tv(rettv);
3954 clear_tv(&var2);
3955 return FAIL;
3956 }
3957 else
3958 {
3959 /* Compare two Lists for being equal or unequal. */
3960 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
3961 if (type == TYPE_NEQUAL)
3962 n1 = !n1;
3963 }
3964 }
3965
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003966 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
3967 {
3968 if (type_is)
3969 {
3970 n1 = (rettv->v_type == var2.v_type
3971 && rettv->vval.v_dict == var2.vval.v_dict);
3972 if (type == TYPE_NEQUAL)
3973 n1 = !n1;
3974 }
3975 else if (rettv->v_type != var2.v_type
3976 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3977 {
3978 if (rettv->v_type != var2.v_type)
3979 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
3980 else
3981 EMSG(_("E736: Invalid operation for Dictionary"));
3982 clear_tv(rettv);
3983 clear_tv(&var2);
3984 return FAIL;
3985 }
3986 else
3987 {
3988 /* Compare two Dictionaries for being equal or unequal. */
3989 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
3990 if (type == TYPE_NEQUAL)
3991 n1 = !n1;
3992 }
3993 }
3994
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003995 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
3996 {
3997 if (rettv->v_type != var2.v_type
3998 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3999 {
4000 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004001 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004002 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004003 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004004 clear_tv(rettv);
4005 clear_tv(&var2);
4006 return FAIL;
4007 }
4008 else
4009 {
4010 /* Compare two Funcrefs for being equal or unequal. */
4011 if (rettv->vval.v_string == NULL
4012 || var2.vval.v_string == NULL)
4013 n1 = FALSE;
4014 else
4015 n1 = STRCMP(rettv->vval.v_string,
4016 var2.vval.v_string) == 0;
4017 if (type == TYPE_NEQUAL)
4018 n1 = !n1;
4019 }
4020 }
4021
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 /*
4023 * If one of the two variables is a number, compare as a number.
4024 * When using "=~" or "!~", always compare as string.
4025 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004026 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4028 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004029 n1 = get_tv_number(rettv);
4030 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 switch (type)
4032 {
4033 case TYPE_EQUAL: n1 = (n1 == n2); break;
4034 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4035 case TYPE_GREATER: n1 = (n1 > n2); break;
4036 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4037 case TYPE_SMALLER: n1 = (n1 < n2); break;
4038 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4039 case TYPE_UNKNOWN:
4040 case TYPE_MATCH:
4041 case TYPE_NOMATCH: break; /* avoid gcc warning */
4042 }
4043 }
4044 else
4045 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004046 s1 = get_tv_string_buf(rettv, buf1);
4047 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4049 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4050 else
4051 i = 0;
4052 n1 = FALSE;
4053 switch (type)
4054 {
4055 case TYPE_EQUAL: n1 = (i == 0); break;
4056 case TYPE_NEQUAL: n1 = (i != 0); break;
4057 case TYPE_GREATER: n1 = (i > 0); break;
4058 case TYPE_GEQUAL: n1 = (i >= 0); break;
4059 case TYPE_SMALLER: n1 = (i < 0); break;
4060 case TYPE_SEQUAL: n1 = (i <= 0); break;
4061
4062 case TYPE_MATCH:
4063 case TYPE_NOMATCH:
4064 /* avoid 'l' flag in 'cpoptions' */
4065 save_cpo = p_cpo;
4066 p_cpo = (char_u *)"";
4067 regmatch.regprog = vim_regcomp(s2,
4068 RE_MAGIC + RE_STRING);
4069 regmatch.rm_ic = ic;
4070 if (regmatch.regprog != NULL)
4071 {
4072 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4073 vim_free(regmatch.regprog);
4074 if (type == TYPE_NOMATCH)
4075 n1 = !n1;
4076 }
4077 p_cpo = save_cpo;
4078 break;
4079
4080 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4081 }
4082 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004083 clear_tv(rettv);
4084 clear_tv(&var2);
4085 rettv->v_type = VAR_NUMBER;
4086 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 }
4088 }
4089
4090 return OK;
4091}
4092
4093/*
4094 * Handle fourth level expression:
4095 * + number addition
4096 * - number subtraction
4097 * . string concatenation
4098 *
4099 * "arg" must point to the first non-white of the expression.
4100 * "arg" is advanced to the next non-white after the recognized expression.
4101 *
4102 * Return OK or FAIL.
4103 */
4104 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004105eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004107 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 int evaluate;
4109{
Bram Moolenaar33570922005-01-25 22:26:29 +00004110 typval_T var2;
4111 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 int op;
4113 long n1, n2;
4114 char_u *s1, *s2;
4115 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4116 char_u *p;
4117
4118 /*
4119 * Get the first variable.
4120 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004121 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 return FAIL;
4123
4124 /*
4125 * Repeat computing, until no '+', '-' or '.' is following.
4126 */
4127 for (;;)
4128 {
4129 op = **arg;
4130 if (op != '+' && op != '-' && op != '.')
4131 break;
4132
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004133 if (op != '+' || rettv->v_type != VAR_LIST)
4134 {
4135 /* For "list + ...", an illegal use of the first operand as
4136 * a number cannot be determined before evaluating the 2nd
4137 * operand: if this is also a list, all is ok.
4138 * For "something . ...", "something - ..." or "non-list + ...",
4139 * we know that the first operand needs to be a string or number
4140 * without evaluating the 2nd operand. So check before to avoid
4141 * side effects after an error. */
4142 if (evaluate && get_tv_string_chk(rettv) == NULL)
4143 {
4144 clear_tv(rettv);
4145 return FAIL;
4146 }
4147 }
4148
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 /*
4150 * Get the second variable.
4151 */
4152 *arg = skipwhite(*arg + 1);
4153 if (eval6(arg, &var2, evaluate) == FAIL)
4154 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004155 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 return FAIL;
4157 }
4158
4159 if (evaluate)
4160 {
4161 /*
4162 * Compute the result.
4163 */
4164 if (op == '.')
4165 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004166 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4167 s2 = get_tv_string_buf_chk(&var2, buf2);
4168 if (s2 == NULL) /* type error ? */
4169 {
4170 clear_tv(rettv);
4171 clear_tv(&var2);
4172 return FAIL;
4173 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004174 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004175 clear_tv(rettv);
4176 rettv->v_type = VAR_STRING;
4177 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004179 else if (op == '+' && rettv->v_type == VAR_LIST
4180 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004181 {
4182 /* concatenate Lists */
4183 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4184 &var3) == FAIL)
4185 {
4186 clear_tv(rettv);
4187 clear_tv(&var2);
4188 return FAIL;
4189 }
4190 clear_tv(rettv);
4191 *rettv = var3;
4192 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 else
4194 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004195 int error = FALSE;
4196
4197 n1 = get_tv_number_chk(rettv, &error);
4198 if (error)
4199 {
4200 /* This can only happen for "list + non-list".
4201 * For "non-list + ..." or "something - ...", we returned
4202 * before evaluating the 2nd operand. */
4203 clear_tv(rettv);
4204 return FAIL;
4205 }
4206 n2 = get_tv_number_chk(&var2, &error);
4207 if (error)
4208 {
4209 clear_tv(rettv);
4210 clear_tv(&var2);
4211 return FAIL;
4212 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004213 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 if (op == '+')
4215 n1 = n1 + n2;
4216 else
4217 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004218 rettv->v_type = VAR_NUMBER;
4219 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004221 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 }
4223 }
4224 return OK;
4225}
4226
4227/*
4228 * Handle fifth level expression:
4229 * * number multiplication
4230 * / number division
4231 * % number modulo
4232 *
4233 * "arg" must point to the first non-white of the expression.
4234 * "arg" is advanced to the next non-white after the recognized expression.
4235 *
4236 * Return OK or FAIL.
4237 */
4238 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004239eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004241 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 int evaluate;
4243{
Bram Moolenaar33570922005-01-25 22:26:29 +00004244 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 int op;
4246 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004247 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248
4249 /*
4250 * Get the first variable.
4251 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004252 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 return FAIL;
4254
4255 /*
4256 * Repeat computing, until no '*', '/' or '%' is following.
4257 */
4258 for (;;)
4259 {
4260 op = **arg;
4261 if (op != '*' && op != '/' && op != '%')
4262 break;
4263
4264 if (evaluate)
4265 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004266 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004267 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004268 if (error)
4269 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 }
4271 else
4272 n1 = 0;
4273
4274 /*
4275 * Get the second variable.
4276 */
4277 *arg = skipwhite(*arg + 1);
4278 if (eval7(arg, &var2, evaluate) == FAIL)
4279 return FAIL;
4280
4281 if (evaluate)
4282 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004283 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004284 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004285 if (error)
4286 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287
4288 /*
4289 * Compute the result.
4290 */
4291 if (op == '*')
4292 n1 = n1 * n2;
4293 else if (op == '/')
4294 {
4295 if (n2 == 0) /* give an error message? */
4296 n1 = 0x7fffffffL;
4297 else
4298 n1 = n1 / n2;
4299 }
4300 else
4301 {
4302 if (n2 == 0) /* give an error message? */
4303 n1 = 0;
4304 else
4305 n1 = n1 % n2;
4306 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004307 rettv->v_type = VAR_NUMBER;
4308 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 }
4310 }
4311
4312 return OK;
4313}
4314
4315/*
4316 * Handle sixth level expression:
4317 * number number constant
4318 * "string" string contstant
4319 * 'string' literal string contstant
4320 * &option-name option value
4321 * @r register contents
4322 * identifier variable value
4323 * function() function call
4324 * $VAR environment variable
4325 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004326 * [expr, expr] List
4327 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 *
4329 * Also handle:
4330 * ! in front logical NOT
4331 * - in front unary minus
4332 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004333 * trailing [] subscript in String or List
4334 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 *
4336 * "arg" must point to the first non-white of the expression.
4337 * "arg" is advanced to the next non-white after the recognized expression.
4338 *
4339 * Return OK or FAIL.
4340 */
4341 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004342eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004344 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 int evaluate;
4346{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 long n;
4348 int len;
4349 char_u *s;
4350 int val;
4351 char_u *start_leader, *end_leader;
4352 int ret = OK;
4353 char_u *alias;
4354
4355 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004356 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004357 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004359 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360
4361 /*
4362 * Skip '!' and '-' characters. They are handled later.
4363 */
4364 start_leader = *arg;
4365 while (**arg == '!' || **arg == '-' || **arg == '+')
4366 *arg = skipwhite(*arg + 1);
4367 end_leader = *arg;
4368
4369 switch (**arg)
4370 {
4371 /*
4372 * Number constant.
4373 */
4374 case '0':
4375 case '1':
4376 case '2':
4377 case '3':
4378 case '4':
4379 case '5':
4380 case '6':
4381 case '7':
4382 case '8':
4383 case '9':
4384 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4385 *arg += len;
4386 if (evaluate)
4387 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004388 rettv->v_type = VAR_NUMBER;
4389 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 }
4391 break;
4392
4393 /*
4394 * String constant: "string".
4395 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004396 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 break;
4398
4399 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004400 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004402 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004403 break;
4404
4405 /*
4406 * List: [expr, expr]
4407 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004408 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 break;
4410
4411 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004412 * Dictionary: {key: val, key: val}
4413 */
4414 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4415 break;
4416
4417 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004418 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004420 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 break;
4422
4423 /*
4424 * Environment variable: $VAR.
4425 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004426 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 break;
4428
4429 /*
4430 * Register contents: @r.
4431 */
4432 case '@': ++*arg;
4433 if (evaluate)
4434 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004435 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004436 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 }
4438 if (**arg != NUL)
4439 ++*arg;
4440 break;
4441
4442 /*
4443 * nested expression: (expression).
4444 */
4445 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004446 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 if (**arg == ')')
4448 ++*arg;
4449 else if (ret == OK)
4450 {
4451 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004452 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 ret = FAIL;
4454 }
4455 break;
4456
Bram Moolenaar8c711452005-01-14 21:53:12 +00004457 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 break;
4459 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004460
4461 if (ret == NOTDONE)
4462 {
4463 /*
4464 * Must be a variable or function name.
4465 * Can also be a curly-braces kind of name: {expr}.
4466 */
4467 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004468 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004469 if (alias != NULL)
4470 s = alias;
4471
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004472 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004473 ret = FAIL;
4474 else
4475 {
4476 if (**arg == '(') /* recursive! */
4477 {
4478 /* If "s" is the name of a variable of type VAR_FUNC
4479 * use its contents. */
4480 s = deref_func_name(s, &len);
4481
4482 /* Invoke the function. */
4483 ret = get_func_tv(s, len, rettv, arg,
4484 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004485 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004486 /* Stop the expression evaluation when immediately
4487 * aborting on error, or when an interrupt occurred or
4488 * an exception was thrown but not caught. */
4489 if (aborting())
4490 {
4491 if (ret == OK)
4492 clear_tv(rettv);
4493 ret = FAIL;
4494 }
4495 }
4496 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004497 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004498 else
4499 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004500 }
4501
4502 if (alias != NULL)
4503 vim_free(alias);
4504 }
4505
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 *arg = skipwhite(*arg);
4507
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004508 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4509 * expr(expr). */
4510 if (ret == OK)
4511 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512
4513 /*
4514 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4515 */
4516 if (ret == OK && evaluate && end_leader > start_leader)
4517 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004518 int error = FALSE;
4519
4520 val = get_tv_number_chk(rettv, &error);
4521 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004523 clear_tv(rettv);
4524 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004526 else
4527 {
4528 while (end_leader > start_leader)
4529 {
4530 --end_leader;
4531 if (*end_leader == '!')
4532 val = !val;
4533 else if (*end_leader == '-')
4534 val = -val;
4535 }
4536 clear_tv(rettv);
4537 rettv->v_type = VAR_NUMBER;
4538 rettv->vval.v_number = val;
4539 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 }
4541
4542 return ret;
4543}
4544
4545/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004546 * Evaluate an "[expr]" or "[expr:expr]" index.
4547 * "*arg" points to the '['.
4548 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4549 */
4550 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004551eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004552 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004553 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004554 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004555 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004556{
4557 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004558 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004559 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004560 long len = -1;
4561 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004562 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004563 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004564
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004565 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004566 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004567 if (verbose)
4568 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004569 return FAIL;
4570 }
4571
Bram Moolenaar8c711452005-01-14 21:53:12 +00004572 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004573 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004574 /*
4575 * dict.name
4576 */
4577 key = *arg + 1;
4578 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4579 ;
4580 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004581 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004582 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004583 }
4584 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004585 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004586 /*
4587 * something[idx]
4588 *
4589 * Get the (first) variable from inside the [].
4590 */
4591 *arg = skipwhite(*arg + 1);
4592 if (**arg == ':')
4593 empty1 = TRUE;
4594 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4595 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004596 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4597 {
4598 /* not a number or string */
4599 clear_tv(&var1);
4600 return FAIL;
4601 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004602
4603 /*
4604 * Get the second variable from inside the [:].
4605 */
4606 if (**arg == ':')
4607 {
4608 range = TRUE;
4609 *arg = skipwhite(*arg + 1);
4610 if (**arg == ']')
4611 empty2 = TRUE;
4612 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4613 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004614 if (!empty1)
4615 clear_tv(&var1);
4616 return FAIL;
4617 }
4618 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4619 {
4620 /* not a number or string */
4621 if (!empty1)
4622 clear_tv(&var1);
4623 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004624 return FAIL;
4625 }
4626 }
4627
4628 /* Check for the ']'. */
4629 if (**arg != ']')
4630 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004631 if (verbose)
4632 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004633 clear_tv(&var1);
4634 if (range)
4635 clear_tv(&var2);
4636 return FAIL;
4637 }
4638 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004639 }
4640
4641 if (evaluate)
4642 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004643 n1 = 0;
4644 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004645 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004646 n1 = get_tv_number(&var1);
4647 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004648 }
4649 if (range)
4650 {
4651 if (empty2)
4652 n2 = -1;
4653 else
4654 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004655 n2 = get_tv_number(&var2);
4656 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004657 }
4658 }
4659
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004660 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004661 {
4662 case VAR_NUMBER:
4663 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004664 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004665 len = (long)STRLEN(s);
4666 if (range)
4667 {
4668 /* The resulting variable is a substring. If the indexes
4669 * are out of range the result is empty. */
4670 if (n1 < 0)
4671 {
4672 n1 = len + n1;
4673 if (n1 < 0)
4674 n1 = 0;
4675 }
4676 if (n2 < 0)
4677 n2 = len + n2;
4678 else if (n2 >= len)
4679 n2 = len;
4680 if (n1 >= len || n2 < 0 || n1 > n2)
4681 s = NULL;
4682 else
4683 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4684 }
4685 else
4686 {
4687 /* The resulting variable is a string of a single
4688 * character. If the index is too big or negative the
4689 * result is empty. */
4690 if (n1 >= len || n1 < 0)
4691 s = NULL;
4692 else
4693 s = vim_strnsave(s + n1, 1);
4694 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004695 clear_tv(rettv);
4696 rettv->v_type = VAR_STRING;
4697 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004698 break;
4699
4700 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004701 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004702 if (n1 < 0)
4703 n1 = len + n1;
4704 if (!empty1 && (n1 < 0 || n1 >= len))
4705 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004706 if (verbose)
4707 EMSGN(_(e_listidx), n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004708 return FAIL;
4709 }
4710 if (range)
4711 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004712 list_T *l;
4713 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004714
4715 if (n2 < 0)
4716 n2 = len + n2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004717 if (!empty2 && (n2 < 0 || n2 >= len || n2 + 1 < n1))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004718 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004719 if (verbose)
4720 EMSGN(_(e_listidx), n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004721 return FAIL;
4722 }
4723 l = list_alloc();
4724 if (l == NULL)
4725 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004726 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004727 n1 <= n2; ++n1)
4728 {
4729 if (list_append_tv(l, &item->li_tv) == FAIL)
4730 {
4731 list_free(l);
4732 return FAIL;
4733 }
4734 item = item->li_next;
4735 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004736 clear_tv(rettv);
4737 rettv->v_type = VAR_LIST;
4738 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004739 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004740 }
4741 else
4742 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004743 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv,
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004744 &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004745 clear_tv(rettv);
4746 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004747 }
4748 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004749
4750 case VAR_DICT:
4751 if (range)
4752 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004753 if (verbose)
4754 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004755 if (len == -1)
4756 clear_tv(&var1);
4757 return FAIL;
4758 }
4759 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004760 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004761
4762 if (len == -1)
4763 {
4764 key = get_tv_string(&var1);
4765 if (*key == NUL)
4766 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004767 if (verbose)
4768 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004769 clear_tv(&var1);
4770 return FAIL;
4771 }
4772 }
4773
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004774 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004775
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004776 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004777 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004778 if (len == -1)
4779 clear_tv(&var1);
4780 if (item == NULL)
4781 return FAIL;
4782
4783 copy_tv(&item->di_tv, &var1);
4784 clear_tv(rettv);
4785 *rettv = var1;
4786 }
4787 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004788 }
4789 }
4790
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004791 return OK;
4792}
4793
4794/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 * Get an option value.
4796 * "arg" points to the '&' or '+' before the option name.
4797 * "arg" is advanced to character after the option name.
4798 * Return OK or FAIL.
4799 */
4800 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004801get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004803 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 int evaluate;
4805{
4806 char_u *option_end;
4807 long numval;
4808 char_u *stringval;
4809 int opt_type;
4810 int c;
4811 int working = (**arg == '+'); /* has("+option") */
4812 int ret = OK;
4813 int opt_flags;
4814
4815 /*
4816 * Isolate the option name and find its value.
4817 */
4818 option_end = find_option_end(arg, &opt_flags);
4819 if (option_end == NULL)
4820 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004821 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 EMSG2(_("E112: Option name missing: %s"), *arg);
4823 return FAIL;
4824 }
4825
4826 if (!evaluate)
4827 {
4828 *arg = option_end;
4829 return OK;
4830 }
4831
4832 c = *option_end;
4833 *option_end = NUL;
4834 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004835 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836
4837 if (opt_type == -3) /* invalid name */
4838 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004839 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 EMSG2(_("E113: Unknown option: %s"), *arg);
4841 ret = FAIL;
4842 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004843 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 {
4845 if (opt_type == -2) /* hidden string option */
4846 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004847 rettv->v_type = VAR_STRING;
4848 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 }
4850 else if (opt_type == -1) /* hidden number option */
4851 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004852 rettv->v_type = VAR_NUMBER;
4853 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 }
4855 else if (opt_type == 1) /* number option */
4856 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004857 rettv->v_type = VAR_NUMBER;
4858 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 }
4860 else /* string option */
4861 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004862 rettv->v_type = VAR_STRING;
4863 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 }
4865 }
4866 else if (working && (opt_type == -2 || opt_type == -1))
4867 ret = FAIL;
4868
4869 *option_end = c; /* put back for error messages */
4870 *arg = option_end;
4871
4872 return ret;
4873}
4874
4875/*
4876 * Allocate a variable for a string constant.
4877 * Return OK or FAIL.
4878 */
4879 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004880get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004882 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 int evaluate;
4884{
4885 char_u *p;
4886 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 int extra = 0;
4888
4889 /*
4890 * Find the end of the string, skipping backslashed characters.
4891 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004892 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 {
4894 if (*p == '\\' && p[1] != NUL)
4895 {
4896 ++p;
4897 /* A "\<x>" form occupies at least 4 characters, and produces up
4898 * to 6 characters: reserve space for 2 extra */
4899 if (*p == '<')
4900 extra += 2;
4901 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 }
4903
4904 if (*p != '"')
4905 {
4906 EMSG2(_("E114: Missing quote: %s"), *arg);
4907 return FAIL;
4908 }
4909
4910 /* If only parsing, set *arg and return here */
4911 if (!evaluate)
4912 {
4913 *arg = p + 1;
4914 return OK;
4915 }
4916
4917 /*
4918 * Copy the string into allocated memory, handling backslashed
4919 * characters.
4920 */
4921 name = alloc((unsigned)(p - *arg + extra));
4922 if (name == NULL)
4923 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004924 rettv->v_type = VAR_STRING;
4925 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926
Bram Moolenaar8c711452005-01-14 21:53:12 +00004927 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 {
4929 if (*p == '\\')
4930 {
4931 switch (*++p)
4932 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004933 case 'b': *name++ = BS; ++p; break;
4934 case 'e': *name++ = ESC; ++p; break;
4935 case 'f': *name++ = FF; ++p; break;
4936 case 'n': *name++ = NL; ++p; break;
4937 case 'r': *name++ = CAR; ++p; break;
4938 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939
4940 case 'X': /* hex: "\x1", "\x12" */
4941 case 'x':
4942 case 'u': /* Unicode: "\u0023" */
4943 case 'U':
4944 if (vim_isxdigit(p[1]))
4945 {
4946 int n, nr;
4947 int c = toupper(*p);
4948
4949 if (c == 'X')
4950 n = 2;
4951 else
4952 n = 4;
4953 nr = 0;
4954 while (--n >= 0 && vim_isxdigit(p[1]))
4955 {
4956 ++p;
4957 nr = (nr << 4) + hex2nr(*p);
4958 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004959 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960#ifdef FEAT_MBYTE
4961 /* For "\u" store the number according to
4962 * 'encoding'. */
4963 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00004964 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 else
4966#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00004967 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 break;
4970
4971 /* octal: "\1", "\12", "\123" */
4972 case '0':
4973 case '1':
4974 case '2':
4975 case '3':
4976 case '4':
4977 case '5':
4978 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00004979 case '7': *name = *p++ - '0';
4980 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004982 *name = (*name << 3) + *p++ - '0';
4983 if (*p >= '0' && *p <= '7')
4984 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004986 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 break;
4988
4989 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00004990 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 if (extra != 0)
4992 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004993 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 break;
4995 }
4996 /* FALLTHROUGH */
4997
Bram Moolenaar8c711452005-01-14 21:53:12 +00004998 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 break;
5000 }
5001 }
5002 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005003 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005006 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 *arg = p + 1;
5008
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 return OK;
5010}
5011
5012/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005013 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 * Return OK or FAIL.
5015 */
5016 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005017get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005019 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 int evaluate;
5021{
5022 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005023 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005024 int reduce = 0;
5025
5026 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005027 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005028 */
5029 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5030 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005031 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005032 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005033 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005034 break;
5035 ++reduce;
5036 ++p;
5037 }
5038 }
5039
Bram Moolenaar8c711452005-01-14 21:53:12 +00005040 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005041 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005042 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005043 return FAIL;
5044 }
5045
Bram Moolenaar8c711452005-01-14 21:53:12 +00005046 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005047 if (!evaluate)
5048 {
5049 *arg = p + 1;
5050 return OK;
5051 }
5052
5053 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005054 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005055 */
5056 str = alloc((unsigned)((p - *arg) - reduce));
5057 if (str == NULL)
5058 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005059 rettv->v_type = VAR_STRING;
5060 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005061
Bram Moolenaar8c711452005-01-14 21:53:12 +00005062 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005063 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005064 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005065 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005066 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005067 break;
5068 ++p;
5069 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005070 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005071 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005072 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005073 *arg = p + 1;
5074
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005075 return OK;
5076}
5077
5078/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005079 * Allocate a variable for a List and fill it from "*arg".
5080 * Return OK or FAIL.
5081 */
5082 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005083get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005084 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005085 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005086 int evaluate;
5087{
Bram Moolenaar33570922005-01-25 22:26:29 +00005088 list_T *l = NULL;
5089 typval_T tv;
5090 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005091
5092 if (evaluate)
5093 {
5094 l = list_alloc();
5095 if (l == NULL)
5096 return FAIL;
5097 }
5098
5099 *arg = skipwhite(*arg + 1);
5100 while (**arg != ']' && **arg != NUL)
5101 {
5102 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5103 goto failret;
5104 if (evaluate)
5105 {
5106 item = listitem_alloc();
5107 if (item != NULL)
5108 {
5109 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005110 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005111 list_append(l, item);
5112 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005113 else
5114 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005115 }
5116
5117 if (**arg == ']')
5118 break;
5119 if (**arg != ',')
5120 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005121 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005122 goto failret;
5123 }
5124 *arg = skipwhite(*arg + 1);
5125 }
5126
5127 if (**arg != ']')
5128 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005129 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005130failret:
5131 if (evaluate)
5132 list_free(l);
5133 return FAIL;
5134 }
5135
5136 *arg = skipwhite(*arg + 1);
5137 if (evaluate)
5138 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005139 rettv->v_type = VAR_LIST;
5140 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005141 ++l->lv_refcount;
5142 }
5143
5144 return OK;
5145}
5146
5147/*
5148 * Allocate an empty header for a list.
5149 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005150 static list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005151list_alloc()
5152{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005153 list_T *l;
5154
5155 l = (list_T *)alloc_clear(sizeof(list_T));
5156 if (l != NULL)
5157 {
5158 /* Prepend the list to the list of lists for garbage collection. */
5159 if (first_list != NULL)
5160 first_list->lv_used_prev = l;
5161 l->lv_used_prev = NULL;
5162 l->lv_used_next = first_list;
5163 first_list = l;
5164 }
5165 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005166}
5167
5168/*
5169 * Unreference a list: decrement the reference count and free it when it
5170 * becomes zero.
5171 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005172 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005173list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005174 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005175{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005176 if (l != NULL && l->lv_refcount != DEL_REFCOUNT && --l->lv_refcount <= 0)
5177 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005178}
5179
5180/*
5181 * Free a list, including all items it points to.
5182 * Ignores the reference count.
5183 */
5184 static void
5185list_free(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005186 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005187{
Bram Moolenaar33570922005-01-25 22:26:29 +00005188 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005189
Bram Moolenaard9fba312005-06-26 22:34:35 +00005190 /* Avoid that recursive reference to the list frees us again. */
5191 l->lv_refcount = DEL_REFCOUNT;
5192
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005193 /* Remove the list from the list of lists for garbage collection. */
5194 if (l->lv_used_prev == NULL)
5195 first_list = l->lv_used_next;
5196 else
5197 l->lv_used_prev->lv_used_next = l->lv_used_next;
5198 if (l->lv_used_next != NULL)
5199 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5200
Bram Moolenaard9fba312005-06-26 22:34:35 +00005201 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005202 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005203 /* Remove the item before deleting it. */
5204 l->lv_first = item->li_next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005205 listitem_free(item);
5206 }
5207 vim_free(l);
5208}
5209
5210/*
5211 * Allocate a list item.
5212 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005213 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005214listitem_alloc()
5215{
Bram Moolenaar33570922005-01-25 22:26:29 +00005216 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005217}
5218
5219/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005220 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005221 */
5222 static void
5223listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005224 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005225{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005226 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005227 vim_free(item);
5228}
5229
5230/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005231 * Remove a list item from a List and free it. Also clears the value.
5232 */
5233 static void
5234listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005235 list_T *l;
5236 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005237{
5238 list_remove(l, item, item);
5239 listitem_free(item);
5240}
5241
5242/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005243 * Get the number of items in a list.
5244 */
5245 static long
5246list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005247 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005248{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005249 if (l == NULL)
5250 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005251 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005252}
5253
5254/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005255 * Return TRUE when two lists have exactly the same values.
5256 */
5257 static int
5258list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005259 list_T *l1;
5260 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005261 int ic; /* ignore case for strings */
5262{
Bram Moolenaar33570922005-01-25 22:26:29 +00005263 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005264
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005265 if (list_len(l1) != list_len(l2))
5266 return FALSE;
5267
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005268 for (item1 = l1->lv_first, item2 = l2->lv_first;
5269 item1 != NULL && item2 != NULL;
5270 item1 = item1->li_next, item2 = item2->li_next)
5271 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5272 return FALSE;
5273 return item1 == NULL && item2 == NULL;
5274}
5275
5276/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005277 * Return TRUE when two dictionaries have exactly the same key/values.
5278 */
5279 static int
5280dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005281 dict_T *d1;
5282 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005283 int ic; /* ignore case for strings */
5284{
Bram Moolenaar33570922005-01-25 22:26:29 +00005285 hashitem_T *hi;
5286 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005287 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005288
5289 if (dict_len(d1) != dict_len(d2))
5290 return FALSE;
5291
Bram Moolenaar33570922005-01-25 22:26:29 +00005292 todo = d1->dv_hashtab.ht_used;
5293 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005294 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005295 if (!HASHITEM_EMPTY(hi))
5296 {
5297 item2 = dict_find(d2, hi->hi_key, -1);
5298 if (item2 == NULL)
5299 return FALSE;
5300 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5301 return FALSE;
5302 --todo;
5303 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005304 }
5305 return TRUE;
5306}
5307
5308/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005309 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005310 * Compares the items just like "==" would compare them, but strings and
5311 * numbers are different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005312 */
5313 static int
5314tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005315 typval_T *tv1;
5316 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005317 int ic; /* ignore case */
5318{
5319 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005320 char_u *s1, *s2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005321
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005322 if (tv1->v_type != tv2->v_type)
5323 return FALSE;
5324
5325 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005326 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005327 case VAR_LIST:
5328 /* recursive! */
5329 return list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5330
5331 case VAR_DICT:
5332 /* recursive! */
5333 return dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5334
5335 case VAR_FUNC:
5336 return (tv1->vval.v_string != NULL
5337 && tv2->vval.v_string != NULL
5338 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5339
5340 case VAR_NUMBER:
5341 return tv1->vval.v_number == tv2->vval.v_number;
5342
5343 case VAR_STRING:
5344 s1 = get_tv_string_buf(tv1, buf1);
5345 s2 = get_tv_string_buf(tv2, buf2);
5346 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005347 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005348
5349 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005350 return TRUE;
5351}
5352
5353/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005354 * Locate item with index "n" in list "l" and return it.
5355 * A negative index is counted from the end; -1 is the last item.
5356 * Returns NULL when "n" is out of range.
5357 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005358 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005359list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005360 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005361 long n;
5362{
Bram Moolenaar33570922005-01-25 22:26:29 +00005363 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005364 long idx;
5365
5366 if (l == NULL)
5367 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005368
5369 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005370 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005371 n = l->lv_len + n;
5372
5373 /* Check for index out of range. */
5374 if (n < 0 || n >= l->lv_len)
5375 return NULL;
5376
5377 /* When there is a cached index may start search from there. */
5378 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005379 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005380 if (n < l->lv_idx / 2)
5381 {
5382 /* closest to the start of the list */
5383 item = l->lv_first;
5384 idx = 0;
5385 }
5386 else if (n > (l->lv_idx + l->lv_len) / 2)
5387 {
5388 /* closest to the end of the list */
5389 item = l->lv_last;
5390 idx = l->lv_len - 1;
5391 }
5392 else
5393 {
5394 /* closest to the cached index */
5395 item = l->lv_idx_item;
5396 idx = l->lv_idx;
5397 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005398 }
5399 else
5400 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005401 if (n < l->lv_len / 2)
5402 {
5403 /* closest to the start of the list */
5404 item = l->lv_first;
5405 idx = 0;
5406 }
5407 else
5408 {
5409 /* closest to the end of the list */
5410 item = l->lv_last;
5411 idx = l->lv_len - 1;
5412 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005413 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005414
5415 while (n > idx)
5416 {
5417 /* search forward */
5418 item = item->li_next;
5419 ++idx;
5420 }
5421 while (n < idx)
5422 {
5423 /* search backward */
5424 item = item->li_prev;
5425 --idx;
5426 }
5427
5428 /* cache the used index */
5429 l->lv_idx = idx;
5430 l->lv_idx_item = item;
5431
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005432 return item;
5433}
5434
5435/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005436 * Locate "item" list "l" and return its index.
5437 * Returns -1 when "item" is not in the list.
5438 */
5439 static long
5440list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005441 list_T *l;
5442 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005443{
5444 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005445 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005446
5447 if (l == NULL)
5448 return -1;
5449 idx = 0;
5450 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5451 ++idx;
5452 if (li == NULL)
5453 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005454 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005455}
5456
5457/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005458 * Append item "item" to the end of list "l".
5459 */
5460 static void
5461list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005462 list_T *l;
5463 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005464{
5465 if (l->lv_last == NULL)
5466 {
5467 /* empty list */
5468 l->lv_first = item;
5469 l->lv_last = item;
5470 item->li_prev = NULL;
5471 }
5472 else
5473 {
5474 l->lv_last->li_next = item;
5475 item->li_prev = l->lv_last;
5476 l->lv_last = item;
5477 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005478 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005479 item->li_next = NULL;
5480}
5481
5482/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005483 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005484 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005485 */
5486 static int
5487list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005488 list_T *l;
5489 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005490{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005491 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005492
Bram Moolenaar05159a02005-02-26 23:04:13 +00005493 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005494 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005495 copy_tv(tv, &li->li_tv);
5496 list_append(l, li);
5497 return OK;
5498}
5499
5500/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005501 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005502 * Return FAIL when out of memory.
5503 */
5504 int
5505list_append_dict(list, dict)
5506 list_T *list;
5507 dict_T *dict;
5508{
5509 listitem_T *li = listitem_alloc();
5510
5511 if (li == NULL)
5512 return FAIL;
5513 li->li_tv.v_type = VAR_DICT;
5514 li->li_tv.v_lock = 0;
5515 li->li_tv.vval.v_dict = dict;
5516 list_append(list, li);
5517 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005518 return OK;
5519}
5520
5521/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005522 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005523 * If "item" is NULL append at the end.
5524 * Return FAIL when out of memory.
5525 */
5526 static int
5527list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005528 list_T *l;
5529 typval_T *tv;
5530 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005531{
Bram Moolenaar33570922005-01-25 22:26:29 +00005532 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005533
5534 if (ni == NULL)
5535 return FAIL;
5536 copy_tv(tv, &ni->li_tv);
5537 if (item == NULL)
5538 /* Append new item at end of list. */
5539 list_append(l, ni);
5540 else
5541 {
5542 /* Insert new item before existing item. */
5543 ni->li_prev = item->li_prev;
5544 ni->li_next = item;
5545 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005546 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005547 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005548 ++l->lv_idx;
5549 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005550 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005551 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005552 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005553 l->lv_idx_item = NULL;
5554 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005555 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005556 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005557 }
5558 return OK;
5559}
5560
5561/*
5562 * Extend "l1" with "l2".
5563 * If "bef" is NULL append at the end, otherwise insert before this item.
5564 * Returns FAIL when out of memory.
5565 */
5566 static int
5567list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005568 list_T *l1;
5569 list_T *l2;
5570 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005571{
Bram Moolenaar33570922005-01-25 22:26:29 +00005572 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005573
5574 for (item = l2->lv_first; item != NULL; item = item->li_next)
5575 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5576 return FAIL;
5577 return OK;
5578}
5579
5580/*
5581 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5582 * Return FAIL when out of memory.
5583 */
5584 static int
5585list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005586 list_T *l1;
5587 list_T *l2;
5588 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005589{
Bram Moolenaar33570922005-01-25 22:26:29 +00005590 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005591
5592 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005593 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005594 if (l == NULL)
5595 return FAIL;
5596 tv->v_type = VAR_LIST;
5597 tv->vval.v_list = l;
5598
5599 /* append all items from the second list */
5600 return list_extend(l, l2, NULL);
5601}
5602
5603/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005604 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005605 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005606 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005607 * Returns NULL when out of memory.
5608 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005609 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005610list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005611 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005612 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005613 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005614{
Bram Moolenaar33570922005-01-25 22:26:29 +00005615 list_T *copy;
5616 listitem_T *item;
5617 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005618
5619 if (orig == NULL)
5620 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005621
5622 copy = list_alloc();
5623 if (copy != NULL)
5624 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005625 if (copyID != 0)
5626 {
5627 /* Do this before adding the items, because one of the items may
5628 * refer back to this list. */
5629 orig->lv_copyID = copyID;
5630 orig->lv_copylist = copy;
5631 }
5632 for (item = orig->lv_first; item != NULL && !got_int;
5633 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005634 {
5635 ni = listitem_alloc();
5636 if (ni == NULL)
5637 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005638 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005639 {
5640 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5641 {
5642 vim_free(ni);
5643 break;
5644 }
5645 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005646 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005647 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005648 list_append(copy, ni);
5649 }
5650 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005651 if (item != NULL)
5652 {
5653 list_unref(copy);
5654 copy = NULL;
5655 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005656 }
5657
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005658 return copy;
5659}
5660
5661/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005662 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005663 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005664 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005665 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005666list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005667 list_T *l;
5668 listitem_T *item;
5669 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005670{
Bram Moolenaar33570922005-01-25 22:26:29 +00005671 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005672
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005673 /* notify watchers */
5674 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005675 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005676 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005677 list_fix_watch(l, ip);
5678 if (ip == item2)
5679 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005680 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005681
5682 if (item2->li_next == NULL)
5683 l->lv_last = item->li_prev;
5684 else
5685 item2->li_next->li_prev = item->li_prev;
5686 if (item->li_prev == NULL)
5687 l->lv_first = item2->li_next;
5688 else
5689 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005690 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005691}
5692
5693/*
5694 * Return an allocated string with the string representation of a list.
5695 * May return NULL.
5696 */
5697 static char_u *
5698list2string(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005699 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005700{
5701 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005702
5703 if (tv->vval.v_list == NULL)
5704 return NULL;
5705 ga_init2(&ga, (int)sizeof(char), 80);
5706 ga_append(&ga, '[');
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005707 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE) == FAIL)
5708 {
5709 vim_free(ga.ga_data);
5710 return NULL;
5711 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005712 ga_append(&ga, ']');
5713 ga_append(&ga, NUL);
5714 return (char_u *)ga.ga_data;
5715}
5716
5717/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005718 * Join list "l" into a string in "*gap", using separator "sep".
5719 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005720 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005721 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005722 static int
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005723list_join(gap, l, sep, echo)
5724 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00005725 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005726 char_u *sep;
5727 int echo;
5728{
5729 int first = TRUE;
5730 char_u *tofree;
5731 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00005732 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005733 char_u *s;
5734
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005735 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005736 {
5737 if (first)
5738 first = FALSE;
5739 else
5740 ga_concat(gap, sep);
5741
5742 if (echo)
5743 s = echo_string(&item->li_tv, &tofree, numbuf);
5744 else
5745 s = tv2string(&item->li_tv, &tofree, numbuf);
5746 if (s != NULL)
5747 ga_concat(gap, s);
5748 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005749 if (s == NULL)
5750 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005751 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005752 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005753}
5754
5755/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005756 * Garbage collection for lists and dictionaries.
5757 *
5758 * We use reference counts to be able to free most items right away when they
5759 * are no longer used. But for composite items it's possible that it becomes
5760 * unused while the reference count is > 0: When there is a recursive
5761 * reference. Example:
5762 * :let l = [1, 2, 3]
5763 * :let d = {9: l}
5764 * :let l[1] = d
5765 *
5766 * Since this is quite unusual we handle this with garbage collection: every
5767 * once in a while find out which lists and dicts are not referenced from any
5768 * variable.
5769 *
5770 * Here is a good reference text about garbage collection (refers to Python
5771 * but it applies to all reference-counting mechanisms):
5772 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005773 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005774
5775/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005776 * Do garbage collection for lists and dicts.
5777 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005778 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005779 int
5780garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00005781{
5782 dict_T *dd;
5783 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005784 int copyID = ++current_copyID;
5785 buf_T *buf;
5786 win_T *wp;
5787 int i;
5788 funccall_T *fc;
5789 int did_free = FALSE;
5790
5791 /*
5792 * 1. Go through all accessible variables and mark all lists and dicts
5793 * with copyID.
5794 */
5795 /* script-local variables */
5796 for (i = 1; i <= ga_scripts.ga_len; ++i)
5797 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
5798
5799 /* buffer-local variables */
5800 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5801 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
5802
5803 /* window-local variables */
5804 FOR_ALL_WINDOWS(wp)
5805 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
5806
5807 /* global variables */
5808 set_ref_in_ht(&globvarht, copyID);
5809
5810 /* function-local variables */
5811 for (fc = current_funccal; fc != NULL; fc = fc->caller)
5812 {
5813 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
5814 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
5815 }
5816
5817 /*
5818 * 2. Go through the list of dicts and free items without the copyID.
5819 */
5820 for (dd = first_dict; dd != NULL; )
5821 if (dd->dv_copyID != copyID)
5822 {
5823 dict_free(dd);
5824 did_free = TRUE;
5825
5826 /* restart, next dict may also have been freed */
5827 dd = first_dict;
5828 }
5829 else
5830 dd = dd->dv_used_next;
5831
5832 /*
5833 * 3. Go through the list of lists and free items without the copyID.
5834 */
5835 for (ll = first_list; ll != NULL; )
5836 if (ll->lv_copyID != copyID)
5837 {
5838 list_free(ll);
5839 did_free = TRUE;
5840
5841 /* restart, next dict may also have been freed */
5842 ll = first_list;
5843 }
5844 else
5845 ll = ll->lv_used_next;
5846
5847 return did_free;
5848}
5849
5850/*
5851 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
5852 */
5853 static void
5854set_ref_in_ht(ht, copyID)
5855 hashtab_T *ht;
5856 int copyID;
5857{
5858 int todo;
5859 hashitem_T *hi;
5860
5861 todo = ht->ht_used;
5862 for (hi = ht->ht_array; todo > 0; ++hi)
5863 if (!HASHITEM_EMPTY(hi))
5864 {
5865 --todo;
5866 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
5867 }
5868}
5869
5870/*
5871 * Mark all lists and dicts referenced through list "l" with "copyID".
5872 */
5873 static void
5874set_ref_in_list(l, copyID)
5875 list_T *l;
5876 int copyID;
5877{
5878 listitem_T *li;
5879
5880 for (li = l->lv_first; li != NULL; li = li->li_next)
5881 set_ref_in_item(&li->li_tv, copyID);
5882}
5883
5884/*
5885 * Mark all lists and dicts referenced through typval "tv" with "copyID".
5886 */
5887 static void
5888set_ref_in_item(tv, copyID)
5889 typval_T *tv;
5890 int copyID;
5891{
5892 dict_T *dd;
5893 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005894
5895 switch (tv->v_type)
5896 {
5897 case VAR_DICT:
5898 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005899 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005900 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005901 /* Didn't see this dict yet. */
5902 dd->dv_copyID = copyID;
5903 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00005904 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005905 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005906
5907 case VAR_LIST:
5908 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005909 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005910 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005911 /* Didn't see this list yet. */
5912 ll->lv_copyID = copyID;
5913 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00005914 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005915 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005916 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005917 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005918}
5919
5920/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005921 * Allocate an empty header for a dictionary.
5922 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005923 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00005924dict_alloc()
5925{
Bram Moolenaar33570922005-01-25 22:26:29 +00005926 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005927
Bram Moolenaar33570922005-01-25 22:26:29 +00005928 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005929 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005930 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005931 /* Add the list to the hashtable for garbage collection. */
5932 if (first_dict != NULL)
5933 first_dict->dv_used_prev = d;
5934 d->dv_used_next = first_dict;
5935 d->dv_used_prev = NULL;
5936
Bram Moolenaar33570922005-01-25 22:26:29 +00005937 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005938 d->dv_lock = 0;
5939 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005940 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005941 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005942 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005943}
5944
5945/*
5946 * Unreference a Dictionary: decrement the reference count and free it when it
5947 * becomes zero.
5948 */
5949 static void
5950dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00005951 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005952{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005953 if (d != NULL && d->dv_refcount != DEL_REFCOUNT && --d->dv_refcount <= 0)
5954 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005955}
5956
5957/*
5958 * Free a Dictionary, including all items it contains.
5959 * Ignores the reference count.
5960 */
5961 static void
5962dict_free(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00005963 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005964{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005965 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00005966 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005967 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005968
Bram Moolenaard9fba312005-06-26 22:34:35 +00005969 /* Avoid that recursive reference to the dict frees us again. */
5970 d->dv_refcount = DEL_REFCOUNT;
5971
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005972 /* Remove the dict from the list of dicts for garbage collection. */
5973 if (d->dv_used_prev == NULL)
5974 first_dict = d->dv_used_next;
5975 else
5976 d->dv_used_prev->dv_used_next = d->dv_used_next;
5977 if (d->dv_used_next != NULL)
5978 d->dv_used_next->dv_used_prev = d->dv_used_prev;
5979
5980 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005981 hash_lock(&d->dv_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +00005982 todo = d->dv_hashtab.ht_used;
5983 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005984 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005985 if (!HASHITEM_EMPTY(hi))
5986 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005987 /* Remove the item before deleting it, just in case there is
5988 * something recursive causing trouble. */
5989 di = HI2DI(hi);
5990 hash_remove(&d->dv_hashtab, hi);
5991 dictitem_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005992 --todo;
5993 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005994 }
Bram Moolenaar33570922005-01-25 22:26:29 +00005995 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005996 vim_free(d);
5997}
5998
5999/*
6000 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006001 * The "key" is copied to the new item.
6002 * Note that the value of the item "di_tv" still needs to be initialized!
6003 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006004 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006005 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006006dictitem_alloc(key)
6007 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006008{
Bram Moolenaar33570922005-01-25 22:26:29 +00006009 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006010
Bram Moolenaar33570922005-01-25 22:26:29 +00006011 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(key));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006012 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006013 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006014 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006015 di->di_flags = 0;
6016 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006017 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006018}
6019
6020/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006021 * Make a copy of a Dictionary item.
6022 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006023 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006024dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006025 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006026{
Bram Moolenaar33570922005-01-25 22:26:29 +00006027 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006028
Bram Moolenaar33570922005-01-25 22:26:29 +00006029 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(org->di_key));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006030 if (di != NULL)
6031 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006032 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006033 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006034 copy_tv(&org->di_tv, &di->di_tv);
6035 }
6036 return di;
6037}
6038
6039/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006040 * Remove item "item" from Dictionary "dict" and free it.
6041 */
6042 static void
6043dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006044 dict_T *dict;
6045 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006046{
Bram Moolenaar33570922005-01-25 22:26:29 +00006047 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006048
Bram Moolenaar33570922005-01-25 22:26:29 +00006049 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006050 if (HASHITEM_EMPTY(hi))
6051 EMSG2(_(e_intern2), "dictitem_remove()");
6052 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006053 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006054 dictitem_free(item);
6055}
6056
6057/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006058 * Free a dict item. Also clears the value.
6059 */
6060 static void
6061dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006062 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006063{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006064 clear_tv(&item->di_tv);
6065 vim_free(item);
6066}
6067
6068/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006069 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6070 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006071 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006072 * Returns NULL when out of memory.
6073 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006074 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006075dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006076 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006077 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006078 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006079{
Bram Moolenaar33570922005-01-25 22:26:29 +00006080 dict_T *copy;
6081 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006082 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006083 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006084
6085 if (orig == NULL)
6086 return NULL;
6087
6088 copy = dict_alloc();
6089 if (copy != NULL)
6090 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006091 if (copyID != 0)
6092 {
6093 orig->dv_copyID = copyID;
6094 orig->dv_copydict = copy;
6095 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006096 todo = orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006097 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006098 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006099 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006100 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006101 --todo;
6102
6103 di = dictitem_alloc(hi->hi_key);
6104 if (di == NULL)
6105 break;
6106 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006107 {
6108 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6109 copyID) == FAIL)
6110 {
6111 vim_free(di);
6112 break;
6113 }
6114 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006115 else
6116 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6117 if (dict_add(copy, di) == FAIL)
6118 {
6119 dictitem_free(di);
6120 break;
6121 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006122 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006123 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006124
Bram Moolenaare9a41262005-01-15 22:18:47 +00006125 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006126 if (todo > 0)
6127 {
6128 dict_unref(copy);
6129 copy = NULL;
6130 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006131 }
6132
6133 return copy;
6134}
6135
6136/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006137 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006138 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006139 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006140 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006141dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006142 dict_T *d;
6143 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006144{
Bram Moolenaar33570922005-01-25 22:26:29 +00006145 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006146}
6147
Bram Moolenaar8c711452005-01-14 21:53:12 +00006148/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006149 * Add a number or string entry to dictionary "d".
6150 * When "str" is NULL use number "nr", otherwise use "str".
6151 * Returns FAIL when out of memory and when key already exists.
6152 */
6153 int
6154dict_add_nr_str(d, key, nr, str)
6155 dict_T *d;
6156 char *key;
6157 long nr;
6158 char_u *str;
6159{
6160 dictitem_T *item;
6161
6162 item = dictitem_alloc((char_u *)key);
6163 if (item == NULL)
6164 return FAIL;
6165 item->di_tv.v_lock = 0;
6166 if (str == NULL)
6167 {
6168 item->di_tv.v_type = VAR_NUMBER;
6169 item->di_tv.vval.v_number = nr;
6170 }
6171 else
6172 {
6173 item->di_tv.v_type = VAR_STRING;
6174 item->di_tv.vval.v_string = vim_strsave(str);
6175 }
6176 if (dict_add(d, item) == FAIL)
6177 {
6178 dictitem_free(item);
6179 return FAIL;
6180 }
6181 return OK;
6182}
6183
6184/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006185 * Get the number of items in a Dictionary.
6186 */
6187 static long
6188dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006189 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006190{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006191 if (d == NULL)
6192 return 0L;
Bram Moolenaar33570922005-01-25 22:26:29 +00006193 return d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006194}
6195
6196/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006197 * Find item "key[len]" in Dictionary "d".
6198 * If "len" is negative use strlen(key).
6199 * Returns NULL when not found.
6200 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006201 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006202dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006203 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006204 char_u *key;
6205 int len;
6206{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006207#define AKEYLEN 200
6208 char_u buf[AKEYLEN];
6209 char_u *akey;
6210 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006211 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006212
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006213 if (len < 0)
6214 akey = key;
6215 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006216 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006217 tofree = akey = vim_strnsave(key, len);
6218 if (akey == NULL)
6219 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006220 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006221 else
6222 {
6223 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006224 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006225 akey = buf;
6226 }
6227
Bram Moolenaar33570922005-01-25 22:26:29 +00006228 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006229 vim_free(tofree);
6230 if (HASHITEM_EMPTY(hi))
6231 return NULL;
6232 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006233}
6234
6235/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006236 * Get a string item from a dictionary in allocated memory.
6237 * Returns NULL if the entry doesn't exist or out of memory.
6238 */
6239 char_u *
6240get_dict_string(d, key)
6241 dict_T *d;
6242 char_u *key;
6243{
6244 dictitem_T *di;
6245
6246 di = dict_find(d, key, -1);
6247 if (di == NULL)
6248 return NULL;
6249 return vim_strsave(get_tv_string(&di->di_tv));
6250}
6251
6252/*
6253 * Get a number item from a dictionary.
6254 * Returns 0 if the entry doesn't exist or out of memory.
6255 */
6256 long
6257get_dict_number(d, key)
6258 dict_T *d;
6259 char_u *key;
6260{
6261 dictitem_T *di;
6262
6263 di = dict_find(d, key, -1);
6264 if (di == NULL)
6265 return 0;
6266 return get_tv_number(&di->di_tv);
6267}
6268
6269/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006270 * Return an allocated string with the string representation of a Dictionary.
6271 * May return NULL.
6272 */
6273 static char_u *
6274dict2string(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006275 typval_T *tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006276{
6277 garray_T ga;
6278 int first = TRUE;
6279 char_u *tofree;
6280 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006281 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006282 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006283 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006284 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006285
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006286 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006287 return NULL;
6288 ga_init2(&ga, (int)sizeof(char), 80);
6289 ga_append(&ga, '{');
6290
Bram Moolenaar33570922005-01-25 22:26:29 +00006291 todo = d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006292 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006293 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006294 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006295 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006296 --todo;
6297
6298 if (first)
6299 first = FALSE;
6300 else
6301 ga_concat(&ga, (char_u *)", ");
6302
6303 tofree = string_quote(hi->hi_key, FALSE);
6304 if (tofree != NULL)
6305 {
6306 ga_concat(&ga, tofree);
6307 vim_free(tofree);
6308 }
6309 ga_concat(&ga, (char_u *)": ");
6310 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf);
6311 if (s != NULL)
6312 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006313 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006314 if (s == NULL)
6315 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006316 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006317 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006318 if (todo > 0)
6319 {
6320 vim_free(ga.ga_data);
6321 return NULL;
6322 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006323
6324 ga_append(&ga, '}');
6325 ga_append(&ga, NUL);
6326 return (char_u *)ga.ga_data;
6327}
6328
6329/*
6330 * Allocate a variable for a Dictionary and fill it from "*arg".
6331 * Return OK or FAIL. Returns NOTDONE for {expr}.
6332 */
6333 static int
6334get_dict_tv(arg, rettv, evaluate)
6335 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006336 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006337 int evaluate;
6338{
Bram Moolenaar33570922005-01-25 22:26:29 +00006339 dict_T *d = NULL;
6340 typval_T tvkey;
6341 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006342 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00006343 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006344 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006345 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00006346
6347 /*
6348 * First check if it's not a curly-braces thing: {expr}.
6349 * Must do this without evaluating, otherwise a function may be called
6350 * twice. Unfortunately this means we need to call eval1() twice for the
6351 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006352 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006353 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006354 if (*start != '}')
6355 {
6356 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
6357 return FAIL;
6358 if (*start == '}')
6359 return NOTDONE;
6360 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006361
6362 if (evaluate)
6363 {
6364 d = dict_alloc();
6365 if (d == NULL)
6366 return FAIL;
6367 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006368 tvkey.v_type = VAR_UNKNOWN;
6369 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006370
6371 *arg = skipwhite(*arg + 1);
6372 while (**arg != '}' && **arg != NUL)
6373 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006374 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006375 goto failret;
6376 if (**arg != ':')
6377 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006378 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006379 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006380 goto failret;
6381 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006382 key = get_tv_string_buf_chk(&tvkey, buf);
6383 if (key == NULL || *key == NUL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006384 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006385 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6386 if (key != NULL)
6387 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006388 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006389 goto failret;
6390 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006391
6392 *arg = skipwhite(*arg + 1);
6393 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6394 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006395 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006396 goto failret;
6397 }
6398 if (evaluate)
6399 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006400 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006401 if (item != NULL)
6402 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00006403 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006404 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006405 clear_tv(&tv);
6406 goto failret;
6407 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006408 item = dictitem_alloc(key);
6409 clear_tv(&tvkey);
6410 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006411 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006412 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006413 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006414 if (dict_add(d, item) == FAIL)
6415 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006416 }
6417 }
6418
6419 if (**arg == '}')
6420 break;
6421 if (**arg != ',')
6422 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006423 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006424 goto failret;
6425 }
6426 *arg = skipwhite(*arg + 1);
6427 }
6428
6429 if (**arg != '}')
6430 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006431 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006432failret:
6433 if (evaluate)
6434 dict_free(d);
6435 return FAIL;
6436 }
6437
6438 *arg = skipwhite(*arg + 1);
6439 if (evaluate)
6440 {
6441 rettv->v_type = VAR_DICT;
6442 rettv->vval.v_dict = d;
6443 ++d->dv_refcount;
6444 }
6445
6446 return OK;
6447}
6448
Bram Moolenaar8c711452005-01-14 21:53:12 +00006449/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006450 * Return a string with the string representation of a variable.
6451 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006452 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006453 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006454 * May return NULL;
6455 */
6456 static char_u *
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006457echo_string(tv, tofree, numbuf)
Bram Moolenaar33570922005-01-25 22:26:29 +00006458 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006459 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006460 char_u *numbuf;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006461{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006462 static int recurse = 0;
6463 char_u *r = NULL;
6464
Bram Moolenaar33570922005-01-25 22:26:29 +00006465 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006466 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006467 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006468 *tofree = NULL;
6469 return NULL;
6470 }
6471 ++recurse;
6472
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006473 switch (tv->v_type)
6474 {
6475 case VAR_FUNC:
6476 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006477 r = tv->vval.v_string;
6478 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006479 case VAR_LIST:
6480 *tofree = list2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006481 r = *tofree;
6482 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006483 case VAR_DICT:
6484 *tofree = dict2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006485 r = *tofree;
6486 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006487 case VAR_STRING:
6488 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006489 *tofree = NULL;
6490 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006491 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006492 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006493 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006494 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006495 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006496
6497 --recurse;
6498 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006499}
6500
6501/*
6502 * Return a string with the string representation of a variable.
6503 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6504 * "numbuf" is used for a number.
6505 * Puts quotes around strings, so that they can be parsed back by eval().
6506 * May return NULL;
6507 */
6508 static char_u *
6509tv2string(tv, tofree, numbuf)
Bram Moolenaar33570922005-01-25 22:26:29 +00006510 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006511 char_u **tofree;
6512 char_u *numbuf;
6513{
6514 switch (tv->v_type)
6515 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006516 case VAR_FUNC:
6517 *tofree = string_quote(tv->vval.v_string, TRUE);
6518 return *tofree;
6519 case VAR_STRING:
6520 *tofree = string_quote(tv->vval.v_string, FALSE);
6521 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006522 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006523 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006524 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006525 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006526 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006527 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006528 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006529 return echo_string(tv, tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006530}
6531
6532/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006533 * Return string "str" in ' quotes, doubling ' characters.
6534 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006535 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006536 */
6537 static char_u *
6538string_quote(str, function)
6539 char_u *str;
6540 int function;
6541{
Bram Moolenaar33570922005-01-25 22:26:29 +00006542 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006543 char_u *p, *r, *s;
6544
Bram Moolenaar33570922005-01-25 22:26:29 +00006545 len = (function ? 13 : 3);
6546 if (str != NULL)
6547 {
6548 len += STRLEN(str);
6549 for (p = str; *p != NUL; mb_ptr_adv(p))
6550 if (*p == '\'')
6551 ++len;
6552 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006553 s = r = alloc(len);
6554 if (r != NULL)
6555 {
6556 if (function)
6557 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006558 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006559 r += 10;
6560 }
6561 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006562 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006563 if (str != NULL)
6564 for (p = str; *p != NUL; )
6565 {
6566 if (*p == '\'')
6567 *r++ = '\'';
6568 MB_COPY_CHAR(p, r);
6569 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006570 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006571 if (function)
6572 *r++ = ')';
6573 *r++ = NUL;
6574 }
6575 return s;
6576}
6577
6578/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579 * Get the value of an environment variable.
6580 * "arg" is pointing to the '$'. It is advanced to after the name.
6581 * If the environment variable was not set, silently assume it is empty.
6582 * Always return OK.
6583 */
6584 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006585get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006587 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588 int evaluate;
6589{
6590 char_u *string = NULL;
6591 int len;
6592 int cc;
6593 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006594 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595
6596 ++*arg;
6597 name = *arg;
6598 len = get_env_len(arg);
6599 if (evaluate)
6600 {
6601 if (len != 0)
6602 {
6603 cc = name[len];
6604 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006605 /* first try vim_getenv(), fast for normal environment vars */
6606 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006608 {
6609 if (!mustfree)
6610 string = vim_strsave(string);
6611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612 else
6613 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00006614 if (mustfree)
6615 vim_free(string);
6616
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617 /* next try expanding things like $VIM and ${HOME} */
6618 string = expand_env_save(name - 1);
6619 if (string != NULL && *string == '$')
6620 {
6621 vim_free(string);
6622 string = NULL;
6623 }
6624 }
6625 name[len] = cc;
6626 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006627 rettv->v_type = VAR_STRING;
6628 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629 }
6630
6631 return OK;
6632}
6633
6634/*
6635 * Array with names and number of arguments of all internal functions
6636 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
6637 */
6638static struct fst
6639{
6640 char *f_name; /* function name */
6641 char f_min_argc; /* minimal number of arguments */
6642 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00006643 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006644 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645} functions[] =
6646{
Bram Moolenaar0d660222005-01-07 21:51:51 +00006647 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 {"append", 2, 2, f_append},
6649 {"argc", 0, 0, f_argc},
6650 {"argidx", 0, 0, f_argidx},
6651 {"argv", 1, 1, f_argv},
6652 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006653 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 {"bufexists", 1, 1, f_bufexists},
6655 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
6656 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
6657 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
6658 {"buflisted", 1, 1, f_buflisted},
6659 {"bufloaded", 1, 1, f_bufloaded},
6660 {"bufname", 1, 1, f_bufname},
6661 {"bufnr", 1, 1, f_bufnr},
6662 {"bufwinnr", 1, 1, f_bufwinnr},
6663 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006664 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006665 {"call", 2, 3, f_call},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666 {"char2nr", 1, 1, f_char2nr},
6667 {"cindent", 1, 1, f_cindent},
6668 {"col", 1, 1, f_col},
6669 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006670 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006671 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 {"cscope_connection",0,3, f_cscope_connection},
6673 {"cursor", 2, 2, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006674 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675 {"delete", 1, 1, f_delete},
6676 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00006677 {"diff_filler", 1, 1, f_diff_filler},
6678 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006679 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006681 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682 {"eventhandler", 0, 0, f_eventhandler},
6683 {"executable", 1, 1, f_executable},
6684 {"exists", 1, 1, f_exists},
6685 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006686 {"extend", 2, 3, f_extend},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
6688 {"filereadable", 1, 1, f_filereadable},
6689 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006690 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006691 {"finddir", 1, 3, f_finddir},
6692 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693 {"fnamemodify", 2, 2, f_fnamemodify},
6694 {"foldclosed", 1, 1, f_foldclosed},
6695 {"foldclosedend", 1, 1, f_foldclosedend},
6696 {"foldlevel", 1, 1, f_foldlevel},
6697 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006698 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006700 {"function", 1, 1, f_function},
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006701 {"garbagecollect", 0, 0, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006702 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00006703 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704 {"getbufvar", 2, 2, f_getbufvar},
6705 {"getchar", 0, 1, f_getchar},
6706 {"getcharmod", 0, 0, f_getcharmod},
6707 {"getcmdline", 0, 0, f_getcmdline},
6708 {"getcmdpos", 0, 0, f_getcmdpos},
6709 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006710 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006711 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712 {"getfsize", 1, 1, f_getfsize},
6713 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006714 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006715 {"getline", 1, 2, f_getline},
Bram Moolenaar2641f772005-03-25 21:58:17 +00006716 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006717 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718 {"getregtype", 0, 1, f_getregtype},
6719 {"getwinposx", 0, 0, f_getwinposx},
6720 {"getwinposy", 0, 0, f_getwinposy},
6721 {"getwinvar", 2, 2, f_getwinvar},
6722 {"glob", 1, 1, f_glob},
6723 {"globpath", 2, 2, f_globpath},
6724 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006725 {"has_key", 2, 2, f_has_key},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726 {"hasmapto", 1, 2, f_hasmapto},
6727 {"highlightID", 1, 1, f_hlID}, /* obsolete */
6728 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
6729 {"histadd", 2, 2, f_histadd},
6730 {"histdel", 1, 2, f_histdel},
6731 {"histget", 1, 2, f_histget},
6732 {"histnr", 1, 1, f_histnr},
6733 {"hlID", 1, 1, f_hlID},
6734 {"hlexists", 1, 1, f_hlexists},
6735 {"hostname", 0, 0, f_hostname},
6736 {"iconv", 3, 3, f_iconv},
6737 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006738 {"index", 2, 4, f_index},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 {"input", 1, 2, f_input},
6740 {"inputdialog", 1, 3, f_inputdialog},
6741 {"inputrestore", 0, 0, f_inputrestore},
6742 {"inputsave", 0, 0, f_inputsave},
6743 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006744 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006746 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006747 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006748 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006749 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006751 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752 {"libcall", 3, 3, f_libcall},
6753 {"libcallnr", 3, 3, f_libcallnr},
6754 {"line", 1, 1, f_line},
6755 {"line2byte", 1, 1, f_line2byte},
6756 {"lispindent", 1, 1, f_lispindent},
6757 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006758 {"map", 2, 2, f_map},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759 {"maparg", 1, 2, f_maparg},
6760 {"mapcheck", 1, 2, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006761 {"match", 2, 4, f_match},
6762 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006763 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006764 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006765 {"max", 1, 1, f_max},
6766 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006767#ifdef vim_mkdir
6768 {"mkdir", 1, 3, f_mkdir},
6769#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770 {"mode", 0, 0, f_mode},
6771 {"nextnonblank", 1, 1, f_nextnonblank},
6772 {"nr2char", 1, 1, f_nr2char},
6773 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006774 {"printf", 2, 19, f_printf},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006775 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006776 {"readfile", 1, 3, f_readfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777 {"remote_expr", 2, 3, f_remote_expr},
6778 {"remote_foreground", 1, 1, f_remote_foreground},
6779 {"remote_peek", 1, 2, f_remote_peek},
6780 {"remote_read", 1, 1, f_remote_read},
6781 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006782 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006784 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006786 {"reverse", 1, 1, f_reverse},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787 {"search", 1, 2, f_search},
6788 {"searchpair", 3, 5, f_searchpair},
6789 {"server2client", 2, 2, f_server2client},
6790 {"serverlist", 0, 0, f_serverlist},
6791 {"setbufvar", 3, 3, f_setbufvar},
6792 {"setcmdpos", 1, 1, f_setcmdpos},
6793 {"setline", 2, 2, f_setline},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00006794 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795 {"setreg", 2, 3, f_setreg},
6796 {"setwinvar", 3, 3, f_setwinvar},
6797 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006798 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006799 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006800 {"spellbadword", 0, 0, f_spellbadword},
6801 {"spellsuggest", 1, 2, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006802 {"split", 1, 3, f_split},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803#ifdef HAVE_STRFTIME
6804 {"strftime", 1, 2, f_strftime},
6805#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00006806 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006807 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808 {"strlen", 1, 1, f_strlen},
6809 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00006810 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 {"strtrans", 1, 1, f_strtrans},
6812 {"submatch", 1, 1, f_submatch},
6813 {"substitute", 4, 4, f_substitute},
6814 {"synID", 3, 3, f_synID},
6815 {"synIDattr", 2, 3, f_synIDattr},
6816 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006817 {"system", 1, 2, f_system},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006818 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 {"tempname", 0, 0, f_tempname},
6820 {"tolower", 1, 1, f_tolower},
6821 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00006822 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006824 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825 {"virtcol", 1, 1, f_virtcol},
6826 {"visualmode", 0, 1, f_visualmode},
6827 {"winbufnr", 1, 1, f_winbufnr},
6828 {"wincol", 0, 0, f_wincol},
6829 {"winheight", 1, 1, f_winheight},
6830 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006831 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832 {"winrestcmd", 0, 0, f_winrestcmd},
6833 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006834 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835};
6836
6837#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6838
6839/*
6840 * Function given to ExpandGeneric() to obtain the list of internal
6841 * or user defined function names.
6842 */
6843 char_u *
6844get_function_name(xp, idx)
6845 expand_T *xp;
6846 int idx;
6847{
6848 static int intidx = -1;
6849 char_u *name;
6850
6851 if (idx == 0)
6852 intidx = -1;
6853 if (intidx < 0)
6854 {
6855 name = get_user_func_name(xp, idx);
6856 if (name != NULL)
6857 return name;
6858 }
6859 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
6860 {
6861 STRCPY(IObuff, functions[intidx].f_name);
6862 STRCAT(IObuff, "(");
6863 if (functions[intidx].f_max_argc == 0)
6864 STRCAT(IObuff, ")");
6865 return IObuff;
6866 }
6867
6868 return NULL;
6869}
6870
6871/*
6872 * Function given to ExpandGeneric() to obtain the list of internal or
6873 * user defined variable or function names.
6874 */
6875/*ARGSUSED*/
6876 char_u *
6877get_expr_name(xp, idx)
6878 expand_T *xp;
6879 int idx;
6880{
6881 static int intidx = -1;
6882 char_u *name;
6883
6884 if (idx == 0)
6885 intidx = -1;
6886 if (intidx < 0)
6887 {
6888 name = get_function_name(xp, idx);
6889 if (name != NULL)
6890 return name;
6891 }
6892 return get_user_var_name(xp, ++intidx);
6893}
6894
6895#endif /* FEAT_CMDL_COMPL */
6896
6897/*
6898 * Find internal function in table above.
6899 * Return index, or -1 if not found
6900 */
6901 static int
6902find_internal_func(name)
6903 char_u *name; /* name of the function */
6904{
6905 int first = 0;
6906 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
6907 int cmp;
6908 int x;
6909
6910 /*
6911 * Find the function name in the table. Binary search.
6912 */
6913 while (first <= last)
6914 {
6915 x = first + ((unsigned)(last - first) >> 1);
6916 cmp = STRCMP(name, functions[x].f_name);
6917 if (cmp < 0)
6918 last = x - 1;
6919 else if (cmp > 0)
6920 first = x + 1;
6921 else
6922 return x;
6923 }
6924 return -1;
6925}
6926
6927/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006928 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
6929 * name it contains, otherwise return "name".
6930 */
6931 static char_u *
6932deref_func_name(name, lenp)
6933 char_u *name;
6934 int *lenp;
6935{
Bram Moolenaar33570922005-01-25 22:26:29 +00006936 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006937 int cc;
6938
6939 cc = name[*lenp];
6940 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00006941 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006942 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00006943 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006944 {
Bram Moolenaar33570922005-01-25 22:26:29 +00006945 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006946 {
6947 *lenp = 0;
6948 return (char_u *)""; /* just in case */
6949 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006950 *lenp = STRLEN(v->di_tv.vval.v_string);
6951 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006952 }
6953
6954 return name;
6955}
6956
6957/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 * Allocate a variable for the result of a function.
6959 * Return OK or FAIL.
6960 */
6961 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00006962get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
6963 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 char_u *name; /* name of the function */
6965 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00006966 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967 char_u **arg; /* argument, pointing to the '(' */
6968 linenr_T firstline; /* first line of range */
6969 linenr_T lastline; /* last line of range */
6970 int *doesrange; /* return: function handled range */
6971 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00006972 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973{
6974 char_u *argp;
6975 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00006976 typval_T argvars[MAX_FUNC_ARGS]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 int argcount = 0; /* number of arguments found */
6978
6979 /*
6980 * Get the arguments.
6981 */
6982 argp = *arg;
6983 while (argcount < MAX_FUNC_ARGS)
6984 {
6985 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
6986 if (*argp == ')' || *argp == ',' || *argp == NUL)
6987 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
6989 {
6990 ret = FAIL;
6991 break;
6992 }
6993 ++argcount;
6994 if (*argp != ',')
6995 break;
6996 }
6997 if (*argp == ')')
6998 ++argp;
6999 else
7000 ret = FAIL;
7001
7002 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007003 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007004 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007006 {
7007 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007008 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007009 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007010 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012
7013 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007014 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015
7016 *arg = skipwhite(argp);
7017 return ret;
7018}
7019
7020
7021/*
7022 * Call a function with its resolved parameters
7023 * Return OK or FAIL.
7024 */
7025 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007026call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007027 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028 char_u *name; /* name of the function */
7029 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007030 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 int argcount; /* number of "argvars" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007032 typval_T *argvars; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 linenr_T firstline; /* first line of range */
7034 linenr_T lastline; /* last line of range */
7035 int *doesrange; /* return: function handled range */
7036 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007037 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038{
7039 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040#define ERROR_UNKNOWN 0
7041#define ERROR_TOOMANY 1
7042#define ERROR_TOOFEW 2
7043#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007044#define ERROR_DICT 4
7045#define ERROR_NONE 5
7046#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047 int error = ERROR_NONE;
7048 int i;
7049 int llen;
7050 ufunc_T *fp;
7051 int cc;
7052#define FLEN_FIXED 40
7053 char_u fname_buf[FLEN_FIXED + 1];
7054 char_u *fname;
7055
7056 /*
7057 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7058 * Change <SNR>123_name() to K_SNR 123_name().
7059 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7060 */
7061 cc = name[len];
7062 name[len] = NUL;
7063 llen = eval_fname_script(name);
7064 if (llen > 0)
7065 {
7066 fname_buf[0] = K_SPECIAL;
7067 fname_buf[1] = KS_EXTRA;
7068 fname_buf[2] = (int)KE_SNR;
7069 i = 3;
7070 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7071 {
7072 if (current_SID <= 0)
7073 error = ERROR_SCRIPT;
7074 else
7075 {
7076 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7077 i = (int)STRLEN(fname_buf);
7078 }
7079 }
7080 if (i + STRLEN(name + llen) < FLEN_FIXED)
7081 {
7082 STRCPY(fname_buf + i, name + llen);
7083 fname = fname_buf;
7084 }
7085 else
7086 {
7087 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7088 if (fname == NULL)
7089 error = ERROR_OTHER;
7090 else
7091 {
7092 mch_memmove(fname, fname_buf, (size_t)i);
7093 STRCPY(fname + i, name + llen);
7094 }
7095 }
7096 }
7097 else
7098 fname = name;
7099
7100 *doesrange = FALSE;
7101
7102
7103 /* execute the function if no errors detected and executing */
7104 if (evaluate && error == ERROR_NONE)
7105 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007106 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107 error = ERROR_UNKNOWN;
7108
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007109 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110 {
7111 /*
7112 * User defined function.
7113 */
7114 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007115
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007117 /* Trigger FuncUndefined event, may load the function. */
7118 if (fp == NULL
7119 && apply_autocmds(EVENT_FUNCUNDEFINED,
7120 fname, fname, TRUE, NULL)
7121 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007123 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124 fp = find_func(fname);
7125 }
7126#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007127 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007128 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007129 {
7130 /* loaded a package, search for the function again */
7131 fp = find_func(fname);
7132 }
7133
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 if (fp != NULL)
7135 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007136 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007138 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007140 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007142 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007143 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144 else
7145 {
7146 /*
7147 * Call the user function.
7148 * Save and restore search patterns, script variables and
7149 * redo buffer.
7150 */
7151 save_search_patterns();
7152 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007153 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007154 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007155 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007156 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7157 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7158 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007159 /* Function was unreferenced while being used, free it
7160 * now. */
7161 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162 restoreRedobuff();
7163 restore_search_patterns();
7164 error = ERROR_NONE;
7165 }
7166 }
7167 }
7168 else
7169 {
7170 /*
7171 * Find the function name in the table, call its implementation.
7172 */
7173 i = find_internal_func(fname);
7174 if (i >= 0)
7175 {
7176 if (argcount < functions[i].f_min_argc)
7177 error = ERROR_TOOFEW;
7178 else if (argcount > functions[i].f_max_argc)
7179 error = ERROR_TOOMANY;
7180 else
7181 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007182 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007183 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184 error = ERROR_NONE;
7185 }
7186 }
7187 }
7188 /*
7189 * The function call (or "FuncUndefined" autocommand sequence) might
7190 * have been aborted by an error, an interrupt, or an explicitly thrown
7191 * exception that has not been caught so far. This situation can be
7192 * tested for by calling aborting(). For an error in an internal
7193 * function or for the "E132" error in call_user_func(), however, the
7194 * throw point at which the "force_abort" flag (temporarily reset by
7195 * emsg()) is normally updated has not been reached yet. We need to
7196 * update that flag first to make aborting() reliable.
7197 */
7198 update_force_abort();
7199 }
7200 if (error == ERROR_NONE)
7201 ret = OK;
7202
7203 /*
7204 * Report an error unless the argument evaluation or function call has been
7205 * cancelled due to an aborting error, an interrupt, or an exception.
7206 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007207 if (!aborting())
7208 {
7209 switch (error)
7210 {
7211 case ERROR_UNKNOWN:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007212 emsg_funcname("E117: Unknown function: %s", name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007213 break;
7214 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007215 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007216 break;
7217 case ERROR_TOOFEW:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007218 emsg_funcname("E119: Not enough arguments for function: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007219 name);
7220 break;
7221 case ERROR_SCRIPT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007222 emsg_funcname("E120: Using <SID> not in a script context: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007223 name);
7224 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007225 case ERROR_DICT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007226 emsg_funcname("E725: Calling dict function without Dictionary: %s",
Bram Moolenaare9a41262005-01-15 22:18:47 +00007227 name);
7228 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007229 }
7230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231
7232 name[len] = cc;
7233 if (fname != name && fname != fname_buf)
7234 vim_free(fname);
7235
7236 return ret;
7237}
7238
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007239/*
7240 * Give an error message with a function name. Handle <SNR> things.
7241 */
7242 static void
7243emsg_funcname(msg, name)
7244 char *msg;
7245 char_u *name;
7246{
7247 char_u *p;
7248
7249 if (*name == K_SPECIAL)
7250 p = concat_str((char_u *)"<SNR>", name + 3);
7251 else
7252 p = name;
7253 EMSG2(_(msg), p);
7254 if (p != name)
7255 vim_free(p);
7256}
7257
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258/*********************************************
7259 * Implementation of the built-in functions
7260 */
7261
7262/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007263 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264 */
7265 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007266f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007267 typval_T *argvars;
7268 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269{
Bram Moolenaar33570922005-01-25 22:26:29 +00007270 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007272 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007273 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007275 if ((l = argvars[0].vval.v_list) != NULL
7276 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7277 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007278 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007279 }
7280 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007281 EMSG(_(e_listreq));
7282}
7283
7284/*
7285 * "append(lnum, string/list)" function
7286 */
7287 static void
7288f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007289 typval_T *argvars;
7290 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007291{
7292 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007293 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007294 list_T *l = NULL;
7295 listitem_T *li = NULL;
7296 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007297 long added = 0;
7298
Bram Moolenaar0d660222005-01-07 21:51:51 +00007299 lnum = get_tv_lnum(argvars);
7300 if (lnum >= 0
7301 && lnum <= curbuf->b_ml.ml_line_count
7302 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007303 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007304 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007305 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007306 l = argvars[1].vval.v_list;
7307 if (l == NULL)
7308 return;
7309 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007310 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007311 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007312 for (;;)
7313 {
7314 if (l == NULL)
7315 tv = &argvars[1]; /* append a string */
7316 else if (li == NULL)
7317 break; /* end of list */
7318 else
7319 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007320 line = get_tv_string_chk(tv);
7321 if (line == NULL) /* type error */
7322 {
7323 rettv->vval.v_number = 1; /* Failed */
7324 break;
7325 }
7326 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007327 ++added;
7328 if (l == NULL)
7329 break;
7330 li = li->li_next;
7331 }
7332
7333 appended_lines_mark(lnum, added);
7334 if (curwin->w_cursor.lnum > lnum)
7335 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007337 else
7338 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339}
7340
7341/*
7342 * "argc()" function
7343 */
7344/* ARGSUSED */
7345 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007346f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007347 typval_T *argvars;
7348 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007350 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351}
7352
7353/*
7354 * "argidx()" function
7355 */
7356/* ARGSUSED */
7357 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007358f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007359 typval_T *argvars;
7360 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007362 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363}
7364
7365/*
7366 * "argv(nr)" function
7367 */
7368 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007369f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007370 typval_T *argvars;
7371 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372{
7373 int idx;
7374
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007375 idx = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376 if (idx >= 0 && idx < ARGCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007377 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007379 rettv->vval.v_string = NULL;
7380 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381}
7382
7383/*
7384 * "browse(save, title, initdir, default)" function
7385 */
7386/* ARGSUSED */
7387 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007388f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007389 typval_T *argvars;
7390 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391{
7392#ifdef FEAT_BROWSE
7393 int save;
7394 char_u *title;
7395 char_u *initdir;
7396 char_u *defname;
7397 char_u buf[NUMBUFLEN];
7398 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007399 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007401 save = get_tv_number_chk(&argvars[0], &error);
7402 title = get_tv_string_chk(&argvars[1]);
7403 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7404 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007406 if (error || title == NULL || initdir == NULL || defname == NULL)
7407 rettv->vval.v_string = NULL;
7408 else
7409 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007410 do_browse(save ? BROWSE_SAVE : 0,
7411 title, defname, NULL, initdir, NULL, curbuf);
7412#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007413 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007414#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007415 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007416}
7417
7418/*
7419 * "browsedir(title, initdir)" function
7420 */
7421/* ARGSUSED */
7422 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007423f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007424 typval_T *argvars;
7425 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007426{
7427#ifdef FEAT_BROWSE
7428 char_u *title;
7429 char_u *initdir;
7430 char_u buf[NUMBUFLEN];
7431
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007432 title = get_tv_string_chk(&argvars[0]);
7433 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007434
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007435 if (title == NULL || initdir == NULL)
7436 rettv->vval.v_string = NULL;
7437 else
7438 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007439 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007441 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007443 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444}
7445
Bram Moolenaar33570922005-01-25 22:26:29 +00007446static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007447
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448/*
7449 * Find a buffer by number or exact name.
7450 */
7451 static buf_T *
7452find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007453 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454{
7455 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007457 if (avar->v_type == VAR_NUMBER)
7458 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007459 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007461 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007462 if (buf == NULL)
7463 {
7464 /* No full path name match, try a match with a URL or a "nofile"
7465 * buffer, these don't use the full path. */
7466 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7467 if (buf->b_fname != NULL
7468 && (path_with_url(buf->b_fname)
7469#ifdef FEAT_QUICKFIX
7470 || bt_nofile(buf)
7471#endif
7472 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007473 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007474 break;
7475 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476 }
7477 return buf;
7478}
7479
7480/*
7481 * "bufexists(expr)" function
7482 */
7483 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007484f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007485 typval_T *argvars;
7486 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007488 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489}
7490
7491/*
7492 * "buflisted(expr)" function
7493 */
7494 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007495f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007496 typval_T *argvars;
7497 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498{
7499 buf_T *buf;
7500
7501 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007502 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503}
7504
7505/*
7506 * "bufloaded(expr)" function
7507 */
7508 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007509f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007510 typval_T *argvars;
7511 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512{
7513 buf_T *buf;
7514
7515 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007516 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517}
7518
Bram Moolenaar33570922005-01-25 22:26:29 +00007519static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007520
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521/*
7522 * Get buffer by number or pattern.
7523 */
7524 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007525get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007526 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007528 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529 int save_magic;
7530 char_u *save_cpo;
7531 buf_T *buf;
7532
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007533 if (tv->v_type == VAR_NUMBER)
7534 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007535 if (tv->v_type != VAR_STRING)
7536 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537 if (name == NULL || *name == NUL)
7538 return curbuf;
7539 if (name[0] == '$' && name[1] == NUL)
7540 return lastbuf;
7541
7542 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7543 save_magic = p_magic;
7544 p_magic = TRUE;
7545 save_cpo = p_cpo;
7546 p_cpo = (char_u *)"";
7547
7548 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
7549 TRUE, FALSE));
7550
7551 p_magic = save_magic;
7552 p_cpo = save_cpo;
7553
7554 /* If not found, try expanding the name, like done for bufexists(). */
7555 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007556 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557
7558 return buf;
7559}
7560
7561/*
7562 * "bufname(expr)" function
7563 */
7564 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007565f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007566 typval_T *argvars;
7567 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568{
7569 buf_T *buf;
7570
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007571 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007573 buf = get_buf_tv(&argvars[0]);
7574 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007576 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007577 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007578 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579 --emsg_off;
7580}
7581
7582/*
7583 * "bufnr(expr)" function
7584 */
7585 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007586f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007587 typval_T *argvars;
7588 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589{
7590 buf_T *buf;
7591
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007592 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007594 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007596 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007598 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599 --emsg_off;
7600}
7601
7602/*
7603 * "bufwinnr(nr)" function
7604 */
7605 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007606f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007607 typval_T *argvars;
7608 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609{
7610#ifdef FEAT_WINDOWS
7611 win_T *wp;
7612 int winnr = 0;
7613#endif
7614 buf_T *buf;
7615
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007616 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007618 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619#ifdef FEAT_WINDOWS
7620 for (wp = firstwin; wp; wp = wp->w_next)
7621 {
7622 ++winnr;
7623 if (wp->w_buffer == buf)
7624 break;
7625 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007626 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007628 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629#endif
7630 --emsg_off;
7631}
7632
7633/*
7634 * "byte2line(byte)" function
7635 */
7636/*ARGSUSED*/
7637 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007638f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007639 typval_T *argvars;
7640 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641{
7642#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007643 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644#else
7645 long boff = 0;
7646
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007647 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007649 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007651 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652 (linenr_T)0, &boff);
7653#endif
7654}
7655
7656/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007657 * "byteidx()" function
7658 */
7659/*ARGSUSED*/
7660 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007661f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007662 typval_T *argvars;
7663 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007664{
7665#ifdef FEAT_MBYTE
7666 char_u *t;
7667#endif
7668 char_u *str;
7669 long idx;
7670
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007671 str = get_tv_string_chk(&argvars[0]);
7672 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007673 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007674 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007675 return;
7676
7677#ifdef FEAT_MBYTE
7678 t = str;
7679 for ( ; idx > 0; idx--)
7680 {
7681 if (*t == NUL) /* EOL reached */
7682 return;
7683 t += mb_ptr2len_check(t);
7684 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007685 rettv->vval.v_number = t - str;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007686#else
7687 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007688 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007689#endif
7690}
7691
7692/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007693 * "call(func, arglist)" function
7694 */
7695 static void
7696f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007697 typval_T *argvars;
7698 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007699{
7700 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00007701 typval_T argv[MAX_FUNC_ARGS];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007702 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007703 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007704 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00007705 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007706
7707 rettv->vval.v_number = 0;
7708 if (argvars[1].v_type != VAR_LIST)
7709 {
7710 EMSG(_(e_listreq));
7711 return;
7712 }
7713 if (argvars[1].vval.v_list == NULL)
7714 return;
7715
7716 if (argvars[0].v_type == VAR_FUNC)
7717 func = argvars[0].vval.v_string;
7718 else
7719 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007720 if (*func == NUL)
7721 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007722
Bram Moolenaare9a41262005-01-15 22:18:47 +00007723 if (argvars[2].v_type != VAR_UNKNOWN)
7724 {
7725 if (argvars[2].v_type != VAR_DICT)
7726 {
7727 EMSG(_(e_dictreq));
7728 return;
7729 }
7730 selfdict = argvars[2].vval.v_dict;
7731 }
7732
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007733 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
7734 item = item->li_next)
7735 {
7736 if (argc == MAX_FUNC_ARGS)
7737 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007738 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007739 break;
7740 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007741 /* Make a copy of each argument. This is needed to be able to set
7742 * v_lock to VAR_FIXED in the copy without changing the original list.
7743 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007744 copy_tv(&item->li_tv, &argv[argc++]);
7745 }
7746
7747 if (item == NULL)
7748 (void)call_func(func, STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007749 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
7750 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007751
7752 /* Free the arguments. */
7753 while (argc > 0)
7754 clear_tv(&argv[--argc]);
7755}
7756
7757/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758 * "char2nr(string)" function
7759 */
7760 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007761f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007762 typval_T *argvars;
7763 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007764{
7765#ifdef FEAT_MBYTE
7766 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007767 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768 else
7769#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007770 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771}
7772
7773/*
7774 * "cindent(lnum)" function
7775 */
7776 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007777f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007778 typval_T *argvars;
7779 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780{
7781#ifdef FEAT_CINDENT
7782 pos_T pos;
7783 linenr_T lnum;
7784
7785 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007786 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7788 {
7789 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007790 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791 curwin->w_cursor = pos;
7792 }
7793 else
7794#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007795 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796}
7797
7798/*
7799 * "col(string)" function
7800 */
7801 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007802f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007803 typval_T *argvars;
7804 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805{
7806 colnr_T col = 0;
7807 pos_T *fp;
7808
7809 fp = var2fpos(&argvars[0], FALSE);
7810 if (fp != NULL)
7811 {
7812 if (fp->col == MAXCOL)
7813 {
7814 /* '> can be MAXCOL, get the length of the line then */
7815 if (fp->lnum <= curbuf->b_ml.ml_line_count)
7816 col = STRLEN(ml_get(fp->lnum)) + 1;
7817 else
7818 col = MAXCOL;
7819 }
7820 else
7821 {
7822 col = fp->col + 1;
7823#ifdef FEAT_VIRTUALEDIT
7824 /* col(".") when the cursor is on the NUL at the end of the line
7825 * because of "coladd" can be seen as an extra column. */
7826 if (virtual_active() && fp == &curwin->w_cursor)
7827 {
7828 char_u *p = ml_get_cursor();
7829
7830 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
7831 curwin->w_virtcol - curwin->w_cursor.coladd))
7832 {
7833# ifdef FEAT_MBYTE
7834 int l;
7835
7836 if (*p != NUL && p[(l = (*mb_ptr2len_check)(p))] == NUL)
7837 col += l;
7838# else
7839 if (*p != NUL && p[1] == NUL)
7840 ++col;
7841# endif
7842 }
7843 }
7844#endif
7845 }
7846 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007847 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848}
7849
7850/*
7851 * "confirm(message, buttons[, default [, type]])" function
7852 */
7853/*ARGSUSED*/
7854 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007855f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007856 typval_T *argvars;
7857 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858{
7859#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
7860 char_u *message;
7861 char_u *buttons = NULL;
7862 char_u buf[NUMBUFLEN];
7863 char_u buf2[NUMBUFLEN];
7864 int def = 1;
7865 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007866 char_u *typestr;
7867 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007869 message = get_tv_string_chk(&argvars[0]);
7870 if (message == NULL)
7871 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007872 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007874 buttons = get_tv_string_buf_chk(&argvars[1], buf);
7875 if (buttons == NULL)
7876 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007877 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007879 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007880 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007882 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
7883 if (typestr == NULL)
7884 error = TRUE;
7885 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007887 switch (TOUPPER_ASC(*typestr))
7888 {
7889 case 'E': type = VIM_ERROR; break;
7890 case 'Q': type = VIM_QUESTION; break;
7891 case 'I': type = VIM_INFO; break;
7892 case 'W': type = VIM_WARNING; break;
7893 case 'G': type = VIM_GENERIC; break;
7894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 }
7896 }
7897 }
7898 }
7899
7900 if (buttons == NULL || *buttons == NUL)
7901 buttons = (char_u *)_("&Ok");
7902
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007903 if (error)
7904 rettv->vval.v_number = 0;
7905 else
7906 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907 def, NULL);
7908#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007909 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910#endif
7911}
7912
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007913/*
7914 * "copy()" function
7915 */
7916 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007917f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007918 typval_T *argvars;
7919 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007920{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007921 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007922}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923
7924/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007925 * "count()" function
7926 */
7927 static void
7928f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007929 typval_T *argvars;
7930 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007931{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007932 long n = 0;
7933 int ic = FALSE;
7934
Bram Moolenaare9a41262005-01-15 22:18:47 +00007935 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007936 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007937 listitem_T *li;
7938 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007939 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007940
Bram Moolenaare9a41262005-01-15 22:18:47 +00007941 if ((l = argvars[0].vval.v_list) != NULL)
7942 {
7943 li = l->lv_first;
7944 if (argvars[2].v_type != VAR_UNKNOWN)
7945 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007946 int error = FALSE;
7947
7948 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007949 if (argvars[3].v_type != VAR_UNKNOWN)
7950 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007951 idx = get_tv_number_chk(&argvars[3], &error);
7952 if (!error)
7953 {
7954 li = list_find(l, idx);
7955 if (li == NULL)
7956 EMSGN(_(e_listidx), idx);
7957 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007958 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007959 if (error)
7960 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007961 }
7962
7963 for ( ; li != NULL; li = li->li_next)
7964 if (tv_equal(&li->li_tv, &argvars[1], ic))
7965 ++n;
7966 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007967 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007968 else if (argvars[0].v_type == VAR_DICT)
7969 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007970 int todo;
7971 dict_T *d;
7972 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007973
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007974 if ((d = argvars[0].vval.v_dict) != NULL)
7975 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007976 int error = FALSE;
7977
Bram Moolenaare9a41262005-01-15 22:18:47 +00007978 if (argvars[2].v_type != VAR_UNKNOWN)
7979 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007980 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007981 if (argvars[3].v_type != VAR_UNKNOWN)
7982 EMSG(_(e_invarg));
7983 }
7984
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007985 todo = error ? 0 : d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007986 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007987 {
7988 if (!HASHITEM_EMPTY(hi))
7989 {
7990 --todo;
7991 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
7992 ++n;
7993 }
7994 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007995 }
7996 }
7997 else
7998 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007999 rettv->vval.v_number = n;
8000}
8001
8002/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8004 *
8005 * Checks the existence of a cscope connection.
8006 */
8007/*ARGSUSED*/
8008 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008009f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008010 typval_T *argvars;
8011 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012{
8013#ifdef FEAT_CSCOPE
8014 int num = 0;
8015 char_u *dbpath = NULL;
8016 char_u *prepend = NULL;
8017 char_u buf[NUMBUFLEN];
8018
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008019 if (argvars[0].v_type != VAR_UNKNOWN
8020 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008022 num = (int)get_tv_number(&argvars[0]);
8023 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008024 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008025 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008026 }
8027
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008028 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008030 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031#endif
8032}
8033
8034/*
8035 * "cursor(lnum, col)" function
8036 *
8037 * Moves the cursor to the specified line and column
8038 */
8039/*ARGSUSED*/
8040 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008041f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008042 typval_T *argvars;
8043 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044{
8045 long line, col;
8046
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008047 line = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008048 col = get_tv_number_chk(&argvars[1], NULL);
8049 if (line < 0 || col < 0)
8050 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051 if (line > 0)
8052 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053 if (col > 0)
8054 curwin->w_cursor.col = col - 1;
8055#ifdef FEAT_VIRTUALEDIT
8056 curwin->w_cursor.coladd = 0;
8057#endif
8058
8059 /* Make sure the cursor is in a valid position. */
8060 check_cursor();
8061#ifdef FEAT_MBYTE
8062 /* Correct cursor for multi-byte character. */
8063 if (has_mbyte)
8064 mb_adjust_cursor();
8065#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008066
8067 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068}
8069
8070/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008071 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072 */
8073 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008074f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008075 typval_T *argvars;
8076 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008078 int noref = 0;
8079
8080 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008081 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008082 if (noref < 0 || noref > 1)
8083 EMSG(_(e_invarg));
8084 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008085 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086}
8087
8088/*
8089 * "delete()" function
8090 */
8091 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008092f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008093 typval_T *argvars;
8094 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095{
8096 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008097 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008099 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100}
8101
8102/*
8103 * "did_filetype()" function
8104 */
8105/*ARGSUSED*/
8106 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008107f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008108 typval_T *argvars;
8109 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110{
8111#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008112 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008114 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115#endif
8116}
8117
8118/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008119 * "diff_filler()" function
8120 */
8121/*ARGSUSED*/
8122 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008123f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008124 typval_T *argvars;
8125 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008126{
8127#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008128 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008129#endif
8130}
8131
8132/*
8133 * "diff_hlID()" function
8134 */
8135/*ARGSUSED*/
8136 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008137f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008138 typval_T *argvars;
8139 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008140{
8141#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008142 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008143 static linenr_T prev_lnum = 0;
8144 static int changedtick = 0;
8145 static int fnum = 0;
8146 static int change_start = 0;
8147 static int change_end = 0;
8148 static enum hlf_value hlID = 0;
8149 int filler_lines;
8150 int col;
8151
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008152 if (lnum < 0) /* ignore type error in {lnum} arg */
8153 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008154 if (lnum != prev_lnum
8155 || changedtick != curbuf->b_changedtick
8156 || fnum != curbuf->b_fnum)
8157 {
8158 /* New line, buffer, change: need to get the values. */
8159 filler_lines = diff_check(curwin, lnum);
8160 if (filler_lines < 0)
8161 {
8162 if (filler_lines == -1)
8163 {
8164 change_start = MAXCOL;
8165 change_end = -1;
8166 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8167 hlID = HLF_ADD; /* added line */
8168 else
8169 hlID = HLF_CHD; /* changed line */
8170 }
8171 else
8172 hlID = HLF_ADD; /* added line */
8173 }
8174 else
8175 hlID = (enum hlf_value)0;
8176 prev_lnum = lnum;
8177 changedtick = curbuf->b_changedtick;
8178 fnum = curbuf->b_fnum;
8179 }
8180
8181 if (hlID == HLF_CHD || hlID == HLF_TXD)
8182 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008183 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008184 if (col >= change_start && col <= change_end)
8185 hlID = HLF_TXD; /* changed text */
8186 else
8187 hlID = HLF_CHD; /* changed line */
8188 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008189 rettv->vval.v_number = hlID == (enum hlf_value)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008190#endif
8191}
8192
8193/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008194 * "empty({expr})" function
8195 */
8196 static void
8197f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008198 typval_T *argvars;
8199 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008200{
8201 int n;
8202
8203 switch (argvars[0].v_type)
8204 {
8205 case VAR_STRING:
8206 case VAR_FUNC:
8207 n = argvars[0].vval.v_string == NULL
8208 || *argvars[0].vval.v_string == NUL;
8209 break;
8210 case VAR_NUMBER:
8211 n = argvars[0].vval.v_number == 0;
8212 break;
8213 case VAR_LIST:
8214 n = argvars[0].vval.v_list == NULL
8215 || argvars[0].vval.v_list->lv_first == NULL;
8216 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008217 case VAR_DICT:
8218 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008219 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008220 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008221 default:
8222 EMSG2(_(e_intern2), "f_empty()");
8223 n = 0;
8224 }
8225
8226 rettv->vval.v_number = n;
8227}
8228
8229/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230 * "escape({string}, {chars})" function
8231 */
8232 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008233f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008234 typval_T *argvars;
8235 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236{
8237 char_u buf[NUMBUFLEN];
8238
Bram Moolenaar758711c2005-02-02 23:11:38 +00008239 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8240 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008241 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242}
8243
8244/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008245 * "eval()" function
8246 */
8247/*ARGSUSED*/
8248 static void
8249f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008250 typval_T *argvars;
8251 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008252{
8253 char_u *s;
8254
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008255 s = get_tv_string_chk(&argvars[0]);
8256 if (s != NULL)
8257 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008258
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008259 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8260 {
8261 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008262 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008263 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008264 else if (*s != NUL)
8265 EMSG(_(e_trailing));
8266}
8267
8268/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269 * "eventhandler()" function
8270 */
8271/*ARGSUSED*/
8272 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008273f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008274 typval_T *argvars;
8275 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008277 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008278}
8279
8280/*
8281 * "executable()" function
8282 */
8283 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008284f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008285 typval_T *argvars;
8286 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008288 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289}
8290
8291/*
8292 * "exists()" function
8293 */
8294 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008295f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008296 typval_T *argvars;
8297 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298{
8299 char_u *p;
8300 char_u *name;
8301 int n = FALSE;
8302 int len = 0;
8303
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008304 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305 if (*p == '$') /* environment variable */
8306 {
8307 /* first try "normal" environment variables (fast) */
8308 if (mch_getenv(p + 1) != NULL)
8309 n = TRUE;
8310 else
8311 {
8312 /* try expanding things like $VIM and ${HOME} */
8313 p = expand_env_save(p);
8314 if (p != NULL && *p != '$')
8315 n = TRUE;
8316 vim_free(p);
8317 }
8318 }
8319 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008320 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 else if (*p == '*') /* internal or user defined function */
8322 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008323 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324 }
8325 else if (*p == ':')
8326 {
8327 n = cmd_exists(p + 1);
8328 }
8329 else if (*p == '#')
8330 {
8331#ifdef FEAT_AUTOCMD
8332 name = p + 1;
8333 p = vim_strchr(name, '#');
8334 if (p != NULL)
8335 n = au_exists(name, p, p + 1);
8336 else
8337 n = au_exists(name, name + STRLEN(name), NULL);
8338#endif
8339 }
8340 else /* internal variable */
8341 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008342 char_u *tofree;
8343 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008345 /* get_name_len() takes care of expanding curly braces */
8346 name = p;
8347 len = get_name_len(&p, &tofree, TRUE, FALSE);
8348 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008350 if (tofree != NULL)
8351 name = tofree;
8352 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8353 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008354 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008355 /* handle d.key, l[idx], f(expr) */
8356 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8357 if (n)
8358 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359 }
8360 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008362 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363 }
8364
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008365 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366}
8367
8368/*
8369 * "expand()" function
8370 */
8371 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008372f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008373 typval_T *argvars;
8374 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375{
8376 char_u *s;
8377 int len;
8378 char_u *errormsg;
8379 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8380 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008381 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008383 rettv->v_type = VAR_STRING;
8384 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385 if (*s == '%' || *s == '#' || *s == '<')
8386 {
8387 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008388 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389 --emsg_off;
8390 }
8391 else
8392 {
8393 /* When the optional second argument is non-zero, don't remove matches
8394 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008395 if (argvars[1].v_type != VAR_UNKNOWN
8396 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008398 if (!error)
8399 {
8400 ExpandInit(&xpc);
8401 xpc.xp_context = EXPAND_FILES;
8402 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
8403 ExpandCleanup(&xpc);
8404 }
8405 else
8406 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407 }
8408}
8409
8410/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008411 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00008412 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008413 */
8414 static void
8415f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008416 typval_T *argvars;
8417 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008418{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008419 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008420 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008421 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008422 list_T *l1, *l2;
8423 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008424 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008425 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008426
Bram Moolenaare9a41262005-01-15 22:18:47 +00008427 l1 = argvars[0].vval.v_list;
8428 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008429 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
8430 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008431 {
8432 if (argvars[2].v_type != VAR_UNKNOWN)
8433 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008434 before = get_tv_number_chk(&argvars[2], &error);
8435 if (error)
8436 return; /* type error; errmsg already given */
8437
Bram Moolenaar758711c2005-02-02 23:11:38 +00008438 if (before == l1->lv_len)
8439 item = NULL;
8440 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008441 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00008442 item = list_find(l1, before);
8443 if (item == NULL)
8444 {
8445 EMSGN(_(e_listidx), before);
8446 return;
8447 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008448 }
8449 }
8450 else
8451 item = NULL;
8452 list_extend(l1, l2, item);
8453
Bram Moolenaare9a41262005-01-15 22:18:47 +00008454 copy_tv(&argvars[0], rettv);
8455 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008456 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008457 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
8458 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008459 dict_T *d1, *d2;
8460 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008461 char_u *action;
8462 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00008463 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008464 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008465
8466 d1 = argvars[0].vval.v_dict;
8467 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008468 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
8469 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008470 {
8471 /* Check the third argument. */
8472 if (argvars[2].v_type != VAR_UNKNOWN)
8473 {
8474 static char *(av[]) = {"keep", "force", "error"};
8475
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008476 action = get_tv_string_chk(&argvars[2]);
8477 if (action == NULL)
8478 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008479 for (i = 0; i < 3; ++i)
8480 if (STRCMP(action, av[i]) == 0)
8481 break;
8482 if (i == 3)
8483 {
8484 EMSGN(_(e_invarg2), action);
8485 return;
8486 }
8487 }
8488 else
8489 action = (char_u *)"force";
8490
8491 /* Go over all entries in the second dict and add them to the
8492 * first dict. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008493 todo = d2->dv_hashtab.ht_used;
8494 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008495 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008496 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008497 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008498 --todo;
8499 di1 = dict_find(d1, hi2->hi_key, -1);
8500 if (di1 == NULL)
8501 {
8502 di1 = dictitem_copy(HI2DI(hi2));
8503 if (di1 != NULL && dict_add(d1, di1) == FAIL)
8504 dictitem_free(di1);
8505 }
8506 else if (*action == 'e')
8507 {
8508 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
8509 break;
8510 }
8511 else if (*action == 'f')
8512 {
8513 clear_tv(&di1->di_tv);
8514 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
8515 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008516 }
8517 }
8518
Bram Moolenaare9a41262005-01-15 22:18:47 +00008519 copy_tv(&argvars[0], rettv);
8520 }
8521 }
8522 else
8523 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008524}
8525
8526/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527 * "filereadable()" function
8528 */
8529 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008530f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008531 typval_T *argvars;
8532 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533{
8534 FILE *fd;
8535 char_u *p;
8536 int n;
8537
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008538 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
8540 {
8541 n = TRUE;
8542 fclose(fd);
8543 }
8544 else
8545 n = FALSE;
8546
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008547 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548}
8549
8550/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008551 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552 * rights to write into.
8553 */
8554 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008555f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008556 typval_T *argvars;
8557 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008559 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008560}
8561
Bram Moolenaar33570922005-01-25 22:26:29 +00008562static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008563
8564 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008565findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00008566 typval_T *argvars;
8567 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008568 int dir;
8569{
8570#ifdef FEAT_SEARCHPATH
8571 char_u *fname;
8572 char_u *fresult = NULL;
8573 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
8574 char_u *p;
8575 char_u pathbuf[NUMBUFLEN];
8576 int count = 1;
8577 int first = TRUE;
8578
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008579 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008580
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008581 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008582 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008583 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
8584 if (p == NULL)
8585 count = -1; /* error */
8586 else
8587 {
8588 if (*p != NUL)
8589 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008590
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008591 if (argvars[2].v_type != VAR_UNKNOWN)
8592 count = get_tv_number_chk(&argvars[2], NULL); /* -1: error */
8593 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008594 }
8595
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008596 if (*fname != NUL && count >= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008597 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008598 do
8599 {
8600 vim_free(fresult);
8601 fresult = find_file_in_path_option(first ? fname : NULL,
8602 first ? (int)STRLEN(fname) : 0,
8603 0, first, path, dir, NULL);
8604 first = FALSE;
8605 } while (--count > 0 && fresult != NULL);
8606 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008607
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008608 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008609#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008610 rettv->vval.v_string = NULL;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008611#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008612 rettv->v_type = VAR_STRING;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008613}
8614
Bram Moolenaar33570922005-01-25 22:26:29 +00008615static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
8616static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008617
8618/*
8619 * Implementation of map() and filter().
8620 */
8621 static void
8622filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00008623 typval_T *argvars;
8624 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008625 int map;
8626{
8627 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00008628 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00008629 listitem_T *li, *nli;
8630 list_T *l = NULL;
8631 dictitem_T *di;
8632 hashtab_T *ht;
8633 hashitem_T *hi;
8634 dict_T *d = NULL;
8635 typval_T save_val;
8636 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008637 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008638 int todo;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008639 char_u *msg = map ? (char_u *)"map()" : (char_u *)"filter()";
8640
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008641
8642 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008643 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008644 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008645 if ((l = argvars[0].vval.v_list) == NULL
8646 || (map && tv_check_lock(l->lv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008647 return;
8648 }
8649 else if (argvars[0].v_type == VAR_DICT)
8650 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008651 if ((d = argvars[0].vval.v_dict) == NULL
8652 || (map && tv_check_lock(d->dv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008653 return;
8654 }
8655 else
8656 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008657 EMSG2(_(e_listdictarg), msg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008658 return;
8659 }
8660
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008661 expr = get_tv_string_buf_chk(&argvars[1], buf);
8662 /* On type errors, the preceding call has already displayed an error
8663 * message. Avoid a misleading error message for an empty string that
8664 * was not passed as argument. */
8665 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008666 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008667 prepare_vimvar(VV_VAL, &save_val);
8668 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008669
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008670 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008671 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008672 prepare_vimvar(VV_KEY, &save_key);
8673 vimvars[VV_KEY].vv_type = VAR_STRING;
8674
8675 ht = &d->dv_hashtab;
8676 hash_lock(ht);
8677 todo = ht->ht_used;
8678 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008679 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008680 if (!HASHITEM_EMPTY(hi))
8681 {
8682 --todo;
8683 di = HI2DI(hi);
8684 if (tv_check_lock(di->di_tv.v_lock, msg))
8685 break;
8686 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
8687 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL)
8688 break;
8689 if (!map && rem)
8690 dictitem_remove(d, di);
8691 clear_tv(&vimvars[VV_KEY].vv_tv);
8692 }
8693 }
8694 hash_unlock(ht);
8695
8696 restore_vimvar(VV_KEY, &save_key);
8697 }
8698 else
8699 {
8700 for (li = l->lv_first; li != NULL; li = nli)
8701 {
8702 if (tv_check_lock(li->li_tv.v_lock, msg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008703 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008704 nli = li->li_next;
8705 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008706 break;
8707 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008708 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008709 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008710 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008711
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008712 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008713 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008714
8715 copy_tv(&argvars[0], rettv);
8716}
8717
8718 static int
8719filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00008720 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008721 char_u *expr;
8722 int map;
8723 int *remp;
8724{
Bram Moolenaar33570922005-01-25 22:26:29 +00008725 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008726 char_u *s;
8727
Bram Moolenaar33570922005-01-25 22:26:29 +00008728 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008729 s = expr;
8730 if (eval1(&s, &rettv, TRUE) == FAIL)
8731 return FAIL;
8732 if (*s != NUL) /* check for trailing chars after expr */
8733 {
8734 EMSG2(_(e_invexpr2), s);
8735 return FAIL;
8736 }
8737 if (map)
8738 {
8739 /* map(): replace the list item value */
8740 clear_tv(tv);
8741 *tv = rettv;
8742 }
8743 else
8744 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008745 int error = FALSE;
8746
Bram Moolenaare9a41262005-01-15 22:18:47 +00008747 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008748 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008749 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008750 /* On type error, nothing has been removed; return FAIL to stop the
8751 * loop. The error message was given by get_tv_number_chk(). */
8752 if (error)
8753 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008754 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008755 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008756 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008757}
8758
8759/*
8760 * "filter()" function
8761 */
8762 static void
8763f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008764 typval_T *argvars;
8765 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008766{
8767 filter_map(argvars, rettv, FALSE);
8768}
8769
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008770/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008771 * "finddir({fname}[, {path}[, {count}]])" function
8772 */
8773 static void
8774f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008775 typval_T *argvars;
8776 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008777{
8778 findfilendir(argvars, rettv, TRUE);
8779}
8780
8781/*
8782 * "findfile({fname}[, {path}[, {count}]])" function
8783 */
8784 static void
8785f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008786 typval_T *argvars;
8787 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008788{
8789 findfilendir(argvars, rettv, FALSE);
8790}
8791
8792/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793 * "fnamemodify({fname}, {mods})" function
8794 */
8795 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008796f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008797 typval_T *argvars;
8798 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008799{
8800 char_u *fname;
8801 char_u *mods;
8802 int usedlen = 0;
8803 int len;
8804 char_u *fbuf = NULL;
8805 char_u buf[NUMBUFLEN];
8806
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008807 fname = get_tv_string_chk(&argvars[0]);
8808 mods = get_tv_string_buf_chk(&argvars[1], buf);
8809 if (fname == NULL || mods == NULL)
8810 fname = NULL;
8811 else
8812 {
8813 len = (int)STRLEN(fname);
8814 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
8815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008817 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008819 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008821 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822 vim_free(fbuf);
8823}
8824
Bram Moolenaar33570922005-01-25 22:26:29 +00008825static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826
8827/*
8828 * "foldclosed()" function
8829 */
8830 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008831foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00008832 typval_T *argvars;
8833 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834 int end;
8835{
8836#ifdef FEAT_FOLDING
8837 linenr_T lnum;
8838 linenr_T first, last;
8839
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008840 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008841 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8842 {
8843 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
8844 {
8845 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008846 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008848 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849 return;
8850 }
8851 }
8852#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008853 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854}
8855
8856/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008857 * "foldclosed()" function
8858 */
8859 static void
8860f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008861 typval_T *argvars;
8862 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008863{
8864 foldclosed_both(argvars, rettv, FALSE);
8865}
8866
8867/*
8868 * "foldclosedend()" function
8869 */
8870 static void
8871f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008872 typval_T *argvars;
8873 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008874{
8875 foldclosed_both(argvars, rettv, TRUE);
8876}
8877
8878/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008879 * "foldlevel()" function
8880 */
8881 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008882f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008883 typval_T *argvars;
8884 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885{
8886#ifdef FEAT_FOLDING
8887 linenr_T lnum;
8888
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008889 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008891 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892 else
8893#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008894 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895}
8896
8897/*
8898 * "foldtext()" function
8899 */
8900/*ARGSUSED*/
8901 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008902f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008903 typval_T *argvars;
8904 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008905{
8906#ifdef FEAT_FOLDING
8907 linenr_T lnum;
8908 char_u *s;
8909 char_u *r;
8910 int len;
8911 char *txt;
8912#endif
8913
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008914 rettv->v_type = VAR_STRING;
8915 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00008917 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
8918 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
8919 <= curbuf->b_ml.ml_line_count
8920 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921 {
8922 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008923 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
8924 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925 {
8926 if (!linewhite(lnum))
8927 break;
8928 ++lnum;
8929 }
8930
8931 /* Find interesting text in this line. */
8932 s = skipwhite(ml_get(lnum));
8933 /* skip C comment-start */
8934 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008935 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008937 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00008938 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008939 {
8940 s = skipwhite(ml_get(lnum + 1));
8941 if (*s == '*')
8942 s = skipwhite(s + 1);
8943 }
8944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945 txt = _("+-%s%3ld lines: ");
8946 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008947 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948 + 20 /* for %3ld */
8949 + STRLEN(s))); /* concatenated */
8950 if (r != NULL)
8951 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00008952 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
8953 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
8954 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955 len = (int)STRLEN(r);
8956 STRCAT(r, s);
8957 /* remove 'foldmarker' and 'commentstring' */
8958 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008959 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960 }
8961 }
8962#endif
8963}
8964
8965/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008966 * "foldtextresult(lnum)" function
8967 */
8968/*ARGSUSED*/
8969 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008970f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008971 typval_T *argvars;
8972 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008973{
8974#ifdef FEAT_FOLDING
8975 linenr_T lnum;
8976 char_u *text;
8977 char_u buf[51];
8978 foldinfo_T foldinfo;
8979 int fold_count;
8980#endif
8981
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008982 rettv->v_type = VAR_STRING;
8983 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008984#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008985 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008986 /* treat illegal types and illegal string values for {lnum} the same */
8987 if (lnum < 0)
8988 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008989 fold_count = foldedCount(curwin, lnum, &foldinfo);
8990 if (fold_count > 0)
8991 {
8992 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
8993 &foldinfo, buf);
8994 if (text == buf)
8995 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008996 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008997 }
8998#endif
8999}
9000
9001/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009002 * "foreground()" function
9003 */
9004/*ARGSUSED*/
9005 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009006f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009007 typval_T *argvars;
9008 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009010 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009011#ifdef FEAT_GUI
9012 if (gui.in_use)
9013 gui_mch_set_foreground();
9014#else
9015# ifdef WIN32
9016 win32_set_foreground();
9017# endif
9018#endif
9019}
9020
9021/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009022 * "function()" function
9023 */
9024/*ARGSUSED*/
9025 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009026f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009027 typval_T *argvars;
9028 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009029{
9030 char_u *s;
9031
Bram Moolenaara7043832005-01-21 11:56:39 +00009032 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009033 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009034 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009035 EMSG2(_(e_invarg2), s);
9036 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009037 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009038 else
9039 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009040 rettv->vval.v_string = vim_strsave(s);
9041 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009042 }
9043}
9044
9045/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009046 * "garbagecollect()" function
9047 */
9048/*ARGSUSED*/
9049 static void
9050f_garbagecollect(argvars, rettv)
9051 typval_T *argvars;
9052 typval_T *rettv;
9053{
9054 garbage_collect();
9055}
9056
9057/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009058 * "get()" function
9059 */
9060 static void
9061f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009062 typval_T *argvars;
9063 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009064{
Bram Moolenaar33570922005-01-25 22:26:29 +00009065 listitem_T *li;
9066 list_T *l;
9067 dictitem_T *di;
9068 dict_T *d;
9069 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009070
Bram Moolenaare9a41262005-01-15 22:18:47 +00009071 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009072 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009073 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009074 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009075 int error = FALSE;
9076
9077 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9078 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009079 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009080 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009081 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009082 else if (argvars[0].v_type == VAR_DICT)
9083 {
9084 if ((d = argvars[0].vval.v_dict) != NULL)
9085 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009086 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009087 if (di != NULL)
9088 tv = &di->di_tv;
9089 }
9090 }
9091 else
9092 EMSG2(_(e_listdictarg), "get()");
9093
9094 if (tv == NULL)
9095 {
9096 if (argvars[2].v_type == VAR_UNKNOWN)
9097 rettv->vval.v_number = 0;
9098 else
9099 copy_tv(&argvars[2], rettv);
9100 }
9101 else
9102 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009103}
9104
Bram Moolenaar342337a2005-07-21 21:11:17 +00009105static 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 +00009106
9107/*
9108 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +00009109 * Return a range (from start to end) of lines in rettv from the specified
9110 * buffer.
9111 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009112 */
9113 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +00009114get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009115 buf_T *buf;
9116 linenr_T start;
9117 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009118 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009119 typval_T *rettv;
9120{
9121 char_u *p;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009122 list_T *l = NULL;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009123 listitem_T *li;
9124
Bram Moolenaar342337a2005-07-21 21:11:17 +00009125 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009126 {
Bram Moolenaar342337a2005-07-21 21:11:17 +00009127 l = list_alloc();
9128 if (l == NULL)
9129 return;
9130
9131 rettv->vval.v_list = l;
9132 rettv->v_type = VAR_LIST;
9133 ++l->lv_refcount;
9134 }
9135 else
9136 rettv->vval.v_number = 0;
9137
9138 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
9139 return;
9140
9141 if (!retlist)
9142 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009143 if (start >= 1 && start <= buf->b_ml.ml_line_count)
9144 p = ml_get_buf(buf, start, FALSE);
9145 else
9146 p = (char_u *)"";
9147
9148 rettv->v_type = VAR_STRING;
9149 rettv->vval.v_string = vim_strsave(p);
9150 }
9151 else
9152 {
9153 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009154 return;
9155
9156 if (start < 1)
9157 start = 1;
9158 if (end > buf->b_ml.ml_line_count)
9159 end = buf->b_ml.ml_line_count;
9160 while (start <= end)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009161 {
Bram Moolenaar342337a2005-07-21 21:11:17 +00009162 li = listitem_alloc();
9163 if (li == NULL)
9164 break;
9165 list_append(l, li);
9166 li->li_tv.v_type = VAR_STRING;
9167 li->li_tv.v_lock = 0;
9168 li->li_tv.vval.v_string =
9169 vim_strsave(ml_get_buf(buf, start++, FALSE));
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009170 }
9171 }
9172}
9173
9174/*
9175 * "getbufline()" function
9176 */
9177 static void
9178f_getbufline(argvars, rettv)
9179 typval_T *argvars;
9180 typval_T *rettv;
9181{
9182 linenr_T lnum;
9183 linenr_T end;
9184 buf_T *buf;
9185
9186 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9187 ++emsg_off;
9188 buf = get_buf_tv(&argvars[0]);
9189 --emsg_off;
9190
Bram Moolenaar661b1822005-07-28 22:36:45 +00009191 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009192 if (argvars[2].v_type == VAR_UNKNOWN)
9193 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009194 else
Bram Moolenaar661b1822005-07-28 22:36:45 +00009195 end = get_tv_lnum_buf(&argvars[2], buf);
9196
Bram Moolenaar342337a2005-07-21 21:11:17 +00009197 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009198}
9199
Bram Moolenaar0d660222005-01-07 21:51:51 +00009200/*
9201 * "getbufvar()" function
9202 */
9203 static void
9204f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009205 typval_T *argvars;
9206 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009207{
9208 buf_T *buf;
9209 buf_T *save_curbuf;
9210 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009211 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009212
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009213 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9214 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009215 ++emsg_off;
9216 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009217
9218 rettv->v_type = VAR_STRING;
9219 rettv->vval.v_string = NULL;
9220
9221 if (buf != NULL && varname != NULL)
9222 {
9223 if (*varname == '&') /* buffer-local-option */
9224 {
9225 /* set curbuf to be our buf, temporarily */
9226 save_curbuf = curbuf;
9227 curbuf = buf;
9228
9229 get_option_tv(&varname, rettv, TRUE);
9230
9231 /* restore previous notion of curbuf */
9232 curbuf = save_curbuf;
9233 }
9234 else
9235 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009236 if (*varname == NUL)
9237 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9238 * scope prefix before the NUL byte is required by
9239 * find_var_in_ht(). */
9240 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009241 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009242 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009243 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009244 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009245 }
9246 }
9247
9248 --emsg_off;
9249}
9250
9251/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009252 * "getchar()" function
9253 */
9254 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009255f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009256 typval_T *argvars;
9257 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009258{
9259 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009260 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009261
9262 ++no_mapping;
9263 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009264 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009265 /* getchar(): blocking wait. */
9266 n = safe_vgetc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009267 else if (get_tv_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009268 /* getchar(1): only check if char avail */
9269 n = vpeekc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009270 else if (error || vpeekc() == NUL)
9271 /* illegal argument or getchar(0) and no char avail: return zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009272 n = 0;
9273 else
9274 /* getchar(0) and char avail: return char */
9275 n = safe_vgetc();
9276 --no_mapping;
9277 --allow_keys;
9278
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009279 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009280 if (IS_SPECIAL(n) || mod_mask != 0)
9281 {
9282 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9283 int i = 0;
9284
9285 /* Turn a special key into three bytes, plus modifier. */
9286 if (mod_mask != 0)
9287 {
9288 temp[i++] = K_SPECIAL;
9289 temp[i++] = KS_MODIFIER;
9290 temp[i++] = mod_mask;
9291 }
9292 if (IS_SPECIAL(n))
9293 {
9294 temp[i++] = K_SPECIAL;
9295 temp[i++] = K_SECOND(n);
9296 temp[i++] = K_THIRD(n);
9297 }
9298#ifdef FEAT_MBYTE
9299 else if (has_mbyte)
9300 i += (*mb_char2bytes)(n, temp + i);
9301#endif
9302 else
9303 temp[i++] = n;
9304 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009305 rettv->v_type = VAR_STRING;
9306 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009307 }
9308}
9309
9310/*
9311 * "getcharmod()" function
9312 */
9313/*ARGSUSED*/
9314 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009315f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009316 typval_T *argvars;
9317 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009318{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009319 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009320}
9321
9322/*
9323 * "getcmdline()" function
9324 */
9325/*ARGSUSED*/
9326 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009327f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009328 typval_T *argvars;
9329 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009330{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009331 rettv->v_type = VAR_STRING;
9332 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009333}
9334
9335/*
9336 * "getcmdpos()" function
9337 */
9338/*ARGSUSED*/
9339 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009340f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009341 typval_T *argvars;
9342 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009343{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009344 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009345}
9346
9347/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009348 * "getcwd()" function
9349 */
9350/*ARGSUSED*/
9351 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009352f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009353 typval_T *argvars;
9354 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009355{
9356 char_u cwd[MAXPATHL];
9357
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009358 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009359 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009360 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009361 else
9362 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009363 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009364#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +00009365 if (rettv->vval.v_string != NULL)
9366 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009367#endif
9368 }
9369}
9370
9371/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009372 * "getfontname()" function
9373 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009374/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009375 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009376f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009377 typval_T *argvars;
9378 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009379{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009380 rettv->v_type = VAR_STRING;
9381 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009382#ifdef FEAT_GUI
9383 if (gui.in_use)
9384 {
9385 GuiFont font;
9386 char_u *name = NULL;
9387
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009388 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009389 {
9390 /* Get the "Normal" font. Either the name saved by
9391 * hl_set_font_name() or from the font ID. */
9392 font = gui.norm_font;
9393 name = hl_get_font_name();
9394 }
9395 else
9396 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009397 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009398 if (STRCMP(name, "*") == 0) /* don't use font dialog */
9399 return;
9400 font = gui_mch_get_font(name, FALSE);
9401 if (font == NOFONT)
9402 return; /* Invalid font name, return empty string. */
9403 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009404 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009405 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009406 gui_mch_free_font(font);
9407 }
9408#endif
9409}
9410
9411/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009412 * "getfperm({fname})" function
9413 */
9414 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009415f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009416 typval_T *argvars;
9417 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009418{
9419 char_u *fname;
9420 struct stat st;
9421 char_u *perm = NULL;
9422 char_u flags[] = "rwx";
9423 int i;
9424
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009425 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009426
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009427 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009428 if (mch_stat((char *)fname, &st) >= 0)
9429 {
9430 perm = vim_strsave((char_u *)"---------");
9431 if (perm != NULL)
9432 {
9433 for (i = 0; i < 9; i++)
9434 {
9435 if (st.st_mode & (1 << (8 - i)))
9436 perm[i] = flags[i % 3];
9437 }
9438 }
9439 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009440 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009441}
9442
9443/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009444 * "getfsize({fname})" function
9445 */
9446 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009447f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009448 typval_T *argvars;
9449 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009450{
9451 char_u *fname;
9452 struct stat st;
9453
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009454 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009455
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009456 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009457
9458 if (mch_stat((char *)fname, &st) >= 0)
9459 {
9460 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009461 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009462 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009463 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009464 }
9465 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009466 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009467}
9468
9469/*
9470 * "getftime({fname})" function
9471 */
9472 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009473f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009474 typval_T *argvars;
9475 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009476{
9477 char_u *fname;
9478 struct stat st;
9479
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009480 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481
9482 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009483 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009484 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009485 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009486}
9487
9488/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009489 * "getftype({fname})" function
9490 */
9491 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009492f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009493 typval_T *argvars;
9494 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009495{
9496 char_u *fname;
9497 struct stat st;
9498 char_u *type = NULL;
9499 char *t;
9500
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009501 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009502
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009503 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009504 if (mch_lstat((char *)fname, &st) >= 0)
9505 {
9506#ifdef S_ISREG
9507 if (S_ISREG(st.st_mode))
9508 t = "file";
9509 else if (S_ISDIR(st.st_mode))
9510 t = "dir";
9511# ifdef S_ISLNK
9512 else if (S_ISLNK(st.st_mode))
9513 t = "link";
9514# endif
9515# ifdef S_ISBLK
9516 else if (S_ISBLK(st.st_mode))
9517 t = "bdev";
9518# endif
9519# ifdef S_ISCHR
9520 else if (S_ISCHR(st.st_mode))
9521 t = "cdev";
9522# endif
9523# ifdef S_ISFIFO
9524 else if (S_ISFIFO(st.st_mode))
9525 t = "fifo";
9526# endif
9527# ifdef S_ISSOCK
9528 else if (S_ISSOCK(st.st_mode))
9529 t = "fifo";
9530# endif
9531 else
9532 t = "other";
9533#else
9534# ifdef S_IFMT
9535 switch (st.st_mode & S_IFMT)
9536 {
9537 case S_IFREG: t = "file"; break;
9538 case S_IFDIR: t = "dir"; break;
9539# ifdef S_IFLNK
9540 case S_IFLNK: t = "link"; break;
9541# endif
9542# ifdef S_IFBLK
9543 case S_IFBLK: t = "bdev"; break;
9544# endif
9545# ifdef S_IFCHR
9546 case S_IFCHR: t = "cdev"; break;
9547# endif
9548# ifdef S_IFIFO
9549 case S_IFIFO: t = "fifo"; break;
9550# endif
9551# ifdef S_IFSOCK
9552 case S_IFSOCK: t = "socket"; break;
9553# endif
9554 default: t = "other";
9555 }
9556# else
9557 if (mch_isdir(fname))
9558 t = "dir";
9559 else
9560 t = "file";
9561# endif
9562#endif
9563 type = vim_strsave((char_u *)t);
9564 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009565 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009566}
9567
9568/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009569 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +00009570 */
9571 static void
9572f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009573 typval_T *argvars;
9574 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009575{
9576 linenr_T lnum;
9577 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009578 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009579
9580 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009581 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009582 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009583 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009584 retlist = FALSE;
9585 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009586 else
Bram Moolenaar342337a2005-07-21 21:11:17 +00009587 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009588 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009589 retlist = TRUE;
9590 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009591
Bram Moolenaar342337a2005-07-21 21:11:17 +00009592 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009593}
9594
9595/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00009596 * "getqflist()" function
9597 */
9598/*ARGSUSED*/
9599 static void
9600f_getqflist(argvars, rettv)
9601 typval_T *argvars;
9602 typval_T *rettv;
9603{
9604#ifdef FEAT_QUICKFIX
9605 list_T *l;
9606#endif
9607
9608 rettv->vval.v_number = FALSE;
9609#ifdef FEAT_QUICKFIX
9610 l = list_alloc();
9611 if (l != NULL)
9612 {
9613 if (get_errorlist(l) != FAIL)
9614 {
9615 rettv->vval.v_list = l;
9616 rettv->v_type = VAR_LIST;
9617 ++l->lv_refcount;
9618 }
9619 else
9620 list_free(l);
9621 }
9622#endif
9623}
9624
9625/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009626 * "getreg()" function
9627 */
9628 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009629f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009630 typval_T *argvars;
9631 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009632{
9633 char_u *strregname;
9634 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009635 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009636 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009637
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009638 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009639 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009640 strregname = get_tv_string_chk(&argvars[0]);
9641 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009642 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009643 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009645 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009646 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009647 regname = (strregname == NULL ? '"' : *strregname);
9648 if (regname == 0)
9649 regname = '"';
9650
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009651 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009652 rettv->vval.v_string = error ? NULL :
9653 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009654}
9655
9656/*
9657 * "getregtype()" function
9658 */
9659 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009660f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009661 typval_T *argvars;
9662 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009663{
9664 char_u *strregname;
9665 int regname;
9666 char_u buf[NUMBUFLEN + 2];
9667 long reglen = 0;
9668
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009669 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009670 {
9671 strregname = get_tv_string_chk(&argvars[0]);
9672 if (strregname == NULL) /* type error; errmsg already given */
9673 {
9674 rettv->v_type = VAR_STRING;
9675 rettv->vval.v_string = NULL;
9676 return;
9677 }
9678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009679 else
9680 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009681 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009682
9683 regname = (strregname == NULL ? '"' : *strregname);
9684 if (regname == 0)
9685 regname = '"';
9686
9687 buf[0] = NUL;
9688 buf[1] = NUL;
9689 switch (get_reg_type(regname, &reglen))
9690 {
9691 case MLINE: buf[0] = 'V'; break;
9692 case MCHAR: buf[0] = 'v'; break;
9693#ifdef FEAT_VISUAL
9694 case MBLOCK:
9695 buf[0] = Ctrl_V;
9696 sprintf((char *)buf + 1, "%ld", reglen + 1);
9697 break;
9698#endif
9699 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009700 rettv->v_type = VAR_STRING;
9701 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009702}
9703
9704/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705 * "getwinposx()" function
9706 */
9707/*ARGSUSED*/
9708 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009709f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009710 typval_T *argvars;
9711 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009712{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009713 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009714#ifdef FEAT_GUI
9715 if (gui.in_use)
9716 {
9717 int x, y;
9718
9719 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009720 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009721 }
9722#endif
9723}
9724
9725/*
9726 * "getwinposy()" function
9727 */
9728/*ARGSUSED*/
9729 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009730f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009731 typval_T *argvars;
9732 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009733{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009734 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009735#ifdef FEAT_GUI
9736 if (gui.in_use)
9737 {
9738 int x, y;
9739
9740 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009741 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009742 }
9743#endif
9744}
9745
Bram Moolenaara40058a2005-07-11 22:42:07 +00009746static win_T *find_win_by_nr __ARGS((typval_T *vp));
9747
9748 static win_T *
9749find_win_by_nr(vp)
9750 typval_T *vp;
9751{
9752#ifdef FEAT_WINDOWS
9753 win_T *wp;
9754#endif
9755 int nr;
9756
9757 nr = get_tv_number_chk(vp, NULL);
9758
9759#ifdef FEAT_WINDOWS
9760 if (nr < 0)
9761 return NULL;
9762 if (nr == 0)
9763 return curwin;
9764
9765 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9766 if (--nr <= 0)
9767 break;
9768 return wp;
9769#else
9770 if (nr == 0 || nr == 1)
9771 return curwin;
9772 return NULL;
9773#endif
9774}
9775
Bram Moolenaar071d4272004-06-13 20:20:40 +00009776/*
9777 * "getwinvar()" function
9778 */
9779 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009780f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009781 typval_T *argvars;
9782 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009783{
9784 win_T *win, *oldcurwin;
9785 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009786 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009787
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009789 varname = get_tv_string_chk(&argvars[1]);
9790 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009791
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009792 rettv->v_type = VAR_STRING;
9793 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009794
9795 if (win != NULL && varname != NULL)
9796 {
9797 if (*varname == '&') /* window-local-option */
9798 {
Bram Moolenaarc0761132005-03-18 20:30:32 +00009799 /* Set curwin to be our win, temporarily. Also set curbuf, so
9800 * that we can get buffer-local options. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009801 oldcurwin = curwin;
9802 curwin = win;
Bram Moolenaarc0761132005-03-18 20:30:32 +00009803 curbuf = win->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009804
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009805 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009806
9807 /* restore previous notion of curwin */
9808 curwin = oldcurwin;
Bram Moolenaarc0761132005-03-18 20:30:32 +00009809 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009810 }
9811 else
9812 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009813 if (*varname == NUL)
9814 /* let getwinvar({nr}, "") return the "w:" dictionary. The
9815 * scope prefix before the NUL byte is required by
9816 * find_var_in_ht(). */
9817 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009818 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009819 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009820 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009821 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009822 }
9823 }
9824
9825 --emsg_off;
9826}
9827
9828/*
9829 * "glob()" function
9830 */
9831 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009832f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009833 typval_T *argvars;
9834 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009835{
9836 expand_T xpc;
9837
9838 ExpandInit(&xpc);
9839 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009840 rettv->v_type = VAR_STRING;
9841 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009842 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
9843 ExpandCleanup(&xpc);
9844}
9845
9846/*
9847 * "globpath()" function
9848 */
9849 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009850f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009851 typval_T *argvars;
9852 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009853{
9854 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009855 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009856
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009857 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009858 if (file == NULL)
9859 rettv->vval.v_string = NULL;
9860 else
9861 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009862}
9863
9864/*
9865 * "has()" function
9866 */
9867 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009868f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009869 typval_T *argvars;
9870 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009871{
9872 int i;
9873 char_u *name;
9874 int n = FALSE;
9875 static char *(has_list[]) =
9876 {
9877#ifdef AMIGA
9878 "amiga",
9879# ifdef FEAT_ARP
9880 "arp",
9881# endif
9882#endif
9883#ifdef __BEOS__
9884 "beos",
9885#endif
9886#ifdef MSDOS
9887# ifdef DJGPP
9888 "dos32",
9889# else
9890 "dos16",
9891# endif
9892#endif
9893#ifdef MACOS /* TODO: Should we add MACOS_CLASSIC, MACOS_X? (Dany) */
9894 "mac",
9895#endif
9896#if defined(MACOS_X_UNIX)
9897 "macunix",
9898#endif
9899#ifdef OS2
9900 "os2",
9901#endif
9902#ifdef __QNX__
9903 "qnx",
9904#endif
9905#ifdef RISCOS
9906 "riscos",
9907#endif
9908#ifdef UNIX
9909 "unix",
9910#endif
9911#ifdef VMS
9912 "vms",
9913#endif
9914#ifdef WIN16
9915 "win16",
9916#endif
9917#ifdef WIN32
9918 "win32",
9919#endif
9920#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
9921 "win32unix",
9922#endif
9923#ifdef WIN64
9924 "win64",
9925#endif
9926#ifdef EBCDIC
9927 "ebcdic",
9928#endif
9929#ifndef CASE_INSENSITIVE_FILENAME
9930 "fname_case",
9931#endif
9932#ifdef FEAT_ARABIC
9933 "arabic",
9934#endif
9935#ifdef FEAT_AUTOCMD
9936 "autocmd",
9937#endif
9938#ifdef FEAT_BEVAL
9939 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +00009940# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
9941 "balloon_multiline",
9942# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009943#endif
9944#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
9945 "builtin_terms",
9946# ifdef ALL_BUILTIN_TCAPS
9947 "all_builtin_terms",
9948# endif
9949#endif
9950#ifdef FEAT_BYTEOFF
9951 "byte_offset",
9952#endif
9953#ifdef FEAT_CINDENT
9954 "cindent",
9955#endif
9956#ifdef FEAT_CLIENTSERVER
9957 "clientserver",
9958#endif
9959#ifdef FEAT_CLIPBOARD
9960 "clipboard",
9961#endif
9962#ifdef FEAT_CMDL_COMPL
9963 "cmdline_compl",
9964#endif
9965#ifdef FEAT_CMDHIST
9966 "cmdline_hist",
9967#endif
9968#ifdef FEAT_COMMENTS
9969 "comments",
9970#endif
9971#ifdef FEAT_CRYPT
9972 "cryptv",
9973#endif
9974#ifdef FEAT_CSCOPE
9975 "cscope",
9976#endif
9977#ifdef DEBUG
9978 "debug",
9979#endif
9980#ifdef FEAT_CON_DIALOG
9981 "dialog_con",
9982#endif
9983#ifdef FEAT_GUI_DIALOG
9984 "dialog_gui",
9985#endif
9986#ifdef FEAT_DIFF
9987 "diff",
9988#endif
9989#ifdef FEAT_DIGRAPHS
9990 "digraphs",
9991#endif
9992#ifdef FEAT_DND
9993 "dnd",
9994#endif
9995#ifdef FEAT_EMACS_TAGS
9996 "emacs_tags",
9997#endif
9998 "eval", /* always present, of course! */
9999#ifdef FEAT_EX_EXTRA
10000 "ex_extra",
10001#endif
10002#ifdef FEAT_SEARCH_EXTRA
10003 "extra_search",
10004#endif
10005#ifdef FEAT_FKMAP
10006 "farsi",
10007#endif
10008#ifdef FEAT_SEARCHPATH
10009 "file_in_path",
10010#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010011#if defined(UNIX) && !defined(USE_SYSTEM)
10012 "filterpipe",
10013#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010014#ifdef FEAT_FIND_ID
10015 "find_in_path",
10016#endif
10017#ifdef FEAT_FOLDING
10018 "folding",
10019#endif
10020#ifdef FEAT_FOOTER
10021 "footer",
10022#endif
10023#if !defined(USE_SYSTEM) && defined(UNIX)
10024 "fork",
10025#endif
10026#ifdef FEAT_GETTEXT
10027 "gettext",
10028#endif
10029#ifdef FEAT_GUI
10030 "gui",
10031#endif
10032#ifdef FEAT_GUI_ATHENA
10033# ifdef FEAT_GUI_NEXTAW
10034 "gui_neXtaw",
10035# else
10036 "gui_athena",
10037# endif
10038#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +000010039#ifdef FEAT_GUI_KDE
10040 "gui_kde",
10041#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010042#ifdef FEAT_GUI_GTK
10043 "gui_gtk",
10044# ifdef HAVE_GTK2
10045 "gui_gtk2",
10046# endif
10047#endif
10048#ifdef FEAT_GUI_MAC
10049 "gui_mac",
10050#endif
10051#ifdef FEAT_GUI_MOTIF
10052 "gui_motif",
10053#endif
10054#ifdef FEAT_GUI_PHOTON
10055 "gui_photon",
10056#endif
10057#ifdef FEAT_GUI_W16
10058 "gui_win16",
10059#endif
10060#ifdef FEAT_GUI_W32
10061 "gui_win32",
10062#endif
10063#ifdef FEAT_HANGULIN
10064 "hangul_input",
10065#endif
10066#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10067 "iconv",
10068#endif
10069#ifdef FEAT_INS_EXPAND
10070 "insert_expand",
10071#endif
10072#ifdef FEAT_JUMPLIST
10073 "jumplist",
10074#endif
10075#ifdef FEAT_KEYMAP
10076 "keymap",
10077#endif
10078#ifdef FEAT_LANGMAP
10079 "langmap",
10080#endif
10081#ifdef FEAT_LIBCALL
10082 "libcall",
10083#endif
10084#ifdef FEAT_LINEBREAK
10085 "linebreak",
10086#endif
10087#ifdef FEAT_LISP
10088 "lispindent",
10089#endif
10090#ifdef FEAT_LISTCMDS
10091 "listcmds",
10092#endif
10093#ifdef FEAT_LOCALMAP
10094 "localmap",
10095#endif
10096#ifdef FEAT_MENU
10097 "menu",
10098#endif
10099#ifdef FEAT_SESSION
10100 "mksession",
10101#endif
10102#ifdef FEAT_MODIFY_FNAME
10103 "modify_fname",
10104#endif
10105#ifdef FEAT_MOUSE
10106 "mouse",
10107#endif
10108#ifdef FEAT_MOUSESHAPE
10109 "mouseshape",
10110#endif
10111#if defined(UNIX) || defined(VMS)
10112# ifdef FEAT_MOUSE_DEC
10113 "mouse_dec",
10114# endif
10115# ifdef FEAT_MOUSE_GPM
10116 "mouse_gpm",
10117# endif
10118# ifdef FEAT_MOUSE_JSB
10119 "mouse_jsbterm",
10120# endif
10121# ifdef FEAT_MOUSE_NET
10122 "mouse_netterm",
10123# endif
10124# ifdef FEAT_MOUSE_PTERM
10125 "mouse_pterm",
10126# endif
10127# ifdef FEAT_MOUSE_XTERM
10128 "mouse_xterm",
10129# endif
10130#endif
10131#ifdef FEAT_MBYTE
10132 "multi_byte",
10133#endif
10134#ifdef FEAT_MBYTE_IME
10135 "multi_byte_ime",
10136#endif
10137#ifdef FEAT_MULTI_LANG
10138 "multi_lang",
10139#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010140#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010141#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010142 "mzscheme",
10143#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010144#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010145#ifdef FEAT_OLE
10146 "ole",
10147#endif
10148#ifdef FEAT_OSFILETYPE
10149 "osfiletype",
10150#endif
10151#ifdef FEAT_PATH_EXTRA
10152 "path_extra",
10153#endif
10154#ifdef FEAT_PERL
10155#ifndef DYNAMIC_PERL
10156 "perl",
10157#endif
10158#endif
10159#ifdef FEAT_PYTHON
10160#ifndef DYNAMIC_PYTHON
10161 "python",
10162#endif
10163#endif
10164#ifdef FEAT_POSTSCRIPT
10165 "postscript",
10166#endif
10167#ifdef FEAT_PRINTER
10168 "printer",
10169#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000010170#ifdef FEAT_PROFILE
10171 "profile",
10172#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010173#ifdef FEAT_QUICKFIX
10174 "quickfix",
10175#endif
10176#ifdef FEAT_RIGHTLEFT
10177 "rightleft",
10178#endif
10179#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
10180 "ruby",
10181#endif
10182#ifdef FEAT_SCROLLBIND
10183 "scrollbind",
10184#endif
10185#ifdef FEAT_CMDL_INFO
10186 "showcmd",
10187 "cmdline_info",
10188#endif
10189#ifdef FEAT_SIGNS
10190 "signs",
10191#endif
10192#ifdef FEAT_SMARTINDENT
10193 "smartindent",
10194#endif
10195#ifdef FEAT_SNIFF
10196 "sniff",
10197#endif
10198#ifdef FEAT_STL_OPT
10199 "statusline",
10200#endif
10201#ifdef FEAT_SUN_WORKSHOP
10202 "sun_workshop",
10203#endif
10204#ifdef FEAT_NETBEANS_INTG
10205 "netbeans_intg",
10206#endif
10207#ifdef FEAT_SYN_HL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010208 "spell",
10209#endif
10210#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010211 "syntax",
10212#endif
10213#if defined(USE_SYSTEM) || !defined(UNIX)
10214 "system",
10215#endif
10216#ifdef FEAT_TAG_BINS
10217 "tag_binary",
10218#endif
10219#ifdef FEAT_TAG_OLDSTATIC
10220 "tag_old_static",
10221#endif
10222#ifdef FEAT_TAG_ANYWHITE
10223 "tag_any_white",
10224#endif
10225#ifdef FEAT_TCL
10226# ifndef DYNAMIC_TCL
10227 "tcl",
10228# endif
10229#endif
10230#ifdef TERMINFO
10231 "terminfo",
10232#endif
10233#ifdef FEAT_TERMRESPONSE
10234 "termresponse",
10235#endif
10236#ifdef FEAT_TEXTOBJ
10237 "textobjects",
10238#endif
10239#ifdef HAVE_TGETENT
10240 "tgetent",
10241#endif
10242#ifdef FEAT_TITLE
10243 "title",
10244#endif
10245#ifdef FEAT_TOOLBAR
10246 "toolbar",
10247#endif
10248#ifdef FEAT_USR_CMDS
10249 "user-commands", /* was accidentally included in 5.4 */
10250 "user_commands",
10251#endif
10252#ifdef FEAT_VIMINFO
10253 "viminfo",
10254#endif
10255#ifdef FEAT_VERTSPLIT
10256 "vertsplit",
10257#endif
10258#ifdef FEAT_VIRTUALEDIT
10259 "virtualedit",
10260#endif
10261#ifdef FEAT_VISUAL
10262 "visual",
10263#endif
10264#ifdef FEAT_VISUALEXTRA
10265 "visualextra",
10266#endif
10267#ifdef FEAT_VREPLACE
10268 "vreplace",
10269#endif
10270#ifdef FEAT_WILDIGN
10271 "wildignore",
10272#endif
10273#ifdef FEAT_WILDMENU
10274 "wildmenu",
10275#endif
10276#ifdef FEAT_WINDOWS
10277 "windows",
10278#endif
10279#ifdef FEAT_WAK
10280 "winaltkeys",
10281#endif
10282#ifdef FEAT_WRITEBACKUP
10283 "writebackup",
10284#endif
10285#ifdef FEAT_XIM
10286 "xim",
10287#endif
10288#ifdef FEAT_XFONTSET
10289 "xfontset",
10290#endif
10291#ifdef USE_XSMP
10292 "xsmp",
10293#endif
10294#ifdef USE_XSMP_INTERACT
10295 "xsmp_interact",
10296#endif
10297#ifdef FEAT_XCLIPBOARD
10298 "xterm_clipboard",
10299#endif
10300#ifdef FEAT_XTERM_SAVE
10301 "xterm_save",
10302#endif
10303#if defined(UNIX) && defined(FEAT_X11)
10304 "X11",
10305#endif
10306 NULL
10307 };
10308
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010309 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010310 for (i = 0; has_list[i] != NULL; ++i)
10311 if (STRICMP(name, has_list[i]) == 0)
10312 {
10313 n = TRUE;
10314 break;
10315 }
10316
10317 if (n == FALSE)
10318 {
10319 if (STRNICMP(name, "patch", 5) == 0)
10320 n = has_patch(atoi((char *)name + 5));
10321 else if (STRICMP(name, "vim_starting") == 0)
10322 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010323#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
10324 else if (STRICMP(name, "balloon_multiline") == 0)
10325 n = multiline_balloon_available();
10326#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010327#ifdef DYNAMIC_TCL
10328 else if (STRICMP(name, "tcl") == 0)
10329 n = tcl_enabled(FALSE);
10330#endif
10331#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
10332 else if (STRICMP(name, "iconv") == 0)
10333 n = iconv_enabled(FALSE);
10334#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010335#ifdef DYNAMIC_MZSCHEME
10336 else if (STRICMP(name, "mzscheme") == 0)
10337 n = mzscheme_enabled(FALSE);
10338#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010339#ifdef DYNAMIC_RUBY
10340 else if (STRICMP(name, "ruby") == 0)
10341 n = ruby_enabled(FALSE);
10342#endif
10343#ifdef DYNAMIC_PYTHON
10344 else if (STRICMP(name, "python") == 0)
10345 n = python_enabled(FALSE);
10346#endif
10347#ifdef DYNAMIC_PERL
10348 else if (STRICMP(name, "perl") == 0)
10349 n = perl_enabled(FALSE);
10350#endif
10351#ifdef FEAT_GUI
10352 else if (STRICMP(name, "gui_running") == 0)
10353 n = (gui.in_use || gui.starting);
10354# ifdef FEAT_GUI_W32
10355 else if (STRICMP(name, "gui_win32s") == 0)
10356 n = gui_is_win32s();
10357# endif
10358# ifdef FEAT_BROWSE
10359 else if (STRICMP(name, "browse") == 0)
10360 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
10361# endif
10362#endif
10363#ifdef FEAT_SYN_HL
10364 else if (STRICMP(name, "syntax_items") == 0)
10365 n = syntax_present(curbuf);
10366#endif
10367#if defined(WIN3264)
10368 else if (STRICMP(name, "win95") == 0)
10369 n = mch_windows95();
10370#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000010371#ifdef FEAT_NETBEANS_INTG
10372 else if (STRICMP(name, "netbeans_enabled") == 0)
10373 n = usingNetbeans;
10374#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010375 }
10376
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010377 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010378}
10379
10380/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000010381 * "has_key()" function
10382 */
10383 static void
10384f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010385 typval_T *argvars;
10386 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010387{
10388 rettv->vval.v_number = 0;
10389 if (argvars[0].v_type != VAR_DICT)
10390 {
10391 EMSG(_(e_dictreq));
10392 return;
10393 }
10394 if (argvars[0].vval.v_dict == NULL)
10395 return;
10396
10397 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010398 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010399}
10400
10401/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010402 * "hasmapto()" function
10403 */
10404 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010405f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010406 typval_T *argvars;
10407 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010408{
10409 char_u *name;
10410 char_u *mode;
10411 char_u buf[NUMBUFLEN];
10412
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010413 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010414 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010415 mode = (char_u *)"nvo";
10416 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010417 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010418
10419 if (map_to_exists(name, mode))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010420 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010421 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010422 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010423}
10424
10425/*
10426 * "histadd()" function
10427 */
10428/*ARGSUSED*/
10429 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010430f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010431 typval_T *argvars;
10432 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010433{
10434#ifdef FEAT_CMDHIST
10435 int histype;
10436 char_u *str;
10437 char_u buf[NUMBUFLEN];
10438#endif
10439
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010440 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010441 if (check_restricted() || check_secure())
10442 return;
10443#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010444 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10445 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010446 if (histype >= 0)
10447 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010448 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010449 if (*str != NUL)
10450 {
10451 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010452 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010453 return;
10454 }
10455 }
10456#endif
10457}
10458
10459/*
10460 * "histdel()" function
10461 */
10462/*ARGSUSED*/
10463 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010464f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010465 typval_T *argvars;
10466 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010467{
10468#ifdef FEAT_CMDHIST
10469 int n;
10470 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010471 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010472
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010473 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10474 if (str == NULL)
10475 n = 0;
10476 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010477 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010478 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010479 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010480 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010481 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010482 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010483 else
10484 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010485 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010486 get_tv_string_buf(&argvars[1], buf));
10487 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010488#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010489 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010490#endif
10491}
10492
10493/*
10494 * "histget()" function
10495 */
10496/*ARGSUSED*/
10497 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010498f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010499 typval_T *argvars;
10500 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010501{
10502#ifdef FEAT_CMDHIST
10503 int type;
10504 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010505 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010506
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010507 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10508 if (str == NULL)
10509 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010510 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010511 {
10512 type = get_histtype(str);
10513 if (argvars[1].v_type == VAR_UNKNOWN)
10514 idx = get_history_idx(type);
10515 else
10516 idx = (int)get_tv_number_chk(&argvars[1], NULL);
10517 /* -1 on type error */
10518 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
10519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010520#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010521 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010522#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010523 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010524}
10525
10526/*
10527 * "histnr()" function
10528 */
10529/*ARGSUSED*/
10530 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010531f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010532 typval_T *argvars;
10533 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010534{
10535 int i;
10536
10537#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010538 char_u *history = get_tv_string_chk(&argvars[0]);
10539
10540 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010541 if (i >= HIST_CMD && i < HIST_COUNT)
10542 i = get_history_idx(i);
10543 else
10544#endif
10545 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010546 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010547}
10548
10549/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010550 * "highlightID(name)" function
10551 */
10552 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010553f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010554 typval_T *argvars;
10555 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010556{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010557 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010558}
10559
10560/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010561 * "highlight_exists()" function
10562 */
10563 static void
10564f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010565 typval_T *argvars;
10566 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010567{
10568 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
10569}
10570
10571/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010572 * "hostname()" function
10573 */
10574/*ARGSUSED*/
10575 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010576f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010577 typval_T *argvars;
10578 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010579{
10580 char_u hostname[256];
10581
10582 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010583 rettv->v_type = VAR_STRING;
10584 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010585}
10586
10587/*
10588 * iconv() function
10589 */
10590/*ARGSUSED*/
10591 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010592f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010593 typval_T *argvars;
10594 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010595{
10596#ifdef FEAT_MBYTE
10597 char_u buf1[NUMBUFLEN];
10598 char_u buf2[NUMBUFLEN];
10599 char_u *from, *to, *str;
10600 vimconv_T vimconv;
10601#endif
10602
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010603 rettv->v_type = VAR_STRING;
10604 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010605
10606#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010607 str = get_tv_string(&argvars[0]);
10608 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
10609 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010610 vimconv.vc_type = CONV_NONE;
10611 convert_setup(&vimconv, from, to);
10612
10613 /* If the encodings are equal, no conversion needed. */
10614 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010615 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010616 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010617 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010618
10619 convert_setup(&vimconv, NULL, NULL);
10620 vim_free(from);
10621 vim_free(to);
10622#endif
10623}
10624
10625/*
10626 * "indent()" function
10627 */
10628 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010629f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010630 typval_T *argvars;
10631 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010632{
10633 linenr_T lnum;
10634
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010635 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010636 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010637 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010638 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010639 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010640}
10641
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010642/*
10643 * "index()" function
10644 */
10645 static void
10646f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010647 typval_T *argvars;
10648 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010649{
Bram Moolenaar33570922005-01-25 22:26:29 +000010650 list_T *l;
10651 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010652 long idx = 0;
10653 int ic = FALSE;
10654
10655 rettv->vval.v_number = -1;
10656 if (argvars[0].v_type != VAR_LIST)
10657 {
10658 EMSG(_(e_listreq));
10659 return;
10660 }
10661 l = argvars[0].vval.v_list;
10662 if (l != NULL)
10663 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010664 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010665 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010666 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010667 int error = FALSE;
10668
Bram Moolenaar758711c2005-02-02 23:11:38 +000010669 /* Start at specified item. Use the cached index that list_find()
10670 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010671 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000010672 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010673 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010674 ic = get_tv_number_chk(&argvars[3], &error);
10675 if (error)
10676 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010677 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010678
Bram Moolenaar758711c2005-02-02 23:11:38 +000010679 for ( ; item != NULL; item = item->li_next, ++idx)
10680 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010681 {
10682 rettv->vval.v_number = idx;
10683 break;
10684 }
10685 }
10686}
10687
Bram Moolenaar071d4272004-06-13 20:20:40 +000010688static int inputsecret_flag = 0;
10689
10690/*
10691 * "input()" function
10692 * Also handles inputsecret() when inputsecret is set.
10693 */
10694 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010695f_input(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010696 typval_T *argvars;
10697 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010698{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010699 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010700 char_u *p = NULL;
10701 int c;
10702 char_u buf[NUMBUFLEN];
10703 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010704 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000010705
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010706 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010707
10708#ifdef NO_CONSOLE_INPUT
10709 /* While starting up, there is no place to enter text. */
10710 if (no_console_input())
10711 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010712 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010713 return;
10714 }
10715#endif
10716
10717 cmd_silent = FALSE; /* Want to see the prompt. */
10718 if (prompt != NULL)
10719 {
10720 /* Only the part of the message after the last NL is considered as
10721 * prompt for the command line */
10722 p = vim_strrchr(prompt, '\n');
10723 if (p == NULL)
10724 p = prompt;
10725 else
10726 {
10727 ++p;
10728 c = *p;
10729 *p = NUL;
10730 msg_start();
10731 msg_clr_eos();
10732 msg_puts_attr(prompt, echo_attr);
10733 msg_didout = FALSE;
10734 msg_starthere();
10735 *p = c;
10736 }
10737 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010738
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010739 if (argvars[1].v_type != VAR_UNKNOWN)
10740 {
10741 defstr = get_tv_string_buf_chk(&argvars[1], buf);
10742 if (defstr != NULL)
10743 stuffReadbuffSpec(defstr);
10744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010745
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010746 if (defstr != NULL)
10747 rettv->vval.v_string =
Bram Moolenaar071d4272004-06-13 20:20:40 +000010748 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr);
10749
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010750 /* since the user typed this, no need to wait for return */
10751 need_wait_return = FALSE;
10752 msg_didout = FALSE;
10753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010754 cmd_silent = cmd_silent_save;
10755}
10756
10757/*
10758 * "inputdialog()" function
10759 */
10760 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010761f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010762 typval_T *argvars;
10763 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010764{
10765#if defined(FEAT_GUI_TEXTDIALOG)
10766 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
10767 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
10768 {
10769 char_u *message;
10770 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010771 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000010772
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010773 message = get_tv_string_chk(&argvars[0]);
10774 if (argvars[1].v_type != VAR_UNKNOWN
10775 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000010776 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010777 else
10778 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010779 if (message != NULL && defstr != NULL
10780 && do_dialog(VIM_QUESTION, NULL, message,
10781 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010782 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010783 else
10784 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010785 if (message != NULL && defstr != NULL
10786 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010787 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010788 rettv->vval.v_string = vim_strsave(
10789 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010790 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010791 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010792 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010793 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010794 }
10795 else
10796#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010797 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010798}
10799
10800static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
10801
10802/*
10803 * "inputrestore()" function
10804 */
10805/*ARGSUSED*/
10806 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010807f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010808 typval_T *argvars;
10809 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010810{
10811 if (ga_userinput.ga_len > 0)
10812 {
10813 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010814 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
10815 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010816 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010817 }
10818 else if (p_verbose > 1)
10819 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000010820 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010821 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010822 }
10823}
10824
10825/*
10826 * "inputsave()" function
10827 */
10828/*ARGSUSED*/
10829 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010830f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010831 typval_T *argvars;
10832 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010833{
10834 /* Add an entry to the stack of typehead storage. */
10835 if (ga_grow(&ga_userinput, 1) == OK)
10836 {
10837 save_typeahead((tasave_T *)(ga_userinput.ga_data)
10838 + ga_userinput.ga_len);
10839 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010840 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010841 }
10842 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010843 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010844}
10845
10846/*
10847 * "inputsecret()" function
10848 */
10849 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010850f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010851 typval_T *argvars;
10852 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010853{
10854 ++cmdline_star;
10855 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010856 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010857 --cmdline_star;
10858 --inputsecret_flag;
10859}
10860
10861/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010862 * "insert()" function
10863 */
10864 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010865f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010866 typval_T *argvars;
10867 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010868{
10869 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000010870 listitem_T *item;
10871 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010872 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010873
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010874 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010875 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010876 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010877 else if ((l = argvars[0].vval.v_list) != NULL
10878 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010879 {
10880 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010881 before = get_tv_number_chk(&argvars[2], &error);
10882 if (error)
10883 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010884
Bram Moolenaar758711c2005-02-02 23:11:38 +000010885 if (before == l->lv_len)
10886 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010887 else
10888 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010889 item = list_find(l, before);
10890 if (item == NULL)
10891 {
10892 EMSGN(_(e_listidx), before);
10893 l = NULL;
10894 }
10895 }
10896 if (l != NULL)
10897 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010898 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010899 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010900 }
10901 }
10902}
10903
10904/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010905 * "isdirectory()" function
10906 */
10907 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010908f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010909 typval_T *argvars;
10910 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010911{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010912 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010913}
10914
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010915/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010916 * "islocked()" function
10917 */
10918 static void
10919f_islocked(argvars, rettv)
10920 typval_T *argvars;
10921 typval_T *rettv;
10922{
10923 lval_T lv;
10924 char_u *end;
10925 dictitem_T *di;
10926
10927 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000010928 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
10929 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010930 if (end != NULL && lv.ll_name != NULL)
10931 {
10932 if (*end != NUL)
10933 EMSG(_(e_trailing));
10934 else
10935 {
10936 if (lv.ll_tv == NULL)
10937 {
10938 if (check_changedtick(lv.ll_name))
10939 rettv->vval.v_number = 1; /* always locked */
10940 else
10941 {
10942 di = find_var(lv.ll_name, NULL);
10943 if (di != NULL)
10944 {
10945 /* Consider a variable locked when:
10946 * 1. the variable itself is locked
10947 * 2. the value of the variable is locked.
10948 * 3. the List or Dict value is locked.
10949 */
10950 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
10951 || tv_islocked(&di->di_tv));
10952 }
10953 }
10954 }
10955 else if (lv.ll_range)
10956 EMSG(_("E745: Range not allowed"));
10957 else if (lv.ll_newkey != NULL)
10958 EMSG2(_(e_dictkey), lv.ll_newkey);
10959 else if (lv.ll_list != NULL)
10960 /* List item. */
10961 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
10962 else
10963 /* Dictionary item. */
10964 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
10965 }
10966 }
10967
10968 clear_lval(&lv);
10969}
10970
Bram Moolenaar33570922005-01-25 22:26:29 +000010971static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000010972
10973/*
10974 * Turn a dict into a list:
10975 * "what" == 0: list of keys
10976 * "what" == 1: list of values
10977 * "what" == 2: list of items
10978 */
10979 static void
10980dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000010981 typval_T *argvars;
10982 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010983 int what;
10984{
Bram Moolenaar33570922005-01-25 22:26:29 +000010985 list_T *l;
10986 list_T *l2;
10987 dictitem_T *di;
10988 hashitem_T *hi;
10989 listitem_T *li;
10990 listitem_T *li2;
10991 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010992 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010993
10994 rettv->vval.v_number = 0;
10995 if (argvars[0].v_type != VAR_DICT)
10996 {
10997 EMSG(_(e_dictreq));
10998 return;
10999 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011000 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011001 return;
11002
11003 l = list_alloc();
11004 if (l == NULL)
11005 return;
11006 rettv->v_type = VAR_LIST;
11007 rettv->vval.v_list = l;
11008 ++l->lv_refcount;
11009
Bram Moolenaar33570922005-01-25 22:26:29 +000011010 todo = d->dv_hashtab.ht_used;
11011 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011012 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011013 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011014 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011015 --todo;
11016 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011017
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011018 li = listitem_alloc();
11019 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011020 break;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011021 list_append(l, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011022
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011023 if (what == 0)
11024 {
11025 /* keys() */
11026 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011027 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011028 li->li_tv.vval.v_string = vim_strsave(di->di_key);
11029 }
11030 else if (what == 1)
11031 {
11032 /* values() */
11033 copy_tv(&di->di_tv, &li->li_tv);
11034 }
11035 else
11036 {
11037 /* items() */
11038 l2 = list_alloc();
11039 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011040 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011041 li->li_tv.vval.v_list = l2;
11042 if (l2 == NULL)
11043 break;
11044 ++l2->lv_refcount;
11045
11046 li2 = listitem_alloc();
11047 if (li2 == NULL)
11048 break;
11049 list_append(l2, li2);
11050 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011051 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011052 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
11053
11054 li2 = listitem_alloc();
11055 if (li2 == NULL)
11056 break;
11057 list_append(l2, li2);
11058 copy_tv(&di->di_tv, &li2->li_tv);
11059 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011060 }
11061 }
11062}
11063
11064/*
11065 * "items(dict)" function
11066 */
11067 static void
11068f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011069 typval_T *argvars;
11070 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011071{
11072 dict_list(argvars, rettv, 2);
11073}
11074
Bram Moolenaar071d4272004-06-13 20:20:40 +000011075/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011076 * "join()" function
11077 */
11078 static void
11079f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011080 typval_T *argvars;
11081 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011082{
11083 garray_T ga;
11084 char_u *sep;
11085
11086 rettv->vval.v_number = 0;
11087 if (argvars[0].v_type != VAR_LIST)
11088 {
11089 EMSG(_(e_listreq));
11090 return;
11091 }
11092 if (argvars[0].vval.v_list == NULL)
11093 return;
11094 if (argvars[1].v_type == VAR_UNKNOWN)
11095 sep = (char_u *)" ";
11096 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011097 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011098
11099 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011100
11101 if (sep != NULL)
11102 {
11103 ga_init2(&ga, (int)sizeof(char), 80);
11104 list_join(&ga, argvars[0].vval.v_list, sep, TRUE);
11105 ga_append(&ga, NUL);
11106 rettv->vval.v_string = (char_u *)ga.ga_data;
11107 }
11108 else
11109 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011110}
11111
11112/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011113 * "keys()" function
11114 */
11115 static void
11116f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011117 typval_T *argvars;
11118 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011119{
11120 dict_list(argvars, rettv, 0);
11121}
11122
11123/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011124 * "last_buffer_nr()" function.
11125 */
11126/*ARGSUSED*/
11127 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011128f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011129 typval_T *argvars;
11130 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011131{
11132 int n = 0;
11133 buf_T *buf;
11134
11135 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
11136 if (n < buf->b_fnum)
11137 n = buf->b_fnum;
11138
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011139 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011140}
11141
11142/*
11143 * "len()" function
11144 */
11145 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011146f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011147 typval_T *argvars;
11148 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011149{
11150 switch (argvars[0].v_type)
11151 {
11152 case VAR_STRING:
11153 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011154 rettv->vval.v_number = (varnumber_T)STRLEN(
11155 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011156 break;
11157 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011158 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011159 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011160 case VAR_DICT:
11161 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
11162 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011163 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011164 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011165 break;
11166 }
11167}
11168
Bram Moolenaar33570922005-01-25 22:26:29 +000011169static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011170
11171 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011172libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011173 typval_T *argvars;
11174 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011175 int type;
11176{
11177#ifdef FEAT_LIBCALL
11178 char_u *string_in;
11179 char_u **string_result;
11180 int nr_result;
11181#endif
11182
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011183 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011184 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011185 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011186 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011187 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011188
11189 if (check_restricted() || check_secure())
11190 return;
11191
11192#ifdef FEAT_LIBCALL
11193 /* The first two args must be strings, otherwise its meaningless */
11194 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
11195 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011196 string_in = NULL;
11197 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011198 string_in = argvars[2].vval.v_string;
11199 if (type == VAR_NUMBER)
11200 string_result = NULL;
11201 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011202 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011203 if (mch_libcall(argvars[0].vval.v_string,
11204 argvars[1].vval.v_string,
11205 string_in,
11206 argvars[2].vval.v_number,
11207 string_result,
11208 &nr_result) == OK
11209 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011210 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011211 }
11212#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011213}
11214
11215/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011216 * "libcall()" function
11217 */
11218 static void
11219f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011220 typval_T *argvars;
11221 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011222{
11223 libcall_common(argvars, rettv, VAR_STRING);
11224}
11225
11226/*
11227 * "libcallnr()" function
11228 */
11229 static void
11230f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011231 typval_T *argvars;
11232 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011233{
11234 libcall_common(argvars, rettv, VAR_NUMBER);
11235}
11236
11237/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011238 * "line(string)" function
11239 */
11240 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011241f_line(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 linenr_T lnum = 0;
11246 pos_T *fp;
11247
11248 fp = var2fpos(&argvars[0], TRUE);
11249 if (fp != NULL)
11250 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011251 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011252}
11253
11254/*
11255 * "line2byte(lnum)" function
11256 */
11257/*ARGSUSED*/
11258 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011259f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011260 typval_T *argvars;
11261 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011262{
11263#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011264 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011265#else
11266 linenr_T lnum;
11267
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011268 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011269 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011270 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011271 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011272 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
11273 if (rettv->vval.v_number >= 0)
11274 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011275#endif
11276}
11277
11278/*
11279 * "lispindent(lnum)" function
11280 */
11281 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011282f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011283 typval_T *argvars;
11284 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011285{
11286#ifdef FEAT_LISP
11287 pos_T pos;
11288 linenr_T lnum;
11289
11290 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011291 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011292 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11293 {
11294 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011295 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011296 curwin->w_cursor = pos;
11297 }
11298 else
11299#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011300 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011301}
11302
11303/*
11304 * "localtime()" function
11305 */
11306/*ARGSUSED*/
11307 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011308f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011309 typval_T *argvars;
11310 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011311{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011312 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011313}
11314
Bram Moolenaar33570922005-01-25 22:26:29 +000011315static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011316
11317 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011318get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000011319 typval_T *argvars;
11320 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011321 int exact;
11322{
11323 char_u *keys;
11324 char_u *which;
11325 char_u buf[NUMBUFLEN];
11326 char_u *keys_buf = NULL;
11327 char_u *rhs;
11328 int mode;
11329 garray_T ga;
11330
11331 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011332 rettv->v_type = VAR_STRING;
11333 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011334
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011335 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011336 if (*keys == NUL)
11337 return;
11338
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011339 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011340 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011341 else
11342 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011343 if (which == NULL)
11344 return;
11345
Bram Moolenaar071d4272004-06-13 20:20:40 +000011346 mode = get_map_mode(&which, 0);
11347
11348 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000011349 rhs = check_map(keys, mode, exact, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011350 vim_free(keys_buf);
11351 if (rhs != NULL)
11352 {
11353 ga_init(&ga);
11354 ga.ga_itemsize = 1;
11355 ga.ga_growsize = 40;
11356
11357 while (*rhs != NUL)
11358 ga_concat(&ga, str2special(&rhs, FALSE));
11359
11360 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011361 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011362 }
11363}
11364
11365/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011366 * "map()" function
11367 */
11368 static void
11369f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011370 typval_T *argvars;
11371 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011372{
11373 filter_map(argvars, rettv, TRUE);
11374}
11375
11376/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011377 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011378 */
11379 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011380f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011381 typval_T *argvars;
11382 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011383{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011384 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011385}
11386
11387/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011388 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011389 */
11390 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011391f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011392 typval_T *argvars;
11393 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011394{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011395 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011396}
11397
Bram Moolenaar33570922005-01-25 22:26:29 +000011398static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011399
11400 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011401find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011402 typval_T *argvars;
11403 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011404 int type;
11405{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011406 char_u *str = NULL;
11407 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011408 char_u *pat;
11409 regmatch_T regmatch;
11410 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011411 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011412 char_u *save_cpo;
11413 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011414 long nth = 1;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011415 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011416 list_T *l = NULL;
11417 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011418 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011419 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011420
11421 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11422 save_cpo = p_cpo;
11423 p_cpo = (char_u *)"";
11424
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011425 rettv->vval.v_number = -1;
11426 if (type == 3)
11427 {
11428 /* return empty list when there are no matches */
11429 if ((rettv->vval.v_list = list_alloc()) == NULL)
11430 goto theend;
11431 rettv->v_type = VAR_LIST;
11432 ++rettv->vval.v_list->lv_refcount;
11433 }
11434 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011435 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011436 rettv->v_type = VAR_STRING;
11437 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011439
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011440 if (argvars[0].v_type == VAR_LIST)
11441 {
11442 if ((l = argvars[0].vval.v_list) == NULL)
11443 goto theend;
11444 li = l->lv_first;
11445 }
11446 else
11447 expr = str = get_tv_string(&argvars[0]);
11448
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011449 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11450 if (pat == NULL)
11451 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011452
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011453 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011454 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011455 int error = FALSE;
11456
11457 start = get_tv_number_chk(&argvars[2], &error);
11458 if (error)
11459 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011460 if (l != NULL)
11461 {
11462 li = list_find(l, start);
11463 if (li == NULL)
11464 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000011465 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011466 }
11467 else
11468 {
11469 if (start < 0)
11470 start = 0;
11471 if (start > (long)STRLEN(str))
11472 goto theend;
11473 str += start;
11474 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011475
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011476 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011477 nth = get_tv_number_chk(&argvars[3], &error);
11478 if (error)
11479 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011480 }
11481
11482 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11483 if (regmatch.regprog != NULL)
11484 {
11485 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011486
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000011487 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011488 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011489 if (l != NULL)
11490 {
11491 if (li == NULL)
11492 {
11493 match = FALSE;
11494 break;
11495 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011496 vim_free(tofree);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011497 str = echo_string(&li->li_tv, &tofree, strbuf);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011498 if (str == NULL)
11499 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011500 }
11501
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011502 match = vim_regexec_nl(&regmatch, str, (colnr_T)0);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011503
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011504 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011505 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011506 if (l == NULL && !match)
11507 break;
11508
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011509 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011510 if (l != NULL)
11511 {
11512 li = li->li_next;
11513 ++idx;
11514 }
11515 else
11516 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011517#ifdef FEAT_MBYTE
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011518 str = regmatch.startp[0] + mb_ptr2len_check(regmatch.startp[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011519#else
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011520 str = regmatch.startp[0] + 1;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011521#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011522 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011523 }
11524
11525 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011526 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011527 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011528 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011529 int i;
11530
11531 /* return list with matched string and submatches */
11532 for (i = 0; i < NSUBEXP; ++i)
11533 {
11534 if (regmatch.endp[i] == NULL)
11535 break;
11536 li = listitem_alloc();
11537 if (li == NULL)
11538 break;
11539 li->li_tv.v_type = VAR_STRING;
11540 li->li_tv.v_lock = 0;
11541 li->li_tv.vval.v_string = vim_strnsave(regmatch.startp[i],
11542 (int)(regmatch.endp[i] - regmatch.startp[i]));
11543 list_append(rettv->vval.v_list, li);
11544 }
11545 }
11546 else if (type == 2)
11547 {
11548 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011549 if (l != NULL)
11550 copy_tv(&li->li_tv, rettv);
11551 else
11552 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000011553 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011554 }
11555 else if (l != NULL)
11556 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011557 else
11558 {
11559 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011560 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011561 (varnumber_T)(regmatch.startp[0] - str);
11562 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011563 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011564 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011565 rettv->vval.v_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011566 }
11567 }
11568 vim_free(regmatch.regprog);
11569 }
11570
11571theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011572 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011573 p_cpo = save_cpo;
11574}
11575
11576/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011577 * "match()" function
11578 */
11579 static void
11580f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011581 typval_T *argvars;
11582 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011583{
11584 find_some_match(argvars, rettv, 1);
11585}
11586
11587/*
11588 * "matchend()" function
11589 */
11590 static void
11591f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011592 typval_T *argvars;
11593 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011594{
11595 find_some_match(argvars, rettv, 0);
11596}
11597
11598/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011599 * "matchlist()" function
11600 */
11601 static void
11602f_matchlist(argvars, rettv)
11603 typval_T *argvars;
11604 typval_T *rettv;
11605{
11606 find_some_match(argvars, rettv, 3);
11607}
11608
11609/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011610 * "matchstr()" function
11611 */
11612 static void
11613f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011614 typval_T *argvars;
11615 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011616{
11617 find_some_match(argvars, rettv, 2);
11618}
11619
Bram Moolenaar33570922005-01-25 22:26:29 +000011620static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011621
11622 static void
11623max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000011624 typval_T *argvars;
11625 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011626 int domax;
11627{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011628 long n = 0;
11629 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011630 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011631
11632 if (argvars[0].v_type == VAR_LIST)
11633 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011634 list_T *l;
11635 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011636
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011637 l = argvars[0].vval.v_list;
11638 if (l != NULL)
11639 {
11640 li = l->lv_first;
11641 if (li != NULL)
11642 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011643 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000011644 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011645 {
11646 li = li->li_next;
11647 if (li == NULL)
11648 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011649 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011650 if (domax ? i > n : i < n)
11651 n = i;
11652 }
11653 }
11654 }
11655 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011656 else if (argvars[0].v_type == VAR_DICT)
11657 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011658 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011659 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000011660 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011661 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011662
11663 d = argvars[0].vval.v_dict;
11664 if (d != NULL)
11665 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011666 todo = d->dv_hashtab.ht_used;
11667 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011668 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011669 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011670 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011671 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011672 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011673 if (first)
11674 {
11675 n = i;
11676 first = FALSE;
11677 }
11678 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011679 n = i;
11680 }
11681 }
11682 }
11683 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011684 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000011685 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011686 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011687}
11688
11689/*
11690 * "max()" function
11691 */
11692 static void
11693f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011694 typval_T *argvars;
11695 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011696{
11697 max_min(argvars, rettv, TRUE);
11698}
11699
11700/*
11701 * "min()" function
11702 */
11703 static void
11704f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011705 typval_T *argvars;
11706 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011707{
11708 max_min(argvars, rettv, FALSE);
11709}
11710
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011711static int mkdir_recurse __ARGS((char_u *dir, int prot));
11712
11713/*
11714 * Create the directory in which "dir" is located, and higher levels when
11715 * needed.
11716 */
11717 static int
11718mkdir_recurse(dir, prot)
11719 char_u *dir;
11720 int prot;
11721{
11722 char_u *p;
11723 char_u *updir;
11724 int r = FAIL;
11725
11726 /* Get end of directory name in "dir".
11727 * We're done when it's "/" or "c:/". */
11728 p = gettail_sep(dir);
11729 if (p <= get_past_head(dir))
11730 return OK;
11731
11732 /* If the directory exists we're done. Otherwise: create it.*/
11733 updir = vim_strnsave(dir, (int)(p - dir));
11734 if (updir == NULL)
11735 return FAIL;
11736 if (mch_isdir(updir))
11737 r = OK;
11738 else if (mkdir_recurse(updir, prot) == OK)
11739 r = vim_mkdir_emsg(updir, prot);
11740 vim_free(updir);
11741 return r;
11742}
11743
11744#ifdef vim_mkdir
11745/*
11746 * "mkdir()" function
11747 */
11748 static void
11749f_mkdir(argvars, rettv)
11750 typval_T *argvars;
11751 typval_T *rettv;
11752{
11753 char_u *dir;
11754 char_u buf[NUMBUFLEN];
11755 int prot = 0755;
11756
11757 rettv->vval.v_number = FAIL;
11758 if (check_restricted() || check_secure())
11759 return;
11760
11761 dir = get_tv_string_buf(&argvars[0], buf);
11762 if (argvars[1].v_type != VAR_UNKNOWN)
11763 {
11764 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011765 prot = get_tv_number_chk(&argvars[2], NULL);
11766 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011767 mkdir_recurse(dir, prot);
11768 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011769 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011770}
11771#endif
11772
Bram Moolenaar0d660222005-01-07 21:51:51 +000011773/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011774 * "mode()" function
11775 */
11776/*ARGSUSED*/
11777 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011778f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011779 typval_T *argvars;
11780 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011781{
11782 char_u buf[2];
11783
11784#ifdef FEAT_VISUAL
11785 if (VIsual_active)
11786 {
11787 if (VIsual_select)
11788 buf[0] = VIsual_mode + 's' - 'v';
11789 else
11790 buf[0] = VIsual_mode;
11791 }
11792 else
11793#endif
11794 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
11795 buf[0] = 'r';
11796 else if (State & INSERT)
11797 {
11798 if (State & REPLACE_FLAG)
11799 buf[0] = 'R';
11800 else
11801 buf[0] = 'i';
11802 }
11803 else if (State & CMDLINE)
11804 buf[0] = 'c';
11805 else
11806 buf[0] = 'n';
11807
11808 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011809 rettv->vval.v_string = vim_strsave(buf);
11810 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011811}
11812
11813/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011814 * "nextnonblank()" function
11815 */
11816 static void
11817f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011818 typval_T *argvars;
11819 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011820{
11821 linenr_T lnum;
11822
11823 for (lnum = get_tv_lnum(argvars); ; ++lnum)
11824 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011825 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011826 {
11827 lnum = 0;
11828 break;
11829 }
11830 if (*skipwhite(ml_get(lnum)) != NUL)
11831 break;
11832 }
11833 rettv->vval.v_number = lnum;
11834}
11835
11836/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011837 * "nr2char()" function
11838 */
11839 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011840f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011841 typval_T *argvars;
11842 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011843{
11844 char_u buf[NUMBUFLEN];
11845
11846#ifdef FEAT_MBYTE
11847 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011848 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011849 else
11850#endif
11851 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011852 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011853 buf[1] = NUL;
11854 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011855 rettv->v_type = VAR_STRING;
11856 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011857}
11858
11859/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011860 * "prevnonblank()" function
11861 */
11862 static void
11863f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011864 typval_T *argvars;
11865 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011866{
11867 linenr_T lnum;
11868
11869 lnum = get_tv_lnum(argvars);
11870 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
11871 lnum = 0;
11872 else
11873 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
11874 --lnum;
11875 rettv->vval.v_number = lnum;
11876}
11877
Bram Moolenaar8c711452005-01-14 21:53:12 +000011878/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000011879 * "printf()" function
11880 */
11881 static void
11882f_printf(argvars, rettv)
11883 typval_T *argvars;
11884 typval_T *rettv;
11885{
11886 rettv->v_type = VAR_STRING;
11887 rettv->vval.v_string = NULL;
11888#ifdef HAVE_STDARG_H
11889 {
11890 char_u buf[NUMBUFLEN];
11891 int len;
11892 char_u *s;
11893 int saved_did_emsg = did_emsg;
11894 char *fmt;
11895
11896 /* Get the required length, allocate the buffer and do it for real. */
11897 did_emsg = FALSE;
11898 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
11899 len = vim_vsnprintf(NULL, 0, fmt, NULL, argvars + 1);
11900 if (!did_emsg)
11901 {
11902 s = alloc(len + 1);
11903 if (s != NULL)
11904 {
11905 rettv->vval.v_string = s;
11906 (void)vim_vsnprintf((char *)s, len + 1, fmt, NULL, argvars + 1);
11907 }
11908 }
11909 did_emsg |= saved_did_emsg;
11910 }
11911#endif
11912}
11913
11914/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011915 * "range()" function
11916 */
11917 static void
11918f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011919 typval_T *argvars;
11920 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011921{
11922 long start;
11923 long end;
11924 long stride = 1;
11925 long i;
Bram Moolenaar33570922005-01-25 22:26:29 +000011926 list_T *l;
11927 listitem_T *li;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011928 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011929
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011930 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011931 if (argvars[1].v_type == VAR_UNKNOWN)
11932 {
11933 end = start - 1;
11934 start = 0;
11935 }
11936 else
11937 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011938 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011939 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011940 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011941 }
11942
11943 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011944 if (error)
11945 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000011946 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000011947 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000011948 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000011949 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011950 else
11951 {
11952 l = list_alloc();
11953 if (l != NULL)
11954 {
11955 rettv->v_type = VAR_LIST;
11956 rettv->vval.v_list = l;
11957 ++l->lv_refcount;
11958
11959 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
11960 {
11961 li = listitem_alloc();
11962 if (li == NULL)
11963 break;
11964 li->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011965 li->li_tv.v_lock = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011966 li->li_tv.vval.v_number = i;
11967 list_append(l, li);
11968 }
11969 }
11970 }
11971}
11972
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011973/*
11974 * "readfile()" function
11975 */
11976 static void
11977f_readfile(argvars, rettv)
11978 typval_T *argvars;
11979 typval_T *rettv;
11980{
11981 int binary = FALSE;
11982 char_u *fname;
11983 FILE *fd;
11984 list_T *l;
11985 listitem_T *li;
11986#define FREAD_SIZE 200 /* optimized for text lines */
11987 char_u buf[FREAD_SIZE];
11988 int readlen; /* size of last fread() */
11989 int buflen; /* nr of valid chars in buf[] */
11990 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
11991 int tolist; /* first byte in buf[] still to be put in list */
11992 int chop; /* how many CR to chop off */
11993 char_u *prev = NULL; /* previously read bytes, if any */
11994 int prevlen = 0; /* length of "prev" if not NULL */
11995 char_u *s;
11996 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011997 long maxline = MAXLNUM;
11998 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011999
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012000 if (argvars[1].v_type != VAR_UNKNOWN)
12001 {
12002 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
12003 binary = TRUE;
12004 if (argvars[2].v_type != VAR_UNKNOWN)
12005 maxline = get_tv_number(&argvars[2]);
12006 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012007
12008 l = list_alloc();
12009 if (l == NULL)
12010 return;
12011 rettv->v_type = VAR_LIST;
12012 rettv->vval.v_list = l;
12013 l->lv_refcount = 1;
12014
12015 /* Always open the file in binary mode, library functions have a mind of
12016 * their own about CR-LF conversion. */
12017 fname = get_tv_string(&argvars[0]);
12018 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
12019 {
12020 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
12021 return;
12022 }
12023
12024 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012025 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012026 {
12027 readlen = fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
12028 buflen = filtd + readlen;
12029 tolist = 0;
12030 for ( ; filtd < buflen || readlen <= 0; ++filtd)
12031 {
12032 if (buf[filtd] == '\n' || readlen <= 0)
12033 {
12034 /* Only when in binary mode add an empty list item when the
12035 * last line ends in a '\n'. */
12036 if (!binary && readlen == 0 && filtd == 0)
12037 break;
12038
12039 /* Found end-of-line or end-of-file: add a text line to the
12040 * list. */
12041 chop = 0;
12042 if (!binary)
12043 while (filtd - chop - 1 >= tolist
12044 && buf[filtd - chop - 1] == '\r')
12045 ++chop;
12046 len = filtd - tolist - chop;
12047 if (prev == NULL)
12048 s = vim_strnsave(buf + tolist, len);
12049 else
12050 {
12051 s = alloc((unsigned)(prevlen + len + 1));
12052 if (s != NULL)
12053 {
12054 mch_memmove(s, prev, prevlen);
12055 vim_free(prev);
12056 prev = NULL;
12057 mch_memmove(s + prevlen, buf + tolist, len);
12058 s[prevlen + len] = NUL;
12059 }
12060 }
12061 tolist = filtd + 1;
12062
12063 li = listitem_alloc();
12064 if (li == NULL)
12065 {
12066 vim_free(s);
12067 break;
12068 }
12069 li->li_tv.v_type = VAR_STRING;
12070 li->li_tv.v_lock = 0;
12071 li->li_tv.vval.v_string = s;
12072 list_append(l, li);
12073
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012074 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012075 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012076 if (readlen <= 0)
12077 break;
12078 }
12079 else if (buf[filtd] == NUL)
12080 buf[filtd] = '\n';
12081 }
12082 if (readlen <= 0)
12083 break;
12084
12085 if (tolist == 0)
12086 {
12087 /* "buf" is full, need to move text to an allocated buffer */
12088 if (prev == NULL)
12089 {
12090 prev = vim_strnsave(buf, buflen);
12091 prevlen = buflen;
12092 }
12093 else
12094 {
12095 s = alloc((unsigned)(prevlen + buflen));
12096 if (s != NULL)
12097 {
12098 mch_memmove(s, prev, prevlen);
12099 mch_memmove(s + prevlen, buf, buflen);
12100 vim_free(prev);
12101 prev = s;
12102 prevlen += buflen;
12103 }
12104 }
12105 filtd = 0;
12106 }
12107 else
12108 {
12109 mch_memmove(buf, buf + tolist, buflen - tolist);
12110 filtd -= tolist;
12111 }
12112 }
12113
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012114 /*
12115 * For a negative line count use only the lines at the end of the file,
12116 * free the rest.
12117 */
12118 if (maxline < 0)
12119 while (cnt > -maxline)
12120 {
12121 listitem_remove(l, l->lv_first);
12122 --cnt;
12123 }
12124
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012125 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012126 fclose(fd);
12127}
12128
12129
Bram Moolenaar0d660222005-01-07 21:51:51 +000012130#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
12131static void make_connection __ARGS((void));
12132static int check_connection __ARGS((void));
12133
12134 static void
12135make_connection()
12136{
12137 if (X_DISPLAY == NULL
12138# ifdef FEAT_GUI
12139 && !gui.in_use
12140# endif
12141 )
12142 {
12143 x_force_connect = TRUE;
12144 setup_term_clip();
12145 x_force_connect = FALSE;
12146 }
12147}
12148
12149 static int
12150check_connection()
12151{
12152 make_connection();
12153 if (X_DISPLAY == NULL)
12154 {
12155 EMSG(_("E240: No connection to Vim server"));
12156 return FAIL;
12157 }
12158 return OK;
12159}
12160#endif
12161
12162#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012163static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012164
12165 static void
12166remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000012167 typval_T *argvars;
12168 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012169 int expr;
12170{
12171 char_u *server_name;
12172 char_u *keys;
12173 char_u *r = NULL;
12174 char_u buf[NUMBUFLEN];
12175# ifdef WIN32
12176 HWND w;
12177# else
12178 Window w;
12179# endif
12180
12181 if (check_restricted() || check_secure())
12182 return;
12183
12184# ifdef FEAT_X11
12185 if (check_connection() == FAIL)
12186 return;
12187# endif
12188
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012189 server_name = get_tv_string_chk(&argvars[0]);
12190 if (server_name == NULL)
12191 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012192 keys = get_tv_string_buf(&argvars[1], buf);
12193# ifdef WIN32
12194 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
12195# else
12196 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
12197 < 0)
12198# endif
12199 {
12200 if (r != NULL)
12201 EMSG(r); /* sending worked but evaluation failed */
12202 else
12203 EMSG2(_("E241: Unable to send to %s"), server_name);
12204 return;
12205 }
12206
12207 rettv->vval.v_string = r;
12208
12209 if (argvars[2].v_type != VAR_UNKNOWN)
12210 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012211 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000012212 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012213 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012214
12215 sprintf((char *)str, "0x%x", (unsigned int)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000012216 v.di_tv.v_type = VAR_STRING;
12217 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012218 idvar = get_tv_string_chk(&argvars[2]);
12219 if (idvar != NULL)
12220 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012221 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012222 }
12223}
12224#endif
12225
12226/*
12227 * "remote_expr()" function
12228 */
12229/*ARGSUSED*/
12230 static void
12231f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012232 typval_T *argvars;
12233 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012234{
12235 rettv->v_type = VAR_STRING;
12236 rettv->vval.v_string = NULL;
12237#ifdef FEAT_CLIENTSERVER
12238 remote_common(argvars, rettv, TRUE);
12239#endif
12240}
12241
12242/*
12243 * "remote_foreground()" function
12244 */
12245/*ARGSUSED*/
12246 static void
12247f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012248 typval_T *argvars;
12249 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012250{
12251 rettv->vval.v_number = 0;
12252#ifdef FEAT_CLIENTSERVER
12253# ifdef WIN32
12254 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012255 {
12256 char_u *server_name = get_tv_string_chk(&argvars[0]);
12257
12258 if (server_name != NULL)
12259 serverForeground(server_name);
12260 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012261# else
12262 /* Send a foreground() expression to the server. */
12263 argvars[1].v_type = VAR_STRING;
12264 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
12265 argvars[2].v_type = VAR_UNKNOWN;
12266 remote_common(argvars, rettv, TRUE);
12267 vim_free(argvars[1].vval.v_string);
12268# endif
12269#endif
12270}
12271
12272/*ARGSUSED*/
12273 static void
12274f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012275 typval_T *argvars;
12276 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012277{
12278#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012279 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012280 char_u *s = NULL;
12281# ifdef WIN32
12282 int n = 0;
12283# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012284 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012285
12286 if (check_restricted() || check_secure())
12287 {
12288 rettv->vval.v_number = -1;
12289 return;
12290 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012291 serverid = get_tv_string_chk(&argvars[0]);
12292 if (serverid == NULL)
12293 {
12294 rettv->vval.v_number = -1;
12295 return; /* type error; errmsg already given */
12296 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012297# ifdef WIN32
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012298 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012299 if (n == 0)
12300 rettv->vval.v_number = -1;
12301 else
12302 {
12303 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
12304 rettv->vval.v_number = (s != NULL);
12305 }
12306# else
12307 rettv->vval.v_number = 0;
12308 if (check_connection() == FAIL)
12309 return;
12310
12311 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012312 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012313# endif
12314
12315 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
12316 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012317 char_u *retvar;
12318
Bram Moolenaar33570922005-01-25 22:26:29 +000012319 v.di_tv.v_type = VAR_STRING;
12320 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012321 retvar = get_tv_string_chk(&argvars[1]);
12322 if (retvar != NULL)
12323 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012324 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012325 }
12326#else
12327 rettv->vval.v_number = -1;
12328#endif
12329}
12330
12331/*ARGSUSED*/
12332 static void
12333f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012334 typval_T *argvars;
12335 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012336{
12337 char_u *r = NULL;
12338
12339#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012340 char_u *serverid = get_tv_string_chk(&argvars[0]);
12341
12342 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000012343 {
12344# ifdef WIN32
12345 /* The server's HWND is encoded in the 'id' parameter */
12346 int n = 0;
12347
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012348 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012349 if (n != 0)
12350 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
12351 if (r == NULL)
12352# else
12353 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012354 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012355# endif
12356 EMSG(_("E277: Unable to read a server reply"));
12357 }
12358#endif
12359 rettv->v_type = VAR_STRING;
12360 rettv->vval.v_string = r;
12361}
12362
12363/*
12364 * "remote_send()" function
12365 */
12366/*ARGSUSED*/
12367 static void
12368f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012369 typval_T *argvars;
12370 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012371{
12372 rettv->v_type = VAR_STRING;
12373 rettv->vval.v_string = NULL;
12374#ifdef FEAT_CLIENTSERVER
12375 remote_common(argvars, rettv, FALSE);
12376#endif
12377}
12378
12379/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012380 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012381 */
12382 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012383f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012384 typval_T *argvars;
12385 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012386{
Bram Moolenaar33570922005-01-25 22:26:29 +000012387 list_T *l;
12388 listitem_T *item, *item2;
12389 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012390 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012391 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012392 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000012393 dict_T *d;
12394 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012395
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012396 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012397 if (argvars[0].v_type == VAR_DICT)
12398 {
12399 if (argvars[2].v_type != VAR_UNKNOWN)
12400 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012401 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar758711c2005-02-02 23:11:38 +000012402 && !tv_check_lock(d->dv_lock, (char_u *)"remove()"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000012403 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012404 key = get_tv_string_chk(&argvars[1]);
12405 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012406 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012407 di = dict_find(d, key, -1);
12408 if (di == NULL)
12409 EMSG2(_(e_dictkey), key);
12410 else
12411 {
12412 *rettv = di->di_tv;
12413 init_tv(&di->di_tv);
12414 dictitem_remove(d, di);
12415 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012416 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012417 }
12418 }
12419 else if (argvars[0].v_type != VAR_LIST)
12420 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012421 else if ((l = argvars[0].vval.v_list) != NULL
12422 && !tv_check_lock(l->lv_lock, (char_u *)"remove()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012423 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012424 int error = FALSE;
12425
12426 idx = get_tv_number_chk(&argvars[1], &error);
12427 if (error)
12428 ; /* type error: do nothing, errmsg already given */
12429 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012430 EMSGN(_(e_listidx), idx);
12431 else
12432 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012433 if (argvars[2].v_type == VAR_UNKNOWN)
12434 {
12435 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012436 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012437 *rettv = item->li_tv;
12438 vim_free(item);
12439 }
12440 else
12441 {
12442 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012443 end = get_tv_number_chk(&argvars[2], &error);
12444 if (error)
12445 ; /* type error: do nothing */
12446 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012447 EMSGN(_(e_listidx), end);
12448 else
12449 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012450 int cnt = 0;
12451
12452 for (li = item; li != NULL; li = li->li_next)
12453 {
12454 ++cnt;
12455 if (li == item2)
12456 break;
12457 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012458 if (li == NULL) /* didn't find "item2" after "item" */
12459 EMSG(_(e_invrange));
12460 else
12461 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012462 list_remove(l, item, item2);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012463 l = list_alloc();
12464 if (l != NULL)
12465 {
12466 rettv->v_type = VAR_LIST;
12467 rettv->vval.v_list = l;
12468 l->lv_first = item;
12469 l->lv_last = item2;
12470 l->lv_refcount = 1;
12471 item->li_prev = NULL;
12472 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012473 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012474 }
12475 }
12476 }
12477 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012478 }
12479 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012480}
12481
12482/*
12483 * "rename({from}, {to})" function
12484 */
12485 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012486f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012487 typval_T *argvars;
12488 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012489{
12490 char_u buf[NUMBUFLEN];
12491
12492 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012493 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012494 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012495 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
12496 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012497}
12498
12499/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012500 * "repeat()" function
12501 */
12502/*ARGSUSED*/
12503 static void
12504f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012505 typval_T *argvars;
12506 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012507{
12508 char_u *p;
12509 int n;
12510 int slen;
12511 int len;
12512 char_u *r;
12513 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000012514 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012515
12516 n = get_tv_number(&argvars[1]);
12517 if (argvars[0].v_type == VAR_LIST)
12518 {
12519 l = list_alloc();
12520 if (l != NULL && argvars[0].vval.v_list != NULL)
12521 {
12522 l->lv_refcount = 1;
12523 while (n-- > 0)
12524 if (list_extend(l, argvars[0].vval.v_list, NULL) == FAIL)
12525 break;
12526 }
12527 rettv->v_type = VAR_LIST;
12528 rettv->vval.v_list = l;
12529 }
12530 else
12531 {
12532 p = get_tv_string(&argvars[0]);
12533 rettv->v_type = VAR_STRING;
12534 rettv->vval.v_string = NULL;
12535
12536 slen = (int)STRLEN(p);
12537 len = slen * n;
12538 if (len <= 0)
12539 return;
12540
12541 r = alloc(len + 1);
12542 if (r != NULL)
12543 {
12544 for (i = 0; i < n; i++)
12545 mch_memmove(r + i * slen, p, (size_t)slen);
12546 r[len] = NUL;
12547 }
12548
12549 rettv->vval.v_string = r;
12550 }
12551}
12552
12553/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012554 * "resolve()" function
12555 */
12556 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012557f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012558 typval_T *argvars;
12559 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012560{
12561 char_u *p;
12562
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012563 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012564#ifdef FEAT_SHORTCUT
12565 {
12566 char_u *v = NULL;
12567
12568 v = mch_resolve_shortcut(p);
12569 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012570 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012571 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012572 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012573 }
12574#else
12575# ifdef HAVE_READLINK
12576 {
12577 char_u buf[MAXPATHL + 1];
12578 char_u *cpy;
12579 int len;
12580 char_u *remain = NULL;
12581 char_u *q;
12582 int is_relative_to_current = FALSE;
12583 int has_trailing_pathsep = FALSE;
12584 int limit = 100;
12585
12586 p = vim_strsave(p);
12587
12588 if (p[0] == '.' && (vim_ispathsep(p[1])
12589 || (p[1] == '.' && (vim_ispathsep(p[2])))))
12590 is_relative_to_current = TRUE;
12591
12592 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000012593 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012594 has_trailing_pathsep = TRUE;
12595
12596 q = getnextcomp(p);
12597 if (*q != NUL)
12598 {
12599 /* Separate the first path component in "p", and keep the
12600 * remainder (beginning with the path separator). */
12601 remain = vim_strsave(q - 1);
12602 q[-1] = NUL;
12603 }
12604
12605 for (;;)
12606 {
12607 for (;;)
12608 {
12609 len = readlink((char *)p, (char *)buf, MAXPATHL);
12610 if (len <= 0)
12611 break;
12612 buf[len] = NUL;
12613
12614 if (limit-- == 0)
12615 {
12616 vim_free(p);
12617 vim_free(remain);
12618 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012619 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012620 goto fail;
12621 }
12622
12623 /* Ensure that the result will have a trailing path separator
12624 * if the argument has one. */
12625 if (remain == NULL && has_trailing_pathsep)
12626 add_pathsep(buf);
12627
12628 /* Separate the first path component in the link value and
12629 * concatenate the remainders. */
12630 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
12631 if (*q != NUL)
12632 {
12633 if (remain == NULL)
12634 remain = vim_strsave(q - 1);
12635 else
12636 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012637 cpy = vim_strnsave(q-1, STRLEN(q-1) + STRLEN(remain));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012638 if (cpy != NULL)
12639 {
12640 STRCAT(cpy, remain);
12641 vim_free(remain);
12642 remain = cpy;
12643 }
12644 }
12645 q[-1] = NUL;
12646 }
12647
12648 q = gettail(p);
12649 if (q > p && *q == NUL)
12650 {
12651 /* Ignore trailing path separator. */
12652 q[-1] = NUL;
12653 q = gettail(p);
12654 }
12655 if (q > p && !mch_isFullName(buf))
12656 {
12657 /* symlink is relative to directory of argument */
12658 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
12659 if (cpy != NULL)
12660 {
12661 STRCPY(cpy, p);
12662 STRCPY(gettail(cpy), buf);
12663 vim_free(p);
12664 p = cpy;
12665 }
12666 }
12667 else
12668 {
12669 vim_free(p);
12670 p = vim_strsave(buf);
12671 }
12672 }
12673
12674 if (remain == NULL)
12675 break;
12676
12677 /* Append the first path component of "remain" to "p". */
12678 q = getnextcomp(remain + 1);
12679 len = q - remain - (*q != NUL);
12680 cpy = vim_strnsave(p, STRLEN(p) + len);
12681 if (cpy != NULL)
12682 {
12683 STRNCAT(cpy, remain, len);
12684 vim_free(p);
12685 p = cpy;
12686 }
12687 /* Shorten "remain". */
12688 if (*q != NUL)
12689 STRCPY(remain, q - 1);
12690 else
12691 {
12692 vim_free(remain);
12693 remain = NULL;
12694 }
12695 }
12696
12697 /* If the result is a relative path name, make it explicitly relative to
12698 * the current directory if and only if the argument had this form. */
12699 if (!vim_ispathsep(*p))
12700 {
12701 if (is_relative_to_current
12702 && *p != NUL
12703 && !(p[0] == '.'
12704 && (p[1] == NUL
12705 || vim_ispathsep(p[1])
12706 || (p[1] == '.'
12707 && (p[2] == NUL
12708 || vim_ispathsep(p[2]))))))
12709 {
12710 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012711 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012712 if (cpy != NULL)
12713 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012714 vim_free(p);
12715 p = cpy;
12716 }
12717 }
12718 else if (!is_relative_to_current)
12719 {
12720 /* Strip leading "./". */
12721 q = p;
12722 while (q[0] == '.' && vim_ispathsep(q[1]))
12723 q += 2;
12724 if (q > p)
12725 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
12726 }
12727 }
12728
12729 /* Ensure that the result will have no trailing path separator
12730 * if the argument had none. But keep "/" or "//". */
12731 if (!has_trailing_pathsep)
12732 {
12733 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000012734 if (after_pathsep(p, q))
12735 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012736 }
12737
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012738 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012739 }
12740# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012741 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012742# endif
12743#endif
12744
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012745 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012746
12747#ifdef HAVE_READLINK
12748fail:
12749#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012750 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012751}
12752
12753/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012754 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012755 */
12756 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012757f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012758 typval_T *argvars;
12759 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012760{
Bram Moolenaar33570922005-01-25 22:26:29 +000012761 list_T *l;
12762 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012763
Bram Moolenaar0d660222005-01-07 21:51:51 +000012764 rettv->vval.v_number = 0;
12765 if (argvars[0].v_type != VAR_LIST)
12766 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012767 else if ((l = argvars[0].vval.v_list) != NULL
12768 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000012769 {
12770 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012771 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012772 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012773 while (li != NULL)
12774 {
12775 ni = li->li_prev;
12776 list_append(l, li);
12777 li = ni;
12778 }
12779 rettv->vval.v_list = l;
12780 rettv->v_type = VAR_LIST;
12781 ++l->lv_refcount;
12782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012783}
12784
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012785#define SP_NOMOVE 1 /* don't move cursor */
12786#define SP_REPEAT 2 /* repeat to find outer pair */
12787#define SP_RETCOUNT 4 /* return matchcount */
Bram Moolenaar231334e2005-07-25 20:46:57 +000012788#define SP_SETPCMARK 8 /* set previous context mark */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012789
Bram Moolenaar33570922005-01-25 22:26:29 +000012790static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012791
12792/*
12793 * Get flags for a search function.
12794 * Possibly sets "p_ws".
12795 * Returns BACKWARD, FORWARD or zero (for an error).
12796 */
12797 static int
12798get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000012799 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012800 int *flagsp;
12801{
12802 int dir = FORWARD;
12803 char_u *flags;
12804 char_u nbuf[NUMBUFLEN];
12805 int mask;
12806
12807 if (varp->v_type != VAR_UNKNOWN)
12808 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012809 flags = get_tv_string_buf_chk(varp, nbuf);
12810 if (flags == NULL)
12811 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012812 while (*flags != NUL)
12813 {
12814 switch (*flags)
12815 {
12816 case 'b': dir = BACKWARD; break;
12817 case 'w': p_ws = TRUE; break;
12818 case 'W': p_ws = FALSE; break;
12819 default: mask = 0;
12820 if (flagsp != NULL)
12821 switch (*flags)
12822 {
12823 case 'n': mask = SP_NOMOVE; break;
12824 case 'r': mask = SP_REPEAT; break;
12825 case 'm': mask = SP_RETCOUNT; break;
Bram Moolenaar231334e2005-07-25 20:46:57 +000012826 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012827 }
12828 if (mask == 0)
12829 {
12830 EMSG2(_(e_invarg2), flags);
12831 dir = 0;
12832 }
12833 else
12834 *flagsp |= mask;
12835 }
12836 if (dir == 0)
12837 break;
12838 ++flags;
12839 }
12840 }
12841 return dir;
12842}
12843
Bram Moolenaar071d4272004-06-13 20:20:40 +000012844/*
12845 * "search()" function
12846 */
12847 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012848f_search(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012849 typval_T *argvars;
12850 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012851{
12852 char_u *pat;
12853 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012854 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012855 int save_p_ws = p_ws;
12856 int dir;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012857 int flags = 0;
12858
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012859 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012860
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012861 pat = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012862 dir = get_search_arg(&argvars[1], &flags); /* may set p_ws */
12863 if (dir == 0)
12864 goto theend;
Bram Moolenaar231334e2005-07-25 20:46:57 +000012865 /*
12866 * This function accepts only SP_NOMOVE and SP_SETPCMARK flags.
12867 * Check to make sure only those flags are set.
12868 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
12869 * flags cannot be set. Check for that condition also.
12870 */
12871 if (((flags & ~(SP_NOMOVE | SP_SETPCMARK)) != 0) ||
12872 ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012873 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012874 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012875 goto theend;
12876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012877
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012878 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012879 if (searchit(curwin, curbuf, &pos, dir, pat, 1L,
12880 SEARCH_KEEP, RE_SEARCH) != FAIL)
12881 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012882 rettv->vval.v_number = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000012883 if (flags & SP_SETPCMARK)
12884 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012885 curwin->w_cursor = pos;
12886 /* "/$" will put the cursor after the end of the line, may need to
12887 * correct that here */
12888 check_cursor();
12889 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012890
12891 /* If 'n' flag is used: restore cursor position. */
12892 if (flags & SP_NOMOVE)
12893 curwin->w_cursor = save_cursor;
12894theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000012895 p_ws = save_p_ws;
12896}
12897
Bram Moolenaar071d4272004-06-13 20:20:40 +000012898/*
12899 * "searchpair()" function
12900 */
12901 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012902f_searchpair(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012903 typval_T *argvars;
12904 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012905{
12906 char_u *spat, *mpat, *epat;
12907 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012908 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012909 int dir;
12910 int flags = 0;
12911 char_u nbuf1[NUMBUFLEN];
12912 char_u nbuf2[NUMBUFLEN];
12913 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000012914
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012915 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012916
Bram Moolenaar071d4272004-06-13 20:20:40 +000012917 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012918 spat = get_tv_string_chk(&argvars[0]);
12919 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
12920 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
12921 if (spat == NULL || mpat == NULL || epat == NULL)
12922 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012923
Bram Moolenaar071d4272004-06-13 20:20:40 +000012924 /* Handle the optional fourth argument: flags */
12925 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012926 if (dir == 0)
12927 goto theend;
Bram Moolenaar231334e2005-07-25 20:46:57 +000012928 /*
12929 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
12930 */
12931 if ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK))
12932 {
12933 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
12934 goto theend;
12935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012936
12937 /* Optional fifth argument: skip expresion */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012938 if (argvars[3].v_type == VAR_UNKNOWN
12939 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012940 skip = (char_u *)"";
12941 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012942 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
12943 if (skip == NULL)
12944 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012945
Bram Moolenaar9fad3082005-07-19 22:22:13 +000012946 rettv->vval.v_number = do_searchpair(spat, mpat, epat, dir, skip, flags);
12947
12948theend:
12949 p_ws = save_p_ws;
12950}
12951
12952/*
12953 * Search for a start/middle/end thing.
12954 * Used by searchpair(), see its documentation for the details.
12955 * Returns 0 or -1 for no match,
12956 */
12957 long
12958do_searchpair(spat, mpat, epat, dir, skip, flags)
12959 char_u *spat; /* start pattern */
12960 char_u *mpat; /* middle pattern */
12961 char_u *epat; /* end pattern */
12962 int dir; /* BACKWARD or FORWARD */
12963 char_u *skip; /* skip expression */
12964 int flags; /* SP_RETCOUNT, SP_REPEAT, SP_NOMOVE */
12965{
12966 char_u *save_cpo;
12967 char_u *pat, *pat2 = NULL, *pat3 = NULL;
12968 long retval = 0;
12969 pos_T pos;
12970 pos_T firstpos;
12971 pos_T foundpos;
12972 pos_T save_cursor;
12973 pos_T save_pos;
12974 int n;
12975 int r;
12976 int nest = 1;
12977 int err;
12978
12979 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12980 save_cpo = p_cpo;
12981 p_cpo = (char_u *)"";
12982
12983 /* Make two search patterns: start/end (pat2, for in nested pairs) and
12984 * start/middle/end (pat3, for the top pair). */
12985 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
12986 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
12987 if (pat2 == NULL || pat3 == NULL)
12988 goto theend;
12989 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
12990 if (*mpat == NUL)
12991 STRCPY(pat3, pat2);
12992 else
12993 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
12994 spat, epat, mpat);
12995
Bram Moolenaar071d4272004-06-13 20:20:40 +000012996 save_cursor = curwin->w_cursor;
12997 pos = curwin->w_cursor;
12998 firstpos.lnum = 0;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000012999 foundpos.lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013000 pat = pat3;
13001 for (;;)
13002 {
13003 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
13004 SEARCH_KEEP, RE_SEARCH);
13005 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
13006 /* didn't find it or found the first match again: FAIL */
13007 break;
13008
13009 if (firstpos.lnum == 0)
13010 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000013011 if (equalpos(pos, foundpos))
13012 {
13013 /* Found the same position again. Can happen with a pattern that
13014 * has "\zs" at the end and searching backwards. Advance one
13015 * character and try again. */
13016 if (dir == BACKWARD)
13017 decl(&pos);
13018 else
13019 incl(&pos);
13020 }
13021 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013022
13023 /* If the skip pattern matches, ignore this match. */
13024 if (*skip != NUL)
13025 {
13026 save_pos = curwin->w_cursor;
13027 curwin->w_cursor = pos;
13028 r = eval_to_bool(skip, &err, NULL, FALSE);
13029 curwin->w_cursor = save_pos;
13030 if (err)
13031 {
13032 /* Evaluating {skip} caused an error, break here. */
13033 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013034 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013035 break;
13036 }
13037 if (r)
13038 continue;
13039 }
13040
13041 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
13042 {
13043 /* Found end when searching backwards or start when searching
13044 * forward: nested pair. */
13045 ++nest;
13046 pat = pat2; /* nested, don't search for middle */
13047 }
13048 else
13049 {
13050 /* Found end when searching forward or start when searching
13051 * backward: end of (nested) pair; or found middle in outer pair. */
13052 if (--nest == 1)
13053 pat = pat3; /* outer level, search for middle */
13054 }
13055
13056 if (nest == 0)
13057 {
13058 /* Found the match: return matchcount or line number. */
13059 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013060 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013061 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013062 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013063 if (flags & SP_SETPCMARK)
13064 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013065 curwin->w_cursor = pos;
13066 if (!(flags & SP_REPEAT))
13067 break;
13068 nest = 1; /* search for next unmatched */
13069 }
13070 }
13071
13072 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013073 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013074 curwin->w_cursor = save_cursor;
13075
13076theend:
13077 vim_free(pat2);
13078 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013079 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013080
13081 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013082}
13083
Bram Moolenaar0d660222005-01-07 21:51:51 +000013084/*ARGSUSED*/
13085 static void
13086f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013087 typval_T *argvars;
13088 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013089{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013090#ifdef FEAT_CLIENTSERVER
13091 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013092 char_u *server = get_tv_string_chk(&argvars[0]);
13093 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013094
Bram Moolenaar0d660222005-01-07 21:51:51 +000013095 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013096 if (server == NULL || reply == NULL)
13097 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013098 if (check_restricted() || check_secure())
13099 return;
13100# ifdef FEAT_X11
13101 if (check_connection() == FAIL)
13102 return;
13103# endif
13104
13105 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013106 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013107 EMSG(_("E258: Unable to send to client"));
13108 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013109 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013110 rettv->vval.v_number = 0;
13111#else
13112 rettv->vval.v_number = -1;
13113#endif
13114}
13115
13116/*ARGSUSED*/
13117 static void
13118f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013119 typval_T *argvars;
13120 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013121{
13122 char_u *r = NULL;
13123
13124#ifdef FEAT_CLIENTSERVER
13125# ifdef WIN32
13126 r = serverGetVimNames();
13127# else
13128 make_connection();
13129 if (X_DISPLAY != NULL)
13130 r = serverGetVimNames(X_DISPLAY);
13131# endif
13132#endif
13133 rettv->v_type = VAR_STRING;
13134 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013135}
13136
13137/*
13138 * "setbufvar()" function
13139 */
13140/*ARGSUSED*/
13141 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013142f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013143 typval_T *argvars;
13144 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013145{
13146 buf_T *buf;
13147#ifdef FEAT_AUTOCMD
13148 aco_save_T aco;
13149#else
13150 buf_T *save_curbuf;
13151#endif
13152 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013153 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013154 char_u nbuf[NUMBUFLEN];
13155
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013156 rettv->vval.v_number = 0;
13157
Bram Moolenaar071d4272004-06-13 20:20:40 +000013158 if (check_restricted() || check_secure())
13159 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013160 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
13161 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013162 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013163 varp = &argvars[2];
13164
13165 if (buf != NULL && varname != NULL && varp != NULL)
13166 {
13167 /* set curbuf to be our buf, temporarily */
13168#ifdef FEAT_AUTOCMD
13169 aucmd_prepbuf(&aco, buf);
13170#else
13171 save_curbuf = curbuf;
13172 curbuf = buf;
13173#endif
13174
13175 if (*varname == '&')
13176 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013177 long numval;
13178 char_u *strval;
13179 int error = FALSE;
13180
Bram Moolenaar071d4272004-06-13 20:20:40 +000013181 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013182 numval = get_tv_number_chk(varp, &error);
13183 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013184 if (!error && strval != NULL)
13185 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013186 }
13187 else
13188 {
13189 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
13190 if (bufvarname != NULL)
13191 {
13192 STRCPY(bufvarname, "b:");
13193 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013194 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013195 vim_free(bufvarname);
13196 }
13197 }
13198
13199 /* reset notion of buffer */
13200#ifdef FEAT_AUTOCMD
13201 aucmd_restbuf(&aco);
13202#else
13203 curbuf = save_curbuf;
13204#endif
13205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013206}
13207
13208/*
13209 * "setcmdpos()" function
13210 */
13211 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013212f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013213 typval_T *argvars;
13214 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013215{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013216 int pos = (int)get_tv_number(&argvars[0]) - 1;
13217
13218 if (pos >= 0)
13219 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013220}
13221
13222/*
13223 * "setline()" function
13224 */
13225 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013226f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013227 typval_T *argvars;
13228 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013229{
13230 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000013231 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013232 list_T *l = NULL;
13233 listitem_T *li = NULL;
13234 long added = 0;
13235 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013236
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013237 lnum = get_tv_lnum(&argvars[0]);
13238 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013239 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013240 l = argvars[1].vval.v_list;
13241 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013242 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013243 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013244 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013245
13246 rettv->vval.v_number = 0; /* OK */
13247 for (;;)
13248 {
13249 if (l != NULL)
13250 {
13251 /* list argument, get next string */
13252 if (li == NULL)
13253 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013254 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013255 li = li->li_next;
13256 }
13257
13258 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013259 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013260 break;
13261 if (lnum <= curbuf->b_ml.ml_line_count)
13262 {
13263 /* existing line, replace it */
13264 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
13265 {
13266 changed_bytes(lnum, 0);
13267 check_cursor_col();
13268 rettv->vval.v_number = 0; /* OK */
13269 }
13270 }
13271 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
13272 {
13273 /* lnum is one past the last line, append the line */
13274 ++added;
13275 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
13276 rettv->vval.v_number = 0; /* OK */
13277 }
13278
13279 if (l == NULL) /* only one string argument */
13280 break;
13281 ++lnum;
13282 }
13283
13284 if (added > 0)
13285 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013286}
13287
13288/*
Bram Moolenaar2641f772005-03-25 21:58:17 +000013289 * "setqflist()" function
13290 */
13291/*ARGSUSED*/
13292 static void
13293f_setqflist(argvars, rettv)
13294 typval_T *argvars;
13295 typval_T *rettv;
13296{
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013297 char_u *act;
13298 int action = ' ';
13299
Bram Moolenaar2641f772005-03-25 21:58:17 +000013300 rettv->vval.v_number = -1;
13301
13302#ifdef FEAT_QUICKFIX
13303 if (argvars[0].v_type != VAR_LIST)
13304 EMSG(_(e_listreq));
13305 else
13306 {
13307 list_T *l = argvars[0].vval.v_list;
13308
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013309 if (argvars[1].v_type == VAR_STRING)
13310 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013311 act = get_tv_string_chk(&argvars[1]);
13312 if (act == NULL)
13313 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013314 if (*act == 'a' || *act == 'r')
13315 action = *act;
13316 }
13317
13318 if (l != NULL && set_errorlist(l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013319 rettv->vval.v_number = 0;
13320 }
13321#endif
13322}
13323
13324/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013325 * "setreg()" function
13326 */
13327 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013328f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013329 typval_T *argvars;
13330 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013331{
13332 int regname;
13333 char_u *strregname;
13334 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013335 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013336 int append;
13337 char_u yank_type;
13338 long block_len;
13339
13340 block_len = -1;
13341 yank_type = MAUTO;
13342 append = FALSE;
13343
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013344 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013345 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013346
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013347 if (strregname == NULL)
13348 return; /* type error; errmsg already given */
13349 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013350 if (regname == 0 || regname == '@')
13351 regname = '"';
13352 else if (regname == '=')
13353 return;
13354
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013355 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013356 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013357 stropt = get_tv_string_chk(&argvars[2]);
13358 if (stropt == NULL)
13359 return; /* type error */
13360 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013361 switch (*stropt)
13362 {
13363 case 'a': case 'A': /* append */
13364 append = TRUE;
13365 break;
13366 case 'v': case 'c': /* character-wise selection */
13367 yank_type = MCHAR;
13368 break;
13369 case 'V': case 'l': /* line-wise selection */
13370 yank_type = MLINE;
13371 break;
13372#ifdef FEAT_VISUAL
13373 case 'b': case Ctrl_V: /* block-wise selection */
13374 yank_type = MBLOCK;
13375 if (VIM_ISDIGIT(stropt[1]))
13376 {
13377 ++stropt;
13378 block_len = getdigits(&stropt) - 1;
13379 --stropt;
13380 }
13381 break;
13382#endif
13383 }
13384 }
13385
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013386 strval = get_tv_string_chk(&argvars[1]);
13387 if (strval != NULL)
13388 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000013389 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013390 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013391}
13392
13393
13394/*
13395 * "setwinvar(expr)" function
13396 */
13397/*ARGSUSED*/
13398 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013399f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013400 typval_T *argvars;
13401 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013402{
13403 win_T *win;
13404#ifdef FEAT_WINDOWS
13405 win_T *save_curwin;
13406#endif
13407 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013408 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013409 char_u nbuf[NUMBUFLEN];
13410
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013411 rettv->vval.v_number = 0;
13412
Bram Moolenaar071d4272004-06-13 20:20:40 +000013413 if (check_restricted() || check_secure())
13414 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013415 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013416 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013417 varp = &argvars[2];
13418
13419 if (win != NULL && varname != NULL && varp != NULL)
13420 {
13421#ifdef FEAT_WINDOWS
13422 /* set curwin to be our win, temporarily */
13423 save_curwin = curwin;
13424 curwin = win;
13425 curbuf = curwin->w_buffer;
13426#endif
13427
13428 if (*varname == '&')
13429 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013430 long numval;
13431 char_u *strval;
13432 int error = FALSE;
13433
Bram Moolenaar071d4272004-06-13 20:20:40 +000013434 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013435 numval = get_tv_number_chk(varp, &error);
13436 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013437 if (!error && strval != NULL)
13438 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013439 }
13440 else
13441 {
13442 winvarname = alloc((unsigned)STRLEN(varname) + 3);
13443 if (winvarname != NULL)
13444 {
13445 STRCPY(winvarname, "w:");
13446 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013447 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013448 vim_free(winvarname);
13449 }
13450 }
13451
13452#ifdef FEAT_WINDOWS
13453 /* Restore current window, if it's still valid (autocomands can make
13454 * it invalid). */
13455 if (win_valid(save_curwin))
13456 {
13457 curwin = save_curwin;
13458 curbuf = curwin->w_buffer;
13459 }
13460#endif
13461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013462}
13463
13464/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013465 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013466 */
13467 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013468f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013469 typval_T *argvars;
13470 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013471{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013472 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013473
Bram Moolenaar0d660222005-01-07 21:51:51 +000013474 p = get_tv_string(&argvars[0]);
13475 rettv->vval.v_string = vim_strsave(p);
13476 simplify_filename(rettv->vval.v_string); /* simplify in place */
13477 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013478}
13479
Bram Moolenaar0d660222005-01-07 21:51:51 +000013480static int
13481#ifdef __BORLANDC__
13482 _RTLENTRYF
13483#endif
13484 item_compare __ARGS((const void *s1, const void *s2));
13485static int
13486#ifdef __BORLANDC__
13487 _RTLENTRYF
13488#endif
13489 item_compare2 __ARGS((const void *s1, const void *s2));
13490
13491static int item_compare_ic;
13492static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013493static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013494#define ITEM_COMPARE_FAIL 999
13495
Bram Moolenaar071d4272004-06-13 20:20:40 +000013496/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013497 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013498 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013499 static int
13500#ifdef __BORLANDC__
13501_RTLENTRYF
13502#endif
13503item_compare(s1, s2)
13504 const void *s1;
13505 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013506{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013507 char_u *p1, *p2;
13508 char_u *tofree1, *tofree2;
13509 int res;
13510 char_u numbuf1[NUMBUFLEN];
13511 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013512
Bram Moolenaar33570922005-01-25 22:26:29 +000013513 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1);
13514 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013515 if (item_compare_ic)
13516 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013517 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000013518 res = STRCMP(p1, p2);
13519 vim_free(tofree1);
13520 vim_free(tofree2);
13521 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013522}
13523
13524 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000013525#ifdef __BORLANDC__
13526_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000013527#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000013528item_compare2(s1, s2)
13529 const void *s1;
13530 const void *s2;
13531{
13532 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000013533 typval_T rettv;
13534 typval_T argv[2];
Bram Moolenaar0d660222005-01-07 21:51:51 +000013535 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013536
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013537 /* shortcut after failure in previous call; compare all items equal */
13538 if (item_compare_func_err)
13539 return 0;
13540
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013541 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
13542 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013543 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
13544 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013545
13546 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
13547 res = call_func(item_compare_func, STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000013548 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013549 clear_tv(&argv[0]);
13550 clear_tv(&argv[1]);
13551
13552 if (res == FAIL)
13553 res = ITEM_COMPARE_FAIL;
13554 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013555 /* return value has wrong type */
13556 res = get_tv_number_chk(&rettv, &item_compare_func_err);
13557 if (item_compare_func_err)
13558 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013559 clear_tv(&rettv);
13560 return res;
13561}
13562
13563/*
13564 * "sort({list})" function
13565 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013566 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013567f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013568 typval_T *argvars;
13569 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013570{
Bram Moolenaar33570922005-01-25 22:26:29 +000013571 list_T *l;
13572 listitem_T *li;
13573 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013574 long len;
13575 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013576
Bram Moolenaar0d660222005-01-07 21:51:51 +000013577 rettv->vval.v_number = 0;
13578 if (argvars[0].v_type != VAR_LIST)
13579 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000013580 else
13581 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013582 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013583 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013584 return;
13585 rettv->vval.v_list = l;
13586 rettv->v_type = VAR_LIST;
13587 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013588
Bram Moolenaar0d660222005-01-07 21:51:51 +000013589 len = list_len(l);
13590 if (len <= 1)
13591 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013592
Bram Moolenaar0d660222005-01-07 21:51:51 +000013593 item_compare_ic = FALSE;
13594 item_compare_func = NULL;
13595 if (argvars[1].v_type != VAR_UNKNOWN)
13596 {
13597 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013598 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013599 else
13600 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013601 int error = FALSE;
13602
13603 i = get_tv_number_chk(&argvars[1], &error);
13604 if (error)
13605 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013606 if (i == 1)
13607 item_compare_ic = TRUE;
13608 else
13609 item_compare_func = get_tv_string(&argvars[1]);
13610 }
13611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013612
Bram Moolenaar0d660222005-01-07 21:51:51 +000013613 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013614 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013615 if (ptrs == NULL)
13616 return;
13617 i = 0;
13618 for (li = l->lv_first; li != NULL; li = li->li_next)
13619 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013620
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013621 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013622 /* test the compare function */
13623 if (item_compare_func != NULL
13624 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
13625 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013626 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013627 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000013628 {
13629 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013630 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000013631 item_compare_func == NULL ? item_compare : item_compare2);
13632
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013633 if (!item_compare_func_err)
13634 {
13635 /* Clear the List and append the items in the sorted order. */
13636 l->lv_first = l->lv_last = NULL;
13637 l->lv_len = 0;
13638 for (i = 0; i < len; ++i)
13639 list_append(l, ptrs[i]);
13640 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013641 }
13642
13643 vim_free(ptrs);
13644 }
13645}
13646
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013647/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000013648 * "soundfold({word})" function
13649 */
13650 static void
13651f_soundfold(argvars, rettv)
13652 typval_T *argvars;
13653 typval_T *rettv;
13654{
13655 char_u *s;
13656
13657 rettv->v_type = VAR_STRING;
13658 s = get_tv_string(&argvars[0]);
13659#ifdef FEAT_SYN_HL
13660 rettv->vval.v_string = eval_soundfold(s);
13661#else
13662 rettv->vval.v_string = vim_strsave(s);
13663#endif
13664}
13665
13666/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013667 * "spellbadword()" function
13668 */
13669/* ARGSUSED */
13670 static void
13671f_spellbadword(argvars, rettv)
13672 typval_T *argvars;
13673 typval_T *rettv;
13674{
13675 int attr;
13676 char_u *ptr;
13677 int len;
13678
13679 rettv->vval.v_string = NULL;
13680 rettv->v_type = VAR_STRING;
13681
13682#ifdef FEAT_SYN_HL
13683 /* Find the start of the badly spelled word. */
13684 if (spell_move_to(FORWARD, TRUE, TRUE) == FAIL)
13685 return;
13686
13687 /* Get the length of the word and copy it. */
13688 ptr = ml_get_cursor();
Bram Moolenaar51ac12f2005-07-02 23:21:11 +000013689 len = spell_check(curwin, ptr, &attr, NULL);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013690 rettv->vval.v_string = vim_strnsave(ptr, len);
13691#endif
13692}
13693
13694/*
13695 * "spellsuggest()" function
13696 */
13697 static void
13698f_spellsuggest(argvars, rettv)
13699 typval_T *argvars;
13700 typval_T *rettv;
13701{
13702 char_u *str;
13703 int maxcount;
13704 garray_T ga;
13705 list_T *l;
13706 listitem_T *li;
13707 int i;
13708
13709 l = list_alloc();
13710 if (l == NULL)
13711 return;
13712 rettv->v_type = VAR_LIST;
13713 rettv->vval.v_list = l;
13714 ++l->lv_refcount;
13715
13716#ifdef FEAT_SYN_HL
13717 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
13718 {
13719 str = get_tv_string(&argvars[0]);
13720 if (argvars[1].v_type != VAR_UNKNOWN)
13721 {
13722 maxcount = get_tv_number(&argvars[1]);
13723 if (maxcount <= 0)
13724 return;
13725 }
13726 else
13727 maxcount = 25;
13728
13729 spell_suggest_list(&ga, str, maxcount);
13730
13731 for (i = 0; i < ga.ga_len; ++i)
13732 {
13733 str = ((char_u **)ga.ga_data)[i];
13734
13735 li = listitem_alloc();
13736 if (li == NULL)
13737 vim_free(str);
13738 else
13739 {
13740 li->li_tv.v_type = VAR_STRING;
13741 li->li_tv.v_lock = 0;
13742 li->li_tv.vval.v_string = str;
13743 list_append(l, li);
13744 }
13745 }
13746 ga_clear(&ga);
13747 }
13748#endif
13749}
13750
Bram Moolenaar0d660222005-01-07 21:51:51 +000013751 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013752f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013753 typval_T *argvars;
13754 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013755{
13756 char_u *str;
13757 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013758 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013759 regmatch_T regmatch;
13760 char_u patbuf[NUMBUFLEN];
13761 char_u *save_cpo;
13762 int match;
Bram Moolenaar33570922005-01-25 22:26:29 +000013763 listitem_T *ni;
13764 list_T *l;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013765 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013766 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013767 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013768
13769 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13770 save_cpo = p_cpo;
13771 p_cpo = (char_u *)"";
13772
13773 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013774 if (argvars[1].v_type != VAR_UNKNOWN)
13775 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013776 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13777 if (pat == NULL)
13778 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013779 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013780 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013781 }
13782 if (pat == NULL || *pat == NUL)
13783 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000013784
13785 l = list_alloc();
13786 if (l == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013787 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013788 rettv->v_type = VAR_LIST;
13789 rettv->vval.v_list = l;
13790 ++l->lv_refcount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013791 if (typeerr)
13792 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013793
Bram Moolenaar0d660222005-01-07 21:51:51 +000013794 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13795 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013796 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013797 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013798 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013799 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013800 if (*str == NUL)
13801 match = FALSE; /* empty item at the end */
13802 else
13803 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013804 if (match)
13805 end = regmatch.startp[0];
13806 else
13807 end = str + STRLEN(str);
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013808 if (keepempty || end > str || (l->lv_len > 0 && *str != NUL
13809 && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013810 {
13811 ni = listitem_alloc();
13812 if (ni == NULL)
13813 break;
13814 ni->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013815 ni->li_tv.v_lock = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013816 ni->li_tv.vval.v_string = vim_strnsave(str, end - str);
13817 list_append(l, ni);
13818 }
13819 if (!match)
13820 break;
13821 /* Advance to just after the match. */
13822 if (regmatch.endp[0] > str)
13823 col = 0;
13824 else
13825 {
13826 /* Don't get stuck at the same match. */
13827#ifdef FEAT_MBYTE
13828 col = mb_ptr2len_check(regmatch.endp[0]);
13829#else
13830 col = 1;
13831#endif
13832 }
13833 str = regmatch.endp[0];
13834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013835
Bram Moolenaar0d660222005-01-07 21:51:51 +000013836 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013838
Bram Moolenaar0d660222005-01-07 21:51:51 +000013839 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013840}
13841
13842#ifdef HAVE_STRFTIME
13843/*
13844 * "strftime({format}[, {time}])" function
13845 */
13846 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013847f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013848 typval_T *argvars;
13849 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013850{
13851 char_u result_buf[256];
13852 struct tm *curtime;
13853 time_t seconds;
13854 char_u *p;
13855
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013856 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013857
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013858 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013859 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013860 seconds = time(NULL);
13861 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013862 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013863 curtime = localtime(&seconds);
13864 /* MSVC returns NULL for an invalid value of seconds. */
13865 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013866 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013867 else
13868 {
13869# ifdef FEAT_MBYTE
13870 vimconv_T conv;
13871 char_u *enc;
13872
13873 conv.vc_type = CONV_NONE;
13874 enc = enc_locale();
13875 convert_setup(&conv, p_enc, enc);
13876 if (conv.vc_type != CONV_NONE)
13877 p = string_convert(&conv, p, NULL);
13878# endif
13879 if (p != NULL)
13880 (void)strftime((char *)result_buf, sizeof(result_buf),
13881 (char *)p, curtime);
13882 else
13883 result_buf[0] = NUL;
13884
13885# ifdef FEAT_MBYTE
13886 if (conv.vc_type != CONV_NONE)
13887 vim_free(p);
13888 convert_setup(&conv, enc, p_enc);
13889 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013890 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013891 else
13892# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013893 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013894
13895# ifdef FEAT_MBYTE
13896 /* Release conversion descriptors */
13897 convert_setup(&conv, NULL, NULL);
13898 vim_free(enc);
13899# endif
13900 }
13901}
13902#endif
13903
13904/*
13905 * "stridx()" function
13906 */
13907 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013908f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013909 typval_T *argvars;
13910 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013911{
13912 char_u buf[NUMBUFLEN];
13913 char_u *needle;
13914 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000013915 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013916 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000013917 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013918
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013919 needle = get_tv_string_chk(&argvars[1]);
13920 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000013921 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013922 if (needle == NULL || haystack == NULL)
13923 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013924
Bram Moolenaar33570922005-01-25 22:26:29 +000013925 if (argvars[2].v_type != VAR_UNKNOWN)
13926 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013927 int error = FALSE;
13928
13929 start_idx = get_tv_number_chk(&argvars[2], &error);
13930 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000013931 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000013932 if (start_idx >= 0)
13933 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000013934 }
13935
13936 pos = (char_u *)strstr((char *)haystack, (char *)needle);
13937 if (pos != NULL)
13938 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013939}
13940
13941/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013942 * "string()" function
13943 */
13944 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013945f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013946 typval_T *argvars;
13947 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013948{
13949 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013950 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013951
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013952 rettv->v_type = VAR_STRING;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013953 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013954 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013955 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013956}
13957
13958/*
13959 * "strlen()" function
13960 */
13961 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013962f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013963 typval_T *argvars;
13964 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013965{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013966 rettv->vval.v_number = (varnumber_T)(STRLEN(
13967 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013968}
13969
13970/*
13971 * "strpart()" function
13972 */
13973 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013974f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013975 typval_T *argvars;
13976 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013977{
13978 char_u *p;
13979 int n;
13980 int len;
13981 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013982 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013983
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013984 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013985 slen = (int)STRLEN(p);
13986
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013987 n = get_tv_number_chk(&argvars[1], &error);
13988 if (error)
13989 len = 0;
13990 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013991 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013992 else
13993 len = slen - n; /* default len: all bytes that are available. */
13994
13995 /*
13996 * Only return the overlap between the specified part and the actual
13997 * string.
13998 */
13999 if (n < 0)
14000 {
14001 len += n;
14002 n = 0;
14003 }
14004 else if (n > slen)
14005 n = slen;
14006 if (len < 0)
14007 len = 0;
14008 else if (n + len > slen)
14009 len = slen - n;
14010
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014011 rettv->v_type = VAR_STRING;
14012 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014013}
14014
14015/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014016 * "strridx()" function
14017 */
14018 static void
14019f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014020 typval_T *argvars;
14021 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014022{
14023 char_u buf[NUMBUFLEN];
14024 char_u *needle;
14025 char_u *haystack;
14026 char_u *rest;
14027 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014028 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014029
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014030 needle = get_tv_string_chk(&argvars[1]);
14031 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar532c7802005-01-27 14:44:31 +000014032 haystack_len = STRLEN(haystack);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014033
14034 rettv->vval.v_number = -1;
14035 if (needle == NULL || haystack == NULL)
14036 return; /* type error; errmsg already given */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014037 if (argvars[2].v_type != VAR_UNKNOWN)
14038 {
14039 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014040 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000014041 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014042 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014043 }
14044 else
14045 end_idx = haystack_len;
14046
Bram Moolenaar0d660222005-01-07 21:51:51 +000014047 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000014048 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014049 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014050 lastmatch = haystack + end_idx;
14051 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014052 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000014053 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014054 for (rest = haystack; *rest != '\0'; ++rest)
14055 {
14056 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000014057 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014058 break;
14059 lastmatch = rest;
14060 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000014061 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014062
14063 if (lastmatch == NULL)
14064 rettv->vval.v_number = -1;
14065 else
14066 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
14067}
14068
14069/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014070 * "strtrans()" function
14071 */
14072 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014073f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014074 typval_T *argvars;
14075 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014076{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014077 rettv->v_type = VAR_STRING;
14078 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014079}
14080
14081/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014082 * "submatch()" function
14083 */
14084 static void
14085f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014086 typval_T *argvars;
14087 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014088{
14089 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014090 rettv->vval.v_string =
14091 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014092}
14093
14094/*
14095 * "substitute()" function
14096 */
14097 static void
14098f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014099 typval_T *argvars;
14100 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014101{
14102 char_u patbuf[NUMBUFLEN];
14103 char_u subbuf[NUMBUFLEN];
14104 char_u flagsbuf[NUMBUFLEN];
14105
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014106 char_u *str = get_tv_string_chk(&argvars[0]);
14107 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14108 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
14109 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
14110
Bram Moolenaar0d660222005-01-07 21:51:51 +000014111 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014112 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
14113 rettv->vval.v_string = NULL;
14114 else
14115 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014116}
14117
14118/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014119 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014120 */
14121/*ARGSUSED*/
14122 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014123f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014124 typval_T *argvars;
14125 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014126{
14127 int id = 0;
14128#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014129 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014130 long col;
14131 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000014132 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014133
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014134 lnum = get_tv_lnum(argvars); /* -1 on type error */
14135 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
14136 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014137
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014138 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014139 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
14140 id = syn_get_id(lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014141#endif
14142
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014143 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014144}
14145
14146/*
14147 * "synIDattr(id, what [, mode])" function
14148 */
14149/*ARGSUSED*/
14150 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014151f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014152 typval_T *argvars;
14153 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014154{
14155 char_u *p = NULL;
14156#ifdef FEAT_SYN_HL
14157 int id;
14158 char_u *what;
14159 char_u *mode;
14160 char_u modebuf[NUMBUFLEN];
14161 int modec;
14162
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014163 id = get_tv_number(&argvars[0]);
14164 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014165 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014166 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014167 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014168 modec = TOLOWER_ASC(mode[0]);
14169 if (modec != 't' && modec != 'c'
14170#ifdef FEAT_GUI
14171 && modec != 'g'
14172#endif
14173 )
14174 modec = 0; /* replace invalid with current */
14175 }
14176 else
14177 {
14178#ifdef FEAT_GUI
14179 if (gui.in_use)
14180 modec = 'g';
14181 else
14182#endif
14183 if (t_colors > 1)
14184 modec = 'c';
14185 else
14186 modec = 't';
14187 }
14188
14189
14190 switch (TOLOWER_ASC(what[0]))
14191 {
14192 case 'b':
14193 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
14194 p = highlight_color(id, what, modec);
14195 else /* bold */
14196 p = highlight_has_attr(id, HL_BOLD, modec);
14197 break;
14198
14199 case 'f': /* fg[#] */
14200 p = highlight_color(id, what, modec);
14201 break;
14202
14203 case 'i':
14204 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
14205 p = highlight_has_attr(id, HL_INVERSE, modec);
14206 else /* italic */
14207 p = highlight_has_attr(id, HL_ITALIC, modec);
14208 break;
14209
14210 case 'n': /* name */
14211 p = get_highlight_name(NULL, id - 1);
14212 break;
14213
14214 case 'r': /* reverse */
14215 p = highlight_has_attr(id, HL_INVERSE, modec);
14216 break;
14217
14218 case 's': /* standout */
14219 p = highlight_has_attr(id, HL_STANDOUT, modec);
14220 break;
14221
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000014222 case 'u':
14223 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
14224 /* underline */
14225 p = highlight_has_attr(id, HL_UNDERLINE, modec);
14226 else
14227 /* undercurl */
14228 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014229 break;
14230 }
14231
14232 if (p != NULL)
14233 p = vim_strsave(p);
14234#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014235 rettv->v_type = VAR_STRING;
14236 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014237}
14238
14239/*
14240 * "synIDtrans(id)" function
14241 */
14242/*ARGSUSED*/
14243 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014244f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014245 typval_T *argvars;
14246 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014247{
14248 int id;
14249
14250#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014251 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014252
14253 if (id > 0)
14254 id = syn_get_final_id(id);
14255 else
14256#endif
14257 id = 0;
14258
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014259 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014260}
14261
14262/*
14263 * "system()" function
14264 */
14265 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014266f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014267 typval_T *argvars;
14268 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014269{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014270 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014271 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014272 char_u *infile = NULL;
14273 char_u buf[NUMBUFLEN];
14274 int err = FALSE;
14275 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014276
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014277 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014278 {
14279 /*
14280 * Write the string to a temp file, to be used for input of the shell
14281 * command.
14282 */
14283 if ((infile = vim_tempname('i')) == NULL)
14284 {
14285 EMSG(_(e_notmp));
14286 return;
14287 }
14288
14289 fd = mch_fopen((char *)infile, WRITEBIN);
14290 if (fd == NULL)
14291 {
14292 EMSG2(_(e_notopen), infile);
14293 goto done;
14294 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014295 p = get_tv_string_buf_chk(&argvars[1], buf);
14296 if (p == NULL)
14297 goto done; /* type error; errmsg already given */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014298 if (fwrite(p, STRLEN(p), 1, fd) != 1)
14299 err = TRUE;
14300 if (fclose(fd) != 0)
14301 err = TRUE;
14302 if (err)
14303 {
14304 EMSG(_("E677: Error writing temp file"));
14305 goto done;
14306 }
14307 }
14308
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014309 res = get_cmd_output(get_tv_string(&argvars[0]), infile, SHELL_SILENT);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014310
Bram Moolenaar071d4272004-06-13 20:20:40 +000014311#ifdef USE_CR
14312 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014313 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014314 {
14315 char_u *s;
14316
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014317 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014318 {
14319 if (*s == CAR)
14320 *s = NL;
14321 }
14322 }
14323#else
14324# ifdef USE_CRNL
14325 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014326 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014327 {
14328 char_u *s, *d;
14329
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014330 d = res;
14331 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014332 {
14333 if (s[0] == CAR && s[1] == NL)
14334 ++s;
14335 *d++ = *s;
14336 }
14337 *d = NUL;
14338 }
14339# endif
14340#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014341
14342done:
14343 if (infile != NULL)
14344 {
14345 mch_remove(infile);
14346 vim_free(infile);
14347 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014348 rettv->v_type = VAR_STRING;
14349 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014350}
14351
14352/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000014353 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014354 */
14355 static void
14356f_taglist(argvars, rettv)
14357 typval_T *argvars;
14358 typval_T *rettv;
14359{
14360 char_u *tag_pattern;
14361 list_T *l;
14362
14363 tag_pattern = get_tv_string(&argvars[0]);
14364
14365 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014366 if (*tag_pattern == NUL)
14367 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014368
14369 l = list_alloc();
14370 if (l != NULL)
14371 {
14372 if (get_tags(l, tag_pattern) != FAIL)
14373 {
14374 rettv->vval.v_list = l;
14375 rettv->v_type = VAR_LIST;
14376 ++l->lv_refcount;
14377 }
14378 else
14379 list_free(l);
14380 }
14381}
14382
14383/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014384 * "tempname()" function
14385 */
14386/*ARGSUSED*/
14387 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014388f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014389 typval_T *argvars;
14390 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014391{
14392 static int x = 'A';
14393
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014394 rettv->v_type = VAR_STRING;
14395 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014396
14397 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
14398 * names. Skip 'I' and 'O', they are used for shell redirection. */
14399 do
14400 {
14401 if (x == 'Z')
14402 x = '0';
14403 else if (x == '9')
14404 x = 'A';
14405 else
14406 {
14407#ifdef EBCDIC
14408 if (x == 'I')
14409 x = 'J';
14410 else if (x == 'R')
14411 x = 'S';
14412 else
14413#endif
14414 ++x;
14415 }
14416 } while (x == 'I' || x == 'O');
14417}
14418
14419/*
14420 * "tolower(string)" function
14421 */
14422 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014423f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014424 typval_T *argvars;
14425 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014426{
14427 char_u *p;
14428
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014429 p = vim_strsave(get_tv_string(&argvars[0]));
14430 rettv->v_type = VAR_STRING;
14431 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014432
14433 if (p != NULL)
14434 while (*p != NUL)
14435 {
14436#ifdef FEAT_MBYTE
14437 int l;
14438
14439 if (enc_utf8)
14440 {
14441 int c, lc;
14442
14443 c = utf_ptr2char(p);
14444 lc = utf_tolower(c);
14445 l = utf_ptr2len_check(p);
14446 /* TODO: reallocate string when byte count changes. */
14447 if (utf_char2len(lc) == l)
14448 utf_char2bytes(lc, p);
14449 p += l;
14450 }
14451 else if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
14452 p += l; /* skip multi-byte character */
14453 else
14454#endif
14455 {
14456 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
14457 ++p;
14458 }
14459 }
14460}
14461
14462/*
14463 * "toupper(string)" function
14464 */
14465 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014466f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014467 typval_T *argvars;
14468 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014469{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014470 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014471 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014472}
14473
14474/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000014475 * "tr(string, fromstr, tostr)" function
14476 */
14477 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014478f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014479 typval_T *argvars;
14480 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014481{
14482 char_u *instr;
14483 char_u *fromstr;
14484 char_u *tostr;
14485 char_u *p;
14486#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000014487 int inlen;
14488 int fromlen;
14489 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014490 int idx;
14491 char_u *cpstr;
14492 int cplen;
14493 int first = TRUE;
14494#endif
14495 char_u buf[NUMBUFLEN];
14496 char_u buf2[NUMBUFLEN];
14497 garray_T ga;
14498
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014499 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014500 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
14501 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014502
14503 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014504 rettv->v_type = VAR_STRING;
14505 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014506 if (fromstr == NULL || tostr == NULL)
14507 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000014508 ga_init2(&ga, (int)sizeof(char), 80);
14509
14510#ifdef FEAT_MBYTE
14511 if (!has_mbyte)
14512#endif
14513 /* not multi-byte: fromstr and tostr must be the same length */
14514 if (STRLEN(fromstr) != STRLEN(tostr))
14515 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014516#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000014517error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014518#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000014519 EMSG2(_(e_invarg2), fromstr);
14520 ga_clear(&ga);
14521 return;
14522 }
14523
14524 /* fromstr and tostr have to contain the same number of chars */
14525 while (*instr != NUL)
14526 {
14527#ifdef FEAT_MBYTE
14528 if (has_mbyte)
14529 {
14530 inlen = mb_ptr2len_check(instr);
14531 cpstr = instr;
14532 cplen = inlen;
14533 idx = 0;
14534 for (p = fromstr; *p != NUL; p += fromlen)
14535 {
14536 fromlen = mb_ptr2len_check(p);
14537 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
14538 {
14539 for (p = tostr; *p != NUL; p += tolen)
14540 {
14541 tolen = mb_ptr2len_check(p);
14542 if (idx-- == 0)
14543 {
14544 cplen = tolen;
14545 cpstr = p;
14546 break;
14547 }
14548 }
14549 if (*p == NUL) /* tostr is shorter than fromstr */
14550 goto error;
14551 break;
14552 }
14553 ++idx;
14554 }
14555
14556 if (first && cpstr == instr)
14557 {
14558 /* Check that fromstr and tostr have the same number of
14559 * (multi-byte) characters. Done only once when a character
14560 * of instr doesn't appear in fromstr. */
14561 first = FALSE;
14562 for (p = tostr; *p != NUL; p += tolen)
14563 {
14564 tolen = mb_ptr2len_check(p);
14565 --idx;
14566 }
14567 if (idx != 0)
14568 goto error;
14569 }
14570
14571 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000014572 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014573 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014574
14575 instr += inlen;
14576 }
14577 else
14578#endif
14579 {
14580 /* When not using multi-byte chars we can do it faster. */
14581 p = vim_strchr(fromstr, *instr);
14582 if (p != NULL)
14583 ga_append(&ga, tostr[p - fromstr]);
14584 else
14585 ga_append(&ga, *instr);
14586 ++instr;
14587 }
14588 }
14589
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014590 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014591}
14592
14593/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014594 * "type(expr)" function
14595 */
14596 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014597f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014598 typval_T *argvars;
14599 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014600{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014601 int n;
14602
14603 switch (argvars[0].v_type)
14604 {
14605 case VAR_NUMBER: n = 0; break;
14606 case VAR_STRING: n = 1; break;
14607 case VAR_FUNC: n = 2; break;
14608 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014609 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014610 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
14611 }
14612 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014613}
14614
14615/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014616 * "values(dict)" function
14617 */
14618 static void
14619f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014620 typval_T *argvars;
14621 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014622{
14623 dict_list(argvars, rettv, 1);
14624}
14625
14626/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014627 * "virtcol(string)" function
14628 */
14629 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014630f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014631 typval_T *argvars;
14632 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014633{
14634 colnr_T vcol = 0;
14635 pos_T *fp;
14636
14637 fp = var2fpos(&argvars[0], FALSE);
14638 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count)
14639 {
14640 getvvcol(curwin, fp, NULL, NULL, &vcol);
14641 ++vcol;
14642 }
14643
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014644 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014645}
14646
14647/*
14648 * "visualmode()" function
14649 */
14650/*ARGSUSED*/
14651 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014652f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014653 typval_T *argvars;
14654 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014655{
14656#ifdef FEAT_VISUAL
14657 char_u str[2];
14658
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014659 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014660 str[0] = curbuf->b_visual_mode_eval;
14661 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014662 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014663
14664 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014665 if ((argvars[0].v_type == VAR_NUMBER
14666 && argvars[0].vval.v_number != 0)
14667 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014668 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014669 curbuf->b_visual_mode_eval = NUL;
14670#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014671 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014672#endif
14673}
14674
14675/*
14676 * "winbufnr(nr)" function
14677 */
14678 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014679f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014680 typval_T *argvars;
14681 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014682{
14683 win_T *wp;
14684
14685 wp = find_win_by_nr(&argvars[0]);
14686 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014687 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014688 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014689 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014690}
14691
14692/*
14693 * "wincol()" function
14694 */
14695/*ARGSUSED*/
14696 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014697f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014698 typval_T *argvars;
14699 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014700{
14701 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014702 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014703}
14704
14705/*
14706 * "winheight(nr)" function
14707 */
14708 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014709f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014710 typval_T *argvars;
14711 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014712{
14713 win_T *wp;
14714
14715 wp = find_win_by_nr(&argvars[0]);
14716 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014717 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014718 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014719 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014720}
14721
14722/*
14723 * "winline()" function
14724 */
14725/*ARGSUSED*/
14726 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014727f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014728 typval_T *argvars;
14729 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014730{
14731 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014732 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014733}
14734
14735/*
14736 * "winnr()" function
14737 */
14738/* ARGSUSED */
14739 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014740f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014741 typval_T *argvars;
14742 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014743{
14744 int nr = 1;
14745#ifdef FEAT_WINDOWS
14746 win_T *wp;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014747 win_T *twin = curwin;
14748 char_u *arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014749
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014750 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014751 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014752 arg = get_tv_string_chk(&argvars[0]);
14753 if (arg == NULL)
14754 nr = 0; /* type error; errmsg already given */
14755 else if (STRCMP(arg, "$") == 0)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014756 twin = lastwin;
14757 else if (STRCMP(arg, "#") == 0)
14758 {
14759 twin = prevwin;
14760 if (prevwin == NULL)
14761 nr = 0;
14762 }
14763 else
14764 {
14765 EMSG2(_(e_invexpr2), arg);
14766 nr = 0;
14767 }
14768 }
14769
14770 if (nr > 0)
14771 for (wp = firstwin; wp != twin; wp = wp->w_next)
14772 ++nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014773#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014774 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014775}
14776
14777/*
14778 * "winrestcmd()" function
14779 */
14780/* ARGSUSED */
14781 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014782f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014783 typval_T *argvars;
14784 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014785{
14786#ifdef FEAT_WINDOWS
14787 win_T *wp;
14788 int winnr = 1;
14789 garray_T ga;
14790 char_u buf[50];
14791
14792 ga_init2(&ga, (int)sizeof(char), 70);
14793 for (wp = firstwin; wp != NULL; wp = wp->w_next)
14794 {
14795 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
14796 ga_concat(&ga, buf);
14797# ifdef FEAT_VERTSPLIT
14798 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
14799 ga_concat(&ga, buf);
14800# endif
14801 ++winnr;
14802 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000014803 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014804
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014805 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014806#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014807 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014808#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014809 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014810}
14811
14812/*
14813 * "winwidth(nr)" function
14814 */
14815 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014816f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014817 typval_T *argvars;
14818 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014819{
14820 win_T *wp;
14821
14822 wp = find_win_by_nr(&argvars[0]);
14823 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014824 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014825 else
14826#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014827 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014828#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014829 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014830#endif
14831}
14832
Bram Moolenaar071d4272004-06-13 20:20:40 +000014833/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014834 * "writefile()" function
14835 */
14836 static void
14837f_writefile(argvars, rettv)
14838 typval_T *argvars;
14839 typval_T *rettv;
14840{
14841 int binary = FALSE;
14842 char_u *fname;
14843 FILE *fd;
14844 listitem_T *li;
14845 char_u *s;
14846 int ret = 0;
14847 int c;
14848
14849 if (argvars[0].v_type != VAR_LIST)
14850 {
14851 EMSG2(_(e_listarg), "writefile()");
14852 return;
14853 }
14854 if (argvars[0].vval.v_list == NULL)
14855 return;
14856
14857 if (argvars[2].v_type != VAR_UNKNOWN
14858 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
14859 binary = TRUE;
14860
14861 /* Always open the file in binary mode, library functions have a mind of
14862 * their own about CR-LF conversion. */
14863 fname = get_tv_string(&argvars[1]);
14864 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
14865 {
14866 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
14867 ret = -1;
14868 }
14869 else
14870 {
14871 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
14872 li = li->li_next)
14873 {
14874 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
14875 {
14876 if (*s == '\n')
14877 c = putc(NUL, fd);
14878 else
14879 c = putc(*s, fd);
14880 if (c == EOF)
14881 {
14882 ret = -1;
14883 break;
14884 }
14885 }
14886 if (!binary || li->li_next != NULL)
14887 if (putc('\n', fd) == EOF)
14888 {
14889 ret = -1;
14890 break;
14891 }
14892 if (ret < 0)
14893 {
14894 EMSG(_(e_write));
14895 break;
14896 }
14897 }
14898 fclose(fd);
14899 }
14900
14901 rettv->vval.v_number = ret;
14902}
14903
14904/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014905 * Translate a String variable into a position.
14906 */
14907 static pos_T *
14908var2fpos(varp, lnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000014909 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014910 int lnum; /* TRUE when $ is last line */
14911{
14912 char_u *name;
14913 static pos_T pos;
14914 pos_T *pp;
14915
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014916 name = get_tv_string_chk(varp);
14917 if (name == NULL)
14918 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014919 if (name[0] == '.') /* cursor */
14920 return &curwin->w_cursor;
14921 if (name[0] == '\'') /* mark */
14922 {
14923 pp = getmark(name[1], FALSE);
14924 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
14925 return NULL;
14926 return pp;
14927 }
14928 if (name[0] == '$') /* last column or line */
14929 {
14930 if (lnum)
14931 {
14932 pos.lnum = curbuf->b_ml.ml_line_count;
14933 pos.col = 0;
14934 }
14935 else
14936 {
14937 pos.lnum = curwin->w_cursor.lnum;
14938 pos.col = (colnr_T)STRLEN(ml_get_curline());
14939 }
14940 return &pos;
14941 }
14942 return NULL;
14943}
14944
14945/*
14946 * Get the length of an environment variable name.
14947 * Advance "arg" to the first character after the name.
14948 * Return 0 for error.
14949 */
14950 static int
14951get_env_len(arg)
14952 char_u **arg;
14953{
14954 char_u *p;
14955 int len;
14956
14957 for (p = *arg; vim_isIDc(*p); ++p)
14958 ;
14959 if (p == *arg) /* no name found */
14960 return 0;
14961
14962 len = (int)(p - *arg);
14963 *arg = p;
14964 return len;
14965}
14966
14967/*
14968 * Get the length of the name of a function or internal variable.
14969 * "arg" is advanced to the first non-white character after the name.
14970 * Return 0 if something is wrong.
14971 */
14972 static int
14973get_id_len(arg)
14974 char_u **arg;
14975{
14976 char_u *p;
14977 int len;
14978
14979 /* Find the end of the name. */
14980 for (p = *arg; eval_isnamec(*p); ++p)
14981 ;
14982 if (p == *arg) /* no name found */
14983 return 0;
14984
14985 len = (int)(p - *arg);
14986 *arg = skipwhite(p);
14987
14988 return len;
14989}
14990
14991/*
Bram Moolenaara7043832005-01-21 11:56:39 +000014992 * Get the length of the name of a variable or function.
14993 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000014994 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014995 * Return -1 if curly braces expansion failed.
14996 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014997 * If the name contains 'magic' {}'s, expand them and return the
14998 * expanded name in an allocated string via 'alias' - caller must free.
14999 */
15000 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015001get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015002 char_u **arg;
15003 char_u **alias;
15004 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015005 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015006{
15007 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015008 char_u *p;
15009 char_u *expr_start;
15010 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015011
15012 *alias = NULL; /* default to no alias */
15013
15014 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
15015 && (*arg)[2] == (int)KE_SNR)
15016 {
15017 /* hard coded <SNR>, already translated */
15018 *arg += 3;
15019 return get_id_len(arg) + 3;
15020 }
15021 len = eval_fname_script(*arg);
15022 if (len > 0)
15023 {
15024 /* literal "<SID>", "s:" or "<SNR>" */
15025 *arg += len;
15026 }
15027
Bram Moolenaar071d4272004-06-13 20:20:40 +000015028 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015029 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015030 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015031 p = find_name_end(*arg, &expr_start, &expr_end,
15032 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015033 if (expr_start != NULL)
15034 {
15035 char_u *temp_string;
15036
15037 if (!evaluate)
15038 {
15039 len += (int)(p - *arg);
15040 *arg = skipwhite(p);
15041 return len;
15042 }
15043
15044 /*
15045 * Include any <SID> etc in the expanded string:
15046 * Thus the -len here.
15047 */
15048 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
15049 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015050 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015051 *alias = temp_string;
15052 *arg = skipwhite(p);
15053 return (int)STRLEN(temp_string);
15054 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015055
15056 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015057 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015058 EMSG2(_(e_invexpr2), *arg);
15059
15060 return len;
15061}
15062
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015063/*
15064 * Find the end of a variable or function name, taking care of magic braces.
15065 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
15066 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015067 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015068 * Return a pointer to just after the name. Equal to "arg" if there is no
15069 * valid name.
15070 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015071 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015072find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015073 char_u *arg;
15074 char_u **expr_start;
15075 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015076 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015077{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015078 int mb_nest = 0;
15079 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015080 char_u *p;
15081
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015082 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015083 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015084 *expr_start = NULL;
15085 *expr_end = NULL;
15086 }
15087
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015088 /* Quick check for valid starting character. */
15089 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
15090 return arg;
15091
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015092 for (p = arg; *p != NUL
15093 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015094 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015095 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015096 || mb_nest != 0
15097 || br_nest != 0); ++p)
15098 {
15099 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015100 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015101 if (*p == '[')
15102 ++br_nest;
15103 else if (*p == ']')
15104 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015105 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015106 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015107 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015108 if (*p == '{')
15109 {
15110 mb_nest++;
15111 if (expr_start != NULL && *expr_start == NULL)
15112 *expr_start = p;
15113 }
15114 else if (*p == '}')
15115 {
15116 mb_nest--;
15117 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
15118 *expr_end = p;
15119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015121 }
15122
15123 return p;
15124}
15125
15126/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015127 * Expands out the 'magic' {}'s in a variable/function name.
15128 * Note that this can call itself recursively, to deal with
15129 * constructs like foo{bar}{baz}{bam}
15130 * The four pointer arguments point to "foo{expre}ss{ion}bar"
15131 * "in_start" ^
15132 * "expr_start" ^
15133 * "expr_end" ^
15134 * "in_end" ^
15135 *
15136 * Returns a new allocated string, which the caller must free.
15137 * Returns NULL for failure.
15138 */
15139 static char_u *
15140make_expanded_name(in_start, expr_start, expr_end, in_end)
15141 char_u *in_start;
15142 char_u *expr_start;
15143 char_u *expr_end;
15144 char_u *in_end;
15145{
15146 char_u c1;
15147 char_u *retval = NULL;
15148 char_u *temp_result;
15149 char_u *nextcmd = NULL;
15150
15151 if (expr_end == NULL || in_end == NULL)
15152 return NULL;
15153 *expr_start = NUL;
15154 *expr_end = NUL;
15155 c1 = *in_end;
15156 *in_end = NUL;
15157
15158 temp_result = eval_to_string(expr_start + 1, &nextcmd);
15159 if (temp_result != NULL && nextcmd == NULL)
15160 {
15161 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
15162 + (in_end - expr_end) + 1));
15163 if (retval != NULL)
15164 {
15165 STRCPY(retval, in_start);
15166 STRCAT(retval, temp_result);
15167 STRCAT(retval, expr_end + 1);
15168 }
15169 }
15170 vim_free(temp_result);
15171
15172 *in_end = c1; /* put char back for error messages */
15173 *expr_start = '{';
15174 *expr_end = '}';
15175
15176 if (retval != NULL)
15177 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015178 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015179 if (expr_start != NULL)
15180 {
15181 /* Further expansion! */
15182 temp_result = make_expanded_name(retval, expr_start,
15183 expr_end, temp_result);
15184 vim_free(retval);
15185 retval = temp_result;
15186 }
15187 }
15188
15189 return retval;
15190}
15191
15192/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015193 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000015194 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015195 */
15196 static int
15197eval_isnamec(c)
15198 int c;
15199{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015200 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
15201}
15202
15203/*
15204 * Return TRUE if character "c" can be used as the first character in a
15205 * variable or function name (excluding '{' and '}').
15206 */
15207 static int
15208eval_isnamec1(c)
15209 int c;
15210{
15211 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000015212}
15213
15214/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015215 * Set number v: variable to "val".
15216 */
15217 void
15218set_vim_var_nr(idx, val)
15219 int idx;
15220 long val;
15221{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015222 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015223}
15224
15225/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015226 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015227 */
15228 long
15229get_vim_var_nr(idx)
15230 int idx;
15231{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015232 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015233}
15234
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015235#if defined(FEAT_AUTOCMD) || defined(PROTO)
15236/*
15237 * Get string v: variable value. Uses a static buffer, can only be used once.
15238 */
15239 char_u *
15240get_vim_var_str(idx)
15241 int idx;
15242{
15243 return get_tv_string(&vimvars[idx].vv_tv);
15244}
15245#endif
15246
Bram Moolenaar071d4272004-06-13 20:20:40 +000015247/*
15248 * Set v:count, v:count1 and v:prevcount.
15249 */
15250 void
15251set_vcount(count, count1)
15252 long count;
15253 long count1;
15254{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015255 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
15256 vimvars[VV_COUNT].vv_nr = count;
15257 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015258}
15259
15260/*
15261 * Set string v: variable to a copy of "val".
15262 */
15263 void
15264set_vim_var_string(idx, val, len)
15265 int idx;
15266 char_u *val;
15267 int len; /* length of "val" to use or -1 (whole string) */
15268{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015269 /* Need to do this (at least) once, since we can't initialize a union.
15270 * Will always be invoked when "v:progname" is set. */
15271 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
15272
Bram Moolenaare9a41262005-01-15 22:18:47 +000015273 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015274 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015275 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015276 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015277 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015278 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000015279 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015280}
15281
15282/*
15283 * Set v:register if needed.
15284 */
15285 void
15286set_reg_var(c)
15287 int c;
15288{
15289 char_u regname;
15290
15291 if (c == 0 || c == ' ')
15292 regname = '"';
15293 else
15294 regname = c;
15295 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000015296 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015297 set_vim_var_string(VV_REG, &regname, 1);
15298}
15299
15300/*
15301 * Get or set v:exception. If "oldval" == NULL, return the current value.
15302 * Otherwise, restore the value to "oldval" and return NULL.
15303 * Must always be called in pairs to save and restore v:exception! Does not
15304 * take care of memory allocations.
15305 */
15306 char_u *
15307v_exception(oldval)
15308 char_u *oldval;
15309{
15310 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015311 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015312
Bram Moolenaare9a41262005-01-15 22:18:47 +000015313 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015314 return NULL;
15315}
15316
15317/*
15318 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
15319 * Otherwise, restore the value to "oldval" and return NULL.
15320 * Must always be called in pairs to save and restore v:throwpoint! Does not
15321 * take care of memory allocations.
15322 */
15323 char_u *
15324v_throwpoint(oldval)
15325 char_u *oldval;
15326{
15327 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015328 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015329
Bram Moolenaare9a41262005-01-15 22:18:47 +000015330 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015331 return NULL;
15332}
15333
15334#if defined(FEAT_AUTOCMD) || defined(PROTO)
15335/*
15336 * Set v:cmdarg.
15337 * If "eap" != NULL, use "eap" to generate the value and return the old value.
15338 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
15339 * Must always be called in pairs!
15340 */
15341 char_u *
15342set_cmdarg(eap, oldarg)
15343 exarg_T *eap;
15344 char_u *oldarg;
15345{
15346 char_u *oldval;
15347 char_u *newval;
15348 unsigned len;
15349
Bram Moolenaare9a41262005-01-15 22:18:47 +000015350 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015351 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015352 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015353 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000015354 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015355 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015356 }
15357
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015358 if (eap->force_bin == FORCE_BIN)
15359 len = 6;
15360 else if (eap->force_bin == FORCE_NOBIN)
15361 len = 8;
15362 else
15363 len = 0;
15364 if (eap->force_ff != 0)
15365 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
15366# ifdef FEAT_MBYTE
15367 if (eap->force_enc != 0)
15368 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
15369# endif
15370
15371 newval = alloc(len + 1);
15372 if (newval == NULL)
15373 return NULL;
15374
15375 if (eap->force_bin == FORCE_BIN)
15376 sprintf((char *)newval, " ++bin");
15377 else if (eap->force_bin == FORCE_NOBIN)
15378 sprintf((char *)newval, " ++nobin");
15379 else
15380 *newval = NUL;
15381 if (eap->force_ff != 0)
15382 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
15383 eap->cmd + eap->force_ff);
15384# ifdef FEAT_MBYTE
15385 if (eap->force_enc != 0)
15386 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
15387 eap->cmd + eap->force_enc);
15388# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000015389 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015390 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015391}
15392#endif
15393
15394/*
15395 * Get the value of internal variable "name".
15396 * Return OK or FAIL.
15397 */
15398 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015399get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015400 char_u *name;
15401 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000015402 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015403 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015404{
15405 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000015406 typval_T *tv = NULL;
15407 typval_T atv;
15408 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015409 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015410
15411 /* truncate the name, so that we can use strcmp() */
15412 cc = name[len];
15413 name[len] = NUL;
15414
15415 /*
15416 * Check for "b:changedtick".
15417 */
15418 if (STRCMP(name, "b:changedtick") == 0)
15419 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000015420 atv.v_type = VAR_NUMBER;
15421 atv.vval.v_number = curbuf->b_changedtick;
15422 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015423 }
15424
15425 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015426 * Check for user-defined variables.
15427 */
15428 else
15429 {
Bram Moolenaara7043832005-01-21 11:56:39 +000015430 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015431 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000015432 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015433 }
15434
Bram Moolenaare9a41262005-01-15 22:18:47 +000015435 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015436 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015437 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015438 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015439 ret = FAIL;
15440 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015441 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015442 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015443
15444 name[len] = cc;
15445
15446 return ret;
15447}
15448
15449/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015450 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
15451 * Also handle function call with Funcref variable: func(expr)
15452 * Can all be combined: dict.func(expr)[idx]['func'](expr)
15453 */
15454 static int
15455handle_subscript(arg, rettv, evaluate, verbose)
15456 char_u **arg;
15457 typval_T *rettv;
15458 int evaluate; /* do more than finding the end */
15459 int verbose; /* give error messages */
15460{
15461 int ret = OK;
15462 dict_T *selfdict = NULL;
15463 char_u *s;
15464 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000015465 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015466
15467 while (ret == OK
15468 && (**arg == '['
15469 || (**arg == '.' && rettv->v_type == VAR_DICT)
15470 || (**arg == '(' && rettv->v_type == VAR_FUNC))
15471 && !vim_iswhite(*(*arg - 1)))
15472 {
15473 if (**arg == '(')
15474 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000015475 /* need to copy the funcref so that we can clear rettv */
15476 functv = *rettv;
15477 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015478
15479 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000015480 s = functv.vval.v_string;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015481 ret = get_func_tv(s, STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000015482 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
15483 &len, evaluate, selfdict);
15484
15485 /* Clear the funcref afterwards, so that deleting it while
15486 * evaluating the arguments is possible (see test55). */
15487 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015488
15489 /* Stop the expression evaluation when immediately aborting on
15490 * error, or when an interrupt occurred or an exception was thrown
15491 * but not caught. */
15492 if (aborting())
15493 {
15494 if (ret == OK)
15495 clear_tv(rettv);
15496 ret = FAIL;
15497 }
15498 dict_unref(selfdict);
15499 selfdict = NULL;
15500 }
15501 else /* **arg == '[' || **arg == '.' */
15502 {
15503 dict_unref(selfdict);
15504 if (rettv->v_type == VAR_DICT)
15505 {
15506 selfdict = rettv->vval.v_dict;
15507 if (selfdict != NULL)
15508 ++selfdict->dv_refcount;
15509 }
15510 else
15511 selfdict = NULL;
15512 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
15513 {
15514 clear_tv(rettv);
15515 ret = FAIL;
15516 }
15517 }
15518 }
15519 dict_unref(selfdict);
15520 return ret;
15521}
15522
15523/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015524 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
15525 * value).
15526 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015527 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015528alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015529{
Bram Moolenaar33570922005-01-25 22:26:29 +000015530 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015531}
15532
15533/*
15534 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015535 * The string "s" must have been allocated, it is consumed.
15536 * Return NULL for out of memory, the variable otherwise.
15537 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015538 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015539alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015540 char_u *s;
15541{
Bram Moolenaar33570922005-01-25 22:26:29 +000015542 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015543
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015544 rettv = alloc_tv();
15545 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015546 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015547 rettv->v_type = VAR_STRING;
15548 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015549 }
15550 else
15551 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015552 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015553}
15554
15555/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015556 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015557 */
15558 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015559free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015560 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015561{
15562 if (varp != NULL)
15563 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015564 switch (varp->v_type)
15565 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015566 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015567 func_unref(varp->vval.v_string);
15568 /*FALLTHROUGH*/
15569 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015570 vim_free(varp->vval.v_string);
15571 break;
15572 case VAR_LIST:
15573 list_unref(varp->vval.v_list);
15574 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015575 case VAR_DICT:
15576 dict_unref(varp->vval.v_dict);
15577 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015578 case VAR_NUMBER:
15579 case VAR_UNKNOWN:
15580 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015581 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000015582 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015583 break;
15584 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015585 vim_free(varp);
15586 }
15587}
15588
15589/*
15590 * Free the memory for a variable value and set the value to NULL or 0.
15591 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000015592 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015593clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015594 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015595{
15596 if (varp != NULL)
15597 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015598 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015599 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015600 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015601 func_unref(varp->vval.v_string);
15602 /*FALLTHROUGH*/
15603 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015604 vim_free(varp->vval.v_string);
15605 varp->vval.v_string = NULL;
15606 break;
15607 case VAR_LIST:
15608 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015609 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015610 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015611 case VAR_DICT:
15612 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015613 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015614 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015615 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015616 varp->vval.v_number = 0;
15617 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015618 case VAR_UNKNOWN:
15619 break;
15620 default:
15621 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000015622 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015623 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015624 }
15625}
15626
15627/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015628 * Set the value of a variable to NULL without freeing items.
15629 */
15630 static void
15631init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015632 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015633{
15634 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000015635 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015636}
15637
15638/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015639 * Get the number value of a variable.
15640 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015641 * For incompatible types, return 0.
15642 * get_tv_number_chk() is similar to get_tv_number(), but informs the
15643 * caller of incompatible types: it sets *denote to TRUE if "denote"
15644 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015645 */
15646 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015647get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015648 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015649{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015650 int error = FALSE;
15651
15652 return get_tv_number_chk(varp, &error); /* return 0L on error */
15653}
15654
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015655 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015656get_tv_number_chk(varp, denote)
15657 typval_T *varp;
15658 int *denote;
15659{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015660 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015661
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015662 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015663 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015664 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015665 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015666 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015667 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015668 break;
15669 case VAR_STRING:
15670 if (varp->vval.v_string != NULL)
15671 vim_str2nr(varp->vval.v_string, NULL, NULL,
15672 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015673 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015674 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000015675 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015676 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015677 case VAR_DICT:
15678 EMSG(_("E728: Using a Dictionary as a number"));
15679 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015680 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015681 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015682 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015683 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015684 if (denote == NULL) /* useful for values that must be unsigned */
15685 n = -1;
15686 else
15687 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015688 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015689}
15690
15691/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000015692 * Get the lnum from the first argument.
15693 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015694 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015695 */
15696 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015697get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000015698 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015699{
Bram Moolenaar33570922005-01-25 22:26:29 +000015700 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015701 linenr_T lnum;
15702
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015703 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015704 if (lnum == 0) /* no valid number, try using line() */
15705 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015706 rettv.v_type = VAR_NUMBER;
15707 f_line(argvars, &rettv);
15708 lnum = rettv.vval.v_number;
15709 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015710 }
15711 return lnum;
15712}
15713
15714/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000015715 * Get the lnum from the first argument.
15716 * Also accepts "$", then "buf" is used.
15717 * Returns 0 on error.
15718 */
15719 static linenr_T
15720get_tv_lnum_buf(argvars, buf)
15721 typval_T *argvars;
15722 buf_T *buf;
15723{
15724 if (argvars[0].v_type == VAR_STRING
15725 && argvars[0].vval.v_string != NULL
15726 && argvars[0].vval.v_string[0] == '$'
15727 && buf != NULL)
15728 return buf->b_ml.ml_line_count;
15729 return get_tv_number_chk(&argvars[0], NULL);
15730}
15731
15732/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015733 * Get the string value of a variable.
15734 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000015735 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
15736 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015737 * If the String variable has never been set, return an empty string.
15738 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015739 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
15740 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015741 */
15742 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015743get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015744 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015745{
15746 static char_u mybuf[NUMBUFLEN];
15747
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015748 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015749}
15750
15751 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015752get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000015753 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015754 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015755{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015756 char_u *res = get_tv_string_buf_chk(varp, buf);
15757
15758 return res != NULL ? res : (char_u *)"";
15759}
15760
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015761 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015762get_tv_string_chk(varp)
15763 typval_T *varp;
15764{
15765 static char_u mybuf[NUMBUFLEN];
15766
15767 return get_tv_string_buf_chk(varp, mybuf);
15768}
15769
15770 static char_u *
15771get_tv_string_buf_chk(varp, buf)
15772 typval_T *varp;
15773 char_u *buf;
15774{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015775 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015776 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015777 case VAR_NUMBER:
15778 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
15779 return buf;
15780 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015781 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015782 break;
15783 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015784 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015785 break;
15786 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015787 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015788 break;
15789 case VAR_STRING:
15790 if (varp->vval.v_string != NULL)
15791 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015792 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015793 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015794 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015795 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015796 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015797 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015798}
15799
15800/*
15801 * Find variable "name" in the list of variables.
15802 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015803 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000015804 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000015805 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015806 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015807 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000015808find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015809 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000015810 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015811{
Bram Moolenaar071d4272004-06-13 20:20:40 +000015812 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015813 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015814
Bram Moolenaara7043832005-01-21 11:56:39 +000015815 ht = find_var_ht(name, &varname);
15816 if (htp != NULL)
15817 *htp = ht;
15818 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015819 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015820 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015821}
15822
15823/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015824 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000015825 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015826 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015827 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015828find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000015829 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000015830 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015831 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000015832{
Bram Moolenaar33570922005-01-25 22:26:29 +000015833 hashitem_T *hi;
15834
15835 if (*varname == NUL)
15836 {
15837 /* Must be something like "s:", otherwise "ht" would be NULL. */
15838 switch (varname[-2])
15839 {
15840 case 's': return &SCRIPT_SV(current_SID).sv_var;
15841 case 'g': return &globvars_var;
15842 case 'v': return &vimvars_var;
15843 case 'b': return &curbuf->b_bufvar;
15844 case 'w': return &curwin->w_winvar;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000015845 case 'l': return current_funccal == NULL
15846 ? NULL : &current_funccal->l_vars_var;
15847 case 'a': return current_funccal == NULL
15848 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000015849 }
15850 return NULL;
15851 }
Bram Moolenaara7043832005-01-21 11:56:39 +000015852
15853 hi = hash_find(ht, varname);
15854 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015855 {
15856 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000015857 * worked find the variable again. Don't auto-load a script if it was
15858 * loaded already, otherwise it would be loaded every time when
15859 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015860 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000015861 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015862 hi = hash_find(ht, varname);
15863 if (HASHITEM_EMPTY(hi))
15864 return NULL;
15865 }
Bram Moolenaar33570922005-01-25 22:26:29 +000015866 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000015867}
15868
15869/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015870 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000015871 * Set "varname" to the start of name without ':'.
15872 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015873 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000015874find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015875 char_u *name;
15876 char_u **varname;
15877{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000015878 hashitem_T *hi;
15879
Bram Moolenaar071d4272004-06-13 20:20:40 +000015880 if (name[1] != ':')
15881 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015882 /* The name must not start with a colon or #. */
15883 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015884 return NULL;
15885 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015886
15887 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000015888 hi = hash_find(&compat_hashtab, name);
15889 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000015890 return &compat_hashtab;
15891
Bram Moolenaar071d4272004-06-13 20:20:40 +000015892 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000015893 return &globvarht; /* global variable */
15894 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015895 }
15896 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015897 if (*name == 'g') /* global variable */
15898 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015899 /* There must be no ':' or '#' in the rest of the name, unless g: is used
15900 */
15901 if (vim_strchr(name + 2, ':') != NULL
15902 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015903 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015904 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000015905 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015906 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000015907 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000015908 if (*name == 'v') /* v: variable */
15909 return &vimvarht;
15910 if (*name == 'a' && current_funccal != NULL) /* function argument */
15911 return &current_funccal->l_avars.dv_hashtab;
15912 if (*name == 'l' && current_funccal != NULL) /* local function variable */
15913 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015914 if (*name == 's' /* script variable */
15915 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
15916 return &SCRIPT_VARS(current_SID);
15917 return NULL;
15918}
15919
15920/*
15921 * Get the string value of a (global/local) variable.
15922 * Returns NULL when it doesn't exist.
15923 */
15924 char_u *
15925get_var_value(name)
15926 char_u *name;
15927{
Bram Moolenaar33570922005-01-25 22:26:29 +000015928 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015929
Bram Moolenaara7043832005-01-21 11:56:39 +000015930 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015931 if (v == NULL)
15932 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000015933 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015934}
15935
15936/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015937 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000015938 * sourcing this script and when executing functions defined in the script.
15939 */
15940 void
15941new_script_vars(id)
15942 scid_T id;
15943{
Bram Moolenaara7043832005-01-21 11:56:39 +000015944 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000015945 hashtab_T *ht;
15946 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000015947
Bram Moolenaar071d4272004-06-13 20:20:40 +000015948 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
15949 {
Bram Moolenaara7043832005-01-21 11:56:39 +000015950 /* Re-allocating ga_data means that an ht_array pointing to
15951 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000015952 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000015953 for (i = 1; i <= ga_scripts.ga_len; ++i)
15954 {
15955 ht = &SCRIPT_VARS(i);
15956 if (ht->ht_mask == HT_INIT_SIZE - 1)
15957 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000015958 sv = &SCRIPT_SV(i);
15959 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000015960 }
15961
Bram Moolenaar071d4272004-06-13 20:20:40 +000015962 while (ga_scripts.ga_len < id)
15963 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015964 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
15965 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015966 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015967 }
15968 }
15969}
15970
15971/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015972 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
15973 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015974 */
15975 void
Bram Moolenaar33570922005-01-25 22:26:29 +000015976init_var_dict(dict, dict_var)
15977 dict_T *dict;
15978 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015979{
Bram Moolenaar33570922005-01-25 22:26:29 +000015980 hash_init(&dict->dv_hashtab);
15981 dict->dv_refcount = 99999;
15982 dict_var->di_tv.vval.v_dict = dict;
15983 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015984 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015985 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
15986 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015987}
15988
15989/*
15990 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000015991 * Frees all allocated variables and the value they contain.
15992 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015993 */
15994 void
Bram Moolenaara7043832005-01-21 11:56:39 +000015995vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000015996 hashtab_T *ht;
15997{
15998 vars_clear_ext(ht, TRUE);
15999}
16000
16001/*
16002 * Like vars_clear(), but only free the value if "free_val" is TRUE.
16003 */
16004 static void
16005vars_clear_ext(ht, free_val)
16006 hashtab_T *ht;
16007 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016008{
Bram Moolenaara7043832005-01-21 11:56:39 +000016009 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000016010 hashitem_T *hi;
16011 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016012
Bram Moolenaar33570922005-01-25 22:26:29 +000016013 hash_lock(ht);
Bram Moolenaara7043832005-01-21 11:56:39 +000016014 todo = ht->ht_used;
16015 for (hi = ht->ht_array; todo > 0; ++hi)
16016 {
16017 if (!HASHITEM_EMPTY(hi))
16018 {
16019 --todo;
16020
Bram Moolenaar33570922005-01-25 22:26:29 +000016021 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000016022 * ht_array might change then. hash_clear() takes care of it
16023 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016024 v = HI2DI(hi);
16025 if (free_val)
16026 clear_tv(&v->di_tv);
16027 if ((v->di_flags & DI_FLAGS_FIX) == 0)
16028 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000016029 }
16030 }
16031 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016032 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016033}
16034
Bram Moolenaara7043832005-01-21 11:56:39 +000016035/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016036 * Delete a variable from hashtab "ht" at item "hi".
16037 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000016038 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016039 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000016040delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000016041 hashtab_T *ht;
16042 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016043{
Bram Moolenaar33570922005-01-25 22:26:29 +000016044 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000016045
16046 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000016047 clear_tv(&di->di_tv);
16048 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016049}
16050
16051/*
16052 * List the value of one internal variable.
16053 */
16054 static void
16055list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000016056 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016057 char_u *prefix;
16058{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016059 char_u *tofree;
16060 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016061 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016062
Bram Moolenaar33570922005-01-25 22:26:29 +000016063 s = echo_string(&v->di_tv, &tofree, numbuf);
16064 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016065 s == NULL ? (char_u *)"" : s);
16066 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016067}
16068
Bram Moolenaar071d4272004-06-13 20:20:40 +000016069 static void
16070list_one_var_a(prefix, name, type, string)
16071 char_u *prefix;
16072 char_u *name;
16073 int type;
16074 char_u *string;
16075{
16076 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
16077 if (name != NULL) /* "a:" vars don't have a name stored */
16078 msg_puts(name);
16079 msg_putchar(' ');
16080 msg_advance(22);
16081 if (type == VAR_NUMBER)
16082 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016083 else if (type == VAR_FUNC)
16084 msg_putchar('*');
16085 else if (type == VAR_LIST)
16086 {
16087 msg_putchar('[');
16088 if (*string == '[')
16089 ++string;
16090 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016091 else if (type == VAR_DICT)
16092 {
16093 msg_putchar('{');
16094 if (*string == '{')
16095 ++string;
16096 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016097 else
16098 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016099
Bram Moolenaar071d4272004-06-13 20:20:40 +000016100 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016101
16102 if (type == VAR_FUNC)
16103 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016104}
16105
16106/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016107 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000016108 * If the variable already exists, the value is updated.
16109 * Otherwise the variable is created.
16110 */
16111 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016112set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016113 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000016114 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016115 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016116{
Bram Moolenaar33570922005-01-25 22:26:29 +000016117 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016118 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016119 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000016120 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016121
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016122 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016123 {
16124 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
16125 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
16126 ? name[2] : name[0]))
16127 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016128 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016129 return;
16130 }
16131 if (function_exists(name))
16132 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016133 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016134 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016135 return;
16136 }
16137 }
16138
Bram Moolenaara7043832005-01-21 11:56:39 +000016139 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000016140 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000016141 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000016142 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000016143 return;
16144 }
16145
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016146 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016147 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016148 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016149 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016150 if (var_check_ro(v->di_flags, name)
16151 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000016152 return;
16153 if (v->di_tv.v_type != tv->v_type
16154 && !((v->di_tv.v_type == VAR_STRING
16155 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016156 && (tv->v_type == VAR_STRING
16157 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016158 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016159 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016160 return;
16161 }
Bram Moolenaar33570922005-01-25 22:26:29 +000016162
16163 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000016164 * Handle setting internal v: variables separately: we don't change
16165 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000016166 */
16167 if (ht == &vimvarht)
16168 {
16169 if (v->di_tv.v_type == VAR_STRING)
16170 {
16171 vim_free(v->di_tv.vval.v_string);
16172 if (copy || tv->v_type != VAR_STRING)
16173 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
16174 else
16175 {
16176 /* Take over the string to avoid an extra alloc/free. */
16177 v->di_tv.vval.v_string = tv->vval.v_string;
16178 tv->vval.v_string = NULL;
16179 }
16180 }
16181 else if (v->di_tv.v_type != VAR_NUMBER)
16182 EMSG2(_(e_intern2), "set_var()");
16183 else
16184 v->di_tv.vval.v_number = get_tv_number(tv);
16185 return;
16186 }
16187
16188 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016189 }
16190 else /* add a new variable */
16191 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000016192 /* Make sure the variable name is valid. */
16193 for (p = varname; *p != NUL; ++p)
16194 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p)))
16195 {
16196 EMSG2(_(e_illvar), varname);
16197 return;
16198 }
16199
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016200 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
16201 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000016202 if (v == NULL)
16203 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000016204 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000016205 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016206 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016207 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016208 return;
16209 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016210 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016211 }
Bram Moolenaara7043832005-01-21 11:56:39 +000016212
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016213 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000016214 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016215 else
16216 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016217 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016218 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016219 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016221}
16222
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016223/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016224 * Return TRUE if di_flags "flags" indicate read-only variable "name".
16225 * Also give an error message.
16226 */
16227 static int
16228var_check_ro(flags, name)
16229 int flags;
16230 char_u *name;
16231{
16232 if (flags & DI_FLAGS_RO)
16233 {
16234 EMSG2(_(e_readonlyvar), name);
16235 return TRUE;
16236 }
16237 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
16238 {
16239 EMSG2(_(e_readonlysbx), name);
16240 return TRUE;
16241 }
16242 return FALSE;
16243}
16244
16245/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016246 * Return TRUE if typeval "tv" is set to be locked (immutable).
16247 * Also give an error message, using "name".
16248 */
16249 static int
16250tv_check_lock(lock, name)
16251 int lock;
16252 char_u *name;
16253{
16254 if (lock & VAR_LOCKED)
16255 {
16256 EMSG2(_("E741: Value is locked: %s"),
16257 name == NULL ? (char_u *)_("Unknown") : name);
16258 return TRUE;
16259 }
16260 if (lock & VAR_FIXED)
16261 {
16262 EMSG2(_("E742: Cannot change value of %s"),
16263 name == NULL ? (char_u *)_("Unknown") : name);
16264 return TRUE;
16265 }
16266 return FALSE;
16267}
16268
16269/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016270 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016271 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016272 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016273 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016274 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016275copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000016276 typval_T *from;
16277 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016278{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016279 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016280 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016281 switch (from->v_type)
16282 {
16283 case VAR_NUMBER:
16284 to->vval.v_number = from->vval.v_number;
16285 break;
16286 case VAR_STRING:
16287 case VAR_FUNC:
16288 if (from->vval.v_string == NULL)
16289 to->vval.v_string = NULL;
16290 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016291 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016292 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016293 if (from->v_type == VAR_FUNC)
16294 func_ref(to->vval.v_string);
16295 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016296 break;
16297 case VAR_LIST:
16298 if (from->vval.v_list == NULL)
16299 to->vval.v_list = NULL;
16300 else
16301 {
16302 to->vval.v_list = from->vval.v_list;
16303 ++to->vval.v_list->lv_refcount;
16304 }
16305 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016306 case VAR_DICT:
16307 if (from->vval.v_dict == NULL)
16308 to->vval.v_dict = NULL;
16309 else
16310 {
16311 to->vval.v_dict = from->vval.v_dict;
16312 ++to->vval.v_dict->dv_refcount;
16313 }
16314 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016315 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016316 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016317 break;
16318 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016319}
16320
16321/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000016322 * Make a copy of an item.
16323 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016324 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
16325 * reference to an already copied list/dict can be used.
16326 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016327 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016328 static int
16329item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000016330 typval_T *from;
16331 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016332 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016333 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016334{
16335 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016336 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016337
Bram Moolenaar33570922005-01-25 22:26:29 +000016338 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016339 {
16340 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016341 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016342 }
16343 ++recurse;
16344
16345 switch (from->v_type)
16346 {
16347 case VAR_NUMBER:
16348 case VAR_STRING:
16349 case VAR_FUNC:
16350 copy_tv(from, to);
16351 break;
16352 case VAR_LIST:
16353 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016354 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016355 if (from->vval.v_list == NULL)
16356 to->vval.v_list = NULL;
16357 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
16358 {
16359 /* use the copy made earlier */
16360 to->vval.v_list = from->vval.v_list->lv_copylist;
16361 ++to->vval.v_list->lv_refcount;
16362 }
16363 else
16364 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
16365 if (to->vval.v_list == NULL)
16366 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016367 break;
16368 case VAR_DICT:
16369 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016370 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016371 if (from->vval.v_dict == NULL)
16372 to->vval.v_dict = NULL;
16373 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
16374 {
16375 /* use the copy made earlier */
16376 to->vval.v_dict = from->vval.v_dict->dv_copydict;
16377 ++to->vval.v_dict->dv_refcount;
16378 }
16379 else
16380 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
16381 if (to->vval.v_dict == NULL)
16382 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016383 break;
16384 default:
16385 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016386 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016387 }
16388 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016389 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016390}
16391
16392/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016393 * ":echo expr1 ..." print each argument separated with a space, add a
16394 * newline at the end.
16395 * ":echon expr1 ..." print each argument plain.
16396 */
16397 void
16398ex_echo(eap)
16399 exarg_T *eap;
16400{
16401 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000016402 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016403 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016404 char_u *p;
16405 int needclr = TRUE;
16406 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016407 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016408
16409 if (eap->skip)
16410 ++emsg_skip;
16411 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
16412 {
16413 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016414 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016415 {
16416 /*
16417 * Report the invalid expression unless the expression evaluation
16418 * has been cancelled due to an aborting error, an interrupt, or an
16419 * exception.
16420 */
16421 if (!aborting())
16422 EMSG2(_(e_invexpr2), p);
16423 break;
16424 }
16425 if (!eap->skip)
16426 {
16427 if (atstart)
16428 {
16429 atstart = FALSE;
16430 /* Call msg_start() after eval1(), evaluating the expression
16431 * may cause a message to appear. */
16432 if (eap->cmdidx == CMD_echo)
16433 msg_start();
16434 }
16435 else if (eap->cmdidx == CMD_echo)
16436 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016437 p = echo_string(&rettv, &tofree, numbuf);
16438 if (p != NULL)
16439 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016440 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016441 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016442 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016443 if (*p != TAB && needclr)
16444 {
16445 /* remove any text still there from the command */
16446 msg_clr_eos();
16447 needclr = FALSE;
16448 }
16449 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016450 }
16451 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016452 {
16453#ifdef FEAT_MBYTE
16454 if (has_mbyte)
16455 {
16456 int i = (*mb_ptr2len_check)(p);
16457
16458 (void)msg_outtrans_len_attr(p, i, echo_attr);
16459 p += i - 1;
16460 }
16461 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000016462#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016463 (void)msg_outtrans_len_attr(p, 1, echo_attr);
16464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016465 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016466 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016467 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016468 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016469 arg = skipwhite(arg);
16470 }
16471 eap->nextcmd = check_nextcmd(arg);
16472
16473 if (eap->skip)
16474 --emsg_skip;
16475 else
16476 {
16477 /* remove text that may still be there from the command */
16478 if (needclr)
16479 msg_clr_eos();
16480 if (eap->cmdidx == CMD_echo)
16481 msg_end();
16482 }
16483}
16484
16485/*
16486 * ":echohl {name}".
16487 */
16488 void
16489ex_echohl(eap)
16490 exarg_T *eap;
16491{
16492 int id;
16493
16494 id = syn_name2id(eap->arg);
16495 if (id == 0)
16496 echo_attr = 0;
16497 else
16498 echo_attr = syn_id2attr(id);
16499}
16500
16501/*
16502 * ":execute expr1 ..." execute the result of an expression.
16503 * ":echomsg expr1 ..." Print a message
16504 * ":echoerr expr1 ..." Print an error
16505 * Each gets spaces around each argument and a newline at the end for
16506 * echo commands
16507 */
16508 void
16509ex_execute(eap)
16510 exarg_T *eap;
16511{
16512 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000016513 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016514 int ret = OK;
16515 char_u *p;
16516 garray_T ga;
16517 int len;
16518 int save_did_emsg;
16519
16520 ga_init2(&ga, 1, 80);
16521
16522 if (eap->skip)
16523 ++emsg_skip;
16524 while (*arg != NUL && *arg != '|' && *arg != '\n')
16525 {
16526 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016527 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016528 {
16529 /*
16530 * Report the invalid expression unless the expression evaluation
16531 * has been cancelled due to an aborting error, an interrupt, or an
16532 * exception.
16533 */
16534 if (!aborting())
16535 EMSG2(_(e_invexpr2), p);
16536 ret = FAIL;
16537 break;
16538 }
16539
16540 if (!eap->skip)
16541 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016542 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016543 len = (int)STRLEN(p);
16544 if (ga_grow(&ga, len + 2) == FAIL)
16545 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016546 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016547 ret = FAIL;
16548 break;
16549 }
16550 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016551 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016552 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016553 ga.ga_len += len;
16554 }
16555
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016556 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016557 arg = skipwhite(arg);
16558 }
16559
16560 if (ret != FAIL && ga.ga_data != NULL)
16561 {
16562 if (eap->cmdidx == CMD_echomsg)
16563 MSG_ATTR(ga.ga_data, echo_attr);
16564 else if (eap->cmdidx == CMD_echoerr)
16565 {
16566 /* We don't want to abort following commands, restore did_emsg. */
16567 save_did_emsg = did_emsg;
16568 EMSG((char_u *)ga.ga_data);
16569 if (!force_abort)
16570 did_emsg = save_did_emsg;
16571 }
16572 else if (eap->cmdidx == CMD_execute)
16573 do_cmdline((char_u *)ga.ga_data,
16574 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
16575 }
16576
16577 ga_clear(&ga);
16578
16579 if (eap->skip)
16580 --emsg_skip;
16581
16582 eap->nextcmd = check_nextcmd(arg);
16583}
16584
16585/*
16586 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
16587 * "arg" points to the "&" or '+' when called, to "option" when returning.
16588 * Returns NULL when no option name found. Otherwise pointer to the char
16589 * after the option name.
16590 */
16591 static char_u *
16592find_option_end(arg, opt_flags)
16593 char_u **arg;
16594 int *opt_flags;
16595{
16596 char_u *p = *arg;
16597
16598 ++p;
16599 if (*p == 'g' && p[1] == ':')
16600 {
16601 *opt_flags = OPT_GLOBAL;
16602 p += 2;
16603 }
16604 else if (*p == 'l' && p[1] == ':')
16605 {
16606 *opt_flags = OPT_LOCAL;
16607 p += 2;
16608 }
16609 else
16610 *opt_flags = 0;
16611
16612 if (!ASCII_ISALPHA(*p))
16613 return NULL;
16614 *arg = p;
16615
16616 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
16617 p += 4; /* termcap option */
16618 else
16619 while (ASCII_ISALPHA(*p))
16620 ++p;
16621 return p;
16622}
16623
16624/*
16625 * ":function"
16626 */
16627 void
16628ex_function(eap)
16629 exarg_T *eap;
16630{
16631 char_u *theline;
16632 int j;
16633 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016634 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016635 char_u *name = NULL;
16636 char_u *p;
16637 char_u *arg;
16638 garray_T newargs;
16639 garray_T newlines;
16640 int varargs = FALSE;
16641 int mustend = FALSE;
16642 int flags = 0;
16643 ufunc_T *fp;
16644 int indent;
16645 int nesting;
16646 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000016647 dictitem_T *v;
16648 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016649 static int func_nr = 0; /* number for nameless function */
16650 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016651 hashtab_T *ht;
16652 int todo;
16653 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016654
16655 /*
16656 * ":function" without argument: list functions.
16657 */
16658 if (ends_excmd(*eap->arg))
16659 {
16660 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016661 {
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000016662 todo = func_hashtab.ht_used;
16663 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016664 {
16665 if (!HASHITEM_EMPTY(hi))
16666 {
16667 --todo;
16668 fp = HI2UF(hi);
16669 if (!isdigit(*fp->uf_name))
16670 list_func_head(fp, FALSE);
16671 }
16672 }
16673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016674 eap->nextcmd = check_nextcmd(eap->arg);
16675 return;
16676 }
16677
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016678 /*
16679 * Get the function name. There are these situations:
16680 * func normal function name
16681 * "name" == func, "fudi.fd_dict" == NULL
16682 * dict.func new dictionary entry
16683 * "name" == NULL, "fudi.fd_dict" set,
16684 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
16685 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016686 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016687 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
16688 * dict.func existing dict entry that's not a Funcref
16689 * "name" == NULL, "fudi.fd_dict" set,
16690 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
16691 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016692 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016693 name = trans_function_name(&p, eap->skip, 0, &fudi);
16694 paren = (vim_strchr(p, '(') != NULL);
16695 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016696 {
16697 /*
16698 * Return on an invalid expression in braces, unless the expression
16699 * evaluation has been cancelled due to an aborting error, an
16700 * interrupt, or an exception.
16701 */
16702 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016703 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016704 if (!eap->skip && fudi.fd_newkey != NULL)
16705 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016706 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016707 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016709 else
16710 eap->skip = TRUE;
16711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016712 /* An error in a function call during evaluation of an expression in magic
16713 * braces should not cause the function not to be defined. */
16714 saved_did_emsg = did_emsg;
16715 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016716
16717 /*
16718 * ":function func" with only function name: list function.
16719 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016720 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016721 {
16722 if (!ends_excmd(*skipwhite(p)))
16723 {
16724 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016725 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016726 }
16727 eap->nextcmd = check_nextcmd(p);
16728 if (eap->nextcmd != NULL)
16729 *p = NUL;
16730 if (!eap->skip && !got_int)
16731 {
16732 fp = find_func(name);
16733 if (fp != NULL)
16734 {
16735 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016736 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016737 {
16738 msg_putchar('\n');
16739 msg_outnum((long)(j + 1));
16740 if (j < 9)
16741 msg_putchar(' ');
16742 if (j < 99)
16743 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016744 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016745 out_flush(); /* show a line at a time */
16746 ui_breakcheck();
16747 }
16748 if (!got_int)
16749 {
16750 msg_putchar('\n');
16751 msg_puts((char_u *)" endfunction");
16752 }
16753 }
16754 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016755 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016756 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016757 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016758 }
16759
16760 /*
16761 * ":function name(arg1, arg2)" Define function.
16762 */
16763 p = skipwhite(p);
16764 if (*p != '(')
16765 {
16766 if (!eap->skip)
16767 {
16768 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016769 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016770 }
16771 /* attempt to continue by skipping some text */
16772 if (vim_strchr(p, '(') != NULL)
16773 p = vim_strchr(p, '(');
16774 }
16775 p = skipwhite(p + 1);
16776
16777 ga_init2(&newargs, (int)sizeof(char_u *), 3);
16778 ga_init2(&newlines, (int)sizeof(char_u *), 3);
16779
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016780 if (!eap->skip)
16781 {
16782 /* Check the name of the function. */
16783 if (name != NULL)
16784 arg = name;
16785 else
16786 arg = fudi.fd_newkey;
16787 if (arg != NULL)
16788 {
16789 if (*arg == K_SPECIAL)
16790 j = 3;
16791 else
16792 j = 0;
16793 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
16794 : eval_isnamec(arg[j])))
16795 ++j;
16796 if (arg[j] != NUL)
16797 emsg_funcname(_(e_invarg2), arg);
16798 }
16799 }
16800
Bram Moolenaar071d4272004-06-13 20:20:40 +000016801 /*
16802 * Isolate the arguments: "arg1, arg2, ...)"
16803 */
16804 while (*p != ')')
16805 {
16806 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
16807 {
16808 varargs = TRUE;
16809 p += 3;
16810 mustend = TRUE;
16811 }
16812 else
16813 {
16814 arg = p;
16815 while (ASCII_ISALNUM(*p) || *p == '_')
16816 ++p;
16817 if (arg == p || isdigit(*arg)
16818 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
16819 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
16820 {
16821 if (!eap->skip)
16822 EMSG2(_("E125: Illegal argument: %s"), arg);
16823 break;
16824 }
16825 if (ga_grow(&newargs, 1) == FAIL)
16826 goto erret;
16827 c = *p;
16828 *p = NUL;
16829 arg = vim_strsave(arg);
16830 if (arg == NULL)
16831 goto erret;
16832 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
16833 *p = c;
16834 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016835 if (*p == ',')
16836 ++p;
16837 else
16838 mustend = TRUE;
16839 }
16840 p = skipwhite(p);
16841 if (mustend && *p != ')')
16842 {
16843 if (!eap->skip)
16844 EMSG2(_(e_invarg2), eap->arg);
16845 break;
16846 }
16847 }
16848 ++p; /* skip the ')' */
16849
Bram Moolenaare9a41262005-01-15 22:18:47 +000016850 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016851 for (;;)
16852 {
16853 p = skipwhite(p);
16854 if (STRNCMP(p, "range", 5) == 0)
16855 {
16856 flags |= FC_RANGE;
16857 p += 5;
16858 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000016859 else if (STRNCMP(p, "dict", 4) == 0)
16860 {
16861 flags |= FC_DICT;
16862 p += 4;
16863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016864 else if (STRNCMP(p, "abort", 5) == 0)
16865 {
16866 flags |= FC_ABORT;
16867 p += 5;
16868 }
16869 else
16870 break;
16871 }
16872
16873 if (*p != NUL && *p != '"' && *p != '\n' && !eap->skip && !did_emsg)
16874 EMSG(_(e_trailing));
16875
16876 /*
16877 * Read the body of the function, until ":endfunction" is found.
16878 */
16879 if (KeyTyped)
16880 {
16881 /* Check if the function already exists, don't let the user type the
16882 * whole function before telling him it doesn't work! For a script we
16883 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016884 if (!eap->skip && !eap->forceit)
16885 {
16886 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
16887 EMSG(_(e_funcdict));
16888 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016889 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016891
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016892 if (!eap->skip && did_emsg)
16893 goto erret;
16894
Bram Moolenaar071d4272004-06-13 20:20:40 +000016895 msg_putchar('\n'); /* don't overwrite the function name */
16896 cmdline_row = msg_row;
16897 }
16898
16899 indent = 2;
16900 nesting = 0;
16901 for (;;)
16902 {
16903 msg_scroll = TRUE;
16904 need_wait_return = FALSE;
16905 if (eap->getline == NULL)
16906 theline = getcmdline(':', 0L, indent);
16907 else
16908 theline = eap->getline(':', eap->cookie, indent);
16909 if (KeyTyped)
16910 lines_left = Rows - 1;
16911 if (theline == NULL)
16912 {
16913 EMSG(_("E126: Missing :endfunction"));
16914 goto erret;
16915 }
16916
16917 if (skip_until != NULL)
16918 {
16919 /* between ":append" and "." and between ":python <<EOF" and "EOF"
16920 * don't check for ":endfunc". */
16921 if (STRCMP(theline, skip_until) == 0)
16922 {
16923 vim_free(skip_until);
16924 skip_until = NULL;
16925 }
16926 }
16927 else
16928 {
16929 /* skip ':' and blanks*/
16930 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
16931 ;
16932
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016933 /* Check for "endfunction". */
16934 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016935 {
16936 vim_free(theline);
16937 break;
16938 }
16939
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016940 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000016941 * at "end". */
16942 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
16943 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016944 else if (STRNCMP(p, "if", 2) == 0
16945 || STRNCMP(p, "wh", 2) == 0
16946 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000016947 || STRNCMP(p, "try", 3) == 0)
16948 indent += 2;
16949
16950 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016951 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016952 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016953 if (*p == '!')
16954 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016955 p += eval_fname_script(p);
16956 if (ASCII_ISALPHA(*p))
16957 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016958 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016959 if (*skipwhite(p) == '(')
16960 {
16961 ++nesting;
16962 indent += 2;
16963 }
16964 }
16965 }
16966
16967 /* Check for ":append" or ":insert". */
16968 p = skip_range(p, NULL);
16969 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
16970 || (p[0] == 'i'
16971 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
16972 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
16973 skip_until = vim_strsave((char_u *)".");
16974
16975 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
16976 arg = skipwhite(skiptowhite(p));
16977 if (arg[0] == '<' && arg[1] =='<'
16978 && ((p[0] == 'p' && p[1] == 'y'
16979 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
16980 || (p[0] == 'p' && p[1] == 'e'
16981 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
16982 || (p[0] == 't' && p[1] == 'c'
16983 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
16984 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
16985 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000016986 || (p[0] == 'm' && p[1] == 'z'
16987 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016988 ))
16989 {
16990 /* ":python <<" continues until a dot, like ":append" */
16991 p = skipwhite(arg + 2);
16992 if (*p == NUL)
16993 skip_until = vim_strsave((char_u *)".");
16994 else
16995 skip_until = vim_strsave(p);
16996 }
16997 }
16998
16999 /* Add the line to the function. */
17000 if (ga_grow(&newlines, 1) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017001 {
17002 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017003 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017004 }
17005
17006 /* Copy the line to newly allocated memory. get_one_sourceline()
17007 * allocates 250 bytes per line, this saves 80% on average. The cost
17008 * is an extra alloc/free. */
17009 p = vim_strsave(theline);
17010 if (p != NULL)
17011 {
17012 vim_free(theline);
17013 theline = p;
17014 }
17015
Bram Moolenaar071d4272004-06-13 20:20:40 +000017016 ((char_u **)(newlines.ga_data))[newlines.ga_len] = theline;
17017 newlines.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017018 }
17019
17020 /* Don't define the function when skipping commands or when an error was
17021 * detected. */
17022 if (eap->skip || did_emsg)
17023 goto erret;
17024
17025 /*
17026 * If there are no errors, add the function
17027 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017028 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017029 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017030 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000017031 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017032 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017033 emsg_funcname("E707: Function name conflicts with variable: %s",
17034 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017035 goto erret;
17036 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017037
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017038 fp = find_func(name);
17039 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017040 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017041 if (!eap->forceit)
17042 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017043 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017044 goto erret;
17045 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017046 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017047 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017048 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017049 name);
17050 goto erret;
17051 }
17052 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017053 ga_clear_strings(&(fp->uf_args));
17054 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017055 vim_free(name);
17056 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017058 }
17059 else
17060 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017061 char numbuf[20];
17062
17063 fp = NULL;
17064 if (fudi.fd_newkey == NULL && !eap->forceit)
17065 {
17066 EMSG(_(e_funcdict));
17067 goto erret;
17068 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000017069 if (fudi.fd_di == NULL)
17070 {
17071 /* Can't add a function to a locked dictionary */
17072 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
17073 goto erret;
17074 }
17075 /* Can't change an existing function if it is locked */
17076 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
17077 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017078
17079 /* Give the function a sequential number. Can only be used with a
17080 * Funcref! */
17081 vim_free(name);
17082 sprintf(numbuf, "%d", ++func_nr);
17083 name = vim_strsave((char_u *)numbuf);
17084 if (name == NULL)
17085 goto erret;
17086 }
17087
17088 if (fp == NULL)
17089 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017090 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017091 {
17092 int slen, plen;
17093 char_u *scriptname;
17094
17095 /* Check that the autoload name matches the script name. */
17096 j = FAIL;
17097 if (sourcing_name != NULL)
17098 {
17099 scriptname = autoload_name(name);
17100 if (scriptname != NULL)
17101 {
17102 p = vim_strchr(scriptname, '/');
17103 plen = STRLEN(p);
17104 slen = STRLEN(sourcing_name);
17105 if (slen > plen && fnamecmp(p,
17106 sourcing_name + slen - plen) == 0)
17107 j = OK;
17108 vim_free(scriptname);
17109 }
17110 }
17111 if (j == FAIL)
17112 {
17113 EMSG2(_("E746: Function name does not match script file name: %s"), name);
17114 goto erret;
17115 }
17116 }
17117
17118 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017119 if (fp == NULL)
17120 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017121
17122 if (fudi.fd_dict != NULL)
17123 {
17124 if (fudi.fd_di == NULL)
17125 {
17126 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017127 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017128 if (fudi.fd_di == NULL)
17129 {
17130 vim_free(fp);
17131 goto erret;
17132 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017133 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
17134 {
17135 vim_free(fudi.fd_di);
17136 goto erret;
17137 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017138 }
17139 else
17140 /* overwrite existing dict entry */
17141 clear_tv(&fudi.fd_di->di_tv);
17142 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017143 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017144 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017145 fp->uf_refcount = 1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017146 }
17147
Bram Moolenaar071d4272004-06-13 20:20:40 +000017148 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017149 STRCPY(fp->uf_name, name);
17150 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017151 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017152 fp->uf_args = newargs;
17153 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017154#ifdef FEAT_PROFILE
17155 fp->uf_tml_count = NULL;
17156 fp->uf_tml_total = NULL;
17157 fp->uf_tml_self = NULL;
17158 fp->uf_profiling = FALSE;
17159 if (prof_def_func())
17160 func_do_profile(fp);
17161#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017162 fp->uf_varargs = varargs;
17163 fp->uf_flags = flags;
17164 fp->uf_calls = 0;
17165 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017166 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017167
17168erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017169 ga_clear_strings(&newargs);
17170 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017171ret_free:
17172 vim_free(skip_until);
17173 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017174 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017175 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017176}
17177
17178/*
17179 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000017180 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017181 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017182 * flags:
17183 * TFN_INT: internal function name OK
17184 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000017185 * Advances "pp" to just after the function name (if no error).
17186 */
17187 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017188trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017189 char_u **pp;
17190 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017191 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000017192 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017193{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017194 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017195 char_u *start;
17196 char_u *end;
17197 int lead;
17198 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017199 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000017200 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017201
17202 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017203 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017204 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000017205
17206 /* Check for hard coded <SNR>: already translated function ID (from a user
17207 * command). */
17208 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
17209 && (*pp)[2] == (int)KE_SNR)
17210 {
17211 *pp += 3;
17212 len = get_id_len(pp) + 3;
17213 return vim_strnsave(start, len);
17214 }
17215
17216 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
17217 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017218 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000017219 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017220 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017221
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017222 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
17223 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017224 if (end == start)
17225 {
17226 if (!skip)
17227 EMSG(_("E129: Function name required"));
17228 goto theend;
17229 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017230 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017231 {
17232 /*
17233 * Report an invalid expression in braces, unless the expression
17234 * evaluation has been cancelled due to an aborting error, an
17235 * interrupt, or an exception.
17236 */
17237 if (!aborting())
17238 {
17239 if (end != NULL)
17240 EMSG2(_(e_invarg2), start);
17241 }
17242 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017243 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017244 goto theend;
17245 }
17246
17247 if (lv.ll_tv != NULL)
17248 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017249 if (fdp != NULL)
17250 {
17251 fdp->fd_dict = lv.ll_dict;
17252 fdp->fd_newkey = lv.ll_newkey;
17253 lv.ll_newkey = NULL;
17254 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017255 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017256 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
17257 {
17258 name = vim_strsave(lv.ll_tv->vval.v_string);
17259 *pp = end;
17260 }
17261 else
17262 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017263 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
17264 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017265 EMSG(_(e_funcref));
17266 else
17267 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017268 name = NULL;
17269 }
17270 goto theend;
17271 }
17272
17273 if (lv.ll_name == NULL)
17274 {
17275 /* Error found, but continue after the function name. */
17276 *pp = end;
17277 goto theend;
17278 }
17279
17280 if (lv.ll_exp_name != NULL)
17281 len = STRLEN(lv.ll_exp_name);
17282 else
Bram Moolenaara7043832005-01-21 11:56:39 +000017283 {
17284 if (lead == 2) /* skip over "s:" */
17285 lv.ll_name += 2;
17286 len = (int)(end - lv.ll_name);
17287 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017288
17289 /*
17290 * Copy the function name to allocated memory.
17291 * Accept <SID>name() inside a script, translate into <SNR>123_name().
17292 * Accept <SNR>123_name() outside a script.
17293 */
17294 if (skip)
17295 lead = 0; /* do nothing */
17296 else if (lead > 0)
17297 {
17298 lead = 3;
17299 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
17300 {
17301 if (current_SID <= 0)
17302 {
17303 EMSG(_(e_usingsid));
17304 goto theend;
17305 }
17306 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
17307 lead += (int)STRLEN(sid_buf);
17308 }
17309 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017310 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017311 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017312 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017313 goto theend;
17314 }
17315 name = alloc((unsigned)(len + lead + 1));
17316 if (name != NULL)
17317 {
17318 if (lead > 0)
17319 {
17320 name[0] = K_SPECIAL;
17321 name[1] = KS_EXTRA;
17322 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000017323 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017324 STRCPY(name + 3, sid_buf);
17325 }
17326 mch_memmove(name + lead, lv.ll_name, (size_t)len);
17327 name[len + lead] = NUL;
17328 }
17329 *pp = end;
17330
17331theend:
17332 clear_lval(&lv);
17333 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017334}
17335
17336/*
17337 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
17338 * Return 2 if "p" starts with "s:".
17339 * Return 0 otherwise.
17340 */
17341 static int
17342eval_fname_script(p)
17343 char_u *p;
17344{
17345 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
17346 || STRNICMP(p + 1, "SNR>", 4) == 0))
17347 return 5;
17348 if (p[0] == 's' && p[1] == ':')
17349 return 2;
17350 return 0;
17351}
17352
17353/*
17354 * Return TRUE if "p" starts with "<SID>" or "s:".
17355 * Only works if eval_fname_script() returned non-zero for "p"!
17356 */
17357 static int
17358eval_fname_sid(p)
17359 char_u *p;
17360{
17361 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
17362}
17363
17364/*
17365 * List the head of the function: "name(arg1, arg2)".
17366 */
17367 static void
17368list_func_head(fp, indent)
17369 ufunc_T *fp;
17370 int indent;
17371{
17372 int j;
17373
17374 msg_start();
17375 if (indent)
17376 MSG_PUTS(" ");
17377 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017378 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017379 {
17380 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017381 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017382 }
17383 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017384 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017385 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017386 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017387 {
17388 if (j)
17389 MSG_PUTS(", ");
17390 msg_puts(FUNCARG(fp, j));
17391 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017392 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017393 {
17394 if (j)
17395 MSG_PUTS(", ");
17396 MSG_PUTS("...");
17397 }
17398 msg_putchar(')');
17399}
17400
17401/*
17402 * Find a function by name, return pointer to it in ufuncs.
17403 * Return NULL for unknown function.
17404 */
17405 static ufunc_T *
17406find_func(name)
17407 char_u *name;
17408{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017409 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017410
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017411 hi = hash_find(&func_hashtab, name);
17412 if (!HASHITEM_EMPTY(hi))
17413 return HI2UF(hi);
17414 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017415}
17416
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017417#if defined(EXITFREE) || defined(PROTO)
17418 void
17419free_all_functions()
17420{
17421 hashitem_T *hi;
17422
17423 /* Need to start all over every time, because func_free() may change the
17424 * hash table. */
17425 while (func_hashtab.ht_used > 0)
17426 for (hi = func_hashtab.ht_array; ; ++hi)
17427 if (!HASHITEM_EMPTY(hi))
17428 {
17429 func_free(HI2UF(hi));
17430 break;
17431 }
17432}
17433#endif
17434
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017435/*
17436 * Return TRUE if a function "name" exists.
17437 */
17438 static int
17439function_exists(name)
17440 char_u *name;
17441{
17442 char_u *p = name;
17443 int n = FALSE;
17444
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017445 p = trans_function_name(&p, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017446 if (p != NULL)
17447 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017448 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017449 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017450 else
17451 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017452 vim_free(p);
17453 }
17454 return n;
17455}
17456
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017457/*
17458 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017459 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017460 */
17461 static int
17462builtin_function(name)
17463 char_u *name;
17464{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017465 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
17466 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017467}
17468
Bram Moolenaar05159a02005-02-26 23:04:13 +000017469#if defined(FEAT_PROFILE) || defined(PROTO)
17470/*
17471 * Start profiling function "fp".
17472 */
17473 static void
17474func_do_profile(fp)
17475 ufunc_T *fp;
17476{
17477 fp->uf_tm_count = 0;
17478 profile_zero(&fp->uf_tm_self);
17479 profile_zero(&fp->uf_tm_total);
17480 if (fp->uf_tml_count == NULL)
17481 fp->uf_tml_count = (int *)alloc_clear((unsigned)
17482 (sizeof(int) * fp->uf_lines.ga_len));
17483 if (fp->uf_tml_total == NULL)
17484 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
17485 (sizeof(proftime_T) * fp->uf_lines.ga_len));
17486 if (fp->uf_tml_self == NULL)
17487 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
17488 (sizeof(proftime_T) * fp->uf_lines.ga_len));
17489 fp->uf_tml_idx = -1;
17490 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
17491 || fp->uf_tml_self == NULL)
17492 return; /* out of memory */
17493
17494 fp->uf_profiling = TRUE;
17495}
17496
17497/*
17498 * Dump the profiling results for all functions in file "fd".
17499 */
17500 void
17501func_dump_profile(fd)
17502 FILE *fd;
17503{
17504 hashitem_T *hi;
17505 int todo;
17506 ufunc_T *fp;
17507 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000017508 ufunc_T **sorttab;
17509 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017510
17511 todo = func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000017512 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
17513
Bram Moolenaar05159a02005-02-26 23:04:13 +000017514 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
17515 {
17516 if (!HASHITEM_EMPTY(hi))
17517 {
17518 --todo;
17519 fp = HI2UF(hi);
17520 if (fp->uf_profiling)
17521 {
Bram Moolenaar73830342005-02-28 22:48:19 +000017522 if (sorttab != NULL)
17523 sorttab[st_len++] = fp;
17524
Bram Moolenaar05159a02005-02-26 23:04:13 +000017525 if (fp->uf_name[0] == K_SPECIAL)
17526 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
17527 else
17528 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
17529 if (fp->uf_tm_count == 1)
17530 fprintf(fd, "Called 1 time\n");
17531 else
17532 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
17533 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
17534 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
17535 fprintf(fd, "\n");
17536 fprintf(fd, "count total (s) self (s)\n");
17537
17538 for (i = 0; i < fp->uf_lines.ga_len; ++i)
17539 {
Bram Moolenaar73830342005-02-28 22:48:19 +000017540 prof_func_line(fd, fp->uf_tml_count[i],
17541 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017542 fprintf(fd, "%s\n", FUNCLINE(fp, i));
17543 }
17544 fprintf(fd, "\n");
17545 }
17546 }
17547 }
Bram Moolenaar73830342005-02-28 22:48:19 +000017548
17549 if (sorttab != NULL && st_len > 0)
17550 {
17551 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
17552 prof_total_cmp);
17553 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
17554 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
17555 prof_self_cmp);
17556 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
17557 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000017558}
Bram Moolenaar73830342005-02-28 22:48:19 +000017559
17560 static void
17561prof_sort_list(fd, sorttab, st_len, title, prefer_self)
17562 FILE *fd;
17563 ufunc_T **sorttab;
17564 int st_len;
17565 char *title;
17566 int prefer_self; /* when equal print only self time */
17567{
17568 int i;
17569 ufunc_T *fp;
17570
17571 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
17572 fprintf(fd, "count total (s) self (s) function\n");
17573 for (i = 0; i < 20 && i < st_len; ++i)
17574 {
17575 fp = sorttab[i];
17576 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
17577 prefer_self);
17578 if (fp->uf_name[0] == K_SPECIAL)
17579 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
17580 else
17581 fprintf(fd, " %s()\n", fp->uf_name);
17582 }
17583 fprintf(fd, "\n");
17584}
17585
17586/*
17587 * Print the count and times for one function or function line.
17588 */
17589 static void
17590prof_func_line(fd, count, total, self, prefer_self)
17591 FILE *fd;
17592 int count;
17593 proftime_T *total;
17594 proftime_T *self;
17595 int prefer_self; /* when equal print only self time */
17596{
17597 if (count > 0)
17598 {
17599 fprintf(fd, "%5d ", count);
17600 if (prefer_self && profile_equal(total, self))
17601 fprintf(fd, " ");
17602 else
17603 fprintf(fd, "%s ", profile_msg(total));
17604 if (!prefer_self && profile_equal(total, self))
17605 fprintf(fd, " ");
17606 else
17607 fprintf(fd, "%s ", profile_msg(self));
17608 }
17609 else
17610 fprintf(fd, " ");
17611}
17612
17613/*
17614 * Compare function for total time sorting.
17615 */
17616 static int
17617#ifdef __BORLANDC__
17618_RTLENTRYF
17619#endif
17620prof_total_cmp(s1, s2)
17621 const void *s1;
17622 const void *s2;
17623{
17624 ufunc_T *p1, *p2;
17625
17626 p1 = *(ufunc_T **)s1;
17627 p2 = *(ufunc_T **)s2;
17628 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
17629}
17630
17631/*
17632 * Compare function for self time sorting.
17633 */
17634 static int
17635#ifdef __BORLANDC__
17636_RTLENTRYF
17637#endif
17638prof_self_cmp(s1, s2)
17639 const void *s1;
17640 const void *s2;
17641{
17642 ufunc_T *p1, *p2;
17643
17644 p1 = *(ufunc_T **)s1;
17645 p2 = *(ufunc_T **)s2;
17646 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
17647}
17648
Bram Moolenaar05159a02005-02-26 23:04:13 +000017649#endif
17650
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017651/* The names of packages that once were loaded is remembered. */
17652static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
17653
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017654/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017655 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017656 * Return TRUE if a package was loaded.
17657 */
17658 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017659script_autoload(name, reload)
17660 char_u *name;
17661 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017662{
17663 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017664 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017665 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017666 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017667
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017668 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017669 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017670 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017671 return FALSE;
17672
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017673 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017674
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017675 /* Find the name in the list of previously loaded package names. Skip
17676 * "autoload/", it's always the same. */
17677 for (i = 0; i < ga_loaded.ga_len; ++i)
17678 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
17679 break;
17680 if (!reload && i < ga_loaded.ga_len)
17681 ret = FALSE; /* was loaded already */
17682 else
17683 {
17684 /* Remember the name if it wasn't loaded already. */
17685 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
17686 {
17687 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
17688 tofree = NULL;
17689 }
17690
17691 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
17692 if (cmd_runtime(scriptname, FALSE) == OK)
17693 ret = TRUE;
17694 }
17695
17696 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017697 return ret;
17698}
17699
17700/*
17701 * Return the autoload script name for a function or variable name.
17702 * Returns NULL when out of memory.
17703 */
17704 static char_u *
17705autoload_name(name)
17706 char_u *name;
17707{
17708 char_u *p;
17709 char_u *scriptname;
17710
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017711 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017712 scriptname = alloc((unsigned)(STRLEN(name) + 14));
17713 if (scriptname == NULL)
17714 return FALSE;
17715 STRCPY(scriptname, "autoload/");
17716 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017717 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017718 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017719 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017720 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017721 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017722}
17723
Bram Moolenaar071d4272004-06-13 20:20:40 +000017724#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
17725
17726/*
17727 * Function given to ExpandGeneric() to obtain the list of user defined
17728 * function names.
17729 */
17730 char_u *
17731get_user_func_name(xp, idx)
17732 expand_T *xp;
17733 int idx;
17734{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017735 static long_u done;
17736 static hashitem_T *hi;
17737 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017738
17739 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017740 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017741 done = 0;
17742 hi = func_hashtab.ht_array;
17743 }
17744 if (done < func_hashtab.ht_used)
17745 {
17746 if (done++ > 0)
17747 ++hi;
17748 while (HASHITEM_EMPTY(hi))
17749 ++hi;
17750 fp = HI2UF(hi);
17751
17752 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
17753 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017754
17755 cat_func_name(IObuff, fp);
17756 if (xp->xp_context != EXPAND_USER_FUNC)
17757 {
17758 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017759 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017760 STRCAT(IObuff, ")");
17761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017762 return IObuff;
17763 }
17764 return NULL;
17765}
17766
17767#endif /* FEAT_CMDL_COMPL */
17768
17769/*
17770 * Copy the function name of "fp" to buffer "buf".
17771 * "buf" must be able to hold the function name plus three bytes.
17772 * Takes care of script-local function names.
17773 */
17774 static void
17775cat_func_name(buf, fp)
17776 char_u *buf;
17777 ufunc_T *fp;
17778{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017779 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017780 {
17781 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017782 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017783 }
17784 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017785 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017786}
17787
17788/*
17789 * ":delfunction {name}"
17790 */
17791 void
17792ex_delfunction(eap)
17793 exarg_T *eap;
17794{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017795 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017796 char_u *p;
17797 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017798 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017799
17800 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017801 name = trans_function_name(&p, eap->skip, 0, &fudi);
17802 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017803 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017804 {
17805 if (fudi.fd_dict != NULL && !eap->skip)
17806 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017807 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017809 if (!ends_excmd(*skipwhite(p)))
17810 {
17811 vim_free(name);
17812 EMSG(_(e_trailing));
17813 return;
17814 }
17815 eap->nextcmd = check_nextcmd(p);
17816 if (eap->nextcmd != NULL)
17817 *p = NUL;
17818
17819 if (!eap->skip)
17820 fp = find_func(name);
17821 vim_free(name);
17822
17823 if (!eap->skip)
17824 {
17825 if (fp == NULL)
17826 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000017827 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017828 return;
17829 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017830 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017831 {
17832 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
17833 return;
17834 }
17835
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017836 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017837 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017838 /* Delete the dict item that refers to the function, it will
17839 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017840 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017841 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017842 else
17843 func_free(fp);
17844 }
17845}
17846
17847/*
17848 * Free a function and remove it from the list of functions.
17849 */
17850 static void
17851func_free(fp)
17852 ufunc_T *fp;
17853{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017854 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017855
17856 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017857 ga_clear_strings(&(fp->uf_args));
17858 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000017859#ifdef FEAT_PROFILE
17860 vim_free(fp->uf_tml_count);
17861 vim_free(fp->uf_tml_total);
17862 vim_free(fp->uf_tml_self);
17863#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017864
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017865 /* remove the function from the function hashtable */
17866 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
17867 if (HASHITEM_EMPTY(hi))
17868 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017869 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017870 hash_remove(&func_hashtab, hi);
17871
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017872 vim_free(fp);
17873}
17874
17875/*
17876 * Unreference a Function: decrement the reference count and free it when it
17877 * becomes zero. Only for numbered functions.
17878 */
17879 static void
17880func_unref(name)
17881 char_u *name;
17882{
17883 ufunc_T *fp;
17884
17885 if (name != NULL && isdigit(*name))
17886 {
17887 fp = find_func(name);
17888 if (fp == NULL)
17889 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017890 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017891 {
17892 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017893 * when "uf_calls" becomes zero. */
17894 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017895 func_free(fp);
17896 }
17897 }
17898}
17899
17900/*
17901 * Count a reference to a Function.
17902 */
17903 static void
17904func_ref(name)
17905 char_u *name;
17906{
17907 ufunc_T *fp;
17908
17909 if (name != NULL && isdigit(*name))
17910 {
17911 fp = find_func(name);
17912 if (fp == NULL)
17913 EMSG2(_(e_intern2), "func_ref()");
17914 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017915 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017916 }
17917}
17918
17919/*
17920 * Call a user function.
17921 */
17922 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000017923call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017924 ufunc_T *fp; /* pointer to function */
17925 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000017926 typval_T *argvars; /* arguments */
17927 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017928 linenr_T firstline; /* first line of range */
17929 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000017930 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017931{
Bram Moolenaar33570922005-01-25 22:26:29 +000017932 char_u *save_sourcing_name;
17933 linenr_T save_sourcing_lnum;
17934 scid_T save_current_SID;
17935 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000017936 int save_did_emsg;
17937 static int depth = 0;
17938 dictitem_T *v;
17939 int fixvar_idx = 0; /* index in fixvar[] */
17940 int i;
17941 int ai;
17942 char_u numbuf[NUMBUFLEN];
17943 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017944#ifdef FEAT_PROFILE
17945 proftime_T wait_start;
17946#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017947
17948 /* If depth of calling is getting too high, don't execute the function */
17949 if (depth >= p_mfd)
17950 {
17951 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017952 rettv->v_type = VAR_NUMBER;
17953 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017954 return;
17955 }
17956 ++depth;
17957
17958 line_breakcheck(); /* check for CTRL-C hit */
17959
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017960 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000017961 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017962 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017963 fc.rettv = rettv;
17964 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017965 fc.linenr = 0;
17966 fc.returned = FALSE;
17967 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017968 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017969 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017970 fc.dbg_tick = debug_tick;
17971
Bram Moolenaar33570922005-01-25 22:26:29 +000017972 /*
17973 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
17974 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
17975 * each argument variable and saves a lot of time.
17976 */
17977 /*
17978 * Init l: variables.
17979 */
17980 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000017981 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017982 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017983 /* Set l:self to "selfdict". */
17984 v = &fc.fixvar[fixvar_idx++].var;
17985 STRCPY(v->di_key, "self");
17986 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
17987 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
17988 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017989 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000017990 v->di_tv.vval.v_dict = selfdict;
17991 ++selfdict->dv_refcount;
17992 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000017993
Bram Moolenaar33570922005-01-25 22:26:29 +000017994 /*
17995 * Init a: variables.
17996 * Set a:0 to "argcount".
17997 * Set a:000 to a list with room for the "..." arguments.
17998 */
17999 init_var_dict(&fc.l_avars, &fc.l_avars_var);
18000 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018001 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000018002 v = &fc.fixvar[fixvar_idx++].var;
18003 STRCPY(v->di_key, "000");
18004 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18005 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
18006 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018007 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018008 v->di_tv.vval.v_list = &fc.l_varlist;
18009 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
18010 fc.l_varlist.lv_refcount = 99999;
18011
18012 /*
18013 * Set a:firstline to "firstline" and a:lastline to "lastline".
18014 * Set a:name to named arguments.
18015 * Set a:N to the "..." arguments.
18016 */
18017 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
18018 (varnumber_T)firstline);
18019 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
18020 (varnumber_T)lastline);
18021 for (i = 0; i < argcount; ++i)
18022 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018023 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000018024 if (ai < 0)
18025 /* named argument a:name */
18026 name = FUNCARG(fp, i);
18027 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000018028 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018029 /* "..." argument a:1, a:2, etc. */
18030 sprintf((char *)numbuf, "%d", ai + 1);
18031 name = numbuf;
18032 }
18033 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
18034 {
18035 v = &fc.fixvar[fixvar_idx++].var;
18036 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18037 }
18038 else
18039 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018040 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
18041 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000018042 if (v == NULL)
18043 break;
18044 v->di_flags = DI_FLAGS_RO;
18045 }
18046 STRCPY(v->di_key, name);
18047 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
18048
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018049 /* Note: the values are copied directly to avoid alloc/free.
18050 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000018051 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018052 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018053
18054 if (ai >= 0 && ai < MAX_FUNC_ARGS)
18055 {
18056 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
18057 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018058 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018059 }
18060 }
18061
Bram Moolenaar071d4272004-06-13 20:20:40 +000018062 /* Don't redraw while executing the function. */
18063 ++RedrawingDisabled;
18064 save_sourcing_name = sourcing_name;
18065 save_sourcing_lnum = sourcing_lnum;
18066 sourcing_lnum = 1;
18067 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018068 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018069 if (sourcing_name != NULL)
18070 {
18071 if (save_sourcing_name != NULL
18072 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
18073 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
18074 else
18075 STRCPY(sourcing_name, "function ");
18076 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
18077
18078 if (p_verbose >= 12)
18079 {
18080 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018081 verbose_enter_scroll();
18082
Bram Moolenaar555b2802005-05-19 21:08:39 +000018083 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018084 if (p_verbose >= 14)
18085 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018086 char_u buf[MSG_BUF_LEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000018087 char_u numbuf[NUMBUFLEN];
18088 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018089
18090 msg_puts((char_u *)"(");
18091 for (i = 0; i < argcount; ++i)
18092 {
18093 if (i > 0)
18094 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018095 if (argvars[i].v_type == VAR_NUMBER)
18096 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018097 else
18098 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000018099 trunc_string(tv2string(&argvars[i], &tofree, numbuf),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018100 buf, MSG_BUF_CLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018101 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000018102 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018103 }
18104 }
18105 msg_puts((char_u *)")");
18106 }
18107 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018108
18109 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018110 --no_wait_return;
18111 }
18112 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000018113#ifdef FEAT_PROFILE
18114 if (do_profiling)
18115 {
18116 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
18117 func_do_profile(fp);
18118 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018119 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000018120 {
18121 ++fp->uf_tm_count;
18122 profile_start(&fp->uf_tm_start);
18123 profile_zero(&fp->uf_tm_children);
18124 }
18125 script_prof_save(&wait_start);
18126 }
18127#endif
18128
Bram Moolenaar071d4272004-06-13 20:20:40 +000018129 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018130 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018131 save_did_emsg = did_emsg;
18132 did_emsg = FALSE;
18133
18134 /* call do_cmdline() to execute the lines */
18135 do_cmdline(NULL, get_func_line, (void *)&fc,
18136 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
18137
18138 --RedrawingDisabled;
18139
18140 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018141 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018142 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018143 clear_tv(rettv);
18144 rettv->v_type = VAR_NUMBER;
18145 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018146 }
18147
Bram Moolenaar05159a02005-02-26 23:04:13 +000018148#ifdef FEAT_PROFILE
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018149 if (fp->uf_profiling || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000018150 {
18151 profile_end(&fp->uf_tm_start);
18152 profile_sub_wait(&wait_start, &fp->uf_tm_start);
18153 profile_add(&fp->uf_tm_total, &fp->uf_tm_start);
18154 profile_add(&fp->uf_tm_self, &fp->uf_tm_start);
18155 profile_sub(&fp->uf_tm_self, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018156 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018157 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018158 profile_add(&fc.caller->func->uf_tm_children, &fp->uf_tm_start);
18159 profile_add(&fc.caller->func->uf_tml_children, &fp->uf_tm_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018160 }
18161 }
18162#endif
18163
Bram Moolenaar071d4272004-06-13 20:20:40 +000018164 /* when being verbose, mention the return value */
18165 if (p_verbose >= 12)
18166 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018167 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018168 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018169
Bram Moolenaar071d4272004-06-13 20:20:40 +000018170 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000018171 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018172 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000018173 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
18174 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000018175 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000018176 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000018177 char_u buf[MSG_BUF_LEN];
18178 char_u numbuf[NUMBUFLEN];
18179 char_u *tofree;
18180
Bram Moolenaar555b2802005-05-19 21:08:39 +000018181 /* The value may be very long. Skip the middle part, so that we
18182 * have some idea how it starts and ends. smsg() would always
18183 * truncate it at the end. */
Bram Moolenaar758711c2005-02-02 23:11:38 +000018184 trunc_string(tv2string(fc.rettv, &tofree, numbuf),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018185 buf, MSG_BUF_CLEN);
Bram Moolenaar555b2802005-05-19 21:08:39 +000018186 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000018187 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018188 }
18189 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018190
18191 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018192 --no_wait_return;
18193 }
18194
18195 vim_free(sourcing_name);
18196 sourcing_name = save_sourcing_name;
18197 sourcing_lnum = save_sourcing_lnum;
18198 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018199#ifdef FEAT_PROFILE
18200 if (do_profiling)
18201 script_prof_restore(&wait_start);
18202#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018203
18204 if (p_verbose >= 12 && sourcing_name != NULL)
18205 {
18206 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018207 verbose_enter_scroll();
18208
Bram Moolenaar555b2802005-05-19 21:08:39 +000018209 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018210 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018211
18212 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018213 --no_wait_return;
18214 }
18215
18216 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018217 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018218
Bram Moolenaar33570922005-01-25 22:26:29 +000018219 /* The a: variables typevals were not alloced, only free the allocated
18220 * variables. */
18221 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
18222
18223 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018224 --depth;
18225}
18226
18227/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018228 * Add a number variable "name" to dict "dp" with value "nr".
18229 */
18230 static void
18231add_nr_var(dp, v, name, nr)
18232 dict_T *dp;
18233 dictitem_T *v;
18234 char *name;
18235 varnumber_T nr;
18236{
18237 STRCPY(v->di_key, name);
18238 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18239 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
18240 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018241 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018242 v->di_tv.vval.v_number = nr;
18243}
18244
18245/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018246 * ":return [expr]"
18247 */
18248 void
18249ex_return(eap)
18250 exarg_T *eap;
18251{
18252 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018253 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018254 int returning = FALSE;
18255
18256 if (current_funccal == NULL)
18257 {
18258 EMSG(_("E133: :return not inside a function"));
18259 return;
18260 }
18261
18262 if (eap->skip)
18263 ++emsg_skip;
18264
18265 eap->nextcmd = NULL;
18266 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018267 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018268 {
18269 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018270 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018271 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018272 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018273 }
18274 /* It's safer to return also on error. */
18275 else if (!eap->skip)
18276 {
18277 /*
18278 * Return unless the expression evaluation has been cancelled due to an
18279 * aborting error, an interrupt, or an exception.
18280 */
18281 if (!aborting())
18282 returning = do_return(eap, FALSE, TRUE, NULL);
18283 }
18284
18285 /* When skipping or the return gets pending, advance to the next command
18286 * in this line (!returning). Otherwise, ignore the rest of the line.
18287 * Following lines will be ignored by get_func_line(). */
18288 if (returning)
18289 eap->nextcmd = NULL;
18290 else if (eap->nextcmd == NULL) /* no argument */
18291 eap->nextcmd = check_nextcmd(arg);
18292
18293 if (eap->skip)
18294 --emsg_skip;
18295}
18296
18297/*
18298 * Return from a function. Possibly makes the return pending. Also called
18299 * for a pending return at the ":endtry" or after returning from an extra
18300 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000018301 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018302 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018303 * FALSE when the return gets pending.
18304 */
18305 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018306do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018307 exarg_T *eap;
18308 int reanimate;
18309 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018310 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018311{
18312 int idx;
18313 struct condstack *cstack = eap->cstack;
18314
18315 if (reanimate)
18316 /* Undo the return. */
18317 current_funccal->returned = FALSE;
18318
18319 /*
18320 * Cleanup (and inactivate) conditionals, but stop when a try conditional
18321 * not in its finally clause (which then is to be executed next) is found.
18322 * In this case, make the ":return" pending for execution at the ":endtry".
18323 * Otherwise, return normally.
18324 */
18325 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
18326 if (idx >= 0)
18327 {
18328 cstack->cs_pending[idx] = CSTP_RETURN;
18329
18330 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018331 /* A pending return again gets pending. "rettv" points to an
18332 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000018333 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018334 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018335 else
18336 {
18337 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018338 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018339 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018340 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018341
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018342 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018343 {
18344 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018345 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018346 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018347 else
18348 EMSG(_(e_outofmem));
18349 }
18350 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018351 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018352
18353 if (reanimate)
18354 {
18355 /* The pending return value could be overwritten by a ":return"
18356 * without argument in a finally clause; reset the default
18357 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018358 current_funccal->rettv->v_type = VAR_NUMBER;
18359 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018360 }
18361 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018362 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018363 }
18364 else
18365 {
18366 current_funccal->returned = TRUE;
18367
18368 /* If the return is carried out now, store the return value. For
18369 * a return immediately after reanimation, the value is already
18370 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018371 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018372 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018373 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000018374 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018375 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018376 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018377 }
18378 }
18379
18380 return idx < 0;
18381}
18382
18383/*
18384 * Free the variable with a pending return value.
18385 */
18386 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018387discard_pending_return(rettv)
18388 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018389{
Bram Moolenaar33570922005-01-25 22:26:29 +000018390 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018391}
18392
18393/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018394 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000018395 * is an allocated string. Used by report_pending() for verbose messages.
18396 */
18397 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018398get_return_cmd(rettv)
18399 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018400{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018401 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018402 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018403 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018404
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018405 if (rettv != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018406 s = echo_string((typval_T *)rettv, &tofree, numbuf);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018407 if (s == NULL)
18408 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018409
18410 STRCPY(IObuff, ":return ");
18411 STRNCPY(IObuff + 8, s, IOSIZE - 8);
18412 if (STRLEN(s) + 8 >= IOSIZE)
18413 STRCPY(IObuff + IOSIZE - 4, "...");
18414 vim_free(tofree);
18415 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018416}
18417
18418/*
18419 * Get next function line.
18420 * Called by do_cmdline() to get the next line.
18421 * Returns allocated string, or NULL for end of function.
18422 */
18423/* ARGSUSED */
18424 char_u *
18425get_func_line(c, cookie, indent)
18426 int c; /* not used */
18427 void *cookie;
18428 int indent; /* not used */
18429{
Bram Moolenaar33570922005-01-25 22:26:29 +000018430 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018431 ufunc_T *fp = fcp->func;
18432 char_u *retval;
18433 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018434
18435 /* If breakpoints have been added/deleted need to check for it. */
18436 if (fcp->dbg_tick != debug_tick)
18437 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000018438 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018439 sourcing_lnum);
18440 fcp->dbg_tick = debug_tick;
18441 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000018442#ifdef FEAT_PROFILE
18443 if (do_profiling)
18444 func_line_end(cookie);
18445#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018446
Bram Moolenaar05159a02005-02-26 23:04:13 +000018447 gap = &fp->uf_lines;
18448 if ((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000018449 retval = NULL;
18450 else if (fcp->returned || fcp->linenr >= gap->ga_len)
18451 retval = NULL;
18452 else
18453 {
18454 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
18455 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018456#ifdef FEAT_PROFILE
18457 if (do_profiling)
18458 func_line_start(cookie);
18459#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018460 }
18461
18462 /* Did we encounter a breakpoint? */
18463 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
18464 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000018465 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018466 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018467 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018468 sourcing_lnum);
18469 fcp->dbg_tick = debug_tick;
18470 }
18471
18472 return retval;
18473}
18474
Bram Moolenaar05159a02005-02-26 23:04:13 +000018475#if defined(FEAT_PROFILE) || defined(PROTO)
18476/*
18477 * Called when starting to read a function line.
18478 * "sourcing_lnum" must be correct!
18479 * When skipping lines it may not actually be executed, but we won't find out
18480 * until later and we need to store the time now.
18481 */
18482 void
18483func_line_start(cookie)
18484 void *cookie;
18485{
18486 funccall_T *fcp = (funccall_T *)cookie;
18487 ufunc_T *fp = fcp->func;
18488
18489 if (fp->uf_profiling && sourcing_lnum >= 1
18490 && sourcing_lnum <= fp->uf_lines.ga_len)
18491 {
18492 fp->uf_tml_idx = sourcing_lnum - 1;
18493 fp->uf_tml_execed = FALSE;
18494 profile_start(&fp->uf_tml_start);
18495 profile_zero(&fp->uf_tml_children);
18496 profile_get_wait(&fp->uf_tml_wait);
18497 }
18498}
18499
18500/*
18501 * Called when actually executing a function line.
18502 */
18503 void
18504func_line_exec(cookie)
18505 void *cookie;
18506{
18507 funccall_T *fcp = (funccall_T *)cookie;
18508 ufunc_T *fp = fcp->func;
18509
18510 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
18511 fp->uf_tml_execed = TRUE;
18512}
18513
18514/*
18515 * Called when done with a function line.
18516 */
18517 void
18518func_line_end(cookie)
18519 void *cookie;
18520{
18521 funccall_T *fcp = (funccall_T *)cookie;
18522 ufunc_T *fp = fcp->func;
18523
18524 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
18525 {
18526 if (fp->uf_tml_execed)
18527 {
18528 ++fp->uf_tml_count[fp->uf_tml_idx];
18529 profile_end(&fp->uf_tml_start);
18530 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
18531 profile_add(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start);
18532 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
18533 profile_sub(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_children);
18534 }
18535 fp->uf_tml_idx = -1;
18536 }
18537}
18538#endif
18539
Bram Moolenaar071d4272004-06-13 20:20:40 +000018540/*
18541 * Return TRUE if the currently active function should be ended, because a
18542 * return was encountered or an error occured. Used inside a ":while".
18543 */
18544 int
18545func_has_ended(cookie)
18546 void *cookie;
18547{
Bram Moolenaar33570922005-01-25 22:26:29 +000018548 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018549
18550 /* Ignore the "abort" flag if the abortion behavior has been changed due to
18551 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018552 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000018553 || fcp->returned);
18554}
18555
18556/*
18557 * return TRUE if cookie indicates a function which "abort"s on errors.
18558 */
18559 int
18560func_has_abort(cookie)
18561 void *cookie;
18562{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018563 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018564}
18565
18566#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
18567typedef enum
18568{
18569 VAR_FLAVOUR_DEFAULT,
18570 VAR_FLAVOUR_SESSION,
18571 VAR_FLAVOUR_VIMINFO
18572} var_flavour_T;
18573
18574static var_flavour_T var_flavour __ARGS((char_u *varname));
18575
18576 static var_flavour_T
18577var_flavour(varname)
18578 char_u *varname;
18579{
18580 char_u *p = varname;
18581
18582 if (ASCII_ISUPPER(*p))
18583 {
18584 while (*(++p))
18585 if (ASCII_ISLOWER(*p))
18586 return VAR_FLAVOUR_SESSION;
18587 return VAR_FLAVOUR_VIMINFO;
18588 }
18589 else
18590 return VAR_FLAVOUR_DEFAULT;
18591}
18592#endif
18593
18594#if defined(FEAT_VIMINFO) || defined(PROTO)
18595/*
18596 * Restore global vars that start with a capital from the viminfo file
18597 */
18598 int
18599read_viminfo_varlist(virp, writing)
18600 vir_T *virp;
18601 int writing;
18602{
18603 char_u *tab;
18604 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000018605 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018606
18607 if (!writing && (find_viminfo_parameter('!') != NULL))
18608 {
18609 tab = vim_strchr(virp->vir_line + 1, '\t');
18610 if (tab != NULL)
18611 {
18612 *tab++ = '\0'; /* isolate the variable name */
18613 if (*tab == 'S') /* string var */
18614 is_string = TRUE;
18615
18616 tab = vim_strchr(tab, '\t');
18617 if (tab != NULL)
18618 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018619 if (is_string)
18620 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000018621 tv.v_type = VAR_STRING;
18622 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018623 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018624 }
18625 else
18626 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000018627 tv.v_type = VAR_NUMBER;
18628 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018629 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000018630 set_var(virp->vir_line + 1, &tv, FALSE);
18631 if (is_string)
18632 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018633 }
18634 }
18635 }
18636
18637 return viminfo_readline(virp);
18638}
18639
18640/*
18641 * Write global vars that start with a capital to the viminfo file
18642 */
18643 void
18644write_viminfo_varlist(fp)
18645 FILE *fp;
18646{
Bram Moolenaar33570922005-01-25 22:26:29 +000018647 hashitem_T *hi;
18648 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000018649 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018650 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018651 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018652 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018653 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018654
18655 if (find_viminfo_parameter('!') == NULL)
18656 return;
18657
18658 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000018659
Bram Moolenaar33570922005-01-25 22:26:29 +000018660 todo = globvarht.ht_used;
18661 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018662 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018663 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018664 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018665 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000018666 this_var = HI2DI(hi);
18667 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018668 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018669 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000018670 {
18671 case VAR_STRING: s = "STR"; break;
18672 case VAR_NUMBER: s = "NUM"; break;
18673 default: continue;
18674 }
Bram Moolenaar33570922005-01-25 22:26:29 +000018675 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018676 p = echo_string(&this_var->di_tv, &tofree, numbuf);
18677 if (p != NULL)
18678 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000018679 vim_free(tofree);
18680 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018681 }
18682 }
18683}
18684#endif
18685
18686#if defined(FEAT_SESSION) || defined(PROTO)
18687 int
18688store_session_globals(fd)
18689 FILE *fd;
18690{
Bram Moolenaar33570922005-01-25 22:26:29 +000018691 hashitem_T *hi;
18692 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000018693 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018694 char_u *p, *t;
18695
Bram Moolenaar33570922005-01-25 22:26:29 +000018696 todo = globvarht.ht_used;
18697 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018698 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018699 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018700 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018701 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000018702 this_var = HI2DI(hi);
18703 if ((this_var->di_tv.v_type == VAR_NUMBER
18704 || this_var->di_tv.v_type == VAR_STRING)
18705 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000018706 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018707 /* Escape special characters with a backslash. Turn a LF and
18708 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000018709 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000018710 (char_u *)"\\\"\n\r");
18711 if (p == NULL) /* out of memory */
18712 break;
18713 for (t = p; *t != NUL; ++t)
18714 if (*t == '\n')
18715 *t = 'n';
18716 else if (*t == '\r')
18717 *t = 'r';
18718 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000018719 this_var->di_key,
18720 (this_var->di_tv.v_type == VAR_STRING) ? '"'
18721 : ' ',
18722 p,
18723 (this_var->di_tv.v_type == VAR_STRING) ? '"'
18724 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000018725 || put_eol(fd) == FAIL)
18726 {
18727 vim_free(p);
18728 return FAIL;
18729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018730 vim_free(p);
18731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018732 }
18733 }
18734 return OK;
18735}
18736#endif
18737
Bram Moolenaar661b1822005-07-28 22:36:45 +000018738/*
18739 * Display script name where an item was last set.
18740 * Should only be invoked when 'verbose' is non-zero.
18741 */
18742 void
18743last_set_msg(scriptID)
18744 scid_T scriptID;
18745{
18746 if (scriptID != 0)
18747 {
18748 verbose_enter();
18749 MSG_PUTS(_("\n\tLast set from "));
18750 MSG_PUTS(get_scriptname(scriptID));
18751 verbose_leave();
18752 }
18753}
18754
Bram Moolenaar071d4272004-06-13 20:20:40 +000018755#endif /* FEAT_EVAL */
18756
18757#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
18758
18759
18760#ifdef WIN3264
18761/*
18762 * Functions for ":8" filename modifier: get 8.3 version of a filename.
18763 */
18764static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
18765static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
18766static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
18767
18768/*
18769 * Get the short pathname of a file.
18770 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
18771 */
18772 static int
18773get_short_pathname(fnamep, bufp, fnamelen)
18774 char_u **fnamep;
18775 char_u **bufp;
18776 int *fnamelen;
18777{
18778 int l,len;
18779 char_u *newbuf;
18780
18781 len = *fnamelen;
18782
18783 l = GetShortPathName(*fnamep, *fnamep, len);
18784 if (l > len - 1)
18785 {
18786 /* If that doesn't work (not enough space), then save the string
18787 * and try again with a new buffer big enough
18788 */
18789 newbuf = vim_strnsave(*fnamep, l);
18790 if (newbuf == NULL)
18791 return 0;
18792
18793 vim_free(*bufp);
18794 *fnamep = *bufp = newbuf;
18795
18796 l = GetShortPathName(*fnamep,*fnamep,l+1);
18797
18798 /* Really should always succeed, as the buffer is big enough */
18799 }
18800
18801 *fnamelen = l;
18802 return 1;
18803}
18804
18805/*
18806 * Create a short path name. Returns the length of the buffer it needs.
18807 * Doesn't copy over the end of the buffer passed in.
18808 */
18809 static int
18810shortpath_for_invalid_fname(fname, bufp, fnamelen)
18811 char_u **fname;
18812 char_u **bufp;
18813 int *fnamelen;
18814{
18815 char_u *s, *p, *pbuf2, *pbuf3;
18816 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000018817 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018818
18819 /* Make a copy */
18820 len2 = *fnamelen;
18821 pbuf2 = vim_strnsave(*fname, len2);
18822 pbuf3 = NULL;
18823
18824 s = pbuf2 + len2 - 1; /* Find the end */
18825 slen = 1;
18826 plen = len2;
18827
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018828 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018829 {
18830 --s;
18831 ++slen;
18832 --plen;
18833 }
18834
18835 do
18836 {
18837 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018838 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018839 {
18840 --s;
18841 ++slen;
18842 --plen;
18843 }
18844 if (s <= pbuf2)
18845 break;
18846
18847 /* Remeber the character that is about to be blatted */
18848 ch = *s;
18849 *s = 0; /* get_short_pathname requires a null-terminated string */
18850
18851 /* Try it in situ */
18852 p = pbuf2;
18853 if (!get_short_pathname(&p, &pbuf3, &plen))
18854 {
18855 vim_free(pbuf2);
18856 return -1;
18857 }
18858 *s = ch; /* Preserve the string */
18859 } while (plen == 0);
18860
18861 if (plen > 0)
18862 {
18863 /* Remeber the length of the new string. */
18864 *fnamelen = len = plen + slen;
18865 vim_free(*bufp);
18866 if (len > len2)
18867 {
18868 /* If there's not enough space in the currently allocated string,
18869 * then copy it to a buffer big enough.
18870 */
18871 *fname= *bufp = vim_strnsave(p, len);
18872 if (*fname == NULL)
18873 return -1;
18874 }
18875 else
18876 {
18877 /* Transfer pbuf2 to being the main buffer (it's big enough) */
18878 *fname = *bufp = pbuf2;
18879 if (p != pbuf2)
18880 strncpy(*fname, p, plen);
18881 pbuf2 = NULL;
18882 }
18883 /* Concat the next bit */
18884 strncpy(*fname + plen, s, slen);
18885 (*fname)[len] = '\0';
18886 }
18887 vim_free(pbuf3);
18888 vim_free(pbuf2);
18889 return 0;
18890}
18891
18892/*
18893 * Get a pathname for a partial path.
18894 */
18895 static int
18896shortpath_for_partial(fnamep, bufp, fnamelen)
18897 char_u **fnamep;
18898 char_u **bufp;
18899 int *fnamelen;
18900{
18901 int sepcount, len, tflen;
18902 char_u *p;
18903 char_u *pbuf, *tfname;
18904 int hasTilde;
18905
18906 /* Count up the path seperators from the RHS.. so we know which part
18907 * of the path to return.
18908 */
18909 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018910 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018911 if (vim_ispathsep(*p))
18912 ++sepcount;
18913
18914 /* Need full path first (use expand_env() to remove a "~/") */
18915 hasTilde = (**fnamep == '~');
18916 if (hasTilde)
18917 pbuf = tfname = expand_env_save(*fnamep);
18918 else
18919 pbuf = tfname = FullName_save(*fnamep, FALSE);
18920
18921 len = tflen = STRLEN(tfname);
18922
18923 if (!get_short_pathname(&tfname, &pbuf, &len))
18924 return -1;
18925
18926 if (len == 0)
18927 {
18928 /* Don't have a valid filename, so shorten the rest of the
18929 * path if we can. This CAN give us invalid 8.3 filenames, but
18930 * there's not a lot of point in guessing what it might be.
18931 */
18932 len = tflen;
18933 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
18934 return -1;
18935 }
18936
18937 /* Count the paths backward to find the beginning of the desired string. */
18938 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018939 {
18940#ifdef FEAT_MBYTE
18941 if (has_mbyte)
18942 p -= mb_head_off(tfname, p);
18943#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018944 if (vim_ispathsep(*p))
18945 {
18946 if (sepcount == 0 || (hasTilde && sepcount == 1))
18947 break;
18948 else
18949 sepcount --;
18950 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018952 if (hasTilde)
18953 {
18954 --p;
18955 if (p >= tfname)
18956 *p = '~';
18957 else
18958 return -1;
18959 }
18960 else
18961 ++p;
18962
18963 /* Copy in the string - p indexes into tfname - allocated at pbuf */
18964 vim_free(*bufp);
18965 *fnamelen = (int)STRLEN(p);
18966 *bufp = pbuf;
18967 *fnamep = p;
18968
18969 return 0;
18970}
18971#endif /* WIN3264 */
18972
18973/*
18974 * Adjust a filename, according to a string of modifiers.
18975 * *fnamep must be NUL terminated when called. When returning, the length is
18976 * determined by *fnamelen.
18977 * Returns valid flags.
18978 * When there is an error, *fnamep is set to NULL.
18979 */
18980 int
18981modify_fname(src, usedlen, fnamep, bufp, fnamelen)
18982 char_u *src; /* string with modifiers */
18983 int *usedlen; /* characters after src that are used */
18984 char_u **fnamep; /* file name so far */
18985 char_u **bufp; /* buffer for allocated file name or NULL */
18986 int *fnamelen; /* length of fnamep */
18987{
18988 int valid = 0;
18989 char_u *tail;
18990 char_u *s, *p, *pbuf;
18991 char_u dirname[MAXPATHL];
18992 int c;
18993 int has_fullname = 0;
18994#ifdef WIN3264
18995 int has_shortname = 0;
18996#endif
18997
18998repeat:
18999 /* ":p" - full path/file_name */
19000 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
19001 {
19002 has_fullname = 1;
19003
19004 valid |= VALID_PATH;
19005 *usedlen += 2;
19006
19007 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
19008 if ((*fnamep)[0] == '~'
19009#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
19010 && ((*fnamep)[1] == '/'
19011# ifdef BACKSLASH_IN_FILENAME
19012 || (*fnamep)[1] == '\\'
19013# endif
19014 || (*fnamep)[1] == NUL)
19015
19016#endif
19017 )
19018 {
19019 *fnamep = expand_env_save(*fnamep);
19020 vim_free(*bufp); /* free any allocated file name */
19021 *bufp = *fnamep;
19022 if (*fnamep == NULL)
19023 return -1;
19024 }
19025
19026 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019027 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019028 {
19029 if (vim_ispathsep(*p)
19030 && p[1] == '.'
19031 && (p[2] == NUL
19032 || vim_ispathsep(p[2])
19033 || (p[2] == '.'
19034 && (p[3] == NUL || vim_ispathsep(p[3])))))
19035 break;
19036 }
19037
19038 /* FullName_save() is slow, don't use it when not needed. */
19039 if (*p != NUL || !vim_isAbsName(*fnamep))
19040 {
19041 *fnamep = FullName_save(*fnamep, *p != NUL);
19042 vim_free(*bufp); /* free any allocated file name */
19043 *bufp = *fnamep;
19044 if (*fnamep == NULL)
19045 return -1;
19046 }
19047
19048 /* Append a path separator to a directory. */
19049 if (mch_isdir(*fnamep))
19050 {
19051 /* Make room for one or two extra characters. */
19052 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
19053 vim_free(*bufp); /* free any allocated file name */
19054 *bufp = *fnamep;
19055 if (*fnamep == NULL)
19056 return -1;
19057 add_pathsep(*fnamep);
19058 }
19059 }
19060
19061 /* ":." - path relative to the current directory */
19062 /* ":~" - path relative to the home directory */
19063 /* ":8" - shortname path - postponed till after */
19064 while (src[*usedlen] == ':'
19065 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
19066 {
19067 *usedlen += 2;
19068 if (c == '8')
19069 {
19070#ifdef WIN3264
19071 has_shortname = 1; /* Postpone this. */
19072#endif
19073 continue;
19074 }
19075 pbuf = NULL;
19076 /* Need full path first (use expand_env() to remove a "~/") */
19077 if (!has_fullname)
19078 {
19079 if (c == '.' && **fnamep == '~')
19080 p = pbuf = expand_env_save(*fnamep);
19081 else
19082 p = pbuf = FullName_save(*fnamep, FALSE);
19083 }
19084 else
19085 p = *fnamep;
19086
19087 has_fullname = 0;
19088
19089 if (p != NULL)
19090 {
19091 if (c == '.')
19092 {
19093 mch_dirname(dirname, MAXPATHL);
19094 s = shorten_fname(p, dirname);
19095 if (s != NULL)
19096 {
19097 *fnamep = s;
19098 if (pbuf != NULL)
19099 {
19100 vim_free(*bufp); /* free any allocated file name */
19101 *bufp = pbuf;
19102 pbuf = NULL;
19103 }
19104 }
19105 }
19106 else
19107 {
19108 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
19109 /* Only replace it when it starts with '~' */
19110 if (*dirname == '~')
19111 {
19112 s = vim_strsave(dirname);
19113 if (s != NULL)
19114 {
19115 *fnamep = s;
19116 vim_free(*bufp);
19117 *bufp = s;
19118 }
19119 }
19120 }
19121 vim_free(pbuf);
19122 }
19123 }
19124
19125 tail = gettail(*fnamep);
19126 *fnamelen = (int)STRLEN(*fnamep);
19127
19128 /* ":h" - head, remove "/file_name", can be repeated */
19129 /* Don't remove the first "/" or "c:\" */
19130 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
19131 {
19132 valid |= VALID_HEAD;
19133 *usedlen += 2;
19134 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019135 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019136 --tail;
19137 *fnamelen = (int)(tail - *fnamep);
19138#ifdef VMS
19139 if (*fnamelen > 0)
19140 *fnamelen += 1; /* the path separator is part of the path */
19141#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019142 while (tail > s && !after_pathsep(s, tail))
19143 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019144 }
19145
19146 /* ":8" - shortname */
19147 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
19148 {
19149 *usedlen += 2;
19150#ifdef WIN3264
19151 has_shortname = 1;
19152#endif
19153 }
19154
19155#ifdef WIN3264
19156 /* Check shortname after we have done 'heads' and before we do 'tails'
19157 */
19158 if (has_shortname)
19159 {
19160 pbuf = NULL;
19161 /* Copy the string if it is shortened by :h */
19162 if (*fnamelen < (int)STRLEN(*fnamep))
19163 {
19164 p = vim_strnsave(*fnamep, *fnamelen);
19165 if (p == 0)
19166 return -1;
19167 vim_free(*bufp);
19168 *bufp = *fnamep = p;
19169 }
19170
19171 /* Split into two implementations - makes it easier. First is where
19172 * there isn't a full name already, second is where there is.
19173 */
19174 if (!has_fullname && !vim_isAbsName(*fnamep))
19175 {
19176 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
19177 return -1;
19178 }
19179 else
19180 {
19181 int l;
19182
19183 /* Simple case, already have the full-name
19184 * Nearly always shorter, so try first time. */
19185 l = *fnamelen;
19186 if (!get_short_pathname(fnamep, bufp, &l))
19187 return -1;
19188
19189 if (l == 0)
19190 {
19191 /* Couldn't find the filename.. search the paths.
19192 */
19193 l = *fnamelen;
19194 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
19195 return -1;
19196 }
19197 *fnamelen = l;
19198 }
19199 }
19200#endif /* WIN3264 */
19201
19202 /* ":t" - tail, just the basename */
19203 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
19204 {
19205 *usedlen += 2;
19206 *fnamelen -= (int)(tail - *fnamep);
19207 *fnamep = tail;
19208 }
19209
19210 /* ":e" - extension, can be repeated */
19211 /* ":r" - root, without extension, can be repeated */
19212 while (src[*usedlen] == ':'
19213 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
19214 {
19215 /* find a '.' in the tail:
19216 * - for second :e: before the current fname
19217 * - otherwise: The last '.'
19218 */
19219 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
19220 s = *fnamep - 2;
19221 else
19222 s = *fnamep + *fnamelen - 1;
19223 for ( ; s > tail; --s)
19224 if (s[0] == '.')
19225 break;
19226 if (src[*usedlen + 1] == 'e') /* :e */
19227 {
19228 if (s > tail)
19229 {
19230 *fnamelen += (int)(*fnamep - (s + 1));
19231 *fnamep = s + 1;
19232#ifdef VMS
19233 /* cut version from the extension */
19234 s = *fnamep + *fnamelen - 1;
19235 for ( ; s > *fnamep; --s)
19236 if (s[0] == ';')
19237 break;
19238 if (s > *fnamep)
19239 *fnamelen = s - *fnamep;
19240#endif
19241 }
19242 else if (*fnamep <= tail)
19243 *fnamelen = 0;
19244 }
19245 else /* :r */
19246 {
19247 if (s > tail) /* remove one extension */
19248 *fnamelen = (int)(s - *fnamep);
19249 }
19250 *usedlen += 2;
19251 }
19252
19253 /* ":s?pat?foo?" - substitute */
19254 /* ":gs?pat?foo?" - global substitute */
19255 if (src[*usedlen] == ':'
19256 && (src[*usedlen + 1] == 's'
19257 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
19258 {
19259 char_u *str;
19260 char_u *pat;
19261 char_u *sub;
19262 int sep;
19263 char_u *flags;
19264 int didit = FALSE;
19265
19266 flags = (char_u *)"";
19267 s = src + *usedlen + 2;
19268 if (src[*usedlen + 1] == 'g')
19269 {
19270 flags = (char_u *)"g";
19271 ++s;
19272 }
19273
19274 sep = *s++;
19275 if (sep)
19276 {
19277 /* find end of pattern */
19278 p = vim_strchr(s, sep);
19279 if (p != NULL)
19280 {
19281 pat = vim_strnsave(s, (int)(p - s));
19282 if (pat != NULL)
19283 {
19284 s = p + 1;
19285 /* find end of substitution */
19286 p = vim_strchr(s, sep);
19287 if (p != NULL)
19288 {
19289 sub = vim_strnsave(s, (int)(p - s));
19290 str = vim_strnsave(*fnamep, *fnamelen);
19291 if (sub != NULL && str != NULL)
19292 {
19293 *usedlen = (int)(p + 1 - src);
19294 s = do_string_sub(str, pat, sub, flags);
19295 if (s != NULL)
19296 {
19297 *fnamep = s;
19298 *fnamelen = (int)STRLEN(s);
19299 vim_free(*bufp);
19300 *bufp = s;
19301 didit = TRUE;
19302 }
19303 }
19304 vim_free(sub);
19305 vim_free(str);
19306 }
19307 vim_free(pat);
19308 }
19309 }
19310 /* after using ":s", repeat all the modifiers */
19311 if (didit)
19312 goto repeat;
19313 }
19314 }
19315
19316 return valid;
19317}
19318
19319/*
19320 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
19321 * "flags" can be "g" to do a global substitute.
19322 * Returns an allocated string, NULL for error.
19323 */
19324 char_u *
19325do_string_sub(str, pat, sub, flags)
19326 char_u *str;
19327 char_u *pat;
19328 char_u *sub;
19329 char_u *flags;
19330{
19331 int sublen;
19332 regmatch_T regmatch;
19333 int i;
19334 int do_all;
19335 char_u *tail;
19336 garray_T ga;
19337 char_u *ret;
19338 char_u *save_cpo;
19339
19340 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
19341 save_cpo = p_cpo;
19342 p_cpo = (char_u *)"";
19343
19344 ga_init2(&ga, 1, 200);
19345
19346 do_all = (flags[0] == 'g');
19347
19348 regmatch.rm_ic = p_ic;
19349 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19350 if (regmatch.regprog != NULL)
19351 {
19352 tail = str;
19353 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
19354 {
19355 /*
19356 * Get some space for a temporary buffer to do the substitution
19357 * into. It will contain:
19358 * - The text up to where the match is.
19359 * - The substituted text.
19360 * - The text after the match.
19361 */
19362 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
19363 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
19364 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
19365 {
19366 ga_clear(&ga);
19367 break;
19368 }
19369
19370 /* copy the text up to where the match is */
19371 i = (int)(regmatch.startp[0] - tail);
19372 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
19373 /* add the substituted text */
19374 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
19375 + ga.ga_len + i, TRUE, TRUE, FALSE);
19376 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019377 /* avoid getting stuck on a match with an empty string */
19378 if (tail == regmatch.endp[0])
19379 {
19380 if (*tail == NUL)
19381 break;
19382 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
19383 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019384 }
19385 else
19386 {
19387 tail = regmatch.endp[0];
19388 if (*tail == NUL)
19389 break;
19390 }
19391 if (!do_all)
19392 break;
19393 }
19394
19395 if (ga.ga_data != NULL)
19396 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
19397
19398 vim_free(regmatch.regprog);
19399 }
19400
19401 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
19402 ga_clear(&ga);
19403 p_cpo = save_cpo;
19404
19405 return ret;
19406}
19407
19408#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */