blob: 40ec9f3e319033fb152cd8230fce6cf6fe455fb1 [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));
565static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000566static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000567static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000584static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000585static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000589static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000590static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000592static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
593#ifdef HAVE_STRFTIME
594static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
595#endif
596static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000608static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000609static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
619static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
620static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000624static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000625
Bram Moolenaar33570922005-01-25 22:26:29 +0000626static pos_T *var2fpos __ARGS((typval_T *varp, int lnum));
627static int get_env_len __ARGS((char_u **arg));
628static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000629static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000630static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
631#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
632#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
633 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000634static 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 +0000635static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000636static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000637static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
638static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000639static typval_T *alloc_tv __ARGS((void));
640static typval_T *alloc_string_tv __ARGS((char_u *string));
641static void free_tv __ARGS((typval_T *varp));
642static void clear_tv __ARGS((typval_T *varp));
643static void init_tv __ARGS((typval_T *varp));
644static long get_tv_number __ARGS((typval_T *varp));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000645static long get_tv_number_chk __ARGS((typval_T *varp, int *denote));
Bram Moolenaar33570922005-01-25 22:26:29 +0000646static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
647static char_u *get_tv_string __ARGS((typval_T *varp));
648static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000649static char_u *get_tv_string_chk __ARGS((typval_T *varp));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000650static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000651static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000652static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000653static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
654static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
655static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
656static void list_one_var __ARGS((dictitem_T *v, char_u *prefix));
657static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
658static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
659static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000660static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000661static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000662static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000663static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
664static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
665static int eval_fname_script __ARGS((char_u *p));
666static int eval_fname_sid __ARGS((char_u *p));
667static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000668static ufunc_T *find_func __ARGS((char_u *name));
669static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000670static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000671#ifdef FEAT_PROFILE
672static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000673static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
674static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
675static int
676# ifdef __BORLANDC__
677 _RTLENTRYF
678# endif
679 prof_total_cmp __ARGS((const void *s1, const void *s2));
680static int
681# ifdef __BORLANDC__
682 _RTLENTRYF
683# endif
684 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000685#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000686static int script_autoload __ARGS((char_u *name));
687static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000688static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000689static void func_free __ARGS((ufunc_T *fp));
690static void func_unref __ARGS((char_u *name));
691static void func_ref __ARGS((char_u *name));
692static 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));
693static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
694
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000695/* Character used as separated in autoload function/variable names. */
696#define AUTOLOAD_CHAR '#'
697
Bram Moolenaar33570922005-01-25 22:26:29 +0000698/*
699 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000700 */
701 void
702eval_init()
703{
Bram Moolenaar33570922005-01-25 22:26:29 +0000704 int i;
705 struct vimvar *p;
706
707 init_var_dict(&globvardict, &globvars_var);
708 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000709 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000710 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000711
712 for (i = 0; i < VV_LEN; ++i)
713 {
714 p = &vimvars[i];
715 STRCPY(p->vv_di.di_key, p->vv_name);
716 if (p->vv_flags & VV_RO)
717 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
718 else if (p->vv_flags & VV_RO_SBX)
719 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
720 else
721 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000722
723 /* add to v: scope dict, unless the value is not always available */
724 if (p->vv_type != VAR_UNKNOWN)
725 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000726 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000727 /* add to compat scope dict */
728 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000729 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000730}
731
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000732#if defined(EXITFREE) || defined(PROTO)
733 void
734eval_clear()
735{
736 int i;
737 struct vimvar *p;
738
739 for (i = 0; i < VV_LEN; ++i)
740 {
741 p = &vimvars[i];
742 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000743 {
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000744 vim_free(p->vv_di.di_tv.vval.v_string);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000745 p->vv_di.di_tv.vval.v_string = NULL;
746 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000747 }
748 hash_clear(&vimvarht);
749 hash_clear(&compat_hashtab);
750
751 /* script-local variables */
752 for (i = 1; i <= ga_scripts.ga_len; ++i)
753 vars_clear(&SCRIPT_VARS(i));
754 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000755 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000756
757 /* global variables */
758 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000759
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000760 /* functions */
Bram Moolenaard9fba312005-06-26 22:34:35 +0000761 free_all_functions();
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000762 hash_clear(&func_hashtab);
763
764 /* unreferenced lists and dicts */
765 (void)garbage_collect();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000766}
767#endif
768
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000769/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 * Return the name of the executed function.
771 */
772 char_u *
773func_name(cookie)
774 void *cookie;
775{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000776 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777}
778
779/*
780 * Return the address holding the next breakpoint line for a funccall cookie.
781 */
782 linenr_T *
783func_breakpoint(cookie)
784 void *cookie;
785{
Bram Moolenaar33570922005-01-25 22:26:29 +0000786 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787}
788
789/*
790 * Return the address holding the debug tick for a funccall cookie.
791 */
792 int *
793func_dbg_tick(cookie)
794 void *cookie;
795{
Bram Moolenaar33570922005-01-25 22:26:29 +0000796 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797}
798
799/*
800 * Return the nesting level for a funccall cookie.
801 */
802 int
803func_level(cookie)
804 void *cookie;
805{
Bram Moolenaar33570922005-01-25 22:26:29 +0000806 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807}
808
809/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000810funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811
812/*
813 * Return TRUE when a function was ended by a ":return" command.
814 */
815 int
816current_func_returned()
817{
818 return current_funccal->returned;
819}
820
821
822/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 * Set an internal variable to a string value. Creates the variable if it does
824 * not already exist.
825 */
826 void
827set_internal_string_var(name, value)
828 char_u *name;
829 char_u *value;
830{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000831 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000832 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833
834 val = vim_strsave(value);
835 if (val != NULL)
836 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000837 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000838 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000840 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000841 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 }
843 }
844}
845
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000846static lval_T *redir_lval = NULL;
847static char_u *redir_endp = NULL;
848static char_u *redir_varname = NULL;
849
850/*
851 * Start recording command output to a variable
852 * Returns OK if successfully completed the setup. FAIL otherwise.
853 */
854 int
855var_redir_start(name, append)
856 char_u *name;
857 int append; /* append to an existing variable */
858{
859 int save_emsg;
860 int err;
861 typval_T tv;
862
863 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000864 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000865 {
866 EMSG(_(e_invarg));
867 return FAIL;
868 }
869
870 redir_varname = vim_strsave(name);
871 if (redir_varname == NULL)
872 return FAIL;
873
874 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
875 if (redir_lval == NULL)
876 {
877 var_redir_stop();
878 return FAIL;
879 }
880
881 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000882 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
883 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000884 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
885 {
886 if (redir_endp != NULL && *redir_endp != NUL)
887 /* Trailing characters are present after the variable name */
888 EMSG(_(e_trailing));
889 else
890 EMSG(_(e_invarg));
891 var_redir_stop();
892 return FAIL;
893 }
894
895 /* check if we can write to the variable: set it to or append an empty
896 * string */
897 save_emsg = did_emsg;
898 did_emsg = FALSE;
899 tv.v_type = VAR_STRING;
900 tv.vval.v_string = (char_u *)"";
901 if (append)
902 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
903 else
904 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
905 err = did_emsg;
906 did_emsg += save_emsg;
907 if (err)
908 {
909 var_redir_stop();
910 return FAIL;
911 }
912 if (redir_lval->ll_newkey != NULL)
913 {
914 /* Dictionary item was created, don't do it again. */
915 vim_free(redir_lval->ll_newkey);
916 redir_lval->ll_newkey = NULL;
917 }
918
919 return OK;
920}
921
922/*
923 * Append "value[len]" to the variable set by var_redir_start().
924 */
925 void
926var_redir_str(value, len)
927 char_u *value;
928 int len;
929{
930 char_u *val;
931 typval_T tv;
932 int save_emsg;
933 int err;
934
935 if (redir_lval == NULL)
936 return;
937
938 if (len == -1)
939 /* Append the entire string */
940 val = vim_strsave(value);
941 else
942 /* Append only the specified number of characters */
943 val = vim_strnsave(value, len);
944 if (val == NULL)
945 return;
946
947 tv.v_type = VAR_STRING;
948 tv.vval.v_string = val;
949
950 save_emsg = did_emsg;
951 did_emsg = FALSE;
952 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
953 err = did_emsg;
954 did_emsg += save_emsg;
955 if (err)
956 var_redir_stop();
957
958 vim_free(tv.vval.v_string);
959}
960
961/*
962 * Stop redirecting command output to a variable.
963 */
964 void
965var_redir_stop()
966{
967 if (redir_lval != NULL)
968 {
969 clear_lval(redir_lval);
970 vim_free(redir_lval);
971 redir_lval = NULL;
972 }
973 vim_free(redir_varname);
974 redir_varname = NULL;
975}
976
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977# if defined(FEAT_MBYTE) || defined(PROTO)
978 int
979eval_charconvert(enc_from, enc_to, fname_from, fname_to)
980 char_u *enc_from;
981 char_u *enc_to;
982 char_u *fname_from;
983 char_u *fname_to;
984{
985 int err = FALSE;
986
987 set_vim_var_string(VV_CC_FROM, enc_from, -1);
988 set_vim_var_string(VV_CC_TO, enc_to, -1);
989 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
990 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
991 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
992 err = TRUE;
993 set_vim_var_string(VV_CC_FROM, NULL, -1);
994 set_vim_var_string(VV_CC_TO, NULL, -1);
995 set_vim_var_string(VV_FNAME_IN, NULL, -1);
996 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
997
998 if (err)
999 return FAIL;
1000 return OK;
1001}
1002# endif
1003
1004# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1005 int
1006eval_printexpr(fname, args)
1007 char_u *fname;
1008 char_u *args;
1009{
1010 int err = FALSE;
1011
1012 set_vim_var_string(VV_FNAME_IN, fname, -1);
1013 set_vim_var_string(VV_CMDARG, args, -1);
1014 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1015 err = TRUE;
1016 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1017 set_vim_var_string(VV_CMDARG, NULL, -1);
1018
1019 if (err)
1020 {
1021 mch_remove(fname);
1022 return FAIL;
1023 }
1024 return OK;
1025}
1026# endif
1027
1028# if defined(FEAT_DIFF) || defined(PROTO)
1029 void
1030eval_diff(origfile, newfile, outfile)
1031 char_u *origfile;
1032 char_u *newfile;
1033 char_u *outfile;
1034{
1035 int err = FALSE;
1036
1037 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1038 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1039 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1040 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1041 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1042 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1043 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1044}
1045
1046 void
1047eval_patch(origfile, difffile, outfile)
1048 char_u *origfile;
1049 char_u *difffile;
1050 char_u *outfile;
1051{
1052 int err;
1053
1054 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1055 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1056 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1057 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1058 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1059 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1060 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1061}
1062# endif
1063
1064/*
1065 * Top level evaluation function, returning a boolean.
1066 * Sets "error" to TRUE if there was an error.
1067 * Return TRUE or FALSE.
1068 */
1069 int
1070eval_to_bool(arg, error, nextcmd, skip)
1071 char_u *arg;
1072 int *error;
1073 char_u **nextcmd;
1074 int skip; /* only parse, don't execute */
1075{
Bram Moolenaar33570922005-01-25 22:26:29 +00001076 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 int retval = FALSE;
1078
1079 if (skip)
1080 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001081 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 else
1084 {
1085 *error = FALSE;
1086 if (!skip)
1087 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001088 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001089 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 }
1091 }
1092 if (skip)
1093 --emsg_skip;
1094
1095 return retval;
1096}
1097
1098/*
1099 * Top level evaluation function, returning a string. If "skip" is TRUE,
1100 * only parsing to "nextcmd" is done, without reporting errors. Return
1101 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1102 */
1103 char_u *
1104eval_to_string_skip(arg, nextcmd, skip)
1105 char_u *arg;
1106 char_u **nextcmd;
1107 int skip; /* only parse, don't execute */
1108{
Bram Moolenaar33570922005-01-25 22:26:29 +00001109 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 char_u *retval;
1111
1112 if (skip)
1113 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001114 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 retval = NULL;
1116 else
1117 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001118 retval = vim_strsave(get_tv_string(&tv));
1119 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 }
1121 if (skip)
1122 --emsg_skip;
1123
1124 return retval;
1125}
1126
1127/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001128 * Skip over an expression at "*pp".
1129 * Return FAIL for an error, OK otherwise.
1130 */
1131 int
1132skip_expr(pp)
1133 char_u **pp;
1134{
Bram Moolenaar33570922005-01-25 22:26:29 +00001135 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001136
1137 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001138 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001139}
1140
1141/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 * Top level evaluation function, returning a string.
1143 * Return pointer to allocated memory, or NULL for failure.
1144 */
1145 char_u *
1146eval_to_string(arg, nextcmd)
1147 char_u *arg;
1148 char_u **nextcmd;
1149{
Bram Moolenaar33570922005-01-25 22:26:29 +00001150 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 char_u *retval;
1152
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001153 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 retval = NULL;
1155 else
1156 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001157 retval = vim_strsave(get_tv_string(&tv));
1158 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 }
1160
1161 return retval;
1162}
1163
1164/*
1165 * Call eval_to_string() with "sandbox" set and not using local variables.
1166 */
1167 char_u *
1168eval_to_string_safe(arg, nextcmd)
1169 char_u *arg;
1170 char_u **nextcmd;
1171{
1172 char_u *retval;
1173 void *save_funccalp;
1174
1175 save_funccalp = save_funccal();
1176 ++sandbox;
1177 retval = eval_to_string(arg, nextcmd);
1178 --sandbox;
1179 restore_funccal(save_funccalp);
1180 return retval;
1181}
1182
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183/*
1184 * Top level evaluation function, returning a number.
1185 * Evaluates "expr" silently.
1186 * Returns -1 for an error.
1187 */
1188 int
1189eval_to_number(expr)
1190 char_u *expr;
1191{
Bram Moolenaar33570922005-01-25 22:26:29 +00001192 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001194 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195
1196 ++emsg_off;
1197
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001198 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 retval = -1;
1200 else
1201 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001202 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001203 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 }
1205 --emsg_off;
1206
1207 return retval;
1208}
1209
Bram Moolenaara40058a2005-07-11 22:42:07 +00001210/*
1211 * Prepare v: variable "idx" to be used.
1212 * Save the current typeval in "save_tv".
1213 * When not used yet add the variable to the v: hashtable.
1214 */
1215 static void
1216prepare_vimvar(idx, save_tv)
1217 int idx;
1218 typval_T *save_tv;
1219{
1220 *save_tv = vimvars[idx].vv_tv;
1221 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1222 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1223}
1224
1225/*
1226 * Restore v: variable "idx" to typeval "save_tv".
1227 * When no longer defined, remove the variable from the v: hashtable.
1228 */
1229 static void
1230restore_vimvar(idx, save_tv)
1231 int idx;
1232 typval_T *save_tv;
1233{
1234 hashitem_T *hi;
1235
1236 clear_tv(&vimvars[idx].vv_tv);
1237 vimvars[idx].vv_tv = *save_tv;
1238 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1239 {
1240 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1241 if (HASHITEM_EMPTY(hi))
1242 EMSG2(_(e_intern2), "restore_vimvar()");
1243 else
1244 hash_remove(&vimvarht, hi);
1245 }
1246}
1247
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001248#if defined(FEAT_SYN_HL) || defined(PROTO)
1249/*
1250 * Evaluate an expression to a list with suggestions.
1251 * For the "expr:" part of 'spellsuggest'.
1252 */
1253 list_T *
1254eval_spell_expr(badword, expr)
1255 char_u *badword;
1256 char_u *expr;
1257{
1258 typval_T save_val;
1259 typval_T rettv;
1260 list_T *list = NULL;
1261 char_u *p = skipwhite(expr);
1262
1263 /* Set "v:val" to the bad word. */
1264 prepare_vimvar(VV_VAL, &save_val);
1265 vimvars[VV_VAL].vv_type = VAR_STRING;
1266 vimvars[VV_VAL].vv_str = badword;
1267 if (p_verbose == 0)
1268 ++emsg_off;
1269
1270 if (eval1(&p, &rettv, TRUE) == OK)
1271 {
1272 if (rettv.v_type != VAR_LIST)
1273 clear_tv(&rettv);
1274 else
1275 list = rettv.vval.v_list;
1276 }
1277
1278 if (p_verbose == 0)
1279 --emsg_off;
1280 vimvars[VV_VAL].vv_str = NULL;
1281 restore_vimvar(VV_VAL, &save_val);
1282
1283 return list;
1284}
1285
1286/*
1287 * "list" is supposed to contain two items: a word and a number. Return the
1288 * word in "pp" and the number as the return value.
1289 * Return -1 if anything isn't right.
1290 * Used to get the good word and score from the eval_spell_expr() result.
1291 */
1292 int
1293get_spellword(list, pp)
1294 list_T *list;
1295 char_u **pp;
1296{
1297 listitem_T *li;
1298
1299 li = list->lv_first;
1300 if (li == NULL)
1301 return -1;
1302 *pp = get_tv_string(&li->li_tv);
1303
1304 li = li->li_next;
1305 if (li == NULL)
1306 return -1;
1307 return get_tv_number(&li->li_tv);
1308}
1309#endif
1310
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1312/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001313 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 * Uses argv[argc] for the function arguments.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001315 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001317 static int
1318call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 char_u *func;
1320 int argc;
1321 char_u **argv;
1322 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001323 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324{
Bram Moolenaar33570922005-01-25 22:26:29 +00001325 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 long n;
1327 int len;
1328 int i;
1329 int doesrange;
1330 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001331 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332
Bram Moolenaar33570922005-01-25 22:26:29 +00001333 argvars = (typval_T *)alloc((unsigned)(argc * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001335 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336
1337 for (i = 0; i < argc; i++)
1338 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001339 /* Pass a NULL or empty argument as an empty string */
1340 if (argv[i] == NULL || *argv[i] == NUL)
1341 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001342 argvars[i].v_type = VAR_STRING;
1343 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001344 continue;
1345 }
1346
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 /* Recognize a number argument, the others must be strings. */
1348 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1349 if (len != 0 && len == (int)STRLEN(argv[i]))
1350 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001351 argvars[i].v_type = VAR_NUMBER;
1352 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 }
1354 else
1355 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001356 argvars[i].v_type = VAR_STRING;
1357 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 }
1359 }
1360
1361 if (safe)
1362 {
1363 save_funccalp = save_funccal();
1364 ++sandbox;
1365 }
1366
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001367 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1368 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001370 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 if (safe)
1372 {
1373 --sandbox;
1374 restore_funccal(save_funccalp);
1375 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001376 vim_free(argvars);
1377
1378 if (ret == FAIL)
1379 clear_tv(rettv);
1380
1381 return ret;
1382}
1383
1384/*
1385 * Call some vimL function and return the result as a string
1386 * Uses argv[argc] for the function arguments.
1387 */
1388 void *
1389call_func_retstr(func, argc, argv, safe)
1390 char_u *func;
1391 int argc;
1392 char_u **argv;
1393 int safe; /* use the sandbox */
1394{
1395 typval_T rettv;
1396 char_u *retval = NULL;
1397
1398 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1399 return NULL;
1400
1401 retval = vim_strsave(get_tv_string(&rettv));
1402
1403 clear_tv(&rettv);
1404
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 return retval;
1406}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001407
1408/*
1409 * Call some vimL function and return the result as a list
1410 * Uses argv[argc] for the function arguments.
1411 */
1412 void *
1413call_func_retlist(func, argc, argv, safe)
1414 char_u *func;
1415 int argc;
1416 char_u **argv;
1417 int safe; /* use the sandbox */
1418{
1419 typval_T rettv;
1420
1421 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1422 return NULL;
1423
1424 if (rettv.v_type != VAR_LIST)
1425 {
1426 clear_tv(&rettv);
1427 return NULL;
1428 }
1429
1430 return rettv.vval.v_list;
1431}
1432
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433#endif
1434
1435/*
1436 * Save the current function call pointer, and set it to NULL.
1437 * Used when executing autocommands and for ":source".
1438 */
1439 void *
1440save_funccal()
1441{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001442 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 current_funccal = NULL;
1445 return (void *)fc;
1446}
1447
1448 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001449restore_funccal(vfc)
1450 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001452 funccall_T *fc = (funccall_T *)vfc;
1453
1454 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455}
1456
Bram Moolenaar05159a02005-02-26 23:04:13 +00001457#if defined(FEAT_PROFILE) || defined(PROTO)
1458/*
1459 * Prepare profiling for entering a child or something else that is not
1460 * counted for the script/function itself.
1461 * Should always be called in pair with prof_child_exit().
1462 */
1463 void
1464prof_child_enter(tm)
1465 proftime_T *tm; /* place to store waittime */
1466{
1467 funccall_T *fc = current_funccal;
1468
1469 if (fc != NULL && fc->func->uf_profiling)
1470 profile_start(&fc->prof_child);
1471 script_prof_save(tm);
1472}
1473
1474/*
1475 * Take care of time spent in a child.
1476 * Should always be called after prof_child_enter().
1477 */
1478 void
1479prof_child_exit(tm)
1480 proftime_T *tm; /* where waittime was stored */
1481{
1482 funccall_T *fc = current_funccal;
1483
1484 if (fc != NULL && fc->func->uf_profiling)
1485 {
1486 profile_end(&fc->prof_child);
1487 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1488 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1489 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1490 }
1491 script_prof_restore(tm);
1492}
1493#endif
1494
1495
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496#ifdef FEAT_FOLDING
1497/*
1498 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1499 * it in "*cp". Doesn't give error messages.
1500 */
1501 int
1502eval_foldexpr(arg, cp)
1503 char_u *arg;
1504 int *cp;
1505{
Bram Moolenaar33570922005-01-25 22:26:29 +00001506 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 int retval;
1508 char_u *s;
1509
1510 ++emsg_off;
1511 ++sandbox;
1512 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001513 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 retval = 0;
1515 else
1516 {
1517 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001518 if (tv.v_type == VAR_NUMBER)
1519 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001520 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 retval = 0;
1522 else
1523 {
1524 /* If the result is a string, check if there is a non-digit before
1525 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001526 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 if (!VIM_ISDIGIT(*s) && *s != '-')
1528 *cp = *s++;
1529 retval = atol((char *)s);
1530 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001531 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 }
1533 --emsg_off;
1534 --sandbox;
1535
1536 return retval;
1537}
1538#endif
1539
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001541 * ":let" list all variable values
1542 * ":let var1 var2" list variable values
1543 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001544 * ":let var += expr" assignment command.
1545 * ":let var -= expr" assignment command.
1546 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001547 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 */
1549 void
1550ex_let(eap)
1551 exarg_T *eap;
1552{
1553 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001554 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001555 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001557 int var_count = 0;
1558 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001559 char_u op[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001561 expr = skip_var_list(arg, &var_count, &semicolon);
1562 if (expr == NULL)
1563 return;
1564 expr = vim_strchr(expr, '=');
1565 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001567 /*
1568 * ":let" without "=": list variables
1569 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001570 if (*arg == '[')
1571 EMSG(_(e_invarg));
1572 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001573 /* ":let var1 var2" */
1574 arg = list_arg_vars(eap, arg);
1575 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001576 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001577 /* ":let" */
Bram Moolenaara7043832005-01-21 11:56:39 +00001578 list_glob_vars();
1579 list_buf_vars();
1580 list_win_vars();
1581 list_vim_vars();
1582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 eap->nextcmd = check_nextcmd(arg);
1584 }
1585 else
1586 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001587 op[0] = '=';
1588 op[1] = NUL;
1589 if (expr > arg)
1590 {
1591 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1592 op[0] = expr[-1]; /* +=, -= or .= */
1593 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001594 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001595
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 if (eap->skip)
1597 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001598 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 if (eap->skip)
1600 {
1601 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001602 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 --emsg_skip;
1604 }
1605 else if (i != FAIL)
1606 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001607 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001608 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001609 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 }
1611 }
1612}
1613
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001614/*
1615 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1616 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001617 * When "nextchars" is not NULL it points to a string with characters that
1618 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1619 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001620 * Returns OK or FAIL;
1621 */
1622 static int
1623ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1624 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001625 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001626 int copy; /* copy values from "tv", don't move */
1627 int semicolon; /* from skip_var_list() */
1628 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001629 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001630{
1631 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001632 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001633 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001634 listitem_T *item;
1635 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001636
1637 if (*arg != '[')
1638 {
1639 /*
1640 * ":let var = expr" or ":for var in list"
1641 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001642 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001643 return FAIL;
1644 return OK;
1645 }
1646
1647 /*
1648 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1649 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001650 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001651 {
1652 EMSG(_(e_listreq));
1653 return FAIL;
1654 }
1655
1656 i = list_len(l);
1657 if (semicolon == 0 && var_count < i)
1658 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001659 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001660 return FAIL;
1661 }
1662 if (var_count - semicolon > i)
1663 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001664 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001665 return FAIL;
1666 }
1667
1668 item = l->lv_first;
1669 while (*arg != ']')
1670 {
1671 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001672 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001673 item = item->li_next;
1674 if (arg == NULL)
1675 return FAIL;
1676
1677 arg = skipwhite(arg);
1678 if (*arg == ';')
1679 {
1680 /* Put the rest of the list (may be empty) in the var after ';'.
1681 * Create a new list for this. */
1682 l = list_alloc();
1683 if (l == NULL)
1684 return FAIL;
1685 while (item != NULL)
1686 {
1687 list_append_tv(l, &item->li_tv);
1688 item = item->li_next;
1689 }
1690
1691 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001692 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001693 ltv.vval.v_list = l;
1694 l->lv_refcount = 1;
1695
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001696 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1697 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001698 clear_tv(&ltv);
1699 if (arg == NULL)
1700 return FAIL;
1701 break;
1702 }
1703 else if (*arg != ',' && *arg != ']')
1704 {
1705 EMSG2(_(e_intern2), "ex_let_vars()");
1706 return FAIL;
1707 }
1708 }
1709
1710 return OK;
1711}
1712
1713/*
1714 * Skip over assignable variable "var" or list of variables "[var, var]".
1715 * Used for ":let varvar = expr" and ":for varvar in expr".
1716 * For "[var, var]" increment "*var_count" for each variable.
1717 * for "[var, var; var]" set "semicolon".
1718 * Return NULL for an error.
1719 */
1720 static char_u *
1721skip_var_list(arg, var_count, semicolon)
1722 char_u *arg;
1723 int *var_count;
1724 int *semicolon;
1725{
1726 char_u *p, *s;
1727
1728 if (*arg == '[')
1729 {
1730 /* "[var, var]": find the matching ']'. */
1731 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001732 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001733 {
1734 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1735 s = skip_var_one(p);
1736 if (s == p)
1737 {
1738 EMSG2(_(e_invarg2), p);
1739 return NULL;
1740 }
1741 ++*var_count;
1742
1743 p = skipwhite(s);
1744 if (*p == ']')
1745 break;
1746 else if (*p == ';')
1747 {
1748 if (*semicolon == 1)
1749 {
1750 EMSG(_("Double ; in list of variables"));
1751 return NULL;
1752 }
1753 *semicolon = 1;
1754 }
1755 else if (*p != ',')
1756 {
1757 EMSG2(_(e_invarg2), p);
1758 return NULL;
1759 }
1760 }
1761 return p + 1;
1762 }
1763 else
1764 return skip_var_one(arg);
1765}
1766
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001767/*
Bram Moolenaar92124a32005-06-17 22:03:40 +00001768 * Skip one (assignable) variable name, includig @r, $VAR, &option, d.key,
1769 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001770 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001771 static char_u *
1772skip_var_one(arg)
1773 char_u *arg;
1774{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001775 if (*arg == '@' && arg[1] != NUL)
1776 return arg + 2;
1777 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1778 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001779}
1780
Bram Moolenaara7043832005-01-21 11:56:39 +00001781/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001782 * List variables for hashtab "ht" with prefix "prefix".
1783 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001784 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001785 static void
Bram Moolenaar33570922005-01-25 22:26:29 +00001786list_hashtable_vars(ht, prefix, empty)
1787 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001788 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001789 int empty;
Bram Moolenaara7043832005-01-21 11:56:39 +00001790{
Bram Moolenaar33570922005-01-25 22:26:29 +00001791 hashitem_T *hi;
1792 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001793 int todo;
1794
1795 todo = ht->ht_used;
1796 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1797 {
1798 if (!HASHITEM_EMPTY(hi))
1799 {
1800 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001801 di = HI2DI(hi);
1802 if (empty || di->di_tv.v_type != VAR_STRING
1803 || di->di_tv.vval.v_string != NULL)
1804 list_one_var(di, prefix);
Bram Moolenaara7043832005-01-21 11:56:39 +00001805 }
1806 }
1807}
1808
1809/*
1810 * List global variables.
1811 */
1812 static void
1813list_glob_vars()
1814{
Bram Moolenaar33570922005-01-25 22:26:29 +00001815 list_hashtable_vars(&globvarht, (char_u *)"", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001816}
1817
1818/*
1819 * List buffer variables.
1820 */
1821 static void
1822list_buf_vars()
1823{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001824 char_u numbuf[NUMBUFLEN];
1825
Bram Moolenaar33570922005-01-25 22:26:29 +00001826 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001827
1828 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
1829 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER, numbuf);
Bram Moolenaara7043832005-01-21 11:56:39 +00001830}
1831
1832/*
1833 * List window variables.
1834 */
1835 static void
1836list_win_vars()
1837{
Bram Moolenaar33570922005-01-25 22:26:29 +00001838 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001839}
1840
1841/*
1842 * List Vim variables.
1843 */
1844 static void
1845list_vim_vars()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001846{
Bram Moolenaar33570922005-01-25 22:26:29 +00001847 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001848}
1849
1850/*
1851 * List variables in "arg".
1852 */
1853 static char_u *
1854list_arg_vars(eap, arg)
1855 exarg_T *eap;
1856 char_u *arg;
1857{
1858 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001859 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001860 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001861 char_u *name_start;
1862 char_u *arg_subsc;
1863 char_u *tofree;
1864 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001865
1866 while (!ends_excmd(*arg) && !got_int)
1867 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001868 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001869 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001870 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001871 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
1872 {
1873 emsg_severe = TRUE;
1874 EMSG(_(e_trailing));
1875 break;
1876 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001877 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001878 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001879 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001880 /* get_name_len() takes care of expanding curly braces */
1881 name_start = name = arg;
1882 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1883 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001884 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001885 /* This is mainly to keep test 49 working: when expanding
1886 * curly braces fails overrule the exception error message. */
1887 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001888 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001889 emsg_severe = TRUE;
1890 EMSG2(_(e_invarg2), arg);
1891 break;
1892 }
1893 error = TRUE;
1894 }
1895 else
1896 {
1897 if (tofree != NULL)
1898 name = tofree;
1899 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001900 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001901 else
1902 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001903 /* handle d.key, l[idx], f(expr) */
1904 arg_subsc = arg;
1905 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001906 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001907 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001908 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001909 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001910 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001911 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001912 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001913 case 'g': list_glob_vars(); break;
1914 case 'b': list_buf_vars(); break;
1915 case 'w': list_win_vars(); break;
1916 case 'v': list_vim_vars(); break;
1917 default:
1918 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001919 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001920 }
1921 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001922 {
1923 char_u numbuf[NUMBUFLEN];
1924 char_u *tf;
1925 int c;
1926 char_u *s;
1927
1928 s = echo_string(&tv, &tf, numbuf);
1929 c = *arg;
1930 *arg = NUL;
1931 list_one_var_a((char_u *)"",
1932 arg == arg_subsc ? name : name_start,
1933 tv.v_type, s == NULL ? (char_u *)"" : s);
1934 *arg = c;
1935 vim_free(tf);
1936 }
1937 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001938 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001939 }
1940 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001941
1942 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001943 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001944
1945 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001946 }
1947
1948 return arg;
1949}
1950
1951/*
1952 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1953 * Returns a pointer to the char just after the var name.
1954 * Returns NULL if there is an error.
1955 */
1956 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001957ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001958 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00001959 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001960 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001961 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001962 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001963{
1964 int c1;
1965 char_u *name;
1966 char_u *p;
1967 char_u *arg_end = NULL;
1968 int len;
1969 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001970 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001971
1972 /*
1973 * ":let $VAR = expr": Set environment variable.
1974 */
1975 if (*arg == '$')
1976 {
1977 /* Find the end of the name. */
1978 ++arg;
1979 name = arg;
1980 len = get_env_len(&arg);
1981 if (len == 0)
1982 EMSG2(_(e_invarg2), name - 1);
1983 else
1984 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001985 if (op != NULL && (*op == '+' || *op == '-'))
1986 EMSG2(_(e_letwrong), op);
1987 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001988 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001989 EMSG(_(e_letunexp));
1990 else
1991 {
1992 c1 = name[len];
1993 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001994 p = get_tv_string_chk(tv);
1995 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001996 {
1997 int mustfree = FALSE;
1998 char_u *s = vim_getenv(name, &mustfree);
1999
2000 if (s != NULL)
2001 {
2002 p = tofree = concat_str(s, p);
2003 if (mustfree)
2004 vim_free(s);
2005 }
2006 }
2007 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002008 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002009 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002010 if (STRICMP(name, "HOME") == 0)
2011 init_homedir();
2012 else if (didset_vim && STRICMP(name, "VIM") == 0)
2013 didset_vim = FALSE;
2014 else if (didset_vimruntime
2015 && STRICMP(name, "VIMRUNTIME") == 0)
2016 didset_vimruntime = FALSE;
2017 arg_end = arg;
2018 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002019 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002020 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002021 }
2022 }
2023 }
2024
2025 /*
2026 * ":let &option = expr": Set option value.
2027 * ":let &l:option = expr": Set local option value.
2028 * ":let &g:option = expr": Set global option value.
2029 */
2030 else if (*arg == '&')
2031 {
2032 /* Find the end of the name. */
2033 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002034 if (p == NULL || (endchars != NULL
2035 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002036 EMSG(_(e_letunexp));
2037 else
2038 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002039 long n;
2040 int opt_type;
2041 long numval;
2042 char_u *stringval = NULL;
2043 char_u *s;
2044
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002045 c1 = *p;
2046 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002047
2048 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002049 s = get_tv_string_chk(tv); /* != NULL if number or string */
2050 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002051 {
2052 opt_type = get_option_value(arg, &numval,
2053 &stringval, opt_flags);
2054 if ((opt_type == 1 && *op == '.')
2055 || (opt_type == 0 && *op != '.'))
2056 EMSG2(_(e_letwrong), op);
2057 else
2058 {
2059 if (opt_type == 1) /* number */
2060 {
2061 if (*op == '+')
2062 n = numval + n;
2063 else
2064 n = numval - n;
2065 }
2066 else if (opt_type == 0 && stringval != NULL) /* string */
2067 {
2068 s = concat_str(stringval, s);
2069 vim_free(stringval);
2070 stringval = s;
2071 }
2072 }
2073 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002074 if (s != NULL)
2075 {
2076 set_option_value(arg, n, s, opt_flags);
2077 arg_end = p;
2078 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002079 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002080 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002081 }
2082 }
2083
2084 /*
2085 * ":let @r = expr": Set register contents.
2086 */
2087 else if (*arg == '@')
2088 {
2089 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002090 if (op != NULL && (*op == '+' || *op == '-'))
2091 EMSG2(_(e_letwrong), op);
2092 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002093 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002094 EMSG(_(e_letunexp));
2095 else
2096 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002097 char_u *tofree = NULL;
2098 char_u *s;
2099
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002100 p = get_tv_string_chk(tv);
2101 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002102 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002103 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002104 if (s != NULL)
2105 {
2106 p = tofree = concat_str(s, p);
2107 vim_free(s);
2108 }
2109 }
2110 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002111 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002112 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002113 arg_end = arg + 1;
2114 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002115 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002116 }
2117 }
2118
2119 /*
2120 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002121 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002122 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002123 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002124 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002125 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002126
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002127 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002128 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002129 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002130 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2131 EMSG(_(e_letunexp));
2132 else
2133 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002134 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002135 arg_end = p;
2136 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002137 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002138 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002139 }
2140
2141 else
2142 EMSG2(_(e_invarg2), arg);
2143
2144 return arg_end;
2145}
2146
2147/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002148 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2149 */
2150 static int
2151check_changedtick(arg)
2152 char_u *arg;
2153{
2154 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2155 {
2156 EMSG2(_(e_readonlyvar), arg);
2157 return TRUE;
2158 }
2159 return FALSE;
2160}
2161
2162/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002163 * Get an lval: variable, Dict item or List item that can be assigned a value
2164 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2165 * "name.key", "name.key[expr]" etc.
2166 * Indexing only works if "name" is an existing List or Dictionary.
2167 * "name" points to the start of the name.
2168 * If "rettv" is not NULL it points to the value to be assigned.
2169 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2170 * wrong; must end in space or cmd separator.
2171 *
2172 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002173 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002174 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002175 */
2176 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002177get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002178 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002179 typval_T *rettv;
2180 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002181 int unlet;
2182 int skip;
2183 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002184 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002185{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002186 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002187 char_u *expr_start, *expr_end;
2188 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002189 dictitem_T *v;
2190 typval_T var1;
2191 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002192 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002193 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002194 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002195 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002196 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002197
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002198 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002199 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002200
2201 if (skip)
2202 {
2203 /* When skipping just find the end of the name. */
2204 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002205 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002206 }
2207
2208 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002209 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002210 if (expr_start != NULL)
2211 {
2212 /* Don't expand the name when we already know there is an error. */
2213 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2214 && *p != '[' && *p != '.')
2215 {
2216 EMSG(_(e_trailing));
2217 return NULL;
2218 }
2219
2220 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2221 if (lp->ll_exp_name == NULL)
2222 {
2223 /* Report an invalid expression in braces, unless the
2224 * expression evaluation has been cancelled due to an
2225 * aborting error, an interrupt, or an exception. */
2226 if (!aborting() && !quiet)
2227 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002228 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002229 EMSG2(_(e_invarg2), name);
2230 return NULL;
2231 }
2232 }
2233 lp->ll_name = lp->ll_exp_name;
2234 }
2235 else
2236 lp->ll_name = name;
2237
2238 /* Without [idx] or .key we are done. */
2239 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2240 return p;
2241
2242 cc = *p;
2243 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002244 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002245 if (v == NULL && !quiet)
2246 EMSG2(_(e_undefvar), lp->ll_name);
2247 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002248 if (v == NULL)
2249 return NULL;
2250
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002251 /*
2252 * Loop until no more [idx] or .key is following.
2253 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002254 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002255 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002256 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002257 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2258 && !(lp->ll_tv->v_type == VAR_DICT
2259 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002260 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002261 if (!quiet)
2262 EMSG(_("E689: Can only index a List or Dictionary"));
2263 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002264 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002265 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002266 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002267 if (!quiet)
2268 EMSG(_("E708: [:] must come last"));
2269 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002270 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002271
Bram Moolenaar8c711452005-01-14 21:53:12 +00002272 len = -1;
2273 if (*p == '.')
2274 {
2275 key = p + 1;
2276 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2277 ;
2278 if (len == 0)
2279 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002280 if (!quiet)
2281 EMSG(_(e_emptykey));
2282 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002283 }
2284 p = key + len;
2285 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002286 else
2287 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002288 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002289 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002290 if (*p == ':')
2291 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002292 else
2293 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002294 empty1 = FALSE;
2295 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002296 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002297 if (get_tv_string_chk(&var1) == NULL)
2298 {
2299 /* not a number or string */
2300 clear_tv(&var1);
2301 return NULL;
2302 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002303 }
2304
2305 /* Optionally get the second index [ :expr]. */
2306 if (*p == ':')
2307 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002308 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002309 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002310 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002311 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002312 if (!empty1)
2313 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002314 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002315 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002316 if (rettv != NULL && (rettv->v_type != VAR_LIST
2317 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002318 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002319 if (!quiet)
2320 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002321 if (!empty1)
2322 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002323 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002324 }
2325 p = skipwhite(p + 1);
2326 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002327 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002328 else
2329 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002330 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002331 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2332 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002333 if (!empty1)
2334 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002335 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002336 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002337 if (get_tv_string_chk(&var2) == NULL)
2338 {
2339 /* not a number or string */
2340 if (!empty1)
2341 clear_tv(&var1);
2342 clear_tv(&var2);
2343 return NULL;
2344 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002345 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002346 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002347 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002348 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002349 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002350
Bram Moolenaar8c711452005-01-14 21:53:12 +00002351 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002352 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002353 if (!quiet)
2354 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002355 if (!empty1)
2356 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002357 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002358 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002359 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002360 }
2361
2362 /* Skip to past ']'. */
2363 ++p;
2364 }
2365
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002366 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002367 {
2368 if (len == -1)
2369 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002370 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002371 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002372 if (*key == NUL)
2373 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002374 if (!quiet)
2375 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002376 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002377 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002378 }
2379 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002380 lp->ll_list = NULL;
2381 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002382 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002383 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002384 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002385 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002386 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002387 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002388 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002389 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002390 if (len == -1)
2391 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002392 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002393 }
2394 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002395 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002396 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002397 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002398 if (len == -1)
2399 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002400 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002401 p = NULL;
2402 break;
2403 }
2404 if (len == -1)
2405 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002406 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002407 }
2408 else
2409 {
2410 /*
2411 * Get the number and item for the only or first index of the List.
2412 */
2413 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002414 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002415 else
2416 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002417 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002418 clear_tv(&var1);
2419 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002420 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002421 lp->ll_list = lp->ll_tv->vval.v_list;
2422 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2423 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002424 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002425 if (!quiet)
2426 EMSGN(_(e_listidx), lp->ll_n1);
2427 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002428 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002429 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002430 }
2431
2432 /*
2433 * May need to find the item or absolute index for the second
2434 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002435 * When no index given: "lp->ll_empty2" is TRUE.
2436 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002437 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002438 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002439 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002440 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002441 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002442 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002443 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002444 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002445 if (ni == NULL)
2446 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002447 if (!quiet)
2448 EMSGN(_(e_listidx), lp->ll_n2);
2449 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002450 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002451 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002452 }
2453
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002454 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2455 if (lp->ll_n1 < 0)
2456 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2457 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002458 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002459 if (!quiet)
2460 EMSGN(_(e_listidx), lp->ll_n2);
2461 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002462 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002463 }
2464
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002465 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002466 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002467 }
2468
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002469 return p;
2470}
2471
2472/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002473 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002474 */
2475 static void
2476clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002477 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002478{
2479 vim_free(lp->ll_exp_name);
2480 vim_free(lp->ll_newkey);
2481}
2482
2483/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002484 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002485 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002486 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002487 */
2488 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002489set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002490 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002491 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002492 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002493 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002494 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002495{
2496 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002497 listitem_T *ni;
2498 listitem_T *ri;
2499 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002500
2501 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002502 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002503 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002504 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002505 cc = *endp;
2506 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002507 if (op != NULL && *op != '=')
2508 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002509 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002510
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002511 /* handle +=, -= and .= */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002512 if (get_var_tv(lp->ll_name, STRLEN(lp->ll_name),
2513 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002514 {
2515 if (tv_op(&tv, rettv, op) == OK)
2516 set_var(lp->ll_name, &tv, FALSE);
2517 clear_tv(&tv);
2518 }
2519 }
2520 else
2521 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002522 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002523 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002524 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002525 else if (tv_check_lock(lp->ll_newkey == NULL
2526 ? lp->ll_tv->v_lock
2527 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2528 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002529 else if (lp->ll_range)
2530 {
2531 /*
2532 * Assign the List values to the list items.
2533 */
2534 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002535 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002536 if (op != NULL && *op != '=')
2537 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2538 else
2539 {
2540 clear_tv(&lp->ll_li->li_tv);
2541 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2542 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002543 ri = ri->li_next;
2544 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2545 break;
2546 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002547 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002548 /* Need to add an empty item. */
2549 ni = listitem_alloc();
2550 if (ni == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002551 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002552 ri = NULL;
2553 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002554 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002555 ni->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002556 ni->li_tv.v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002557 ni->li_tv.vval.v_number = 0;
2558 list_append(lp->ll_list, ni);
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002559 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002560 lp->ll_li = lp->ll_li->li_next;
2561 ++lp->ll_n1;
2562 }
2563 if (ri != NULL)
2564 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002565 else if (lp->ll_empty2
2566 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002567 : lp->ll_n1 != lp->ll_n2)
2568 EMSG(_("E711: List value has not enough items"));
2569 }
2570 else
2571 {
2572 /*
2573 * Assign to a List or Dictionary item.
2574 */
2575 if (lp->ll_newkey != NULL)
2576 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002577 if (op != NULL && *op != '=')
2578 {
2579 EMSG2(_(e_letwrong), op);
2580 return;
2581 }
2582
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002583 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002584 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002585 if (di == NULL)
2586 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002587 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2588 {
2589 vim_free(di);
2590 return;
2591 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002592 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002593 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002594 else if (op != NULL && *op != '=')
2595 {
2596 tv_op(lp->ll_tv, rettv, op);
2597 return;
2598 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002599 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002600 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002601
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002602 /*
2603 * Assign the value to the variable or list item.
2604 */
2605 if (copy)
2606 copy_tv(rettv, lp->ll_tv);
2607 else
2608 {
2609 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002610 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002611 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002612 }
2613 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002614}
2615
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002616/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002617 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2618 * Returns OK or FAIL.
2619 */
2620 static int
2621tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002622 typval_T *tv1;
2623 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002624 char_u *op;
2625{
2626 long n;
2627 char_u numbuf[NUMBUFLEN];
2628 char_u *s;
2629
2630 /* Can't do anything with a Funcref or a Dict on the right. */
2631 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2632 {
2633 switch (tv1->v_type)
2634 {
2635 case VAR_DICT:
2636 case VAR_FUNC:
2637 break;
2638
2639 case VAR_LIST:
2640 if (*op != '+' || tv2->v_type != VAR_LIST)
2641 break;
2642 /* List += List */
2643 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2644 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2645 return OK;
2646
2647 case VAR_NUMBER:
2648 case VAR_STRING:
2649 if (tv2->v_type == VAR_LIST)
2650 break;
2651 if (*op == '+' || *op == '-')
2652 {
2653 /* nr += nr or nr -= nr*/
2654 n = get_tv_number(tv1);
2655 if (*op == '+')
2656 n += get_tv_number(tv2);
2657 else
2658 n -= get_tv_number(tv2);
2659 clear_tv(tv1);
2660 tv1->v_type = VAR_NUMBER;
2661 tv1->vval.v_number = n;
2662 }
2663 else
2664 {
2665 /* str .= str */
2666 s = get_tv_string(tv1);
2667 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2668 clear_tv(tv1);
2669 tv1->v_type = VAR_STRING;
2670 tv1->vval.v_string = s;
2671 }
2672 return OK;
2673 }
2674 }
2675
2676 EMSG2(_(e_letwrong), op);
2677 return FAIL;
2678}
2679
2680/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002681 * Add a watcher to a list.
2682 */
2683 static void
2684list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002685 list_T *l;
2686 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002687{
2688 lw->lw_next = l->lv_watch;
2689 l->lv_watch = lw;
2690}
2691
2692/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002693 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002694 * No warning when it isn't found...
2695 */
2696 static void
2697list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002698 list_T *l;
2699 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002700{
Bram Moolenaar33570922005-01-25 22:26:29 +00002701 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002702
2703 lwp = &l->lv_watch;
2704 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2705 {
2706 if (lw == lwrem)
2707 {
2708 *lwp = lw->lw_next;
2709 break;
2710 }
2711 lwp = &lw->lw_next;
2712 }
2713}
2714
2715/*
2716 * Just before removing an item from a list: advance watchers to the next
2717 * item.
2718 */
2719 static void
2720list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002721 list_T *l;
2722 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002723{
Bram Moolenaar33570922005-01-25 22:26:29 +00002724 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002725
2726 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2727 if (lw->lw_item == item)
2728 lw->lw_item = item->li_next;
2729}
2730
2731/*
2732 * Evaluate the expression used in a ":for var in expr" command.
2733 * "arg" points to "var".
2734 * Set "*errp" to TRUE for an error, FALSE otherwise;
2735 * Return a pointer that holds the info. Null when there is an error.
2736 */
2737 void *
2738eval_for_line(arg, errp, nextcmdp, skip)
2739 char_u *arg;
2740 int *errp;
2741 char_u **nextcmdp;
2742 int skip;
2743{
Bram Moolenaar33570922005-01-25 22:26:29 +00002744 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002745 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002746 typval_T tv;
2747 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002748
2749 *errp = TRUE; /* default: there is an error */
2750
Bram Moolenaar33570922005-01-25 22:26:29 +00002751 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002752 if (fi == NULL)
2753 return NULL;
2754
2755 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2756 if (expr == NULL)
2757 return fi;
2758
2759 expr = skipwhite(expr);
2760 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2761 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002762 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002763 return fi;
2764 }
2765
2766 if (skip)
2767 ++emsg_skip;
2768 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2769 {
2770 *errp = FALSE;
2771 if (!skip)
2772 {
2773 l = tv.vval.v_list;
2774 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002775 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002776 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002777 clear_tv(&tv);
2778 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002779 else
2780 {
2781 fi->fi_list = l;
2782 list_add_watch(l, &fi->fi_lw);
2783 fi->fi_lw.lw_item = l->lv_first;
2784 }
2785 }
2786 }
2787 if (skip)
2788 --emsg_skip;
2789
2790 return fi;
2791}
2792
2793/*
2794 * Use the first item in a ":for" list. Advance to the next.
2795 * Assign the values to the variable (list). "arg" points to the first one.
2796 * Return TRUE when a valid item was found, FALSE when at end of list or
2797 * something wrong.
2798 */
2799 int
2800next_for_item(fi_void, arg)
2801 void *fi_void;
2802 char_u *arg;
2803{
Bram Moolenaar33570922005-01-25 22:26:29 +00002804 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002805 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002806 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002807
2808 item = fi->fi_lw.lw_item;
2809 if (item == NULL)
2810 result = FALSE;
2811 else
2812 {
2813 fi->fi_lw.lw_item = item->li_next;
2814 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2815 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2816 }
2817 return result;
2818}
2819
2820/*
2821 * Free the structure used to store info used by ":for".
2822 */
2823 void
2824free_for_info(fi_void)
2825 void *fi_void;
2826{
Bram Moolenaar33570922005-01-25 22:26:29 +00002827 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002828
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002829 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002830 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002831 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002832 list_unref(fi->fi_list);
2833 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002834 vim_free(fi);
2835}
2836
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2838
2839 void
2840set_context_for_expression(xp, arg, cmdidx)
2841 expand_T *xp;
2842 char_u *arg;
2843 cmdidx_T cmdidx;
2844{
2845 int got_eq = FALSE;
2846 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002847 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002849 if (cmdidx == CMD_let)
2850 {
2851 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002852 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002853 {
2854 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002855 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002856 {
2857 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00002858 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002859 if (vim_iswhite(*p))
2860 break;
2861 }
2862 return;
2863 }
2864 }
2865 else
2866 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2867 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 while ((xp->xp_pattern = vim_strpbrk(arg,
2869 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2870 {
2871 c = *xp->xp_pattern;
2872 if (c == '&')
2873 {
2874 c = xp->xp_pattern[1];
2875 if (c == '&')
2876 {
2877 ++xp->xp_pattern;
2878 xp->xp_context = cmdidx != CMD_let || got_eq
2879 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2880 }
2881 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002882 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002884 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2885 xp->xp_pattern += 2;
2886
2887 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 }
2889 else if (c == '$')
2890 {
2891 /* environment variable */
2892 xp->xp_context = EXPAND_ENV_VARS;
2893 }
2894 else if (c == '=')
2895 {
2896 got_eq = TRUE;
2897 xp->xp_context = EXPAND_EXPRESSION;
2898 }
2899 else if (c == '<'
2900 && xp->xp_context == EXPAND_FUNCTIONS
2901 && vim_strchr(xp->xp_pattern, '(') == NULL)
2902 {
2903 /* Function name can start with "<SNR>" */
2904 break;
2905 }
2906 else if (cmdidx != CMD_let || got_eq)
2907 {
2908 if (c == '"') /* string */
2909 {
2910 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2911 if (c == '\\' && xp->xp_pattern[1] != NUL)
2912 ++xp->xp_pattern;
2913 xp->xp_context = EXPAND_NOTHING;
2914 }
2915 else if (c == '\'') /* literal string */
2916 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002917 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2919 /* skip */ ;
2920 xp->xp_context = EXPAND_NOTHING;
2921 }
2922 else if (c == '|')
2923 {
2924 if (xp->xp_pattern[1] == '|')
2925 {
2926 ++xp->xp_pattern;
2927 xp->xp_context = EXPAND_EXPRESSION;
2928 }
2929 else
2930 xp->xp_context = EXPAND_COMMANDS;
2931 }
2932 else
2933 xp->xp_context = EXPAND_EXPRESSION;
2934 }
2935 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002936 /* Doesn't look like something valid, expand as an expression
2937 * anyway. */
2938 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 arg = xp->xp_pattern;
2940 if (*arg != NUL)
2941 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2942 /* skip */ ;
2943 }
2944 xp->xp_pattern = arg;
2945}
2946
2947#endif /* FEAT_CMDL_COMPL */
2948
2949/*
2950 * ":1,25call func(arg1, arg2)" function call.
2951 */
2952 void
2953ex_call(eap)
2954 exarg_T *eap;
2955{
2956 char_u *arg = eap->arg;
2957 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002959 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002961 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 linenr_T lnum;
2963 int doesrange;
2964 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002965 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002967 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
2968 vim_free(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002969 if (tofree == NULL)
2970 return;
2971
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002972 /* Increase refcount on dictionary, it could get deleted when evaluating
2973 * the arguments. */
2974 if (fudi.fd_dict != NULL)
2975 ++fudi.fd_dict->dv_refcount;
2976
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002977 /* If it is the name of a variable of type VAR_FUNC use its contents. */
2978 len = STRLEN(tofree);
2979 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980
Bram Moolenaar532c7802005-01-27 14:44:31 +00002981 /* Skip white space to allow ":call func ()". Not good, but required for
2982 * backward compatibility. */
2983 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002984 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985
2986 if (*startarg != '(')
2987 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00002988 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 goto end;
2990 }
2991
2992 /*
2993 * When skipping, evaluate the function once, to find the end of the
2994 * arguments.
2995 * When the function takes a range, this is discovered after the first
2996 * call, and the loop is broken.
2997 */
2998 if (eap->skip)
2999 {
3000 ++emsg_skip;
3001 lnum = eap->line2; /* do it once, also with an invalid range */
3002 }
3003 else
3004 lnum = eap->line1;
3005 for ( ; lnum <= eap->line2; ++lnum)
3006 {
3007 if (!eap->skip && eap->addr_count > 0)
3008 {
3009 curwin->w_cursor.lnum = lnum;
3010 curwin->w_cursor.col = 0;
3011 }
3012 arg = startarg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003013 if (get_func_tv(name, STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003014 eap->line1, eap->line2, &doesrange,
3015 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 {
3017 failed = TRUE;
3018 break;
3019 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003020 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 if (doesrange || eap->skip)
3022 break;
3023 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003024 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003025 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003026 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 if (aborting())
3028 break;
3029 }
3030 if (eap->skip)
3031 --emsg_skip;
3032
3033 if (!failed)
3034 {
3035 /* Check for trailing illegal characters and a following command. */
3036 if (!ends_excmd(*arg))
3037 {
3038 emsg_severe = TRUE;
3039 EMSG(_(e_trailing));
3040 }
3041 else
3042 eap->nextcmd = check_nextcmd(arg);
3043 }
3044
3045end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003046 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003047 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048}
3049
3050/*
3051 * ":unlet[!] var1 ... " command.
3052 */
3053 void
3054ex_unlet(eap)
3055 exarg_T *eap;
3056{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003057 ex_unletlock(eap, eap->arg, 0);
3058}
3059
3060/*
3061 * ":lockvar" and ":unlockvar" commands
3062 */
3063 void
3064ex_lockvar(eap)
3065 exarg_T *eap;
3066{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003068 int deep = 2;
3069
3070 if (eap->forceit)
3071 deep = -1;
3072 else if (vim_isdigit(*arg))
3073 {
3074 deep = getdigits(&arg);
3075 arg = skipwhite(arg);
3076 }
3077
3078 ex_unletlock(eap, arg, deep);
3079}
3080
3081/*
3082 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3083 */
3084 static void
3085ex_unletlock(eap, argstart, deep)
3086 exarg_T *eap;
3087 char_u *argstart;
3088 int deep;
3089{
3090 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003093 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094
3095 do
3096 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003097 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003098 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3099 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003100 if (lv.ll_name == NULL)
3101 error = TRUE; /* error but continue parsing */
3102 if (name_end == NULL || (!vim_iswhite(*name_end)
3103 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003105 if (name_end != NULL)
3106 {
3107 emsg_severe = TRUE;
3108 EMSG(_(e_trailing));
3109 }
3110 if (!(eap->skip || error))
3111 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 break;
3113 }
3114
3115 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003116 {
3117 if (eap->cmdidx == CMD_unlet)
3118 {
3119 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3120 error = TRUE;
3121 }
3122 else
3123 {
3124 if (do_lock_var(&lv, name_end, deep,
3125 eap->cmdidx == CMD_lockvar) == FAIL)
3126 error = TRUE;
3127 }
3128 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003130 if (!eap->skip)
3131 clear_lval(&lv);
3132
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 arg = skipwhite(name_end);
3134 } while (!ends_excmd(*arg));
3135
3136 eap->nextcmd = check_nextcmd(arg);
3137}
3138
Bram Moolenaar8c711452005-01-14 21:53:12 +00003139 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003140do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003141 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003142 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003143 int forceit;
3144{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003145 int ret = OK;
3146 int cc;
3147
3148 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003149 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003150 cc = *name_end;
3151 *name_end = NUL;
3152
3153 /* Normal name or expanded name. */
3154 if (check_changedtick(lp->ll_name))
3155 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003156 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003157 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003158 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003159 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003160 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3161 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003162 else if (lp->ll_range)
3163 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003164 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003165
3166 /* Delete a range of List items. */
3167 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3168 {
3169 li = lp->ll_li->li_next;
3170 listitem_remove(lp->ll_list, lp->ll_li);
3171 lp->ll_li = li;
3172 ++lp->ll_n1;
3173 }
3174 }
3175 else
3176 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003177 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003178 /* unlet a List item. */
3179 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003180 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003181 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003182 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003183 }
3184
3185 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003186}
3187
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188/*
3189 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003190 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 */
3192 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003193do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003195 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196{
Bram Moolenaar33570922005-01-25 22:26:29 +00003197 hashtab_T *ht;
3198 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003199 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200
Bram Moolenaar33570922005-01-25 22:26:29 +00003201 ht = find_var_ht(name, &varname);
3202 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003204 hi = hash_find(ht, varname);
3205 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003206 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003207 if (var_check_ro(HI2DI(hi)->di_flags, name))
3208 return FAIL;
3209 delete_var(ht, hi);
3210 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003213 if (forceit)
3214 return OK;
3215 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 return FAIL;
3217}
3218
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003219/*
3220 * Lock or unlock variable indicated by "lp".
3221 * "deep" is the levels to go (-1 for unlimited);
3222 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3223 */
3224 static int
3225do_lock_var(lp, name_end, deep, lock)
3226 lval_T *lp;
3227 char_u *name_end;
3228 int deep;
3229 int lock;
3230{
3231 int ret = OK;
3232 int cc;
3233 dictitem_T *di;
3234
3235 if (deep == 0) /* nothing to do */
3236 return OK;
3237
3238 if (lp->ll_tv == NULL)
3239 {
3240 cc = *name_end;
3241 *name_end = NUL;
3242
3243 /* Normal name or expanded name. */
3244 if (check_changedtick(lp->ll_name))
3245 ret = FAIL;
3246 else
3247 {
3248 di = find_var(lp->ll_name, NULL);
3249 if (di == NULL)
3250 ret = FAIL;
3251 else
3252 {
3253 if (lock)
3254 di->di_flags |= DI_FLAGS_LOCK;
3255 else
3256 di->di_flags &= ~DI_FLAGS_LOCK;
3257 item_lock(&di->di_tv, deep, lock);
3258 }
3259 }
3260 *name_end = cc;
3261 }
3262 else if (lp->ll_range)
3263 {
3264 listitem_T *li = lp->ll_li;
3265
3266 /* (un)lock a range of List items. */
3267 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3268 {
3269 item_lock(&li->li_tv, deep, lock);
3270 li = li->li_next;
3271 ++lp->ll_n1;
3272 }
3273 }
3274 else if (lp->ll_list != NULL)
3275 /* (un)lock a List item. */
3276 item_lock(&lp->ll_li->li_tv, deep, lock);
3277 else
3278 /* un(lock) a Dictionary item. */
3279 item_lock(&lp->ll_di->di_tv, deep, lock);
3280
3281 return ret;
3282}
3283
3284/*
3285 * Lock or unlock an item. "deep" is nr of levels to go.
3286 */
3287 static void
3288item_lock(tv, deep, lock)
3289 typval_T *tv;
3290 int deep;
3291 int lock;
3292{
3293 static int recurse = 0;
3294 list_T *l;
3295 listitem_T *li;
3296 dict_T *d;
3297 hashitem_T *hi;
3298 int todo;
3299
3300 if (recurse >= DICT_MAXNEST)
3301 {
3302 EMSG(_("E743: variable nested too deep for (un)lock"));
3303 return;
3304 }
3305 if (deep == 0)
3306 return;
3307 ++recurse;
3308
3309 /* lock/unlock the item itself */
3310 if (lock)
3311 tv->v_lock |= VAR_LOCKED;
3312 else
3313 tv->v_lock &= ~VAR_LOCKED;
3314
3315 switch (tv->v_type)
3316 {
3317 case VAR_LIST:
3318 if ((l = tv->vval.v_list) != NULL)
3319 {
3320 if (lock)
3321 l->lv_lock |= VAR_LOCKED;
3322 else
3323 l->lv_lock &= ~VAR_LOCKED;
3324 if (deep < 0 || deep > 1)
3325 /* recursive: lock/unlock the items the List contains */
3326 for (li = l->lv_first; li != NULL; li = li->li_next)
3327 item_lock(&li->li_tv, deep - 1, lock);
3328 }
3329 break;
3330 case VAR_DICT:
3331 if ((d = tv->vval.v_dict) != NULL)
3332 {
3333 if (lock)
3334 d->dv_lock |= VAR_LOCKED;
3335 else
3336 d->dv_lock &= ~VAR_LOCKED;
3337 if (deep < 0 || deep > 1)
3338 {
3339 /* recursive: lock/unlock the items the List contains */
3340 todo = d->dv_hashtab.ht_used;
3341 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3342 {
3343 if (!HASHITEM_EMPTY(hi))
3344 {
3345 --todo;
3346 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3347 }
3348 }
3349 }
3350 }
3351 }
3352 --recurse;
3353}
3354
Bram Moolenaara40058a2005-07-11 22:42:07 +00003355/*
3356 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3357 * it refers to a List or Dictionary that is locked.
3358 */
3359 static int
3360tv_islocked(tv)
3361 typval_T *tv;
3362{
3363 return (tv->v_lock & VAR_LOCKED)
3364 || (tv->v_type == VAR_LIST
3365 && tv->vval.v_list != NULL
3366 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3367 || (tv->v_type == VAR_DICT
3368 && tv->vval.v_dict != NULL
3369 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3370}
3371
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3373/*
3374 * Delete all "menutrans_" variables.
3375 */
3376 void
3377del_menutrans_vars()
3378{
Bram Moolenaar33570922005-01-25 22:26:29 +00003379 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003380 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381
Bram Moolenaar33570922005-01-25 22:26:29 +00003382 hash_lock(&globvarht);
3383 todo = globvarht.ht_used;
3384 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003385 {
3386 if (!HASHITEM_EMPTY(hi))
3387 {
3388 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003389 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3390 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003391 }
3392 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003393 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394}
3395#endif
3396
3397#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3398
3399/*
3400 * Local string buffer for the next two functions to store a variable name
3401 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3402 * get_user_var_name().
3403 */
3404
3405static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3406
3407static char_u *varnamebuf = NULL;
3408static int varnamebuflen = 0;
3409
3410/*
3411 * Function to concatenate a prefix and a variable name.
3412 */
3413 static char_u *
3414cat_prefix_varname(prefix, name)
3415 int prefix;
3416 char_u *name;
3417{
3418 int len;
3419
3420 len = (int)STRLEN(name) + 3;
3421 if (len > varnamebuflen)
3422 {
3423 vim_free(varnamebuf);
3424 len += 10; /* some additional space */
3425 varnamebuf = alloc(len);
3426 if (varnamebuf == NULL)
3427 {
3428 varnamebuflen = 0;
3429 return NULL;
3430 }
3431 varnamebuflen = len;
3432 }
3433 *varnamebuf = prefix;
3434 varnamebuf[1] = ':';
3435 STRCPY(varnamebuf + 2, name);
3436 return varnamebuf;
3437}
3438
3439/*
3440 * Function given to ExpandGeneric() to obtain the list of user defined
3441 * (global/buffer/window/built-in) variable names.
3442 */
3443/*ARGSUSED*/
3444 char_u *
3445get_user_var_name(xp, idx)
3446 expand_T *xp;
3447 int idx;
3448{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003449 static long_u gdone;
3450 static long_u bdone;
3451 static long_u wdone;
3452 static int vidx;
3453 static hashitem_T *hi;
3454 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455
3456 if (idx == 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00003457 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00003458
3459 /* Global variables */
3460 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003462 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003463 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003464 else
3465 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003466 while (HASHITEM_EMPTY(hi))
3467 ++hi;
3468 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3469 return cat_prefix_varname('g', hi->hi_key);
3470 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003472
3473 /* b: variables */
3474 ht = &curbuf->b_vars.dv_hashtab;
3475 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003477 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003478 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003479 else
3480 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003481 while (HASHITEM_EMPTY(hi))
3482 ++hi;
3483 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003485 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003487 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 return (char_u *)"b:changedtick";
3489 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003490
3491 /* w: variables */
3492 ht = &curwin->w_vars.dv_hashtab;
3493 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003495 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003496 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003497 else
3498 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003499 while (HASHITEM_EMPTY(hi))
3500 ++hi;
3501 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003503
3504 /* v: variables */
3505 if (vidx < VV_LEN)
3506 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507
3508 vim_free(varnamebuf);
3509 varnamebuf = NULL;
3510 varnamebuflen = 0;
3511 return NULL;
3512}
3513
3514#endif /* FEAT_CMDL_COMPL */
3515
3516/*
3517 * types for expressions.
3518 */
3519typedef enum
3520{
3521 TYPE_UNKNOWN = 0
3522 , TYPE_EQUAL /* == */
3523 , TYPE_NEQUAL /* != */
3524 , TYPE_GREATER /* > */
3525 , TYPE_GEQUAL /* >= */
3526 , TYPE_SMALLER /* < */
3527 , TYPE_SEQUAL /* <= */
3528 , TYPE_MATCH /* =~ */
3529 , TYPE_NOMATCH /* !~ */
3530} exptype_T;
3531
3532/*
3533 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003534 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3536 */
3537
3538/*
3539 * Handle zero level expression.
3540 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003541 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 * Return OK or FAIL.
3543 */
3544 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003545eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003547 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 char_u **nextcmd;
3549 int evaluate;
3550{
3551 int ret;
3552 char_u *p;
3553
3554 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003555 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 if (ret == FAIL || !ends_excmd(*p))
3557 {
3558 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003559 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 /*
3561 * Report the invalid expression unless the expression evaluation has
3562 * been cancelled due to an aborting error, an interrupt, or an
3563 * exception.
3564 */
3565 if (!aborting())
3566 EMSG2(_(e_invexpr2), arg);
3567 ret = FAIL;
3568 }
3569 if (nextcmd != NULL)
3570 *nextcmd = check_nextcmd(p);
3571
3572 return ret;
3573}
3574
3575/*
3576 * Handle top level expression:
3577 * expr1 ? expr0 : expr0
3578 *
3579 * "arg" must point to the first non-white of the expression.
3580 * "arg" is advanced to the next non-white after the recognized expression.
3581 *
3582 * Return OK or FAIL.
3583 */
3584 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003585eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003587 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 int evaluate;
3589{
3590 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003591 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592
3593 /*
3594 * Get the first variable.
3595 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003596 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 return FAIL;
3598
3599 if ((*arg)[0] == '?')
3600 {
3601 result = FALSE;
3602 if (evaluate)
3603 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003604 int error = FALSE;
3605
3606 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003608 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003609 if (error)
3610 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 }
3612
3613 /*
3614 * Get the second variable.
3615 */
3616 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003617 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 return FAIL;
3619
3620 /*
3621 * Check for the ":".
3622 */
3623 if ((*arg)[0] != ':')
3624 {
3625 EMSG(_("E109: Missing ':' after '?'"));
3626 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003627 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 return FAIL;
3629 }
3630
3631 /*
3632 * Get the third variable.
3633 */
3634 *arg = skipwhite(*arg + 1);
3635 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3636 {
3637 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003638 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 return FAIL;
3640 }
3641 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003642 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 }
3644
3645 return OK;
3646}
3647
3648/*
3649 * Handle first level expression:
3650 * expr2 || expr2 || expr2 logical OR
3651 *
3652 * "arg" must point to the first non-white of the expression.
3653 * "arg" is advanced to the next non-white after the recognized expression.
3654 *
3655 * Return OK or FAIL.
3656 */
3657 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003658eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003660 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 int evaluate;
3662{
Bram Moolenaar33570922005-01-25 22:26:29 +00003663 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 long result;
3665 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003666 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667
3668 /*
3669 * Get the first variable.
3670 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003671 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 return FAIL;
3673
3674 /*
3675 * Repeat until there is no following "||".
3676 */
3677 first = TRUE;
3678 result = FALSE;
3679 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3680 {
3681 if (evaluate && first)
3682 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003683 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003685 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003686 if (error)
3687 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 first = FALSE;
3689 }
3690
3691 /*
3692 * Get the second variable.
3693 */
3694 *arg = skipwhite(*arg + 2);
3695 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3696 return FAIL;
3697
3698 /*
3699 * Compute the result.
3700 */
3701 if (evaluate && !result)
3702 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003703 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003705 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003706 if (error)
3707 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 }
3709 if (evaluate)
3710 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003711 rettv->v_type = VAR_NUMBER;
3712 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 }
3714 }
3715
3716 return OK;
3717}
3718
3719/*
3720 * Handle second level expression:
3721 * expr3 && expr3 && expr3 logical AND
3722 *
3723 * "arg" must point to the first non-white of the expression.
3724 * "arg" is advanced to the next non-white after the recognized expression.
3725 *
3726 * Return OK or FAIL.
3727 */
3728 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003729eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003731 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 int evaluate;
3733{
Bram Moolenaar33570922005-01-25 22:26:29 +00003734 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 long result;
3736 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003737 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738
3739 /*
3740 * Get the first variable.
3741 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003742 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 return FAIL;
3744
3745 /*
3746 * Repeat until there is no following "&&".
3747 */
3748 first = TRUE;
3749 result = TRUE;
3750 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3751 {
3752 if (evaluate && first)
3753 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003754 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003756 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003757 if (error)
3758 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 first = FALSE;
3760 }
3761
3762 /*
3763 * Get the second variable.
3764 */
3765 *arg = skipwhite(*arg + 2);
3766 if (eval4(arg, &var2, evaluate && result) == FAIL)
3767 return FAIL;
3768
3769 /*
3770 * Compute the result.
3771 */
3772 if (evaluate && result)
3773 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003774 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003776 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003777 if (error)
3778 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 }
3780 if (evaluate)
3781 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003782 rettv->v_type = VAR_NUMBER;
3783 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 }
3785 }
3786
3787 return OK;
3788}
3789
3790/*
3791 * Handle third level expression:
3792 * var1 == var2
3793 * var1 =~ var2
3794 * var1 != var2
3795 * var1 !~ var2
3796 * var1 > var2
3797 * var1 >= var2
3798 * var1 < var2
3799 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003800 * var1 is var2
3801 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 *
3803 * "arg" must point to the first non-white of the expression.
3804 * "arg" is advanced to the next non-white after the recognized expression.
3805 *
3806 * Return OK or FAIL.
3807 */
3808 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003809eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003811 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 int evaluate;
3813{
Bram Moolenaar33570922005-01-25 22:26:29 +00003814 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 char_u *p;
3816 int i;
3817 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003818 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 int len = 2;
3820 long n1, n2;
3821 char_u *s1, *s2;
3822 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3823 regmatch_T regmatch;
3824 int ic;
3825 char_u *save_cpo;
3826
3827 /*
3828 * Get the first variable.
3829 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003830 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 return FAIL;
3832
3833 p = *arg;
3834 switch (p[0])
3835 {
3836 case '=': if (p[1] == '=')
3837 type = TYPE_EQUAL;
3838 else if (p[1] == '~')
3839 type = TYPE_MATCH;
3840 break;
3841 case '!': if (p[1] == '=')
3842 type = TYPE_NEQUAL;
3843 else if (p[1] == '~')
3844 type = TYPE_NOMATCH;
3845 break;
3846 case '>': if (p[1] != '=')
3847 {
3848 type = TYPE_GREATER;
3849 len = 1;
3850 }
3851 else
3852 type = TYPE_GEQUAL;
3853 break;
3854 case '<': if (p[1] != '=')
3855 {
3856 type = TYPE_SMALLER;
3857 len = 1;
3858 }
3859 else
3860 type = TYPE_SEQUAL;
3861 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003862 case 'i': if (p[1] == 's')
3863 {
3864 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3865 len = 5;
3866 if (!vim_isIDc(p[len]))
3867 {
3868 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3869 type_is = TRUE;
3870 }
3871 }
3872 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 }
3874
3875 /*
3876 * If there is a comparitive operator, use it.
3877 */
3878 if (type != TYPE_UNKNOWN)
3879 {
3880 /* extra question mark appended: ignore case */
3881 if (p[len] == '?')
3882 {
3883 ic = TRUE;
3884 ++len;
3885 }
3886 /* extra '#' appended: match case */
3887 else if (p[len] == '#')
3888 {
3889 ic = FALSE;
3890 ++len;
3891 }
3892 /* nothing appened: use 'ignorecase' */
3893 else
3894 ic = p_ic;
3895
3896 /*
3897 * Get the second variable.
3898 */
3899 *arg = skipwhite(p + len);
3900 if (eval5(arg, &var2, evaluate) == FAIL)
3901 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003902 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 return FAIL;
3904 }
3905
3906 if (evaluate)
3907 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003908 if (type_is && rettv->v_type != var2.v_type)
3909 {
3910 /* For "is" a different type always means FALSE, for "notis"
3911 * it means TRUE. */
3912 n1 = (type == TYPE_NEQUAL);
3913 }
3914 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
3915 {
3916 if (type_is)
3917 {
3918 n1 = (rettv->v_type == var2.v_type
3919 && rettv->vval.v_list == var2.vval.v_list);
3920 if (type == TYPE_NEQUAL)
3921 n1 = !n1;
3922 }
3923 else if (rettv->v_type != var2.v_type
3924 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3925 {
3926 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003927 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003928 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003929 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003930 clear_tv(rettv);
3931 clear_tv(&var2);
3932 return FAIL;
3933 }
3934 else
3935 {
3936 /* Compare two Lists for being equal or unequal. */
3937 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
3938 if (type == TYPE_NEQUAL)
3939 n1 = !n1;
3940 }
3941 }
3942
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003943 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
3944 {
3945 if (type_is)
3946 {
3947 n1 = (rettv->v_type == var2.v_type
3948 && rettv->vval.v_dict == var2.vval.v_dict);
3949 if (type == TYPE_NEQUAL)
3950 n1 = !n1;
3951 }
3952 else if (rettv->v_type != var2.v_type
3953 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3954 {
3955 if (rettv->v_type != var2.v_type)
3956 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
3957 else
3958 EMSG(_("E736: Invalid operation for Dictionary"));
3959 clear_tv(rettv);
3960 clear_tv(&var2);
3961 return FAIL;
3962 }
3963 else
3964 {
3965 /* Compare two Dictionaries for being equal or unequal. */
3966 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
3967 if (type == TYPE_NEQUAL)
3968 n1 = !n1;
3969 }
3970 }
3971
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003972 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
3973 {
3974 if (rettv->v_type != var2.v_type
3975 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3976 {
3977 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003978 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003979 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003980 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003981 clear_tv(rettv);
3982 clear_tv(&var2);
3983 return FAIL;
3984 }
3985 else
3986 {
3987 /* Compare two Funcrefs for being equal or unequal. */
3988 if (rettv->vval.v_string == NULL
3989 || var2.vval.v_string == NULL)
3990 n1 = FALSE;
3991 else
3992 n1 = STRCMP(rettv->vval.v_string,
3993 var2.vval.v_string) == 0;
3994 if (type == TYPE_NEQUAL)
3995 n1 = !n1;
3996 }
3997 }
3998
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 /*
4000 * If one of the two variables is a number, compare as a number.
4001 * When using "=~" or "!~", always compare as string.
4002 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004003 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4005 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004006 n1 = get_tv_number(rettv);
4007 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 switch (type)
4009 {
4010 case TYPE_EQUAL: n1 = (n1 == n2); break;
4011 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4012 case TYPE_GREATER: n1 = (n1 > n2); break;
4013 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4014 case TYPE_SMALLER: n1 = (n1 < n2); break;
4015 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4016 case TYPE_UNKNOWN:
4017 case TYPE_MATCH:
4018 case TYPE_NOMATCH: break; /* avoid gcc warning */
4019 }
4020 }
4021 else
4022 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004023 s1 = get_tv_string_buf(rettv, buf1);
4024 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4026 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4027 else
4028 i = 0;
4029 n1 = FALSE;
4030 switch (type)
4031 {
4032 case TYPE_EQUAL: n1 = (i == 0); break;
4033 case TYPE_NEQUAL: n1 = (i != 0); break;
4034 case TYPE_GREATER: n1 = (i > 0); break;
4035 case TYPE_GEQUAL: n1 = (i >= 0); break;
4036 case TYPE_SMALLER: n1 = (i < 0); break;
4037 case TYPE_SEQUAL: n1 = (i <= 0); break;
4038
4039 case TYPE_MATCH:
4040 case TYPE_NOMATCH:
4041 /* avoid 'l' flag in 'cpoptions' */
4042 save_cpo = p_cpo;
4043 p_cpo = (char_u *)"";
4044 regmatch.regprog = vim_regcomp(s2,
4045 RE_MAGIC + RE_STRING);
4046 regmatch.rm_ic = ic;
4047 if (regmatch.regprog != NULL)
4048 {
4049 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4050 vim_free(regmatch.regprog);
4051 if (type == TYPE_NOMATCH)
4052 n1 = !n1;
4053 }
4054 p_cpo = save_cpo;
4055 break;
4056
4057 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4058 }
4059 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004060 clear_tv(rettv);
4061 clear_tv(&var2);
4062 rettv->v_type = VAR_NUMBER;
4063 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 }
4065 }
4066
4067 return OK;
4068}
4069
4070/*
4071 * Handle fourth level expression:
4072 * + number addition
4073 * - number subtraction
4074 * . string concatenation
4075 *
4076 * "arg" must point to the first non-white of the expression.
4077 * "arg" is advanced to the next non-white after the recognized expression.
4078 *
4079 * Return OK or FAIL.
4080 */
4081 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004082eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004084 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 int evaluate;
4086{
Bram Moolenaar33570922005-01-25 22:26:29 +00004087 typval_T var2;
4088 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 int op;
4090 long n1, n2;
4091 char_u *s1, *s2;
4092 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4093 char_u *p;
4094
4095 /*
4096 * Get the first variable.
4097 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004098 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 return FAIL;
4100
4101 /*
4102 * Repeat computing, until no '+', '-' or '.' is following.
4103 */
4104 for (;;)
4105 {
4106 op = **arg;
4107 if (op != '+' && op != '-' && op != '.')
4108 break;
4109
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004110 if (op != '+' || rettv->v_type != VAR_LIST)
4111 {
4112 /* For "list + ...", an illegal use of the first operand as
4113 * a number cannot be determined before evaluating the 2nd
4114 * operand: if this is also a list, all is ok.
4115 * For "something . ...", "something - ..." or "non-list + ...",
4116 * we know that the first operand needs to be a string or number
4117 * without evaluating the 2nd operand. So check before to avoid
4118 * side effects after an error. */
4119 if (evaluate && get_tv_string_chk(rettv) == NULL)
4120 {
4121 clear_tv(rettv);
4122 return FAIL;
4123 }
4124 }
4125
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 /*
4127 * Get the second variable.
4128 */
4129 *arg = skipwhite(*arg + 1);
4130 if (eval6(arg, &var2, evaluate) == FAIL)
4131 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004132 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 return FAIL;
4134 }
4135
4136 if (evaluate)
4137 {
4138 /*
4139 * Compute the result.
4140 */
4141 if (op == '.')
4142 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004143 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4144 s2 = get_tv_string_buf_chk(&var2, buf2);
4145 if (s2 == NULL) /* type error ? */
4146 {
4147 clear_tv(rettv);
4148 clear_tv(&var2);
4149 return FAIL;
4150 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004151 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004152 clear_tv(rettv);
4153 rettv->v_type = VAR_STRING;
4154 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004156 else if (op == '+' && rettv->v_type == VAR_LIST
4157 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004158 {
4159 /* concatenate Lists */
4160 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4161 &var3) == FAIL)
4162 {
4163 clear_tv(rettv);
4164 clear_tv(&var2);
4165 return FAIL;
4166 }
4167 clear_tv(rettv);
4168 *rettv = var3;
4169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 else
4171 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004172 int error = FALSE;
4173
4174 n1 = get_tv_number_chk(rettv, &error);
4175 if (error)
4176 {
4177 /* This can only happen for "list + non-list".
4178 * For "non-list + ..." or "something - ...", we returned
4179 * before evaluating the 2nd operand. */
4180 clear_tv(rettv);
4181 return FAIL;
4182 }
4183 n2 = get_tv_number_chk(&var2, &error);
4184 if (error)
4185 {
4186 clear_tv(rettv);
4187 clear_tv(&var2);
4188 return FAIL;
4189 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004190 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 if (op == '+')
4192 n1 = n1 + n2;
4193 else
4194 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004195 rettv->v_type = VAR_NUMBER;
4196 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004198 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 }
4200 }
4201 return OK;
4202}
4203
4204/*
4205 * Handle fifth level expression:
4206 * * number multiplication
4207 * / number division
4208 * % number modulo
4209 *
4210 * "arg" must point to the first non-white of the expression.
4211 * "arg" is advanced to the next non-white after the recognized expression.
4212 *
4213 * Return OK or FAIL.
4214 */
4215 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004216eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004218 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 int evaluate;
4220{
Bram Moolenaar33570922005-01-25 22:26:29 +00004221 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 int op;
4223 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004224 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225
4226 /*
4227 * Get the first variable.
4228 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004229 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 return FAIL;
4231
4232 /*
4233 * Repeat computing, until no '*', '/' or '%' is following.
4234 */
4235 for (;;)
4236 {
4237 op = **arg;
4238 if (op != '*' && op != '/' && op != '%')
4239 break;
4240
4241 if (evaluate)
4242 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004243 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004244 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004245 if (error)
4246 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 }
4248 else
4249 n1 = 0;
4250
4251 /*
4252 * Get the second variable.
4253 */
4254 *arg = skipwhite(*arg + 1);
4255 if (eval7(arg, &var2, evaluate) == FAIL)
4256 return FAIL;
4257
4258 if (evaluate)
4259 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004260 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004261 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004262 if (error)
4263 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264
4265 /*
4266 * Compute the result.
4267 */
4268 if (op == '*')
4269 n1 = n1 * n2;
4270 else if (op == '/')
4271 {
4272 if (n2 == 0) /* give an error message? */
4273 n1 = 0x7fffffffL;
4274 else
4275 n1 = n1 / n2;
4276 }
4277 else
4278 {
4279 if (n2 == 0) /* give an error message? */
4280 n1 = 0;
4281 else
4282 n1 = n1 % n2;
4283 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004284 rettv->v_type = VAR_NUMBER;
4285 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 }
4287 }
4288
4289 return OK;
4290}
4291
4292/*
4293 * Handle sixth level expression:
4294 * number number constant
4295 * "string" string contstant
4296 * 'string' literal string contstant
4297 * &option-name option value
4298 * @r register contents
4299 * identifier variable value
4300 * function() function call
4301 * $VAR environment variable
4302 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004303 * [expr, expr] List
4304 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 *
4306 * Also handle:
4307 * ! in front logical NOT
4308 * - in front unary minus
4309 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004310 * trailing [] subscript in String or List
4311 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 *
4313 * "arg" must point to the first non-white of the expression.
4314 * "arg" is advanced to the next non-white after the recognized expression.
4315 *
4316 * Return OK or FAIL.
4317 */
4318 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004319eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004321 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 int evaluate;
4323{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 long n;
4325 int len;
4326 char_u *s;
4327 int val;
4328 char_u *start_leader, *end_leader;
4329 int ret = OK;
4330 char_u *alias;
4331
4332 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004333 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004334 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004336 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337
4338 /*
4339 * Skip '!' and '-' characters. They are handled later.
4340 */
4341 start_leader = *arg;
4342 while (**arg == '!' || **arg == '-' || **arg == '+')
4343 *arg = skipwhite(*arg + 1);
4344 end_leader = *arg;
4345
4346 switch (**arg)
4347 {
4348 /*
4349 * Number constant.
4350 */
4351 case '0':
4352 case '1':
4353 case '2':
4354 case '3':
4355 case '4':
4356 case '5':
4357 case '6':
4358 case '7':
4359 case '8':
4360 case '9':
4361 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4362 *arg += len;
4363 if (evaluate)
4364 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004365 rettv->v_type = VAR_NUMBER;
4366 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 }
4368 break;
4369
4370 /*
4371 * String constant: "string".
4372 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004373 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 break;
4375
4376 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004377 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004379 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004380 break;
4381
4382 /*
4383 * List: [expr, expr]
4384 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004385 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 break;
4387
4388 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004389 * Dictionary: {key: val, key: val}
4390 */
4391 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4392 break;
4393
4394 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004395 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004397 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 break;
4399
4400 /*
4401 * Environment variable: $VAR.
4402 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004403 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 break;
4405
4406 /*
4407 * Register contents: @r.
4408 */
4409 case '@': ++*arg;
4410 if (evaluate)
4411 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004412 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004413 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 }
4415 if (**arg != NUL)
4416 ++*arg;
4417 break;
4418
4419 /*
4420 * nested expression: (expression).
4421 */
4422 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004423 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 if (**arg == ')')
4425 ++*arg;
4426 else if (ret == OK)
4427 {
4428 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004429 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 ret = FAIL;
4431 }
4432 break;
4433
Bram Moolenaar8c711452005-01-14 21:53:12 +00004434 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 break;
4436 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004437
4438 if (ret == NOTDONE)
4439 {
4440 /*
4441 * Must be a variable or function name.
4442 * Can also be a curly-braces kind of name: {expr}.
4443 */
4444 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004445 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004446 if (alias != NULL)
4447 s = alias;
4448
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004449 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004450 ret = FAIL;
4451 else
4452 {
4453 if (**arg == '(') /* recursive! */
4454 {
4455 /* If "s" is the name of a variable of type VAR_FUNC
4456 * use its contents. */
4457 s = deref_func_name(s, &len);
4458
4459 /* Invoke the function. */
4460 ret = get_func_tv(s, len, rettv, arg,
4461 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004462 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004463 /* Stop the expression evaluation when immediately
4464 * aborting on error, or when an interrupt occurred or
4465 * an exception was thrown but not caught. */
4466 if (aborting())
4467 {
4468 if (ret == OK)
4469 clear_tv(rettv);
4470 ret = FAIL;
4471 }
4472 }
4473 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004474 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004475 else
4476 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004477 }
4478
4479 if (alias != NULL)
4480 vim_free(alias);
4481 }
4482
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 *arg = skipwhite(*arg);
4484
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004485 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4486 * expr(expr). */
4487 if (ret == OK)
4488 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489
4490 /*
4491 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4492 */
4493 if (ret == OK && evaluate && end_leader > start_leader)
4494 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004495 int error = FALSE;
4496
4497 val = get_tv_number_chk(rettv, &error);
4498 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004500 clear_tv(rettv);
4501 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004503 else
4504 {
4505 while (end_leader > start_leader)
4506 {
4507 --end_leader;
4508 if (*end_leader == '!')
4509 val = !val;
4510 else if (*end_leader == '-')
4511 val = -val;
4512 }
4513 clear_tv(rettv);
4514 rettv->v_type = VAR_NUMBER;
4515 rettv->vval.v_number = val;
4516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 }
4518
4519 return ret;
4520}
4521
4522/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004523 * Evaluate an "[expr]" or "[expr:expr]" index.
4524 * "*arg" points to the '['.
4525 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4526 */
4527 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004528eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004529 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004530 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004531 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004532 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004533{
4534 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004535 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004536 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004537 long len = -1;
4538 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004539 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004540 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004541
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004542 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004543 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004544 if (verbose)
4545 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004546 return FAIL;
4547 }
4548
Bram Moolenaar8c711452005-01-14 21:53:12 +00004549 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004550 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004551 /*
4552 * dict.name
4553 */
4554 key = *arg + 1;
4555 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4556 ;
4557 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004558 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004559 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004560 }
4561 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004562 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004563 /*
4564 * something[idx]
4565 *
4566 * Get the (first) variable from inside the [].
4567 */
4568 *arg = skipwhite(*arg + 1);
4569 if (**arg == ':')
4570 empty1 = TRUE;
4571 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4572 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004573 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4574 {
4575 /* not a number or string */
4576 clear_tv(&var1);
4577 return FAIL;
4578 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004579
4580 /*
4581 * Get the second variable from inside the [:].
4582 */
4583 if (**arg == ':')
4584 {
4585 range = TRUE;
4586 *arg = skipwhite(*arg + 1);
4587 if (**arg == ']')
4588 empty2 = TRUE;
4589 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4590 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004591 if (!empty1)
4592 clear_tv(&var1);
4593 return FAIL;
4594 }
4595 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4596 {
4597 /* not a number or string */
4598 if (!empty1)
4599 clear_tv(&var1);
4600 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004601 return FAIL;
4602 }
4603 }
4604
4605 /* Check for the ']'. */
4606 if (**arg != ']')
4607 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004608 if (verbose)
4609 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004610 clear_tv(&var1);
4611 if (range)
4612 clear_tv(&var2);
4613 return FAIL;
4614 }
4615 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004616 }
4617
4618 if (evaluate)
4619 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004620 n1 = 0;
4621 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004622 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004623 n1 = get_tv_number(&var1);
4624 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004625 }
4626 if (range)
4627 {
4628 if (empty2)
4629 n2 = -1;
4630 else
4631 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004632 n2 = get_tv_number(&var2);
4633 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004634 }
4635 }
4636
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004637 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004638 {
4639 case VAR_NUMBER:
4640 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004641 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004642 len = (long)STRLEN(s);
4643 if (range)
4644 {
4645 /* The resulting variable is a substring. If the indexes
4646 * are out of range the result is empty. */
4647 if (n1 < 0)
4648 {
4649 n1 = len + n1;
4650 if (n1 < 0)
4651 n1 = 0;
4652 }
4653 if (n2 < 0)
4654 n2 = len + n2;
4655 else if (n2 >= len)
4656 n2 = len;
4657 if (n1 >= len || n2 < 0 || n1 > n2)
4658 s = NULL;
4659 else
4660 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4661 }
4662 else
4663 {
4664 /* The resulting variable is a string of a single
4665 * character. If the index is too big or negative the
4666 * result is empty. */
4667 if (n1 >= len || n1 < 0)
4668 s = NULL;
4669 else
4670 s = vim_strnsave(s + n1, 1);
4671 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004672 clear_tv(rettv);
4673 rettv->v_type = VAR_STRING;
4674 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004675 break;
4676
4677 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004678 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004679 if (n1 < 0)
4680 n1 = len + n1;
4681 if (!empty1 && (n1 < 0 || n1 >= len))
4682 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004683 if (verbose)
4684 EMSGN(_(e_listidx), n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004685 return FAIL;
4686 }
4687 if (range)
4688 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004689 list_T *l;
4690 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004691
4692 if (n2 < 0)
4693 n2 = len + n2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004694 if (!empty2 && (n2 < 0 || n2 >= len || n2 + 1 < n1))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004695 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004696 if (verbose)
4697 EMSGN(_(e_listidx), n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004698 return FAIL;
4699 }
4700 l = list_alloc();
4701 if (l == NULL)
4702 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004703 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004704 n1 <= n2; ++n1)
4705 {
4706 if (list_append_tv(l, &item->li_tv) == FAIL)
4707 {
4708 list_free(l);
4709 return FAIL;
4710 }
4711 item = item->li_next;
4712 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004713 clear_tv(rettv);
4714 rettv->v_type = VAR_LIST;
4715 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004716 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004717 }
4718 else
4719 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004720 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv,
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004721 &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004722 clear_tv(rettv);
4723 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004724 }
4725 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004726
4727 case VAR_DICT:
4728 if (range)
4729 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004730 if (verbose)
4731 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004732 if (len == -1)
4733 clear_tv(&var1);
4734 return FAIL;
4735 }
4736 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004737 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004738
4739 if (len == -1)
4740 {
4741 key = get_tv_string(&var1);
4742 if (*key == NUL)
4743 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004744 if (verbose)
4745 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004746 clear_tv(&var1);
4747 return FAIL;
4748 }
4749 }
4750
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004751 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004752
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004753 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004754 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004755 if (len == -1)
4756 clear_tv(&var1);
4757 if (item == NULL)
4758 return FAIL;
4759
4760 copy_tv(&item->di_tv, &var1);
4761 clear_tv(rettv);
4762 *rettv = var1;
4763 }
4764 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004765 }
4766 }
4767
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004768 return OK;
4769}
4770
4771/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 * Get an option value.
4773 * "arg" points to the '&' or '+' before the option name.
4774 * "arg" is advanced to character after the option name.
4775 * Return OK or FAIL.
4776 */
4777 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004778get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004780 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 int evaluate;
4782{
4783 char_u *option_end;
4784 long numval;
4785 char_u *stringval;
4786 int opt_type;
4787 int c;
4788 int working = (**arg == '+'); /* has("+option") */
4789 int ret = OK;
4790 int opt_flags;
4791
4792 /*
4793 * Isolate the option name and find its value.
4794 */
4795 option_end = find_option_end(arg, &opt_flags);
4796 if (option_end == NULL)
4797 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004798 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 EMSG2(_("E112: Option name missing: %s"), *arg);
4800 return FAIL;
4801 }
4802
4803 if (!evaluate)
4804 {
4805 *arg = option_end;
4806 return OK;
4807 }
4808
4809 c = *option_end;
4810 *option_end = NUL;
4811 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004812 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813
4814 if (opt_type == -3) /* invalid name */
4815 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004816 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 EMSG2(_("E113: Unknown option: %s"), *arg);
4818 ret = FAIL;
4819 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004820 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 {
4822 if (opt_type == -2) /* hidden string option */
4823 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004824 rettv->v_type = VAR_STRING;
4825 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 }
4827 else if (opt_type == -1) /* hidden number option */
4828 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004829 rettv->v_type = VAR_NUMBER;
4830 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 }
4832 else if (opt_type == 1) /* number option */
4833 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004834 rettv->v_type = VAR_NUMBER;
4835 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 }
4837 else /* string option */
4838 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004839 rettv->v_type = VAR_STRING;
4840 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 }
4842 }
4843 else if (working && (opt_type == -2 || opt_type == -1))
4844 ret = FAIL;
4845
4846 *option_end = c; /* put back for error messages */
4847 *arg = option_end;
4848
4849 return ret;
4850}
4851
4852/*
4853 * Allocate a variable for a string constant.
4854 * Return OK or FAIL.
4855 */
4856 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004857get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004859 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 int evaluate;
4861{
4862 char_u *p;
4863 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 int extra = 0;
4865
4866 /*
4867 * Find the end of the string, skipping backslashed characters.
4868 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004869 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 {
4871 if (*p == '\\' && p[1] != NUL)
4872 {
4873 ++p;
4874 /* A "\<x>" form occupies at least 4 characters, and produces up
4875 * to 6 characters: reserve space for 2 extra */
4876 if (*p == '<')
4877 extra += 2;
4878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 }
4880
4881 if (*p != '"')
4882 {
4883 EMSG2(_("E114: Missing quote: %s"), *arg);
4884 return FAIL;
4885 }
4886
4887 /* If only parsing, set *arg and return here */
4888 if (!evaluate)
4889 {
4890 *arg = p + 1;
4891 return OK;
4892 }
4893
4894 /*
4895 * Copy the string into allocated memory, handling backslashed
4896 * characters.
4897 */
4898 name = alloc((unsigned)(p - *arg + extra));
4899 if (name == NULL)
4900 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004901 rettv->v_type = VAR_STRING;
4902 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903
Bram Moolenaar8c711452005-01-14 21:53:12 +00004904 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 {
4906 if (*p == '\\')
4907 {
4908 switch (*++p)
4909 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004910 case 'b': *name++ = BS; ++p; break;
4911 case 'e': *name++ = ESC; ++p; break;
4912 case 'f': *name++ = FF; ++p; break;
4913 case 'n': *name++ = NL; ++p; break;
4914 case 'r': *name++ = CAR; ++p; break;
4915 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916
4917 case 'X': /* hex: "\x1", "\x12" */
4918 case 'x':
4919 case 'u': /* Unicode: "\u0023" */
4920 case 'U':
4921 if (vim_isxdigit(p[1]))
4922 {
4923 int n, nr;
4924 int c = toupper(*p);
4925
4926 if (c == 'X')
4927 n = 2;
4928 else
4929 n = 4;
4930 nr = 0;
4931 while (--n >= 0 && vim_isxdigit(p[1]))
4932 {
4933 ++p;
4934 nr = (nr << 4) + hex2nr(*p);
4935 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004936 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937#ifdef FEAT_MBYTE
4938 /* For "\u" store the number according to
4939 * 'encoding'. */
4940 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00004941 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 else
4943#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00004944 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 break;
4947
4948 /* octal: "\1", "\12", "\123" */
4949 case '0':
4950 case '1':
4951 case '2':
4952 case '3':
4953 case '4':
4954 case '5':
4955 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00004956 case '7': *name = *p++ - '0';
4957 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004959 *name = (*name << 3) + *p++ - '0';
4960 if (*p >= '0' && *p <= '7')
4961 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004963 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 break;
4965
4966 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00004967 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 if (extra != 0)
4969 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004970 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 break;
4972 }
4973 /* FALLTHROUGH */
4974
Bram Moolenaar8c711452005-01-14 21:53:12 +00004975 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 break;
4977 }
4978 }
4979 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004980 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004983 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 *arg = p + 1;
4985
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 return OK;
4987}
4988
4989/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004990 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 * Return OK or FAIL.
4992 */
4993 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004994get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004996 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 int evaluate;
4998{
4999 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005000 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005001 int reduce = 0;
5002
5003 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005004 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005005 */
5006 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5007 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005008 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005009 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005010 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005011 break;
5012 ++reduce;
5013 ++p;
5014 }
5015 }
5016
Bram Moolenaar8c711452005-01-14 21:53:12 +00005017 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005018 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005019 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005020 return FAIL;
5021 }
5022
Bram Moolenaar8c711452005-01-14 21:53:12 +00005023 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005024 if (!evaluate)
5025 {
5026 *arg = p + 1;
5027 return OK;
5028 }
5029
5030 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005031 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005032 */
5033 str = alloc((unsigned)((p - *arg) - reduce));
5034 if (str == NULL)
5035 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005036 rettv->v_type = VAR_STRING;
5037 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005038
Bram Moolenaar8c711452005-01-14 21:53:12 +00005039 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005040 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005041 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005042 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005043 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005044 break;
5045 ++p;
5046 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005047 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005048 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005049 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005050 *arg = p + 1;
5051
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005052 return OK;
5053}
5054
5055/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005056 * Allocate a variable for a List and fill it from "*arg".
5057 * Return OK or FAIL.
5058 */
5059 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005060get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005061 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005062 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005063 int evaluate;
5064{
Bram Moolenaar33570922005-01-25 22:26:29 +00005065 list_T *l = NULL;
5066 typval_T tv;
5067 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005068
5069 if (evaluate)
5070 {
5071 l = list_alloc();
5072 if (l == NULL)
5073 return FAIL;
5074 }
5075
5076 *arg = skipwhite(*arg + 1);
5077 while (**arg != ']' && **arg != NUL)
5078 {
5079 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5080 goto failret;
5081 if (evaluate)
5082 {
5083 item = listitem_alloc();
5084 if (item != NULL)
5085 {
5086 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005087 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005088 list_append(l, item);
5089 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005090 else
5091 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005092 }
5093
5094 if (**arg == ']')
5095 break;
5096 if (**arg != ',')
5097 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005098 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005099 goto failret;
5100 }
5101 *arg = skipwhite(*arg + 1);
5102 }
5103
5104 if (**arg != ']')
5105 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005106 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005107failret:
5108 if (evaluate)
5109 list_free(l);
5110 return FAIL;
5111 }
5112
5113 *arg = skipwhite(*arg + 1);
5114 if (evaluate)
5115 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005116 rettv->v_type = VAR_LIST;
5117 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005118 ++l->lv_refcount;
5119 }
5120
5121 return OK;
5122}
5123
5124/*
5125 * Allocate an empty header for a list.
5126 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005127 static list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005128list_alloc()
5129{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005130 list_T *l;
5131
5132 l = (list_T *)alloc_clear(sizeof(list_T));
5133 if (l != NULL)
5134 {
5135 /* Prepend the list to the list of lists for garbage collection. */
5136 if (first_list != NULL)
5137 first_list->lv_used_prev = l;
5138 l->lv_used_prev = NULL;
5139 l->lv_used_next = first_list;
5140 first_list = l;
5141 }
5142 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005143}
5144
5145/*
5146 * Unreference a list: decrement the reference count and free it when it
5147 * becomes zero.
5148 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005149 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005150list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005151 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005152{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005153 if (l != NULL && l->lv_refcount != DEL_REFCOUNT && --l->lv_refcount <= 0)
5154 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005155}
5156
5157/*
5158 * Free a list, including all items it points to.
5159 * Ignores the reference count.
5160 */
5161 static void
5162list_free(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005163 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005164{
Bram Moolenaar33570922005-01-25 22:26:29 +00005165 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005166
Bram Moolenaard9fba312005-06-26 22:34:35 +00005167 /* Avoid that recursive reference to the list frees us again. */
5168 l->lv_refcount = DEL_REFCOUNT;
5169
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005170 /* Remove the list from the list of lists for garbage collection. */
5171 if (l->lv_used_prev == NULL)
5172 first_list = l->lv_used_next;
5173 else
5174 l->lv_used_prev->lv_used_next = l->lv_used_next;
5175 if (l->lv_used_next != NULL)
5176 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5177
Bram Moolenaard9fba312005-06-26 22:34:35 +00005178 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005179 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005180 /* Remove the item before deleting it. */
5181 l->lv_first = item->li_next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005182 listitem_free(item);
5183 }
5184 vim_free(l);
5185}
5186
5187/*
5188 * Allocate a list item.
5189 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005190 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005191listitem_alloc()
5192{
Bram Moolenaar33570922005-01-25 22:26:29 +00005193 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005194}
5195
5196/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005197 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005198 */
5199 static void
5200listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005201 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005202{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005203 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005204 vim_free(item);
5205}
5206
5207/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005208 * Remove a list item from a List and free it. Also clears the value.
5209 */
5210 static void
5211listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005212 list_T *l;
5213 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005214{
5215 list_remove(l, item, item);
5216 listitem_free(item);
5217}
5218
5219/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005220 * Get the number of items in a list.
5221 */
5222 static long
5223list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005224 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005225{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005226 if (l == NULL)
5227 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005228 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005229}
5230
5231/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005232 * Return TRUE when two lists have exactly the same values.
5233 */
5234 static int
5235list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005236 list_T *l1;
5237 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005238 int ic; /* ignore case for strings */
5239{
Bram Moolenaar33570922005-01-25 22:26:29 +00005240 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005241
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005242 if (list_len(l1) != list_len(l2))
5243 return FALSE;
5244
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005245 for (item1 = l1->lv_first, item2 = l2->lv_first;
5246 item1 != NULL && item2 != NULL;
5247 item1 = item1->li_next, item2 = item2->li_next)
5248 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5249 return FALSE;
5250 return item1 == NULL && item2 == NULL;
5251}
5252
5253/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005254 * Return TRUE when two dictionaries have exactly the same key/values.
5255 */
5256 static int
5257dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005258 dict_T *d1;
5259 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005260 int ic; /* ignore case for strings */
5261{
Bram Moolenaar33570922005-01-25 22:26:29 +00005262 hashitem_T *hi;
5263 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005264 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005265
5266 if (dict_len(d1) != dict_len(d2))
5267 return FALSE;
5268
Bram Moolenaar33570922005-01-25 22:26:29 +00005269 todo = d1->dv_hashtab.ht_used;
5270 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005271 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005272 if (!HASHITEM_EMPTY(hi))
5273 {
5274 item2 = dict_find(d2, hi->hi_key, -1);
5275 if (item2 == NULL)
5276 return FALSE;
5277 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5278 return FALSE;
5279 --todo;
5280 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005281 }
5282 return TRUE;
5283}
5284
5285/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005286 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005287 * Compares the items just like "==" would compare them, but strings and
5288 * numbers are different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005289 */
5290 static int
5291tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005292 typval_T *tv1;
5293 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005294 int ic; /* ignore case */
5295{
5296 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005297 char_u *s1, *s2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005298
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005299 if (tv1->v_type != tv2->v_type)
5300 return FALSE;
5301
5302 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005303 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005304 case VAR_LIST:
5305 /* recursive! */
5306 return list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5307
5308 case VAR_DICT:
5309 /* recursive! */
5310 return dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5311
5312 case VAR_FUNC:
5313 return (tv1->vval.v_string != NULL
5314 && tv2->vval.v_string != NULL
5315 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5316
5317 case VAR_NUMBER:
5318 return tv1->vval.v_number == tv2->vval.v_number;
5319
5320 case VAR_STRING:
5321 s1 = get_tv_string_buf(tv1, buf1);
5322 s2 = get_tv_string_buf(tv2, buf2);
5323 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005324 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005325
5326 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005327 return TRUE;
5328}
5329
5330/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005331 * Locate item with index "n" in list "l" and return it.
5332 * A negative index is counted from the end; -1 is the last item.
5333 * Returns NULL when "n" is out of range.
5334 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005335 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005336list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005337 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005338 long n;
5339{
Bram Moolenaar33570922005-01-25 22:26:29 +00005340 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005341 long idx;
5342
5343 if (l == NULL)
5344 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005345
5346 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005347 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005348 n = l->lv_len + n;
5349
5350 /* Check for index out of range. */
5351 if (n < 0 || n >= l->lv_len)
5352 return NULL;
5353
5354 /* When there is a cached index may start search from there. */
5355 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005356 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005357 if (n < l->lv_idx / 2)
5358 {
5359 /* closest to the start of the list */
5360 item = l->lv_first;
5361 idx = 0;
5362 }
5363 else if (n > (l->lv_idx + l->lv_len) / 2)
5364 {
5365 /* closest to the end of the list */
5366 item = l->lv_last;
5367 idx = l->lv_len - 1;
5368 }
5369 else
5370 {
5371 /* closest to the cached index */
5372 item = l->lv_idx_item;
5373 idx = l->lv_idx;
5374 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005375 }
5376 else
5377 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005378 if (n < l->lv_len / 2)
5379 {
5380 /* closest to the start of the list */
5381 item = l->lv_first;
5382 idx = 0;
5383 }
5384 else
5385 {
5386 /* closest to the end of the list */
5387 item = l->lv_last;
5388 idx = l->lv_len - 1;
5389 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005390 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005391
5392 while (n > idx)
5393 {
5394 /* search forward */
5395 item = item->li_next;
5396 ++idx;
5397 }
5398 while (n < idx)
5399 {
5400 /* search backward */
5401 item = item->li_prev;
5402 --idx;
5403 }
5404
5405 /* cache the used index */
5406 l->lv_idx = idx;
5407 l->lv_idx_item = item;
5408
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005409 return item;
5410}
5411
5412/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005413 * Locate "item" list "l" and return its index.
5414 * Returns -1 when "item" is not in the list.
5415 */
5416 static long
5417list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005418 list_T *l;
5419 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005420{
5421 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005422 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005423
5424 if (l == NULL)
5425 return -1;
5426 idx = 0;
5427 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5428 ++idx;
5429 if (li == NULL)
5430 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005431 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005432}
5433
5434/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005435 * Append item "item" to the end of list "l".
5436 */
5437 static void
5438list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005439 list_T *l;
5440 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005441{
5442 if (l->lv_last == NULL)
5443 {
5444 /* empty list */
5445 l->lv_first = item;
5446 l->lv_last = item;
5447 item->li_prev = NULL;
5448 }
5449 else
5450 {
5451 l->lv_last->li_next = item;
5452 item->li_prev = l->lv_last;
5453 l->lv_last = item;
5454 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005455 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005456 item->li_next = NULL;
5457}
5458
5459/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005460 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005461 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005462 */
5463 static int
5464list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005465 list_T *l;
5466 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005467{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005468 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005469
Bram Moolenaar05159a02005-02-26 23:04:13 +00005470 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005471 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005472 copy_tv(tv, &li->li_tv);
5473 list_append(l, li);
5474 return OK;
5475}
5476
5477/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005478 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005479 * Return FAIL when out of memory.
5480 */
5481 int
5482list_append_dict(list, dict)
5483 list_T *list;
5484 dict_T *dict;
5485{
5486 listitem_T *li = listitem_alloc();
5487
5488 if (li == NULL)
5489 return FAIL;
5490 li->li_tv.v_type = VAR_DICT;
5491 li->li_tv.v_lock = 0;
5492 li->li_tv.vval.v_dict = dict;
5493 list_append(list, li);
5494 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005495 return OK;
5496}
5497
5498/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005499 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005500 * If "item" is NULL append at the end.
5501 * Return FAIL when out of memory.
5502 */
5503 static int
5504list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005505 list_T *l;
5506 typval_T *tv;
5507 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005508{
Bram Moolenaar33570922005-01-25 22:26:29 +00005509 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005510
5511 if (ni == NULL)
5512 return FAIL;
5513 copy_tv(tv, &ni->li_tv);
5514 if (item == NULL)
5515 /* Append new item at end of list. */
5516 list_append(l, ni);
5517 else
5518 {
5519 /* Insert new item before existing item. */
5520 ni->li_prev = item->li_prev;
5521 ni->li_next = item;
5522 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005523 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005524 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005525 ++l->lv_idx;
5526 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005527 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005528 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005529 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005530 l->lv_idx_item = NULL;
5531 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005532 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005533 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005534 }
5535 return OK;
5536}
5537
5538/*
5539 * Extend "l1" with "l2".
5540 * If "bef" is NULL append at the end, otherwise insert before this item.
5541 * Returns FAIL when out of memory.
5542 */
5543 static int
5544list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005545 list_T *l1;
5546 list_T *l2;
5547 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005548{
Bram Moolenaar33570922005-01-25 22:26:29 +00005549 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005550
5551 for (item = l2->lv_first; item != NULL; item = item->li_next)
5552 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5553 return FAIL;
5554 return OK;
5555}
5556
5557/*
5558 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5559 * Return FAIL when out of memory.
5560 */
5561 static int
5562list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005563 list_T *l1;
5564 list_T *l2;
5565 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005566{
Bram Moolenaar33570922005-01-25 22:26:29 +00005567 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005568
5569 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005570 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005571 if (l == NULL)
5572 return FAIL;
5573 tv->v_type = VAR_LIST;
5574 tv->vval.v_list = l;
5575
5576 /* append all items from the second list */
5577 return list_extend(l, l2, NULL);
5578}
5579
5580/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005581 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005582 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005583 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005584 * Returns NULL when out of memory.
5585 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005586 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005587list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005588 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005589 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005590 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005591{
Bram Moolenaar33570922005-01-25 22:26:29 +00005592 list_T *copy;
5593 listitem_T *item;
5594 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005595
5596 if (orig == NULL)
5597 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005598
5599 copy = list_alloc();
5600 if (copy != NULL)
5601 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005602 if (copyID != 0)
5603 {
5604 /* Do this before adding the items, because one of the items may
5605 * refer back to this list. */
5606 orig->lv_copyID = copyID;
5607 orig->lv_copylist = copy;
5608 }
5609 for (item = orig->lv_first; item != NULL && !got_int;
5610 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005611 {
5612 ni = listitem_alloc();
5613 if (ni == NULL)
5614 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005615 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005616 {
5617 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5618 {
5619 vim_free(ni);
5620 break;
5621 }
5622 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005623 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005624 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005625 list_append(copy, ni);
5626 }
5627 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005628 if (item != NULL)
5629 {
5630 list_unref(copy);
5631 copy = NULL;
5632 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005633 }
5634
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005635 return copy;
5636}
5637
5638/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005639 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005640 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005641 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005642 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005643list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005644 list_T *l;
5645 listitem_T *item;
5646 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005647{
Bram Moolenaar33570922005-01-25 22:26:29 +00005648 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005649
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005650 /* notify watchers */
5651 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005652 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005653 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005654 list_fix_watch(l, ip);
5655 if (ip == item2)
5656 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005657 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005658
5659 if (item2->li_next == NULL)
5660 l->lv_last = item->li_prev;
5661 else
5662 item2->li_next->li_prev = item->li_prev;
5663 if (item->li_prev == NULL)
5664 l->lv_first = item2->li_next;
5665 else
5666 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005667 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005668}
5669
5670/*
5671 * Return an allocated string with the string representation of a list.
5672 * May return NULL.
5673 */
5674 static char_u *
5675list2string(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005676 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005677{
5678 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005679
5680 if (tv->vval.v_list == NULL)
5681 return NULL;
5682 ga_init2(&ga, (int)sizeof(char), 80);
5683 ga_append(&ga, '[');
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005684 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE) == FAIL)
5685 {
5686 vim_free(ga.ga_data);
5687 return NULL;
5688 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005689 ga_append(&ga, ']');
5690 ga_append(&ga, NUL);
5691 return (char_u *)ga.ga_data;
5692}
5693
5694/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005695 * Join list "l" into a string in "*gap", using separator "sep".
5696 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005697 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005698 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005699 static int
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005700list_join(gap, l, sep, echo)
5701 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00005702 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005703 char_u *sep;
5704 int echo;
5705{
5706 int first = TRUE;
5707 char_u *tofree;
5708 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00005709 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005710 char_u *s;
5711
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005712 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005713 {
5714 if (first)
5715 first = FALSE;
5716 else
5717 ga_concat(gap, sep);
5718
5719 if (echo)
5720 s = echo_string(&item->li_tv, &tofree, numbuf);
5721 else
5722 s = tv2string(&item->li_tv, &tofree, numbuf);
5723 if (s != NULL)
5724 ga_concat(gap, s);
5725 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005726 if (s == NULL)
5727 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005728 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005729 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005730}
5731
5732/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005733 * Garbage collection for lists and dictionaries.
5734 *
5735 * We use reference counts to be able to free most items right away when they
5736 * are no longer used. But for composite items it's possible that it becomes
5737 * unused while the reference count is > 0: When there is a recursive
5738 * reference. Example:
5739 * :let l = [1, 2, 3]
5740 * :let d = {9: l}
5741 * :let l[1] = d
5742 *
5743 * Since this is quite unusual we handle this with garbage collection: every
5744 * once in a while find out which lists and dicts are not referenced from any
5745 * variable.
5746 *
5747 * Here is a good reference text about garbage collection (refers to Python
5748 * but it applies to all reference-counting mechanisms):
5749 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005750 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005751
5752/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005753 * Do garbage collection for lists and dicts.
5754 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005755 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005756 int
5757garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00005758{
5759 dict_T *dd;
5760 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005761 int copyID = ++current_copyID;
5762 buf_T *buf;
5763 win_T *wp;
5764 int i;
5765 funccall_T *fc;
5766 int did_free = FALSE;
5767
5768 /*
5769 * 1. Go through all accessible variables and mark all lists and dicts
5770 * with copyID.
5771 */
5772 /* script-local variables */
5773 for (i = 1; i <= ga_scripts.ga_len; ++i)
5774 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
5775
5776 /* buffer-local variables */
5777 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5778 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
5779
5780 /* window-local variables */
5781 FOR_ALL_WINDOWS(wp)
5782 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
5783
5784 /* global variables */
5785 set_ref_in_ht(&globvarht, copyID);
5786
5787 /* function-local variables */
5788 for (fc = current_funccal; fc != NULL; fc = fc->caller)
5789 {
5790 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
5791 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
5792 }
5793
5794 /*
5795 * 2. Go through the list of dicts and free items without the copyID.
5796 */
5797 for (dd = first_dict; dd != NULL; )
5798 if (dd->dv_copyID != copyID)
5799 {
5800 dict_free(dd);
5801 did_free = TRUE;
5802
5803 /* restart, next dict may also have been freed */
5804 dd = first_dict;
5805 }
5806 else
5807 dd = dd->dv_used_next;
5808
5809 /*
5810 * 3. Go through the list of lists and free items without the copyID.
5811 */
5812 for (ll = first_list; ll != NULL; )
5813 if (ll->lv_copyID != copyID)
5814 {
5815 list_free(ll);
5816 did_free = TRUE;
5817
5818 /* restart, next dict may also have been freed */
5819 ll = first_list;
5820 }
5821 else
5822 ll = ll->lv_used_next;
5823
5824 return did_free;
5825}
5826
5827/*
5828 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
5829 */
5830 static void
5831set_ref_in_ht(ht, copyID)
5832 hashtab_T *ht;
5833 int copyID;
5834{
5835 int todo;
5836 hashitem_T *hi;
5837
5838 todo = ht->ht_used;
5839 for (hi = ht->ht_array; todo > 0; ++hi)
5840 if (!HASHITEM_EMPTY(hi))
5841 {
5842 --todo;
5843 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
5844 }
5845}
5846
5847/*
5848 * Mark all lists and dicts referenced through list "l" with "copyID".
5849 */
5850 static void
5851set_ref_in_list(l, copyID)
5852 list_T *l;
5853 int copyID;
5854{
5855 listitem_T *li;
5856
5857 for (li = l->lv_first; li != NULL; li = li->li_next)
5858 set_ref_in_item(&li->li_tv, copyID);
5859}
5860
5861/*
5862 * Mark all lists and dicts referenced through typval "tv" with "copyID".
5863 */
5864 static void
5865set_ref_in_item(tv, copyID)
5866 typval_T *tv;
5867 int copyID;
5868{
5869 dict_T *dd;
5870 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005871
5872 switch (tv->v_type)
5873 {
5874 case VAR_DICT:
5875 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005876 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005877 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005878 /* Didn't see this dict yet. */
5879 dd->dv_copyID = copyID;
5880 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00005881 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005882 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005883
5884 case VAR_LIST:
5885 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005886 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005887 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005888 /* Didn't see this list yet. */
5889 ll->lv_copyID = copyID;
5890 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00005891 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005892 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005893 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005894 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005895}
5896
5897/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005898 * Allocate an empty header for a dictionary.
5899 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005900 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00005901dict_alloc()
5902{
Bram Moolenaar33570922005-01-25 22:26:29 +00005903 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005904
Bram Moolenaar33570922005-01-25 22:26:29 +00005905 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005906 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005907 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005908 /* Add the list to the hashtable for garbage collection. */
5909 if (first_dict != NULL)
5910 first_dict->dv_used_prev = d;
5911 d->dv_used_next = first_dict;
5912 d->dv_used_prev = NULL;
5913
Bram Moolenaar33570922005-01-25 22:26:29 +00005914 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005915 d->dv_lock = 0;
5916 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005917 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005918 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005919 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005920}
5921
5922/*
5923 * Unreference a Dictionary: decrement the reference count and free it when it
5924 * becomes zero.
5925 */
5926 static void
5927dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00005928 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005929{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005930 if (d != NULL && d->dv_refcount != DEL_REFCOUNT && --d->dv_refcount <= 0)
5931 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005932}
5933
5934/*
5935 * Free a Dictionary, including all items it contains.
5936 * Ignores the reference count.
5937 */
5938 static void
5939dict_free(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00005940 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005941{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005942 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00005943 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005944 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005945
Bram Moolenaard9fba312005-06-26 22:34:35 +00005946 /* Avoid that recursive reference to the dict frees us again. */
5947 d->dv_refcount = DEL_REFCOUNT;
5948
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005949 /* Remove the dict from the list of dicts for garbage collection. */
5950 if (d->dv_used_prev == NULL)
5951 first_dict = d->dv_used_next;
5952 else
5953 d->dv_used_prev->dv_used_next = d->dv_used_next;
5954 if (d->dv_used_next != NULL)
5955 d->dv_used_next->dv_used_prev = d->dv_used_prev;
5956
5957 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005958 hash_lock(&d->dv_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +00005959 todo = d->dv_hashtab.ht_used;
5960 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005961 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005962 if (!HASHITEM_EMPTY(hi))
5963 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005964 /* Remove the item before deleting it, just in case there is
5965 * something recursive causing trouble. */
5966 di = HI2DI(hi);
5967 hash_remove(&d->dv_hashtab, hi);
5968 dictitem_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005969 --todo;
5970 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005971 }
Bram Moolenaar33570922005-01-25 22:26:29 +00005972 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005973 vim_free(d);
5974}
5975
5976/*
5977 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005978 * The "key" is copied to the new item.
5979 * Note that the value of the item "di_tv" still needs to be initialized!
5980 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005981 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005982 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005983dictitem_alloc(key)
5984 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005985{
Bram Moolenaar33570922005-01-25 22:26:29 +00005986 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005987
Bram Moolenaar33570922005-01-25 22:26:29 +00005988 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(key));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005989 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005990 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005991 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005992 di->di_flags = 0;
5993 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005994 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005995}
5996
5997/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005998 * Make a copy of a Dictionary item.
5999 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006000 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006001dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006002 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006003{
Bram Moolenaar33570922005-01-25 22:26:29 +00006004 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006005
Bram Moolenaar33570922005-01-25 22:26:29 +00006006 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(org->di_key));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006007 if (di != NULL)
6008 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006009 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006010 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006011 copy_tv(&org->di_tv, &di->di_tv);
6012 }
6013 return di;
6014}
6015
6016/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006017 * Remove item "item" from Dictionary "dict" and free it.
6018 */
6019 static void
6020dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006021 dict_T *dict;
6022 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006023{
Bram Moolenaar33570922005-01-25 22:26:29 +00006024 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006025
Bram Moolenaar33570922005-01-25 22:26:29 +00006026 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006027 if (HASHITEM_EMPTY(hi))
6028 EMSG2(_(e_intern2), "dictitem_remove()");
6029 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006030 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006031 dictitem_free(item);
6032}
6033
6034/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006035 * Free a dict item. Also clears the value.
6036 */
6037 static void
6038dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006039 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006040{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006041 clear_tv(&item->di_tv);
6042 vim_free(item);
6043}
6044
6045/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006046 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6047 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006048 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006049 * Returns NULL when out of memory.
6050 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006051 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006052dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006053 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006054 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006055 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006056{
Bram Moolenaar33570922005-01-25 22:26:29 +00006057 dict_T *copy;
6058 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006059 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006060 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006061
6062 if (orig == NULL)
6063 return NULL;
6064
6065 copy = dict_alloc();
6066 if (copy != NULL)
6067 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006068 if (copyID != 0)
6069 {
6070 orig->dv_copyID = copyID;
6071 orig->dv_copydict = copy;
6072 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006073 todo = orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006074 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006075 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006076 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006077 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006078 --todo;
6079
6080 di = dictitem_alloc(hi->hi_key);
6081 if (di == NULL)
6082 break;
6083 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006084 {
6085 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6086 copyID) == FAIL)
6087 {
6088 vim_free(di);
6089 break;
6090 }
6091 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006092 else
6093 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6094 if (dict_add(copy, di) == FAIL)
6095 {
6096 dictitem_free(di);
6097 break;
6098 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006099 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006100 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006101
Bram Moolenaare9a41262005-01-15 22:18:47 +00006102 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006103 if (todo > 0)
6104 {
6105 dict_unref(copy);
6106 copy = NULL;
6107 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006108 }
6109
6110 return copy;
6111}
6112
6113/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006114 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006115 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006116 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006117 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006118dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006119 dict_T *d;
6120 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006121{
Bram Moolenaar33570922005-01-25 22:26:29 +00006122 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006123}
6124
Bram Moolenaar8c711452005-01-14 21:53:12 +00006125/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006126 * Add a number or string entry to dictionary "d".
6127 * When "str" is NULL use number "nr", otherwise use "str".
6128 * Returns FAIL when out of memory and when key already exists.
6129 */
6130 int
6131dict_add_nr_str(d, key, nr, str)
6132 dict_T *d;
6133 char *key;
6134 long nr;
6135 char_u *str;
6136{
6137 dictitem_T *item;
6138
6139 item = dictitem_alloc((char_u *)key);
6140 if (item == NULL)
6141 return FAIL;
6142 item->di_tv.v_lock = 0;
6143 if (str == NULL)
6144 {
6145 item->di_tv.v_type = VAR_NUMBER;
6146 item->di_tv.vval.v_number = nr;
6147 }
6148 else
6149 {
6150 item->di_tv.v_type = VAR_STRING;
6151 item->di_tv.vval.v_string = vim_strsave(str);
6152 }
6153 if (dict_add(d, item) == FAIL)
6154 {
6155 dictitem_free(item);
6156 return FAIL;
6157 }
6158 return OK;
6159}
6160
6161/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006162 * Get the number of items in a Dictionary.
6163 */
6164 static long
6165dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006166 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006167{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006168 if (d == NULL)
6169 return 0L;
Bram Moolenaar33570922005-01-25 22:26:29 +00006170 return d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006171}
6172
6173/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006174 * Find item "key[len]" in Dictionary "d".
6175 * If "len" is negative use strlen(key).
6176 * Returns NULL when not found.
6177 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006178 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006179dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006180 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006181 char_u *key;
6182 int len;
6183{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006184#define AKEYLEN 200
6185 char_u buf[AKEYLEN];
6186 char_u *akey;
6187 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006188 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006189
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006190 if (len < 0)
6191 akey = key;
6192 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006193 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006194 tofree = akey = vim_strnsave(key, len);
6195 if (akey == NULL)
6196 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006197 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006198 else
6199 {
6200 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006201 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006202 akey = buf;
6203 }
6204
Bram Moolenaar33570922005-01-25 22:26:29 +00006205 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006206 vim_free(tofree);
6207 if (HASHITEM_EMPTY(hi))
6208 return NULL;
6209 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006210}
6211
6212/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006213 * Get a string item from a dictionary in allocated memory.
6214 * Returns NULL if the entry doesn't exist or out of memory.
6215 */
6216 char_u *
6217get_dict_string(d, key)
6218 dict_T *d;
6219 char_u *key;
6220{
6221 dictitem_T *di;
6222
6223 di = dict_find(d, key, -1);
6224 if (di == NULL)
6225 return NULL;
6226 return vim_strsave(get_tv_string(&di->di_tv));
6227}
6228
6229/*
6230 * Get a number item from a dictionary.
6231 * Returns 0 if the entry doesn't exist or out of memory.
6232 */
6233 long
6234get_dict_number(d, key)
6235 dict_T *d;
6236 char_u *key;
6237{
6238 dictitem_T *di;
6239
6240 di = dict_find(d, key, -1);
6241 if (di == NULL)
6242 return 0;
6243 return get_tv_number(&di->di_tv);
6244}
6245
6246/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006247 * Return an allocated string with the string representation of a Dictionary.
6248 * May return NULL.
6249 */
6250 static char_u *
6251dict2string(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006252 typval_T *tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006253{
6254 garray_T ga;
6255 int first = TRUE;
6256 char_u *tofree;
6257 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006258 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006259 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006260 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006261 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006262
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006263 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006264 return NULL;
6265 ga_init2(&ga, (int)sizeof(char), 80);
6266 ga_append(&ga, '{');
6267
Bram Moolenaar33570922005-01-25 22:26:29 +00006268 todo = d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006269 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006270 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006271 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006272 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006273 --todo;
6274
6275 if (first)
6276 first = FALSE;
6277 else
6278 ga_concat(&ga, (char_u *)", ");
6279
6280 tofree = string_quote(hi->hi_key, FALSE);
6281 if (tofree != NULL)
6282 {
6283 ga_concat(&ga, tofree);
6284 vim_free(tofree);
6285 }
6286 ga_concat(&ga, (char_u *)": ");
6287 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf);
6288 if (s != NULL)
6289 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006290 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006291 if (s == NULL)
6292 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006293 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006294 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006295 if (todo > 0)
6296 {
6297 vim_free(ga.ga_data);
6298 return NULL;
6299 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006300
6301 ga_append(&ga, '}');
6302 ga_append(&ga, NUL);
6303 return (char_u *)ga.ga_data;
6304}
6305
6306/*
6307 * Allocate a variable for a Dictionary and fill it from "*arg".
6308 * Return OK or FAIL. Returns NOTDONE for {expr}.
6309 */
6310 static int
6311get_dict_tv(arg, rettv, evaluate)
6312 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006313 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006314 int evaluate;
6315{
Bram Moolenaar33570922005-01-25 22:26:29 +00006316 dict_T *d = NULL;
6317 typval_T tvkey;
6318 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006319 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00006320 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006321 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006322 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00006323
6324 /*
6325 * First check if it's not a curly-braces thing: {expr}.
6326 * Must do this without evaluating, otherwise a function may be called
6327 * twice. Unfortunately this means we need to call eval1() twice for the
6328 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006329 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006330 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006331 if (*start != '}')
6332 {
6333 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
6334 return FAIL;
6335 if (*start == '}')
6336 return NOTDONE;
6337 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006338
6339 if (evaluate)
6340 {
6341 d = dict_alloc();
6342 if (d == NULL)
6343 return FAIL;
6344 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006345 tvkey.v_type = VAR_UNKNOWN;
6346 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006347
6348 *arg = skipwhite(*arg + 1);
6349 while (**arg != '}' && **arg != NUL)
6350 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006351 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006352 goto failret;
6353 if (**arg != ':')
6354 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006355 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006356 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006357 goto failret;
6358 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006359 key = get_tv_string_buf_chk(&tvkey, buf);
6360 if (key == NULL || *key == NUL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006361 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006362 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6363 if (key != NULL)
6364 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006365 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006366 goto failret;
6367 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006368
6369 *arg = skipwhite(*arg + 1);
6370 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6371 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006372 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006373 goto failret;
6374 }
6375 if (evaluate)
6376 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006377 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006378 if (item != NULL)
6379 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00006380 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006381 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006382 clear_tv(&tv);
6383 goto failret;
6384 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006385 item = dictitem_alloc(key);
6386 clear_tv(&tvkey);
6387 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006388 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006389 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006390 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006391 if (dict_add(d, item) == FAIL)
6392 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006393 }
6394 }
6395
6396 if (**arg == '}')
6397 break;
6398 if (**arg != ',')
6399 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006400 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006401 goto failret;
6402 }
6403 *arg = skipwhite(*arg + 1);
6404 }
6405
6406 if (**arg != '}')
6407 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006408 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006409failret:
6410 if (evaluate)
6411 dict_free(d);
6412 return FAIL;
6413 }
6414
6415 *arg = skipwhite(*arg + 1);
6416 if (evaluate)
6417 {
6418 rettv->v_type = VAR_DICT;
6419 rettv->vval.v_dict = d;
6420 ++d->dv_refcount;
6421 }
6422
6423 return OK;
6424}
6425
Bram Moolenaar8c711452005-01-14 21:53:12 +00006426/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006427 * Return a string with the string representation of a variable.
6428 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006429 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006430 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006431 * May return NULL;
6432 */
6433 static char_u *
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006434echo_string(tv, tofree, numbuf)
Bram Moolenaar33570922005-01-25 22:26:29 +00006435 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006436 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006437 char_u *numbuf;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006438{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006439 static int recurse = 0;
6440 char_u *r = NULL;
6441
Bram Moolenaar33570922005-01-25 22:26:29 +00006442 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006443 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006444 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006445 *tofree = NULL;
6446 return NULL;
6447 }
6448 ++recurse;
6449
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006450 switch (tv->v_type)
6451 {
6452 case VAR_FUNC:
6453 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006454 r = tv->vval.v_string;
6455 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006456 case VAR_LIST:
6457 *tofree = list2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006458 r = *tofree;
6459 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006460 case VAR_DICT:
6461 *tofree = dict2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006462 r = *tofree;
6463 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006464 case VAR_STRING:
6465 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006466 *tofree = NULL;
6467 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006468 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006469 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006470 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006471 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006472 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006473
6474 --recurse;
6475 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006476}
6477
6478/*
6479 * Return a string with the string representation of a variable.
6480 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6481 * "numbuf" is used for a number.
6482 * Puts quotes around strings, so that they can be parsed back by eval().
6483 * May return NULL;
6484 */
6485 static char_u *
6486tv2string(tv, tofree, numbuf)
Bram Moolenaar33570922005-01-25 22:26:29 +00006487 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006488 char_u **tofree;
6489 char_u *numbuf;
6490{
6491 switch (tv->v_type)
6492 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006493 case VAR_FUNC:
6494 *tofree = string_quote(tv->vval.v_string, TRUE);
6495 return *tofree;
6496 case VAR_STRING:
6497 *tofree = string_quote(tv->vval.v_string, FALSE);
6498 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006499 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006500 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006501 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006502 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006503 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006504 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006505 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006506 return echo_string(tv, tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006507}
6508
6509/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006510 * Return string "str" in ' quotes, doubling ' characters.
6511 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006512 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006513 */
6514 static char_u *
6515string_quote(str, function)
6516 char_u *str;
6517 int function;
6518{
Bram Moolenaar33570922005-01-25 22:26:29 +00006519 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006520 char_u *p, *r, *s;
6521
Bram Moolenaar33570922005-01-25 22:26:29 +00006522 len = (function ? 13 : 3);
6523 if (str != NULL)
6524 {
6525 len += STRLEN(str);
6526 for (p = str; *p != NUL; mb_ptr_adv(p))
6527 if (*p == '\'')
6528 ++len;
6529 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006530 s = r = alloc(len);
6531 if (r != NULL)
6532 {
6533 if (function)
6534 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006535 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006536 r += 10;
6537 }
6538 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006539 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006540 if (str != NULL)
6541 for (p = str; *p != NUL; )
6542 {
6543 if (*p == '\'')
6544 *r++ = '\'';
6545 MB_COPY_CHAR(p, r);
6546 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006547 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006548 if (function)
6549 *r++ = ')';
6550 *r++ = NUL;
6551 }
6552 return s;
6553}
6554
6555/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556 * Get the value of an environment variable.
6557 * "arg" is pointing to the '$'. It is advanced to after the name.
6558 * If the environment variable was not set, silently assume it is empty.
6559 * Always return OK.
6560 */
6561 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006562get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006564 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565 int evaluate;
6566{
6567 char_u *string = NULL;
6568 int len;
6569 int cc;
6570 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006571 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572
6573 ++*arg;
6574 name = *arg;
6575 len = get_env_len(arg);
6576 if (evaluate)
6577 {
6578 if (len != 0)
6579 {
6580 cc = name[len];
6581 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006582 /* first try vim_getenv(), fast for normal environment vars */
6583 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006585 {
6586 if (!mustfree)
6587 string = vim_strsave(string);
6588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 else
6590 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00006591 if (mustfree)
6592 vim_free(string);
6593
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594 /* next try expanding things like $VIM and ${HOME} */
6595 string = expand_env_save(name - 1);
6596 if (string != NULL && *string == '$')
6597 {
6598 vim_free(string);
6599 string = NULL;
6600 }
6601 }
6602 name[len] = cc;
6603 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006604 rettv->v_type = VAR_STRING;
6605 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 }
6607
6608 return OK;
6609}
6610
6611/*
6612 * Array with names and number of arguments of all internal functions
6613 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
6614 */
6615static struct fst
6616{
6617 char *f_name; /* function name */
6618 char f_min_argc; /* minimal number of arguments */
6619 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00006620 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006621 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622} functions[] =
6623{
Bram Moolenaar0d660222005-01-07 21:51:51 +00006624 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625 {"append", 2, 2, f_append},
6626 {"argc", 0, 0, f_argc},
6627 {"argidx", 0, 0, f_argidx},
6628 {"argv", 1, 1, f_argv},
6629 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006630 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631 {"bufexists", 1, 1, f_bufexists},
6632 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
6633 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
6634 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
6635 {"buflisted", 1, 1, f_buflisted},
6636 {"bufloaded", 1, 1, f_bufloaded},
6637 {"bufname", 1, 1, f_bufname},
6638 {"bufnr", 1, 1, f_bufnr},
6639 {"bufwinnr", 1, 1, f_bufwinnr},
6640 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006641 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006642 {"call", 2, 3, f_call},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643 {"char2nr", 1, 1, f_char2nr},
6644 {"cindent", 1, 1, f_cindent},
6645 {"col", 1, 1, f_col},
6646 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006647 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006648 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649 {"cscope_connection",0,3, f_cscope_connection},
6650 {"cursor", 2, 2, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006651 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652 {"delete", 1, 1, f_delete},
6653 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00006654 {"diff_filler", 1, 1, f_diff_filler},
6655 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006656 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006658 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659 {"eventhandler", 0, 0, f_eventhandler},
6660 {"executable", 1, 1, f_executable},
6661 {"exists", 1, 1, f_exists},
6662 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006663 {"extend", 2, 3, f_extend},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
6665 {"filereadable", 1, 1, f_filereadable},
6666 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006667 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006668 {"finddir", 1, 3, f_finddir},
6669 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 {"fnamemodify", 2, 2, f_fnamemodify},
6671 {"foldclosed", 1, 1, f_foldclosed},
6672 {"foldclosedend", 1, 1, f_foldclosedend},
6673 {"foldlevel", 1, 1, f_foldlevel},
6674 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006675 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006677 {"function", 1, 1, f_function},
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006678 {"garbagecollect", 0, 0, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006679 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00006680 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681 {"getbufvar", 2, 2, f_getbufvar},
6682 {"getchar", 0, 1, f_getchar},
6683 {"getcharmod", 0, 0, f_getcharmod},
6684 {"getcmdline", 0, 0, f_getcmdline},
6685 {"getcmdpos", 0, 0, f_getcmdpos},
6686 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006687 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006688 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689 {"getfsize", 1, 1, f_getfsize},
6690 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006691 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006692 {"getline", 1, 2, f_getline},
Bram Moolenaar2641f772005-03-25 21:58:17 +00006693 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006694 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695 {"getregtype", 0, 1, f_getregtype},
6696 {"getwinposx", 0, 0, f_getwinposx},
6697 {"getwinposy", 0, 0, f_getwinposy},
6698 {"getwinvar", 2, 2, f_getwinvar},
6699 {"glob", 1, 1, f_glob},
6700 {"globpath", 2, 2, f_globpath},
6701 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006702 {"has_key", 2, 2, f_has_key},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703 {"hasmapto", 1, 2, f_hasmapto},
6704 {"highlightID", 1, 1, f_hlID}, /* obsolete */
6705 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
6706 {"histadd", 2, 2, f_histadd},
6707 {"histdel", 1, 2, f_histdel},
6708 {"histget", 1, 2, f_histget},
6709 {"histnr", 1, 1, f_histnr},
6710 {"hlID", 1, 1, f_hlID},
6711 {"hlexists", 1, 1, f_hlexists},
6712 {"hostname", 0, 0, f_hostname},
6713 {"iconv", 3, 3, f_iconv},
6714 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006715 {"index", 2, 4, f_index},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716 {"input", 1, 2, f_input},
6717 {"inputdialog", 1, 3, f_inputdialog},
6718 {"inputrestore", 0, 0, f_inputrestore},
6719 {"inputsave", 0, 0, f_inputsave},
6720 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006721 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006723 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006724 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006725 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006726 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006728 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729 {"libcall", 3, 3, f_libcall},
6730 {"libcallnr", 3, 3, f_libcallnr},
6731 {"line", 1, 1, f_line},
6732 {"line2byte", 1, 1, f_line2byte},
6733 {"lispindent", 1, 1, f_lispindent},
6734 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006735 {"map", 2, 2, f_map},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736 {"maparg", 1, 2, f_maparg},
6737 {"mapcheck", 1, 2, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006738 {"match", 2, 4, f_match},
6739 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006740 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006741 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006742 {"max", 1, 1, f_max},
6743 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006744#ifdef vim_mkdir
6745 {"mkdir", 1, 3, f_mkdir},
6746#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747 {"mode", 0, 0, f_mode},
6748 {"nextnonblank", 1, 1, f_nextnonblank},
6749 {"nr2char", 1, 1, f_nr2char},
6750 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006751 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006752 {"readfile", 1, 3, f_readfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753 {"remote_expr", 2, 3, f_remote_expr},
6754 {"remote_foreground", 1, 1, f_remote_foreground},
6755 {"remote_peek", 1, 2, f_remote_peek},
6756 {"remote_read", 1, 1, f_remote_read},
6757 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006758 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006760 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006762 {"reverse", 1, 1, f_reverse},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763 {"search", 1, 2, f_search},
6764 {"searchpair", 3, 5, f_searchpair},
6765 {"server2client", 2, 2, f_server2client},
6766 {"serverlist", 0, 0, f_serverlist},
6767 {"setbufvar", 3, 3, f_setbufvar},
6768 {"setcmdpos", 1, 1, f_setcmdpos},
6769 {"setline", 2, 2, f_setline},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00006770 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771 {"setreg", 2, 3, f_setreg},
6772 {"setwinvar", 3, 3, f_setwinvar},
6773 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006774 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006775 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006776 {"spellbadword", 0, 0, f_spellbadword},
6777 {"spellsuggest", 1, 2, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006778 {"split", 1, 3, f_split},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779#ifdef HAVE_STRFTIME
6780 {"strftime", 1, 2, f_strftime},
6781#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00006782 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006783 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784 {"strlen", 1, 1, f_strlen},
6785 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00006786 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787 {"strtrans", 1, 1, f_strtrans},
6788 {"submatch", 1, 1, f_submatch},
6789 {"substitute", 4, 4, f_substitute},
6790 {"synID", 3, 3, f_synID},
6791 {"synIDattr", 2, 3, f_synIDattr},
6792 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006793 {"system", 1, 2, f_system},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006794 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795 {"tempname", 0, 0, f_tempname},
6796 {"tolower", 1, 1, f_tolower},
6797 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00006798 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006800 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 {"virtcol", 1, 1, f_virtcol},
6802 {"visualmode", 0, 1, f_visualmode},
6803 {"winbufnr", 1, 1, f_winbufnr},
6804 {"wincol", 0, 0, f_wincol},
6805 {"winheight", 1, 1, f_winheight},
6806 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006807 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808 {"winrestcmd", 0, 0, f_winrestcmd},
6809 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006810 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811};
6812
6813#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6814
6815/*
6816 * Function given to ExpandGeneric() to obtain the list of internal
6817 * or user defined function names.
6818 */
6819 char_u *
6820get_function_name(xp, idx)
6821 expand_T *xp;
6822 int idx;
6823{
6824 static int intidx = -1;
6825 char_u *name;
6826
6827 if (idx == 0)
6828 intidx = -1;
6829 if (intidx < 0)
6830 {
6831 name = get_user_func_name(xp, idx);
6832 if (name != NULL)
6833 return name;
6834 }
6835 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
6836 {
6837 STRCPY(IObuff, functions[intidx].f_name);
6838 STRCAT(IObuff, "(");
6839 if (functions[intidx].f_max_argc == 0)
6840 STRCAT(IObuff, ")");
6841 return IObuff;
6842 }
6843
6844 return NULL;
6845}
6846
6847/*
6848 * Function given to ExpandGeneric() to obtain the list of internal or
6849 * user defined variable or function names.
6850 */
6851/*ARGSUSED*/
6852 char_u *
6853get_expr_name(xp, idx)
6854 expand_T *xp;
6855 int idx;
6856{
6857 static int intidx = -1;
6858 char_u *name;
6859
6860 if (idx == 0)
6861 intidx = -1;
6862 if (intidx < 0)
6863 {
6864 name = get_function_name(xp, idx);
6865 if (name != NULL)
6866 return name;
6867 }
6868 return get_user_var_name(xp, ++intidx);
6869}
6870
6871#endif /* FEAT_CMDL_COMPL */
6872
6873/*
6874 * Find internal function in table above.
6875 * Return index, or -1 if not found
6876 */
6877 static int
6878find_internal_func(name)
6879 char_u *name; /* name of the function */
6880{
6881 int first = 0;
6882 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
6883 int cmp;
6884 int x;
6885
6886 /*
6887 * Find the function name in the table. Binary search.
6888 */
6889 while (first <= last)
6890 {
6891 x = first + ((unsigned)(last - first) >> 1);
6892 cmp = STRCMP(name, functions[x].f_name);
6893 if (cmp < 0)
6894 last = x - 1;
6895 else if (cmp > 0)
6896 first = x + 1;
6897 else
6898 return x;
6899 }
6900 return -1;
6901}
6902
6903/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006904 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
6905 * name it contains, otherwise return "name".
6906 */
6907 static char_u *
6908deref_func_name(name, lenp)
6909 char_u *name;
6910 int *lenp;
6911{
Bram Moolenaar33570922005-01-25 22:26:29 +00006912 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006913 int cc;
6914
6915 cc = name[*lenp];
6916 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00006917 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006918 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00006919 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006920 {
Bram Moolenaar33570922005-01-25 22:26:29 +00006921 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006922 {
6923 *lenp = 0;
6924 return (char_u *)""; /* just in case */
6925 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006926 *lenp = STRLEN(v->di_tv.vval.v_string);
6927 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006928 }
6929
6930 return name;
6931}
6932
6933/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934 * Allocate a variable for the result of a function.
6935 * Return OK or FAIL.
6936 */
6937 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00006938get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
6939 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940 char_u *name; /* name of the function */
6941 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00006942 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943 char_u **arg; /* argument, pointing to the '(' */
6944 linenr_T firstline; /* first line of range */
6945 linenr_T lastline; /* last line of range */
6946 int *doesrange; /* return: function handled range */
6947 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00006948 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949{
6950 char_u *argp;
6951 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00006952 typval_T argvars[MAX_FUNC_ARGS]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 int argcount = 0; /* number of arguments found */
6954
6955 /*
6956 * Get the arguments.
6957 */
6958 argp = *arg;
6959 while (argcount < MAX_FUNC_ARGS)
6960 {
6961 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
6962 if (*argp == ')' || *argp == ',' || *argp == NUL)
6963 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
6965 {
6966 ret = FAIL;
6967 break;
6968 }
6969 ++argcount;
6970 if (*argp != ',')
6971 break;
6972 }
6973 if (*argp == ')')
6974 ++argp;
6975 else
6976 ret = FAIL;
6977
6978 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006979 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00006980 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00006982 {
6983 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006984 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00006985 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006986 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00006987 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988
6989 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006990 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991
6992 *arg = skipwhite(argp);
6993 return ret;
6994}
6995
6996
6997/*
6998 * Call a function with its resolved parameters
6999 * Return OK or FAIL.
7000 */
7001 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007002call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007003 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 char_u *name; /* name of the function */
7005 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007006 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 int argcount; /* number of "argvars" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007008 typval_T *argvars; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 linenr_T firstline; /* first line of range */
7010 linenr_T lastline; /* last line of range */
7011 int *doesrange; /* return: function handled range */
7012 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007013 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014{
7015 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016#define ERROR_UNKNOWN 0
7017#define ERROR_TOOMANY 1
7018#define ERROR_TOOFEW 2
7019#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007020#define ERROR_DICT 4
7021#define ERROR_NONE 5
7022#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023 int error = ERROR_NONE;
7024 int i;
7025 int llen;
7026 ufunc_T *fp;
7027 int cc;
7028#define FLEN_FIXED 40
7029 char_u fname_buf[FLEN_FIXED + 1];
7030 char_u *fname;
7031
7032 /*
7033 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7034 * Change <SNR>123_name() to K_SNR 123_name().
7035 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7036 */
7037 cc = name[len];
7038 name[len] = NUL;
7039 llen = eval_fname_script(name);
7040 if (llen > 0)
7041 {
7042 fname_buf[0] = K_SPECIAL;
7043 fname_buf[1] = KS_EXTRA;
7044 fname_buf[2] = (int)KE_SNR;
7045 i = 3;
7046 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7047 {
7048 if (current_SID <= 0)
7049 error = ERROR_SCRIPT;
7050 else
7051 {
7052 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7053 i = (int)STRLEN(fname_buf);
7054 }
7055 }
7056 if (i + STRLEN(name + llen) < FLEN_FIXED)
7057 {
7058 STRCPY(fname_buf + i, name + llen);
7059 fname = fname_buf;
7060 }
7061 else
7062 {
7063 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7064 if (fname == NULL)
7065 error = ERROR_OTHER;
7066 else
7067 {
7068 mch_memmove(fname, fname_buf, (size_t)i);
7069 STRCPY(fname + i, name + llen);
7070 }
7071 }
7072 }
7073 else
7074 fname = name;
7075
7076 *doesrange = FALSE;
7077
7078
7079 /* execute the function if no errors detected and executing */
7080 if (evaluate && error == ERROR_NONE)
7081 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007082 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083 error = ERROR_UNKNOWN;
7084
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007085 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086 {
7087 /*
7088 * User defined function.
7089 */
7090 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007091
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007093 /* Trigger FuncUndefined event, may load the function. */
7094 if (fp == NULL
7095 && apply_autocmds(EVENT_FUNCUNDEFINED,
7096 fname, fname, TRUE, NULL)
7097 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007099 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 fp = find_func(fname);
7101 }
7102#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007103 /* Try loading a package. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007104 if (fp == NULL && script_autoload(fname) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007105 {
7106 /* loaded a package, search for the function again */
7107 fp = find_func(fname);
7108 }
7109
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110 if (fp != NULL)
7111 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007112 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007114 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007116 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007118 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007119 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 else
7121 {
7122 /*
7123 * Call the user function.
7124 * Save and restore search patterns, script variables and
7125 * redo buffer.
7126 */
7127 save_search_patterns();
7128 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007129 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007130 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007131 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007132 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7133 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7134 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007135 /* Function was unreferenced while being used, free it
7136 * now. */
7137 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138 restoreRedobuff();
7139 restore_search_patterns();
7140 error = ERROR_NONE;
7141 }
7142 }
7143 }
7144 else
7145 {
7146 /*
7147 * Find the function name in the table, call its implementation.
7148 */
7149 i = find_internal_func(fname);
7150 if (i >= 0)
7151 {
7152 if (argcount < functions[i].f_min_argc)
7153 error = ERROR_TOOFEW;
7154 else if (argcount > functions[i].f_max_argc)
7155 error = ERROR_TOOMANY;
7156 else
7157 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007158 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007159 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 error = ERROR_NONE;
7161 }
7162 }
7163 }
7164 /*
7165 * The function call (or "FuncUndefined" autocommand sequence) might
7166 * have been aborted by an error, an interrupt, or an explicitly thrown
7167 * exception that has not been caught so far. This situation can be
7168 * tested for by calling aborting(). For an error in an internal
7169 * function or for the "E132" error in call_user_func(), however, the
7170 * throw point at which the "force_abort" flag (temporarily reset by
7171 * emsg()) is normally updated has not been reached yet. We need to
7172 * update that flag first to make aborting() reliable.
7173 */
7174 update_force_abort();
7175 }
7176 if (error == ERROR_NONE)
7177 ret = OK;
7178
7179 /*
7180 * Report an error unless the argument evaluation or function call has been
7181 * cancelled due to an aborting error, an interrupt, or an exception.
7182 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007183 if (!aborting())
7184 {
7185 switch (error)
7186 {
7187 case ERROR_UNKNOWN:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007188 emsg_funcname("E117: Unknown function: %s", name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007189 break;
7190 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007191 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007192 break;
7193 case ERROR_TOOFEW:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007194 emsg_funcname("E119: Not enough arguments for function: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007195 name);
7196 break;
7197 case ERROR_SCRIPT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007198 emsg_funcname("E120: Using <SID> not in a script context: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007199 name);
7200 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007201 case ERROR_DICT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007202 emsg_funcname("E725: Calling dict function without Dictionary: %s",
Bram Moolenaare9a41262005-01-15 22:18:47 +00007203 name);
7204 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007205 }
7206 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207
7208 name[len] = cc;
7209 if (fname != name && fname != fname_buf)
7210 vim_free(fname);
7211
7212 return ret;
7213}
7214
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007215/*
7216 * Give an error message with a function name. Handle <SNR> things.
7217 */
7218 static void
7219emsg_funcname(msg, name)
7220 char *msg;
7221 char_u *name;
7222{
7223 char_u *p;
7224
7225 if (*name == K_SPECIAL)
7226 p = concat_str((char_u *)"<SNR>", name + 3);
7227 else
7228 p = name;
7229 EMSG2(_(msg), p);
7230 if (p != name)
7231 vim_free(p);
7232}
7233
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234/*********************************************
7235 * Implementation of the built-in functions
7236 */
7237
7238/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007239 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240 */
7241 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007242f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007243 typval_T *argvars;
7244 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245{
Bram Moolenaar33570922005-01-25 22:26:29 +00007246 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007248 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007249 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007251 if ((l = argvars[0].vval.v_list) != NULL
7252 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7253 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007254 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007255 }
7256 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007257 EMSG(_(e_listreq));
7258}
7259
7260/*
7261 * "append(lnum, string/list)" function
7262 */
7263 static void
7264f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007265 typval_T *argvars;
7266 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007267{
7268 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007269 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007270 list_T *l = NULL;
7271 listitem_T *li = NULL;
7272 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007273 long added = 0;
7274
Bram Moolenaar0d660222005-01-07 21:51:51 +00007275 lnum = get_tv_lnum(argvars);
7276 if (lnum >= 0
7277 && lnum <= curbuf->b_ml.ml_line_count
7278 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007279 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007280 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007281 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007282 l = argvars[1].vval.v_list;
7283 if (l == NULL)
7284 return;
7285 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007286 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007287 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007288 for (;;)
7289 {
7290 if (l == NULL)
7291 tv = &argvars[1]; /* append a string */
7292 else if (li == NULL)
7293 break; /* end of list */
7294 else
7295 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007296 line = get_tv_string_chk(tv);
7297 if (line == NULL) /* type error */
7298 {
7299 rettv->vval.v_number = 1; /* Failed */
7300 break;
7301 }
7302 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007303 ++added;
7304 if (l == NULL)
7305 break;
7306 li = li->li_next;
7307 }
7308
7309 appended_lines_mark(lnum, added);
7310 if (curwin->w_cursor.lnum > lnum)
7311 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007313 else
7314 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315}
7316
7317/*
7318 * "argc()" function
7319 */
7320/* ARGSUSED */
7321 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007322f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007323 typval_T *argvars;
7324 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007326 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327}
7328
7329/*
7330 * "argidx()" function
7331 */
7332/* ARGSUSED */
7333 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007334f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007335 typval_T *argvars;
7336 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007338 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339}
7340
7341/*
7342 * "argv(nr)" function
7343 */
7344 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007345f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007346 typval_T *argvars;
7347 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348{
7349 int idx;
7350
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007351 idx = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352 if (idx >= 0 && idx < ARGCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007353 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007355 rettv->vval.v_string = NULL;
7356 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357}
7358
7359/*
7360 * "browse(save, title, initdir, default)" function
7361 */
7362/* ARGSUSED */
7363 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007364f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007365 typval_T *argvars;
7366 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367{
7368#ifdef FEAT_BROWSE
7369 int save;
7370 char_u *title;
7371 char_u *initdir;
7372 char_u *defname;
7373 char_u buf[NUMBUFLEN];
7374 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007375 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007377 save = get_tv_number_chk(&argvars[0], &error);
7378 title = get_tv_string_chk(&argvars[1]);
7379 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7380 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007382 if (error || title == NULL || initdir == NULL || defname == NULL)
7383 rettv->vval.v_string = NULL;
7384 else
7385 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007386 do_browse(save ? BROWSE_SAVE : 0,
7387 title, defname, NULL, initdir, NULL, curbuf);
7388#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007389 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007390#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007391 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007392}
7393
7394/*
7395 * "browsedir(title, initdir)" function
7396 */
7397/* ARGSUSED */
7398 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007399f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007400 typval_T *argvars;
7401 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007402{
7403#ifdef FEAT_BROWSE
7404 char_u *title;
7405 char_u *initdir;
7406 char_u buf[NUMBUFLEN];
7407
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007408 title = get_tv_string_chk(&argvars[0]);
7409 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007410
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007411 if (title == NULL || initdir == NULL)
7412 rettv->vval.v_string = NULL;
7413 else
7414 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007415 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007417 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007419 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420}
7421
Bram Moolenaar33570922005-01-25 22:26:29 +00007422static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007423
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424/*
7425 * Find a buffer by number or exact name.
7426 */
7427 static buf_T *
7428find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007429 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430{
7431 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007433 if (avar->v_type == VAR_NUMBER)
7434 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007435 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007437 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007438 if (buf == NULL)
7439 {
7440 /* No full path name match, try a match with a URL or a "nofile"
7441 * buffer, these don't use the full path. */
7442 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7443 if (buf->b_fname != NULL
7444 && (path_with_url(buf->b_fname)
7445#ifdef FEAT_QUICKFIX
7446 || bt_nofile(buf)
7447#endif
7448 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007449 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007450 break;
7451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452 }
7453 return buf;
7454}
7455
7456/*
7457 * "bufexists(expr)" function
7458 */
7459 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007460f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007461 typval_T *argvars;
7462 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007464 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465}
7466
7467/*
7468 * "buflisted(expr)" function
7469 */
7470 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007471f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007472 typval_T *argvars;
7473 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474{
7475 buf_T *buf;
7476
7477 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007478 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479}
7480
7481/*
7482 * "bufloaded(expr)" function
7483 */
7484 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007485f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007486 typval_T *argvars;
7487 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488{
7489 buf_T *buf;
7490
7491 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007492 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493}
7494
Bram Moolenaar33570922005-01-25 22:26:29 +00007495static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007496
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497/*
7498 * Get buffer by number or pattern.
7499 */
7500 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007501get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007502 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007504 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505 int save_magic;
7506 char_u *save_cpo;
7507 buf_T *buf;
7508
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007509 if (tv->v_type == VAR_NUMBER)
7510 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007511 if (tv->v_type != VAR_STRING)
7512 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513 if (name == NULL || *name == NUL)
7514 return curbuf;
7515 if (name[0] == '$' && name[1] == NUL)
7516 return lastbuf;
7517
7518 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7519 save_magic = p_magic;
7520 p_magic = TRUE;
7521 save_cpo = p_cpo;
7522 p_cpo = (char_u *)"";
7523
7524 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
7525 TRUE, FALSE));
7526
7527 p_magic = save_magic;
7528 p_cpo = save_cpo;
7529
7530 /* If not found, try expanding the name, like done for bufexists(). */
7531 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007532 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533
7534 return buf;
7535}
7536
7537/*
7538 * "bufname(expr)" function
7539 */
7540 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007541f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007542 typval_T *argvars;
7543 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544{
7545 buf_T *buf;
7546
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007547 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007549 buf = get_buf_tv(&argvars[0]);
7550 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007552 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007554 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007555 --emsg_off;
7556}
7557
7558/*
7559 * "bufnr(expr)" function
7560 */
7561 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007562f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007563 typval_T *argvars;
7564 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565{
7566 buf_T *buf;
7567
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007568 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007570 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007572 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007574 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575 --emsg_off;
7576}
7577
7578/*
7579 * "bufwinnr(nr)" function
7580 */
7581 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007582f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007583 typval_T *argvars;
7584 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585{
7586#ifdef FEAT_WINDOWS
7587 win_T *wp;
7588 int winnr = 0;
7589#endif
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#ifdef FEAT_WINDOWS
7596 for (wp = firstwin; wp; wp = wp->w_next)
7597 {
7598 ++winnr;
7599 if (wp->w_buffer == buf)
7600 break;
7601 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007602 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007604 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605#endif
7606 --emsg_off;
7607}
7608
7609/*
7610 * "byte2line(byte)" function
7611 */
7612/*ARGSUSED*/
7613 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007614f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007615 typval_T *argvars;
7616 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617{
7618#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007619 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620#else
7621 long boff = 0;
7622
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007623 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007625 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007627 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628 (linenr_T)0, &boff);
7629#endif
7630}
7631
7632/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007633 * "byteidx()" function
7634 */
7635/*ARGSUSED*/
7636 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007637f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007638 typval_T *argvars;
7639 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007640{
7641#ifdef FEAT_MBYTE
7642 char_u *t;
7643#endif
7644 char_u *str;
7645 long idx;
7646
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007647 str = get_tv_string_chk(&argvars[0]);
7648 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007649 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007650 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007651 return;
7652
7653#ifdef FEAT_MBYTE
7654 t = str;
7655 for ( ; idx > 0; idx--)
7656 {
7657 if (*t == NUL) /* EOL reached */
7658 return;
7659 t += mb_ptr2len_check(t);
7660 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007661 rettv->vval.v_number = t - str;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007662#else
7663 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007664 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007665#endif
7666}
7667
7668/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007669 * "call(func, arglist)" function
7670 */
7671 static void
7672f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007673 typval_T *argvars;
7674 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007675{
7676 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00007677 typval_T argv[MAX_FUNC_ARGS];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007678 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007679 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007680 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00007681 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007682
7683 rettv->vval.v_number = 0;
7684 if (argvars[1].v_type != VAR_LIST)
7685 {
7686 EMSG(_(e_listreq));
7687 return;
7688 }
7689 if (argvars[1].vval.v_list == NULL)
7690 return;
7691
7692 if (argvars[0].v_type == VAR_FUNC)
7693 func = argvars[0].vval.v_string;
7694 else
7695 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007696 if (*func == NUL)
7697 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007698
Bram Moolenaare9a41262005-01-15 22:18:47 +00007699 if (argvars[2].v_type != VAR_UNKNOWN)
7700 {
7701 if (argvars[2].v_type != VAR_DICT)
7702 {
7703 EMSG(_(e_dictreq));
7704 return;
7705 }
7706 selfdict = argvars[2].vval.v_dict;
7707 }
7708
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007709 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
7710 item = item->li_next)
7711 {
7712 if (argc == MAX_FUNC_ARGS)
7713 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007714 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007715 break;
7716 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007717 /* Make a copy of each argument. This is needed to be able to set
7718 * v_lock to VAR_FIXED in the copy without changing the original list.
7719 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007720 copy_tv(&item->li_tv, &argv[argc++]);
7721 }
7722
7723 if (item == NULL)
7724 (void)call_func(func, STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007725 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
7726 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007727
7728 /* Free the arguments. */
7729 while (argc > 0)
7730 clear_tv(&argv[--argc]);
7731}
7732
7733/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734 * "char2nr(string)" function
7735 */
7736 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007737f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007738 typval_T *argvars;
7739 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740{
7741#ifdef FEAT_MBYTE
7742 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007743 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744 else
7745#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007746 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747}
7748
7749/*
7750 * "cindent(lnum)" function
7751 */
7752 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007753f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007754 typval_T *argvars;
7755 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756{
7757#ifdef FEAT_CINDENT
7758 pos_T pos;
7759 linenr_T lnum;
7760
7761 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007762 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7764 {
7765 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007766 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767 curwin->w_cursor = pos;
7768 }
7769 else
7770#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007771 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772}
7773
7774/*
7775 * "col(string)" function
7776 */
7777 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007778f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007779 typval_T *argvars;
7780 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781{
7782 colnr_T col = 0;
7783 pos_T *fp;
7784
7785 fp = var2fpos(&argvars[0], FALSE);
7786 if (fp != NULL)
7787 {
7788 if (fp->col == MAXCOL)
7789 {
7790 /* '> can be MAXCOL, get the length of the line then */
7791 if (fp->lnum <= curbuf->b_ml.ml_line_count)
7792 col = STRLEN(ml_get(fp->lnum)) + 1;
7793 else
7794 col = MAXCOL;
7795 }
7796 else
7797 {
7798 col = fp->col + 1;
7799#ifdef FEAT_VIRTUALEDIT
7800 /* col(".") when the cursor is on the NUL at the end of the line
7801 * because of "coladd" can be seen as an extra column. */
7802 if (virtual_active() && fp == &curwin->w_cursor)
7803 {
7804 char_u *p = ml_get_cursor();
7805
7806 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
7807 curwin->w_virtcol - curwin->w_cursor.coladd))
7808 {
7809# ifdef FEAT_MBYTE
7810 int l;
7811
7812 if (*p != NUL && p[(l = (*mb_ptr2len_check)(p))] == NUL)
7813 col += l;
7814# else
7815 if (*p != NUL && p[1] == NUL)
7816 ++col;
7817# endif
7818 }
7819 }
7820#endif
7821 }
7822 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007823 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824}
7825
7826/*
7827 * "confirm(message, buttons[, default [, type]])" function
7828 */
7829/*ARGSUSED*/
7830 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007831f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007832 typval_T *argvars;
7833 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834{
7835#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
7836 char_u *message;
7837 char_u *buttons = NULL;
7838 char_u buf[NUMBUFLEN];
7839 char_u buf2[NUMBUFLEN];
7840 int def = 1;
7841 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007842 char_u *typestr;
7843 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007845 message = get_tv_string_chk(&argvars[0]);
7846 if (message == NULL)
7847 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007848 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007850 buttons = get_tv_string_buf_chk(&argvars[1], buf);
7851 if (buttons == NULL)
7852 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007853 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007855 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007856 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007858 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
7859 if (typestr == NULL)
7860 error = TRUE;
7861 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007863 switch (TOUPPER_ASC(*typestr))
7864 {
7865 case 'E': type = VIM_ERROR; break;
7866 case 'Q': type = VIM_QUESTION; break;
7867 case 'I': type = VIM_INFO; break;
7868 case 'W': type = VIM_WARNING; break;
7869 case 'G': type = VIM_GENERIC; break;
7870 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871 }
7872 }
7873 }
7874 }
7875
7876 if (buttons == NULL || *buttons == NUL)
7877 buttons = (char_u *)_("&Ok");
7878
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007879 if (error)
7880 rettv->vval.v_number = 0;
7881 else
7882 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883 def, NULL);
7884#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007885 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886#endif
7887}
7888
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007889/*
7890 * "copy()" function
7891 */
7892 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007893f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007894 typval_T *argvars;
7895 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007896{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007897 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007898}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899
7900/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007901 * "count()" function
7902 */
7903 static void
7904f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007905 typval_T *argvars;
7906 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007907{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007908 long n = 0;
7909 int ic = FALSE;
7910
Bram Moolenaare9a41262005-01-15 22:18:47 +00007911 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007912 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007913 listitem_T *li;
7914 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007915 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007916
Bram Moolenaare9a41262005-01-15 22:18:47 +00007917 if ((l = argvars[0].vval.v_list) != NULL)
7918 {
7919 li = l->lv_first;
7920 if (argvars[2].v_type != VAR_UNKNOWN)
7921 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007922 int error = FALSE;
7923
7924 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007925 if (argvars[3].v_type != VAR_UNKNOWN)
7926 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007927 idx = get_tv_number_chk(&argvars[3], &error);
7928 if (!error)
7929 {
7930 li = list_find(l, idx);
7931 if (li == NULL)
7932 EMSGN(_(e_listidx), idx);
7933 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007934 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007935 if (error)
7936 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007937 }
7938
7939 for ( ; li != NULL; li = li->li_next)
7940 if (tv_equal(&li->li_tv, &argvars[1], ic))
7941 ++n;
7942 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007943 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007944 else if (argvars[0].v_type == VAR_DICT)
7945 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007946 int todo;
7947 dict_T *d;
7948 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007949
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007950 if ((d = argvars[0].vval.v_dict) != NULL)
7951 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007952 int error = FALSE;
7953
Bram Moolenaare9a41262005-01-15 22:18:47 +00007954 if (argvars[2].v_type != VAR_UNKNOWN)
7955 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007956 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007957 if (argvars[3].v_type != VAR_UNKNOWN)
7958 EMSG(_(e_invarg));
7959 }
7960
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007961 todo = error ? 0 : d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007962 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007963 {
7964 if (!HASHITEM_EMPTY(hi))
7965 {
7966 --todo;
7967 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
7968 ++n;
7969 }
7970 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007971 }
7972 }
7973 else
7974 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007975 rettv->vval.v_number = n;
7976}
7977
7978/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
7980 *
7981 * Checks the existence of a cscope connection.
7982 */
7983/*ARGSUSED*/
7984 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007985f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007986 typval_T *argvars;
7987 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988{
7989#ifdef FEAT_CSCOPE
7990 int num = 0;
7991 char_u *dbpath = NULL;
7992 char_u *prepend = NULL;
7993 char_u buf[NUMBUFLEN];
7994
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007995 if (argvars[0].v_type != VAR_UNKNOWN
7996 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007998 num = (int)get_tv_number(&argvars[0]);
7999 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008000 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008001 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002 }
8003
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008004 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008006 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007#endif
8008}
8009
8010/*
8011 * "cursor(lnum, col)" function
8012 *
8013 * Moves the cursor to the specified line and column
8014 */
8015/*ARGSUSED*/
8016 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008017f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008018 typval_T *argvars;
8019 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008020{
8021 long line, col;
8022
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008023 line = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008024 col = get_tv_number_chk(&argvars[1], NULL);
8025 if (line < 0 || col < 0)
8026 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027 if (line > 0)
8028 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029 if (col > 0)
8030 curwin->w_cursor.col = col - 1;
8031#ifdef FEAT_VIRTUALEDIT
8032 curwin->w_cursor.coladd = 0;
8033#endif
8034
8035 /* Make sure the cursor is in a valid position. */
8036 check_cursor();
8037#ifdef FEAT_MBYTE
8038 /* Correct cursor for multi-byte character. */
8039 if (has_mbyte)
8040 mb_adjust_cursor();
8041#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008042
8043 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044}
8045
8046/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008047 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008048 */
8049 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008050f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008051 typval_T *argvars;
8052 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008054 int noref = 0;
8055
8056 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008057 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008058 if (noref < 0 || noref > 1)
8059 EMSG(_(e_invarg));
8060 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008061 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062}
8063
8064/*
8065 * "delete()" function
8066 */
8067 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008068f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008069 typval_T *argvars;
8070 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071{
8072 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008073 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008075 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076}
8077
8078/*
8079 * "did_filetype()" function
8080 */
8081/*ARGSUSED*/
8082 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008083f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008084 typval_T *argvars;
8085 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086{
8087#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008088 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008090 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091#endif
8092}
8093
8094/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008095 * "diff_filler()" function
8096 */
8097/*ARGSUSED*/
8098 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008099f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008100 typval_T *argvars;
8101 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008102{
8103#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008104 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008105#endif
8106}
8107
8108/*
8109 * "diff_hlID()" function
8110 */
8111/*ARGSUSED*/
8112 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008113f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008114 typval_T *argvars;
8115 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008116{
8117#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008118 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008119 static linenr_T prev_lnum = 0;
8120 static int changedtick = 0;
8121 static int fnum = 0;
8122 static int change_start = 0;
8123 static int change_end = 0;
8124 static enum hlf_value hlID = 0;
8125 int filler_lines;
8126 int col;
8127
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008128 if (lnum < 0) /* ignore type error in {lnum} arg */
8129 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008130 if (lnum != prev_lnum
8131 || changedtick != curbuf->b_changedtick
8132 || fnum != curbuf->b_fnum)
8133 {
8134 /* New line, buffer, change: need to get the values. */
8135 filler_lines = diff_check(curwin, lnum);
8136 if (filler_lines < 0)
8137 {
8138 if (filler_lines == -1)
8139 {
8140 change_start = MAXCOL;
8141 change_end = -1;
8142 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8143 hlID = HLF_ADD; /* added line */
8144 else
8145 hlID = HLF_CHD; /* changed line */
8146 }
8147 else
8148 hlID = HLF_ADD; /* added line */
8149 }
8150 else
8151 hlID = (enum hlf_value)0;
8152 prev_lnum = lnum;
8153 changedtick = curbuf->b_changedtick;
8154 fnum = curbuf->b_fnum;
8155 }
8156
8157 if (hlID == HLF_CHD || hlID == HLF_TXD)
8158 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008159 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008160 if (col >= change_start && col <= change_end)
8161 hlID = HLF_TXD; /* changed text */
8162 else
8163 hlID = HLF_CHD; /* changed line */
8164 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008165 rettv->vval.v_number = hlID == (enum hlf_value)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008166#endif
8167}
8168
8169/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008170 * "empty({expr})" function
8171 */
8172 static void
8173f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008174 typval_T *argvars;
8175 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008176{
8177 int n;
8178
8179 switch (argvars[0].v_type)
8180 {
8181 case VAR_STRING:
8182 case VAR_FUNC:
8183 n = argvars[0].vval.v_string == NULL
8184 || *argvars[0].vval.v_string == NUL;
8185 break;
8186 case VAR_NUMBER:
8187 n = argvars[0].vval.v_number == 0;
8188 break;
8189 case VAR_LIST:
8190 n = argvars[0].vval.v_list == NULL
8191 || argvars[0].vval.v_list->lv_first == NULL;
8192 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008193 case VAR_DICT:
8194 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008195 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008196 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008197 default:
8198 EMSG2(_(e_intern2), "f_empty()");
8199 n = 0;
8200 }
8201
8202 rettv->vval.v_number = n;
8203}
8204
8205/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206 * "escape({string}, {chars})" function
8207 */
8208 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008209f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008210 typval_T *argvars;
8211 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212{
8213 char_u buf[NUMBUFLEN];
8214
Bram Moolenaar758711c2005-02-02 23:11:38 +00008215 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8216 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008217 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218}
8219
8220/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008221 * "eval()" function
8222 */
8223/*ARGSUSED*/
8224 static void
8225f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008226 typval_T *argvars;
8227 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008228{
8229 char_u *s;
8230
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008231 s = get_tv_string_chk(&argvars[0]);
8232 if (s != NULL)
8233 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008234
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008235 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8236 {
8237 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008238 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008239 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008240 else if (*s != NUL)
8241 EMSG(_(e_trailing));
8242}
8243
8244/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 * "eventhandler()" function
8246 */
8247/*ARGSUSED*/
8248 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008249f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008250 typval_T *argvars;
8251 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008253 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254}
8255
8256/*
8257 * "executable()" function
8258 */
8259 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008260f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008261 typval_T *argvars;
8262 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008264 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265}
8266
8267/*
8268 * "exists()" function
8269 */
8270 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008271f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008272 typval_T *argvars;
8273 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274{
8275 char_u *p;
8276 char_u *name;
8277 int n = FALSE;
8278 int len = 0;
8279
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008280 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281 if (*p == '$') /* environment variable */
8282 {
8283 /* first try "normal" environment variables (fast) */
8284 if (mch_getenv(p + 1) != NULL)
8285 n = TRUE;
8286 else
8287 {
8288 /* try expanding things like $VIM and ${HOME} */
8289 p = expand_env_save(p);
8290 if (p != NULL && *p != '$')
8291 n = TRUE;
8292 vim_free(p);
8293 }
8294 }
8295 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008296 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297 else if (*p == '*') /* internal or user defined function */
8298 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008299 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300 }
8301 else if (*p == ':')
8302 {
8303 n = cmd_exists(p + 1);
8304 }
8305 else if (*p == '#')
8306 {
8307#ifdef FEAT_AUTOCMD
8308 name = p + 1;
8309 p = vim_strchr(name, '#');
8310 if (p != NULL)
8311 n = au_exists(name, p, p + 1);
8312 else
8313 n = au_exists(name, name + STRLEN(name), NULL);
8314#endif
8315 }
8316 else /* internal variable */
8317 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008318 char_u *tofree;
8319 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008321 /* get_name_len() takes care of expanding curly braces */
8322 name = p;
8323 len = get_name_len(&p, &tofree, TRUE, FALSE);
8324 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008326 if (tofree != NULL)
8327 name = tofree;
8328 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8329 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008331 /* handle d.key, l[idx], f(expr) */
8332 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8333 if (n)
8334 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 }
8336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008338 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339 }
8340
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008341 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342}
8343
8344/*
8345 * "expand()" function
8346 */
8347 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008348f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008349 typval_T *argvars;
8350 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351{
8352 char_u *s;
8353 int len;
8354 char_u *errormsg;
8355 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8356 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008357 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008359 rettv->v_type = VAR_STRING;
8360 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361 if (*s == '%' || *s == '#' || *s == '<')
8362 {
8363 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008364 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365 --emsg_off;
8366 }
8367 else
8368 {
8369 /* When the optional second argument is non-zero, don't remove matches
8370 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008371 if (argvars[1].v_type != VAR_UNKNOWN
8372 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008374 if (!error)
8375 {
8376 ExpandInit(&xpc);
8377 xpc.xp_context = EXPAND_FILES;
8378 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
8379 ExpandCleanup(&xpc);
8380 }
8381 else
8382 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383 }
8384}
8385
8386/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008387 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00008388 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008389 */
8390 static void
8391f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008392 typval_T *argvars;
8393 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008394{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008395 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008396 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008397 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008398 list_T *l1, *l2;
8399 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008400 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008401 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008402
Bram Moolenaare9a41262005-01-15 22:18:47 +00008403 l1 = argvars[0].vval.v_list;
8404 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008405 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
8406 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008407 {
8408 if (argvars[2].v_type != VAR_UNKNOWN)
8409 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008410 before = get_tv_number_chk(&argvars[2], &error);
8411 if (error)
8412 return; /* type error; errmsg already given */
8413
Bram Moolenaar758711c2005-02-02 23:11:38 +00008414 if (before == l1->lv_len)
8415 item = NULL;
8416 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008417 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00008418 item = list_find(l1, before);
8419 if (item == NULL)
8420 {
8421 EMSGN(_(e_listidx), before);
8422 return;
8423 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008424 }
8425 }
8426 else
8427 item = NULL;
8428 list_extend(l1, l2, item);
8429
Bram Moolenaare9a41262005-01-15 22:18:47 +00008430 copy_tv(&argvars[0], rettv);
8431 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008432 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008433 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
8434 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008435 dict_T *d1, *d2;
8436 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008437 char_u *action;
8438 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00008439 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008440 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008441
8442 d1 = argvars[0].vval.v_dict;
8443 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008444 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
8445 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008446 {
8447 /* Check the third argument. */
8448 if (argvars[2].v_type != VAR_UNKNOWN)
8449 {
8450 static char *(av[]) = {"keep", "force", "error"};
8451
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008452 action = get_tv_string_chk(&argvars[2]);
8453 if (action == NULL)
8454 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008455 for (i = 0; i < 3; ++i)
8456 if (STRCMP(action, av[i]) == 0)
8457 break;
8458 if (i == 3)
8459 {
8460 EMSGN(_(e_invarg2), action);
8461 return;
8462 }
8463 }
8464 else
8465 action = (char_u *)"force";
8466
8467 /* Go over all entries in the second dict and add them to the
8468 * first dict. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008469 todo = d2->dv_hashtab.ht_used;
8470 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008471 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008472 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008473 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008474 --todo;
8475 di1 = dict_find(d1, hi2->hi_key, -1);
8476 if (di1 == NULL)
8477 {
8478 di1 = dictitem_copy(HI2DI(hi2));
8479 if (di1 != NULL && dict_add(d1, di1) == FAIL)
8480 dictitem_free(di1);
8481 }
8482 else if (*action == 'e')
8483 {
8484 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
8485 break;
8486 }
8487 else if (*action == 'f')
8488 {
8489 clear_tv(&di1->di_tv);
8490 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
8491 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008492 }
8493 }
8494
Bram Moolenaare9a41262005-01-15 22:18:47 +00008495 copy_tv(&argvars[0], rettv);
8496 }
8497 }
8498 else
8499 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008500}
8501
8502/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503 * "filereadable()" function
8504 */
8505 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008506f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008507 typval_T *argvars;
8508 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509{
8510 FILE *fd;
8511 char_u *p;
8512 int n;
8513
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008514 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
8516 {
8517 n = TRUE;
8518 fclose(fd);
8519 }
8520 else
8521 n = FALSE;
8522
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008523 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524}
8525
8526/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008527 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528 * rights to write into.
8529 */
8530 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008531f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008532 typval_T *argvars;
8533 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008535 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536}
8537
Bram Moolenaar33570922005-01-25 22:26:29 +00008538static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008539
8540 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008541findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00008542 typval_T *argvars;
8543 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008544 int dir;
8545{
8546#ifdef FEAT_SEARCHPATH
8547 char_u *fname;
8548 char_u *fresult = NULL;
8549 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
8550 char_u *p;
8551 char_u pathbuf[NUMBUFLEN];
8552 int count = 1;
8553 int first = TRUE;
8554
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008555 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008556
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008557 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008558 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008559 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
8560 if (p == NULL)
8561 count = -1; /* error */
8562 else
8563 {
8564 if (*p != NUL)
8565 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008566
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008567 if (argvars[2].v_type != VAR_UNKNOWN)
8568 count = get_tv_number_chk(&argvars[2], NULL); /* -1: error */
8569 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008570 }
8571
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008572 if (*fname != NUL && count >= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008573 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008574 do
8575 {
8576 vim_free(fresult);
8577 fresult = find_file_in_path_option(first ? fname : NULL,
8578 first ? (int)STRLEN(fname) : 0,
8579 0, first, path, dir, NULL);
8580 first = FALSE;
8581 } while (--count > 0 && fresult != NULL);
8582 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008583
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008584 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008585#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008586 rettv->vval.v_string = NULL;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008587#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008588 rettv->v_type = VAR_STRING;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008589}
8590
Bram Moolenaar33570922005-01-25 22:26:29 +00008591static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
8592static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008593
8594/*
8595 * Implementation of map() and filter().
8596 */
8597 static void
8598filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00008599 typval_T *argvars;
8600 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008601 int map;
8602{
8603 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00008604 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00008605 listitem_T *li, *nli;
8606 list_T *l = NULL;
8607 dictitem_T *di;
8608 hashtab_T *ht;
8609 hashitem_T *hi;
8610 dict_T *d = NULL;
8611 typval_T save_val;
8612 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008613 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008614 int todo;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008615 char_u *msg = map ? (char_u *)"map()" : (char_u *)"filter()";
8616
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008617
8618 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008619 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008620 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008621 if ((l = argvars[0].vval.v_list) == NULL
8622 || (map && tv_check_lock(l->lv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008623 return;
8624 }
8625 else if (argvars[0].v_type == VAR_DICT)
8626 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008627 if ((d = argvars[0].vval.v_dict) == NULL
8628 || (map && tv_check_lock(d->dv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008629 return;
8630 }
8631 else
8632 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008633 EMSG2(_(e_listdictarg), msg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008634 return;
8635 }
8636
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008637 expr = get_tv_string_buf_chk(&argvars[1], buf);
8638 /* On type errors, the preceding call has already displayed an error
8639 * message. Avoid a misleading error message for an empty string that
8640 * was not passed as argument. */
8641 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008642 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008643 prepare_vimvar(VV_VAL, &save_val);
8644 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008645
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008646 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008647 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008648 prepare_vimvar(VV_KEY, &save_key);
8649 vimvars[VV_KEY].vv_type = VAR_STRING;
8650
8651 ht = &d->dv_hashtab;
8652 hash_lock(ht);
8653 todo = ht->ht_used;
8654 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008655 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008656 if (!HASHITEM_EMPTY(hi))
8657 {
8658 --todo;
8659 di = HI2DI(hi);
8660 if (tv_check_lock(di->di_tv.v_lock, msg))
8661 break;
8662 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
8663 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL)
8664 break;
8665 if (!map && rem)
8666 dictitem_remove(d, di);
8667 clear_tv(&vimvars[VV_KEY].vv_tv);
8668 }
8669 }
8670 hash_unlock(ht);
8671
8672 restore_vimvar(VV_KEY, &save_key);
8673 }
8674 else
8675 {
8676 for (li = l->lv_first; li != NULL; li = nli)
8677 {
8678 if (tv_check_lock(li->li_tv.v_lock, msg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008679 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008680 nli = li->li_next;
8681 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008682 break;
8683 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008684 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008685 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008686 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008687
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008688 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008689 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008690
8691 copy_tv(&argvars[0], rettv);
8692}
8693
8694 static int
8695filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00008696 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008697 char_u *expr;
8698 int map;
8699 int *remp;
8700{
Bram Moolenaar33570922005-01-25 22:26:29 +00008701 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008702 char_u *s;
8703
Bram Moolenaar33570922005-01-25 22:26:29 +00008704 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008705 s = expr;
8706 if (eval1(&s, &rettv, TRUE) == FAIL)
8707 return FAIL;
8708 if (*s != NUL) /* check for trailing chars after expr */
8709 {
8710 EMSG2(_(e_invexpr2), s);
8711 return FAIL;
8712 }
8713 if (map)
8714 {
8715 /* map(): replace the list item value */
8716 clear_tv(tv);
8717 *tv = rettv;
8718 }
8719 else
8720 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008721 int error = FALSE;
8722
Bram Moolenaare9a41262005-01-15 22:18:47 +00008723 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008724 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008725 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008726 /* On type error, nothing has been removed; return FAIL to stop the
8727 * loop. The error message was given by get_tv_number_chk(). */
8728 if (error)
8729 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008730 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008731 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008732 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008733}
8734
8735/*
8736 * "filter()" function
8737 */
8738 static void
8739f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008740 typval_T *argvars;
8741 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008742{
8743 filter_map(argvars, rettv, FALSE);
8744}
8745
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008746/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008747 * "finddir({fname}[, {path}[, {count}]])" function
8748 */
8749 static void
8750f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008751 typval_T *argvars;
8752 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008753{
8754 findfilendir(argvars, rettv, TRUE);
8755}
8756
8757/*
8758 * "findfile({fname}[, {path}[, {count}]])" function
8759 */
8760 static void
8761f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008762 typval_T *argvars;
8763 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008764{
8765 findfilendir(argvars, rettv, FALSE);
8766}
8767
8768/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769 * "fnamemodify({fname}, {mods})" function
8770 */
8771 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008772f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008773 typval_T *argvars;
8774 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775{
8776 char_u *fname;
8777 char_u *mods;
8778 int usedlen = 0;
8779 int len;
8780 char_u *fbuf = NULL;
8781 char_u buf[NUMBUFLEN];
8782
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008783 fname = get_tv_string_chk(&argvars[0]);
8784 mods = get_tv_string_buf_chk(&argvars[1], buf);
8785 if (fname == NULL || mods == NULL)
8786 fname = NULL;
8787 else
8788 {
8789 len = (int)STRLEN(fname);
8790 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
8791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008792
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008793 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008795 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008796 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008797 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798 vim_free(fbuf);
8799}
8800
Bram Moolenaar33570922005-01-25 22:26:29 +00008801static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008802
8803/*
8804 * "foldclosed()" function
8805 */
8806 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008807foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00008808 typval_T *argvars;
8809 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008810 int end;
8811{
8812#ifdef FEAT_FOLDING
8813 linenr_T lnum;
8814 linenr_T first, last;
8815
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008816 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008817 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8818 {
8819 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
8820 {
8821 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008822 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008823 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008824 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825 return;
8826 }
8827 }
8828#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008829 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830}
8831
8832/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008833 * "foldclosed()" function
8834 */
8835 static void
8836f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008837 typval_T *argvars;
8838 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008839{
8840 foldclosed_both(argvars, rettv, FALSE);
8841}
8842
8843/*
8844 * "foldclosedend()" function
8845 */
8846 static void
8847f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008848 typval_T *argvars;
8849 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008850{
8851 foldclosed_both(argvars, rettv, TRUE);
8852}
8853
8854/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855 * "foldlevel()" function
8856 */
8857 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008858f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008859 typval_T *argvars;
8860 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861{
8862#ifdef FEAT_FOLDING
8863 linenr_T lnum;
8864
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008865 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008867 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868 else
8869#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008870 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871}
8872
8873/*
8874 * "foldtext()" function
8875 */
8876/*ARGSUSED*/
8877 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008878f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008879 typval_T *argvars;
8880 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881{
8882#ifdef FEAT_FOLDING
8883 linenr_T lnum;
8884 char_u *s;
8885 char_u *r;
8886 int len;
8887 char *txt;
8888#endif
8889
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008890 rettv->v_type = VAR_STRING;
8891 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00008893 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
8894 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
8895 <= curbuf->b_ml.ml_line_count
8896 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008897 {
8898 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008899 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
8900 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901 {
8902 if (!linewhite(lnum))
8903 break;
8904 ++lnum;
8905 }
8906
8907 /* Find interesting text in this line. */
8908 s = skipwhite(ml_get(lnum));
8909 /* skip C comment-start */
8910 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008911 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008913 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00008914 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008915 {
8916 s = skipwhite(ml_get(lnum + 1));
8917 if (*s == '*')
8918 s = skipwhite(s + 1);
8919 }
8920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921 txt = _("+-%s%3ld lines: ");
8922 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008923 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924 + 20 /* for %3ld */
8925 + STRLEN(s))); /* concatenated */
8926 if (r != NULL)
8927 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00008928 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
8929 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
8930 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931 len = (int)STRLEN(r);
8932 STRCAT(r, s);
8933 /* remove 'foldmarker' and 'commentstring' */
8934 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008935 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936 }
8937 }
8938#endif
8939}
8940
8941/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008942 * "foldtextresult(lnum)" function
8943 */
8944/*ARGSUSED*/
8945 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008946f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008947 typval_T *argvars;
8948 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008949{
8950#ifdef FEAT_FOLDING
8951 linenr_T lnum;
8952 char_u *text;
8953 char_u buf[51];
8954 foldinfo_T foldinfo;
8955 int fold_count;
8956#endif
8957
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008958 rettv->v_type = VAR_STRING;
8959 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008960#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008961 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008962 /* treat illegal types and illegal string values for {lnum} the same */
8963 if (lnum < 0)
8964 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008965 fold_count = foldedCount(curwin, lnum, &foldinfo);
8966 if (fold_count > 0)
8967 {
8968 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
8969 &foldinfo, buf);
8970 if (text == buf)
8971 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008972 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008973 }
8974#endif
8975}
8976
8977/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978 * "foreground()" function
8979 */
8980/*ARGSUSED*/
8981 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008982f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008983 typval_T *argvars;
8984 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008985{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008986 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008987#ifdef FEAT_GUI
8988 if (gui.in_use)
8989 gui_mch_set_foreground();
8990#else
8991# ifdef WIN32
8992 win32_set_foreground();
8993# endif
8994#endif
8995}
8996
8997/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008998 * "function()" function
8999 */
9000/*ARGSUSED*/
9001 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009002f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009003 typval_T *argvars;
9004 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009005{
9006 char_u *s;
9007
Bram Moolenaara7043832005-01-21 11:56:39 +00009008 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009009 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009010 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009011 EMSG2(_(e_invarg2), s);
9012 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009013 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009014 else
9015 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009016 rettv->vval.v_string = vim_strsave(s);
9017 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009018 }
9019}
9020
9021/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009022 * "garbagecollect()" function
9023 */
9024/*ARGSUSED*/
9025 static void
9026f_garbagecollect(argvars, rettv)
9027 typval_T *argvars;
9028 typval_T *rettv;
9029{
9030 garbage_collect();
9031}
9032
9033/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009034 * "get()" function
9035 */
9036 static void
9037f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009038 typval_T *argvars;
9039 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009040{
Bram Moolenaar33570922005-01-25 22:26:29 +00009041 listitem_T *li;
9042 list_T *l;
9043 dictitem_T *di;
9044 dict_T *d;
9045 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009046
Bram Moolenaare9a41262005-01-15 22:18:47 +00009047 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009048 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009049 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009050 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009051 int error = FALSE;
9052
9053 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9054 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009055 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009056 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009057 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009058 else if (argvars[0].v_type == VAR_DICT)
9059 {
9060 if ((d = argvars[0].vval.v_dict) != NULL)
9061 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009062 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009063 if (di != NULL)
9064 tv = &di->di_tv;
9065 }
9066 }
9067 else
9068 EMSG2(_(e_listdictarg), "get()");
9069
9070 if (tv == NULL)
9071 {
9072 if (argvars[2].v_type == VAR_UNKNOWN)
9073 rettv->vval.v_number = 0;
9074 else
9075 copy_tv(&argvars[2], rettv);
9076 }
9077 else
9078 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009079}
9080
Bram Moolenaar342337a2005-07-21 21:11:17 +00009081static 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 +00009082
9083/*
9084 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +00009085 * Return a range (from start to end) of lines in rettv from the specified
9086 * buffer.
9087 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009088 */
9089 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +00009090get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009091 buf_T *buf;
9092 linenr_T start;
9093 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009094 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009095 typval_T *rettv;
9096{
9097 char_u *p;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009098 list_T *l = NULL;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009099 listitem_T *li;
9100
Bram Moolenaar342337a2005-07-21 21:11:17 +00009101 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009102 {
Bram Moolenaar342337a2005-07-21 21:11:17 +00009103 l = list_alloc();
9104 if (l == NULL)
9105 return;
9106
9107 rettv->vval.v_list = l;
9108 rettv->v_type = VAR_LIST;
9109 ++l->lv_refcount;
9110 }
9111 else
9112 rettv->vval.v_number = 0;
9113
9114 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
9115 return;
9116
9117 if (!retlist)
9118 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009119 if (start >= 1 && start <= buf->b_ml.ml_line_count)
9120 p = ml_get_buf(buf, start, FALSE);
9121 else
9122 p = (char_u *)"";
9123
9124 rettv->v_type = VAR_STRING;
9125 rettv->vval.v_string = vim_strsave(p);
9126 }
9127 else
9128 {
9129 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009130 return;
9131
9132 if (start < 1)
9133 start = 1;
9134 if (end > buf->b_ml.ml_line_count)
9135 end = buf->b_ml.ml_line_count;
9136 while (start <= end)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009137 {
Bram Moolenaar342337a2005-07-21 21:11:17 +00009138 li = listitem_alloc();
9139 if (li == NULL)
9140 break;
9141 list_append(l, li);
9142 li->li_tv.v_type = VAR_STRING;
9143 li->li_tv.v_lock = 0;
9144 li->li_tv.vval.v_string =
9145 vim_strsave(ml_get_buf(buf, start++, FALSE));
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009146 }
9147 }
9148}
9149
9150/*
9151 * "getbufline()" function
9152 */
9153 static void
9154f_getbufline(argvars, rettv)
9155 typval_T *argvars;
9156 typval_T *rettv;
9157{
9158 linenr_T lnum;
9159 linenr_T end;
9160 buf_T *buf;
9161
9162 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9163 ++emsg_off;
9164 buf = get_buf_tv(&argvars[0]);
9165 --emsg_off;
9166
Bram Moolenaar342337a2005-07-21 21:11:17 +00009167 lnum = get_tv_lnum(&argvars[1]);
9168 if (argvars[2].v_type == VAR_UNKNOWN)
9169 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009170 else
Bram Moolenaar342337a2005-07-21 21:11:17 +00009171 end = get_tv_lnum(&argvars[2]);
9172 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009173}
9174
Bram Moolenaar0d660222005-01-07 21:51:51 +00009175/*
9176 * "getbufvar()" function
9177 */
9178 static void
9179f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009180 typval_T *argvars;
9181 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009182{
9183 buf_T *buf;
9184 buf_T *save_curbuf;
9185 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009186 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009187
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009188 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9189 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009190 ++emsg_off;
9191 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009192
9193 rettv->v_type = VAR_STRING;
9194 rettv->vval.v_string = NULL;
9195
9196 if (buf != NULL && varname != NULL)
9197 {
9198 if (*varname == '&') /* buffer-local-option */
9199 {
9200 /* set curbuf to be our buf, temporarily */
9201 save_curbuf = curbuf;
9202 curbuf = buf;
9203
9204 get_option_tv(&varname, rettv, TRUE);
9205
9206 /* restore previous notion of curbuf */
9207 curbuf = save_curbuf;
9208 }
9209 else
9210 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009211 if (*varname == NUL)
9212 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9213 * scope prefix before the NUL byte is required by
9214 * find_var_in_ht(). */
9215 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009216 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009217 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009218 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009219 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009220 }
9221 }
9222
9223 --emsg_off;
9224}
9225
9226/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009227 * "getchar()" function
9228 */
9229 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009230f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009231 typval_T *argvars;
9232 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009233{
9234 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009235 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009236
9237 ++no_mapping;
9238 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009239 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009240 /* getchar(): blocking wait. */
9241 n = safe_vgetc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009242 else if (get_tv_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009243 /* getchar(1): only check if char avail */
9244 n = vpeekc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009245 else if (error || vpeekc() == NUL)
9246 /* illegal argument or getchar(0) and no char avail: return zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009247 n = 0;
9248 else
9249 /* getchar(0) and char avail: return char */
9250 n = safe_vgetc();
9251 --no_mapping;
9252 --allow_keys;
9253
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009254 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009255 if (IS_SPECIAL(n) || mod_mask != 0)
9256 {
9257 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9258 int i = 0;
9259
9260 /* Turn a special key into three bytes, plus modifier. */
9261 if (mod_mask != 0)
9262 {
9263 temp[i++] = K_SPECIAL;
9264 temp[i++] = KS_MODIFIER;
9265 temp[i++] = mod_mask;
9266 }
9267 if (IS_SPECIAL(n))
9268 {
9269 temp[i++] = K_SPECIAL;
9270 temp[i++] = K_SECOND(n);
9271 temp[i++] = K_THIRD(n);
9272 }
9273#ifdef FEAT_MBYTE
9274 else if (has_mbyte)
9275 i += (*mb_char2bytes)(n, temp + i);
9276#endif
9277 else
9278 temp[i++] = n;
9279 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009280 rettv->v_type = VAR_STRING;
9281 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009282 }
9283}
9284
9285/*
9286 * "getcharmod()" function
9287 */
9288/*ARGSUSED*/
9289 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009290f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009291 typval_T *argvars;
9292 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009293{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009294 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009295}
9296
9297/*
9298 * "getcmdline()" function
9299 */
9300/*ARGSUSED*/
9301 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009302f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009303 typval_T *argvars;
9304 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009305{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009306 rettv->v_type = VAR_STRING;
9307 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009308}
9309
9310/*
9311 * "getcmdpos()" function
9312 */
9313/*ARGSUSED*/
9314 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009315f_getcmdpos(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 = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009320}
9321
9322/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009323 * "getcwd()" function
9324 */
9325/*ARGSUSED*/
9326 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009327f_getcwd(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{
9331 char_u cwd[MAXPATHL];
9332
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009333 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009334 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009335 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009336 else
9337 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009338 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009339#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +00009340 if (rettv->vval.v_string != NULL)
9341 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009342#endif
9343 }
9344}
9345
9346/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009347 * "getfontname()" function
9348 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009349/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009350 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009351f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009352 typval_T *argvars;
9353 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009354{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009355 rettv->v_type = VAR_STRING;
9356 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009357#ifdef FEAT_GUI
9358 if (gui.in_use)
9359 {
9360 GuiFont font;
9361 char_u *name = NULL;
9362
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009363 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009364 {
9365 /* Get the "Normal" font. Either the name saved by
9366 * hl_set_font_name() or from the font ID. */
9367 font = gui.norm_font;
9368 name = hl_get_font_name();
9369 }
9370 else
9371 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009372 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009373 if (STRCMP(name, "*") == 0) /* don't use font dialog */
9374 return;
9375 font = gui_mch_get_font(name, FALSE);
9376 if (font == NOFONT)
9377 return; /* Invalid font name, return empty string. */
9378 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009379 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009380 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009381 gui_mch_free_font(font);
9382 }
9383#endif
9384}
9385
9386/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009387 * "getfperm({fname})" function
9388 */
9389 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009390f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009391 typval_T *argvars;
9392 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009393{
9394 char_u *fname;
9395 struct stat st;
9396 char_u *perm = NULL;
9397 char_u flags[] = "rwx";
9398 int i;
9399
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009400 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009401
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009402 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009403 if (mch_stat((char *)fname, &st) >= 0)
9404 {
9405 perm = vim_strsave((char_u *)"---------");
9406 if (perm != NULL)
9407 {
9408 for (i = 0; i < 9; i++)
9409 {
9410 if (st.st_mode & (1 << (8 - i)))
9411 perm[i] = flags[i % 3];
9412 }
9413 }
9414 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009415 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009416}
9417
9418/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009419 * "getfsize({fname})" function
9420 */
9421 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009422f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009423 typval_T *argvars;
9424 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009425{
9426 char_u *fname;
9427 struct stat st;
9428
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009429 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009431 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009432
9433 if (mch_stat((char *)fname, &st) >= 0)
9434 {
9435 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009436 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009437 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009438 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439 }
9440 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009441 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009442}
9443
9444/*
9445 * "getftime({fname})" function
9446 */
9447 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009448f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009449 typval_T *argvars;
9450 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009451{
9452 char_u *fname;
9453 struct stat st;
9454
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009455 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009456
9457 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009458 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009459 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009460 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461}
9462
9463/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009464 * "getftype({fname})" function
9465 */
9466 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009467f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009468 typval_T *argvars;
9469 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009470{
9471 char_u *fname;
9472 struct stat st;
9473 char_u *type = NULL;
9474 char *t;
9475
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009476 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009477
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009478 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009479 if (mch_lstat((char *)fname, &st) >= 0)
9480 {
9481#ifdef S_ISREG
9482 if (S_ISREG(st.st_mode))
9483 t = "file";
9484 else if (S_ISDIR(st.st_mode))
9485 t = "dir";
9486# ifdef S_ISLNK
9487 else if (S_ISLNK(st.st_mode))
9488 t = "link";
9489# endif
9490# ifdef S_ISBLK
9491 else if (S_ISBLK(st.st_mode))
9492 t = "bdev";
9493# endif
9494# ifdef S_ISCHR
9495 else if (S_ISCHR(st.st_mode))
9496 t = "cdev";
9497# endif
9498# ifdef S_ISFIFO
9499 else if (S_ISFIFO(st.st_mode))
9500 t = "fifo";
9501# endif
9502# ifdef S_ISSOCK
9503 else if (S_ISSOCK(st.st_mode))
9504 t = "fifo";
9505# endif
9506 else
9507 t = "other";
9508#else
9509# ifdef S_IFMT
9510 switch (st.st_mode & S_IFMT)
9511 {
9512 case S_IFREG: t = "file"; break;
9513 case S_IFDIR: t = "dir"; break;
9514# ifdef S_IFLNK
9515 case S_IFLNK: t = "link"; break;
9516# endif
9517# ifdef S_IFBLK
9518 case S_IFBLK: t = "bdev"; break;
9519# endif
9520# ifdef S_IFCHR
9521 case S_IFCHR: t = "cdev"; break;
9522# endif
9523# ifdef S_IFIFO
9524 case S_IFIFO: t = "fifo"; break;
9525# endif
9526# ifdef S_IFSOCK
9527 case S_IFSOCK: t = "socket"; break;
9528# endif
9529 default: t = "other";
9530 }
9531# else
9532 if (mch_isdir(fname))
9533 t = "dir";
9534 else
9535 t = "file";
9536# endif
9537#endif
9538 type = vim_strsave((char_u *)t);
9539 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009540 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009541}
9542
9543/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009544 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +00009545 */
9546 static void
9547f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009548 typval_T *argvars;
9549 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009550{
9551 linenr_T lnum;
9552 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009553 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009554
9555 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009556 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009557 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009558 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009559 retlist = FALSE;
9560 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009561 else
Bram Moolenaar342337a2005-07-21 21:11:17 +00009562 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009563 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009564 retlist = TRUE;
9565 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009566
Bram Moolenaar342337a2005-07-21 21:11:17 +00009567 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009568}
9569
9570/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00009571 * "getqflist()" function
9572 */
9573/*ARGSUSED*/
9574 static void
9575f_getqflist(argvars, rettv)
9576 typval_T *argvars;
9577 typval_T *rettv;
9578{
9579#ifdef FEAT_QUICKFIX
9580 list_T *l;
9581#endif
9582
9583 rettv->vval.v_number = FALSE;
9584#ifdef FEAT_QUICKFIX
9585 l = list_alloc();
9586 if (l != NULL)
9587 {
9588 if (get_errorlist(l) != FAIL)
9589 {
9590 rettv->vval.v_list = l;
9591 rettv->v_type = VAR_LIST;
9592 ++l->lv_refcount;
9593 }
9594 else
9595 list_free(l);
9596 }
9597#endif
9598}
9599
9600/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601 * "getreg()" function
9602 */
9603 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009604f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009605 typval_T *argvars;
9606 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607{
9608 char_u *strregname;
9609 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009610 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009611 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009612
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009613 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009614 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009615 strregname = get_tv_string_chk(&argvars[0]);
9616 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009617 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009618 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009619 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009620 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009621 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009622 regname = (strregname == NULL ? '"' : *strregname);
9623 if (regname == 0)
9624 regname = '"';
9625
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009626 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009627 rettv->vval.v_string = error ? NULL :
9628 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009629}
9630
9631/*
9632 * "getregtype()" function
9633 */
9634 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009635f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009636 typval_T *argvars;
9637 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009638{
9639 char_u *strregname;
9640 int regname;
9641 char_u buf[NUMBUFLEN + 2];
9642 long reglen = 0;
9643
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009644 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009645 {
9646 strregname = get_tv_string_chk(&argvars[0]);
9647 if (strregname == NULL) /* type error; errmsg already given */
9648 {
9649 rettv->v_type = VAR_STRING;
9650 rettv->vval.v_string = NULL;
9651 return;
9652 }
9653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009654 else
9655 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009656 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009657
9658 regname = (strregname == NULL ? '"' : *strregname);
9659 if (regname == 0)
9660 regname = '"';
9661
9662 buf[0] = NUL;
9663 buf[1] = NUL;
9664 switch (get_reg_type(regname, &reglen))
9665 {
9666 case MLINE: buf[0] = 'V'; break;
9667 case MCHAR: buf[0] = 'v'; break;
9668#ifdef FEAT_VISUAL
9669 case MBLOCK:
9670 buf[0] = Ctrl_V;
9671 sprintf((char *)buf + 1, "%ld", reglen + 1);
9672 break;
9673#endif
9674 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009675 rettv->v_type = VAR_STRING;
9676 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677}
9678
9679/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009680 * "getwinposx()" function
9681 */
9682/*ARGSUSED*/
9683 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009684f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009685 typval_T *argvars;
9686 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009687{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009688 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009689#ifdef FEAT_GUI
9690 if (gui.in_use)
9691 {
9692 int x, y;
9693
9694 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009695 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009696 }
9697#endif
9698}
9699
9700/*
9701 * "getwinposy()" function
9702 */
9703/*ARGSUSED*/
9704 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009705f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009706 typval_T *argvars;
9707 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009708{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009709 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009710#ifdef FEAT_GUI
9711 if (gui.in_use)
9712 {
9713 int x, y;
9714
9715 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009716 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009717 }
9718#endif
9719}
9720
Bram Moolenaara40058a2005-07-11 22:42:07 +00009721static win_T *find_win_by_nr __ARGS((typval_T *vp));
9722
9723 static win_T *
9724find_win_by_nr(vp)
9725 typval_T *vp;
9726{
9727#ifdef FEAT_WINDOWS
9728 win_T *wp;
9729#endif
9730 int nr;
9731
9732 nr = get_tv_number_chk(vp, NULL);
9733
9734#ifdef FEAT_WINDOWS
9735 if (nr < 0)
9736 return NULL;
9737 if (nr == 0)
9738 return curwin;
9739
9740 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9741 if (--nr <= 0)
9742 break;
9743 return wp;
9744#else
9745 if (nr == 0 || nr == 1)
9746 return curwin;
9747 return NULL;
9748#endif
9749}
9750
Bram Moolenaar071d4272004-06-13 20:20:40 +00009751/*
9752 * "getwinvar()" function
9753 */
9754 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009755f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009756 typval_T *argvars;
9757 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009758{
9759 win_T *win, *oldcurwin;
9760 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009761 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009762
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009764 varname = get_tv_string_chk(&argvars[1]);
9765 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009767 rettv->v_type = VAR_STRING;
9768 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009769
9770 if (win != NULL && varname != NULL)
9771 {
9772 if (*varname == '&') /* window-local-option */
9773 {
Bram Moolenaarc0761132005-03-18 20:30:32 +00009774 /* Set curwin to be our win, temporarily. Also set curbuf, so
9775 * that we can get buffer-local options. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009776 oldcurwin = curwin;
9777 curwin = win;
Bram Moolenaarc0761132005-03-18 20:30:32 +00009778 curbuf = win->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009779
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009780 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009781
9782 /* restore previous notion of curwin */
9783 curwin = oldcurwin;
Bram Moolenaarc0761132005-03-18 20:30:32 +00009784 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009785 }
9786 else
9787 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009788 if (*varname == NUL)
9789 /* let getwinvar({nr}, "") return the "w:" dictionary. The
9790 * scope prefix before the NUL byte is required by
9791 * find_var_in_ht(). */
9792 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009793 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009794 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009795 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009796 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009797 }
9798 }
9799
9800 --emsg_off;
9801}
9802
9803/*
9804 * "glob()" function
9805 */
9806 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009807f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009808 typval_T *argvars;
9809 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009810{
9811 expand_T xpc;
9812
9813 ExpandInit(&xpc);
9814 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009815 rettv->v_type = VAR_STRING;
9816 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009817 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
9818 ExpandCleanup(&xpc);
9819}
9820
9821/*
9822 * "globpath()" function
9823 */
9824 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009825f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009826 typval_T *argvars;
9827 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009828{
9829 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009830 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009831
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009832 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009833 if (file == NULL)
9834 rettv->vval.v_string = NULL;
9835 else
9836 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009837}
9838
9839/*
9840 * "has()" function
9841 */
9842 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009843f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009844 typval_T *argvars;
9845 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009846{
9847 int i;
9848 char_u *name;
9849 int n = FALSE;
9850 static char *(has_list[]) =
9851 {
9852#ifdef AMIGA
9853 "amiga",
9854# ifdef FEAT_ARP
9855 "arp",
9856# endif
9857#endif
9858#ifdef __BEOS__
9859 "beos",
9860#endif
9861#ifdef MSDOS
9862# ifdef DJGPP
9863 "dos32",
9864# else
9865 "dos16",
9866# endif
9867#endif
9868#ifdef MACOS /* TODO: Should we add MACOS_CLASSIC, MACOS_X? (Dany) */
9869 "mac",
9870#endif
9871#if defined(MACOS_X_UNIX)
9872 "macunix",
9873#endif
9874#ifdef OS2
9875 "os2",
9876#endif
9877#ifdef __QNX__
9878 "qnx",
9879#endif
9880#ifdef RISCOS
9881 "riscos",
9882#endif
9883#ifdef UNIX
9884 "unix",
9885#endif
9886#ifdef VMS
9887 "vms",
9888#endif
9889#ifdef WIN16
9890 "win16",
9891#endif
9892#ifdef WIN32
9893 "win32",
9894#endif
9895#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
9896 "win32unix",
9897#endif
9898#ifdef WIN64
9899 "win64",
9900#endif
9901#ifdef EBCDIC
9902 "ebcdic",
9903#endif
9904#ifndef CASE_INSENSITIVE_FILENAME
9905 "fname_case",
9906#endif
9907#ifdef FEAT_ARABIC
9908 "arabic",
9909#endif
9910#ifdef FEAT_AUTOCMD
9911 "autocmd",
9912#endif
9913#ifdef FEAT_BEVAL
9914 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +00009915# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
9916 "balloon_multiline",
9917# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009918#endif
9919#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
9920 "builtin_terms",
9921# ifdef ALL_BUILTIN_TCAPS
9922 "all_builtin_terms",
9923# endif
9924#endif
9925#ifdef FEAT_BYTEOFF
9926 "byte_offset",
9927#endif
9928#ifdef FEAT_CINDENT
9929 "cindent",
9930#endif
9931#ifdef FEAT_CLIENTSERVER
9932 "clientserver",
9933#endif
9934#ifdef FEAT_CLIPBOARD
9935 "clipboard",
9936#endif
9937#ifdef FEAT_CMDL_COMPL
9938 "cmdline_compl",
9939#endif
9940#ifdef FEAT_CMDHIST
9941 "cmdline_hist",
9942#endif
9943#ifdef FEAT_COMMENTS
9944 "comments",
9945#endif
9946#ifdef FEAT_CRYPT
9947 "cryptv",
9948#endif
9949#ifdef FEAT_CSCOPE
9950 "cscope",
9951#endif
9952#ifdef DEBUG
9953 "debug",
9954#endif
9955#ifdef FEAT_CON_DIALOG
9956 "dialog_con",
9957#endif
9958#ifdef FEAT_GUI_DIALOG
9959 "dialog_gui",
9960#endif
9961#ifdef FEAT_DIFF
9962 "diff",
9963#endif
9964#ifdef FEAT_DIGRAPHS
9965 "digraphs",
9966#endif
9967#ifdef FEAT_DND
9968 "dnd",
9969#endif
9970#ifdef FEAT_EMACS_TAGS
9971 "emacs_tags",
9972#endif
9973 "eval", /* always present, of course! */
9974#ifdef FEAT_EX_EXTRA
9975 "ex_extra",
9976#endif
9977#ifdef FEAT_SEARCH_EXTRA
9978 "extra_search",
9979#endif
9980#ifdef FEAT_FKMAP
9981 "farsi",
9982#endif
9983#ifdef FEAT_SEARCHPATH
9984 "file_in_path",
9985#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009986#if defined(UNIX) && !defined(USE_SYSTEM)
9987 "filterpipe",
9988#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009989#ifdef FEAT_FIND_ID
9990 "find_in_path",
9991#endif
9992#ifdef FEAT_FOLDING
9993 "folding",
9994#endif
9995#ifdef FEAT_FOOTER
9996 "footer",
9997#endif
9998#if !defined(USE_SYSTEM) && defined(UNIX)
9999 "fork",
10000#endif
10001#ifdef FEAT_GETTEXT
10002 "gettext",
10003#endif
10004#ifdef FEAT_GUI
10005 "gui",
10006#endif
10007#ifdef FEAT_GUI_ATHENA
10008# ifdef FEAT_GUI_NEXTAW
10009 "gui_neXtaw",
10010# else
10011 "gui_athena",
10012# endif
10013#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +000010014#ifdef FEAT_GUI_KDE
10015 "gui_kde",
10016#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010017#ifdef FEAT_GUI_GTK
10018 "gui_gtk",
10019# ifdef HAVE_GTK2
10020 "gui_gtk2",
10021# endif
10022#endif
10023#ifdef FEAT_GUI_MAC
10024 "gui_mac",
10025#endif
10026#ifdef FEAT_GUI_MOTIF
10027 "gui_motif",
10028#endif
10029#ifdef FEAT_GUI_PHOTON
10030 "gui_photon",
10031#endif
10032#ifdef FEAT_GUI_W16
10033 "gui_win16",
10034#endif
10035#ifdef FEAT_GUI_W32
10036 "gui_win32",
10037#endif
10038#ifdef FEAT_HANGULIN
10039 "hangul_input",
10040#endif
10041#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10042 "iconv",
10043#endif
10044#ifdef FEAT_INS_EXPAND
10045 "insert_expand",
10046#endif
10047#ifdef FEAT_JUMPLIST
10048 "jumplist",
10049#endif
10050#ifdef FEAT_KEYMAP
10051 "keymap",
10052#endif
10053#ifdef FEAT_LANGMAP
10054 "langmap",
10055#endif
10056#ifdef FEAT_LIBCALL
10057 "libcall",
10058#endif
10059#ifdef FEAT_LINEBREAK
10060 "linebreak",
10061#endif
10062#ifdef FEAT_LISP
10063 "lispindent",
10064#endif
10065#ifdef FEAT_LISTCMDS
10066 "listcmds",
10067#endif
10068#ifdef FEAT_LOCALMAP
10069 "localmap",
10070#endif
10071#ifdef FEAT_MENU
10072 "menu",
10073#endif
10074#ifdef FEAT_SESSION
10075 "mksession",
10076#endif
10077#ifdef FEAT_MODIFY_FNAME
10078 "modify_fname",
10079#endif
10080#ifdef FEAT_MOUSE
10081 "mouse",
10082#endif
10083#ifdef FEAT_MOUSESHAPE
10084 "mouseshape",
10085#endif
10086#if defined(UNIX) || defined(VMS)
10087# ifdef FEAT_MOUSE_DEC
10088 "mouse_dec",
10089# endif
10090# ifdef FEAT_MOUSE_GPM
10091 "mouse_gpm",
10092# endif
10093# ifdef FEAT_MOUSE_JSB
10094 "mouse_jsbterm",
10095# endif
10096# ifdef FEAT_MOUSE_NET
10097 "mouse_netterm",
10098# endif
10099# ifdef FEAT_MOUSE_PTERM
10100 "mouse_pterm",
10101# endif
10102# ifdef FEAT_MOUSE_XTERM
10103 "mouse_xterm",
10104# endif
10105#endif
10106#ifdef FEAT_MBYTE
10107 "multi_byte",
10108#endif
10109#ifdef FEAT_MBYTE_IME
10110 "multi_byte_ime",
10111#endif
10112#ifdef FEAT_MULTI_LANG
10113 "multi_lang",
10114#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010115#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010116#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010117 "mzscheme",
10118#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010119#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120#ifdef FEAT_OLE
10121 "ole",
10122#endif
10123#ifdef FEAT_OSFILETYPE
10124 "osfiletype",
10125#endif
10126#ifdef FEAT_PATH_EXTRA
10127 "path_extra",
10128#endif
10129#ifdef FEAT_PERL
10130#ifndef DYNAMIC_PERL
10131 "perl",
10132#endif
10133#endif
10134#ifdef FEAT_PYTHON
10135#ifndef DYNAMIC_PYTHON
10136 "python",
10137#endif
10138#endif
10139#ifdef FEAT_POSTSCRIPT
10140 "postscript",
10141#endif
10142#ifdef FEAT_PRINTER
10143 "printer",
10144#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000010145#ifdef FEAT_PROFILE
10146 "profile",
10147#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010148#ifdef FEAT_QUICKFIX
10149 "quickfix",
10150#endif
10151#ifdef FEAT_RIGHTLEFT
10152 "rightleft",
10153#endif
10154#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
10155 "ruby",
10156#endif
10157#ifdef FEAT_SCROLLBIND
10158 "scrollbind",
10159#endif
10160#ifdef FEAT_CMDL_INFO
10161 "showcmd",
10162 "cmdline_info",
10163#endif
10164#ifdef FEAT_SIGNS
10165 "signs",
10166#endif
10167#ifdef FEAT_SMARTINDENT
10168 "smartindent",
10169#endif
10170#ifdef FEAT_SNIFF
10171 "sniff",
10172#endif
10173#ifdef FEAT_STL_OPT
10174 "statusline",
10175#endif
10176#ifdef FEAT_SUN_WORKSHOP
10177 "sun_workshop",
10178#endif
10179#ifdef FEAT_NETBEANS_INTG
10180 "netbeans_intg",
10181#endif
10182#ifdef FEAT_SYN_HL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010183 "spell",
10184#endif
10185#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010186 "syntax",
10187#endif
10188#if defined(USE_SYSTEM) || !defined(UNIX)
10189 "system",
10190#endif
10191#ifdef FEAT_TAG_BINS
10192 "tag_binary",
10193#endif
10194#ifdef FEAT_TAG_OLDSTATIC
10195 "tag_old_static",
10196#endif
10197#ifdef FEAT_TAG_ANYWHITE
10198 "tag_any_white",
10199#endif
10200#ifdef FEAT_TCL
10201# ifndef DYNAMIC_TCL
10202 "tcl",
10203# endif
10204#endif
10205#ifdef TERMINFO
10206 "terminfo",
10207#endif
10208#ifdef FEAT_TERMRESPONSE
10209 "termresponse",
10210#endif
10211#ifdef FEAT_TEXTOBJ
10212 "textobjects",
10213#endif
10214#ifdef HAVE_TGETENT
10215 "tgetent",
10216#endif
10217#ifdef FEAT_TITLE
10218 "title",
10219#endif
10220#ifdef FEAT_TOOLBAR
10221 "toolbar",
10222#endif
10223#ifdef FEAT_USR_CMDS
10224 "user-commands", /* was accidentally included in 5.4 */
10225 "user_commands",
10226#endif
10227#ifdef FEAT_VIMINFO
10228 "viminfo",
10229#endif
10230#ifdef FEAT_VERTSPLIT
10231 "vertsplit",
10232#endif
10233#ifdef FEAT_VIRTUALEDIT
10234 "virtualedit",
10235#endif
10236#ifdef FEAT_VISUAL
10237 "visual",
10238#endif
10239#ifdef FEAT_VISUALEXTRA
10240 "visualextra",
10241#endif
10242#ifdef FEAT_VREPLACE
10243 "vreplace",
10244#endif
10245#ifdef FEAT_WILDIGN
10246 "wildignore",
10247#endif
10248#ifdef FEAT_WILDMENU
10249 "wildmenu",
10250#endif
10251#ifdef FEAT_WINDOWS
10252 "windows",
10253#endif
10254#ifdef FEAT_WAK
10255 "winaltkeys",
10256#endif
10257#ifdef FEAT_WRITEBACKUP
10258 "writebackup",
10259#endif
10260#ifdef FEAT_XIM
10261 "xim",
10262#endif
10263#ifdef FEAT_XFONTSET
10264 "xfontset",
10265#endif
10266#ifdef USE_XSMP
10267 "xsmp",
10268#endif
10269#ifdef USE_XSMP_INTERACT
10270 "xsmp_interact",
10271#endif
10272#ifdef FEAT_XCLIPBOARD
10273 "xterm_clipboard",
10274#endif
10275#ifdef FEAT_XTERM_SAVE
10276 "xterm_save",
10277#endif
10278#if defined(UNIX) && defined(FEAT_X11)
10279 "X11",
10280#endif
10281 NULL
10282 };
10283
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010284 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010285 for (i = 0; has_list[i] != NULL; ++i)
10286 if (STRICMP(name, has_list[i]) == 0)
10287 {
10288 n = TRUE;
10289 break;
10290 }
10291
10292 if (n == FALSE)
10293 {
10294 if (STRNICMP(name, "patch", 5) == 0)
10295 n = has_patch(atoi((char *)name + 5));
10296 else if (STRICMP(name, "vim_starting") == 0)
10297 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010298#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
10299 else if (STRICMP(name, "balloon_multiline") == 0)
10300 n = multiline_balloon_available();
10301#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010302#ifdef DYNAMIC_TCL
10303 else if (STRICMP(name, "tcl") == 0)
10304 n = tcl_enabled(FALSE);
10305#endif
10306#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
10307 else if (STRICMP(name, "iconv") == 0)
10308 n = iconv_enabled(FALSE);
10309#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010310#ifdef DYNAMIC_MZSCHEME
10311 else if (STRICMP(name, "mzscheme") == 0)
10312 n = mzscheme_enabled(FALSE);
10313#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010314#ifdef DYNAMIC_RUBY
10315 else if (STRICMP(name, "ruby") == 0)
10316 n = ruby_enabled(FALSE);
10317#endif
10318#ifdef DYNAMIC_PYTHON
10319 else if (STRICMP(name, "python") == 0)
10320 n = python_enabled(FALSE);
10321#endif
10322#ifdef DYNAMIC_PERL
10323 else if (STRICMP(name, "perl") == 0)
10324 n = perl_enabled(FALSE);
10325#endif
10326#ifdef FEAT_GUI
10327 else if (STRICMP(name, "gui_running") == 0)
10328 n = (gui.in_use || gui.starting);
10329# ifdef FEAT_GUI_W32
10330 else if (STRICMP(name, "gui_win32s") == 0)
10331 n = gui_is_win32s();
10332# endif
10333# ifdef FEAT_BROWSE
10334 else if (STRICMP(name, "browse") == 0)
10335 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
10336# endif
10337#endif
10338#ifdef FEAT_SYN_HL
10339 else if (STRICMP(name, "syntax_items") == 0)
10340 n = syntax_present(curbuf);
10341#endif
10342#if defined(WIN3264)
10343 else if (STRICMP(name, "win95") == 0)
10344 n = mch_windows95();
10345#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000010346#ifdef FEAT_NETBEANS_INTG
10347 else if (STRICMP(name, "netbeans_enabled") == 0)
10348 n = usingNetbeans;
10349#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010350 }
10351
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010352 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010353}
10354
10355/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000010356 * "has_key()" function
10357 */
10358 static void
10359f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010360 typval_T *argvars;
10361 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010362{
10363 rettv->vval.v_number = 0;
10364 if (argvars[0].v_type != VAR_DICT)
10365 {
10366 EMSG(_(e_dictreq));
10367 return;
10368 }
10369 if (argvars[0].vval.v_dict == NULL)
10370 return;
10371
10372 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010373 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010374}
10375
10376/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010377 * "hasmapto()" function
10378 */
10379 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010380f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010381 typval_T *argvars;
10382 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010383{
10384 char_u *name;
10385 char_u *mode;
10386 char_u buf[NUMBUFLEN];
10387
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010388 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010389 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010390 mode = (char_u *)"nvo";
10391 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010392 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010393
10394 if (map_to_exists(name, mode))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010395 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010396 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010397 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010398}
10399
10400/*
10401 * "histadd()" function
10402 */
10403/*ARGSUSED*/
10404 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010405f_histadd(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#ifdef FEAT_CMDHIST
10410 int histype;
10411 char_u *str;
10412 char_u buf[NUMBUFLEN];
10413#endif
10414
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010415 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010416 if (check_restricted() || check_secure())
10417 return;
10418#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010419 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10420 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010421 if (histype >= 0)
10422 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010423 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010424 if (*str != NUL)
10425 {
10426 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010427 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010428 return;
10429 }
10430 }
10431#endif
10432}
10433
10434/*
10435 * "histdel()" function
10436 */
10437/*ARGSUSED*/
10438 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010439f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010440 typval_T *argvars;
10441 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010442{
10443#ifdef FEAT_CMDHIST
10444 int n;
10445 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010446 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010447
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010448 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10449 if (str == NULL)
10450 n = 0;
10451 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010452 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010453 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010454 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010455 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010456 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010457 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010458 else
10459 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010460 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010461 get_tv_string_buf(&argvars[1], buf));
10462 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010463#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010464 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010465#endif
10466}
10467
10468/*
10469 * "histget()" function
10470 */
10471/*ARGSUSED*/
10472 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010473f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010474 typval_T *argvars;
10475 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010476{
10477#ifdef FEAT_CMDHIST
10478 int type;
10479 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010480 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010481
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010482 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10483 if (str == NULL)
10484 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010485 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010486 {
10487 type = get_histtype(str);
10488 if (argvars[1].v_type == VAR_UNKNOWN)
10489 idx = get_history_idx(type);
10490 else
10491 idx = (int)get_tv_number_chk(&argvars[1], NULL);
10492 /* -1 on type error */
10493 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
10494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010495#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010496 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010497#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010498 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010499}
10500
10501/*
10502 * "histnr()" function
10503 */
10504/*ARGSUSED*/
10505 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010506f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010507 typval_T *argvars;
10508 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010509{
10510 int i;
10511
10512#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010513 char_u *history = get_tv_string_chk(&argvars[0]);
10514
10515 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010516 if (i >= HIST_CMD && i < HIST_COUNT)
10517 i = get_history_idx(i);
10518 else
10519#endif
10520 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010521 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010522}
10523
10524/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010525 * "highlightID(name)" function
10526 */
10527 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010528f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010529 typval_T *argvars;
10530 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010531{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010532 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010533}
10534
10535/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010536 * "highlight_exists()" function
10537 */
10538 static void
10539f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010540 typval_T *argvars;
10541 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010542{
10543 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
10544}
10545
10546/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010547 * "hostname()" function
10548 */
10549/*ARGSUSED*/
10550 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010551f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010552 typval_T *argvars;
10553 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010554{
10555 char_u hostname[256];
10556
10557 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010558 rettv->v_type = VAR_STRING;
10559 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010560}
10561
10562/*
10563 * iconv() function
10564 */
10565/*ARGSUSED*/
10566 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010567f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010568 typval_T *argvars;
10569 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010570{
10571#ifdef FEAT_MBYTE
10572 char_u buf1[NUMBUFLEN];
10573 char_u buf2[NUMBUFLEN];
10574 char_u *from, *to, *str;
10575 vimconv_T vimconv;
10576#endif
10577
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010578 rettv->v_type = VAR_STRING;
10579 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010580
10581#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010582 str = get_tv_string(&argvars[0]);
10583 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
10584 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010585 vimconv.vc_type = CONV_NONE;
10586 convert_setup(&vimconv, from, to);
10587
10588 /* If the encodings are equal, no conversion needed. */
10589 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010590 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010591 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010592 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010593
10594 convert_setup(&vimconv, NULL, NULL);
10595 vim_free(from);
10596 vim_free(to);
10597#endif
10598}
10599
10600/*
10601 * "indent()" function
10602 */
10603 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010604f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010605 typval_T *argvars;
10606 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010607{
10608 linenr_T lnum;
10609
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010610 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010611 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010612 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010613 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010614 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010615}
10616
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010617/*
10618 * "index()" function
10619 */
10620 static void
10621f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010622 typval_T *argvars;
10623 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010624{
Bram Moolenaar33570922005-01-25 22:26:29 +000010625 list_T *l;
10626 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010627 long idx = 0;
10628 int ic = FALSE;
10629
10630 rettv->vval.v_number = -1;
10631 if (argvars[0].v_type != VAR_LIST)
10632 {
10633 EMSG(_(e_listreq));
10634 return;
10635 }
10636 l = argvars[0].vval.v_list;
10637 if (l != NULL)
10638 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010639 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010640 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010641 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010642 int error = FALSE;
10643
Bram Moolenaar758711c2005-02-02 23:11:38 +000010644 /* Start at specified item. Use the cached index that list_find()
10645 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010646 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000010647 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010648 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010649 ic = get_tv_number_chk(&argvars[3], &error);
10650 if (error)
10651 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010652 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010653
Bram Moolenaar758711c2005-02-02 23:11:38 +000010654 for ( ; item != NULL; item = item->li_next, ++idx)
10655 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010656 {
10657 rettv->vval.v_number = idx;
10658 break;
10659 }
10660 }
10661}
10662
Bram Moolenaar071d4272004-06-13 20:20:40 +000010663static int inputsecret_flag = 0;
10664
10665/*
10666 * "input()" function
10667 * Also handles inputsecret() when inputsecret is set.
10668 */
10669 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010670f_input(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010671 typval_T *argvars;
10672 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010673{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010674 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010675 char_u *p = NULL;
10676 int c;
10677 char_u buf[NUMBUFLEN];
10678 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010679 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000010680
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010681 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010682
10683#ifdef NO_CONSOLE_INPUT
10684 /* While starting up, there is no place to enter text. */
10685 if (no_console_input())
10686 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010687 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010688 return;
10689 }
10690#endif
10691
10692 cmd_silent = FALSE; /* Want to see the prompt. */
10693 if (prompt != NULL)
10694 {
10695 /* Only the part of the message after the last NL is considered as
10696 * prompt for the command line */
10697 p = vim_strrchr(prompt, '\n');
10698 if (p == NULL)
10699 p = prompt;
10700 else
10701 {
10702 ++p;
10703 c = *p;
10704 *p = NUL;
10705 msg_start();
10706 msg_clr_eos();
10707 msg_puts_attr(prompt, echo_attr);
10708 msg_didout = FALSE;
10709 msg_starthere();
10710 *p = c;
10711 }
10712 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010713
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010714 if (argvars[1].v_type != VAR_UNKNOWN)
10715 {
10716 defstr = get_tv_string_buf_chk(&argvars[1], buf);
10717 if (defstr != NULL)
10718 stuffReadbuffSpec(defstr);
10719 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010720
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010721 if (defstr != NULL)
10722 rettv->vval.v_string =
Bram Moolenaar071d4272004-06-13 20:20:40 +000010723 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr);
10724
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010725 /* since the user typed this, no need to wait for return */
10726 need_wait_return = FALSE;
10727 msg_didout = FALSE;
10728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010729 cmd_silent = cmd_silent_save;
10730}
10731
10732/*
10733 * "inputdialog()" function
10734 */
10735 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010736f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010737 typval_T *argvars;
10738 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010739{
10740#if defined(FEAT_GUI_TEXTDIALOG)
10741 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
10742 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
10743 {
10744 char_u *message;
10745 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010746 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000010747
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010748 message = get_tv_string_chk(&argvars[0]);
10749 if (argvars[1].v_type != VAR_UNKNOWN
10750 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000010751 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010752 else
10753 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010754 if (message != NULL && defstr != NULL
10755 && do_dialog(VIM_QUESTION, NULL, message,
10756 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010757 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010758 else
10759 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010760 if (message != NULL && defstr != NULL
10761 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010762 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010763 rettv->vval.v_string = vim_strsave(
10764 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010765 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010766 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010767 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010768 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010769 }
10770 else
10771#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010772 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010773}
10774
10775static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
10776
10777/*
10778 * "inputrestore()" function
10779 */
10780/*ARGSUSED*/
10781 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010782f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010783 typval_T *argvars;
10784 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010785{
10786 if (ga_userinput.ga_len > 0)
10787 {
10788 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010789 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
10790 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010791 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010792 }
10793 else if (p_verbose > 1)
10794 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000010795 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010796 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010797 }
10798}
10799
10800/*
10801 * "inputsave()" function
10802 */
10803/*ARGSUSED*/
10804 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010805f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010806 typval_T *argvars;
10807 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010808{
10809 /* Add an entry to the stack of typehead storage. */
10810 if (ga_grow(&ga_userinput, 1) == OK)
10811 {
10812 save_typeahead((tasave_T *)(ga_userinput.ga_data)
10813 + ga_userinput.ga_len);
10814 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010815 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010816 }
10817 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010818 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010819}
10820
10821/*
10822 * "inputsecret()" function
10823 */
10824 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010825f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010826 typval_T *argvars;
10827 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010828{
10829 ++cmdline_star;
10830 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010831 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010832 --cmdline_star;
10833 --inputsecret_flag;
10834}
10835
10836/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010837 * "insert()" function
10838 */
10839 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010840f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010841 typval_T *argvars;
10842 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010843{
10844 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000010845 listitem_T *item;
10846 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010847 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010848
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010849 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010850 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010851 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010852 else if ((l = argvars[0].vval.v_list) != NULL
10853 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010854 {
10855 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010856 before = get_tv_number_chk(&argvars[2], &error);
10857 if (error)
10858 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010859
Bram Moolenaar758711c2005-02-02 23:11:38 +000010860 if (before == l->lv_len)
10861 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010862 else
10863 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010864 item = list_find(l, before);
10865 if (item == NULL)
10866 {
10867 EMSGN(_(e_listidx), before);
10868 l = NULL;
10869 }
10870 }
10871 if (l != NULL)
10872 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010873 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010874 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010875 }
10876 }
10877}
10878
10879/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010880 * "isdirectory()" function
10881 */
10882 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010883f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010884 typval_T *argvars;
10885 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010886{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010887 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010888}
10889
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010890/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010891 * "islocked()" function
10892 */
10893 static void
10894f_islocked(argvars, rettv)
10895 typval_T *argvars;
10896 typval_T *rettv;
10897{
10898 lval_T lv;
10899 char_u *end;
10900 dictitem_T *di;
10901
10902 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000010903 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
10904 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010905 if (end != NULL && lv.ll_name != NULL)
10906 {
10907 if (*end != NUL)
10908 EMSG(_(e_trailing));
10909 else
10910 {
10911 if (lv.ll_tv == NULL)
10912 {
10913 if (check_changedtick(lv.ll_name))
10914 rettv->vval.v_number = 1; /* always locked */
10915 else
10916 {
10917 di = find_var(lv.ll_name, NULL);
10918 if (di != NULL)
10919 {
10920 /* Consider a variable locked when:
10921 * 1. the variable itself is locked
10922 * 2. the value of the variable is locked.
10923 * 3. the List or Dict value is locked.
10924 */
10925 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
10926 || tv_islocked(&di->di_tv));
10927 }
10928 }
10929 }
10930 else if (lv.ll_range)
10931 EMSG(_("E745: Range not allowed"));
10932 else if (lv.ll_newkey != NULL)
10933 EMSG2(_(e_dictkey), lv.ll_newkey);
10934 else if (lv.ll_list != NULL)
10935 /* List item. */
10936 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
10937 else
10938 /* Dictionary item. */
10939 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
10940 }
10941 }
10942
10943 clear_lval(&lv);
10944}
10945
Bram Moolenaar33570922005-01-25 22:26:29 +000010946static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000010947
10948/*
10949 * Turn a dict into a list:
10950 * "what" == 0: list of keys
10951 * "what" == 1: list of values
10952 * "what" == 2: list of items
10953 */
10954 static void
10955dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000010956 typval_T *argvars;
10957 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010958 int what;
10959{
Bram Moolenaar33570922005-01-25 22:26:29 +000010960 list_T *l;
10961 list_T *l2;
10962 dictitem_T *di;
10963 hashitem_T *hi;
10964 listitem_T *li;
10965 listitem_T *li2;
10966 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010967 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010968
10969 rettv->vval.v_number = 0;
10970 if (argvars[0].v_type != VAR_DICT)
10971 {
10972 EMSG(_(e_dictreq));
10973 return;
10974 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010975 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000010976 return;
10977
10978 l = list_alloc();
10979 if (l == NULL)
10980 return;
10981 rettv->v_type = VAR_LIST;
10982 rettv->vval.v_list = l;
10983 ++l->lv_refcount;
10984
Bram Moolenaar33570922005-01-25 22:26:29 +000010985 todo = d->dv_hashtab.ht_used;
10986 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000010987 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010988 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000010989 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010990 --todo;
10991 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000010992
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010993 li = listitem_alloc();
10994 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000010995 break;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010996 list_append(l, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000010997
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010998 if (what == 0)
10999 {
11000 /* keys() */
11001 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011002 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011003 li->li_tv.vval.v_string = vim_strsave(di->di_key);
11004 }
11005 else if (what == 1)
11006 {
11007 /* values() */
11008 copy_tv(&di->di_tv, &li->li_tv);
11009 }
11010 else
11011 {
11012 /* items() */
11013 l2 = list_alloc();
11014 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011015 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011016 li->li_tv.vval.v_list = l2;
11017 if (l2 == NULL)
11018 break;
11019 ++l2->lv_refcount;
11020
11021 li2 = listitem_alloc();
11022 if (li2 == NULL)
11023 break;
11024 list_append(l2, li2);
11025 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011026 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011027 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
11028
11029 li2 = listitem_alloc();
11030 if (li2 == NULL)
11031 break;
11032 list_append(l2, li2);
11033 copy_tv(&di->di_tv, &li2->li_tv);
11034 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011035 }
11036 }
11037}
11038
11039/*
11040 * "items(dict)" function
11041 */
11042 static void
11043f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011044 typval_T *argvars;
11045 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011046{
11047 dict_list(argvars, rettv, 2);
11048}
11049
Bram Moolenaar071d4272004-06-13 20:20:40 +000011050/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011051 * "join()" function
11052 */
11053 static void
11054f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011055 typval_T *argvars;
11056 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011057{
11058 garray_T ga;
11059 char_u *sep;
11060
11061 rettv->vval.v_number = 0;
11062 if (argvars[0].v_type != VAR_LIST)
11063 {
11064 EMSG(_(e_listreq));
11065 return;
11066 }
11067 if (argvars[0].vval.v_list == NULL)
11068 return;
11069 if (argvars[1].v_type == VAR_UNKNOWN)
11070 sep = (char_u *)" ";
11071 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011072 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011073
11074 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011075
11076 if (sep != NULL)
11077 {
11078 ga_init2(&ga, (int)sizeof(char), 80);
11079 list_join(&ga, argvars[0].vval.v_list, sep, TRUE);
11080 ga_append(&ga, NUL);
11081 rettv->vval.v_string = (char_u *)ga.ga_data;
11082 }
11083 else
11084 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011085}
11086
11087/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011088 * "keys()" function
11089 */
11090 static void
11091f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011092 typval_T *argvars;
11093 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011094{
11095 dict_list(argvars, rettv, 0);
11096}
11097
11098/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011099 * "last_buffer_nr()" function.
11100 */
11101/*ARGSUSED*/
11102 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011103f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011104 typval_T *argvars;
11105 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011106{
11107 int n = 0;
11108 buf_T *buf;
11109
11110 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
11111 if (n < buf->b_fnum)
11112 n = buf->b_fnum;
11113
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011114 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011115}
11116
11117/*
11118 * "len()" function
11119 */
11120 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011121f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011122 typval_T *argvars;
11123 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011124{
11125 switch (argvars[0].v_type)
11126 {
11127 case VAR_STRING:
11128 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011129 rettv->vval.v_number = (varnumber_T)STRLEN(
11130 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011131 break;
11132 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011133 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011134 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011135 case VAR_DICT:
11136 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
11137 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011138 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011139 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011140 break;
11141 }
11142}
11143
Bram Moolenaar33570922005-01-25 22:26:29 +000011144static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011145
11146 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011147libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011148 typval_T *argvars;
11149 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011150 int type;
11151{
11152#ifdef FEAT_LIBCALL
11153 char_u *string_in;
11154 char_u **string_result;
11155 int nr_result;
11156#endif
11157
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011158 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011159 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011160 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011161 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011162 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011163
11164 if (check_restricted() || check_secure())
11165 return;
11166
11167#ifdef FEAT_LIBCALL
11168 /* The first two args must be strings, otherwise its meaningless */
11169 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
11170 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011171 string_in = NULL;
11172 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011173 string_in = argvars[2].vval.v_string;
11174 if (type == VAR_NUMBER)
11175 string_result = NULL;
11176 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011177 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011178 if (mch_libcall(argvars[0].vval.v_string,
11179 argvars[1].vval.v_string,
11180 string_in,
11181 argvars[2].vval.v_number,
11182 string_result,
11183 &nr_result) == OK
11184 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011185 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011186 }
11187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011188}
11189
11190/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011191 * "libcall()" function
11192 */
11193 static void
11194f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011195 typval_T *argvars;
11196 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011197{
11198 libcall_common(argvars, rettv, VAR_STRING);
11199}
11200
11201/*
11202 * "libcallnr()" function
11203 */
11204 static void
11205f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011206 typval_T *argvars;
11207 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011208{
11209 libcall_common(argvars, rettv, VAR_NUMBER);
11210}
11211
11212/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011213 * "line(string)" function
11214 */
11215 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011216f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011217 typval_T *argvars;
11218 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011219{
11220 linenr_T lnum = 0;
11221 pos_T *fp;
11222
11223 fp = var2fpos(&argvars[0], TRUE);
11224 if (fp != NULL)
11225 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011226 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011227}
11228
11229/*
11230 * "line2byte(lnum)" function
11231 */
11232/*ARGSUSED*/
11233 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011234f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011235 typval_T *argvars;
11236 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011237{
11238#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011239 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011240#else
11241 linenr_T lnum;
11242
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011243 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011244 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011245 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011246 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011247 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
11248 if (rettv->vval.v_number >= 0)
11249 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011250#endif
11251}
11252
11253/*
11254 * "lispindent(lnum)" function
11255 */
11256 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011257f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011258 typval_T *argvars;
11259 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011260{
11261#ifdef FEAT_LISP
11262 pos_T pos;
11263 linenr_T lnum;
11264
11265 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011266 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011267 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11268 {
11269 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011270 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011271 curwin->w_cursor = pos;
11272 }
11273 else
11274#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011275 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011276}
11277
11278/*
11279 * "localtime()" function
11280 */
11281/*ARGSUSED*/
11282 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011283f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011284 typval_T *argvars;
11285 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011286{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011287 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011288}
11289
Bram Moolenaar33570922005-01-25 22:26:29 +000011290static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011291
11292 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011293get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000011294 typval_T *argvars;
11295 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011296 int exact;
11297{
11298 char_u *keys;
11299 char_u *which;
11300 char_u buf[NUMBUFLEN];
11301 char_u *keys_buf = NULL;
11302 char_u *rhs;
11303 int mode;
11304 garray_T ga;
11305
11306 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011307 rettv->v_type = VAR_STRING;
11308 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011309
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011310 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011311 if (*keys == NUL)
11312 return;
11313
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011314 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011315 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011316 else
11317 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011318 if (which == NULL)
11319 return;
11320
Bram Moolenaar071d4272004-06-13 20:20:40 +000011321 mode = get_map_mode(&which, 0);
11322
11323 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000011324 rhs = check_map(keys, mode, exact, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011325 vim_free(keys_buf);
11326 if (rhs != NULL)
11327 {
11328 ga_init(&ga);
11329 ga.ga_itemsize = 1;
11330 ga.ga_growsize = 40;
11331
11332 while (*rhs != NUL)
11333 ga_concat(&ga, str2special(&rhs, FALSE));
11334
11335 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011336 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011337 }
11338}
11339
11340/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011341 * "map()" function
11342 */
11343 static void
11344f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011345 typval_T *argvars;
11346 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011347{
11348 filter_map(argvars, rettv, TRUE);
11349}
11350
11351/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011352 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011353 */
11354 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011355f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011356 typval_T *argvars;
11357 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011358{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011359 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011360}
11361
11362/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011363 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011364 */
11365 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011366f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011367 typval_T *argvars;
11368 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011369{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011370 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011371}
11372
Bram Moolenaar33570922005-01-25 22:26:29 +000011373static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011374
11375 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011376find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011377 typval_T *argvars;
11378 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011379 int type;
11380{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011381 char_u *str = NULL;
11382 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011383 char_u *pat;
11384 regmatch_T regmatch;
11385 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011386 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011387 char_u *save_cpo;
11388 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011389 long nth = 1;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011390 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011391 list_T *l = NULL;
11392 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011393 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011394 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011395
11396 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11397 save_cpo = p_cpo;
11398 p_cpo = (char_u *)"";
11399
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011400 rettv->vval.v_number = -1;
11401 if (type == 3)
11402 {
11403 /* return empty list when there are no matches */
11404 if ((rettv->vval.v_list = list_alloc()) == NULL)
11405 goto theend;
11406 rettv->v_type = VAR_LIST;
11407 ++rettv->vval.v_list->lv_refcount;
11408 }
11409 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011410 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011411 rettv->v_type = VAR_STRING;
11412 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011414
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011415 if (argvars[0].v_type == VAR_LIST)
11416 {
11417 if ((l = argvars[0].vval.v_list) == NULL)
11418 goto theend;
11419 li = l->lv_first;
11420 }
11421 else
11422 expr = str = get_tv_string(&argvars[0]);
11423
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011424 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11425 if (pat == NULL)
11426 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011427
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011428 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011429 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011430 int error = FALSE;
11431
11432 start = get_tv_number_chk(&argvars[2], &error);
11433 if (error)
11434 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011435 if (l != NULL)
11436 {
11437 li = list_find(l, start);
11438 if (li == NULL)
11439 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000011440 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011441 }
11442 else
11443 {
11444 if (start < 0)
11445 start = 0;
11446 if (start > (long)STRLEN(str))
11447 goto theend;
11448 str += start;
11449 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011450
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011451 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011452 nth = get_tv_number_chk(&argvars[3], &error);
11453 if (error)
11454 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011455 }
11456
11457 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11458 if (regmatch.regprog != NULL)
11459 {
11460 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011461
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000011462 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011463 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011464 if (l != NULL)
11465 {
11466 if (li == NULL)
11467 {
11468 match = FALSE;
11469 break;
11470 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011471 vim_free(tofree);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011472 str = echo_string(&li->li_tv, &tofree, strbuf);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011473 if (str == NULL)
11474 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011475 }
11476
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011477 match = vim_regexec_nl(&regmatch, str, (colnr_T)0);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011478
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011479 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011480 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011481 if (l == NULL && !match)
11482 break;
11483
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011484 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011485 if (l != NULL)
11486 {
11487 li = li->li_next;
11488 ++idx;
11489 }
11490 else
11491 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011492#ifdef FEAT_MBYTE
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011493 str = regmatch.startp[0] + mb_ptr2len_check(regmatch.startp[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011494#else
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011495 str = regmatch.startp[0] + 1;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011496#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011497 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011498 }
11499
11500 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011501 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011502 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011503 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011504 int i;
11505
11506 /* return list with matched string and submatches */
11507 for (i = 0; i < NSUBEXP; ++i)
11508 {
11509 if (regmatch.endp[i] == NULL)
11510 break;
11511 li = listitem_alloc();
11512 if (li == NULL)
11513 break;
11514 li->li_tv.v_type = VAR_STRING;
11515 li->li_tv.v_lock = 0;
11516 li->li_tv.vval.v_string = vim_strnsave(regmatch.startp[i],
11517 (int)(regmatch.endp[i] - regmatch.startp[i]));
11518 list_append(rettv->vval.v_list, li);
11519 }
11520 }
11521 else if (type == 2)
11522 {
11523 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011524 if (l != NULL)
11525 copy_tv(&li->li_tv, rettv);
11526 else
11527 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000011528 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011529 }
11530 else if (l != NULL)
11531 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011532 else
11533 {
11534 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011535 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011536 (varnumber_T)(regmatch.startp[0] - str);
11537 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011538 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011539 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011540 rettv->vval.v_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011541 }
11542 }
11543 vim_free(regmatch.regprog);
11544 }
11545
11546theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011547 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011548 p_cpo = save_cpo;
11549}
11550
11551/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011552 * "match()" function
11553 */
11554 static void
11555f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011556 typval_T *argvars;
11557 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011558{
11559 find_some_match(argvars, rettv, 1);
11560}
11561
11562/*
11563 * "matchend()" function
11564 */
11565 static void
11566f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011567 typval_T *argvars;
11568 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011569{
11570 find_some_match(argvars, rettv, 0);
11571}
11572
11573/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011574 * "matchlist()" function
11575 */
11576 static void
11577f_matchlist(argvars, rettv)
11578 typval_T *argvars;
11579 typval_T *rettv;
11580{
11581 find_some_match(argvars, rettv, 3);
11582}
11583
11584/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011585 * "matchstr()" function
11586 */
11587 static void
11588f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011589 typval_T *argvars;
11590 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011591{
11592 find_some_match(argvars, rettv, 2);
11593}
11594
Bram Moolenaar33570922005-01-25 22:26:29 +000011595static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011596
11597 static void
11598max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000011599 typval_T *argvars;
11600 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011601 int domax;
11602{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011603 long n = 0;
11604 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011605 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011606
11607 if (argvars[0].v_type == VAR_LIST)
11608 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011609 list_T *l;
11610 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011611
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011612 l = argvars[0].vval.v_list;
11613 if (l != NULL)
11614 {
11615 li = l->lv_first;
11616 if (li != NULL)
11617 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011618 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000011619 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011620 {
11621 li = li->li_next;
11622 if (li == NULL)
11623 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011624 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011625 if (domax ? i > n : i < n)
11626 n = i;
11627 }
11628 }
11629 }
11630 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011631 else if (argvars[0].v_type == VAR_DICT)
11632 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011633 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011634 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000011635 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011636 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011637
11638 d = argvars[0].vval.v_dict;
11639 if (d != NULL)
11640 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011641 todo = d->dv_hashtab.ht_used;
11642 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011643 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011644 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011645 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011646 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011647 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011648 if (first)
11649 {
11650 n = i;
11651 first = FALSE;
11652 }
11653 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011654 n = i;
11655 }
11656 }
11657 }
11658 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011659 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000011660 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011661 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011662}
11663
11664/*
11665 * "max()" function
11666 */
11667 static void
11668f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011669 typval_T *argvars;
11670 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011671{
11672 max_min(argvars, rettv, TRUE);
11673}
11674
11675/*
11676 * "min()" function
11677 */
11678 static void
11679f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011680 typval_T *argvars;
11681 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011682{
11683 max_min(argvars, rettv, FALSE);
11684}
11685
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011686static int mkdir_recurse __ARGS((char_u *dir, int prot));
11687
11688/*
11689 * Create the directory in which "dir" is located, and higher levels when
11690 * needed.
11691 */
11692 static int
11693mkdir_recurse(dir, prot)
11694 char_u *dir;
11695 int prot;
11696{
11697 char_u *p;
11698 char_u *updir;
11699 int r = FAIL;
11700
11701 /* Get end of directory name in "dir".
11702 * We're done when it's "/" or "c:/". */
11703 p = gettail_sep(dir);
11704 if (p <= get_past_head(dir))
11705 return OK;
11706
11707 /* If the directory exists we're done. Otherwise: create it.*/
11708 updir = vim_strnsave(dir, (int)(p - dir));
11709 if (updir == NULL)
11710 return FAIL;
11711 if (mch_isdir(updir))
11712 r = OK;
11713 else if (mkdir_recurse(updir, prot) == OK)
11714 r = vim_mkdir_emsg(updir, prot);
11715 vim_free(updir);
11716 return r;
11717}
11718
11719#ifdef vim_mkdir
11720/*
11721 * "mkdir()" function
11722 */
11723 static void
11724f_mkdir(argvars, rettv)
11725 typval_T *argvars;
11726 typval_T *rettv;
11727{
11728 char_u *dir;
11729 char_u buf[NUMBUFLEN];
11730 int prot = 0755;
11731
11732 rettv->vval.v_number = FAIL;
11733 if (check_restricted() || check_secure())
11734 return;
11735
11736 dir = get_tv_string_buf(&argvars[0], buf);
11737 if (argvars[1].v_type != VAR_UNKNOWN)
11738 {
11739 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011740 prot = get_tv_number_chk(&argvars[2], NULL);
11741 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011742 mkdir_recurse(dir, prot);
11743 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011744 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011745}
11746#endif
11747
Bram Moolenaar0d660222005-01-07 21:51:51 +000011748/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011749 * "mode()" function
11750 */
11751/*ARGSUSED*/
11752 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011753f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011754 typval_T *argvars;
11755 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011756{
11757 char_u buf[2];
11758
11759#ifdef FEAT_VISUAL
11760 if (VIsual_active)
11761 {
11762 if (VIsual_select)
11763 buf[0] = VIsual_mode + 's' - 'v';
11764 else
11765 buf[0] = VIsual_mode;
11766 }
11767 else
11768#endif
11769 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
11770 buf[0] = 'r';
11771 else if (State & INSERT)
11772 {
11773 if (State & REPLACE_FLAG)
11774 buf[0] = 'R';
11775 else
11776 buf[0] = 'i';
11777 }
11778 else if (State & CMDLINE)
11779 buf[0] = 'c';
11780 else
11781 buf[0] = 'n';
11782
11783 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011784 rettv->vval.v_string = vim_strsave(buf);
11785 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011786}
11787
11788/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011789 * "nextnonblank()" function
11790 */
11791 static void
11792f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011793 typval_T *argvars;
11794 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011795{
11796 linenr_T lnum;
11797
11798 for (lnum = get_tv_lnum(argvars); ; ++lnum)
11799 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011800 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011801 {
11802 lnum = 0;
11803 break;
11804 }
11805 if (*skipwhite(ml_get(lnum)) != NUL)
11806 break;
11807 }
11808 rettv->vval.v_number = lnum;
11809}
11810
11811/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011812 * "nr2char()" function
11813 */
11814 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011815f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011816 typval_T *argvars;
11817 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011818{
11819 char_u buf[NUMBUFLEN];
11820
11821#ifdef FEAT_MBYTE
11822 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011823 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011824 else
11825#endif
11826 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011827 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011828 buf[1] = NUL;
11829 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011830 rettv->v_type = VAR_STRING;
11831 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011832}
11833
11834/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011835 * "prevnonblank()" function
11836 */
11837 static void
11838f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011839 typval_T *argvars;
11840 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011841{
11842 linenr_T lnum;
11843
11844 lnum = get_tv_lnum(argvars);
11845 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
11846 lnum = 0;
11847 else
11848 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
11849 --lnum;
11850 rettv->vval.v_number = lnum;
11851}
11852
Bram Moolenaar8c711452005-01-14 21:53:12 +000011853/*
11854 * "range()" function
11855 */
11856 static void
11857f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011858 typval_T *argvars;
11859 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011860{
11861 long start;
11862 long end;
11863 long stride = 1;
11864 long i;
Bram Moolenaar33570922005-01-25 22:26:29 +000011865 list_T *l;
11866 listitem_T *li;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011867 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011868
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011869 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011870 if (argvars[1].v_type == VAR_UNKNOWN)
11871 {
11872 end = start - 1;
11873 start = 0;
11874 }
11875 else
11876 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011877 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011878 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011879 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011880 }
11881
11882 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011883 if (error)
11884 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000011885 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000011886 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000011887 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000011888 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011889 else
11890 {
11891 l = list_alloc();
11892 if (l != NULL)
11893 {
11894 rettv->v_type = VAR_LIST;
11895 rettv->vval.v_list = l;
11896 ++l->lv_refcount;
11897
11898 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
11899 {
11900 li = listitem_alloc();
11901 if (li == NULL)
11902 break;
11903 li->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011904 li->li_tv.v_lock = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011905 li->li_tv.vval.v_number = i;
11906 list_append(l, li);
11907 }
11908 }
11909 }
11910}
11911
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011912/*
11913 * "readfile()" function
11914 */
11915 static void
11916f_readfile(argvars, rettv)
11917 typval_T *argvars;
11918 typval_T *rettv;
11919{
11920 int binary = FALSE;
11921 char_u *fname;
11922 FILE *fd;
11923 list_T *l;
11924 listitem_T *li;
11925#define FREAD_SIZE 200 /* optimized for text lines */
11926 char_u buf[FREAD_SIZE];
11927 int readlen; /* size of last fread() */
11928 int buflen; /* nr of valid chars in buf[] */
11929 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
11930 int tolist; /* first byte in buf[] still to be put in list */
11931 int chop; /* how many CR to chop off */
11932 char_u *prev = NULL; /* previously read bytes, if any */
11933 int prevlen = 0; /* length of "prev" if not NULL */
11934 char_u *s;
11935 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011936 long maxline = MAXLNUM;
11937 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011938
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011939 if (argvars[1].v_type != VAR_UNKNOWN)
11940 {
11941 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
11942 binary = TRUE;
11943 if (argvars[2].v_type != VAR_UNKNOWN)
11944 maxline = get_tv_number(&argvars[2]);
11945 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011946
11947 l = list_alloc();
11948 if (l == NULL)
11949 return;
11950 rettv->v_type = VAR_LIST;
11951 rettv->vval.v_list = l;
11952 l->lv_refcount = 1;
11953
11954 /* Always open the file in binary mode, library functions have a mind of
11955 * their own about CR-LF conversion. */
11956 fname = get_tv_string(&argvars[0]);
11957 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
11958 {
11959 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
11960 return;
11961 }
11962
11963 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000011964 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011965 {
11966 readlen = fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
11967 buflen = filtd + readlen;
11968 tolist = 0;
11969 for ( ; filtd < buflen || readlen <= 0; ++filtd)
11970 {
11971 if (buf[filtd] == '\n' || readlen <= 0)
11972 {
11973 /* Only when in binary mode add an empty list item when the
11974 * last line ends in a '\n'. */
11975 if (!binary && readlen == 0 && filtd == 0)
11976 break;
11977
11978 /* Found end-of-line or end-of-file: add a text line to the
11979 * list. */
11980 chop = 0;
11981 if (!binary)
11982 while (filtd - chop - 1 >= tolist
11983 && buf[filtd - chop - 1] == '\r')
11984 ++chop;
11985 len = filtd - tolist - chop;
11986 if (prev == NULL)
11987 s = vim_strnsave(buf + tolist, len);
11988 else
11989 {
11990 s = alloc((unsigned)(prevlen + len + 1));
11991 if (s != NULL)
11992 {
11993 mch_memmove(s, prev, prevlen);
11994 vim_free(prev);
11995 prev = NULL;
11996 mch_memmove(s + prevlen, buf + tolist, len);
11997 s[prevlen + len] = NUL;
11998 }
11999 }
12000 tolist = filtd + 1;
12001
12002 li = listitem_alloc();
12003 if (li == NULL)
12004 {
12005 vim_free(s);
12006 break;
12007 }
12008 li->li_tv.v_type = VAR_STRING;
12009 li->li_tv.v_lock = 0;
12010 li->li_tv.vval.v_string = s;
12011 list_append(l, li);
12012
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012013 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012014 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012015 if (readlen <= 0)
12016 break;
12017 }
12018 else if (buf[filtd] == NUL)
12019 buf[filtd] = '\n';
12020 }
12021 if (readlen <= 0)
12022 break;
12023
12024 if (tolist == 0)
12025 {
12026 /* "buf" is full, need to move text to an allocated buffer */
12027 if (prev == NULL)
12028 {
12029 prev = vim_strnsave(buf, buflen);
12030 prevlen = buflen;
12031 }
12032 else
12033 {
12034 s = alloc((unsigned)(prevlen + buflen));
12035 if (s != NULL)
12036 {
12037 mch_memmove(s, prev, prevlen);
12038 mch_memmove(s + prevlen, buf, buflen);
12039 vim_free(prev);
12040 prev = s;
12041 prevlen += buflen;
12042 }
12043 }
12044 filtd = 0;
12045 }
12046 else
12047 {
12048 mch_memmove(buf, buf + tolist, buflen - tolist);
12049 filtd -= tolist;
12050 }
12051 }
12052
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012053 /*
12054 * For a negative line count use only the lines at the end of the file,
12055 * free the rest.
12056 */
12057 if (maxline < 0)
12058 while (cnt > -maxline)
12059 {
12060 listitem_remove(l, l->lv_first);
12061 --cnt;
12062 }
12063
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012064 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012065 fclose(fd);
12066}
12067
12068
Bram Moolenaar0d660222005-01-07 21:51:51 +000012069#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
12070static void make_connection __ARGS((void));
12071static int check_connection __ARGS((void));
12072
12073 static void
12074make_connection()
12075{
12076 if (X_DISPLAY == NULL
12077# ifdef FEAT_GUI
12078 && !gui.in_use
12079# endif
12080 )
12081 {
12082 x_force_connect = TRUE;
12083 setup_term_clip();
12084 x_force_connect = FALSE;
12085 }
12086}
12087
12088 static int
12089check_connection()
12090{
12091 make_connection();
12092 if (X_DISPLAY == NULL)
12093 {
12094 EMSG(_("E240: No connection to Vim server"));
12095 return FAIL;
12096 }
12097 return OK;
12098}
12099#endif
12100
12101#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012102static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012103
12104 static void
12105remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000012106 typval_T *argvars;
12107 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012108 int expr;
12109{
12110 char_u *server_name;
12111 char_u *keys;
12112 char_u *r = NULL;
12113 char_u buf[NUMBUFLEN];
12114# ifdef WIN32
12115 HWND w;
12116# else
12117 Window w;
12118# endif
12119
12120 if (check_restricted() || check_secure())
12121 return;
12122
12123# ifdef FEAT_X11
12124 if (check_connection() == FAIL)
12125 return;
12126# endif
12127
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012128 server_name = get_tv_string_chk(&argvars[0]);
12129 if (server_name == NULL)
12130 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012131 keys = get_tv_string_buf(&argvars[1], buf);
12132# ifdef WIN32
12133 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
12134# else
12135 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
12136 < 0)
12137# endif
12138 {
12139 if (r != NULL)
12140 EMSG(r); /* sending worked but evaluation failed */
12141 else
12142 EMSG2(_("E241: Unable to send to %s"), server_name);
12143 return;
12144 }
12145
12146 rettv->vval.v_string = r;
12147
12148 if (argvars[2].v_type != VAR_UNKNOWN)
12149 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012150 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000012151 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012152 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012153
12154 sprintf((char *)str, "0x%x", (unsigned int)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000012155 v.di_tv.v_type = VAR_STRING;
12156 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012157 idvar = get_tv_string_chk(&argvars[2]);
12158 if (idvar != NULL)
12159 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012160 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012161 }
12162}
12163#endif
12164
12165/*
12166 * "remote_expr()" function
12167 */
12168/*ARGSUSED*/
12169 static void
12170f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012171 typval_T *argvars;
12172 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012173{
12174 rettv->v_type = VAR_STRING;
12175 rettv->vval.v_string = NULL;
12176#ifdef FEAT_CLIENTSERVER
12177 remote_common(argvars, rettv, TRUE);
12178#endif
12179}
12180
12181/*
12182 * "remote_foreground()" function
12183 */
12184/*ARGSUSED*/
12185 static void
12186f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012187 typval_T *argvars;
12188 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012189{
12190 rettv->vval.v_number = 0;
12191#ifdef FEAT_CLIENTSERVER
12192# ifdef WIN32
12193 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012194 {
12195 char_u *server_name = get_tv_string_chk(&argvars[0]);
12196
12197 if (server_name != NULL)
12198 serverForeground(server_name);
12199 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012200# else
12201 /* Send a foreground() expression to the server. */
12202 argvars[1].v_type = VAR_STRING;
12203 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
12204 argvars[2].v_type = VAR_UNKNOWN;
12205 remote_common(argvars, rettv, TRUE);
12206 vim_free(argvars[1].vval.v_string);
12207# endif
12208#endif
12209}
12210
12211/*ARGSUSED*/
12212 static void
12213f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012214 typval_T *argvars;
12215 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012216{
12217#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012218 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012219 char_u *s = NULL;
12220# ifdef WIN32
12221 int n = 0;
12222# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012223 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012224
12225 if (check_restricted() || check_secure())
12226 {
12227 rettv->vval.v_number = -1;
12228 return;
12229 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012230 serverid = get_tv_string_chk(&argvars[0]);
12231 if (serverid == NULL)
12232 {
12233 rettv->vval.v_number = -1;
12234 return; /* type error; errmsg already given */
12235 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012236# ifdef WIN32
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012237 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012238 if (n == 0)
12239 rettv->vval.v_number = -1;
12240 else
12241 {
12242 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
12243 rettv->vval.v_number = (s != NULL);
12244 }
12245# else
12246 rettv->vval.v_number = 0;
12247 if (check_connection() == FAIL)
12248 return;
12249
12250 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012251 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012252# endif
12253
12254 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
12255 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012256 char_u *retvar;
12257
Bram Moolenaar33570922005-01-25 22:26:29 +000012258 v.di_tv.v_type = VAR_STRING;
12259 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012260 retvar = get_tv_string_chk(&argvars[1]);
12261 if (retvar != NULL)
12262 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012263 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012264 }
12265#else
12266 rettv->vval.v_number = -1;
12267#endif
12268}
12269
12270/*ARGSUSED*/
12271 static void
12272f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012273 typval_T *argvars;
12274 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012275{
12276 char_u *r = NULL;
12277
12278#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012279 char_u *serverid = get_tv_string_chk(&argvars[0]);
12280
12281 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000012282 {
12283# ifdef WIN32
12284 /* The server's HWND is encoded in the 'id' parameter */
12285 int n = 0;
12286
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012287 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012288 if (n != 0)
12289 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
12290 if (r == NULL)
12291# else
12292 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012293 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012294# endif
12295 EMSG(_("E277: Unable to read a server reply"));
12296 }
12297#endif
12298 rettv->v_type = VAR_STRING;
12299 rettv->vval.v_string = r;
12300}
12301
12302/*
12303 * "remote_send()" function
12304 */
12305/*ARGSUSED*/
12306 static void
12307f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012308 typval_T *argvars;
12309 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012310{
12311 rettv->v_type = VAR_STRING;
12312 rettv->vval.v_string = NULL;
12313#ifdef FEAT_CLIENTSERVER
12314 remote_common(argvars, rettv, FALSE);
12315#endif
12316}
12317
12318/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012319 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012320 */
12321 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012322f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012323 typval_T *argvars;
12324 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012325{
Bram Moolenaar33570922005-01-25 22:26:29 +000012326 list_T *l;
12327 listitem_T *item, *item2;
12328 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012329 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012330 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012331 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000012332 dict_T *d;
12333 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012334
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012335 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012336 if (argvars[0].v_type == VAR_DICT)
12337 {
12338 if (argvars[2].v_type != VAR_UNKNOWN)
12339 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012340 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar758711c2005-02-02 23:11:38 +000012341 && !tv_check_lock(d->dv_lock, (char_u *)"remove()"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000012342 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012343 key = get_tv_string_chk(&argvars[1]);
12344 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012345 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012346 di = dict_find(d, key, -1);
12347 if (di == NULL)
12348 EMSG2(_(e_dictkey), key);
12349 else
12350 {
12351 *rettv = di->di_tv;
12352 init_tv(&di->di_tv);
12353 dictitem_remove(d, di);
12354 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012355 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012356 }
12357 }
12358 else if (argvars[0].v_type != VAR_LIST)
12359 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012360 else if ((l = argvars[0].vval.v_list) != NULL
12361 && !tv_check_lock(l->lv_lock, (char_u *)"remove()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012362 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012363 int error = FALSE;
12364
12365 idx = get_tv_number_chk(&argvars[1], &error);
12366 if (error)
12367 ; /* type error: do nothing, errmsg already given */
12368 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012369 EMSGN(_(e_listidx), idx);
12370 else
12371 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012372 if (argvars[2].v_type == VAR_UNKNOWN)
12373 {
12374 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012375 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012376 *rettv = item->li_tv;
12377 vim_free(item);
12378 }
12379 else
12380 {
12381 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012382 end = get_tv_number_chk(&argvars[2], &error);
12383 if (error)
12384 ; /* type error: do nothing */
12385 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012386 EMSGN(_(e_listidx), end);
12387 else
12388 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012389 int cnt = 0;
12390
12391 for (li = item; li != NULL; li = li->li_next)
12392 {
12393 ++cnt;
12394 if (li == item2)
12395 break;
12396 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012397 if (li == NULL) /* didn't find "item2" after "item" */
12398 EMSG(_(e_invrange));
12399 else
12400 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012401 list_remove(l, item, item2);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012402 l = list_alloc();
12403 if (l != NULL)
12404 {
12405 rettv->v_type = VAR_LIST;
12406 rettv->vval.v_list = l;
12407 l->lv_first = item;
12408 l->lv_last = item2;
12409 l->lv_refcount = 1;
12410 item->li_prev = NULL;
12411 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012412 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012413 }
12414 }
12415 }
12416 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012417 }
12418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012419}
12420
12421/*
12422 * "rename({from}, {to})" function
12423 */
12424 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012425f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012426 typval_T *argvars;
12427 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012428{
12429 char_u buf[NUMBUFLEN];
12430
12431 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012432 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012433 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012434 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
12435 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012436}
12437
12438/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012439 * "repeat()" function
12440 */
12441/*ARGSUSED*/
12442 static void
12443f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012444 typval_T *argvars;
12445 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012446{
12447 char_u *p;
12448 int n;
12449 int slen;
12450 int len;
12451 char_u *r;
12452 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000012453 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012454
12455 n = get_tv_number(&argvars[1]);
12456 if (argvars[0].v_type == VAR_LIST)
12457 {
12458 l = list_alloc();
12459 if (l != NULL && argvars[0].vval.v_list != NULL)
12460 {
12461 l->lv_refcount = 1;
12462 while (n-- > 0)
12463 if (list_extend(l, argvars[0].vval.v_list, NULL) == FAIL)
12464 break;
12465 }
12466 rettv->v_type = VAR_LIST;
12467 rettv->vval.v_list = l;
12468 }
12469 else
12470 {
12471 p = get_tv_string(&argvars[0]);
12472 rettv->v_type = VAR_STRING;
12473 rettv->vval.v_string = NULL;
12474
12475 slen = (int)STRLEN(p);
12476 len = slen * n;
12477 if (len <= 0)
12478 return;
12479
12480 r = alloc(len + 1);
12481 if (r != NULL)
12482 {
12483 for (i = 0; i < n; i++)
12484 mch_memmove(r + i * slen, p, (size_t)slen);
12485 r[len] = NUL;
12486 }
12487
12488 rettv->vval.v_string = r;
12489 }
12490}
12491
12492/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012493 * "resolve()" function
12494 */
12495 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012496f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012497 typval_T *argvars;
12498 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012499{
12500 char_u *p;
12501
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012502 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012503#ifdef FEAT_SHORTCUT
12504 {
12505 char_u *v = NULL;
12506
12507 v = mch_resolve_shortcut(p);
12508 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012509 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012510 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012511 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012512 }
12513#else
12514# ifdef HAVE_READLINK
12515 {
12516 char_u buf[MAXPATHL + 1];
12517 char_u *cpy;
12518 int len;
12519 char_u *remain = NULL;
12520 char_u *q;
12521 int is_relative_to_current = FALSE;
12522 int has_trailing_pathsep = FALSE;
12523 int limit = 100;
12524
12525 p = vim_strsave(p);
12526
12527 if (p[0] == '.' && (vim_ispathsep(p[1])
12528 || (p[1] == '.' && (vim_ispathsep(p[2])))))
12529 is_relative_to_current = TRUE;
12530
12531 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000012532 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012533 has_trailing_pathsep = TRUE;
12534
12535 q = getnextcomp(p);
12536 if (*q != NUL)
12537 {
12538 /* Separate the first path component in "p", and keep the
12539 * remainder (beginning with the path separator). */
12540 remain = vim_strsave(q - 1);
12541 q[-1] = NUL;
12542 }
12543
12544 for (;;)
12545 {
12546 for (;;)
12547 {
12548 len = readlink((char *)p, (char *)buf, MAXPATHL);
12549 if (len <= 0)
12550 break;
12551 buf[len] = NUL;
12552
12553 if (limit-- == 0)
12554 {
12555 vim_free(p);
12556 vim_free(remain);
12557 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012558 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012559 goto fail;
12560 }
12561
12562 /* Ensure that the result will have a trailing path separator
12563 * if the argument has one. */
12564 if (remain == NULL && has_trailing_pathsep)
12565 add_pathsep(buf);
12566
12567 /* Separate the first path component in the link value and
12568 * concatenate the remainders. */
12569 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
12570 if (*q != NUL)
12571 {
12572 if (remain == NULL)
12573 remain = vim_strsave(q - 1);
12574 else
12575 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012576 cpy = vim_strnsave(q-1, STRLEN(q-1) + STRLEN(remain));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012577 if (cpy != NULL)
12578 {
12579 STRCAT(cpy, remain);
12580 vim_free(remain);
12581 remain = cpy;
12582 }
12583 }
12584 q[-1] = NUL;
12585 }
12586
12587 q = gettail(p);
12588 if (q > p && *q == NUL)
12589 {
12590 /* Ignore trailing path separator. */
12591 q[-1] = NUL;
12592 q = gettail(p);
12593 }
12594 if (q > p && !mch_isFullName(buf))
12595 {
12596 /* symlink is relative to directory of argument */
12597 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
12598 if (cpy != NULL)
12599 {
12600 STRCPY(cpy, p);
12601 STRCPY(gettail(cpy), buf);
12602 vim_free(p);
12603 p = cpy;
12604 }
12605 }
12606 else
12607 {
12608 vim_free(p);
12609 p = vim_strsave(buf);
12610 }
12611 }
12612
12613 if (remain == NULL)
12614 break;
12615
12616 /* Append the first path component of "remain" to "p". */
12617 q = getnextcomp(remain + 1);
12618 len = q - remain - (*q != NUL);
12619 cpy = vim_strnsave(p, STRLEN(p) + len);
12620 if (cpy != NULL)
12621 {
12622 STRNCAT(cpy, remain, len);
12623 vim_free(p);
12624 p = cpy;
12625 }
12626 /* Shorten "remain". */
12627 if (*q != NUL)
12628 STRCPY(remain, q - 1);
12629 else
12630 {
12631 vim_free(remain);
12632 remain = NULL;
12633 }
12634 }
12635
12636 /* If the result is a relative path name, make it explicitly relative to
12637 * the current directory if and only if the argument had this form. */
12638 if (!vim_ispathsep(*p))
12639 {
12640 if (is_relative_to_current
12641 && *p != NUL
12642 && !(p[0] == '.'
12643 && (p[1] == NUL
12644 || vim_ispathsep(p[1])
12645 || (p[1] == '.'
12646 && (p[2] == NUL
12647 || vim_ispathsep(p[2]))))))
12648 {
12649 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012650 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012651 if (cpy != NULL)
12652 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012653 vim_free(p);
12654 p = cpy;
12655 }
12656 }
12657 else if (!is_relative_to_current)
12658 {
12659 /* Strip leading "./". */
12660 q = p;
12661 while (q[0] == '.' && vim_ispathsep(q[1]))
12662 q += 2;
12663 if (q > p)
12664 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
12665 }
12666 }
12667
12668 /* Ensure that the result will have no trailing path separator
12669 * if the argument had none. But keep "/" or "//". */
12670 if (!has_trailing_pathsep)
12671 {
12672 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000012673 if (after_pathsep(p, q))
12674 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012675 }
12676
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012677 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012678 }
12679# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012680 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012681# endif
12682#endif
12683
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012684 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012685
12686#ifdef HAVE_READLINK
12687fail:
12688#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012689 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012690}
12691
12692/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012693 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012694 */
12695 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012696f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012697 typval_T *argvars;
12698 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012699{
Bram Moolenaar33570922005-01-25 22:26:29 +000012700 list_T *l;
12701 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012702
Bram Moolenaar0d660222005-01-07 21:51:51 +000012703 rettv->vval.v_number = 0;
12704 if (argvars[0].v_type != VAR_LIST)
12705 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012706 else if ((l = argvars[0].vval.v_list) != NULL
12707 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000012708 {
12709 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012710 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012711 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012712 while (li != NULL)
12713 {
12714 ni = li->li_prev;
12715 list_append(l, li);
12716 li = ni;
12717 }
12718 rettv->vval.v_list = l;
12719 rettv->v_type = VAR_LIST;
12720 ++l->lv_refcount;
12721 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012722}
12723
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012724#define SP_NOMOVE 1 /* don't move cursor */
12725#define SP_REPEAT 2 /* repeat to find outer pair */
12726#define SP_RETCOUNT 4 /* return matchcount */
12727
Bram Moolenaar33570922005-01-25 22:26:29 +000012728static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012729
12730/*
12731 * Get flags for a search function.
12732 * Possibly sets "p_ws".
12733 * Returns BACKWARD, FORWARD or zero (for an error).
12734 */
12735 static int
12736get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000012737 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012738 int *flagsp;
12739{
12740 int dir = FORWARD;
12741 char_u *flags;
12742 char_u nbuf[NUMBUFLEN];
12743 int mask;
12744
12745 if (varp->v_type != VAR_UNKNOWN)
12746 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012747 flags = get_tv_string_buf_chk(varp, nbuf);
12748 if (flags == NULL)
12749 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012750 while (*flags != NUL)
12751 {
12752 switch (*flags)
12753 {
12754 case 'b': dir = BACKWARD; break;
12755 case 'w': p_ws = TRUE; break;
12756 case 'W': p_ws = FALSE; break;
12757 default: mask = 0;
12758 if (flagsp != NULL)
12759 switch (*flags)
12760 {
12761 case 'n': mask = SP_NOMOVE; break;
12762 case 'r': mask = SP_REPEAT; break;
12763 case 'm': mask = SP_RETCOUNT; break;
12764 }
12765 if (mask == 0)
12766 {
12767 EMSG2(_(e_invarg2), flags);
12768 dir = 0;
12769 }
12770 else
12771 *flagsp |= mask;
12772 }
12773 if (dir == 0)
12774 break;
12775 ++flags;
12776 }
12777 }
12778 return dir;
12779}
12780
Bram Moolenaar071d4272004-06-13 20:20:40 +000012781/*
12782 * "search()" function
12783 */
12784 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012785f_search(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012786 typval_T *argvars;
12787 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012788{
12789 char_u *pat;
12790 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012791 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012792 int save_p_ws = p_ws;
12793 int dir;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012794 int flags = 0;
12795
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012796 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012797
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012798 pat = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012799 dir = get_search_arg(&argvars[1], &flags); /* may set p_ws */
12800 if (dir == 0)
12801 goto theend;
12802 if ((flags & ~SP_NOMOVE) != 0)
12803 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012804 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012805 goto theend;
12806 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012807
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012808 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012809 if (searchit(curwin, curbuf, &pos, dir, pat, 1L,
12810 SEARCH_KEEP, RE_SEARCH) != FAIL)
12811 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012812 rettv->vval.v_number = pos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012813 curwin->w_cursor = pos;
12814 /* "/$" will put the cursor after the end of the line, may need to
12815 * correct that here */
12816 check_cursor();
12817 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012818
12819 /* If 'n' flag is used: restore cursor position. */
12820 if (flags & SP_NOMOVE)
12821 curwin->w_cursor = save_cursor;
12822theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000012823 p_ws = save_p_ws;
12824}
12825
Bram Moolenaar071d4272004-06-13 20:20:40 +000012826/*
12827 * "searchpair()" function
12828 */
12829 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012830f_searchpair(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012831 typval_T *argvars;
12832 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012833{
12834 char_u *spat, *mpat, *epat;
12835 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012836 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012837 int dir;
12838 int flags = 0;
12839 char_u nbuf1[NUMBUFLEN];
12840 char_u nbuf2[NUMBUFLEN];
12841 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000012842
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012843 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012844
Bram Moolenaar071d4272004-06-13 20:20:40 +000012845 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012846 spat = get_tv_string_chk(&argvars[0]);
12847 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
12848 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
12849 if (spat == NULL || mpat == NULL || epat == NULL)
12850 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012851
Bram Moolenaar071d4272004-06-13 20:20:40 +000012852 /* Handle the optional fourth argument: flags */
12853 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012854 if (dir == 0)
12855 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012856
12857 /* Optional fifth argument: skip expresion */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012858 if (argvars[3].v_type == VAR_UNKNOWN
12859 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012860 skip = (char_u *)"";
12861 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012862 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
12863 if (skip == NULL)
12864 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012865
Bram Moolenaar9fad3082005-07-19 22:22:13 +000012866 rettv->vval.v_number = do_searchpair(spat, mpat, epat, dir, skip, flags);
12867
12868theend:
12869 p_ws = save_p_ws;
12870}
12871
12872/*
12873 * Search for a start/middle/end thing.
12874 * Used by searchpair(), see its documentation for the details.
12875 * Returns 0 or -1 for no match,
12876 */
12877 long
12878do_searchpair(spat, mpat, epat, dir, skip, flags)
12879 char_u *spat; /* start pattern */
12880 char_u *mpat; /* middle pattern */
12881 char_u *epat; /* end pattern */
12882 int dir; /* BACKWARD or FORWARD */
12883 char_u *skip; /* skip expression */
12884 int flags; /* SP_RETCOUNT, SP_REPEAT, SP_NOMOVE */
12885{
12886 char_u *save_cpo;
12887 char_u *pat, *pat2 = NULL, *pat3 = NULL;
12888 long retval = 0;
12889 pos_T pos;
12890 pos_T firstpos;
12891 pos_T foundpos;
12892 pos_T save_cursor;
12893 pos_T save_pos;
12894 int n;
12895 int r;
12896 int nest = 1;
12897 int err;
12898
12899 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12900 save_cpo = p_cpo;
12901 p_cpo = (char_u *)"";
12902
12903 /* Make two search patterns: start/end (pat2, for in nested pairs) and
12904 * start/middle/end (pat3, for the top pair). */
12905 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
12906 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
12907 if (pat2 == NULL || pat3 == NULL)
12908 goto theend;
12909 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
12910 if (*mpat == NUL)
12911 STRCPY(pat3, pat2);
12912 else
12913 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
12914 spat, epat, mpat);
12915
Bram Moolenaar071d4272004-06-13 20:20:40 +000012916 save_cursor = curwin->w_cursor;
12917 pos = curwin->w_cursor;
12918 firstpos.lnum = 0;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000012919 foundpos.lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012920 pat = pat3;
12921 for (;;)
12922 {
12923 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
12924 SEARCH_KEEP, RE_SEARCH);
12925 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
12926 /* didn't find it or found the first match again: FAIL */
12927 break;
12928
12929 if (firstpos.lnum == 0)
12930 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000012931 if (equalpos(pos, foundpos))
12932 {
12933 /* Found the same position again. Can happen with a pattern that
12934 * has "\zs" at the end and searching backwards. Advance one
12935 * character and try again. */
12936 if (dir == BACKWARD)
12937 decl(&pos);
12938 else
12939 incl(&pos);
12940 }
12941 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012942
12943 /* If the skip pattern matches, ignore this match. */
12944 if (*skip != NUL)
12945 {
12946 save_pos = curwin->w_cursor;
12947 curwin->w_cursor = pos;
12948 r = eval_to_bool(skip, &err, NULL, FALSE);
12949 curwin->w_cursor = save_pos;
12950 if (err)
12951 {
12952 /* Evaluating {skip} caused an error, break here. */
12953 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000012954 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012955 break;
12956 }
12957 if (r)
12958 continue;
12959 }
12960
12961 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
12962 {
12963 /* Found end when searching backwards or start when searching
12964 * forward: nested pair. */
12965 ++nest;
12966 pat = pat2; /* nested, don't search for middle */
12967 }
12968 else
12969 {
12970 /* Found end when searching forward or start when searching
12971 * backward: end of (nested) pair; or found middle in outer pair. */
12972 if (--nest == 1)
12973 pat = pat3; /* outer level, search for middle */
12974 }
12975
12976 if (nest == 0)
12977 {
12978 /* Found the match: return matchcount or line number. */
12979 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000012980 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012981 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000012982 retval = pos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012983 curwin->w_cursor = pos;
12984 if (!(flags & SP_REPEAT))
12985 break;
12986 nest = 1; /* search for next unmatched */
12987 }
12988 }
12989
12990 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000012991 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012992 curwin->w_cursor = save_cursor;
12993
12994theend:
12995 vim_free(pat2);
12996 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012997 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000012998
12999 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013000}
13001
Bram Moolenaar0d660222005-01-07 21:51:51 +000013002/*ARGSUSED*/
13003 static void
13004f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013005 typval_T *argvars;
13006 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013007{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013008#ifdef FEAT_CLIENTSERVER
13009 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013010 char_u *server = get_tv_string_chk(&argvars[0]);
13011 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013012
Bram Moolenaar0d660222005-01-07 21:51:51 +000013013 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013014 if (server == NULL || reply == NULL)
13015 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013016 if (check_restricted() || check_secure())
13017 return;
13018# ifdef FEAT_X11
13019 if (check_connection() == FAIL)
13020 return;
13021# endif
13022
13023 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013024 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013025 EMSG(_("E258: Unable to send to client"));
13026 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013027 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013028 rettv->vval.v_number = 0;
13029#else
13030 rettv->vval.v_number = -1;
13031#endif
13032}
13033
13034/*ARGSUSED*/
13035 static void
13036f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013037 typval_T *argvars;
13038 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013039{
13040 char_u *r = NULL;
13041
13042#ifdef FEAT_CLIENTSERVER
13043# ifdef WIN32
13044 r = serverGetVimNames();
13045# else
13046 make_connection();
13047 if (X_DISPLAY != NULL)
13048 r = serverGetVimNames(X_DISPLAY);
13049# endif
13050#endif
13051 rettv->v_type = VAR_STRING;
13052 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013053}
13054
13055/*
13056 * "setbufvar()" function
13057 */
13058/*ARGSUSED*/
13059 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013060f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013061 typval_T *argvars;
13062 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013063{
13064 buf_T *buf;
13065#ifdef FEAT_AUTOCMD
13066 aco_save_T aco;
13067#else
13068 buf_T *save_curbuf;
13069#endif
13070 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013071 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013072 char_u nbuf[NUMBUFLEN];
13073
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013074 rettv->vval.v_number = 0;
13075
Bram Moolenaar071d4272004-06-13 20:20:40 +000013076 if (check_restricted() || check_secure())
13077 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013078 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
13079 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013080 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013081 varp = &argvars[2];
13082
13083 if (buf != NULL && varname != NULL && varp != NULL)
13084 {
13085 /* set curbuf to be our buf, temporarily */
13086#ifdef FEAT_AUTOCMD
13087 aucmd_prepbuf(&aco, buf);
13088#else
13089 save_curbuf = curbuf;
13090 curbuf = buf;
13091#endif
13092
13093 if (*varname == '&')
13094 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013095 long numval;
13096 char_u *strval;
13097 int error = FALSE;
13098
Bram Moolenaar071d4272004-06-13 20:20:40 +000013099 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013100 numval = get_tv_number_chk(varp, &error);
13101 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013102 if (!error && strval != NULL)
13103 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013104 }
13105 else
13106 {
13107 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
13108 if (bufvarname != NULL)
13109 {
13110 STRCPY(bufvarname, "b:");
13111 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013112 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013113 vim_free(bufvarname);
13114 }
13115 }
13116
13117 /* reset notion of buffer */
13118#ifdef FEAT_AUTOCMD
13119 aucmd_restbuf(&aco);
13120#else
13121 curbuf = save_curbuf;
13122#endif
13123 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013124}
13125
13126/*
13127 * "setcmdpos()" function
13128 */
13129 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013130f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013131 typval_T *argvars;
13132 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013133{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013134 int pos = (int)get_tv_number(&argvars[0]) - 1;
13135
13136 if (pos >= 0)
13137 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013138}
13139
13140/*
13141 * "setline()" function
13142 */
13143 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013144f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013145 typval_T *argvars;
13146 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013147{
13148 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000013149 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013150 list_T *l = NULL;
13151 listitem_T *li = NULL;
13152 long added = 0;
13153 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013154
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013155 lnum = get_tv_lnum(&argvars[0]);
13156 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013157 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013158 l = argvars[1].vval.v_list;
13159 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013160 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013161 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013162 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013163
13164 rettv->vval.v_number = 0; /* OK */
13165 for (;;)
13166 {
13167 if (l != NULL)
13168 {
13169 /* list argument, get next string */
13170 if (li == NULL)
13171 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013172 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013173 li = li->li_next;
13174 }
13175
13176 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013177 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013178 break;
13179 if (lnum <= curbuf->b_ml.ml_line_count)
13180 {
13181 /* existing line, replace it */
13182 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
13183 {
13184 changed_bytes(lnum, 0);
13185 check_cursor_col();
13186 rettv->vval.v_number = 0; /* OK */
13187 }
13188 }
13189 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
13190 {
13191 /* lnum is one past the last line, append the line */
13192 ++added;
13193 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
13194 rettv->vval.v_number = 0; /* OK */
13195 }
13196
13197 if (l == NULL) /* only one string argument */
13198 break;
13199 ++lnum;
13200 }
13201
13202 if (added > 0)
13203 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013204}
13205
13206/*
Bram Moolenaar2641f772005-03-25 21:58:17 +000013207 * "setqflist()" function
13208 */
13209/*ARGSUSED*/
13210 static void
13211f_setqflist(argvars, rettv)
13212 typval_T *argvars;
13213 typval_T *rettv;
13214{
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013215 char_u *act;
13216 int action = ' ';
13217
Bram Moolenaar2641f772005-03-25 21:58:17 +000013218 rettv->vval.v_number = -1;
13219
13220#ifdef FEAT_QUICKFIX
13221 if (argvars[0].v_type != VAR_LIST)
13222 EMSG(_(e_listreq));
13223 else
13224 {
13225 list_T *l = argvars[0].vval.v_list;
13226
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013227 if (argvars[1].v_type == VAR_STRING)
13228 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013229 act = get_tv_string_chk(&argvars[1]);
13230 if (act == NULL)
13231 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013232 if (*act == 'a' || *act == 'r')
13233 action = *act;
13234 }
13235
13236 if (l != NULL && set_errorlist(l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013237 rettv->vval.v_number = 0;
13238 }
13239#endif
13240}
13241
13242/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013243 * "setreg()" function
13244 */
13245 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013246f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013247 typval_T *argvars;
13248 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013249{
13250 int regname;
13251 char_u *strregname;
13252 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013253 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013254 int append;
13255 char_u yank_type;
13256 long block_len;
13257
13258 block_len = -1;
13259 yank_type = MAUTO;
13260 append = FALSE;
13261
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013262 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013263 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013264
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013265 if (strregname == NULL)
13266 return; /* type error; errmsg already given */
13267 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013268 if (regname == 0 || regname == '@')
13269 regname = '"';
13270 else if (regname == '=')
13271 return;
13272
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013273 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013274 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013275 stropt = get_tv_string_chk(&argvars[2]);
13276 if (stropt == NULL)
13277 return; /* type error */
13278 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013279 switch (*stropt)
13280 {
13281 case 'a': case 'A': /* append */
13282 append = TRUE;
13283 break;
13284 case 'v': case 'c': /* character-wise selection */
13285 yank_type = MCHAR;
13286 break;
13287 case 'V': case 'l': /* line-wise selection */
13288 yank_type = MLINE;
13289 break;
13290#ifdef FEAT_VISUAL
13291 case 'b': case Ctrl_V: /* block-wise selection */
13292 yank_type = MBLOCK;
13293 if (VIM_ISDIGIT(stropt[1]))
13294 {
13295 ++stropt;
13296 block_len = getdigits(&stropt) - 1;
13297 --stropt;
13298 }
13299 break;
13300#endif
13301 }
13302 }
13303
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013304 strval = get_tv_string_chk(&argvars[1]);
13305 if (strval != NULL)
13306 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000013307 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013308 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013309}
13310
13311
13312/*
13313 * "setwinvar(expr)" function
13314 */
13315/*ARGSUSED*/
13316 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013317f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013318 typval_T *argvars;
13319 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013320{
13321 win_T *win;
13322#ifdef FEAT_WINDOWS
13323 win_T *save_curwin;
13324#endif
13325 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013326 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013327 char_u nbuf[NUMBUFLEN];
13328
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013329 rettv->vval.v_number = 0;
13330
Bram Moolenaar071d4272004-06-13 20:20:40 +000013331 if (check_restricted() || check_secure())
13332 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013333 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013334 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013335 varp = &argvars[2];
13336
13337 if (win != NULL && varname != NULL && varp != NULL)
13338 {
13339#ifdef FEAT_WINDOWS
13340 /* set curwin to be our win, temporarily */
13341 save_curwin = curwin;
13342 curwin = win;
13343 curbuf = curwin->w_buffer;
13344#endif
13345
13346 if (*varname == '&')
13347 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013348 long numval;
13349 char_u *strval;
13350 int error = FALSE;
13351
Bram Moolenaar071d4272004-06-13 20:20:40 +000013352 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013353 numval = get_tv_number_chk(varp, &error);
13354 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013355 if (!error && strval != NULL)
13356 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013357 }
13358 else
13359 {
13360 winvarname = alloc((unsigned)STRLEN(varname) + 3);
13361 if (winvarname != NULL)
13362 {
13363 STRCPY(winvarname, "w:");
13364 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013365 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013366 vim_free(winvarname);
13367 }
13368 }
13369
13370#ifdef FEAT_WINDOWS
13371 /* Restore current window, if it's still valid (autocomands can make
13372 * it invalid). */
13373 if (win_valid(save_curwin))
13374 {
13375 curwin = save_curwin;
13376 curbuf = curwin->w_buffer;
13377 }
13378#endif
13379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013380}
13381
13382/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013383 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013384 */
13385 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013386f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013387 typval_T *argvars;
13388 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013389{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013390 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013391
Bram Moolenaar0d660222005-01-07 21:51:51 +000013392 p = get_tv_string(&argvars[0]);
13393 rettv->vval.v_string = vim_strsave(p);
13394 simplify_filename(rettv->vval.v_string); /* simplify in place */
13395 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013396}
13397
Bram Moolenaar0d660222005-01-07 21:51:51 +000013398static int
13399#ifdef __BORLANDC__
13400 _RTLENTRYF
13401#endif
13402 item_compare __ARGS((const void *s1, const void *s2));
13403static int
13404#ifdef __BORLANDC__
13405 _RTLENTRYF
13406#endif
13407 item_compare2 __ARGS((const void *s1, const void *s2));
13408
13409static int item_compare_ic;
13410static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013411static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013412#define ITEM_COMPARE_FAIL 999
13413
Bram Moolenaar071d4272004-06-13 20:20:40 +000013414/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013415 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013416 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013417 static int
13418#ifdef __BORLANDC__
13419_RTLENTRYF
13420#endif
13421item_compare(s1, s2)
13422 const void *s1;
13423 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013424{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013425 char_u *p1, *p2;
13426 char_u *tofree1, *tofree2;
13427 int res;
13428 char_u numbuf1[NUMBUFLEN];
13429 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013430
Bram Moolenaar33570922005-01-25 22:26:29 +000013431 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1);
13432 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013433 if (item_compare_ic)
13434 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013435 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000013436 res = STRCMP(p1, p2);
13437 vim_free(tofree1);
13438 vim_free(tofree2);
13439 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013440}
13441
13442 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000013443#ifdef __BORLANDC__
13444_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000013445#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000013446item_compare2(s1, s2)
13447 const void *s1;
13448 const void *s2;
13449{
13450 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000013451 typval_T rettv;
13452 typval_T argv[2];
Bram Moolenaar0d660222005-01-07 21:51:51 +000013453 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013454
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013455 /* shortcut after failure in previous call; compare all items equal */
13456 if (item_compare_func_err)
13457 return 0;
13458
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013459 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
13460 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013461 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
13462 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013463
13464 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
13465 res = call_func(item_compare_func, STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000013466 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013467 clear_tv(&argv[0]);
13468 clear_tv(&argv[1]);
13469
13470 if (res == FAIL)
13471 res = ITEM_COMPARE_FAIL;
13472 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013473 /* return value has wrong type */
13474 res = get_tv_number_chk(&rettv, &item_compare_func_err);
13475 if (item_compare_func_err)
13476 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013477 clear_tv(&rettv);
13478 return res;
13479}
13480
13481/*
13482 * "sort({list})" function
13483 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013484 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013485f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013486 typval_T *argvars;
13487 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013488{
Bram Moolenaar33570922005-01-25 22:26:29 +000013489 list_T *l;
13490 listitem_T *li;
13491 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013492 long len;
13493 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013494
Bram Moolenaar0d660222005-01-07 21:51:51 +000013495 rettv->vval.v_number = 0;
13496 if (argvars[0].v_type != VAR_LIST)
13497 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000013498 else
13499 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013500 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013501 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013502 return;
13503 rettv->vval.v_list = l;
13504 rettv->v_type = VAR_LIST;
13505 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013506
Bram Moolenaar0d660222005-01-07 21:51:51 +000013507 len = list_len(l);
13508 if (len <= 1)
13509 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013510
Bram Moolenaar0d660222005-01-07 21:51:51 +000013511 item_compare_ic = FALSE;
13512 item_compare_func = NULL;
13513 if (argvars[1].v_type != VAR_UNKNOWN)
13514 {
13515 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013516 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013517 else
13518 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013519 int error = FALSE;
13520
13521 i = get_tv_number_chk(&argvars[1], &error);
13522 if (error)
13523 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013524 if (i == 1)
13525 item_compare_ic = TRUE;
13526 else
13527 item_compare_func = get_tv_string(&argvars[1]);
13528 }
13529 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013530
Bram Moolenaar0d660222005-01-07 21:51:51 +000013531 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013532 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013533 if (ptrs == NULL)
13534 return;
13535 i = 0;
13536 for (li = l->lv_first; li != NULL; li = li->li_next)
13537 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013538
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013539 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013540 /* test the compare function */
13541 if (item_compare_func != NULL
13542 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
13543 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013544 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013545 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000013546 {
13547 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013548 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000013549 item_compare_func == NULL ? item_compare : item_compare2);
13550
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013551 if (!item_compare_func_err)
13552 {
13553 /* Clear the List and append the items in the sorted order. */
13554 l->lv_first = l->lv_last = NULL;
13555 l->lv_len = 0;
13556 for (i = 0; i < len; ++i)
13557 list_append(l, ptrs[i]);
13558 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013559 }
13560
13561 vim_free(ptrs);
13562 }
13563}
13564
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013565/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000013566 * "soundfold({word})" function
13567 */
13568 static void
13569f_soundfold(argvars, rettv)
13570 typval_T *argvars;
13571 typval_T *rettv;
13572{
13573 char_u *s;
13574
13575 rettv->v_type = VAR_STRING;
13576 s = get_tv_string(&argvars[0]);
13577#ifdef FEAT_SYN_HL
13578 rettv->vval.v_string = eval_soundfold(s);
13579#else
13580 rettv->vval.v_string = vim_strsave(s);
13581#endif
13582}
13583
13584/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013585 * "spellbadword()" function
13586 */
13587/* ARGSUSED */
13588 static void
13589f_spellbadword(argvars, rettv)
13590 typval_T *argvars;
13591 typval_T *rettv;
13592{
13593 int attr;
13594 char_u *ptr;
13595 int len;
13596
13597 rettv->vval.v_string = NULL;
13598 rettv->v_type = VAR_STRING;
13599
13600#ifdef FEAT_SYN_HL
13601 /* Find the start of the badly spelled word. */
13602 if (spell_move_to(FORWARD, TRUE, TRUE) == FAIL)
13603 return;
13604
13605 /* Get the length of the word and copy it. */
13606 ptr = ml_get_cursor();
Bram Moolenaar51ac12f2005-07-02 23:21:11 +000013607 len = spell_check(curwin, ptr, &attr, NULL);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013608 rettv->vval.v_string = vim_strnsave(ptr, len);
13609#endif
13610}
13611
13612/*
13613 * "spellsuggest()" function
13614 */
13615 static void
13616f_spellsuggest(argvars, rettv)
13617 typval_T *argvars;
13618 typval_T *rettv;
13619{
13620 char_u *str;
13621 int maxcount;
13622 garray_T ga;
13623 list_T *l;
13624 listitem_T *li;
13625 int i;
13626
13627 l = list_alloc();
13628 if (l == NULL)
13629 return;
13630 rettv->v_type = VAR_LIST;
13631 rettv->vval.v_list = l;
13632 ++l->lv_refcount;
13633
13634#ifdef FEAT_SYN_HL
13635 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
13636 {
13637 str = get_tv_string(&argvars[0]);
13638 if (argvars[1].v_type != VAR_UNKNOWN)
13639 {
13640 maxcount = get_tv_number(&argvars[1]);
13641 if (maxcount <= 0)
13642 return;
13643 }
13644 else
13645 maxcount = 25;
13646
13647 spell_suggest_list(&ga, str, maxcount);
13648
13649 for (i = 0; i < ga.ga_len; ++i)
13650 {
13651 str = ((char_u **)ga.ga_data)[i];
13652
13653 li = listitem_alloc();
13654 if (li == NULL)
13655 vim_free(str);
13656 else
13657 {
13658 li->li_tv.v_type = VAR_STRING;
13659 li->li_tv.v_lock = 0;
13660 li->li_tv.vval.v_string = str;
13661 list_append(l, li);
13662 }
13663 }
13664 ga_clear(&ga);
13665 }
13666#endif
13667}
13668
Bram Moolenaar0d660222005-01-07 21:51:51 +000013669 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013670f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013671 typval_T *argvars;
13672 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013673{
13674 char_u *str;
13675 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013676 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013677 regmatch_T regmatch;
13678 char_u patbuf[NUMBUFLEN];
13679 char_u *save_cpo;
13680 int match;
Bram Moolenaar33570922005-01-25 22:26:29 +000013681 listitem_T *ni;
13682 list_T *l;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013683 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013684 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013685 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013686
13687 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13688 save_cpo = p_cpo;
13689 p_cpo = (char_u *)"";
13690
13691 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013692 if (argvars[1].v_type != VAR_UNKNOWN)
13693 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013694 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13695 if (pat == NULL)
13696 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013697 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013698 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013699 }
13700 if (pat == NULL || *pat == NUL)
13701 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000013702
13703 l = list_alloc();
13704 if (l == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013705 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013706 rettv->v_type = VAR_LIST;
13707 rettv->vval.v_list = l;
13708 ++l->lv_refcount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013709 if (typeerr)
13710 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013711
Bram Moolenaar0d660222005-01-07 21:51:51 +000013712 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13713 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013714 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013715 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013716 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013717 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013718 if (*str == NUL)
13719 match = FALSE; /* empty item at the end */
13720 else
13721 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013722 if (match)
13723 end = regmatch.startp[0];
13724 else
13725 end = str + STRLEN(str);
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013726 if (keepempty || end > str || (l->lv_len > 0 && *str != NUL
13727 && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013728 {
13729 ni = listitem_alloc();
13730 if (ni == NULL)
13731 break;
13732 ni->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013733 ni->li_tv.v_lock = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013734 ni->li_tv.vval.v_string = vim_strnsave(str, end - str);
13735 list_append(l, ni);
13736 }
13737 if (!match)
13738 break;
13739 /* Advance to just after the match. */
13740 if (regmatch.endp[0] > str)
13741 col = 0;
13742 else
13743 {
13744 /* Don't get stuck at the same match. */
13745#ifdef FEAT_MBYTE
13746 col = mb_ptr2len_check(regmatch.endp[0]);
13747#else
13748 col = 1;
13749#endif
13750 }
13751 str = regmatch.endp[0];
13752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013753
Bram Moolenaar0d660222005-01-07 21:51:51 +000013754 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013756
Bram Moolenaar0d660222005-01-07 21:51:51 +000013757 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013758}
13759
13760#ifdef HAVE_STRFTIME
13761/*
13762 * "strftime({format}[, {time}])" function
13763 */
13764 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013765f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013766 typval_T *argvars;
13767 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013768{
13769 char_u result_buf[256];
13770 struct tm *curtime;
13771 time_t seconds;
13772 char_u *p;
13773
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013774 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013775
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013776 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013777 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013778 seconds = time(NULL);
13779 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013780 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013781 curtime = localtime(&seconds);
13782 /* MSVC returns NULL for an invalid value of seconds. */
13783 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013784 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013785 else
13786 {
13787# ifdef FEAT_MBYTE
13788 vimconv_T conv;
13789 char_u *enc;
13790
13791 conv.vc_type = CONV_NONE;
13792 enc = enc_locale();
13793 convert_setup(&conv, p_enc, enc);
13794 if (conv.vc_type != CONV_NONE)
13795 p = string_convert(&conv, p, NULL);
13796# endif
13797 if (p != NULL)
13798 (void)strftime((char *)result_buf, sizeof(result_buf),
13799 (char *)p, curtime);
13800 else
13801 result_buf[0] = NUL;
13802
13803# ifdef FEAT_MBYTE
13804 if (conv.vc_type != CONV_NONE)
13805 vim_free(p);
13806 convert_setup(&conv, enc, p_enc);
13807 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013808 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013809 else
13810# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013811 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013812
13813# ifdef FEAT_MBYTE
13814 /* Release conversion descriptors */
13815 convert_setup(&conv, NULL, NULL);
13816 vim_free(enc);
13817# endif
13818 }
13819}
13820#endif
13821
13822/*
13823 * "stridx()" function
13824 */
13825 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013826f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013827 typval_T *argvars;
13828 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013829{
13830 char_u buf[NUMBUFLEN];
13831 char_u *needle;
13832 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000013833 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013834 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000013835 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013836
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013837 needle = get_tv_string_chk(&argvars[1]);
13838 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000013839 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013840 if (needle == NULL || haystack == NULL)
13841 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013842
Bram Moolenaar33570922005-01-25 22:26:29 +000013843 if (argvars[2].v_type != VAR_UNKNOWN)
13844 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013845 int error = FALSE;
13846
13847 start_idx = get_tv_number_chk(&argvars[2], &error);
13848 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000013849 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000013850 if (start_idx >= 0)
13851 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000013852 }
13853
13854 pos = (char_u *)strstr((char *)haystack, (char *)needle);
13855 if (pos != NULL)
13856 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013857}
13858
13859/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013860 * "string()" function
13861 */
13862 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013863f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013864 typval_T *argvars;
13865 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013866{
13867 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013868 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013869
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013870 rettv->v_type = VAR_STRING;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013871 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013872 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013873 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013874}
13875
13876/*
13877 * "strlen()" function
13878 */
13879 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013880f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013881 typval_T *argvars;
13882 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013883{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013884 rettv->vval.v_number = (varnumber_T)(STRLEN(
13885 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013886}
13887
13888/*
13889 * "strpart()" function
13890 */
13891 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013892f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013893 typval_T *argvars;
13894 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013895{
13896 char_u *p;
13897 int n;
13898 int len;
13899 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013900 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013901
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013902 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013903 slen = (int)STRLEN(p);
13904
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013905 n = get_tv_number_chk(&argvars[1], &error);
13906 if (error)
13907 len = 0;
13908 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013909 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013910 else
13911 len = slen - n; /* default len: all bytes that are available. */
13912
13913 /*
13914 * Only return the overlap between the specified part and the actual
13915 * string.
13916 */
13917 if (n < 0)
13918 {
13919 len += n;
13920 n = 0;
13921 }
13922 else if (n > slen)
13923 n = slen;
13924 if (len < 0)
13925 len = 0;
13926 else if (n + len > slen)
13927 len = slen - n;
13928
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013929 rettv->v_type = VAR_STRING;
13930 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013931}
13932
13933/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013934 * "strridx()" function
13935 */
13936 static void
13937f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013938 typval_T *argvars;
13939 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013940{
13941 char_u buf[NUMBUFLEN];
13942 char_u *needle;
13943 char_u *haystack;
13944 char_u *rest;
13945 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000013946 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013947
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013948 needle = get_tv_string_chk(&argvars[1]);
13949 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar532c7802005-01-27 14:44:31 +000013950 haystack_len = STRLEN(haystack);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013951
13952 rettv->vval.v_number = -1;
13953 if (needle == NULL || haystack == NULL)
13954 return; /* type error; errmsg already given */
Bram Moolenaar05159a02005-02-26 23:04:13 +000013955 if (argvars[2].v_type != VAR_UNKNOWN)
13956 {
13957 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013958 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000013959 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013960 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000013961 }
13962 else
13963 end_idx = haystack_len;
13964
Bram Moolenaar0d660222005-01-07 21:51:51 +000013965 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000013966 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013967 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000013968 lastmatch = haystack + end_idx;
13969 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013970 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000013971 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013972 for (rest = haystack; *rest != '\0'; ++rest)
13973 {
13974 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000013975 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013976 break;
13977 lastmatch = rest;
13978 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000013979 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013980
13981 if (lastmatch == NULL)
13982 rettv->vval.v_number = -1;
13983 else
13984 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
13985}
13986
13987/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013988 * "strtrans()" function
13989 */
13990 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013991f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013992 typval_T *argvars;
13993 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013994{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013995 rettv->v_type = VAR_STRING;
13996 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013997}
13998
13999/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014000 * "submatch()" function
14001 */
14002 static void
14003f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014004 typval_T *argvars;
14005 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014006{
14007 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014008 rettv->vval.v_string =
14009 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014010}
14011
14012/*
14013 * "substitute()" function
14014 */
14015 static void
14016f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014017 typval_T *argvars;
14018 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014019{
14020 char_u patbuf[NUMBUFLEN];
14021 char_u subbuf[NUMBUFLEN];
14022 char_u flagsbuf[NUMBUFLEN];
14023
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014024 char_u *str = get_tv_string_chk(&argvars[0]);
14025 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14026 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
14027 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
14028
Bram Moolenaar0d660222005-01-07 21:51:51 +000014029 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014030 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
14031 rettv->vval.v_string = NULL;
14032 else
14033 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014034}
14035
14036/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014037 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014038 */
14039/*ARGSUSED*/
14040 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014041f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014042 typval_T *argvars;
14043 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014044{
14045 int id = 0;
14046#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014047 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014048 long col;
14049 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000014050 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014051
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014052 lnum = get_tv_lnum(argvars); /* -1 on type error */
14053 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
14054 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014055
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014056 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014057 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
14058 id = syn_get_id(lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014059#endif
14060
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014061 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014062}
14063
14064/*
14065 * "synIDattr(id, what [, mode])" function
14066 */
14067/*ARGSUSED*/
14068 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014069f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014070 typval_T *argvars;
14071 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014072{
14073 char_u *p = NULL;
14074#ifdef FEAT_SYN_HL
14075 int id;
14076 char_u *what;
14077 char_u *mode;
14078 char_u modebuf[NUMBUFLEN];
14079 int modec;
14080
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014081 id = get_tv_number(&argvars[0]);
14082 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014083 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014084 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014085 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014086 modec = TOLOWER_ASC(mode[0]);
14087 if (modec != 't' && modec != 'c'
14088#ifdef FEAT_GUI
14089 && modec != 'g'
14090#endif
14091 )
14092 modec = 0; /* replace invalid with current */
14093 }
14094 else
14095 {
14096#ifdef FEAT_GUI
14097 if (gui.in_use)
14098 modec = 'g';
14099 else
14100#endif
14101 if (t_colors > 1)
14102 modec = 'c';
14103 else
14104 modec = 't';
14105 }
14106
14107
14108 switch (TOLOWER_ASC(what[0]))
14109 {
14110 case 'b':
14111 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
14112 p = highlight_color(id, what, modec);
14113 else /* bold */
14114 p = highlight_has_attr(id, HL_BOLD, modec);
14115 break;
14116
14117 case 'f': /* fg[#] */
14118 p = highlight_color(id, what, modec);
14119 break;
14120
14121 case 'i':
14122 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
14123 p = highlight_has_attr(id, HL_INVERSE, modec);
14124 else /* italic */
14125 p = highlight_has_attr(id, HL_ITALIC, modec);
14126 break;
14127
14128 case 'n': /* name */
14129 p = get_highlight_name(NULL, id - 1);
14130 break;
14131
14132 case 'r': /* reverse */
14133 p = highlight_has_attr(id, HL_INVERSE, modec);
14134 break;
14135
14136 case 's': /* standout */
14137 p = highlight_has_attr(id, HL_STANDOUT, modec);
14138 break;
14139
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000014140 case 'u':
14141 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
14142 /* underline */
14143 p = highlight_has_attr(id, HL_UNDERLINE, modec);
14144 else
14145 /* undercurl */
14146 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014147 break;
14148 }
14149
14150 if (p != NULL)
14151 p = vim_strsave(p);
14152#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014153 rettv->v_type = VAR_STRING;
14154 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014155}
14156
14157/*
14158 * "synIDtrans(id)" function
14159 */
14160/*ARGSUSED*/
14161 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014162f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014163 typval_T *argvars;
14164 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014165{
14166 int id;
14167
14168#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014169 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014170
14171 if (id > 0)
14172 id = syn_get_final_id(id);
14173 else
14174#endif
14175 id = 0;
14176
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014177 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014178}
14179
14180/*
14181 * "system()" function
14182 */
14183 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014184f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014185 typval_T *argvars;
14186 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014187{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014188 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014189 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014190 char_u *infile = NULL;
14191 char_u buf[NUMBUFLEN];
14192 int err = FALSE;
14193 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014194
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014195 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014196 {
14197 /*
14198 * Write the string to a temp file, to be used for input of the shell
14199 * command.
14200 */
14201 if ((infile = vim_tempname('i')) == NULL)
14202 {
14203 EMSG(_(e_notmp));
14204 return;
14205 }
14206
14207 fd = mch_fopen((char *)infile, WRITEBIN);
14208 if (fd == NULL)
14209 {
14210 EMSG2(_(e_notopen), infile);
14211 goto done;
14212 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014213 p = get_tv_string_buf_chk(&argvars[1], buf);
14214 if (p == NULL)
14215 goto done; /* type error; errmsg already given */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014216 if (fwrite(p, STRLEN(p), 1, fd) != 1)
14217 err = TRUE;
14218 if (fclose(fd) != 0)
14219 err = TRUE;
14220 if (err)
14221 {
14222 EMSG(_("E677: Error writing temp file"));
14223 goto done;
14224 }
14225 }
14226
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014227 res = get_cmd_output(get_tv_string(&argvars[0]), infile, SHELL_SILENT);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014228
Bram Moolenaar071d4272004-06-13 20:20:40 +000014229#ifdef USE_CR
14230 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014231 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014232 {
14233 char_u *s;
14234
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014235 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014236 {
14237 if (*s == CAR)
14238 *s = NL;
14239 }
14240 }
14241#else
14242# ifdef USE_CRNL
14243 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014244 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014245 {
14246 char_u *s, *d;
14247
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014248 d = res;
14249 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014250 {
14251 if (s[0] == CAR && s[1] == NL)
14252 ++s;
14253 *d++ = *s;
14254 }
14255 *d = NUL;
14256 }
14257# endif
14258#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014259
14260done:
14261 if (infile != NULL)
14262 {
14263 mch_remove(infile);
14264 vim_free(infile);
14265 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014266 rettv->v_type = VAR_STRING;
14267 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014268}
14269
14270/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000014271 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014272 */
14273 static void
14274f_taglist(argvars, rettv)
14275 typval_T *argvars;
14276 typval_T *rettv;
14277{
14278 char_u *tag_pattern;
14279 list_T *l;
14280
14281 tag_pattern = get_tv_string(&argvars[0]);
14282
14283 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014284 if (*tag_pattern == NUL)
14285 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014286
14287 l = list_alloc();
14288 if (l != NULL)
14289 {
14290 if (get_tags(l, tag_pattern) != FAIL)
14291 {
14292 rettv->vval.v_list = l;
14293 rettv->v_type = VAR_LIST;
14294 ++l->lv_refcount;
14295 }
14296 else
14297 list_free(l);
14298 }
14299}
14300
14301/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014302 * "tempname()" function
14303 */
14304/*ARGSUSED*/
14305 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014306f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014307 typval_T *argvars;
14308 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014309{
14310 static int x = 'A';
14311
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014312 rettv->v_type = VAR_STRING;
14313 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014314
14315 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
14316 * names. Skip 'I' and 'O', they are used for shell redirection. */
14317 do
14318 {
14319 if (x == 'Z')
14320 x = '0';
14321 else if (x == '9')
14322 x = 'A';
14323 else
14324 {
14325#ifdef EBCDIC
14326 if (x == 'I')
14327 x = 'J';
14328 else if (x == 'R')
14329 x = 'S';
14330 else
14331#endif
14332 ++x;
14333 }
14334 } while (x == 'I' || x == 'O');
14335}
14336
14337/*
14338 * "tolower(string)" function
14339 */
14340 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014341f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014342 typval_T *argvars;
14343 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014344{
14345 char_u *p;
14346
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014347 p = vim_strsave(get_tv_string(&argvars[0]));
14348 rettv->v_type = VAR_STRING;
14349 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014350
14351 if (p != NULL)
14352 while (*p != NUL)
14353 {
14354#ifdef FEAT_MBYTE
14355 int l;
14356
14357 if (enc_utf8)
14358 {
14359 int c, lc;
14360
14361 c = utf_ptr2char(p);
14362 lc = utf_tolower(c);
14363 l = utf_ptr2len_check(p);
14364 /* TODO: reallocate string when byte count changes. */
14365 if (utf_char2len(lc) == l)
14366 utf_char2bytes(lc, p);
14367 p += l;
14368 }
14369 else if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
14370 p += l; /* skip multi-byte character */
14371 else
14372#endif
14373 {
14374 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
14375 ++p;
14376 }
14377 }
14378}
14379
14380/*
14381 * "toupper(string)" function
14382 */
14383 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014384f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014385 typval_T *argvars;
14386 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014387{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014388 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014389 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014390}
14391
14392/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000014393 * "tr(string, fromstr, tostr)" function
14394 */
14395 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014396f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014397 typval_T *argvars;
14398 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014399{
14400 char_u *instr;
14401 char_u *fromstr;
14402 char_u *tostr;
14403 char_u *p;
14404#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000014405 int inlen;
14406 int fromlen;
14407 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014408 int idx;
14409 char_u *cpstr;
14410 int cplen;
14411 int first = TRUE;
14412#endif
14413 char_u buf[NUMBUFLEN];
14414 char_u buf2[NUMBUFLEN];
14415 garray_T ga;
14416
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014417 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014418 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
14419 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014420
14421 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014422 rettv->v_type = VAR_STRING;
14423 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014424 if (fromstr == NULL || tostr == NULL)
14425 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000014426 ga_init2(&ga, (int)sizeof(char), 80);
14427
14428#ifdef FEAT_MBYTE
14429 if (!has_mbyte)
14430#endif
14431 /* not multi-byte: fromstr and tostr must be the same length */
14432 if (STRLEN(fromstr) != STRLEN(tostr))
14433 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014434#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000014435error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014436#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000014437 EMSG2(_(e_invarg2), fromstr);
14438 ga_clear(&ga);
14439 return;
14440 }
14441
14442 /* fromstr and tostr have to contain the same number of chars */
14443 while (*instr != NUL)
14444 {
14445#ifdef FEAT_MBYTE
14446 if (has_mbyte)
14447 {
14448 inlen = mb_ptr2len_check(instr);
14449 cpstr = instr;
14450 cplen = inlen;
14451 idx = 0;
14452 for (p = fromstr; *p != NUL; p += fromlen)
14453 {
14454 fromlen = mb_ptr2len_check(p);
14455 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
14456 {
14457 for (p = tostr; *p != NUL; p += tolen)
14458 {
14459 tolen = mb_ptr2len_check(p);
14460 if (idx-- == 0)
14461 {
14462 cplen = tolen;
14463 cpstr = p;
14464 break;
14465 }
14466 }
14467 if (*p == NUL) /* tostr is shorter than fromstr */
14468 goto error;
14469 break;
14470 }
14471 ++idx;
14472 }
14473
14474 if (first && cpstr == instr)
14475 {
14476 /* Check that fromstr and tostr have the same number of
14477 * (multi-byte) characters. Done only once when a character
14478 * of instr doesn't appear in fromstr. */
14479 first = FALSE;
14480 for (p = tostr; *p != NUL; p += tolen)
14481 {
14482 tolen = mb_ptr2len_check(p);
14483 --idx;
14484 }
14485 if (idx != 0)
14486 goto error;
14487 }
14488
14489 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000014490 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014491 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014492
14493 instr += inlen;
14494 }
14495 else
14496#endif
14497 {
14498 /* When not using multi-byte chars we can do it faster. */
14499 p = vim_strchr(fromstr, *instr);
14500 if (p != NULL)
14501 ga_append(&ga, tostr[p - fromstr]);
14502 else
14503 ga_append(&ga, *instr);
14504 ++instr;
14505 }
14506 }
14507
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014508 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014509}
14510
14511/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014512 * "type(expr)" function
14513 */
14514 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014515f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014516 typval_T *argvars;
14517 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014518{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014519 int n;
14520
14521 switch (argvars[0].v_type)
14522 {
14523 case VAR_NUMBER: n = 0; break;
14524 case VAR_STRING: n = 1; break;
14525 case VAR_FUNC: n = 2; break;
14526 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014527 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014528 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
14529 }
14530 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014531}
14532
14533/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014534 * "values(dict)" function
14535 */
14536 static void
14537f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014538 typval_T *argvars;
14539 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014540{
14541 dict_list(argvars, rettv, 1);
14542}
14543
14544/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014545 * "virtcol(string)" function
14546 */
14547 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014548f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014549 typval_T *argvars;
14550 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014551{
14552 colnr_T vcol = 0;
14553 pos_T *fp;
14554
14555 fp = var2fpos(&argvars[0], FALSE);
14556 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count)
14557 {
14558 getvvcol(curwin, fp, NULL, NULL, &vcol);
14559 ++vcol;
14560 }
14561
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014562 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014563}
14564
14565/*
14566 * "visualmode()" function
14567 */
14568/*ARGSUSED*/
14569 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014570f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014571 typval_T *argvars;
14572 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014573{
14574#ifdef FEAT_VISUAL
14575 char_u str[2];
14576
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014577 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014578 str[0] = curbuf->b_visual_mode_eval;
14579 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014580 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014581
14582 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014583 if ((argvars[0].v_type == VAR_NUMBER
14584 && argvars[0].vval.v_number != 0)
14585 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014586 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014587 curbuf->b_visual_mode_eval = NUL;
14588#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014589 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014590#endif
14591}
14592
14593/*
14594 * "winbufnr(nr)" function
14595 */
14596 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014597f_winbufnr(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{
14601 win_T *wp;
14602
14603 wp = find_win_by_nr(&argvars[0]);
14604 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014605 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014606 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014607 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014608}
14609
14610/*
14611 * "wincol()" function
14612 */
14613/*ARGSUSED*/
14614 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014615f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014616 typval_T *argvars;
14617 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014618{
14619 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014620 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014621}
14622
14623/*
14624 * "winheight(nr)" function
14625 */
14626 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014627f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014628 typval_T *argvars;
14629 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014630{
14631 win_T *wp;
14632
14633 wp = find_win_by_nr(&argvars[0]);
14634 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014635 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014636 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014637 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014638}
14639
14640/*
14641 * "winline()" function
14642 */
14643/*ARGSUSED*/
14644 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014645f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014646 typval_T *argvars;
14647 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014648{
14649 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014650 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014651}
14652
14653/*
14654 * "winnr()" function
14655 */
14656/* ARGSUSED */
14657 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014658f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014659 typval_T *argvars;
14660 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014661{
14662 int nr = 1;
14663#ifdef FEAT_WINDOWS
14664 win_T *wp;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014665 win_T *twin = curwin;
14666 char_u *arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014667
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014668 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014669 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014670 arg = get_tv_string_chk(&argvars[0]);
14671 if (arg == NULL)
14672 nr = 0; /* type error; errmsg already given */
14673 else if (STRCMP(arg, "$") == 0)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014674 twin = lastwin;
14675 else if (STRCMP(arg, "#") == 0)
14676 {
14677 twin = prevwin;
14678 if (prevwin == NULL)
14679 nr = 0;
14680 }
14681 else
14682 {
14683 EMSG2(_(e_invexpr2), arg);
14684 nr = 0;
14685 }
14686 }
14687
14688 if (nr > 0)
14689 for (wp = firstwin; wp != twin; wp = wp->w_next)
14690 ++nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014691#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014692 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014693}
14694
14695/*
14696 * "winrestcmd()" function
14697 */
14698/* ARGSUSED */
14699 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014700f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014701 typval_T *argvars;
14702 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014703{
14704#ifdef FEAT_WINDOWS
14705 win_T *wp;
14706 int winnr = 1;
14707 garray_T ga;
14708 char_u buf[50];
14709
14710 ga_init2(&ga, (int)sizeof(char), 70);
14711 for (wp = firstwin; wp != NULL; wp = wp->w_next)
14712 {
14713 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
14714 ga_concat(&ga, buf);
14715# ifdef FEAT_VERTSPLIT
14716 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
14717 ga_concat(&ga, buf);
14718# endif
14719 ++winnr;
14720 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000014721 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014722
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014723 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014724#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014725 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014726#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014727 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014728}
14729
14730/*
14731 * "winwidth(nr)" function
14732 */
14733 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014734f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014735 typval_T *argvars;
14736 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014737{
14738 win_T *wp;
14739
14740 wp = find_win_by_nr(&argvars[0]);
14741 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014742 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014743 else
14744#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014745 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014746#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014747 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014748#endif
14749}
14750
Bram Moolenaar071d4272004-06-13 20:20:40 +000014751/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014752 * "writefile()" function
14753 */
14754 static void
14755f_writefile(argvars, rettv)
14756 typval_T *argvars;
14757 typval_T *rettv;
14758{
14759 int binary = FALSE;
14760 char_u *fname;
14761 FILE *fd;
14762 listitem_T *li;
14763 char_u *s;
14764 int ret = 0;
14765 int c;
14766
14767 if (argvars[0].v_type != VAR_LIST)
14768 {
14769 EMSG2(_(e_listarg), "writefile()");
14770 return;
14771 }
14772 if (argvars[0].vval.v_list == NULL)
14773 return;
14774
14775 if (argvars[2].v_type != VAR_UNKNOWN
14776 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
14777 binary = TRUE;
14778
14779 /* Always open the file in binary mode, library functions have a mind of
14780 * their own about CR-LF conversion. */
14781 fname = get_tv_string(&argvars[1]);
14782 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
14783 {
14784 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
14785 ret = -1;
14786 }
14787 else
14788 {
14789 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
14790 li = li->li_next)
14791 {
14792 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
14793 {
14794 if (*s == '\n')
14795 c = putc(NUL, fd);
14796 else
14797 c = putc(*s, fd);
14798 if (c == EOF)
14799 {
14800 ret = -1;
14801 break;
14802 }
14803 }
14804 if (!binary || li->li_next != NULL)
14805 if (putc('\n', fd) == EOF)
14806 {
14807 ret = -1;
14808 break;
14809 }
14810 if (ret < 0)
14811 {
14812 EMSG(_(e_write));
14813 break;
14814 }
14815 }
14816 fclose(fd);
14817 }
14818
14819 rettv->vval.v_number = ret;
14820}
14821
14822/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014823 * Translate a String variable into a position.
14824 */
14825 static pos_T *
14826var2fpos(varp, lnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000014827 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014828 int lnum; /* TRUE when $ is last line */
14829{
14830 char_u *name;
14831 static pos_T pos;
14832 pos_T *pp;
14833
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014834 name = get_tv_string_chk(varp);
14835 if (name == NULL)
14836 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014837 if (name[0] == '.') /* cursor */
14838 return &curwin->w_cursor;
14839 if (name[0] == '\'') /* mark */
14840 {
14841 pp = getmark(name[1], FALSE);
14842 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
14843 return NULL;
14844 return pp;
14845 }
14846 if (name[0] == '$') /* last column or line */
14847 {
14848 if (lnum)
14849 {
14850 pos.lnum = curbuf->b_ml.ml_line_count;
14851 pos.col = 0;
14852 }
14853 else
14854 {
14855 pos.lnum = curwin->w_cursor.lnum;
14856 pos.col = (colnr_T)STRLEN(ml_get_curline());
14857 }
14858 return &pos;
14859 }
14860 return NULL;
14861}
14862
14863/*
14864 * Get the length of an environment variable name.
14865 * Advance "arg" to the first character after the name.
14866 * Return 0 for error.
14867 */
14868 static int
14869get_env_len(arg)
14870 char_u **arg;
14871{
14872 char_u *p;
14873 int len;
14874
14875 for (p = *arg; vim_isIDc(*p); ++p)
14876 ;
14877 if (p == *arg) /* no name found */
14878 return 0;
14879
14880 len = (int)(p - *arg);
14881 *arg = p;
14882 return len;
14883}
14884
14885/*
14886 * Get the length of the name of a function or internal variable.
14887 * "arg" is advanced to the first non-white character after the name.
14888 * Return 0 if something is wrong.
14889 */
14890 static int
14891get_id_len(arg)
14892 char_u **arg;
14893{
14894 char_u *p;
14895 int len;
14896
14897 /* Find the end of the name. */
14898 for (p = *arg; eval_isnamec(*p); ++p)
14899 ;
14900 if (p == *arg) /* no name found */
14901 return 0;
14902
14903 len = (int)(p - *arg);
14904 *arg = skipwhite(p);
14905
14906 return len;
14907}
14908
14909/*
Bram Moolenaara7043832005-01-21 11:56:39 +000014910 * Get the length of the name of a variable or function.
14911 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000014912 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014913 * Return -1 if curly braces expansion failed.
14914 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014915 * If the name contains 'magic' {}'s, expand them and return the
14916 * expanded name in an allocated string via 'alias' - caller must free.
14917 */
14918 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014919get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014920 char_u **arg;
14921 char_u **alias;
14922 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014923 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014924{
14925 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014926 char_u *p;
14927 char_u *expr_start;
14928 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014929
14930 *alias = NULL; /* default to no alias */
14931
14932 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
14933 && (*arg)[2] == (int)KE_SNR)
14934 {
14935 /* hard coded <SNR>, already translated */
14936 *arg += 3;
14937 return get_id_len(arg) + 3;
14938 }
14939 len = eval_fname_script(*arg);
14940 if (len > 0)
14941 {
14942 /* literal "<SID>", "s:" or "<SNR>" */
14943 *arg += len;
14944 }
14945
Bram Moolenaar071d4272004-06-13 20:20:40 +000014946 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014947 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014948 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014949 p = find_name_end(*arg, &expr_start, &expr_end,
14950 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014951 if (expr_start != NULL)
14952 {
14953 char_u *temp_string;
14954
14955 if (!evaluate)
14956 {
14957 len += (int)(p - *arg);
14958 *arg = skipwhite(p);
14959 return len;
14960 }
14961
14962 /*
14963 * Include any <SID> etc in the expanded string:
14964 * Thus the -len here.
14965 */
14966 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
14967 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014968 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014969 *alias = temp_string;
14970 *arg = skipwhite(p);
14971 return (int)STRLEN(temp_string);
14972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014973
14974 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014975 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014976 EMSG2(_(e_invexpr2), *arg);
14977
14978 return len;
14979}
14980
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014981/*
14982 * Find the end of a variable or function name, taking care of magic braces.
14983 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
14984 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014985 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014986 * Return a pointer to just after the name. Equal to "arg" if there is no
14987 * valid name.
14988 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014989 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014990find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014991 char_u *arg;
14992 char_u **expr_start;
14993 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014994 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014995{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014996 int mb_nest = 0;
14997 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014998 char_u *p;
14999
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015000 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015001 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015002 *expr_start = NULL;
15003 *expr_end = NULL;
15004 }
15005
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015006 /* Quick check for valid starting character. */
15007 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
15008 return arg;
15009
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015010 for (p = arg; *p != NUL
15011 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015012 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015013 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015014 || mb_nest != 0
15015 || br_nest != 0); ++p)
15016 {
15017 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015018 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015019 if (*p == '[')
15020 ++br_nest;
15021 else if (*p == ']')
15022 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015023 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015024 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015025 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015026 if (*p == '{')
15027 {
15028 mb_nest++;
15029 if (expr_start != NULL && *expr_start == NULL)
15030 *expr_start = p;
15031 }
15032 else if (*p == '}')
15033 {
15034 mb_nest--;
15035 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
15036 *expr_end = p;
15037 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015038 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015039 }
15040
15041 return p;
15042}
15043
15044/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015045 * Expands out the 'magic' {}'s in a variable/function name.
15046 * Note that this can call itself recursively, to deal with
15047 * constructs like foo{bar}{baz}{bam}
15048 * The four pointer arguments point to "foo{expre}ss{ion}bar"
15049 * "in_start" ^
15050 * "expr_start" ^
15051 * "expr_end" ^
15052 * "in_end" ^
15053 *
15054 * Returns a new allocated string, which the caller must free.
15055 * Returns NULL for failure.
15056 */
15057 static char_u *
15058make_expanded_name(in_start, expr_start, expr_end, in_end)
15059 char_u *in_start;
15060 char_u *expr_start;
15061 char_u *expr_end;
15062 char_u *in_end;
15063{
15064 char_u c1;
15065 char_u *retval = NULL;
15066 char_u *temp_result;
15067 char_u *nextcmd = NULL;
15068
15069 if (expr_end == NULL || in_end == NULL)
15070 return NULL;
15071 *expr_start = NUL;
15072 *expr_end = NUL;
15073 c1 = *in_end;
15074 *in_end = NUL;
15075
15076 temp_result = eval_to_string(expr_start + 1, &nextcmd);
15077 if (temp_result != NULL && nextcmd == NULL)
15078 {
15079 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
15080 + (in_end - expr_end) + 1));
15081 if (retval != NULL)
15082 {
15083 STRCPY(retval, in_start);
15084 STRCAT(retval, temp_result);
15085 STRCAT(retval, expr_end + 1);
15086 }
15087 }
15088 vim_free(temp_result);
15089
15090 *in_end = c1; /* put char back for error messages */
15091 *expr_start = '{';
15092 *expr_end = '}';
15093
15094 if (retval != NULL)
15095 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015096 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015097 if (expr_start != NULL)
15098 {
15099 /* Further expansion! */
15100 temp_result = make_expanded_name(retval, expr_start,
15101 expr_end, temp_result);
15102 vim_free(retval);
15103 retval = temp_result;
15104 }
15105 }
15106
15107 return retval;
15108}
15109
15110/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015111 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000015112 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015113 */
15114 static int
15115eval_isnamec(c)
15116 int c;
15117{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015118 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
15119}
15120
15121/*
15122 * Return TRUE if character "c" can be used as the first character in a
15123 * variable or function name (excluding '{' and '}').
15124 */
15125 static int
15126eval_isnamec1(c)
15127 int c;
15128{
15129 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000015130}
15131
15132/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015133 * Set number v: variable to "val".
15134 */
15135 void
15136set_vim_var_nr(idx, val)
15137 int idx;
15138 long val;
15139{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015140 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015141}
15142
15143/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015144 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015145 */
15146 long
15147get_vim_var_nr(idx)
15148 int idx;
15149{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015150 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015151}
15152
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015153#if defined(FEAT_AUTOCMD) || defined(PROTO)
15154/*
15155 * Get string v: variable value. Uses a static buffer, can only be used once.
15156 */
15157 char_u *
15158get_vim_var_str(idx)
15159 int idx;
15160{
15161 return get_tv_string(&vimvars[idx].vv_tv);
15162}
15163#endif
15164
Bram Moolenaar071d4272004-06-13 20:20:40 +000015165/*
15166 * Set v:count, v:count1 and v:prevcount.
15167 */
15168 void
15169set_vcount(count, count1)
15170 long count;
15171 long count1;
15172{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015173 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
15174 vimvars[VV_COUNT].vv_nr = count;
15175 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015176}
15177
15178/*
15179 * Set string v: variable to a copy of "val".
15180 */
15181 void
15182set_vim_var_string(idx, val, len)
15183 int idx;
15184 char_u *val;
15185 int len; /* length of "val" to use or -1 (whole string) */
15186{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015187 /* Need to do this (at least) once, since we can't initialize a union.
15188 * Will always be invoked when "v:progname" is set. */
15189 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
15190
Bram Moolenaare9a41262005-01-15 22:18:47 +000015191 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015192 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015193 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015194 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015195 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015196 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000015197 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015198}
15199
15200/*
15201 * Set v:register if needed.
15202 */
15203 void
15204set_reg_var(c)
15205 int c;
15206{
15207 char_u regname;
15208
15209 if (c == 0 || c == ' ')
15210 regname = '"';
15211 else
15212 regname = c;
15213 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000015214 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015215 set_vim_var_string(VV_REG, &regname, 1);
15216}
15217
15218/*
15219 * Get or set v:exception. If "oldval" == NULL, return the current value.
15220 * Otherwise, restore the value to "oldval" and return NULL.
15221 * Must always be called in pairs to save and restore v:exception! Does not
15222 * take care of memory allocations.
15223 */
15224 char_u *
15225v_exception(oldval)
15226 char_u *oldval;
15227{
15228 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015229 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015230
Bram Moolenaare9a41262005-01-15 22:18:47 +000015231 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015232 return NULL;
15233}
15234
15235/*
15236 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
15237 * Otherwise, restore the value to "oldval" and return NULL.
15238 * Must always be called in pairs to save and restore v:throwpoint! Does not
15239 * take care of memory allocations.
15240 */
15241 char_u *
15242v_throwpoint(oldval)
15243 char_u *oldval;
15244{
15245 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015246 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015247
Bram Moolenaare9a41262005-01-15 22:18:47 +000015248 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015249 return NULL;
15250}
15251
15252#if defined(FEAT_AUTOCMD) || defined(PROTO)
15253/*
15254 * Set v:cmdarg.
15255 * If "eap" != NULL, use "eap" to generate the value and return the old value.
15256 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
15257 * Must always be called in pairs!
15258 */
15259 char_u *
15260set_cmdarg(eap, oldarg)
15261 exarg_T *eap;
15262 char_u *oldarg;
15263{
15264 char_u *oldval;
15265 char_u *newval;
15266 unsigned len;
15267
Bram Moolenaare9a41262005-01-15 22:18:47 +000015268 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015269 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015270 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015271 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000015272 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015273 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015274 }
15275
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015276 if (eap->force_bin == FORCE_BIN)
15277 len = 6;
15278 else if (eap->force_bin == FORCE_NOBIN)
15279 len = 8;
15280 else
15281 len = 0;
15282 if (eap->force_ff != 0)
15283 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
15284# ifdef FEAT_MBYTE
15285 if (eap->force_enc != 0)
15286 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
15287# endif
15288
15289 newval = alloc(len + 1);
15290 if (newval == NULL)
15291 return NULL;
15292
15293 if (eap->force_bin == FORCE_BIN)
15294 sprintf((char *)newval, " ++bin");
15295 else if (eap->force_bin == FORCE_NOBIN)
15296 sprintf((char *)newval, " ++nobin");
15297 else
15298 *newval = NUL;
15299 if (eap->force_ff != 0)
15300 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
15301 eap->cmd + eap->force_ff);
15302# ifdef FEAT_MBYTE
15303 if (eap->force_enc != 0)
15304 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
15305 eap->cmd + eap->force_enc);
15306# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000015307 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015308 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015309}
15310#endif
15311
15312/*
15313 * Get the value of internal variable "name".
15314 * Return OK or FAIL.
15315 */
15316 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015317get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015318 char_u *name;
15319 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000015320 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015321 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015322{
15323 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000015324 typval_T *tv = NULL;
15325 typval_T atv;
15326 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015327 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015328
15329 /* truncate the name, so that we can use strcmp() */
15330 cc = name[len];
15331 name[len] = NUL;
15332
15333 /*
15334 * Check for "b:changedtick".
15335 */
15336 if (STRCMP(name, "b:changedtick") == 0)
15337 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000015338 atv.v_type = VAR_NUMBER;
15339 atv.vval.v_number = curbuf->b_changedtick;
15340 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015341 }
15342
15343 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015344 * Check for user-defined variables.
15345 */
15346 else
15347 {
Bram Moolenaara7043832005-01-21 11:56:39 +000015348 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015349 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000015350 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015351 }
15352
Bram Moolenaare9a41262005-01-15 22:18:47 +000015353 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015354 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015355 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015356 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015357 ret = FAIL;
15358 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015359 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015360 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015361
15362 name[len] = cc;
15363
15364 return ret;
15365}
15366
15367/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015368 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
15369 * Also handle function call with Funcref variable: func(expr)
15370 * Can all be combined: dict.func(expr)[idx]['func'](expr)
15371 */
15372 static int
15373handle_subscript(arg, rettv, evaluate, verbose)
15374 char_u **arg;
15375 typval_T *rettv;
15376 int evaluate; /* do more than finding the end */
15377 int verbose; /* give error messages */
15378{
15379 int ret = OK;
15380 dict_T *selfdict = NULL;
15381 char_u *s;
15382 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000015383 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015384
15385 while (ret == OK
15386 && (**arg == '['
15387 || (**arg == '.' && rettv->v_type == VAR_DICT)
15388 || (**arg == '(' && rettv->v_type == VAR_FUNC))
15389 && !vim_iswhite(*(*arg - 1)))
15390 {
15391 if (**arg == '(')
15392 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000015393 /* need to copy the funcref so that we can clear rettv */
15394 functv = *rettv;
15395 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015396
15397 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000015398 s = functv.vval.v_string;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015399 ret = get_func_tv(s, STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000015400 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
15401 &len, evaluate, selfdict);
15402
15403 /* Clear the funcref afterwards, so that deleting it while
15404 * evaluating the arguments is possible (see test55). */
15405 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015406
15407 /* Stop the expression evaluation when immediately aborting on
15408 * error, or when an interrupt occurred or an exception was thrown
15409 * but not caught. */
15410 if (aborting())
15411 {
15412 if (ret == OK)
15413 clear_tv(rettv);
15414 ret = FAIL;
15415 }
15416 dict_unref(selfdict);
15417 selfdict = NULL;
15418 }
15419 else /* **arg == '[' || **arg == '.' */
15420 {
15421 dict_unref(selfdict);
15422 if (rettv->v_type == VAR_DICT)
15423 {
15424 selfdict = rettv->vval.v_dict;
15425 if (selfdict != NULL)
15426 ++selfdict->dv_refcount;
15427 }
15428 else
15429 selfdict = NULL;
15430 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
15431 {
15432 clear_tv(rettv);
15433 ret = FAIL;
15434 }
15435 }
15436 }
15437 dict_unref(selfdict);
15438 return ret;
15439}
15440
15441/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015442 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
15443 * value).
15444 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015445 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015446alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015447{
Bram Moolenaar33570922005-01-25 22:26:29 +000015448 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015449}
15450
15451/*
15452 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015453 * The string "s" must have been allocated, it is consumed.
15454 * Return NULL for out of memory, the variable otherwise.
15455 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015456 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015457alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015458 char_u *s;
15459{
Bram Moolenaar33570922005-01-25 22:26:29 +000015460 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015461
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015462 rettv = alloc_tv();
15463 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015464 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015465 rettv->v_type = VAR_STRING;
15466 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015467 }
15468 else
15469 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015470 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015471}
15472
15473/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015474 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015475 */
15476 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015477free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015478 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015479{
15480 if (varp != NULL)
15481 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015482 switch (varp->v_type)
15483 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015484 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015485 func_unref(varp->vval.v_string);
15486 /*FALLTHROUGH*/
15487 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015488 vim_free(varp->vval.v_string);
15489 break;
15490 case VAR_LIST:
15491 list_unref(varp->vval.v_list);
15492 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015493 case VAR_DICT:
15494 dict_unref(varp->vval.v_dict);
15495 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015496 case VAR_NUMBER:
15497 case VAR_UNKNOWN:
15498 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015499 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000015500 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015501 break;
15502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015503 vim_free(varp);
15504 }
15505}
15506
15507/*
15508 * Free the memory for a variable value and set the value to NULL or 0.
15509 */
15510 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015511clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015512 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015513{
15514 if (varp != NULL)
15515 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015516 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015517 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015518 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015519 func_unref(varp->vval.v_string);
15520 /*FALLTHROUGH*/
15521 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015522 vim_free(varp->vval.v_string);
15523 varp->vval.v_string = NULL;
15524 break;
15525 case VAR_LIST:
15526 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015527 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015528 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015529 case VAR_DICT:
15530 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015531 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015532 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015533 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015534 varp->vval.v_number = 0;
15535 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015536 case VAR_UNKNOWN:
15537 break;
15538 default:
15539 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000015540 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015541 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015542 }
15543}
15544
15545/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015546 * Set the value of a variable to NULL without freeing items.
15547 */
15548 static void
15549init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015550 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015551{
15552 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000015553 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015554}
15555
15556/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015557 * Get the number value of a variable.
15558 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015559 * For incompatible types, return 0.
15560 * get_tv_number_chk() is similar to get_tv_number(), but informs the
15561 * caller of incompatible types: it sets *denote to TRUE if "denote"
15562 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015563 */
15564 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015565get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015566 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015567{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015568 int error = FALSE;
15569
15570 return get_tv_number_chk(varp, &error); /* return 0L on error */
15571}
15572
15573 static long
15574get_tv_number_chk(varp, denote)
15575 typval_T *varp;
15576 int *denote;
15577{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015578 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015579
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015580 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015581 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015582 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015583 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015584 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015585 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015586 break;
15587 case VAR_STRING:
15588 if (varp->vval.v_string != NULL)
15589 vim_str2nr(varp->vval.v_string, NULL, NULL,
15590 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015591 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015592 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000015593 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015594 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015595 case VAR_DICT:
15596 EMSG(_("E728: Using a Dictionary as a number"));
15597 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015598 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015599 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015600 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015601 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015602 if (denote == NULL) /* useful for values that must be unsigned */
15603 n = -1;
15604 else
15605 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015606 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015607}
15608
15609/*
15610 * Get the lnum from the first argument. Also accepts ".", "$", etc.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015611 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015612 */
15613 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015614get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000015615 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015616{
Bram Moolenaar33570922005-01-25 22:26:29 +000015617 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015618 linenr_T lnum;
15619
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015620 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015621 if (lnum == 0) /* no valid number, try using line() */
15622 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015623 rettv.v_type = VAR_NUMBER;
15624 f_line(argvars, &rettv);
15625 lnum = rettv.vval.v_number;
15626 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015627 }
15628 return lnum;
15629}
15630
15631/*
15632 * Get the string value of a variable.
15633 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000015634 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
15635 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015636 * If the String variable has never been set, return an empty string.
15637 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015638 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
15639 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015640 */
15641 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015642get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015643 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015644{
15645 static char_u mybuf[NUMBUFLEN];
15646
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015647 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015648}
15649
15650 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015651get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000015652 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015653 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015654{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015655 char_u *res = get_tv_string_buf_chk(varp, buf);
15656
15657 return res != NULL ? res : (char_u *)"";
15658}
15659
15660 static char_u *
15661get_tv_string_chk(varp)
15662 typval_T *varp;
15663{
15664 static char_u mybuf[NUMBUFLEN];
15665
15666 return get_tv_string_buf_chk(varp, mybuf);
15667}
15668
15669 static char_u *
15670get_tv_string_buf_chk(varp, buf)
15671 typval_T *varp;
15672 char_u *buf;
15673{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015674 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015675 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015676 case VAR_NUMBER:
15677 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
15678 return buf;
15679 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015680 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015681 break;
15682 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015683 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015684 break;
15685 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015686 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015687 break;
15688 case VAR_STRING:
15689 if (varp->vval.v_string != NULL)
15690 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015691 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015692 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015693 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015694 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015695 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015696 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015697}
15698
15699/*
15700 * Find variable "name" in the list of variables.
15701 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015702 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000015703 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000015704 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015705 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015706 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000015707find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015708 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000015709 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015710{
Bram Moolenaar071d4272004-06-13 20:20:40 +000015711 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015712 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015713
Bram Moolenaara7043832005-01-21 11:56:39 +000015714 ht = find_var_ht(name, &varname);
15715 if (htp != NULL)
15716 *htp = ht;
15717 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015718 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015719 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015720}
15721
15722/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015723 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000015724 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015725 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015726 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015727find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000015728 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000015729 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015730 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000015731{
Bram Moolenaar33570922005-01-25 22:26:29 +000015732 hashitem_T *hi;
15733
15734 if (*varname == NUL)
15735 {
15736 /* Must be something like "s:", otherwise "ht" would be NULL. */
15737 switch (varname[-2])
15738 {
15739 case 's': return &SCRIPT_SV(current_SID).sv_var;
15740 case 'g': return &globvars_var;
15741 case 'v': return &vimvars_var;
15742 case 'b': return &curbuf->b_bufvar;
15743 case 'w': return &curwin->w_winvar;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000015744 case 'l': return current_funccal == NULL
15745 ? NULL : &current_funccal->l_vars_var;
15746 case 'a': return current_funccal == NULL
15747 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000015748 }
15749 return NULL;
15750 }
Bram Moolenaara7043832005-01-21 11:56:39 +000015751
15752 hi = hash_find(ht, varname);
15753 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015754 {
15755 /* For global variables we may try auto-loading the script. If it
15756 * worked find the variable again. */
15757 if (ht == &globvarht && !writing
15758 && script_autoload(varname) && !aborting())
15759 hi = hash_find(ht, varname);
15760 if (HASHITEM_EMPTY(hi))
15761 return NULL;
15762 }
Bram Moolenaar33570922005-01-25 22:26:29 +000015763 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000015764}
15765
15766/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015767 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000015768 * Set "varname" to the start of name without ':'.
15769 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015770 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000015771find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015772 char_u *name;
15773 char_u **varname;
15774{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000015775 hashitem_T *hi;
15776
Bram Moolenaar071d4272004-06-13 20:20:40 +000015777 if (name[1] != ':')
15778 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015779 /* The name must not start with a colon or #. */
15780 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015781 return NULL;
15782 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015783
15784 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000015785 hi = hash_find(&compat_hashtab, name);
15786 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000015787 return &compat_hashtab;
15788
Bram Moolenaar071d4272004-06-13 20:20:40 +000015789 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000015790 return &globvarht; /* global variable */
15791 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015792 }
15793 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015794 if (*name == 'g') /* global variable */
15795 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015796 /* There must be no ':' or '#' in the rest of the name, unless g: is used
15797 */
15798 if (vim_strchr(name + 2, ':') != NULL
15799 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015800 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015801 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000015802 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015803 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000015804 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000015805 if (*name == 'v') /* v: variable */
15806 return &vimvarht;
15807 if (*name == 'a' && current_funccal != NULL) /* function argument */
15808 return &current_funccal->l_avars.dv_hashtab;
15809 if (*name == 'l' && current_funccal != NULL) /* local function variable */
15810 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015811 if (*name == 's' /* script variable */
15812 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
15813 return &SCRIPT_VARS(current_SID);
15814 return NULL;
15815}
15816
15817/*
15818 * Get the string value of a (global/local) variable.
15819 * Returns NULL when it doesn't exist.
15820 */
15821 char_u *
15822get_var_value(name)
15823 char_u *name;
15824{
Bram Moolenaar33570922005-01-25 22:26:29 +000015825 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015826
Bram Moolenaara7043832005-01-21 11:56:39 +000015827 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015828 if (v == NULL)
15829 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000015830 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015831}
15832
15833/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015834 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000015835 * sourcing this script and when executing functions defined in the script.
15836 */
15837 void
15838new_script_vars(id)
15839 scid_T id;
15840{
Bram Moolenaara7043832005-01-21 11:56:39 +000015841 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000015842 hashtab_T *ht;
15843 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000015844
Bram Moolenaar071d4272004-06-13 20:20:40 +000015845 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
15846 {
Bram Moolenaara7043832005-01-21 11:56:39 +000015847 /* Re-allocating ga_data means that an ht_array pointing to
15848 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000015849 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000015850 for (i = 1; i <= ga_scripts.ga_len; ++i)
15851 {
15852 ht = &SCRIPT_VARS(i);
15853 if (ht->ht_mask == HT_INIT_SIZE - 1)
15854 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000015855 sv = &SCRIPT_SV(i);
15856 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000015857 }
15858
Bram Moolenaar071d4272004-06-13 20:20:40 +000015859 while (ga_scripts.ga_len < id)
15860 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015861 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
15862 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015863 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015864 }
15865 }
15866}
15867
15868/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015869 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
15870 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015871 */
15872 void
Bram Moolenaar33570922005-01-25 22:26:29 +000015873init_var_dict(dict, dict_var)
15874 dict_T *dict;
15875 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015876{
Bram Moolenaar33570922005-01-25 22:26:29 +000015877 hash_init(&dict->dv_hashtab);
15878 dict->dv_refcount = 99999;
15879 dict_var->di_tv.vval.v_dict = dict;
15880 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015881 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015882 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
15883 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015884}
15885
15886/*
15887 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000015888 * Frees all allocated variables and the value they contain.
15889 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015890 */
15891 void
Bram Moolenaara7043832005-01-21 11:56:39 +000015892vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000015893 hashtab_T *ht;
15894{
15895 vars_clear_ext(ht, TRUE);
15896}
15897
15898/*
15899 * Like vars_clear(), but only free the value if "free_val" is TRUE.
15900 */
15901 static void
15902vars_clear_ext(ht, free_val)
15903 hashtab_T *ht;
15904 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015905{
Bram Moolenaara7043832005-01-21 11:56:39 +000015906 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000015907 hashitem_T *hi;
15908 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015909
Bram Moolenaar33570922005-01-25 22:26:29 +000015910 hash_lock(ht);
Bram Moolenaara7043832005-01-21 11:56:39 +000015911 todo = ht->ht_used;
15912 for (hi = ht->ht_array; todo > 0; ++hi)
15913 {
15914 if (!HASHITEM_EMPTY(hi))
15915 {
15916 --todo;
15917
Bram Moolenaar33570922005-01-25 22:26:29 +000015918 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000015919 * ht_array might change then. hash_clear() takes care of it
15920 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015921 v = HI2DI(hi);
15922 if (free_val)
15923 clear_tv(&v->di_tv);
15924 if ((v->di_flags & DI_FLAGS_FIX) == 0)
15925 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000015926 }
15927 }
15928 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000015929 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015930}
15931
Bram Moolenaara7043832005-01-21 11:56:39 +000015932/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015933 * Delete a variable from hashtab "ht" at item "hi".
15934 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000015935 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015936 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000015937delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000015938 hashtab_T *ht;
15939 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015940{
Bram Moolenaar33570922005-01-25 22:26:29 +000015941 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000015942
15943 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000015944 clear_tv(&di->di_tv);
15945 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015946}
15947
15948/*
15949 * List the value of one internal variable.
15950 */
15951 static void
15952list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000015953 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015954 char_u *prefix;
15955{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015956 char_u *tofree;
15957 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015958 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015959
Bram Moolenaar33570922005-01-25 22:26:29 +000015960 s = echo_string(&v->di_tv, &tofree, numbuf);
15961 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015962 s == NULL ? (char_u *)"" : s);
15963 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015964}
15965
Bram Moolenaar071d4272004-06-13 20:20:40 +000015966 static void
15967list_one_var_a(prefix, name, type, string)
15968 char_u *prefix;
15969 char_u *name;
15970 int type;
15971 char_u *string;
15972{
15973 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
15974 if (name != NULL) /* "a:" vars don't have a name stored */
15975 msg_puts(name);
15976 msg_putchar(' ');
15977 msg_advance(22);
15978 if (type == VAR_NUMBER)
15979 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015980 else if (type == VAR_FUNC)
15981 msg_putchar('*');
15982 else if (type == VAR_LIST)
15983 {
15984 msg_putchar('[');
15985 if (*string == '[')
15986 ++string;
15987 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015988 else if (type == VAR_DICT)
15989 {
15990 msg_putchar('{');
15991 if (*string == '{')
15992 ++string;
15993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015994 else
15995 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015996
Bram Moolenaar071d4272004-06-13 20:20:40 +000015997 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015998
15999 if (type == VAR_FUNC)
16000 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016001}
16002
16003/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016004 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000016005 * If the variable already exists, the value is updated.
16006 * Otherwise the variable is created.
16007 */
16008 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016009set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016010 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000016011 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016012 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016013{
Bram Moolenaar33570922005-01-25 22:26:29 +000016014 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016015 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016016 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000016017 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016018
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016019 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016020 {
16021 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
16022 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
16023 ? name[2] : name[0]))
16024 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016025 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016026 return;
16027 }
16028 if (function_exists(name))
16029 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016030 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016031 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016032 return;
16033 }
16034 }
16035
Bram Moolenaara7043832005-01-21 11:56:39 +000016036 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000016037 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000016038 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000016039 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000016040 return;
16041 }
16042
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016043 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016044 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016045 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016046 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016047 if (var_check_ro(v->di_flags, name)
16048 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000016049 return;
16050 if (v->di_tv.v_type != tv->v_type
16051 && !((v->di_tv.v_type == VAR_STRING
16052 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016053 && (tv->v_type == VAR_STRING
16054 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016055 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016056 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016057 return;
16058 }
Bram Moolenaar33570922005-01-25 22:26:29 +000016059
16060 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000016061 * Handle setting internal v: variables separately: we don't change
16062 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000016063 */
16064 if (ht == &vimvarht)
16065 {
16066 if (v->di_tv.v_type == VAR_STRING)
16067 {
16068 vim_free(v->di_tv.vval.v_string);
16069 if (copy || tv->v_type != VAR_STRING)
16070 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
16071 else
16072 {
16073 /* Take over the string to avoid an extra alloc/free. */
16074 v->di_tv.vval.v_string = tv->vval.v_string;
16075 tv->vval.v_string = NULL;
16076 }
16077 }
16078 else if (v->di_tv.v_type != VAR_NUMBER)
16079 EMSG2(_(e_intern2), "set_var()");
16080 else
16081 v->di_tv.vval.v_number = get_tv_number(tv);
16082 return;
16083 }
16084
16085 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016086 }
16087 else /* add a new variable */
16088 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000016089 /* Make sure the variable name is valid. */
16090 for (p = varname; *p != NUL; ++p)
16091 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p)))
16092 {
16093 EMSG2(_(e_illvar), varname);
16094 return;
16095 }
16096
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016097 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
16098 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000016099 if (v == NULL)
16100 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000016101 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000016102 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016103 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016104 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016105 return;
16106 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016107 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016108 }
Bram Moolenaara7043832005-01-21 11:56:39 +000016109
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016110 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000016111 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016112 else
16113 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016114 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016115 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016116 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016118}
16119
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016120/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016121 * Return TRUE if di_flags "flags" indicate read-only variable "name".
16122 * Also give an error message.
16123 */
16124 static int
16125var_check_ro(flags, name)
16126 int flags;
16127 char_u *name;
16128{
16129 if (flags & DI_FLAGS_RO)
16130 {
16131 EMSG2(_(e_readonlyvar), name);
16132 return TRUE;
16133 }
16134 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
16135 {
16136 EMSG2(_(e_readonlysbx), name);
16137 return TRUE;
16138 }
16139 return FALSE;
16140}
16141
16142/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016143 * Return TRUE if typeval "tv" is set to be locked (immutable).
16144 * Also give an error message, using "name".
16145 */
16146 static int
16147tv_check_lock(lock, name)
16148 int lock;
16149 char_u *name;
16150{
16151 if (lock & VAR_LOCKED)
16152 {
16153 EMSG2(_("E741: Value is locked: %s"),
16154 name == NULL ? (char_u *)_("Unknown") : name);
16155 return TRUE;
16156 }
16157 if (lock & VAR_FIXED)
16158 {
16159 EMSG2(_("E742: Cannot change value of %s"),
16160 name == NULL ? (char_u *)_("Unknown") : name);
16161 return TRUE;
16162 }
16163 return FALSE;
16164}
16165
16166/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016167 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016168 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016169 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016170 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016171 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016172copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000016173 typval_T *from;
16174 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016175{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016176 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016177 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016178 switch (from->v_type)
16179 {
16180 case VAR_NUMBER:
16181 to->vval.v_number = from->vval.v_number;
16182 break;
16183 case VAR_STRING:
16184 case VAR_FUNC:
16185 if (from->vval.v_string == NULL)
16186 to->vval.v_string = NULL;
16187 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016188 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016189 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016190 if (from->v_type == VAR_FUNC)
16191 func_ref(to->vval.v_string);
16192 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016193 break;
16194 case VAR_LIST:
16195 if (from->vval.v_list == NULL)
16196 to->vval.v_list = NULL;
16197 else
16198 {
16199 to->vval.v_list = from->vval.v_list;
16200 ++to->vval.v_list->lv_refcount;
16201 }
16202 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016203 case VAR_DICT:
16204 if (from->vval.v_dict == NULL)
16205 to->vval.v_dict = NULL;
16206 else
16207 {
16208 to->vval.v_dict = from->vval.v_dict;
16209 ++to->vval.v_dict->dv_refcount;
16210 }
16211 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016212 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016213 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016214 break;
16215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016216}
16217
16218/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000016219 * Make a copy of an item.
16220 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016221 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
16222 * reference to an already copied list/dict can be used.
16223 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016224 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016225 static int
16226item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000016227 typval_T *from;
16228 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016229 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016230 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016231{
16232 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016233 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016234
Bram Moolenaar33570922005-01-25 22:26:29 +000016235 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016236 {
16237 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016238 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016239 }
16240 ++recurse;
16241
16242 switch (from->v_type)
16243 {
16244 case VAR_NUMBER:
16245 case VAR_STRING:
16246 case VAR_FUNC:
16247 copy_tv(from, to);
16248 break;
16249 case VAR_LIST:
16250 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016251 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016252 if (from->vval.v_list == NULL)
16253 to->vval.v_list = NULL;
16254 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
16255 {
16256 /* use the copy made earlier */
16257 to->vval.v_list = from->vval.v_list->lv_copylist;
16258 ++to->vval.v_list->lv_refcount;
16259 }
16260 else
16261 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
16262 if (to->vval.v_list == NULL)
16263 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016264 break;
16265 case VAR_DICT:
16266 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016267 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016268 if (from->vval.v_dict == NULL)
16269 to->vval.v_dict = NULL;
16270 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
16271 {
16272 /* use the copy made earlier */
16273 to->vval.v_dict = from->vval.v_dict->dv_copydict;
16274 ++to->vval.v_dict->dv_refcount;
16275 }
16276 else
16277 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
16278 if (to->vval.v_dict == NULL)
16279 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016280 break;
16281 default:
16282 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016283 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016284 }
16285 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016286 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016287}
16288
16289/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016290 * ":echo expr1 ..." print each argument separated with a space, add a
16291 * newline at the end.
16292 * ":echon expr1 ..." print each argument plain.
16293 */
16294 void
16295ex_echo(eap)
16296 exarg_T *eap;
16297{
16298 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000016299 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016300 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016301 char_u *p;
16302 int needclr = TRUE;
16303 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016304 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016305
16306 if (eap->skip)
16307 ++emsg_skip;
16308 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
16309 {
16310 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016311 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016312 {
16313 /*
16314 * Report the invalid expression unless the expression evaluation
16315 * has been cancelled due to an aborting error, an interrupt, or an
16316 * exception.
16317 */
16318 if (!aborting())
16319 EMSG2(_(e_invexpr2), p);
16320 break;
16321 }
16322 if (!eap->skip)
16323 {
16324 if (atstart)
16325 {
16326 atstart = FALSE;
16327 /* Call msg_start() after eval1(), evaluating the expression
16328 * may cause a message to appear. */
16329 if (eap->cmdidx == CMD_echo)
16330 msg_start();
16331 }
16332 else if (eap->cmdidx == CMD_echo)
16333 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016334 p = echo_string(&rettv, &tofree, numbuf);
16335 if (p != NULL)
16336 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016337 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016338 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016339 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016340 if (*p != TAB && needclr)
16341 {
16342 /* remove any text still there from the command */
16343 msg_clr_eos();
16344 needclr = FALSE;
16345 }
16346 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016347 }
16348 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016349 {
16350#ifdef FEAT_MBYTE
16351 if (has_mbyte)
16352 {
16353 int i = (*mb_ptr2len_check)(p);
16354
16355 (void)msg_outtrans_len_attr(p, i, echo_attr);
16356 p += i - 1;
16357 }
16358 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000016359#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016360 (void)msg_outtrans_len_attr(p, 1, echo_attr);
16361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016362 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016363 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016364 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016365 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016366 arg = skipwhite(arg);
16367 }
16368 eap->nextcmd = check_nextcmd(arg);
16369
16370 if (eap->skip)
16371 --emsg_skip;
16372 else
16373 {
16374 /* remove text that may still be there from the command */
16375 if (needclr)
16376 msg_clr_eos();
16377 if (eap->cmdidx == CMD_echo)
16378 msg_end();
16379 }
16380}
16381
16382/*
16383 * ":echohl {name}".
16384 */
16385 void
16386ex_echohl(eap)
16387 exarg_T *eap;
16388{
16389 int id;
16390
16391 id = syn_name2id(eap->arg);
16392 if (id == 0)
16393 echo_attr = 0;
16394 else
16395 echo_attr = syn_id2attr(id);
16396}
16397
16398/*
16399 * ":execute expr1 ..." execute the result of an expression.
16400 * ":echomsg expr1 ..." Print a message
16401 * ":echoerr expr1 ..." Print an error
16402 * Each gets spaces around each argument and a newline at the end for
16403 * echo commands
16404 */
16405 void
16406ex_execute(eap)
16407 exarg_T *eap;
16408{
16409 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000016410 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016411 int ret = OK;
16412 char_u *p;
16413 garray_T ga;
16414 int len;
16415 int save_did_emsg;
16416
16417 ga_init2(&ga, 1, 80);
16418
16419 if (eap->skip)
16420 ++emsg_skip;
16421 while (*arg != NUL && *arg != '|' && *arg != '\n')
16422 {
16423 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016424 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016425 {
16426 /*
16427 * Report the invalid expression unless the expression evaluation
16428 * has been cancelled due to an aborting error, an interrupt, or an
16429 * exception.
16430 */
16431 if (!aborting())
16432 EMSG2(_(e_invexpr2), p);
16433 ret = FAIL;
16434 break;
16435 }
16436
16437 if (!eap->skip)
16438 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016439 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016440 len = (int)STRLEN(p);
16441 if (ga_grow(&ga, len + 2) == FAIL)
16442 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016443 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016444 ret = FAIL;
16445 break;
16446 }
16447 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016448 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016449 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016450 ga.ga_len += len;
16451 }
16452
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016453 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016454 arg = skipwhite(arg);
16455 }
16456
16457 if (ret != FAIL && ga.ga_data != NULL)
16458 {
16459 if (eap->cmdidx == CMD_echomsg)
16460 MSG_ATTR(ga.ga_data, echo_attr);
16461 else if (eap->cmdidx == CMD_echoerr)
16462 {
16463 /* We don't want to abort following commands, restore did_emsg. */
16464 save_did_emsg = did_emsg;
16465 EMSG((char_u *)ga.ga_data);
16466 if (!force_abort)
16467 did_emsg = save_did_emsg;
16468 }
16469 else if (eap->cmdidx == CMD_execute)
16470 do_cmdline((char_u *)ga.ga_data,
16471 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
16472 }
16473
16474 ga_clear(&ga);
16475
16476 if (eap->skip)
16477 --emsg_skip;
16478
16479 eap->nextcmd = check_nextcmd(arg);
16480}
16481
16482/*
16483 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
16484 * "arg" points to the "&" or '+' when called, to "option" when returning.
16485 * Returns NULL when no option name found. Otherwise pointer to the char
16486 * after the option name.
16487 */
16488 static char_u *
16489find_option_end(arg, opt_flags)
16490 char_u **arg;
16491 int *opt_flags;
16492{
16493 char_u *p = *arg;
16494
16495 ++p;
16496 if (*p == 'g' && p[1] == ':')
16497 {
16498 *opt_flags = OPT_GLOBAL;
16499 p += 2;
16500 }
16501 else if (*p == 'l' && p[1] == ':')
16502 {
16503 *opt_flags = OPT_LOCAL;
16504 p += 2;
16505 }
16506 else
16507 *opt_flags = 0;
16508
16509 if (!ASCII_ISALPHA(*p))
16510 return NULL;
16511 *arg = p;
16512
16513 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
16514 p += 4; /* termcap option */
16515 else
16516 while (ASCII_ISALPHA(*p))
16517 ++p;
16518 return p;
16519}
16520
16521/*
16522 * ":function"
16523 */
16524 void
16525ex_function(eap)
16526 exarg_T *eap;
16527{
16528 char_u *theline;
16529 int j;
16530 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016531 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016532 char_u *name = NULL;
16533 char_u *p;
16534 char_u *arg;
16535 garray_T newargs;
16536 garray_T newlines;
16537 int varargs = FALSE;
16538 int mustend = FALSE;
16539 int flags = 0;
16540 ufunc_T *fp;
16541 int indent;
16542 int nesting;
16543 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000016544 dictitem_T *v;
16545 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016546 static int func_nr = 0; /* number for nameless function */
16547 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016548 hashtab_T *ht;
16549 int todo;
16550 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016551
16552 /*
16553 * ":function" without argument: list functions.
16554 */
16555 if (ends_excmd(*eap->arg))
16556 {
16557 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016558 {
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000016559 todo = func_hashtab.ht_used;
16560 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016561 {
16562 if (!HASHITEM_EMPTY(hi))
16563 {
16564 --todo;
16565 fp = HI2UF(hi);
16566 if (!isdigit(*fp->uf_name))
16567 list_func_head(fp, FALSE);
16568 }
16569 }
16570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016571 eap->nextcmd = check_nextcmd(eap->arg);
16572 return;
16573 }
16574
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016575 /*
16576 * Get the function name. There are these situations:
16577 * func normal function name
16578 * "name" == func, "fudi.fd_dict" == NULL
16579 * dict.func new dictionary entry
16580 * "name" == NULL, "fudi.fd_dict" set,
16581 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
16582 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016583 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016584 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
16585 * dict.func existing dict entry that's not a Funcref
16586 * "name" == NULL, "fudi.fd_dict" set,
16587 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
16588 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016589 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016590 name = trans_function_name(&p, eap->skip, 0, &fudi);
16591 paren = (vim_strchr(p, '(') != NULL);
16592 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016593 {
16594 /*
16595 * Return on an invalid expression in braces, unless the expression
16596 * evaluation has been cancelled due to an aborting error, an
16597 * interrupt, or an exception.
16598 */
16599 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016600 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016601 if (!eap->skip && fudi.fd_newkey != NULL)
16602 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016603 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016604 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016606 else
16607 eap->skip = TRUE;
16608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016609 /* An error in a function call during evaluation of an expression in magic
16610 * braces should not cause the function not to be defined. */
16611 saved_did_emsg = did_emsg;
16612 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016613
16614 /*
16615 * ":function func" with only function name: list function.
16616 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016617 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016618 {
16619 if (!ends_excmd(*skipwhite(p)))
16620 {
16621 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016622 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016623 }
16624 eap->nextcmd = check_nextcmd(p);
16625 if (eap->nextcmd != NULL)
16626 *p = NUL;
16627 if (!eap->skip && !got_int)
16628 {
16629 fp = find_func(name);
16630 if (fp != NULL)
16631 {
16632 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016633 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016634 {
16635 msg_putchar('\n');
16636 msg_outnum((long)(j + 1));
16637 if (j < 9)
16638 msg_putchar(' ');
16639 if (j < 99)
16640 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016641 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016642 out_flush(); /* show a line at a time */
16643 ui_breakcheck();
16644 }
16645 if (!got_int)
16646 {
16647 msg_putchar('\n');
16648 msg_puts((char_u *)" endfunction");
16649 }
16650 }
16651 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016652 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016653 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016654 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016655 }
16656
16657 /*
16658 * ":function name(arg1, arg2)" Define function.
16659 */
16660 p = skipwhite(p);
16661 if (*p != '(')
16662 {
16663 if (!eap->skip)
16664 {
16665 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016666 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016667 }
16668 /* attempt to continue by skipping some text */
16669 if (vim_strchr(p, '(') != NULL)
16670 p = vim_strchr(p, '(');
16671 }
16672 p = skipwhite(p + 1);
16673
16674 ga_init2(&newargs, (int)sizeof(char_u *), 3);
16675 ga_init2(&newlines, (int)sizeof(char_u *), 3);
16676
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016677 if (!eap->skip)
16678 {
16679 /* Check the name of the function. */
16680 if (name != NULL)
16681 arg = name;
16682 else
16683 arg = fudi.fd_newkey;
16684 if (arg != NULL)
16685 {
16686 if (*arg == K_SPECIAL)
16687 j = 3;
16688 else
16689 j = 0;
16690 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
16691 : eval_isnamec(arg[j])))
16692 ++j;
16693 if (arg[j] != NUL)
16694 emsg_funcname(_(e_invarg2), arg);
16695 }
16696 }
16697
Bram Moolenaar071d4272004-06-13 20:20:40 +000016698 /*
16699 * Isolate the arguments: "arg1, arg2, ...)"
16700 */
16701 while (*p != ')')
16702 {
16703 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
16704 {
16705 varargs = TRUE;
16706 p += 3;
16707 mustend = TRUE;
16708 }
16709 else
16710 {
16711 arg = p;
16712 while (ASCII_ISALNUM(*p) || *p == '_')
16713 ++p;
16714 if (arg == p || isdigit(*arg)
16715 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
16716 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
16717 {
16718 if (!eap->skip)
16719 EMSG2(_("E125: Illegal argument: %s"), arg);
16720 break;
16721 }
16722 if (ga_grow(&newargs, 1) == FAIL)
16723 goto erret;
16724 c = *p;
16725 *p = NUL;
16726 arg = vim_strsave(arg);
16727 if (arg == NULL)
16728 goto erret;
16729 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
16730 *p = c;
16731 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016732 if (*p == ',')
16733 ++p;
16734 else
16735 mustend = TRUE;
16736 }
16737 p = skipwhite(p);
16738 if (mustend && *p != ')')
16739 {
16740 if (!eap->skip)
16741 EMSG2(_(e_invarg2), eap->arg);
16742 break;
16743 }
16744 }
16745 ++p; /* skip the ')' */
16746
Bram Moolenaare9a41262005-01-15 22:18:47 +000016747 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016748 for (;;)
16749 {
16750 p = skipwhite(p);
16751 if (STRNCMP(p, "range", 5) == 0)
16752 {
16753 flags |= FC_RANGE;
16754 p += 5;
16755 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000016756 else if (STRNCMP(p, "dict", 4) == 0)
16757 {
16758 flags |= FC_DICT;
16759 p += 4;
16760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016761 else if (STRNCMP(p, "abort", 5) == 0)
16762 {
16763 flags |= FC_ABORT;
16764 p += 5;
16765 }
16766 else
16767 break;
16768 }
16769
16770 if (*p != NUL && *p != '"' && *p != '\n' && !eap->skip && !did_emsg)
16771 EMSG(_(e_trailing));
16772
16773 /*
16774 * Read the body of the function, until ":endfunction" is found.
16775 */
16776 if (KeyTyped)
16777 {
16778 /* Check if the function already exists, don't let the user type the
16779 * whole function before telling him it doesn't work! For a script we
16780 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016781 if (!eap->skip && !eap->forceit)
16782 {
16783 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
16784 EMSG(_(e_funcdict));
16785 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016786 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016788
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016789 if (!eap->skip && did_emsg)
16790 goto erret;
16791
Bram Moolenaar071d4272004-06-13 20:20:40 +000016792 msg_putchar('\n'); /* don't overwrite the function name */
16793 cmdline_row = msg_row;
16794 }
16795
16796 indent = 2;
16797 nesting = 0;
16798 for (;;)
16799 {
16800 msg_scroll = TRUE;
16801 need_wait_return = FALSE;
16802 if (eap->getline == NULL)
16803 theline = getcmdline(':', 0L, indent);
16804 else
16805 theline = eap->getline(':', eap->cookie, indent);
16806 if (KeyTyped)
16807 lines_left = Rows - 1;
16808 if (theline == NULL)
16809 {
16810 EMSG(_("E126: Missing :endfunction"));
16811 goto erret;
16812 }
16813
16814 if (skip_until != NULL)
16815 {
16816 /* between ":append" and "." and between ":python <<EOF" and "EOF"
16817 * don't check for ":endfunc". */
16818 if (STRCMP(theline, skip_until) == 0)
16819 {
16820 vim_free(skip_until);
16821 skip_until = NULL;
16822 }
16823 }
16824 else
16825 {
16826 /* skip ':' and blanks*/
16827 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
16828 ;
16829
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016830 /* Check for "endfunction". */
16831 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016832 {
16833 vim_free(theline);
16834 break;
16835 }
16836
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016837 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000016838 * at "end". */
16839 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
16840 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016841 else if (STRNCMP(p, "if", 2) == 0
16842 || STRNCMP(p, "wh", 2) == 0
16843 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000016844 || STRNCMP(p, "try", 3) == 0)
16845 indent += 2;
16846
16847 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016848 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016849 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016850 if (*p == '!')
16851 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016852 p += eval_fname_script(p);
16853 if (ASCII_ISALPHA(*p))
16854 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016855 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016856 if (*skipwhite(p) == '(')
16857 {
16858 ++nesting;
16859 indent += 2;
16860 }
16861 }
16862 }
16863
16864 /* Check for ":append" or ":insert". */
16865 p = skip_range(p, NULL);
16866 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
16867 || (p[0] == 'i'
16868 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
16869 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
16870 skip_until = vim_strsave((char_u *)".");
16871
16872 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
16873 arg = skipwhite(skiptowhite(p));
16874 if (arg[0] == '<' && arg[1] =='<'
16875 && ((p[0] == 'p' && p[1] == 'y'
16876 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
16877 || (p[0] == 'p' && p[1] == 'e'
16878 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
16879 || (p[0] == 't' && p[1] == 'c'
16880 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
16881 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
16882 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000016883 || (p[0] == 'm' && p[1] == 'z'
16884 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016885 ))
16886 {
16887 /* ":python <<" continues until a dot, like ":append" */
16888 p = skipwhite(arg + 2);
16889 if (*p == NUL)
16890 skip_until = vim_strsave((char_u *)".");
16891 else
16892 skip_until = vim_strsave(p);
16893 }
16894 }
16895
16896 /* Add the line to the function. */
16897 if (ga_grow(&newlines, 1) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000016898 {
16899 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016900 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000016901 }
16902
16903 /* Copy the line to newly allocated memory. get_one_sourceline()
16904 * allocates 250 bytes per line, this saves 80% on average. The cost
16905 * is an extra alloc/free. */
16906 p = vim_strsave(theline);
16907 if (p != NULL)
16908 {
16909 vim_free(theline);
16910 theline = p;
16911 }
16912
Bram Moolenaar071d4272004-06-13 20:20:40 +000016913 ((char_u **)(newlines.ga_data))[newlines.ga_len] = theline;
16914 newlines.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016915 }
16916
16917 /* Don't define the function when skipping commands or when an error was
16918 * detected. */
16919 if (eap->skip || did_emsg)
16920 goto erret;
16921
16922 /*
16923 * If there are no errors, add the function
16924 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016925 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016926 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016927 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000016928 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016929 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016930 emsg_funcname("E707: Function name conflicts with variable: %s",
16931 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016932 goto erret;
16933 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016934
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016935 fp = find_func(name);
16936 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016937 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016938 if (!eap->forceit)
16939 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016940 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016941 goto erret;
16942 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016943 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016944 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016945 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016946 name);
16947 goto erret;
16948 }
16949 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016950 ga_clear_strings(&(fp->uf_args));
16951 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016952 vim_free(name);
16953 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016954 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016955 }
16956 else
16957 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016958 char numbuf[20];
16959
16960 fp = NULL;
16961 if (fudi.fd_newkey == NULL && !eap->forceit)
16962 {
16963 EMSG(_(e_funcdict));
16964 goto erret;
16965 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000016966 if (fudi.fd_di == NULL)
16967 {
16968 /* Can't add a function to a locked dictionary */
16969 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
16970 goto erret;
16971 }
16972 /* Can't change an existing function if it is locked */
16973 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
16974 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016975
16976 /* Give the function a sequential number. Can only be used with a
16977 * Funcref! */
16978 vim_free(name);
16979 sprintf(numbuf, "%d", ++func_nr);
16980 name = vim_strsave((char_u *)numbuf);
16981 if (name == NULL)
16982 goto erret;
16983 }
16984
16985 if (fp == NULL)
16986 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016987 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016988 {
16989 int slen, plen;
16990 char_u *scriptname;
16991
16992 /* Check that the autoload name matches the script name. */
16993 j = FAIL;
16994 if (sourcing_name != NULL)
16995 {
16996 scriptname = autoload_name(name);
16997 if (scriptname != NULL)
16998 {
16999 p = vim_strchr(scriptname, '/');
17000 plen = STRLEN(p);
17001 slen = STRLEN(sourcing_name);
17002 if (slen > plen && fnamecmp(p,
17003 sourcing_name + slen - plen) == 0)
17004 j = OK;
17005 vim_free(scriptname);
17006 }
17007 }
17008 if (j == FAIL)
17009 {
17010 EMSG2(_("E746: Function name does not match script file name: %s"), name);
17011 goto erret;
17012 }
17013 }
17014
17015 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017016 if (fp == NULL)
17017 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017018
17019 if (fudi.fd_dict != NULL)
17020 {
17021 if (fudi.fd_di == NULL)
17022 {
17023 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017024 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017025 if (fudi.fd_di == NULL)
17026 {
17027 vim_free(fp);
17028 goto erret;
17029 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017030 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
17031 {
17032 vim_free(fudi.fd_di);
17033 goto erret;
17034 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017035 }
17036 else
17037 /* overwrite existing dict entry */
17038 clear_tv(&fudi.fd_di->di_tv);
17039 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017040 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017041 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017042 fp->uf_refcount = 1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017043 }
17044
Bram Moolenaar071d4272004-06-13 20:20:40 +000017045 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017046 STRCPY(fp->uf_name, name);
17047 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017048 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017049 fp->uf_args = newargs;
17050 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017051#ifdef FEAT_PROFILE
17052 fp->uf_tml_count = NULL;
17053 fp->uf_tml_total = NULL;
17054 fp->uf_tml_self = NULL;
17055 fp->uf_profiling = FALSE;
17056 if (prof_def_func())
17057 func_do_profile(fp);
17058#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017059 fp->uf_varargs = varargs;
17060 fp->uf_flags = flags;
17061 fp->uf_calls = 0;
17062 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017063 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017064
17065erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017066 ga_clear_strings(&newargs);
17067 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017068ret_free:
17069 vim_free(skip_until);
17070 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017071 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017072 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017073}
17074
17075/*
17076 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000017077 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017078 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017079 * flags:
17080 * TFN_INT: internal function name OK
17081 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000017082 * Advances "pp" to just after the function name (if no error).
17083 */
17084 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017085trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017086 char_u **pp;
17087 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017088 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000017089 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017090{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017091 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017092 char_u *start;
17093 char_u *end;
17094 int lead;
17095 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017096 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000017097 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017098
17099 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017100 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017101 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000017102
17103 /* Check for hard coded <SNR>: already translated function ID (from a user
17104 * command). */
17105 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
17106 && (*pp)[2] == (int)KE_SNR)
17107 {
17108 *pp += 3;
17109 len = get_id_len(pp) + 3;
17110 return vim_strnsave(start, len);
17111 }
17112
17113 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
17114 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017115 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000017116 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017117 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017118
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017119 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
17120 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017121 if (end == start)
17122 {
17123 if (!skip)
17124 EMSG(_("E129: Function name required"));
17125 goto theend;
17126 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017127 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017128 {
17129 /*
17130 * Report an invalid expression in braces, unless the expression
17131 * evaluation has been cancelled due to an aborting error, an
17132 * interrupt, or an exception.
17133 */
17134 if (!aborting())
17135 {
17136 if (end != NULL)
17137 EMSG2(_(e_invarg2), start);
17138 }
17139 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017140 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017141 goto theend;
17142 }
17143
17144 if (lv.ll_tv != NULL)
17145 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017146 if (fdp != NULL)
17147 {
17148 fdp->fd_dict = lv.ll_dict;
17149 fdp->fd_newkey = lv.ll_newkey;
17150 lv.ll_newkey = NULL;
17151 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017152 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017153 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
17154 {
17155 name = vim_strsave(lv.ll_tv->vval.v_string);
17156 *pp = end;
17157 }
17158 else
17159 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017160 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
17161 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017162 EMSG(_(e_funcref));
17163 else
17164 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017165 name = NULL;
17166 }
17167 goto theend;
17168 }
17169
17170 if (lv.ll_name == NULL)
17171 {
17172 /* Error found, but continue after the function name. */
17173 *pp = end;
17174 goto theend;
17175 }
17176
17177 if (lv.ll_exp_name != NULL)
17178 len = STRLEN(lv.ll_exp_name);
17179 else
Bram Moolenaara7043832005-01-21 11:56:39 +000017180 {
17181 if (lead == 2) /* skip over "s:" */
17182 lv.ll_name += 2;
17183 len = (int)(end - lv.ll_name);
17184 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017185
17186 /*
17187 * Copy the function name to allocated memory.
17188 * Accept <SID>name() inside a script, translate into <SNR>123_name().
17189 * Accept <SNR>123_name() outside a script.
17190 */
17191 if (skip)
17192 lead = 0; /* do nothing */
17193 else if (lead > 0)
17194 {
17195 lead = 3;
17196 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
17197 {
17198 if (current_SID <= 0)
17199 {
17200 EMSG(_(e_usingsid));
17201 goto theend;
17202 }
17203 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
17204 lead += (int)STRLEN(sid_buf);
17205 }
17206 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017207 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017208 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017209 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017210 goto theend;
17211 }
17212 name = alloc((unsigned)(len + lead + 1));
17213 if (name != NULL)
17214 {
17215 if (lead > 0)
17216 {
17217 name[0] = K_SPECIAL;
17218 name[1] = KS_EXTRA;
17219 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000017220 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017221 STRCPY(name + 3, sid_buf);
17222 }
17223 mch_memmove(name + lead, lv.ll_name, (size_t)len);
17224 name[len + lead] = NUL;
17225 }
17226 *pp = end;
17227
17228theend:
17229 clear_lval(&lv);
17230 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017231}
17232
17233/*
17234 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
17235 * Return 2 if "p" starts with "s:".
17236 * Return 0 otherwise.
17237 */
17238 static int
17239eval_fname_script(p)
17240 char_u *p;
17241{
17242 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
17243 || STRNICMP(p + 1, "SNR>", 4) == 0))
17244 return 5;
17245 if (p[0] == 's' && p[1] == ':')
17246 return 2;
17247 return 0;
17248}
17249
17250/*
17251 * Return TRUE if "p" starts with "<SID>" or "s:".
17252 * Only works if eval_fname_script() returned non-zero for "p"!
17253 */
17254 static int
17255eval_fname_sid(p)
17256 char_u *p;
17257{
17258 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
17259}
17260
17261/*
17262 * List the head of the function: "name(arg1, arg2)".
17263 */
17264 static void
17265list_func_head(fp, indent)
17266 ufunc_T *fp;
17267 int indent;
17268{
17269 int j;
17270
17271 msg_start();
17272 if (indent)
17273 MSG_PUTS(" ");
17274 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017275 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017276 {
17277 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017278 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017279 }
17280 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017281 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017282 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017283 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017284 {
17285 if (j)
17286 MSG_PUTS(", ");
17287 msg_puts(FUNCARG(fp, j));
17288 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017289 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017290 {
17291 if (j)
17292 MSG_PUTS(", ");
17293 MSG_PUTS("...");
17294 }
17295 msg_putchar(')');
17296}
17297
17298/*
17299 * Find a function by name, return pointer to it in ufuncs.
17300 * Return NULL for unknown function.
17301 */
17302 static ufunc_T *
17303find_func(name)
17304 char_u *name;
17305{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017306 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017307
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017308 hi = hash_find(&func_hashtab, name);
17309 if (!HASHITEM_EMPTY(hi))
17310 return HI2UF(hi);
17311 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017312}
17313
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017314#if defined(EXITFREE) || defined(PROTO)
17315 void
17316free_all_functions()
17317{
17318 hashitem_T *hi;
17319
17320 /* Need to start all over every time, because func_free() may change the
17321 * hash table. */
17322 while (func_hashtab.ht_used > 0)
17323 for (hi = func_hashtab.ht_array; ; ++hi)
17324 if (!HASHITEM_EMPTY(hi))
17325 {
17326 func_free(HI2UF(hi));
17327 break;
17328 }
17329}
17330#endif
17331
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017332/*
17333 * Return TRUE if a function "name" exists.
17334 */
17335 static int
17336function_exists(name)
17337 char_u *name;
17338{
17339 char_u *p = name;
17340 int n = FALSE;
17341
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017342 p = trans_function_name(&p, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017343 if (p != NULL)
17344 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017345 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017346 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017347 else
17348 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017349 vim_free(p);
17350 }
17351 return n;
17352}
17353
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017354/*
17355 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017356 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017357 */
17358 static int
17359builtin_function(name)
17360 char_u *name;
17361{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017362 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
17363 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017364}
17365
Bram Moolenaar05159a02005-02-26 23:04:13 +000017366#if defined(FEAT_PROFILE) || defined(PROTO)
17367/*
17368 * Start profiling function "fp".
17369 */
17370 static void
17371func_do_profile(fp)
17372 ufunc_T *fp;
17373{
17374 fp->uf_tm_count = 0;
17375 profile_zero(&fp->uf_tm_self);
17376 profile_zero(&fp->uf_tm_total);
17377 if (fp->uf_tml_count == NULL)
17378 fp->uf_tml_count = (int *)alloc_clear((unsigned)
17379 (sizeof(int) * fp->uf_lines.ga_len));
17380 if (fp->uf_tml_total == NULL)
17381 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
17382 (sizeof(proftime_T) * fp->uf_lines.ga_len));
17383 if (fp->uf_tml_self == NULL)
17384 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
17385 (sizeof(proftime_T) * fp->uf_lines.ga_len));
17386 fp->uf_tml_idx = -1;
17387 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
17388 || fp->uf_tml_self == NULL)
17389 return; /* out of memory */
17390
17391 fp->uf_profiling = TRUE;
17392}
17393
17394/*
17395 * Dump the profiling results for all functions in file "fd".
17396 */
17397 void
17398func_dump_profile(fd)
17399 FILE *fd;
17400{
17401 hashitem_T *hi;
17402 int todo;
17403 ufunc_T *fp;
17404 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000017405 ufunc_T **sorttab;
17406 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017407
17408 todo = func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000017409 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
17410
Bram Moolenaar05159a02005-02-26 23:04:13 +000017411 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
17412 {
17413 if (!HASHITEM_EMPTY(hi))
17414 {
17415 --todo;
17416 fp = HI2UF(hi);
17417 if (fp->uf_profiling)
17418 {
Bram Moolenaar73830342005-02-28 22:48:19 +000017419 if (sorttab != NULL)
17420 sorttab[st_len++] = fp;
17421
Bram Moolenaar05159a02005-02-26 23:04:13 +000017422 if (fp->uf_name[0] == K_SPECIAL)
17423 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
17424 else
17425 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
17426 if (fp->uf_tm_count == 1)
17427 fprintf(fd, "Called 1 time\n");
17428 else
17429 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
17430 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
17431 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
17432 fprintf(fd, "\n");
17433 fprintf(fd, "count total (s) self (s)\n");
17434
17435 for (i = 0; i < fp->uf_lines.ga_len; ++i)
17436 {
Bram Moolenaar73830342005-02-28 22:48:19 +000017437 prof_func_line(fd, fp->uf_tml_count[i],
17438 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017439 fprintf(fd, "%s\n", FUNCLINE(fp, i));
17440 }
17441 fprintf(fd, "\n");
17442 }
17443 }
17444 }
Bram Moolenaar73830342005-02-28 22:48:19 +000017445
17446 if (sorttab != NULL && st_len > 0)
17447 {
17448 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
17449 prof_total_cmp);
17450 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
17451 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
17452 prof_self_cmp);
17453 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
17454 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000017455}
Bram Moolenaar73830342005-02-28 22:48:19 +000017456
17457 static void
17458prof_sort_list(fd, sorttab, st_len, title, prefer_self)
17459 FILE *fd;
17460 ufunc_T **sorttab;
17461 int st_len;
17462 char *title;
17463 int prefer_self; /* when equal print only self time */
17464{
17465 int i;
17466 ufunc_T *fp;
17467
17468 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
17469 fprintf(fd, "count total (s) self (s) function\n");
17470 for (i = 0; i < 20 && i < st_len; ++i)
17471 {
17472 fp = sorttab[i];
17473 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
17474 prefer_self);
17475 if (fp->uf_name[0] == K_SPECIAL)
17476 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
17477 else
17478 fprintf(fd, " %s()\n", fp->uf_name);
17479 }
17480 fprintf(fd, "\n");
17481}
17482
17483/*
17484 * Print the count and times for one function or function line.
17485 */
17486 static void
17487prof_func_line(fd, count, total, self, prefer_self)
17488 FILE *fd;
17489 int count;
17490 proftime_T *total;
17491 proftime_T *self;
17492 int prefer_self; /* when equal print only self time */
17493{
17494 if (count > 0)
17495 {
17496 fprintf(fd, "%5d ", count);
17497 if (prefer_self && profile_equal(total, self))
17498 fprintf(fd, " ");
17499 else
17500 fprintf(fd, "%s ", profile_msg(total));
17501 if (!prefer_self && profile_equal(total, self))
17502 fprintf(fd, " ");
17503 else
17504 fprintf(fd, "%s ", profile_msg(self));
17505 }
17506 else
17507 fprintf(fd, " ");
17508}
17509
17510/*
17511 * Compare function for total time sorting.
17512 */
17513 static int
17514#ifdef __BORLANDC__
17515_RTLENTRYF
17516#endif
17517prof_total_cmp(s1, s2)
17518 const void *s1;
17519 const void *s2;
17520{
17521 ufunc_T *p1, *p2;
17522
17523 p1 = *(ufunc_T **)s1;
17524 p2 = *(ufunc_T **)s2;
17525 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
17526}
17527
17528/*
17529 * Compare function for self time sorting.
17530 */
17531 static int
17532#ifdef __BORLANDC__
17533_RTLENTRYF
17534#endif
17535prof_self_cmp(s1, s2)
17536 const void *s1;
17537 const void *s2;
17538{
17539 ufunc_T *p1, *p2;
17540
17541 p1 = *(ufunc_T **)s1;
17542 p2 = *(ufunc_T **)s2;
17543 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
17544}
17545
Bram Moolenaar05159a02005-02-26 23:04:13 +000017546#endif
17547
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017548/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017549 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017550 * Return TRUE if a package was loaded.
17551 */
17552 static int
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017553script_autoload(name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017554 char_u *name;
17555{
17556 char_u *p;
17557 char_u *scriptname;
17558 int ret = FALSE;
17559
17560 /* If there is no colon after name[1] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017561 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017562 if (p == NULL || p <= name + 2)
17563 return FALSE;
17564
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017565 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
17566 scriptname = autoload_name(name);
17567 if (cmd_runtime(scriptname, FALSE) == OK)
17568 ret = TRUE;
17569
17570 vim_free(scriptname);
17571 return ret;
17572}
17573
17574/*
17575 * Return the autoload script name for a function or variable name.
17576 * Returns NULL when out of memory.
17577 */
17578 static char_u *
17579autoload_name(name)
17580 char_u *name;
17581{
17582 char_u *p;
17583 char_u *scriptname;
17584
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017585 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017586 scriptname = alloc((unsigned)(STRLEN(name) + 14));
17587 if (scriptname == NULL)
17588 return FALSE;
17589 STRCPY(scriptname, "autoload/");
17590 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017591 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017592 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017593 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017594 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017595 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017596}
17597
Bram Moolenaar071d4272004-06-13 20:20:40 +000017598#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
17599
17600/*
17601 * Function given to ExpandGeneric() to obtain the list of user defined
17602 * function names.
17603 */
17604 char_u *
17605get_user_func_name(xp, idx)
17606 expand_T *xp;
17607 int idx;
17608{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017609 static long_u done;
17610 static hashitem_T *hi;
17611 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017612
17613 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017614 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017615 done = 0;
17616 hi = func_hashtab.ht_array;
17617 }
17618 if (done < func_hashtab.ht_used)
17619 {
17620 if (done++ > 0)
17621 ++hi;
17622 while (HASHITEM_EMPTY(hi))
17623 ++hi;
17624 fp = HI2UF(hi);
17625
17626 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
17627 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017628
17629 cat_func_name(IObuff, fp);
17630 if (xp->xp_context != EXPAND_USER_FUNC)
17631 {
17632 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017633 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017634 STRCAT(IObuff, ")");
17635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017636 return IObuff;
17637 }
17638 return NULL;
17639}
17640
17641#endif /* FEAT_CMDL_COMPL */
17642
17643/*
17644 * Copy the function name of "fp" to buffer "buf".
17645 * "buf" must be able to hold the function name plus three bytes.
17646 * Takes care of script-local function names.
17647 */
17648 static void
17649cat_func_name(buf, fp)
17650 char_u *buf;
17651 ufunc_T *fp;
17652{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017653 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017654 {
17655 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017656 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017657 }
17658 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017659 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017660}
17661
17662/*
17663 * ":delfunction {name}"
17664 */
17665 void
17666ex_delfunction(eap)
17667 exarg_T *eap;
17668{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017669 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017670 char_u *p;
17671 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017672 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017673
17674 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017675 name = trans_function_name(&p, eap->skip, 0, &fudi);
17676 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017677 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017678 {
17679 if (fudi.fd_dict != NULL && !eap->skip)
17680 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017681 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017683 if (!ends_excmd(*skipwhite(p)))
17684 {
17685 vim_free(name);
17686 EMSG(_(e_trailing));
17687 return;
17688 }
17689 eap->nextcmd = check_nextcmd(p);
17690 if (eap->nextcmd != NULL)
17691 *p = NUL;
17692
17693 if (!eap->skip)
17694 fp = find_func(name);
17695 vim_free(name);
17696
17697 if (!eap->skip)
17698 {
17699 if (fp == NULL)
17700 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000017701 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017702 return;
17703 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017704 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017705 {
17706 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
17707 return;
17708 }
17709
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017710 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017711 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017712 /* Delete the dict item that refers to the function, it will
17713 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017714 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017715 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017716 else
17717 func_free(fp);
17718 }
17719}
17720
17721/*
17722 * Free a function and remove it from the list of functions.
17723 */
17724 static void
17725func_free(fp)
17726 ufunc_T *fp;
17727{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017728 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017729
17730 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017731 ga_clear_strings(&(fp->uf_args));
17732 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000017733#ifdef FEAT_PROFILE
17734 vim_free(fp->uf_tml_count);
17735 vim_free(fp->uf_tml_total);
17736 vim_free(fp->uf_tml_self);
17737#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017738
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017739 /* remove the function from the function hashtable */
17740 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
17741 if (HASHITEM_EMPTY(hi))
17742 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017743 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017744 hash_remove(&func_hashtab, hi);
17745
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017746 vim_free(fp);
17747}
17748
17749/*
17750 * Unreference a Function: decrement the reference count and free it when it
17751 * becomes zero. Only for numbered functions.
17752 */
17753 static void
17754func_unref(name)
17755 char_u *name;
17756{
17757 ufunc_T *fp;
17758
17759 if (name != NULL && isdigit(*name))
17760 {
17761 fp = find_func(name);
17762 if (fp == NULL)
17763 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017764 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017765 {
17766 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017767 * when "uf_calls" becomes zero. */
17768 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017769 func_free(fp);
17770 }
17771 }
17772}
17773
17774/*
17775 * Count a reference to a Function.
17776 */
17777 static void
17778func_ref(name)
17779 char_u *name;
17780{
17781 ufunc_T *fp;
17782
17783 if (name != NULL && isdigit(*name))
17784 {
17785 fp = find_func(name);
17786 if (fp == NULL)
17787 EMSG2(_(e_intern2), "func_ref()");
17788 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017789 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017790 }
17791}
17792
17793/*
17794 * Call a user function.
17795 */
17796 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000017797call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017798 ufunc_T *fp; /* pointer to function */
17799 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000017800 typval_T *argvars; /* arguments */
17801 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017802 linenr_T firstline; /* first line of range */
17803 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000017804 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017805{
Bram Moolenaar33570922005-01-25 22:26:29 +000017806 char_u *save_sourcing_name;
17807 linenr_T save_sourcing_lnum;
17808 scid_T save_current_SID;
17809 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000017810 int save_did_emsg;
17811 static int depth = 0;
17812 dictitem_T *v;
17813 int fixvar_idx = 0; /* index in fixvar[] */
17814 int i;
17815 int ai;
17816 char_u numbuf[NUMBUFLEN];
17817 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017818#ifdef FEAT_PROFILE
17819 proftime_T wait_start;
17820#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017821
17822 /* If depth of calling is getting too high, don't execute the function */
17823 if (depth >= p_mfd)
17824 {
17825 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017826 rettv->v_type = VAR_NUMBER;
17827 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017828 return;
17829 }
17830 ++depth;
17831
17832 line_breakcheck(); /* check for CTRL-C hit */
17833
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017834 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000017835 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017836 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017837 fc.rettv = rettv;
17838 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017839 fc.linenr = 0;
17840 fc.returned = FALSE;
17841 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017842 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017843 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017844 fc.dbg_tick = debug_tick;
17845
Bram Moolenaar33570922005-01-25 22:26:29 +000017846 /*
17847 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
17848 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
17849 * each argument variable and saves a lot of time.
17850 */
17851 /*
17852 * Init l: variables.
17853 */
17854 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000017855 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017856 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017857 /* Set l:self to "selfdict". */
17858 v = &fc.fixvar[fixvar_idx++].var;
17859 STRCPY(v->di_key, "self");
17860 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
17861 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
17862 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017863 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000017864 v->di_tv.vval.v_dict = selfdict;
17865 ++selfdict->dv_refcount;
17866 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000017867
Bram Moolenaar33570922005-01-25 22:26:29 +000017868 /*
17869 * Init a: variables.
17870 * Set a:0 to "argcount".
17871 * Set a:000 to a list with room for the "..." arguments.
17872 */
17873 init_var_dict(&fc.l_avars, &fc.l_avars_var);
17874 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017875 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000017876 v = &fc.fixvar[fixvar_idx++].var;
17877 STRCPY(v->di_key, "000");
17878 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
17879 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
17880 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017881 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017882 v->di_tv.vval.v_list = &fc.l_varlist;
17883 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
17884 fc.l_varlist.lv_refcount = 99999;
17885
17886 /*
17887 * Set a:firstline to "firstline" and a:lastline to "lastline".
17888 * Set a:name to named arguments.
17889 * Set a:N to the "..." arguments.
17890 */
17891 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
17892 (varnumber_T)firstline);
17893 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
17894 (varnumber_T)lastline);
17895 for (i = 0; i < argcount; ++i)
17896 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017897 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000017898 if (ai < 0)
17899 /* named argument a:name */
17900 name = FUNCARG(fp, i);
17901 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000017902 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017903 /* "..." argument a:1, a:2, etc. */
17904 sprintf((char *)numbuf, "%d", ai + 1);
17905 name = numbuf;
17906 }
17907 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
17908 {
17909 v = &fc.fixvar[fixvar_idx++].var;
17910 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
17911 }
17912 else
17913 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017914 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
17915 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000017916 if (v == NULL)
17917 break;
17918 v->di_flags = DI_FLAGS_RO;
17919 }
17920 STRCPY(v->di_key, name);
17921 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
17922
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017923 /* Note: the values are copied directly to avoid alloc/free.
17924 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017925 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017926 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017927
17928 if (ai >= 0 && ai < MAX_FUNC_ARGS)
17929 {
17930 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
17931 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017932 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017933 }
17934 }
17935
Bram Moolenaar071d4272004-06-13 20:20:40 +000017936 /* Don't redraw while executing the function. */
17937 ++RedrawingDisabled;
17938 save_sourcing_name = sourcing_name;
17939 save_sourcing_lnum = sourcing_lnum;
17940 sourcing_lnum = 1;
17941 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017942 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017943 if (sourcing_name != NULL)
17944 {
17945 if (save_sourcing_name != NULL
17946 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
17947 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
17948 else
17949 STRCPY(sourcing_name, "function ");
17950 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
17951
17952 if (p_verbose >= 12)
17953 {
17954 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000017955 verbose_enter_scroll();
17956
Bram Moolenaar555b2802005-05-19 21:08:39 +000017957 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017958 if (p_verbose >= 14)
17959 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017960 char_u buf[MSG_BUF_LEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000017961 char_u numbuf[NUMBUFLEN];
17962 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017963
17964 msg_puts((char_u *)"(");
17965 for (i = 0; i < argcount; ++i)
17966 {
17967 if (i > 0)
17968 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017969 if (argvars[i].v_type == VAR_NUMBER)
17970 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017971 else
17972 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000017973 trunc_string(tv2string(&argvars[i], &tofree, numbuf),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017974 buf, MSG_BUF_CLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017975 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000017976 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017977 }
17978 }
17979 msg_puts((char_u *)")");
17980 }
17981 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000017982
17983 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017984 --no_wait_return;
17985 }
17986 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000017987#ifdef FEAT_PROFILE
17988 if (do_profiling)
17989 {
17990 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
17991 func_do_profile(fp);
17992 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017993 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000017994 {
17995 ++fp->uf_tm_count;
17996 profile_start(&fp->uf_tm_start);
17997 profile_zero(&fp->uf_tm_children);
17998 }
17999 script_prof_save(&wait_start);
18000 }
18001#endif
18002
Bram Moolenaar071d4272004-06-13 20:20:40 +000018003 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018004 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018005 save_did_emsg = did_emsg;
18006 did_emsg = FALSE;
18007
18008 /* call do_cmdline() to execute the lines */
18009 do_cmdline(NULL, get_func_line, (void *)&fc,
18010 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
18011
18012 --RedrawingDisabled;
18013
18014 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018015 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018016 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018017 clear_tv(rettv);
18018 rettv->v_type = VAR_NUMBER;
18019 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018020 }
18021
Bram Moolenaar05159a02005-02-26 23:04:13 +000018022#ifdef FEAT_PROFILE
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018023 if (fp->uf_profiling || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000018024 {
18025 profile_end(&fp->uf_tm_start);
18026 profile_sub_wait(&wait_start, &fp->uf_tm_start);
18027 profile_add(&fp->uf_tm_total, &fp->uf_tm_start);
18028 profile_add(&fp->uf_tm_self, &fp->uf_tm_start);
18029 profile_sub(&fp->uf_tm_self, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018030 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018031 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018032 profile_add(&fc.caller->func->uf_tm_children, &fp->uf_tm_start);
18033 profile_add(&fc.caller->func->uf_tml_children, &fp->uf_tm_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018034 }
18035 }
18036#endif
18037
Bram Moolenaar071d4272004-06-13 20:20:40 +000018038 /* when being verbose, mention the return value */
18039 if (p_verbose >= 12)
18040 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018041 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018042 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018043
Bram Moolenaar071d4272004-06-13 20:20:40 +000018044 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000018045 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018046 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000018047 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
18048 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000018049 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000018050 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000018051 char_u buf[MSG_BUF_LEN];
18052 char_u numbuf[NUMBUFLEN];
18053 char_u *tofree;
18054
Bram Moolenaar555b2802005-05-19 21:08:39 +000018055 /* The value may be very long. Skip the middle part, so that we
18056 * have some idea how it starts and ends. smsg() would always
18057 * truncate it at the end. */
Bram Moolenaar758711c2005-02-02 23:11:38 +000018058 trunc_string(tv2string(fc.rettv, &tofree, numbuf),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018059 buf, MSG_BUF_CLEN);
Bram Moolenaar555b2802005-05-19 21:08:39 +000018060 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000018061 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018062 }
18063 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018064
18065 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018066 --no_wait_return;
18067 }
18068
18069 vim_free(sourcing_name);
18070 sourcing_name = save_sourcing_name;
18071 sourcing_lnum = save_sourcing_lnum;
18072 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018073#ifdef FEAT_PROFILE
18074 if (do_profiling)
18075 script_prof_restore(&wait_start);
18076#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018077
18078 if (p_verbose >= 12 && sourcing_name != NULL)
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 *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018084 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018085
18086 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018087 --no_wait_return;
18088 }
18089
18090 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018091 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018092
Bram Moolenaar33570922005-01-25 22:26:29 +000018093 /* The a: variables typevals were not alloced, only free the allocated
18094 * variables. */
18095 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
18096
18097 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018098 --depth;
18099}
18100
18101/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018102 * Add a number variable "name" to dict "dp" with value "nr".
18103 */
18104 static void
18105add_nr_var(dp, v, name, nr)
18106 dict_T *dp;
18107 dictitem_T *v;
18108 char *name;
18109 varnumber_T nr;
18110{
18111 STRCPY(v->di_key, name);
18112 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18113 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
18114 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018115 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018116 v->di_tv.vval.v_number = nr;
18117}
18118
18119/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018120 * ":return [expr]"
18121 */
18122 void
18123ex_return(eap)
18124 exarg_T *eap;
18125{
18126 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018127 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018128 int returning = FALSE;
18129
18130 if (current_funccal == NULL)
18131 {
18132 EMSG(_("E133: :return not inside a function"));
18133 return;
18134 }
18135
18136 if (eap->skip)
18137 ++emsg_skip;
18138
18139 eap->nextcmd = NULL;
18140 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018141 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018142 {
18143 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018144 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018145 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018146 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018147 }
18148 /* It's safer to return also on error. */
18149 else if (!eap->skip)
18150 {
18151 /*
18152 * Return unless the expression evaluation has been cancelled due to an
18153 * aborting error, an interrupt, or an exception.
18154 */
18155 if (!aborting())
18156 returning = do_return(eap, FALSE, TRUE, NULL);
18157 }
18158
18159 /* When skipping or the return gets pending, advance to the next command
18160 * in this line (!returning). Otherwise, ignore the rest of the line.
18161 * Following lines will be ignored by get_func_line(). */
18162 if (returning)
18163 eap->nextcmd = NULL;
18164 else if (eap->nextcmd == NULL) /* no argument */
18165 eap->nextcmd = check_nextcmd(arg);
18166
18167 if (eap->skip)
18168 --emsg_skip;
18169}
18170
18171/*
18172 * Return from a function. Possibly makes the return pending. Also called
18173 * for a pending return at the ":endtry" or after returning from an extra
18174 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000018175 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018176 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018177 * FALSE when the return gets pending.
18178 */
18179 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018180do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018181 exarg_T *eap;
18182 int reanimate;
18183 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018184 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018185{
18186 int idx;
18187 struct condstack *cstack = eap->cstack;
18188
18189 if (reanimate)
18190 /* Undo the return. */
18191 current_funccal->returned = FALSE;
18192
18193 /*
18194 * Cleanup (and inactivate) conditionals, but stop when a try conditional
18195 * not in its finally clause (which then is to be executed next) is found.
18196 * In this case, make the ":return" pending for execution at the ":endtry".
18197 * Otherwise, return normally.
18198 */
18199 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
18200 if (idx >= 0)
18201 {
18202 cstack->cs_pending[idx] = CSTP_RETURN;
18203
18204 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018205 /* A pending return again gets pending. "rettv" points to an
18206 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000018207 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018208 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018209 else
18210 {
18211 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018212 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018213 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018214 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018215
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018216 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018217 {
18218 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018219 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018220 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018221 else
18222 EMSG(_(e_outofmem));
18223 }
18224 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018225 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018226
18227 if (reanimate)
18228 {
18229 /* The pending return value could be overwritten by a ":return"
18230 * without argument in a finally clause; reset the default
18231 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018232 current_funccal->rettv->v_type = VAR_NUMBER;
18233 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018234 }
18235 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018236 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018237 }
18238 else
18239 {
18240 current_funccal->returned = TRUE;
18241
18242 /* If the return is carried out now, store the return value. For
18243 * a return immediately after reanimation, the value is already
18244 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018245 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018246 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018247 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000018248 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018249 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018250 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018251 }
18252 }
18253
18254 return idx < 0;
18255}
18256
18257/*
18258 * Free the variable with a pending return value.
18259 */
18260 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018261discard_pending_return(rettv)
18262 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018263{
Bram Moolenaar33570922005-01-25 22:26:29 +000018264 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018265}
18266
18267/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018268 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000018269 * is an allocated string. Used by report_pending() for verbose messages.
18270 */
18271 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018272get_return_cmd(rettv)
18273 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018274{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018275 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018276 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018277 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018278
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018279 if (rettv != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018280 s = echo_string((typval_T *)rettv, &tofree, numbuf);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018281 if (s == NULL)
18282 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018283
18284 STRCPY(IObuff, ":return ");
18285 STRNCPY(IObuff + 8, s, IOSIZE - 8);
18286 if (STRLEN(s) + 8 >= IOSIZE)
18287 STRCPY(IObuff + IOSIZE - 4, "...");
18288 vim_free(tofree);
18289 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018290}
18291
18292/*
18293 * Get next function line.
18294 * Called by do_cmdline() to get the next line.
18295 * Returns allocated string, or NULL for end of function.
18296 */
18297/* ARGSUSED */
18298 char_u *
18299get_func_line(c, cookie, indent)
18300 int c; /* not used */
18301 void *cookie;
18302 int indent; /* not used */
18303{
Bram Moolenaar33570922005-01-25 22:26:29 +000018304 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018305 ufunc_T *fp = fcp->func;
18306 char_u *retval;
18307 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018308
18309 /* If breakpoints have been added/deleted need to check for it. */
18310 if (fcp->dbg_tick != debug_tick)
18311 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000018312 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018313 sourcing_lnum);
18314 fcp->dbg_tick = debug_tick;
18315 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000018316#ifdef FEAT_PROFILE
18317 if (do_profiling)
18318 func_line_end(cookie);
18319#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018320
Bram Moolenaar05159a02005-02-26 23:04:13 +000018321 gap = &fp->uf_lines;
18322 if ((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000018323 retval = NULL;
18324 else if (fcp->returned || fcp->linenr >= gap->ga_len)
18325 retval = NULL;
18326 else
18327 {
18328 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
18329 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018330#ifdef FEAT_PROFILE
18331 if (do_profiling)
18332 func_line_start(cookie);
18333#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018334 }
18335
18336 /* Did we encounter a breakpoint? */
18337 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
18338 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000018339 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018340 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018341 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018342 sourcing_lnum);
18343 fcp->dbg_tick = debug_tick;
18344 }
18345
18346 return retval;
18347}
18348
Bram Moolenaar05159a02005-02-26 23:04:13 +000018349#if defined(FEAT_PROFILE) || defined(PROTO)
18350/*
18351 * Called when starting to read a function line.
18352 * "sourcing_lnum" must be correct!
18353 * When skipping lines it may not actually be executed, but we won't find out
18354 * until later and we need to store the time now.
18355 */
18356 void
18357func_line_start(cookie)
18358 void *cookie;
18359{
18360 funccall_T *fcp = (funccall_T *)cookie;
18361 ufunc_T *fp = fcp->func;
18362
18363 if (fp->uf_profiling && sourcing_lnum >= 1
18364 && sourcing_lnum <= fp->uf_lines.ga_len)
18365 {
18366 fp->uf_tml_idx = sourcing_lnum - 1;
18367 fp->uf_tml_execed = FALSE;
18368 profile_start(&fp->uf_tml_start);
18369 profile_zero(&fp->uf_tml_children);
18370 profile_get_wait(&fp->uf_tml_wait);
18371 }
18372}
18373
18374/*
18375 * Called when actually executing a function line.
18376 */
18377 void
18378func_line_exec(cookie)
18379 void *cookie;
18380{
18381 funccall_T *fcp = (funccall_T *)cookie;
18382 ufunc_T *fp = fcp->func;
18383
18384 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
18385 fp->uf_tml_execed = TRUE;
18386}
18387
18388/*
18389 * Called when done with a function line.
18390 */
18391 void
18392func_line_end(cookie)
18393 void *cookie;
18394{
18395 funccall_T *fcp = (funccall_T *)cookie;
18396 ufunc_T *fp = fcp->func;
18397
18398 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
18399 {
18400 if (fp->uf_tml_execed)
18401 {
18402 ++fp->uf_tml_count[fp->uf_tml_idx];
18403 profile_end(&fp->uf_tml_start);
18404 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
18405 profile_add(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start);
18406 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
18407 profile_sub(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_children);
18408 }
18409 fp->uf_tml_idx = -1;
18410 }
18411}
18412#endif
18413
Bram Moolenaar071d4272004-06-13 20:20:40 +000018414/*
18415 * Return TRUE if the currently active function should be ended, because a
18416 * return was encountered or an error occured. Used inside a ":while".
18417 */
18418 int
18419func_has_ended(cookie)
18420 void *cookie;
18421{
Bram Moolenaar33570922005-01-25 22:26:29 +000018422 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018423
18424 /* Ignore the "abort" flag if the abortion behavior has been changed due to
18425 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018426 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000018427 || fcp->returned);
18428}
18429
18430/*
18431 * return TRUE if cookie indicates a function which "abort"s on errors.
18432 */
18433 int
18434func_has_abort(cookie)
18435 void *cookie;
18436{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018437 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018438}
18439
18440#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
18441typedef enum
18442{
18443 VAR_FLAVOUR_DEFAULT,
18444 VAR_FLAVOUR_SESSION,
18445 VAR_FLAVOUR_VIMINFO
18446} var_flavour_T;
18447
18448static var_flavour_T var_flavour __ARGS((char_u *varname));
18449
18450 static var_flavour_T
18451var_flavour(varname)
18452 char_u *varname;
18453{
18454 char_u *p = varname;
18455
18456 if (ASCII_ISUPPER(*p))
18457 {
18458 while (*(++p))
18459 if (ASCII_ISLOWER(*p))
18460 return VAR_FLAVOUR_SESSION;
18461 return VAR_FLAVOUR_VIMINFO;
18462 }
18463 else
18464 return VAR_FLAVOUR_DEFAULT;
18465}
18466#endif
18467
18468#if defined(FEAT_VIMINFO) || defined(PROTO)
18469/*
18470 * Restore global vars that start with a capital from the viminfo file
18471 */
18472 int
18473read_viminfo_varlist(virp, writing)
18474 vir_T *virp;
18475 int writing;
18476{
18477 char_u *tab;
18478 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000018479 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018480
18481 if (!writing && (find_viminfo_parameter('!') != NULL))
18482 {
18483 tab = vim_strchr(virp->vir_line + 1, '\t');
18484 if (tab != NULL)
18485 {
18486 *tab++ = '\0'; /* isolate the variable name */
18487 if (*tab == 'S') /* string var */
18488 is_string = TRUE;
18489
18490 tab = vim_strchr(tab, '\t');
18491 if (tab != NULL)
18492 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018493 if (is_string)
18494 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000018495 tv.v_type = VAR_STRING;
18496 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018497 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018498 }
18499 else
18500 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000018501 tv.v_type = VAR_NUMBER;
18502 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018503 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000018504 set_var(virp->vir_line + 1, &tv, FALSE);
18505 if (is_string)
18506 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018507 }
18508 }
18509 }
18510
18511 return viminfo_readline(virp);
18512}
18513
18514/*
18515 * Write global vars that start with a capital to the viminfo file
18516 */
18517 void
18518write_viminfo_varlist(fp)
18519 FILE *fp;
18520{
Bram Moolenaar33570922005-01-25 22:26:29 +000018521 hashitem_T *hi;
18522 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000018523 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018524 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018525 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018526 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018527 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018528
18529 if (find_viminfo_parameter('!') == NULL)
18530 return;
18531
18532 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000018533
Bram Moolenaar33570922005-01-25 22:26:29 +000018534 todo = globvarht.ht_used;
18535 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018536 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018537 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018538 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018539 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000018540 this_var = HI2DI(hi);
18541 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018542 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018543 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000018544 {
18545 case VAR_STRING: s = "STR"; break;
18546 case VAR_NUMBER: s = "NUM"; break;
18547 default: continue;
18548 }
Bram Moolenaar33570922005-01-25 22:26:29 +000018549 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018550 p = echo_string(&this_var->di_tv, &tofree, numbuf);
18551 if (p != NULL)
18552 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000018553 vim_free(tofree);
18554 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018555 }
18556 }
18557}
18558#endif
18559
18560#if defined(FEAT_SESSION) || defined(PROTO)
18561 int
18562store_session_globals(fd)
18563 FILE *fd;
18564{
Bram Moolenaar33570922005-01-25 22:26:29 +000018565 hashitem_T *hi;
18566 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000018567 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018568 char_u *p, *t;
18569
Bram Moolenaar33570922005-01-25 22:26:29 +000018570 todo = globvarht.ht_used;
18571 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018572 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018573 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018574 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018575 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000018576 this_var = HI2DI(hi);
18577 if ((this_var->di_tv.v_type == VAR_NUMBER
18578 || this_var->di_tv.v_type == VAR_STRING)
18579 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000018580 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018581 /* Escape special characters with a backslash. Turn a LF and
18582 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000018583 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000018584 (char_u *)"\\\"\n\r");
18585 if (p == NULL) /* out of memory */
18586 break;
18587 for (t = p; *t != NUL; ++t)
18588 if (*t == '\n')
18589 *t = 'n';
18590 else if (*t == '\r')
18591 *t = 'r';
18592 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000018593 this_var->di_key,
18594 (this_var->di_tv.v_type == VAR_STRING) ? '"'
18595 : ' ',
18596 p,
18597 (this_var->di_tv.v_type == VAR_STRING) ? '"'
18598 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000018599 || put_eol(fd) == FAIL)
18600 {
18601 vim_free(p);
18602 return FAIL;
18603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018604 vim_free(p);
18605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018606 }
18607 }
18608 return OK;
18609}
18610#endif
18611
18612#endif /* FEAT_EVAL */
18613
18614#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
18615
18616
18617#ifdef WIN3264
18618/*
18619 * Functions for ":8" filename modifier: get 8.3 version of a filename.
18620 */
18621static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
18622static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
18623static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
18624
18625/*
18626 * Get the short pathname of a file.
18627 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
18628 */
18629 static int
18630get_short_pathname(fnamep, bufp, fnamelen)
18631 char_u **fnamep;
18632 char_u **bufp;
18633 int *fnamelen;
18634{
18635 int l,len;
18636 char_u *newbuf;
18637
18638 len = *fnamelen;
18639
18640 l = GetShortPathName(*fnamep, *fnamep, len);
18641 if (l > len - 1)
18642 {
18643 /* If that doesn't work (not enough space), then save the string
18644 * and try again with a new buffer big enough
18645 */
18646 newbuf = vim_strnsave(*fnamep, l);
18647 if (newbuf == NULL)
18648 return 0;
18649
18650 vim_free(*bufp);
18651 *fnamep = *bufp = newbuf;
18652
18653 l = GetShortPathName(*fnamep,*fnamep,l+1);
18654
18655 /* Really should always succeed, as the buffer is big enough */
18656 }
18657
18658 *fnamelen = l;
18659 return 1;
18660}
18661
18662/*
18663 * Create a short path name. Returns the length of the buffer it needs.
18664 * Doesn't copy over the end of the buffer passed in.
18665 */
18666 static int
18667shortpath_for_invalid_fname(fname, bufp, fnamelen)
18668 char_u **fname;
18669 char_u **bufp;
18670 int *fnamelen;
18671{
18672 char_u *s, *p, *pbuf2, *pbuf3;
18673 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000018674 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018675
18676 /* Make a copy */
18677 len2 = *fnamelen;
18678 pbuf2 = vim_strnsave(*fname, len2);
18679 pbuf3 = NULL;
18680
18681 s = pbuf2 + len2 - 1; /* Find the end */
18682 slen = 1;
18683 plen = len2;
18684
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018685 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018686 {
18687 --s;
18688 ++slen;
18689 --plen;
18690 }
18691
18692 do
18693 {
18694 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018695 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018696 {
18697 --s;
18698 ++slen;
18699 --plen;
18700 }
18701 if (s <= pbuf2)
18702 break;
18703
18704 /* Remeber the character that is about to be blatted */
18705 ch = *s;
18706 *s = 0; /* get_short_pathname requires a null-terminated string */
18707
18708 /* Try it in situ */
18709 p = pbuf2;
18710 if (!get_short_pathname(&p, &pbuf3, &plen))
18711 {
18712 vim_free(pbuf2);
18713 return -1;
18714 }
18715 *s = ch; /* Preserve the string */
18716 } while (plen == 0);
18717
18718 if (plen > 0)
18719 {
18720 /* Remeber the length of the new string. */
18721 *fnamelen = len = plen + slen;
18722 vim_free(*bufp);
18723 if (len > len2)
18724 {
18725 /* If there's not enough space in the currently allocated string,
18726 * then copy it to a buffer big enough.
18727 */
18728 *fname= *bufp = vim_strnsave(p, len);
18729 if (*fname == NULL)
18730 return -1;
18731 }
18732 else
18733 {
18734 /* Transfer pbuf2 to being the main buffer (it's big enough) */
18735 *fname = *bufp = pbuf2;
18736 if (p != pbuf2)
18737 strncpy(*fname, p, plen);
18738 pbuf2 = NULL;
18739 }
18740 /* Concat the next bit */
18741 strncpy(*fname + plen, s, slen);
18742 (*fname)[len] = '\0';
18743 }
18744 vim_free(pbuf3);
18745 vim_free(pbuf2);
18746 return 0;
18747}
18748
18749/*
18750 * Get a pathname for a partial path.
18751 */
18752 static int
18753shortpath_for_partial(fnamep, bufp, fnamelen)
18754 char_u **fnamep;
18755 char_u **bufp;
18756 int *fnamelen;
18757{
18758 int sepcount, len, tflen;
18759 char_u *p;
18760 char_u *pbuf, *tfname;
18761 int hasTilde;
18762
18763 /* Count up the path seperators from the RHS.. so we know which part
18764 * of the path to return.
18765 */
18766 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018767 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018768 if (vim_ispathsep(*p))
18769 ++sepcount;
18770
18771 /* Need full path first (use expand_env() to remove a "~/") */
18772 hasTilde = (**fnamep == '~');
18773 if (hasTilde)
18774 pbuf = tfname = expand_env_save(*fnamep);
18775 else
18776 pbuf = tfname = FullName_save(*fnamep, FALSE);
18777
18778 len = tflen = STRLEN(tfname);
18779
18780 if (!get_short_pathname(&tfname, &pbuf, &len))
18781 return -1;
18782
18783 if (len == 0)
18784 {
18785 /* Don't have a valid filename, so shorten the rest of the
18786 * path if we can. This CAN give us invalid 8.3 filenames, but
18787 * there's not a lot of point in guessing what it might be.
18788 */
18789 len = tflen;
18790 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
18791 return -1;
18792 }
18793
18794 /* Count the paths backward to find the beginning of the desired string. */
18795 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018796 {
18797#ifdef FEAT_MBYTE
18798 if (has_mbyte)
18799 p -= mb_head_off(tfname, p);
18800#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018801 if (vim_ispathsep(*p))
18802 {
18803 if (sepcount == 0 || (hasTilde && sepcount == 1))
18804 break;
18805 else
18806 sepcount --;
18807 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018809 if (hasTilde)
18810 {
18811 --p;
18812 if (p >= tfname)
18813 *p = '~';
18814 else
18815 return -1;
18816 }
18817 else
18818 ++p;
18819
18820 /* Copy in the string - p indexes into tfname - allocated at pbuf */
18821 vim_free(*bufp);
18822 *fnamelen = (int)STRLEN(p);
18823 *bufp = pbuf;
18824 *fnamep = p;
18825
18826 return 0;
18827}
18828#endif /* WIN3264 */
18829
18830/*
18831 * Adjust a filename, according to a string of modifiers.
18832 * *fnamep must be NUL terminated when called. When returning, the length is
18833 * determined by *fnamelen.
18834 * Returns valid flags.
18835 * When there is an error, *fnamep is set to NULL.
18836 */
18837 int
18838modify_fname(src, usedlen, fnamep, bufp, fnamelen)
18839 char_u *src; /* string with modifiers */
18840 int *usedlen; /* characters after src that are used */
18841 char_u **fnamep; /* file name so far */
18842 char_u **bufp; /* buffer for allocated file name or NULL */
18843 int *fnamelen; /* length of fnamep */
18844{
18845 int valid = 0;
18846 char_u *tail;
18847 char_u *s, *p, *pbuf;
18848 char_u dirname[MAXPATHL];
18849 int c;
18850 int has_fullname = 0;
18851#ifdef WIN3264
18852 int has_shortname = 0;
18853#endif
18854
18855repeat:
18856 /* ":p" - full path/file_name */
18857 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
18858 {
18859 has_fullname = 1;
18860
18861 valid |= VALID_PATH;
18862 *usedlen += 2;
18863
18864 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
18865 if ((*fnamep)[0] == '~'
18866#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
18867 && ((*fnamep)[1] == '/'
18868# ifdef BACKSLASH_IN_FILENAME
18869 || (*fnamep)[1] == '\\'
18870# endif
18871 || (*fnamep)[1] == NUL)
18872
18873#endif
18874 )
18875 {
18876 *fnamep = expand_env_save(*fnamep);
18877 vim_free(*bufp); /* free any allocated file name */
18878 *bufp = *fnamep;
18879 if (*fnamep == NULL)
18880 return -1;
18881 }
18882
18883 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018884 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018885 {
18886 if (vim_ispathsep(*p)
18887 && p[1] == '.'
18888 && (p[2] == NUL
18889 || vim_ispathsep(p[2])
18890 || (p[2] == '.'
18891 && (p[3] == NUL || vim_ispathsep(p[3])))))
18892 break;
18893 }
18894
18895 /* FullName_save() is slow, don't use it when not needed. */
18896 if (*p != NUL || !vim_isAbsName(*fnamep))
18897 {
18898 *fnamep = FullName_save(*fnamep, *p != NUL);
18899 vim_free(*bufp); /* free any allocated file name */
18900 *bufp = *fnamep;
18901 if (*fnamep == NULL)
18902 return -1;
18903 }
18904
18905 /* Append a path separator to a directory. */
18906 if (mch_isdir(*fnamep))
18907 {
18908 /* Make room for one or two extra characters. */
18909 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
18910 vim_free(*bufp); /* free any allocated file name */
18911 *bufp = *fnamep;
18912 if (*fnamep == NULL)
18913 return -1;
18914 add_pathsep(*fnamep);
18915 }
18916 }
18917
18918 /* ":." - path relative to the current directory */
18919 /* ":~" - path relative to the home directory */
18920 /* ":8" - shortname path - postponed till after */
18921 while (src[*usedlen] == ':'
18922 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
18923 {
18924 *usedlen += 2;
18925 if (c == '8')
18926 {
18927#ifdef WIN3264
18928 has_shortname = 1; /* Postpone this. */
18929#endif
18930 continue;
18931 }
18932 pbuf = NULL;
18933 /* Need full path first (use expand_env() to remove a "~/") */
18934 if (!has_fullname)
18935 {
18936 if (c == '.' && **fnamep == '~')
18937 p = pbuf = expand_env_save(*fnamep);
18938 else
18939 p = pbuf = FullName_save(*fnamep, FALSE);
18940 }
18941 else
18942 p = *fnamep;
18943
18944 has_fullname = 0;
18945
18946 if (p != NULL)
18947 {
18948 if (c == '.')
18949 {
18950 mch_dirname(dirname, MAXPATHL);
18951 s = shorten_fname(p, dirname);
18952 if (s != NULL)
18953 {
18954 *fnamep = s;
18955 if (pbuf != NULL)
18956 {
18957 vim_free(*bufp); /* free any allocated file name */
18958 *bufp = pbuf;
18959 pbuf = NULL;
18960 }
18961 }
18962 }
18963 else
18964 {
18965 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
18966 /* Only replace it when it starts with '~' */
18967 if (*dirname == '~')
18968 {
18969 s = vim_strsave(dirname);
18970 if (s != NULL)
18971 {
18972 *fnamep = s;
18973 vim_free(*bufp);
18974 *bufp = s;
18975 }
18976 }
18977 }
18978 vim_free(pbuf);
18979 }
18980 }
18981
18982 tail = gettail(*fnamep);
18983 *fnamelen = (int)STRLEN(*fnamep);
18984
18985 /* ":h" - head, remove "/file_name", can be repeated */
18986 /* Don't remove the first "/" or "c:\" */
18987 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
18988 {
18989 valid |= VALID_HEAD;
18990 *usedlen += 2;
18991 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018992 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018993 --tail;
18994 *fnamelen = (int)(tail - *fnamep);
18995#ifdef VMS
18996 if (*fnamelen > 0)
18997 *fnamelen += 1; /* the path separator is part of the path */
18998#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018999 while (tail > s && !after_pathsep(s, tail))
19000 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019001 }
19002
19003 /* ":8" - shortname */
19004 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
19005 {
19006 *usedlen += 2;
19007#ifdef WIN3264
19008 has_shortname = 1;
19009#endif
19010 }
19011
19012#ifdef WIN3264
19013 /* Check shortname after we have done 'heads' and before we do 'tails'
19014 */
19015 if (has_shortname)
19016 {
19017 pbuf = NULL;
19018 /* Copy the string if it is shortened by :h */
19019 if (*fnamelen < (int)STRLEN(*fnamep))
19020 {
19021 p = vim_strnsave(*fnamep, *fnamelen);
19022 if (p == 0)
19023 return -1;
19024 vim_free(*bufp);
19025 *bufp = *fnamep = p;
19026 }
19027
19028 /* Split into two implementations - makes it easier. First is where
19029 * there isn't a full name already, second is where there is.
19030 */
19031 if (!has_fullname && !vim_isAbsName(*fnamep))
19032 {
19033 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
19034 return -1;
19035 }
19036 else
19037 {
19038 int l;
19039
19040 /* Simple case, already have the full-name
19041 * Nearly always shorter, so try first time. */
19042 l = *fnamelen;
19043 if (!get_short_pathname(fnamep, bufp, &l))
19044 return -1;
19045
19046 if (l == 0)
19047 {
19048 /* Couldn't find the filename.. search the paths.
19049 */
19050 l = *fnamelen;
19051 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
19052 return -1;
19053 }
19054 *fnamelen = l;
19055 }
19056 }
19057#endif /* WIN3264 */
19058
19059 /* ":t" - tail, just the basename */
19060 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
19061 {
19062 *usedlen += 2;
19063 *fnamelen -= (int)(tail - *fnamep);
19064 *fnamep = tail;
19065 }
19066
19067 /* ":e" - extension, can be repeated */
19068 /* ":r" - root, without extension, can be repeated */
19069 while (src[*usedlen] == ':'
19070 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
19071 {
19072 /* find a '.' in the tail:
19073 * - for second :e: before the current fname
19074 * - otherwise: The last '.'
19075 */
19076 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
19077 s = *fnamep - 2;
19078 else
19079 s = *fnamep + *fnamelen - 1;
19080 for ( ; s > tail; --s)
19081 if (s[0] == '.')
19082 break;
19083 if (src[*usedlen + 1] == 'e') /* :e */
19084 {
19085 if (s > tail)
19086 {
19087 *fnamelen += (int)(*fnamep - (s + 1));
19088 *fnamep = s + 1;
19089#ifdef VMS
19090 /* cut version from the extension */
19091 s = *fnamep + *fnamelen - 1;
19092 for ( ; s > *fnamep; --s)
19093 if (s[0] == ';')
19094 break;
19095 if (s > *fnamep)
19096 *fnamelen = s - *fnamep;
19097#endif
19098 }
19099 else if (*fnamep <= tail)
19100 *fnamelen = 0;
19101 }
19102 else /* :r */
19103 {
19104 if (s > tail) /* remove one extension */
19105 *fnamelen = (int)(s - *fnamep);
19106 }
19107 *usedlen += 2;
19108 }
19109
19110 /* ":s?pat?foo?" - substitute */
19111 /* ":gs?pat?foo?" - global substitute */
19112 if (src[*usedlen] == ':'
19113 && (src[*usedlen + 1] == 's'
19114 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
19115 {
19116 char_u *str;
19117 char_u *pat;
19118 char_u *sub;
19119 int sep;
19120 char_u *flags;
19121 int didit = FALSE;
19122
19123 flags = (char_u *)"";
19124 s = src + *usedlen + 2;
19125 if (src[*usedlen + 1] == 'g')
19126 {
19127 flags = (char_u *)"g";
19128 ++s;
19129 }
19130
19131 sep = *s++;
19132 if (sep)
19133 {
19134 /* find end of pattern */
19135 p = vim_strchr(s, sep);
19136 if (p != NULL)
19137 {
19138 pat = vim_strnsave(s, (int)(p - s));
19139 if (pat != NULL)
19140 {
19141 s = p + 1;
19142 /* find end of substitution */
19143 p = vim_strchr(s, sep);
19144 if (p != NULL)
19145 {
19146 sub = vim_strnsave(s, (int)(p - s));
19147 str = vim_strnsave(*fnamep, *fnamelen);
19148 if (sub != NULL && str != NULL)
19149 {
19150 *usedlen = (int)(p + 1 - src);
19151 s = do_string_sub(str, pat, sub, flags);
19152 if (s != NULL)
19153 {
19154 *fnamep = s;
19155 *fnamelen = (int)STRLEN(s);
19156 vim_free(*bufp);
19157 *bufp = s;
19158 didit = TRUE;
19159 }
19160 }
19161 vim_free(sub);
19162 vim_free(str);
19163 }
19164 vim_free(pat);
19165 }
19166 }
19167 /* after using ":s", repeat all the modifiers */
19168 if (didit)
19169 goto repeat;
19170 }
19171 }
19172
19173 return valid;
19174}
19175
19176/*
19177 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
19178 * "flags" can be "g" to do a global substitute.
19179 * Returns an allocated string, NULL for error.
19180 */
19181 char_u *
19182do_string_sub(str, pat, sub, flags)
19183 char_u *str;
19184 char_u *pat;
19185 char_u *sub;
19186 char_u *flags;
19187{
19188 int sublen;
19189 regmatch_T regmatch;
19190 int i;
19191 int do_all;
19192 char_u *tail;
19193 garray_T ga;
19194 char_u *ret;
19195 char_u *save_cpo;
19196
19197 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
19198 save_cpo = p_cpo;
19199 p_cpo = (char_u *)"";
19200
19201 ga_init2(&ga, 1, 200);
19202
19203 do_all = (flags[0] == 'g');
19204
19205 regmatch.rm_ic = p_ic;
19206 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19207 if (regmatch.regprog != NULL)
19208 {
19209 tail = str;
19210 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
19211 {
19212 /*
19213 * Get some space for a temporary buffer to do the substitution
19214 * into. It will contain:
19215 * - The text up to where the match is.
19216 * - The substituted text.
19217 * - The text after the match.
19218 */
19219 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
19220 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
19221 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
19222 {
19223 ga_clear(&ga);
19224 break;
19225 }
19226
19227 /* copy the text up to where the match is */
19228 i = (int)(regmatch.startp[0] - tail);
19229 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
19230 /* add the substituted text */
19231 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
19232 + ga.ga_len + i, TRUE, TRUE, FALSE);
19233 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019234 /* avoid getting stuck on a match with an empty string */
19235 if (tail == regmatch.endp[0])
19236 {
19237 if (*tail == NUL)
19238 break;
19239 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
19240 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019241 }
19242 else
19243 {
19244 tail = regmatch.endp[0];
19245 if (*tail == NUL)
19246 break;
19247 }
19248 if (!do_all)
19249 break;
19250 }
19251
19252 if (ga.ga_data != NULL)
19253 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
19254
19255 vim_free(regmatch.regprog);
19256 }
19257
19258 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
19259 ga_clear(&ga);
19260 p_cpo = save_cpo;
19261
19262 return ret;
19263}
19264
19265#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */