blob: 2c512279acfa9c7fa36cd6bf97df58b0d87ad92a [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 Moolenaar80fc0432005-07-20 22:06:07 +00009081static void get_buffer_lines __ARGS((buf_T *buf, linenr_T start, linenr_T end, typval_T *rettv));
9082
9083/*
9084 * Get line or list of lines from buffer "buf" into "rettv".
9085 */
9086 static void
9087get_buffer_lines(buf, start, end, rettv)
9088 buf_T *buf;
9089 linenr_T start;
9090 linenr_T end;
9091 typval_T *rettv;
9092{
9093 char_u *p;
9094 list_T *l;
9095 listitem_T *li;
9096
9097 if (start < 0)
9098 rettv->vval.v_number = 0; /* failure; error message already given */
9099 else if (end == 0)
9100 {
9101 /* getline(lnum): return one line as a string */
9102 if (start >= 1 && start <= buf->b_ml.ml_line_count)
9103 p = ml_get_buf(buf, start, FALSE);
9104 else
9105 p = (char_u *)"";
9106
9107 rettv->v_type = VAR_STRING;
9108 rettv->vval.v_string = vim_strsave(p);
9109 }
9110 else
9111 {
9112 if (end < start)
9113 {
9114 if (end >= 0) /* else: error message already given */
9115 EMSG(_(e_invrange));
9116 rettv->vval.v_number = 0;
9117 }
9118 else
9119 {
9120 l = list_alloc();
9121 if (l != NULL)
9122 {
9123 if (start < 1)
9124 start = 1;
9125 if (end > buf->b_ml.ml_line_count)
9126 end = buf->b_ml.ml_line_count;
9127 while (start <= end)
9128 {
9129 li = listitem_alloc();
9130 if (li == NULL)
9131 break;
9132 list_append(l, li);
9133 li->li_tv.v_type = VAR_STRING;
9134 li->li_tv.v_lock = 0;
9135 li->li_tv.vval.v_string =
9136 vim_strsave(ml_get_buf(buf, start++, FALSE));
9137 }
9138 rettv->vval.v_list = l;
9139 rettv->v_type = VAR_LIST;
9140 ++l->lv_refcount;
9141 }
9142 }
9143 }
9144}
9145
9146/*
9147 * "getbufline()" function
9148 */
9149 static void
9150f_getbufline(argvars, rettv)
9151 typval_T *argvars;
9152 typval_T *rettv;
9153{
9154 linenr_T lnum;
9155 linenr_T end;
9156 buf_T *buf;
9157
9158 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9159 ++emsg_off;
9160 buf = get_buf_tv(&argvars[0]);
9161 --emsg_off;
9162
9163 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
9164 rettv->vval.v_number = 0;
9165 else
9166 {
9167 lnum = get_tv_lnum(&argvars[1]);
9168 if (argvars[2].v_type == VAR_UNKNOWN)
9169 end = lnum;
9170 else
9171 end = get_tv_lnum(&argvars[2]);
9172 get_buffer_lines(buf, lnum, end, rettv);
9173 }
9174}
9175
Bram Moolenaar0d660222005-01-07 21:51:51 +00009176/*
9177 * "getbufvar()" function
9178 */
9179 static void
9180f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009181 typval_T *argvars;
9182 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009183{
9184 buf_T *buf;
9185 buf_T *save_curbuf;
9186 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009187 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009188
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009189 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9190 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009191 ++emsg_off;
9192 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009193
9194 rettv->v_type = VAR_STRING;
9195 rettv->vval.v_string = NULL;
9196
9197 if (buf != NULL && varname != NULL)
9198 {
9199 if (*varname == '&') /* buffer-local-option */
9200 {
9201 /* set curbuf to be our buf, temporarily */
9202 save_curbuf = curbuf;
9203 curbuf = buf;
9204
9205 get_option_tv(&varname, rettv, TRUE);
9206
9207 /* restore previous notion of curbuf */
9208 curbuf = save_curbuf;
9209 }
9210 else
9211 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009212 if (*varname == NUL)
9213 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9214 * scope prefix before the NUL byte is required by
9215 * find_var_in_ht(). */
9216 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009217 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009218 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009219 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009220 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009221 }
9222 }
9223
9224 --emsg_off;
9225}
9226
9227/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009228 * "getchar()" function
9229 */
9230 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009231f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009232 typval_T *argvars;
9233 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009234{
9235 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009236 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009237
9238 ++no_mapping;
9239 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009240 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009241 /* getchar(): blocking wait. */
9242 n = safe_vgetc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009243 else if (get_tv_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009244 /* getchar(1): only check if char avail */
9245 n = vpeekc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009246 else if (error || vpeekc() == NUL)
9247 /* illegal argument or getchar(0) and no char avail: return zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009248 n = 0;
9249 else
9250 /* getchar(0) and char avail: return char */
9251 n = safe_vgetc();
9252 --no_mapping;
9253 --allow_keys;
9254
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009255 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009256 if (IS_SPECIAL(n) || mod_mask != 0)
9257 {
9258 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9259 int i = 0;
9260
9261 /* Turn a special key into three bytes, plus modifier. */
9262 if (mod_mask != 0)
9263 {
9264 temp[i++] = K_SPECIAL;
9265 temp[i++] = KS_MODIFIER;
9266 temp[i++] = mod_mask;
9267 }
9268 if (IS_SPECIAL(n))
9269 {
9270 temp[i++] = K_SPECIAL;
9271 temp[i++] = K_SECOND(n);
9272 temp[i++] = K_THIRD(n);
9273 }
9274#ifdef FEAT_MBYTE
9275 else if (has_mbyte)
9276 i += (*mb_char2bytes)(n, temp + i);
9277#endif
9278 else
9279 temp[i++] = n;
9280 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009281 rettv->v_type = VAR_STRING;
9282 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009283 }
9284}
9285
9286/*
9287 * "getcharmod()" function
9288 */
9289/*ARGSUSED*/
9290 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009291f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009292 typval_T *argvars;
9293 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009294{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009295 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009296}
9297
9298/*
9299 * "getcmdline()" function
9300 */
9301/*ARGSUSED*/
9302 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009303f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009304 typval_T *argvars;
9305 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009306{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009307 rettv->v_type = VAR_STRING;
9308 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009309}
9310
9311/*
9312 * "getcmdpos()" function
9313 */
9314/*ARGSUSED*/
9315 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009316f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009317 typval_T *argvars;
9318 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009319{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009320 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009321}
9322
9323/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009324 * "getcwd()" function
9325 */
9326/*ARGSUSED*/
9327 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009328f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009329 typval_T *argvars;
9330 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009331{
9332 char_u cwd[MAXPATHL];
9333
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009334 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009335 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009336 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009337 else
9338 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009339 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009340#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009341 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 Moolenaar0d660222005-01-07 21:51:51 +00009553
9554 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009555 if (argvars[1].v_type == VAR_UNKNOWN)
9556 end = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009557 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009558 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009559
9560 get_buffer_lines(curbuf, lnum, end, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009561}
9562
9563/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00009564 * "getqflist()" function
9565 */
9566/*ARGSUSED*/
9567 static void
9568f_getqflist(argvars, rettv)
9569 typval_T *argvars;
9570 typval_T *rettv;
9571{
9572#ifdef FEAT_QUICKFIX
9573 list_T *l;
9574#endif
9575
9576 rettv->vval.v_number = FALSE;
9577#ifdef FEAT_QUICKFIX
9578 l = list_alloc();
9579 if (l != NULL)
9580 {
9581 if (get_errorlist(l) != FAIL)
9582 {
9583 rettv->vval.v_list = l;
9584 rettv->v_type = VAR_LIST;
9585 ++l->lv_refcount;
9586 }
9587 else
9588 list_free(l);
9589 }
9590#endif
9591}
9592
9593/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009594 * "getreg()" function
9595 */
9596 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009597f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009598 typval_T *argvars;
9599 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009600{
9601 char_u *strregname;
9602 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009603 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009604 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009605
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009606 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009607 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009608 strregname = get_tv_string_chk(&argvars[0]);
9609 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009610 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009611 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009614 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009615 regname = (strregname == NULL ? '"' : *strregname);
9616 if (regname == 0)
9617 regname = '"';
9618
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009619 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009620 rettv->vval.v_string = error ? NULL :
9621 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009622}
9623
9624/*
9625 * "getregtype()" function
9626 */
9627 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009628f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009629 typval_T *argvars;
9630 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009631{
9632 char_u *strregname;
9633 int regname;
9634 char_u buf[NUMBUFLEN + 2];
9635 long reglen = 0;
9636
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009637 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009638 {
9639 strregname = get_tv_string_chk(&argvars[0]);
9640 if (strregname == NULL) /* type error; errmsg already given */
9641 {
9642 rettv->v_type = VAR_STRING;
9643 rettv->vval.v_string = NULL;
9644 return;
9645 }
9646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009647 else
9648 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009649 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009650
9651 regname = (strregname == NULL ? '"' : *strregname);
9652 if (regname == 0)
9653 regname = '"';
9654
9655 buf[0] = NUL;
9656 buf[1] = NUL;
9657 switch (get_reg_type(regname, &reglen))
9658 {
9659 case MLINE: buf[0] = 'V'; break;
9660 case MCHAR: buf[0] = 'v'; break;
9661#ifdef FEAT_VISUAL
9662 case MBLOCK:
9663 buf[0] = Ctrl_V;
9664 sprintf((char *)buf + 1, "%ld", reglen + 1);
9665 break;
9666#endif
9667 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009668 rettv->v_type = VAR_STRING;
9669 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009670}
9671
9672/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009673 * "getwinposx()" function
9674 */
9675/*ARGSUSED*/
9676 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009677f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009678 typval_T *argvars;
9679 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009680{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009681 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009682#ifdef FEAT_GUI
9683 if (gui.in_use)
9684 {
9685 int x, y;
9686
9687 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009688 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009689 }
9690#endif
9691}
9692
9693/*
9694 * "getwinposy()" function
9695 */
9696/*ARGSUSED*/
9697 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009698f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009699 typval_T *argvars;
9700 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009702 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009703#ifdef FEAT_GUI
9704 if (gui.in_use)
9705 {
9706 int x, y;
9707
9708 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009709 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009710 }
9711#endif
9712}
9713
Bram Moolenaara40058a2005-07-11 22:42:07 +00009714static win_T *find_win_by_nr __ARGS((typval_T *vp));
9715
9716 static win_T *
9717find_win_by_nr(vp)
9718 typval_T *vp;
9719{
9720#ifdef FEAT_WINDOWS
9721 win_T *wp;
9722#endif
9723 int nr;
9724
9725 nr = get_tv_number_chk(vp, NULL);
9726
9727#ifdef FEAT_WINDOWS
9728 if (nr < 0)
9729 return NULL;
9730 if (nr == 0)
9731 return curwin;
9732
9733 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9734 if (--nr <= 0)
9735 break;
9736 return wp;
9737#else
9738 if (nr == 0 || nr == 1)
9739 return curwin;
9740 return NULL;
9741#endif
9742}
9743
Bram Moolenaar071d4272004-06-13 20:20:40 +00009744/*
9745 * "getwinvar()" function
9746 */
9747 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009748f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009749 typval_T *argvars;
9750 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009751{
9752 win_T *win, *oldcurwin;
9753 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009754 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009755
Bram Moolenaar071d4272004-06-13 20:20:40 +00009756 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009757 varname = get_tv_string_chk(&argvars[1]);
9758 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009759
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009760 rettv->v_type = VAR_STRING;
9761 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009762
9763 if (win != NULL && varname != NULL)
9764 {
9765 if (*varname == '&') /* window-local-option */
9766 {
Bram Moolenaarc0761132005-03-18 20:30:32 +00009767 /* Set curwin to be our win, temporarily. Also set curbuf, so
9768 * that we can get buffer-local options. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009769 oldcurwin = curwin;
9770 curwin = win;
Bram Moolenaarc0761132005-03-18 20:30:32 +00009771 curbuf = win->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009772
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009773 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009774
9775 /* restore previous notion of curwin */
9776 curwin = oldcurwin;
Bram Moolenaarc0761132005-03-18 20:30:32 +00009777 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009778 }
9779 else
9780 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009781 if (*varname == NUL)
9782 /* let getwinvar({nr}, "") return the "w:" dictionary. The
9783 * scope prefix before the NUL byte is required by
9784 * find_var_in_ht(). */
9785 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009786 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009787 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009789 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009790 }
9791 }
9792
9793 --emsg_off;
9794}
9795
9796/*
9797 * "glob()" function
9798 */
9799 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009800f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009801 typval_T *argvars;
9802 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009803{
9804 expand_T xpc;
9805
9806 ExpandInit(&xpc);
9807 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009808 rettv->v_type = VAR_STRING;
9809 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009810 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
9811 ExpandCleanup(&xpc);
9812}
9813
9814/*
9815 * "globpath()" function
9816 */
9817 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009818f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009819 typval_T *argvars;
9820 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009821{
9822 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009823 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009824
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009825 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009826 if (file == NULL)
9827 rettv->vval.v_string = NULL;
9828 else
9829 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009830}
9831
9832/*
9833 * "has()" function
9834 */
9835 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009836f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009837 typval_T *argvars;
9838 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009839{
9840 int i;
9841 char_u *name;
9842 int n = FALSE;
9843 static char *(has_list[]) =
9844 {
9845#ifdef AMIGA
9846 "amiga",
9847# ifdef FEAT_ARP
9848 "arp",
9849# endif
9850#endif
9851#ifdef __BEOS__
9852 "beos",
9853#endif
9854#ifdef MSDOS
9855# ifdef DJGPP
9856 "dos32",
9857# else
9858 "dos16",
9859# endif
9860#endif
9861#ifdef MACOS /* TODO: Should we add MACOS_CLASSIC, MACOS_X? (Dany) */
9862 "mac",
9863#endif
9864#if defined(MACOS_X_UNIX)
9865 "macunix",
9866#endif
9867#ifdef OS2
9868 "os2",
9869#endif
9870#ifdef __QNX__
9871 "qnx",
9872#endif
9873#ifdef RISCOS
9874 "riscos",
9875#endif
9876#ifdef UNIX
9877 "unix",
9878#endif
9879#ifdef VMS
9880 "vms",
9881#endif
9882#ifdef WIN16
9883 "win16",
9884#endif
9885#ifdef WIN32
9886 "win32",
9887#endif
9888#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
9889 "win32unix",
9890#endif
9891#ifdef WIN64
9892 "win64",
9893#endif
9894#ifdef EBCDIC
9895 "ebcdic",
9896#endif
9897#ifndef CASE_INSENSITIVE_FILENAME
9898 "fname_case",
9899#endif
9900#ifdef FEAT_ARABIC
9901 "arabic",
9902#endif
9903#ifdef FEAT_AUTOCMD
9904 "autocmd",
9905#endif
9906#ifdef FEAT_BEVAL
9907 "balloon_eval",
9908#endif
9909#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
9910 "builtin_terms",
9911# ifdef ALL_BUILTIN_TCAPS
9912 "all_builtin_terms",
9913# endif
9914#endif
9915#ifdef FEAT_BYTEOFF
9916 "byte_offset",
9917#endif
9918#ifdef FEAT_CINDENT
9919 "cindent",
9920#endif
9921#ifdef FEAT_CLIENTSERVER
9922 "clientserver",
9923#endif
9924#ifdef FEAT_CLIPBOARD
9925 "clipboard",
9926#endif
9927#ifdef FEAT_CMDL_COMPL
9928 "cmdline_compl",
9929#endif
9930#ifdef FEAT_CMDHIST
9931 "cmdline_hist",
9932#endif
9933#ifdef FEAT_COMMENTS
9934 "comments",
9935#endif
9936#ifdef FEAT_CRYPT
9937 "cryptv",
9938#endif
9939#ifdef FEAT_CSCOPE
9940 "cscope",
9941#endif
9942#ifdef DEBUG
9943 "debug",
9944#endif
9945#ifdef FEAT_CON_DIALOG
9946 "dialog_con",
9947#endif
9948#ifdef FEAT_GUI_DIALOG
9949 "dialog_gui",
9950#endif
9951#ifdef FEAT_DIFF
9952 "diff",
9953#endif
9954#ifdef FEAT_DIGRAPHS
9955 "digraphs",
9956#endif
9957#ifdef FEAT_DND
9958 "dnd",
9959#endif
9960#ifdef FEAT_EMACS_TAGS
9961 "emacs_tags",
9962#endif
9963 "eval", /* always present, of course! */
9964#ifdef FEAT_EX_EXTRA
9965 "ex_extra",
9966#endif
9967#ifdef FEAT_SEARCH_EXTRA
9968 "extra_search",
9969#endif
9970#ifdef FEAT_FKMAP
9971 "farsi",
9972#endif
9973#ifdef FEAT_SEARCHPATH
9974 "file_in_path",
9975#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009976#if defined(UNIX) && !defined(USE_SYSTEM)
9977 "filterpipe",
9978#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009979#ifdef FEAT_FIND_ID
9980 "find_in_path",
9981#endif
9982#ifdef FEAT_FOLDING
9983 "folding",
9984#endif
9985#ifdef FEAT_FOOTER
9986 "footer",
9987#endif
9988#if !defined(USE_SYSTEM) && defined(UNIX)
9989 "fork",
9990#endif
9991#ifdef FEAT_GETTEXT
9992 "gettext",
9993#endif
9994#ifdef FEAT_GUI
9995 "gui",
9996#endif
9997#ifdef FEAT_GUI_ATHENA
9998# ifdef FEAT_GUI_NEXTAW
9999 "gui_neXtaw",
10000# else
10001 "gui_athena",
10002# endif
10003#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +000010004#ifdef FEAT_GUI_KDE
10005 "gui_kde",
10006#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010007#ifdef FEAT_GUI_GTK
10008 "gui_gtk",
10009# ifdef HAVE_GTK2
10010 "gui_gtk2",
10011# endif
10012#endif
10013#ifdef FEAT_GUI_MAC
10014 "gui_mac",
10015#endif
10016#ifdef FEAT_GUI_MOTIF
10017 "gui_motif",
10018#endif
10019#ifdef FEAT_GUI_PHOTON
10020 "gui_photon",
10021#endif
10022#ifdef FEAT_GUI_W16
10023 "gui_win16",
10024#endif
10025#ifdef FEAT_GUI_W32
10026 "gui_win32",
10027#endif
10028#ifdef FEAT_HANGULIN
10029 "hangul_input",
10030#endif
10031#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10032 "iconv",
10033#endif
10034#ifdef FEAT_INS_EXPAND
10035 "insert_expand",
10036#endif
10037#ifdef FEAT_JUMPLIST
10038 "jumplist",
10039#endif
10040#ifdef FEAT_KEYMAP
10041 "keymap",
10042#endif
10043#ifdef FEAT_LANGMAP
10044 "langmap",
10045#endif
10046#ifdef FEAT_LIBCALL
10047 "libcall",
10048#endif
10049#ifdef FEAT_LINEBREAK
10050 "linebreak",
10051#endif
10052#ifdef FEAT_LISP
10053 "lispindent",
10054#endif
10055#ifdef FEAT_LISTCMDS
10056 "listcmds",
10057#endif
10058#ifdef FEAT_LOCALMAP
10059 "localmap",
10060#endif
10061#ifdef FEAT_MENU
10062 "menu",
10063#endif
10064#ifdef FEAT_SESSION
10065 "mksession",
10066#endif
10067#ifdef FEAT_MODIFY_FNAME
10068 "modify_fname",
10069#endif
10070#ifdef FEAT_MOUSE
10071 "mouse",
10072#endif
10073#ifdef FEAT_MOUSESHAPE
10074 "mouseshape",
10075#endif
10076#if defined(UNIX) || defined(VMS)
10077# ifdef FEAT_MOUSE_DEC
10078 "mouse_dec",
10079# endif
10080# ifdef FEAT_MOUSE_GPM
10081 "mouse_gpm",
10082# endif
10083# ifdef FEAT_MOUSE_JSB
10084 "mouse_jsbterm",
10085# endif
10086# ifdef FEAT_MOUSE_NET
10087 "mouse_netterm",
10088# endif
10089# ifdef FEAT_MOUSE_PTERM
10090 "mouse_pterm",
10091# endif
10092# ifdef FEAT_MOUSE_XTERM
10093 "mouse_xterm",
10094# endif
10095#endif
10096#ifdef FEAT_MBYTE
10097 "multi_byte",
10098#endif
10099#ifdef FEAT_MBYTE_IME
10100 "multi_byte_ime",
10101#endif
10102#ifdef FEAT_MULTI_LANG
10103 "multi_lang",
10104#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010105#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010106#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010107 "mzscheme",
10108#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010109#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010110#ifdef FEAT_OLE
10111 "ole",
10112#endif
10113#ifdef FEAT_OSFILETYPE
10114 "osfiletype",
10115#endif
10116#ifdef FEAT_PATH_EXTRA
10117 "path_extra",
10118#endif
10119#ifdef FEAT_PERL
10120#ifndef DYNAMIC_PERL
10121 "perl",
10122#endif
10123#endif
10124#ifdef FEAT_PYTHON
10125#ifndef DYNAMIC_PYTHON
10126 "python",
10127#endif
10128#endif
10129#ifdef FEAT_POSTSCRIPT
10130 "postscript",
10131#endif
10132#ifdef FEAT_PRINTER
10133 "printer",
10134#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000010135#ifdef FEAT_PROFILE
10136 "profile",
10137#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010138#ifdef FEAT_QUICKFIX
10139 "quickfix",
10140#endif
10141#ifdef FEAT_RIGHTLEFT
10142 "rightleft",
10143#endif
10144#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
10145 "ruby",
10146#endif
10147#ifdef FEAT_SCROLLBIND
10148 "scrollbind",
10149#endif
10150#ifdef FEAT_CMDL_INFO
10151 "showcmd",
10152 "cmdline_info",
10153#endif
10154#ifdef FEAT_SIGNS
10155 "signs",
10156#endif
10157#ifdef FEAT_SMARTINDENT
10158 "smartindent",
10159#endif
10160#ifdef FEAT_SNIFF
10161 "sniff",
10162#endif
10163#ifdef FEAT_STL_OPT
10164 "statusline",
10165#endif
10166#ifdef FEAT_SUN_WORKSHOP
10167 "sun_workshop",
10168#endif
10169#ifdef FEAT_NETBEANS_INTG
10170 "netbeans_intg",
10171#endif
10172#ifdef FEAT_SYN_HL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010173 "spell",
10174#endif
10175#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010176 "syntax",
10177#endif
10178#if defined(USE_SYSTEM) || !defined(UNIX)
10179 "system",
10180#endif
10181#ifdef FEAT_TAG_BINS
10182 "tag_binary",
10183#endif
10184#ifdef FEAT_TAG_OLDSTATIC
10185 "tag_old_static",
10186#endif
10187#ifdef FEAT_TAG_ANYWHITE
10188 "tag_any_white",
10189#endif
10190#ifdef FEAT_TCL
10191# ifndef DYNAMIC_TCL
10192 "tcl",
10193# endif
10194#endif
10195#ifdef TERMINFO
10196 "terminfo",
10197#endif
10198#ifdef FEAT_TERMRESPONSE
10199 "termresponse",
10200#endif
10201#ifdef FEAT_TEXTOBJ
10202 "textobjects",
10203#endif
10204#ifdef HAVE_TGETENT
10205 "tgetent",
10206#endif
10207#ifdef FEAT_TITLE
10208 "title",
10209#endif
10210#ifdef FEAT_TOOLBAR
10211 "toolbar",
10212#endif
10213#ifdef FEAT_USR_CMDS
10214 "user-commands", /* was accidentally included in 5.4 */
10215 "user_commands",
10216#endif
10217#ifdef FEAT_VIMINFO
10218 "viminfo",
10219#endif
10220#ifdef FEAT_VERTSPLIT
10221 "vertsplit",
10222#endif
10223#ifdef FEAT_VIRTUALEDIT
10224 "virtualedit",
10225#endif
10226#ifdef FEAT_VISUAL
10227 "visual",
10228#endif
10229#ifdef FEAT_VISUALEXTRA
10230 "visualextra",
10231#endif
10232#ifdef FEAT_VREPLACE
10233 "vreplace",
10234#endif
10235#ifdef FEAT_WILDIGN
10236 "wildignore",
10237#endif
10238#ifdef FEAT_WILDMENU
10239 "wildmenu",
10240#endif
10241#ifdef FEAT_WINDOWS
10242 "windows",
10243#endif
10244#ifdef FEAT_WAK
10245 "winaltkeys",
10246#endif
10247#ifdef FEAT_WRITEBACKUP
10248 "writebackup",
10249#endif
10250#ifdef FEAT_XIM
10251 "xim",
10252#endif
10253#ifdef FEAT_XFONTSET
10254 "xfontset",
10255#endif
10256#ifdef USE_XSMP
10257 "xsmp",
10258#endif
10259#ifdef USE_XSMP_INTERACT
10260 "xsmp_interact",
10261#endif
10262#ifdef FEAT_XCLIPBOARD
10263 "xterm_clipboard",
10264#endif
10265#ifdef FEAT_XTERM_SAVE
10266 "xterm_save",
10267#endif
10268#if defined(UNIX) && defined(FEAT_X11)
10269 "X11",
10270#endif
10271 NULL
10272 };
10273
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010274 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010275 for (i = 0; has_list[i] != NULL; ++i)
10276 if (STRICMP(name, has_list[i]) == 0)
10277 {
10278 n = TRUE;
10279 break;
10280 }
10281
10282 if (n == FALSE)
10283 {
10284 if (STRNICMP(name, "patch", 5) == 0)
10285 n = has_patch(atoi((char *)name + 5));
10286 else if (STRICMP(name, "vim_starting") == 0)
10287 n = (starting != 0);
10288#ifdef DYNAMIC_TCL
10289 else if (STRICMP(name, "tcl") == 0)
10290 n = tcl_enabled(FALSE);
10291#endif
10292#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
10293 else if (STRICMP(name, "iconv") == 0)
10294 n = iconv_enabled(FALSE);
10295#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010296#ifdef DYNAMIC_MZSCHEME
10297 else if (STRICMP(name, "mzscheme") == 0)
10298 n = mzscheme_enabled(FALSE);
10299#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010300#ifdef DYNAMIC_RUBY
10301 else if (STRICMP(name, "ruby") == 0)
10302 n = ruby_enabled(FALSE);
10303#endif
10304#ifdef DYNAMIC_PYTHON
10305 else if (STRICMP(name, "python") == 0)
10306 n = python_enabled(FALSE);
10307#endif
10308#ifdef DYNAMIC_PERL
10309 else if (STRICMP(name, "perl") == 0)
10310 n = perl_enabled(FALSE);
10311#endif
10312#ifdef FEAT_GUI
10313 else if (STRICMP(name, "gui_running") == 0)
10314 n = (gui.in_use || gui.starting);
10315# ifdef FEAT_GUI_W32
10316 else if (STRICMP(name, "gui_win32s") == 0)
10317 n = gui_is_win32s();
10318# endif
10319# ifdef FEAT_BROWSE
10320 else if (STRICMP(name, "browse") == 0)
10321 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
10322# endif
10323#endif
10324#ifdef FEAT_SYN_HL
10325 else if (STRICMP(name, "syntax_items") == 0)
10326 n = syntax_present(curbuf);
10327#endif
10328#if defined(WIN3264)
10329 else if (STRICMP(name, "win95") == 0)
10330 n = mch_windows95();
10331#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000010332#ifdef FEAT_NETBEANS_INTG
10333 else if (STRICMP(name, "netbeans_enabled") == 0)
10334 n = usingNetbeans;
10335#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010336 }
10337
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010338 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010339}
10340
10341/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000010342 * "has_key()" function
10343 */
10344 static void
10345f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010346 typval_T *argvars;
10347 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010348{
10349 rettv->vval.v_number = 0;
10350 if (argvars[0].v_type != VAR_DICT)
10351 {
10352 EMSG(_(e_dictreq));
10353 return;
10354 }
10355 if (argvars[0].vval.v_dict == NULL)
10356 return;
10357
10358 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010359 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010360}
10361
10362/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010363 * "hasmapto()" function
10364 */
10365 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010366f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010367 typval_T *argvars;
10368 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010369{
10370 char_u *name;
10371 char_u *mode;
10372 char_u buf[NUMBUFLEN];
10373
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010374 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010375 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010376 mode = (char_u *)"nvo";
10377 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010378 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010379
10380 if (map_to_exists(name, mode))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010381 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010382 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010383 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010384}
10385
10386/*
10387 * "histadd()" function
10388 */
10389/*ARGSUSED*/
10390 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010391f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010392 typval_T *argvars;
10393 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010394{
10395#ifdef FEAT_CMDHIST
10396 int histype;
10397 char_u *str;
10398 char_u buf[NUMBUFLEN];
10399#endif
10400
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010401 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010402 if (check_restricted() || check_secure())
10403 return;
10404#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010405 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10406 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010407 if (histype >= 0)
10408 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010409 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010410 if (*str != NUL)
10411 {
10412 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010413 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010414 return;
10415 }
10416 }
10417#endif
10418}
10419
10420/*
10421 * "histdel()" function
10422 */
10423/*ARGSUSED*/
10424 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010425f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010426 typval_T *argvars;
10427 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010428{
10429#ifdef FEAT_CMDHIST
10430 int n;
10431 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010432 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010433
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010434 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10435 if (str == NULL)
10436 n = 0;
10437 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010438 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010439 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010440 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010441 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010442 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010443 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010444 else
10445 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010446 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010447 get_tv_string_buf(&argvars[1], buf));
10448 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010449#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010450 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010451#endif
10452}
10453
10454/*
10455 * "histget()" function
10456 */
10457/*ARGSUSED*/
10458 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010459f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010460 typval_T *argvars;
10461 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010462{
10463#ifdef FEAT_CMDHIST
10464 int type;
10465 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010466 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010467
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010468 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10469 if (str == NULL)
10470 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010471 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010472 {
10473 type = get_histtype(str);
10474 if (argvars[1].v_type == VAR_UNKNOWN)
10475 idx = get_history_idx(type);
10476 else
10477 idx = (int)get_tv_number_chk(&argvars[1], NULL);
10478 /* -1 on type error */
10479 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
10480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010481#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010482 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010483#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010484 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010485}
10486
10487/*
10488 * "histnr()" function
10489 */
10490/*ARGSUSED*/
10491 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010492f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010493 typval_T *argvars;
10494 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010495{
10496 int i;
10497
10498#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010499 char_u *history = get_tv_string_chk(&argvars[0]);
10500
10501 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010502 if (i >= HIST_CMD && i < HIST_COUNT)
10503 i = get_history_idx(i);
10504 else
10505#endif
10506 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010507 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010508}
10509
10510/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010511 * "highlightID(name)" function
10512 */
10513 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010514f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010515 typval_T *argvars;
10516 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010517{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010518 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010519}
10520
10521/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010522 * "highlight_exists()" function
10523 */
10524 static void
10525f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010526 typval_T *argvars;
10527 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010528{
10529 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
10530}
10531
10532/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010533 * "hostname()" function
10534 */
10535/*ARGSUSED*/
10536 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010537f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010538 typval_T *argvars;
10539 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010540{
10541 char_u hostname[256];
10542
10543 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010544 rettv->v_type = VAR_STRING;
10545 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010546}
10547
10548/*
10549 * iconv() function
10550 */
10551/*ARGSUSED*/
10552 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010553f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010554 typval_T *argvars;
10555 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010556{
10557#ifdef FEAT_MBYTE
10558 char_u buf1[NUMBUFLEN];
10559 char_u buf2[NUMBUFLEN];
10560 char_u *from, *to, *str;
10561 vimconv_T vimconv;
10562#endif
10563
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010564 rettv->v_type = VAR_STRING;
10565 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010566
10567#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010568 str = get_tv_string(&argvars[0]);
10569 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
10570 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010571 vimconv.vc_type = CONV_NONE;
10572 convert_setup(&vimconv, from, to);
10573
10574 /* If the encodings are equal, no conversion needed. */
10575 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010576 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010577 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010578 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010579
10580 convert_setup(&vimconv, NULL, NULL);
10581 vim_free(from);
10582 vim_free(to);
10583#endif
10584}
10585
10586/*
10587 * "indent()" function
10588 */
10589 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010590f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010591 typval_T *argvars;
10592 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010593{
10594 linenr_T lnum;
10595
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010596 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010597 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010598 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010599 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010600 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010601}
10602
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010603/*
10604 * "index()" function
10605 */
10606 static void
10607f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010608 typval_T *argvars;
10609 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010610{
Bram Moolenaar33570922005-01-25 22:26:29 +000010611 list_T *l;
10612 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010613 long idx = 0;
10614 int ic = FALSE;
10615
10616 rettv->vval.v_number = -1;
10617 if (argvars[0].v_type != VAR_LIST)
10618 {
10619 EMSG(_(e_listreq));
10620 return;
10621 }
10622 l = argvars[0].vval.v_list;
10623 if (l != NULL)
10624 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010625 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010626 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010627 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010628 int error = FALSE;
10629
Bram Moolenaar758711c2005-02-02 23:11:38 +000010630 /* Start at specified item. Use the cached index that list_find()
10631 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010632 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000010633 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010634 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010635 ic = get_tv_number_chk(&argvars[3], &error);
10636 if (error)
10637 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010638 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010639
Bram Moolenaar758711c2005-02-02 23:11:38 +000010640 for ( ; item != NULL; item = item->li_next, ++idx)
10641 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010642 {
10643 rettv->vval.v_number = idx;
10644 break;
10645 }
10646 }
10647}
10648
Bram Moolenaar071d4272004-06-13 20:20:40 +000010649static int inputsecret_flag = 0;
10650
10651/*
10652 * "input()" function
10653 * Also handles inputsecret() when inputsecret is set.
10654 */
10655 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010656f_input(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010657 typval_T *argvars;
10658 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010659{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010660 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010661 char_u *p = NULL;
10662 int c;
10663 char_u buf[NUMBUFLEN];
10664 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010665 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000010666
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010667 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010668
10669#ifdef NO_CONSOLE_INPUT
10670 /* While starting up, there is no place to enter text. */
10671 if (no_console_input())
10672 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010673 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010674 return;
10675 }
10676#endif
10677
10678 cmd_silent = FALSE; /* Want to see the prompt. */
10679 if (prompt != NULL)
10680 {
10681 /* Only the part of the message after the last NL is considered as
10682 * prompt for the command line */
10683 p = vim_strrchr(prompt, '\n');
10684 if (p == NULL)
10685 p = prompt;
10686 else
10687 {
10688 ++p;
10689 c = *p;
10690 *p = NUL;
10691 msg_start();
10692 msg_clr_eos();
10693 msg_puts_attr(prompt, echo_attr);
10694 msg_didout = FALSE;
10695 msg_starthere();
10696 *p = c;
10697 }
10698 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010699
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010700 if (argvars[1].v_type != VAR_UNKNOWN)
10701 {
10702 defstr = get_tv_string_buf_chk(&argvars[1], buf);
10703 if (defstr != NULL)
10704 stuffReadbuffSpec(defstr);
10705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010706
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010707 if (defstr != NULL)
10708 rettv->vval.v_string =
Bram Moolenaar071d4272004-06-13 20:20:40 +000010709 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr);
10710
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010711 /* since the user typed this, no need to wait for return */
10712 need_wait_return = FALSE;
10713 msg_didout = FALSE;
10714 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010715 cmd_silent = cmd_silent_save;
10716}
10717
10718/*
10719 * "inputdialog()" function
10720 */
10721 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010722f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010723 typval_T *argvars;
10724 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010725{
10726#if defined(FEAT_GUI_TEXTDIALOG)
10727 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
10728 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
10729 {
10730 char_u *message;
10731 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010732 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000010733
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010734 message = get_tv_string_chk(&argvars[0]);
10735 if (argvars[1].v_type != VAR_UNKNOWN
10736 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000010737 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010738 else
10739 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010740 if (message != NULL && defstr != NULL
10741 && do_dialog(VIM_QUESTION, NULL, message,
10742 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010743 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010744 else
10745 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010746 if (message != NULL && defstr != NULL
10747 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010748 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010749 rettv->vval.v_string = vim_strsave(
10750 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010751 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010752 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010753 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010754 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010755 }
10756 else
10757#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010758 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010759}
10760
10761static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
10762
10763/*
10764 * "inputrestore()" function
10765 */
10766/*ARGSUSED*/
10767 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010768f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010769 typval_T *argvars;
10770 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010771{
10772 if (ga_userinput.ga_len > 0)
10773 {
10774 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010775 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
10776 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010777 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010778 }
10779 else if (p_verbose > 1)
10780 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000010781 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010782 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010783 }
10784}
10785
10786/*
10787 * "inputsave()" function
10788 */
10789/*ARGSUSED*/
10790 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010791f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010792 typval_T *argvars;
10793 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010794{
10795 /* Add an entry to the stack of typehead storage. */
10796 if (ga_grow(&ga_userinput, 1) == OK)
10797 {
10798 save_typeahead((tasave_T *)(ga_userinput.ga_data)
10799 + ga_userinput.ga_len);
10800 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010801 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010802 }
10803 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010804 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010805}
10806
10807/*
10808 * "inputsecret()" function
10809 */
10810 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010811f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010812 typval_T *argvars;
10813 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010814{
10815 ++cmdline_star;
10816 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010817 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010818 --cmdline_star;
10819 --inputsecret_flag;
10820}
10821
10822/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010823 * "insert()" function
10824 */
10825 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010826f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010827 typval_T *argvars;
10828 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010829{
10830 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000010831 listitem_T *item;
10832 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010833 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010834
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010835 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010836 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010837 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010838 else if ((l = argvars[0].vval.v_list) != NULL
10839 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010840 {
10841 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010842 before = get_tv_number_chk(&argvars[2], &error);
10843 if (error)
10844 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010845
Bram Moolenaar758711c2005-02-02 23:11:38 +000010846 if (before == l->lv_len)
10847 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010848 else
10849 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010850 item = list_find(l, before);
10851 if (item == NULL)
10852 {
10853 EMSGN(_(e_listidx), before);
10854 l = NULL;
10855 }
10856 }
10857 if (l != NULL)
10858 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010859 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010860 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010861 }
10862 }
10863}
10864
10865/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010866 * "isdirectory()" function
10867 */
10868 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010869f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010870 typval_T *argvars;
10871 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010872{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010873 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010874}
10875
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010876/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010877 * "islocked()" function
10878 */
10879 static void
10880f_islocked(argvars, rettv)
10881 typval_T *argvars;
10882 typval_T *rettv;
10883{
10884 lval_T lv;
10885 char_u *end;
10886 dictitem_T *di;
10887
10888 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000010889 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
10890 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010891 if (end != NULL && lv.ll_name != NULL)
10892 {
10893 if (*end != NUL)
10894 EMSG(_(e_trailing));
10895 else
10896 {
10897 if (lv.ll_tv == NULL)
10898 {
10899 if (check_changedtick(lv.ll_name))
10900 rettv->vval.v_number = 1; /* always locked */
10901 else
10902 {
10903 di = find_var(lv.ll_name, NULL);
10904 if (di != NULL)
10905 {
10906 /* Consider a variable locked when:
10907 * 1. the variable itself is locked
10908 * 2. the value of the variable is locked.
10909 * 3. the List or Dict value is locked.
10910 */
10911 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
10912 || tv_islocked(&di->di_tv));
10913 }
10914 }
10915 }
10916 else if (lv.ll_range)
10917 EMSG(_("E745: Range not allowed"));
10918 else if (lv.ll_newkey != NULL)
10919 EMSG2(_(e_dictkey), lv.ll_newkey);
10920 else if (lv.ll_list != NULL)
10921 /* List item. */
10922 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
10923 else
10924 /* Dictionary item. */
10925 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
10926 }
10927 }
10928
10929 clear_lval(&lv);
10930}
10931
Bram Moolenaar33570922005-01-25 22:26:29 +000010932static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000010933
10934/*
10935 * Turn a dict into a list:
10936 * "what" == 0: list of keys
10937 * "what" == 1: list of values
10938 * "what" == 2: list of items
10939 */
10940 static void
10941dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000010942 typval_T *argvars;
10943 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010944 int what;
10945{
Bram Moolenaar33570922005-01-25 22:26:29 +000010946 list_T *l;
10947 list_T *l2;
10948 dictitem_T *di;
10949 hashitem_T *hi;
10950 listitem_T *li;
10951 listitem_T *li2;
10952 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010953 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010954
10955 rettv->vval.v_number = 0;
10956 if (argvars[0].v_type != VAR_DICT)
10957 {
10958 EMSG(_(e_dictreq));
10959 return;
10960 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010961 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000010962 return;
10963
10964 l = list_alloc();
10965 if (l == NULL)
10966 return;
10967 rettv->v_type = VAR_LIST;
10968 rettv->vval.v_list = l;
10969 ++l->lv_refcount;
10970
Bram Moolenaar33570922005-01-25 22:26:29 +000010971 todo = d->dv_hashtab.ht_used;
10972 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000010973 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010974 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000010975 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010976 --todo;
10977 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000010978
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010979 li = listitem_alloc();
10980 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000010981 break;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010982 list_append(l, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000010983
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010984 if (what == 0)
10985 {
10986 /* keys() */
10987 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010988 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010989 li->li_tv.vval.v_string = vim_strsave(di->di_key);
10990 }
10991 else if (what == 1)
10992 {
10993 /* values() */
10994 copy_tv(&di->di_tv, &li->li_tv);
10995 }
10996 else
10997 {
10998 /* items() */
10999 l2 = list_alloc();
11000 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011001 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011002 li->li_tv.vval.v_list = l2;
11003 if (l2 == NULL)
11004 break;
11005 ++l2->lv_refcount;
11006
11007 li2 = listitem_alloc();
11008 if (li2 == NULL)
11009 break;
11010 list_append(l2, li2);
11011 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011012 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011013 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
11014
11015 li2 = listitem_alloc();
11016 if (li2 == NULL)
11017 break;
11018 list_append(l2, li2);
11019 copy_tv(&di->di_tv, &li2->li_tv);
11020 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011021 }
11022 }
11023}
11024
11025/*
11026 * "items(dict)" function
11027 */
11028 static void
11029f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011030 typval_T *argvars;
11031 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011032{
11033 dict_list(argvars, rettv, 2);
11034}
11035
Bram Moolenaar071d4272004-06-13 20:20:40 +000011036/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011037 * "join()" function
11038 */
11039 static void
11040f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011041 typval_T *argvars;
11042 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011043{
11044 garray_T ga;
11045 char_u *sep;
11046
11047 rettv->vval.v_number = 0;
11048 if (argvars[0].v_type != VAR_LIST)
11049 {
11050 EMSG(_(e_listreq));
11051 return;
11052 }
11053 if (argvars[0].vval.v_list == NULL)
11054 return;
11055 if (argvars[1].v_type == VAR_UNKNOWN)
11056 sep = (char_u *)" ";
11057 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011058 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011059
11060 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011061
11062 if (sep != NULL)
11063 {
11064 ga_init2(&ga, (int)sizeof(char), 80);
11065 list_join(&ga, argvars[0].vval.v_list, sep, TRUE);
11066 ga_append(&ga, NUL);
11067 rettv->vval.v_string = (char_u *)ga.ga_data;
11068 }
11069 else
11070 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011071}
11072
11073/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011074 * "keys()" function
11075 */
11076 static void
11077f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011078 typval_T *argvars;
11079 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011080{
11081 dict_list(argvars, rettv, 0);
11082}
11083
11084/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011085 * "last_buffer_nr()" function.
11086 */
11087/*ARGSUSED*/
11088 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011089f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011090 typval_T *argvars;
11091 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011092{
11093 int n = 0;
11094 buf_T *buf;
11095
11096 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
11097 if (n < buf->b_fnum)
11098 n = buf->b_fnum;
11099
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011100 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011101}
11102
11103/*
11104 * "len()" function
11105 */
11106 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011107f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011108 typval_T *argvars;
11109 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011110{
11111 switch (argvars[0].v_type)
11112 {
11113 case VAR_STRING:
11114 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011115 rettv->vval.v_number = (varnumber_T)STRLEN(
11116 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011117 break;
11118 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011119 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011120 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011121 case VAR_DICT:
11122 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
11123 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011124 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011125 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011126 break;
11127 }
11128}
11129
Bram Moolenaar33570922005-01-25 22:26:29 +000011130static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011131
11132 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011133libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011134 typval_T *argvars;
11135 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011136 int type;
11137{
11138#ifdef FEAT_LIBCALL
11139 char_u *string_in;
11140 char_u **string_result;
11141 int nr_result;
11142#endif
11143
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011144 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011145 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011146 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011147 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011148 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011149
11150 if (check_restricted() || check_secure())
11151 return;
11152
11153#ifdef FEAT_LIBCALL
11154 /* The first two args must be strings, otherwise its meaningless */
11155 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
11156 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011157 string_in = NULL;
11158 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011159 string_in = argvars[2].vval.v_string;
11160 if (type == VAR_NUMBER)
11161 string_result = NULL;
11162 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011163 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011164 if (mch_libcall(argvars[0].vval.v_string,
11165 argvars[1].vval.v_string,
11166 string_in,
11167 argvars[2].vval.v_number,
11168 string_result,
11169 &nr_result) == OK
11170 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011171 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011172 }
11173#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011174}
11175
11176/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011177 * "libcall()" function
11178 */
11179 static void
11180f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011181 typval_T *argvars;
11182 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011183{
11184 libcall_common(argvars, rettv, VAR_STRING);
11185}
11186
11187/*
11188 * "libcallnr()" function
11189 */
11190 static void
11191f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011192 typval_T *argvars;
11193 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011194{
11195 libcall_common(argvars, rettv, VAR_NUMBER);
11196}
11197
11198/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011199 * "line(string)" function
11200 */
11201 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011202f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011203 typval_T *argvars;
11204 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011205{
11206 linenr_T lnum = 0;
11207 pos_T *fp;
11208
11209 fp = var2fpos(&argvars[0], TRUE);
11210 if (fp != NULL)
11211 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011212 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011213}
11214
11215/*
11216 * "line2byte(lnum)" function
11217 */
11218/*ARGSUSED*/
11219 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011220f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011221 typval_T *argvars;
11222 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011223{
11224#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011225 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011226#else
11227 linenr_T lnum;
11228
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011229 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011230 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011231 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011232 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011233 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
11234 if (rettv->vval.v_number >= 0)
11235 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011236#endif
11237}
11238
11239/*
11240 * "lispindent(lnum)" function
11241 */
11242 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011243f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011244 typval_T *argvars;
11245 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011246{
11247#ifdef FEAT_LISP
11248 pos_T pos;
11249 linenr_T lnum;
11250
11251 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011252 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011253 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11254 {
11255 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011256 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011257 curwin->w_cursor = pos;
11258 }
11259 else
11260#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011261 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011262}
11263
11264/*
11265 * "localtime()" function
11266 */
11267/*ARGSUSED*/
11268 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011269f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011270 typval_T *argvars;
11271 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011272{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011273 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011274}
11275
Bram Moolenaar33570922005-01-25 22:26:29 +000011276static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011277
11278 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011279get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000011280 typval_T *argvars;
11281 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011282 int exact;
11283{
11284 char_u *keys;
11285 char_u *which;
11286 char_u buf[NUMBUFLEN];
11287 char_u *keys_buf = NULL;
11288 char_u *rhs;
11289 int mode;
11290 garray_T ga;
11291
11292 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011293 rettv->v_type = VAR_STRING;
11294 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011295
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011296 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011297 if (*keys == NUL)
11298 return;
11299
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011300 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011301 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011302 else
11303 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011304 if (which == NULL)
11305 return;
11306
Bram Moolenaar071d4272004-06-13 20:20:40 +000011307 mode = get_map_mode(&which, 0);
11308
11309 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000011310 rhs = check_map(keys, mode, exact, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011311 vim_free(keys_buf);
11312 if (rhs != NULL)
11313 {
11314 ga_init(&ga);
11315 ga.ga_itemsize = 1;
11316 ga.ga_growsize = 40;
11317
11318 while (*rhs != NUL)
11319 ga_concat(&ga, str2special(&rhs, FALSE));
11320
11321 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011322 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011323 }
11324}
11325
11326/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011327 * "map()" function
11328 */
11329 static void
11330f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011331 typval_T *argvars;
11332 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011333{
11334 filter_map(argvars, rettv, TRUE);
11335}
11336
11337/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011338 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011339 */
11340 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011341f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011342 typval_T *argvars;
11343 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011344{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011345 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011346}
11347
11348/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011349 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011350 */
11351 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011352f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011353 typval_T *argvars;
11354 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011355{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011356 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011357}
11358
Bram Moolenaar33570922005-01-25 22:26:29 +000011359static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011360
11361 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011362find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011363 typval_T *argvars;
11364 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011365 int type;
11366{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011367 char_u *str = NULL;
11368 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011369 char_u *pat;
11370 regmatch_T regmatch;
11371 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011372 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011373 char_u *save_cpo;
11374 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011375 long nth = 1;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011376 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011377 list_T *l = NULL;
11378 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011379 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011380 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011381
11382 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11383 save_cpo = p_cpo;
11384 p_cpo = (char_u *)"";
11385
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011386 rettv->vval.v_number = -1;
11387 if (type == 3)
11388 {
11389 /* return empty list when there are no matches */
11390 if ((rettv->vval.v_list = list_alloc()) == NULL)
11391 goto theend;
11392 rettv->v_type = VAR_LIST;
11393 ++rettv->vval.v_list->lv_refcount;
11394 }
11395 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011396 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011397 rettv->v_type = VAR_STRING;
11398 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011399 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011400
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011401 if (argvars[0].v_type == VAR_LIST)
11402 {
11403 if ((l = argvars[0].vval.v_list) == NULL)
11404 goto theend;
11405 li = l->lv_first;
11406 }
11407 else
11408 expr = str = get_tv_string(&argvars[0]);
11409
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011410 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11411 if (pat == NULL)
11412 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011413
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011414 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011415 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011416 int error = FALSE;
11417
11418 start = get_tv_number_chk(&argvars[2], &error);
11419 if (error)
11420 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011421 if (l != NULL)
11422 {
11423 li = list_find(l, start);
11424 if (li == NULL)
11425 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000011426 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011427 }
11428 else
11429 {
11430 if (start < 0)
11431 start = 0;
11432 if (start > (long)STRLEN(str))
11433 goto theend;
11434 str += start;
11435 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011436
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011437 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011438 nth = get_tv_number_chk(&argvars[3], &error);
11439 if (error)
11440 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011441 }
11442
11443 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11444 if (regmatch.regprog != NULL)
11445 {
11446 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011447
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000011448 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011449 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011450 if (l != NULL)
11451 {
11452 if (li == NULL)
11453 {
11454 match = FALSE;
11455 break;
11456 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011457 vim_free(tofree);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011458 str = echo_string(&li->li_tv, &tofree, strbuf);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011459 if (str == NULL)
11460 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011461 }
11462
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011463 match = vim_regexec_nl(&regmatch, str, (colnr_T)0);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011464
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011465 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011466 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011467 if (l == NULL && !match)
11468 break;
11469
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011470 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011471 if (l != NULL)
11472 {
11473 li = li->li_next;
11474 ++idx;
11475 }
11476 else
11477 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011478#ifdef FEAT_MBYTE
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011479 str = regmatch.startp[0] + mb_ptr2len_check(regmatch.startp[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011480#else
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011481 str = regmatch.startp[0] + 1;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011482#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011483 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011484 }
11485
11486 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011487 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011488 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011489 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011490 int i;
11491
11492 /* return list with matched string and submatches */
11493 for (i = 0; i < NSUBEXP; ++i)
11494 {
11495 if (regmatch.endp[i] == NULL)
11496 break;
11497 li = listitem_alloc();
11498 if (li == NULL)
11499 break;
11500 li->li_tv.v_type = VAR_STRING;
11501 li->li_tv.v_lock = 0;
11502 li->li_tv.vval.v_string = vim_strnsave(regmatch.startp[i],
11503 (int)(regmatch.endp[i] - regmatch.startp[i]));
11504 list_append(rettv->vval.v_list, li);
11505 }
11506 }
11507 else if (type == 2)
11508 {
11509 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011510 if (l != NULL)
11511 copy_tv(&li->li_tv, rettv);
11512 else
11513 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000011514 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011515 }
11516 else if (l != NULL)
11517 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011518 else
11519 {
11520 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011521 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011522 (varnumber_T)(regmatch.startp[0] - str);
11523 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011524 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011525 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011526 rettv->vval.v_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011527 }
11528 }
11529 vim_free(regmatch.regprog);
11530 }
11531
11532theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011533 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011534 p_cpo = save_cpo;
11535}
11536
11537/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011538 * "match()" function
11539 */
11540 static void
11541f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011542 typval_T *argvars;
11543 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011544{
11545 find_some_match(argvars, rettv, 1);
11546}
11547
11548/*
11549 * "matchend()" function
11550 */
11551 static void
11552f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011553 typval_T *argvars;
11554 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011555{
11556 find_some_match(argvars, rettv, 0);
11557}
11558
11559/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011560 * "matchlist()" function
11561 */
11562 static void
11563f_matchlist(argvars, rettv)
11564 typval_T *argvars;
11565 typval_T *rettv;
11566{
11567 find_some_match(argvars, rettv, 3);
11568}
11569
11570/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011571 * "matchstr()" function
11572 */
11573 static void
11574f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011575 typval_T *argvars;
11576 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011577{
11578 find_some_match(argvars, rettv, 2);
11579}
11580
Bram Moolenaar33570922005-01-25 22:26:29 +000011581static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011582
11583 static void
11584max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000011585 typval_T *argvars;
11586 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011587 int domax;
11588{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011589 long n = 0;
11590 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011591 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011592
11593 if (argvars[0].v_type == VAR_LIST)
11594 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011595 list_T *l;
11596 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011597
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011598 l = argvars[0].vval.v_list;
11599 if (l != NULL)
11600 {
11601 li = l->lv_first;
11602 if (li != NULL)
11603 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011604 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000011605 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011606 {
11607 li = li->li_next;
11608 if (li == NULL)
11609 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011610 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011611 if (domax ? i > n : i < n)
11612 n = i;
11613 }
11614 }
11615 }
11616 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011617 else if (argvars[0].v_type == VAR_DICT)
11618 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011619 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011620 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000011621 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011622 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011623
11624 d = argvars[0].vval.v_dict;
11625 if (d != NULL)
11626 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011627 todo = d->dv_hashtab.ht_used;
11628 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011629 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011630 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011631 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011632 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011633 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011634 if (first)
11635 {
11636 n = i;
11637 first = FALSE;
11638 }
11639 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011640 n = i;
11641 }
11642 }
11643 }
11644 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011645 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000011646 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011647 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011648}
11649
11650/*
11651 * "max()" function
11652 */
11653 static void
11654f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011655 typval_T *argvars;
11656 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011657{
11658 max_min(argvars, rettv, TRUE);
11659}
11660
11661/*
11662 * "min()" function
11663 */
11664 static void
11665f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011666 typval_T *argvars;
11667 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011668{
11669 max_min(argvars, rettv, FALSE);
11670}
11671
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011672static int mkdir_recurse __ARGS((char_u *dir, int prot));
11673
11674/*
11675 * Create the directory in which "dir" is located, and higher levels when
11676 * needed.
11677 */
11678 static int
11679mkdir_recurse(dir, prot)
11680 char_u *dir;
11681 int prot;
11682{
11683 char_u *p;
11684 char_u *updir;
11685 int r = FAIL;
11686
11687 /* Get end of directory name in "dir".
11688 * We're done when it's "/" or "c:/". */
11689 p = gettail_sep(dir);
11690 if (p <= get_past_head(dir))
11691 return OK;
11692
11693 /* If the directory exists we're done. Otherwise: create it.*/
11694 updir = vim_strnsave(dir, (int)(p - dir));
11695 if (updir == NULL)
11696 return FAIL;
11697 if (mch_isdir(updir))
11698 r = OK;
11699 else if (mkdir_recurse(updir, prot) == OK)
11700 r = vim_mkdir_emsg(updir, prot);
11701 vim_free(updir);
11702 return r;
11703}
11704
11705#ifdef vim_mkdir
11706/*
11707 * "mkdir()" function
11708 */
11709 static void
11710f_mkdir(argvars, rettv)
11711 typval_T *argvars;
11712 typval_T *rettv;
11713{
11714 char_u *dir;
11715 char_u buf[NUMBUFLEN];
11716 int prot = 0755;
11717
11718 rettv->vval.v_number = FAIL;
11719 if (check_restricted() || check_secure())
11720 return;
11721
11722 dir = get_tv_string_buf(&argvars[0], buf);
11723 if (argvars[1].v_type != VAR_UNKNOWN)
11724 {
11725 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011726 prot = get_tv_number_chk(&argvars[2], NULL);
11727 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011728 mkdir_recurse(dir, prot);
11729 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011730 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011731}
11732#endif
11733
Bram Moolenaar0d660222005-01-07 21:51:51 +000011734/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011735 * "mode()" function
11736 */
11737/*ARGSUSED*/
11738 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011739f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011740 typval_T *argvars;
11741 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011742{
11743 char_u buf[2];
11744
11745#ifdef FEAT_VISUAL
11746 if (VIsual_active)
11747 {
11748 if (VIsual_select)
11749 buf[0] = VIsual_mode + 's' - 'v';
11750 else
11751 buf[0] = VIsual_mode;
11752 }
11753 else
11754#endif
11755 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
11756 buf[0] = 'r';
11757 else if (State & INSERT)
11758 {
11759 if (State & REPLACE_FLAG)
11760 buf[0] = 'R';
11761 else
11762 buf[0] = 'i';
11763 }
11764 else if (State & CMDLINE)
11765 buf[0] = 'c';
11766 else
11767 buf[0] = 'n';
11768
11769 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011770 rettv->vval.v_string = vim_strsave(buf);
11771 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011772}
11773
11774/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011775 * "nextnonblank()" function
11776 */
11777 static void
11778f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011779 typval_T *argvars;
11780 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011781{
11782 linenr_T lnum;
11783
11784 for (lnum = get_tv_lnum(argvars); ; ++lnum)
11785 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011786 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011787 {
11788 lnum = 0;
11789 break;
11790 }
11791 if (*skipwhite(ml_get(lnum)) != NUL)
11792 break;
11793 }
11794 rettv->vval.v_number = lnum;
11795}
11796
11797/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011798 * "nr2char()" function
11799 */
11800 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011801f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011802 typval_T *argvars;
11803 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011804{
11805 char_u buf[NUMBUFLEN];
11806
11807#ifdef FEAT_MBYTE
11808 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011809 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011810 else
11811#endif
11812 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011813 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011814 buf[1] = NUL;
11815 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011816 rettv->v_type = VAR_STRING;
11817 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011818}
11819
11820/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011821 * "prevnonblank()" function
11822 */
11823 static void
11824f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011825 typval_T *argvars;
11826 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011827{
11828 linenr_T lnum;
11829
11830 lnum = get_tv_lnum(argvars);
11831 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
11832 lnum = 0;
11833 else
11834 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
11835 --lnum;
11836 rettv->vval.v_number = lnum;
11837}
11838
Bram Moolenaar8c711452005-01-14 21:53:12 +000011839/*
11840 * "range()" function
11841 */
11842 static void
11843f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011844 typval_T *argvars;
11845 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011846{
11847 long start;
11848 long end;
11849 long stride = 1;
11850 long i;
Bram Moolenaar33570922005-01-25 22:26:29 +000011851 list_T *l;
11852 listitem_T *li;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011853 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011854
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011855 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011856 if (argvars[1].v_type == VAR_UNKNOWN)
11857 {
11858 end = start - 1;
11859 start = 0;
11860 }
11861 else
11862 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011863 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011864 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011865 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011866 }
11867
11868 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011869 if (error)
11870 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000011871 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000011872 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000011873 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000011874 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011875 else
11876 {
11877 l = list_alloc();
11878 if (l != NULL)
11879 {
11880 rettv->v_type = VAR_LIST;
11881 rettv->vval.v_list = l;
11882 ++l->lv_refcount;
11883
11884 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
11885 {
11886 li = listitem_alloc();
11887 if (li == NULL)
11888 break;
11889 li->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011890 li->li_tv.v_lock = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011891 li->li_tv.vval.v_number = i;
11892 list_append(l, li);
11893 }
11894 }
11895 }
11896}
11897
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011898/*
11899 * "readfile()" function
11900 */
11901 static void
11902f_readfile(argvars, rettv)
11903 typval_T *argvars;
11904 typval_T *rettv;
11905{
11906 int binary = FALSE;
11907 char_u *fname;
11908 FILE *fd;
11909 list_T *l;
11910 listitem_T *li;
11911#define FREAD_SIZE 200 /* optimized for text lines */
11912 char_u buf[FREAD_SIZE];
11913 int readlen; /* size of last fread() */
11914 int buflen; /* nr of valid chars in buf[] */
11915 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
11916 int tolist; /* first byte in buf[] still to be put in list */
11917 int chop; /* how many CR to chop off */
11918 char_u *prev = NULL; /* previously read bytes, if any */
11919 int prevlen = 0; /* length of "prev" if not NULL */
11920 char_u *s;
11921 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011922 long maxline = MAXLNUM;
11923 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011924
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011925 if (argvars[1].v_type != VAR_UNKNOWN)
11926 {
11927 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
11928 binary = TRUE;
11929 if (argvars[2].v_type != VAR_UNKNOWN)
11930 maxline = get_tv_number(&argvars[2]);
11931 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011932
11933 l = list_alloc();
11934 if (l == NULL)
11935 return;
11936 rettv->v_type = VAR_LIST;
11937 rettv->vval.v_list = l;
11938 l->lv_refcount = 1;
11939
11940 /* Always open the file in binary mode, library functions have a mind of
11941 * their own about CR-LF conversion. */
11942 fname = get_tv_string(&argvars[0]);
11943 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
11944 {
11945 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
11946 return;
11947 }
11948
11949 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000011950 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011951 {
11952 readlen = fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
11953 buflen = filtd + readlen;
11954 tolist = 0;
11955 for ( ; filtd < buflen || readlen <= 0; ++filtd)
11956 {
11957 if (buf[filtd] == '\n' || readlen <= 0)
11958 {
11959 /* Only when in binary mode add an empty list item when the
11960 * last line ends in a '\n'. */
11961 if (!binary && readlen == 0 && filtd == 0)
11962 break;
11963
11964 /* Found end-of-line or end-of-file: add a text line to the
11965 * list. */
11966 chop = 0;
11967 if (!binary)
11968 while (filtd - chop - 1 >= tolist
11969 && buf[filtd - chop - 1] == '\r')
11970 ++chop;
11971 len = filtd - tolist - chop;
11972 if (prev == NULL)
11973 s = vim_strnsave(buf + tolist, len);
11974 else
11975 {
11976 s = alloc((unsigned)(prevlen + len + 1));
11977 if (s != NULL)
11978 {
11979 mch_memmove(s, prev, prevlen);
11980 vim_free(prev);
11981 prev = NULL;
11982 mch_memmove(s + prevlen, buf + tolist, len);
11983 s[prevlen + len] = NUL;
11984 }
11985 }
11986 tolist = filtd + 1;
11987
11988 li = listitem_alloc();
11989 if (li == NULL)
11990 {
11991 vim_free(s);
11992 break;
11993 }
11994 li->li_tv.v_type = VAR_STRING;
11995 li->li_tv.v_lock = 0;
11996 li->li_tv.vval.v_string = s;
11997 list_append(l, li);
11998
Bram Moolenaarb982ca52005-03-28 21:02:15 +000011999 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012000 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012001 if (readlen <= 0)
12002 break;
12003 }
12004 else if (buf[filtd] == NUL)
12005 buf[filtd] = '\n';
12006 }
12007 if (readlen <= 0)
12008 break;
12009
12010 if (tolist == 0)
12011 {
12012 /* "buf" is full, need to move text to an allocated buffer */
12013 if (prev == NULL)
12014 {
12015 prev = vim_strnsave(buf, buflen);
12016 prevlen = buflen;
12017 }
12018 else
12019 {
12020 s = alloc((unsigned)(prevlen + buflen));
12021 if (s != NULL)
12022 {
12023 mch_memmove(s, prev, prevlen);
12024 mch_memmove(s + prevlen, buf, buflen);
12025 vim_free(prev);
12026 prev = s;
12027 prevlen += buflen;
12028 }
12029 }
12030 filtd = 0;
12031 }
12032 else
12033 {
12034 mch_memmove(buf, buf + tolist, buflen - tolist);
12035 filtd -= tolist;
12036 }
12037 }
12038
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012039 /*
12040 * For a negative line count use only the lines at the end of the file,
12041 * free the rest.
12042 */
12043 if (maxline < 0)
12044 while (cnt > -maxline)
12045 {
12046 listitem_remove(l, l->lv_first);
12047 --cnt;
12048 }
12049
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012050 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012051 fclose(fd);
12052}
12053
12054
Bram Moolenaar0d660222005-01-07 21:51:51 +000012055#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
12056static void make_connection __ARGS((void));
12057static int check_connection __ARGS((void));
12058
12059 static void
12060make_connection()
12061{
12062 if (X_DISPLAY == NULL
12063# ifdef FEAT_GUI
12064 && !gui.in_use
12065# endif
12066 )
12067 {
12068 x_force_connect = TRUE;
12069 setup_term_clip();
12070 x_force_connect = FALSE;
12071 }
12072}
12073
12074 static int
12075check_connection()
12076{
12077 make_connection();
12078 if (X_DISPLAY == NULL)
12079 {
12080 EMSG(_("E240: No connection to Vim server"));
12081 return FAIL;
12082 }
12083 return OK;
12084}
12085#endif
12086
12087#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012088static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012089
12090 static void
12091remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000012092 typval_T *argvars;
12093 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012094 int expr;
12095{
12096 char_u *server_name;
12097 char_u *keys;
12098 char_u *r = NULL;
12099 char_u buf[NUMBUFLEN];
12100# ifdef WIN32
12101 HWND w;
12102# else
12103 Window w;
12104# endif
12105
12106 if (check_restricted() || check_secure())
12107 return;
12108
12109# ifdef FEAT_X11
12110 if (check_connection() == FAIL)
12111 return;
12112# endif
12113
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012114 server_name = get_tv_string_chk(&argvars[0]);
12115 if (server_name == NULL)
12116 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012117 keys = get_tv_string_buf(&argvars[1], buf);
12118# ifdef WIN32
12119 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
12120# else
12121 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
12122 < 0)
12123# endif
12124 {
12125 if (r != NULL)
12126 EMSG(r); /* sending worked but evaluation failed */
12127 else
12128 EMSG2(_("E241: Unable to send to %s"), server_name);
12129 return;
12130 }
12131
12132 rettv->vval.v_string = r;
12133
12134 if (argvars[2].v_type != VAR_UNKNOWN)
12135 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012136 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000012137 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012138 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012139
12140 sprintf((char *)str, "0x%x", (unsigned int)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000012141 v.di_tv.v_type = VAR_STRING;
12142 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012143 idvar = get_tv_string_chk(&argvars[2]);
12144 if (idvar != NULL)
12145 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012146 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012147 }
12148}
12149#endif
12150
12151/*
12152 * "remote_expr()" function
12153 */
12154/*ARGSUSED*/
12155 static void
12156f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012157 typval_T *argvars;
12158 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012159{
12160 rettv->v_type = VAR_STRING;
12161 rettv->vval.v_string = NULL;
12162#ifdef FEAT_CLIENTSERVER
12163 remote_common(argvars, rettv, TRUE);
12164#endif
12165}
12166
12167/*
12168 * "remote_foreground()" function
12169 */
12170/*ARGSUSED*/
12171 static void
12172f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012173 typval_T *argvars;
12174 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012175{
12176 rettv->vval.v_number = 0;
12177#ifdef FEAT_CLIENTSERVER
12178# ifdef WIN32
12179 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012180 {
12181 char_u *server_name = get_tv_string_chk(&argvars[0]);
12182
12183 if (server_name != NULL)
12184 serverForeground(server_name);
12185 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012186# else
12187 /* Send a foreground() expression to the server. */
12188 argvars[1].v_type = VAR_STRING;
12189 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
12190 argvars[2].v_type = VAR_UNKNOWN;
12191 remote_common(argvars, rettv, TRUE);
12192 vim_free(argvars[1].vval.v_string);
12193# endif
12194#endif
12195}
12196
12197/*ARGSUSED*/
12198 static void
12199f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012200 typval_T *argvars;
12201 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012202{
12203#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012204 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012205 char_u *s = NULL;
12206# ifdef WIN32
12207 int n = 0;
12208# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012209 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012210
12211 if (check_restricted() || check_secure())
12212 {
12213 rettv->vval.v_number = -1;
12214 return;
12215 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012216 serverid = get_tv_string_chk(&argvars[0]);
12217 if (serverid == NULL)
12218 {
12219 rettv->vval.v_number = -1;
12220 return; /* type error; errmsg already given */
12221 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012222# ifdef WIN32
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012223 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012224 if (n == 0)
12225 rettv->vval.v_number = -1;
12226 else
12227 {
12228 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
12229 rettv->vval.v_number = (s != NULL);
12230 }
12231# else
12232 rettv->vval.v_number = 0;
12233 if (check_connection() == FAIL)
12234 return;
12235
12236 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012237 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012238# endif
12239
12240 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
12241 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012242 char_u *retvar;
12243
Bram Moolenaar33570922005-01-25 22:26:29 +000012244 v.di_tv.v_type = VAR_STRING;
12245 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012246 retvar = get_tv_string_chk(&argvars[1]);
12247 if (retvar != NULL)
12248 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012249 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012250 }
12251#else
12252 rettv->vval.v_number = -1;
12253#endif
12254}
12255
12256/*ARGSUSED*/
12257 static void
12258f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012259 typval_T *argvars;
12260 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012261{
12262 char_u *r = NULL;
12263
12264#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012265 char_u *serverid = get_tv_string_chk(&argvars[0]);
12266
12267 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000012268 {
12269# ifdef WIN32
12270 /* The server's HWND is encoded in the 'id' parameter */
12271 int n = 0;
12272
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012273 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012274 if (n != 0)
12275 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
12276 if (r == NULL)
12277# else
12278 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012279 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012280# endif
12281 EMSG(_("E277: Unable to read a server reply"));
12282 }
12283#endif
12284 rettv->v_type = VAR_STRING;
12285 rettv->vval.v_string = r;
12286}
12287
12288/*
12289 * "remote_send()" function
12290 */
12291/*ARGSUSED*/
12292 static void
12293f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012294 typval_T *argvars;
12295 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012296{
12297 rettv->v_type = VAR_STRING;
12298 rettv->vval.v_string = NULL;
12299#ifdef FEAT_CLIENTSERVER
12300 remote_common(argvars, rettv, FALSE);
12301#endif
12302}
12303
12304/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012305 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012306 */
12307 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012308f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012309 typval_T *argvars;
12310 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012311{
Bram Moolenaar33570922005-01-25 22:26:29 +000012312 list_T *l;
12313 listitem_T *item, *item2;
12314 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012315 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012316 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012317 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000012318 dict_T *d;
12319 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012320
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012321 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012322 if (argvars[0].v_type == VAR_DICT)
12323 {
12324 if (argvars[2].v_type != VAR_UNKNOWN)
12325 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012326 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar758711c2005-02-02 23:11:38 +000012327 && !tv_check_lock(d->dv_lock, (char_u *)"remove()"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000012328 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012329 key = get_tv_string_chk(&argvars[1]);
12330 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012331 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012332 di = dict_find(d, key, -1);
12333 if (di == NULL)
12334 EMSG2(_(e_dictkey), key);
12335 else
12336 {
12337 *rettv = di->di_tv;
12338 init_tv(&di->di_tv);
12339 dictitem_remove(d, di);
12340 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012341 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012342 }
12343 }
12344 else if (argvars[0].v_type != VAR_LIST)
12345 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012346 else if ((l = argvars[0].vval.v_list) != NULL
12347 && !tv_check_lock(l->lv_lock, (char_u *)"remove()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012348 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012349 int error = FALSE;
12350
12351 idx = get_tv_number_chk(&argvars[1], &error);
12352 if (error)
12353 ; /* type error: do nothing, errmsg already given */
12354 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012355 EMSGN(_(e_listidx), idx);
12356 else
12357 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012358 if (argvars[2].v_type == VAR_UNKNOWN)
12359 {
12360 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012361 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012362 *rettv = item->li_tv;
12363 vim_free(item);
12364 }
12365 else
12366 {
12367 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012368 end = get_tv_number_chk(&argvars[2], &error);
12369 if (error)
12370 ; /* type error: do nothing */
12371 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012372 EMSGN(_(e_listidx), end);
12373 else
12374 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012375 int cnt = 0;
12376
12377 for (li = item; li != NULL; li = li->li_next)
12378 {
12379 ++cnt;
12380 if (li == item2)
12381 break;
12382 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012383 if (li == NULL) /* didn't find "item2" after "item" */
12384 EMSG(_(e_invrange));
12385 else
12386 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012387 list_remove(l, item, item2);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012388 l = list_alloc();
12389 if (l != NULL)
12390 {
12391 rettv->v_type = VAR_LIST;
12392 rettv->vval.v_list = l;
12393 l->lv_first = item;
12394 l->lv_last = item2;
12395 l->lv_refcount = 1;
12396 item->li_prev = NULL;
12397 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012398 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012399 }
12400 }
12401 }
12402 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012403 }
12404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012405}
12406
12407/*
12408 * "rename({from}, {to})" function
12409 */
12410 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012411f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012412 typval_T *argvars;
12413 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012414{
12415 char_u buf[NUMBUFLEN];
12416
12417 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012418 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012419 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012420 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
12421 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012422}
12423
12424/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012425 * "repeat()" function
12426 */
12427/*ARGSUSED*/
12428 static void
12429f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012430 typval_T *argvars;
12431 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012432{
12433 char_u *p;
12434 int n;
12435 int slen;
12436 int len;
12437 char_u *r;
12438 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000012439 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012440
12441 n = get_tv_number(&argvars[1]);
12442 if (argvars[0].v_type == VAR_LIST)
12443 {
12444 l = list_alloc();
12445 if (l != NULL && argvars[0].vval.v_list != NULL)
12446 {
12447 l->lv_refcount = 1;
12448 while (n-- > 0)
12449 if (list_extend(l, argvars[0].vval.v_list, NULL) == FAIL)
12450 break;
12451 }
12452 rettv->v_type = VAR_LIST;
12453 rettv->vval.v_list = l;
12454 }
12455 else
12456 {
12457 p = get_tv_string(&argvars[0]);
12458 rettv->v_type = VAR_STRING;
12459 rettv->vval.v_string = NULL;
12460
12461 slen = (int)STRLEN(p);
12462 len = slen * n;
12463 if (len <= 0)
12464 return;
12465
12466 r = alloc(len + 1);
12467 if (r != NULL)
12468 {
12469 for (i = 0; i < n; i++)
12470 mch_memmove(r + i * slen, p, (size_t)slen);
12471 r[len] = NUL;
12472 }
12473
12474 rettv->vval.v_string = r;
12475 }
12476}
12477
12478/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012479 * "resolve()" function
12480 */
12481 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012482f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012483 typval_T *argvars;
12484 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012485{
12486 char_u *p;
12487
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012488 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012489#ifdef FEAT_SHORTCUT
12490 {
12491 char_u *v = NULL;
12492
12493 v = mch_resolve_shortcut(p);
12494 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012495 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012496 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012497 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012498 }
12499#else
12500# ifdef HAVE_READLINK
12501 {
12502 char_u buf[MAXPATHL + 1];
12503 char_u *cpy;
12504 int len;
12505 char_u *remain = NULL;
12506 char_u *q;
12507 int is_relative_to_current = FALSE;
12508 int has_trailing_pathsep = FALSE;
12509 int limit = 100;
12510
12511 p = vim_strsave(p);
12512
12513 if (p[0] == '.' && (vim_ispathsep(p[1])
12514 || (p[1] == '.' && (vim_ispathsep(p[2])))))
12515 is_relative_to_current = TRUE;
12516
12517 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000012518 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012519 has_trailing_pathsep = TRUE;
12520
12521 q = getnextcomp(p);
12522 if (*q != NUL)
12523 {
12524 /* Separate the first path component in "p", and keep the
12525 * remainder (beginning with the path separator). */
12526 remain = vim_strsave(q - 1);
12527 q[-1] = NUL;
12528 }
12529
12530 for (;;)
12531 {
12532 for (;;)
12533 {
12534 len = readlink((char *)p, (char *)buf, MAXPATHL);
12535 if (len <= 0)
12536 break;
12537 buf[len] = NUL;
12538
12539 if (limit-- == 0)
12540 {
12541 vim_free(p);
12542 vim_free(remain);
12543 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012544 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012545 goto fail;
12546 }
12547
12548 /* Ensure that the result will have a trailing path separator
12549 * if the argument has one. */
12550 if (remain == NULL && has_trailing_pathsep)
12551 add_pathsep(buf);
12552
12553 /* Separate the first path component in the link value and
12554 * concatenate the remainders. */
12555 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
12556 if (*q != NUL)
12557 {
12558 if (remain == NULL)
12559 remain = vim_strsave(q - 1);
12560 else
12561 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012562 cpy = vim_strnsave(q-1, STRLEN(q-1) + STRLEN(remain));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012563 if (cpy != NULL)
12564 {
12565 STRCAT(cpy, remain);
12566 vim_free(remain);
12567 remain = cpy;
12568 }
12569 }
12570 q[-1] = NUL;
12571 }
12572
12573 q = gettail(p);
12574 if (q > p && *q == NUL)
12575 {
12576 /* Ignore trailing path separator. */
12577 q[-1] = NUL;
12578 q = gettail(p);
12579 }
12580 if (q > p && !mch_isFullName(buf))
12581 {
12582 /* symlink is relative to directory of argument */
12583 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
12584 if (cpy != NULL)
12585 {
12586 STRCPY(cpy, p);
12587 STRCPY(gettail(cpy), buf);
12588 vim_free(p);
12589 p = cpy;
12590 }
12591 }
12592 else
12593 {
12594 vim_free(p);
12595 p = vim_strsave(buf);
12596 }
12597 }
12598
12599 if (remain == NULL)
12600 break;
12601
12602 /* Append the first path component of "remain" to "p". */
12603 q = getnextcomp(remain + 1);
12604 len = q - remain - (*q != NUL);
12605 cpy = vim_strnsave(p, STRLEN(p) + len);
12606 if (cpy != NULL)
12607 {
12608 STRNCAT(cpy, remain, len);
12609 vim_free(p);
12610 p = cpy;
12611 }
12612 /* Shorten "remain". */
12613 if (*q != NUL)
12614 STRCPY(remain, q - 1);
12615 else
12616 {
12617 vim_free(remain);
12618 remain = NULL;
12619 }
12620 }
12621
12622 /* If the result is a relative path name, make it explicitly relative to
12623 * the current directory if and only if the argument had this form. */
12624 if (!vim_ispathsep(*p))
12625 {
12626 if (is_relative_to_current
12627 && *p != NUL
12628 && !(p[0] == '.'
12629 && (p[1] == NUL
12630 || vim_ispathsep(p[1])
12631 || (p[1] == '.'
12632 && (p[2] == NUL
12633 || vim_ispathsep(p[2]))))))
12634 {
12635 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012636 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012637 if (cpy != NULL)
12638 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012639 vim_free(p);
12640 p = cpy;
12641 }
12642 }
12643 else if (!is_relative_to_current)
12644 {
12645 /* Strip leading "./". */
12646 q = p;
12647 while (q[0] == '.' && vim_ispathsep(q[1]))
12648 q += 2;
12649 if (q > p)
12650 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
12651 }
12652 }
12653
12654 /* Ensure that the result will have no trailing path separator
12655 * if the argument had none. But keep "/" or "//". */
12656 if (!has_trailing_pathsep)
12657 {
12658 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000012659 if (after_pathsep(p, q))
12660 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012661 }
12662
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012663 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012664 }
12665# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012666 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012667# endif
12668#endif
12669
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012670 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012671
12672#ifdef HAVE_READLINK
12673fail:
12674#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012675 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012676}
12677
12678/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012679 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012680 */
12681 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012682f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012683 typval_T *argvars;
12684 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012685{
Bram Moolenaar33570922005-01-25 22:26:29 +000012686 list_T *l;
12687 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012688
Bram Moolenaar0d660222005-01-07 21:51:51 +000012689 rettv->vval.v_number = 0;
12690 if (argvars[0].v_type != VAR_LIST)
12691 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012692 else if ((l = argvars[0].vval.v_list) != NULL
12693 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000012694 {
12695 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012696 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012697 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012698 while (li != NULL)
12699 {
12700 ni = li->li_prev;
12701 list_append(l, li);
12702 li = ni;
12703 }
12704 rettv->vval.v_list = l;
12705 rettv->v_type = VAR_LIST;
12706 ++l->lv_refcount;
12707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012708}
12709
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012710#define SP_NOMOVE 1 /* don't move cursor */
12711#define SP_REPEAT 2 /* repeat to find outer pair */
12712#define SP_RETCOUNT 4 /* return matchcount */
12713
Bram Moolenaar33570922005-01-25 22:26:29 +000012714static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012715
12716/*
12717 * Get flags for a search function.
12718 * Possibly sets "p_ws".
12719 * Returns BACKWARD, FORWARD or zero (for an error).
12720 */
12721 static int
12722get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000012723 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012724 int *flagsp;
12725{
12726 int dir = FORWARD;
12727 char_u *flags;
12728 char_u nbuf[NUMBUFLEN];
12729 int mask;
12730
12731 if (varp->v_type != VAR_UNKNOWN)
12732 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012733 flags = get_tv_string_buf_chk(varp, nbuf);
12734 if (flags == NULL)
12735 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012736 while (*flags != NUL)
12737 {
12738 switch (*flags)
12739 {
12740 case 'b': dir = BACKWARD; break;
12741 case 'w': p_ws = TRUE; break;
12742 case 'W': p_ws = FALSE; break;
12743 default: mask = 0;
12744 if (flagsp != NULL)
12745 switch (*flags)
12746 {
12747 case 'n': mask = SP_NOMOVE; break;
12748 case 'r': mask = SP_REPEAT; break;
12749 case 'm': mask = SP_RETCOUNT; break;
12750 }
12751 if (mask == 0)
12752 {
12753 EMSG2(_(e_invarg2), flags);
12754 dir = 0;
12755 }
12756 else
12757 *flagsp |= mask;
12758 }
12759 if (dir == 0)
12760 break;
12761 ++flags;
12762 }
12763 }
12764 return dir;
12765}
12766
Bram Moolenaar071d4272004-06-13 20:20:40 +000012767/*
12768 * "search()" function
12769 */
12770 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012771f_search(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012772 typval_T *argvars;
12773 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012774{
12775 char_u *pat;
12776 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012777 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012778 int save_p_ws = p_ws;
12779 int dir;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012780 int flags = 0;
12781
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012782 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012783
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012784 pat = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012785 dir = get_search_arg(&argvars[1], &flags); /* may set p_ws */
12786 if (dir == 0)
12787 goto theend;
12788 if ((flags & ~SP_NOMOVE) != 0)
12789 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012790 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012791 goto theend;
12792 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012793
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012794 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012795 if (searchit(curwin, curbuf, &pos, dir, pat, 1L,
12796 SEARCH_KEEP, RE_SEARCH) != FAIL)
12797 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012798 rettv->vval.v_number = pos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012799 curwin->w_cursor = pos;
12800 /* "/$" will put the cursor after the end of the line, may need to
12801 * correct that here */
12802 check_cursor();
12803 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012804
12805 /* If 'n' flag is used: restore cursor position. */
12806 if (flags & SP_NOMOVE)
12807 curwin->w_cursor = save_cursor;
12808theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000012809 p_ws = save_p_ws;
12810}
12811
Bram Moolenaar071d4272004-06-13 20:20:40 +000012812/*
12813 * "searchpair()" function
12814 */
12815 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012816f_searchpair(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012817 typval_T *argvars;
12818 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012819{
12820 char_u *spat, *mpat, *epat;
12821 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012822 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012823 int dir;
12824 int flags = 0;
12825 char_u nbuf1[NUMBUFLEN];
12826 char_u nbuf2[NUMBUFLEN];
12827 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000012828
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012829 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012830
Bram Moolenaar071d4272004-06-13 20:20:40 +000012831 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012832 spat = get_tv_string_chk(&argvars[0]);
12833 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
12834 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
12835 if (spat == NULL || mpat == NULL || epat == NULL)
12836 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012837
Bram Moolenaar071d4272004-06-13 20:20:40 +000012838 /* Handle the optional fourth argument: flags */
12839 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012840 if (dir == 0)
12841 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012842
12843 /* Optional fifth argument: skip expresion */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012844 if (argvars[3].v_type == VAR_UNKNOWN
12845 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012846 skip = (char_u *)"";
12847 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012848 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
12849 if (skip == NULL)
12850 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012851
Bram Moolenaar9fad3082005-07-19 22:22:13 +000012852 rettv->vval.v_number = do_searchpair(spat, mpat, epat, dir, skip, flags);
12853
12854theend:
12855 p_ws = save_p_ws;
12856}
12857
12858/*
12859 * Search for a start/middle/end thing.
12860 * Used by searchpair(), see its documentation for the details.
12861 * Returns 0 or -1 for no match,
12862 */
12863 long
12864do_searchpair(spat, mpat, epat, dir, skip, flags)
12865 char_u *spat; /* start pattern */
12866 char_u *mpat; /* middle pattern */
12867 char_u *epat; /* end pattern */
12868 int dir; /* BACKWARD or FORWARD */
12869 char_u *skip; /* skip expression */
12870 int flags; /* SP_RETCOUNT, SP_REPEAT, SP_NOMOVE */
12871{
12872 char_u *save_cpo;
12873 char_u *pat, *pat2 = NULL, *pat3 = NULL;
12874 long retval = 0;
12875 pos_T pos;
12876 pos_T firstpos;
12877 pos_T foundpos;
12878 pos_T save_cursor;
12879 pos_T save_pos;
12880 int n;
12881 int r;
12882 int nest = 1;
12883 int err;
12884
12885 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12886 save_cpo = p_cpo;
12887 p_cpo = (char_u *)"";
12888
12889 /* Make two search patterns: start/end (pat2, for in nested pairs) and
12890 * start/middle/end (pat3, for the top pair). */
12891 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
12892 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
12893 if (pat2 == NULL || pat3 == NULL)
12894 goto theend;
12895 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
12896 if (*mpat == NUL)
12897 STRCPY(pat3, pat2);
12898 else
12899 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
12900 spat, epat, mpat);
12901
Bram Moolenaar071d4272004-06-13 20:20:40 +000012902 save_cursor = curwin->w_cursor;
12903 pos = curwin->w_cursor;
12904 firstpos.lnum = 0;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000012905 foundpos.lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012906 pat = pat3;
12907 for (;;)
12908 {
12909 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
12910 SEARCH_KEEP, RE_SEARCH);
12911 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
12912 /* didn't find it or found the first match again: FAIL */
12913 break;
12914
12915 if (firstpos.lnum == 0)
12916 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000012917 if (equalpos(pos, foundpos))
12918 {
12919 /* Found the same position again. Can happen with a pattern that
12920 * has "\zs" at the end and searching backwards. Advance one
12921 * character and try again. */
12922 if (dir == BACKWARD)
12923 decl(&pos);
12924 else
12925 incl(&pos);
12926 }
12927 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012928
12929 /* If the skip pattern matches, ignore this match. */
12930 if (*skip != NUL)
12931 {
12932 save_pos = curwin->w_cursor;
12933 curwin->w_cursor = pos;
12934 r = eval_to_bool(skip, &err, NULL, FALSE);
12935 curwin->w_cursor = save_pos;
12936 if (err)
12937 {
12938 /* Evaluating {skip} caused an error, break here. */
12939 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000012940 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012941 break;
12942 }
12943 if (r)
12944 continue;
12945 }
12946
12947 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
12948 {
12949 /* Found end when searching backwards or start when searching
12950 * forward: nested pair. */
12951 ++nest;
12952 pat = pat2; /* nested, don't search for middle */
12953 }
12954 else
12955 {
12956 /* Found end when searching forward or start when searching
12957 * backward: end of (nested) pair; or found middle in outer pair. */
12958 if (--nest == 1)
12959 pat = pat3; /* outer level, search for middle */
12960 }
12961
12962 if (nest == 0)
12963 {
12964 /* Found the match: return matchcount or line number. */
12965 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000012966 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012967 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000012968 retval = pos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012969 curwin->w_cursor = pos;
12970 if (!(flags & SP_REPEAT))
12971 break;
12972 nest = 1; /* search for next unmatched */
12973 }
12974 }
12975
12976 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000012977 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012978 curwin->w_cursor = save_cursor;
12979
12980theend:
12981 vim_free(pat2);
12982 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012983 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000012984
12985 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012986}
12987
Bram Moolenaar0d660222005-01-07 21:51:51 +000012988/*ARGSUSED*/
12989 static void
12990f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012991 typval_T *argvars;
12992 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012993{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012994#ifdef FEAT_CLIENTSERVER
12995 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012996 char_u *server = get_tv_string_chk(&argvars[0]);
12997 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012998
Bram Moolenaar0d660222005-01-07 21:51:51 +000012999 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013000 if (server == NULL || reply == NULL)
13001 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013002 if (check_restricted() || check_secure())
13003 return;
13004# ifdef FEAT_X11
13005 if (check_connection() == FAIL)
13006 return;
13007# endif
13008
13009 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013010 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013011 EMSG(_("E258: Unable to send to client"));
13012 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013013 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013014 rettv->vval.v_number = 0;
13015#else
13016 rettv->vval.v_number = -1;
13017#endif
13018}
13019
13020/*ARGSUSED*/
13021 static void
13022f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013023 typval_T *argvars;
13024 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013025{
13026 char_u *r = NULL;
13027
13028#ifdef FEAT_CLIENTSERVER
13029# ifdef WIN32
13030 r = serverGetVimNames();
13031# else
13032 make_connection();
13033 if (X_DISPLAY != NULL)
13034 r = serverGetVimNames(X_DISPLAY);
13035# endif
13036#endif
13037 rettv->v_type = VAR_STRING;
13038 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013039}
13040
13041/*
13042 * "setbufvar()" function
13043 */
13044/*ARGSUSED*/
13045 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013046f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013047 typval_T *argvars;
13048 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013049{
13050 buf_T *buf;
13051#ifdef FEAT_AUTOCMD
13052 aco_save_T aco;
13053#else
13054 buf_T *save_curbuf;
13055#endif
13056 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013057 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013058 char_u nbuf[NUMBUFLEN];
13059
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013060 rettv->vval.v_number = 0;
13061
Bram Moolenaar071d4272004-06-13 20:20:40 +000013062 if (check_restricted() || check_secure())
13063 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013064 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
13065 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013066 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013067 varp = &argvars[2];
13068
13069 if (buf != NULL && varname != NULL && varp != NULL)
13070 {
13071 /* set curbuf to be our buf, temporarily */
13072#ifdef FEAT_AUTOCMD
13073 aucmd_prepbuf(&aco, buf);
13074#else
13075 save_curbuf = curbuf;
13076 curbuf = buf;
13077#endif
13078
13079 if (*varname == '&')
13080 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013081 long numval;
13082 char_u *strval;
13083 int error = FALSE;
13084
Bram Moolenaar071d4272004-06-13 20:20:40 +000013085 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013086 numval = get_tv_number_chk(varp, &error);
13087 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013088 if (!error && strval != NULL)
13089 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013090 }
13091 else
13092 {
13093 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
13094 if (bufvarname != NULL)
13095 {
13096 STRCPY(bufvarname, "b:");
13097 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013098 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013099 vim_free(bufvarname);
13100 }
13101 }
13102
13103 /* reset notion of buffer */
13104#ifdef FEAT_AUTOCMD
13105 aucmd_restbuf(&aco);
13106#else
13107 curbuf = save_curbuf;
13108#endif
13109 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013110}
13111
13112/*
13113 * "setcmdpos()" function
13114 */
13115 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013116f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013117 typval_T *argvars;
13118 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013119{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013120 int pos = (int)get_tv_number(&argvars[0]) - 1;
13121
13122 if (pos >= 0)
13123 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013124}
13125
13126/*
13127 * "setline()" function
13128 */
13129 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013130f_setline(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{
13134 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000013135 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013136 list_T *l = NULL;
13137 listitem_T *li = NULL;
13138 long added = 0;
13139 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013140
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013141 lnum = get_tv_lnum(&argvars[0]);
13142 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013143 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013144 l = argvars[1].vval.v_list;
13145 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013146 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013147 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013148 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013149
13150 rettv->vval.v_number = 0; /* OK */
13151 for (;;)
13152 {
13153 if (l != NULL)
13154 {
13155 /* list argument, get next string */
13156 if (li == NULL)
13157 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013158 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013159 li = li->li_next;
13160 }
13161
13162 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013163 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013164 break;
13165 if (lnum <= curbuf->b_ml.ml_line_count)
13166 {
13167 /* existing line, replace it */
13168 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
13169 {
13170 changed_bytes(lnum, 0);
13171 check_cursor_col();
13172 rettv->vval.v_number = 0; /* OK */
13173 }
13174 }
13175 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
13176 {
13177 /* lnum is one past the last line, append the line */
13178 ++added;
13179 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
13180 rettv->vval.v_number = 0; /* OK */
13181 }
13182
13183 if (l == NULL) /* only one string argument */
13184 break;
13185 ++lnum;
13186 }
13187
13188 if (added > 0)
13189 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013190}
13191
13192/*
Bram Moolenaar2641f772005-03-25 21:58:17 +000013193 * "setqflist()" function
13194 */
13195/*ARGSUSED*/
13196 static void
13197f_setqflist(argvars, rettv)
13198 typval_T *argvars;
13199 typval_T *rettv;
13200{
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013201 char_u *act;
13202 int action = ' ';
13203
Bram Moolenaar2641f772005-03-25 21:58:17 +000013204 rettv->vval.v_number = -1;
13205
13206#ifdef FEAT_QUICKFIX
13207 if (argvars[0].v_type != VAR_LIST)
13208 EMSG(_(e_listreq));
13209 else
13210 {
13211 list_T *l = argvars[0].vval.v_list;
13212
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013213 if (argvars[1].v_type == VAR_STRING)
13214 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013215 act = get_tv_string_chk(&argvars[1]);
13216 if (act == NULL)
13217 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013218 if (*act == 'a' || *act == 'r')
13219 action = *act;
13220 }
13221
13222 if (l != NULL && set_errorlist(l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013223 rettv->vval.v_number = 0;
13224 }
13225#endif
13226}
13227
13228/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013229 * "setreg()" function
13230 */
13231 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013232f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013233 typval_T *argvars;
13234 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013235{
13236 int regname;
13237 char_u *strregname;
13238 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013239 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013240 int append;
13241 char_u yank_type;
13242 long block_len;
13243
13244 block_len = -1;
13245 yank_type = MAUTO;
13246 append = FALSE;
13247
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013248 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013249 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013250
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013251 if (strregname == NULL)
13252 return; /* type error; errmsg already given */
13253 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013254 if (regname == 0 || regname == '@')
13255 regname = '"';
13256 else if (regname == '=')
13257 return;
13258
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013259 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013260 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013261 stropt = get_tv_string_chk(&argvars[2]);
13262 if (stropt == NULL)
13263 return; /* type error */
13264 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013265 switch (*stropt)
13266 {
13267 case 'a': case 'A': /* append */
13268 append = TRUE;
13269 break;
13270 case 'v': case 'c': /* character-wise selection */
13271 yank_type = MCHAR;
13272 break;
13273 case 'V': case 'l': /* line-wise selection */
13274 yank_type = MLINE;
13275 break;
13276#ifdef FEAT_VISUAL
13277 case 'b': case Ctrl_V: /* block-wise selection */
13278 yank_type = MBLOCK;
13279 if (VIM_ISDIGIT(stropt[1]))
13280 {
13281 ++stropt;
13282 block_len = getdigits(&stropt) - 1;
13283 --stropt;
13284 }
13285 break;
13286#endif
13287 }
13288 }
13289
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013290 strval = get_tv_string_chk(&argvars[1]);
13291 if (strval != NULL)
13292 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000013293 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013294 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013295}
13296
13297
13298/*
13299 * "setwinvar(expr)" function
13300 */
13301/*ARGSUSED*/
13302 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013303f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013304 typval_T *argvars;
13305 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013306{
13307 win_T *win;
13308#ifdef FEAT_WINDOWS
13309 win_T *save_curwin;
13310#endif
13311 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013312 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013313 char_u nbuf[NUMBUFLEN];
13314
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013315 rettv->vval.v_number = 0;
13316
Bram Moolenaar071d4272004-06-13 20:20:40 +000013317 if (check_restricted() || check_secure())
13318 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013319 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013320 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013321 varp = &argvars[2];
13322
13323 if (win != NULL && varname != NULL && varp != NULL)
13324 {
13325#ifdef FEAT_WINDOWS
13326 /* set curwin to be our win, temporarily */
13327 save_curwin = curwin;
13328 curwin = win;
13329 curbuf = curwin->w_buffer;
13330#endif
13331
13332 if (*varname == '&')
13333 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013334 long numval;
13335 char_u *strval;
13336 int error = FALSE;
13337
Bram Moolenaar071d4272004-06-13 20:20:40 +000013338 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013339 numval = get_tv_number_chk(varp, &error);
13340 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013341 if (!error && strval != NULL)
13342 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013343 }
13344 else
13345 {
13346 winvarname = alloc((unsigned)STRLEN(varname) + 3);
13347 if (winvarname != NULL)
13348 {
13349 STRCPY(winvarname, "w:");
13350 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013351 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013352 vim_free(winvarname);
13353 }
13354 }
13355
13356#ifdef FEAT_WINDOWS
13357 /* Restore current window, if it's still valid (autocomands can make
13358 * it invalid). */
13359 if (win_valid(save_curwin))
13360 {
13361 curwin = save_curwin;
13362 curbuf = curwin->w_buffer;
13363 }
13364#endif
13365 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013366}
13367
13368/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013369 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013370 */
13371 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013372f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013373 typval_T *argvars;
13374 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013375{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013376 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013377
Bram Moolenaar0d660222005-01-07 21:51:51 +000013378 p = get_tv_string(&argvars[0]);
13379 rettv->vval.v_string = vim_strsave(p);
13380 simplify_filename(rettv->vval.v_string); /* simplify in place */
13381 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013382}
13383
Bram Moolenaar0d660222005-01-07 21:51:51 +000013384static int
13385#ifdef __BORLANDC__
13386 _RTLENTRYF
13387#endif
13388 item_compare __ARGS((const void *s1, const void *s2));
13389static int
13390#ifdef __BORLANDC__
13391 _RTLENTRYF
13392#endif
13393 item_compare2 __ARGS((const void *s1, const void *s2));
13394
13395static int item_compare_ic;
13396static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013397static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013398#define ITEM_COMPARE_FAIL 999
13399
Bram Moolenaar071d4272004-06-13 20:20:40 +000013400/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013401 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013402 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013403 static int
13404#ifdef __BORLANDC__
13405_RTLENTRYF
13406#endif
13407item_compare(s1, s2)
13408 const void *s1;
13409 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013410{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013411 char_u *p1, *p2;
13412 char_u *tofree1, *tofree2;
13413 int res;
13414 char_u numbuf1[NUMBUFLEN];
13415 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013416
Bram Moolenaar33570922005-01-25 22:26:29 +000013417 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1);
13418 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013419 if (item_compare_ic)
13420 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013421 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000013422 res = STRCMP(p1, p2);
13423 vim_free(tofree1);
13424 vim_free(tofree2);
13425 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013426}
13427
13428 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000013429#ifdef __BORLANDC__
13430_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000013431#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000013432item_compare2(s1, s2)
13433 const void *s1;
13434 const void *s2;
13435{
13436 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000013437 typval_T rettv;
13438 typval_T argv[2];
Bram Moolenaar0d660222005-01-07 21:51:51 +000013439 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013440
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013441 /* shortcut after failure in previous call; compare all items equal */
13442 if (item_compare_func_err)
13443 return 0;
13444
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013445 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
13446 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013447 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
13448 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013449
13450 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
13451 res = call_func(item_compare_func, STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000013452 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013453 clear_tv(&argv[0]);
13454 clear_tv(&argv[1]);
13455
13456 if (res == FAIL)
13457 res = ITEM_COMPARE_FAIL;
13458 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013459 /* return value has wrong type */
13460 res = get_tv_number_chk(&rettv, &item_compare_func_err);
13461 if (item_compare_func_err)
13462 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013463 clear_tv(&rettv);
13464 return res;
13465}
13466
13467/*
13468 * "sort({list})" function
13469 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013470 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013471f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013472 typval_T *argvars;
13473 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013474{
Bram Moolenaar33570922005-01-25 22:26:29 +000013475 list_T *l;
13476 listitem_T *li;
13477 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013478 long len;
13479 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013480
Bram Moolenaar0d660222005-01-07 21:51:51 +000013481 rettv->vval.v_number = 0;
13482 if (argvars[0].v_type != VAR_LIST)
13483 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000013484 else
13485 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013486 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013487 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013488 return;
13489 rettv->vval.v_list = l;
13490 rettv->v_type = VAR_LIST;
13491 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013492
Bram Moolenaar0d660222005-01-07 21:51:51 +000013493 len = list_len(l);
13494 if (len <= 1)
13495 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013496
Bram Moolenaar0d660222005-01-07 21:51:51 +000013497 item_compare_ic = FALSE;
13498 item_compare_func = NULL;
13499 if (argvars[1].v_type != VAR_UNKNOWN)
13500 {
13501 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013502 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013503 else
13504 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013505 int error = FALSE;
13506
13507 i = get_tv_number_chk(&argvars[1], &error);
13508 if (error)
13509 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013510 if (i == 1)
13511 item_compare_ic = TRUE;
13512 else
13513 item_compare_func = get_tv_string(&argvars[1]);
13514 }
13515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013516
Bram Moolenaar0d660222005-01-07 21:51:51 +000013517 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013518 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013519 if (ptrs == NULL)
13520 return;
13521 i = 0;
13522 for (li = l->lv_first; li != NULL; li = li->li_next)
13523 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013524
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013525 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013526 /* test the compare function */
13527 if (item_compare_func != NULL
13528 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
13529 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013530 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013531 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000013532 {
13533 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013534 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000013535 item_compare_func == NULL ? item_compare : item_compare2);
13536
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013537 if (!item_compare_func_err)
13538 {
13539 /* Clear the List and append the items in the sorted order. */
13540 l->lv_first = l->lv_last = NULL;
13541 l->lv_len = 0;
13542 for (i = 0; i < len; ++i)
13543 list_append(l, ptrs[i]);
13544 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013545 }
13546
13547 vim_free(ptrs);
13548 }
13549}
13550
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013551/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000013552 * "soundfold({word})" function
13553 */
13554 static void
13555f_soundfold(argvars, rettv)
13556 typval_T *argvars;
13557 typval_T *rettv;
13558{
13559 char_u *s;
13560
13561 rettv->v_type = VAR_STRING;
13562 s = get_tv_string(&argvars[0]);
13563#ifdef FEAT_SYN_HL
13564 rettv->vval.v_string = eval_soundfold(s);
13565#else
13566 rettv->vval.v_string = vim_strsave(s);
13567#endif
13568}
13569
13570/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013571 * "spellbadword()" function
13572 */
13573/* ARGSUSED */
13574 static void
13575f_spellbadword(argvars, rettv)
13576 typval_T *argvars;
13577 typval_T *rettv;
13578{
13579 int attr;
13580 char_u *ptr;
13581 int len;
13582
13583 rettv->vval.v_string = NULL;
13584 rettv->v_type = VAR_STRING;
13585
13586#ifdef FEAT_SYN_HL
13587 /* Find the start of the badly spelled word. */
13588 if (spell_move_to(FORWARD, TRUE, TRUE) == FAIL)
13589 return;
13590
13591 /* Get the length of the word and copy it. */
13592 ptr = ml_get_cursor();
Bram Moolenaar51ac12f2005-07-02 23:21:11 +000013593 len = spell_check(curwin, ptr, &attr, NULL);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013594 rettv->vval.v_string = vim_strnsave(ptr, len);
13595#endif
13596}
13597
13598/*
13599 * "spellsuggest()" function
13600 */
13601 static void
13602f_spellsuggest(argvars, rettv)
13603 typval_T *argvars;
13604 typval_T *rettv;
13605{
13606 char_u *str;
13607 int maxcount;
13608 garray_T ga;
13609 list_T *l;
13610 listitem_T *li;
13611 int i;
13612
13613 l = list_alloc();
13614 if (l == NULL)
13615 return;
13616 rettv->v_type = VAR_LIST;
13617 rettv->vval.v_list = l;
13618 ++l->lv_refcount;
13619
13620#ifdef FEAT_SYN_HL
13621 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
13622 {
13623 str = get_tv_string(&argvars[0]);
13624 if (argvars[1].v_type != VAR_UNKNOWN)
13625 {
13626 maxcount = get_tv_number(&argvars[1]);
13627 if (maxcount <= 0)
13628 return;
13629 }
13630 else
13631 maxcount = 25;
13632
13633 spell_suggest_list(&ga, str, maxcount);
13634
13635 for (i = 0; i < ga.ga_len; ++i)
13636 {
13637 str = ((char_u **)ga.ga_data)[i];
13638
13639 li = listitem_alloc();
13640 if (li == NULL)
13641 vim_free(str);
13642 else
13643 {
13644 li->li_tv.v_type = VAR_STRING;
13645 li->li_tv.v_lock = 0;
13646 li->li_tv.vval.v_string = str;
13647 list_append(l, li);
13648 }
13649 }
13650 ga_clear(&ga);
13651 }
13652#endif
13653}
13654
Bram Moolenaar0d660222005-01-07 21:51:51 +000013655 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013656f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013657 typval_T *argvars;
13658 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013659{
13660 char_u *str;
13661 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013662 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013663 regmatch_T regmatch;
13664 char_u patbuf[NUMBUFLEN];
13665 char_u *save_cpo;
13666 int match;
Bram Moolenaar33570922005-01-25 22:26:29 +000013667 listitem_T *ni;
13668 list_T *l;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013669 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013670 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013671 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013672
13673 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13674 save_cpo = p_cpo;
13675 p_cpo = (char_u *)"";
13676
13677 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013678 if (argvars[1].v_type != VAR_UNKNOWN)
13679 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013680 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13681 if (pat == NULL)
13682 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013683 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013684 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013685 }
13686 if (pat == NULL || *pat == NUL)
13687 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000013688
13689 l = list_alloc();
13690 if (l == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013691 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013692 rettv->v_type = VAR_LIST;
13693 rettv->vval.v_list = l;
13694 ++l->lv_refcount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013695 if (typeerr)
13696 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013697
Bram Moolenaar0d660222005-01-07 21:51:51 +000013698 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13699 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013700 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013701 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013702 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013703 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013704 if (*str == NUL)
13705 match = FALSE; /* empty item at the end */
13706 else
13707 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013708 if (match)
13709 end = regmatch.startp[0];
13710 else
13711 end = str + STRLEN(str);
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013712 if (keepempty || end > str || (l->lv_len > 0 && *str != NUL
13713 && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013714 {
13715 ni = listitem_alloc();
13716 if (ni == NULL)
13717 break;
13718 ni->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013719 ni->li_tv.v_lock = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013720 ni->li_tv.vval.v_string = vim_strnsave(str, end - str);
13721 list_append(l, ni);
13722 }
13723 if (!match)
13724 break;
13725 /* Advance to just after the match. */
13726 if (regmatch.endp[0] > str)
13727 col = 0;
13728 else
13729 {
13730 /* Don't get stuck at the same match. */
13731#ifdef FEAT_MBYTE
13732 col = mb_ptr2len_check(regmatch.endp[0]);
13733#else
13734 col = 1;
13735#endif
13736 }
13737 str = regmatch.endp[0];
13738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013739
Bram Moolenaar0d660222005-01-07 21:51:51 +000013740 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013742
Bram Moolenaar0d660222005-01-07 21:51:51 +000013743 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013744}
13745
13746#ifdef HAVE_STRFTIME
13747/*
13748 * "strftime({format}[, {time}])" function
13749 */
13750 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013751f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013752 typval_T *argvars;
13753 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013754{
13755 char_u result_buf[256];
13756 struct tm *curtime;
13757 time_t seconds;
13758 char_u *p;
13759
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013760 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013761
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013762 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013763 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013764 seconds = time(NULL);
13765 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013766 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013767 curtime = localtime(&seconds);
13768 /* MSVC returns NULL for an invalid value of seconds. */
13769 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013770 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013771 else
13772 {
13773# ifdef FEAT_MBYTE
13774 vimconv_T conv;
13775 char_u *enc;
13776
13777 conv.vc_type = CONV_NONE;
13778 enc = enc_locale();
13779 convert_setup(&conv, p_enc, enc);
13780 if (conv.vc_type != CONV_NONE)
13781 p = string_convert(&conv, p, NULL);
13782# endif
13783 if (p != NULL)
13784 (void)strftime((char *)result_buf, sizeof(result_buf),
13785 (char *)p, curtime);
13786 else
13787 result_buf[0] = NUL;
13788
13789# ifdef FEAT_MBYTE
13790 if (conv.vc_type != CONV_NONE)
13791 vim_free(p);
13792 convert_setup(&conv, enc, p_enc);
13793 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013794 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013795 else
13796# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013797 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013798
13799# ifdef FEAT_MBYTE
13800 /* Release conversion descriptors */
13801 convert_setup(&conv, NULL, NULL);
13802 vim_free(enc);
13803# endif
13804 }
13805}
13806#endif
13807
13808/*
13809 * "stridx()" function
13810 */
13811 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013812f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013813 typval_T *argvars;
13814 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013815{
13816 char_u buf[NUMBUFLEN];
13817 char_u *needle;
13818 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000013819 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013820 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000013821 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013822
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013823 needle = get_tv_string_chk(&argvars[1]);
13824 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000013825 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013826 if (needle == NULL || haystack == NULL)
13827 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013828
Bram Moolenaar33570922005-01-25 22:26:29 +000013829 if (argvars[2].v_type != VAR_UNKNOWN)
13830 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013831 int error = FALSE;
13832
13833 start_idx = get_tv_number_chk(&argvars[2], &error);
13834 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000013835 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000013836 if (start_idx >= 0)
13837 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000013838 }
13839
13840 pos = (char_u *)strstr((char *)haystack, (char *)needle);
13841 if (pos != NULL)
13842 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013843}
13844
13845/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013846 * "string()" function
13847 */
13848 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013849f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013850 typval_T *argvars;
13851 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013852{
13853 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013854 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013855
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013856 rettv->v_type = VAR_STRING;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013857 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013858 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013859 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013860}
13861
13862/*
13863 * "strlen()" function
13864 */
13865 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013866f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013867 typval_T *argvars;
13868 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013869{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013870 rettv->vval.v_number = (varnumber_T)(STRLEN(
13871 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013872}
13873
13874/*
13875 * "strpart()" function
13876 */
13877 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013878f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013879 typval_T *argvars;
13880 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013881{
13882 char_u *p;
13883 int n;
13884 int len;
13885 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013886 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013887
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013888 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013889 slen = (int)STRLEN(p);
13890
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013891 n = get_tv_number_chk(&argvars[1], &error);
13892 if (error)
13893 len = 0;
13894 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013895 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013896 else
13897 len = slen - n; /* default len: all bytes that are available. */
13898
13899 /*
13900 * Only return the overlap between the specified part and the actual
13901 * string.
13902 */
13903 if (n < 0)
13904 {
13905 len += n;
13906 n = 0;
13907 }
13908 else if (n > slen)
13909 n = slen;
13910 if (len < 0)
13911 len = 0;
13912 else if (n + len > slen)
13913 len = slen - n;
13914
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013915 rettv->v_type = VAR_STRING;
13916 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013917}
13918
13919/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013920 * "strridx()" function
13921 */
13922 static void
13923f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013924 typval_T *argvars;
13925 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013926{
13927 char_u buf[NUMBUFLEN];
13928 char_u *needle;
13929 char_u *haystack;
13930 char_u *rest;
13931 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000013932 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013933
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013934 needle = get_tv_string_chk(&argvars[1]);
13935 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar532c7802005-01-27 14:44:31 +000013936 haystack_len = STRLEN(haystack);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013937
13938 rettv->vval.v_number = -1;
13939 if (needle == NULL || haystack == NULL)
13940 return; /* type error; errmsg already given */
Bram Moolenaar05159a02005-02-26 23:04:13 +000013941 if (argvars[2].v_type != VAR_UNKNOWN)
13942 {
13943 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013944 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000013945 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013946 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000013947 }
13948 else
13949 end_idx = haystack_len;
13950
Bram Moolenaar0d660222005-01-07 21:51:51 +000013951 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000013952 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013953 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000013954 lastmatch = haystack + end_idx;
13955 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013956 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000013957 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013958 for (rest = haystack; *rest != '\0'; ++rest)
13959 {
13960 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000013961 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013962 break;
13963 lastmatch = rest;
13964 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000013965 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013966
13967 if (lastmatch == NULL)
13968 rettv->vval.v_number = -1;
13969 else
13970 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
13971}
13972
13973/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013974 * "strtrans()" function
13975 */
13976 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013977f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013978 typval_T *argvars;
13979 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013980{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013981 rettv->v_type = VAR_STRING;
13982 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013983}
13984
13985/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013986 * "submatch()" function
13987 */
13988 static void
13989f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013990 typval_T *argvars;
13991 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013992{
13993 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013994 rettv->vval.v_string =
13995 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013996}
13997
13998/*
13999 * "substitute()" function
14000 */
14001 static void
14002f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014003 typval_T *argvars;
14004 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014005{
14006 char_u patbuf[NUMBUFLEN];
14007 char_u subbuf[NUMBUFLEN];
14008 char_u flagsbuf[NUMBUFLEN];
14009
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014010 char_u *str = get_tv_string_chk(&argvars[0]);
14011 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14012 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
14013 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
14014
Bram Moolenaar0d660222005-01-07 21:51:51 +000014015 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014016 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
14017 rettv->vval.v_string = NULL;
14018 else
14019 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014020}
14021
14022/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014023 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014024 */
14025/*ARGSUSED*/
14026 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014027f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014028 typval_T *argvars;
14029 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014030{
14031 int id = 0;
14032#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014033 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014034 long col;
14035 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000014036 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014037
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014038 lnum = get_tv_lnum(argvars); /* -1 on type error */
14039 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
14040 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014041
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014042 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014043 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
14044 id = syn_get_id(lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014045#endif
14046
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014047 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014048}
14049
14050/*
14051 * "synIDattr(id, what [, mode])" function
14052 */
14053/*ARGSUSED*/
14054 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014055f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014056 typval_T *argvars;
14057 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014058{
14059 char_u *p = NULL;
14060#ifdef FEAT_SYN_HL
14061 int id;
14062 char_u *what;
14063 char_u *mode;
14064 char_u modebuf[NUMBUFLEN];
14065 int modec;
14066
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014067 id = get_tv_number(&argvars[0]);
14068 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014069 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014070 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014071 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014072 modec = TOLOWER_ASC(mode[0]);
14073 if (modec != 't' && modec != 'c'
14074#ifdef FEAT_GUI
14075 && modec != 'g'
14076#endif
14077 )
14078 modec = 0; /* replace invalid with current */
14079 }
14080 else
14081 {
14082#ifdef FEAT_GUI
14083 if (gui.in_use)
14084 modec = 'g';
14085 else
14086#endif
14087 if (t_colors > 1)
14088 modec = 'c';
14089 else
14090 modec = 't';
14091 }
14092
14093
14094 switch (TOLOWER_ASC(what[0]))
14095 {
14096 case 'b':
14097 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
14098 p = highlight_color(id, what, modec);
14099 else /* bold */
14100 p = highlight_has_attr(id, HL_BOLD, modec);
14101 break;
14102
14103 case 'f': /* fg[#] */
14104 p = highlight_color(id, what, modec);
14105 break;
14106
14107 case 'i':
14108 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
14109 p = highlight_has_attr(id, HL_INVERSE, modec);
14110 else /* italic */
14111 p = highlight_has_attr(id, HL_ITALIC, modec);
14112 break;
14113
14114 case 'n': /* name */
14115 p = get_highlight_name(NULL, id - 1);
14116 break;
14117
14118 case 'r': /* reverse */
14119 p = highlight_has_attr(id, HL_INVERSE, modec);
14120 break;
14121
14122 case 's': /* standout */
14123 p = highlight_has_attr(id, HL_STANDOUT, modec);
14124 break;
14125
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000014126 case 'u':
14127 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
14128 /* underline */
14129 p = highlight_has_attr(id, HL_UNDERLINE, modec);
14130 else
14131 /* undercurl */
14132 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014133 break;
14134 }
14135
14136 if (p != NULL)
14137 p = vim_strsave(p);
14138#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014139 rettv->v_type = VAR_STRING;
14140 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014141}
14142
14143/*
14144 * "synIDtrans(id)" function
14145 */
14146/*ARGSUSED*/
14147 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014148f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014149 typval_T *argvars;
14150 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014151{
14152 int id;
14153
14154#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014155 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014156
14157 if (id > 0)
14158 id = syn_get_final_id(id);
14159 else
14160#endif
14161 id = 0;
14162
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014163 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014164}
14165
14166/*
14167 * "system()" function
14168 */
14169 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014170f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014171 typval_T *argvars;
14172 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014173{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014174 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014175 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014176 char_u *infile = NULL;
14177 char_u buf[NUMBUFLEN];
14178 int err = FALSE;
14179 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014180
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014181 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014182 {
14183 /*
14184 * Write the string to a temp file, to be used for input of the shell
14185 * command.
14186 */
14187 if ((infile = vim_tempname('i')) == NULL)
14188 {
14189 EMSG(_(e_notmp));
14190 return;
14191 }
14192
14193 fd = mch_fopen((char *)infile, WRITEBIN);
14194 if (fd == NULL)
14195 {
14196 EMSG2(_(e_notopen), infile);
14197 goto done;
14198 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014199 p = get_tv_string_buf_chk(&argvars[1], buf);
14200 if (p == NULL)
14201 goto done; /* type error; errmsg already given */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014202 if (fwrite(p, STRLEN(p), 1, fd) != 1)
14203 err = TRUE;
14204 if (fclose(fd) != 0)
14205 err = TRUE;
14206 if (err)
14207 {
14208 EMSG(_("E677: Error writing temp file"));
14209 goto done;
14210 }
14211 }
14212
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014213 res = get_cmd_output(get_tv_string(&argvars[0]), infile, SHELL_SILENT);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014214
Bram Moolenaar071d4272004-06-13 20:20:40 +000014215#ifdef USE_CR
14216 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014217 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014218 {
14219 char_u *s;
14220
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014221 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014222 {
14223 if (*s == CAR)
14224 *s = NL;
14225 }
14226 }
14227#else
14228# ifdef USE_CRNL
14229 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014230 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014231 {
14232 char_u *s, *d;
14233
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014234 d = res;
14235 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014236 {
14237 if (s[0] == CAR && s[1] == NL)
14238 ++s;
14239 *d++ = *s;
14240 }
14241 *d = NUL;
14242 }
14243# endif
14244#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014245
14246done:
14247 if (infile != NULL)
14248 {
14249 mch_remove(infile);
14250 vim_free(infile);
14251 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014252 rettv->v_type = VAR_STRING;
14253 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014254}
14255
14256/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000014257 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014258 */
14259 static void
14260f_taglist(argvars, rettv)
14261 typval_T *argvars;
14262 typval_T *rettv;
14263{
14264 char_u *tag_pattern;
14265 list_T *l;
14266
14267 tag_pattern = get_tv_string(&argvars[0]);
14268
14269 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014270 if (*tag_pattern == NUL)
14271 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014272
14273 l = list_alloc();
14274 if (l != NULL)
14275 {
14276 if (get_tags(l, tag_pattern) != FAIL)
14277 {
14278 rettv->vval.v_list = l;
14279 rettv->v_type = VAR_LIST;
14280 ++l->lv_refcount;
14281 }
14282 else
14283 list_free(l);
14284 }
14285}
14286
14287/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014288 * "tempname()" function
14289 */
14290/*ARGSUSED*/
14291 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014292f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014293 typval_T *argvars;
14294 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014295{
14296 static int x = 'A';
14297
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014298 rettv->v_type = VAR_STRING;
14299 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014300
14301 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
14302 * names. Skip 'I' and 'O', they are used for shell redirection. */
14303 do
14304 {
14305 if (x == 'Z')
14306 x = '0';
14307 else if (x == '9')
14308 x = 'A';
14309 else
14310 {
14311#ifdef EBCDIC
14312 if (x == 'I')
14313 x = 'J';
14314 else if (x == 'R')
14315 x = 'S';
14316 else
14317#endif
14318 ++x;
14319 }
14320 } while (x == 'I' || x == 'O');
14321}
14322
14323/*
14324 * "tolower(string)" function
14325 */
14326 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014327f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014328 typval_T *argvars;
14329 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014330{
14331 char_u *p;
14332
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014333 p = vim_strsave(get_tv_string(&argvars[0]));
14334 rettv->v_type = VAR_STRING;
14335 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014336
14337 if (p != NULL)
14338 while (*p != NUL)
14339 {
14340#ifdef FEAT_MBYTE
14341 int l;
14342
14343 if (enc_utf8)
14344 {
14345 int c, lc;
14346
14347 c = utf_ptr2char(p);
14348 lc = utf_tolower(c);
14349 l = utf_ptr2len_check(p);
14350 /* TODO: reallocate string when byte count changes. */
14351 if (utf_char2len(lc) == l)
14352 utf_char2bytes(lc, p);
14353 p += l;
14354 }
14355 else if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
14356 p += l; /* skip multi-byte character */
14357 else
14358#endif
14359 {
14360 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
14361 ++p;
14362 }
14363 }
14364}
14365
14366/*
14367 * "toupper(string)" function
14368 */
14369 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014370f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014371 typval_T *argvars;
14372 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014373{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014374 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014375 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014376}
14377
14378/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000014379 * "tr(string, fromstr, tostr)" function
14380 */
14381 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014382f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014383 typval_T *argvars;
14384 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014385{
14386 char_u *instr;
14387 char_u *fromstr;
14388 char_u *tostr;
14389 char_u *p;
14390#ifdef FEAT_MBYTE
14391 int inlen;
14392 int fromlen;
14393 int tolen;
14394 int idx;
14395 char_u *cpstr;
14396 int cplen;
14397 int first = TRUE;
14398#endif
14399 char_u buf[NUMBUFLEN];
14400 char_u buf2[NUMBUFLEN];
14401 garray_T ga;
14402
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014403 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014404 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
14405 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014406
14407 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014408 rettv->v_type = VAR_STRING;
14409 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014410 if (fromstr == NULL || tostr == NULL)
14411 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000014412 ga_init2(&ga, (int)sizeof(char), 80);
14413
14414#ifdef FEAT_MBYTE
14415 if (!has_mbyte)
14416#endif
14417 /* not multi-byte: fromstr and tostr must be the same length */
14418 if (STRLEN(fromstr) != STRLEN(tostr))
14419 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014420#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000014421error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014422#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000014423 EMSG2(_(e_invarg2), fromstr);
14424 ga_clear(&ga);
14425 return;
14426 }
14427
14428 /* fromstr and tostr have to contain the same number of chars */
14429 while (*instr != NUL)
14430 {
14431#ifdef FEAT_MBYTE
14432 if (has_mbyte)
14433 {
14434 inlen = mb_ptr2len_check(instr);
14435 cpstr = instr;
14436 cplen = inlen;
14437 idx = 0;
14438 for (p = fromstr; *p != NUL; p += fromlen)
14439 {
14440 fromlen = mb_ptr2len_check(p);
14441 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
14442 {
14443 for (p = tostr; *p != NUL; p += tolen)
14444 {
14445 tolen = mb_ptr2len_check(p);
14446 if (idx-- == 0)
14447 {
14448 cplen = tolen;
14449 cpstr = p;
14450 break;
14451 }
14452 }
14453 if (*p == NUL) /* tostr is shorter than fromstr */
14454 goto error;
14455 break;
14456 }
14457 ++idx;
14458 }
14459
14460 if (first && cpstr == instr)
14461 {
14462 /* Check that fromstr and tostr have the same number of
14463 * (multi-byte) characters. Done only once when a character
14464 * of instr doesn't appear in fromstr. */
14465 first = FALSE;
14466 for (p = tostr; *p != NUL; p += tolen)
14467 {
14468 tolen = mb_ptr2len_check(p);
14469 --idx;
14470 }
14471 if (idx != 0)
14472 goto error;
14473 }
14474
14475 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000014476 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014477 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014478
14479 instr += inlen;
14480 }
14481 else
14482#endif
14483 {
14484 /* When not using multi-byte chars we can do it faster. */
14485 p = vim_strchr(fromstr, *instr);
14486 if (p != NULL)
14487 ga_append(&ga, tostr[p - fromstr]);
14488 else
14489 ga_append(&ga, *instr);
14490 ++instr;
14491 }
14492 }
14493
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014494 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014495}
14496
14497/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014498 * "type(expr)" function
14499 */
14500 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014501f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014502 typval_T *argvars;
14503 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014504{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014505 int n;
14506
14507 switch (argvars[0].v_type)
14508 {
14509 case VAR_NUMBER: n = 0; break;
14510 case VAR_STRING: n = 1; break;
14511 case VAR_FUNC: n = 2; break;
14512 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014513 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014514 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
14515 }
14516 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014517}
14518
14519/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014520 * "values(dict)" function
14521 */
14522 static void
14523f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014524 typval_T *argvars;
14525 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014526{
14527 dict_list(argvars, rettv, 1);
14528}
14529
14530/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014531 * "virtcol(string)" function
14532 */
14533 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014534f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014535 typval_T *argvars;
14536 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014537{
14538 colnr_T vcol = 0;
14539 pos_T *fp;
14540
14541 fp = var2fpos(&argvars[0], FALSE);
14542 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count)
14543 {
14544 getvvcol(curwin, fp, NULL, NULL, &vcol);
14545 ++vcol;
14546 }
14547
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014548 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014549}
14550
14551/*
14552 * "visualmode()" function
14553 */
14554/*ARGSUSED*/
14555 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014556f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014557 typval_T *argvars;
14558 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014559{
14560#ifdef FEAT_VISUAL
14561 char_u str[2];
14562
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014563 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014564 str[0] = curbuf->b_visual_mode_eval;
14565 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014566 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014567
14568 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014569 if ((argvars[0].v_type == VAR_NUMBER
14570 && argvars[0].vval.v_number != 0)
14571 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014572 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014573 curbuf->b_visual_mode_eval = NUL;
14574#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014575 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014576#endif
14577}
14578
14579/*
14580 * "winbufnr(nr)" function
14581 */
14582 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014583f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014584 typval_T *argvars;
14585 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014586{
14587 win_T *wp;
14588
14589 wp = find_win_by_nr(&argvars[0]);
14590 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014591 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014592 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014593 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014594}
14595
14596/*
14597 * "wincol()" function
14598 */
14599/*ARGSUSED*/
14600 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014601f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014602 typval_T *argvars;
14603 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014604{
14605 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014606 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014607}
14608
14609/*
14610 * "winheight(nr)" function
14611 */
14612 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014613f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014614 typval_T *argvars;
14615 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014616{
14617 win_T *wp;
14618
14619 wp = find_win_by_nr(&argvars[0]);
14620 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014621 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014622 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014623 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014624}
14625
14626/*
14627 * "winline()" function
14628 */
14629/*ARGSUSED*/
14630 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014631f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014632 typval_T *argvars;
14633 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014634{
14635 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014636 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014637}
14638
14639/*
14640 * "winnr()" function
14641 */
14642/* ARGSUSED */
14643 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014644f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014645 typval_T *argvars;
14646 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014647{
14648 int nr = 1;
14649#ifdef FEAT_WINDOWS
14650 win_T *wp;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014651 win_T *twin = curwin;
14652 char_u *arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014653
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014654 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014655 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014656 arg = get_tv_string_chk(&argvars[0]);
14657 if (arg == NULL)
14658 nr = 0; /* type error; errmsg already given */
14659 else if (STRCMP(arg, "$") == 0)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014660 twin = lastwin;
14661 else if (STRCMP(arg, "#") == 0)
14662 {
14663 twin = prevwin;
14664 if (prevwin == NULL)
14665 nr = 0;
14666 }
14667 else
14668 {
14669 EMSG2(_(e_invexpr2), arg);
14670 nr = 0;
14671 }
14672 }
14673
14674 if (nr > 0)
14675 for (wp = firstwin; wp != twin; wp = wp->w_next)
14676 ++nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014677#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014678 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014679}
14680
14681/*
14682 * "winrestcmd()" function
14683 */
14684/* ARGSUSED */
14685 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014686f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014687 typval_T *argvars;
14688 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014689{
14690#ifdef FEAT_WINDOWS
14691 win_T *wp;
14692 int winnr = 1;
14693 garray_T ga;
14694 char_u buf[50];
14695
14696 ga_init2(&ga, (int)sizeof(char), 70);
14697 for (wp = firstwin; wp != NULL; wp = wp->w_next)
14698 {
14699 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
14700 ga_concat(&ga, buf);
14701# ifdef FEAT_VERTSPLIT
14702 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
14703 ga_concat(&ga, buf);
14704# endif
14705 ++winnr;
14706 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000014707 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014708
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014709 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014710#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014711 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014712#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014713 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014714}
14715
14716/*
14717 * "winwidth(nr)" function
14718 */
14719 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014720f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014721 typval_T *argvars;
14722 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014723{
14724 win_T *wp;
14725
14726 wp = find_win_by_nr(&argvars[0]);
14727 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014728 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014729 else
14730#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014731 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014732#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014733 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014734#endif
14735}
14736
Bram Moolenaar071d4272004-06-13 20:20:40 +000014737/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014738 * "writefile()" function
14739 */
14740 static void
14741f_writefile(argvars, rettv)
14742 typval_T *argvars;
14743 typval_T *rettv;
14744{
14745 int binary = FALSE;
14746 char_u *fname;
14747 FILE *fd;
14748 listitem_T *li;
14749 char_u *s;
14750 int ret = 0;
14751 int c;
14752
14753 if (argvars[0].v_type != VAR_LIST)
14754 {
14755 EMSG2(_(e_listarg), "writefile()");
14756 return;
14757 }
14758 if (argvars[0].vval.v_list == NULL)
14759 return;
14760
14761 if (argvars[2].v_type != VAR_UNKNOWN
14762 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
14763 binary = TRUE;
14764
14765 /* Always open the file in binary mode, library functions have a mind of
14766 * their own about CR-LF conversion. */
14767 fname = get_tv_string(&argvars[1]);
14768 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
14769 {
14770 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
14771 ret = -1;
14772 }
14773 else
14774 {
14775 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
14776 li = li->li_next)
14777 {
14778 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
14779 {
14780 if (*s == '\n')
14781 c = putc(NUL, fd);
14782 else
14783 c = putc(*s, fd);
14784 if (c == EOF)
14785 {
14786 ret = -1;
14787 break;
14788 }
14789 }
14790 if (!binary || li->li_next != NULL)
14791 if (putc('\n', fd) == EOF)
14792 {
14793 ret = -1;
14794 break;
14795 }
14796 if (ret < 0)
14797 {
14798 EMSG(_(e_write));
14799 break;
14800 }
14801 }
14802 fclose(fd);
14803 }
14804
14805 rettv->vval.v_number = ret;
14806}
14807
14808/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014809 * Translate a String variable into a position.
14810 */
14811 static pos_T *
14812var2fpos(varp, lnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000014813 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014814 int lnum; /* TRUE when $ is last line */
14815{
14816 char_u *name;
14817 static pos_T pos;
14818 pos_T *pp;
14819
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014820 name = get_tv_string_chk(varp);
14821 if (name == NULL)
14822 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014823 if (name[0] == '.') /* cursor */
14824 return &curwin->w_cursor;
14825 if (name[0] == '\'') /* mark */
14826 {
14827 pp = getmark(name[1], FALSE);
14828 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
14829 return NULL;
14830 return pp;
14831 }
14832 if (name[0] == '$') /* last column or line */
14833 {
14834 if (lnum)
14835 {
14836 pos.lnum = curbuf->b_ml.ml_line_count;
14837 pos.col = 0;
14838 }
14839 else
14840 {
14841 pos.lnum = curwin->w_cursor.lnum;
14842 pos.col = (colnr_T)STRLEN(ml_get_curline());
14843 }
14844 return &pos;
14845 }
14846 return NULL;
14847}
14848
14849/*
14850 * Get the length of an environment variable name.
14851 * Advance "arg" to the first character after the name.
14852 * Return 0 for error.
14853 */
14854 static int
14855get_env_len(arg)
14856 char_u **arg;
14857{
14858 char_u *p;
14859 int len;
14860
14861 for (p = *arg; vim_isIDc(*p); ++p)
14862 ;
14863 if (p == *arg) /* no name found */
14864 return 0;
14865
14866 len = (int)(p - *arg);
14867 *arg = p;
14868 return len;
14869}
14870
14871/*
14872 * Get the length of the name of a function or internal variable.
14873 * "arg" is advanced to the first non-white character after the name.
14874 * Return 0 if something is wrong.
14875 */
14876 static int
14877get_id_len(arg)
14878 char_u **arg;
14879{
14880 char_u *p;
14881 int len;
14882
14883 /* Find the end of the name. */
14884 for (p = *arg; eval_isnamec(*p); ++p)
14885 ;
14886 if (p == *arg) /* no name found */
14887 return 0;
14888
14889 len = (int)(p - *arg);
14890 *arg = skipwhite(p);
14891
14892 return len;
14893}
14894
14895/*
Bram Moolenaara7043832005-01-21 11:56:39 +000014896 * Get the length of the name of a variable or function.
14897 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000014898 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014899 * Return -1 if curly braces expansion failed.
14900 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014901 * If the name contains 'magic' {}'s, expand them and return the
14902 * expanded name in an allocated string via 'alias' - caller must free.
14903 */
14904 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014905get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014906 char_u **arg;
14907 char_u **alias;
14908 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014909 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014910{
14911 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014912 char_u *p;
14913 char_u *expr_start;
14914 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014915
14916 *alias = NULL; /* default to no alias */
14917
14918 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
14919 && (*arg)[2] == (int)KE_SNR)
14920 {
14921 /* hard coded <SNR>, already translated */
14922 *arg += 3;
14923 return get_id_len(arg) + 3;
14924 }
14925 len = eval_fname_script(*arg);
14926 if (len > 0)
14927 {
14928 /* literal "<SID>", "s:" or "<SNR>" */
14929 *arg += len;
14930 }
14931
Bram Moolenaar071d4272004-06-13 20:20:40 +000014932 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014933 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014934 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014935 p = find_name_end(*arg, &expr_start, &expr_end,
14936 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014937 if (expr_start != NULL)
14938 {
14939 char_u *temp_string;
14940
14941 if (!evaluate)
14942 {
14943 len += (int)(p - *arg);
14944 *arg = skipwhite(p);
14945 return len;
14946 }
14947
14948 /*
14949 * Include any <SID> etc in the expanded string:
14950 * Thus the -len here.
14951 */
14952 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
14953 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014954 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014955 *alias = temp_string;
14956 *arg = skipwhite(p);
14957 return (int)STRLEN(temp_string);
14958 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014959
14960 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014961 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014962 EMSG2(_(e_invexpr2), *arg);
14963
14964 return len;
14965}
14966
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014967/*
14968 * Find the end of a variable or function name, taking care of magic braces.
14969 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
14970 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014971 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014972 * Return a pointer to just after the name. Equal to "arg" if there is no
14973 * valid name.
14974 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014975 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014976find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014977 char_u *arg;
14978 char_u **expr_start;
14979 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014980 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014981{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014982 int mb_nest = 0;
14983 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014984 char_u *p;
14985
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014986 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014987 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014988 *expr_start = NULL;
14989 *expr_end = NULL;
14990 }
14991
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014992 /* Quick check for valid starting character. */
14993 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
14994 return arg;
14995
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014996 for (p = arg; *p != NUL
14997 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014998 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014999 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015000 || mb_nest != 0
15001 || br_nest != 0); ++p)
15002 {
15003 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015004 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015005 if (*p == '[')
15006 ++br_nest;
15007 else if (*p == ']')
15008 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015009 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015010 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015011 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015012 if (*p == '{')
15013 {
15014 mb_nest++;
15015 if (expr_start != NULL && *expr_start == NULL)
15016 *expr_start = p;
15017 }
15018 else if (*p == '}')
15019 {
15020 mb_nest--;
15021 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
15022 *expr_end = p;
15023 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015025 }
15026
15027 return p;
15028}
15029
15030/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015031 * Expands out the 'magic' {}'s in a variable/function name.
15032 * Note that this can call itself recursively, to deal with
15033 * constructs like foo{bar}{baz}{bam}
15034 * The four pointer arguments point to "foo{expre}ss{ion}bar"
15035 * "in_start" ^
15036 * "expr_start" ^
15037 * "expr_end" ^
15038 * "in_end" ^
15039 *
15040 * Returns a new allocated string, which the caller must free.
15041 * Returns NULL for failure.
15042 */
15043 static char_u *
15044make_expanded_name(in_start, expr_start, expr_end, in_end)
15045 char_u *in_start;
15046 char_u *expr_start;
15047 char_u *expr_end;
15048 char_u *in_end;
15049{
15050 char_u c1;
15051 char_u *retval = NULL;
15052 char_u *temp_result;
15053 char_u *nextcmd = NULL;
15054
15055 if (expr_end == NULL || in_end == NULL)
15056 return NULL;
15057 *expr_start = NUL;
15058 *expr_end = NUL;
15059 c1 = *in_end;
15060 *in_end = NUL;
15061
15062 temp_result = eval_to_string(expr_start + 1, &nextcmd);
15063 if (temp_result != NULL && nextcmd == NULL)
15064 {
15065 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
15066 + (in_end - expr_end) + 1));
15067 if (retval != NULL)
15068 {
15069 STRCPY(retval, in_start);
15070 STRCAT(retval, temp_result);
15071 STRCAT(retval, expr_end + 1);
15072 }
15073 }
15074 vim_free(temp_result);
15075
15076 *in_end = c1; /* put char back for error messages */
15077 *expr_start = '{';
15078 *expr_end = '}';
15079
15080 if (retval != NULL)
15081 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015082 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015083 if (expr_start != NULL)
15084 {
15085 /* Further expansion! */
15086 temp_result = make_expanded_name(retval, expr_start,
15087 expr_end, temp_result);
15088 vim_free(retval);
15089 retval = temp_result;
15090 }
15091 }
15092
15093 return retval;
15094}
15095
15096/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015097 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000015098 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015099 */
15100 static int
15101eval_isnamec(c)
15102 int c;
15103{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015104 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
15105}
15106
15107/*
15108 * Return TRUE if character "c" can be used as the first character in a
15109 * variable or function name (excluding '{' and '}').
15110 */
15111 static int
15112eval_isnamec1(c)
15113 int c;
15114{
15115 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000015116}
15117
15118/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015119 * Set number v: variable to "val".
15120 */
15121 void
15122set_vim_var_nr(idx, val)
15123 int idx;
15124 long val;
15125{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015126 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015127}
15128
15129/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015130 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015131 */
15132 long
15133get_vim_var_nr(idx)
15134 int idx;
15135{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015136 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015137}
15138
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015139#if defined(FEAT_AUTOCMD) || defined(PROTO)
15140/*
15141 * Get string v: variable value. Uses a static buffer, can only be used once.
15142 */
15143 char_u *
15144get_vim_var_str(idx)
15145 int idx;
15146{
15147 return get_tv_string(&vimvars[idx].vv_tv);
15148}
15149#endif
15150
Bram Moolenaar071d4272004-06-13 20:20:40 +000015151/*
15152 * Set v:count, v:count1 and v:prevcount.
15153 */
15154 void
15155set_vcount(count, count1)
15156 long count;
15157 long count1;
15158{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015159 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
15160 vimvars[VV_COUNT].vv_nr = count;
15161 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015162}
15163
15164/*
15165 * Set string v: variable to a copy of "val".
15166 */
15167 void
15168set_vim_var_string(idx, val, len)
15169 int idx;
15170 char_u *val;
15171 int len; /* length of "val" to use or -1 (whole string) */
15172{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015173 /* Need to do this (at least) once, since we can't initialize a union.
15174 * Will always be invoked when "v:progname" is set. */
15175 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
15176
Bram Moolenaare9a41262005-01-15 22:18:47 +000015177 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015178 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015179 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015180 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015181 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015182 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000015183 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015184}
15185
15186/*
15187 * Set v:register if needed.
15188 */
15189 void
15190set_reg_var(c)
15191 int c;
15192{
15193 char_u regname;
15194
15195 if (c == 0 || c == ' ')
15196 regname = '"';
15197 else
15198 regname = c;
15199 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000015200 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015201 set_vim_var_string(VV_REG, &regname, 1);
15202}
15203
15204/*
15205 * Get or set v:exception. If "oldval" == NULL, return the current value.
15206 * Otherwise, restore the value to "oldval" and return NULL.
15207 * Must always be called in pairs to save and restore v:exception! Does not
15208 * take care of memory allocations.
15209 */
15210 char_u *
15211v_exception(oldval)
15212 char_u *oldval;
15213{
15214 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015215 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015216
Bram Moolenaare9a41262005-01-15 22:18:47 +000015217 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015218 return NULL;
15219}
15220
15221/*
15222 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
15223 * Otherwise, restore the value to "oldval" and return NULL.
15224 * Must always be called in pairs to save and restore v:throwpoint! Does not
15225 * take care of memory allocations.
15226 */
15227 char_u *
15228v_throwpoint(oldval)
15229 char_u *oldval;
15230{
15231 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015232 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015233
Bram Moolenaare9a41262005-01-15 22:18:47 +000015234 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015235 return NULL;
15236}
15237
15238#if defined(FEAT_AUTOCMD) || defined(PROTO)
15239/*
15240 * Set v:cmdarg.
15241 * If "eap" != NULL, use "eap" to generate the value and return the old value.
15242 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
15243 * Must always be called in pairs!
15244 */
15245 char_u *
15246set_cmdarg(eap, oldarg)
15247 exarg_T *eap;
15248 char_u *oldarg;
15249{
15250 char_u *oldval;
15251 char_u *newval;
15252 unsigned len;
15253
Bram Moolenaare9a41262005-01-15 22:18:47 +000015254 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015255 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015256 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015257 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000015258 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015259 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015260 }
15261
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015262 if (eap->force_bin == FORCE_BIN)
15263 len = 6;
15264 else if (eap->force_bin == FORCE_NOBIN)
15265 len = 8;
15266 else
15267 len = 0;
15268 if (eap->force_ff != 0)
15269 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
15270# ifdef FEAT_MBYTE
15271 if (eap->force_enc != 0)
15272 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
15273# endif
15274
15275 newval = alloc(len + 1);
15276 if (newval == NULL)
15277 return NULL;
15278
15279 if (eap->force_bin == FORCE_BIN)
15280 sprintf((char *)newval, " ++bin");
15281 else if (eap->force_bin == FORCE_NOBIN)
15282 sprintf((char *)newval, " ++nobin");
15283 else
15284 *newval = NUL;
15285 if (eap->force_ff != 0)
15286 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
15287 eap->cmd + eap->force_ff);
15288# ifdef FEAT_MBYTE
15289 if (eap->force_enc != 0)
15290 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
15291 eap->cmd + eap->force_enc);
15292# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000015293 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015294 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015295}
15296#endif
15297
15298/*
15299 * Get the value of internal variable "name".
15300 * Return OK or FAIL.
15301 */
15302 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015303get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015304 char_u *name;
15305 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000015306 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015307 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015308{
15309 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000015310 typval_T *tv = NULL;
15311 typval_T atv;
15312 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015313 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015314
15315 /* truncate the name, so that we can use strcmp() */
15316 cc = name[len];
15317 name[len] = NUL;
15318
15319 /*
15320 * Check for "b:changedtick".
15321 */
15322 if (STRCMP(name, "b:changedtick") == 0)
15323 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000015324 atv.v_type = VAR_NUMBER;
15325 atv.vval.v_number = curbuf->b_changedtick;
15326 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015327 }
15328
15329 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015330 * Check for user-defined variables.
15331 */
15332 else
15333 {
Bram Moolenaara7043832005-01-21 11:56:39 +000015334 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015335 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000015336 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015337 }
15338
Bram Moolenaare9a41262005-01-15 22:18:47 +000015339 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015340 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015341 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015342 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015343 ret = FAIL;
15344 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015345 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015346 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015347
15348 name[len] = cc;
15349
15350 return ret;
15351}
15352
15353/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015354 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
15355 * Also handle function call with Funcref variable: func(expr)
15356 * Can all be combined: dict.func(expr)[idx]['func'](expr)
15357 */
15358 static int
15359handle_subscript(arg, rettv, evaluate, verbose)
15360 char_u **arg;
15361 typval_T *rettv;
15362 int evaluate; /* do more than finding the end */
15363 int verbose; /* give error messages */
15364{
15365 int ret = OK;
15366 dict_T *selfdict = NULL;
15367 char_u *s;
15368 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000015369 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015370
15371 while (ret == OK
15372 && (**arg == '['
15373 || (**arg == '.' && rettv->v_type == VAR_DICT)
15374 || (**arg == '(' && rettv->v_type == VAR_FUNC))
15375 && !vim_iswhite(*(*arg - 1)))
15376 {
15377 if (**arg == '(')
15378 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000015379 /* need to copy the funcref so that we can clear rettv */
15380 functv = *rettv;
15381 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015382
15383 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000015384 s = functv.vval.v_string;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015385 ret = get_func_tv(s, STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000015386 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
15387 &len, evaluate, selfdict);
15388
15389 /* Clear the funcref afterwards, so that deleting it while
15390 * evaluating the arguments is possible (see test55). */
15391 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015392
15393 /* Stop the expression evaluation when immediately aborting on
15394 * error, or when an interrupt occurred or an exception was thrown
15395 * but not caught. */
15396 if (aborting())
15397 {
15398 if (ret == OK)
15399 clear_tv(rettv);
15400 ret = FAIL;
15401 }
15402 dict_unref(selfdict);
15403 selfdict = NULL;
15404 }
15405 else /* **arg == '[' || **arg == '.' */
15406 {
15407 dict_unref(selfdict);
15408 if (rettv->v_type == VAR_DICT)
15409 {
15410 selfdict = rettv->vval.v_dict;
15411 if (selfdict != NULL)
15412 ++selfdict->dv_refcount;
15413 }
15414 else
15415 selfdict = NULL;
15416 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
15417 {
15418 clear_tv(rettv);
15419 ret = FAIL;
15420 }
15421 }
15422 }
15423 dict_unref(selfdict);
15424 return ret;
15425}
15426
15427/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015428 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
15429 * value).
15430 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015431 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015432alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015433{
Bram Moolenaar33570922005-01-25 22:26:29 +000015434 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015435}
15436
15437/*
15438 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015439 * The string "s" must have been allocated, it is consumed.
15440 * Return NULL for out of memory, the variable otherwise.
15441 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015442 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015443alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015444 char_u *s;
15445{
Bram Moolenaar33570922005-01-25 22:26:29 +000015446 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015447
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015448 rettv = alloc_tv();
15449 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015450 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015451 rettv->v_type = VAR_STRING;
15452 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015453 }
15454 else
15455 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015456 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015457}
15458
15459/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015460 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015461 */
15462 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015463free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015464 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015465{
15466 if (varp != NULL)
15467 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015468 switch (varp->v_type)
15469 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015470 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015471 func_unref(varp->vval.v_string);
15472 /*FALLTHROUGH*/
15473 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015474 vim_free(varp->vval.v_string);
15475 break;
15476 case VAR_LIST:
15477 list_unref(varp->vval.v_list);
15478 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015479 case VAR_DICT:
15480 dict_unref(varp->vval.v_dict);
15481 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015482 case VAR_NUMBER:
15483 case VAR_UNKNOWN:
15484 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015485 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000015486 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015487 break;
15488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015489 vim_free(varp);
15490 }
15491}
15492
15493/*
15494 * Free the memory for a variable value and set the value to NULL or 0.
15495 */
15496 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015497clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015498 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015499{
15500 if (varp != NULL)
15501 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015502 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015503 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015504 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015505 func_unref(varp->vval.v_string);
15506 /*FALLTHROUGH*/
15507 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015508 vim_free(varp->vval.v_string);
15509 varp->vval.v_string = NULL;
15510 break;
15511 case VAR_LIST:
15512 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015513 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015514 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015515 case VAR_DICT:
15516 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015517 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015518 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015519 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015520 varp->vval.v_number = 0;
15521 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015522 case VAR_UNKNOWN:
15523 break;
15524 default:
15525 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000015526 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015527 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015528 }
15529}
15530
15531/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015532 * Set the value of a variable to NULL without freeing items.
15533 */
15534 static void
15535init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015536 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015537{
15538 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000015539 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015540}
15541
15542/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015543 * Get the number value of a variable.
15544 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015545 * For incompatible types, return 0.
15546 * get_tv_number_chk() is similar to get_tv_number(), but informs the
15547 * caller of incompatible types: it sets *denote to TRUE if "denote"
15548 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015549 */
15550 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015551get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015552 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015553{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015554 int error = FALSE;
15555
15556 return get_tv_number_chk(varp, &error); /* return 0L on error */
15557}
15558
15559 static long
15560get_tv_number_chk(varp, denote)
15561 typval_T *varp;
15562 int *denote;
15563{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015564 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015565
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015566 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015567 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015568 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015569 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015570 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015571 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015572 break;
15573 case VAR_STRING:
15574 if (varp->vval.v_string != NULL)
15575 vim_str2nr(varp->vval.v_string, NULL, NULL,
15576 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015577 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015578 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000015579 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015580 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015581 case VAR_DICT:
15582 EMSG(_("E728: Using a Dictionary as a number"));
15583 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015584 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015585 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015586 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015587 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015588 if (denote == NULL) /* useful for values that must be unsigned */
15589 n = -1;
15590 else
15591 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015592 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015593}
15594
15595/*
15596 * Get the lnum from the first argument. Also accepts ".", "$", etc.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015597 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015598 */
15599 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015600get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000015601 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015602{
Bram Moolenaar33570922005-01-25 22:26:29 +000015603 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015604 linenr_T lnum;
15605
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015606 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015607 if (lnum == 0) /* no valid number, try using line() */
15608 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015609 rettv.v_type = VAR_NUMBER;
15610 f_line(argvars, &rettv);
15611 lnum = rettv.vval.v_number;
15612 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015613 }
15614 return lnum;
15615}
15616
15617/*
15618 * Get the string value of a variable.
15619 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000015620 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
15621 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015622 * If the String variable has never been set, return an empty string.
15623 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015624 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
15625 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015626 */
15627 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015628get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015629 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015630{
15631 static char_u mybuf[NUMBUFLEN];
15632
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015633 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015634}
15635
15636 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015637get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000015638 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015639 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015640{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015641 char_u *res = get_tv_string_buf_chk(varp, buf);
15642
15643 return res != NULL ? res : (char_u *)"";
15644}
15645
15646 static char_u *
15647get_tv_string_chk(varp)
15648 typval_T *varp;
15649{
15650 static char_u mybuf[NUMBUFLEN];
15651
15652 return get_tv_string_buf_chk(varp, mybuf);
15653}
15654
15655 static char_u *
15656get_tv_string_buf_chk(varp, buf)
15657 typval_T *varp;
15658 char_u *buf;
15659{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015660 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015661 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015662 case VAR_NUMBER:
15663 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
15664 return buf;
15665 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015666 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015667 break;
15668 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015669 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015670 break;
15671 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015672 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015673 break;
15674 case VAR_STRING:
15675 if (varp->vval.v_string != NULL)
15676 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015677 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015678 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015679 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015680 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015681 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015682 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015683}
15684
15685/*
15686 * Find variable "name" in the list of variables.
15687 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015688 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000015689 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000015690 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015691 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015692 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000015693find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015694 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000015695 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015696{
Bram Moolenaar071d4272004-06-13 20:20:40 +000015697 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015698 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015699
Bram Moolenaara7043832005-01-21 11:56:39 +000015700 ht = find_var_ht(name, &varname);
15701 if (htp != NULL)
15702 *htp = ht;
15703 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015704 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015705 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015706}
15707
15708/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015709 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000015710 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015711 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015712 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015713find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000015714 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000015715 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015716 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000015717{
Bram Moolenaar33570922005-01-25 22:26:29 +000015718 hashitem_T *hi;
15719
15720 if (*varname == NUL)
15721 {
15722 /* Must be something like "s:", otherwise "ht" would be NULL. */
15723 switch (varname[-2])
15724 {
15725 case 's': return &SCRIPT_SV(current_SID).sv_var;
15726 case 'g': return &globvars_var;
15727 case 'v': return &vimvars_var;
15728 case 'b': return &curbuf->b_bufvar;
15729 case 'w': return &curwin->w_winvar;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000015730 case 'l': return current_funccal == NULL
15731 ? NULL : &current_funccal->l_vars_var;
15732 case 'a': return current_funccal == NULL
15733 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000015734 }
15735 return NULL;
15736 }
Bram Moolenaara7043832005-01-21 11:56:39 +000015737
15738 hi = hash_find(ht, varname);
15739 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015740 {
15741 /* For global variables we may try auto-loading the script. If it
15742 * worked find the variable again. */
15743 if (ht == &globvarht && !writing
15744 && script_autoload(varname) && !aborting())
15745 hi = hash_find(ht, varname);
15746 if (HASHITEM_EMPTY(hi))
15747 return NULL;
15748 }
Bram Moolenaar33570922005-01-25 22:26:29 +000015749 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000015750}
15751
15752/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015753 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000015754 * Set "varname" to the start of name without ':'.
15755 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015756 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000015757find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015758 char_u *name;
15759 char_u **varname;
15760{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000015761 hashitem_T *hi;
15762
Bram Moolenaar071d4272004-06-13 20:20:40 +000015763 if (name[1] != ':')
15764 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015765 /* The name must not start with a colon or #. */
15766 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015767 return NULL;
15768 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015769
15770 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000015771 hi = hash_find(&compat_hashtab, name);
15772 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000015773 return &compat_hashtab;
15774
Bram Moolenaar071d4272004-06-13 20:20:40 +000015775 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000015776 return &globvarht; /* global variable */
15777 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015778 }
15779 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015780 if (*name == 'g') /* global variable */
15781 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015782 /* There must be no ':' or '#' in the rest of the name, unless g: is used
15783 */
15784 if (vim_strchr(name + 2, ':') != NULL
15785 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015786 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015787 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000015788 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015789 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000015790 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000015791 if (*name == 'v') /* v: variable */
15792 return &vimvarht;
15793 if (*name == 'a' && current_funccal != NULL) /* function argument */
15794 return &current_funccal->l_avars.dv_hashtab;
15795 if (*name == 'l' && current_funccal != NULL) /* local function variable */
15796 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015797 if (*name == 's' /* script variable */
15798 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
15799 return &SCRIPT_VARS(current_SID);
15800 return NULL;
15801}
15802
15803/*
15804 * Get the string value of a (global/local) variable.
15805 * Returns NULL when it doesn't exist.
15806 */
15807 char_u *
15808get_var_value(name)
15809 char_u *name;
15810{
Bram Moolenaar33570922005-01-25 22:26:29 +000015811 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015812
Bram Moolenaara7043832005-01-21 11:56:39 +000015813 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015814 if (v == NULL)
15815 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000015816 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015817}
15818
15819/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015820 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000015821 * sourcing this script and when executing functions defined in the script.
15822 */
15823 void
15824new_script_vars(id)
15825 scid_T id;
15826{
Bram Moolenaara7043832005-01-21 11:56:39 +000015827 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000015828 hashtab_T *ht;
15829 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000015830
Bram Moolenaar071d4272004-06-13 20:20:40 +000015831 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
15832 {
Bram Moolenaara7043832005-01-21 11:56:39 +000015833 /* Re-allocating ga_data means that an ht_array pointing to
15834 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000015835 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000015836 for (i = 1; i <= ga_scripts.ga_len; ++i)
15837 {
15838 ht = &SCRIPT_VARS(i);
15839 if (ht->ht_mask == HT_INIT_SIZE - 1)
15840 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000015841 sv = &SCRIPT_SV(i);
15842 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000015843 }
15844
Bram Moolenaar071d4272004-06-13 20:20:40 +000015845 while (ga_scripts.ga_len < id)
15846 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015847 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
15848 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015849 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015850 }
15851 }
15852}
15853
15854/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015855 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
15856 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015857 */
15858 void
Bram Moolenaar33570922005-01-25 22:26:29 +000015859init_var_dict(dict, dict_var)
15860 dict_T *dict;
15861 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015862{
Bram Moolenaar33570922005-01-25 22:26:29 +000015863 hash_init(&dict->dv_hashtab);
15864 dict->dv_refcount = 99999;
15865 dict_var->di_tv.vval.v_dict = dict;
15866 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015867 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015868 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
15869 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015870}
15871
15872/*
15873 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000015874 * Frees all allocated variables and the value they contain.
15875 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015876 */
15877 void
Bram Moolenaara7043832005-01-21 11:56:39 +000015878vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000015879 hashtab_T *ht;
15880{
15881 vars_clear_ext(ht, TRUE);
15882}
15883
15884/*
15885 * Like vars_clear(), but only free the value if "free_val" is TRUE.
15886 */
15887 static void
15888vars_clear_ext(ht, free_val)
15889 hashtab_T *ht;
15890 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015891{
Bram Moolenaara7043832005-01-21 11:56:39 +000015892 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000015893 hashitem_T *hi;
15894 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015895
Bram Moolenaar33570922005-01-25 22:26:29 +000015896 hash_lock(ht);
Bram Moolenaara7043832005-01-21 11:56:39 +000015897 todo = ht->ht_used;
15898 for (hi = ht->ht_array; todo > 0; ++hi)
15899 {
15900 if (!HASHITEM_EMPTY(hi))
15901 {
15902 --todo;
15903
Bram Moolenaar33570922005-01-25 22:26:29 +000015904 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000015905 * ht_array might change then. hash_clear() takes care of it
15906 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015907 v = HI2DI(hi);
15908 if (free_val)
15909 clear_tv(&v->di_tv);
15910 if ((v->di_flags & DI_FLAGS_FIX) == 0)
15911 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000015912 }
15913 }
15914 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000015915 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015916}
15917
Bram Moolenaara7043832005-01-21 11:56:39 +000015918/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015919 * Delete a variable from hashtab "ht" at item "hi".
15920 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000015921 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015922 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000015923delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000015924 hashtab_T *ht;
15925 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015926{
Bram Moolenaar33570922005-01-25 22:26:29 +000015927 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000015928
15929 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000015930 clear_tv(&di->di_tv);
15931 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015932}
15933
15934/*
15935 * List the value of one internal variable.
15936 */
15937 static void
15938list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000015939 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015940 char_u *prefix;
15941{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015942 char_u *tofree;
15943 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015944 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015945
Bram Moolenaar33570922005-01-25 22:26:29 +000015946 s = echo_string(&v->di_tv, &tofree, numbuf);
15947 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015948 s == NULL ? (char_u *)"" : s);
15949 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015950}
15951
Bram Moolenaar071d4272004-06-13 20:20:40 +000015952 static void
15953list_one_var_a(prefix, name, type, string)
15954 char_u *prefix;
15955 char_u *name;
15956 int type;
15957 char_u *string;
15958{
15959 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
15960 if (name != NULL) /* "a:" vars don't have a name stored */
15961 msg_puts(name);
15962 msg_putchar(' ');
15963 msg_advance(22);
15964 if (type == VAR_NUMBER)
15965 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015966 else if (type == VAR_FUNC)
15967 msg_putchar('*');
15968 else if (type == VAR_LIST)
15969 {
15970 msg_putchar('[');
15971 if (*string == '[')
15972 ++string;
15973 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015974 else if (type == VAR_DICT)
15975 {
15976 msg_putchar('{');
15977 if (*string == '{')
15978 ++string;
15979 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015980 else
15981 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015982
Bram Moolenaar071d4272004-06-13 20:20:40 +000015983 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015984
15985 if (type == VAR_FUNC)
15986 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000015987}
15988
15989/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015990 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000015991 * If the variable already exists, the value is updated.
15992 * Otherwise the variable is created.
15993 */
15994 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015995set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015996 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000015997 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015998 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015999{
Bram Moolenaar33570922005-01-25 22:26:29 +000016000 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016001 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016002 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000016003 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016004
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016005 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016006 {
16007 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
16008 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
16009 ? name[2] : name[0]))
16010 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016011 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016012 return;
16013 }
16014 if (function_exists(name))
16015 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016016 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016017 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016018 return;
16019 }
16020 }
16021
Bram Moolenaara7043832005-01-21 11:56:39 +000016022 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000016023 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000016024 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000016025 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000016026 return;
16027 }
16028
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016029 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016030 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016031 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016032 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016033 if (var_check_ro(v->di_flags, name)
16034 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000016035 return;
16036 if (v->di_tv.v_type != tv->v_type
16037 && !((v->di_tv.v_type == VAR_STRING
16038 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016039 && (tv->v_type == VAR_STRING
16040 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016041 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016042 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016043 return;
16044 }
Bram Moolenaar33570922005-01-25 22:26:29 +000016045
16046 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000016047 * Handle setting internal v: variables separately: we don't change
16048 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000016049 */
16050 if (ht == &vimvarht)
16051 {
16052 if (v->di_tv.v_type == VAR_STRING)
16053 {
16054 vim_free(v->di_tv.vval.v_string);
16055 if (copy || tv->v_type != VAR_STRING)
16056 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
16057 else
16058 {
16059 /* Take over the string to avoid an extra alloc/free. */
16060 v->di_tv.vval.v_string = tv->vval.v_string;
16061 tv->vval.v_string = NULL;
16062 }
16063 }
16064 else if (v->di_tv.v_type != VAR_NUMBER)
16065 EMSG2(_(e_intern2), "set_var()");
16066 else
16067 v->di_tv.vval.v_number = get_tv_number(tv);
16068 return;
16069 }
16070
16071 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016072 }
16073 else /* add a new variable */
16074 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000016075 /* Make sure the variable name is valid. */
16076 for (p = varname; *p != NUL; ++p)
16077 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p)))
16078 {
16079 EMSG2(_(e_illvar), varname);
16080 return;
16081 }
16082
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016083 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
16084 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000016085 if (v == NULL)
16086 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000016087 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000016088 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016089 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016090 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016091 return;
16092 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016093 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016094 }
Bram Moolenaara7043832005-01-21 11:56:39 +000016095
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016096 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000016097 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016098 else
16099 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016100 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016101 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016102 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016104}
16105
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016106/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016107 * Return TRUE if di_flags "flags" indicate read-only variable "name".
16108 * Also give an error message.
16109 */
16110 static int
16111var_check_ro(flags, name)
16112 int flags;
16113 char_u *name;
16114{
16115 if (flags & DI_FLAGS_RO)
16116 {
16117 EMSG2(_(e_readonlyvar), name);
16118 return TRUE;
16119 }
16120 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
16121 {
16122 EMSG2(_(e_readonlysbx), name);
16123 return TRUE;
16124 }
16125 return FALSE;
16126}
16127
16128/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016129 * Return TRUE if typeval "tv" is set to be locked (immutable).
16130 * Also give an error message, using "name".
16131 */
16132 static int
16133tv_check_lock(lock, name)
16134 int lock;
16135 char_u *name;
16136{
16137 if (lock & VAR_LOCKED)
16138 {
16139 EMSG2(_("E741: Value is locked: %s"),
16140 name == NULL ? (char_u *)_("Unknown") : name);
16141 return TRUE;
16142 }
16143 if (lock & VAR_FIXED)
16144 {
16145 EMSG2(_("E742: Cannot change value of %s"),
16146 name == NULL ? (char_u *)_("Unknown") : name);
16147 return TRUE;
16148 }
16149 return FALSE;
16150}
16151
16152/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016153 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016154 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016155 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016156 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016157 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016158copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000016159 typval_T *from;
16160 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016161{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016162 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016163 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016164 switch (from->v_type)
16165 {
16166 case VAR_NUMBER:
16167 to->vval.v_number = from->vval.v_number;
16168 break;
16169 case VAR_STRING:
16170 case VAR_FUNC:
16171 if (from->vval.v_string == NULL)
16172 to->vval.v_string = NULL;
16173 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016174 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016175 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016176 if (from->v_type == VAR_FUNC)
16177 func_ref(to->vval.v_string);
16178 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016179 break;
16180 case VAR_LIST:
16181 if (from->vval.v_list == NULL)
16182 to->vval.v_list = NULL;
16183 else
16184 {
16185 to->vval.v_list = from->vval.v_list;
16186 ++to->vval.v_list->lv_refcount;
16187 }
16188 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016189 case VAR_DICT:
16190 if (from->vval.v_dict == NULL)
16191 to->vval.v_dict = NULL;
16192 else
16193 {
16194 to->vval.v_dict = from->vval.v_dict;
16195 ++to->vval.v_dict->dv_refcount;
16196 }
16197 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016198 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016199 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016200 break;
16201 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016202}
16203
16204/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000016205 * Make a copy of an item.
16206 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016207 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
16208 * reference to an already copied list/dict can be used.
16209 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016210 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016211 static int
16212item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000016213 typval_T *from;
16214 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016215 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016216 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016217{
16218 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016219 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016220
Bram Moolenaar33570922005-01-25 22:26:29 +000016221 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016222 {
16223 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016224 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016225 }
16226 ++recurse;
16227
16228 switch (from->v_type)
16229 {
16230 case VAR_NUMBER:
16231 case VAR_STRING:
16232 case VAR_FUNC:
16233 copy_tv(from, to);
16234 break;
16235 case VAR_LIST:
16236 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016237 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016238 if (from->vval.v_list == NULL)
16239 to->vval.v_list = NULL;
16240 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
16241 {
16242 /* use the copy made earlier */
16243 to->vval.v_list = from->vval.v_list->lv_copylist;
16244 ++to->vval.v_list->lv_refcount;
16245 }
16246 else
16247 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
16248 if (to->vval.v_list == NULL)
16249 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016250 break;
16251 case VAR_DICT:
16252 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016253 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016254 if (from->vval.v_dict == NULL)
16255 to->vval.v_dict = NULL;
16256 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
16257 {
16258 /* use the copy made earlier */
16259 to->vval.v_dict = from->vval.v_dict->dv_copydict;
16260 ++to->vval.v_dict->dv_refcount;
16261 }
16262 else
16263 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
16264 if (to->vval.v_dict == NULL)
16265 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016266 break;
16267 default:
16268 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016269 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016270 }
16271 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016272 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016273}
16274
16275/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016276 * ":echo expr1 ..." print each argument separated with a space, add a
16277 * newline at the end.
16278 * ":echon expr1 ..." print each argument plain.
16279 */
16280 void
16281ex_echo(eap)
16282 exarg_T *eap;
16283{
16284 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000016285 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016286 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016287 char_u *p;
16288 int needclr = TRUE;
16289 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016290 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016291
16292 if (eap->skip)
16293 ++emsg_skip;
16294 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
16295 {
16296 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016297 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016298 {
16299 /*
16300 * Report the invalid expression unless the expression evaluation
16301 * has been cancelled due to an aborting error, an interrupt, or an
16302 * exception.
16303 */
16304 if (!aborting())
16305 EMSG2(_(e_invexpr2), p);
16306 break;
16307 }
16308 if (!eap->skip)
16309 {
16310 if (atstart)
16311 {
16312 atstart = FALSE;
16313 /* Call msg_start() after eval1(), evaluating the expression
16314 * may cause a message to appear. */
16315 if (eap->cmdidx == CMD_echo)
16316 msg_start();
16317 }
16318 else if (eap->cmdidx == CMD_echo)
16319 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016320 p = echo_string(&rettv, &tofree, numbuf);
16321 if (p != NULL)
16322 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016323 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016324 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016325 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016326 if (*p != TAB && needclr)
16327 {
16328 /* remove any text still there from the command */
16329 msg_clr_eos();
16330 needclr = FALSE;
16331 }
16332 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016333 }
16334 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016335 {
16336#ifdef FEAT_MBYTE
16337 if (has_mbyte)
16338 {
16339 int i = (*mb_ptr2len_check)(p);
16340
16341 (void)msg_outtrans_len_attr(p, i, echo_attr);
16342 p += i - 1;
16343 }
16344 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000016345#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016346 (void)msg_outtrans_len_attr(p, 1, echo_attr);
16347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016348 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016349 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016350 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016351 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016352 arg = skipwhite(arg);
16353 }
16354 eap->nextcmd = check_nextcmd(arg);
16355
16356 if (eap->skip)
16357 --emsg_skip;
16358 else
16359 {
16360 /* remove text that may still be there from the command */
16361 if (needclr)
16362 msg_clr_eos();
16363 if (eap->cmdidx == CMD_echo)
16364 msg_end();
16365 }
16366}
16367
16368/*
16369 * ":echohl {name}".
16370 */
16371 void
16372ex_echohl(eap)
16373 exarg_T *eap;
16374{
16375 int id;
16376
16377 id = syn_name2id(eap->arg);
16378 if (id == 0)
16379 echo_attr = 0;
16380 else
16381 echo_attr = syn_id2attr(id);
16382}
16383
16384/*
16385 * ":execute expr1 ..." execute the result of an expression.
16386 * ":echomsg expr1 ..." Print a message
16387 * ":echoerr expr1 ..." Print an error
16388 * Each gets spaces around each argument and a newline at the end for
16389 * echo commands
16390 */
16391 void
16392ex_execute(eap)
16393 exarg_T *eap;
16394{
16395 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000016396 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016397 int ret = OK;
16398 char_u *p;
16399 garray_T ga;
16400 int len;
16401 int save_did_emsg;
16402
16403 ga_init2(&ga, 1, 80);
16404
16405 if (eap->skip)
16406 ++emsg_skip;
16407 while (*arg != NUL && *arg != '|' && *arg != '\n')
16408 {
16409 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016410 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016411 {
16412 /*
16413 * Report the invalid expression unless the expression evaluation
16414 * has been cancelled due to an aborting error, an interrupt, or an
16415 * exception.
16416 */
16417 if (!aborting())
16418 EMSG2(_(e_invexpr2), p);
16419 ret = FAIL;
16420 break;
16421 }
16422
16423 if (!eap->skip)
16424 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016425 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016426 len = (int)STRLEN(p);
16427 if (ga_grow(&ga, len + 2) == FAIL)
16428 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016429 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016430 ret = FAIL;
16431 break;
16432 }
16433 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016434 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016435 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016436 ga.ga_len += len;
16437 }
16438
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016439 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016440 arg = skipwhite(arg);
16441 }
16442
16443 if (ret != FAIL && ga.ga_data != NULL)
16444 {
16445 if (eap->cmdidx == CMD_echomsg)
16446 MSG_ATTR(ga.ga_data, echo_attr);
16447 else if (eap->cmdidx == CMD_echoerr)
16448 {
16449 /* We don't want to abort following commands, restore did_emsg. */
16450 save_did_emsg = did_emsg;
16451 EMSG((char_u *)ga.ga_data);
16452 if (!force_abort)
16453 did_emsg = save_did_emsg;
16454 }
16455 else if (eap->cmdidx == CMD_execute)
16456 do_cmdline((char_u *)ga.ga_data,
16457 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
16458 }
16459
16460 ga_clear(&ga);
16461
16462 if (eap->skip)
16463 --emsg_skip;
16464
16465 eap->nextcmd = check_nextcmd(arg);
16466}
16467
16468/*
16469 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
16470 * "arg" points to the "&" or '+' when called, to "option" when returning.
16471 * Returns NULL when no option name found. Otherwise pointer to the char
16472 * after the option name.
16473 */
16474 static char_u *
16475find_option_end(arg, opt_flags)
16476 char_u **arg;
16477 int *opt_flags;
16478{
16479 char_u *p = *arg;
16480
16481 ++p;
16482 if (*p == 'g' && p[1] == ':')
16483 {
16484 *opt_flags = OPT_GLOBAL;
16485 p += 2;
16486 }
16487 else if (*p == 'l' && p[1] == ':')
16488 {
16489 *opt_flags = OPT_LOCAL;
16490 p += 2;
16491 }
16492 else
16493 *opt_flags = 0;
16494
16495 if (!ASCII_ISALPHA(*p))
16496 return NULL;
16497 *arg = p;
16498
16499 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
16500 p += 4; /* termcap option */
16501 else
16502 while (ASCII_ISALPHA(*p))
16503 ++p;
16504 return p;
16505}
16506
16507/*
16508 * ":function"
16509 */
16510 void
16511ex_function(eap)
16512 exarg_T *eap;
16513{
16514 char_u *theline;
16515 int j;
16516 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016517 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016518 char_u *name = NULL;
16519 char_u *p;
16520 char_u *arg;
16521 garray_T newargs;
16522 garray_T newlines;
16523 int varargs = FALSE;
16524 int mustend = FALSE;
16525 int flags = 0;
16526 ufunc_T *fp;
16527 int indent;
16528 int nesting;
16529 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000016530 dictitem_T *v;
16531 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016532 static int func_nr = 0; /* number for nameless function */
16533 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016534 hashtab_T *ht;
16535 int todo;
16536 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016537
16538 /*
16539 * ":function" without argument: list functions.
16540 */
16541 if (ends_excmd(*eap->arg))
16542 {
16543 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016544 {
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000016545 todo = func_hashtab.ht_used;
16546 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016547 {
16548 if (!HASHITEM_EMPTY(hi))
16549 {
16550 --todo;
16551 fp = HI2UF(hi);
16552 if (!isdigit(*fp->uf_name))
16553 list_func_head(fp, FALSE);
16554 }
16555 }
16556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016557 eap->nextcmd = check_nextcmd(eap->arg);
16558 return;
16559 }
16560
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016561 /*
16562 * Get the function name. There are these situations:
16563 * func normal function name
16564 * "name" == func, "fudi.fd_dict" == NULL
16565 * dict.func new dictionary entry
16566 * "name" == NULL, "fudi.fd_dict" set,
16567 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
16568 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016569 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016570 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
16571 * dict.func existing dict entry that's not a Funcref
16572 * "name" == NULL, "fudi.fd_dict" set,
16573 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
16574 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016575 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016576 name = trans_function_name(&p, eap->skip, 0, &fudi);
16577 paren = (vim_strchr(p, '(') != NULL);
16578 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016579 {
16580 /*
16581 * Return on an invalid expression in braces, unless the expression
16582 * evaluation has been cancelled due to an aborting error, an
16583 * interrupt, or an exception.
16584 */
16585 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016586 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016587 if (!eap->skip && fudi.fd_newkey != NULL)
16588 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016589 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016590 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016592 else
16593 eap->skip = TRUE;
16594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016595 /* An error in a function call during evaluation of an expression in magic
16596 * braces should not cause the function not to be defined. */
16597 saved_did_emsg = did_emsg;
16598 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016599
16600 /*
16601 * ":function func" with only function name: list function.
16602 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016603 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016604 {
16605 if (!ends_excmd(*skipwhite(p)))
16606 {
16607 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016608 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016609 }
16610 eap->nextcmd = check_nextcmd(p);
16611 if (eap->nextcmd != NULL)
16612 *p = NUL;
16613 if (!eap->skip && !got_int)
16614 {
16615 fp = find_func(name);
16616 if (fp != NULL)
16617 {
16618 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016619 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016620 {
16621 msg_putchar('\n');
16622 msg_outnum((long)(j + 1));
16623 if (j < 9)
16624 msg_putchar(' ');
16625 if (j < 99)
16626 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016627 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016628 out_flush(); /* show a line at a time */
16629 ui_breakcheck();
16630 }
16631 if (!got_int)
16632 {
16633 msg_putchar('\n');
16634 msg_puts((char_u *)" endfunction");
16635 }
16636 }
16637 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016638 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016639 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016640 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016641 }
16642
16643 /*
16644 * ":function name(arg1, arg2)" Define function.
16645 */
16646 p = skipwhite(p);
16647 if (*p != '(')
16648 {
16649 if (!eap->skip)
16650 {
16651 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016652 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016653 }
16654 /* attempt to continue by skipping some text */
16655 if (vim_strchr(p, '(') != NULL)
16656 p = vim_strchr(p, '(');
16657 }
16658 p = skipwhite(p + 1);
16659
16660 ga_init2(&newargs, (int)sizeof(char_u *), 3);
16661 ga_init2(&newlines, (int)sizeof(char_u *), 3);
16662
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016663 if (!eap->skip)
16664 {
16665 /* Check the name of the function. */
16666 if (name != NULL)
16667 arg = name;
16668 else
16669 arg = fudi.fd_newkey;
16670 if (arg != NULL)
16671 {
16672 if (*arg == K_SPECIAL)
16673 j = 3;
16674 else
16675 j = 0;
16676 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
16677 : eval_isnamec(arg[j])))
16678 ++j;
16679 if (arg[j] != NUL)
16680 emsg_funcname(_(e_invarg2), arg);
16681 }
16682 }
16683
Bram Moolenaar071d4272004-06-13 20:20:40 +000016684 /*
16685 * Isolate the arguments: "arg1, arg2, ...)"
16686 */
16687 while (*p != ')')
16688 {
16689 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
16690 {
16691 varargs = TRUE;
16692 p += 3;
16693 mustend = TRUE;
16694 }
16695 else
16696 {
16697 arg = p;
16698 while (ASCII_ISALNUM(*p) || *p == '_')
16699 ++p;
16700 if (arg == p || isdigit(*arg)
16701 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
16702 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
16703 {
16704 if (!eap->skip)
16705 EMSG2(_("E125: Illegal argument: %s"), arg);
16706 break;
16707 }
16708 if (ga_grow(&newargs, 1) == FAIL)
16709 goto erret;
16710 c = *p;
16711 *p = NUL;
16712 arg = vim_strsave(arg);
16713 if (arg == NULL)
16714 goto erret;
16715 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
16716 *p = c;
16717 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016718 if (*p == ',')
16719 ++p;
16720 else
16721 mustend = TRUE;
16722 }
16723 p = skipwhite(p);
16724 if (mustend && *p != ')')
16725 {
16726 if (!eap->skip)
16727 EMSG2(_(e_invarg2), eap->arg);
16728 break;
16729 }
16730 }
16731 ++p; /* skip the ')' */
16732
Bram Moolenaare9a41262005-01-15 22:18:47 +000016733 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016734 for (;;)
16735 {
16736 p = skipwhite(p);
16737 if (STRNCMP(p, "range", 5) == 0)
16738 {
16739 flags |= FC_RANGE;
16740 p += 5;
16741 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000016742 else if (STRNCMP(p, "dict", 4) == 0)
16743 {
16744 flags |= FC_DICT;
16745 p += 4;
16746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016747 else if (STRNCMP(p, "abort", 5) == 0)
16748 {
16749 flags |= FC_ABORT;
16750 p += 5;
16751 }
16752 else
16753 break;
16754 }
16755
16756 if (*p != NUL && *p != '"' && *p != '\n' && !eap->skip && !did_emsg)
16757 EMSG(_(e_trailing));
16758
16759 /*
16760 * Read the body of the function, until ":endfunction" is found.
16761 */
16762 if (KeyTyped)
16763 {
16764 /* Check if the function already exists, don't let the user type the
16765 * whole function before telling him it doesn't work! For a script we
16766 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016767 if (!eap->skip && !eap->forceit)
16768 {
16769 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
16770 EMSG(_(e_funcdict));
16771 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016772 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016774
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016775 if (!eap->skip && did_emsg)
16776 goto erret;
16777
Bram Moolenaar071d4272004-06-13 20:20:40 +000016778 msg_putchar('\n'); /* don't overwrite the function name */
16779 cmdline_row = msg_row;
16780 }
16781
16782 indent = 2;
16783 nesting = 0;
16784 for (;;)
16785 {
16786 msg_scroll = TRUE;
16787 need_wait_return = FALSE;
16788 if (eap->getline == NULL)
16789 theline = getcmdline(':', 0L, indent);
16790 else
16791 theline = eap->getline(':', eap->cookie, indent);
16792 if (KeyTyped)
16793 lines_left = Rows - 1;
16794 if (theline == NULL)
16795 {
16796 EMSG(_("E126: Missing :endfunction"));
16797 goto erret;
16798 }
16799
16800 if (skip_until != NULL)
16801 {
16802 /* between ":append" and "." and between ":python <<EOF" and "EOF"
16803 * don't check for ":endfunc". */
16804 if (STRCMP(theline, skip_until) == 0)
16805 {
16806 vim_free(skip_until);
16807 skip_until = NULL;
16808 }
16809 }
16810 else
16811 {
16812 /* skip ':' and blanks*/
16813 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
16814 ;
16815
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016816 /* Check for "endfunction". */
16817 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016818 {
16819 vim_free(theline);
16820 break;
16821 }
16822
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016823 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000016824 * at "end". */
16825 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
16826 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016827 else if (STRNCMP(p, "if", 2) == 0
16828 || STRNCMP(p, "wh", 2) == 0
16829 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000016830 || STRNCMP(p, "try", 3) == 0)
16831 indent += 2;
16832
16833 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016834 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016835 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016836 if (*p == '!')
16837 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016838 p += eval_fname_script(p);
16839 if (ASCII_ISALPHA(*p))
16840 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016841 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016842 if (*skipwhite(p) == '(')
16843 {
16844 ++nesting;
16845 indent += 2;
16846 }
16847 }
16848 }
16849
16850 /* Check for ":append" or ":insert". */
16851 p = skip_range(p, NULL);
16852 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
16853 || (p[0] == 'i'
16854 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
16855 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
16856 skip_until = vim_strsave((char_u *)".");
16857
16858 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
16859 arg = skipwhite(skiptowhite(p));
16860 if (arg[0] == '<' && arg[1] =='<'
16861 && ((p[0] == 'p' && p[1] == 'y'
16862 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
16863 || (p[0] == 'p' && p[1] == 'e'
16864 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
16865 || (p[0] == 't' && p[1] == 'c'
16866 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
16867 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
16868 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000016869 || (p[0] == 'm' && p[1] == 'z'
16870 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016871 ))
16872 {
16873 /* ":python <<" continues until a dot, like ":append" */
16874 p = skipwhite(arg + 2);
16875 if (*p == NUL)
16876 skip_until = vim_strsave((char_u *)".");
16877 else
16878 skip_until = vim_strsave(p);
16879 }
16880 }
16881
16882 /* Add the line to the function. */
16883 if (ga_grow(&newlines, 1) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000016884 {
16885 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016886 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000016887 }
16888
16889 /* Copy the line to newly allocated memory. get_one_sourceline()
16890 * allocates 250 bytes per line, this saves 80% on average. The cost
16891 * is an extra alloc/free. */
16892 p = vim_strsave(theline);
16893 if (p != NULL)
16894 {
16895 vim_free(theline);
16896 theline = p;
16897 }
16898
Bram Moolenaar071d4272004-06-13 20:20:40 +000016899 ((char_u **)(newlines.ga_data))[newlines.ga_len] = theline;
16900 newlines.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016901 }
16902
16903 /* Don't define the function when skipping commands or when an error was
16904 * detected. */
16905 if (eap->skip || did_emsg)
16906 goto erret;
16907
16908 /*
16909 * If there are no errors, add the function
16910 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016911 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016912 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016913 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000016914 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016915 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016916 emsg_funcname("E707: Function name conflicts with variable: %s",
16917 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016918 goto erret;
16919 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016920
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016921 fp = find_func(name);
16922 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016923 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016924 if (!eap->forceit)
16925 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016926 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016927 goto erret;
16928 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016929 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016930 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016931 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016932 name);
16933 goto erret;
16934 }
16935 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016936 ga_clear_strings(&(fp->uf_args));
16937 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016938 vim_free(name);
16939 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016940 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016941 }
16942 else
16943 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016944 char numbuf[20];
16945
16946 fp = NULL;
16947 if (fudi.fd_newkey == NULL && !eap->forceit)
16948 {
16949 EMSG(_(e_funcdict));
16950 goto erret;
16951 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000016952 if (fudi.fd_di == NULL)
16953 {
16954 /* Can't add a function to a locked dictionary */
16955 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
16956 goto erret;
16957 }
16958 /* Can't change an existing function if it is locked */
16959 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
16960 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016961
16962 /* Give the function a sequential number. Can only be used with a
16963 * Funcref! */
16964 vim_free(name);
16965 sprintf(numbuf, "%d", ++func_nr);
16966 name = vim_strsave((char_u *)numbuf);
16967 if (name == NULL)
16968 goto erret;
16969 }
16970
16971 if (fp == NULL)
16972 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016973 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016974 {
16975 int slen, plen;
16976 char_u *scriptname;
16977
16978 /* Check that the autoload name matches the script name. */
16979 j = FAIL;
16980 if (sourcing_name != NULL)
16981 {
16982 scriptname = autoload_name(name);
16983 if (scriptname != NULL)
16984 {
16985 p = vim_strchr(scriptname, '/');
16986 plen = STRLEN(p);
16987 slen = STRLEN(sourcing_name);
16988 if (slen > plen && fnamecmp(p,
16989 sourcing_name + slen - plen) == 0)
16990 j = OK;
16991 vim_free(scriptname);
16992 }
16993 }
16994 if (j == FAIL)
16995 {
16996 EMSG2(_("E746: Function name does not match script file name: %s"), name);
16997 goto erret;
16998 }
16999 }
17000
17001 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017002 if (fp == NULL)
17003 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017004
17005 if (fudi.fd_dict != NULL)
17006 {
17007 if (fudi.fd_di == NULL)
17008 {
17009 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017010 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017011 if (fudi.fd_di == NULL)
17012 {
17013 vim_free(fp);
17014 goto erret;
17015 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017016 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
17017 {
17018 vim_free(fudi.fd_di);
17019 goto erret;
17020 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017021 }
17022 else
17023 /* overwrite existing dict entry */
17024 clear_tv(&fudi.fd_di->di_tv);
17025 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017026 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017027 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017028 fp->uf_refcount = 1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017029 }
17030
Bram Moolenaar071d4272004-06-13 20:20:40 +000017031 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017032 STRCPY(fp->uf_name, name);
17033 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017034 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017035 fp->uf_args = newargs;
17036 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017037#ifdef FEAT_PROFILE
17038 fp->uf_tml_count = NULL;
17039 fp->uf_tml_total = NULL;
17040 fp->uf_tml_self = NULL;
17041 fp->uf_profiling = FALSE;
17042 if (prof_def_func())
17043 func_do_profile(fp);
17044#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017045 fp->uf_varargs = varargs;
17046 fp->uf_flags = flags;
17047 fp->uf_calls = 0;
17048 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017049 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017050
17051erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017052 ga_clear_strings(&newargs);
17053 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017054ret_free:
17055 vim_free(skip_until);
17056 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017057 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017058 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017059}
17060
17061/*
17062 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000017063 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017064 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017065 * flags:
17066 * TFN_INT: internal function name OK
17067 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000017068 * Advances "pp" to just after the function name (if no error).
17069 */
17070 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017071trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017072 char_u **pp;
17073 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017074 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000017075 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017076{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017077 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017078 char_u *start;
17079 char_u *end;
17080 int lead;
17081 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017082 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000017083 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017084
17085 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017086 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017087 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000017088
17089 /* Check for hard coded <SNR>: already translated function ID (from a user
17090 * command). */
17091 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
17092 && (*pp)[2] == (int)KE_SNR)
17093 {
17094 *pp += 3;
17095 len = get_id_len(pp) + 3;
17096 return vim_strnsave(start, len);
17097 }
17098
17099 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
17100 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017101 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000017102 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017103 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017104
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017105 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
17106 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017107 if (end == start)
17108 {
17109 if (!skip)
17110 EMSG(_("E129: Function name required"));
17111 goto theend;
17112 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017113 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017114 {
17115 /*
17116 * Report an invalid expression in braces, unless the expression
17117 * evaluation has been cancelled due to an aborting error, an
17118 * interrupt, or an exception.
17119 */
17120 if (!aborting())
17121 {
17122 if (end != NULL)
17123 EMSG2(_(e_invarg2), start);
17124 }
17125 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017126 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017127 goto theend;
17128 }
17129
17130 if (lv.ll_tv != NULL)
17131 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017132 if (fdp != NULL)
17133 {
17134 fdp->fd_dict = lv.ll_dict;
17135 fdp->fd_newkey = lv.ll_newkey;
17136 lv.ll_newkey = NULL;
17137 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017138 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017139 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
17140 {
17141 name = vim_strsave(lv.ll_tv->vval.v_string);
17142 *pp = end;
17143 }
17144 else
17145 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017146 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
17147 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017148 EMSG(_(e_funcref));
17149 else
17150 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017151 name = NULL;
17152 }
17153 goto theend;
17154 }
17155
17156 if (lv.ll_name == NULL)
17157 {
17158 /* Error found, but continue after the function name. */
17159 *pp = end;
17160 goto theend;
17161 }
17162
17163 if (lv.ll_exp_name != NULL)
17164 len = STRLEN(lv.ll_exp_name);
17165 else
Bram Moolenaara7043832005-01-21 11:56:39 +000017166 {
17167 if (lead == 2) /* skip over "s:" */
17168 lv.ll_name += 2;
17169 len = (int)(end - lv.ll_name);
17170 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017171
17172 /*
17173 * Copy the function name to allocated memory.
17174 * Accept <SID>name() inside a script, translate into <SNR>123_name().
17175 * Accept <SNR>123_name() outside a script.
17176 */
17177 if (skip)
17178 lead = 0; /* do nothing */
17179 else if (lead > 0)
17180 {
17181 lead = 3;
17182 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
17183 {
17184 if (current_SID <= 0)
17185 {
17186 EMSG(_(e_usingsid));
17187 goto theend;
17188 }
17189 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
17190 lead += (int)STRLEN(sid_buf);
17191 }
17192 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017193 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017194 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017195 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017196 goto theend;
17197 }
17198 name = alloc((unsigned)(len + lead + 1));
17199 if (name != NULL)
17200 {
17201 if (lead > 0)
17202 {
17203 name[0] = K_SPECIAL;
17204 name[1] = KS_EXTRA;
17205 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000017206 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017207 STRCPY(name + 3, sid_buf);
17208 }
17209 mch_memmove(name + lead, lv.ll_name, (size_t)len);
17210 name[len + lead] = NUL;
17211 }
17212 *pp = end;
17213
17214theend:
17215 clear_lval(&lv);
17216 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017217}
17218
17219/*
17220 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
17221 * Return 2 if "p" starts with "s:".
17222 * Return 0 otherwise.
17223 */
17224 static int
17225eval_fname_script(p)
17226 char_u *p;
17227{
17228 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
17229 || STRNICMP(p + 1, "SNR>", 4) == 0))
17230 return 5;
17231 if (p[0] == 's' && p[1] == ':')
17232 return 2;
17233 return 0;
17234}
17235
17236/*
17237 * Return TRUE if "p" starts with "<SID>" or "s:".
17238 * Only works if eval_fname_script() returned non-zero for "p"!
17239 */
17240 static int
17241eval_fname_sid(p)
17242 char_u *p;
17243{
17244 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
17245}
17246
17247/*
17248 * List the head of the function: "name(arg1, arg2)".
17249 */
17250 static void
17251list_func_head(fp, indent)
17252 ufunc_T *fp;
17253 int indent;
17254{
17255 int j;
17256
17257 msg_start();
17258 if (indent)
17259 MSG_PUTS(" ");
17260 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017261 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017262 {
17263 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017264 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017265 }
17266 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017267 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017268 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017269 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017270 {
17271 if (j)
17272 MSG_PUTS(", ");
17273 msg_puts(FUNCARG(fp, j));
17274 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017275 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017276 {
17277 if (j)
17278 MSG_PUTS(", ");
17279 MSG_PUTS("...");
17280 }
17281 msg_putchar(')');
17282}
17283
17284/*
17285 * Find a function by name, return pointer to it in ufuncs.
17286 * Return NULL for unknown function.
17287 */
17288 static ufunc_T *
17289find_func(name)
17290 char_u *name;
17291{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017292 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017293
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017294 hi = hash_find(&func_hashtab, name);
17295 if (!HASHITEM_EMPTY(hi))
17296 return HI2UF(hi);
17297 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017298}
17299
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017300#if defined(EXITFREE) || defined(PROTO)
17301 void
17302free_all_functions()
17303{
17304 hashitem_T *hi;
17305
17306 /* Need to start all over every time, because func_free() may change the
17307 * hash table. */
17308 while (func_hashtab.ht_used > 0)
17309 for (hi = func_hashtab.ht_array; ; ++hi)
17310 if (!HASHITEM_EMPTY(hi))
17311 {
17312 func_free(HI2UF(hi));
17313 break;
17314 }
17315}
17316#endif
17317
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017318/*
17319 * Return TRUE if a function "name" exists.
17320 */
17321 static int
17322function_exists(name)
17323 char_u *name;
17324{
17325 char_u *p = name;
17326 int n = FALSE;
17327
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017328 p = trans_function_name(&p, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017329 if (p != NULL)
17330 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017331 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017332 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017333 else
17334 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017335 vim_free(p);
17336 }
17337 return n;
17338}
17339
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017340/*
17341 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017342 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017343 */
17344 static int
17345builtin_function(name)
17346 char_u *name;
17347{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017348 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
17349 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017350}
17351
Bram Moolenaar05159a02005-02-26 23:04:13 +000017352#if defined(FEAT_PROFILE) || defined(PROTO)
17353/*
17354 * Start profiling function "fp".
17355 */
17356 static void
17357func_do_profile(fp)
17358 ufunc_T *fp;
17359{
17360 fp->uf_tm_count = 0;
17361 profile_zero(&fp->uf_tm_self);
17362 profile_zero(&fp->uf_tm_total);
17363 if (fp->uf_tml_count == NULL)
17364 fp->uf_tml_count = (int *)alloc_clear((unsigned)
17365 (sizeof(int) * fp->uf_lines.ga_len));
17366 if (fp->uf_tml_total == NULL)
17367 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
17368 (sizeof(proftime_T) * fp->uf_lines.ga_len));
17369 if (fp->uf_tml_self == NULL)
17370 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
17371 (sizeof(proftime_T) * fp->uf_lines.ga_len));
17372 fp->uf_tml_idx = -1;
17373 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
17374 || fp->uf_tml_self == NULL)
17375 return; /* out of memory */
17376
17377 fp->uf_profiling = TRUE;
17378}
17379
17380/*
17381 * Dump the profiling results for all functions in file "fd".
17382 */
17383 void
17384func_dump_profile(fd)
17385 FILE *fd;
17386{
17387 hashitem_T *hi;
17388 int todo;
17389 ufunc_T *fp;
17390 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000017391 ufunc_T **sorttab;
17392 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017393
17394 todo = func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000017395 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
17396
Bram Moolenaar05159a02005-02-26 23:04:13 +000017397 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
17398 {
17399 if (!HASHITEM_EMPTY(hi))
17400 {
17401 --todo;
17402 fp = HI2UF(hi);
17403 if (fp->uf_profiling)
17404 {
Bram Moolenaar73830342005-02-28 22:48:19 +000017405 if (sorttab != NULL)
17406 sorttab[st_len++] = fp;
17407
Bram Moolenaar05159a02005-02-26 23:04:13 +000017408 if (fp->uf_name[0] == K_SPECIAL)
17409 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
17410 else
17411 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
17412 if (fp->uf_tm_count == 1)
17413 fprintf(fd, "Called 1 time\n");
17414 else
17415 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
17416 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
17417 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
17418 fprintf(fd, "\n");
17419 fprintf(fd, "count total (s) self (s)\n");
17420
17421 for (i = 0; i < fp->uf_lines.ga_len; ++i)
17422 {
Bram Moolenaar73830342005-02-28 22:48:19 +000017423 prof_func_line(fd, fp->uf_tml_count[i],
17424 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017425 fprintf(fd, "%s\n", FUNCLINE(fp, i));
17426 }
17427 fprintf(fd, "\n");
17428 }
17429 }
17430 }
Bram Moolenaar73830342005-02-28 22:48:19 +000017431
17432 if (sorttab != NULL && st_len > 0)
17433 {
17434 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
17435 prof_total_cmp);
17436 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
17437 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
17438 prof_self_cmp);
17439 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
17440 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000017441}
Bram Moolenaar73830342005-02-28 22:48:19 +000017442
17443 static void
17444prof_sort_list(fd, sorttab, st_len, title, prefer_self)
17445 FILE *fd;
17446 ufunc_T **sorttab;
17447 int st_len;
17448 char *title;
17449 int prefer_self; /* when equal print only self time */
17450{
17451 int i;
17452 ufunc_T *fp;
17453
17454 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
17455 fprintf(fd, "count total (s) self (s) function\n");
17456 for (i = 0; i < 20 && i < st_len; ++i)
17457 {
17458 fp = sorttab[i];
17459 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
17460 prefer_self);
17461 if (fp->uf_name[0] == K_SPECIAL)
17462 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
17463 else
17464 fprintf(fd, " %s()\n", fp->uf_name);
17465 }
17466 fprintf(fd, "\n");
17467}
17468
17469/*
17470 * Print the count and times for one function or function line.
17471 */
17472 static void
17473prof_func_line(fd, count, total, self, prefer_self)
17474 FILE *fd;
17475 int count;
17476 proftime_T *total;
17477 proftime_T *self;
17478 int prefer_self; /* when equal print only self time */
17479{
17480 if (count > 0)
17481 {
17482 fprintf(fd, "%5d ", count);
17483 if (prefer_self && profile_equal(total, self))
17484 fprintf(fd, " ");
17485 else
17486 fprintf(fd, "%s ", profile_msg(total));
17487 if (!prefer_self && profile_equal(total, self))
17488 fprintf(fd, " ");
17489 else
17490 fprintf(fd, "%s ", profile_msg(self));
17491 }
17492 else
17493 fprintf(fd, " ");
17494}
17495
17496/*
17497 * Compare function for total time sorting.
17498 */
17499 static int
17500#ifdef __BORLANDC__
17501_RTLENTRYF
17502#endif
17503prof_total_cmp(s1, s2)
17504 const void *s1;
17505 const void *s2;
17506{
17507 ufunc_T *p1, *p2;
17508
17509 p1 = *(ufunc_T **)s1;
17510 p2 = *(ufunc_T **)s2;
17511 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
17512}
17513
17514/*
17515 * Compare function for self time sorting.
17516 */
17517 static int
17518#ifdef __BORLANDC__
17519_RTLENTRYF
17520#endif
17521prof_self_cmp(s1, s2)
17522 const void *s1;
17523 const void *s2;
17524{
17525 ufunc_T *p1, *p2;
17526
17527 p1 = *(ufunc_T **)s1;
17528 p2 = *(ufunc_T **)s2;
17529 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
17530}
17531
Bram Moolenaar05159a02005-02-26 23:04:13 +000017532#endif
17533
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017534/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017535 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017536 * Return TRUE if a package was loaded.
17537 */
17538 static int
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017539script_autoload(name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017540 char_u *name;
17541{
17542 char_u *p;
17543 char_u *scriptname;
17544 int ret = FALSE;
17545
17546 /* If there is no colon after name[1] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017547 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017548 if (p == NULL || p <= name + 2)
17549 return FALSE;
17550
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017551 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
17552 scriptname = autoload_name(name);
17553 if (cmd_runtime(scriptname, FALSE) == OK)
17554 ret = TRUE;
17555
17556 vim_free(scriptname);
17557 return ret;
17558}
17559
17560/*
17561 * Return the autoload script name for a function or variable name.
17562 * Returns NULL when out of memory.
17563 */
17564 static char_u *
17565autoload_name(name)
17566 char_u *name;
17567{
17568 char_u *p;
17569 char_u *scriptname;
17570
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017571 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017572 scriptname = alloc((unsigned)(STRLEN(name) + 14));
17573 if (scriptname == NULL)
17574 return FALSE;
17575 STRCPY(scriptname, "autoload/");
17576 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017577 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017578 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017579 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017580 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017581 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017582}
17583
Bram Moolenaar071d4272004-06-13 20:20:40 +000017584#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
17585
17586/*
17587 * Function given to ExpandGeneric() to obtain the list of user defined
17588 * function names.
17589 */
17590 char_u *
17591get_user_func_name(xp, idx)
17592 expand_T *xp;
17593 int idx;
17594{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017595 static long_u done;
17596 static hashitem_T *hi;
17597 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017598
17599 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017600 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017601 done = 0;
17602 hi = func_hashtab.ht_array;
17603 }
17604 if (done < func_hashtab.ht_used)
17605 {
17606 if (done++ > 0)
17607 ++hi;
17608 while (HASHITEM_EMPTY(hi))
17609 ++hi;
17610 fp = HI2UF(hi);
17611
17612 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
17613 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017614
17615 cat_func_name(IObuff, fp);
17616 if (xp->xp_context != EXPAND_USER_FUNC)
17617 {
17618 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017619 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017620 STRCAT(IObuff, ")");
17621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017622 return IObuff;
17623 }
17624 return NULL;
17625}
17626
17627#endif /* FEAT_CMDL_COMPL */
17628
17629/*
17630 * Copy the function name of "fp" to buffer "buf".
17631 * "buf" must be able to hold the function name plus three bytes.
17632 * Takes care of script-local function names.
17633 */
17634 static void
17635cat_func_name(buf, fp)
17636 char_u *buf;
17637 ufunc_T *fp;
17638{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017639 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017640 {
17641 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017642 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017643 }
17644 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017645 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017646}
17647
17648/*
17649 * ":delfunction {name}"
17650 */
17651 void
17652ex_delfunction(eap)
17653 exarg_T *eap;
17654{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017655 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017656 char_u *p;
17657 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017658 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017659
17660 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017661 name = trans_function_name(&p, eap->skip, 0, &fudi);
17662 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017663 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017664 {
17665 if (fudi.fd_dict != NULL && !eap->skip)
17666 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017667 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017668 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017669 if (!ends_excmd(*skipwhite(p)))
17670 {
17671 vim_free(name);
17672 EMSG(_(e_trailing));
17673 return;
17674 }
17675 eap->nextcmd = check_nextcmd(p);
17676 if (eap->nextcmd != NULL)
17677 *p = NUL;
17678
17679 if (!eap->skip)
17680 fp = find_func(name);
17681 vim_free(name);
17682
17683 if (!eap->skip)
17684 {
17685 if (fp == NULL)
17686 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000017687 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017688 return;
17689 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017690 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017691 {
17692 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
17693 return;
17694 }
17695
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017696 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017697 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017698 /* Delete the dict item that refers to the function, it will
17699 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017700 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017701 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017702 else
17703 func_free(fp);
17704 }
17705}
17706
17707/*
17708 * Free a function and remove it from the list of functions.
17709 */
17710 static void
17711func_free(fp)
17712 ufunc_T *fp;
17713{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017714 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017715
17716 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017717 ga_clear_strings(&(fp->uf_args));
17718 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000017719#ifdef FEAT_PROFILE
17720 vim_free(fp->uf_tml_count);
17721 vim_free(fp->uf_tml_total);
17722 vim_free(fp->uf_tml_self);
17723#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017724
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017725 /* remove the function from the function hashtable */
17726 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
17727 if (HASHITEM_EMPTY(hi))
17728 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017729 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017730 hash_remove(&func_hashtab, hi);
17731
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017732 vim_free(fp);
17733}
17734
17735/*
17736 * Unreference a Function: decrement the reference count and free it when it
17737 * becomes zero. Only for numbered functions.
17738 */
17739 static void
17740func_unref(name)
17741 char_u *name;
17742{
17743 ufunc_T *fp;
17744
17745 if (name != NULL && isdigit(*name))
17746 {
17747 fp = find_func(name);
17748 if (fp == NULL)
17749 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017750 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017751 {
17752 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017753 * when "uf_calls" becomes zero. */
17754 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017755 func_free(fp);
17756 }
17757 }
17758}
17759
17760/*
17761 * Count a reference to a Function.
17762 */
17763 static void
17764func_ref(name)
17765 char_u *name;
17766{
17767 ufunc_T *fp;
17768
17769 if (name != NULL && isdigit(*name))
17770 {
17771 fp = find_func(name);
17772 if (fp == NULL)
17773 EMSG2(_(e_intern2), "func_ref()");
17774 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017775 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017776 }
17777}
17778
17779/*
17780 * Call a user function.
17781 */
17782 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000017783call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017784 ufunc_T *fp; /* pointer to function */
17785 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000017786 typval_T *argvars; /* arguments */
17787 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017788 linenr_T firstline; /* first line of range */
17789 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000017790 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017791{
Bram Moolenaar33570922005-01-25 22:26:29 +000017792 char_u *save_sourcing_name;
17793 linenr_T save_sourcing_lnum;
17794 scid_T save_current_SID;
17795 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000017796 int save_did_emsg;
17797 static int depth = 0;
17798 dictitem_T *v;
17799 int fixvar_idx = 0; /* index in fixvar[] */
17800 int i;
17801 int ai;
17802 char_u numbuf[NUMBUFLEN];
17803 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017804#ifdef FEAT_PROFILE
17805 proftime_T wait_start;
17806#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017807
17808 /* If depth of calling is getting too high, don't execute the function */
17809 if (depth >= p_mfd)
17810 {
17811 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017812 rettv->v_type = VAR_NUMBER;
17813 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017814 return;
17815 }
17816 ++depth;
17817
17818 line_breakcheck(); /* check for CTRL-C hit */
17819
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017820 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000017821 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017822 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017823 fc.rettv = rettv;
17824 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017825 fc.linenr = 0;
17826 fc.returned = FALSE;
17827 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017828 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017829 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017830 fc.dbg_tick = debug_tick;
17831
Bram Moolenaar33570922005-01-25 22:26:29 +000017832 /*
17833 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
17834 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
17835 * each argument variable and saves a lot of time.
17836 */
17837 /*
17838 * Init l: variables.
17839 */
17840 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000017841 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017842 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017843 /* Set l:self to "selfdict". */
17844 v = &fc.fixvar[fixvar_idx++].var;
17845 STRCPY(v->di_key, "self");
17846 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
17847 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
17848 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017849 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000017850 v->di_tv.vval.v_dict = selfdict;
17851 ++selfdict->dv_refcount;
17852 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000017853
Bram Moolenaar33570922005-01-25 22:26:29 +000017854 /*
17855 * Init a: variables.
17856 * Set a:0 to "argcount".
17857 * Set a:000 to a list with room for the "..." arguments.
17858 */
17859 init_var_dict(&fc.l_avars, &fc.l_avars_var);
17860 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017861 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000017862 v = &fc.fixvar[fixvar_idx++].var;
17863 STRCPY(v->di_key, "000");
17864 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
17865 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
17866 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017867 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017868 v->di_tv.vval.v_list = &fc.l_varlist;
17869 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
17870 fc.l_varlist.lv_refcount = 99999;
17871
17872 /*
17873 * Set a:firstline to "firstline" and a:lastline to "lastline".
17874 * Set a:name to named arguments.
17875 * Set a:N to the "..." arguments.
17876 */
17877 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
17878 (varnumber_T)firstline);
17879 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
17880 (varnumber_T)lastline);
17881 for (i = 0; i < argcount; ++i)
17882 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017883 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000017884 if (ai < 0)
17885 /* named argument a:name */
17886 name = FUNCARG(fp, i);
17887 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000017888 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017889 /* "..." argument a:1, a:2, etc. */
17890 sprintf((char *)numbuf, "%d", ai + 1);
17891 name = numbuf;
17892 }
17893 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
17894 {
17895 v = &fc.fixvar[fixvar_idx++].var;
17896 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
17897 }
17898 else
17899 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017900 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
17901 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000017902 if (v == NULL)
17903 break;
17904 v->di_flags = DI_FLAGS_RO;
17905 }
17906 STRCPY(v->di_key, name);
17907 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
17908
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017909 /* Note: the values are copied directly to avoid alloc/free.
17910 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017911 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017912 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017913
17914 if (ai >= 0 && ai < MAX_FUNC_ARGS)
17915 {
17916 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
17917 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017918 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017919 }
17920 }
17921
Bram Moolenaar071d4272004-06-13 20:20:40 +000017922 /* Don't redraw while executing the function. */
17923 ++RedrawingDisabled;
17924 save_sourcing_name = sourcing_name;
17925 save_sourcing_lnum = sourcing_lnum;
17926 sourcing_lnum = 1;
17927 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017928 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017929 if (sourcing_name != NULL)
17930 {
17931 if (save_sourcing_name != NULL
17932 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
17933 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
17934 else
17935 STRCPY(sourcing_name, "function ");
17936 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
17937
17938 if (p_verbose >= 12)
17939 {
17940 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000017941 verbose_enter_scroll();
17942
Bram Moolenaar555b2802005-05-19 21:08:39 +000017943 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017944 if (p_verbose >= 14)
17945 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017946 char_u buf[MSG_BUF_LEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000017947 char_u numbuf[NUMBUFLEN];
17948 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017949
17950 msg_puts((char_u *)"(");
17951 for (i = 0; i < argcount; ++i)
17952 {
17953 if (i > 0)
17954 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017955 if (argvars[i].v_type == VAR_NUMBER)
17956 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017957 else
17958 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000017959 trunc_string(tv2string(&argvars[i], &tofree, numbuf),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017960 buf, MSG_BUF_CLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017961 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000017962 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017963 }
17964 }
17965 msg_puts((char_u *)")");
17966 }
17967 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000017968
17969 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017970 --no_wait_return;
17971 }
17972 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000017973#ifdef FEAT_PROFILE
17974 if (do_profiling)
17975 {
17976 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
17977 func_do_profile(fp);
17978 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017979 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000017980 {
17981 ++fp->uf_tm_count;
17982 profile_start(&fp->uf_tm_start);
17983 profile_zero(&fp->uf_tm_children);
17984 }
17985 script_prof_save(&wait_start);
17986 }
17987#endif
17988
Bram Moolenaar071d4272004-06-13 20:20:40 +000017989 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017990 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017991 save_did_emsg = did_emsg;
17992 did_emsg = FALSE;
17993
17994 /* call do_cmdline() to execute the lines */
17995 do_cmdline(NULL, get_func_line, (void *)&fc,
17996 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
17997
17998 --RedrawingDisabled;
17999
18000 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018001 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018002 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018003 clear_tv(rettv);
18004 rettv->v_type = VAR_NUMBER;
18005 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018006 }
18007
Bram Moolenaar05159a02005-02-26 23:04:13 +000018008#ifdef FEAT_PROFILE
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018009 if (fp->uf_profiling || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000018010 {
18011 profile_end(&fp->uf_tm_start);
18012 profile_sub_wait(&wait_start, &fp->uf_tm_start);
18013 profile_add(&fp->uf_tm_total, &fp->uf_tm_start);
18014 profile_add(&fp->uf_tm_self, &fp->uf_tm_start);
18015 profile_sub(&fp->uf_tm_self, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018016 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018017 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018018 profile_add(&fc.caller->func->uf_tm_children, &fp->uf_tm_start);
18019 profile_add(&fc.caller->func->uf_tml_children, &fp->uf_tm_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018020 }
18021 }
18022#endif
18023
Bram Moolenaar071d4272004-06-13 20:20:40 +000018024 /* when being verbose, mention the return value */
18025 if (p_verbose >= 12)
18026 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018027 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018028 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018029
Bram Moolenaar071d4272004-06-13 20:20:40 +000018030 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000018031 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018032 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000018033 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
18034 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000018035 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000018036 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000018037 char_u buf[MSG_BUF_LEN];
18038 char_u numbuf[NUMBUFLEN];
18039 char_u *tofree;
18040
Bram Moolenaar555b2802005-05-19 21:08:39 +000018041 /* The value may be very long. Skip the middle part, so that we
18042 * have some idea how it starts and ends. smsg() would always
18043 * truncate it at the end. */
Bram Moolenaar758711c2005-02-02 23:11:38 +000018044 trunc_string(tv2string(fc.rettv, &tofree, numbuf),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018045 buf, MSG_BUF_CLEN);
Bram Moolenaar555b2802005-05-19 21:08:39 +000018046 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000018047 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018048 }
18049 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018050
18051 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018052 --no_wait_return;
18053 }
18054
18055 vim_free(sourcing_name);
18056 sourcing_name = save_sourcing_name;
18057 sourcing_lnum = save_sourcing_lnum;
18058 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018059#ifdef FEAT_PROFILE
18060 if (do_profiling)
18061 script_prof_restore(&wait_start);
18062#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018063
18064 if (p_verbose >= 12 && sourcing_name != NULL)
18065 {
18066 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018067 verbose_enter_scroll();
18068
Bram Moolenaar555b2802005-05-19 21:08:39 +000018069 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018070 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018071
18072 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018073 --no_wait_return;
18074 }
18075
18076 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018077 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018078
Bram Moolenaar33570922005-01-25 22:26:29 +000018079 /* The a: variables typevals were not alloced, only free the allocated
18080 * variables. */
18081 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
18082
18083 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018084 --depth;
18085}
18086
18087/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018088 * Add a number variable "name" to dict "dp" with value "nr".
18089 */
18090 static void
18091add_nr_var(dp, v, name, nr)
18092 dict_T *dp;
18093 dictitem_T *v;
18094 char *name;
18095 varnumber_T nr;
18096{
18097 STRCPY(v->di_key, name);
18098 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18099 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
18100 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018101 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018102 v->di_tv.vval.v_number = nr;
18103}
18104
18105/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018106 * ":return [expr]"
18107 */
18108 void
18109ex_return(eap)
18110 exarg_T *eap;
18111{
18112 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018113 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018114 int returning = FALSE;
18115
18116 if (current_funccal == NULL)
18117 {
18118 EMSG(_("E133: :return not inside a function"));
18119 return;
18120 }
18121
18122 if (eap->skip)
18123 ++emsg_skip;
18124
18125 eap->nextcmd = NULL;
18126 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018127 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018128 {
18129 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018130 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018131 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018132 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018133 }
18134 /* It's safer to return also on error. */
18135 else if (!eap->skip)
18136 {
18137 /*
18138 * Return unless the expression evaluation has been cancelled due to an
18139 * aborting error, an interrupt, or an exception.
18140 */
18141 if (!aborting())
18142 returning = do_return(eap, FALSE, TRUE, NULL);
18143 }
18144
18145 /* When skipping or the return gets pending, advance to the next command
18146 * in this line (!returning). Otherwise, ignore the rest of the line.
18147 * Following lines will be ignored by get_func_line(). */
18148 if (returning)
18149 eap->nextcmd = NULL;
18150 else if (eap->nextcmd == NULL) /* no argument */
18151 eap->nextcmd = check_nextcmd(arg);
18152
18153 if (eap->skip)
18154 --emsg_skip;
18155}
18156
18157/*
18158 * Return from a function. Possibly makes the return pending. Also called
18159 * for a pending return at the ":endtry" or after returning from an extra
18160 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000018161 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018162 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018163 * FALSE when the return gets pending.
18164 */
18165 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018166do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018167 exarg_T *eap;
18168 int reanimate;
18169 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018170 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018171{
18172 int idx;
18173 struct condstack *cstack = eap->cstack;
18174
18175 if (reanimate)
18176 /* Undo the return. */
18177 current_funccal->returned = FALSE;
18178
18179 /*
18180 * Cleanup (and inactivate) conditionals, but stop when a try conditional
18181 * not in its finally clause (which then is to be executed next) is found.
18182 * In this case, make the ":return" pending for execution at the ":endtry".
18183 * Otherwise, return normally.
18184 */
18185 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
18186 if (idx >= 0)
18187 {
18188 cstack->cs_pending[idx] = CSTP_RETURN;
18189
18190 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018191 /* A pending return again gets pending. "rettv" points to an
18192 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000018193 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018194 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018195 else
18196 {
18197 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018198 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018199 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018200 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018201
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018202 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018203 {
18204 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018205 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018206 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018207 else
18208 EMSG(_(e_outofmem));
18209 }
18210 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018211 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018212
18213 if (reanimate)
18214 {
18215 /* The pending return value could be overwritten by a ":return"
18216 * without argument in a finally clause; reset the default
18217 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018218 current_funccal->rettv->v_type = VAR_NUMBER;
18219 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018220 }
18221 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018222 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018223 }
18224 else
18225 {
18226 current_funccal->returned = TRUE;
18227
18228 /* If the return is carried out now, store the return value. For
18229 * a return immediately after reanimation, the value is already
18230 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018231 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018232 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018233 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000018234 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018235 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018236 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018237 }
18238 }
18239
18240 return idx < 0;
18241}
18242
18243/*
18244 * Free the variable with a pending return value.
18245 */
18246 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018247discard_pending_return(rettv)
18248 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018249{
Bram Moolenaar33570922005-01-25 22:26:29 +000018250 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018251}
18252
18253/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018254 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000018255 * is an allocated string. Used by report_pending() for verbose messages.
18256 */
18257 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018258get_return_cmd(rettv)
18259 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018260{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018261 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018262 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018263 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018264
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018265 if (rettv != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018266 s = echo_string((typval_T *)rettv, &tofree, numbuf);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018267 if (s == NULL)
18268 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018269
18270 STRCPY(IObuff, ":return ");
18271 STRNCPY(IObuff + 8, s, IOSIZE - 8);
18272 if (STRLEN(s) + 8 >= IOSIZE)
18273 STRCPY(IObuff + IOSIZE - 4, "...");
18274 vim_free(tofree);
18275 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018276}
18277
18278/*
18279 * Get next function line.
18280 * Called by do_cmdline() to get the next line.
18281 * Returns allocated string, or NULL for end of function.
18282 */
18283/* ARGSUSED */
18284 char_u *
18285get_func_line(c, cookie, indent)
18286 int c; /* not used */
18287 void *cookie;
18288 int indent; /* not used */
18289{
Bram Moolenaar33570922005-01-25 22:26:29 +000018290 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018291 ufunc_T *fp = fcp->func;
18292 char_u *retval;
18293 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018294
18295 /* If breakpoints have been added/deleted need to check for it. */
18296 if (fcp->dbg_tick != debug_tick)
18297 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000018298 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018299 sourcing_lnum);
18300 fcp->dbg_tick = debug_tick;
18301 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000018302#ifdef FEAT_PROFILE
18303 if (do_profiling)
18304 func_line_end(cookie);
18305#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018306
Bram Moolenaar05159a02005-02-26 23:04:13 +000018307 gap = &fp->uf_lines;
18308 if ((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000018309 retval = NULL;
18310 else if (fcp->returned || fcp->linenr >= gap->ga_len)
18311 retval = NULL;
18312 else
18313 {
18314 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
18315 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018316#ifdef FEAT_PROFILE
18317 if (do_profiling)
18318 func_line_start(cookie);
18319#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018320 }
18321
18322 /* Did we encounter a breakpoint? */
18323 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
18324 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000018325 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018326 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018327 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018328 sourcing_lnum);
18329 fcp->dbg_tick = debug_tick;
18330 }
18331
18332 return retval;
18333}
18334
Bram Moolenaar05159a02005-02-26 23:04:13 +000018335#if defined(FEAT_PROFILE) || defined(PROTO)
18336/*
18337 * Called when starting to read a function line.
18338 * "sourcing_lnum" must be correct!
18339 * When skipping lines it may not actually be executed, but we won't find out
18340 * until later and we need to store the time now.
18341 */
18342 void
18343func_line_start(cookie)
18344 void *cookie;
18345{
18346 funccall_T *fcp = (funccall_T *)cookie;
18347 ufunc_T *fp = fcp->func;
18348
18349 if (fp->uf_profiling && sourcing_lnum >= 1
18350 && sourcing_lnum <= fp->uf_lines.ga_len)
18351 {
18352 fp->uf_tml_idx = sourcing_lnum - 1;
18353 fp->uf_tml_execed = FALSE;
18354 profile_start(&fp->uf_tml_start);
18355 profile_zero(&fp->uf_tml_children);
18356 profile_get_wait(&fp->uf_tml_wait);
18357 }
18358}
18359
18360/*
18361 * Called when actually executing a function line.
18362 */
18363 void
18364func_line_exec(cookie)
18365 void *cookie;
18366{
18367 funccall_T *fcp = (funccall_T *)cookie;
18368 ufunc_T *fp = fcp->func;
18369
18370 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
18371 fp->uf_tml_execed = TRUE;
18372}
18373
18374/*
18375 * Called when done with a function line.
18376 */
18377 void
18378func_line_end(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 {
18386 if (fp->uf_tml_execed)
18387 {
18388 ++fp->uf_tml_count[fp->uf_tml_idx];
18389 profile_end(&fp->uf_tml_start);
18390 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
18391 profile_add(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start);
18392 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
18393 profile_sub(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_children);
18394 }
18395 fp->uf_tml_idx = -1;
18396 }
18397}
18398#endif
18399
Bram Moolenaar071d4272004-06-13 20:20:40 +000018400/*
18401 * Return TRUE if the currently active function should be ended, because a
18402 * return was encountered or an error occured. Used inside a ":while".
18403 */
18404 int
18405func_has_ended(cookie)
18406 void *cookie;
18407{
Bram Moolenaar33570922005-01-25 22:26:29 +000018408 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018409
18410 /* Ignore the "abort" flag if the abortion behavior has been changed due to
18411 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018412 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000018413 || fcp->returned);
18414}
18415
18416/*
18417 * return TRUE if cookie indicates a function which "abort"s on errors.
18418 */
18419 int
18420func_has_abort(cookie)
18421 void *cookie;
18422{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018423 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018424}
18425
18426#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
18427typedef enum
18428{
18429 VAR_FLAVOUR_DEFAULT,
18430 VAR_FLAVOUR_SESSION,
18431 VAR_FLAVOUR_VIMINFO
18432} var_flavour_T;
18433
18434static var_flavour_T var_flavour __ARGS((char_u *varname));
18435
18436 static var_flavour_T
18437var_flavour(varname)
18438 char_u *varname;
18439{
18440 char_u *p = varname;
18441
18442 if (ASCII_ISUPPER(*p))
18443 {
18444 while (*(++p))
18445 if (ASCII_ISLOWER(*p))
18446 return VAR_FLAVOUR_SESSION;
18447 return VAR_FLAVOUR_VIMINFO;
18448 }
18449 else
18450 return VAR_FLAVOUR_DEFAULT;
18451}
18452#endif
18453
18454#if defined(FEAT_VIMINFO) || defined(PROTO)
18455/*
18456 * Restore global vars that start with a capital from the viminfo file
18457 */
18458 int
18459read_viminfo_varlist(virp, writing)
18460 vir_T *virp;
18461 int writing;
18462{
18463 char_u *tab;
18464 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000018465 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018466
18467 if (!writing && (find_viminfo_parameter('!') != NULL))
18468 {
18469 tab = vim_strchr(virp->vir_line + 1, '\t');
18470 if (tab != NULL)
18471 {
18472 *tab++ = '\0'; /* isolate the variable name */
18473 if (*tab == 'S') /* string var */
18474 is_string = TRUE;
18475
18476 tab = vim_strchr(tab, '\t');
18477 if (tab != NULL)
18478 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018479 if (is_string)
18480 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000018481 tv.v_type = VAR_STRING;
18482 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018483 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018484 }
18485 else
18486 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000018487 tv.v_type = VAR_NUMBER;
18488 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018489 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000018490 set_var(virp->vir_line + 1, &tv, FALSE);
18491 if (is_string)
18492 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018493 }
18494 }
18495 }
18496
18497 return viminfo_readline(virp);
18498}
18499
18500/*
18501 * Write global vars that start with a capital to the viminfo file
18502 */
18503 void
18504write_viminfo_varlist(fp)
18505 FILE *fp;
18506{
Bram Moolenaar33570922005-01-25 22:26:29 +000018507 hashitem_T *hi;
18508 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000018509 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018510 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018511 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018512 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018513 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018514
18515 if (find_viminfo_parameter('!') == NULL)
18516 return;
18517
18518 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000018519
Bram Moolenaar33570922005-01-25 22:26:29 +000018520 todo = globvarht.ht_used;
18521 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018522 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018523 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018524 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018525 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000018526 this_var = HI2DI(hi);
18527 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018528 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018529 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000018530 {
18531 case VAR_STRING: s = "STR"; break;
18532 case VAR_NUMBER: s = "NUM"; break;
18533 default: continue;
18534 }
Bram Moolenaar33570922005-01-25 22:26:29 +000018535 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018536 p = echo_string(&this_var->di_tv, &tofree, numbuf);
18537 if (p != NULL)
18538 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000018539 vim_free(tofree);
18540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018541 }
18542 }
18543}
18544#endif
18545
18546#if defined(FEAT_SESSION) || defined(PROTO)
18547 int
18548store_session_globals(fd)
18549 FILE *fd;
18550{
Bram Moolenaar33570922005-01-25 22:26:29 +000018551 hashitem_T *hi;
18552 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000018553 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018554 char_u *p, *t;
18555
Bram Moolenaar33570922005-01-25 22:26:29 +000018556 todo = globvarht.ht_used;
18557 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018558 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018559 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018560 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018561 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000018562 this_var = HI2DI(hi);
18563 if ((this_var->di_tv.v_type == VAR_NUMBER
18564 || this_var->di_tv.v_type == VAR_STRING)
18565 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000018566 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018567 /* Escape special characters with a backslash. Turn a LF and
18568 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000018569 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000018570 (char_u *)"\\\"\n\r");
18571 if (p == NULL) /* out of memory */
18572 break;
18573 for (t = p; *t != NUL; ++t)
18574 if (*t == '\n')
18575 *t = 'n';
18576 else if (*t == '\r')
18577 *t = 'r';
18578 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000018579 this_var->di_key,
18580 (this_var->di_tv.v_type == VAR_STRING) ? '"'
18581 : ' ',
18582 p,
18583 (this_var->di_tv.v_type == VAR_STRING) ? '"'
18584 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000018585 || put_eol(fd) == FAIL)
18586 {
18587 vim_free(p);
18588 return FAIL;
18589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018590 vim_free(p);
18591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018592 }
18593 }
18594 return OK;
18595}
18596#endif
18597
18598#endif /* FEAT_EVAL */
18599
18600#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
18601
18602
18603#ifdef WIN3264
18604/*
18605 * Functions for ":8" filename modifier: get 8.3 version of a filename.
18606 */
18607static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
18608static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
18609static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
18610
18611/*
18612 * Get the short pathname of a file.
18613 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
18614 */
18615 static int
18616get_short_pathname(fnamep, bufp, fnamelen)
18617 char_u **fnamep;
18618 char_u **bufp;
18619 int *fnamelen;
18620{
18621 int l,len;
18622 char_u *newbuf;
18623
18624 len = *fnamelen;
18625
18626 l = GetShortPathName(*fnamep, *fnamep, len);
18627 if (l > len - 1)
18628 {
18629 /* If that doesn't work (not enough space), then save the string
18630 * and try again with a new buffer big enough
18631 */
18632 newbuf = vim_strnsave(*fnamep, l);
18633 if (newbuf == NULL)
18634 return 0;
18635
18636 vim_free(*bufp);
18637 *fnamep = *bufp = newbuf;
18638
18639 l = GetShortPathName(*fnamep,*fnamep,l+1);
18640
18641 /* Really should always succeed, as the buffer is big enough */
18642 }
18643
18644 *fnamelen = l;
18645 return 1;
18646}
18647
18648/*
18649 * Create a short path name. Returns the length of the buffer it needs.
18650 * Doesn't copy over the end of the buffer passed in.
18651 */
18652 static int
18653shortpath_for_invalid_fname(fname, bufp, fnamelen)
18654 char_u **fname;
18655 char_u **bufp;
18656 int *fnamelen;
18657{
18658 char_u *s, *p, *pbuf2, *pbuf3;
18659 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000018660 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018661
18662 /* Make a copy */
18663 len2 = *fnamelen;
18664 pbuf2 = vim_strnsave(*fname, len2);
18665 pbuf3 = NULL;
18666
18667 s = pbuf2 + len2 - 1; /* Find the end */
18668 slen = 1;
18669 plen = len2;
18670
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018671 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018672 {
18673 --s;
18674 ++slen;
18675 --plen;
18676 }
18677
18678 do
18679 {
18680 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018681 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018682 {
18683 --s;
18684 ++slen;
18685 --plen;
18686 }
18687 if (s <= pbuf2)
18688 break;
18689
18690 /* Remeber the character that is about to be blatted */
18691 ch = *s;
18692 *s = 0; /* get_short_pathname requires a null-terminated string */
18693
18694 /* Try it in situ */
18695 p = pbuf2;
18696 if (!get_short_pathname(&p, &pbuf3, &plen))
18697 {
18698 vim_free(pbuf2);
18699 return -1;
18700 }
18701 *s = ch; /* Preserve the string */
18702 } while (plen == 0);
18703
18704 if (plen > 0)
18705 {
18706 /* Remeber the length of the new string. */
18707 *fnamelen = len = plen + slen;
18708 vim_free(*bufp);
18709 if (len > len2)
18710 {
18711 /* If there's not enough space in the currently allocated string,
18712 * then copy it to a buffer big enough.
18713 */
18714 *fname= *bufp = vim_strnsave(p, len);
18715 if (*fname == NULL)
18716 return -1;
18717 }
18718 else
18719 {
18720 /* Transfer pbuf2 to being the main buffer (it's big enough) */
18721 *fname = *bufp = pbuf2;
18722 if (p != pbuf2)
18723 strncpy(*fname, p, plen);
18724 pbuf2 = NULL;
18725 }
18726 /* Concat the next bit */
18727 strncpy(*fname + plen, s, slen);
18728 (*fname)[len] = '\0';
18729 }
18730 vim_free(pbuf3);
18731 vim_free(pbuf2);
18732 return 0;
18733}
18734
18735/*
18736 * Get a pathname for a partial path.
18737 */
18738 static int
18739shortpath_for_partial(fnamep, bufp, fnamelen)
18740 char_u **fnamep;
18741 char_u **bufp;
18742 int *fnamelen;
18743{
18744 int sepcount, len, tflen;
18745 char_u *p;
18746 char_u *pbuf, *tfname;
18747 int hasTilde;
18748
18749 /* Count up the path seperators from the RHS.. so we know which part
18750 * of the path to return.
18751 */
18752 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018753 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018754 if (vim_ispathsep(*p))
18755 ++sepcount;
18756
18757 /* Need full path first (use expand_env() to remove a "~/") */
18758 hasTilde = (**fnamep == '~');
18759 if (hasTilde)
18760 pbuf = tfname = expand_env_save(*fnamep);
18761 else
18762 pbuf = tfname = FullName_save(*fnamep, FALSE);
18763
18764 len = tflen = STRLEN(tfname);
18765
18766 if (!get_short_pathname(&tfname, &pbuf, &len))
18767 return -1;
18768
18769 if (len == 0)
18770 {
18771 /* Don't have a valid filename, so shorten the rest of the
18772 * path if we can. This CAN give us invalid 8.3 filenames, but
18773 * there's not a lot of point in guessing what it might be.
18774 */
18775 len = tflen;
18776 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
18777 return -1;
18778 }
18779
18780 /* Count the paths backward to find the beginning of the desired string. */
18781 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018782 {
18783#ifdef FEAT_MBYTE
18784 if (has_mbyte)
18785 p -= mb_head_off(tfname, p);
18786#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018787 if (vim_ispathsep(*p))
18788 {
18789 if (sepcount == 0 || (hasTilde && sepcount == 1))
18790 break;
18791 else
18792 sepcount --;
18793 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018795 if (hasTilde)
18796 {
18797 --p;
18798 if (p >= tfname)
18799 *p = '~';
18800 else
18801 return -1;
18802 }
18803 else
18804 ++p;
18805
18806 /* Copy in the string - p indexes into tfname - allocated at pbuf */
18807 vim_free(*bufp);
18808 *fnamelen = (int)STRLEN(p);
18809 *bufp = pbuf;
18810 *fnamep = p;
18811
18812 return 0;
18813}
18814#endif /* WIN3264 */
18815
18816/*
18817 * Adjust a filename, according to a string of modifiers.
18818 * *fnamep must be NUL terminated when called. When returning, the length is
18819 * determined by *fnamelen.
18820 * Returns valid flags.
18821 * When there is an error, *fnamep is set to NULL.
18822 */
18823 int
18824modify_fname(src, usedlen, fnamep, bufp, fnamelen)
18825 char_u *src; /* string with modifiers */
18826 int *usedlen; /* characters after src that are used */
18827 char_u **fnamep; /* file name so far */
18828 char_u **bufp; /* buffer for allocated file name or NULL */
18829 int *fnamelen; /* length of fnamep */
18830{
18831 int valid = 0;
18832 char_u *tail;
18833 char_u *s, *p, *pbuf;
18834 char_u dirname[MAXPATHL];
18835 int c;
18836 int has_fullname = 0;
18837#ifdef WIN3264
18838 int has_shortname = 0;
18839#endif
18840
18841repeat:
18842 /* ":p" - full path/file_name */
18843 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
18844 {
18845 has_fullname = 1;
18846
18847 valid |= VALID_PATH;
18848 *usedlen += 2;
18849
18850 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
18851 if ((*fnamep)[0] == '~'
18852#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
18853 && ((*fnamep)[1] == '/'
18854# ifdef BACKSLASH_IN_FILENAME
18855 || (*fnamep)[1] == '\\'
18856# endif
18857 || (*fnamep)[1] == NUL)
18858
18859#endif
18860 )
18861 {
18862 *fnamep = expand_env_save(*fnamep);
18863 vim_free(*bufp); /* free any allocated file name */
18864 *bufp = *fnamep;
18865 if (*fnamep == NULL)
18866 return -1;
18867 }
18868
18869 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018870 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018871 {
18872 if (vim_ispathsep(*p)
18873 && p[1] == '.'
18874 && (p[2] == NUL
18875 || vim_ispathsep(p[2])
18876 || (p[2] == '.'
18877 && (p[3] == NUL || vim_ispathsep(p[3])))))
18878 break;
18879 }
18880
18881 /* FullName_save() is slow, don't use it when not needed. */
18882 if (*p != NUL || !vim_isAbsName(*fnamep))
18883 {
18884 *fnamep = FullName_save(*fnamep, *p != NUL);
18885 vim_free(*bufp); /* free any allocated file name */
18886 *bufp = *fnamep;
18887 if (*fnamep == NULL)
18888 return -1;
18889 }
18890
18891 /* Append a path separator to a directory. */
18892 if (mch_isdir(*fnamep))
18893 {
18894 /* Make room for one or two extra characters. */
18895 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
18896 vim_free(*bufp); /* free any allocated file name */
18897 *bufp = *fnamep;
18898 if (*fnamep == NULL)
18899 return -1;
18900 add_pathsep(*fnamep);
18901 }
18902 }
18903
18904 /* ":." - path relative to the current directory */
18905 /* ":~" - path relative to the home directory */
18906 /* ":8" - shortname path - postponed till after */
18907 while (src[*usedlen] == ':'
18908 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
18909 {
18910 *usedlen += 2;
18911 if (c == '8')
18912 {
18913#ifdef WIN3264
18914 has_shortname = 1; /* Postpone this. */
18915#endif
18916 continue;
18917 }
18918 pbuf = NULL;
18919 /* Need full path first (use expand_env() to remove a "~/") */
18920 if (!has_fullname)
18921 {
18922 if (c == '.' && **fnamep == '~')
18923 p = pbuf = expand_env_save(*fnamep);
18924 else
18925 p = pbuf = FullName_save(*fnamep, FALSE);
18926 }
18927 else
18928 p = *fnamep;
18929
18930 has_fullname = 0;
18931
18932 if (p != NULL)
18933 {
18934 if (c == '.')
18935 {
18936 mch_dirname(dirname, MAXPATHL);
18937 s = shorten_fname(p, dirname);
18938 if (s != NULL)
18939 {
18940 *fnamep = s;
18941 if (pbuf != NULL)
18942 {
18943 vim_free(*bufp); /* free any allocated file name */
18944 *bufp = pbuf;
18945 pbuf = NULL;
18946 }
18947 }
18948 }
18949 else
18950 {
18951 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
18952 /* Only replace it when it starts with '~' */
18953 if (*dirname == '~')
18954 {
18955 s = vim_strsave(dirname);
18956 if (s != NULL)
18957 {
18958 *fnamep = s;
18959 vim_free(*bufp);
18960 *bufp = s;
18961 }
18962 }
18963 }
18964 vim_free(pbuf);
18965 }
18966 }
18967
18968 tail = gettail(*fnamep);
18969 *fnamelen = (int)STRLEN(*fnamep);
18970
18971 /* ":h" - head, remove "/file_name", can be repeated */
18972 /* Don't remove the first "/" or "c:\" */
18973 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
18974 {
18975 valid |= VALID_HEAD;
18976 *usedlen += 2;
18977 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018978 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018979 --tail;
18980 *fnamelen = (int)(tail - *fnamep);
18981#ifdef VMS
18982 if (*fnamelen > 0)
18983 *fnamelen += 1; /* the path separator is part of the path */
18984#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018985 while (tail > s && !after_pathsep(s, tail))
18986 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018987 }
18988
18989 /* ":8" - shortname */
18990 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
18991 {
18992 *usedlen += 2;
18993#ifdef WIN3264
18994 has_shortname = 1;
18995#endif
18996 }
18997
18998#ifdef WIN3264
18999 /* Check shortname after we have done 'heads' and before we do 'tails'
19000 */
19001 if (has_shortname)
19002 {
19003 pbuf = NULL;
19004 /* Copy the string if it is shortened by :h */
19005 if (*fnamelen < (int)STRLEN(*fnamep))
19006 {
19007 p = vim_strnsave(*fnamep, *fnamelen);
19008 if (p == 0)
19009 return -1;
19010 vim_free(*bufp);
19011 *bufp = *fnamep = p;
19012 }
19013
19014 /* Split into two implementations - makes it easier. First is where
19015 * there isn't a full name already, second is where there is.
19016 */
19017 if (!has_fullname && !vim_isAbsName(*fnamep))
19018 {
19019 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
19020 return -1;
19021 }
19022 else
19023 {
19024 int l;
19025
19026 /* Simple case, already have the full-name
19027 * Nearly always shorter, so try first time. */
19028 l = *fnamelen;
19029 if (!get_short_pathname(fnamep, bufp, &l))
19030 return -1;
19031
19032 if (l == 0)
19033 {
19034 /* Couldn't find the filename.. search the paths.
19035 */
19036 l = *fnamelen;
19037 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
19038 return -1;
19039 }
19040 *fnamelen = l;
19041 }
19042 }
19043#endif /* WIN3264 */
19044
19045 /* ":t" - tail, just the basename */
19046 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
19047 {
19048 *usedlen += 2;
19049 *fnamelen -= (int)(tail - *fnamep);
19050 *fnamep = tail;
19051 }
19052
19053 /* ":e" - extension, can be repeated */
19054 /* ":r" - root, without extension, can be repeated */
19055 while (src[*usedlen] == ':'
19056 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
19057 {
19058 /* find a '.' in the tail:
19059 * - for second :e: before the current fname
19060 * - otherwise: The last '.'
19061 */
19062 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
19063 s = *fnamep - 2;
19064 else
19065 s = *fnamep + *fnamelen - 1;
19066 for ( ; s > tail; --s)
19067 if (s[0] == '.')
19068 break;
19069 if (src[*usedlen + 1] == 'e') /* :e */
19070 {
19071 if (s > tail)
19072 {
19073 *fnamelen += (int)(*fnamep - (s + 1));
19074 *fnamep = s + 1;
19075#ifdef VMS
19076 /* cut version from the extension */
19077 s = *fnamep + *fnamelen - 1;
19078 for ( ; s > *fnamep; --s)
19079 if (s[0] == ';')
19080 break;
19081 if (s > *fnamep)
19082 *fnamelen = s - *fnamep;
19083#endif
19084 }
19085 else if (*fnamep <= tail)
19086 *fnamelen = 0;
19087 }
19088 else /* :r */
19089 {
19090 if (s > tail) /* remove one extension */
19091 *fnamelen = (int)(s - *fnamep);
19092 }
19093 *usedlen += 2;
19094 }
19095
19096 /* ":s?pat?foo?" - substitute */
19097 /* ":gs?pat?foo?" - global substitute */
19098 if (src[*usedlen] == ':'
19099 && (src[*usedlen + 1] == 's'
19100 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
19101 {
19102 char_u *str;
19103 char_u *pat;
19104 char_u *sub;
19105 int sep;
19106 char_u *flags;
19107 int didit = FALSE;
19108
19109 flags = (char_u *)"";
19110 s = src + *usedlen + 2;
19111 if (src[*usedlen + 1] == 'g')
19112 {
19113 flags = (char_u *)"g";
19114 ++s;
19115 }
19116
19117 sep = *s++;
19118 if (sep)
19119 {
19120 /* find end of pattern */
19121 p = vim_strchr(s, sep);
19122 if (p != NULL)
19123 {
19124 pat = vim_strnsave(s, (int)(p - s));
19125 if (pat != NULL)
19126 {
19127 s = p + 1;
19128 /* find end of substitution */
19129 p = vim_strchr(s, sep);
19130 if (p != NULL)
19131 {
19132 sub = vim_strnsave(s, (int)(p - s));
19133 str = vim_strnsave(*fnamep, *fnamelen);
19134 if (sub != NULL && str != NULL)
19135 {
19136 *usedlen = (int)(p + 1 - src);
19137 s = do_string_sub(str, pat, sub, flags);
19138 if (s != NULL)
19139 {
19140 *fnamep = s;
19141 *fnamelen = (int)STRLEN(s);
19142 vim_free(*bufp);
19143 *bufp = s;
19144 didit = TRUE;
19145 }
19146 }
19147 vim_free(sub);
19148 vim_free(str);
19149 }
19150 vim_free(pat);
19151 }
19152 }
19153 /* after using ":s", repeat all the modifiers */
19154 if (didit)
19155 goto repeat;
19156 }
19157 }
19158
19159 return valid;
19160}
19161
19162/*
19163 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
19164 * "flags" can be "g" to do a global substitute.
19165 * Returns an allocated string, NULL for error.
19166 */
19167 char_u *
19168do_string_sub(str, pat, sub, flags)
19169 char_u *str;
19170 char_u *pat;
19171 char_u *sub;
19172 char_u *flags;
19173{
19174 int sublen;
19175 regmatch_T regmatch;
19176 int i;
19177 int do_all;
19178 char_u *tail;
19179 garray_T ga;
19180 char_u *ret;
19181 char_u *save_cpo;
19182
19183 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
19184 save_cpo = p_cpo;
19185 p_cpo = (char_u *)"";
19186
19187 ga_init2(&ga, 1, 200);
19188
19189 do_all = (flags[0] == 'g');
19190
19191 regmatch.rm_ic = p_ic;
19192 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19193 if (regmatch.regprog != NULL)
19194 {
19195 tail = str;
19196 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
19197 {
19198 /*
19199 * Get some space for a temporary buffer to do the substitution
19200 * into. It will contain:
19201 * - The text up to where the match is.
19202 * - The substituted text.
19203 * - The text after the match.
19204 */
19205 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
19206 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
19207 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
19208 {
19209 ga_clear(&ga);
19210 break;
19211 }
19212
19213 /* copy the text up to where the match is */
19214 i = (int)(regmatch.startp[0] - tail);
19215 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
19216 /* add the substituted text */
19217 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
19218 + ga.ga_len + i, TRUE, TRUE, FALSE);
19219 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019220 /* avoid getting stuck on a match with an empty string */
19221 if (tail == regmatch.endp[0])
19222 {
19223 if (*tail == NUL)
19224 break;
19225 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
19226 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019227 }
19228 else
19229 {
19230 tail = regmatch.endp[0];
19231 if (*tail == NUL)
19232 break;
19233 }
19234 if (!do_all)
19235 break;
19236 }
19237
19238 if (ga.ga_data != NULL)
19239 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
19240
19241 vim_free(regmatch.regprog);
19242 }
19243
19244 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
19245 ga_clear(&ga);
19246 p_cpo = save_cpo;
19247
19248 return ret;
19249}
19250
19251#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */