blob: 206bbfbfaffeed09a1b98b5f8c1a74e7da81bcc0 [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));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000412static int list_append_string __ARGS((list_T *l, char_u *str, int len));
413static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000414static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
415static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
416static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000417static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000418static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
419static char_u *list2string __ARGS((typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000420static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000421static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
422static void set_ref_in_list __ARGS((list_T *l, int copyID));
423static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000424static void dict_unref __ARGS((dict_T *d));
425static void dict_free __ARGS((dict_T *d));
426static dictitem_T *dictitem_alloc __ARGS((char_u *key));
427static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
428static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
429static void dictitem_free __ARGS((dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000430static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000431static int dict_add __ARGS((dict_T *d, dictitem_T *item));
432static long dict_len __ARGS((dict_T *d));
433static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
434static char_u *dict2string __ARGS((typval_T *tv));
435static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar33570922005-01-25 22:26:29 +0000436static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf));
437static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf));
438static char_u *string_quote __ARGS((char_u *str, int function));
439static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
440static int find_internal_func __ARGS((char_u *name));
441static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
442static 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));
443static 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 +0000444static void emsg_funcname __ARGS((char *msg, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000445
446static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
447static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
448static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
449static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
450static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
451static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
452static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
453static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
454static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
455static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
456static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
457static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
458static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
459static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
460static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
463static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
464static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000465#if defined(FEAT_INS_EXPAND)
466static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
467static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
468#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000469static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
470static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
474static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000500static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000501static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000502static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000503static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000508static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000509static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000516static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000517static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
534static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000539static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000540static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000545static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000546static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000562static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000563static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000566#ifdef vim_mkdir
567static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
568#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000569static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000573static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000574static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000575static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000576static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000587static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000588static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000594static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000595static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000599static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000600static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000602static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
603#ifdef HAVE_STRFTIME
604static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
605#endif
606static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000618static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000619static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000620static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000621static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000622static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
624static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
625static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
626static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
627static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
628static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
629static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
630static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
631static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
632static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
633static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
634static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
635static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000636static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000637
Bram Moolenaar33570922005-01-25 22:26:29 +0000638static pos_T *var2fpos __ARGS((typval_T *varp, int lnum));
639static int get_env_len __ARGS((char_u **arg));
640static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000641static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000642static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
643#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
644#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
645 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000646static 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 +0000647static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000648static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000649static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
650static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000651static typval_T *alloc_tv __ARGS((void));
652static typval_T *alloc_string_tv __ARGS((char_u *string));
653static void free_tv __ARGS((typval_T *varp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000654static void init_tv __ARGS((typval_T *varp));
655static long get_tv_number __ARGS((typval_T *varp));
656static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000657static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000658static char_u *get_tv_string __ARGS((typval_T *varp));
659static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000660static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000661static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000662static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000663static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
664static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
665static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
666static void list_one_var __ARGS((dictitem_T *v, char_u *prefix));
667static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
668static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
669static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000670static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000671static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000672static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000673static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
674static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
675static int eval_fname_script __ARGS((char_u *p));
676static int eval_fname_sid __ARGS((char_u *p));
677static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000678static ufunc_T *find_func __ARGS((char_u *name));
679static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000680static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000681#ifdef FEAT_PROFILE
682static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000683static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
684static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
685static int
686# ifdef __BORLANDC__
687 _RTLENTRYF
688# endif
689 prof_total_cmp __ARGS((const void *s1, const void *s2));
690static int
691# ifdef __BORLANDC__
692 _RTLENTRYF
693# endif
694 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000695#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000696static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000697static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000698static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000699static void func_free __ARGS((ufunc_T *fp));
700static void func_unref __ARGS((char_u *name));
701static void func_ref __ARGS((char_u *name));
702static 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));
703static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
704
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000705/* Character used as separated in autoload function/variable names. */
706#define AUTOLOAD_CHAR '#'
707
Bram Moolenaar33570922005-01-25 22:26:29 +0000708/*
709 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000710 */
711 void
712eval_init()
713{
Bram Moolenaar33570922005-01-25 22:26:29 +0000714 int i;
715 struct vimvar *p;
716
717 init_var_dict(&globvardict, &globvars_var);
718 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000719 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000720 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000721
722 for (i = 0; i < VV_LEN; ++i)
723 {
724 p = &vimvars[i];
725 STRCPY(p->vv_di.di_key, p->vv_name);
726 if (p->vv_flags & VV_RO)
727 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
728 else if (p->vv_flags & VV_RO_SBX)
729 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
730 else
731 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000732
733 /* add to v: scope dict, unless the value is not always available */
734 if (p->vv_type != VAR_UNKNOWN)
735 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000736 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000737 /* add to compat scope dict */
738 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000739 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000740}
741
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000742#if defined(EXITFREE) || defined(PROTO)
743 void
744eval_clear()
745{
746 int i;
747 struct vimvar *p;
748
749 for (i = 0; i < VV_LEN; ++i)
750 {
751 p = &vimvars[i];
752 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000753 {
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000754 vim_free(p->vv_di.di_tv.vval.v_string);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000755 p->vv_di.di_tv.vval.v_string = NULL;
756 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000757 }
758 hash_clear(&vimvarht);
759 hash_clear(&compat_hashtab);
760
761 /* script-local variables */
762 for (i = 1; i <= ga_scripts.ga_len; ++i)
763 vars_clear(&SCRIPT_VARS(i));
764 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000765 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000766
767 /* global variables */
768 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000769
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000770 /* functions */
Bram Moolenaard9fba312005-06-26 22:34:35 +0000771 free_all_functions();
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000772 hash_clear(&func_hashtab);
773
774 /* unreferenced lists and dicts */
775 (void)garbage_collect();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000776}
777#endif
778
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000779/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 * Return the name of the executed function.
781 */
782 char_u *
783func_name(cookie)
784 void *cookie;
785{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000786 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787}
788
789/*
790 * Return the address holding the next breakpoint line for a funccall cookie.
791 */
792 linenr_T *
793func_breakpoint(cookie)
794 void *cookie;
795{
Bram Moolenaar33570922005-01-25 22:26:29 +0000796 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797}
798
799/*
800 * Return the address holding the debug tick for a funccall cookie.
801 */
802 int *
803func_dbg_tick(cookie)
804 void *cookie;
805{
Bram Moolenaar33570922005-01-25 22:26:29 +0000806 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807}
808
809/*
810 * Return the nesting level for a funccall cookie.
811 */
812 int
813func_level(cookie)
814 void *cookie;
815{
Bram Moolenaar33570922005-01-25 22:26:29 +0000816 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817}
818
819/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000820funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821
822/*
823 * Return TRUE when a function was ended by a ":return" command.
824 */
825 int
826current_func_returned()
827{
828 return current_funccal->returned;
829}
830
831
832/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 * Set an internal variable to a string value. Creates the variable if it does
834 * not already exist.
835 */
836 void
837set_internal_string_var(name, value)
838 char_u *name;
839 char_u *value;
840{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000841 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000842 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843
844 val = vim_strsave(value);
845 if (val != NULL)
846 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000847 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000848 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000850 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000851 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 }
853 }
854}
855
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000856static lval_T *redir_lval = NULL;
857static char_u *redir_endp = NULL;
858static char_u *redir_varname = NULL;
859
860/*
861 * Start recording command output to a variable
862 * Returns OK if successfully completed the setup. FAIL otherwise.
863 */
864 int
865var_redir_start(name, append)
866 char_u *name;
867 int append; /* append to an existing variable */
868{
869 int save_emsg;
870 int err;
871 typval_T tv;
872
873 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000874 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000875 {
876 EMSG(_(e_invarg));
877 return FAIL;
878 }
879
880 redir_varname = vim_strsave(name);
881 if (redir_varname == NULL)
882 return FAIL;
883
884 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
885 if (redir_lval == NULL)
886 {
887 var_redir_stop();
888 return FAIL;
889 }
890
891 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000892 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
893 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000894 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
895 {
896 if (redir_endp != NULL && *redir_endp != NUL)
897 /* Trailing characters are present after the variable name */
898 EMSG(_(e_trailing));
899 else
900 EMSG(_(e_invarg));
901 var_redir_stop();
902 return FAIL;
903 }
904
905 /* check if we can write to the variable: set it to or append an empty
906 * string */
907 save_emsg = did_emsg;
908 did_emsg = FALSE;
909 tv.v_type = VAR_STRING;
910 tv.vval.v_string = (char_u *)"";
911 if (append)
912 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
913 else
914 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
915 err = did_emsg;
916 did_emsg += save_emsg;
917 if (err)
918 {
919 var_redir_stop();
920 return FAIL;
921 }
922 if (redir_lval->ll_newkey != NULL)
923 {
924 /* Dictionary item was created, don't do it again. */
925 vim_free(redir_lval->ll_newkey);
926 redir_lval->ll_newkey = NULL;
927 }
928
929 return OK;
930}
931
932/*
933 * Append "value[len]" to the variable set by var_redir_start().
934 */
935 void
936var_redir_str(value, len)
937 char_u *value;
938 int len;
939{
940 char_u *val;
941 typval_T tv;
942 int save_emsg;
943 int err;
944
945 if (redir_lval == NULL)
946 return;
947
948 if (len == -1)
949 /* Append the entire string */
950 val = vim_strsave(value);
951 else
952 /* Append only the specified number of characters */
953 val = vim_strnsave(value, len);
954 if (val == NULL)
955 return;
956
957 tv.v_type = VAR_STRING;
958 tv.vval.v_string = val;
959
960 save_emsg = did_emsg;
961 did_emsg = FALSE;
962 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
963 err = did_emsg;
964 did_emsg += save_emsg;
965 if (err)
966 var_redir_stop();
967
968 vim_free(tv.vval.v_string);
969}
970
971/*
972 * Stop redirecting command output to a variable.
973 */
974 void
975var_redir_stop()
976{
977 if (redir_lval != NULL)
978 {
979 clear_lval(redir_lval);
980 vim_free(redir_lval);
981 redir_lval = NULL;
982 }
983 vim_free(redir_varname);
984 redir_varname = NULL;
985}
986
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987# if defined(FEAT_MBYTE) || defined(PROTO)
988 int
989eval_charconvert(enc_from, enc_to, fname_from, fname_to)
990 char_u *enc_from;
991 char_u *enc_to;
992 char_u *fname_from;
993 char_u *fname_to;
994{
995 int err = FALSE;
996
997 set_vim_var_string(VV_CC_FROM, enc_from, -1);
998 set_vim_var_string(VV_CC_TO, enc_to, -1);
999 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1000 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1001 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1002 err = TRUE;
1003 set_vim_var_string(VV_CC_FROM, NULL, -1);
1004 set_vim_var_string(VV_CC_TO, NULL, -1);
1005 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1006 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1007
1008 if (err)
1009 return FAIL;
1010 return OK;
1011}
1012# endif
1013
1014# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1015 int
1016eval_printexpr(fname, args)
1017 char_u *fname;
1018 char_u *args;
1019{
1020 int err = FALSE;
1021
1022 set_vim_var_string(VV_FNAME_IN, fname, -1);
1023 set_vim_var_string(VV_CMDARG, args, -1);
1024 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1025 err = TRUE;
1026 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1027 set_vim_var_string(VV_CMDARG, NULL, -1);
1028
1029 if (err)
1030 {
1031 mch_remove(fname);
1032 return FAIL;
1033 }
1034 return OK;
1035}
1036# endif
1037
1038# if defined(FEAT_DIFF) || defined(PROTO)
1039 void
1040eval_diff(origfile, newfile, outfile)
1041 char_u *origfile;
1042 char_u *newfile;
1043 char_u *outfile;
1044{
1045 int err = FALSE;
1046
1047 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1048 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1049 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1050 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1051 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1052 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1053 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1054}
1055
1056 void
1057eval_patch(origfile, difffile, outfile)
1058 char_u *origfile;
1059 char_u *difffile;
1060 char_u *outfile;
1061{
1062 int err;
1063
1064 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1065 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1066 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1067 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1068 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1069 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1070 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1071}
1072# endif
1073
1074/*
1075 * Top level evaluation function, returning a boolean.
1076 * Sets "error" to TRUE if there was an error.
1077 * Return TRUE or FALSE.
1078 */
1079 int
1080eval_to_bool(arg, error, nextcmd, skip)
1081 char_u *arg;
1082 int *error;
1083 char_u **nextcmd;
1084 int skip; /* only parse, don't execute */
1085{
Bram Moolenaar33570922005-01-25 22:26:29 +00001086 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 int retval = FALSE;
1088
1089 if (skip)
1090 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001091 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 else
1094 {
1095 *error = FALSE;
1096 if (!skip)
1097 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001098 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001099 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 }
1101 }
1102 if (skip)
1103 --emsg_skip;
1104
1105 return retval;
1106}
1107
1108/*
1109 * Top level evaluation function, returning a string. If "skip" is TRUE,
1110 * only parsing to "nextcmd" is done, without reporting errors. Return
1111 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1112 */
1113 char_u *
1114eval_to_string_skip(arg, nextcmd, skip)
1115 char_u *arg;
1116 char_u **nextcmd;
1117 int skip; /* only parse, don't execute */
1118{
Bram Moolenaar33570922005-01-25 22:26:29 +00001119 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 char_u *retval;
1121
1122 if (skip)
1123 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001124 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 retval = NULL;
1126 else
1127 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001128 retval = vim_strsave(get_tv_string(&tv));
1129 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 }
1131 if (skip)
1132 --emsg_skip;
1133
1134 return retval;
1135}
1136
1137/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001138 * Skip over an expression at "*pp".
1139 * Return FAIL for an error, OK otherwise.
1140 */
1141 int
1142skip_expr(pp)
1143 char_u **pp;
1144{
Bram Moolenaar33570922005-01-25 22:26:29 +00001145 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001146
1147 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001148 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001149}
1150
1151/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 * Top level evaluation function, returning a string.
1153 * Return pointer to allocated memory, or NULL for failure.
1154 */
1155 char_u *
1156eval_to_string(arg, nextcmd)
1157 char_u *arg;
1158 char_u **nextcmd;
1159{
Bram Moolenaar33570922005-01-25 22:26:29 +00001160 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 char_u *retval;
1162
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001163 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 retval = NULL;
1165 else
1166 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001167 retval = vim_strsave(get_tv_string(&tv));
1168 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 }
1170
1171 return retval;
1172}
1173
1174/*
1175 * Call eval_to_string() with "sandbox" set and not using local variables.
1176 */
1177 char_u *
1178eval_to_string_safe(arg, nextcmd)
1179 char_u *arg;
1180 char_u **nextcmd;
1181{
1182 char_u *retval;
1183 void *save_funccalp;
1184
1185 save_funccalp = save_funccal();
1186 ++sandbox;
1187 retval = eval_to_string(arg, nextcmd);
1188 --sandbox;
1189 restore_funccal(save_funccalp);
1190 return retval;
1191}
1192
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193/*
1194 * Top level evaluation function, returning a number.
1195 * Evaluates "expr" silently.
1196 * Returns -1 for an error.
1197 */
1198 int
1199eval_to_number(expr)
1200 char_u *expr;
1201{
Bram Moolenaar33570922005-01-25 22:26:29 +00001202 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001204 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205
1206 ++emsg_off;
1207
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001208 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 retval = -1;
1210 else
1211 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001212 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001213 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 }
1215 --emsg_off;
1216
1217 return retval;
1218}
1219
Bram Moolenaara40058a2005-07-11 22:42:07 +00001220/*
1221 * Prepare v: variable "idx" to be used.
1222 * Save the current typeval in "save_tv".
1223 * When not used yet add the variable to the v: hashtable.
1224 */
1225 static void
1226prepare_vimvar(idx, save_tv)
1227 int idx;
1228 typval_T *save_tv;
1229{
1230 *save_tv = vimvars[idx].vv_tv;
1231 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1232 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1233}
1234
1235/*
1236 * Restore v: variable "idx" to typeval "save_tv".
1237 * When no longer defined, remove the variable from the v: hashtable.
1238 */
1239 static void
1240restore_vimvar(idx, save_tv)
1241 int idx;
1242 typval_T *save_tv;
1243{
1244 hashitem_T *hi;
1245
1246 clear_tv(&vimvars[idx].vv_tv);
1247 vimvars[idx].vv_tv = *save_tv;
1248 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1249 {
1250 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1251 if (HASHITEM_EMPTY(hi))
1252 EMSG2(_(e_intern2), "restore_vimvar()");
1253 else
1254 hash_remove(&vimvarht, hi);
1255 }
1256}
1257
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001258#if defined(FEAT_SYN_HL) || defined(PROTO)
1259/*
1260 * Evaluate an expression to a list with suggestions.
1261 * For the "expr:" part of 'spellsuggest'.
1262 */
1263 list_T *
1264eval_spell_expr(badword, expr)
1265 char_u *badword;
1266 char_u *expr;
1267{
1268 typval_T save_val;
1269 typval_T rettv;
1270 list_T *list = NULL;
1271 char_u *p = skipwhite(expr);
1272
1273 /* Set "v:val" to the bad word. */
1274 prepare_vimvar(VV_VAL, &save_val);
1275 vimvars[VV_VAL].vv_type = VAR_STRING;
1276 vimvars[VV_VAL].vv_str = badword;
1277 if (p_verbose == 0)
1278 ++emsg_off;
1279
1280 if (eval1(&p, &rettv, TRUE) == OK)
1281 {
1282 if (rettv.v_type != VAR_LIST)
1283 clear_tv(&rettv);
1284 else
1285 list = rettv.vval.v_list;
1286 }
1287
1288 if (p_verbose == 0)
1289 --emsg_off;
1290 vimvars[VV_VAL].vv_str = NULL;
1291 restore_vimvar(VV_VAL, &save_val);
1292
1293 return list;
1294}
1295
1296/*
1297 * "list" is supposed to contain two items: a word and a number. Return the
1298 * word in "pp" and the number as the return value.
1299 * Return -1 if anything isn't right.
1300 * Used to get the good word and score from the eval_spell_expr() result.
1301 */
1302 int
1303get_spellword(list, pp)
1304 list_T *list;
1305 char_u **pp;
1306{
1307 listitem_T *li;
1308
1309 li = list->lv_first;
1310 if (li == NULL)
1311 return -1;
1312 *pp = get_tv_string(&li->li_tv);
1313
1314 li = li->li_next;
1315 if (li == NULL)
1316 return -1;
1317 return get_tv_number(&li->li_tv);
1318}
1319#endif
1320
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001321/*
1322 * Top level evaluation function,
1323 */
1324 typval_T *
1325eval_expr(arg, nextcmd)
1326 char_u *arg;
1327 char_u **nextcmd;
1328{
1329 typval_T *tv;
1330
1331 tv = (typval_T *)alloc(sizeof(typval_T));
1332 if (!tv)
1333 return NULL;
1334
1335 if (eval0(arg, tv, nextcmd, TRUE) == FAIL)
1336 {
1337 vim_free(tv);
1338 return NULL;
1339 }
1340
1341 return tv;
1342}
1343
1344
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1346/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001347 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 * Uses argv[argc] for the function arguments.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001349 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001351 static int
1352call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 char_u *func;
1354 int argc;
1355 char_u **argv;
1356 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001357 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358{
Bram Moolenaar33570922005-01-25 22:26:29 +00001359 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 long n;
1361 int len;
1362 int i;
1363 int doesrange;
1364 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001365 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366
Bram Moolenaar33570922005-01-25 22:26:29 +00001367 argvars = (typval_T *)alloc((unsigned)(argc * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001369 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370
1371 for (i = 0; i < argc; i++)
1372 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001373 /* Pass a NULL or empty argument as an empty string */
1374 if (argv[i] == NULL || *argv[i] == NUL)
1375 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001376 argvars[i].v_type = VAR_STRING;
1377 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001378 continue;
1379 }
1380
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 /* Recognize a number argument, the others must be strings. */
1382 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1383 if (len != 0 && len == (int)STRLEN(argv[i]))
1384 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001385 argvars[i].v_type = VAR_NUMBER;
1386 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 }
1388 else
1389 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001390 argvars[i].v_type = VAR_STRING;
1391 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 }
1393 }
1394
1395 if (safe)
1396 {
1397 save_funccalp = save_funccal();
1398 ++sandbox;
1399 }
1400
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001401 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1402 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001404 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 if (safe)
1406 {
1407 --sandbox;
1408 restore_funccal(save_funccalp);
1409 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001410 vim_free(argvars);
1411
1412 if (ret == FAIL)
1413 clear_tv(rettv);
1414
1415 return ret;
1416}
1417
1418/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001419 * Call vimL function "func" and return the result as a string.
1420 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001421 * Uses argv[argc] for the function arguments.
1422 */
1423 void *
1424call_func_retstr(func, argc, argv, safe)
1425 char_u *func;
1426 int argc;
1427 char_u **argv;
1428 int safe; /* use the sandbox */
1429{
1430 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001431 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001432
1433 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1434 return NULL;
1435
1436 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001437 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 return retval;
1439}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001440
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001441#if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001442/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001443 * Call vimL function "func" and return the result as a number.
1444 * Returns -1 when calling the function fails.
1445 * Uses argv[argc] for the function arguments.
1446 */
1447 long
1448call_func_retnr(func, argc, argv, safe)
1449 char_u *func;
1450 int argc;
1451 char_u **argv;
1452 int safe; /* use the sandbox */
1453{
1454 typval_T rettv;
1455 long retval;
1456
1457 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1458 return -1;
1459
1460 retval = get_tv_number_chk(&rettv, NULL);
1461 clear_tv(&rettv);
1462 return retval;
1463}
1464#endif
1465
1466/*
1467 * Call vimL function "func" and return the result as a list
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001468 * Uses argv[argc] for the function arguments.
1469 */
1470 void *
1471call_func_retlist(func, argc, argv, safe)
1472 char_u *func;
1473 int argc;
1474 char_u **argv;
1475 int safe; /* use the sandbox */
1476{
1477 typval_T rettv;
1478
1479 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1480 return NULL;
1481
1482 if (rettv.v_type != VAR_LIST)
1483 {
1484 clear_tv(&rettv);
1485 return NULL;
1486 }
1487
1488 return rettv.vval.v_list;
1489}
1490
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491#endif
1492
1493/*
1494 * Save the current function call pointer, and set it to NULL.
1495 * Used when executing autocommands and for ":source".
1496 */
1497 void *
1498save_funccal()
1499{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001500 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 current_funccal = NULL;
1503 return (void *)fc;
1504}
1505
1506 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001507restore_funccal(vfc)
1508 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001510 funccall_T *fc = (funccall_T *)vfc;
1511
1512 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513}
1514
Bram Moolenaar05159a02005-02-26 23:04:13 +00001515#if defined(FEAT_PROFILE) || defined(PROTO)
1516/*
1517 * Prepare profiling for entering a child or something else that is not
1518 * counted for the script/function itself.
1519 * Should always be called in pair with prof_child_exit().
1520 */
1521 void
1522prof_child_enter(tm)
1523 proftime_T *tm; /* place to store waittime */
1524{
1525 funccall_T *fc = current_funccal;
1526
1527 if (fc != NULL && fc->func->uf_profiling)
1528 profile_start(&fc->prof_child);
1529 script_prof_save(tm);
1530}
1531
1532/*
1533 * Take care of time spent in a child.
1534 * Should always be called after prof_child_enter().
1535 */
1536 void
1537prof_child_exit(tm)
1538 proftime_T *tm; /* where waittime was stored */
1539{
1540 funccall_T *fc = current_funccal;
1541
1542 if (fc != NULL && fc->func->uf_profiling)
1543 {
1544 profile_end(&fc->prof_child);
1545 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1546 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1547 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1548 }
1549 script_prof_restore(tm);
1550}
1551#endif
1552
1553
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554#ifdef FEAT_FOLDING
1555/*
1556 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1557 * it in "*cp". Doesn't give error messages.
1558 */
1559 int
1560eval_foldexpr(arg, cp)
1561 char_u *arg;
1562 int *cp;
1563{
Bram Moolenaar33570922005-01-25 22:26:29 +00001564 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 int retval;
1566 char_u *s;
1567
1568 ++emsg_off;
1569 ++sandbox;
1570 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001571 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 retval = 0;
1573 else
1574 {
1575 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001576 if (tv.v_type == VAR_NUMBER)
1577 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001578 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 retval = 0;
1580 else
1581 {
1582 /* If the result is a string, check if there is a non-digit before
1583 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001584 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 if (!VIM_ISDIGIT(*s) && *s != '-')
1586 *cp = *s++;
1587 retval = atol((char *)s);
1588 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001589 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 }
1591 --emsg_off;
1592 --sandbox;
1593
1594 return retval;
1595}
1596#endif
1597
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001599 * ":let" list all variable values
1600 * ":let var1 var2" list variable values
1601 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001602 * ":let var += expr" assignment command.
1603 * ":let var -= expr" assignment command.
1604 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001605 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 */
1607 void
1608ex_let(eap)
1609 exarg_T *eap;
1610{
1611 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001612 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001613 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001615 int var_count = 0;
1616 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001617 char_u op[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001619 expr = skip_var_list(arg, &var_count, &semicolon);
1620 if (expr == NULL)
1621 return;
1622 expr = vim_strchr(expr, '=');
1623 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001625 /*
1626 * ":let" without "=": list variables
1627 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001628 if (*arg == '[')
1629 EMSG(_(e_invarg));
1630 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001631 /* ":let var1 var2" */
1632 arg = list_arg_vars(eap, arg);
1633 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001634 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001635 /* ":let" */
Bram Moolenaara7043832005-01-21 11:56:39 +00001636 list_glob_vars();
1637 list_buf_vars();
1638 list_win_vars();
1639 list_vim_vars();
1640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 eap->nextcmd = check_nextcmd(arg);
1642 }
1643 else
1644 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001645 op[0] = '=';
1646 op[1] = NUL;
1647 if (expr > arg)
1648 {
1649 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1650 op[0] = expr[-1]; /* +=, -= or .= */
1651 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001652 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001653
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 if (eap->skip)
1655 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001656 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 if (eap->skip)
1658 {
1659 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001660 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 --emsg_skip;
1662 }
1663 else if (i != FAIL)
1664 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001665 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001666 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001667 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 }
1669 }
1670}
1671
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001672/*
1673 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1674 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001675 * When "nextchars" is not NULL it points to a string with characters that
1676 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1677 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001678 * Returns OK or FAIL;
1679 */
1680 static int
1681ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1682 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001683 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001684 int copy; /* copy values from "tv", don't move */
1685 int semicolon; /* from skip_var_list() */
1686 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001687 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001688{
1689 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001690 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001691 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001692 listitem_T *item;
1693 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001694
1695 if (*arg != '[')
1696 {
1697 /*
1698 * ":let var = expr" or ":for var in list"
1699 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001700 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001701 return FAIL;
1702 return OK;
1703 }
1704
1705 /*
1706 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1707 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001708 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001709 {
1710 EMSG(_(e_listreq));
1711 return FAIL;
1712 }
1713
1714 i = list_len(l);
1715 if (semicolon == 0 && var_count < i)
1716 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001717 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001718 return FAIL;
1719 }
1720 if (var_count - semicolon > i)
1721 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001722 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001723 return FAIL;
1724 }
1725
1726 item = l->lv_first;
1727 while (*arg != ']')
1728 {
1729 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001730 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001731 item = item->li_next;
1732 if (arg == NULL)
1733 return FAIL;
1734
1735 arg = skipwhite(arg);
1736 if (*arg == ';')
1737 {
1738 /* Put the rest of the list (may be empty) in the var after ';'.
1739 * Create a new list for this. */
1740 l = list_alloc();
1741 if (l == NULL)
1742 return FAIL;
1743 while (item != NULL)
1744 {
1745 list_append_tv(l, &item->li_tv);
1746 item = item->li_next;
1747 }
1748
1749 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001750 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001751 ltv.vval.v_list = l;
1752 l->lv_refcount = 1;
1753
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001754 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1755 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001756 clear_tv(&ltv);
1757 if (arg == NULL)
1758 return FAIL;
1759 break;
1760 }
1761 else if (*arg != ',' && *arg != ']')
1762 {
1763 EMSG2(_(e_intern2), "ex_let_vars()");
1764 return FAIL;
1765 }
1766 }
1767
1768 return OK;
1769}
1770
1771/*
1772 * Skip over assignable variable "var" or list of variables "[var, var]".
1773 * Used for ":let varvar = expr" and ":for varvar in expr".
1774 * For "[var, var]" increment "*var_count" for each variable.
1775 * for "[var, var; var]" set "semicolon".
1776 * Return NULL for an error.
1777 */
1778 static char_u *
1779skip_var_list(arg, var_count, semicolon)
1780 char_u *arg;
1781 int *var_count;
1782 int *semicolon;
1783{
1784 char_u *p, *s;
1785
1786 if (*arg == '[')
1787 {
1788 /* "[var, var]": find the matching ']'. */
1789 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001790 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001791 {
1792 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1793 s = skip_var_one(p);
1794 if (s == p)
1795 {
1796 EMSG2(_(e_invarg2), p);
1797 return NULL;
1798 }
1799 ++*var_count;
1800
1801 p = skipwhite(s);
1802 if (*p == ']')
1803 break;
1804 else if (*p == ';')
1805 {
1806 if (*semicolon == 1)
1807 {
1808 EMSG(_("Double ; in list of variables"));
1809 return NULL;
1810 }
1811 *semicolon = 1;
1812 }
1813 else if (*p != ',')
1814 {
1815 EMSG2(_(e_invarg2), p);
1816 return NULL;
1817 }
1818 }
1819 return p + 1;
1820 }
1821 else
1822 return skip_var_one(arg);
1823}
1824
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001825/*
Bram Moolenaar92124a32005-06-17 22:03:40 +00001826 * Skip one (assignable) variable name, includig @r, $VAR, &option, d.key,
1827 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001828 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001829 static char_u *
1830skip_var_one(arg)
1831 char_u *arg;
1832{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001833 if (*arg == '@' && arg[1] != NUL)
1834 return arg + 2;
1835 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1836 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001837}
1838
Bram Moolenaara7043832005-01-21 11:56:39 +00001839/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001840 * List variables for hashtab "ht" with prefix "prefix".
1841 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001842 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001843 static void
Bram Moolenaar33570922005-01-25 22:26:29 +00001844list_hashtable_vars(ht, prefix, empty)
1845 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001846 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001847 int empty;
Bram Moolenaara7043832005-01-21 11:56:39 +00001848{
Bram Moolenaar33570922005-01-25 22:26:29 +00001849 hashitem_T *hi;
1850 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001851 int todo;
1852
1853 todo = ht->ht_used;
1854 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1855 {
1856 if (!HASHITEM_EMPTY(hi))
1857 {
1858 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001859 di = HI2DI(hi);
1860 if (empty || di->di_tv.v_type != VAR_STRING
1861 || di->di_tv.vval.v_string != NULL)
1862 list_one_var(di, prefix);
Bram Moolenaara7043832005-01-21 11:56:39 +00001863 }
1864 }
1865}
1866
1867/*
1868 * List global variables.
1869 */
1870 static void
1871list_glob_vars()
1872{
Bram Moolenaar33570922005-01-25 22:26:29 +00001873 list_hashtable_vars(&globvarht, (char_u *)"", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001874}
1875
1876/*
1877 * List buffer variables.
1878 */
1879 static void
1880list_buf_vars()
1881{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001882 char_u numbuf[NUMBUFLEN];
1883
Bram Moolenaar33570922005-01-25 22:26:29 +00001884 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001885
1886 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
1887 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER, numbuf);
Bram Moolenaara7043832005-01-21 11:56:39 +00001888}
1889
1890/*
1891 * List window variables.
1892 */
1893 static void
1894list_win_vars()
1895{
Bram Moolenaar33570922005-01-25 22:26:29 +00001896 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001897}
1898
1899/*
1900 * List Vim variables.
1901 */
1902 static void
1903list_vim_vars()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001904{
Bram Moolenaar33570922005-01-25 22:26:29 +00001905 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001906}
1907
1908/*
1909 * List variables in "arg".
1910 */
1911 static char_u *
1912list_arg_vars(eap, arg)
1913 exarg_T *eap;
1914 char_u *arg;
1915{
1916 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001917 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001918 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001919 char_u *name_start;
1920 char_u *arg_subsc;
1921 char_u *tofree;
1922 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001923
1924 while (!ends_excmd(*arg) && !got_int)
1925 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001926 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001927 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001928 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001929 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
1930 {
1931 emsg_severe = TRUE;
1932 EMSG(_(e_trailing));
1933 break;
1934 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001935 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001936 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001937 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001938 /* get_name_len() takes care of expanding curly braces */
1939 name_start = name = arg;
1940 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1941 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001942 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001943 /* This is mainly to keep test 49 working: when expanding
1944 * curly braces fails overrule the exception error message. */
1945 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001946 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001947 emsg_severe = TRUE;
1948 EMSG2(_(e_invarg2), arg);
1949 break;
1950 }
1951 error = TRUE;
1952 }
1953 else
1954 {
1955 if (tofree != NULL)
1956 name = tofree;
1957 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001958 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001959 else
1960 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001961 /* handle d.key, l[idx], f(expr) */
1962 arg_subsc = arg;
1963 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001964 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001965 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001966 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001967 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001968 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001969 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001970 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001971 case 'g': list_glob_vars(); break;
1972 case 'b': list_buf_vars(); break;
1973 case 'w': list_win_vars(); break;
1974 case 'v': list_vim_vars(); break;
1975 default:
1976 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001977 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001978 }
1979 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001980 {
1981 char_u numbuf[NUMBUFLEN];
1982 char_u *tf;
1983 int c;
1984 char_u *s;
1985
1986 s = echo_string(&tv, &tf, numbuf);
1987 c = *arg;
1988 *arg = NUL;
1989 list_one_var_a((char_u *)"",
1990 arg == arg_subsc ? name : name_start,
1991 tv.v_type, s == NULL ? (char_u *)"" : s);
1992 *arg = c;
1993 vim_free(tf);
1994 }
1995 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001996 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001997 }
1998 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001999
2000 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002001 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002002
2003 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002004 }
2005
2006 return arg;
2007}
2008
2009/*
2010 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2011 * Returns a pointer to the char just after the var name.
2012 * Returns NULL if there is an error.
2013 */
2014 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002015ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002016 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002017 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002018 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002019 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002020 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002021{
2022 int c1;
2023 char_u *name;
2024 char_u *p;
2025 char_u *arg_end = NULL;
2026 int len;
2027 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002028 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002029
2030 /*
2031 * ":let $VAR = expr": Set environment variable.
2032 */
2033 if (*arg == '$')
2034 {
2035 /* Find the end of the name. */
2036 ++arg;
2037 name = arg;
2038 len = get_env_len(&arg);
2039 if (len == 0)
2040 EMSG2(_(e_invarg2), name - 1);
2041 else
2042 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002043 if (op != NULL && (*op == '+' || *op == '-'))
2044 EMSG2(_(e_letwrong), op);
2045 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002046 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002047 EMSG(_(e_letunexp));
2048 else
2049 {
2050 c1 = name[len];
2051 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002052 p = get_tv_string_chk(tv);
2053 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002054 {
2055 int mustfree = FALSE;
2056 char_u *s = vim_getenv(name, &mustfree);
2057
2058 if (s != NULL)
2059 {
2060 p = tofree = concat_str(s, p);
2061 if (mustfree)
2062 vim_free(s);
2063 }
2064 }
2065 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002066 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002067 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002068 if (STRICMP(name, "HOME") == 0)
2069 init_homedir();
2070 else if (didset_vim && STRICMP(name, "VIM") == 0)
2071 didset_vim = FALSE;
2072 else if (didset_vimruntime
2073 && STRICMP(name, "VIMRUNTIME") == 0)
2074 didset_vimruntime = FALSE;
2075 arg_end = arg;
2076 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002077 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002078 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002079 }
2080 }
2081 }
2082
2083 /*
2084 * ":let &option = expr": Set option value.
2085 * ":let &l:option = expr": Set local option value.
2086 * ":let &g:option = expr": Set global option value.
2087 */
2088 else if (*arg == '&')
2089 {
2090 /* Find the end of the name. */
2091 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002092 if (p == NULL || (endchars != NULL
2093 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002094 EMSG(_(e_letunexp));
2095 else
2096 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002097 long n;
2098 int opt_type;
2099 long numval;
2100 char_u *stringval = NULL;
2101 char_u *s;
2102
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002103 c1 = *p;
2104 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002105
2106 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002107 s = get_tv_string_chk(tv); /* != NULL if number or string */
2108 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002109 {
2110 opt_type = get_option_value(arg, &numval,
2111 &stringval, opt_flags);
2112 if ((opt_type == 1 && *op == '.')
2113 || (opt_type == 0 && *op != '.'))
2114 EMSG2(_(e_letwrong), op);
2115 else
2116 {
2117 if (opt_type == 1) /* number */
2118 {
2119 if (*op == '+')
2120 n = numval + n;
2121 else
2122 n = numval - n;
2123 }
2124 else if (opt_type == 0 && stringval != NULL) /* string */
2125 {
2126 s = concat_str(stringval, s);
2127 vim_free(stringval);
2128 stringval = s;
2129 }
2130 }
2131 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002132 if (s != NULL)
2133 {
2134 set_option_value(arg, n, s, opt_flags);
2135 arg_end = p;
2136 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002137 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002138 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002139 }
2140 }
2141
2142 /*
2143 * ":let @r = expr": Set register contents.
2144 */
2145 else if (*arg == '@')
2146 {
2147 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002148 if (op != NULL && (*op == '+' || *op == '-'))
2149 EMSG2(_(e_letwrong), op);
2150 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002151 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002152 EMSG(_(e_letunexp));
2153 else
2154 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002155 char_u *tofree = NULL;
2156 char_u *s;
2157
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002158 p = get_tv_string_chk(tv);
2159 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002160 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002161 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002162 if (s != NULL)
2163 {
2164 p = tofree = concat_str(s, p);
2165 vim_free(s);
2166 }
2167 }
2168 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002169 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002170 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002171 arg_end = arg + 1;
2172 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002173 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002174 }
2175 }
2176
2177 /*
2178 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002179 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002180 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002181 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002182 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002183 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002184
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002185 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002186 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002187 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002188 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2189 EMSG(_(e_letunexp));
2190 else
2191 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002192 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002193 arg_end = p;
2194 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002195 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002196 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002197 }
2198
2199 else
2200 EMSG2(_(e_invarg2), arg);
2201
2202 return arg_end;
2203}
2204
2205/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002206 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2207 */
2208 static int
2209check_changedtick(arg)
2210 char_u *arg;
2211{
2212 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2213 {
2214 EMSG2(_(e_readonlyvar), arg);
2215 return TRUE;
2216 }
2217 return FALSE;
2218}
2219
2220/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002221 * Get an lval: variable, Dict item or List item that can be assigned a value
2222 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2223 * "name.key", "name.key[expr]" etc.
2224 * Indexing only works if "name" is an existing List or Dictionary.
2225 * "name" points to the start of the name.
2226 * If "rettv" is not NULL it points to the value to be assigned.
2227 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2228 * wrong; must end in space or cmd separator.
2229 *
2230 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002231 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002232 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002233 */
2234 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002235get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002236 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002237 typval_T *rettv;
2238 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002239 int unlet;
2240 int skip;
2241 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002242 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002243{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002244 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002245 char_u *expr_start, *expr_end;
2246 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002247 dictitem_T *v;
2248 typval_T var1;
2249 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002250 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002251 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002252 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002253 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002254 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002255
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002256 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002257 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002258
2259 if (skip)
2260 {
2261 /* When skipping just find the end of the name. */
2262 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002263 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002264 }
2265
2266 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002267 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002268 if (expr_start != NULL)
2269 {
2270 /* Don't expand the name when we already know there is an error. */
2271 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2272 && *p != '[' && *p != '.')
2273 {
2274 EMSG(_(e_trailing));
2275 return NULL;
2276 }
2277
2278 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2279 if (lp->ll_exp_name == NULL)
2280 {
2281 /* Report an invalid expression in braces, unless the
2282 * expression evaluation has been cancelled due to an
2283 * aborting error, an interrupt, or an exception. */
2284 if (!aborting() && !quiet)
2285 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002286 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002287 EMSG2(_(e_invarg2), name);
2288 return NULL;
2289 }
2290 }
2291 lp->ll_name = lp->ll_exp_name;
2292 }
2293 else
2294 lp->ll_name = name;
2295
2296 /* Without [idx] or .key we are done. */
2297 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2298 return p;
2299
2300 cc = *p;
2301 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002302 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002303 if (v == NULL && !quiet)
2304 EMSG2(_(e_undefvar), lp->ll_name);
2305 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002306 if (v == NULL)
2307 return NULL;
2308
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002309 /*
2310 * Loop until no more [idx] or .key is following.
2311 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002312 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002313 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002314 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002315 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2316 && !(lp->ll_tv->v_type == VAR_DICT
2317 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002318 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002319 if (!quiet)
2320 EMSG(_("E689: Can only index a List or Dictionary"));
2321 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002322 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002323 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002324 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002325 if (!quiet)
2326 EMSG(_("E708: [:] must come last"));
2327 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002328 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002329
Bram Moolenaar8c711452005-01-14 21:53:12 +00002330 len = -1;
2331 if (*p == '.')
2332 {
2333 key = p + 1;
2334 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2335 ;
2336 if (len == 0)
2337 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002338 if (!quiet)
2339 EMSG(_(e_emptykey));
2340 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002341 }
2342 p = key + len;
2343 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002344 else
2345 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002346 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002347 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002348 if (*p == ':')
2349 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002350 else
2351 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002352 empty1 = FALSE;
2353 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002354 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002355 if (get_tv_string_chk(&var1) == NULL)
2356 {
2357 /* not a number or string */
2358 clear_tv(&var1);
2359 return NULL;
2360 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002361 }
2362
2363 /* Optionally get the second index [ :expr]. */
2364 if (*p == ':')
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 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002368 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002369 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002370 if (!empty1)
2371 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002372 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002373 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002374 if (rettv != NULL && (rettv->v_type != VAR_LIST
2375 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002376 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002377 if (!quiet)
2378 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002379 if (!empty1)
2380 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002381 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002382 }
2383 p = skipwhite(p + 1);
2384 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002385 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002386 else
2387 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002388 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002389 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2390 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002391 if (!empty1)
2392 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002393 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002394 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002395 if (get_tv_string_chk(&var2) == NULL)
2396 {
2397 /* not a number or string */
2398 if (!empty1)
2399 clear_tv(&var1);
2400 clear_tv(&var2);
2401 return NULL;
2402 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002403 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002404 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002405 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002406 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002407 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002408
Bram Moolenaar8c711452005-01-14 21:53:12 +00002409 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002410 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002411 if (!quiet)
2412 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002413 if (!empty1)
2414 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002415 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002416 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002417 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002418 }
2419
2420 /* Skip to past ']'. */
2421 ++p;
2422 }
2423
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002424 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002425 {
2426 if (len == -1)
2427 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002428 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002429 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002430 if (*key == NUL)
2431 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002432 if (!quiet)
2433 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002434 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002435 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002436 }
2437 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002438 lp->ll_list = NULL;
2439 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002440 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002441 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002442 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002443 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002444 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002445 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002446 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002447 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002448 if (len == -1)
2449 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002450 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002451 }
2452 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002453 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002454 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002455 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002456 if (len == -1)
2457 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002458 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002459 p = NULL;
2460 break;
2461 }
2462 if (len == -1)
2463 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002464 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002465 }
2466 else
2467 {
2468 /*
2469 * Get the number and item for the only or first index of the List.
2470 */
2471 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002472 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002473 else
2474 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002475 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002476 clear_tv(&var1);
2477 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002478 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002479 lp->ll_list = lp->ll_tv->vval.v_list;
2480 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2481 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002482 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002483 if (!quiet)
2484 EMSGN(_(e_listidx), lp->ll_n1);
2485 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002486 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002487 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002488 }
2489
2490 /*
2491 * May need to find the item or absolute index for the second
2492 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002493 * When no index given: "lp->ll_empty2" is TRUE.
2494 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002495 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002496 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002497 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002498 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002499 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002500 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002501 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002502 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002503 if (ni == NULL)
2504 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002505 if (!quiet)
2506 EMSGN(_(e_listidx), lp->ll_n2);
2507 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002508 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002509 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002510 }
2511
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002512 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2513 if (lp->ll_n1 < 0)
2514 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2515 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002516 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002517 if (!quiet)
2518 EMSGN(_(e_listidx), lp->ll_n2);
2519 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002520 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002521 }
2522
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002523 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002524 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002525 }
2526
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002527 return p;
2528}
2529
2530/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002531 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002532 */
2533 static void
2534clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002535 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002536{
2537 vim_free(lp->ll_exp_name);
2538 vim_free(lp->ll_newkey);
2539}
2540
2541/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002542 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002543 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002544 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002545 */
2546 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002547set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002548 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002549 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002550 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002551 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002552 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002553{
2554 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002555 listitem_T *ri;
2556 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002557
2558 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002559 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002560 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002561 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002562 cc = *endp;
2563 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002564 if (op != NULL && *op != '=')
2565 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002566 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002567
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002568 /* handle +=, -= and .= */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002569 if (get_var_tv(lp->ll_name, STRLEN(lp->ll_name),
2570 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002571 {
2572 if (tv_op(&tv, rettv, op) == OK)
2573 set_var(lp->ll_name, &tv, FALSE);
2574 clear_tv(&tv);
2575 }
2576 }
2577 else
2578 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002579 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002580 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002581 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002582 else if (tv_check_lock(lp->ll_newkey == NULL
2583 ? lp->ll_tv->v_lock
2584 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2585 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002586 else if (lp->ll_range)
2587 {
2588 /*
2589 * Assign the List values to the list items.
2590 */
2591 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002592 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002593 if (op != NULL && *op != '=')
2594 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2595 else
2596 {
2597 clear_tv(&lp->ll_li->li_tv);
2598 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2599 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002600 ri = ri->li_next;
2601 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2602 break;
2603 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002604 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002605 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002606 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002607 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002608 ri = NULL;
2609 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002610 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002611 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002612 lp->ll_li = lp->ll_li->li_next;
2613 ++lp->ll_n1;
2614 }
2615 if (ri != NULL)
2616 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002617 else if (lp->ll_empty2
2618 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002619 : lp->ll_n1 != lp->ll_n2)
2620 EMSG(_("E711: List value has not enough items"));
2621 }
2622 else
2623 {
2624 /*
2625 * Assign to a List or Dictionary item.
2626 */
2627 if (lp->ll_newkey != NULL)
2628 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002629 if (op != NULL && *op != '=')
2630 {
2631 EMSG2(_(e_letwrong), op);
2632 return;
2633 }
2634
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002635 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002636 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002637 if (di == NULL)
2638 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002639 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2640 {
2641 vim_free(di);
2642 return;
2643 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002644 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002645 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002646 else if (op != NULL && *op != '=')
2647 {
2648 tv_op(lp->ll_tv, rettv, op);
2649 return;
2650 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002651 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002652 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002653
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002654 /*
2655 * Assign the value to the variable or list item.
2656 */
2657 if (copy)
2658 copy_tv(rettv, lp->ll_tv);
2659 else
2660 {
2661 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002662 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002663 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002664 }
2665 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002666}
2667
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002668/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002669 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2670 * Returns OK or FAIL.
2671 */
2672 static int
2673tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002674 typval_T *tv1;
2675 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002676 char_u *op;
2677{
2678 long n;
2679 char_u numbuf[NUMBUFLEN];
2680 char_u *s;
2681
2682 /* Can't do anything with a Funcref or a Dict on the right. */
2683 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2684 {
2685 switch (tv1->v_type)
2686 {
2687 case VAR_DICT:
2688 case VAR_FUNC:
2689 break;
2690
2691 case VAR_LIST:
2692 if (*op != '+' || tv2->v_type != VAR_LIST)
2693 break;
2694 /* List += List */
2695 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2696 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2697 return OK;
2698
2699 case VAR_NUMBER:
2700 case VAR_STRING:
2701 if (tv2->v_type == VAR_LIST)
2702 break;
2703 if (*op == '+' || *op == '-')
2704 {
2705 /* nr += nr or nr -= nr*/
2706 n = get_tv_number(tv1);
2707 if (*op == '+')
2708 n += get_tv_number(tv2);
2709 else
2710 n -= get_tv_number(tv2);
2711 clear_tv(tv1);
2712 tv1->v_type = VAR_NUMBER;
2713 tv1->vval.v_number = n;
2714 }
2715 else
2716 {
2717 /* str .= str */
2718 s = get_tv_string(tv1);
2719 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2720 clear_tv(tv1);
2721 tv1->v_type = VAR_STRING;
2722 tv1->vval.v_string = s;
2723 }
2724 return OK;
2725 }
2726 }
2727
2728 EMSG2(_(e_letwrong), op);
2729 return FAIL;
2730}
2731
2732/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002733 * Add a watcher to a list.
2734 */
2735 static void
2736list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002737 list_T *l;
2738 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002739{
2740 lw->lw_next = l->lv_watch;
2741 l->lv_watch = lw;
2742}
2743
2744/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002745 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002746 * No warning when it isn't found...
2747 */
2748 static void
2749list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002750 list_T *l;
2751 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002752{
Bram Moolenaar33570922005-01-25 22:26:29 +00002753 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002754
2755 lwp = &l->lv_watch;
2756 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2757 {
2758 if (lw == lwrem)
2759 {
2760 *lwp = lw->lw_next;
2761 break;
2762 }
2763 lwp = &lw->lw_next;
2764 }
2765}
2766
2767/*
2768 * Just before removing an item from a list: advance watchers to the next
2769 * item.
2770 */
2771 static void
2772list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002773 list_T *l;
2774 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002775{
Bram Moolenaar33570922005-01-25 22:26:29 +00002776 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002777
2778 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2779 if (lw->lw_item == item)
2780 lw->lw_item = item->li_next;
2781}
2782
2783/*
2784 * Evaluate the expression used in a ":for var in expr" command.
2785 * "arg" points to "var".
2786 * Set "*errp" to TRUE for an error, FALSE otherwise;
2787 * Return a pointer that holds the info. Null when there is an error.
2788 */
2789 void *
2790eval_for_line(arg, errp, nextcmdp, skip)
2791 char_u *arg;
2792 int *errp;
2793 char_u **nextcmdp;
2794 int skip;
2795{
Bram Moolenaar33570922005-01-25 22:26:29 +00002796 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002797 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002798 typval_T tv;
2799 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002800
2801 *errp = TRUE; /* default: there is an error */
2802
Bram Moolenaar33570922005-01-25 22:26:29 +00002803 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002804 if (fi == NULL)
2805 return NULL;
2806
2807 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2808 if (expr == NULL)
2809 return fi;
2810
2811 expr = skipwhite(expr);
2812 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2813 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002814 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002815 return fi;
2816 }
2817
2818 if (skip)
2819 ++emsg_skip;
2820 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2821 {
2822 *errp = FALSE;
2823 if (!skip)
2824 {
2825 l = tv.vval.v_list;
2826 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002827 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002828 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002829 clear_tv(&tv);
2830 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002831 else
2832 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002833 /* No need to increment the refcount, it's already set for the
2834 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002835 fi->fi_list = l;
2836 list_add_watch(l, &fi->fi_lw);
2837 fi->fi_lw.lw_item = l->lv_first;
2838 }
2839 }
2840 }
2841 if (skip)
2842 --emsg_skip;
2843
2844 return fi;
2845}
2846
2847/*
2848 * Use the first item in a ":for" list. Advance to the next.
2849 * Assign the values to the variable (list). "arg" points to the first one.
2850 * Return TRUE when a valid item was found, FALSE when at end of list or
2851 * something wrong.
2852 */
2853 int
2854next_for_item(fi_void, arg)
2855 void *fi_void;
2856 char_u *arg;
2857{
Bram Moolenaar33570922005-01-25 22:26:29 +00002858 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002859 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002860 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002861
2862 item = fi->fi_lw.lw_item;
2863 if (item == NULL)
2864 result = FALSE;
2865 else
2866 {
2867 fi->fi_lw.lw_item = item->li_next;
2868 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2869 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2870 }
2871 return result;
2872}
2873
2874/*
2875 * Free the structure used to store info used by ":for".
2876 */
2877 void
2878free_for_info(fi_void)
2879 void *fi_void;
2880{
Bram Moolenaar33570922005-01-25 22:26:29 +00002881 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002882
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002883 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002884 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002885 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002886 list_unref(fi->fi_list);
2887 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002888 vim_free(fi);
2889}
2890
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2892
2893 void
2894set_context_for_expression(xp, arg, cmdidx)
2895 expand_T *xp;
2896 char_u *arg;
2897 cmdidx_T cmdidx;
2898{
2899 int got_eq = FALSE;
2900 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002901 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002903 if (cmdidx == CMD_let)
2904 {
2905 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002906 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002907 {
2908 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002909 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002910 {
2911 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00002912 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002913 if (vim_iswhite(*p))
2914 break;
2915 }
2916 return;
2917 }
2918 }
2919 else
2920 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2921 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 while ((xp->xp_pattern = vim_strpbrk(arg,
2923 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2924 {
2925 c = *xp->xp_pattern;
2926 if (c == '&')
2927 {
2928 c = xp->xp_pattern[1];
2929 if (c == '&')
2930 {
2931 ++xp->xp_pattern;
2932 xp->xp_context = cmdidx != CMD_let || got_eq
2933 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2934 }
2935 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002936 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002938 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2939 xp->xp_pattern += 2;
2940
2941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 }
2943 else if (c == '$')
2944 {
2945 /* environment variable */
2946 xp->xp_context = EXPAND_ENV_VARS;
2947 }
2948 else if (c == '=')
2949 {
2950 got_eq = TRUE;
2951 xp->xp_context = EXPAND_EXPRESSION;
2952 }
2953 else if (c == '<'
2954 && xp->xp_context == EXPAND_FUNCTIONS
2955 && vim_strchr(xp->xp_pattern, '(') == NULL)
2956 {
2957 /* Function name can start with "<SNR>" */
2958 break;
2959 }
2960 else if (cmdidx != CMD_let || got_eq)
2961 {
2962 if (c == '"') /* string */
2963 {
2964 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2965 if (c == '\\' && xp->xp_pattern[1] != NUL)
2966 ++xp->xp_pattern;
2967 xp->xp_context = EXPAND_NOTHING;
2968 }
2969 else if (c == '\'') /* literal string */
2970 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002971 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2973 /* skip */ ;
2974 xp->xp_context = EXPAND_NOTHING;
2975 }
2976 else if (c == '|')
2977 {
2978 if (xp->xp_pattern[1] == '|')
2979 {
2980 ++xp->xp_pattern;
2981 xp->xp_context = EXPAND_EXPRESSION;
2982 }
2983 else
2984 xp->xp_context = EXPAND_COMMANDS;
2985 }
2986 else
2987 xp->xp_context = EXPAND_EXPRESSION;
2988 }
2989 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002990 /* Doesn't look like something valid, expand as an expression
2991 * anyway. */
2992 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 arg = xp->xp_pattern;
2994 if (*arg != NUL)
2995 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2996 /* skip */ ;
2997 }
2998 xp->xp_pattern = arg;
2999}
3000
3001#endif /* FEAT_CMDL_COMPL */
3002
3003/*
3004 * ":1,25call func(arg1, arg2)" function call.
3005 */
3006 void
3007ex_call(eap)
3008 exarg_T *eap;
3009{
3010 char_u *arg = eap->arg;
3011 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003013 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003015 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 linenr_T lnum;
3017 int doesrange;
3018 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003019 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003021 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
3022 vim_free(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003023 if (tofree == NULL)
3024 return;
3025
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003026 /* Increase refcount on dictionary, it could get deleted when evaluating
3027 * the arguments. */
3028 if (fudi.fd_dict != NULL)
3029 ++fudi.fd_dict->dv_refcount;
3030
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003031 /* If it is the name of a variable of type VAR_FUNC use its contents. */
3032 len = STRLEN(tofree);
3033 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034
Bram Moolenaar532c7802005-01-27 14:44:31 +00003035 /* Skip white space to allow ":call func ()". Not good, but required for
3036 * backward compatibility. */
3037 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003038 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039
3040 if (*startarg != '(')
3041 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003042 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 goto end;
3044 }
3045
3046 /*
3047 * When skipping, evaluate the function once, to find the end of the
3048 * arguments.
3049 * When the function takes a range, this is discovered after the first
3050 * call, and the loop is broken.
3051 */
3052 if (eap->skip)
3053 {
3054 ++emsg_skip;
3055 lnum = eap->line2; /* do it once, also with an invalid range */
3056 }
3057 else
3058 lnum = eap->line1;
3059 for ( ; lnum <= eap->line2; ++lnum)
3060 {
3061 if (!eap->skip && eap->addr_count > 0)
3062 {
3063 curwin->w_cursor.lnum = lnum;
3064 curwin->w_cursor.col = 0;
3065 }
3066 arg = startarg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003067 if (get_func_tv(name, STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003068 eap->line1, eap->line2, &doesrange,
3069 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 {
3071 failed = TRUE;
3072 break;
3073 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003074 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 if (doesrange || eap->skip)
3076 break;
3077 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003078 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003079 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003080 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 if (aborting())
3082 break;
3083 }
3084 if (eap->skip)
3085 --emsg_skip;
3086
3087 if (!failed)
3088 {
3089 /* Check for trailing illegal characters and a following command. */
3090 if (!ends_excmd(*arg))
3091 {
3092 emsg_severe = TRUE;
3093 EMSG(_(e_trailing));
3094 }
3095 else
3096 eap->nextcmd = check_nextcmd(arg);
3097 }
3098
3099end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003100 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003101 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102}
3103
3104/*
3105 * ":unlet[!] var1 ... " command.
3106 */
3107 void
3108ex_unlet(eap)
3109 exarg_T *eap;
3110{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003111 ex_unletlock(eap, eap->arg, 0);
3112}
3113
3114/*
3115 * ":lockvar" and ":unlockvar" commands
3116 */
3117 void
3118ex_lockvar(eap)
3119 exarg_T *eap;
3120{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003122 int deep = 2;
3123
3124 if (eap->forceit)
3125 deep = -1;
3126 else if (vim_isdigit(*arg))
3127 {
3128 deep = getdigits(&arg);
3129 arg = skipwhite(arg);
3130 }
3131
3132 ex_unletlock(eap, arg, deep);
3133}
3134
3135/*
3136 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3137 */
3138 static void
3139ex_unletlock(eap, argstart, deep)
3140 exarg_T *eap;
3141 char_u *argstart;
3142 int deep;
3143{
3144 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003147 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148
3149 do
3150 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003151 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003152 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3153 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003154 if (lv.ll_name == NULL)
3155 error = TRUE; /* error but continue parsing */
3156 if (name_end == NULL || (!vim_iswhite(*name_end)
3157 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003159 if (name_end != NULL)
3160 {
3161 emsg_severe = TRUE;
3162 EMSG(_(e_trailing));
3163 }
3164 if (!(eap->skip || error))
3165 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 break;
3167 }
3168
3169 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003170 {
3171 if (eap->cmdidx == CMD_unlet)
3172 {
3173 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3174 error = TRUE;
3175 }
3176 else
3177 {
3178 if (do_lock_var(&lv, name_end, deep,
3179 eap->cmdidx == CMD_lockvar) == FAIL)
3180 error = TRUE;
3181 }
3182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003184 if (!eap->skip)
3185 clear_lval(&lv);
3186
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 arg = skipwhite(name_end);
3188 } while (!ends_excmd(*arg));
3189
3190 eap->nextcmd = check_nextcmd(arg);
3191}
3192
Bram Moolenaar8c711452005-01-14 21:53:12 +00003193 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003194do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003195 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003196 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003197 int forceit;
3198{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003199 int ret = OK;
3200 int cc;
3201
3202 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003203 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003204 cc = *name_end;
3205 *name_end = NUL;
3206
3207 /* Normal name or expanded name. */
3208 if (check_changedtick(lp->ll_name))
3209 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003210 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003211 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003212 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003213 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003214 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3215 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003216 else if (lp->ll_range)
3217 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003218 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003219
3220 /* Delete a range of List items. */
3221 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3222 {
3223 li = lp->ll_li->li_next;
3224 listitem_remove(lp->ll_list, lp->ll_li);
3225 lp->ll_li = li;
3226 ++lp->ll_n1;
3227 }
3228 }
3229 else
3230 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003231 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003232 /* unlet a List item. */
3233 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003234 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003235 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003236 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003237 }
3238
3239 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003240}
3241
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242/*
3243 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003244 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 */
3246 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003247do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003249 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250{
Bram Moolenaar33570922005-01-25 22:26:29 +00003251 hashtab_T *ht;
3252 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003253 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254
Bram Moolenaar33570922005-01-25 22:26:29 +00003255 ht = find_var_ht(name, &varname);
3256 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003258 hi = hash_find(ht, varname);
3259 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003260 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003261 if (var_check_ro(HI2DI(hi)->di_flags, name))
3262 return FAIL;
3263 delete_var(ht, hi);
3264 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003267 if (forceit)
3268 return OK;
3269 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 return FAIL;
3271}
3272
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003273/*
3274 * Lock or unlock variable indicated by "lp".
3275 * "deep" is the levels to go (-1 for unlimited);
3276 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3277 */
3278 static int
3279do_lock_var(lp, name_end, deep, lock)
3280 lval_T *lp;
3281 char_u *name_end;
3282 int deep;
3283 int lock;
3284{
3285 int ret = OK;
3286 int cc;
3287 dictitem_T *di;
3288
3289 if (deep == 0) /* nothing to do */
3290 return OK;
3291
3292 if (lp->ll_tv == NULL)
3293 {
3294 cc = *name_end;
3295 *name_end = NUL;
3296
3297 /* Normal name or expanded name. */
3298 if (check_changedtick(lp->ll_name))
3299 ret = FAIL;
3300 else
3301 {
3302 di = find_var(lp->ll_name, NULL);
3303 if (di == NULL)
3304 ret = FAIL;
3305 else
3306 {
3307 if (lock)
3308 di->di_flags |= DI_FLAGS_LOCK;
3309 else
3310 di->di_flags &= ~DI_FLAGS_LOCK;
3311 item_lock(&di->di_tv, deep, lock);
3312 }
3313 }
3314 *name_end = cc;
3315 }
3316 else if (lp->ll_range)
3317 {
3318 listitem_T *li = lp->ll_li;
3319
3320 /* (un)lock a range of List items. */
3321 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3322 {
3323 item_lock(&li->li_tv, deep, lock);
3324 li = li->li_next;
3325 ++lp->ll_n1;
3326 }
3327 }
3328 else if (lp->ll_list != NULL)
3329 /* (un)lock a List item. */
3330 item_lock(&lp->ll_li->li_tv, deep, lock);
3331 else
3332 /* un(lock) a Dictionary item. */
3333 item_lock(&lp->ll_di->di_tv, deep, lock);
3334
3335 return ret;
3336}
3337
3338/*
3339 * Lock or unlock an item. "deep" is nr of levels to go.
3340 */
3341 static void
3342item_lock(tv, deep, lock)
3343 typval_T *tv;
3344 int deep;
3345 int lock;
3346{
3347 static int recurse = 0;
3348 list_T *l;
3349 listitem_T *li;
3350 dict_T *d;
3351 hashitem_T *hi;
3352 int todo;
3353
3354 if (recurse >= DICT_MAXNEST)
3355 {
3356 EMSG(_("E743: variable nested too deep for (un)lock"));
3357 return;
3358 }
3359 if (deep == 0)
3360 return;
3361 ++recurse;
3362
3363 /* lock/unlock the item itself */
3364 if (lock)
3365 tv->v_lock |= VAR_LOCKED;
3366 else
3367 tv->v_lock &= ~VAR_LOCKED;
3368
3369 switch (tv->v_type)
3370 {
3371 case VAR_LIST:
3372 if ((l = tv->vval.v_list) != NULL)
3373 {
3374 if (lock)
3375 l->lv_lock |= VAR_LOCKED;
3376 else
3377 l->lv_lock &= ~VAR_LOCKED;
3378 if (deep < 0 || deep > 1)
3379 /* recursive: lock/unlock the items the List contains */
3380 for (li = l->lv_first; li != NULL; li = li->li_next)
3381 item_lock(&li->li_tv, deep - 1, lock);
3382 }
3383 break;
3384 case VAR_DICT:
3385 if ((d = tv->vval.v_dict) != NULL)
3386 {
3387 if (lock)
3388 d->dv_lock |= VAR_LOCKED;
3389 else
3390 d->dv_lock &= ~VAR_LOCKED;
3391 if (deep < 0 || deep > 1)
3392 {
3393 /* recursive: lock/unlock the items the List contains */
3394 todo = d->dv_hashtab.ht_used;
3395 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3396 {
3397 if (!HASHITEM_EMPTY(hi))
3398 {
3399 --todo;
3400 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3401 }
3402 }
3403 }
3404 }
3405 }
3406 --recurse;
3407}
3408
Bram Moolenaara40058a2005-07-11 22:42:07 +00003409/*
3410 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3411 * it refers to a List or Dictionary that is locked.
3412 */
3413 static int
3414tv_islocked(tv)
3415 typval_T *tv;
3416{
3417 return (tv->v_lock & VAR_LOCKED)
3418 || (tv->v_type == VAR_LIST
3419 && tv->vval.v_list != NULL
3420 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3421 || (tv->v_type == VAR_DICT
3422 && tv->vval.v_dict != NULL
3423 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3424}
3425
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3427/*
3428 * Delete all "menutrans_" variables.
3429 */
3430 void
3431del_menutrans_vars()
3432{
Bram Moolenaar33570922005-01-25 22:26:29 +00003433 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003434 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435
Bram Moolenaar33570922005-01-25 22:26:29 +00003436 hash_lock(&globvarht);
3437 todo = globvarht.ht_used;
3438 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003439 {
3440 if (!HASHITEM_EMPTY(hi))
3441 {
3442 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003443 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3444 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003445 }
3446 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003447 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448}
3449#endif
3450
3451#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3452
3453/*
3454 * Local string buffer for the next two functions to store a variable name
3455 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3456 * get_user_var_name().
3457 */
3458
3459static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3460
3461static char_u *varnamebuf = NULL;
3462static int varnamebuflen = 0;
3463
3464/*
3465 * Function to concatenate a prefix and a variable name.
3466 */
3467 static char_u *
3468cat_prefix_varname(prefix, name)
3469 int prefix;
3470 char_u *name;
3471{
3472 int len;
3473
3474 len = (int)STRLEN(name) + 3;
3475 if (len > varnamebuflen)
3476 {
3477 vim_free(varnamebuf);
3478 len += 10; /* some additional space */
3479 varnamebuf = alloc(len);
3480 if (varnamebuf == NULL)
3481 {
3482 varnamebuflen = 0;
3483 return NULL;
3484 }
3485 varnamebuflen = len;
3486 }
3487 *varnamebuf = prefix;
3488 varnamebuf[1] = ':';
3489 STRCPY(varnamebuf + 2, name);
3490 return varnamebuf;
3491}
3492
3493/*
3494 * Function given to ExpandGeneric() to obtain the list of user defined
3495 * (global/buffer/window/built-in) variable names.
3496 */
3497/*ARGSUSED*/
3498 char_u *
3499get_user_var_name(xp, idx)
3500 expand_T *xp;
3501 int idx;
3502{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003503 static long_u gdone;
3504 static long_u bdone;
3505 static long_u wdone;
3506 static int vidx;
3507 static hashitem_T *hi;
3508 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509
3510 if (idx == 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00003511 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00003512
3513 /* Global variables */
3514 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003516 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003517 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003518 else
3519 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003520 while (HASHITEM_EMPTY(hi))
3521 ++hi;
3522 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3523 return cat_prefix_varname('g', hi->hi_key);
3524 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003526
3527 /* b: variables */
3528 ht = &curbuf->b_vars.dv_hashtab;
3529 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003531 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003532 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003533 else
3534 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003535 while (HASHITEM_EMPTY(hi))
3536 ++hi;
3537 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003539 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003541 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 return (char_u *)"b:changedtick";
3543 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003544
3545 /* w: variables */
3546 ht = &curwin->w_vars.dv_hashtab;
3547 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003549 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003550 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003551 else
3552 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003553 while (HASHITEM_EMPTY(hi))
3554 ++hi;
3555 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003557
3558 /* v: variables */
3559 if (vidx < VV_LEN)
3560 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561
3562 vim_free(varnamebuf);
3563 varnamebuf = NULL;
3564 varnamebuflen = 0;
3565 return NULL;
3566}
3567
3568#endif /* FEAT_CMDL_COMPL */
3569
3570/*
3571 * types for expressions.
3572 */
3573typedef enum
3574{
3575 TYPE_UNKNOWN = 0
3576 , TYPE_EQUAL /* == */
3577 , TYPE_NEQUAL /* != */
3578 , TYPE_GREATER /* > */
3579 , TYPE_GEQUAL /* >= */
3580 , TYPE_SMALLER /* < */
3581 , TYPE_SEQUAL /* <= */
3582 , TYPE_MATCH /* =~ */
3583 , TYPE_NOMATCH /* !~ */
3584} exptype_T;
3585
3586/*
3587 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003588 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3590 */
3591
3592/*
3593 * Handle zero level expression.
3594 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003595 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003596 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 * Return OK or FAIL.
3598 */
3599 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003600eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003602 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 char_u **nextcmd;
3604 int evaluate;
3605{
3606 int ret;
3607 char_u *p;
3608
3609 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003610 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 if (ret == FAIL || !ends_excmd(*p))
3612 {
3613 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003614 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 /*
3616 * Report the invalid expression unless the expression evaluation has
3617 * been cancelled due to an aborting error, an interrupt, or an
3618 * exception.
3619 */
3620 if (!aborting())
3621 EMSG2(_(e_invexpr2), arg);
3622 ret = FAIL;
3623 }
3624 if (nextcmd != NULL)
3625 *nextcmd = check_nextcmd(p);
3626
3627 return ret;
3628}
3629
3630/*
3631 * Handle top level expression:
3632 * expr1 ? expr0 : expr0
3633 *
3634 * "arg" must point to the first non-white of the expression.
3635 * "arg" is advanced to the next non-white after the recognized expression.
3636 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003637 * Note: "rettv.v_lock" is not set.
3638 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 * Return OK or FAIL.
3640 */
3641 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003642eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003644 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 int evaluate;
3646{
3647 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003648 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649
3650 /*
3651 * Get the first variable.
3652 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003653 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 return FAIL;
3655
3656 if ((*arg)[0] == '?')
3657 {
3658 result = FALSE;
3659 if (evaluate)
3660 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003661 int error = FALSE;
3662
3663 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003665 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003666 if (error)
3667 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 }
3669
3670 /*
3671 * Get the second variable.
3672 */
3673 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003674 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 return FAIL;
3676
3677 /*
3678 * Check for the ":".
3679 */
3680 if ((*arg)[0] != ':')
3681 {
3682 EMSG(_("E109: Missing ':' after '?'"));
3683 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003684 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 return FAIL;
3686 }
3687
3688 /*
3689 * Get the third variable.
3690 */
3691 *arg = skipwhite(*arg + 1);
3692 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3693 {
3694 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003695 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 return FAIL;
3697 }
3698 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003699 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 }
3701
3702 return OK;
3703}
3704
3705/*
3706 * Handle first level expression:
3707 * expr2 || expr2 || expr2 logical OR
3708 *
3709 * "arg" must point to the first non-white of the expression.
3710 * "arg" is advanced to the next non-white after the recognized expression.
3711 *
3712 * Return OK or FAIL.
3713 */
3714 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003715eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003717 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 int evaluate;
3719{
Bram Moolenaar33570922005-01-25 22:26:29 +00003720 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 long result;
3722 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003723 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724
3725 /*
3726 * Get the first variable.
3727 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003728 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 return FAIL;
3730
3731 /*
3732 * Repeat until there is no following "||".
3733 */
3734 first = TRUE;
3735 result = FALSE;
3736 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3737 {
3738 if (evaluate && first)
3739 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003740 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003742 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003743 if (error)
3744 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 first = FALSE;
3746 }
3747
3748 /*
3749 * Get the second variable.
3750 */
3751 *arg = skipwhite(*arg + 2);
3752 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3753 return FAIL;
3754
3755 /*
3756 * Compute the result.
3757 */
3758 if (evaluate && !result)
3759 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003760 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003762 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003763 if (error)
3764 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 }
3766 if (evaluate)
3767 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003768 rettv->v_type = VAR_NUMBER;
3769 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 }
3771 }
3772
3773 return OK;
3774}
3775
3776/*
3777 * Handle second level expression:
3778 * expr3 && expr3 && expr3 logical AND
3779 *
3780 * "arg" must point to the first non-white of the expression.
3781 * "arg" is advanced to the next non-white after the recognized expression.
3782 *
3783 * Return OK or FAIL.
3784 */
3785 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003786eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003788 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 int evaluate;
3790{
Bram Moolenaar33570922005-01-25 22:26:29 +00003791 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 long result;
3793 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003794 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795
3796 /*
3797 * Get the first variable.
3798 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003799 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 return FAIL;
3801
3802 /*
3803 * Repeat until there is no following "&&".
3804 */
3805 first = TRUE;
3806 result = TRUE;
3807 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3808 {
3809 if (evaluate && first)
3810 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003811 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003813 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003814 if (error)
3815 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 first = FALSE;
3817 }
3818
3819 /*
3820 * Get the second variable.
3821 */
3822 *arg = skipwhite(*arg + 2);
3823 if (eval4(arg, &var2, evaluate && result) == FAIL)
3824 return FAIL;
3825
3826 /*
3827 * Compute the result.
3828 */
3829 if (evaluate && result)
3830 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003831 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003833 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003834 if (error)
3835 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 }
3837 if (evaluate)
3838 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003839 rettv->v_type = VAR_NUMBER;
3840 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 }
3842 }
3843
3844 return OK;
3845}
3846
3847/*
3848 * Handle third level expression:
3849 * var1 == var2
3850 * var1 =~ var2
3851 * var1 != var2
3852 * var1 !~ var2
3853 * var1 > var2
3854 * var1 >= var2
3855 * var1 < var2
3856 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003857 * var1 is var2
3858 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 *
3860 * "arg" must point to the first non-white of the expression.
3861 * "arg" is advanced to the next non-white after the recognized expression.
3862 *
3863 * Return OK or FAIL.
3864 */
3865 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003866eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003868 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 int evaluate;
3870{
Bram Moolenaar33570922005-01-25 22:26:29 +00003871 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 char_u *p;
3873 int i;
3874 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003875 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 int len = 2;
3877 long n1, n2;
3878 char_u *s1, *s2;
3879 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3880 regmatch_T regmatch;
3881 int ic;
3882 char_u *save_cpo;
3883
3884 /*
3885 * Get the first variable.
3886 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003887 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 return FAIL;
3889
3890 p = *arg;
3891 switch (p[0])
3892 {
3893 case '=': if (p[1] == '=')
3894 type = TYPE_EQUAL;
3895 else if (p[1] == '~')
3896 type = TYPE_MATCH;
3897 break;
3898 case '!': if (p[1] == '=')
3899 type = TYPE_NEQUAL;
3900 else if (p[1] == '~')
3901 type = TYPE_NOMATCH;
3902 break;
3903 case '>': if (p[1] != '=')
3904 {
3905 type = TYPE_GREATER;
3906 len = 1;
3907 }
3908 else
3909 type = TYPE_GEQUAL;
3910 break;
3911 case '<': if (p[1] != '=')
3912 {
3913 type = TYPE_SMALLER;
3914 len = 1;
3915 }
3916 else
3917 type = TYPE_SEQUAL;
3918 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003919 case 'i': if (p[1] == 's')
3920 {
3921 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3922 len = 5;
3923 if (!vim_isIDc(p[len]))
3924 {
3925 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3926 type_is = TRUE;
3927 }
3928 }
3929 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 }
3931
3932 /*
3933 * If there is a comparitive operator, use it.
3934 */
3935 if (type != TYPE_UNKNOWN)
3936 {
3937 /* extra question mark appended: ignore case */
3938 if (p[len] == '?')
3939 {
3940 ic = TRUE;
3941 ++len;
3942 }
3943 /* extra '#' appended: match case */
3944 else if (p[len] == '#')
3945 {
3946 ic = FALSE;
3947 ++len;
3948 }
3949 /* nothing appened: use 'ignorecase' */
3950 else
3951 ic = p_ic;
3952
3953 /*
3954 * Get the second variable.
3955 */
3956 *arg = skipwhite(p + len);
3957 if (eval5(arg, &var2, evaluate) == FAIL)
3958 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003959 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 return FAIL;
3961 }
3962
3963 if (evaluate)
3964 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003965 if (type_is && rettv->v_type != var2.v_type)
3966 {
3967 /* For "is" a different type always means FALSE, for "notis"
3968 * it means TRUE. */
3969 n1 = (type == TYPE_NEQUAL);
3970 }
3971 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
3972 {
3973 if (type_is)
3974 {
3975 n1 = (rettv->v_type == var2.v_type
3976 && rettv->vval.v_list == var2.vval.v_list);
3977 if (type == TYPE_NEQUAL)
3978 n1 = !n1;
3979 }
3980 else if (rettv->v_type != var2.v_type
3981 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3982 {
3983 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003984 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003985 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003986 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003987 clear_tv(rettv);
3988 clear_tv(&var2);
3989 return FAIL;
3990 }
3991 else
3992 {
3993 /* Compare two Lists for being equal or unequal. */
3994 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
3995 if (type == TYPE_NEQUAL)
3996 n1 = !n1;
3997 }
3998 }
3999
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004000 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4001 {
4002 if (type_is)
4003 {
4004 n1 = (rettv->v_type == var2.v_type
4005 && rettv->vval.v_dict == var2.vval.v_dict);
4006 if (type == TYPE_NEQUAL)
4007 n1 = !n1;
4008 }
4009 else if (rettv->v_type != var2.v_type
4010 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4011 {
4012 if (rettv->v_type != var2.v_type)
4013 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4014 else
4015 EMSG(_("E736: Invalid operation for Dictionary"));
4016 clear_tv(rettv);
4017 clear_tv(&var2);
4018 return FAIL;
4019 }
4020 else
4021 {
4022 /* Compare two Dictionaries for being equal or unequal. */
4023 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4024 if (type == TYPE_NEQUAL)
4025 n1 = !n1;
4026 }
4027 }
4028
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004029 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4030 {
4031 if (rettv->v_type != var2.v_type
4032 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4033 {
4034 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004035 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004036 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004037 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004038 clear_tv(rettv);
4039 clear_tv(&var2);
4040 return FAIL;
4041 }
4042 else
4043 {
4044 /* Compare two Funcrefs for being equal or unequal. */
4045 if (rettv->vval.v_string == NULL
4046 || var2.vval.v_string == NULL)
4047 n1 = FALSE;
4048 else
4049 n1 = STRCMP(rettv->vval.v_string,
4050 var2.vval.v_string) == 0;
4051 if (type == TYPE_NEQUAL)
4052 n1 = !n1;
4053 }
4054 }
4055
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 /*
4057 * If one of the two variables is a number, compare as a number.
4058 * When using "=~" or "!~", always compare as string.
4059 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004060 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4062 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004063 n1 = get_tv_number(rettv);
4064 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 switch (type)
4066 {
4067 case TYPE_EQUAL: n1 = (n1 == n2); break;
4068 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4069 case TYPE_GREATER: n1 = (n1 > n2); break;
4070 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4071 case TYPE_SMALLER: n1 = (n1 < n2); break;
4072 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4073 case TYPE_UNKNOWN:
4074 case TYPE_MATCH:
4075 case TYPE_NOMATCH: break; /* avoid gcc warning */
4076 }
4077 }
4078 else
4079 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004080 s1 = get_tv_string_buf(rettv, buf1);
4081 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4083 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4084 else
4085 i = 0;
4086 n1 = FALSE;
4087 switch (type)
4088 {
4089 case TYPE_EQUAL: n1 = (i == 0); break;
4090 case TYPE_NEQUAL: n1 = (i != 0); break;
4091 case TYPE_GREATER: n1 = (i > 0); break;
4092 case TYPE_GEQUAL: n1 = (i >= 0); break;
4093 case TYPE_SMALLER: n1 = (i < 0); break;
4094 case TYPE_SEQUAL: n1 = (i <= 0); break;
4095
4096 case TYPE_MATCH:
4097 case TYPE_NOMATCH:
4098 /* avoid 'l' flag in 'cpoptions' */
4099 save_cpo = p_cpo;
4100 p_cpo = (char_u *)"";
4101 regmatch.regprog = vim_regcomp(s2,
4102 RE_MAGIC + RE_STRING);
4103 regmatch.rm_ic = ic;
4104 if (regmatch.regprog != NULL)
4105 {
4106 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4107 vim_free(regmatch.regprog);
4108 if (type == TYPE_NOMATCH)
4109 n1 = !n1;
4110 }
4111 p_cpo = save_cpo;
4112 break;
4113
4114 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4115 }
4116 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004117 clear_tv(rettv);
4118 clear_tv(&var2);
4119 rettv->v_type = VAR_NUMBER;
4120 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 }
4122 }
4123
4124 return OK;
4125}
4126
4127/*
4128 * Handle fourth level expression:
4129 * + number addition
4130 * - number subtraction
4131 * . string concatenation
4132 *
4133 * "arg" must point to the first non-white of the expression.
4134 * "arg" is advanced to the next non-white after the recognized expression.
4135 *
4136 * Return OK or FAIL.
4137 */
4138 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004139eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004141 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 int evaluate;
4143{
Bram Moolenaar33570922005-01-25 22:26:29 +00004144 typval_T var2;
4145 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 int op;
4147 long n1, n2;
4148 char_u *s1, *s2;
4149 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4150 char_u *p;
4151
4152 /*
4153 * Get the first variable.
4154 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004155 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 return FAIL;
4157
4158 /*
4159 * Repeat computing, until no '+', '-' or '.' is following.
4160 */
4161 for (;;)
4162 {
4163 op = **arg;
4164 if (op != '+' && op != '-' && op != '.')
4165 break;
4166
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004167 if (op != '+' || rettv->v_type != VAR_LIST)
4168 {
4169 /* For "list + ...", an illegal use of the first operand as
4170 * a number cannot be determined before evaluating the 2nd
4171 * operand: if this is also a list, all is ok.
4172 * For "something . ...", "something - ..." or "non-list + ...",
4173 * we know that the first operand needs to be a string or number
4174 * without evaluating the 2nd operand. So check before to avoid
4175 * side effects after an error. */
4176 if (evaluate && get_tv_string_chk(rettv) == NULL)
4177 {
4178 clear_tv(rettv);
4179 return FAIL;
4180 }
4181 }
4182
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 /*
4184 * Get the second variable.
4185 */
4186 *arg = skipwhite(*arg + 1);
4187 if (eval6(arg, &var2, evaluate) == FAIL)
4188 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004189 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 return FAIL;
4191 }
4192
4193 if (evaluate)
4194 {
4195 /*
4196 * Compute the result.
4197 */
4198 if (op == '.')
4199 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004200 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4201 s2 = get_tv_string_buf_chk(&var2, buf2);
4202 if (s2 == NULL) /* type error ? */
4203 {
4204 clear_tv(rettv);
4205 clear_tv(&var2);
4206 return FAIL;
4207 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004208 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004209 clear_tv(rettv);
4210 rettv->v_type = VAR_STRING;
4211 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004213 else if (op == '+' && rettv->v_type == VAR_LIST
4214 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004215 {
4216 /* concatenate Lists */
4217 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4218 &var3) == FAIL)
4219 {
4220 clear_tv(rettv);
4221 clear_tv(&var2);
4222 return FAIL;
4223 }
4224 clear_tv(rettv);
4225 *rettv = var3;
4226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 else
4228 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004229 int error = FALSE;
4230
4231 n1 = get_tv_number_chk(rettv, &error);
4232 if (error)
4233 {
4234 /* This can only happen for "list + non-list".
4235 * For "non-list + ..." or "something - ...", we returned
4236 * before evaluating the 2nd operand. */
4237 clear_tv(rettv);
4238 return FAIL;
4239 }
4240 n2 = get_tv_number_chk(&var2, &error);
4241 if (error)
4242 {
4243 clear_tv(rettv);
4244 clear_tv(&var2);
4245 return FAIL;
4246 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004247 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 if (op == '+')
4249 n1 = n1 + n2;
4250 else
4251 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004252 rettv->v_type = VAR_NUMBER;
4253 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004255 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 }
4257 }
4258 return OK;
4259}
4260
4261/*
4262 * Handle fifth level expression:
4263 * * number multiplication
4264 * / number division
4265 * % number modulo
4266 *
4267 * "arg" must point to the first non-white of the expression.
4268 * "arg" is advanced to the next non-white after the recognized expression.
4269 *
4270 * Return OK or FAIL.
4271 */
4272 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004273eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004275 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 int evaluate;
4277{
Bram Moolenaar33570922005-01-25 22:26:29 +00004278 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 int op;
4280 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004281 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282
4283 /*
4284 * Get the first variable.
4285 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004286 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 return FAIL;
4288
4289 /*
4290 * Repeat computing, until no '*', '/' or '%' is following.
4291 */
4292 for (;;)
4293 {
4294 op = **arg;
4295 if (op != '*' && op != '/' && op != '%')
4296 break;
4297
4298 if (evaluate)
4299 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004300 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004301 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004302 if (error)
4303 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 }
4305 else
4306 n1 = 0;
4307
4308 /*
4309 * Get the second variable.
4310 */
4311 *arg = skipwhite(*arg + 1);
4312 if (eval7(arg, &var2, evaluate) == FAIL)
4313 return FAIL;
4314
4315 if (evaluate)
4316 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004317 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004318 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004319 if (error)
4320 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321
4322 /*
4323 * Compute the result.
4324 */
4325 if (op == '*')
4326 n1 = n1 * n2;
4327 else if (op == '/')
4328 {
4329 if (n2 == 0) /* give an error message? */
4330 n1 = 0x7fffffffL;
4331 else
4332 n1 = n1 / n2;
4333 }
4334 else
4335 {
4336 if (n2 == 0) /* give an error message? */
4337 n1 = 0;
4338 else
4339 n1 = n1 % n2;
4340 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004341 rettv->v_type = VAR_NUMBER;
4342 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 }
4344 }
4345
4346 return OK;
4347}
4348
4349/*
4350 * Handle sixth level expression:
4351 * number number constant
4352 * "string" string contstant
4353 * 'string' literal string contstant
4354 * &option-name option value
4355 * @r register contents
4356 * identifier variable value
4357 * function() function call
4358 * $VAR environment variable
4359 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004360 * [expr, expr] List
4361 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 *
4363 * Also handle:
4364 * ! in front logical NOT
4365 * - in front unary minus
4366 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004367 * trailing [] subscript in String or List
4368 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 *
4370 * "arg" must point to the first non-white of the expression.
4371 * "arg" is advanced to the next non-white after the recognized expression.
4372 *
4373 * Return OK or FAIL.
4374 */
4375 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004376eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004378 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 int evaluate;
4380{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 long n;
4382 int len;
4383 char_u *s;
4384 int val;
4385 char_u *start_leader, *end_leader;
4386 int ret = OK;
4387 char_u *alias;
4388
4389 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004390 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004391 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004393 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394
4395 /*
4396 * Skip '!' and '-' characters. They are handled later.
4397 */
4398 start_leader = *arg;
4399 while (**arg == '!' || **arg == '-' || **arg == '+')
4400 *arg = skipwhite(*arg + 1);
4401 end_leader = *arg;
4402
4403 switch (**arg)
4404 {
4405 /*
4406 * Number constant.
4407 */
4408 case '0':
4409 case '1':
4410 case '2':
4411 case '3':
4412 case '4':
4413 case '5':
4414 case '6':
4415 case '7':
4416 case '8':
4417 case '9':
4418 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4419 *arg += len;
4420 if (evaluate)
4421 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004422 rettv->v_type = VAR_NUMBER;
4423 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 }
4425 break;
4426
4427 /*
4428 * String constant: "string".
4429 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004430 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 break;
4432
4433 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004434 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004436 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004437 break;
4438
4439 /*
4440 * List: [expr, expr]
4441 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004442 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 break;
4444
4445 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004446 * Dictionary: {key: val, key: val}
4447 */
4448 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4449 break;
4450
4451 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004452 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004454 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 break;
4456
4457 /*
4458 * Environment variable: $VAR.
4459 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004460 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 break;
4462
4463 /*
4464 * Register contents: @r.
4465 */
4466 case '@': ++*arg;
4467 if (evaluate)
4468 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004469 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004470 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 }
4472 if (**arg != NUL)
4473 ++*arg;
4474 break;
4475
4476 /*
4477 * nested expression: (expression).
4478 */
4479 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004480 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 if (**arg == ')')
4482 ++*arg;
4483 else if (ret == OK)
4484 {
4485 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004486 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 ret = FAIL;
4488 }
4489 break;
4490
Bram Moolenaar8c711452005-01-14 21:53:12 +00004491 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 break;
4493 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004494
4495 if (ret == NOTDONE)
4496 {
4497 /*
4498 * Must be a variable or function name.
4499 * Can also be a curly-braces kind of name: {expr}.
4500 */
4501 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004502 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004503 if (alias != NULL)
4504 s = alias;
4505
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004506 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004507 ret = FAIL;
4508 else
4509 {
4510 if (**arg == '(') /* recursive! */
4511 {
4512 /* If "s" is the name of a variable of type VAR_FUNC
4513 * use its contents. */
4514 s = deref_func_name(s, &len);
4515
4516 /* Invoke the function. */
4517 ret = get_func_tv(s, len, rettv, arg,
4518 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004519 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004520 /* Stop the expression evaluation when immediately
4521 * aborting on error, or when an interrupt occurred or
4522 * an exception was thrown but not caught. */
4523 if (aborting())
4524 {
4525 if (ret == OK)
4526 clear_tv(rettv);
4527 ret = FAIL;
4528 }
4529 }
4530 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004531 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004532 else
4533 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004534 }
4535
4536 if (alias != NULL)
4537 vim_free(alias);
4538 }
4539
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 *arg = skipwhite(*arg);
4541
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004542 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4543 * expr(expr). */
4544 if (ret == OK)
4545 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546
4547 /*
4548 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4549 */
4550 if (ret == OK && evaluate && end_leader > start_leader)
4551 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004552 int error = FALSE;
4553
4554 val = get_tv_number_chk(rettv, &error);
4555 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004557 clear_tv(rettv);
4558 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004560 else
4561 {
4562 while (end_leader > start_leader)
4563 {
4564 --end_leader;
4565 if (*end_leader == '!')
4566 val = !val;
4567 else if (*end_leader == '-')
4568 val = -val;
4569 }
4570 clear_tv(rettv);
4571 rettv->v_type = VAR_NUMBER;
4572 rettv->vval.v_number = val;
4573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 }
4575
4576 return ret;
4577}
4578
4579/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004580 * Evaluate an "[expr]" or "[expr:expr]" index.
4581 * "*arg" points to the '['.
4582 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4583 */
4584 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004585eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004586 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004587 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004588 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004589 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004590{
4591 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004592 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004593 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004594 long len = -1;
4595 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004596 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004597 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004598
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004599 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004600 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004601 if (verbose)
4602 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004603 return FAIL;
4604 }
4605
Bram Moolenaar8c711452005-01-14 21:53:12 +00004606 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004607 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004608 /*
4609 * dict.name
4610 */
4611 key = *arg + 1;
4612 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4613 ;
4614 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004615 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004616 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004617 }
4618 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004619 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004620 /*
4621 * something[idx]
4622 *
4623 * Get the (first) variable from inside the [].
4624 */
4625 *arg = skipwhite(*arg + 1);
4626 if (**arg == ':')
4627 empty1 = TRUE;
4628 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4629 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004630 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4631 {
4632 /* not a number or string */
4633 clear_tv(&var1);
4634 return FAIL;
4635 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004636
4637 /*
4638 * Get the second variable from inside the [:].
4639 */
4640 if (**arg == ':')
4641 {
4642 range = TRUE;
4643 *arg = skipwhite(*arg + 1);
4644 if (**arg == ']')
4645 empty2 = TRUE;
4646 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4647 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004648 if (!empty1)
4649 clear_tv(&var1);
4650 return FAIL;
4651 }
4652 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4653 {
4654 /* not a number or string */
4655 if (!empty1)
4656 clear_tv(&var1);
4657 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004658 return FAIL;
4659 }
4660 }
4661
4662 /* Check for the ']'. */
4663 if (**arg != ']')
4664 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004665 if (verbose)
4666 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004667 clear_tv(&var1);
4668 if (range)
4669 clear_tv(&var2);
4670 return FAIL;
4671 }
4672 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004673 }
4674
4675 if (evaluate)
4676 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004677 n1 = 0;
4678 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004679 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004680 n1 = get_tv_number(&var1);
4681 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004682 }
4683 if (range)
4684 {
4685 if (empty2)
4686 n2 = -1;
4687 else
4688 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004689 n2 = get_tv_number(&var2);
4690 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004691 }
4692 }
4693
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004694 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004695 {
4696 case VAR_NUMBER:
4697 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004698 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004699 len = (long)STRLEN(s);
4700 if (range)
4701 {
4702 /* The resulting variable is a substring. If the indexes
4703 * are out of range the result is empty. */
4704 if (n1 < 0)
4705 {
4706 n1 = len + n1;
4707 if (n1 < 0)
4708 n1 = 0;
4709 }
4710 if (n2 < 0)
4711 n2 = len + n2;
4712 else if (n2 >= len)
4713 n2 = len;
4714 if (n1 >= len || n2 < 0 || n1 > n2)
4715 s = NULL;
4716 else
4717 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4718 }
4719 else
4720 {
4721 /* The resulting variable is a string of a single
4722 * character. If the index is too big or negative the
4723 * result is empty. */
4724 if (n1 >= len || n1 < 0)
4725 s = NULL;
4726 else
4727 s = vim_strnsave(s + n1, 1);
4728 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004729 clear_tv(rettv);
4730 rettv->v_type = VAR_STRING;
4731 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004732 break;
4733
4734 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004735 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004736 if (n1 < 0)
4737 n1 = len + n1;
4738 if (!empty1 && (n1 < 0 || n1 >= len))
4739 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004740 if (verbose)
4741 EMSGN(_(e_listidx), n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004742 return FAIL;
4743 }
4744 if (range)
4745 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004746 list_T *l;
4747 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004748
4749 if (n2 < 0)
4750 n2 = len + n2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004751 if (!empty2 && (n2 < 0 || n2 >= len || n2 + 1 < n1))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004752 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004753 if (verbose)
4754 EMSGN(_(e_listidx), n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004755 return FAIL;
4756 }
4757 l = list_alloc();
4758 if (l == NULL)
4759 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004760 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004761 n1 <= n2; ++n1)
4762 {
4763 if (list_append_tv(l, &item->li_tv) == FAIL)
4764 {
4765 list_free(l);
4766 return FAIL;
4767 }
4768 item = item->li_next;
4769 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004770 clear_tv(rettv);
4771 rettv->v_type = VAR_LIST;
4772 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004773 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004774 }
4775 else
4776 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004777 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv,
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004778 &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004779 clear_tv(rettv);
4780 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004781 }
4782 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004783
4784 case VAR_DICT:
4785 if (range)
4786 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004787 if (verbose)
4788 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004789 if (len == -1)
4790 clear_tv(&var1);
4791 return FAIL;
4792 }
4793 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004794 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004795
4796 if (len == -1)
4797 {
4798 key = get_tv_string(&var1);
4799 if (*key == NUL)
4800 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004801 if (verbose)
4802 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004803 clear_tv(&var1);
4804 return FAIL;
4805 }
4806 }
4807
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004808 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004809
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004810 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004811 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004812 if (len == -1)
4813 clear_tv(&var1);
4814 if (item == NULL)
4815 return FAIL;
4816
4817 copy_tv(&item->di_tv, &var1);
4818 clear_tv(rettv);
4819 *rettv = var1;
4820 }
4821 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004822 }
4823 }
4824
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004825 return OK;
4826}
4827
4828/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 * Get an option value.
4830 * "arg" points to the '&' or '+' before the option name.
4831 * "arg" is advanced to character after the option name.
4832 * Return OK or FAIL.
4833 */
4834 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004835get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004837 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 int evaluate;
4839{
4840 char_u *option_end;
4841 long numval;
4842 char_u *stringval;
4843 int opt_type;
4844 int c;
4845 int working = (**arg == '+'); /* has("+option") */
4846 int ret = OK;
4847 int opt_flags;
4848
4849 /*
4850 * Isolate the option name and find its value.
4851 */
4852 option_end = find_option_end(arg, &opt_flags);
4853 if (option_end == NULL)
4854 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004855 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 EMSG2(_("E112: Option name missing: %s"), *arg);
4857 return FAIL;
4858 }
4859
4860 if (!evaluate)
4861 {
4862 *arg = option_end;
4863 return OK;
4864 }
4865
4866 c = *option_end;
4867 *option_end = NUL;
4868 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004869 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870
4871 if (opt_type == -3) /* invalid name */
4872 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004873 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 EMSG2(_("E113: Unknown option: %s"), *arg);
4875 ret = FAIL;
4876 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004877 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 {
4879 if (opt_type == -2) /* hidden string option */
4880 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004881 rettv->v_type = VAR_STRING;
4882 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 }
4884 else if (opt_type == -1) /* hidden number option */
4885 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004886 rettv->v_type = VAR_NUMBER;
4887 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 }
4889 else if (opt_type == 1) /* number option */
4890 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004891 rettv->v_type = VAR_NUMBER;
4892 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 }
4894 else /* string option */
4895 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004896 rettv->v_type = VAR_STRING;
4897 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 }
4899 }
4900 else if (working && (opt_type == -2 || opt_type == -1))
4901 ret = FAIL;
4902
4903 *option_end = c; /* put back for error messages */
4904 *arg = option_end;
4905
4906 return ret;
4907}
4908
4909/*
4910 * Allocate a variable for a string constant.
4911 * Return OK or FAIL.
4912 */
4913 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004914get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004916 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 int evaluate;
4918{
4919 char_u *p;
4920 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 int extra = 0;
4922
4923 /*
4924 * Find the end of the string, skipping backslashed characters.
4925 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004926 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 {
4928 if (*p == '\\' && p[1] != NUL)
4929 {
4930 ++p;
4931 /* A "\<x>" form occupies at least 4 characters, and produces up
4932 * to 6 characters: reserve space for 2 extra */
4933 if (*p == '<')
4934 extra += 2;
4935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 }
4937
4938 if (*p != '"')
4939 {
4940 EMSG2(_("E114: Missing quote: %s"), *arg);
4941 return FAIL;
4942 }
4943
4944 /* If only parsing, set *arg and return here */
4945 if (!evaluate)
4946 {
4947 *arg = p + 1;
4948 return OK;
4949 }
4950
4951 /*
4952 * Copy the string into allocated memory, handling backslashed
4953 * characters.
4954 */
4955 name = alloc((unsigned)(p - *arg + extra));
4956 if (name == NULL)
4957 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004958 rettv->v_type = VAR_STRING;
4959 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960
Bram Moolenaar8c711452005-01-14 21:53:12 +00004961 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 {
4963 if (*p == '\\')
4964 {
4965 switch (*++p)
4966 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004967 case 'b': *name++ = BS; ++p; break;
4968 case 'e': *name++ = ESC; ++p; break;
4969 case 'f': *name++ = FF; ++p; break;
4970 case 'n': *name++ = NL; ++p; break;
4971 case 'r': *name++ = CAR; ++p; break;
4972 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973
4974 case 'X': /* hex: "\x1", "\x12" */
4975 case 'x':
4976 case 'u': /* Unicode: "\u0023" */
4977 case 'U':
4978 if (vim_isxdigit(p[1]))
4979 {
4980 int n, nr;
4981 int c = toupper(*p);
4982
4983 if (c == 'X')
4984 n = 2;
4985 else
4986 n = 4;
4987 nr = 0;
4988 while (--n >= 0 && vim_isxdigit(p[1]))
4989 {
4990 ++p;
4991 nr = (nr << 4) + hex2nr(*p);
4992 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004993 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994#ifdef FEAT_MBYTE
4995 /* For "\u" store the number according to
4996 * 'encoding'. */
4997 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00004998 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 else
5000#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005001 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 break;
5004
5005 /* octal: "\1", "\12", "\123" */
5006 case '0':
5007 case '1':
5008 case '2':
5009 case '3':
5010 case '4':
5011 case '5':
5012 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005013 case '7': *name = *p++ - '0';
5014 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005016 *name = (*name << 3) + *p++ - '0';
5017 if (*p >= '0' && *p <= '7')
5018 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005020 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 break;
5022
5023 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005024 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 if (extra != 0)
5026 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005027 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 break;
5029 }
5030 /* FALLTHROUGH */
5031
Bram Moolenaar8c711452005-01-14 21:53:12 +00005032 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 break;
5034 }
5035 }
5036 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005037 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005040 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 *arg = p + 1;
5042
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 return OK;
5044}
5045
5046/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005047 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 * Return OK or FAIL.
5049 */
5050 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005051get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005053 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 int evaluate;
5055{
5056 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005057 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005058 int reduce = 0;
5059
5060 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005061 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005062 */
5063 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5064 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005065 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005066 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005067 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005068 break;
5069 ++reduce;
5070 ++p;
5071 }
5072 }
5073
Bram Moolenaar8c711452005-01-14 21:53:12 +00005074 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005075 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005076 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005077 return FAIL;
5078 }
5079
Bram Moolenaar8c711452005-01-14 21:53:12 +00005080 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005081 if (!evaluate)
5082 {
5083 *arg = p + 1;
5084 return OK;
5085 }
5086
5087 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005088 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005089 */
5090 str = alloc((unsigned)((p - *arg) - reduce));
5091 if (str == NULL)
5092 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005093 rettv->v_type = VAR_STRING;
5094 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005095
Bram Moolenaar8c711452005-01-14 21:53:12 +00005096 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005097 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005098 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005099 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005100 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005101 break;
5102 ++p;
5103 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005104 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005105 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005106 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005107 *arg = p + 1;
5108
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005109 return OK;
5110}
5111
5112/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005113 * Allocate a variable for a List and fill it from "*arg".
5114 * Return OK or FAIL.
5115 */
5116 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005117get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005118 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005119 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005120 int evaluate;
5121{
Bram Moolenaar33570922005-01-25 22:26:29 +00005122 list_T *l = NULL;
5123 typval_T tv;
5124 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005125
5126 if (evaluate)
5127 {
5128 l = list_alloc();
5129 if (l == NULL)
5130 return FAIL;
5131 }
5132
5133 *arg = skipwhite(*arg + 1);
5134 while (**arg != ']' && **arg != NUL)
5135 {
5136 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5137 goto failret;
5138 if (evaluate)
5139 {
5140 item = listitem_alloc();
5141 if (item != NULL)
5142 {
5143 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005144 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005145 list_append(l, item);
5146 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005147 else
5148 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005149 }
5150
5151 if (**arg == ']')
5152 break;
5153 if (**arg != ',')
5154 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005155 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005156 goto failret;
5157 }
5158 *arg = skipwhite(*arg + 1);
5159 }
5160
5161 if (**arg != ']')
5162 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005163 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005164failret:
5165 if (evaluate)
5166 list_free(l);
5167 return FAIL;
5168 }
5169
5170 *arg = skipwhite(*arg + 1);
5171 if (evaluate)
5172 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005173 rettv->v_type = VAR_LIST;
5174 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005175 ++l->lv_refcount;
5176 }
5177
5178 return OK;
5179}
5180
5181/*
5182 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005183 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005184 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005185 static list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005186list_alloc()
5187{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005188 list_T *l;
5189
5190 l = (list_T *)alloc_clear(sizeof(list_T));
5191 if (l != NULL)
5192 {
5193 /* Prepend the list to the list of lists for garbage collection. */
5194 if (first_list != NULL)
5195 first_list->lv_used_prev = l;
5196 l->lv_used_prev = NULL;
5197 l->lv_used_next = first_list;
5198 first_list = l;
5199 }
5200 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005201}
5202
5203/*
5204 * Unreference a list: decrement the reference count and free it when it
5205 * becomes zero.
5206 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005207 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005208list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005209 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005210{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005211 if (l != NULL && l->lv_refcount != DEL_REFCOUNT && --l->lv_refcount <= 0)
5212 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005213}
5214
5215/*
5216 * Free a list, including all items it points to.
5217 * Ignores the reference count.
5218 */
5219 static void
5220list_free(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005221 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005222{
Bram Moolenaar33570922005-01-25 22:26:29 +00005223 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005224
Bram Moolenaard9fba312005-06-26 22:34:35 +00005225 /* Avoid that recursive reference to the list frees us again. */
5226 l->lv_refcount = DEL_REFCOUNT;
5227
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005228 /* Remove the list from the list of lists for garbage collection. */
5229 if (l->lv_used_prev == NULL)
5230 first_list = l->lv_used_next;
5231 else
5232 l->lv_used_prev->lv_used_next = l->lv_used_next;
5233 if (l->lv_used_next != NULL)
5234 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5235
Bram Moolenaard9fba312005-06-26 22:34:35 +00005236 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005237 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005238 /* Remove the item before deleting it. */
5239 l->lv_first = item->li_next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005240 listitem_free(item);
5241 }
5242 vim_free(l);
5243}
5244
5245/*
5246 * Allocate a list item.
5247 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005248 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005249listitem_alloc()
5250{
Bram Moolenaar33570922005-01-25 22:26:29 +00005251 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005252}
5253
5254/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005255 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005256 */
5257 static void
5258listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005259 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005260{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005261 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005262 vim_free(item);
5263}
5264
5265/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005266 * Remove a list item from a List and free it. Also clears the value.
5267 */
5268 static void
5269listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005270 list_T *l;
5271 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005272{
5273 list_remove(l, item, item);
5274 listitem_free(item);
5275}
5276
5277/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005278 * Get the number of items in a list.
5279 */
5280 static long
5281list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005282 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005283{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005284 if (l == NULL)
5285 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005286 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005287}
5288
5289/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005290 * Return TRUE when two lists have exactly the same values.
5291 */
5292 static int
5293list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005294 list_T *l1;
5295 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005296 int ic; /* ignore case for strings */
5297{
Bram Moolenaar33570922005-01-25 22:26:29 +00005298 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005299
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005300 if (list_len(l1) != list_len(l2))
5301 return FALSE;
5302
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005303 for (item1 = l1->lv_first, item2 = l2->lv_first;
5304 item1 != NULL && item2 != NULL;
5305 item1 = item1->li_next, item2 = item2->li_next)
5306 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5307 return FALSE;
5308 return item1 == NULL && item2 == NULL;
5309}
5310
5311/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005312 * Return TRUE when two dictionaries have exactly the same key/values.
5313 */
5314 static int
5315dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005316 dict_T *d1;
5317 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005318 int ic; /* ignore case for strings */
5319{
Bram Moolenaar33570922005-01-25 22:26:29 +00005320 hashitem_T *hi;
5321 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005322 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005323
5324 if (dict_len(d1) != dict_len(d2))
5325 return FALSE;
5326
Bram Moolenaar33570922005-01-25 22:26:29 +00005327 todo = d1->dv_hashtab.ht_used;
5328 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005329 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005330 if (!HASHITEM_EMPTY(hi))
5331 {
5332 item2 = dict_find(d2, hi->hi_key, -1);
5333 if (item2 == NULL)
5334 return FALSE;
5335 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5336 return FALSE;
5337 --todo;
5338 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005339 }
5340 return TRUE;
5341}
5342
5343/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005344 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005345 * Compares the items just like "==" would compare them, but strings and
5346 * numbers are different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005347 */
5348 static int
5349tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005350 typval_T *tv1;
5351 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005352 int ic; /* ignore case */
5353{
5354 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005355 char_u *s1, *s2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005356
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005357 if (tv1->v_type != tv2->v_type)
5358 return FALSE;
5359
5360 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005361 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005362 case VAR_LIST:
5363 /* recursive! */
5364 return list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5365
5366 case VAR_DICT:
5367 /* recursive! */
5368 return dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5369
5370 case VAR_FUNC:
5371 return (tv1->vval.v_string != NULL
5372 && tv2->vval.v_string != NULL
5373 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5374
5375 case VAR_NUMBER:
5376 return tv1->vval.v_number == tv2->vval.v_number;
5377
5378 case VAR_STRING:
5379 s1 = get_tv_string_buf(tv1, buf1);
5380 s2 = get_tv_string_buf(tv2, buf2);
5381 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005382 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005383
5384 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005385 return TRUE;
5386}
5387
5388/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005389 * Locate item with index "n" in list "l" and return it.
5390 * A negative index is counted from the end; -1 is the last item.
5391 * Returns NULL when "n" is out of range.
5392 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005393 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005394list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005395 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005396 long n;
5397{
Bram Moolenaar33570922005-01-25 22:26:29 +00005398 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005399 long idx;
5400
5401 if (l == NULL)
5402 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005403
5404 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005405 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005406 n = l->lv_len + n;
5407
5408 /* Check for index out of range. */
5409 if (n < 0 || n >= l->lv_len)
5410 return NULL;
5411
5412 /* When there is a cached index may start search from there. */
5413 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005414 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005415 if (n < l->lv_idx / 2)
5416 {
5417 /* closest to the start of the list */
5418 item = l->lv_first;
5419 idx = 0;
5420 }
5421 else if (n > (l->lv_idx + l->lv_len) / 2)
5422 {
5423 /* closest to the end of the list */
5424 item = l->lv_last;
5425 idx = l->lv_len - 1;
5426 }
5427 else
5428 {
5429 /* closest to the cached index */
5430 item = l->lv_idx_item;
5431 idx = l->lv_idx;
5432 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005433 }
5434 else
5435 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005436 if (n < l->lv_len / 2)
5437 {
5438 /* closest to the start of the list */
5439 item = l->lv_first;
5440 idx = 0;
5441 }
5442 else
5443 {
5444 /* closest to the end of the list */
5445 item = l->lv_last;
5446 idx = l->lv_len - 1;
5447 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005448 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005449
5450 while (n > idx)
5451 {
5452 /* search forward */
5453 item = item->li_next;
5454 ++idx;
5455 }
5456 while (n < idx)
5457 {
5458 /* search backward */
5459 item = item->li_prev;
5460 --idx;
5461 }
5462
5463 /* cache the used index */
5464 l->lv_idx = idx;
5465 l->lv_idx_item = item;
5466
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005467 return item;
5468}
5469
5470/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005471 * Locate "item" list "l" and return its index.
5472 * Returns -1 when "item" is not in the list.
5473 */
5474 static long
5475list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005476 list_T *l;
5477 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005478{
5479 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005480 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005481
5482 if (l == NULL)
5483 return -1;
5484 idx = 0;
5485 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5486 ++idx;
5487 if (li == NULL)
5488 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005489 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005490}
5491
5492/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005493 * Append item "item" to the end of list "l".
5494 */
5495 static void
5496list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005497 list_T *l;
5498 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005499{
5500 if (l->lv_last == NULL)
5501 {
5502 /* empty list */
5503 l->lv_first = item;
5504 l->lv_last = item;
5505 item->li_prev = NULL;
5506 }
5507 else
5508 {
5509 l->lv_last->li_next = item;
5510 item->li_prev = l->lv_last;
5511 l->lv_last = item;
5512 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005513 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005514 item->li_next = NULL;
5515}
5516
5517/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005518 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005519 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005520 */
5521 static int
5522list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005523 list_T *l;
5524 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005525{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005526 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005527
Bram Moolenaar05159a02005-02-26 23:04:13 +00005528 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005529 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005530 copy_tv(tv, &li->li_tv);
5531 list_append(l, li);
5532 return OK;
5533}
5534
5535/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005536 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005537 * Return FAIL when out of memory.
5538 */
5539 int
5540list_append_dict(list, dict)
5541 list_T *list;
5542 dict_T *dict;
5543{
5544 listitem_T *li = listitem_alloc();
5545
5546 if (li == NULL)
5547 return FAIL;
5548 li->li_tv.v_type = VAR_DICT;
5549 li->li_tv.v_lock = 0;
5550 li->li_tv.vval.v_dict = dict;
5551 list_append(list, li);
5552 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005553 return OK;
5554}
5555
5556/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005557 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00005558 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005559 * Returns FAIL when out of memory.
5560 */
5561 static int
Bram Moolenaar4463f292005-09-25 22:20:24 +00005562list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005563 list_T *l;
5564 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00005565 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005566{
5567 listitem_T *li = listitem_alloc();
5568
5569 if (li == NULL)
5570 return FAIL;
5571 list_append(l, li);
5572 li->li_tv.v_type = VAR_STRING;
5573 li->li_tv.v_lock = 0;
Bram Moolenaar4463f292005-09-25 22:20:24 +00005574 if ((li->li_tv.vval.v_string = len >= 0 ? vim_strnsave(str, len)
5575 : vim_strsave(str)) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005576 return FAIL;
5577 return OK;
5578}
5579
5580/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00005581 * Append "n" to list "l".
5582 * Returns FAIL when out of memory.
5583 */
5584 static int
5585list_append_number(l, n)
5586 list_T *l;
5587 varnumber_T n;
5588{
5589 listitem_T *li;
5590
5591 li = listitem_alloc();
5592 if (li == NULL)
5593 return FAIL;
5594 li->li_tv.v_type = VAR_NUMBER;
5595 li->li_tv.v_lock = 0;
5596 li->li_tv.vval.v_number = n;
5597 list_append(l, li);
5598 return OK;
5599}
5600
5601/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005602 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005603 * If "item" is NULL append at the end.
5604 * Return FAIL when out of memory.
5605 */
5606 static int
5607list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005608 list_T *l;
5609 typval_T *tv;
5610 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005611{
Bram Moolenaar33570922005-01-25 22:26:29 +00005612 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005613
5614 if (ni == NULL)
5615 return FAIL;
5616 copy_tv(tv, &ni->li_tv);
5617 if (item == NULL)
5618 /* Append new item at end of list. */
5619 list_append(l, ni);
5620 else
5621 {
5622 /* Insert new item before existing item. */
5623 ni->li_prev = item->li_prev;
5624 ni->li_next = item;
5625 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005626 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005627 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005628 ++l->lv_idx;
5629 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005630 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005631 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005632 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005633 l->lv_idx_item = NULL;
5634 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005635 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005636 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005637 }
5638 return OK;
5639}
5640
5641/*
5642 * Extend "l1" with "l2".
5643 * If "bef" is NULL append at the end, otherwise insert before this item.
5644 * Returns FAIL when out of memory.
5645 */
5646 static int
5647list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005648 list_T *l1;
5649 list_T *l2;
5650 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005651{
Bram Moolenaar33570922005-01-25 22:26:29 +00005652 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005653
5654 for (item = l2->lv_first; item != NULL; item = item->li_next)
5655 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5656 return FAIL;
5657 return OK;
5658}
5659
5660/*
5661 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5662 * Return FAIL when out of memory.
5663 */
5664 static int
5665list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005666 list_T *l1;
5667 list_T *l2;
5668 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005669{
Bram Moolenaar33570922005-01-25 22:26:29 +00005670 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005671
5672 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005673 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005674 if (l == NULL)
5675 return FAIL;
5676 tv->v_type = VAR_LIST;
5677 tv->vval.v_list = l;
5678
5679 /* append all items from the second list */
5680 return list_extend(l, l2, NULL);
5681}
5682
5683/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005684 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005685 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005686 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005687 * Returns NULL when out of memory.
5688 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005689 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005690list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005691 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005692 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005693 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005694{
Bram Moolenaar33570922005-01-25 22:26:29 +00005695 list_T *copy;
5696 listitem_T *item;
5697 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005698
5699 if (orig == NULL)
5700 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005701
5702 copy = list_alloc();
5703 if (copy != NULL)
5704 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005705 if (copyID != 0)
5706 {
5707 /* Do this before adding the items, because one of the items may
5708 * refer back to this list. */
5709 orig->lv_copyID = copyID;
5710 orig->lv_copylist = copy;
5711 }
5712 for (item = orig->lv_first; item != NULL && !got_int;
5713 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005714 {
5715 ni = listitem_alloc();
5716 if (ni == NULL)
5717 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005718 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005719 {
5720 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5721 {
5722 vim_free(ni);
5723 break;
5724 }
5725 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005726 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005727 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005728 list_append(copy, ni);
5729 }
5730 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005731 if (item != NULL)
5732 {
5733 list_unref(copy);
5734 copy = NULL;
5735 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005736 }
5737
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005738 return copy;
5739}
5740
5741/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005742 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005743 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005744 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005745 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005746list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005747 list_T *l;
5748 listitem_T *item;
5749 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005750{
Bram Moolenaar33570922005-01-25 22:26:29 +00005751 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005752
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005753 /* notify watchers */
5754 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005755 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005756 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005757 list_fix_watch(l, ip);
5758 if (ip == item2)
5759 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005760 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005761
5762 if (item2->li_next == NULL)
5763 l->lv_last = item->li_prev;
5764 else
5765 item2->li_next->li_prev = item->li_prev;
5766 if (item->li_prev == NULL)
5767 l->lv_first = item2->li_next;
5768 else
5769 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005770 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005771}
5772
5773/*
5774 * Return an allocated string with the string representation of a list.
5775 * May return NULL.
5776 */
5777 static char_u *
5778list2string(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005779 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005780{
5781 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005782
5783 if (tv->vval.v_list == NULL)
5784 return NULL;
5785 ga_init2(&ga, (int)sizeof(char), 80);
5786 ga_append(&ga, '[');
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005787 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE) == FAIL)
5788 {
5789 vim_free(ga.ga_data);
5790 return NULL;
5791 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005792 ga_append(&ga, ']');
5793 ga_append(&ga, NUL);
5794 return (char_u *)ga.ga_data;
5795}
5796
5797/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005798 * Join list "l" into a string in "*gap", using separator "sep".
5799 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005800 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005801 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005802 static int
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005803list_join(gap, l, sep, echo)
5804 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00005805 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005806 char_u *sep;
5807 int echo;
5808{
5809 int first = TRUE;
5810 char_u *tofree;
5811 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00005812 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005813 char_u *s;
5814
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005815 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005816 {
5817 if (first)
5818 first = FALSE;
5819 else
5820 ga_concat(gap, sep);
5821
5822 if (echo)
5823 s = echo_string(&item->li_tv, &tofree, numbuf);
5824 else
5825 s = tv2string(&item->li_tv, &tofree, numbuf);
5826 if (s != NULL)
5827 ga_concat(gap, s);
5828 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005829 if (s == NULL)
5830 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005831 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005832 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005833}
5834
5835/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005836 * Garbage collection for lists and dictionaries.
5837 *
5838 * We use reference counts to be able to free most items right away when they
5839 * are no longer used. But for composite items it's possible that it becomes
5840 * unused while the reference count is > 0: When there is a recursive
5841 * reference. Example:
5842 * :let l = [1, 2, 3]
5843 * :let d = {9: l}
5844 * :let l[1] = d
5845 *
5846 * Since this is quite unusual we handle this with garbage collection: every
5847 * once in a while find out which lists and dicts are not referenced from any
5848 * variable.
5849 *
5850 * Here is a good reference text about garbage collection (refers to Python
5851 * but it applies to all reference-counting mechanisms):
5852 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005853 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005854
5855/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005856 * Do garbage collection for lists and dicts.
5857 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005858 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005859 int
5860garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00005861{
5862 dict_T *dd;
5863 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005864 int copyID = ++current_copyID;
5865 buf_T *buf;
5866 win_T *wp;
5867 int i;
5868 funccall_T *fc;
5869 int did_free = FALSE;
5870
5871 /*
5872 * 1. Go through all accessible variables and mark all lists and dicts
5873 * with copyID.
5874 */
5875 /* script-local variables */
5876 for (i = 1; i <= ga_scripts.ga_len; ++i)
5877 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
5878
5879 /* buffer-local variables */
5880 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5881 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
5882
5883 /* window-local variables */
5884 FOR_ALL_WINDOWS(wp)
5885 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
5886
5887 /* global variables */
5888 set_ref_in_ht(&globvarht, copyID);
5889
5890 /* function-local variables */
5891 for (fc = current_funccal; fc != NULL; fc = fc->caller)
5892 {
5893 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
5894 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
5895 }
5896
5897 /*
5898 * 2. Go through the list of dicts and free items without the copyID.
5899 */
5900 for (dd = first_dict; dd != NULL; )
5901 if (dd->dv_copyID != copyID)
5902 {
5903 dict_free(dd);
5904 did_free = TRUE;
5905
5906 /* restart, next dict may also have been freed */
5907 dd = first_dict;
5908 }
5909 else
5910 dd = dd->dv_used_next;
5911
5912 /*
5913 * 3. Go through the list of lists and free items without the copyID.
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00005914 * But don't free a list that has a watcher (used in a for loop), these
5915 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005916 */
5917 for (ll = first_list; ll != NULL; )
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00005918 if (ll->lv_copyID != copyID && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005919 {
5920 list_free(ll);
5921 did_free = TRUE;
5922
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00005923 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005924 ll = first_list;
5925 }
5926 else
5927 ll = ll->lv_used_next;
5928
5929 return did_free;
5930}
5931
5932/*
5933 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
5934 */
5935 static void
5936set_ref_in_ht(ht, copyID)
5937 hashtab_T *ht;
5938 int copyID;
5939{
5940 int todo;
5941 hashitem_T *hi;
5942
5943 todo = ht->ht_used;
5944 for (hi = ht->ht_array; todo > 0; ++hi)
5945 if (!HASHITEM_EMPTY(hi))
5946 {
5947 --todo;
5948 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
5949 }
5950}
5951
5952/*
5953 * Mark all lists and dicts referenced through list "l" with "copyID".
5954 */
5955 static void
5956set_ref_in_list(l, copyID)
5957 list_T *l;
5958 int copyID;
5959{
5960 listitem_T *li;
5961
5962 for (li = l->lv_first; li != NULL; li = li->li_next)
5963 set_ref_in_item(&li->li_tv, copyID);
5964}
5965
5966/*
5967 * Mark all lists and dicts referenced through typval "tv" with "copyID".
5968 */
5969 static void
5970set_ref_in_item(tv, copyID)
5971 typval_T *tv;
5972 int copyID;
5973{
5974 dict_T *dd;
5975 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005976
5977 switch (tv->v_type)
5978 {
5979 case VAR_DICT:
5980 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005981 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005982 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005983 /* Didn't see this dict yet. */
5984 dd->dv_copyID = copyID;
5985 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00005986 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005987 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005988
5989 case VAR_LIST:
5990 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005991 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005992 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005993 /* Didn't see this list yet. */
5994 ll->lv_copyID = copyID;
5995 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00005996 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005997 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005998 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005999 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006000}
6001
6002/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006003 * Allocate an empty header for a dictionary.
6004 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006005 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006006dict_alloc()
6007{
Bram Moolenaar33570922005-01-25 22:26:29 +00006008 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006009
Bram Moolenaar33570922005-01-25 22:26:29 +00006010 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006011 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006012 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006013 /* Add the list to the hashtable for garbage collection. */
6014 if (first_dict != NULL)
6015 first_dict->dv_used_prev = d;
6016 d->dv_used_next = first_dict;
6017 d->dv_used_prev = NULL;
6018
Bram Moolenaar33570922005-01-25 22:26:29 +00006019 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006020 d->dv_lock = 0;
6021 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006022 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006023 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006024 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006025}
6026
6027/*
6028 * Unreference a Dictionary: decrement the reference count and free it when it
6029 * becomes zero.
6030 */
6031 static void
6032dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006033 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006034{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006035 if (d != NULL && d->dv_refcount != DEL_REFCOUNT && --d->dv_refcount <= 0)
6036 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006037}
6038
6039/*
6040 * Free a Dictionary, including all items it contains.
6041 * Ignores the reference count.
6042 */
6043 static void
6044dict_free(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006045 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006046{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006047 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006048 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006049 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006050
Bram Moolenaard9fba312005-06-26 22:34:35 +00006051 /* Avoid that recursive reference to the dict frees us again. */
6052 d->dv_refcount = DEL_REFCOUNT;
6053
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006054 /* Remove the dict from the list of dicts for garbage collection. */
6055 if (d->dv_used_prev == NULL)
6056 first_dict = d->dv_used_next;
6057 else
6058 d->dv_used_prev->dv_used_next = d->dv_used_next;
6059 if (d->dv_used_next != NULL)
6060 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6061
6062 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006063 hash_lock(&d->dv_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +00006064 todo = d->dv_hashtab.ht_used;
6065 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006066 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006067 if (!HASHITEM_EMPTY(hi))
6068 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006069 /* Remove the item before deleting it, just in case there is
6070 * something recursive causing trouble. */
6071 di = HI2DI(hi);
6072 hash_remove(&d->dv_hashtab, hi);
6073 dictitem_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006074 --todo;
6075 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006076 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006077 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006078 vim_free(d);
6079}
6080
6081/*
6082 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006083 * The "key" is copied to the new item.
6084 * Note that the value of the item "di_tv" still needs to be initialized!
6085 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006086 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006087 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006088dictitem_alloc(key)
6089 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006090{
Bram Moolenaar33570922005-01-25 22:26:29 +00006091 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006092
Bram Moolenaar33570922005-01-25 22:26:29 +00006093 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(key));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006094 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006095 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006096 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006097 di->di_flags = 0;
6098 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006099 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006100}
6101
6102/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006103 * Make a copy of a Dictionary item.
6104 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006105 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006106dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006107 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006108{
Bram Moolenaar33570922005-01-25 22:26:29 +00006109 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006110
Bram Moolenaar33570922005-01-25 22:26:29 +00006111 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(org->di_key));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006112 if (di != NULL)
6113 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006114 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006115 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006116 copy_tv(&org->di_tv, &di->di_tv);
6117 }
6118 return di;
6119}
6120
6121/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006122 * Remove item "item" from Dictionary "dict" and free it.
6123 */
6124 static void
6125dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006126 dict_T *dict;
6127 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006128{
Bram Moolenaar33570922005-01-25 22:26:29 +00006129 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006130
Bram Moolenaar33570922005-01-25 22:26:29 +00006131 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006132 if (HASHITEM_EMPTY(hi))
6133 EMSG2(_(e_intern2), "dictitem_remove()");
6134 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006135 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006136 dictitem_free(item);
6137}
6138
6139/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006140 * Free a dict item. Also clears the value.
6141 */
6142 static void
6143dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006144 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006145{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006146 clear_tv(&item->di_tv);
6147 vim_free(item);
6148}
6149
6150/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006151 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6152 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006153 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006154 * Returns NULL when out of memory.
6155 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006156 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006157dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006158 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006159 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006160 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006161{
Bram Moolenaar33570922005-01-25 22:26:29 +00006162 dict_T *copy;
6163 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006164 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006165 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006166
6167 if (orig == NULL)
6168 return NULL;
6169
6170 copy = dict_alloc();
6171 if (copy != NULL)
6172 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006173 if (copyID != 0)
6174 {
6175 orig->dv_copyID = copyID;
6176 orig->dv_copydict = copy;
6177 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006178 todo = orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006179 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006180 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006181 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006182 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006183 --todo;
6184
6185 di = dictitem_alloc(hi->hi_key);
6186 if (di == NULL)
6187 break;
6188 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006189 {
6190 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6191 copyID) == FAIL)
6192 {
6193 vim_free(di);
6194 break;
6195 }
6196 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006197 else
6198 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6199 if (dict_add(copy, di) == FAIL)
6200 {
6201 dictitem_free(di);
6202 break;
6203 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006204 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006205 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006206
Bram Moolenaare9a41262005-01-15 22:18:47 +00006207 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006208 if (todo > 0)
6209 {
6210 dict_unref(copy);
6211 copy = NULL;
6212 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006213 }
6214
6215 return copy;
6216}
6217
6218/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006219 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006220 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006221 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006222 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006223dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006224 dict_T *d;
6225 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006226{
Bram Moolenaar33570922005-01-25 22:26:29 +00006227 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006228}
6229
Bram Moolenaar8c711452005-01-14 21:53:12 +00006230/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006231 * Add a number or string entry to dictionary "d".
6232 * When "str" is NULL use number "nr", otherwise use "str".
6233 * Returns FAIL when out of memory and when key already exists.
6234 */
6235 int
6236dict_add_nr_str(d, key, nr, str)
6237 dict_T *d;
6238 char *key;
6239 long nr;
6240 char_u *str;
6241{
6242 dictitem_T *item;
6243
6244 item = dictitem_alloc((char_u *)key);
6245 if (item == NULL)
6246 return FAIL;
6247 item->di_tv.v_lock = 0;
6248 if (str == NULL)
6249 {
6250 item->di_tv.v_type = VAR_NUMBER;
6251 item->di_tv.vval.v_number = nr;
6252 }
6253 else
6254 {
6255 item->di_tv.v_type = VAR_STRING;
6256 item->di_tv.vval.v_string = vim_strsave(str);
6257 }
6258 if (dict_add(d, item) == FAIL)
6259 {
6260 dictitem_free(item);
6261 return FAIL;
6262 }
6263 return OK;
6264}
6265
6266/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006267 * Get the number of items in a Dictionary.
6268 */
6269 static long
6270dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006271 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006272{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006273 if (d == NULL)
6274 return 0L;
Bram Moolenaar33570922005-01-25 22:26:29 +00006275 return d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006276}
6277
6278/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006279 * Find item "key[len]" in Dictionary "d".
6280 * If "len" is negative use strlen(key).
6281 * Returns NULL when not found.
6282 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006283 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006284dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006285 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006286 char_u *key;
6287 int len;
6288{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006289#define AKEYLEN 200
6290 char_u buf[AKEYLEN];
6291 char_u *akey;
6292 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006293 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006294
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006295 if (len < 0)
6296 akey = key;
6297 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006298 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006299 tofree = akey = vim_strnsave(key, len);
6300 if (akey == NULL)
6301 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006302 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006303 else
6304 {
6305 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006306 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006307 akey = buf;
6308 }
6309
Bram Moolenaar33570922005-01-25 22:26:29 +00006310 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006311 vim_free(tofree);
6312 if (HASHITEM_EMPTY(hi))
6313 return NULL;
6314 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006315}
6316
6317/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006318 * Get a string item from a dictionary in allocated memory.
6319 * Returns NULL if the entry doesn't exist or out of memory.
6320 */
6321 char_u *
6322get_dict_string(d, key)
6323 dict_T *d;
6324 char_u *key;
6325{
6326 dictitem_T *di;
6327
6328 di = dict_find(d, key, -1);
6329 if (di == NULL)
6330 return NULL;
6331 return vim_strsave(get_tv_string(&di->di_tv));
6332}
6333
6334/*
6335 * Get a number item from a dictionary.
6336 * Returns 0 if the entry doesn't exist or out of memory.
6337 */
6338 long
6339get_dict_number(d, key)
6340 dict_T *d;
6341 char_u *key;
6342{
6343 dictitem_T *di;
6344
6345 di = dict_find(d, key, -1);
6346 if (di == NULL)
6347 return 0;
6348 return get_tv_number(&di->di_tv);
6349}
6350
6351/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006352 * Return an allocated string with the string representation of a Dictionary.
6353 * May return NULL.
6354 */
6355 static char_u *
6356dict2string(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006357 typval_T *tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006358{
6359 garray_T ga;
6360 int first = TRUE;
6361 char_u *tofree;
6362 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006363 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006364 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006365 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006366 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006367
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006368 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006369 return NULL;
6370 ga_init2(&ga, (int)sizeof(char), 80);
6371 ga_append(&ga, '{');
6372
Bram Moolenaar33570922005-01-25 22:26:29 +00006373 todo = d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006374 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006375 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006376 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006377 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006378 --todo;
6379
6380 if (first)
6381 first = FALSE;
6382 else
6383 ga_concat(&ga, (char_u *)", ");
6384
6385 tofree = string_quote(hi->hi_key, FALSE);
6386 if (tofree != NULL)
6387 {
6388 ga_concat(&ga, tofree);
6389 vim_free(tofree);
6390 }
6391 ga_concat(&ga, (char_u *)": ");
6392 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf);
6393 if (s != NULL)
6394 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006395 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006396 if (s == NULL)
6397 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006398 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006399 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006400 if (todo > 0)
6401 {
6402 vim_free(ga.ga_data);
6403 return NULL;
6404 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006405
6406 ga_append(&ga, '}');
6407 ga_append(&ga, NUL);
6408 return (char_u *)ga.ga_data;
6409}
6410
6411/*
6412 * Allocate a variable for a Dictionary and fill it from "*arg".
6413 * Return OK or FAIL. Returns NOTDONE for {expr}.
6414 */
6415 static int
6416get_dict_tv(arg, rettv, evaluate)
6417 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006418 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006419 int evaluate;
6420{
Bram Moolenaar33570922005-01-25 22:26:29 +00006421 dict_T *d = NULL;
6422 typval_T tvkey;
6423 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006424 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00006425 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006426 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006427 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00006428
6429 /*
6430 * First check if it's not a curly-braces thing: {expr}.
6431 * Must do this without evaluating, otherwise a function may be called
6432 * twice. Unfortunately this means we need to call eval1() twice for the
6433 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006434 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006435 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006436 if (*start != '}')
6437 {
6438 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
6439 return FAIL;
6440 if (*start == '}')
6441 return NOTDONE;
6442 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006443
6444 if (evaluate)
6445 {
6446 d = dict_alloc();
6447 if (d == NULL)
6448 return FAIL;
6449 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006450 tvkey.v_type = VAR_UNKNOWN;
6451 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006452
6453 *arg = skipwhite(*arg + 1);
6454 while (**arg != '}' && **arg != NUL)
6455 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006456 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006457 goto failret;
6458 if (**arg != ':')
6459 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006460 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006461 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006462 goto failret;
6463 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006464 key = get_tv_string_buf_chk(&tvkey, buf);
6465 if (key == NULL || *key == NUL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006466 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006467 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6468 if (key != NULL)
6469 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006470 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006471 goto failret;
6472 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006473
6474 *arg = skipwhite(*arg + 1);
6475 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6476 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006477 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006478 goto failret;
6479 }
6480 if (evaluate)
6481 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006482 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006483 if (item != NULL)
6484 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00006485 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006486 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006487 clear_tv(&tv);
6488 goto failret;
6489 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006490 item = dictitem_alloc(key);
6491 clear_tv(&tvkey);
6492 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006493 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006494 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006495 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006496 if (dict_add(d, item) == FAIL)
6497 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006498 }
6499 }
6500
6501 if (**arg == '}')
6502 break;
6503 if (**arg != ',')
6504 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006505 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006506 goto failret;
6507 }
6508 *arg = skipwhite(*arg + 1);
6509 }
6510
6511 if (**arg != '}')
6512 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006513 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006514failret:
6515 if (evaluate)
6516 dict_free(d);
6517 return FAIL;
6518 }
6519
6520 *arg = skipwhite(*arg + 1);
6521 if (evaluate)
6522 {
6523 rettv->v_type = VAR_DICT;
6524 rettv->vval.v_dict = d;
6525 ++d->dv_refcount;
6526 }
6527
6528 return OK;
6529}
6530
Bram Moolenaar8c711452005-01-14 21:53:12 +00006531/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006532 * Return a string with the string representation of a variable.
6533 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006534 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006535 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006536 * May return NULL;
6537 */
6538 static char_u *
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006539echo_string(tv, tofree, numbuf)
Bram Moolenaar33570922005-01-25 22:26:29 +00006540 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006541 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006542 char_u *numbuf;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006543{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006544 static int recurse = 0;
6545 char_u *r = NULL;
6546
Bram Moolenaar33570922005-01-25 22:26:29 +00006547 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006548 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006549 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006550 *tofree = NULL;
6551 return NULL;
6552 }
6553 ++recurse;
6554
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006555 switch (tv->v_type)
6556 {
6557 case VAR_FUNC:
6558 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006559 r = tv->vval.v_string;
6560 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006561 case VAR_LIST:
6562 *tofree = list2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006563 r = *tofree;
6564 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006565 case VAR_DICT:
6566 *tofree = dict2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006567 r = *tofree;
6568 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006569 case VAR_STRING:
6570 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006571 *tofree = NULL;
6572 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006573 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006574 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006575 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006576 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006577 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006578
6579 --recurse;
6580 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006581}
6582
6583/*
6584 * Return a string with the string representation of a variable.
6585 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6586 * "numbuf" is used for a number.
6587 * Puts quotes around strings, so that they can be parsed back by eval().
6588 * May return NULL;
6589 */
6590 static char_u *
6591tv2string(tv, tofree, numbuf)
Bram Moolenaar33570922005-01-25 22:26:29 +00006592 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006593 char_u **tofree;
6594 char_u *numbuf;
6595{
6596 switch (tv->v_type)
6597 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006598 case VAR_FUNC:
6599 *tofree = string_quote(tv->vval.v_string, TRUE);
6600 return *tofree;
6601 case VAR_STRING:
6602 *tofree = string_quote(tv->vval.v_string, FALSE);
6603 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006604 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006605 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006606 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006607 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006608 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006609 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006610 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006611 return echo_string(tv, tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006612}
6613
6614/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006615 * Return string "str" in ' quotes, doubling ' characters.
6616 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006617 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006618 */
6619 static char_u *
6620string_quote(str, function)
6621 char_u *str;
6622 int function;
6623{
Bram Moolenaar33570922005-01-25 22:26:29 +00006624 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006625 char_u *p, *r, *s;
6626
Bram Moolenaar33570922005-01-25 22:26:29 +00006627 len = (function ? 13 : 3);
6628 if (str != NULL)
6629 {
6630 len += STRLEN(str);
6631 for (p = str; *p != NUL; mb_ptr_adv(p))
6632 if (*p == '\'')
6633 ++len;
6634 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006635 s = r = alloc(len);
6636 if (r != NULL)
6637 {
6638 if (function)
6639 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006640 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006641 r += 10;
6642 }
6643 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006644 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006645 if (str != NULL)
6646 for (p = str; *p != NUL; )
6647 {
6648 if (*p == '\'')
6649 *r++ = '\'';
6650 MB_COPY_CHAR(p, r);
6651 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006652 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006653 if (function)
6654 *r++ = ')';
6655 *r++ = NUL;
6656 }
6657 return s;
6658}
6659
6660/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 * Get the value of an environment variable.
6662 * "arg" is pointing to the '$'. It is advanced to after the name.
6663 * If the environment variable was not set, silently assume it is empty.
6664 * Always return OK.
6665 */
6666 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006667get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006669 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 int evaluate;
6671{
6672 char_u *string = NULL;
6673 int len;
6674 int cc;
6675 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006676 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677
6678 ++*arg;
6679 name = *arg;
6680 len = get_env_len(arg);
6681 if (evaluate)
6682 {
6683 if (len != 0)
6684 {
6685 cc = name[len];
6686 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006687 /* first try vim_getenv(), fast for normal environment vars */
6688 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006690 {
6691 if (!mustfree)
6692 string = vim_strsave(string);
6693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694 else
6695 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00006696 if (mustfree)
6697 vim_free(string);
6698
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699 /* next try expanding things like $VIM and ${HOME} */
6700 string = expand_env_save(name - 1);
6701 if (string != NULL && *string == '$')
6702 {
6703 vim_free(string);
6704 string = NULL;
6705 }
6706 }
6707 name[len] = cc;
6708 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006709 rettv->v_type = VAR_STRING;
6710 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711 }
6712
6713 return OK;
6714}
6715
6716/*
6717 * Array with names and number of arguments of all internal functions
6718 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
6719 */
6720static struct fst
6721{
6722 char *f_name; /* function name */
6723 char f_min_argc; /* minimal number of arguments */
6724 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00006725 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006726 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727} functions[] =
6728{
Bram Moolenaar0d660222005-01-07 21:51:51 +00006729 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730 {"append", 2, 2, f_append},
6731 {"argc", 0, 0, f_argc},
6732 {"argidx", 0, 0, f_argidx},
6733 {"argv", 1, 1, f_argv},
6734 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006735 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736 {"bufexists", 1, 1, f_bufexists},
6737 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
6738 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
6739 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
6740 {"buflisted", 1, 1, f_buflisted},
6741 {"bufloaded", 1, 1, f_bufloaded},
6742 {"bufname", 1, 1, f_bufname},
6743 {"bufnr", 1, 1, f_bufnr},
6744 {"bufwinnr", 1, 1, f_bufwinnr},
6745 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006746 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006747 {"call", 2, 3, f_call},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748 {"char2nr", 1, 1, f_char2nr},
6749 {"cindent", 1, 1, f_cindent},
6750 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00006751#if defined(FEAT_INS_EXPAND)
6752 {"complete_add", 1, 1, f_complete_add},
6753 {"complete_check", 0, 0, f_complete_check},
6754#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006756 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006757 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758 {"cscope_connection",0,3, f_cscope_connection},
6759 {"cursor", 2, 2, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006760 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761 {"delete", 1, 1, f_delete},
6762 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00006763 {"diff_filler", 1, 1, f_diff_filler},
6764 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006765 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006767 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768 {"eventhandler", 0, 0, f_eventhandler},
6769 {"executable", 1, 1, f_executable},
6770 {"exists", 1, 1, f_exists},
6771 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006772 {"extend", 2, 3, f_extend},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
6774 {"filereadable", 1, 1, f_filereadable},
6775 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006776 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006777 {"finddir", 1, 3, f_finddir},
6778 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779 {"fnamemodify", 2, 2, f_fnamemodify},
6780 {"foldclosed", 1, 1, f_foldclosed},
6781 {"foldclosedend", 1, 1, f_foldclosedend},
6782 {"foldlevel", 1, 1, f_foldlevel},
6783 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006784 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006786 {"function", 1, 1, f_function},
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006787 {"garbagecollect", 0, 0, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006788 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00006789 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790 {"getbufvar", 2, 2, f_getbufvar},
6791 {"getchar", 0, 1, f_getchar},
6792 {"getcharmod", 0, 0, f_getcharmod},
6793 {"getcmdline", 0, 0, f_getcmdline},
6794 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006795 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006797 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006798 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 {"getfsize", 1, 1, f_getfsize},
6800 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006801 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006802 {"getline", 1, 2, f_getline},
Bram Moolenaar2641f772005-03-25 21:58:17 +00006803 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006804 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805 {"getregtype", 0, 1, f_getregtype},
6806 {"getwinposx", 0, 0, f_getwinposx},
6807 {"getwinposy", 0, 0, f_getwinposy},
6808 {"getwinvar", 2, 2, f_getwinvar},
6809 {"glob", 1, 1, f_glob},
6810 {"globpath", 2, 2, f_globpath},
6811 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006812 {"has_key", 2, 2, f_has_key},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813 {"hasmapto", 1, 2, f_hasmapto},
6814 {"highlightID", 1, 1, f_hlID}, /* obsolete */
6815 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
6816 {"histadd", 2, 2, f_histadd},
6817 {"histdel", 1, 2, f_histdel},
6818 {"histget", 1, 2, f_histget},
6819 {"histnr", 1, 1, f_histnr},
6820 {"hlID", 1, 1, f_hlID},
6821 {"hlexists", 1, 1, f_hlexists},
6822 {"hostname", 0, 0, f_hostname},
6823 {"iconv", 3, 3, f_iconv},
6824 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006825 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006826 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00006828 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829 {"inputrestore", 0, 0, f_inputrestore},
6830 {"inputsave", 0, 0, f_inputsave},
6831 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006832 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006834 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006835 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006836 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006837 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006839 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 {"libcall", 3, 3, f_libcall},
6841 {"libcallnr", 3, 3, f_libcallnr},
6842 {"line", 1, 1, f_line},
6843 {"line2byte", 1, 1, f_line2byte},
6844 {"lispindent", 1, 1, f_lispindent},
6845 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006846 {"map", 2, 2, f_map},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847 {"maparg", 1, 2, f_maparg},
6848 {"mapcheck", 1, 2, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006849 {"match", 2, 4, f_match},
6850 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006851 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006852 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006853 {"max", 1, 1, f_max},
6854 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006855#ifdef vim_mkdir
6856 {"mkdir", 1, 3, f_mkdir},
6857#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858 {"mode", 0, 0, f_mode},
6859 {"nextnonblank", 1, 1, f_nextnonblank},
6860 {"nr2char", 1, 1, f_nr2char},
6861 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006862 {"printf", 2, 19, f_printf},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006863 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006864 {"readfile", 1, 3, f_readfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 {"remote_expr", 2, 3, f_remote_expr},
6866 {"remote_foreground", 1, 1, f_remote_foreground},
6867 {"remote_peek", 1, 2, f_remote_peek},
6868 {"remote_read", 1, 1, f_remote_read},
6869 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006870 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006872 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006874 {"reverse", 1, 1, f_reverse},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875 {"search", 1, 2, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00006876 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877 {"searchpair", 3, 5, f_searchpair},
6878 {"server2client", 2, 2, f_server2client},
6879 {"serverlist", 0, 0, f_serverlist},
6880 {"setbufvar", 3, 3, f_setbufvar},
6881 {"setcmdpos", 1, 1, f_setcmdpos},
6882 {"setline", 2, 2, f_setline},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00006883 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884 {"setreg", 2, 3, f_setreg},
6885 {"setwinvar", 3, 3, f_setwinvar},
6886 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006887 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006888 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00006889 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006890 {"spellsuggest", 1, 2, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006891 {"split", 1, 3, f_split},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892#ifdef HAVE_STRFTIME
6893 {"strftime", 1, 2, f_strftime},
6894#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00006895 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006896 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897 {"strlen", 1, 1, f_strlen},
6898 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00006899 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900 {"strtrans", 1, 1, f_strtrans},
6901 {"submatch", 1, 1, f_submatch},
6902 {"substitute", 4, 4, f_substitute},
6903 {"synID", 3, 3, f_synID},
6904 {"synIDattr", 2, 3, f_synIDattr},
6905 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006906 {"system", 1, 2, f_system},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006907 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006908 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00006910 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911 {"tolower", 1, 1, f_tolower},
6912 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00006913 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006915 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916 {"virtcol", 1, 1, f_virtcol},
6917 {"visualmode", 0, 1, f_visualmode},
6918 {"winbufnr", 1, 1, f_winbufnr},
6919 {"wincol", 0, 0, f_wincol},
6920 {"winheight", 1, 1, f_winheight},
6921 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006922 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923 {"winrestcmd", 0, 0, f_winrestcmd},
6924 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006925 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926};
6927
6928#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6929
6930/*
6931 * Function given to ExpandGeneric() to obtain the list of internal
6932 * or user defined function names.
6933 */
6934 char_u *
6935get_function_name(xp, idx)
6936 expand_T *xp;
6937 int idx;
6938{
6939 static int intidx = -1;
6940 char_u *name;
6941
6942 if (idx == 0)
6943 intidx = -1;
6944 if (intidx < 0)
6945 {
6946 name = get_user_func_name(xp, idx);
6947 if (name != NULL)
6948 return name;
6949 }
6950 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
6951 {
6952 STRCPY(IObuff, functions[intidx].f_name);
6953 STRCAT(IObuff, "(");
6954 if (functions[intidx].f_max_argc == 0)
6955 STRCAT(IObuff, ")");
6956 return IObuff;
6957 }
6958
6959 return NULL;
6960}
6961
6962/*
6963 * Function given to ExpandGeneric() to obtain the list of internal or
6964 * user defined variable or function names.
6965 */
6966/*ARGSUSED*/
6967 char_u *
6968get_expr_name(xp, idx)
6969 expand_T *xp;
6970 int idx;
6971{
6972 static int intidx = -1;
6973 char_u *name;
6974
6975 if (idx == 0)
6976 intidx = -1;
6977 if (intidx < 0)
6978 {
6979 name = get_function_name(xp, idx);
6980 if (name != NULL)
6981 return name;
6982 }
6983 return get_user_var_name(xp, ++intidx);
6984}
6985
6986#endif /* FEAT_CMDL_COMPL */
6987
6988/*
6989 * Find internal function in table above.
6990 * Return index, or -1 if not found
6991 */
6992 static int
6993find_internal_func(name)
6994 char_u *name; /* name of the function */
6995{
6996 int first = 0;
6997 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
6998 int cmp;
6999 int x;
7000
7001 /*
7002 * Find the function name in the table. Binary search.
7003 */
7004 while (first <= last)
7005 {
7006 x = first + ((unsigned)(last - first) >> 1);
7007 cmp = STRCMP(name, functions[x].f_name);
7008 if (cmp < 0)
7009 last = x - 1;
7010 else if (cmp > 0)
7011 first = x + 1;
7012 else
7013 return x;
7014 }
7015 return -1;
7016}
7017
7018/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007019 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7020 * name it contains, otherwise return "name".
7021 */
7022 static char_u *
7023deref_func_name(name, lenp)
7024 char_u *name;
7025 int *lenp;
7026{
Bram Moolenaar33570922005-01-25 22:26:29 +00007027 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007028 int cc;
7029
7030 cc = name[*lenp];
7031 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007032 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007033 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007034 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007035 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007036 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007037 {
7038 *lenp = 0;
7039 return (char_u *)""; /* just in case */
7040 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007041 *lenp = STRLEN(v->di_tv.vval.v_string);
7042 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007043 }
7044
7045 return name;
7046}
7047
7048/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049 * Allocate a variable for the result of a function.
7050 * Return OK or FAIL.
7051 */
7052 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007053get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7054 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 char_u *name; /* name of the function */
7056 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007057 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 char_u **arg; /* argument, pointing to the '(' */
7059 linenr_T firstline; /* first line of range */
7060 linenr_T lastline; /* last line of range */
7061 int *doesrange; /* return: function handled range */
7062 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007063 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064{
7065 char_u *argp;
7066 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00007067 typval_T argvars[MAX_FUNC_ARGS]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068 int argcount = 0; /* number of arguments found */
7069
7070 /*
7071 * Get the arguments.
7072 */
7073 argp = *arg;
7074 while (argcount < MAX_FUNC_ARGS)
7075 {
7076 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7077 if (*argp == ')' || *argp == ',' || *argp == NUL)
7078 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7080 {
7081 ret = FAIL;
7082 break;
7083 }
7084 ++argcount;
7085 if (*argp != ',')
7086 break;
7087 }
7088 if (*argp == ')')
7089 ++argp;
7090 else
7091 ret = FAIL;
7092
7093 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007094 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007095 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007097 {
7098 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007099 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007100 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007101 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007102 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103
7104 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007105 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106
7107 *arg = skipwhite(argp);
7108 return ret;
7109}
7110
7111
7112/*
7113 * Call a function with its resolved parameters
7114 * Return OK or FAIL.
7115 */
7116 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007117call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007118 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119 char_u *name; /* name of the function */
7120 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007121 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122 int argcount; /* number of "argvars" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007123 typval_T *argvars; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124 linenr_T firstline; /* first line of range */
7125 linenr_T lastline; /* last line of range */
7126 int *doesrange; /* return: function handled range */
7127 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007128 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129{
7130 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131#define ERROR_UNKNOWN 0
7132#define ERROR_TOOMANY 1
7133#define ERROR_TOOFEW 2
7134#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007135#define ERROR_DICT 4
7136#define ERROR_NONE 5
7137#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138 int error = ERROR_NONE;
7139 int i;
7140 int llen;
7141 ufunc_T *fp;
7142 int cc;
7143#define FLEN_FIXED 40
7144 char_u fname_buf[FLEN_FIXED + 1];
7145 char_u *fname;
7146
7147 /*
7148 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7149 * Change <SNR>123_name() to K_SNR 123_name().
7150 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7151 */
7152 cc = name[len];
7153 name[len] = NUL;
7154 llen = eval_fname_script(name);
7155 if (llen > 0)
7156 {
7157 fname_buf[0] = K_SPECIAL;
7158 fname_buf[1] = KS_EXTRA;
7159 fname_buf[2] = (int)KE_SNR;
7160 i = 3;
7161 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7162 {
7163 if (current_SID <= 0)
7164 error = ERROR_SCRIPT;
7165 else
7166 {
7167 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7168 i = (int)STRLEN(fname_buf);
7169 }
7170 }
7171 if (i + STRLEN(name + llen) < FLEN_FIXED)
7172 {
7173 STRCPY(fname_buf + i, name + llen);
7174 fname = fname_buf;
7175 }
7176 else
7177 {
7178 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7179 if (fname == NULL)
7180 error = ERROR_OTHER;
7181 else
7182 {
7183 mch_memmove(fname, fname_buf, (size_t)i);
7184 STRCPY(fname + i, name + llen);
7185 }
7186 }
7187 }
7188 else
7189 fname = name;
7190
7191 *doesrange = FALSE;
7192
7193
7194 /* execute the function if no errors detected and executing */
7195 if (evaluate && error == ERROR_NONE)
7196 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007197 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198 error = ERROR_UNKNOWN;
7199
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007200 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201 {
7202 /*
7203 * User defined function.
7204 */
7205 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007206
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007208 /* Trigger FuncUndefined event, may load the function. */
7209 if (fp == NULL
7210 && apply_autocmds(EVENT_FUNCUNDEFINED,
7211 fname, fname, TRUE, NULL)
7212 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007214 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215 fp = find_func(fname);
7216 }
7217#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007218 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007219 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007220 {
7221 /* loaded a package, search for the function again */
7222 fp = find_func(fname);
7223 }
7224
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225 if (fp != NULL)
7226 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007227 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007229 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007231 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007233 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007234 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 else
7236 {
7237 /*
7238 * Call the user function.
7239 * Save and restore search patterns, script variables and
7240 * redo buffer.
7241 */
7242 save_search_patterns();
7243 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007244 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007245 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007246 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007247 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7248 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7249 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007250 /* Function was unreferenced while being used, free it
7251 * now. */
7252 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 restoreRedobuff();
7254 restore_search_patterns();
7255 error = ERROR_NONE;
7256 }
7257 }
7258 }
7259 else
7260 {
7261 /*
7262 * Find the function name in the table, call its implementation.
7263 */
7264 i = find_internal_func(fname);
7265 if (i >= 0)
7266 {
7267 if (argcount < functions[i].f_min_argc)
7268 error = ERROR_TOOFEW;
7269 else if (argcount > functions[i].f_max_argc)
7270 error = ERROR_TOOMANY;
7271 else
7272 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007273 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007274 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 error = ERROR_NONE;
7276 }
7277 }
7278 }
7279 /*
7280 * The function call (or "FuncUndefined" autocommand sequence) might
7281 * have been aborted by an error, an interrupt, or an explicitly thrown
7282 * exception that has not been caught so far. This situation can be
7283 * tested for by calling aborting(). For an error in an internal
7284 * function or for the "E132" error in call_user_func(), however, the
7285 * throw point at which the "force_abort" flag (temporarily reset by
7286 * emsg()) is normally updated has not been reached yet. We need to
7287 * update that flag first to make aborting() reliable.
7288 */
7289 update_force_abort();
7290 }
7291 if (error == ERROR_NONE)
7292 ret = OK;
7293
7294 /*
7295 * Report an error unless the argument evaluation or function call has been
7296 * cancelled due to an aborting error, an interrupt, or an exception.
7297 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007298 if (!aborting())
7299 {
7300 switch (error)
7301 {
7302 case ERROR_UNKNOWN:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007303 emsg_funcname("E117: Unknown function: %s", name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007304 break;
7305 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007306 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007307 break;
7308 case ERROR_TOOFEW:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007309 emsg_funcname("E119: Not enough arguments for function: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007310 name);
7311 break;
7312 case ERROR_SCRIPT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007313 emsg_funcname("E120: Using <SID> not in a script context: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007314 name);
7315 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007316 case ERROR_DICT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007317 emsg_funcname("E725: Calling dict function without Dictionary: %s",
Bram Moolenaare9a41262005-01-15 22:18:47 +00007318 name);
7319 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007320 }
7321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322
7323 name[len] = cc;
7324 if (fname != name && fname != fname_buf)
7325 vim_free(fname);
7326
7327 return ret;
7328}
7329
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007330/*
7331 * Give an error message with a function name. Handle <SNR> things.
7332 */
7333 static void
7334emsg_funcname(msg, name)
7335 char *msg;
7336 char_u *name;
7337{
7338 char_u *p;
7339
7340 if (*name == K_SPECIAL)
7341 p = concat_str((char_u *)"<SNR>", name + 3);
7342 else
7343 p = name;
7344 EMSG2(_(msg), p);
7345 if (p != name)
7346 vim_free(p);
7347}
7348
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349/*********************************************
7350 * Implementation of the built-in functions
7351 */
7352
7353/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007354 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 */
7356 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007357f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007358 typval_T *argvars;
7359 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360{
Bram Moolenaar33570922005-01-25 22:26:29 +00007361 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007363 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007364 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007366 if ((l = argvars[0].vval.v_list) != NULL
7367 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7368 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007369 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007370 }
7371 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007372 EMSG(_(e_listreq));
7373}
7374
7375/*
7376 * "append(lnum, string/list)" function
7377 */
7378 static void
7379f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007380 typval_T *argvars;
7381 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007382{
7383 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007384 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007385 list_T *l = NULL;
7386 listitem_T *li = NULL;
7387 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007388 long added = 0;
7389
Bram Moolenaar0d660222005-01-07 21:51:51 +00007390 lnum = get_tv_lnum(argvars);
7391 if (lnum >= 0
7392 && lnum <= curbuf->b_ml.ml_line_count
7393 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007394 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007395 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007396 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007397 l = argvars[1].vval.v_list;
7398 if (l == NULL)
7399 return;
7400 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007401 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007402 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007403 for (;;)
7404 {
7405 if (l == NULL)
7406 tv = &argvars[1]; /* append a string */
7407 else if (li == NULL)
7408 break; /* end of list */
7409 else
7410 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007411 line = get_tv_string_chk(tv);
7412 if (line == NULL) /* type error */
7413 {
7414 rettv->vval.v_number = 1; /* Failed */
7415 break;
7416 }
7417 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007418 ++added;
7419 if (l == NULL)
7420 break;
7421 li = li->li_next;
7422 }
7423
7424 appended_lines_mark(lnum, added);
7425 if (curwin->w_cursor.lnum > lnum)
7426 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007428 else
7429 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430}
7431
7432/*
7433 * "argc()" function
7434 */
7435/* ARGSUSED */
7436 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007437f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007438 typval_T *argvars;
7439 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007441 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442}
7443
7444/*
7445 * "argidx()" function
7446 */
7447/* ARGSUSED */
7448 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007449f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007450 typval_T *argvars;
7451 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007453 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454}
7455
7456/*
7457 * "argv(nr)" function
7458 */
7459 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007460f_argv(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{
7464 int idx;
7465
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007466 idx = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467 if (idx >= 0 && idx < ARGCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007468 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007470 rettv->vval.v_string = NULL;
7471 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472}
7473
7474/*
7475 * "browse(save, title, initdir, default)" function
7476 */
7477/* ARGSUSED */
7478 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007479f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007480 typval_T *argvars;
7481 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482{
7483#ifdef FEAT_BROWSE
7484 int save;
7485 char_u *title;
7486 char_u *initdir;
7487 char_u *defname;
7488 char_u buf[NUMBUFLEN];
7489 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007490 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007492 save = get_tv_number_chk(&argvars[0], &error);
7493 title = get_tv_string_chk(&argvars[1]);
7494 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7495 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007497 if (error || title == NULL || initdir == NULL || defname == NULL)
7498 rettv->vval.v_string = NULL;
7499 else
7500 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007501 do_browse(save ? BROWSE_SAVE : 0,
7502 title, defname, NULL, initdir, NULL, curbuf);
7503#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007504 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007505#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007506 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007507}
7508
7509/*
7510 * "browsedir(title, initdir)" function
7511 */
7512/* ARGSUSED */
7513 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007514f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007515 typval_T *argvars;
7516 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007517{
7518#ifdef FEAT_BROWSE
7519 char_u *title;
7520 char_u *initdir;
7521 char_u buf[NUMBUFLEN];
7522
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007523 title = get_tv_string_chk(&argvars[0]);
7524 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007525
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007526 if (title == NULL || initdir == NULL)
7527 rettv->vval.v_string = NULL;
7528 else
7529 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007530 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007532 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007534 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535}
7536
Bram Moolenaar33570922005-01-25 22:26:29 +00007537static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007538
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539/*
7540 * Find a buffer by number or exact name.
7541 */
7542 static buf_T *
7543find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007544 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007545{
7546 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007548 if (avar->v_type == VAR_NUMBER)
7549 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007550 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007552 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007553 if (buf == NULL)
7554 {
7555 /* No full path name match, try a match with a URL or a "nofile"
7556 * buffer, these don't use the full path. */
7557 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7558 if (buf->b_fname != NULL
7559 && (path_with_url(buf->b_fname)
7560#ifdef FEAT_QUICKFIX
7561 || bt_nofile(buf)
7562#endif
7563 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007564 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007565 break;
7566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567 }
7568 return buf;
7569}
7570
7571/*
7572 * "bufexists(expr)" function
7573 */
7574 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007575f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007576 typval_T *argvars;
7577 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007579 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580}
7581
7582/*
7583 * "buflisted(expr)" function
7584 */
7585 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007586f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007587 typval_T *argvars;
7588 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589{
7590 buf_T *buf;
7591
7592 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007593 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007594}
7595
7596/*
7597 * "bufloaded(expr)" function
7598 */
7599 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007600f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007601 typval_T *argvars;
7602 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603{
7604 buf_T *buf;
7605
7606 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007607 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608}
7609
Bram Moolenaar33570922005-01-25 22:26:29 +00007610static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007611
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612/*
7613 * Get buffer by number or pattern.
7614 */
7615 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007616get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007617 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007618{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007619 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620 int save_magic;
7621 char_u *save_cpo;
7622 buf_T *buf;
7623
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007624 if (tv->v_type == VAR_NUMBER)
7625 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007626 if (tv->v_type != VAR_STRING)
7627 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628 if (name == NULL || *name == NUL)
7629 return curbuf;
7630 if (name[0] == '$' && name[1] == NUL)
7631 return lastbuf;
7632
7633 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7634 save_magic = p_magic;
7635 p_magic = TRUE;
7636 save_cpo = p_cpo;
7637 p_cpo = (char_u *)"";
7638
7639 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
7640 TRUE, FALSE));
7641
7642 p_magic = save_magic;
7643 p_cpo = save_cpo;
7644
7645 /* If not found, try expanding the name, like done for bufexists(). */
7646 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007647 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648
7649 return buf;
7650}
7651
7652/*
7653 * "bufname(expr)" function
7654 */
7655 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007656f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007657 typval_T *argvars;
7658 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659{
7660 buf_T *buf;
7661
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007662 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007664 buf = get_buf_tv(&argvars[0]);
7665 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007667 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007669 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670 --emsg_off;
7671}
7672
7673/*
7674 * "bufnr(expr)" function
7675 */
7676 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007677f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007678 typval_T *argvars;
7679 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680{
7681 buf_T *buf;
7682
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007683 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007685 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007687 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007689 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 --emsg_off;
7691}
7692
7693/*
7694 * "bufwinnr(nr)" function
7695 */
7696 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007697f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007698 typval_T *argvars;
7699 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700{
7701#ifdef FEAT_WINDOWS
7702 win_T *wp;
7703 int winnr = 0;
7704#endif
7705 buf_T *buf;
7706
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007707 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007709 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710#ifdef FEAT_WINDOWS
7711 for (wp = firstwin; wp; wp = wp->w_next)
7712 {
7713 ++winnr;
7714 if (wp->w_buffer == buf)
7715 break;
7716 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007717 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007719 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720#endif
7721 --emsg_off;
7722}
7723
7724/*
7725 * "byte2line(byte)" function
7726 */
7727/*ARGSUSED*/
7728 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007729f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007730 typval_T *argvars;
7731 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732{
7733#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007734 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735#else
7736 long boff = 0;
7737
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007738 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007740 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007742 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743 (linenr_T)0, &boff);
7744#endif
7745}
7746
7747/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007748 * "byteidx()" function
7749 */
7750/*ARGSUSED*/
7751 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007752f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007753 typval_T *argvars;
7754 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007755{
7756#ifdef FEAT_MBYTE
7757 char_u *t;
7758#endif
7759 char_u *str;
7760 long idx;
7761
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007762 str = get_tv_string_chk(&argvars[0]);
7763 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007764 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007765 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007766 return;
7767
7768#ifdef FEAT_MBYTE
7769 t = str;
7770 for ( ; idx > 0; idx--)
7771 {
7772 if (*t == NUL) /* EOL reached */
7773 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007774 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007775 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007776 rettv->vval.v_number = t - str;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007777#else
7778 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007779 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007780#endif
7781}
7782
7783/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007784 * "call(func, arglist)" function
7785 */
7786 static void
7787f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007788 typval_T *argvars;
7789 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007790{
7791 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00007792 typval_T argv[MAX_FUNC_ARGS];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007793 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007794 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007795 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00007796 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007797
7798 rettv->vval.v_number = 0;
7799 if (argvars[1].v_type != VAR_LIST)
7800 {
7801 EMSG(_(e_listreq));
7802 return;
7803 }
7804 if (argvars[1].vval.v_list == NULL)
7805 return;
7806
7807 if (argvars[0].v_type == VAR_FUNC)
7808 func = argvars[0].vval.v_string;
7809 else
7810 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007811 if (*func == NUL)
7812 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007813
Bram Moolenaare9a41262005-01-15 22:18:47 +00007814 if (argvars[2].v_type != VAR_UNKNOWN)
7815 {
7816 if (argvars[2].v_type != VAR_DICT)
7817 {
7818 EMSG(_(e_dictreq));
7819 return;
7820 }
7821 selfdict = argvars[2].vval.v_dict;
7822 }
7823
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007824 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
7825 item = item->li_next)
7826 {
7827 if (argc == MAX_FUNC_ARGS)
7828 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007829 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007830 break;
7831 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007832 /* Make a copy of each argument. This is needed to be able to set
7833 * v_lock to VAR_FIXED in the copy without changing the original list.
7834 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007835 copy_tv(&item->li_tv, &argv[argc++]);
7836 }
7837
7838 if (item == NULL)
7839 (void)call_func(func, STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007840 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
7841 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007842
7843 /* Free the arguments. */
7844 while (argc > 0)
7845 clear_tv(&argv[--argc]);
7846}
7847
7848/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849 * "char2nr(string)" function
7850 */
7851 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007852f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007853 typval_T *argvars;
7854 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855{
7856#ifdef FEAT_MBYTE
7857 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007858 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859 else
7860#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007861 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862}
7863
7864/*
7865 * "cindent(lnum)" function
7866 */
7867 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007868f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007869 typval_T *argvars;
7870 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871{
7872#ifdef FEAT_CINDENT
7873 pos_T pos;
7874 linenr_T lnum;
7875
7876 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007877 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7879 {
7880 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007881 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882 curwin->w_cursor = pos;
7883 }
7884 else
7885#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007886 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887}
7888
7889/*
7890 * "col(string)" function
7891 */
7892 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007893f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007894 typval_T *argvars;
7895 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896{
7897 colnr_T col = 0;
7898 pos_T *fp;
7899
7900 fp = var2fpos(&argvars[0], FALSE);
7901 if (fp != NULL)
7902 {
7903 if (fp->col == MAXCOL)
7904 {
7905 /* '> can be MAXCOL, get the length of the line then */
7906 if (fp->lnum <= curbuf->b_ml.ml_line_count)
7907 col = STRLEN(ml_get(fp->lnum)) + 1;
7908 else
7909 col = MAXCOL;
7910 }
7911 else
7912 {
7913 col = fp->col + 1;
7914#ifdef FEAT_VIRTUALEDIT
7915 /* col(".") when the cursor is on the NUL at the end of the line
7916 * because of "coladd" can be seen as an extra column. */
7917 if (virtual_active() && fp == &curwin->w_cursor)
7918 {
7919 char_u *p = ml_get_cursor();
7920
7921 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
7922 curwin->w_virtcol - curwin->w_cursor.coladd))
7923 {
7924# ifdef FEAT_MBYTE
7925 int l;
7926
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007927 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928 col += l;
7929# else
7930 if (*p != NUL && p[1] == NUL)
7931 ++col;
7932# endif
7933 }
7934 }
7935#endif
7936 }
7937 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007938 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939}
7940
Bram Moolenaar572cb562005-08-05 21:35:02 +00007941#if defined(FEAT_INS_EXPAND)
7942/*
7943 * "complete_add()" function
7944 */
7945/*ARGSUSED*/
7946 static void
7947f_complete_add(argvars, rettv)
7948 typval_T *argvars;
7949 typval_T *rettv;
7950{
7951 char_u *s;
7952
7953 s = get_tv_string_chk(&argvars[0]);
7954 if (s != NULL)
7955 rettv->vval.v_number = ins_compl_add(s, -1, NULL, FORWARD, 0);
7956}
7957
7958/*
7959 * "complete_check()" function
7960 */
7961/*ARGSUSED*/
7962 static void
7963f_complete_check(argvars, rettv)
7964 typval_T *argvars;
7965 typval_T *rettv;
7966{
7967 int saved = RedrawingDisabled;
7968
7969 RedrawingDisabled = 0;
7970 ins_compl_check_keys(0);
7971 rettv->vval.v_number = compl_interrupted;
7972 RedrawingDisabled = saved;
7973}
7974#endif
7975
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976/*
7977 * "confirm(message, buttons[, default [, type]])" function
7978 */
7979/*ARGSUSED*/
7980 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007981f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007982 typval_T *argvars;
7983 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984{
7985#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
7986 char_u *message;
7987 char_u *buttons = NULL;
7988 char_u buf[NUMBUFLEN];
7989 char_u buf2[NUMBUFLEN];
7990 int def = 1;
7991 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007992 char_u *typestr;
7993 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007995 message = get_tv_string_chk(&argvars[0]);
7996 if (message == NULL)
7997 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007998 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008000 buttons = get_tv_string_buf_chk(&argvars[1], buf);
8001 if (buttons == NULL)
8002 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008003 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008005 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008006 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008008 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
8009 if (typestr == NULL)
8010 error = TRUE;
8011 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008013 switch (TOUPPER_ASC(*typestr))
8014 {
8015 case 'E': type = VIM_ERROR; break;
8016 case 'Q': type = VIM_QUESTION; break;
8017 case 'I': type = VIM_INFO; break;
8018 case 'W': type = VIM_WARNING; break;
8019 case 'G': type = VIM_GENERIC; break;
8020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021 }
8022 }
8023 }
8024 }
8025
8026 if (buttons == NULL || *buttons == NUL)
8027 buttons = (char_u *)_("&Ok");
8028
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008029 if (error)
8030 rettv->vval.v_number = 0;
8031 else
8032 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033 def, NULL);
8034#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008035 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036#endif
8037}
8038
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008039/*
8040 * "copy()" function
8041 */
8042 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008043f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008044 typval_T *argvars;
8045 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008046{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008047 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008048}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049
8050/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008051 * "count()" function
8052 */
8053 static void
8054f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008055 typval_T *argvars;
8056 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008057{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008058 long n = 0;
8059 int ic = FALSE;
8060
Bram Moolenaare9a41262005-01-15 22:18:47 +00008061 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008062 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008063 listitem_T *li;
8064 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008065 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008066
Bram Moolenaare9a41262005-01-15 22:18:47 +00008067 if ((l = argvars[0].vval.v_list) != NULL)
8068 {
8069 li = l->lv_first;
8070 if (argvars[2].v_type != VAR_UNKNOWN)
8071 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008072 int error = FALSE;
8073
8074 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008075 if (argvars[3].v_type != VAR_UNKNOWN)
8076 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008077 idx = get_tv_number_chk(&argvars[3], &error);
8078 if (!error)
8079 {
8080 li = list_find(l, idx);
8081 if (li == NULL)
8082 EMSGN(_(e_listidx), idx);
8083 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008084 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008085 if (error)
8086 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008087 }
8088
8089 for ( ; li != NULL; li = li->li_next)
8090 if (tv_equal(&li->li_tv, &argvars[1], ic))
8091 ++n;
8092 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008093 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008094 else if (argvars[0].v_type == VAR_DICT)
8095 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008096 int todo;
8097 dict_T *d;
8098 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008099
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008100 if ((d = argvars[0].vval.v_dict) != NULL)
8101 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008102 int error = FALSE;
8103
Bram Moolenaare9a41262005-01-15 22:18:47 +00008104 if (argvars[2].v_type != VAR_UNKNOWN)
8105 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008106 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008107 if (argvars[3].v_type != VAR_UNKNOWN)
8108 EMSG(_(e_invarg));
8109 }
8110
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008111 todo = error ? 0 : d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008112 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008113 {
8114 if (!HASHITEM_EMPTY(hi))
8115 {
8116 --todo;
8117 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
8118 ++n;
8119 }
8120 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008121 }
8122 }
8123 else
8124 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008125 rettv->vval.v_number = n;
8126}
8127
8128/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8130 *
8131 * Checks the existence of a cscope connection.
8132 */
8133/*ARGSUSED*/
8134 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008135f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008136 typval_T *argvars;
8137 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138{
8139#ifdef FEAT_CSCOPE
8140 int num = 0;
8141 char_u *dbpath = NULL;
8142 char_u *prepend = NULL;
8143 char_u buf[NUMBUFLEN];
8144
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008145 if (argvars[0].v_type != VAR_UNKNOWN
8146 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008148 num = (int)get_tv_number(&argvars[0]);
8149 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008150 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008151 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152 }
8153
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008154 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008156 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157#endif
8158}
8159
8160/*
8161 * "cursor(lnum, col)" function
8162 *
8163 * Moves the cursor to the specified line and column
8164 */
8165/*ARGSUSED*/
8166 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008167f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008168 typval_T *argvars;
8169 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170{
8171 long line, col;
8172
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008173 line = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008174 col = get_tv_number_chk(&argvars[1], NULL);
8175 if (line < 0 || col < 0)
8176 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177 if (line > 0)
8178 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179 if (col > 0)
8180 curwin->w_cursor.col = col - 1;
8181#ifdef FEAT_VIRTUALEDIT
8182 curwin->w_cursor.coladd = 0;
8183#endif
8184
8185 /* Make sure the cursor is in a valid position. */
8186 check_cursor();
8187#ifdef FEAT_MBYTE
8188 /* Correct cursor for multi-byte character. */
8189 if (has_mbyte)
8190 mb_adjust_cursor();
8191#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008192
8193 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008194}
8195
8196/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008197 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 */
8199 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008200f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008201 typval_T *argvars;
8202 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008203{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008204 int noref = 0;
8205
8206 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008207 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008208 if (noref < 0 || noref > 1)
8209 EMSG(_(e_invarg));
8210 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008211 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212}
8213
8214/*
8215 * "delete()" function
8216 */
8217 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008218f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008219 typval_T *argvars;
8220 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221{
8222 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008223 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008225 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226}
8227
8228/*
8229 * "did_filetype()" function
8230 */
8231/*ARGSUSED*/
8232 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008233f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008234 typval_T *argvars;
8235 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236{
8237#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008238 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008240 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241#endif
8242}
8243
8244/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008245 * "diff_filler()" function
8246 */
8247/*ARGSUSED*/
8248 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008249f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008250 typval_T *argvars;
8251 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008252{
8253#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008254 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008255#endif
8256}
8257
8258/*
8259 * "diff_hlID()" function
8260 */
8261/*ARGSUSED*/
8262 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008263f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008264 typval_T *argvars;
8265 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008266{
8267#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008268 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008269 static linenr_T prev_lnum = 0;
8270 static int changedtick = 0;
8271 static int fnum = 0;
8272 static int change_start = 0;
8273 static int change_end = 0;
8274 static enum hlf_value hlID = 0;
8275 int filler_lines;
8276 int col;
8277
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008278 if (lnum < 0) /* ignore type error in {lnum} arg */
8279 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008280 if (lnum != prev_lnum
8281 || changedtick != curbuf->b_changedtick
8282 || fnum != curbuf->b_fnum)
8283 {
8284 /* New line, buffer, change: need to get the values. */
8285 filler_lines = diff_check(curwin, lnum);
8286 if (filler_lines < 0)
8287 {
8288 if (filler_lines == -1)
8289 {
8290 change_start = MAXCOL;
8291 change_end = -1;
8292 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8293 hlID = HLF_ADD; /* added line */
8294 else
8295 hlID = HLF_CHD; /* changed line */
8296 }
8297 else
8298 hlID = HLF_ADD; /* added line */
8299 }
8300 else
8301 hlID = (enum hlf_value)0;
8302 prev_lnum = lnum;
8303 changedtick = curbuf->b_changedtick;
8304 fnum = curbuf->b_fnum;
8305 }
8306
8307 if (hlID == HLF_CHD || hlID == HLF_TXD)
8308 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008309 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008310 if (col >= change_start && col <= change_end)
8311 hlID = HLF_TXD; /* changed text */
8312 else
8313 hlID = HLF_CHD; /* changed line */
8314 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008315 rettv->vval.v_number = hlID == (enum hlf_value)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008316#endif
8317}
8318
8319/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008320 * "empty({expr})" function
8321 */
8322 static void
8323f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008324 typval_T *argvars;
8325 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008326{
8327 int n;
8328
8329 switch (argvars[0].v_type)
8330 {
8331 case VAR_STRING:
8332 case VAR_FUNC:
8333 n = argvars[0].vval.v_string == NULL
8334 || *argvars[0].vval.v_string == NUL;
8335 break;
8336 case VAR_NUMBER:
8337 n = argvars[0].vval.v_number == 0;
8338 break;
8339 case VAR_LIST:
8340 n = argvars[0].vval.v_list == NULL
8341 || argvars[0].vval.v_list->lv_first == NULL;
8342 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008343 case VAR_DICT:
8344 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008345 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008346 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008347 default:
8348 EMSG2(_(e_intern2), "f_empty()");
8349 n = 0;
8350 }
8351
8352 rettv->vval.v_number = n;
8353}
8354
8355/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356 * "escape({string}, {chars})" function
8357 */
8358 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008359f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008360 typval_T *argvars;
8361 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362{
8363 char_u buf[NUMBUFLEN];
8364
Bram Moolenaar758711c2005-02-02 23:11:38 +00008365 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8366 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008367 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368}
8369
8370/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008371 * "eval()" function
8372 */
8373/*ARGSUSED*/
8374 static void
8375f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008376 typval_T *argvars;
8377 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008378{
8379 char_u *s;
8380
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008381 s = get_tv_string_chk(&argvars[0]);
8382 if (s != NULL)
8383 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008384
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008385 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8386 {
8387 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008388 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008389 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008390 else if (*s != NUL)
8391 EMSG(_(e_trailing));
8392}
8393
8394/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395 * "eventhandler()" function
8396 */
8397/*ARGSUSED*/
8398 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008399f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008400 typval_T *argvars;
8401 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008403 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404}
8405
8406/*
8407 * "executable()" function
8408 */
8409 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008410f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008411 typval_T *argvars;
8412 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008414 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008415}
8416
8417/*
8418 * "exists()" function
8419 */
8420 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008421f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008422 typval_T *argvars;
8423 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424{
8425 char_u *p;
8426 char_u *name;
8427 int n = FALSE;
8428 int len = 0;
8429
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008430 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431 if (*p == '$') /* environment variable */
8432 {
8433 /* first try "normal" environment variables (fast) */
8434 if (mch_getenv(p + 1) != NULL)
8435 n = TRUE;
8436 else
8437 {
8438 /* try expanding things like $VIM and ${HOME} */
8439 p = expand_env_save(p);
8440 if (p != NULL && *p != '$')
8441 n = TRUE;
8442 vim_free(p);
8443 }
8444 }
8445 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008446 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447 else if (*p == '*') /* internal or user defined function */
8448 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008449 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450 }
8451 else if (*p == ':')
8452 {
8453 n = cmd_exists(p + 1);
8454 }
8455 else if (*p == '#')
8456 {
8457#ifdef FEAT_AUTOCMD
8458 name = p + 1;
8459 p = vim_strchr(name, '#');
8460 if (p != NULL)
8461 n = au_exists(name, p, p + 1);
8462 else
8463 n = au_exists(name, name + STRLEN(name), NULL);
8464#endif
8465 }
8466 else /* internal variable */
8467 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008468 char_u *tofree;
8469 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008471 /* get_name_len() takes care of expanding curly braces */
8472 name = p;
8473 len = get_name_len(&p, &tofree, TRUE, FALSE);
8474 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008476 if (tofree != NULL)
8477 name = tofree;
8478 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8479 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008481 /* handle d.key, l[idx], f(expr) */
8482 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8483 if (n)
8484 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485 }
8486 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008488 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489 }
8490
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008491 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492}
8493
8494/*
8495 * "expand()" function
8496 */
8497 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008498f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008499 typval_T *argvars;
8500 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501{
8502 char_u *s;
8503 int len;
8504 char_u *errormsg;
8505 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8506 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008507 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008509 rettv->v_type = VAR_STRING;
8510 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511 if (*s == '%' || *s == '#' || *s == '<')
8512 {
8513 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008514 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515 --emsg_off;
8516 }
8517 else
8518 {
8519 /* When the optional second argument is non-zero, don't remove matches
8520 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008521 if (argvars[1].v_type != VAR_UNKNOWN
8522 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008524 if (!error)
8525 {
8526 ExpandInit(&xpc);
8527 xpc.xp_context = EXPAND_FILES;
8528 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
8529 ExpandCleanup(&xpc);
8530 }
8531 else
8532 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533 }
8534}
8535
8536/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008537 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00008538 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008539 */
8540 static void
8541f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008542 typval_T *argvars;
8543 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008544{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008545 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008546 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008547 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008548 list_T *l1, *l2;
8549 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008550 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008551 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008552
Bram Moolenaare9a41262005-01-15 22:18:47 +00008553 l1 = argvars[0].vval.v_list;
8554 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008555 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
8556 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008557 {
8558 if (argvars[2].v_type != VAR_UNKNOWN)
8559 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008560 before = get_tv_number_chk(&argvars[2], &error);
8561 if (error)
8562 return; /* type error; errmsg already given */
8563
Bram Moolenaar758711c2005-02-02 23:11:38 +00008564 if (before == l1->lv_len)
8565 item = NULL;
8566 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008567 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00008568 item = list_find(l1, before);
8569 if (item == NULL)
8570 {
8571 EMSGN(_(e_listidx), before);
8572 return;
8573 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008574 }
8575 }
8576 else
8577 item = NULL;
8578 list_extend(l1, l2, item);
8579
Bram Moolenaare9a41262005-01-15 22:18:47 +00008580 copy_tv(&argvars[0], rettv);
8581 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008582 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008583 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
8584 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008585 dict_T *d1, *d2;
8586 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008587 char_u *action;
8588 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00008589 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008590 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008591
8592 d1 = argvars[0].vval.v_dict;
8593 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008594 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
8595 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008596 {
8597 /* Check the third argument. */
8598 if (argvars[2].v_type != VAR_UNKNOWN)
8599 {
8600 static char *(av[]) = {"keep", "force", "error"};
8601
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008602 action = get_tv_string_chk(&argvars[2]);
8603 if (action == NULL)
8604 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008605 for (i = 0; i < 3; ++i)
8606 if (STRCMP(action, av[i]) == 0)
8607 break;
8608 if (i == 3)
8609 {
8610 EMSGN(_(e_invarg2), action);
8611 return;
8612 }
8613 }
8614 else
8615 action = (char_u *)"force";
8616
8617 /* Go over all entries in the second dict and add them to the
8618 * first dict. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008619 todo = d2->dv_hashtab.ht_used;
8620 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008621 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008622 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008623 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008624 --todo;
8625 di1 = dict_find(d1, hi2->hi_key, -1);
8626 if (di1 == NULL)
8627 {
8628 di1 = dictitem_copy(HI2DI(hi2));
8629 if (di1 != NULL && dict_add(d1, di1) == FAIL)
8630 dictitem_free(di1);
8631 }
8632 else if (*action == 'e')
8633 {
8634 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
8635 break;
8636 }
8637 else if (*action == 'f')
8638 {
8639 clear_tv(&di1->di_tv);
8640 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
8641 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008642 }
8643 }
8644
Bram Moolenaare9a41262005-01-15 22:18:47 +00008645 copy_tv(&argvars[0], rettv);
8646 }
8647 }
8648 else
8649 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008650}
8651
8652/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653 * "filereadable()" function
8654 */
8655 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008656f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008657 typval_T *argvars;
8658 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008659{
8660 FILE *fd;
8661 char_u *p;
8662 int n;
8663
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008664 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008665 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
8666 {
8667 n = TRUE;
8668 fclose(fd);
8669 }
8670 else
8671 n = FALSE;
8672
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008673 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674}
8675
8676/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008677 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678 * rights to write into.
8679 */
8680 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008681f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008682 typval_T *argvars;
8683 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008685 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686}
8687
Bram Moolenaar33570922005-01-25 22:26:29 +00008688static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008689
8690 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008691findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00008692 typval_T *argvars;
8693 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008694 int dir;
8695{
8696#ifdef FEAT_SEARCHPATH
8697 char_u *fname;
8698 char_u *fresult = NULL;
8699 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
8700 char_u *p;
8701 char_u pathbuf[NUMBUFLEN];
8702 int count = 1;
8703 int first = TRUE;
8704
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008705 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008706
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008707 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008708 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008709 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
8710 if (p == NULL)
8711 count = -1; /* error */
8712 else
8713 {
8714 if (*p != NUL)
8715 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008716
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008717 if (argvars[2].v_type != VAR_UNKNOWN)
8718 count = get_tv_number_chk(&argvars[2], NULL); /* -1: error */
8719 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008720 }
8721
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008722 if (*fname != NUL && count >= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008723 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008724 do
8725 {
8726 vim_free(fresult);
8727 fresult = find_file_in_path_option(first ? fname : NULL,
8728 first ? (int)STRLEN(fname) : 0,
8729 0, first, path, dir, NULL);
8730 first = FALSE;
8731 } while (--count > 0 && fresult != NULL);
8732 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008733
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008734 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008735#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008736 rettv->vval.v_string = NULL;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008737#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008738 rettv->v_type = VAR_STRING;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008739}
8740
Bram Moolenaar33570922005-01-25 22:26:29 +00008741static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
8742static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008743
8744/*
8745 * Implementation of map() and filter().
8746 */
8747 static void
8748filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00008749 typval_T *argvars;
8750 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008751 int map;
8752{
8753 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00008754 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00008755 listitem_T *li, *nli;
8756 list_T *l = NULL;
8757 dictitem_T *di;
8758 hashtab_T *ht;
8759 hashitem_T *hi;
8760 dict_T *d = NULL;
8761 typval_T save_val;
8762 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008763 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008764 int todo;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008765 char_u *msg = map ? (char_u *)"map()" : (char_u *)"filter()";
8766
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008767
8768 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008769 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008770 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008771 if ((l = argvars[0].vval.v_list) == NULL
8772 || (map && tv_check_lock(l->lv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008773 return;
8774 }
8775 else if (argvars[0].v_type == VAR_DICT)
8776 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008777 if ((d = argvars[0].vval.v_dict) == NULL
8778 || (map && tv_check_lock(d->dv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008779 return;
8780 }
8781 else
8782 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008783 EMSG2(_(e_listdictarg), msg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008784 return;
8785 }
8786
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008787 expr = get_tv_string_buf_chk(&argvars[1], buf);
8788 /* On type errors, the preceding call has already displayed an error
8789 * message. Avoid a misleading error message for an empty string that
8790 * was not passed as argument. */
8791 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008792 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008793 prepare_vimvar(VV_VAL, &save_val);
8794 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008795
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008796 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008797 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008798 prepare_vimvar(VV_KEY, &save_key);
8799 vimvars[VV_KEY].vv_type = VAR_STRING;
8800
8801 ht = &d->dv_hashtab;
8802 hash_lock(ht);
8803 todo = ht->ht_used;
8804 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008805 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008806 if (!HASHITEM_EMPTY(hi))
8807 {
8808 --todo;
8809 di = HI2DI(hi);
8810 if (tv_check_lock(di->di_tv.v_lock, msg))
8811 break;
8812 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
8813 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL)
8814 break;
8815 if (!map && rem)
8816 dictitem_remove(d, di);
8817 clear_tv(&vimvars[VV_KEY].vv_tv);
8818 }
8819 }
8820 hash_unlock(ht);
8821
8822 restore_vimvar(VV_KEY, &save_key);
8823 }
8824 else
8825 {
8826 for (li = l->lv_first; li != NULL; li = nli)
8827 {
8828 if (tv_check_lock(li->li_tv.v_lock, msg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008829 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008830 nli = li->li_next;
8831 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008832 break;
8833 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008834 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008835 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008836 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008837
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008838 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008839 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008840
8841 copy_tv(&argvars[0], rettv);
8842}
8843
8844 static int
8845filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00008846 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008847 char_u *expr;
8848 int map;
8849 int *remp;
8850{
Bram Moolenaar33570922005-01-25 22:26:29 +00008851 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008852 char_u *s;
8853
Bram Moolenaar33570922005-01-25 22:26:29 +00008854 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008855 s = expr;
8856 if (eval1(&s, &rettv, TRUE) == FAIL)
8857 return FAIL;
8858 if (*s != NUL) /* check for trailing chars after expr */
8859 {
8860 EMSG2(_(e_invexpr2), s);
8861 return FAIL;
8862 }
8863 if (map)
8864 {
8865 /* map(): replace the list item value */
8866 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +00008867 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008868 *tv = rettv;
8869 }
8870 else
8871 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008872 int error = FALSE;
8873
Bram Moolenaare9a41262005-01-15 22:18:47 +00008874 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008875 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008876 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008877 /* On type error, nothing has been removed; return FAIL to stop the
8878 * loop. The error message was given by get_tv_number_chk(). */
8879 if (error)
8880 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008881 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008882 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008883 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008884}
8885
8886/*
8887 * "filter()" function
8888 */
8889 static void
8890f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008891 typval_T *argvars;
8892 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008893{
8894 filter_map(argvars, rettv, FALSE);
8895}
8896
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008897/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008898 * "finddir({fname}[, {path}[, {count}]])" function
8899 */
8900 static void
8901f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008902 typval_T *argvars;
8903 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008904{
8905 findfilendir(argvars, rettv, TRUE);
8906}
8907
8908/*
8909 * "findfile({fname}[, {path}[, {count}]])" function
8910 */
8911 static void
8912f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008913 typval_T *argvars;
8914 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008915{
8916 findfilendir(argvars, rettv, FALSE);
8917}
8918
8919/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920 * "fnamemodify({fname}, {mods})" function
8921 */
8922 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008923f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008924 typval_T *argvars;
8925 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926{
8927 char_u *fname;
8928 char_u *mods;
8929 int usedlen = 0;
8930 int len;
8931 char_u *fbuf = NULL;
8932 char_u buf[NUMBUFLEN];
8933
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008934 fname = get_tv_string_chk(&argvars[0]);
8935 mods = get_tv_string_buf_chk(&argvars[1], buf);
8936 if (fname == NULL || mods == NULL)
8937 fname = NULL;
8938 else
8939 {
8940 len = (int)STRLEN(fname);
8941 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
8942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008944 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008946 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008948 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949 vim_free(fbuf);
8950}
8951
Bram Moolenaar33570922005-01-25 22:26:29 +00008952static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953
8954/*
8955 * "foldclosed()" function
8956 */
8957 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008958foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00008959 typval_T *argvars;
8960 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961 int end;
8962{
8963#ifdef FEAT_FOLDING
8964 linenr_T lnum;
8965 linenr_T first, last;
8966
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008967 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8969 {
8970 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
8971 {
8972 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008973 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008974 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008975 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976 return;
8977 }
8978 }
8979#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008980 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981}
8982
8983/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008984 * "foldclosed()" function
8985 */
8986 static void
8987f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008988 typval_T *argvars;
8989 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008990{
8991 foldclosed_both(argvars, rettv, FALSE);
8992}
8993
8994/*
8995 * "foldclosedend()" function
8996 */
8997 static void
8998f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008999 typval_T *argvars;
9000 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009001{
9002 foldclosed_both(argvars, rettv, TRUE);
9003}
9004
9005/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009006 * "foldlevel()" function
9007 */
9008 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009009f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009010 typval_T *argvars;
9011 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009012{
9013#ifdef FEAT_FOLDING
9014 linenr_T lnum;
9015
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009016 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009017 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009018 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019 else
9020#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009021 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009022}
9023
9024/*
9025 * "foldtext()" function
9026 */
9027/*ARGSUSED*/
9028 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009029f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009030 typval_T *argvars;
9031 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009032{
9033#ifdef FEAT_FOLDING
9034 linenr_T lnum;
9035 char_u *s;
9036 char_u *r;
9037 int len;
9038 char *txt;
9039#endif
9040
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009041 rettv->v_type = VAR_STRING;
9042 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009043#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00009044 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
9045 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
9046 <= curbuf->b_ml.ml_line_count
9047 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009048 {
9049 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009050 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
9051 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009052 {
9053 if (!linewhite(lnum))
9054 break;
9055 ++lnum;
9056 }
9057
9058 /* Find interesting text in this line. */
9059 s = skipwhite(ml_get(lnum));
9060 /* skip C comment-start */
9061 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009062 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009063 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009064 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00009065 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009066 {
9067 s = skipwhite(ml_get(lnum + 1));
9068 if (*s == '*')
9069 s = skipwhite(s + 1);
9070 }
9071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072 txt = _("+-%s%3ld lines: ");
9073 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009074 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009075 + 20 /* for %3ld */
9076 + STRLEN(s))); /* concatenated */
9077 if (r != NULL)
9078 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009079 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
9080 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
9081 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082 len = (int)STRLEN(r);
9083 STRCAT(r, s);
9084 /* remove 'foldmarker' and 'commentstring' */
9085 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009086 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009087 }
9088 }
9089#endif
9090}
9091
9092/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009093 * "foldtextresult(lnum)" function
9094 */
9095/*ARGSUSED*/
9096 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009097f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009098 typval_T *argvars;
9099 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009100{
9101#ifdef FEAT_FOLDING
9102 linenr_T lnum;
9103 char_u *text;
9104 char_u buf[51];
9105 foldinfo_T foldinfo;
9106 int fold_count;
9107#endif
9108
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009109 rettv->v_type = VAR_STRING;
9110 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009111#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009112 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009113 /* treat illegal types and illegal string values for {lnum} the same */
9114 if (lnum < 0)
9115 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009116 fold_count = foldedCount(curwin, lnum, &foldinfo);
9117 if (fold_count > 0)
9118 {
9119 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
9120 &foldinfo, buf);
9121 if (text == buf)
9122 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009123 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009124 }
9125#endif
9126}
9127
9128/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009129 * "foreground()" function
9130 */
9131/*ARGSUSED*/
9132 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009133f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009134 typval_T *argvars;
9135 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009136{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009137 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009138#ifdef FEAT_GUI
9139 if (gui.in_use)
9140 gui_mch_set_foreground();
9141#else
9142# ifdef WIN32
9143 win32_set_foreground();
9144# endif
9145#endif
9146}
9147
9148/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009149 * "function()" function
9150 */
9151/*ARGSUSED*/
9152 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009153f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009154 typval_T *argvars;
9155 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009156{
9157 char_u *s;
9158
Bram Moolenaara7043832005-01-21 11:56:39 +00009159 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009160 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009161 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009162 EMSG2(_(e_invarg2), s);
9163 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009164 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009165 else
9166 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009167 rettv->vval.v_string = vim_strsave(s);
9168 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009169 }
9170}
9171
9172/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009173 * "garbagecollect()" function
9174 */
9175/*ARGSUSED*/
9176 static void
9177f_garbagecollect(argvars, rettv)
9178 typval_T *argvars;
9179 typval_T *rettv;
9180{
9181 garbage_collect();
9182}
9183
9184/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009185 * "get()" function
9186 */
9187 static void
9188f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009189 typval_T *argvars;
9190 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009191{
Bram Moolenaar33570922005-01-25 22:26:29 +00009192 listitem_T *li;
9193 list_T *l;
9194 dictitem_T *di;
9195 dict_T *d;
9196 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009197
Bram Moolenaare9a41262005-01-15 22:18:47 +00009198 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009199 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009200 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009201 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009202 int error = FALSE;
9203
9204 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9205 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009206 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009207 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009208 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009209 else if (argvars[0].v_type == VAR_DICT)
9210 {
9211 if ((d = argvars[0].vval.v_dict) != NULL)
9212 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009213 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009214 if (di != NULL)
9215 tv = &di->di_tv;
9216 }
9217 }
9218 else
9219 EMSG2(_(e_listdictarg), "get()");
9220
9221 if (tv == NULL)
9222 {
9223 if (argvars[2].v_type == VAR_UNKNOWN)
9224 rettv->vval.v_number = 0;
9225 else
9226 copy_tv(&argvars[2], rettv);
9227 }
9228 else
9229 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009230}
9231
Bram Moolenaar342337a2005-07-21 21:11:17 +00009232static void get_buffer_lines __ARGS((buf_T *buf, linenr_T start, linenr_T end, int retlist, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009233
9234/*
9235 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +00009236 * Return a range (from start to end) of lines in rettv from the specified
9237 * buffer.
9238 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009239 */
9240 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +00009241get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009242 buf_T *buf;
9243 linenr_T start;
9244 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009245 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009246 typval_T *rettv;
9247{
9248 char_u *p;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009249 list_T *l = NULL;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009250
Bram Moolenaar342337a2005-07-21 21:11:17 +00009251 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009252 {
Bram Moolenaar342337a2005-07-21 21:11:17 +00009253 l = list_alloc();
9254 if (l == NULL)
9255 return;
9256
9257 rettv->vval.v_list = l;
9258 rettv->v_type = VAR_LIST;
9259 ++l->lv_refcount;
9260 }
9261 else
9262 rettv->vval.v_number = 0;
9263
9264 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
9265 return;
9266
9267 if (!retlist)
9268 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009269 if (start >= 1 && start <= buf->b_ml.ml_line_count)
9270 p = ml_get_buf(buf, start, FALSE);
9271 else
9272 p = (char_u *)"";
9273
9274 rettv->v_type = VAR_STRING;
9275 rettv->vval.v_string = vim_strsave(p);
9276 }
9277 else
9278 {
9279 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009280 return;
9281
9282 if (start < 1)
9283 start = 1;
9284 if (end > buf->b_ml.ml_line_count)
9285 end = buf->b_ml.ml_line_count;
9286 while (start <= end)
Bram Moolenaar4463f292005-09-25 22:20:24 +00009287 if (list_append_string(l, ml_get_buf(buf, start++, FALSE), -1)
9288 == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009289 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009290 }
9291}
9292
9293/*
9294 * "getbufline()" function
9295 */
9296 static void
9297f_getbufline(argvars, rettv)
9298 typval_T *argvars;
9299 typval_T *rettv;
9300{
9301 linenr_T lnum;
9302 linenr_T end;
9303 buf_T *buf;
9304
9305 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9306 ++emsg_off;
9307 buf = get_buf_tv(&argvars[0]);
9308 --emsg_off;
9309
Bram Moolenaar661b1822005-07-28 22:36:45 +00009310 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009311 if (argvars[2].v_type == VAR_UNKNOWN)
9312 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009313 else
Bram Moolenaar661b1822005-07-28 22:36:45 +00009314 end = get_tv_lnum_buf(&argvars[2], buf);
9315
Bram Moolenaar342337a2005-07-21 21:11:17 +00009316 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009317}
9318
Bram Moolenaar0d660222005-01-07 21:51:51 +00009319/*
9320 * "getbufvar()" function
9321 */
9322 static void
9323f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009324 typval_T *argvars;
9325 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009326{
9327 buf_T *buf;
9328 buf_T *save_curbuf;
9329 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009330 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009331
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009332 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9333 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009334 ++emsg_off;
9335 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009336
9337 rettv->v_type = VAR_STRING;
9338 rettv->vval.v_string = NULL;
9339
9340 if (buf != NULL && varname != NULL)
9341 {
9342 if (*varname == '&') /* buffer-local-option */
9343 {
9344 /* set curbuf to be our buf, temporarily */
9345 save_curbuf = curbuf;
9346 curbuf = buf;
9347
9348 get_option_tv(&varname, rettv, TRUE);
9349
9350 /* restore previous notion of curbuf */
9351 curbuf = save_curbuf;
9352 }
9353 else
9354 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009355 if (*varname == NUL)
9356 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9357 * scope prefix before the NUL byte is required by
9358 * find_var_in_ht(). */
9359 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009360 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009361 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009362 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009363 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009364 }
9365 }
9366
9367 --emsg_off;
9368}
9369
9370/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009371 * "getchar()" function
9372 */
9373 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009374f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009375 typval_T *argvars;
9376 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009377{
9378 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009379 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009380
9381 ++no_mapping;
9382 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009383 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009384 /* getchar(): blocking wait. */
9385 n = safe_vgetc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009386 else if (get_tv_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009387 /* getchar(1): only check if char avail */
9388 n = vpeekc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009389 else if (error || vpeekc() == NUL)
9390 /* illegal argument or getchar(0) and no char avail: return zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009391 n = 0;
9392 else
9393 /* getchar(0) and char avail: return char */
9394 n = safe_vgetc();
9395 --no_mapping;
9396 --allow_keys;
9397
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009398 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009399 if (IS_SPECIAL(n) || mod_mask != 0)
9400 {
9401 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9402 int i = 0;
9403
9404 /* Turn a special key into three bytes, plus modifier. */
9405 if (mod_mask != 0)
9406 {
9407 temp[i++] = K_SPECIAL;
9408 temp[i++] = KS_MODIFIER;
9409 temp[i++] = mod_mask;
9410 }
9411 if (IS_SPECIAL(n))
9412 {
9413 temp[i++] = K_SPECIAL;
9414 temp[i++] = K_SECOND(n);
9415 temp[i++] = K_THIRD(n);
9416 }
9417#ifdef FEAT_MBYTE
9418 else if (has_mbyte)
9419 i += (*mb_char2bytes)(n, temp + i);
9420#endif
9421 else
9422 temp[i++] = n;
9423 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009424 rettv->v_type = VAR_STRING;
9425 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009426 }
9427}
9428
9429/*
9430 * "getcharmod()" function
9431 */
9432/*ARGSUSED*/
9433 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009434f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009435 typval_T *argvars;
9436 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009437{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009438 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439}
9440
9441/*
9442 * "getcmdline()" function
9443 */
9444/*ARGSUSED*/
9445 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009446f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009447 typval_T *argvars;
9448 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009449{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009450 rettv->v_type = VAR_STRING;
9451 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009452}
9453
9454/*
9455 * "getcmdpos()" function
9456 */
9457/*ARGSUSED*/
9458 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009459f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009460 typval_T *argvars;
9461 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009462{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009463 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009464}
9465
9466/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00009467 * "getcmdtype()" function
9468 */
9469/*ARGSUSED*/
9470 static void
9471f_getcmdtype(argvars, rettv)
9472 typval_T *argvars;
9473 typval_T *rettv;
9474{
9475 rettv->v_type = VAR_STRING;
9476 rettv->vval.v_string = alloc(2);
9477 if (rettv->vval.v_string != NULL)
9478 {
9479 rettv->vval.v_string[0] = get_cmdline_type();
9480 rettv->vval.v_string[1] = NUL;
9481 }
9482}
9483
9484/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485 * "getcwd()" function
9486 */
9487/*ARGSUSED*/
9488 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009489f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009490 typval_T *argvars;
9491 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492{
9493 char_u cwd[MAXPATHL];
9494
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009495 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009496 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009497 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009498 else
9499 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009500 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +00009502 if (rettv->vval.v_string != NULL)
9503 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009504#endif
9505 }
9506}
9507
9508/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009509 * "getfontname()" function
9510 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009511/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009512 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009513f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009514 typval_T *argvars;
9515 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009516{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009517 rettv->v_type = VAR_STRING;
9518 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009519#ifdef FEAT_GUI
9520 if (gui.in_use)
9521 {
9522 GuiFont font;
9523 char_u *name = NULL;
9524
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009525 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009526 {
9527 /* Get the "Normal" font. Either the name saved by
9528 * hl_set_font_name() or from the font ID. */
9529 font = gui.norm_font;
9530 name = hl_get_font_name();
9531 }
9532 else
9533 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009534 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009535 if (STRCMP(name, "*") == 0) /* don't use font dialog */
9536 return;
9537 font = gui_mch_get_font(name, FALSE);
9538 if (font == NOFONT)
9539 return; /* Invalid font name, return empty string. */
9540 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009541 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009542 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009543 gui_mch_free_font(font);
9544 }
9545#endif
9546}
9547
9548/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009549 * "getfperm({fname})" function
9550 */
9551 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009552f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009553 typval_T *argvars;
9554 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009555{
9556 char_u *fname;
9557 struct stat st;
9558 char_u *perm = NULL;
9559 char_u flags[] = "rwx";
9560 int i;
9561
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009562 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009563
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009564 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009565 if (mch_stat((char *)fname, &st) >= 0)
9566 {
9567 perm = vim_strsave((char_u *)"---------");
9568 if (perm != NULL)
9569 {
9570 for (i = 0; i < 9; i++)
9571 {
9572 if (st.st_mode & (1 << (8 - i)))
9573 perm[i] = flags[i % 3];
9574 }
9575 }
9576 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009577 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009578}
9579
9580/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009581 * "getfsize({fname})" function
9582 */
9583 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009584f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009585 typval_T *argvars;
9586 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009587{
9588 char_u *fname;
9589 struct stat st;
9590
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009591 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009592
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009593 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009594
9595 if (mch_stat((char *)fname, &st) >= 0)
9596 {
9597 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009598 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009600 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601 }
9602 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009603 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009604}
9605
9606/*
9607 * "getftime({fname})" function
9608 */
9609 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009610f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009611 typval_T *argvars;
9612 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613{
9614 char_u *fname;
9615 struct stat st;
9616
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009617 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009618
9619 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009620 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009621 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009622 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623}
9624
9625/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009626 * "getftype({fname})" function
9627 */
9628 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009629f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009630 typval_T *argvars;
9631 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009632{
9633 char_u *fname;
9634 struct stat st;
9635 char_u *type = NULL;
9636 char *t;
9637
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009638 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009639
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009640 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009641 if (mch_lstat((char *)fname, &st) >= 0)
9642 {
9643#ifdef S_ISREG
9644 if (S_ISREG(st.st_mode))
9645 t = "file";
9646 else if (S_ISDIR(st.st_mode))
9647 t = "dir";
9648# ifdef S_ISLNK
9649 else if (S_ISLNK(st.st_mode))
9650 t = "link";
9651# endif
9652# ifdef S_ISBLK
9653 else if (S_ISBLK(st.st_mode))
9654 t = "bdev";
9655# endif
9656# ifdef S_ISCHR
9657 else if (S_ISCHR(st.st_mode))
9658 t = "cdev";
9659# endif
9660# ifdef S_ISFIFO
9661 else if (S_ISFIFO(st.st_mode))
9662 t = "fifo";
9663# endif
9664# ifdef S_ISSOCK
9665 else if (S_ISSOCK(st.st_mode))
9666 t = "fifo";
9667# endif
9668 else
9669 t = "other";
9670#else
9671# ifdef S_IFMT
9672 switch (st.st_mode & S_IFMT)
9673 {
9674 case S_IFREG: t = "file"; break;
9675 case S_IFDIR: t = "dir"; break;
9676# ifdef S_IFLNK
9677 case S_IFLNK: t = "link"; break;
9678# endif
9679# ifdef S_IFBLK
9680 case S_IFBLK: t = "bdev"; break;
9681# endif
9682# ifdef S_IFCHR
9683 case S_IFCHR: t = "cdev"; break;
9684# endif
9685# ifdef S_IFIFO
9686 case S_IFIFO: t = "fifo"; break;
9687# endif
9688# ifdef S_IFSOCK
9689 case S_IFSOCK: t = "socket"; break;
9690# endif
9691 default: t = "other";
9692 }
9693# else
9694 if (mch_isdir(fname))
9695 t = "dir";
9696 else
9697 t = "file";
9698# endif
9699#endif
9700 type = vim_strsave((char_u *)t);
9701 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009702 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009703}
9704
9705/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009706 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +00009707 */
9708 static void
9709f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009710 typval_T *argvars;
9711 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009712{
9713 linenr_T lnum;
9714 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009715 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009716
9717 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009718 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009719 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009720 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009721 retlist = FALSE;
9722 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009723 else
Bram Moolenaar342337a2005-07-21 21:11:17 +00009724 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009725 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009726 retlist = TRUE;
9727 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009728
Bram Moolenaar342337a2005-07-21 21:11:17 +00009729 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009730}
9731
9732/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00009733 * "getqflist()" function
9734 */
9735/*ARGSUSED*/
9736 static void
9737f_getqflist(argvars, rettv)
9738 typval_T *argvars;
9739 typval_T *rettv;
9740{
9741#ifdef FEAT_QUICKFIX
9742 list_T *l;
9743#endif
9744
9745 rettv->vval.v_number = FALSE;
9746#ifdef FEAT_QUICKFIX
9747 l = list_alloc();
9748 if (l != NULL)
9749 {
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00009750 rettv->vval.v_list = l;
9751 rettv->v_type = VAR_LIST;
9752 ++l->lv_refcount;
9753 (void)get_errorlist(l);
Bram Moolenaar2641f772005-03-25 21:58:17 +00009754 }
9755#endif
9756}
9757
9758/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009759 * "getreg()" function
9760 */
9761 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009762f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009763 typval_T *argvars;
9764 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009765{
9766 char_u *strregname;
9767 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009768 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009769 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009770
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009771 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009772 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009773 strregname = get_tv_string_chk(&argvars[0]);
9774 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009775 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009776 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009777 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009778 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009779 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780 regname = (strregname == NULL ? '"' : *strregname);
9781 if (regname == 0)
9782 regname = '"';
9783
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009784 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009785 rettv->vval.v_string = error ? NULL :
9786 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009787}
9788
9789/*
9790 * "getregtype()" function
9791 */
9792 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009793f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009794 typval_T *argvars;
9795 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009796{
9797 char_u *strregname;
9798 int regname;
9799 char_u buf[NUMBUFLEN + 2];
9800 long reglen = 0;
9801
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009802 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009803 {
9804 strregname = get_tv_string_chk(&argvars[0]);
9805 if (strregname == NULL) /* type error; errmsg already given */
9806 {
9807 rettv->v_type = VAR_STRING;
9808 rettv->vval.v_string = NULL;
9809 return;
9810 }
9811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009812 else
9813 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009814 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009815
9816 regname = (strregname == NULL ? '"' : *strregname);
9817 if (regname == 0)
9818 regname = '"';
9819
9820 buf[0] = NUL;
9821 buf[1] = NUL;
9822 switch (get_reg_type(regname, &reglen))
9823 {
9824 case MLINE: buf[0] = 'V'; break;
9825 case MCHAR: buf[0] = 'v'; break;
9826#ifdef FEAT_VISUAL
9827 case MBLOCK:
9828 buf[0] = Ctrl_V;
9829 sprintf((char *)buf + 1, "%ld", reglen + 1);
9830 break;
9831#endif
9832 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009833 rettv->v_type = VAR_STRING;
9834 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009835}
9836
9837/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009838 * "getwinposx()" function
9839 */
9840/*ARGSUSED*/
9841 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009842f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009843 typval_T *argvars;
9844 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009845{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009846 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009847#ifdef FEAT_GUI
9848 if (gui.in_use)
9849 {
9850 int x, y;
9851
9852 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009853 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009854 }
9855#endif
9856}
9857
9858/*
9859 * "getwinposy()" function
9860 */
9861/*ARGSUSED*/
9862 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009863f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009864 typval_T *argvars;
9865 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009867 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009868#ifdef FEAT_GUI
9869 if (gui.in_use)
9870 {
9871 int x, y;
9872
9873 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009874 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009875 }
9876#endif
9877}
9878
Bram Moolenaara40058a2005-07-11 22:42:07 +00009879static win_T *find_win_by_nr __ARGS((typval_T *vp));
9880
9881 static win_T *
9882find_win_by_nr(vp)
9883 typval_T *vp;
9884{
9885#ifdef FEAT_WINDOWS
9886 win_T *wp;
9887#endif
9888 int nr;
9889
9890 nr = get_tv_number_chk(vp, NULL);
9891
9892#ifdef FEAT_WINDOWS
9893 if (nr < 0)
9894 return NULL;
9895 if (nr == 0)
9896 return curwin;
9897
9898 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9899 if (--nr <= 0)
9900 break;
9901 return wp;
9902#else
9903 if (nr == 0 || nr == 1)
9904 return curwin;
9905 return NULL;
9906#endif
9907}
9908
Bram Moolenaar071d4272004-06-13 20:20:40 +00009909/*
9910 * "getwinvar()" function
9911 */
9912 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009913f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009914 typval_T *argvars;
9915 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009916{
9917 win_T *win, *oldcurwin;
9918 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009919 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009920
Bram Moolenaar071d4272004-06-13 20:20:40 +00009921 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009922 varname = get_tv_string_chk(&argvars[1]);
9923 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009925 rettv->v_type = VAR_STRING;
9926 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009927
9928 if (win != NULL && varname != NULL)
9929 {
9930 if (*varname == '&') /* window-local-option */
9931 {
Bram Moolenaarc0761132005-03-18 20:30:32 +00009932 /* Set curwin to be our win, temporarily. Also set curbuf, so
9933 * that we can get buffer-local options. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009934 oldcurwin = curwin;
9935 curwin = win;
Bram Moolenaarc0761132005-03-18 20:30:32 +00009936 curbuf = win->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009937
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009938 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009939
9940 /* restore previous notion of curwin */
9941 curwin = oldcurwin;
Bram Moolenaarc0761132005-03-18 20:30:32 +00009942 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009943 }
9944 else
9945 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009946 if (*varname == NUL)
9947 /* let getwinvar({nr}, "") return the "w:" dictionary. The
9948 * scope prefix before the NUL byte is required by
9949 * find_var_in_ht(). */
9950 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009951 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009952 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009953 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009954 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009955 }
9956 }
9957
9958 --emsg_off;
9959}
9960
9961/*
9962 * "glob()" function
9963 */
9964 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009965f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009966 typval_T *argvars;
9967 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009968{
9969 expand_T xpc;
9970
9971 ExpandInit(&xpc);
9972 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009973 rettv->v_type = VAR_STRING;
9974 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009975 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
9976 ExpandCleanup(&xpc);
9977}
9978
9979/*
9980 * "globpath()" function
9981 */
9982 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009983f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009984 typval_T *argvars;
9985 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009986{
9987 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009988 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009989
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009990 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009991 if (file == NULL)
9992 rettv->vval.v_string = NULL;
9993 else
9994 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009995}
9996
9997/*
9998 * "has()" function
9999 */
10000 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010001f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010002 typval_T *argvars;
10003 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010004{
10005 int i;
10006 char_u *name;
10007 int n = FALSE;
10008 static char *(has_list[]) =
10009 {
10010#ifdef AMIGA
10011 "amiga",
10012# ifdef FEAT_ARP
10013 "arp",
10014# endif
10015#endif
10016#ifdef __BEOS__
10017 "beos",
10018#endif
10019#ifdef MSDOS
10020# ifdef DJGPP
10021 "dos32",
10022# else
10023 "dos16",
10024# endif
10025#endif
10026#ifdef MACOS /* TODO: Should we add MACOS_CLASSIC, MACOS_X? (Dany) */
10027 "mac",
10028#endif
10029#if defined(MACOS_X_UNIX)
10030 "macunix",
10031#endif
10032#ifdef OS2
10033 "os2",
10034#endif
10035#ifdef __QNX__
10036 "qnx",
10037#endif
10038#ifdef RISCOS
10039 "riscos",
10040#endif
10041#ifdef UNIX
10042 "unix",
10043#endif
10044#ifdef VMS
10045 "vms",
10046#endif
10047#ifdef WIN16
10048 "win16",
10049#endif
10050#ifdef WIN32
10051 "win32",
10052#endif
10053#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
10054 "win32unix",
10055#endif
10056#ifdef WIN64
10057 "win64",
10058#endif
10059#ifdef EBCDIC
10060 "ebcdic",
10061#endif
10062#ifndef CASE_INSENSITIVE_FILENAME
10063 "fname_case",
10064#endif
10065#ifdef FEAT_ARABIC
10066 "arabic",
10067#endif
10068#ifdef FEAT_AUTOCMD
10069 "autocmd",
10070#endif
10071#ifdef FEAT_BEVAL
10072 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000010073# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
10074 "balloon_multiline",
10075# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010076#endif
10077#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
10078 "builtin_terms",
10079# ifdef ALL_BUILTIN_TCAPS
10080 "all_builtin_terms",
10081# endif
10082#endif
10083#ifdef FEAT_BYTEOFF
10084 "byte_offset",
10085#endif
10086#ifdef FEAT_CINDENT
10087 "cindent",
10088#endif
10089#ifdef FEAT_CLIENTSERVER
10090 "clientserver",
10091#endif
10092#ifdef FEAT_CLIPBOARD
10093 "clipboard",
10094#endif
10095#ifdef FEAT_CMDL_COMPL
10096 "cmdline_compl",
10097#endif
10098#ifdef FEAT_CMDHIST
10099 "cmdline_hist",
10100#endif
10101#ifdef FEAT_COMMENTS
10102 "comments",
10103#endif
10104#ifdef FEAT_CRYPT
10105 "cryptv",
10106#endif
10107#ifdef FEAT_CSCOPE
10108 "cscope",
10109#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010110#ifdef CURSOR_SHAPE
10111 "cursorshape",
10112#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010113#ifdef DEBUG
10114 "debug",
10115#endif
10116#ifdef FEAT_CON_DIALOG
10117 "dialog_con",
10118#endif
10119#ifdef FEAT_GUI_DIALOG
10120 "dialog_gui",
10121#endif
10122#ifdef FEAT_DIFF
10123 "diff",
10124#endif
10125#ifdef FEAT_DIGRAPHS
10126 "digraphs",
10127#endif
10128#ifdef FEAT_DND
10129 "dnd",
10130#endif
10131#ifdef FEAT_EMACS_TAGS
10132 "emacs_tags",
10133#endif
10134 "eval", /* always present, of course! */
10135#ifdef FEAT_EX_EXTRA
10136 "ex_extra",
10137#endif
10138#ifdef FEAT_SEARCH_EXTRA
10139 "extra_search",
10140#endif
10141#ifdef FEAT_FKMAP
10142 "farsi",
10143#endif
10144#ifdef FEAT_SEARCHPATH
10145 "file_in_path",
10146#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010147#if defined(UNIX) && !defined(USE_SYSTEM)
10148 "filterpipe",
10149#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010150#ifdef FEAT_FIND_ID
10151 "find_in_path",
10152#endif
10153#ifdef FEAT_FOLDING
10154 "folding",
10155#endif
10156#ifdef FEAT_FOOTER
10157 "footer",
10158#endif
10159#if !defined(USE_SYSTEM) && defined(UNIX)
10160 "fork",
10161#endif
10162#ifdef FEAT_GETTEXT
10163 "gettext",
10164#endif
10165#ifdef FEAT_GUI
10166 "gui",
10167#endif
10168#ifdef FEAT_GUI_ATHENA
10169# ifdef FEAT_GUI_NEXTAW
10170 "gui_neXtaw",
10171# else
10172 "gui_athena",
10173# endif
10174#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +000010175#ifdef FEAT_GUI_KDE
10176 "gui_kde",
10177#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010178#ifdef FEAT_GUI_GTK
10179 "gui_gtk",
10180# ifdef HAVE_GTK2
10181 "gui_gtk2",
10182# endif
10183#endif
10184#ifdef FEAT_GUI_MAC
10185 "gui_mac",
10186#endif
10187#ifdef FEAT_GUI_MOTIF
10188 "gui_motif",
10189#endif
10190#ifdef FEAT_GUI_PHOTON
10191 "gui_photon",
10192#endif
10193#ifdef FEAT_GUI_W16
10194 "gui_win16",
10195#endif
10196#ifdef FEAT_GUI_W32
10197 "gui_win32",
10198#endif
10199#ifdef FEAT_HANGULIN
10200 "hangul_input",
10201#endif
10202#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10203 "iconv",
10204#endif
10205#ifdef FEAT_INS_EXPAND
10206 "insert_expand",
10207#endif
10208#ifdef FEAT_JUMPLIST
10209 "jumplist",
10210#endif
10211#ifdef FEAT_KEYMAP
10212 "keymap",
10213#endif
10214#ifdef FEAT_LANGMAP
10215 "langmap",
10216#endif
10217#ifdef FEAT_LIBCALL
10218 "libcall",
10219#endif
10220#ifdef FEAT_LINEBREAK
10221 "linebreak",
10222#endif
10223#ifdef FEAT_LISP
10224 "lispindent",
10225#endif
10226#ifdef FEAT_LISTCMDS
10227 "listcmds",
10228#endif
10229#ifdef FEAT_LOCALMAP
10230 "localmap",
10231#endif
10232#ifdef FEAT_MENU
10233 "menu",
10234#endif
10235#ifdef FEAT_SESSION
10236 "mksession",
10237#endif
10238#ifdef FEAT_MODIFY_FNAME
10239 "modify_fname",
10240#endif
10241#ifdef FEAT_MOUSE
10242 "mouse",
10243#endif
10244#ifdef FEAT_MOUSESHAPE
10245 "mouseshape",
10246#endif
10247#if defined(UNIX) || defined(VMS)
10248# ifdef FEAT_MOUSE_DEC
10249 "mouse_dec",
10250# endif
10251# ifdef FEAT_MOUSE_GPM
10252 "mouse_gpm",
10253# endif
10254# ifdef FEAT_MOUSE_JSB
10255 "mouse_jsbterm",
10256# endif
10257# ifdef FEAT_MOUSE_NET
10258 "mouse_netterm",
10259# endif
10260# ifdef FEAT_MOUSE_PTERM
10261 "mouse_pterm",
10262# endif
10263# ifdef FEAT_MOUSE_XTERM
10264 "mouse_xterm",
10265# endif
10266#endif
10267#ifdef FEAT_MBYTE
10268 "multi_byte",
10269#endif
10270#ifdef FEAT_MBYTE_IME
10271 "multi_byte_ime",
10272#endif
10273#ifdef FEAT_MULTI_LANG
10274 "multi_lang",
10275#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010276#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010277#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010278 "mzscheme",
10279#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010280#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010281#ifdef FEAT_OLE
10282 "ole",
10283#endif
10284#ifdef FEAT_OSFILETYPE
10285 "osfiletype",
10286#endif
10287#ifdef FEAT_PATH_EXTRA
10288 "path_extra",
10289#endif
10290#ifdef FEAT_PERL
10291#ifndef DYNAMIC_PERL
10292 "perl",
10293#endif
10294#endif
10295#ifdef FEAT_PYTHON
10296#ifndef DYNAMIC_PYTHON
10297 "python",
10298#endif
10299#endif
10300#ifdef FEAT_POSTSCRIPT
10301 "postscript",
10302#endif
10303#ifdef FEAT_PRINTER
10304 "printer",
10305#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000010306#ifdef FEAT_PROFILE
10307 "profile",
10308#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010309#ifdef FEAT_QUICKFIX
10310 "quickfix",
10311#endif
10312#ifdef FEAT_RIGHTLEFT
10313 "rightleft",
10314#endif
10315#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
10316 "ruby",
10317#endif
10318#ifdef FEAT_SCROLLBIND
10319 "scrollbind",
10320#endif
10321#ifdef FEAT_CMDL_INFO
10322 "showcmd",
10323 "cmdline_info",
10324#endif
10325#ifdef FEAT_SIGNS
10326 "signs",
10327#endif
10328#ifdef FEAT_SMARTINDENT
10329 "smartindent",
10330#endif
10331#ifdef FEAT_SNIFF
10332 "sniff",
10333#endif
10334#ifdef FEAT_STL_OPT
10335 "statusline",
10336#endif
10337#ifdef FEAT_SUN_WORKSHOP
10338 "sun_workshop",
10339#endif
10340#ifdef FEAT_NETBEANS_INTG
10341 "netbeans_intg",
10342#endif
10343#ifdef FEAT_SYN_HL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010344 "spell",
10345#endif
10346#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010347 "syntax",
10348#endif
10349#if defined(USE_SYSTEM) || !defined(UNIX)
10350 "system",
10351#endif
10352#ifdef FEAT_TAG_BINS
10353 "tag_binary",
10354#endif
10355#ifdef FEAT_TAG_OLDSTATIC
10356 "tag_old_static",
10357#endif
10358#ifdef FEAT_TAG_ANYWHITE
10359 "tag_any_white",
10360#endif
10361#ifdef FEAT_TCL
10362# ifndef DYNAMIC_TCL
10363 "tcl",
10364# endif
10365#endif
10366#ifdef TERMINFO
10367 "terminfo",
10368#endif
10369#ifdef FEAT_TERMRESPONSE
10370 "termresponse",
10371#endif
10372#ifdef FEAT_TEXTOBJ
10373 "textobjects",
10374#endif
10375#ifdef HAVE_TGETENT
10376 "tgetent",
10377#endif
10378#ifdef FEAT_TITLE
10379 "title",
10380#endif
10381#ifdef FEAT_TOOLBAR
10382 "toolbar",
10383#endif
10384#ifdef FEAT_USR_CMDS
10385 "user-commands", /* was accidentally included in 5.4 */
10386 "user_commands",
10387#endif
10388#ifdef FEAT_VIMINFO
10389 "viminfo",
10390#endif
10391#ifdef FEAT_VERTSPLIT
10392 "vertsplit",
10393#endif
10394#ifdef FEAT_VIRTUALEDIT
10395 "virtualedit",
10396#endif
10397#ifdef FEAT_VISUAL
10398 "visual",
10399#endif
10400#ifdef FEAT_VISUALEXTRA
10401 "visualextra",
10402#endif
10403#ifdef FEAT_VREPLACE
10404 "vreplace",
10405#endif
10406#ifdef FEAT_WILDIGN
10407 "wildignore",
10408#endif
10409#ifdef FEAT_WILDMENU
10410 "wildmenu",
10411#endif
10412#ifdef FEAT_WINDOWS
10413 "windows",
10414#endif
10415#ifdef FEAT_WAK
10416 "winaltkeys",
10417#endif
10418#ifdef FEAT_WRITEBACKUP
10419 "writebackup",
10420#endif
10421#ifdef FEAT_XIM
10422 "xim",
10423#endif
10424#ifdef FEAT_XFONTSET
10425 "xfontset",
10426#endif
10427#ifdef USE_XSMP
10428 "xsmp",
10429#endif
10430#ifdef USE_XSMP_INTERACT
10431 "xsmp_interact",
10432#endif
10433#ifdef FEAT_XCLIPBOARD
10434 "xterm_clipboard",
10435#endif
10436#ifdef FEAT_XTERM_SAVE
10437 "xterm_save",
10438#endif
10439#if defined(UNIX) && defined(FEAT_X11)
10440 "X11",
10441#endif
10442 NULL
10443 };
10444
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010445 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010446 for (i = 0; has_list[i] != NULL; ++i)
10447 if (STRICMP(name, has_list[i]) == 0)
10448 {
10449 n = TRUE;
10450 break;
10451 }
10452
10453 if (n == FALSE)
10454 {
10455 if (STRNICMP(name, "patch", 5) == 0)
10456 n = has_patch(atoi((char *)name + 5));
10457 else if (STRICMP(name, "vim_starting") == 0)
10458 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010459#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
10460 else if (STRICMP(name, "balloon_multiline") == 0)
10461 n = multiline_balloon_available();
10462#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010463#ifdef DYNAMIC_TCL
10464 else if (STRICMP(name, "tcl") == 0)
10465 n = tcl_enabled(FALSE);
10466#endif
10467#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
10468 else if (STRICMP(name, "iconv") == 0)
10469 n = iconv_enabled(FALSE);
10470#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010471#ifdef DYNAMIC_MZSCHEME
10472 else if (STRICMP(name, "mzscheme") == 0)
10473 n = mzscheme_enabled(FALSE);
10474#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010475#ifdef DYNAMIC_RUBY
10476 else if (STRICMP(name, "ruby") == 0)
10477 n = ruby_enabled(FALSE);
10478#endif
10479#ifdef DYNAMIC_PYTHON
10480 else if (STRICMP(name, "python") == 0)
10481 n = python_enabled(FALSE);
10482#endif
10483#ifdef DYNAMIC_PERL
10484 else if (STRICMP(name, "perl") == 0)
10485 n = perl_enabled(FALSE);
10486#endif
10487#ifdef FEAT_GUI
10488 else if (STRICMP(name, "gui_running") == 0)
10489 n = (gui.in_use || gui.starting);
10490# ifdef FEAT_GUI_W32
10491 else if (STRICMP(name, "gui_win32s") == 0)
10492 n = gui_is_win32s();
10493# endif
10494# ifdef FEAT_BROWSE
10495 else if (STRICMP(name, "browse") == 0)
10496 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
10497# endif
10498#endif
10499#ifdef FEAT_SYN_HL
10500 else if (STRICMP(name, "syntax_items") == 0)
10501 n = syntax_present(curbuf);
10502#endif
10503#if defined(WIN3264)
10504 else if (STRICMP(name, "win95") == 0)
10505 n = mch_windows95();
10506#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000010507#ifdef FEAT_NETBEANS_INTG
10508 else if (STRICMP(name, "netbeans_enabled") == 0)
10509 n = usingNetbeans;
10510#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010511 }
10512
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010513 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010514}
10515
10516/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000010517 * "has_key()" function
10518 */
10519 static void
10520f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010521 typval_T *argvars;
10522 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010523{
10524 rettv->vval.v_number = 0;
10525 if (argvars[0].v_type != VAR_DICT)
10526 {
10527 EMSG(_(e_dictreq));
10528 return;
10529 }
10530 if (argvars[0].vval.v_dict == NULL)
10531 return;
10532
10533 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010534 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010535}
10536
10537/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010538 * "hasmapto()" function
10539 */
10540 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010541f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010542 typval_T *argvars;
10543 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010544{
10545 char_u *name;
10546 char_u *mode;
10547 char_u buf[NUMBUFLEN];
10548
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010549 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010550 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010551 mode = (char_u *)"nvo";
10552 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010553 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010554
10555 if (map_to_exists(name, mode))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010556 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010557 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010558 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010559}
10560
10561/*
10562 * "histadd()" function
10563 */
10564/*ARGSUSED*/
10565 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010566f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010567 typval_T *argvars;
10568 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010569{
10570#ifdef FEAT_CMDHIST
10571 int histype;
10572 char_u *str;
10573 char_u buf[NUMBUFLEN];
10574#endif
10575
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010576 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010577 if (check_restricted() || check_secure())
10578 return;
10579#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010580 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10581 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010582 if (histype >= 0)
10583 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010584 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010585 if (*str != NUL)
10586 {
10587 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010588 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010589 return;
10590 }
10591 }
10592#endif
10593}
10594
10595/*
10596 * "histdel()" function
10597 */
10598/*ARGSUSED*/
10599 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010600f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010601 typval_T *argvars;
10602 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010603{
10604#ifdef FEAT_CMDHIST
10605 int n;
10606 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010607 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010608
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010609 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10610 if (str == NULL)
10611 n = 0;
10612 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010613 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010614 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010615 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010616 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010617 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010618 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010619 else
10620 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010621 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010622 get_tv_string_buf(&argvars[1], buf));
10623 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010624#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010625 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010626#endif
10627}
10628
10629/*
10630 * "histget()" function
10631 */
10632/*ARGSUSED*/
10633 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010634f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010635 typval_T *argvars;
10636 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010637{
10638#ifdef FEAT_CMDHIST
10639 int type;
10640 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010641 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010642
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010643 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10644 if (str == NULL)
10645 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010646 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010647 {
10648 type = get_histtype(str);
10649 if (argvars[1].v_type == VAR_UNKNOWN)
10650 idx = get_history_idx(type);
10651 else
10652 idx = (int)get_tv_number_chk(&argvars[1], NULL);
10653 /* -1 on type error */
10654 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
10655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010656#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010657 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010658#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010659 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010660}
10661
10662/*
10663 * "histnr()" function
10664 */
10665/*ARGSUSED*/
10666 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010667f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010668 typval_T *argvars;
10669 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010670{
10671 int i;
10672
10673#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010674 char_u *history = get_tv_string_chk(&argvars[0]);
10675
10676 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010677 if (i >= HIST_CMD && i < HIST_COUNT)
10678 i = get_history_idx(i);
10679 else
10680#endif
10681 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010682 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010683}
10684
10685/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010686 * "highlightID(name)" function
10687 */
10688 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010689f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010690 typval_T *argvars;
10691 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010692{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010693 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010694}
10695
10696/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010697 * "highlight_exists()" function
10698 */
10699 static void
10700f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010701 typval_T *argvars;
10702 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010703{
10704 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
10705}
10706
10707/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010708 * "hostname()" function
10709 */
10710/*ARGSUSED*/
10711 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010712f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010713 typval_T *argvars;
10714 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010715{
10716 char_u hostname[256];
10717
10718 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010719 rettv->v_type = VAR_STRING;
10720 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010721}
10722
10723/*
10724 * iconv() function
10725 */
10726/*ARGSUSED*/
10727 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010728f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010729 typval_T *argvars;
10730 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010731{
10732#ifdef FEAT_MBYTE
10733 char_u buf1[NUMBUFLEN];
10734 char_u buf2[NUMBUFLEN];
10735 char_u *from, *to, *str;
10736 vimconv_T vimconv;
10737#endif
10738
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010739 rettv->v_type = VAR_STRING;
10740 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010741
10742#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010743 str = get_tv_string(&argvars[0]);
10744 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
10745 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010746 vimconv.vc_type = CONV_NONE;
10747 convert_setup(&vimconv, from, to);
10748
10749 /* If the encodings are equal, no conversion needed. */
10750 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010751 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010752 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010753 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010754
10755 convert_setup(&vimconv, NULL, NULL);
10756 vim_free(from);
10757 vim_free(to);
10758#endif
10759}
10760
10761/*
10762 * "indent()" function
10763 */
10764 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010765f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010766 typval_T *argvars;
10767 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010768{
10769 linenr_T lnum;
10770
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010771 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010772 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010773 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010774 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010775 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010776}
10777
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010778/*
10779 * "index()" function
10780 */
10781 static void
10782f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010783 typval_T *argvars;
10784 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010785{
Bram Moolenaar33570922005-01-25 22:26:29 +000010786 list_T *l;
10787 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010788 long idx = 0;
10789 int ic = FALSE;
10790
10791 rettv->vval.v_number = -1;
10792 if (argvars[0].v_type != VAR_LIST)
10793 {
10794 EMSG(_(e_listreq));
10795 return;
10796 }
10797 l = argvars[0].vval.v_list;
10798 if (l != NULL)
10799 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010800 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010801 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010802 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010803 int error = FALSE;
10804
Bram Moolenaar758711c2005-02-02 23:11:38 +000010805 /* Start at specified item. Use the cached index that list_find()
10806 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010807 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000010808 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010809 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010810 ic = get_tv_number_chk(&argvars[3], &error);
10811 if (error)
10812 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010813 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010814
Bram Moolenaar758711c2005-02-02 23:11:38 +000010815 for ( ; item != NULL; item = item->li_next, ++idx)
10816 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010817 {
10818 rettv->vval.v_number = idx;
10819 break;
10820 }
10821 }
10822}
10823
Bram Moolenaar071d4272004-06-13 20:20:40 +000010824static int inputsecret_flag = 0;
10825
10826/*
10827 * "input()" function
10828 * Also handles inputsecret() when inputsecret is set.
10829 */
10830 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010831f_input(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010832 typval_T *argvars;
10833 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010834{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010835 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010836 char_u *p = NULL;
10837 int c;
10838 char_u buf[NUMBUFLEN];
10839 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010840 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010841 int xp_type = EXPAND_NOTHING;
10842 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010843
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010844 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010845
10846#ifdef NO_CONSOLE_INPUT
10847 /* While starting up, there is no place to enter text. */
10848 if (no_console_input())
10849 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010850 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010851 return;
10852 }
10853#endif
10854
10855 cmd_silent = FALSE; /* Want to see the prompt. */
10856 if (prompt != NULL)
10857 {
10858 /* Only the part of the message after the last NL is considered as
10859 * prompt for the command line */
10860 p = vim_strrchr(prompt, '\n');
10861 if (p == NULL)
10862 p = prompt;
10863 else
10864 {
10865 ++p;
10866 c = *p;
10867 *p = NUL;
10868 msg_start();
10869 msg_clr_eos();
10870 msg_puts_attr(prompt, echo_attr);
10871 msg_didout = FALSE;
10872 msg_starthere();
10873 *p = c;
10874 }
10875 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010876
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010877 if (argvars[1].v_type != VAR_UNKNOWN)
10878 {
10879 defstr = get_tv_string_buf_chk(&argvars[1], buf);
10880 if (defstr != NULL)
10881 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010882
Bram Moolenaar4463f292005-09-25 22:20:24 +000010883 if (argvars[2].v_type != VAR_UNKNOWN)
10884 {
10885 char_u *xp_name;
10886 int xp_namelen;
10887 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010888
Bram Moolenaar4463f292005-09-25 22:20:24 +000010889 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010890
Bram Moolenaar4463f292005-09-25 22:20:24 +000010891 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
10892 if (xp_name == NULL)
10893 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010894
Bram Moolenaar4463f292005-09-25 22:20:24 +000010895 xp_namelen = STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010896
Bram Moolenaar4463f292005-09-25 22:20:24 +000010897 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
10898 &xp_arg) == FAIL)
10899 return;
10900 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010901 }
10902
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010903 if (defstr != NULL)
10904 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010905 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
10906 xp_type, xp_arg);
10907
10908 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010909
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010910 /* since the user typed this, no need to wait for return */
10911 need_wait_return = FALSE;
10912 msg_didout = FALSE;
10913 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010914 cmd_silent = cmd_silent_save;
10915}
10916
10917/*
10918 * "inputdialog()" function
10919 */
10920 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010921f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010922 typval_T *argvars;
10923 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010924{
10925#if defined(FEAT_GUI_TEXTDIALOG)
10926 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
10927 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
10928 {
10929 char_u *message;
10930 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010931 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000010932
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010933 message = get_tv_string_chk(&argvars[0]);
10934 if (argvars[1].v_type != VAR_UNKNOWN
10935 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000010936 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010937 else
10938 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010939 if (message != NULL && defstr != NULL
10940 && do_dialog(VIM_QUESTION, NULL, message,
10941 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010942 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010943 else
10944 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010945 if (message != NULL && defstr != NULL
10946 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010947 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010948 rettv->vval.v_string = vim_strsave(
10949 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010950 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010951 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010952 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010953 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010954 }
10955 else
10956#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010957 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010958}
10959
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000010960/*
10961 * "inputlist()" function
10962 */
10963 static void
10964f_inputlist(argvars, rettv)
10965 typval_T *argvars;
10966 typval_T *rettv;
10967{
10968 listitem_T *li;
10969 int selected;
10970 int mouse_used;
10971
10972 rettv->vval.v_number = 0;
10973#ifdef NO_CONSOLE_INPUT
10974 /* While starting up, there is no place to enter text. */
10975 if (no_console_input())
10976 return;
10977#endif
10978 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
10979 {
10980 EMSG2(_(e_listarg), "inputlist()");
10981 return;
10982 }
10983
10984 msg_start();
10985 lines_left = Rows; /* avoid more prompt */
10986 msg_scroll = TRUE;
10987 msg_clr_eos();
10988
10989 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
10990 {
10991 msg_puts(get_tv_string(&li->li_tv));
10992 msg_putchar('\n');
10993 }
10994
10995 /* Ask for choice. */
10996 selected = prompt_for_number(&mouse_used);
10997 if (mouse_used)
10998 selected -= lines_left;
10999
11000 rettv->vval.v_number = selected;
11001}
11002
11003
Bram Moolenaar071d4272004-06-13 20:20:40 +000011004static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
11005
11006/*
11007 * "inputrestore()" function
11008 */
11009/*ARGSUSED*/
11010 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011011f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011012 typval_T *argvars;
11013 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011014{
11015 if (ga_userinput.ga_len > 0)
11016 {
11017 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011018 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
11019 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011020 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011021 }
11022 else if (p_verbose > 1)
11023 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000011024 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011025 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011026 }
11027}
11028
11029/*
11030 * "inputsave()" function
11031 */
11032/*ARGSUSED*/
11033 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011034f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011035 typval_T *argvars;
11036 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011037{
11038 /* Add an entry to the stack of typehead storage. */
11039 if (ga_grow(&ga_userinput, 1) == OK)
11040 {
11041 save_typeahead((tasave_T *)(ga_userinput.ga_data)
11042 + ga_userinput.ga_len);
11043 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011044 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011045 }
11046 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011047 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011048}
11049
11050/*
11051 * "inputsecret()" function
11052 */
11053 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011054f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011055 typval_T *argvars;
11056 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011057{
11058 ++cmdline_star;
11059 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011060 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011061 --cmdline_star;
11062 --inputsecret_flag;
11063}
11064
11065/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011066 * "insert()" function
11067 */
11068 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011069f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011070 typval_T *argvars;
11071 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011072{
11073 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011074 listitem_T *item;
11075 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011076 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011077
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011078 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011079 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011080 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011081 else if ((l = argvars[0].vval.v_list) != NULL
11082 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011083 {
11084 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011085 before = get_tv_number_chk(&argvars[2], &error);
11086 if (error)
11087 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011088
Bram Moolenaar758711c2005-02-02 23:11:38 +000011089 if (before == l->lv_len)
11090 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011091 else
11092 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011093 item = list_find(l, before);
11094 if (item == NULL)
11095 {
11096 EMSGN(_(e_listidx), before);
11097 l = NULL;
11098 }
11099 }
11100 if (l != NULL)
11101 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011102 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011103 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011104 }
11105 }
11106}
11107
11108/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011109 * "isdirectory()" function
11110 */
11111 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011112f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011113 typval_T *argvars;
11114 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011115{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011116 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011117}
11118
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011119/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011120 * "islocked()" function
11121 */
11122 static void
11123f_islocked(argvars, rettv)
11124 typval_T *argvars;
11125 typval_T *rettv;
11126{
11127 lval_T lv;
11128 char_u *end;
11129 dictitem_T *di;
11130
11131 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000011132 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
11133 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011134 if (end != NULL && lv.ll_name != NULL)
11135 {
11136 if (*end != NUL)
11137 EMSG(_(e_trailing));
11138 else
11139 {
11140 if (lv.ll_tv == NULL)
11141 {
11142 if (check_changedtick(lv.ll_name))
11143 rettv->vval.v_number = 1; /* always locked */
11144 else
11145 {
11146 di = find_var(lv.ll_name, NULL);
11147 if (di != NULL)
11148 {
11149 /* Consider a variable locked when:
11150 * 1. the variable itself is locked
11151 * 2. the value of the variable is locked.
11152 * 3. the List or Dict value is locked.
11153 */
11154 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
11155 || tv_islocked(&di->di_tv));
11156 }
11157 }
11158 }
11159 else if (lv.ll_range)
11160 EMSG(_("E745: Range not allowed"));
11161 else if (lv.ll_newkey != NULL)
11162 EMSG2(_(e_dictkey), lv.ll_newkey);
11163 else if (lv.ll_list != NULL)
11164 /* List item. */
11165 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
11166 else
11167 /* Dictionary item. */
11168 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
11169 }
11170 }
11171
11172 clear_lval(&lv);
11173}
11174
Bram Moolenaar33570922005-01-25 22:26:29 +000011175static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011176
11177/*
11178 * Turn a dict into a list:
11179 * "what" == 0: list of keys
11180 * "what" == 1: list of values
11181 * "what" == 2: list of items
11182 */
11183 static void
11184dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000011185 typval_T *argvars;
11186 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011187 int what;
11188{
Bram Moolenaar33570922005-01-25 22:26:29 +000011189 list_T *l;
11190 list_T *l2;
11191 dictitem_T *di;
11192 hashitem_T *hi;
11193 listitem_T *li;
11194 listitem_T *li2;
11195 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011196 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011197
11198 rettv->vval.v_number = 0;
11199 if (argvars[0].v_type != VAR_DICT)
11200 {
11201 EMSG(_(e_dictreq));
11202 return;
11203 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011204 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011205 return;
11206
11207 l = list_alloc();
11208 if (l == NULL)
11209 return;
11210 rettv->v_type = VAR_LIST;
11211 rettv->vval.v_list = l;
11212 ++l->lv_refcount;
11213
Bram Moolenaar33570922005-01-25 22:26:29 +000011214 todo = d->dv_hashtab.ht_used;
11215 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011216 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011217 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011218 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011219 --todo;
11220 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011221
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011222 li = listitem_alloc();
11223 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011224 break;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011225 list_append(l, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011226
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011227 if (what == 0)
11228 {
11229 /* keys() */
11230 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011231 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011232 li->li_tv.vval.v_string = vim_strsave(di->di_key);
11233 }
11234 else if (what == 1)
11235 {
11236 /* values() */
11237 copy_tv(&di->di_tv, &li->li_tv);
11238 }
11239 else
11240 {
11241 /* items() */
11242 l2 = list_alloc();
11243 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011244 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011245 li->li_tv.vval.v_list = l2;
11246 if (l2 == NULL)
11247 break;
11248 ++l2->lv_refcount;
11249
11250 li2 = listitem_alloc();
11251 if (li2 == NULL)
11252 break;
11253 list_append(l2, li2);
11254 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011255 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011256 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
11257
11258 li2 = listitem_alloc();
11259 if (li2 == NULL)
11260 break;
11261 list_append(l2, li2);
11262 copy_tv(&di->di_tv, &li2->li_tv);
11263 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011264 }
11265 }
11266}
11267
11268/*
11269 * "items(dict)" function
11270 */
11271 static void
11272f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011273 typval_T *argvars;
11274 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011275{
11276 dict_list(argvars, rettv, 2);
11277}
11278
Bram Moolenaar071d4272004-06-13 20:20:40 +000011279/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011280 * "join()" function
11281 */
11282 static void
11283f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011284 typval_T *argvars;
11285 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011286{
11287 garray_T ga;
11288 char_u *sep;
11289
11290 rettv->vval.v_number = 0;
11291 if (argvars[0].v_type != VAR_LIST)
11292 {
11293 EMSG(_(e_listreq));
11294 return;
11295 }
11296 if (argvars[0].vval.v_list == NULL)
11297 return;
11298 if (argvars[1].v_type == VAR_UNKNOWN)
11299 sep = (char_u *)" ";
11300 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011301 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011302
11303 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011304
11305 if (sep != NULL)
11306 {
11307 ga_init2(&ga, (int)sizeof(char), 80);
11308 list_join(&ga, argvars[0].vval.v_list, sep, TRUE);
11309 ga_append(&ga, NUL);
11310 rettv->vval.v_string = (char_u *)ga.ga_data;
11311 }
11312 else
11313 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011314}
11315
11316/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011317 * "keys()" function
11318 */
11319 static void
11320f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011321 typval_T *argvars;
11322 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011323{
11324 dict_list(argvars, rettv, 0);
11325}
11326
11327/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011328 * "last_buffer_nr()" function.
11329 */
11330/*ARGSUSED*/
11331 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011332f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011333 typval_T *argvars;
11334 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011335{
11336 int n = 0;
11337 buf_T *buf;
11338
11339 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
11340 if (n < buf->b_fnum)
11341 n = buf->b_fnum;
11342
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011343 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011344}
11345
11346/*
11347 * "len()" function
11348 */
11349 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011350f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011351 typval_T *argvars;
11352 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011353{
11354 switch (argvars[0].v_type)
11355 {
11356 case VAR_STRING:
11357 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011358 rettv->vval.v_number = (varnumber_T)STRLEN(
11359 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011360 break;
11361 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011362 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011363 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011364 case VAR_DICT:
11365 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
11366 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011367 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011368 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011369 break;
11370 }
11371}
11372
Bram Moolenaar33570922005-01-25 22:26:29 +000011373static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011374
11375 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011376libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011377 typval_T *argvars;
11378 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011379 int type;
11380{
11381#ifdef FEAT_LIBCALL
11382 char_u *string_in;
11383 char_u **string_result;
11384 int nr_result;
11385#endif
11386
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011387 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011388 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011389 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011390 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011391 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011392
11393 if (check_restricted() || check_secure())
11394 return;
11395
11396#ifdef FEAT_LIBCALL
11397 /* The first two args must be strings, otherwise its meaningless */
11398 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
11399 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011400 string_in = NULL;
11401 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011402 string_in = argvars[2].vval.v_string;
11403 if (type == VAR_NUMBER)
11404 string_result = NULL;
11405 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011406 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011407 if (mch_libcall(argvars[0].vval.v_string,
11408 argvars[1].vval.v_string,
11409 string_in,
11410 argvars[2].vval.v_number,
11411 string_result,
11412 &nr_result) == OK
11413 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011414 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011415 }
11416#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011417}
11418
11419/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011420 * "libcall()" function
11421 */
11422 static void
11423f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011424 typval_T *argvars;
11425 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011426{
11427 libcall_common(argvars, rettv, VAR_STRING);
11428}
11429
11430/*
11431 * "libcallnr()" function
11432 */
11433 static void
11434f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011435 typval_T *argvars;
11436 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011437{
11438 libcall_common(argvars, rettv, VAR_NUMBER);
11439}
11440
11441/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011442 * "line(string)" function
11443 */
11444 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011445f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011446 typval_T *argvars;
11447 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011448{
11449 linenr_T lnum = 0;
11450 pos_T *fp;
11451
11452 fp = var2fpos(&argvars[0], TRUE);
11453 if (fp != NULL)
11454 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011455 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011456}
11457
11458/*
11459 * "line2byte(lnum)" function
11460 */
11461/*ARGSUSED*/
11462 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011463f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011464 typval_T *argvars;
11465 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011466{
11467#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011468 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011469#else
11470 linenr_T lnum;
11471
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011472 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011473 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011474 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011475 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011476 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
11477 if (rettv->vval.v_number >= 0)
11478 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011479#endif
11480}
11481
11482/*
11483 * "lispindent(lnum)" function
11484 */
11485 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011486f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011487 typval_T *argvars;
11488 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011489{
11490#ifdef FEAT_LISP
11491 pos_T pos;
11492 linenr_T lnum;
11493
11494 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011495 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011496 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11497 {
11498 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011499 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011500 curwin->w_cursor = pos;
11501 }
11502 else
11503#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011504 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011505}
11506
11507/*
11508 * "localtime()" function
11509 */
11510/*ARGSUSED*/
11511 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011512f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011513 typval_T *argvars;
11514 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011515{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011516 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011517}
11518
Bram Moolenaar33570922005-01-25 22:26:29 +000011519static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011520
11521 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011522get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000011523 typval_T *argvars;
11524 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011525 int exact;
11526{
11527 char_u *keys;
11528 char_u *which;
11529 char_u buf[NUMBUFLEN];
11530 char_u *keys_buf = NULL;
11531 char_u *rhs;
11532 int mode;
11533 garray_T ga;
11534
11535 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011536 rettv->v_type = VAR_STRING;
11537 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011538
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011539 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011540 if (*keys == NUL)
11541 return;
11542
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011543 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011544 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011545 else
11546 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011547 if (which == NULL)
11548 return;
11549
Bram Moolenaar071d4272004-06-13 20:20:40 +000011550 mode = get_map_mode(&which, 0);
11551
11552 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000011553 rhs = check_map(keys, mode, exact, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011554 vim_free(keys_buf);
11555 if (rhs != NULL)
11556 {
11557 ga_init(&ga);
11558 ga.ga_itemsize = 1;
11559 ga.ga_growsize = 40;
11560
11561 while (*rhs != NUL)
11562 ga_concat(&ga, str2special(&rhs, FALSE));
11563
11564 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011565 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011566 }
11567}
11568
11569/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011570 * "map()" function
11571 */
11572 static void
11573f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011574 typval_T *argvars;
11575 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011576{
11577 filter_map(argvars, rettv, TRUE);
11578}
11579
11580/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011581 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011582 */
11583 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011584f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011585 typval_T *argvars;
11586 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011587{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011588 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011589}
11590
11591/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011592 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011593 */
11594 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011595f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011596 typval_T *argvars;
11597 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011598{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011599 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011600}
11601
Bram Moolenaar33570922005-01-25 22:26:29 +000011602static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011603
11604 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011605find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011606 typval_T *argvars;
11607 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011608 int type;
11609{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011610 char_u *str = NULL;
11611 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011612 char_u *pat;
11613 regmatch_T regmatch;
11614 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011615 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011616 char_u *save_cpo;
11617 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011618 long nth = 1;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011619 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011620 list_T *l = NULL;
11621 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011622 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011623 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011624
11625 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11626 save_cpo = p_cpo;
11627 p_cpo = (char_u *)"";
11628
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011629 rettv->vval.v_number = -1;
11630 if (type == 3)
11631 {
11632 /* return empty list when there are no matches */
11633 if ((rettv->vval.v_list = list_alloc()) == NULL)
11634 goto theend;
11635 rettv->v_type = VAR_LIST;
11636 ++rettv->vval.v_list->lv_refcount;
11637 }
11638 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011639 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011640 rettv->v_type = VAR_STRING;
11641 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011643
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011644 if (argvars[0].v_type == VAR_LIST)
11645 {
11646 if ((l = argvars[0].vval.v_list) == NULL)
11647 goto theend;
11648 li = l->lv_first;
11649 }
11650 else
11651 expr = str = get_tv_string(&argvars[0]);
11652
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011653 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11654 if (pat == NULL)
11655 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011656
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011657 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011658 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011659 int error = FALSE;
11660
11661 start = get_tv_number_chk(&argvars[2], &error);
11662 if (error)
11663 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011664 if (l != NULL)
11665 {
11666 li = list_find(l, start);
11667 if (li == NULL)
11668 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000011669 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011670 }
11671 else
11672 {
11673 if (start < 0)
11674 start = 0;
11675 if (start > (long)STRLEN(str))
11676 goto theend;
11677 str += start;
11678 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011679
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011680 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011681 nth = get_tv_number_chk(&argvars[3], &error);
11682 if (error)
11683 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011684 }
11685
11686 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11687 if (regmatch.regprog != NULL)
11688 {
11689 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011690
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000011691 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011692 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011693 if (l != NULL)
11694 {
11695 if (li == NULL)
11696 {
11697 match = FALSE;
11698 break;
11699 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011700 vim_free(tofree);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011701 str = echo_string(&li->li_tv, &tofree, strbuf);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011702 if (str == NULL)
11703 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011704 }
11705
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011706 match = vim_regexec_nl(&regmatch, str, (colnr_T)0);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011707
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011708 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011709 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011710 if (l == NULL && !match)
11711 break;
11712
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011713 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011714 if (l != NULL)
11715 {
11716 li = li->li_next;
11717 ++idx;
11718 }
11719 else
11720 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011721#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011722 str = regmatch.startp[0] + (*mb_ptr2len)(regmatch.startp[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011723#else
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011724 str = regmatch.startp[0] + 1;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011725#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011726 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011727 }
11728
11729 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011730 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011731 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011732 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011733 int i;
11734
11735 /* return list with matched string and submatches */
11736 for (i = 0; i < NSUBEXP; ++i)
11737 {
11738 if (regmatch.endp[i] == NULL)
11739 break;
Bram Moolenaar4463f292005-09-25 22:20:24 +000011740 if (list_append_string(rettv->vval.v_list,
11741 regmatch.startp[i],
11742 (int)(regmatch.endp[i] - regmatch.startp[i]))
11743 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011744 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011745 }
11746 }
11747 else if (type == 2)
11748 {
11749 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011750 if (l != NULL)
11751 copy_tv(&li->li_tv, rettv);
11752 else
11753 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000011754 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011755 }
11756 else if (l != NULL)
11757 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011758 else
11759 {
11760 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011761 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011762 (varnumber_T)(regmatch.startp[0] - str);
11763 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011764 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011765 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011766 rettv->vval.v_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011767 }
11768 }
11769 vim_free(regmatch.regprog);
11770 }
11771
11772theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011773 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011774 p_cpo = save_cpo;
11775}
11776
11777/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011778 * "match()" function
11779 */
11780 static void
11781f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011782 typval_T *argvars;
11783 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011784{
11785 find_some_match(argvars, rettv, 1);
11786}
11787
11788/*
11789 * "matchend()" function
11790 */
11791 static void
11792f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011793 typval_T *argvars;
11794 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011795{
11796 find_some_match(argvars, rettv, 0);
11797}
11798
11799/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011800 * "matchlist()" function
11801 */
11802 static void
11803f_matchlist(argvars, rettv)
11804 typval_T *argvars;
11805 typval_T *rettv;
11806{
11807 find_some_match(argvars, rettv, 3);
11808}
11809
11810/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011811 * "matchstr()" function
11812 */
11813 static void
11814f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011815 typval_T *argvars;
11816 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011817{
11818 find_some_match(argvars, rettv, 2);
11819}
11820
Bram Moolenaar33570922005-01-25 22:26:29 +000011821static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011822
11823 static void
11824max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000011825 typval_T *argvars;
11826 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011827 int domax;
11828{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011829 long n = 0;
11830 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011831 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011832
11833 if (argvars[0].v_type == VAR_LIST)
11834 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011835 list_T *l;
11836 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011837
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011838 l = argvars[0].vval.v_list;
11839 if (l != NULL)
11840 {
11841 li = l->lv_first;
11842 if (li != NULL)
11843 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011844 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000011845 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011846 {
11847 li = li->li_next;
11848 if (li == NULL)
11849 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011850 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011851 if (domax ? i > n : i < n)
11852 n = i;
11853 }
11854 }
11855 }
11856 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011857 else if (argvars[0].v_type == VAR_DICT)
11858 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011859 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011860 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000011861 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011862 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011863
11864 d = argvars[0].vval.v_dict;
11865 if (d != NULL)
11866 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011867 todo = d->dv_hashtab.ht_used;
11868 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011869 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011870 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011871 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011872 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011873 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011874 if (first)
11875 {
11876 n = i;
11877 first = FALSE;
11878 }
11879 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011880 n = i;
11881 }
11882 }
11883 }
11884 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011885 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000011886 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011887 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011888}
11889
11890/*
11891 * "max()" function
11892 */
11893 static void
11894f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011895 typval_T *argvars;
11896 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011897{
11898 max_min(argvars, rettv, TRUE);
11899}
11900
11901/*
11902 * "min()" function
11903 */
11904 static void
11905f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011906 typval_T *argvars;
11907 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011908{
11909 max_min(argvars, rettv, FALSE);
11910}
11911
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011912static int mkdir_recurse __ARGS((char_u *dir, int prot));
11913
11914/*
11915 * Create the directory in which "dir" is located, and higher levels when
11916 * needed.
11917 */
11918 static int
11919mkdir_recurse(dir, prot)
11920 char_u *dir;
11921 int prot;
11922{
11923 char_u *p;
11924 char_u *updir;
11925 int r = FAIL;
11926
11927 /* Get end of directory name in "dir".
11928 * We're done when it's "/" or "c:/". */
11929 p = gettail_sep(dir);
11930 if (p <= get_past_head(dir))
11931 return OK;
11932
11933 /* If the directory exists we're done. Otherwise: create it.*/
11934 updir = vim_strnsave(dir, (int)(p - dir));
11935 if (updir == NULL)
11936 return FAIL;
11937 if (mch_isdir(updir))
11938 r = OK;
11939 else if (mkdir_recurse(updir, prot) == OK)
11940 r = vim_mkdir_emsg(updir, prot);
11941 vim_free(updir);
11942 return r;
11943}
11944
11945#ifdef vim_mkdir
11946/*
11947 * "mkdir()" function
11948 */
11949 static void
11950f_mkdir(argvars, rettv)
11951 typval_T *argvars;
11952 typval_T *rettv;
11953{
11954 char_u *dir;
11955 char_u buf[NUMBUFLEN];
11956 int prot = 0755;
11957
11958 rettv->vval.v_number = FAIL;
11959 if (check_restricted() || check_secure())
11960 return;
11961
11962 dir = get_tv_string_buf(&argvars[0], buf);
11963 if (argvars[1].v_type != VAR_UNKNOWN)
11964 {
11965 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011966 prot = get_tv_number_chk(&argvars[2], NULL);
11967 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011968 mkdir_recurse(dir, prot);
11969 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011970 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011971}
11972#endif
11973
Bram Moolenaar0d660222005-01-07 21:51:51 +000011974/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011975 * "mode()" function
11976 */
11977/*ARGSUSED*/
11978 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011979f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011980 typval_T *argvars;
11981 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011982{
11983 char_u buf[2];
11984
11985#ifdef FEAT_VISUAL
11986 if (VIsual_active)
11987 {
11988 if (VIsual_select)
11989 buf[0] = VIsual_mode + 's' - 'v';
11990 else
11991 buf[0] = VIsual_mode;
11992 }
11993 else
11994#endif
11995 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
11996 buf[0] = 'r';
11997 else if (State & INSERT)
11998 {
11999 if (State & REPLACE_FLAG)
12000 buf[0] = 'R';
12001 else
12002 buf[0] = 'i';
12003 }
12004 else if (State & CMDLINE)
12005 buf[0] = 'c';
12006 else
12007 buf[0] = 'n';
12008
12009 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012010 rettv->vval.v_string = vim_strsave(buf);
12011 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012012}
12013
12014/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012015 * "nextnonblank()" function
12016 */
12017 static void
12018f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012019 typval_T *argvars;
12020 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012021{
12022 linenr_T lnum;
12023
12024 for (lnum = get_tv_lnum(argvars); ; ++lnum)
12025 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012026 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012027 {
12028 lnum = 0;
12029 break;
12030 }
12031 if (*skipwhite(ml_get(lnum)) != NUL)
12032 break;
12033 }
12034 rettv->vval.v_number = lnum;
12035}
12036
12037/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012038 * "nr2char()" function
12039 */
12040 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012041f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012042 typval_T *argvars;
12043 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012044{
12045 char_u buf[NUMBUFLEN];
12046
12047#ifdef FEAT_MBYTE
12048 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012049 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012050 else
12051#endif
12052 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012053 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012054 buf[1] = NUL;
12055 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012056 rettv->v_type = VAR_STRING;
12057 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012058}
12059
12060/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012061 * "prevnonblank()" function
12062 */
12063 static void
12064f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012065 typval_T *argvars;
12066 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012067{
12068 linenr_T lnum;
12069
12070 lnum = get_tv_lnum(argvars);
12071 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
12072 lnum = 0;
12073 else
12074 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
12075 --lnum;
12076 rettv->vval.v_number = lnum;
12077}
12078
Bram Moolenaara6c840d2005-08-22 22:59:46 +000012079#ifdef HAVE_STDARG_H
12080/* This dummy va_list is here because:
12081 * - passing a NULL pointer doesn't work when va_list isn't a pointer
12082 * - locally in the function results in a "used before set" warning
12083 * - using va_start() to initialize it gives "function with fixed args" error */
12084static va_list ap;
12085#endif
12086
Bram Moolenaar8c711452005-01-14 21:53:12 +000012087/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012088 * "printf()" function
12089 */
12090 static void
12091f_printf(argvars, rettv)
12092 typval_T *argvars;
12093 typval_T *rettv;
12094{
12095 rettv->v_type = VAR_STRING;
12096 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000012097#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012098 {
12099 char_u buf[NUMBUFLEN];
12100 int len;
12101 char_u *s;
12102 int saved_did_emsg = did_emsg;
12103 char *fmt;
12104
12105 /* Get the required length, allocate the buffer and do it for real. */
12106 did_emsg = FALSE;
12107 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012108 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012109 if (!did_emsg)
12110 {
12111 s = alloc(len + 1);
12112 if (s != NULL)
12113 {
12114 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012115 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012116 }
12117 }
12118 did_emsg |= saved_did_emsg;
12119 }
12120#endif
12121}
12122
12123/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012124 * "range()" function
12125 */
12126 static void
12127f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012128 typval_T *argvars;
12129 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012130{
12131 long start;
12132 long end;
12133 long stride = 1;
12134 long i;
Bram Moolenaar33570922005-01-25 22:26:29 +000012135 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012136 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012137
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012138 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012139 if (argvars[1].v_type == VAR_UNKNOWN)
12140 {
12141 end = start - 1;
12142 start = 0;
12143 }
12144 else
12145 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012146 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012147 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012148 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012149 }
12150
12151 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012152 if (error)
12153 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000012154 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012155 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000012156 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012157 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012158 else
12159 {
12160 l = list_alloc();
12161 if (l != NULL)
12162 {
12163 rettv->v_type = VAR_LIST;
12164 rettv->vval.v_list = l;
12165 ++l->lv_refcount;
12166
12167 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012168 if (list_append_number(l, (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012169 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012170 }
12171 }
12172}
12173
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012174/*
12175 * "readfile()" function
12176 */
12177 static void
12178f_readfile(argvars, rettv)
12179 typval_T *argvars;
12180 typval_T *rettv;
12181{
12182 int binary = FALSE;
12183 char_u *fname;
12184 FILE *fd;
12185 list_T *l;
12186 listitem_T *li;
12187#define FREAD_SIZE 200 /* optimized for text lines */
12188 char_u buf[FREAD_SIZE];
12189 int readlen; /* size of last fread() */
12190 int buflen; /* nr of valid chars in buf[] */
12191 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
12192 int tolist; /* first byte in buf[] still to be put in list */
12193 int chop; /* how many CR to chop off */
12194 char_u *prev = NULL; /* previously read bytes, if any */
12195 int prevlen = 0; /* length of "prev" if not NULL */
12196 char_u *s;
12197 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012198 long maxline = MAXLNUM;
12199 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012200
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012201 if (argvars[1].v_type != VAR_UNKNOWN)
12202 {
12203 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
12204 binary = TRUE;
12205 if (argvars[2].v_type != VAR_UNKNOWN)
12206 maxline = get_tv_number(&argvars[2]);
12207 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012208
12209 l = list_alloc();
12210 if (l == NULL)
12211 return;
12212 rettv->v_type = VAR_LIST;
12213 rettv->vval.v_list = l;
12214 l->lv_refcount = 1;
12215
12216 /* Always open the file in binary mode, library functions have a mind of
12217 * their own about CR-LF conversion. */
12218 fname = get_tv_string(&argvars[0]);
12219 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
12220 {
12221 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
12222 return;
12223 }
12224
12225 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012226 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012227 {
12228 readlen = fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
12229 buflen = filtd + readlen;
12230 tolist = 0;
12231 for ( ; filtd < buflen || readlen <= 0; ++filtd)
12232 {
12233 if (buf[filtd] == '\n' || readlen <= 0)
12234 {
12235 /* Only when in binary mode add an empty list item when the
12236 * last line ends in a '\n'. */
12237 if (!binary && readlen == 0 && filtd == 0)
12238 break;
12239
12240 /* Found end-of-line or end-of-file: add a text line to the
12241 * list. */
12242 chop = 0;
12243 if (!binary)
12244 while (filtd - chop - 1 >= tolist
12245 && buf[filtd - chop - 1] == '\r')
12246 ++chop;
12247 len = filtd - tolist - chop;
12248 if (prev == NULL)
12249 s = vim_strnsave(buf + tolist, len);
12250 else
12251 {
12252 s = alloc((unsigned)(prevlen + len + 1));
12253 if (s != NULL)
12254 {
12255 mch_memmove(s, prev, prevlen);
12256 vim_free(prev);
12257 prev = NULL;
12258 mch_memmove(s + prevlen, buf + tolist, len);
12259 s[prevlen + len] = NUL;
12260 }
12261 }
12262 tolist = filtd + 1;
12263
12264 li = listitem_alloc();
12265 if (li == NULL)
12266 {
12267 vim_free(s);
12268 break;
12269 }
12270 li->li_tv.v_type = VAR_STRING;
12271 li->li_tv.v_lock = 0;
12272 li->li_tv.vval.v_string = s;
12273 list_append(l, li);
12274
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012275 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012276 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012277 if (readlen <= 0)
12278 break;
12279 }
12280 else if (buf[filtd] == NUL)
12281 buf[filtd] = '\n';
12282 }
12283 if (readlen <= 0)
12284 break;
12285
12286 if (tolist == 0)
12287 {
12288 /* "buf" is full, need to move text to an allocated buffer */
12289 if (prev == NULL)
12290 {
12291 prev = vim_strnsave(buf, buflen);
12292 prevlen = buflen;
12293 }
12294 else
12295 {
12296 s = alloc((unsigned)(prevlen + buflen));
12297 if (s != NULL)
12298 {
12299 mch_memmove(s, prev, prevlen);
12300 mch_memmove(s + prevlen, buf, buflen);
12301 vim_free(prev);
12302 prev = s;
12303 prevlen += buflen;
12304 }
12305 }
12306 filtd = 0;
12307 }
12308 else
12309 {
12310 mch_memmove(buf, buf + tolist, buflen - tolist);
12311 filtd -= tolist;
12312 }
12313 }
12314
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012315 /*
12316 * For a negative line count use only the lines at the end of the file,
12317 * free the rest.
12318 */
12319 if (maxline < 0)
12320 while (cnt > -maxline)
12321 {
12322 listitem_remove(l, l->lv_first);
12323 --cnt;
12324 }
12325
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012326 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012327 fclose(fd);
12328}
12329
12330
Bram Moolenaar0d660222005-01-07 21:51:51 +000012331#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
12332static void make_connection __ARGS((void));
12333static int check_connection __ARGS((void));
12334
12335 static void
12336make_connection()
12337{
12338 if (X_DISPLAY == NULL
12339# ifdef FEAT_GUI
12340 && !gui.in_use
12341# endif
12342 )
12343 {
12344 x_force_connect = TRUE;
12345 setup_term_clip();
12346 x_force_connect = FALSE;
12347 }
12348}
12349
12350 static int
12351check_connection()
12352{
12353 make_connection();
12354 if (X_DISPLAY == NULL)
12355 {
12356 EMSG(_("E240: No connection to Vim server"));
12357 return FAIL;
12358 }
12359 return OK;
12360}
12361#endif
12362
12363#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012364static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012365
12366 static void
12367remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000012368 typval_T *argvars;
12369 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012370 int expr;
12371{
12372 char_u *server_name;
12373 char_u *keys;
12374 char_u *r = NULL;
12375 char_u buf[NUMBUFLEN];
12376# ifdef WIN32
12377 HWND w;
12378# else
12379 Window w;
12380# endif
12381
12382 if (check_restricted() || check_secure())
12383 return;
12384
12385# ifdef FEAT_X11
12386 if (check_connection() == FAIL)
12387 return;
12388# endif
12389
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012390 server_name = get_tv_string_chk(&argvars[0]);
12391 if (server_name == NULL)
12392 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012393 keys = get_tv_string_buf(&argvars[1], buf);
12394# ifdef WIN32
12395 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
12396# else
12397 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
12398 < 0)
12399# endif
12400 {
12401 if (r != NULL)
12402 EMSG(r); /* sending worked but evaluation failed */
12403 else
12404 EMSG2(_("E241: Unable to send to %s"), server_name);
12405 return;
12406 }
12407
12408 rettv->vval.v_string = r;
12409
12410 if (argvars[2].v_type != VAR_UNKNOWN)
12411 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012412 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000012413 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012414 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012415
12416 sprintf((char *)str, "0x%x", (unsigned int)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000012417 v.di_tv.v_type = VAR_STRING;
12418 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012419 idvar = get_tv_string_chk(&argvars[2]);
12420 if (idvar != NULL)
12421 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012422 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012423 }
12424}
12425#endif
12426
12427/*
12428 * "remote_expr()" function
12429 */
12430/*ARGSUSED*/
12431 static void
12432f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012433 typval_T *argvars;
12434 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012435{
12436 rettv->v_type = VAR_STRING;
12437 rettv->vval.v_string = NULL;
12438#ifdef FEAT_CLIENTSERVER
12439 remote_common(argvars, rettv, TRUE);
12440#endif
12441}
12442
12443/*
12444 * "remote_foreground()" function
12445 */
12446/*ARGSUSED*/
12447 static void
12448f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012449 typval_T *argvars;
12450 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012451{
12452 rettv->vval.v_number = 0;
12453#ifdef FEAT_CLIENTSERVER
12454# ifdef WIN32
12455 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012456 {
12457 char_u *server_name = get_tv_string_chk(&argvars[0]);
12458
12459 if (server_name != NULL)
12460 serverForeground(server_name);
12461 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012462# else
12463 /* Send a foreground() expression to the server. */
12464 argvars[1].v_type = VAR_STRING;
12465 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
12466 argvars[2].v_type = VAR_UNKNOWN;
12467 remote_common(argvars, rettv, TRUE);
12468 vim_free(argvars[1].vval.v_string);
12469# endif
12470#endif
12471}
12472
12473/*ARGSUSED*/
12474 static void
12475f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012476 typval_T *argvars;
12477 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012478{
12479#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012480 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012481 char_u *s = NULL;
12482# ifdef WIN32
12483 int n = 0;
12484# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012485 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012486
12487 if (check_restricted() || check_secure())
12488 {
12489 rettv->vval.v_number = -1;
12490 return;
12491 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012492 serverid = get_tv_string_chk(&argvars[0]);
12493 if (serverid == NULL)
12494 {
12495 rettv->vval.v_number = -1;
12496 return; /* type error; errmsg already given */
12497 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012498# ifdef WIN32
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012499 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012500 if (n == 0)
12501 rettv->vval.v_number = -1;
12502 else
12503 {
12504 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
12505 rettv->vval.v_number = (s != NULL);
12506 }
12507# else
12508 rettv->vval.v_number = 0;
12509 if (check_connection() == FAIL)
12510 return;
12511
12512 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012513 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012514# endif
12515
12516 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
12517 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012518 char_u *retvar;
12519
Bram Moolenaar33570922005-01-25 22:26:29 +000012520 v.di_tv.v_type = VAR_STRING;
12521 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012522 retvar = get_tv_string_chk(&argvars[1]);
12523 if (retvar != NULL)
12524 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012525 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012526 }
12527#else
12528 rettv->vval.v_number = -1;
12529#endif
12530}
12531
12532/*ARGSUSED*/
12533 static void
12534f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012535 typval_T *argvars;
12536 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012537{
12538 char_u *r = NULL;
12539
12540#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012541 char_u *serverid = get_tv_string_chk(&argvars[0]);
12542
12543 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000012544 {
12545# ifdef WIN32
12546 /* The server's HWND is encoded in the 'id' parameter */
12547 int n = 0;
12548
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012549 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012550 if (n != 0)
12551 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
12552 if (r == NULL)
12553# else
12554 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012555 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012556# endif
12557 EMSG(_("E277: Unable to read a server reply"));
12558 }
12559#endif
12560 rettv->v_type = VAR_STRING;
12561 rettv->vval.v_string = r;
12562}
12563
12564/*
12565 * "remote_send()" function
12566 */
12567/*ARGSUSED*/
12568 static void
12569f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012570 typval_T *argvars;
12571 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012572{
12573 rettv->v_type = VAR_STRING;
12574 rettv->vval.v_string = NULL;
12575#ifdef FEAT_CLIENTSERVER
12576 remote_common(argvars, rettv, FALSE);
12577#endif
12578}
12579
12580/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012581 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012582 */
12583 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012584f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012585 typval_T *argvars;
12586 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012587{
Bram Moolenaar33570922005-01-25 22:26:29 +000012588 list_T *l;
12589 listitem_T *item, *item2;
12590 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012591 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012592 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012593 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000012594 dict_T *d;
12595 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012596
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012597 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012598 if (argvars[0].v_type == VAR_DICT)
12599 {
12600 if (argvars[2].v_type != VAR_UNKNOWN)
12601 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012602 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar758711c2005-02-02 23:11:38 +000012603 && !tv_check_lock(d->dv_lock, (char_u *)"remove()"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000012604 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012605 key = get_tv_string_chk(&argvars[1]);
12606 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012607 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012608 di = dict_find(d, key, -1);
12609 if (di == NULL)
12610 EMSG2(_(e_dictkey), key);
12611 else
12612 {
12613 *rettv = di->di_tv;
12614 init_tv(&di->di_tv);
12615 dictitem_remove(d, di);
12616 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012617 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012618 }
12619 }
12620 else if (argvars[0].v_type != VAR_LIST)
12621 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012622 else if ((l = argvars[0].vval.v_list) != NULL
12623 && !tv_check_lock(l->lv_lock, (char_u *)"remove()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012624 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012625 int error = FALSE;
12626
12627 idx = get_tv_number_chk(&argvars[1], &error);
12628 if (error)
12629 ; /* type error: do nothing, errmsg already given */
12630 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012631 EMSGN(_(e_listidx), idx);
12632 else
12633 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012634 if (argvars[2].v_type == VAR_UNKNOWN)
12635 {
12636 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012637 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012638 *rettv = item->li_tv;
12639 vim_free(item);
12640 }
12641 else
12642 {
12643 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012644 end = get_tv_number_chk(&argvars[2], &error);
12645 if (error)
12646 ; /* type error: do nothing */
12647 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012648 EMSGN(_(e_listidx), end);
12649 else
12650 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012651 int cnt = 0;
12652
12653 for (li = item; li != NULL; li = li->li_next)
12654 {
12655 ++cnt;
12656 if (li == item2)
12657 break;
12658 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012659 if (li == NULL) /* didn't find "item2" after "item" */
12660 EMSG(_(e_invrange));
12661 else
12662 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012663 list_remove(l, item, item2);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012664 l = list_alloc();
12665 if (l != NULL)
12666 {
12667 rettv->v_type = VAR_LIST;
12668 rettv->vval.v_list = l;
12669 l->lv_first = item;
12670 l->lv_last = item2;
12671 l->lv_refcount = 1;
12672 item->li_prev = NULL;
12673 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012674 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012675 }
12676 }
12677 }
12678 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012679 }
12680 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012681}
12682
12683/*
12684 * "rename({from}, {to})" function
12685 */
12686 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012687f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012688 typval_T *argvars;
12689 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012690{
12691 char_u buf[NUMBUFLEN];
12692
12693 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012694 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012695 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012696 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
12697 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012698}
12699
12700/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012701 * "repeat()" function
12702 */
12703/*ARGSUSED*/
12704 static void
12705f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012706 typval_T *argvars;
12707 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012708{
12709 char_u *p;
12710 int n;
12711 int slen;
12712 int len;
12713 char_u *r;
12714 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000012715 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012716
12717 n = get_tv_number(&argvars[1]);
12718 if (argvars[0].v_type == VAR_LIST)
12719 {
12720 l = list_alloc();
12721 if (l != NULL && argvars[0].vval.v_list != NULL)
12722 {
12723 l->lv_refcount = 1;
12724 while (n-- > 0)
12725 if (list_extend(l, argvars[0].vval.v_list, NULL) == FAIL)
12726 break;
12727 }
12728 rettv->v_type = VAR_LIST;
12729 rettv->vval.v_list = l;
12730 }
12731 else
12732 {
12733 p = get_tv_string(&argvars[0]);
12734 rettv->v_type = VAR_STRING;
12735 rettv->vval.v_string = NULL;
12736
12737 slen = (int)STRLEN(p);
12738 len = slen * n;
12739 if (len <= 0)
12740 return;
12741
12742 r = alloc(len + 1);
12743 if (r != NULL)
12744 {
12745 for (i = 0; i < n; i++)
12746 mch_memmove(r + i * slen, p, (size_t)slen);
12747 r[len] = NUL;
12748 }
12749
12750 rettv->vval.v_string = r;
12751 }
12752}
12753
12754/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012755 * "resolve()" function
12756 */
12757 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012758f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012759 typval_T *argvars;
12760 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012761{
12762 char_u *p;
12763
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012764 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012765#ifdef FEAT_SHORTCUT
12766 {
12767 char_u *v = NULL;
12768
12769 v = mch_resolve_shortcut(p);
12770 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012771 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012772 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012773 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012774 }
12775#else
12776# ifdef HAVE_READLINK
12777 {
12778 char_u buf[MAXPATHL + 1];
12779 char_u *cpy;
12780 int len;
12781 char_u *remain = NULL;
12782 char_u *q;
12783 int is_relative_to_current = FALSE;
12784 int has_trailing_pathsep = FALSE;
12785 int limit = 100;
12786
12787 p = vim_strsave(p);
12788
12789 if (p[0] == '.' && (vim_ispathsep(p[1])
12790 || (p[1] == '.' && (vim_ispathsep(p[2])))))
12791 is_relative_to_current = TRUE;
12792
12793 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000012794 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012795 has_trailing_pathsep = TRUE;
12796
12797 q = getnextcomp(p);
12798 if (*q != NUL)
12799 {
12800 /* Separate the first path component in "p", and keep the
12801 * remainder (beginning with the path separator). */
12802 remain = vim_strsave(q - 1);
12803 q[-1] = NUL;
12804 }
12805
12806 for (;;)
12807 {
12808 for (;;)
12809 {
12810 len = readlink((char *)p, (char *)buf, MAXPATHL);
12811 if (len <= 0)
12812 break;
12813 buf[len] = NUL;
12814
12815 if (limit-- == 0)
12816 {
12817 vim_free(p);
12818 vim_free(remain);
12819 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012820 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012821 goto fail;
12822 }
12823
12824 /* Ensure that the result will have a trailing path separator
12825 * if the argument has one. */
12826 if (remain == NULL && has_trailing_pathsep)
12827 add_pathsep(buf);
12828
12829 /* Separate the first path component in the link value and
12830 * concatenate the remainders. */
12831 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
12832 if (*q != NUL)
12833 {
12834 if (remain == NULL)
12835 remain = vim_strsave(q - 1);
12836 else
12837 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012838 cpy = vim_strnsave(q-1, STRLEN(q-1) + STRLEN(remain));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012839 if (cpy != NULL)
12840 {
12841 STRCAT(cpy, remain);
12842 vim_free(remain);
12843 remain = cpy;
12844 }
12845 }
12846 q[-1] = NUL;
12847 }
12848
12849 q = gettail(p);
12850 if (q > p && *q == NUL)
12851 {
12852 /* Ignore trailing path separator. */
12853 q[-1] = NUL;
12854 q = gettail(p);
12855 }
12856 if (q > p && !mch_isFullName(buf))
12857 {
12858 /* symlink is relative to directory of argument */
12859 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
12860 if (cpy != NULL)
12861 {
12862 STRCPY(cpy, p);
12863 STRCPY(gettail(cpy), buf);
12864 vim_free(p);
12865 p = cpy;
12866 }
12867 }
12868 else
12869 {
12870 vim_free(p);
12871 p = vim_strsave(buf);
12872 }
12873 }
12874
12875 if (remain == NULL)
12876 break;
12877
12878 /* Append the first path component of "remain" to "p". */
12879 q = getnextcomp(remain + 1);
12880 len = q - remain - (*q != NUL);
12881 cpy = vim_strnsave(p, STRLEN(p) + len);
12882 if (cpy != NULL)
12883 {
12884 STRNCAT(cpy, remain, len);
12885 vim_free(p);
12886 p = cpy;
12887 }
12888 /* Shorten "remain". */
12889 if (*q != NUL)
12890 STRCPY(remain, q - 1);
12891 else
12892 {
12893 vim_free(remain);
12894 remain = NULL;
12895 }
12896 }
12897
12898 /* If the result is a relative path name, make it explicitly relative to
12899 * the current directory if and only if the argument had this form. */
12900 if (!vim_ispathsep(*p))
12901 {
12902 if (is_relative_to_current
12903 && *p != NUL
12904 && !(p[0] == '.'
12905 && (p[1] == NUL
12906 || vim_ispathsep(p[1])
12907 || (p[1] == '.'
12908 && (p[2] == NUL
12909 || vim_ispathsep(p[2]))))))
12910 {
12911 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012912 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012913 if (cpy != NULL)
12914 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012915 vim_free(p);
12916 p = cpy;
12917 }
12918 }
12919 else if (!is_relative_to_current)
12920 {
12921 /* Strip leading "./". */
12922 q = p;
12923 while (q[0] == '.' && vim_ispathsep(q[1]))
12924 q += 2;
12925 if (q > p)
12926 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
12927 }
12928 }
12929
12930 /* Ensure that the result will have no trailing path separator
12931 * if the argument had none. But keep "/" or "//". */
12932 if (!has_trailing_pathsep)
12933 {
12934 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000012935 if (after_pathsep(p, q))
12936 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012937 }
12938
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012939 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012940 }
12941# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012942 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012943# endif
12944#endif
12945
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012946 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012947
12948#ifdef HAVE_READLINK
12949fail:
12950#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012951 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012952}
12953
12954/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012955 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012956 */
12957 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012958f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012959 typval_T *argvars;
12960 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012961{
Bram Moolenaar33570922005-01-25 22:26:29 +000012962 list_T *l;
12963 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012964
Bram Moolenaar0d660222005-01-07 21:51:51 +000012965 rettv->vval.v_number = 0;
12966 if (argvars[0].v_type != VAR_LIST)
12967 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012968 else if ((l = argvars[0].vval.v_list) != NULL
12969 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000012970 {
12971 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012972 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012973 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012974 while (li != NULL)
12975 {
12976 ni = li->li_prev;
12977 list_append(l, li);
12978 li = ni;
12979 }
12980 rettv->vval.v_list = l;
12981 rettv->v_type = VAR_LIST;
12982 ++l->lv_refcount;
12983 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012984}
12985
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012986#define SP_NOMOVE 1 /* don't move cursor */
12987#define SP_REPEAT 2 /* repeat to find outer pair */
12988#define SP_RETCOUNT 4 /* return matchcount */
Bram Moolenaar231334e2005-07-25 20:46:57 +000012989#define SP_SETPCMARK 8 /* set previous context mark */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012990
Bram Moolenaar33570922005-01-25 22:26:29 +000012991static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012992
12993/*
12994 * Get flags for a search function.
12995 * Possibly sets "p_ws".
12996 * Returns BACKWARD, FORWARD or zero (for an error).
12997 */
12998 static int
12999get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013000 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013001 int *flagsp;
13002{
13003 int dir = FORWARD;
13004 char_u *flags;
13005 char_u nbuf[NUMBUFLEN];
13006 int mask;
13007
13008 if (varp->v_type != VAR_UNKNOWN)
13009 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013010 flags = get_tv_string_buf_chk(varp, nbuf);
13011 if (flags == NULL)
13012 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013013 while (*flags != NUL)
13014 {
13015 switch (*flags)
13016 {
13017 case 'b': dir = BACKWARD; break;
13018 case 'w': p_ws = TRUE; break;
13019 case 'W': p_ws = FALSE; break;
13020 default: mask = 0;
13021 if (flagsp != NULL)
13022 switch (*flags)
13023 {
13024 case 'n': mask = SP_NOMOVE; break;
13025 case 'r': mask = SP_REPEAT; break;
13026 case 'm': mask = SP_RETCOUNT; break;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013027 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013028 }
13029 if (mask == 0)
13030 {
13031 EMSG2(_(e_invarg2), flags);
13032 dir = 0;
13033 }
13034 else
13035 *flagsp |= mask;
13036 }
13037 if (dir == 0)
13038 break;
13039 ++flags;
13040 }
13041 }
13042 return dir;
13043}
13044
Bram Moolenaar071d4272004-06-13 20:20:40 +000013045/*
13046 * "search()" function
13047 */
13048 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013049f_search(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013050 typval_T *argvars;
13051 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013052{
13053 char_u *pat;
13054 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013055 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013056 int save_p_ws = p_ws;
13057 int dir;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013058 int flags = 0;
13059
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013060 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013061
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013062 pat = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013063 dir = get_search_arg(&argvars[1], &flags); /* may set p_ws */
13064 if (dir == 0)
13065 goto theend;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013066 /*
13067 * This function accepts only SP_NOMOVE and SP_SETPCMARK flags.
13068 * Check to make sure only those flags are set.
13069 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
13070 * flags cannot be set. Check for that condition also.
13071 */
13072 if (((flags & ~(SP_NOMOVE | SP_SETPCMARK)) != 0) ||
13073 ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013074 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013075 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013076 goto theend;
13077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013078
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013079 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013080 if (searchit(curwin, curbuf, &pos, dir, pat, 1L,
13081 SEARCH_KEEP, RE_SEARCH) != FAIL)
13082 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013083 rettv->vval.v_number = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013084 if (flags & SP_SETPCMARK)
13085 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013086 curwin->w_cursor = pos;
13087 /* "/$" will put the cursor after the end of the line, may need to
13088 * correct that here */
13089 check_cursor();
13090 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013091
13092 /* If 'n' flag is used: restore cursor position. */
13093 if (flags & SP_NOMOVE)
13094 curwin->w_cursor = save_cursor;
13095theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000013096 p_ws = save_p_ws;
13097}
13098
Bram Moolenaar071d4272004-06-13 20:20:40 +000013099/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013100 * "searchdecl()" function
13101 */
13102 static void
13103f_searchdecl(argvars, rettv)
13104 typval_T *argvars;
13105 typval_T *rettv;
13106{
13107 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013108 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013109 int error = FALSE;
13110 char_u *name;
13111
13112 rettv->vval.v_number = 1; /* default: FAIL */
13113
13114 name = get_tv_string_chk(&argvars[0]);
13115 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000013116 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013117 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013118 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13119 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
13120 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013121 if (!error && name != NULL)
13122 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000013123 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013124}
13125
13126/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013127 * "searchpair()" function
13128 */
13129 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013130f_searchpair(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 char_u *spat, *mpat, *epat;
13135 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013136 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013137 int dir;
13138 int flags = 0;
13139 char_u nbuf1[NUMBUFLEN];
13140 char_u nbuf2[NUMBUFLEN];
13141 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013142
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013143 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013144
Bram Moolenaar071d4272004-06-13 20:20:40 +000013145 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013146 spat = get_tv_string_chk(&argvars[0]);
13147 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
13148 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
13149 if (spat == NULL || mpat == NULL || epat == NULL)
13150 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013151
Bram Moolenaar071d4272004-06-13 20:20:40 +000013152 /* Handle the optional fourth argument: flags */
13153 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013154 if (dir == 0)
13155 goto theend;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013156 /*
13157 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
13158 */
13159 if ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK))
13160 {
13161 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
13162 goto theend;
13163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013164
13165 /* Optional fifth argument: skip expresion */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013166 if (argvars[3].v_type == VAR_UNKNOWN
13167 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013168 skip = (char_u *)"";
13169 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013170 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
13171 if (skip == NULL)
13172 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013173
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013174 rettv->vval.v_number = do_searchpair(spat, mpat, epat, dir, skip, flags);
13175
13176theend:
13177 p_ws = save_p_ws;
13178}
13179
13180/*
13181 * Search for a start/middle/end thing.
13182 * Used by searchpair(), see its documentation for the details.
13183 * Returns 0 or -1 for no match,
13184 */
13185 long
13186do_searchpair(spat, mpat, epat, dir, skip, flags)
13187 char_u *spat; /* start pattern */
13188 char_u *mpat; /* middle pattern */
13189 char_u *epat; /* end pattern */
13190 int dir; /* BACKWARD or FORWARD */
13191 char_u *skip; /* skip expression */
13192 int flags; /* SP_RETCOUNT, SP_REPEAT, SP_NOMOVE */
13193{
13194 char_u *save_cpo;
13195 char_u *pat, *pat2 = NULL, *pat3 = NULL;
13196 long retval = 0;
13197 pos_T pos;
13198 pos_T firstpos;
13199 pos_T foundpos;
13200 pos_T save_cursor;
13201 pos_T save_pos;
13202 int n;
13203 int r;
13204 int nest = 1;
13205 int err;
13206
13207 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13208 save_cpo = p_cpo;
13209 p_cpo = (char_u *)"";
13210
13211 /* Make two search patterns: start/end (pat2, for in nested pairs) and
13212 * start/middle/end (pat3, for the top pair). */
13213 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
13214 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
13215 if (pat2 == NULL || pat3 == NULL)
13216 goto theend;
13217 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
13218 if (*mpat == NUL)
13219 STRCPY(pat3, pat2);
13220 else
13221 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
13222 spat, epat, mpat);
13223
Bram Moolenaar071d4272004-06-13 20:20:40 +000013224 save_cursor = curwin->w_cursor;
13225 pos = curwin->w_cursor;
13226 firstpos.lnum = 0;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000013227 foundpos.lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013228 pat = pat3;
13229 for (;;)
13230 {
13231 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
13232 SEARCH_KEEP, RE_SEARCH);
13233 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
13234 /* didn't find it or found the first match again: FAIL */
13235 break;
13236
13237 if (firstpos.lnum == 0)
13238 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000013239 if (equalpos(pos, foundpos))
13240 {
13241 /* Found the same position again. Can happen with a pattern that
13242 * has "\zs" at the end and searching backwards. Advance one
13243 * character and try again. */
13244 if (dir == BACKWARD)
13245 decl(&pos);
13246 else
13247 incl(&pos);
13248 }
13249 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013250
13251 /* If the skip pattern matches, ignore this match. */
13252 if (*skip != NUL)
13253 {
13254 save_pos = curwin->w_cursor;
13255 curwin->w_cursor = pos;
13256 r = eval_to_bool(skip, &err, NULL, FALSE);
13257 curwin->w_cursor = save_pos;
13258 if (err)
13259 {
13260 /* Evaluating {skip} caused an error, break here. */
13261 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013262 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013263 break;
13264 }
13265 if (r)
13266 continue;
13267 }
13268
13269 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
13270 {
13271 /* Found end when searching backwards or start when searching
13272 * forward: nested pair. */
13273 ++nest;
13274 pat = pat2; /* nested, don't search for middle */
13275 }
13276 else
13277 {
13278 /* Found end when searching forward or start when searching
13279 * backward: end of (nested) pair; or found middle in outer pair. */
13280 if (--nest == 1)
13281 pat = pat3; /* outer level, search for middle */
13282 }
13283
13284 if (nest == 0)
13285 {
13286 /* Found the match: return matchcount or line number. */
13287 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013288 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013289 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013290 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013291 if (flags & SP_SETPCMARK)
13292 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013293 curwin->w_cursor = pos;
13294 if (!(flags & SP_REPEAT))
13295 break;
13296 nest = 1; /* search for next unmatched */
13297 }
13298 }
13299
13300 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013301 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013302 curwin->w_cursor = save_cursor;
13303
13304theend:
13305 vim_free(pat2);
13306 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013307 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013308
13309 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013310}
13311
Bram Moolenaar0d660222005-01-07 21:51:51 +000013312/*ARGSUSED*/
13313 static void
13314f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013315 typval_T *argvars;
13316 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013317{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013318#ifdef FEAT_CLIENTSERVER
13319 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013320 char_u *server = get_tv_string_chk(&argvars[0]);
13321 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013322
Bram Moolenaar0d660222005-01-07 21:51:51 +000013323 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013324 if (server == NULL || reply == NULL)
13325 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013326 if (check_restricted() || check_secure())
13327 return;
13328# ifdef FEAT_X11
13329 if (check_connection() == FAIL)
13330 return;
13331# endif
13332
13333 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013334 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013335 EMSG(_("E258: Unable to send to client"));
13336 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013337 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013338 rettv->vval.v_number = 0;
13339#else
13340 rettv->vval.v_number = -1;
13341#endif
13342}
13343
13344/*ARGSUSED*/
13345 static void
13346f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013347 typval_T *argvars;
13348 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013349{
13350 char_u *r = NULL;
13351
13352#ifdef FEAT_CLIENTSERVER
13353# ifdef WIN32
13354 r = serverGetVimNames();
13355# else
13356 make_connection();
13357 if (X_DISPLAY != NULL)
13358 r = serverGetVimNames(X_DISPLAY);
13359# endif
13360#endif
13361 rettv->v_type = VAR_STRING;
13362 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013363}
13364
13365/*
13366 * "setbufvar()" function
13367 */
13368/*ARGSUSED*/
13369 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013370f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013371 typval_T *argvars;
13372 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013373{
13374 buf_T *buf;
13375#ifdef FEAT_AUTOCMD
13376 aco_save_T aco;
13377#else
13378 buf_T *save_curbuf;
13379#endif
13380 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013381 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013382 char_u nbuf[NUMBUFLEN];
13383
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013384 rettv->vval.v_number = 0;
13385
Bram Moolenaar071d4272004-06-13 20:20:40 +000013386 if (check_restricted() || check_secure())
13387 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013388 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
13389 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013390 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013391 varp = &argvars[2];
13392
13393 if (buf != NULL && varname != NULL && varp != NULL)
13394 {
13395 /* set curbuf to be our buf, temporarily */
13396#ifdef FEAT_AUTOCMD
13397 aucmd_prepbuf(&aco, buf);
13398#else
13399 save_curbuf = curbuf;
13400 curbuf = buf;
13401#endif
13402
13403 if (*varname == '&')
13404 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013405 long numval;
13406 char_u *strval;
13407 int error = FALSE;
13408
Bram Moolenaar071d4272004-06-13 20:20:40 +000013409 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013410 numval = get_tv_number_chk(varp, &error);
13411 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013412 if (!error && strval != NULL)
13413 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013414 }
13415 else
13416 {
13417 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
13418 if (bufvarname != NULL)
13419 {
13420 STRCPY(bufvarname, "b:");
13421 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013422 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013423 vim_free(bufvarname);
13424 }
13425 }
13426
13427 /* reset notion of buffer */
13428#ifdef FEAT_AUTOCMD
13429 aucmd_restbuf(&aco);
13430#else
13431 curbuf = save_curbuf;
13432#endif
13433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013434}
13435
13436/*
13437 * "setcmdpos()" function
13438 */
13439 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013440f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013441 typval_T *argvars;
13442 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013443{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013444 int pos = (int)get_tv_number(&argvars[0]) - 1;
13445
13446 if (pos >= 0)
13447 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013448}
13449
13450/*
13451 * "setline()" function
13452 */
13453 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013454f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013455 typval_T *argvars;
13456 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013457{
13458 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000013459 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013460 list_T *l = NULL;
13461 listitem_T *li = NULL;
13462 long added = 0;
13463 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013464
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013465 lnum = get_tv_lnum(&argvars[0]);
13466 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013467 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013468 l = argvars[1].vval.v_list;
13469 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013470 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013471 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013472 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013473
13474 rettv->vval.v_number = 0; /* OK */
13475 for (;;)
13476 {
13477 if (l != NULL)
13478 {
13479 /* list argument, get next string */
13480 if (li == NULL)
13481 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013482 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013483 li = li->li_next;
13484 }
13485
13486 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013487 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013488 break;
13489 if (lnum <= curbuf->b_ml.ml_line_count)
13490 {
13491 /* existing line, replace it */
13492 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
13493 {
13494 changed_bytes(lnum, 0);
13495 check_cursor_col();
13496 rettv->vval.v_number = 0; /* OK */
13497 }
13498 }
13499 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
13500 {
13501 /* lnum is one past the last line, append the line */
13502 ++added;
13503 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
13504 rettv->vval.v_number = 0; /* OK */
13505 }
13506
13507 if (l == NULL) /* only one string argument */
13508 break;
13509 ++lnum;
13510 }
13511
13512 if (added > 0)
13513 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013514}
13515
13516/*
Bram Moolenaar2641f772005-03-25 21:58:17 +000013517 * "setqflist()" function
13518 */
13519/*ARGSUSED*/
13520 static void
13521f_setqflist(argvars, rettv)
13522 typval_T *argvars;
13523 typval_T *rettv;
13524{
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013525 char_u *act;
13526 int action = ' ';
13527
Bram Moolenaar2641f772005-03-25 21:58:17 +000013528 rettv->vval.v_number = -1;
13529
13530#ifdef FEAT_QUICKFIX
13531 if (argvars[0].v_type != VAR_LIST)
13532 EMSG(_(e_listreq));
13533 else
13534 {
13535 list_T *l = argvars[0].vval.v_list;
13536
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013537 if (argvars[1].v_type == VAR_STRING)
13538 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013539 act = get_tv_string_chk(&argvars[1]);
13540 if (act == NULL)
13541 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013542 if (*act == 'a' || *act == 'r')
13543 action = *act;
13544 }
13545
13546 if (l != NULL && set_errorlist(l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013547 rettv->vval.v_number = 0;
13548 }
13549#endif
13550}
13551
13552/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013553 * "setreg()" function
13554 */
13555 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013556f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013557 typval_T *argvars;
13558 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013559{
13560 int regname;
13561 char_u *strregname;
13562 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013563 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013564 int append;
13565 char_u yank_type;
13566 long block_len;
13567
13568 block_len = -1;
13569 yank_type = MAUTO;
13570 append = FALSE;
13571
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013572 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013573 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013574
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013575 if (strregname == NULL)
13576 return; /* type error; errmsg already given */
13577 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013578 if (regname == 0 || regname == '@')
13579 regname = '"';
13580 else if (regname == '=')
13581 return;
13582
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013583 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013584 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013585 stropt = get_tv_string_chk(&argvars[2]);
13586 if (stropt == NULL)
13587 return; /* type error */
13588 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013589 switch (*stropt)
13590 {
13591 case 'a': case 'A': /* append */
13592 append = TRUE;
13593 break;
13594 case 'v': case 'c': /* character-wise selection */
13595 yank_type = MCHAR;
13596 break;
13597 case 'V': case 'l': /* line-wise selection */
13598 yank_type = MLINE;
13599 break;
13600#ifdef FEAT_VISUAL
13601 case 'b': case Ctrl_V: /* block-wise selection */
13602 yank_type = MBLOCK;
13603 if (VIM_ISDIGIT(stropt[1]))
13604 {
13605 ++stropt;
13606 block_len = getdigits(&stropt) - 1;
13607 --stropt;
13608 }
13609 break;
13610#endif
13611 }
13612 }
13613
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013614 strval = get_tv_string_chk(&argvars[1]);
13615 if (strval != NULL)
13616 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000013617 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013618 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013619}
13620
13621
13622/*
13623 * "setwinvar(expr)" function
13624 */
13625/*ARGSUSED*/
13626 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013627f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013628 typval_T *argvars;
13629 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013630{
13631 win_T *win;
13632#ifdef FEAT_WINDOWS
13633 win_T *save_curwin;
13634#endif
13635 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013636 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013637 char_u nbuf[NUMBUFLEN];
13638
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013639 rettv->vval.v_number = 0;
13640
Bram Moolenaar071d4272004-06-13 20:20:40 +000013641 if (check_restricted() || check_secure())
13642 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013643 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013644 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013645 varp = &argvars[2];
13646
13647 if (win != NULL && varname != NULL && varp != NULL)
13648 {
13649#ifdef FEAT_WINDOWS
13650 /* set curwin to be our win, temporarily */
13651 save_curwin = curwin;
13652 curwin = win;
13653 curbuf = curwin->w_buffer;
13654#endif
13655
13656 if (*varname == '&')
13657 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013658 long numval;
13659 char_u *strval;
13660 int error = FALSE;
13661
Bram Moolenaar071d4272004-06-13 20:20:40 +000013662 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013663 numval = get_tv_number_chk(varp, &error);
13664 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013665 if (!error && strval != NULL)
13666 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013667 }
13668 else
13669 {
13670 winvarname = alloc((unsigned)STRLEN(varname) + 3);
13671 if (winvarname != NULL)
13672 {
13673 STRCPY(winvarname, "w:");
13674 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013675 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013676 vim_free(winvarname);
13677 }
13678 }
13679
13680#ifdef FEAT_WINDOWS
13681 /* Restore current window, if it's still valid (autocomands can make
13682 * it invalid). */
13683 if (win_valid(save_curwin))
13684 {
13685 curwin = save_curwin;
13686 curbuf = curwin->w_buffer;
13687 }
13688#endif
13689 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013690}
13691
13692/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013693 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013694 */
13695 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013696f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013697 typval_T *argvars;
13698 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013699{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013700 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013701
Bram Moolenaar0d660222005-01-07 21:51:51 +000013702 p = get_tv_string(&argvars[0]);
13703 rettv->vval.v_string = vim_strsave(p);
13704 simplify_filename(rettv->vval.v_string); /* simplify in place */
13705 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013706}
13707
Bram Moolenaar0d660222005-01-07 21:51:51 +000013708static int
13709#ifdef __BORLANDC__
13710 _RTLENTRYF
13711#endif
13712 item_compare __ARGS((const void *s1, const void *s2));
13713static int
13714#ifdef __BORLANDC__
13715 _RTLENTRYF
13716#endif
13717 item_compare2 __ARGS((const void *s1, const void *s2));
13718
13719static int item_compare_ic;
13720static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013721static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013722#define ITEM_COMPARE_FAIL 999
13723
Bram Moolenaar071d4272004-06-13 20:20:40 +000013724/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013725 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013726 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013727 static int
13728#ifdef __BORLANDC__
13729_RTLENTRYF
13730#endif
13731item_compare(s1, s2)
13732 const void *s1;
13733 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013734{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013735 char_u *p1, *p2;
13736 char_u *tofree1, *tofree2;
13737 int res;
13738 char_u numbuf1[NUMBUFLEN];
13739 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013740
Bram Moolenaar33570922005-01-25 22:26:29 +000013741 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1);
13742 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013743 if (item_compare_ic)
13744 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013745 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000013746 res = STRCMP(p1, p2);
13747 vim_free(tofree1);
13748 vim_free(tofree2);
13749 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013750}
13751
13752 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000013753#ifdef __BORLANDC__
13754_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000013755#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000013756item_compare2(s1, s2)
13757 const void *s1;
13758 const void *s2;
13759{
13760 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000013761 typval_T rettv;
13762 typval_T argv[2];
Bram Moolenaar0d660222005-01-07 21:51:51 +000013763 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013764
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013765 /* shortcut after failure in previous call; compare all items equal */
13766 if (item_compare_func_err)
13767 return 0;
13768
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013769 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
13770 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013771 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
13772 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013773
13774 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
13775 res = call_func(item_compare_func, STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000013776 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013777 clear_tv(&argv[0]);
13778 clear_tv(&argv[1]);
13779
13780 if (res == FAIL)
13781 res = ITEM_COMPARE_FAIL;
13782 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013783 /* return value has wrong type */
13784 res = get_tv_number_chk(&rettv, &item_compare_func_err);
13785 if (item_compare_func_err)
13786 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013787 clear_tv(&rettv);
13788 return res;
13789}
13790
13791/*
13792 * "sort({list})" function
13793 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013794 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013795f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013796 typval_T *argvars;
13797 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013798{
Bram Moolenaar33570922005-01-25 22:26:29 +000013799 list_T *l;
13800 listitem_T *li;
13801 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013802 long len;
13803 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013804
Bram Moolenaar0d660222005-01-07 21:51:51 +000013805 rettv->vval.v_number = 0;
13806 if (argvars[0].v_type != VAR_LIST)
13807 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000013808 else
13809 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013810 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013811 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013812 return;
13813 rettv->vval.v_list = l;
13814 rettv->v_type = VAR_LIST;
13815 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013816
Bram Moolenaar0d660222005-01-07 21:51:51 +000013817 len = list_len(l);
13818 if (len <= 1)
13819 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013820
Bram Moolenaar0d660222005-01-07 21:51:51 +000013821 item_compare_ic = FALSE;
13822 item_compare_func = NULL;
13823 if (argvars[1].v_type != VAR_UNKNOWN)
13824 {
13825 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013826 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013827 else
13828 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013829 int error = FALSE;
13830
13831 i = get_tv_number_chk(&argvars[1], &error);
13832 if (error)
13833 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013834 if (i == 1)
13835 item_compare_ic = TRUE;
13836 else
13837 item_compare_func = get_tv_string(&argvars[1]);
13838 }
13839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013840
Bram Moolenaar0d660222005-01-07 21:51:51 +000013841 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013842 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013843 if (ptrs == NULL)
13844 return;
13845 i = 0;
13846 for (li = l->lv_first; li != NULL; li = li->li_next)
13847 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013848
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013849 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013850 /* test the compare function */
13851 if (item_compare_func != NULL
13852 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
13853 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013854 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013855 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000013856 {
13857 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013858 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000013859 item_compare_func == NULL ? item_compare : item_compare2);
13860
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013861 if (!item_compare_func_err)
13862 {
13863 /* Clear the List and append the items in the sorted order. */
13864 l->lv_first = l->lv_last = NULL;
13865 l->lv_len = 0;
13866 for (i = 0; i < len; ++i)
13867 list_append(l, ptrs[i]);
13868 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013869 }
13870
13871 vim_free(ptrs);
13872 }
13873}
13874
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013875/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000013876 * "soundfold({word})" function
13877 */
13878 static void
13879f_soundfold(argvars, rettv)
13880 typval_T *argvars;
13881 typval_T *rettv;
13882{
13883 char_u *s;
13884
13885 rettv->v_type = VAR_STRING;
13886 s = get_tv_string(&argvars[0]);
13887#ifdef FEAT_SYN_HL
13888 rettv->vval.v_string = eval_soundfold(s);
13889#else
13890 rettv->vval.v_string = vim_strsave(s);
13891#endif
13892}
13893
13894/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013895 * "spellbadword()" function
13896 */
13897/* ARGSUSED */
13898 static void
13899f_spellbadword(argvars, rettv)
13900 typval_T *argvars;
13901 typval_T *rettv;
13902{
Bram Moolenaar4463f292005-09-25 22:20:24 +000013903 char_u *word = (char_u *)"";
13904#ifdef FEAT_SYN_HL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013905 int len;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013906 int attr = 0;
13907 list_T *l;
13908#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013909
Bram Moolenaar4463f292005-09-25 22:20:24 +000013910 l = list_alloc();
13911 if (l == NULL)
13912 return;
13913 rettv->v_type = VAR_LIST;
13914 rettv->vval.v_list = l;
13915 ++l->lv_refcount;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013916
13917#ifdef FEAT_SYN_HL
Bram Moolenaar4463f292005-09-25 22:20:24 +000013918 if (argvars[0].v_type == VAR_UNKNOWN)
13919 {
13920 /* Find the start and length of the badly spelled word. */
13921 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
13922 if (len != 0)
13923 word = ml_get_cursor();
13924 }
13925 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
13926 {
13927 char_u *str = get_tv_string_chk(&argvars[0]);
13928 int capcol = -1;
13929
13930 if (str != NULL)
13931 {
13932 /* Check the argument for spelling. */
13933 while (*str != NUL)
13934 {
13935 len = spell_check(curwin, str, &attr, &capcol);
13936 if (attr != 0)
13937 {
13938 word = str;
13939 break;
13940 }
13941 str += len;
13942 }
13943 }
13944 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013945#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000013946
13947 list_append_string(l, word, len);
13948 list_append_string(l, (char_u *)(
13949 attr == highlight_attr[HLF_SPB] ? "bad" :
13950 attr == highlight_attr[HLF_SPR] ? "rare" :
13951 attr == highlight_attr[HLF_SPL] ? "local" :
13952 attr == highlight_attr[HLF_SPC] ? "caps" :
13953 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013954}
13955
13956/*
13957 * "spellsuggest()" function
13958 */
13959 static void
13960f_spellsuggest(argvars, rettv)
13961 typval_T *argvars;
13962 typval_T *rettv;
13963{
13964 char_u *str;
13965 int maxcount;
13966 garray_T ga;
13967 list_T *l;
13968 listitem_T *li;
13969 int i;
13970
13971 l = list_alloc();
13972 if (l == NULL)
13973 return;
13974 rettv->v_type = VAR_LIST;
13975 rettv->vval.v_list = l;
13976 ++l->lv_refcount;
13977
13978#ifdef FEAT_SYN_HL
13979 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
13980 {
13981 str = get_tv_string(&argvars[0]);
13982 if (argvars[1].v_type != VAR_UNKNOWN)
13983 {
13984 maxcount = get_tv_number(&argvars[1]);
13985 if (maxcount <= 0)
13986 return;
13987 }
13988 else
13989 maxcount = 25;
13990
Bram Moolenaar8c45cdf2005-08-11 20:11:38 +000013991 spell_suggest_list(&ga, str, maxcount, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013992
13993 for (i = 0; i < ga.ga_len; ++i)
13994 {
13995 str = ((char_u **)ga.ga_data)[i];
13996
13997 li = listitem_alloc();
13998 if (li == NULL)
13999 vim_free(str);
14000 else
14001 {
14002 li->li_tv.v_type = VAR_STRING;
14003 li->li_tv.v_lock = 0;
14004 li->li_tv.vval.v_string = str;
14005 list_append(l, li);
14006 }
14007 }
14008 ga_clear(&ga);
14009 }
14010#endif
14011}
14012
Bram Moolenaar0d660222005-01-07 21:51:51 +000014013 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014014f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014015 typval_T *argvars;
14016 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014017{
14018 char_u *str;
14019 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014020 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014021 regmatch_T regmatch;
14022 char_u patbuf[NUMBUFLEN];
14023 char_u *save_cpo;
14024 int match;
Bram Moolenaar33570922005-01-25 22:26:29 +000014025 list_T *l;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014026 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014027 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014028 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014029
14030 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14031 save_cpo = p_cpo;
14032 p_cpo = (char_u *)"";
14033
14034 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014035 if (argvars[1].v_type != VAR_UNKNOWN)
14036 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014037 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14038 if (pat == NULL)
14039 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014040 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014041 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014042 }
14043 if (pat == NULL || *pat == NUL)
14044 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000014045
14046 l = list_alloc();
14047 if (l == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014048 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014049 rettv->v_type = VAR_LIST;
14050 rettv->vval.v_list = l;
14051 ++l->lv_refcount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014052 if (typeerr)
14053 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014054
Bram Moolenaar0d660222005-01-07 21:51:51 +000014055 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14056 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014057 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014058 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014059 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014060 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014061 if (*str == NUL)
14062 match = FALSE; /* empty item at the end */
14063 else
14064 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014065 if (match)
14066 end = regmatch.startp[0];
14067 else
14068 end = str + STRLEN(str);
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014069 if (keepempty || end > str || (l->lv_len > 0 && *str != NUL
14070 && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014071 {
Bram Moolenaar4463f292005-09-25 22:20:24 +000014072 if (list_append_string(l, str, (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014073 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014074 }
14075 if (!match)
14076 break;
14077 /* Advance to just after the match. */
14078 if (regmatch.endp[0] > str)
14079 col = 0;
14080 else
14081 {
14082 /* Don't get stuck at the same match. */
14083#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014084 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014085#else
14086 col = 1;
14087#endif
14088 }
14089 str = regmatch.endp[0];
14090 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014091
Bram Moolenaar0d660222005-01-07 21:51:51 +000014092 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014094
Bram Moolenaar0d660222005-01-07 21:51:51 +000014095 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014096}
14097
14098#ifdef HAVE_STRFTIME
14099/*
14100 * "strftime({format}[, {time}])" function
14101 */
14102 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014103f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014104 typval_T *argvars;
14105 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014106{
14107 char_u result_buf[256];
14108 struct tm *curtime;
14109 time_t seconds;
14110 char_u *p;
14111
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014112 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014113
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014114 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014115 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014116 seconds = time(NULL);
14117 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014118 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014119 curtime = localtime(&seconds);
14120 /* MSVC returns NULL for an invalid value of seconds. */
14121 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014122 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014123 else
14124 {
14125# ifdef FEAT_MBYTE
14126 vimconv_T conv;
14127 char_u *enc;
14128
14129 conv.vc_type = CONV_NONE;
14130 enc = enc_locale();
14131 convert_setup(&conv, p_enc, enc);
14132 if (conv.vc_type != CONV_NONE)
14133 p = string_convert(&conv, p, NULL);
14134# endif
14135 if (p != NULL)
14136 (void)strftime((char *)result_buf, sizeof(result_buf),
14137 (char *)p, curtime);
14138 else
14139 result_buf[0] = NUL;
14140
14141# ifdef FEAT_MBYTE
14142 if (conv.vc_type != CONV_NONE)
14143 vim_free(p);
14144 convert_setup(&conv, enc, p_enc);
14145 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014146 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014147 else
14148# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014149 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014150
14151# ifdef FEAT_MBYTE
14152 /* Release conversion descriptors */
14153 convert_setup(&conv, NULL, NULL);
14154 vim_free(enc);
14155# endif
14156 }
14157}
14158#endif
14159
14160/*
14161 * "stridx()" function
14162 */
14163 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014164f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014165 typval_T *argvars;
14166 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014167{
14168 char_u buf[NUMBUFLEN];
14169 char_u *needle;
14170 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000014171 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014172 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000014173 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014174
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014175 needle = get_tv_string_chk(&argvars[1]);
14176 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000014177 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014178 if (needle == NULL || haystack == NULL)
14179 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014180
Bram Moolenaar33570922005-01-25 22:26:29 +000014181 if (argvars[2].v_type != VAR_UNKNOWN)
14182 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014183 int error = FALSE;
14184
14185 start_idx = get_tv_number_chk(&argvars[2], &error);
14186 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000014187 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014188 if (start_idx >= 0)
14189 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000014190 }
14191
14192 pos = (char_u *)strstr((char *)haystack, (char *)needle);
14193 if (pos != NULL)
14194 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014195}
14196
14197/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014198 * "string()" function
14199 */
14200 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014201f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014202 typval_T *argvars;
14203 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014204{
14205 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014206 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014207
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014208 rettv->v_type = VAR_STRING;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014209 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014210 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014211 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014212}
14213
14214/*
14215 * "strlen()" function
14216 */
14217 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014218f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014219 typval_T *argvars;
14220 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014221{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014222 rettv->vval.v_number = (varnumber_T)(STRLEN(
14223 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014224}
14225
14226/*
14227 * "strpart()" function
14228 */
14229 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014230f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014231 typval_T *argvars;
14232 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014233{
14234 char_u *p;
14235 int n;
14236 int len;
14237 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014238 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014239
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014240 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014241 slen = (int)STRLEN(p);
14242
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014243 n = get_tv_number_chk(&argvars[1], &error);
14244 if (error)
14245 len = 0;
14246 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014247 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014248 else
14249 len = slen - n; /* default len: all bytes that are available. */
14250
14251 /*
14252 * Only return the overlap between the specified part and the actual
14253 * string.
14254 */
14255 if (n < 0)
14256 {
14257 len += n;
14258 n = 0;
14259 }
14260 else if (n > slen)
14261 n = slen;
14262 if (len < 0)
14263 len = 0;
14264 else if (n + len > slen)
14265 len = slen - n;
14266
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014267 rettv->v_type = VAR_STRING;
14268 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014269}
14270
14271/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014272 * "strridx()" function
14273 */
14274 static void
14275f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014276 typval_T *argvars;
14277 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014278{
14279 char_u buf[NUMBUFLEN];
14280 char_u *needle;
14281 char_u *haystack;
14282 char_u *rest;
14283 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014284 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014285
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014286 needle = get_tv_string_chk(&argvars[1]);
14287 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar532c7802005-01-27 14:44:31 +000014288 haystack_len = STRLEN(haystack);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014289
14290 rettv->vval.v_number = -1;
14291 if (needle == NULL || haystack == NULL)
14292 return; /* type error; errmsg already given */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014293 if (argvars[2].v_type != VAR_UNKNOWN)
14294 {
14295 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014296 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000014297 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014298 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014299 }
14300 else
14301 end_idx = haystack_len;
14302
Bram Moolenaar0d660222005-01-07 21:51:51 +000014303 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000014304 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014305 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014306 lastmatch = haystack + end_idx;
14307 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014308 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000014309 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014310 for (rest = haystack; *rest != '\0'; ++rest)
14311 {
14312 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000014313 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014314 break;
14315 lastmatch = rest;
14316 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000014317 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014318
14319 if (lastmatch == NULL)
14320 rettv->vval.v_number = -1;
14321 else
14322 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
14323}
14324
14325/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014326 * "strtrans()" function
14327 */
14328 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014329f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014330 typval_T *argvars;
14331 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014332{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014333 rettv->v_type = VAR_STRING;
14334 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014335}
14336
14337/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014338 * "submatch()" function
14339 */
14340 static void
14341f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014342 typval_T *argvars;
14343 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014344{
14345 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014346 rettv->vval.v_string =
14347 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014348}
14349
14350/*
14351 * "substitute()" function
14352 */
14353 static void
14354f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014355 typval_T *argvars;
14356 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014357{
14358 char_u patbuf[NUMBUFLEN];
14359 char_u subbuf[NUMBUFLEN];
14360 char_u flagsbuf[NUMBUFLEN];
14361
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014362 char_u *str = get_tv_string_chk(&argvars[0]);
14363 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14364 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
14365 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
14366
Bram Moolenaar0d660222005-01-07 21:51:51 +000014367 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014368 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
14369 rettv->vval.v_string = NULL;
14370 else
14371 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014372}
14373
14374/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014375 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014376 */
14377/*ARGSUSED*/
14378 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014379f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014380 typval_T *argvars;
14381 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014382{
14383 int id = 0;
14384#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014385 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014386 long col;
14387 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000014388 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014389
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014390 lnum = get_tv_lnum(argvars); /* -1 on type error */
14391 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
14392 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014393
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014394 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014395 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +000014396 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014397#endif
14398
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014399 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014400}
14401
14402/*
14403 * "synIDattr(id, what [, mode])" function
14404 */
14405/*ARGSUSED*/
14406 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014407f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014408 typval_T *argvars;
14409 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014410{
14411 char_u *p = NULL;
14412#ifdef FEAT_SYN_HL
14413 int id;
14414 char_u *what;
14415 char_u *mode;
14416 char_u modebuf[NUMBUFLEN];
14417 int modec;
14418
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014419 id = get_tv_number(&argvars[0]);
14420 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014421 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014422 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014423 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014424 modec = TOLOWER_ASC(mode[0]);
14425 if (modec != 't' && modec != 'c'
14426#ifdef FEAT_GUI
14427 && modec != 'g'
14428#endif
14429 )
14430 modec = 0; /* replace invalid with current */
14431 }
14432 else
14433 {
14434#ifdef FEAT_GUI
14435 if (gui.in_use)
14436 modec = 'g';
14437 else
14438#endif
14439 if (t_colors > 1)
14440 modec = 'c';
14441 else
14442 modec = 't';
14443 }
14444
14445
14446 switch (TOLOWER_ASC(what[0]))
14447 {
14448 case 'b':
14449 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
14450 p = highlight_color(id, what, modec);
14451 else /* bold */
14452 p = highlight_has_attr(id, HL_BOLD, modec);
14453 break;
14454
14455 case 'f': /* fg[#] */
14456 p = highlight_color(id, what, modec);
14457 break;
14458
14459 case 'i':
14460 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
14461 p = highlight_has_attr(id, HL_INVERSE, modec);
14462 else /* italic */
14463 p = highlight_has_attr(id, HL_ITALIC, modec);
14464 break;
14465
14466 case 'n': /* name */
14467 p = get_highlight_name(NULL, id - 1);
14468 break;
14469
14470 case 'r': /* reverse */
14471 p = highlight_has_attr(id, HL_INVERSE, modec);
14472 break;
14473
14474 case 's': /* standout */
14475 p = highlight_has_attr(id, HL_STANDOUT, modec);
14476 break;
14477
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000014478 case 'u':
14479 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
14480 /* underline */
14481 p = highlight_has_attr(id, HL_UNDERLINE, modec);
14482 else
14483 /* undercurl */
14484 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014485 break;
14486 }
14487
14488 if (p != NULL)
14489 p = vim_strsave(p);
14490#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014491 rettv->v_type = VAR_STRING;
14492 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014493}
14494
14495/*
14496 * "synIDtrans(id)" function
14497 */
14498/*ARGSUSED*/
14499 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014500f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014501 typval_T *argvars;
14502 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014503{
14504 int id;
14505
14506#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014507 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014508
14509 if (id > 0)
14510 id = syn_get_final_id(id);
14511 else
14512#endif
14513 id = 0;
14514
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014515 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014516}
14517
14518/*
14519 * "system()" function
14520 */
14521 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014522f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014523 typval_T *argvars;
14524 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014525{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014526 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014527 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014528 char_u *infile = NULL;
14529 char_u buf[NUMBUFLEN];
14530 int err = FALSE;
14531 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014532
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014533 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014534 {
14535 /*
14536 * Write the string to a temp file, to be used for input of the shell
14537 * command.
14538 */
14539 if ((infile = vim_tempname('i')) == NULL)
14540 {
14541 EMSG(_(e_notmp));
14542 return;
14543 }
14544
14545 fd = mch_fopen((char *)infile, WRITEBIN);
14546 if (fd == NULL)
14547 {
14548 EMSG2(_(e_notopen), infile);
14549 goto done;
14550 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014551 p = get_tv_string_buf_chk(&argvars[1], buf);
14552 if (p == NULL)
14553 goto done; /* type error; errmsg already given */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014554 if (fwrite(p, STRLEN(p), 1, fd) != 1)
14555 err = TRUE;
14556 if (fclose(fd) != 0)
14557 err = TRUE;
14558 if (err)
14559 {
14560 EMSG(_("E677: Error writing temp file"));
14561 goto done;
14562 }
14563 }
14564
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014565 res = get_cmd_output(get_tv_string(&argvars[0]), infile, SHELL_SILENT);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014566
Bram Moolenaar071d4272004-06-13 20:20:40 +000014567#ifdef USE_CR
14568 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014569 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014570 {
14571 char_u *s;
14572
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014573 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014574 {
14575 if (*s == CAR)
14576 *s = NL;
14577 }
14578 }
14579#else
14580# ifdef USE_CRNL
14581 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014582 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014583 {
14584 char_u *s, *d;
14585
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014586 d = res;
14587 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014588 {
14589 if (s[0] == CAR && s[1] == NL)
14590 ++s;
14591 *d++ = *s;
14592 }
14593 *d = NUL;
14594 }
14595# endif
14596#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014597
14598done:
14599 if (infile != NULL)
14600 {
14601 mch_remove(infile);
14602 vim_free(infile);
14603 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014604 rettv->v_type = VAR_STRING;
14605 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014606}
14607
14608/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000014609 * "tagfiles()" function
14610 */
14611/*ARGSUSED*/
14612 static void
14613f_tagfiles(argvars, rettv)
14614 typval_T *argvars;
14615 typval_T *rettv;
14616{
14617 char_u fname[MAXPATHL + 1];
14618 list_T *l;
14619
14620 l = list_alloc();
14621 if (l == NULL)
14622 {
14623 rettv->vval.v_number = 0;
14624 return;
14625 }
14626 rettv->vval.v_list = l;
14627 rettv->v_type = VAR_LIST;
14628 ++l->lv_refcount;
14629
14630 get_tagfname(TRUE, NULL);
14631 for (;;)
14632 if (get_tagfname(FALSE, fname) == FAIL
Bram Moolenaar4463f292005-09-25 22:20:24 +000014633 || list_append_string(l, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000014634 break;
14635}
14636
14637/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000014638 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014639 */
14640 static void
14641f_taglist(argvars, rettv)
14642 typval_T *argvars;
14643 typval_T *rettv;
14644{
14645 char_u *tag_pattern;
14646 list_T *l;
14647
14648 tag_pattern = get_tv_string(&argvars[0]);
14649
14650 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014651 if (*tag_pattern == NUL)
14652 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014653
14654 l = list_alloc();
14655 if (l != NULL)
14656 {
14657 if (get_tags(l, tag_pattern) != FAIL)
14658 {
14659 rettv->vval.v_list = l;
14660 rettv->v_type = VAR_LIST;
14661 ++l->lv_refcount;
14662 }
14663 else
14664 list_free(l);
14665 }
14666}
14667
14668/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014669 * "tempname()" function
14670 */
14671/*ARGSUSED*/
14672 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014673f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014674 typval_T *argvars;
14675 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014676{
14677 static int x = 'A';
14678
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014679 rettv->v_type = VAR_STRING;
14680 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014681
14682 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
14683 * names. Skip 'I' and 'O', they are used for shell redirection. */
14684 do
14685 {
14686 if (x == 'Z')
14687 x = '0';
14688 else if (x == '9')
14689 x = 'A';
14690 else
14691 {
14692#ifdef EBCDIC
14693 if (x == 'I')
14694 x = 'J';
14695 else if (x == 'R')
14696 x = 'S';
14697 else
14698#endif
14699 ++x;
14700 }
14701 } while (x == 'I' || x == 'O');
14702}
14703
14704/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000014705 * "test(list)" function: Just checking the walls...
14706 */
14707/*ARGSUSED*/
14708 static void
14709f_test(argvars, rettv)
14710 typval_T *argvars;
14711 typval_T *rettv;
14712{
14713 /* Used for unit testing. Change the code below to your liking. */
14714#if 0
14715 listitem_T *li;
14716 list_T *l;
14717 char_u *bad, *good;
14718
14719 if (argvars[0].v_type != VAR_LIST)
14720 return;
14721 l = argvars[0].vval.v_list;
14722 if (l == NULL)
14723 return;
14724 li = l->lv_first;
14725 if (li == NULL)
14726 return;
14727 bad = get_tv_string(&li->li_tv);
14728 li = li->li_next;
14729 if (li == NULL)
14730 return;
14731 good = get_tv_string(&li->li_tv);
14732 rettv->vval.v_number = test_edit_score(bad, good);
14733#endif
14734}
14735
14736/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014737 * "tolower(string)" function
14738 */
14739 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014740f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014741 typval_T *argvars;
14742 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014743{
14744 char_u *p;
14745
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014746 p = vim_strsave(get_tv_string(&argvars[0]));
14747 rettv->v_type = VAR_STRING;
14748 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014749
14750 if (p != NULL)
14751 while (*p != NUL)
14752 {
14753#ifdef FEAT_MBYTE
14754 int l;
14755
14756 if (enc_utf8)
14757 {
14758 int c, lc;
14759
14760 c = utf_ptr2char(p);
14761 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014762 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014763 /* TODO: reallocate string when byte count changes. */
14764 if (utf_char2len(lc) == l)
14765 utf_char2bytes(lc, p);
14766 p += l;
14767 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014768 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014769 p += l; /* skip multi-byte character */
14770 else
14771#endif
14772 {
14773 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
14774 ++p;
14775 }
14776 }
14777}
14778
14779/*
14780 * "toupper(string)" function
14781 */
14782 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014783f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014784 typval_T *argvars;
14785 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014786{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014787 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014788 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014789}
14790
14791/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000014792 * "tr(string, fromstr, tostr)" function
14793 */
14794 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014795f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014796 typval_T *argvars;
14797 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014798{
14799 char_u *instr;
14800 char_u *fromstr;
14801 char_u *tostr;
14802 char_u *p;
14803#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000014804 int inlen;
14805 int fromlen;
14806 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014807 int idx;
14808 char_u *cpstr;
14809 int cplen;
14810 int first = TRUE;
14811#endif
14812 char_u buf[NUMBUFLEN];
14813 char_u buf2[NUMBUFLEN];
14814 garray_T ga;
14815
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014816 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014817 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
14818 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014819
14820 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014821 rettv->v_type = VAR_STRING;
14822 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014823 if (fromstr == NULL || tostr == NULL)
14824 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000014825 ga_init2(&ga, (int)sizeof(char), 80);
14826
14827#ifdef FEAT_MBYTE
14828 if (!has_mbyte)
14829#endif
14830 /* not multi-byte: fromstr and tostr must be the same length */
14831 if (STRLEN(fromstr) != STRLEN(tostr))
14832 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014833#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000014834error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014835#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000014836 EMSG2(_(e_invarg2), fromstr);
14837 ga_clear(&ga);
14838 return;
14839 }
14840
14841 /* fromstr and tostr have to contain the same number of chars */
14842 while (*instr != NUL)
14843 {
14844#ifdef FEAT_MBYTE
14845 if (has_mbyte)
14846 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014847 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014848 cpstr = instr;
14849 cplen = inlen;
14850 idx = 0;
14851 for (p = fromstr; *p != NUL; p += fromlen)
14852 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014853 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014854 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
14855 {
14856 for (p = tostr; *p != NUL; p += tolen)
14857 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014858 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014859 if (idx-- == 0)
14860 {
14861 cplen = tolen;
14862 cpstr = p;
14863 break;
14864 }
14865 }
14866 if (*p == NUL) /* tostr is shorter than fromstr */
14867 goto error;
14868 break;
14869 }
14870 ++idx;
14871 }
14872
14873 if (first && cpstr == instr)
14874 {
14875 /* Check that fromstr and tostr have the same number of
14876 * (multi-byte) characters. Done only once when a character
14877 * of instr doesn't appear in fromstr. */
14878 first = FALSE;
14879 for (p = tostr; *p != NUL; p += tolen)
14880 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014881 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014882 --idx;
14883 }
14884 if (idx != 0)
14885 goto error;
14886 }
14887
14888 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000014889 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014890 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014891
14892 instr += inlen;
14893 }
14894 else
14895#endif
14896 {
14897 /* When not using multi-byte chars we can do it faster. */
14898 p = vim_strchr(fromstr, *instr);
14899 if (p != NULL)
14900 ga_append(&ga, tostr[p - fromstr]);
14901 else
14902 ga_append(&ga, *instr);
14903 ++instr;
14904 }
14905 }
14906
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014907 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014908}
14909
14910/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014911 * "type(expr)" function
14912 */
14913 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014914f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014915 typval_T *argvars;
14916 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014917{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014918 int n;
14919
14920 switch (argvars[0].v_type)
14921 {
14922 case VAR_NUMBER: n = 0; break;
14923 case VAR_STRING: n = 1; break;
14924 case VAR_FUNC: n = 2; break;
14925 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014926 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014927 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
14928 }
14929 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014930}
14931
14932/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014933 * "values(dict)" function
14934 */
14935 static void
14936f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014937 typval_T *argvars;
14938 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014939{
14940 dict_list(argvars, rettv, 1);
14941}
14942
14943/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014944 * "virtcol(string)" function
14945 */
14946 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014947f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014948 typval_T *argvars;
14949 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014950{
14951 colnr_T vcol = 0;
14952 pos_T *fp;
14953
14954 fp = var2fpos(&argvars[0], FALSE);
14955 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count)
14956 {
14957 getvvcol(curwin, fp, NULL, NULL, &vcol);
14958 ++vcol;
14959 }
14960
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014961 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014962}
14963
14964/*
14965 * "visualmode()" function
14966 */
14967/*ARGSUSED*/
14968 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014969f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014970 typval_T *argvars;
14971 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014972{
14973#ifdef FEAT_VISUAL
14974 char_u str[2];
14975
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014976 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014977 str[0] = curbuf->b_visual_mode_eval;
14978 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014979 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014980
14981 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014982 if ((argvars[0].v_type == VAR_NUMBER
14983 && argvars[0].vval.v_number != 0)
14984 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014985 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014986 curbuf->b_visual_mode_eval = NUL;
14987#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014988 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014989#endif
14990}
14991
14992/*
14993 * "winbufnr(nr)" function
14994 */
14995 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014996f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014997 typval_T *argvars;
14998 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014999{
15000 win_T *wp;
15001
15002 wp = find_win_by_nr(&argvars[0]);
15003 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015004 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015005 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015006 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015007}
15008
15009/*
15010 * "wincol()" function
15011 */
15012/*ARGSUSED*/
15013 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015014f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015015 typval_T *argvars;
15016 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015017{
15018 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015019 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015020}
15021
15022/*
15023 * "winheight(nr)" function
15024 */
15025 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015026f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015027 typval_T *argvars;
15028 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015029{
15030 win_T *wp;
15031
15032 wp = find_win_by_nr(&argvars[0]);
15033 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015034 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015035 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015036 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015037}
15038
15039/*
15040 * "winline()" function
15041 */
15042/*ARGSUSED*/
15043 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015044f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015045 typval_T *argvars;
15046 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015047{
15048 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015049 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015050}
15051
15052/*
15053 * "winnr()" function
15054 */
15055/* ARGSUSED */
15056 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015057f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015058 typval_T *argvars;
15059 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015060{
15061 int nr = 1;
15062#ifdef FEAT_WINDOWS
15063 win_T *wp;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015064 win_T *twin = curwin;
15065 char_u *arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015066
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015067 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015068 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015069 arg = get_tv_string_chk(&argvars[0]);
15070 if (arg == NULL)
15071 nr = 0; /* type error; errmsg already given */
15072 else if (STRCMP(arg, "$") == 0)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015073 twin = lastwin;
15074 else if (STRCMP(arg, "#") == 0)
15075 {
15076 twin = prevwin;
15077 if (prevwin == NULL)
15078 nr = 0;
15079 }
15080 else
15081 {
15082 EMSG2(_(e_invexpr2), arg);
15083 nr = 0;
15084 }
15085 }
15086
15087 if (nr > 0)
15088 for (wp = firstwin; wp != twin; wp = wp->w_next)
15089 ++nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015090#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015091 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015092}
15093
15094/*
15095 * "winrestcmd()" function
15096 */
15097/* ARGSUSED */
15098 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015099f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015100 typval_T *argvars;
15101 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015102{
15103#ifdef FEAT_WINDOWS
15104 win_T *wp;
15105 int winnr = 1;
15106 garray_T ga;
15107 char_u buf[50];
15108
15109 ga_init2(&ga, (int)sizeof(char), 70);
15110 for (wp = firstwin; wp != NULL; wp = wp->w_next)
15111 {
15112 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
15113 ga_concat(&ga, buf);
15114# ifdef FEAT_VERTSPLIT
15115 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
15116 ga_concat(&ga, buf);
15117# endif
15118 ++winnr;
15119 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000015120 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015121
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015122 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015123#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015124 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015125#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015126 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015127}
15128
15129/*
15130 * "winwidth(nr)" function
15131 */
15132 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015133f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015134 typval_T *argvars;
15135 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015136{
15137 win_T *wp;
15138
15139 wp = find_win_by_nr(&argvars[0]);
15140 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015141 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015142 else
15143#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015144 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015145#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015146 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015147#endif
15148}
15149
Bram Moolenaar071d4272004-06-13 20:20:40 +000015150/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015151 * "writefile()" function
15152 */
15153 static void
15154f_writefile(argvars, rettv)
15155 typval_T *argvars;
15156 typval_T *rettv;
15157{
15158 int binary = FALSE;
15159 char_u *fname;
15160 FILE *fd;
15161 listitem_T *li;
15162 char_u *s;
15163 int ret = 0;
15164 int c;
15165
15166 if (argvars[0].v_type != VAR_LIST)
15167 {
15168 EMSG2(_(e_listarg), "writefile()");
15169 return;
15170 }
15171 if (argvars[0].vval.v_list == NULL)
15172 return;
15173
15174 if (argvars[2].v_type != VAR_UNKNOWN
15175 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
15176 binary = TRUE;
15177
15178 /* Always open the file in binary mode, library functions have a mind of
15179 * their own about CR-LF conversion. */
15180 fname = get_tv_string(&argvars[1]);
15181 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
15182 {
15183 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
15184 ret = -1;
15185 }
15186 else
15187 {
15188 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
15189 li = li->li_next)
15190 {
15191 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
15192 {
15193 if (*s == '\n')
15194 c = putc(NUL, fd);
15195 else
15196 c = putc(*s, fd);
15197 if (c == EOF)
15198 {
15199 ret = -1;
15200 break;
15201 }
15202 }
15203 if (!binary || li->li_next != NULL)
15204 if (putc('\n', fd) == EOF)
15205 {
15206 ret = -1;
15207 break;
15208 }
15209 if (ret < 0)
15210 {
15211 EMSG(_(e_write));
15212 break;
15213 }
15214 }
15215 fclose(fd);
15216 }
15217
15218 rettv->vval.v_number = ret;
15219}
15220
15221/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015222 * Translate a String variable into a position.
15223 */
15224 static pos_T *
15225var2fpos(varp, lnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000015226 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015227 int lnum; /* TRUE when $ is last line */
15228{
15229 char_u *name;
15230 static pos_T pos;
15231 pos_T *pp;
15232
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015233 name = get_tv_string_chk(varp);
15234 if (name == NULL)
15235 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015236 if (name[0] == '.') /* cursor */
15237 return &curwin->w_cursor;
15238 if (name[0] == '\'') /* mark */
15239 {
15240 pp = getmark(name[1], FALSE);
15241 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
15242 return NULL;
15243 return pp;
15244 }
15245 if (name[0] == '$') /* last column or line */
15246 {
15247 if (lnum)
15248 {
15249 pos.lnum = curbuf->b_ml.ml_line_count;
15250 pos.col = 0;
15251 }
15252 else
15253 {
15254 pos.lnum = curwin->w_cursor.lnum;
15255 pos.col = (colnr_T)STRLEN(ml_get_curline());
15256 }
15257 return &pos;
15258 }
15259 return NULL;
15260}
15261
15262/*
15263 * Get the length of an environment variable name.
15264 * Advance "arg" to the first character after the name.
15265 * Return 0 for error.
15266 */
15267 static int
15268get_env_len(arg)
15269 char_u **arg;
15270{
15271 char_u *p;
15272 int len;
15273
15274 for (p = *arg; vim_isIDc(*p); ++p)
15275 ;
15276 if (p == *arg) /* no name found */
15277 return 0;
15278
15279 len = (int)(p - *arg);
15280 *arg = p;
15281 return len;
15282}
15283
15284/*
15285 * Get the length of the name of a function or internal variable.
15286 * "arg" is advanced to the first non-white character after the name.
15287 * Return 0 if something is wrong.
15288 */
15289 static int
15290get_id_len(arg)
15291 char_u **arg;
15292{
15293 char_u *p;
15294 int len;
15295
15296 /* Find the end of the name. */
15297 for (p = *arg; eval_isnamec(*p); ++p)
15298 ;
15299 if (p == *arg) /* no name found */
15300 return 0;
15301
15302 len = (int)(p - *arg);
15303 *arg = skipwhite(p);
15304
15305 return len;
15306}
15307
15308/*
Bram Moolenaara7043832005-01-21 11:56:39 +000015309 * Get the length of the name of a variable or function.
15310 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000015311 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015312 * Return -1 if curly braces expansion failed.
15313 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015314 * If the name contains 'magic' {}'s, expand them and return the
15315 * expanded name in an allocated string via 'alias' - caller must free.
15316 */
15317 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015318get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015319 char_u **arg;
15320 char_u **alias;
15321 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015322 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015323{
15324 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015325 char_u *p;
15326 char_u *expr_start;
15327 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015328
15329 *alias = NULL; /* default to no alias */
15330
15331 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
15332 && (*arg)[2] == (int)KE_SNR)
15333 {
15334 /* hard coded <SNR>, already translated */
15335 *arg += 3;
15336 return get_id_len(arg) + 3;
15337 }
15338 len = eval_fname_script(*arg);
15339 if (len > 0)
15340 {
15341 /* literal "<SID>", "s:" or "<SNR>" */
15342 *arg += len;
15343 }
15344
Bram Moolenaar071d4272004-06-13 20:20:40 +000015345 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015346 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015347 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015348 p = find_name_end(*arg, &expr_start, &expr_end,
15349 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015350 if (expr_start != NULL)
15351 {
15352 char_u *temp_string;
15353
15354 if (!evaluate)
15355 {
15356 len += (int)(p - *arg);
15357 *arg = skipwhite(p);
15358 return len;
15359 }
15360
15361 /*
15362 * Include any <SID> etc in the expanded string:
15363 * Thus the -len here.
15364 */
15365 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
15366 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015367 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015368 *alias = temp_string;
15369 *arg = skipwhite(p);
15370 return (int)STRLEN(temp_string);
15371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015372
15373 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015374 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015375 EMSG2(_(e_invexpr2), *arg);
15376
15377 return len;
15378}
15379
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015380/*
15381 * Find the end of a variable or function name, taking care of magic braces.
15382 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
15383 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015384 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015385 * Return a pointer to just after the name. Equal to "arg" if there is no
15386 * valid name.
15387 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015388 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015389find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015390 char_u *arg;
15391 char_u **expr_start;
15392 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015393 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015394{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015395 int mb_nest = 0;
15396 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015397 char_u *p;
15398
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015399 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015400 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015401 *expr_start = NULL;
15402 *expr_end = NULL;
15403 }
15404
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015405 /* Quick check for valid starting character. */
15406 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
15407 return arg;
15408
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015409 for (p = arg; *p != NUL
15410 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015411 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015412 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015413 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000015414 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015415 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000015416 if (*p == '\'')
15417 {
15418 /* skip over 'string' to avoid counting [ and ] inside it. */
15419 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
15420 ;
15421 if (*p == NUL)
15422 break;
15423 }
15424 else if (*p == '"')
15425 {
15426 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
15427 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
15428 if (*p == '\\' && p[1] != NUL)
15429 ++p;
15430 if (*p == NUL)
15431 break;
15432 }
15433
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015434 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015435 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015436 if (*p == '[')
15437 ++br_nest;
15438 else if (*p == ']')
15439 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015440 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000015441
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015442 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015443 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015444 if (*p == '{')
15445 {
15446 mb_nest++;
15447 if (expr_start != NULL && *expr_start == NULL)
15448 *expr_start = p;
15449 }
15450 else if (*p == '}')
15451 {
15452 mb_nest--;
15453 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
15454 *expr_end = p;
15455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015457 }
15458
15459 return p;
15460}
15461
15462/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015463 * Expands out the 'magic' {}'s in a variable/function name.
15464 * Note that this can call itself recursively, to deal with
15465 * constructs like foo{bar}{baz}{bam}
15466 * The four pointer arguments point to "foo{expre}ss{ion}bar"
15467 * "in_start" ^
15468 * "expr_start" ^
15469 * "expr_end" ^
15470 * "in_end" ^
15471 *
15472 * Returns a new allocated string, which the caller must free.
15473 * Returns NULL for failure.
15474 */
15475 static char_u *
15476make_expanded_name(in_start, expr_start, expr_end, in_end)
15477 char_u *in_start;
15478 char_u *expr_start;
15479 char_u *expr_end;
15480 char_u *in_end;
15481{
15482 char_u c1;
15483 char_u *retval = NULL;
15484 char_u *temp_result;
15485 char_u *nextcmd = NULL;
15486
15487 if (expr_end == NULL || in_end == NULL)
15488 return NULL;
15489 *expr_start = NUL;
15490 *expr_end = NUL;
15491 c1 = *in_end;
15492 *in_end = NUL;
15493
15494 temp_result = eval_to_string(expr_start + 1, &nextcmd);
15495 if (temp_result != NULL && nextcmd == NULL)
15496 {
15497 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
15498 + (in_end - expr_end) + 1));
15499 if (retval != NULL)
15500 {
15501 STRCPY(retval, in_start);
15502 STRCAT(retval, temp_result);
15503 STRCAT(retval, expr_end + 1);
15504 }
15505 }
15506 vim_free(temp_result);
15507
15508 *in_end = c1; /* put char back for error messages */
15509 *expr_start = '{';
15510 *expr_end = '}';
15511
15512 if (retval != NULL)
15513 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015514 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015515 if (expr_start != NULL)
15516 {
15517 /* Further expansion! */
15518 temp_result = make_expanded_name(retval, expr_start,
15519 expr_end, temp_result);
15520 vim_free(retval);
15521 retval = temp_result;
15522 }
15523 }
15524
15525 return retval;
15526}
15527
15528/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015529 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000015530 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015531 */
15532 static int
15533eval_isnamec(c)
15534 int c;
15535{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015536 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
15537}
15538
15539/*
15540 * Return TRUE if character "c" can be used as the first character in a
15541 * variable or function name (excluding '{' and '}').
15542 */
15543 static int
15544eval_isnamec1(c)
15545 int c;
15546{
15547 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000015548}
15549
15550/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015551 * Set number v: variable to "val".
15552 */
15553 void
15554set_vim_var_nr(idx, val)
15555 int idx;
15556 long val;
15557{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015558 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015559}
15560
15561/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015562 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015563 */
15564 long
15565get_vim_var_nr(idx)
15566 int idx;
15567{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015568 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015569}
15570
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015571#if defined(FEAT_AUTOCMD) || defined(PROTO)
15572/*
15573 * Get string v: variable value. Uses a static buffer, can only be used once.
15574 */
15575 char_u *
15576get_vim_var_str(idx)
15577 int idx;
15578{
15579 return get_tv_string(&vimvars[idx].vv_tv);
15580}
15581#endif
15582
Bram Moolenaar071d4272004-06-13 20:20:40 +000015583/*
15584 * Set v:count, v:count1 and v:prevcount.
15585 */
15586 void
15587set_vcount(count, count1)
15588 long count;
15589 long count1;
15590{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015591 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
15592 vimvars[VV_COUNT].vv_nr = count;
15593 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015594}
15595
15596/*
15597 * Set string v: variable to a copy of "val".
15598 */
15599 void
15600set_vim_var_string(idx, val, len)
15601 int idx;
15602 char_u *val;
15603 int len; /* length of "val" to use or -1 (whole string) */
15604{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015605 /* Need to do this (at least) once, since we can't initialize a union.
15606 * Will always be invoked when "v:progname" is set. */
15607 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
15608
Bram Moolenaare9a41262005-01-15 22:18:47 +000015609 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015610 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015611 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015612 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015613 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015614 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000015615 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015616}
15617
15618/*
15619 * Set v:register if needed.
15620 */
15621 void
15622set_reg_var(c)
15623 int c;
15624{
15625 char_u regname;
15626
15627 if (c == 0 || c == ' ')
15628 regname = '"';
15629 else
15630 regname = c;
15631 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000015632 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015633 set_vim_var_string(VV_REG, &regname, 1);
15634}
15635
15636/*
15637 * Get or set v:exception. If "oldval" == NULL, return the current value.
15638 * Otherwise, restore the value to "oldval" and return NULL.
15639 * Must always be called in pairs to save and restore v:exception! Does not
15640 * take care of memory allocations.
15641 */
15642 char_u *
15643v_exception(oldval)
15644 char_u *oldval;
15645{
15646 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015647 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015648
Bram Moolenaare9a41262005-01-15 22:18:47 +000015649 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015650 return NULL;
15651}
15652
15653/*
15654 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
15655 * Otherwise, restore the value to "oldval" and return NULL.
15656 * Must always be called in pairs to save and restore v:throwpoint! Does not
15657 * take care of memory allocations.
15658 */
15659 char_u *
15660v_throwpoint(oldval)
15661 char_u *oldval;
15662{
15663 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015664 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015665
Bram Moolenaare9a41262005-01-15 22:18:47 +000015666 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015667 return NULL;
15668}
15669
15670#if defined(FEAT_AUTOCMD) || defined(PROTO)
15671/*
15672 * Set v:cmdarg.
15673 * If "eap" != NULL, use "eap" to generate the value and return the old value.
15674 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
15675 * Must always be called in pairs!
15676 */
15677 char_u *
15678set_cmdarg(eap, oldarg)
15679 exarg_T *eap;
15680 char_u *oldarg;
15681{
15682 char_u *oldval;
15683 char_u *newval;
15684 unsigned len;
15685
Bram Moolenaare9a41262005-01-15 22:18:47 +000015686 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015687 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015688 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015689 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000015690 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015691 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015692 }
15693
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015694 if (eap->force_bin == FORCE_BIN)
15695 len = 6;
15696 else if (eap->force_bin == FORCE_NOBIN)
15697 len = 8;
15698 else
15699 len = 0;
15700 if (eap->force_ff != 0)
15701 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
15702# ifdef FEAT_MBYTE
15703 if (eap->force_enc != 0)
15704 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
15705# endif
15706
15707 newval = alloc(len + 1);
15708 if (newval == NULL)
15709 return NULL;
15710
15711 if (eap->force_bin == FORCE_BIN)
15712 sprintf((char *)newval, " ++bin");
15713 else if (eap->force_bin == FORCE_NOBIN)
15714 sprintf((char *)newval, " ++nobin");
15715 else
15716 *newval = NUL;
15717 if (eap->force_ff != 0)
15718 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
15719 eap->cmd + eap->force_ff);
15720# ifdef FEAT_MBYTE
15721 if (eap->force_enc != 0)
15722 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
15723 eap->cmd + eap->force_enc);
15724# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000015725 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015726 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015727}
15728#endif
15729
15730/*
15731 * Get the value of internal variable "name".
15732 * Return OK or FAIL.
15733 */
15734 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015735get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015736 char_u *name;
15737 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000015738 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015739 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015740{
15741 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000015742 typval_T *tv = NULL;
15743 typval_T atv;
15744 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015745 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015746
15747 /* truncate the name, so that we can use strcmp() */
15748 cc = name[len];
15749 name[len] = NUL;
15750
15751 /*
15752 * Check for "b:changedtick".
15753 */
15754 if (STRCMP(name, "b:changedtick") == 0)
15755 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000015756 atv.v_type = VAR_NUMBER;
15757 atv.vval.v_number = curbuf->b_changedtick;
15758 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015759 }
15760
15761 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015762 * Check for user-defined variables.
15763 */
15764 else
15765 {
Bram Moolenaara7043832005-01-21 11:56:39 +000015766 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015767 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000015768 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015769 }
15770
Bram Moolenaare9a41262005-01-15 22:18:47 +000015771 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015772 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015773 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015774 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015775 ret = FAIL;
15776 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015777 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015778 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015779
15780 name[len] = cc;
15781
15782 return ret;
15783}
15784
15785/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015786 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
15787 * Also handle function call with Funcref variable: func(expr)
15788 * Can all be combined: dict.func(expr)[idx]['func'](expr)
15789 */
15790 static int
15791handle_subscript(arg, rettv, evaluate, verbose)
15792 char_u **arg;
15793 typval_T *rettv;
15794 int evaluate; /* do more than finding the end */
15795 int verbose; /* give error messages */
15796{
15797 int ret = OK;
15798 dict_T *selfdict = NULL;
15799 char_u *s;
15800 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000015801 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015802
15803 while (ret == OK
15804 && (**arg == '['
15805 || (**arg == '.' && rettv->v_type == VAR_DICT)
15806 || (**arg == '(' && rettv->v_type == VAR_FUNC))
15807 && !vim_iswhite(*(*arg - 1)))
15808 {
15809 if (**arg == '(')
15810 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000015811 /* need to copy the funcref so that we can clear rettv */
15812 functv = *rettv;
15813 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015814
15815 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000015816 s = functv.vval.v_string;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015817 ret = get_func_tv(s, STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000015818 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
15819 &len, evaluate, selfdict);
15820
15821 /* Clear the funcref afterwards, so that deleting it while
15822 * evaluating the arguments is possible (see test55). */
15823 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015824
15825 /* Stop the expression evaluation when immediately aborting on
15826 * error, or when an interrupt occurred or an exception was thrown
15827 * but not caught. */
15828 if (aborting())
15829 {
15830 if (ret == OK)
15831 clear_tv(rettv);
15832 ret = FAIL;
15833 }
15834 dict_unref(selfdict);
15835 selfdict = NULL;
15836 }
15837 else /* **arg == '[' || **arg == '.' */
15838 {
15839 dict_unref(selfdict);
15840 if (rettv->v_type == VAR_DICT)
15841 {
15842 selfdict = rettv->vval.v_dict;
15843 if (selfdict != NULL)
15844 ++selfdict->dv_refcount;
15845 }
15846 else
15847 selfdict = NULL;
15848 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
15849 {
15850 clear_tv(rettv);
15851 ret = FAIL;
15852 }
15853 }
15854 }
15855 dict_unref(selfdict);
15856 return ret;
15857}
15858
15859/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015860 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
15861 * value).
15862 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015863 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015864alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015865{
Bram Moolenaar33570922005-01-25 22:26:29 +000015866 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015867}
15868
15869/*
15870 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015871 * The string "s" must have been allocated, it is consumed.
15872 * Return NULL for out of memory, the variable otherwise.
15873 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015874 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015875alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015876 char_u *s;
15877{
Bram Moolenaar33570922005-01-25 22:26:29 +000015878 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015879
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015880 rettv = alloc_tv();
15881 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015882 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015883 rettv->v_type = VAR_STRING;
15884 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015885 }
15886 else
15887 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015888 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015889}
15890
15891/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015892 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015893 */
15894 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015895free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015896 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015897{
15898 if (varp != NULL)
15899 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015900 switch (varp->v_type)
15901 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015902 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015903 func_unref(varp->vval.v_string);
15904 /*FALLTHROUGH*/
15905 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015906 vim_free(varp->vval.v_string);
15907 break;
15908 case VAR_LIST:
15909 list_unref(varp->vval.v_list);
15910 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015911 case VAR_DICT:
15912 dict_unref(varp->vval.v_dict);
15913 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015914 case VAR_NUMBER:
15915 case VAR_UNKNOWN:
15916 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015917 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000015918 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015919 break;
15920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015921 vim_free(varp);
15922 }
15923}
15924
15925/*
15926 * Free the memory for a variable value and set the value to NULL or 0.
15927 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000015928 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015929clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015930 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015931{
15932 if (varp != NULL)
15933 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015934 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015935 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015936 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015937 func_unref(varp->vval.v_string);
15938 /*FALLTHROUGH*/
15939 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015940 vim_free(varp->vval.v_string);
15941 varp->vval.v_string = NULL;
15942 break;
15943 case VAR_LIST:
15944 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015945 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015946 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015947 case VAR_DICT:
15948 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015949 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015950 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015951 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015952 varp->vval.v_number = 0;
15953 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015954 case VAR_UNKNOWN:
15955 break;
15956 default:
15957 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000015958 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015959 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015960 }
15961}
15962
15963/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015964 * Set the value of a variable to NULL without freeing items.
15965 */
15966 static void
15967init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015968 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015969{
15970 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000015971 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015972}
15973
15974/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015975 * Get the number value of a variable.
15976 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015977 * For incompatible types, return 0.
15978 * get_tv_number_chk() is similar to get_tv_number(), but informs the
15979 * caller of incompatible types: it sets *denote to TRUE if "denote"
15980 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015981 */
15982 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015983get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015984 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015985{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015986 int error = FALSE;
15987
15988 return get_tv_number_chk(varp, &error); /* return 0L on error */
15989}
15990
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015991 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015992get_tv_number_chk(varp, denote)
15993 typval_T *varp;
15994 int *denote;
15995{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015996 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015997
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015998 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015999 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016000 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016001 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016002 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016003 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016004 break;
16005 case VAR_STRING:
16006 if (varp->vval.v_string != NULL)
16007 vim_str2nr(varp->vval.v_string, NULL, NULL,
16008 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016009 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016010 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000016011 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016012 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016013 case VAR_DICT:
16014 EMSG(_("E728: Using a Dictionary as a number"));
16015 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016016 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016017 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016018 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016019 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016020 if (denote == NULL) /* useful for values that must be unsigned */
16021 n = -1;
16022 else
16023 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016024 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016025}
16026
16027/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000016028 * Get the lnum from the first argument.
16029 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016030 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016031 */
16032 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016033get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000016034 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016035{
Bram Moolenaar33570922005-01-25 22:26:29 +000016036 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016037 linenr_T lnum;
16038
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016039 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016040 if (lnum == 0) /* no valid number, try using line() */
16041 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016042 rettv.v_type = VAR_NUMBER;
16043 f_line(argvars, &rettv);
16044 lnum = rettv.vval.v_number;
16045 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016046 }
16047 return lnum;
16048}
16049
16050/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000016051 * Get the lnum from the first argument.
16052 * Also accepts "$", then "buf" is used.
16053 * Returns 0 on error.
16054 */
16055 static linenr_T
16056get_tv_lnum_buf(argvars, buf)
16057 typval_T *argvars;
16058 buf_T *buf;
16059{
16060 if (argvars[0].v_type == VAR_STRING
16061 && argvars[0].vval.v_string != NULL
16062 && argvars[0].vval.v_string[0] == '$'
16063 && buf != NULL)
16064 return buf->b_ml.ml_line_count;
16065 return get_tv_number_chk(&argvars[0], NULL);
16066}
16067
16068/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016069 * Get the string value of a variable.
16070 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000016071 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
16072 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016073 * If the String variable has never been set, return an empty string.
16074 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016075 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
16076 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016077 */
16078 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016079get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016080 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016081{
16082 static char_u mybuf[NUMBUFLEN];
16083
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016084 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016085}
16086
16087 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016088get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000016089 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016090 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016091{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016092 char_u *res = get_tv_string_buf_chk(varp, buf);
16093
16094 return res != NULL ? res : (char_u *)"";
16095}
16096
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016097 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016098get_tv_string_chk(varp)
16099 typval_T *varp;
16100{
16101 static char_u mybuf[NUMBUFLEN];
16102
16103 return get_tv_string_buf_chk(varp, mybuf);
16104}
16105
16106 static char_u *
16107get_tv_string_buf_chk(varp, buf)
16108 typval_T *varp;
16109 char_u *buf;
16110{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016111 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016112 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016113 case VAR_NUMBER:
16114 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
16115 return buf;
16116 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016117 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016118 break;
16119 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016120 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016121 break;
16122 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016123 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016124 break;
16125 case VAR_STRING:
16126 if (varp->vval.v_string != NULL)
16127 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016128 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016129 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016130 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016131 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016132 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016133 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016134}
16135
16136/*
16137 * Find variable "name" in the list of variables.
16138 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016139 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000016140 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000016141 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016142 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016143 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000016144find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016145 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000016146 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016147{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016148 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016149 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016150
Bram Moolenaara7043832005-01-21 11:56:39 +000016151 ht = find_var_ht(name, &varname);
16152 if (htp != NULL)
16153 *htp = ht;
16154 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016155 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016156 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016157}
16158
16159/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016160 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000016161 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016162 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016163 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016164find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000016165 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000016166 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016167 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000016168{
Bram Moolenaar33570922005-01-25 22:26:29 +000016169 hashitem_T *hi;
16170
16171 if (*varname == NUL)
16172 {
16173 /* Must be something like "s:", otherwise "ht" would be NULL. */
16174 switch (varname[-2])
16175 {
16176 case 's': return &SCRIPT_SV(current_SID).sv_var;
16177 case 'g': return &globvars_var;
16178 case 'v': return &vimvars_var;
16179 case 'b': return &curbuf->b_bufvar;
16180 case 'w': return &curwin->w_winvar;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016181 case 'l': return current_funccal == NULL
16182 ? NULL : &current_funccal->l_vars_var;
16183 case 'a': return current_funccal == NULL
16184 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000016185 }
16186 return NULL;
16187 }
Bram Moolenaara7043832005-01-21 11:56:39 +000016188
16189 hi = hash_find(ht, varname);
16190 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016191 {
16192 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000016193 * worked find the variable again. Don't auto-load a script if it was
16194 * loaded already, otherwise it would be loaded every time when
16195 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016196 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000016197 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016198 hi = hash_find(ht, varname);
16199 if (HASHITEM_EMPTY(hi))
16200 return NULL;
16201 }
Bram Moolenaar33570922005-01-25 22:26:29 +000016202 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000016203}
16204
16205/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016206 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000016207 * Set "varname" to the start of name without ':'.
16208 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016209 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000016210find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016211 char_u *name;
16212 char_u **varname;
16213{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000016214 hashitem_T *hi;
16215
Bram Moolenaar071d4272004-06-13 20:20:40 +000016216 if (name[1] != ':')
16217 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016218 /* The name must not start with a colon or #. */
16219 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016220 return NULL;
16221 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016222
16223 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000016224 hi = hash_find(&compat_hashtab, name);
16225 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000016226 return &compat_hashtab;
16227
Bram Moolenaar071d4272004-06-13 20:20:40 +000016228 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016229 return &globvarht; /* global variable */
16230 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016231 }
16232 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016233 if (*name == 'g') /* global variable */
16234 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016235 /* There must be no ':' or '#' in the rest of the name, unless g: is used
16236 */
16237 if (vim_strchr(name + 2, ':') != NULL
16238 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016239 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016240 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000016241 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016242 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000016243 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000016244 if (*name == 'v') /* v: variable */
16245 return &vimvarht;
16246 if (*name == 'a' && current_funccal != NULL) /* function argument */
16247 return &current_funccal->l_avars.dv_hashtab;
16248 if (*name == 'l' && current_funccal != NULL) /* local function variable */
16249 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016250 if (*name == 's' /* script variable */
16251 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
16252 return &SCRIPT_VARS(current_SID);
16253 return NULL;
16254}
16255
16256/*
16257 * Get the string value of a (global/local) variable.
16258 * Returns NULL when it doesn't exist.
16259 */
16260 char_u *
16261get_var_value(name)
16262 char_u *name;
16263{
Bram Moolenaar33570922005-01-25 22:26:29 +000016264 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016265
Bram Moolenaara7043832005-01-21 11:56:39 +000016266 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016267 if (v == NULL)
16268 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000016269 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016270}
16271
16272/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016273 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000016274 * sourcing this script and when executing functions defined in the script.
16275 */
16276 void
16277new_script_vars(id)
16278 scid_T id;
16279{
Bram Moolenaara7043832005-01-21 11:56:39 +000016280 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000016281 hashtab_T *ht;
16282 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000016283
Bram Moolenaar071d4272004-06-13 20:20:40 +000016284 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
16285 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016286 /* Re-allocating ga_data means that an ht_array pointing to
16287 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000016288 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000016289 for (i = 1; i <= ga_scripts.ga_len; ++i)
16290 {
16291 ht = &SCRIPT_VARS(i);
16292 if (ht->ht_mask == HT_INIT_SIZE - 1)
16293 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000016294 sv = &SCRIPT_SV(i);
16295 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000016296 }
16297
Bram Moolenaar071d4272004-06-13 20:20:40 +000016298 while (ga_scripts.ga_len < id)
16299 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016300 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
16301 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016302 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016303 }
16304 }
16305}
16306
16307/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016308 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
16309 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016310 */
16311 void
Bram Moolenaar33570922005-01-25 22:26:29 +000016312init_var_dict(dict, dict_var)
16313 dict_T *dict;
16314 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016315{
Bram Moolenaar33570922005-01-25 22:26:29 +000016316 hash_init(&dict->dv_hashtab);
16317 dict->dv_refcount = 99999;
16318 dict_var->di_tv.vval.v_dict = dict;
16319 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016320 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016321 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
16322 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016323}
16324
16325/*
16326 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000016327 * Frees all allocated variables and the value they contain.
16328 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016329 */
16330 void
Bram Moolenaara7043832005-01-21 11:56:39 +000016331vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000016332 hashtab_T *ht;
16333{
16334 vars_clear_ext(ht, TRUE);
16335}
16336
16337/*
16338 * Like vars_clear(), but only free the value if "free_val" is TRUE.
16339 */
16340 static void
16341vars_clear_ext(ht, free_val)
16342 hashtab_T *ht;
16343 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016344{
Bram Moolenaara7043832005-01-21 11:56:39 +000016345 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000016346 hashitem_T *hi;
16347 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016348
Bram Moolenaar33570922005-01-25 22:26:29 +000016349 hash_lock(ht);
Bram Moolenaara7043832005-01-21 11:56:39 +000016350 todo = ht->ht_used;
16351 for (hi = ht->ht_array; todo > 0; ++hi)
16352 {
16353 if (!HASHITEM_EMPTY(hi))
16354 {
16355 --todo;
16356
Bram Moolenaar33570922005-01-25 22:26:29 +000016357 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000016358 * ht_array might change then. hash_clear() takes care of it
16359 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016360 v = HI2DI(hi);
16361 if (free_val)
16362 clear_tv(&v->di_tv);
16363 if ((v->di_flags & DI_FLAGS_FIX) == 0)
16364 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000016365 }
16366 }
16367 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016368 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016369}
16370
Bram Moolenaara7043832005-01-21 11:56:39 +000016371/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016372 * Delete a variable from hashtab "ht" at item "hi".
16373 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000016374 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016375 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000016376delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000016377 hashtab_T *ht;
16378 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016379{
Bram Moolenaar33570922005-01-25 22:26:29 +000016380 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000016381
16382 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000016383 clear_tv(&di->di_tv);
16384 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016385}
16386
16387/*
16388 * List the value of one internal variable.
16389 */
16390 static void
16391list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000016392 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016393 char_u *prefix;
16394{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016395 char_u *tofree;
16396 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016397 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016398
Bram Moolenaar33570922005-01-25 22:26:29 +000016399 s = echo_string(&v->di_tv, &tofree, numbuf);
16400 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016401 s == NULL ? (char_u *)"" : s);
16402 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016403}
16404
Bram Moolenaar071d4272004-06-13 20:20:40 +000016405 static void
16406list_one_var_a(prefix, name, type, string)
16407 char_u *prefix;
16408 char_u *name;
16409 int type;
16410 char_u *string;
16411{
16412 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
16413 if (name != NULL) /* "a:" vars don't have a name stored */
16414 msg_puts(name);
16415 msg_putchar(' ');
16416 msg_advance(22);
16417 if (type == VAR_NUMBER)
16418 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016419 else if (type == VAR_FUNC)
16420 msg_putchar('*');
16421 else if (type == VAR_LIST)
16422 {
16423 msg_putchar('[');
16424 if (*string == '[')
16425 ++string;
16426 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016427 else if (type == VAR_DICT)
16428 {
16429 msg_putchar('{');
16430 if (*string == '{')
16431 ++string;
16432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016433 else
16434 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016435
Bram Moolenaar071d4272004-06-13 20:20:40 +000016436 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016437
16438 if (type == VAR_FUNC)
16439 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016440}
16441
16442/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016443 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000016444 * If the variable already exists, the value is updated.
16445 * Otherwise the variable is created.
16446 */
16447 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016448set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016449 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000016450 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016451 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016452{
Bram Moolenaar33570922005-01-25 22:26:29 +000016453 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016454 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016455 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000016456 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016457
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016458 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016459 {
16460 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
16461 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
16462 ? name[2] : name[0]))
16463 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016464 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016465 return;
16466 }
16467 if (function_exists(name))
16468 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016469 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016470 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016471 return;
16472 }
16473 }
16474
Bram Moolenaara7043832005-01-21 11:56:39 +000016475 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000016476 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000016477 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000016478 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000016479 return;
16480 }
16481
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016482 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016483 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016484 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016485 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016486 if (var_check_ro(v->di_flags, name)
16487 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000016488 return;
16489 if (v->di_tv.v_type != tv->v_type
16490 && !((v->di_tv.v_type == VAR_STRING
16491 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016492 && (tv->v_type == VAR_STRING
16493 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016494 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016495 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016496 return;
16497 }
Bram Moolenaar33570922005-01-25 22:26:29 +000016498
16499 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000016500 * Handle setting internal v: variables separately: we don't change
16501 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000016502 */
16503 if (ht == &vimvarht)
16504 {
16505 if (v->di_tv.v_type == VAR_STRING)
16506 {
16507 vim_free(v->di_tv.vval.v_string);
16508 if (copy || tv->v_type != VAR_STRING)
16509 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
16510 else
16511 {
16512 /* Take over the string to avoid an extra alloc/free. */
16513 v->di_tv.vval.v_string = tv->vval.v_string;
16514 tv->vval.v_string = NULL;
16515 }
16516 }
16517 else if (v->di_tv.v_type != VAR_NUMBER)
16518 EMSG2(_(e_intern2), "set_var()");
16519 else
16520 v->di_tv.vval.v_number = get_tv_number(tv);
16521 return;
16522 }
16523
16524 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016525 }
16526 else /* add a new variable */
16527 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000016528 /* Make sure the variable name is valid. */
16529 for (p = varname; *p != NUL; ++p)
16530 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p)))
16531 {
16532 EMSG2(_(e_illvar), varname);
16533 return;
16534 }
16535
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016536 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
16537 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000016538 if (v == NULL)
16539 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000016540 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000016541 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016542 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016543 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016544 return;
16545 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016546 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016547 }
Bram Moolenaara7043832005-01-21 11:56:39 +000016548
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016549 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000016550 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016551 else
16552 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016553 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016554 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016555 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016557}
16558
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016559/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016560 * Return TRUE if di_flags "flags" indicate read-only variable "name".
16561 * Also give an error message.
16562 */
16563 static int
16564var_check_ro(flags, name)
16565 int flags;
16566 char_u *name;
16567{
16568 if (flags & DI_FLAGS_RO)
16569 {
16570 EMSG2(_(e_readonlyvar), name);
16571 return TRUE;
16572 }
16573 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
16574 {
16575 EMSG2(_(e_readonlysbx), name);
16576 return TRUE;
16577 }
16578 return FALSE;
16579}
16580
16581/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016582 * Return TRUE if typeval "tv" is set to be locked (immutable).
16583 * Also give an error message, using "name".
16584 */
16585 static int
16586tv_check_lock(lock, name)
16587 int lock;
16588 char_u *name;
16589{
16590 if (lock & VAR_LOCKED)
16591 {
16592 EMSG2(_("E741: Value is locked: %s"),
16593 name == NULL ? (char_u *)_("Unknown") : name);
16594 return TRUE;
16595 }
16596 if (lock & VAR_FIXED)
16597 {
16598 EMSG2(_("E742: Cannot change value of %s"),
16599 name == NULL ? (char_u *)_("Unknown") : name);
16600 return TRUE;
16601 }
16602 return FALSE;
16603}
16604
16605/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016606 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016607 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016608 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016609 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016610 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016611copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000016612 typval_T *from;
16613 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016614{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016615 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016616 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016617 switch (from->v_type)
16618 {
16619 case VAR_NUMBER:
16620 to->vval.v_number = from->vval.v_number;
16621 break;
16622 case VAR_STRING:
16623 case VAR_FUNC:
16624 if (from->vval.v_string == NULL)
16625 to->vval.v_string = NULL;
16626 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016627 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016628 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016629 if (from->v_type == VAR_FUNC)
16630 func_ref(to->vval.v_string);
16631 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016632 break;
16633 case VAR_LIST:
16634 if (from->vval.v_list == NULL)
16635 to->vval.v_list = NULL;
16636 else
16637 {
16638 to->vval.v_list = from->vval.v_list;
16639 ++to->vval.v_list->lv_refcount;
16640 }
16641 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016642 case VAR_DICT:
16643 if (from->vval.v_dict == NULL)
16644 to->vval.v_dict = NULL;
16645 else
16646 {
16647 to->vval.v_dict = from->vval.v_dict;
16648 ++to->vval.v_dict->dv_refcount;
16649 }
16650 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016651 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016652 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016653 break;
16654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016655}
16656
16657/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000016658 * Make a copy of an item.
16659 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016660 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
16661 * reference to an already copied list/dict can be used.
16662 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016663 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016664 static int
16665item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000016666 typval_T *from;
16667 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016668 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016669 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016670{
16671 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016672 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016673
Bram Moolenaar33570922005-01-25 22:26:29 +000016674 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016675 {
16676 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016677 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016678 }
16679 ++recurse;
16680
16681 switch (from->v_type)
16682 {
16683 case VAR_NUMBER:
16684 case VAR_STRING:
16685 case VAR_FUNC:
16686 copy_tv(from, to);
16687 break;
16688 case VAR_LIST:
16689 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016690 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016691 if (from->vval.v_list == NULL)
16692 to->vval.v_list = NULL;
16693 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
16694 {
16695 /* use the copy made earlier */
16696 to->vval.v_list = from->vval.v_list->lv_copylist;
16697 ++to->vval.v_list->lv_refcount;
16698 }
16699 else
16700 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
16701 if (to->vval.v_list == NULL)
16702 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016703 break;
16704 case VAR_DICT:
16705 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016706 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016707 if (from->vval.v_dict == NULL)
16708 to->vval.v_dict = NULL;
16709 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
16710 {
16711 /* use the copy made earlier */
16712 to->vval.v_dict = from->vval.v_dict->dv_copydict;
16713 ++to->vval.v_dict->dv_refcount;
16714 }
16715 else
16716 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
16717 if (to->vval.v_dict == NULL)
16718 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016719 break;
16720 default:
16721 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016722 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016723 }
16724 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016725 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016726}
16727
16728/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016729 * ":echo expr1 ..." print each argument separated with a space, add a
16730 * newline at the end.
16731 * ":echon expr1 ..." print each argument plain.
16732 */
16733 void
16734ex_echo(eap)
16735 exarg_T *eap;
16736{
16737 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000016738 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016739 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016740 char_u *p;
16741 int needclr = TRUE;
16742 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016743 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016744
16745 if (eap->skip)
16746 ++emsg_skip;
16747 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
16748 {
16749 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016750 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016751 {
16752 /*
16753 * Report the invalid expression unless the expression evaluation
16754 * has been cancelled due to an aborting error, an interrupt, or an
16755 * exception.
16756 */
16757 if (!aborting())
16758 EMSG2(_(e_invexpr2), p);
16759 break;
16760 }
16761 if (!eap->skip)
16762 {
16763 if (atstart)
16764 {
16765 atstart = FALSE;
16766 /* Call msg_start() after eval1(), evaluating the expression
16767 * may cause a message to appear. */
16768 if (eap->cmdidx == CMD_echo)
16769 msg_start();
16770 }
16771 else if (eap->cmdidx == CMD_echo)
16772 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016773 p = echo_string(&rettv, &tofree, numbuf);
16774 if (p != NULL)
16775 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016776 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016777 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016778 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016779 if (*p != TAB && needclr)
16780 {
16781 /* remove any text still there from the command */
16782 msg_clr_eos();
16783 needclr = FALSE;
16784 }
16785 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016786 }
16787 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016788 {
16789#ifdef FEAT_MBYTE
16790 if (has_mbyte)
16791 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016792 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016793
16794 (void)msg_outtrans_len_attr(p, i, echo_attr);
16795 p += i - 1;
16796 }
16797 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000016798#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016799 (void)msg_outtrans_len_attr(p, 1, echo_attr);
16800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016801 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016802 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016803 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016804 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016805 arg = skipwhite(arg);
16806 }
16807 eap->nextcmd = check_nextcmd(arg);
16808
16809 if (eap->skip)
16810 --emsg_skip;
16811 else
16812 {
16813 /* remove text that may still be there from the command */
16814 if (needclr)
16815 msg_clr_eos();
16816 if (eap->cmdidx == CMD_echo)
16817 msg_end();
16818 }
16819}
16820
16821/*
16822 * ":echohl {name}".
16823 */
16824 void
16825ex_echohl(eap)
16826 exarg_T *eap;
16827{
16828 int id;
16829
16830 id = syn_name2id(eap->arg);
16831 if (id == 0)
16832 echo_attr = 0;
16833 else
16834 echo_attr = syn_id2attr(id);
16835}
16836
16837/*
16838 * ":execute expr1 ..." execute the result of an expression.
16839 * ":echomsg expr1 ..." Print a message
16840 * ":echoerr expr1 ..." Print an error
16841 * Each gets spaces around each argument and a newline at the end for
16842 * echo commands
16843 */
16844 void
16845ex_execute(eap)
16846 exarg_T *eap;
16847{
16848 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000016849 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016850 int ret = OK;
16851 char_u *p;
16852 garray_T ga;
16853 int len;
16854 int save_did_emsg;
16855
16856 ga_init2(&ga, 1, 80);
16857
16858 if (eap->skip)
16859 ++emsg_skip;
16860 while (*arg != NUL && *arg != '|' && *arg != '\n')
16861 {
16862 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016863 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016864 {
16865 /*
16866 * Report the invalid expression unless the expression evaluation
16867 * has been cancelled due to an aborting error, an interrupt, or an
16868 * exception.
16869 */
16870 if (!aborting())
16871 EMSG2(_(e_invexpr2), p);
16872 ret = FAIL;
16873 break;
16874 }
16875
16876 if (!eap->skip)
16877 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016878 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016879 len = (int)STRLEN(p);
16880 if (ga_grow(&ga, len + 2) == FAIL)
16881 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016882 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016883 ret = FAIL;
16884 break;
16885 }
16886 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016887 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016888 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016889 ga.ga_len += len;
16890 }
16891
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016892 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016893 arg = skipwhite(arg);
16894 }
16895
16896 if (ret != FAIL && ga.ga_data != NULL)
16897 {
16898 if (eap->cmdidx == CMD_echomsg)
16899 MSG_ATTR(ga.ga_data, echo_attr);
16900 else if (eap->cmdidx == CMD_echoerr)
16901 {
16902 /* We don't want to abort following commands, restore did_emsg. */
16903 save_did_emsg = did_emsg;
16904 EMSG((char_u *)ga.ga_data);
16905 if (!force_abort)
16906 did_emsg = save_did_emsg;
16907 }
16908 else if (eap->cmdidx == CMD_execute)
16909 do_cmdline((char_u *)ga.ga_data,
16910 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
16911 }
16912
16913 ga_clear(&ga);
16914
16915 if (eap->skip)
16916 --emsg_skip;
16917
16918 eap->nextcmd = check_nextcmd(arg);
16919}
16920
16921/*
16922 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
16923 * "arg" points to the "&" or '+' when called, to "option" when returning.
16924 * Returns NULL when no option name found. Otherwise pointer to the char
16925 * after the option name.
16926 */
16927 static char_u *
16928find_option_end(arg, opt_flags)
16929 char_u **arg;
16930 int *opt_flags;
16931{
16932 char_u *p = *arg;
16933
16934 ++p;
16935 if (*p == 'g' && p[1] == ':')
16936 {
16937 *opt_flags = OPT_GLOBAL;
16938 p += 2;
16939 }
16940 else if (*p == 'l' && p[1] == ':')
16941 {
16942 *opt_flags = OPT_LOCAL;
16943 p += 2;
16944 }
16945 else
16946 *opt_flags = 0;
16947
16948 if (!ASCII_ISALPHA(*p))
16949 return NULL;
16950 *arg = p;
16951
16952 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
16953 p += 4; /* termcap option */
16954 else
16955 while (ASCII_ISALPHA(*p))
16956 ++p;
16957 return p;
16958}
16959
16960/*
16961 * ":function"
16962 */
16963 void
16964ex_function(eap)
16965 exarg_T *eap;
16966{
16967 char_u *theline;
16968 int j;
16969 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016970 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016971 char_u *name = NULL;
16972 char_u *p;
16973 char_u *arg;
16974 garray_T newargs;
16975 garray_T newlines;
16976 int varargs = FALSE;
16977 int mustend = FALSE;
16978 int flags = 0;
16979 ufunc_T *fp;
16980 int indent;
16981 int nesting;
16982 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000016983 dictitem_T *v;
16984 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016985 static int func_nr = 0; /* number for nameless function */
16986 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016987 hashtab_T *ht;
16988 int todo;
16989 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016990
16991 /*
16992 * ":function" without argument: list functions.
16993 */
16994 if (ends_excmd(*eap->arg))
16995 {
16996 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016997 {
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000016998 todo = func_hashtab.ht_used;
16999 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017000 {
17001 if (!HASHITEM_EMPTY(hi))
17002 {
17003 --todo;
17004 fp = HI2UF(hi);
17005 if (!isdigit(*fp->uf_name))
17006 list_func_head(fp, FALSE);
17007 }
17008 }
17009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017010 eap->nextcmd = check_nextcmd(eap->arg);
17011 return;
17012 }
17013
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017014 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017015 * ":function /pat": list functions matching pattern.
17016 */
17017 if (*eap->arg == '/')
17018 {
17019 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
17020 if (!eap->skip)
17021 {
17022 regmatch_T regmatch;
17023
17024 c = *p;
17025 *p = NUL;
17026 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
17027 *p = c;
17028 if (regmatch.regprog != NULL)
17029 {
17030 regmatch.rm_ic = p_ic;
17031
17032 todo = func_hashtab.ht_used;
17033 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
17034 {
17035 if (!HASHITEM_EMPTY(hi))
17036 {
17037 --todo;
17038 fp = HI2UF(hi);
17039 if (!isdigit(*fp->uf_name)
17040 && vim_regexec(&regmatch, fp->uf_name, 0))
17041 list_func_head(fp, FALSE);
17042 }
17043 }
17044 }
17045 }
17046 if (*p == '/')
17047 ++p;
17048 eap->nextcmd = check_nextcmd(p);
17049 return;
17050 }
17051
17052 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017053 * Get the function name. There are these situations:
17054 * func normal function name
17055 * "name" == func, "fudi.fd_dict" == NULL
17056 * dict.func new dictionary entry
17057 * "name" == NULL, "fudi.fd_dict" set,
17058 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
17059 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017060 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017061 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
17062 * dict.func existing dict entry that's not a Funcref
17063 * "name" == NULL, "fudi.fd_dict" set,
17064 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
17065 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017066 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017067 name = trans_function_name(&p, eap->skip, 0, &fudi);
17068 paren = (vim_strchr(p, '(') != NULL);
17069 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017070 {
17071 /*
17072 * Return on an invalid expression in braces, unless the expression
17073 * evaluation has been cancelled due to an aborting error, an
17074 * interrupt, or an exception.
17075 */
17076 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017077 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017078 if (!eap->skip && fudi.fd_newkey != NULL)
17079 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017080 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017081 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017083 else
17084 eap->skip = TRUE;
17085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017086 /* An error in a function call during evaluation of an expression in magic
17087 * braces should not cause the function not to be defined. */
17088 saved_did_emsg = did_emsg;
17089 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017090
17091 /*
17092 * ":function func" with only function name: list function.
17093 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017094 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017095 {
17096 if (!ends_excmd(*skipwhite(p)))
17097 {
17098 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017099 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017100 }
17101 eap->nextcmd = check_nextcmd(p);
17102 if (eap->nextcmd != NULL)
17103 *p = NUL;
17104 if (!eap->skip && !got_int)
17105 {
17106 fp = find_func(name);
17107 if (fp != NULL)
17108 {
17109 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017110 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017111 {
17112 msg_putchar('\n');
17113 msg_outnum((long)(j + 1));
17114 if (j < 9)
17115 msg_putchar(' ');
17116 if (j < 99)
17117 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017118 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017119 out_flush(); /* show a line at a time */
17120 ui_breakcheck();
17121 }
17122 if (!got_int)
17123 {
17124 msg_putchar('\n');
17125 msg_puts((char_u *)" endfunction");
17126 }
17127 }
17128 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017129 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017130 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017131 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017132 }
17133
17134 /*
17135 * ":function name(arg1, arg2)" Define function.
17136 */
17137 p = skipwhite(p);
17138 if (*p != '(')
17139 {
17140 if (!eap->skip)
17141 {
17142 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017143 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017144 }
17145 /* attempt to continue by skipping some text */
17146 if (vim_strchr(p, '(') != NULL)
17147 p = vim_strchr(p, '(');
17148 }
17149 p = skipwhite(p + 1);
17150
17151 ga_init2(&newargs, (int)sizeof(char_u *), 3);
17152 ga_init2(&newlines, (int)sizeof(char_u *), 3);
17153
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017154 if (!eap->skip)
17155 {
17156 /* Check the name of the function. */
17157 if (name != NULL)
17158 arg = name;
17159 else
17160 arg = fudi.fd_newkey;
17161 if (arg != NULL)
17162 {
17163 if (*arg == K_SPECIAL)
17164 j = 3;
17165 else
17166 j = 0;
17167 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
17168 : eval_isnamec(arg[j])))
17169 ++j;
17170 if (arg[j] != NUL)
17171 emsg_funcname(_(e_invarg2), arg);
17172 }
17173 }
17174
Bram Moolenaar071d4272004-06-13 20:20:40 +000017175 /*
17176 * Isolate the arguments: "arg1, arg2, ...)"
17177 */
17178 while (*p != ')')
17179 {
17180 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
17181 {
17182 varargs = TRUE;
17183 p += 3;
17184 mustend = TRUE;
17185 }
17186 else
17187 {
17188 arg = p;
17189 while (ASCII_ISALNUM(*p) || *p == '_')
17190 ++p;
17191 if (arg == p || isdigit(*arg)
17192 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
17193 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
17194 {
17195 if (!eap->skip)
17196 EMSG2(_("E125: Illegal argument: %s"), arg);
17197 break;
17198 }
17199 if (ga_grow(&newargs, 1) == FAIL)
17200 goto erret;
17201 c = *p;
17202 *p = NUL;
17203 arg = vim_strsave(arg);
17204 if (arg == NULL)
17205 goto erret;
17206 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
17207 *p = c;
17208 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017209 if (*p == ',')
17210 ++p;
17211 else
17212 mustend = TRUE;
17213 }
17214 p = skipwhite(p);
17215 if (mustend && *p != ')')
17216 {
17217 if (!eap->skip)
17218 EMSG2(_(e_invarg2), eap->arg);
17219 break;
17220 }
17221 }
17222 ++p; /* skip the ')' */
17223
Bram Moolenaare9a41262005-01-15 22:18:47 +000017224 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017225 for (;;)
17226 {
17227 p = skipwhite(p);
17228 if (STRNCMP(p, "range", 5) == 0)
17229 {
17230 flags |= FC_RANGE;
17231 p += 5;
17232 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000017233 else if (STRNCMP(p, "dict", 4) == 0)
17234 {
17235 flags |= FC_DICT;
17236 p += 4;
17237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017238 else if (STRNCMP(p, "abort", 5) == 0)
17239 {
17240 flags |= FC_ABORT;
17241 p += 5;
17242 }
17243 else
17244 break;
17245 }
17246
17247 if (*p != NUL && *p != '"' && *p != '\n' && !eap->skip && !did_emsg)
17248 EMSG(_(e_trailing));
17249
17250 /*
17251 * Read the body of the function, until ":endfunction" is found.
17252 */
17253 if (KeyTyped)
17254 {
17255 /* Check if the function already exists, don't let the user type the
17256 * whole function before telling him it doesn't work! For a script we
17257 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017258 if (!eap->skip && !eap->forceit)
17259 {
17260 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
17261 EMSG(_(e_funcdict));
17262 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017263 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017265
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017266 if (!eap->skip && did_emsg)
17267 goto erret;
17268
Bram Moolenaar071d4272004-06-13 20:20:40 +000017269 msg_putchar('\n'); /* don't overwrite the function name */
17270 cmdline_row = msg_row;
17271 }
17272
17273 indent = 2;
17274 nesting = 0;
17275 for (;;)
17276 {
17277 msg_scroll = TRUE;
17278 need_wait_return = FALSE;
17279 if (eap->getline == NULL)
17280 theline = getcmdline(':', 0L, indent);
17281 else
17282 theline = eap->getline(':', eap->cookie, indent);
17283 if (KeyTyped)
17284 lines_left = Rows - 1;
17285 if (theline == NULL)
17286 {
17287 EMSG(_("E126: Missing :endfunction"));
17288 goto erret;
17289 }
17290
17291 if (skip_until != NULL)
17292 {
17293 /* between ":append" and "." and between ":python <<EOF" and "EOF"
17294 * don't check for ":endfunc". */
17295 if (STRCMP(theline, skip_until) == 0)
17296 {
17297 vim_free(skip_until);
17298 skip_until = NULL;
17299 }
17300 }
17301 else
17302 {
17303 /* skip ':' and blanks*/
17304 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
17305 ;
17306
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017307 /* Check for "endfunction". */
17308 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017309 {
17310 vim_free(theline);
17311 break;
17312 }
17313
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017314 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000017315 * at "end". */
17316 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
17317 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017318 else if (STRNCMP(p, "if", 2) == 0
17319 || STRNCMP(p, "wh", 2) == 0
17320 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000017321 || STRNCMP(p, "try", 3) == 0)
17322 indent += 2;
17323
17324 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017325 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017326 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017327 if (*p == '!')
17328 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017329 p += eval_fname_script(p);
17330 if (ASCII_ISALPHA(*p))
17331 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017332 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017333 if (*skipwhite(p) == '(')
17334 {
17335 ++nesting;
17336 indent += 2;
17337 }
17338 }
17339 }
17340
17341 /* Check for ":append" or ":insert". */
17342 p = skip_range(p, NULL);
17343 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
17344 || (p[0] == 'i'
17345 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
17346 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
17347 skip_until = vim_strsave((char_u *)".");
17348
17349 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
17350 arg = skipwhite(skiptowhite(p));
17351 if (arg[0] == '<' && arg[1] =='<'
17352 && ((p[0] == 'p' && p[1] == 'y'
17353 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
17354 || (p[0] == 'p' && p[1] == 'e'
17355 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
17356 || (p[0] == 't' && p[1] == 'c'
17357 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
17358 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
17359 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000017360 || (p[0] == 'm' && p[1] == 'z'
17361 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017362 ))
17363 {
17364 /* ":python <<" continues until a dot, like ":append" */
17365 p = skipwhite(arg + 2);
17366 if (*p == NUL)
17367 skip_until = vim_strsave((char_u *)".");
17368 else
17369 skip_until = vim_strsave(p);
17370 }
17371 }
17372
17373 /* Add the line to the function. */
17374 if (ga_grow(&newlines, 1) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017375 {
17376 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017377 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017378 }
17379
17380 /* Copy the line to newly allocated memory. get_one_sourceline()
17381 * allocates 250 bytes per line, this saves 80% on average. The cost
17382 * is an extra alloc/free. */
17383 p = vim_strsave(theline);
17384 if (p != NULL)
17385 {
17386 vim_free(theline);
17387 theline = p;
17388 }
17389
Bram Moolenaar071d4272004-06-13 20:20:40 +000017390 ((char_u **)(newlines.ga_data))[newlines.ga_len] = theline;
17391 newlines.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017392 }
17393
17394 /* Don't define the function when skipping commands or when an error was
17395 * detected. */
17396 if (eap->skip || did_emsg)
17397 goto erret;
17398
17399 /*
17400 * If there are no errors, add the function
17401 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017402 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017403 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017404 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000017405 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017406 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017407 emsg_funcname("E707: Function name conflicts with variable: %s",
17408 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017409 goto erret;
17410 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017411
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017412 fp = find_func(name);
17413 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017414 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017415 if (!eap->forceit)
17416 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017417 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017418 goto erret;
17419 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017420 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017421 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017422 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017423 name);
17424 goto erret;
17425 }
17426 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017427 ga_clear_strings(&(fp->uf_args));
17428 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017429 vim_free(name);
17430 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017432 }
17433 else
17434 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017435 char numbuf[20];
17436
17437 fp = NULL;
17438 if (fudi.fd_newkey == NULL && !eap->forceit)
17439 {
17440 EMSG(_(e_funcdict));
17441 goto erret;
17442 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000017443 if (fudi.fd_di == NULL)
17444 {
17445 /* Can't add a function to a locked dictionary */
17446 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
17447 goto erret;
17448 }
17449 /* Can't change an existing function if it is locked */
17450 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
17451 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017452
17453 /* Give the function a sequential number. Can only be used with a
17454 * Funcref! */
17455 vim_free(name);
17456 sprintf(numbuf, "%d", ++func_nr);
17457 name = vim_strsave((char_u *)numbuf);
17458 if (name == NULL)
17459 goto erret;
17460 }
17461
17462 if (fp == NULL)
17463 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017464 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017465 {
17466 int slen, plen;
17467 char_u *scriptname;
17468
17469 /* Check that the autoload name matches the script name. */
17470 j = FAIL;
17471 if (sourcing_name != NULL)
17472 {
17473 scriptname = autoload_name(name);
17474 if (scriptname != NULL)
17475 {
17476 p = vim_strchr(scriptname, '/');
17477 plen = STRLEN(p);
17478 slen = STRLEN(sourcing_name);
17479 if (slen > plen && fnamecmp(p,
17480 sourcing_name + slen - plen) == 0)
17481 j = OK;
17482 vim_free(scriptname);
17483 }
17484 }
17485 if (j == FAIL)
17486 {
17487 EMSG2(_("E746: Function name does not match script file name: %s"), name);
17488 goto erret;
17489 }
17490 }
17491
17492 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017493 if (fp == NULL)
17494 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017495
17496 if (fudi.fd_dict != NULL)
17497 {
17498 if (fudi.fd_di == NULL)
17499 {
17500 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017501 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017502 if (fudi.fd_di == NULL)
17503 {
17504 vim_free(fp);
17505 goto erret;
17506 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017507 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
17508 {
17509 vim_free(fudi.fd_di);
17510 goto erret;
17511 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017512 }
17513 else
17514 /* overwrite existing dict entry */
17515 clear_tv(&fudi.fd_di->di_tv);
17516 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017517 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017518 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017519 fp->uf_refcount = 1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017520 }
17521
Bram Moolenaar071d4272004-06-13 20:20:40 +000017522 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017523 STRCPY(fp->uf_name, name);
17524 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017525 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017526 fp->uf_args = newargs;
17527 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017528#ifdef FEAT_PROFILE
17529 fp->uf_tml_count = NULL;
17530 fp->uf_tml_total = NULL;
17531 fp->uf_tml_self = NULL;
17532 fp->uf_profiling = FALSE;
17533 if (prof_def_func())
17534 func_do_profile(fp);
17535#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017536 fp->uf_varargs = varargs;
17537 fp->uf_flags = flags;
17538 fp->uf_calls = 0;
17539 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017540 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017541
17542erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017543 ga_clear_strings(&newargs);
17544 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017545ret_free:
17546 vim_free(skip_until);
17547 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017548 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017549 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017550}
17551
17552/*
17553 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000017554 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017555 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017556 * flags:
17557 * TFN_INT: internal function name OK
17558 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000017559 * Advances "pp" to just after the function name (if no error).
17560 */
17561 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017562trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017563 char_u **pp;
17564 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017565 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000017566 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017567{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017568 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017569 char_u *start;
17570 char_u *end;
17571 int lead;
17572 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017573 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000017574 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017575
17576 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017577 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017578 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000017579
17580 /* Check for hard coded <SNR>: already translated function ID (from a user
17581 * command). */
17582 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
17583 && (*pp)[2] == (int)KE_SNR)
17584 {
17585 *pp += 3;
17586 len = get_id_len(pp) + 3;
17587 return vim_strnsave(start, len);
17588 }
17589
17590 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
17591 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017592 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000017593 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017594 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017595
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017596 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
17597 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017598 if (end == start)
17599 {
17600 if (!skip)
17601 EMSG(_("E129: Function name required"));
17602 goto theend;
17603 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017604 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017605 {
17606 /*
17607 * Report an invalid expression in braces, unless the expression
17608 * evaluation has been cancelled due to an aborting error, an
17609 * interrupt, or an exception.
17610 */
17611 if (!aborting())
17612 {
17613 if (end != NULL)
17614 EMSG2(_(e_invarg2), start);
17615 }
17616 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017617 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017618 goto theend;
17619 }
17620
17621 if (lv.ll_tv != NULL)
17622 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017623 if (fdp != NULL)
17624 {
17625 fdp->fd_dict = lv.ll_dict;
17626 fdp->fd_newkey = lv.ll_newkey;
17627 lv.ll_newkey = NULL;
17628 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017629 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017630 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
17631 {
17632 name = vim_strsave(lv.ll_tv->vval.v_string);
17633 *pp = end;
17634 }
17635 else
17636 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017637 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
17638 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017639 EMSG(_(e_funcref));
17640 else
17641 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017642 name = NULL;
17643 }
17644 goto theend;
17645 }
17646
17647 if (lv.ll_name == NULL)
17648 {
17649 /* Error found, but continue after the function name. */
17650 *pp = end;
17651 goto theend;
17652 }
17653
17654 if (lv.ll_exp_name != NULL)
17655 len = STRLEN(lv.ll_exp_name);
17656 else
Bram Moolenaara7043832005-01-21 11:56:39 +000017657 {
17658 if (lead == 2) /* skip over "s:" */
17659 lv.ll_name += 2;
17660 len = (int)(end - lv.ll_name);
17661 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017662
17663 /*
17664 * Copy the function name to allocated memory.
17665 * Accept <SID>name() inside a script, translate into <SNR>123_name().
17666 * Accept <SNR>123_name() outside a script.
17667 */
17668 if (skip)
17669 lead = 0; /* do nothing */
17670 else if (lead > 0)
17671 {
17672 lead = 3;
17673 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
17674 {
17675 if (current_SID <= 0)
17676 {
17677 EMSG(_(e_usingsid));
17678 goto theend;
17679 }
17680 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
17681 lead += (int)STRLEN(sid_buf);
17682 }
17683 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017684 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017685 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017686 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017687 goto theend;
17688 }
17689 name = alloc((unsigned)(len + lead + 1));
17690 if (name != NULL)
17691 {
17692 if (lead > 0)
17693 {
17694 name[0] = K_SPECIAL;
17695 name[1] = KS_EXTRA;
17696 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000017697 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017698 STRCPY(name + 3, sid_buf);
17699 }
17700 mch_memmove(name + lead, lv.ll_name, (size_t)len);
17701 name[len + lead] = NUL;
17702 }
17703 *pp = end;
17704
17705theend:
17706 clear_lval(&lv);
17707 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017708}
17709
17710/*
17711 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
17712 * Return 2 if "p" starts with "s:".
17713 * Return 0 otherwise.
17714 */
17715 static int
17716eval_fname_script(p)
17717 char_u *p;
17718{
17719 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
17720 || STRNICMP(p + 1, "SNR>", 4) == 0))
17721 return 5;
17722 if (p[0] == 's' && p[1] == ':')
17723 return 2;
17724 return 0;
17725}
17726
17727/*
17728 * Return TRUE if "p" starts with "<SID>" or "s:".
17729 * Only works if eval_fname_script() returned non-zero for "p"!
17730 */
17731 static int
17732eval_fname_sid(p)
17733 char_u *p;
17734{
17735 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
17736}
17737
17738/*
17739 * List the head of the function: "name(arg1, arg2)".
17740 */
17741 static void
17742list_func_head(fp, indent)
17743 ufunc_T *fp;
17744 int indent;
17745{
17746 int j;
17747
17748 msg_start();
17749 if (indent)
17750 MSG_PUTS(" ");
17751 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017752 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017753 {
17754 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017755 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017756 }
17757 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017758 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017759 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017760 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017761 {
17762 if (j)
17763 MSG_PUTS(", ");
17764 msg_puts(FUNCARG(fp, j));
17765 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017766 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017767 {
17768 if (j)
17769 MSG_PUTS(", ");
17770 MSG_PUTS("...");
17771 }
17772 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000017773 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000017774 if (p_verbose > 0)
17775 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017776}
17777
17778/*
17779 * Find a function by name, return pointer to it in ufuncs.
17780 * Return NULL for unknown function.
17781 */
17782 static ufunc_T *
17783find_func(name)
17784 char_u *name;
17785{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017786 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017787
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017788 hi = hash_find(&func_hashtab, name);
17789 if (!HASHITEM_EMPTY(hi))
17790 return HI2UF(hi);
17791 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017792}
17793
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017794#if defined(EXITFREE) || defined(PROTO)
17795 void
17796free_all_functions()
17797{
17798 hashitem_T *hi;
17799
17800 /* Need to start all over every time, because func_free() may change the
17801 * hash table. */
17802 while (func_hashtab.ht_used > 0)
17803 for (hi = func_hashtab.ht_array; ; ++hi)
17804 if (!HASHITEM_EMPTY(hi))
17805 {
17806 func_free(HI2UF(hi));
17807 break;
17808 }
17809}
17810#endif
17811
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017812/*
17813 * Return TRUE if a function "name" exists.
17814 */
17815 static int
17816function_exists(name)
17817 char_u *name;
17818{
17819 char_u *p = name;
17820 int n = FALSE;
17821
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017822 p = trans_function_name(&p, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017823 if (p != NULL)
17824 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017825 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017826 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017827 else
17828 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017829 vim_free(p);
17830 }
17831 return n;
17832}
17833
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017834/*
17835 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017836 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017837 */
17838 static int
17839builtin_function(name)
17840 char_u *name;
17841{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017842 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
17843 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017844}
17845
Bram Moolenaar05159a02005-02-26 23:04:13 +000017846#if defined(FEAT_PROFILE) || defined(PROTO)
17847/*
17848 * Start profiling function "fp".
17849 */
17850 static void
17851func_do_profile(fp)
17852 ufunc_T *fp;
17853{
17854 fp->uf_tm_count = 0;
17855 profile_zero(&fp->uf_tm_self);
17856 profile_zero(&fp->uf_tm_total);
17857 if (fp->uf_tml_count == NULL)
17858 fp->uf_tml_count = (int *)alloc_clear((unsigned)
17859 (sizeof(int) * fp->uf_lines.ga_len));
17860 if (fp->uf_tml_total == NULL)
17861 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
17862 (sizeof(proftime_T) * fp->uf_lines.ga_len));
17863 if (fp->uf_tml_self == NULL)
17864 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
17865 (sizeof(proftime_T) * fp->uf_lines.ga_len));
17866 fp->uf_tml_idx = -1;
17867 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
17868 || fp->uf_tml_self == NULL)
17869 return; /* out of memory */
17870
17871 fp->uf_profiling = TRUE;
17872}
17873
17874/*
17875 * Dump the profiling results for all functions in file "fd".
17876 */
17877 void
17878func_dump_profile(fd)
17879 FILE *fd;
17880{
17881 hashitem_T *hi;
17882 int todo;
17883 ufunc_T *fp;
17884 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000017885 ufunc_T **sorttab;
17886 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017887
17888 todo = func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000017889 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
17890
Bram Moolenaar05159a02005-02-26 23:04:13 +000017891 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
17892 {
17893 if (!HASHITEM_EMPTY(hi))
17894 {
17895 --todo;
17896 fp = HI2UF(hi);
17897 if (fp->uf_profiling)
17898 {
Bram Moolenaar73830342005-02-28 22:48:19 +000017899 if (sorttab != NULL)
17900 sorttab[st_len++] = fp;
17901
Bram Moolenaar05159a02005-02-26 23:04:13 +000017902 if (fp->uf_name[0] == K_SPECIAL)
17903 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
17904 else
17905 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
17906 if (fp->uf_tm_count == 1)
17907 fprintf(fd, "Called 1 time\n");
17908 else
17909 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
17910 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
17911 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
17912 fprintf(fd, "\n");
17913 fprintf(fd, "count total (s) self (s)\n");
17914
17915 for (i = 0; i < fp->uf_lines.ga_len; ++i)
17916 {
Bram Moolenaar73830342005-02-28 22:48:19 +000017917 prof_func_line(fd, fp->uf_tml_count[i],
17918 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017919 fprintf(fd, "%s\n", FUNCLINE(fp, i));
17920 }
17921 fprintf(fd, "\n");
17922 }
17923 }
17924 }
Bram Moolenaar73830342005-02-28 22:48:19 +000017925
17926 if (sorttab != NULL && st_len > 0)
17927 {
17928 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
17929 prof_total_cmp);
17930 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
17931 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
17932 prof_self_cmp);
17933 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
17934 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000017935}
Bram Moolenaar73830342005-02-28 22:48:19 +000017936
17937 static void
17938prof_sort_list(fd, sorttab, st_len, title, prefer_self)
17939 FILE *fd;
17940 ufunc_T **sorttab;
17941 int st_len;
17942 char *title;
17943 int prefer_self; /* when equal print only self time */
17944{
17945 int i;
17946 ufunc_T *fp;
17947
17948 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
17949 fprintf(fd, "count total (s) self (s) function\n");
17950 for (i = 0; i < 20 && i < st_len; ++i)
17951 {
17952 fp = sorttab[i];
17953 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
17954 prefer_self);
17955 if (fp->uf_name[0] == K_SPECIAL)
17956 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
17957 else
17958 fprintf(fd, " %s()\n", fp->uf_name);
17959 }
17960 fprintf(fd, "\n");
17961}
17962
17963/*
17964 * Print the count and times for one function or function line.
17965 */
17966 static void
17967prof_func_line(fd, count, total, self, prefer_self)
17968 FILE *fd;
17969 int count;
17970 proftime_T *total;
17971 proftime_T *self;
17972 int prefer_self; /* when equal print only self time */
17973{
17974 if (count > 0)
17975 {
17976 fprintf(fd, "%5d ", count);
17977 if (prefer_self && profile_equal(total, self))
17978 fprintf(fd, " ");
17979 else
17980 fprintf(fd, "%s ", profile_msg(total));
17981 if (!prefer_self && profile_equal(total, self))
17982 fprintf(fd, " ");
17983 else
17984 fprintf(fd, "%s ", profile_msg(self));
17985 }
17986 else
17987 fprintf(fd, " ");
17988}
17989
17990/*
17991 * Compare function for total time sorting.
17992 */
17993 static int
17994#ifdef __BORLANDC__
17995_RTLENTRYF
17996#endif
17997prof_total_cmp(s1, s2)
17998 const void *s1;
17999 const void *s2;
18000{
18001 ufunc_T *p1, *p2;
18002
18003 p1 = *(ufunc_T **)s1;
18004 p2 = *(ufunc_T **)s2;
18005 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
18006}
18007
18008/*
18009 * Compare function for self time sorting.
18010 */
18011 static int
18012#ifdef __BORLANDC__
18013_RTLENTRYF
18014#endif
18015prof_self_cmp(s1, s2)
18016 const void *s1;
18017 const void *s2;
18018{
18019 ufunc_T *p1, *p2;
18020
18021 p1 = *(ufunc_T **)s1;
18022 p2 = *(ufunc_T **)s2;
18023 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
18024}
18025
Bram Moolenaar05159a02005-02-26 23:04:13 +000018026#endif
18027
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018028/* The names of packages that once were loaded is remembered. */
18029static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
18030
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018031/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018032 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018033 * Return TRUE if a package was loaded.
18034 */
18035 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018036script_autoload(name, reload)
18037 char_u *name;
18038 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018039{
18040 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018041 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018042 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018043 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018044
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018045 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018046 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018047 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018048 return FALSE;
18049
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018050 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018051
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018052 /* Find the name in the list of previously loaded package names. Skip
18053 * "autoload/", it's always the same. */
18054 for (i = 0; i < ga_loaded.ga_len; ++i)
18055 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
18056 break;
18057 if (!reload && i < ga_loaded.ga_len)
18058 ret = FALSE; /* was loaded already */
18059 else
18060 {
18061 /* Remember the name if it wasn't loaded already. */
18062 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
18063 {
18064 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
18065 tofree = NULL;
18066 }
18067
18068 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000018069 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018070 ret = TRUE;
18071 }
18072
18073 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018074 return ret;
18075}
18076
18077/*
18078 * Return the autoload script name for a function or variable name.
18079 * Returns NULL when out of memory.
18080 */
18081 static char_u *
18082autoload_name(name)
18083 char_u *name;
18084{
18085 char_u *p;
18086 char_u *scriptname;
18087
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018088 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018089 scriptname = alloc((unsigned)(STRLEN(name) + 14));
18090 if (scriptname == NULL)
18091 return FALSE;
18092 STRCPY(scriptname, "autoload/");
18093 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018094 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018095 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018096 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018097 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018098 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018099}
18100
Bram Moolenaar071d4272004-06-13 20:20:40 +000018101#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
18102
18103/*
18104 * Function given to ExpandGeneric() to obtain the list of user defined
18105 * function names.
18106 */
18107 char_u *
18108get_user_func_name(xp, idx)
18109 expand_T *xp;
18110 int idx;
18111{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018112 static long_u done;
18113 static hashitem_T *hi;
18114 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018115
18116 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018117 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018118 done = 0;
18119 hi = func_hashtab.ht_array;
18120 }
18121 if (done < func_hashtab.ht_used)
18122 {
18123 if (done++ > 0)
18124 ++hi;
18125 while (HASHITEM_EMPTY(hi))
18126 ++hi;
18127 fp = HI2UF(hi);
18128
18129 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
18130 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018131
18132 cat_func_name(IObuff, fp);
18133 if (xp->xp_context != EXPAND_USER_FUNC)
18134 {
18135 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018136 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018137 STRCAT(IObuff, ")");
18138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018139 return IObuff;
18140 }
18141 return NULL;
18142}
18143
18144#endif /* FEAT_CMDL_COMPL */
18145
18146/*
18147 * Copy the function name of "fp" to buffer "buf".
18148 * "buf" must be able to hold the function name plus three bytes.
18149 * Takes care of script-local function names.
18150 */
18151 static void
18152cat_func_name(buf, fp)
18153 char_u *buf;
18154 ufunc_T *fp;
18155{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018156 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018157 {
18158 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018159 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018160 }
18161 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018162 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018163}
18164
18165/*
18166 * ":delfunction {name}"
18167 */
18168 void
18169ex_delfunction(eap)
18170 exarg_T *eap;
18171{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018172 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018173 char_u *p;
18174 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000018175 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018176
18177 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018178 name = trans_function_name(&p, eap->skip, 0, &fudi);
18179 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018180 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018181 {
18182 if (fudi.fd_dict != NULL && !eap->skip)
18183 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018184 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018186 if (!ends_excmd(*skipwhite(p)))
18187 {
18188 vim_free(name);
18189 EMSG(_(e_trailing));
18190 return;
18191 }
18192 eap->nextcmd = check_nextcmd(p);
18193 if (eap->nextcmd != NULL)
18194 *p = NUL;
18195
18196 if (!eap->skip)
18197 fp = find_func(name);
18198 vim_free(name);
18199
18200 if (!eap->skip)
18201 {
18202 if (fp == NULL)
18203 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000018204 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018205 return;
18206 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018207 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018208 {
18209 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
18210 return;
18211 }
18212
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018213 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018214 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018215 /* Delete the dict item that refers to the function, it will
18216 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018217 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018218 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018219 else
18220 func_free(fp);
18221 }
18222}
18223
18224/*
18225 * Free a function and remove it from the list of functions.
18226 */
18227 static void
18228func_free(fp)
18229 ufunc_T *fp;
18230{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018231 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018232
18233 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018234 ga_clear_strings(&(fp->uf_args));
18235 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000018236#ifdef FEAT_PROFILE
18237 vim_free(fp->uf_tml_count);
18238 vim_free(fp->uf_tml_total);
18239 vim_free(fp->uf_tml_self);
18240#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018241
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018242 /* remove the function from the function hashtable */
18243 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
18244 if (HASHITEM_EMPTY(hi))
18245 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018246 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018247 hash_remove(&func_hashtab, hi);
18248
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018249 vim_free(fp);
18250}
18251
18252/*
18253 * Unreference a Function: decrement the reference count and free it when it
18254 * becomes zero. Only for numbered functions.
18255 */
18256 static void
18257func_unref(name)
18258 char_u *name;
18259{
18260 ufunc_T *fp;
18261
18262 if (name != NULL && isdigit(*name))
18263 {
18264 fp = find_func(name);
18265 if (fp == NULL)
18266 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018267 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018268 {
18269 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018270 * when "uf_calls" becomes zero. */
18271 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018272 func_free(fp);
18273 }
18274 }
18275}
18276
18277/*
18278 * Count a reference to a Function.
18279 */
18280 static void
18281func_ref(name)
18282 char_u *name;
18283{
18284 ufunc_T *fp;
18285
18286 if (name != NULL && isdigit(*name))
18287 {
18288 fp = find_func(name);
18289 if (fp == NULL)
18290 EMSG2(_(e_intern2), "func_ref()");
18291 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018292 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018293 }
18294}
18295
18296/*
18297 * Call a user function.
18298 */
18299 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000018300call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018301 ufunc_T *fp; /* pointer to function */
18302 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000018303 typval_T *argvars; /* arguments */
18304 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018305 linenr_T firstline; /* first line of range */
18306 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000018307 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018308{
Bram Moolenaar33570922005-01-25 22:26:29 +000018309 char_u *save_sourcing_name;
18310 linenr_T save_sourcing_lnum;
18311 scid_T save_current_SID;
18312 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000018313 int save_did_emsg;
18314 static int depth = 0;
18315 dictitem_T *v;
18316 int fixvar_idx = 0; /* index in fixvar[] */
18317 int i;
18318 int ai;
18319 char_u numbuf[NUMBUFLEN];
18320 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018321#ifdef FEAT_PROFILE
18322 proftime_T wait_start;
18323#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018324
18325 /* If depth of calling is getting too high, don't execute the function */
18326 if (depth >= p_mfd)
18327 {
18328 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018329 rettv->v_type = VAR_NUMBER;
18330 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018331 return;
18332 }
18333 ++depth;
18334
18335 line_breakcheck(); /* check for CTRL-C hit */
18336
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018337 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000018338 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018339 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018340 fc.rettv = rettv;
18341 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018342 fc.linenr = 0;
18343 fc.returned = FALSE;
18344 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018345 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018346 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018347 fc.dbg_tick = debug_tick;
18348
Bram Moolenaar33570922005-01-25 22:26:29 +000018349 /*
18350 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
18351 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
18352 * each argument variable and saves a lot of time.
18353 */
18354 /*
18355 * Init l: variables.
18356 */
18357 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000018358 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018359 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018360 /* Set l:self to "selfdict". */
18361 v = &fc.fixvar[fixvar_idx++].var;
18362 STRCPY(v->di_key, "self");
18363 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
18364 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
18365 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018366 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000018367 v->di_tv.vval.v_dict = selfdict;
18368 ++selfdict->dv_refcount;
18369 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000018370
Bram Moolenaar33570922005-01-25 22:26:29 +000018371 /*
18372 * Init a: variables.
18373 * Set a:0 to "argcount".
18374 * Set a:000 to a list with room for the "..." arguments.
18375 */
18376 init_var_dict(&fc.l_avars, &fc.l_avars_var);
18377 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018378 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000018379 v = &fc.fixvar[fixvar_idx++].var;
18380 STRCPY(v->di_key, "000");
18381 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18382 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
18383 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018384 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018385 v->di_tv.vval.v_list = &fc.l_varlist;
18386 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
18387 fc.l_varlist.lv_refcount = 99999;
18388
18389 /*
18390 * Set a:firstline to "firstline" and a:lastline to "lastline".
18391 * Set a:name to named arguments.
18392 * Set a:N to the "..." arguments.
18393 */
18394 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
18395 (varnumber_T)firstline);
18396 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
18397 (varnumber_T)lastline);
18398 for (i = 0; i < argcount; ++i)
18399 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018400 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000018401 if (ai < 0)
18402 /* named argument a:name */
18403 name = FUNCARG(fp, i);
18404 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000018405 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018406 /* "..." argument a:1, a:2, etc. */
18407 sprintf((char *)numbuf, "%d", ai + 1);
18408 name = numbuf;
18409 }
18410 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
18411 {
18412 v = &fc.fixvar[fixvar_idx++].var;
18413 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18414 }
18415 else
18416 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018417 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
18418 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000018419 if (v == NULL)
18420 break;
18421 v->di_flags = DI_FLAGS_RO;
18422 }
18423 STRCPY(v->di_key, name);
18424 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
18425
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018426 /* Note: the values are copied directly to avoid alloc/free.
18427 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000018428 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018429 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018430
18431 if (ai >= 0 && ai < MAX_FUNC_ARGS)
18432 {
18433 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
18434 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018435 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018436 }
18437 }
18438
Bram Moolenaar071d4272004-06-13 20:20:40 +000018439 /* Don't redraw while executing the function. */
18440 ++RedrawingDisabled;
18441 save_sourcing_name = sourcing_name;
18442 save_sourcing_lnum = sourcing_lnum;
18443 sourcing_lnum = 1;
18444 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018445 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018446 if (sourcing_name != NULL)
18447 {
18448 if (save_sourcing_name != NULL
18449 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
18450 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
18451 else
18452 STRCPY(sourcing_name, "function ");
18453 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
18454
18455 if (p_verbose >= 12)
18456 {
18457 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018458 verbose_enter_scroll();
18459
Bram Moolenaar555b2802005-05-19 21:08:39 +000018460 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018461 if (p_verbose >= 14)
18462 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018463 char_u buf[MSG_BUF_LEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000018464 char_u numbuf[NUMBUFLEN];
18465 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018466
18467 msg_puts((char_u *)"(");
18468 for (i = 0; i < argcount; ++i)
18469 {
18470 if (i > 0)
18471 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018472 if (argvars[i].v_type == VAR_NUMBER)
18473 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018474 else
18475 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000018476 trunc_string(tv2string(&argvars[i], &tofree, numbuf),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018477 buf, MSG_BUF_CLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018478 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000018479 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018480 }
18481 }
18482 msg_puts((char_u *)")");
18483 }
18484 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018485
18486 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018487 --no_wait_return;
18488 }
18489 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000018490#ifdef FEAT_PROFILE
18491 if (do_profiling)
18492 {
18493 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
18494 func_do_profile(fp);
18495 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018496 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000018497 {
18498 ++fp->uf_tm_count;
18499 profile_start(&fp->uf_tm_start);
18500 profile_zero(&fp->uf_tm_children);
18501 }
18502 script_prof_save(&wait_start);
18503 }
18504#endif
18505
Bram Moolenaar071d4272004-06-13 20:20:40 +000018506 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018507 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018508 save_did_emsg = did_emsg;
18509 did_emsg = FALSE;
18510
18511 /* call do_cmdline() to execute the lines */
18512 do_cmdline(NULL, get_func_line, (void *)&fc,
18513 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
18514
18515 --RedrawingDisabled;
18516
18517 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018518 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018519 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018520 clear_tv(rettv);
18521 rettv->v_type = VAR_NUMBER;
18522 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018523 }
18524
Bram Moolenaar05159a02005-02-26 23:04:13 +000018525#ifdef FEAT_PROFILE
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018526 if (fp->uf_profiling || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000018527 {
18528 profile_end(&fp->uf_tm_start);
18529 profile_sub_wait(&wait_start, &fp->uf_tm_start);
18530 profile_add(&fp->uf_tm_total, &fp->uf_tm_start);
18531 profile_add(&fp->uf_tm_self, &fp->uf_tm_start);
18532 profile_sub(&fp->uf_tm_self, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018533 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018534 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018535 profile_add(&fc.caller->func->uf_tm_children, &fp->uf_tm_start);
18536 profile_add(&fc.caller->func->uf_tml_children, &fp->uf_tm_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018537 }
18538 }
18539#endif
18540
Bram Moolenaar071d4272004-06-13 20:20:40 +000018541 /* when being verbose, mention the return value */
18542 if (p_verbose >= 12)
18543 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018544 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018545 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018546
Bram Moolenaar071d4272004-06-13 20:20:40 +000018547 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000018548 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018549 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000018550 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
18551 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000018552 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000018553 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000018554 char_u buf[MSG_BUF_LEN];
18555 char_u numbuf[NUMBUFLEN];
18556 char_u *tofree;
18557
Bram Moolenaar555b2802005-05-19 21:08:39 +000018558 /* The value may be very long. Skip the middle part, so that we
18559 * have some idea how it starts and ends. smsg() would always
18560 * truncate it at the end. */
Bram Moolenaar758711c2005-02-02 23:11:38 +000018561 trunc_string(tv2string(fc.rettv, &tofree, numbuf),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018562 buf, MSG_BUF_CLEN);
Bram Moolenaar555b2802005-05-19 21:08:39 +000018563 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000018564 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018565 }
18566 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018567
18568 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018569 --no_wait_return;
18570 }
18571
18572 vim_free(sourcing_name);
18573 sourcing_name = save_sourcing_name;
18574 sourcing_lnum = save_sourcing_lnum;
18575 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018576#ifdef FEAT_PROFILE
18577 if (do_profiling)
18578 script_prof_restore(&wait_start);
18579#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018580
18581 if (p_verbose >= 12 && sourcing_name != NULL)
18582 {
18583 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018584 verbose_enter_scroll();
18585
Bram Moolenaar555b2802005-05-19 21:08:39 +000018586 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018587 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018588
18589 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018590 --no_wait_return;
18591 }
18592
18593 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018594 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018595
Bram Moolenaar33570922005-01-25 22:26:29 +000018596 /* The a: variables typevals were not alloced, only free the allocated
18597 * variables. */
18598 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
18599
18600 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018601 --depth;
18602}
18603
18604/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018605 * Add a number variable "name" to dict "dp" with value "nr".
18606 */
18607 static void
18608add_nr_var(dp, v, name, nr)
18609 dict_T *dp;
18610 dictitem_T *v;
18611 char *name;
18612 varnumber_T nr;
18613{
18614 STRCPY(v->di_key, name);
18615 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18616 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
18617 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018618 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018619 v->di_tv.vval.v_number = nr;
18620}
18621
18622/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018623 * ":return [expr]"
18624 */
18625 void
18626ex_return(eap)
18627 exarg_T *eap;
18628{
18629 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018630 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018631 int returning = FALSE;
18632
18633 if (current_funccal == NULL)
18634 {
18635 EMSG(_("E133: :return not inside a function"));
18636 return;
18637 }
18638
18639 if (eap->skip)
18640 ++emsg_skip;
18641
18642 eap->nextcmd = NULL;
18643 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018644 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018645 {
18646 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018647 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018648 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018649 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018650 }
18651 /* It's safer to return also on error. */
18652 else if (!eap->skip)
18653 {
18654 /*
18655 * Return unless the expression evaluation has been cancelled due to an
18656 * aborting error, an interrupt, or an exception.
18657 */
18658 if (!aborting())
18659 returning = do_return(eap, FALSE, TRUE, NULL);
18660 }
18661
18662 /* When skipping or the return gets pending, advance to the next command
18663 * in this line (!returning). Otherwise, ignore the rest of the line.
18664 * Following lines will be ignored by get_func_line(). */
18665 if (returning)
18666 eap->nextcmd = NULL;
18667 else if (eap->nextcmd == NULL) /* no argument */
18668 eap->nextcmd = check_nextcmd(arg);
18669
18670 if (eap->skip)
18671 --emsg_skip;
18672}
18673
18674/*
18675 * Return from a function. Possibly makes the return pending. Also called
18676 * for a pending return at the ":endtry" or after returning from an extra
18677 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000018678 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018679 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018680 * FALSE when the return gets pending.
18681 */
18682 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018683do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018684 exarg_T *eap;
18685 int reanimate;
18686 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018687 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018688{
18689 int idx;
18690 struct condstack *cstack = eap->cstack;
18691
18692 if (reanimate)
18693 /* Undo the return. */
18694 current_funccal->returned = FALSE;
18695
18696 /*
18697 * Cleanup (and inactivate) conditionals, but stop when a try conditional
18698 * not in its finally clause (which then is to be executed next) is found.
18699 * In this case, make the ":return" pending for execution at the ":endtry".
18700 * Otherwise, return normally.
18701 */
18702 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
18703 if (idx >= 0)
18704 {
18705 cstack->cs_pending[idx] = CSTP_RETURN;
18706
18707 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018708 /* A pending return again gets pending. "rettv" points to an
18709 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000018710 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018711 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018712 else
18713 {
18714 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018715 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018716 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018717 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018718
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018719 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018720 {
18721 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018722 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018723 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018724 else
18725 EMSG(_(e_outofmem));
18726 }
18727 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018728 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018729
18730 if (reanimate)
18731 {
18732 /* The pending return value could be overwritten by a ":return"
18733 * without argument in a finally clause; reset the default
18734 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018735 current_funccal->rettv->v_type = VAR_NUMBER;
18736 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018737 }
18738 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018739 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018740 }
18741 else
18742 {
18743 current_funccal->returned = TRUE;
18744
18745 /* If the return is carried out now, store the return value. For
18746 * a return immediately after reanimation, the value is already
18747 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018748 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018749 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018750 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000018751 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018752 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018753 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018754 }
18755 }
18756
18757 return idx < 0;
18758}
18759
18760/*
18761 * Free the variable with a pending return value.
18762 */
18763 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018764discard_pending_return(rettv)
18765 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018766{
Bram Moolenaar33570922005-01-25 22:26:29 +000018767 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018768}
18769
18770/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018771 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000018772 * is an allocated string. Used by report_pending() for verbose messages.
18773 */
18774 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018775get_return_cmd(rettv)
18776 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018777{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018778 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018779 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018780 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018781
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018782 if (rettv != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018783 s = echo_string((typval_T *)rettv, &tofree, numbuf);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018784 if (s == NULL)
18785 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018786
18787 STRCPY(IObuff, ":return ");
18788 STRNCPY(IObuff + 8, s, IOSIZE - 8);
18789 if (STRLEN(s) + 8 >= IOSIZE)
18790 STRCPY(IObuff + IOSIZE - 4, "...");
18791 vim_free(tofree);
18792 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018793}
18794
18795/*
18796 * Get next function line.
18797 * Called by do_cmdline() to get the next line.
18798 * Returns allocated string, or NULL for end of function.
18799 */
18800/* ARGSUSED */
18801 char_u *
18802get_func_line(c, cookie, indent)
18803 int c; /* not used */
18804 void *cookie;
18805 int indent; /* not used */
18806{
Bram Moolenaar33570922005-01-25 22:26:29 +000018807 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018808 ufunc_T *fp = fcp->func;
18809 char_u *retval;
18810 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018811
18812 /* If breakpoints have been added/deleted need to check for it. */
18813 if (fcp->dbg_tick != debug_tick)
18814 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000018815 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018816 sourcing_lnum);
18817 fcp->dbg_tick = debug_tick;
18818 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000018819#ifdef FEAT_PROFILE
18820 if (do_profiling)
18821 func_line_end(cookie);
18822#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018823
Bram Moolenaar05159a02005-02-26 23:04:13 +000018824 gap = &fp->uf_lines;
18825 if ((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000018826 retval = NULL;
18827 else if (fcp->returned || fcp->linenr >= gap->ga_len)
18828 retval = NULL;
18829 else
18830 {
18831 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
18832 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018833#ifdef FEAT_PROFILE
18834 if (do_profiling)
18835 func_line_start(cookie);
18836#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018837 }
18838
18839 /* Did we encounter a breakpoint? */
18840 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
18841 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000018842 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018843 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018844 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018845 sourcing_lnum);
18846 fcp->dbg_tick = debug_tick;
18847 }
18848
18849 return retval;
18850}
18851
Bram Moolenaar05159a02005-02-26 23:04:13 +000018852#if defined(FEAT_PROFILE) || defined(PROTO)
18853/*
18854 * Called when starting to read a function line.
18855 * "sourcing_lnum" must be correct!
18856 * When skipping lines it may not actually be executed, but we won't find out
18857 * until later and we need to store the time now.
18858 */
18859 void
18860func_line_start(cookie)
18861 void *cookie;
18862{
18863 funccall_T *fcp = (funccall_T *)cookie;
18864 ufunc_T *fp = fcp->func;
18865
18866 if (fp->uf_profiling && sourcing_lnum >= 1
18867 && sourcing_lnum <= fp->uf_lines.ga_len)
18868 {
18869 fp->uf_tml_idx = sourcing_lnum - 1;
18870 fp->uf_tml_execed = FALSE;
18871 profile_start(&fp->uf_tml_start);
18872 profile_zero(&fp->uf_tml_children);
18873 profile_get_wait(&fp->uf_tml_wait);
18874 }
18875}
18876
18877/*
18878 * Called when actually executing a function line.
18879 */
18880 void
18881func_line_exec(cookie)
18882 void *cookie;
18883{
18884 funccall_T *fcp = (funccall_T *)cookie;
18885 ufunc_T *fp = fcp->func;
18886
18887 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
18888 fp->uf_tml_execed = TRUE;
18889}
18890
18891/*
18892 * Called when done with a function line.
18893 */
18894 void
18895func_line_end(cookie)
18896 void *cookie;
18897{
18898 funccall_T *fcp = (funccall_T *)cookie;
18899 ufunc_T *fp = fcp->func;
18900
18901 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
18902 {
18903 if (fp->uf_tml_execed)
18904 {
18905 ++fp->uf_tml_count[fp->uf_tml_idx];
18906 profile_end(&fp->uf_tml_start);
18907 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
18908 profile_add(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start);
18909 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
18910 profile_sub(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_children);
18911 }
18912 fp->uf_tml_idx = -1;
18913 }
18914}
18915#endif
18916
Bram Moolenaar071d4272004-06-13 20:20:40 +000018917/*
18918 * Return TRUE if the currently active function should be ended, because a
18919 * return was encountered or an error occured. Used inside a ":while".
18920 */
18921 int
18922func_has_ended(cookie)
18923 void *cookie;
18924{
Bram Moolenaar33570922005-01-25 22:26:29 +000018925 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018926
18927 /* Ignore the "abort" flag if the abortion behavior has been changed due to
18928 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018929 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000018930 || fcp->returned);
18931}
18932
18933/*
18934 * return TRUE if cookie indicates a function which "abort"s on errors.
18935 */
18936 int
18937func_has_abort(cookie)
18938 void *cookie;
18939{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018940 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018941}
18942
18943#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
18944typedef enum
18945{
18946 VAR_FLAVOUR_DEFAULT,
18947 VAR_FLAVOUR_SESSION,
18948 VAR_FLAVOUR_VIMINFO
18949} var_flavour_T;
18950
18951static var_flavour_T var_flavour __ARGS((char_u *varname));
18952
18953 static var_flavour_T
18954var_flavour(varname)
18955 char_u *varname;
18956{
18957 char_u *p = varname;
18958
18959 if (ASCII_ISUPPER(*p))
18960 {
18961 while (*(++p))
18962 if (ASCII_ISLOWER(*p))
18963 return VAR_FLAVOUR_SESSION;
18964 return VAR_FLAVOUR_VIMINFO;
18965 }
18966 else
18967 return VAR_FLAVOUR_DEFAULT;
18968}
18969#endif
18970
18971#if defined(FEAT_VIMINFO) || defined(PROTO)
18972/*
18973 * Restore global vars that start with a capital from the viminfo file
18974 */
18975 int
18976read_viminfo_varlist(virp, writing)
18977 vir_T *virp;
18978 int writing;
18979{
18980 char_u *tab;
18981 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000018982 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018983
18984 if (!writing && (find_viminfo_parameter('!') != NULL))
18985 {
18986 tab = vim_strchr(virp->vir_line + 1, '\t');
18987 if (tab != NULL)
18988 {
18989 *tab++ = '\0'; /* isolate the variable name */
18990 if (*tab == 'S') /* string var */
18991 is_string = TRUE;
18992
18993 tab = vim_strchr(tab, '\t');
18994 if (tab != NULL)
18995 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018996 if (is_string)
18997 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000018998 tv.v_type = VAR_STRING;
18999 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019000 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019001 }
19002 else
19003 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019004 tv.v_type = VAR_NUMBER;
19005 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019006 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019007 set_var(virp->vir_line + 1, &tv, FALSE);
19008 if (is_string)
19009 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019010 }
19011 }
19012 }
19013
19014 return viminfo_readline(virp);
19015}
19016
19017/*
19018 * Write global vars that start with a capital to the viminfo file
19019 */
19020 void
19021write_viminfo_varlist(fp)
19022 FILE *fp;
19023{
Bram Moolenaar33570922005-01-25 22:26:29 +000019024 hashitem_T *hi;
19025 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000019026 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019027 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019028 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019029 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019030 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019031
19032 if (find_viminfo_parameter('!') == NULL)
19033 return;
19034
19035 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000019036
Bram Moolenaar33570922005-01-25 22:26:29 +000019037 todo = globvarht.ht_used;
19038 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019039 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019040 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019041 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019042 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019043 this_var = HI2DI(hi);
19044 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019045 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019046 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000019047 {
19048 case VAR_STRING: s = "STR"; break;
19049 case VAR_NUMBER: s = "NUM"; break;
19050 default: continue;
19051 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019052 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019053 p = echo_string(&this_var->di_tv, &tofree, numbuf);
19054 if (p != NULL)
19055 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000019056 vim_free(tofree);
19057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019058 }
19059 }
19060}
19061#endif
19062
19063#if defined(FEAT_SESSION) || defined(PROTO)
19064 int
19065store_session_globals(fd)
19066 FILE *fd;
19067{
Bram Moolenaar33570922005-01-25 22:26:29 +000019068 hashitem_T *hi;
19069 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000019070 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019071 char_u *p, *t;
19072
Bram Moolenaar33570922005-01-25 22:26:29 +000019073 todo = globvarht.ht_used;
19074 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019075 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019076 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019077 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019078 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019079 this_var = HI2DI(hi);
19080 if ((this_var->di_tv.v_type == VAR_NUMBER
19081 || this_var->di_tv.v_type == VAR_STRING)
19082 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019083 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019084 /* Escape special characters with a backslash. Turn a LF and
19085 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019086 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000019087 (char_u *)"\\\"\n\r");
19088 if (p == NULL) /* out of memory */
19089 break;
19090 for (t = p; *t != NUL; ++t)
19091 if (*t == '\n')
19092 *t = 'n';
19093 else if (*t == '\r')
19094 *t = 'r';
19095 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000019096 this_var->di_key,
19097 (this_var->di_tv.v_type == VAR_STRING) ? '"'
19098 : ' ',
19099 p,
19100 (this_var->di_tv.v_type == VAR_STRING) ? '"'
19101 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000019102 || put_eol(fd) == FAIL)
19103 {
19104 vim_free(p);
19105 return FAIL;
19106 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019107 vim_free(p);
19108 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019109 }
19110 }
19111 return OK;
19112}
19113#endif
19114
Bram Moolenaar661b1822005-07-28 22:36:45 +000019115/*
19116 * Display script name where an item was last set.
19117 * Should only be invoked when 'verbose' is non-zero.
19118 */
19119 void
19120last_set_msg(scriptID)
19121 scid_T scriptID;
19122{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019123 char_u *p;
19124
Bram Moolenaar661b1822005-07-28 22:36:45 +000019125 if (scriptID != 0)
19126 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019127 p = home_replace_save(NULL, get_scriptname(scriptID));
19128 if (p != NULL)
19129 {
19130 verbose_enter();
19131 MSG_PUTS(_("\n\tLast set from "));
19132 MSG_PUTS(p);
19133 vim_free(p);
19134 verbose_leave();
19135 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000019136 }
19137}
19138
Bram Moolenaar071d4272004-06-13 20:20:40 +000019139#endif /* FEAT_EVAL */
19140
19141#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
19142
19143
19144#ifdef WIN3264
19145/*
19146 * Functions for ":8" filename modifier: get 8.3 version of a filename.
19147 */
19148static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
19149static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
19150static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
19151
19152/*
19153 * Get the short pathname of a file.
19154 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
19155 */
19156 static int
19157get_short_pathname(fnamep, bufp, fnamelen)
19158 char_u **fnamep;
19159 char_u **bufp;
19160 int *fnamelen;
19161{
19162 int l,len;
19163 char_u *newbuf;
19164
19165 len = *fnamelen;
19166
19167 l = GetShortPathName(*fnamep, *fnamep, len);
19168 if (l > len - 1)
19169 {
19170 /* If that doesn't work (not enough space), then save the string
19171 * and try again with a new buffer big enough
19172 */
19173 newbuf = vim_strnsave(*fnamep, l);
19174 if (newbuf == NULL)
19175 return 0;
19176
19177 vim_free(*bufp);
19178 *fnamep = *bufp = newbuf;
19179
19180 l = GetShortPathName(*fnamep,*fnamep,l+1);
19181
19182 /* Really should always succeed, as the buffer is big enough */
19183 }
19184
19185 *fnamelen = l;
19186 return 1;
19187}
19188
19189/*
19190 * Create a short path name. Returns the length of the buffer it needs.
19191 * Doesn't copy over the end of the buffer passed in.
19192 */
19193 static int
19194shortpath_for_invalid_fname(fname, bufp, fnamelen)
19195 char_u **fname;
19196 char_u **bufp;
19197 int *fnamelen;
19198{
19199 char_u *s, *p, *pbuf2, *pbuf3;
19200 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019201 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019202
19203 /* Make a copy */
19204 len2 = *fnamelen;
19205 pbuf2 = vim_strnsave(*fname, len2);
19206 pbuf3 = NULL;
19207
19208 s = pbuf2 + len2 - 1; /* Find the end */
19209 slen = 1;
19210 plen = len2;
19211
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019212 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019213 {
19214 --s;
19215 ++slen;
19216 --plen;
19217 }
19218
19219 do
19220 {
19221 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019222 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019223 {
19224 --s;
19225 ++slen;
19226 --plen;
19227 }
19228 if (s <= pbuf2)
19229 break;
19230
19231 /* Remeber the character that is about to be blatted */
19232 ch = *s;
19233 *s = 0; /* get_short_pathname requires a null-terminated string */
19234
19235 /* Try it in situ */
19236 p = pbuf2;
19237 if (!get_short_pathname(&p, &pbuf3, &plen))
19238 {
19239 vim_free(pbuf2);
19240 return -1;
19241 }
19242 *s = ch; /* Preserve the string */
19243 } while (plen == 0);
19244
19245 if (plen > 0)
19246 {
19247 /* Remeber the length of the new string. */
19248 *fnamelen = len = plen + slen;
19249 vim_free(*bufp);
19250 if (len > len2)
19251 {
19252 /* If there's not enough space in the currently allocated string,
19253 * then copy it to a buffer big enough.
19254 */
19255 *fname= *bufp = vim_strnsave(p, len);
19256 if (*fname == NULL)
19257 return -1;
19258 }
19259 else
19260 {
19261 /* Transfer pbuf2 to being the main buffer (it's big enough) */
19262 *fname = *bufp = pbuf2;
19263 if (p != pbuf2)
19264 strncpy(*fname, p, plen);
19265 pbuf2 = NULL;
19266 }
19267 /* Concat the next bit */
19268 strncpy(*fname + plen, s, slen);
19269 (*fname)[len] = '\0';
19270 }
19271 vim_free(pbuf3);
19272 vim_free(pbuf2);
19273 return 0;
19274}
19275
19276/*
19277 * Get a pathname for a partial path.
19278 */
19279 static int
19280shortpath_for_partial(fnamep, bufp, fnamelen)
19281 char_u **fnamep;
19282 char_u **bufp;
19283 int *fnamelen;
19284{
19285 int sepcount, len, tflen;
19286 char_u *p;
19287 char_u *pbuf, *tfname;
19288 int hasTilde;
19289
19290 /* Count up the path seperators from the RHS.. so we know which part
19291 * of the path to return.
19292 */
19293 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019294 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019295 if (vim_ispathsep(*p))
19296 ++sepcount;
19297
19298 /* Need full path first (use expand_env() to remove a "~/") */
19299 hasTilde = (**fnamep == '~');
19300 if (hasTilde)
19301 pbuf = tfname = expand_env_save(*fnamep);
19302 else
19303 pbuf = tfname = FullName_save(*fnamep, FALSE);
19304
19305 len = tflen = STRLEN(tfname);
19306
19307 if (!get_short_pathname(&tfname, &pbuf, &len))
19308 return -1;
19309
19310 if (len == 0)
19311 {
19312 /* Don't have a valid filename, so shorten the rest of the
19313 * path if we can. This CAN give us invalid 8.3 filenames, but
19314 * there's not a lot of point in guessing what it might be.
19315 */
19316 len = tflen;
19317 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
19318 return -1;
19319 }
19320
19321 /* Count the paths backward to find the beginning of the desired string. */
19322 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019323 {
19324#ifdef FEAT_MBYTE
19325 if (has_mbyte)
19326 p -= mb_head_off(tfname, p);
19327#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019328 if (vim_ispathsep(*p))
19329 {
19330 if (sepcount == 0 || (hasTilde && sepcount == 1))
19331 break;
19332 else
19333 sepcount --;
19334 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019336 if (hasTilde)
19337 {
19338 --p;
19339 if (p >= tfname)
19340 *p = '~';
19341 else
19342 return -1;
19343 }
19344 else
19345 ++p;
19346
19347 /* Copy in the string - p indexes into tfname - allocated at pbuf */
19348 vim_free(*bufp);
19349 *fnamelen = (int)STRLEN(p);
19350 *bufp = pbuf;
19351 *fnamep = p;
19352
19353 return 0;
19354}
19355#endif /* WIN3264 */
19356
19357/*
19358 * Adjust a filename, according to a string of modifiers.
19359 * *fnamep must be NUL terminated when called. When returning, the length is
19360 * determined by *fnamelen.
19361 * Returns valid flags.
19362 * When there is an error, *fnamep is set to NULL.
19363 */
19364 int
19365modify_fname(src, usedlen, fnamep, bufp, fnamelen)
19366 char_u *src; /* string with modifiers */
19367 int *usedlen; /* characters after src that are used */
19368 char_u **fnamep; /* file name so far */
19369 char_u **bufp; /* buffer for allocated file name or NULL */
19370 int *fnamelen; /* length of fnamep */
19371{
19372 int valid = 0;
19373 char_u *tail;
19374 char_u *s, *p, *pbuf;
19375 char_u dirname[MAXPATHL];
19376 int c;
19377 int has_fullname = 0;
19378#ifdef WIN3264
19379 int has_shortname = 0;
19380#endif
19381
19382repeat:
19383 /* ":p" - full path/file_name */
19384 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
19385 {
19386 has_fullname = 1;
19387
19388 valid |= VALID_PATH;
19389 *usedlen += 2;
19390
19391 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
19392 if ((*fnamep)[0] == '~'
19393#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
19394 && ((*fnamep)[1] == '/'
19395# ifdef BACKSLASH_IN_FILENAME
19396 || (*fnamep)[1] == '\\'
19397# endif
19398 || (*fnamep)[1] == NUL)
19399
19400#endif
19401 )
19402 {
19403 *fnamep = expand_env_save(*fnamep);
19404 vim_free(*bufp); /* free any allocated file name */
19405 *bufp = *fnamep;
19406 if (*fnamep == NULL)
19407 return -1;
19408 }
19409
19410 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019411 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019412 {
19413 if (vim_ispathsep(*p)
19414 && p[1] == '.'
19415 && (p[2] == NUL
19416 || vim_ispathsep(p[2])
19417 || (p[2] == '.'
19418 && (p[3] == NUL || vim_ispathsep(p[3])))))
19419 break;
19420 }
19421
19422 /* FullName_save() is slow, don't use it when not needed. */
19423 if (*p != NUL || !vim_isAbsName(*fnamep))
19424 {
19425 *fnamep = FullName_save(*fnamep, *p != NUL);
19426 vim_free(*bufp); /* free any allocated file name */
19427 *bufp = *fnamep;
19428 if (*fnamep == NULL)
19429 return -1;
19430 }
19431
19432 /* Append a path separator to a directory. */
19433 if (mch_isdir(*fnamep))
19434 {
19435 /* Make room for one or two extra characters. */
19436 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
19437 vim_free(*bufp); /* free any allocated file name */
19438 *bufp = *fnamep;
19439 if (*fnamep == NULL)
19440 return -1;
19441 add_pathsep(*fnamep);
19442 }
19443 }
19444
19445 /* ":." - path relative to the current directory */
19446 /* ":~" - path relative to the home directory */
19447 /* ":8" - shortname path - postponed till after */
19448 while (src[*usedlen] == ':'
19449 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
19450 {
19451 *usedlen += 2;
19452 if (c == '8')
19453 {
19454#ifdef WIN3264
19455 has_shortname = 1; /* Postpone this. */
19456#endif
19457 continue;
19458 }
19459 pbuf = NULL;
19460 /* Need full path first (use expand_env() to remove a "~/") */
19461 if (!has_fullname)
19462 {
19463 if (c == '.' && **fnamep == '~')
19464 p = pbuf = expand_env_save(*fnamep);
19465 else
19466 p = pbuf = FullName_save(*fnamep, FALSE);
19467 }
19468 else
19469 p = *fnamep;
19470
19471 has_fullname = 0;
19472
19473 if (p != NULL)
19474 {
19475 if (c == '.')
19476 {
19477 mch_dirname(dirname, MAXPATHL);
19478 s = shorten_fname(p, dirname);
19479 if (s != NULL)
19480 {
19481 *fnamep = s;
19482 if (pbuf != NULL)
19483 {
19484 vim_free(*bufp); /* free any allocated file name */
19485 *bufp = pbuf;
19486 pbuf = NULL;
19487 }
19488 }
19489 }
19490 else
19491 {
19492 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
19493 /* Only replace it when it starts with '~' */
19494 if (*dirname == '~')
19495 {
19496 s = vim_strsave(dirname);
19497 if (s != NULL)
19498 {
19499 *fnamep = s;
19500 vim_free(*bufp);
19501 *bufp = s;
19502 }
19503 }
19504 }
19505 vim_free(pbuf);
19506 }
19507 }
19508
19509 tail = gettail(*fnamep);
19510 *fnamelen = (int)STRLEN(*fnamep);
19511
19512 /* ":h" - head, remove "/file_name", can be repeated */
19513 /* Don't remove the first "/" or "c:\" */
19514 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
19515 {
19516 valid |= VALID_HEAD;
19517 *usedlen += 2;
19518 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019519 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019520 --tail;
19521 *fnamelen = (int)(tail - *fnamep);
19522#ifdef VMS
19523 if (*fnamelen > 0)
19524 *fnamelen += 1; /* the path separator is part of the path */
19525#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019526 while (tail > s && !after_pathsep(s, tail))
19527 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019528 }
19529
19530 /* ":8" - shortname */
19531 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
19532 {
19533 *usedlen += 2;
19534#ifdef WIN3264
19535 has_shortname = 1;
19536#endif
19537 }
19538
19539#ifdef WIN3264
19540 /* Check shortname after we have done 'heads' and before we do 'tails'
19541 */
19542 if (has_shortname)
19543 {
19544 pbuf = NULL;
19545 /* Copy the string if it is shortened by :h */
19546 if (*fnamelen < (int)STRLEN(*fnamep))
19547 {
19548 p = vim_strnsave(*fnamep, *fnamelen);
19549 if (p == 0)
19550 return -1;
19551 vim_free(*bufp);
19552 *bufp = *fnamep = p;
19553 }
19554
19555 /* Split into two implementations - makes it easier. First is where
19556 * there isn't a full name already, second is where there is.
19557 */
19558 if (!has_fullname && !vim_isAbsName(*fnamep))
19559 {
19560 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
19561 return -1;
19562 }
19563 else
19564 {
19565 int l;
19566
19567 /* Simple case, already have the full-name
19568 * Nearly always shorter, so try first time. */
19569 l = *fnamelen;
19570 if (!get_short_pathname(fnamep, bufp, &l))
19571 return -1;
19572
19573 if (l == 0)
19574 {
19575 /* Couldn't find the filename.. search the paths.
19576 */
19577 l = *fnamelen;
19578 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
19579 return -1;
19580 }
19581 *fnamelen = l;
19582 }
19583 }
19584#endif /* WIN3264 */
19585
19586 /* ":t" - tail, just the basename */
19587 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
19588 {
19589 *usedlen += 2;
19590 *fnamelen -= (int)(tail - *fnamep);
19591 *fnamep = tail;
19592 }
19593
19594 /* ":e" - extension, can be repeated */
19595 /* ":r" - root, without extension, can be repeated */
19596 while (src[*usedlen] == ':'
19597 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
19598 {
19599 /* find a '.' in the tail:
19600 * - for second :e: before the current fname
19601 * - otherwise: The last '.'
19602 */
19603 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
19604 s = *fnamep - 2;
19605 else
19606 s = *fnamep + *fnamelen - 1;
19607 for ( ; s > tail; --s)
19608 if (s[0] == '.')
19609 break;
19610 if (src[*usedlen + 1] == 'e') /* :e */
19611 {
19612 if (s > tail)
19613 {
19614 *fnamelen += (int)(*fnamep - (s + 1));
19615 *fnamep = s + 1;
19616#ifdef VMS
19617 /* cut version from the extension */
19618 s = *fnamep + *fnamelen - 1;
19619 for ( ; s > *fnamep; --s)
19620 if (s[0] == ';')
19621 break;
19622 if (s > *fnamep)
19623 *fnamelen = s - *fnamep;
19624#endif
19625 }
19626 else if (*fnamep <= tail)
19627 *fnamelen = 0;
19628 }
19629 else /* :r */
19630 {
19631 if (s > tail) /* remove one extension */
19632 *fnamelen = (int)(s - *fnamep);
19633 }
19634 *usedlen += 2;
19635 }
19636
19637 /* ":s?pat?foo?" - substitute */
19638 /* ":gs?pat?foo?" - global substitute */
19639 if (src[*usedlen] == ':'
19640 && (src[*usedlen + 1] == 's'
19641 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
19642 {
19643 char_u *str;
19644 char_u *pat;
19645 char_u *sub;
19646 int sep;
19647 char_u *flags;
19648 int didit = FALSE;
19649
19650 flags = (char_u *)"";
19651 s = src + *usedlen + 2;
19652 if (src[*usedlen + 1] == 'g')
19653 {
19654 flags = (char_u *)"g";
19655 ++s;
19656 }
19657
19658 sep = *s++;
19659 if (sep)
19660 {
19661 /* find end of pattern */
19662 p = vim_strchr(s, sep);
19663 if (p != NULL)
19664 {
19665 pat = vim_strnsave(s, (int)(p - s));
19666 if (pat != NULL)
19667 {
19668 s = p + 1;
19669 /* find end of substitution */
19670 p = vim_strchr(s, sep);
19671 if (p != NULL)
19672 {
19673 sub = vim_strnsave(s, (int)(p - s));
19674 str = vim_strnsave(*fnamep, *fnamelen);
19675 if (sub != NULL && str != NULL)
19676 {
19677 *usedlen = (int)(p + 1 - src);
19678 s = do_string_sub(str, pat, sub, flags);
19679 if (s != NULL)
19680 {
19681 *fnamep = s;
19682 *fnamelen = (int)STRLEN(s);
19683 vim_free(*bufp);
19684 *bufp = s;
19685 didit = TRUE;
19686 }
19687 }
19688 vim_free(sub);
19689 vim_free(str);
19690 }
19691 vim_free(pat);
19692 }
19693 }
19694 /* after using ":s", repeat all the modifiers */
19695 if (didit)
19696 goto repeat;
19697 }
19698 }
19699
19700 return valid;
19701}
19702
19703/*
19704 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
19705 * "flags" can be "g" to do a global substitute.
19706 * Returns an allocated string, NULL for error.
19707 */
19708 char_u *
19709do_string_sub(str, pat, sub, flags)
19710 char_u *str;
19711 char_u *pat;
19712 char_u *sub;
19713 char_u *flags;
19714{
19715 int sublen;
19716 regmatch_T regmatch;
19717 int i;
19718 int do_all;
19719 char_u *tail;
19720 garray_T ga;
19721 char_u *ret;
19722 char_u *save_cpo;
19723
19724 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
19725 save_cpo = p_cpo;
19726 p_cpo = (char_u *)"";
19727
19728 ga_init2(&ga, 1, 200);
19729
19730 do_all = (flags[0] == 'g');
19731
19732 regmatch.rm_ic = p_ic;
19733 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19734 if (regmatch.regprog != NULL)
19735 {
19736 tail = str;
19737 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
19738 {
19739 /*
19740 * Get some space for a temporary buffer to do the substitution
19741 * into. It will contain:
19742 * - The text up to where the match is.
19743 * - The substituted text.
19744 * - The text after the match.
19745 */
19746 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
19747 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
19748 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
19749 {
19750 ga_clear(&ga);
19751 break;
19752 }
19753
19754 /* copy the text up to where the match is */
19755 i = (int)(regmatch.startp[0] - tail);
19756 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
19757 /* add the substituted text */
19758 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
19759 + ga.ga_len + i, TRUE, TRUE, FALSE);
19760 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019761 /* avoid getting stuck on a match with an empty string */
19762 if (tail == regmatch.endp[0])
19763 {
19764 if (*tail == NUL)
19765 break;
19766 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
19767 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019768 }
19769 else
19770 {
19771 tail = regmatch.endp[0];
19772 if (*tail == NUL)
19773 break;
19774 }
19775 if (!do_all)
19776 break;
19777 }
19778
19779 if (ga.ga_data != NULL)
19780 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
19781
19782 vim_free(regmatch.regprog);
19783 }
19784
19785 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
19786 ga_clear(&ga);
19787 p_cpo = save_cpo;
19788
19789 return ret;
19790}
19791
19792#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */