blob: 2365b3fedef76addc8e06cd863a3f5741524ee10 [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 Moolenaard43b6cf2005-09-09 19:53:42 +0000412static int list_append_string __ARGS((list_T *l, char_u *str));
Bram Moolenaar33570922005-01-25 22:26:29 +0000413static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
414static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
415static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000416static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000417static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
418static char_u *list2string __ARGS((typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000419static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000420static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
421static void set_ref_in_list __ARGS((list_T *l, int copyID));
422static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000423static void dict_unref __ARGS((dict_T *d));
424static void dict_free __ARGS((dict_T *d));
425static dictitem_T *dictitem_alloc __ARGS((char_u *key));
426static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
427static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
428static void dictitem_free __ARGS((dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000429static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000430static int dict_add __ARGS((dict_T *d, dictitem_T *item));
431static long dict_len __ARGS((dict_T *d));
432static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
433static char_u *dict2string __ARGS((typval_T *tv));
434static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar33570922005-01-25 22:26:29 +0000435static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf));
436static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf));
437static char_u *string_quote __ARGS((char_u *str, int function));
438static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
439static int find_internal_func __ARGS((char_u *name));
440static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
441static 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));
442static 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 +0000443static void emsg_funcname __ARGS((char *msg, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000444
445static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
446static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
447static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
448static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
449static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
450static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
451static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
452static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
453static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
454static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
455static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
456static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
457static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
458static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
459static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
460static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
463static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000464#if defined(FEAT_INS_EXPAND)
465static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
467#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000468static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
469static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
470static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
473static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000499static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000500static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000501static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000502static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
503static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000507static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000508static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000515static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000516static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
534static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000538static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000539static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000544static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000545static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000561static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000562static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000565#ifdef vim_mkdir
566static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
567#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000568static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000572static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000573static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000574static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000575static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000586static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000587static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000593static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000594static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000598static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000599static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000601static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
602#ifdef HAVE_STRFTIME
603static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
604#endif
605static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000617static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000618static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000619static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000620static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000621static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
624static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
625static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
626static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
627static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
628static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
629static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
630static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
631static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
632static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
633static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
634static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000635static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000636
Bram Moolenaar33570922005-01-25 22:26:29 +0000637static pos_T *var2fpos __ARGS((typval_T *varp, int lnum));
638static int get_env_len __ARGS((char_u **arg));
639static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000640static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000641static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
642#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
643#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
644 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000645static 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 +0000646static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000647static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000648static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
649static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000650static typval_T *alloc_tv __ARGS((void));
651static typval_T *alloc_string_tv __ARGS((char_u *string));
652static void free_tv __ARGS((typval_T *varp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000653static void init_tv __ARGS((typval_T *varp));
654static long get_tv_number __ARGS((typval_T *varp));
655static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000656static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000657static char_u *get_tv_string __ARGS((typval_T *varp));
658static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000659static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000660static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000661static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000662static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
663static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
664static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
665static void list_one_var __ARGS((dictitem_T *v, char_u *prefix));
666static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
667static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
668static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000669static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000670static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000671static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000672static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
673static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
674static int eval_fname_script __ARGS((char_u *p));
675static int eval_fname_sid __ARGS((char_u *p));
676static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000677static ufunc_T *find_func __ARGS((char_u *name));
678static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000679static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000680#ifdef FEAT_PROFILE
681static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000682static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
683static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
684static int
685# ifdef __BORLANDC__
686 _RTLENTRYF
687# endif
688 prof_total_cmp __ARGS((const void *s1, const void *s2));
689static int
690# ifdef __BORLANDC__
691 _RTLENTRYF
692# endif
693 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000694#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000695static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000696static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000697static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000698static void func_free __ARGS((ufunc_T *fp));
699static void func_unref __ARGS((char_u *name));
700static void func_ref __ARGS((char_u *name));
701static 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));
702static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
703
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000704/* Character used as separated in autoload function/variable names. */
705#define AUTOLOAD_CHAR '#'
706
Bram Moolenaar33570922005-01-25 22:26:29 +0000707/*
708 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000709 */
710 void
711eval_init()
712{
Bram Moolenaar33570922005-01-25 22:26:29 +0000713 int i;
714 struct vimvar *p;
715
716 init_var_dict(&globvardict, &globvars_var);
717 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000718 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000719 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000720
721 for (i = 0; i < VV_LEN; ++i)
722 {
723 p = &vimvars[i];
724 STRCPY(p->vv_di.di_key, p->vv_name);
725 if (p->vv_flags & VV_RO)
726 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
727 else if (p->vv_flags & VV_RO_SBX)
728 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
729 else
730 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000731
732 /* add to v: scope dict, unless the value is not always available */
733 if (p->vv_type != VAR_UNKNOWN)
734 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000735 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000736 /* add to compat scope dict */
737 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000738 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000739}
740
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000741#if defined(EXITFREE) || defined(PROTO)
742 void
743eval_clear()
744{
745 int i;
746 struct vimvar *p;
747
748 for (i = 0; i < VV_LEN; ++i)
749 {
750 p = &vimvars[i];
751 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000752 {
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000753 vim_free(p->vv_di.di_tv.vval.v_string);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000754 p->vv_di.di_tv.vval.v_string = NULL;
755 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000756 }
757 hash_clear(&vimvarht);
758 hash_clear(&compat_hashtab);
759
760 /* script-local variables */
761 for (i = 1; i <= ga_scripts.ga_len; ++i)
762 vars_clear(&SCRIPT_VARS(i));
763 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000764 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000765
766 /* global variables */
767 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000768
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000769 /* functions */
Bram Moolenaard9fba312005-06-26 22:34:35 +0000770 free_all_functions();
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000771 hash_clear(&func_hashtab);
772
773 /* unreferenced lists and dicts */
774 (void)garbage_collect();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000775}
776#endif
777
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000778/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 * Return the name of the executed function.
780 */
781 char_u *
782func_name(cookie)
783 void *cookie;
784{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000785 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786}
787
788/*
789 * Return the address holding the next breakpoint line for a funccall cookie.
790 */
791 linenr_T *
792func_breakpoint(cookie)
793 void *cookie;
794{
Bram Moolenaar33570922005-01-25 22:26:29 +0000795 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796}
797
798/*
799 * Return the address holding the debug tick for a funccall cookie.
800 */
801 int *
802func_dbg_tick(cookie)
803 void *cookie;
804{
Bram Moolenaar33570922005-01-25 22:26:29 +0000805 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806}
807
808/*
809 * Return the nesting level for a funccall cookie.
810 */
811 int
812func_level(cookie)
813 void *cookie;
814{
Bram Moolenaar33570922005-01-25 22:26:29 +0000815 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816}
817
818/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000819funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820
821/*
822 * Return TRUE when a function was ended by a ":return" command.
823 */
824 int
825current_func_returned()
826{
827 return current_funccal->returned;
828}
829
830
831/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 * Set an internal variable to a string value. Creates the variable if it does
833 * not already exist.
834 */
835 void
836set_internal_string_var(name, value)
837 char_u *name;
838 char_u *value;
839{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000840 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000841 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842
843 val = vim_strsave(value);
844 if (val != NULL)
845 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000846 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000847 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000849 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000850 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 }
852 }
853}
854
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000855static lval_T *redir_lval = NULL;
856static char_u *redir_endp = NULL;
857static char_u *redir_varname = NULL;
858
859/*
860 * Start recording command output to a variable
861 * Returns OK if successfully completed the setup. FAIL otherwise.
862 */
863 int
864var_redir_start(name, append)
865 char_u *name;
866 int append; /* append to an existing variable */
867{
868 int save_emsg;
869 int err;
870 typval_T tv;
871
872 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000873 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000874 {
875 EMSG(_(e_invarg));
876 return FAIL;
877 }
878
879 redir_varname = vim_strsave(name);
880 if (redir_varname == NULL)
881 return FAIL;
882
883 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
884 if (redir_lval == NULL)
885 {
886 var_redir_stop();
887 return FAIL;
888 }
889
890 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000891 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
892 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000893 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
894 {
895 if (redir_endp != NULL && *redir_endp != NUL)
896 /* Trailing characters are present after the variable name */
897 EMSG(_(e_trailing));
898 else
899 EMSG(_(e_invarg));
900 var_redir_stop();
901 return FAIL;
902 }
903
904 /* check if we can write to the variable: set it to or append an empty
905 * string */
906 save_emsg = did_emsg;
907 did_emsg = FALSE;
908 tv.v_type = VAR_STRING;
909 tv.vval.v_string = (char_u *)"";
910 if (append)
911 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
912 else
913 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
914 err = did_emsg;
915 did_emsg += save_emsg;
916 if (err)
917 {
918 var_redir_stop();
919 return FAIL;
920 }
921 if (redir_lval->ll_newkey != NULL)
922 {
923 /* Dictionary item was created, don't do it again. */
924 vim_free(redir_lval->ll_newkey);
925 redir_lval->ll_newkey = NULL;
926 }
927
928 return OK;
929}
930
931/*
932 * Append "value[len]" to the variable set by var_redir_start().
933 */
934 void
935var_redir_str(value, len)
936 char_u *value;
937 int len;
938{
939 char_u *val;
940 typval_T tv;
941 int save_emsg;
942 int err;
943
944 if (redir_lval == NULL)
945 return;
946
947 if (len == -1)
948 /* Append the entire string */
949 val = vim_strsave(value);
950 else
951 /* Append only the specified number of characters */
952 val = vim_strnsave(value, len);
953 if (val == NULL)
954 return;
955
956 tv.v_type = VAR_STRING;
957 tv.vval.v_string = val;
958
959 save_emsg = did_emsg;
960 did_emsg = FALSE;
961 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
962 err = did_emsg;
963 did_emsg += save_emsg;
964 if (err)
965 var_redir_stop();
966
967 vim_free(tv.vval.v_string);
968}
969
970/*
971 * Stop redirecting command output to a variable.
972 */
973 void
974var_redir_stop()
975{
976 if (redir_lval != NULL)
977 {
978 clear_lval(redir_lval);
979 vim_free(redir_lval);
980 redir_lval = NULL;
981 }
982 vim_free(redir_varname);
983 redir_varname = NULL;
984}
985
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986# if defined(FEAT_MBYTE) || defined(PROTO)
987 int
988eval_charconvert(enc_from, enc_to, fname_from, fname_to)
989 char_u *enc_from;
990 char_u *enc_to;
991 char_u *fname_from;
992 char_u *fname_to;
993{
994 int err = FALSE;
995
996 set_vim_var_string(VV_CC_FROM, enc_from, -1);
997 set_vim_var_string(VV_CC_TO, enc_to, -1);
998 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
999 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1000 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1001 err = TRUE;
1002 set_vim_var_string(VV_CC_FROM, NULL, -1);
1003 set_vim_var_string(VV_CC_TO, NULL, -1);
1004 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1005 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1006
1007 if (err)
1008 return FAIL;
1009 return OK;
1010}
1011# endif
1012
1013# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1014 int
1015eval_printexpr(fname, args)
1016 char_u *fname;
1017 char_u *args;
1018{
1019 int err = FALSE;
1020
1021 set_vim_var_string(VV_FNAME_IN, fname, -1);
1022 set_vim_var_string(VV_CMDARG, args, -1);
1023 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1024 err = TRUE;
1025 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1026 set_vim_var_string(VV_CMDARG, NULL, -1);
1027
1028 if (err)
1029 {
1030 mch_remove(fname);
1031 return FAIL;
1032 }
1033 return OK;
1034}
1035# endif
1036
1037# if defined(FEAT_DIFF) || defined(PROTO)
1038 void
1039eval_diff(origfile, newfile, outfile)
1040 char_u *origfile;
1041 char_u *newfile;
1042 char_u *outfile;
1043{
1044 int err = FALSE;
1045
1046 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1047 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1048 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1049 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1050 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1051 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1052 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1053}
1054
1055 void
1056eval_patch(origfile, difffile, outfile)
1057 char_u *origfile;
1058 char_u *difffile;
1059 char_u *outfile;
1060{
1061 int err;
1062
1063 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1064 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1065 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1066 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1067 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1068 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1069 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1070}
1071# endif
1072
1073/*
1074 * Top level evaluation function, returning a boolean.
1075 * Sets "error" to TRUE if there was an error.
1076 * Return TRUE or FALSE.
1077 */
1078 int
1079eval_to_bool(arg, error, nextcmd, skip)
1080 char_u *arg;
1081 int *error;
1082 char_u **nextcmd;
1083 int skip; /* only parse, don't execute */
1084{
Bram Moolenaar33570922005-01-25 22:26:29 +00001085 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 int retval = FALSE;
1087
1088 if (skip)
1089 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001090 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 else
1093 {
1094 *error = FALSE;
1095 if (!skip)
1096 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001097 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001098 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 }
1100 }
1101 if (skip)
1102 --emsg_skip;
1103
1104 return retval;
1105}
1106
1107/*
1108 * Top level evaluation function, returning a string. If "skip" is TRUE,
1109 * only parsing to "nextcmd" is done, without reporting errors. Return
1110 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1111 */
1112 char_u *
1113eval_to_string_skip(arg, nextcmd, skip)
1114 char_u *arg;
1115 char_u **nextcmd;
1116 int skip; /* only parse, don't execute */
1117{
Bram Moolenaar33570922005-01-25 22:26:29 +00001118 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 char_u *retval;
1120
1121 if (skip)
1122 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001123 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 retval = NULL;
1125 else
1126 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001127 retval = vim_strsave(get_tv_string(&tv));
1128 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 }
1130 if (skip)
1131 --emsg_skip;
1132
1133 return retval;
1134}
1135
1136/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001137 * Skip over an expression at "*pp".
1138 * Return FAIL for an error, OK otherwise.
1139 */
1140 int
1141skip_expr(pp)
1142 char_u **pp;
1143{
Bram Moolenaar33570922005-01-25 22:26:29 +00001144 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001145
1146 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001147 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001148}
1149
1150/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 * Top level evaluation function, returning a string.
1152 * Return pointer to allocated memory, or NULL for failure.
1153 */
1154 char_u *
1155eval_to_string(arg, nextcmd)
1156 char_u *arg;
1157 char_u **nextcmd;
1158{
Bram Moolenaar33570922005-01-25 22:26:29 +00001159 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 char_u *retval;
1161
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001162 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 retval = NULL;
1164 else
1165 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001166 retval = vim_strsave(get_tv_string(&tv));
1167 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 }
1169
1170 return retval;
1171}
1172
1173/*
1174 * Call eval_to_string() with "sandbox" set and not using local variables.
1175 */
1176 char_u *
1177eval_to_string_safe(arg, nextcmd)
1178 char_u *arg;
1179 char_u **nextcmd;
1180{
1181 char_u *retval;
1182 void *save_funccalp;
1183
1184 save_funccalp = save_funccal();
1185 ++sandbox;
1186 retval = eval_to_string(arg, nextcmd);
1187 --sandbox;
1188 restore_funccal(save_funccalp);
1189 return retval;
1190}
1191
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192/*
1193 * Top level evaluation function, returning a number.
1194 * Evaluates "expr" silently.
1195 * Returns -1 for an error.
1196 */
1197 int
1198eval_to_number(expr)
1199 char_u *expr;
1200{
Bram Moolenaar33570922005-01-25 22:26:29 +00001201 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001203 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204
1205 ++emsg_off;
1206
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001207 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 retval = -1;
1209 else
1210 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001211 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001212 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 }
1214 --emsg_off;
1215
1216 return retval;
1217}
1218
Bram Moolenaara40058a2005-07-11 22:42:07 +00001219/*
1220 * Prepare v: variable "idx" to be used.
1221 * Save the current typeval in "save_tv".
1222 * When not used yet add the variable to the v: hashtable.
1223 */
1224 static void
1225prepare_vimvar(idx, save_tv)
1226 int idx;
1227 typval_T *save_tv;
1228{
1229 *save_tv = vimvars[idx].vv_tv;
1230 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1231 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1232}
1233
1234/*
1235 * Restore v: variable "idx" to typeval "save_tv".
1236 * When no longer defined, remove the variable from the v: hashtable.
1237 */
1238 static void
1239restore_vimvar(idx, save_tv)
1240 int idx;
1241 typval_T *save_tv;
1242{
1243 hashitem_T *hi;
1244
1245 clear_tv(&vimvars[idx].vv_tv);
1246 vimvars[idx].vv_tv = *save_tv;
1247 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1248 {
1249 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1250 if (HASHITEM_EMPTY(hi))
1251 EMSG2(_(e_intern2), "restore_vimvar()");
1252 else
1253 hash_remove(&vimvarht, hi);
1254 }
1255}
1256
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001257#if defined(FEAT_SYN_HL) || defined(PROTO)
1258/*
1259 * Evaluate an expression to a list with suggestions.
1260 * For the "expr:" part of 'spellsuggest'.
1261 */
1262 list_T *
1263eval_spell_expr(badword, expr)
1264 char_u *badword;
1265 char_u *expr;
1266{
1267 typval_T save_val;
1268 typval_T rettv;
1269 list_T *list = NULL;
1270 char_u *p = skipwhite(expr);
1271
1272 /* Set "v:val" to the bad word. */
1273 prepare_vimvar(VV_VAL, &save_val);
1274 vimvars[VV_VAL].vv_type = VAR_STRING;
1275 vimvars[VV_VAL].vv_str = badword;
1276 if (p_verbose == 0)
1277 ++emsg_off;
1278
1279 if (eval1(&p, &rettv, TRUE) == OK)
1280 {
1281 if (rettv.v_type != VAR_LIST)
1282 clear_tv(&rettv);
1283 else
1284 list = rettv.vval.v_list;
1285 }
1286
1287 if (p_verbose == 0)
1288 --emsg_off;
1289 vimvars[VV_VAL].vv_str = NULL;
1290 restore_vimvar(VV_VAL, &save_val);
1291
1292 return list;
1293}
1294
1295/*
1296 * "list" is supposed to contain two items: a word and a number. Return the
1297 * word in "pp" and the number as the return value.
1298 * Return -1 if anything isn't right.
1299 * Used to get the good word and score from the eval_spell_expr() result.
1300 */
1301 int
1302get_spellword(list, pp)
1303 list_T *list;
1304 char_u **pp;
1305{
1306 listitem_T *li;
1307
1308 li = list->lv_first;
1309 if (li == NULL)
1310 return -1;
1311 *pp = get_tv_string(&li->li_tv);
1312
1313 li = li->li_next;
1314 if (li == NULL)
1315 return -1;
1316 return get_tv_number(&li->li_tv);
1317}
1318#endif
1319
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001320/*
1321 * Top level evaluation function,
1322 */
1323 typval_T *
1324eval_expr(arg, nextcmd)
1325 char_u *arg;
1326 char_u **nextcmd;
1327{
1328 typval_T *tv;
1329
1330 tv = (typval_T *)alloc(sizeof(typval_T));
1331 if (!tv)
1332 return NULL;
1333
1334 if (eval0(arg, tv, nextcmd, TRUE) == FAIL)
1335 {
1336 vim_free(tv);
1337 return NULL;
1338 }
1339
1340 return tv;
1341}
1342
1343
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1345/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001346 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 * Uses argv[argc] for the function arguments.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001348 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001350 static int
1351call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 char_u *func;
1353 int argc;
1354 char_u **argv;
1355 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001356 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357{
Bram Moolenaar33570922005-01-25 22:26:29 +00001358 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 long n;
1360 int len;
1361 int i;
1362 int doesrange;
1363 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001364 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365
Bram Moolenaar33570922005-01-25 22:26:29 +00001366 argvars = (typval_T *)alloc((unsigned)(argc * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001368 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369
1370 for (i = 0; i < argc; i++)
1371 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001372 /* Pass a NULL or empty argument as an empty string */
1373 if (argv[i] == NULL || *argv[i] == NUL)
1374 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001375 argvars[i].v_type = VAR_STRING;
1376 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001377 continue;
1378 }
1379
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 /* Recognize a number argument, the others must be strings. */
1381 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1382 if (len != 0 && len == (int)STRLEN(argv[i]))
1383 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001384 argvars[i].v_type = VAR_NUMBER;
1385 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 }
1387 else
1388 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001389 argvars[i].v_type = VAR_STRING;
1390 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 }
1392 }
1393
1394 if (safe)
1395 {
1396 save_funccalp = save_funccal();
1397 ++sandbox;
1398 }
1399
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001400 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1401 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001403 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 if (safe)
1405 {
1406 --sandbox;
1407 restore_funccal(save_funccalp);
1408 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001409 vim_free(argvars);
1410
1411 if (ret == FAIL)
1412 clear_tv(rettv);
1413
1414 return ret;
1415}
1416
1417/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001418 * Call vimL function "func" and return the result as a string.
1419 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001420 * Uses argv[argc] for the function arguments.
1421 */
1422 void *
1423call_func_retstr(func, argc, argv, safe)
1424 char_u *func;
1425 int argc;
1426 char_u **argv;
1427 int safe; /* use the sandbox */
1428{
1429 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001430 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001431
1432 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1433 return NULL;
1434
1435 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001436 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 return retval;
1438}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001439
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001440#if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001441/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001442 * Call vimL function "func" and return the result as a number.
1443 * Returns -1 when calling the function fails.
1444 * Uses argv[argc] for the function arguments.
1445 */
1446 long
1447call_func_retnr(func, argc, argv, safe)
1448 char_u *func;
1449 int argc;
1450 char_u **argv;
1451 int safe; /* use the sandbox */
1452{
1453 typval_T rettv;
1454 long retval;
1455
1456 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1457 return -1;
1458
1459 retval = get_tv_number_chk(&rettv, NULL);
1460 clear_tv(&rettv);
1461 return retval;
1462}
1463#endif
1464
1465/*
1466 * Call vimL function "func" and return the result as a list
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001467 * Uses argv[argc] for the function arguments.
1468 */
1469 void *
1470call_func_retlist(func, argc, argv, safe)
1471 char_u *func;
1472 int argc;
1473 char_u **argv;
1474 int safe; /* use the sandbox */
1475{
1476 typval_T rettv;
1477
1478 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1479 return NULL;
1480
1481 if (rettv.v_type != VAR_LIST)
1482 {
1483 clear_tv(&rettv);
1484 return NULL;
1485 }
1486
1487 return rettv.vval.v_list;
1488}
1489
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490#endif
1491
1492/*
1493 * Save the current function call pointer, and set it to NULL.
1494 * Used when executing autocommands and for ":source".
1495 */
1496 void *
1497save_funccal()
1498{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001499 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 current_funccal = NULL;
1502 return (void *)fc;
1503}
1504
1505 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001506restore_funccal(vfc)
1507 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001509 funccall_T *fc = (funccall_T *)vfc;
1510
1511 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512}
1513
Bram Moolenaar05159a02005-02-26 23:04:13 +00001514#if defined(FEAT_PROFILE) || defined(PROTO)
1515/*
1516 * Prepare profiling for entering a child or something else that is not
1517 * counted for the script/function itself.
1518 * Should always be called in pair with prof_child_exit().
1519 */
1520 void
1521prof_child_enter(tm)
1522 proftime_T *tm; /* place to store waittime */
1523{
1524 funccall_T *fc = current_funccal;
1525
1526 if (fc != NULL && fc->func->uf_profiling)
1527 profile_start(&fc->prof_child);
1528 script_prof_save(tm);
1529}
1530
1531/*
1532 * Take care of time spent in a child.
1533 * Should always be called after prof_child_enter().
1534 */
1535 void
1536prof_child_exit(tm)
1537 proftime_T *tm; /* where waittime was stored */
1538{
1539 funccall_T *fc = current_funccal;
1540
1541 if (fc != NULL && fc->func->uf_profiling)
1542 {
1543 profile_end(&fc->prof_child);
1544 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1545 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1546 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1547 }
1548 script_prof_restore(tm);
1549}
1550#endif
1551
1552
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553#ifdef FEAT_FOLDING
1554/*
1555 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1556 * it in "*cp". Doesn't give error messages.
1557 */
1558 int
1559eval_foldexpr(arg, cp)
1560 char_u *arg;
1561 int *cp;
1562{
Bram Moolenaar33570922005-01-25 22:26:29 +00001563 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 int retval;
1565 char_u *s;
1566
1567 ++emsg_off;
1568 ++sandbox;
1569 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001570 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 retval = 0;
1572 else
1573 {
1574 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001575 if (tv.v_type == VAR_NUMBER)
1576 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001577 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 retval = 0;
1579 else
1580 {
1581 /* If the result is a string, check if there is a non-digit before
1582 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001583 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 if (!VIM_ISDIGIT(*s) && *s != '-')
1585 *cp = *s++;
1586 retval = atol((char *)s);
1587 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001588 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 }
1590 --emsg_off;
1591 --sandbox;
1592
1593 return retval;
1594}
1595#endif
1596
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001598 * ":let" list all variable values
1599 * ":let var1 var2" list variable values
1600 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001601 * ":let var += expr" assignment command.
1602 * ":let var -= expr" assignment command.
1603 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001604 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 */
1606 void
1607ex_let(eap)
1608 exarg_T *eap;
1609{
1610 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001611 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001612 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001614 int var_count = 0;
1615 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001616 char_u op[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001618 expr = skip_var_list(arg, &var_count, &semicolon);
1619 if (expr == NULL)
1620 return;
1621 expr = vim_strchr(expr, '=');
1622 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001624 /*
1625 * ":let" without "=": list variables
1626 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001627 if (*arg == '[')
1628 EMSG(_(e_invarg));
1629 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001630 /* ":let var1 var2" */
1631 arg = list_arg_vars(eap, arg);
1632 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001633 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001634 /* ":let" */
Bram Moolenaara7043832005-01-21 11:56:39 +00001635 list_glob_vars();
1636 list_buf_vars();
1637 list_win_vars();
1638 list_vim_vars();
1639 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 eap->nextcmd = check_nextcmd(arg);
1641 }
1642 else
1643 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001644 op[0] = '=';
1645 op[1] = NUL;
1646 if (expr > arg)
1647 {
1648 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1649 op[0] = expr[-1]; /* +=, -= or .= */
1650 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001651 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001652
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 if (eap->skip)
1654 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001655 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 if (eap->skip)
1657 {
1658 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001659 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 --emsg_skip;
1661 }
1662 else if (i != FAIL)
1663 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001664 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001665 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001666 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 }
1668 }
1669}
1670
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001671/*
1672 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1673 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001674 * When "nextchars" is not NULL it points to a string with characters that
1675 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1676 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001677 * Returns OK or FAIL;
1678 */
1679 static int
1680ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1681 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001682 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001683 int copy; /* copy values from "tv", don't move */
1684 int semicolon; /* from skip_var_list() */
1685 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001686 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001687{
1688 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001689 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001690 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001691 listitem_T *item;
1692 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001693
1694 if (*arg != '[')
1695 {
1696 /*
1697 * ":let var = expr" or ":for var in list"
1698 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001699 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001700 return FAIL;
1701 return OK;
1702 }
1703
1704 /*
1705 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1706 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001707 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001708 {
1709 EMSG(_(e_listreq));
1710 return FAIL;
1711 }
1712
1713 i = list_len(l);
1714 if (semicolon == 0 && var_count < i)
1715 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001716 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001717 return FAIL;
1718 }
1719 if (var_count - semicolon > i)
1720 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001721 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001722 return FAIL;
1723 }
1724
1725 item = l->lv_first;
1726 while (*arg != ']')
1727 {
1728 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001729 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001730 item = item->li_next;
1731 if (arg == NULL)
1732 return FAIL;
1733
1734 arg = skipwhite(arg);
1735 if (*arg == ';')
1736 {
1737 /* Put the rest of the list (may be empty) in the var after ';'.
1738 * Create a new list for this. */
1739 l = list_alloc();
1740 if (l == NULL)
1741 return FAIL;
1742 while (item != NULL)
1743 {
1744 list_append_tv(l, &item->li_tv);
1745 item = item->li_next;
1746 }
1747
1748 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001749 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001750 ltv.vval.v_list = l;
1751 l->lv_refcount = 1;
1752
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001753 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1754 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001755 clear_tv(&ltv);
1756 if (arg == NULL)
1757 return FAIL;
1758 break;
1759 }
1760 else if (*arg != ',' && *arg != ']')
1761 {
1762 EMSG2(_(e_intern2), "ex_let_vars()");
1763 return FAIL;
1764 }
1765 }
1766
1767 return OK;
1768}
1769
1770/*
1771 * Skip over assignable variable "var" or list of variables "[var, var]".
1772 * Used for ":let varvar = expr" and ":for varvar in expr".
1773 * For "[var, var]" increment "*var_count" for each variable.
1774 * for "[var, var; var]" set "semicolon".
1775 * Return NULL for an error.
1776 */
1777 static char_u *
1778skip_var_list(arg, var_count, semicolon)
1779 char_u *arg;
1780 int *var_count;
1781 int *semicolon;
1782{
1783 char_u *p, *s;
1784
1785 if (*arg == '[')
1786 {
1787 /* "[var, var]": find the matching ']'. */
1788 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001789 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001790 {
1791 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1792 s = skip_var_one(p);
1793 if (s == p)
1794 {
1795 EMSG2(_(e_invarg2), p);
1796 return NULL;
1797 }
1798 ++*var_count;
1799
1800 p = skipwhite(s);
1801 if (*p == ']')
1802 break;
1803 else if (*p == ';')
1804 {
1805 if (*semicolon == 1)
1806 {
1807 EMSG(_("Double ; in list of variables"));
1808 return NULL;
1809 }
1810 *semicolon = 1;
1811 }
1812 else if (*p != ',')
1813 {
1814 EMSG2(_(e_invarg2), p);
1815 return NULL;
1816 }
1817 }
1818 return p + 1;
1819 }
1820 else
1821 return skip_var_one(arg);
1822}
1823
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001824/*
Bram Moolenaar92124a32005-06-17 22:03:40 +00001825 * Skip one (assignable) variable name, includig @r, $VAR, &option, d.key,
1826 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001827 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001828 static char_u *
1829skip_var_one(arg)
1830 char_u *arg;
1831{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001832 if (*arg == '@' && arg[1] != NUL)
1833 return arg + 2;
1834 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1835 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001836}
1837
Bram Moolenaara7043832005-01-21 11:56:39 +00001838/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001839 * List variables for hashtab "ht" with prefix "prefix".
1840 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001841 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001842 static void
Bram Moolenaar33570922005-01-25 22:26:29 +00001843list_hashtable_vars(ht, prefix, empty)
1844 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001845 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001846 int empty;
Bram Moolenaara7043832005-01-21 11:56:39 +00001847{
Bram Moolenaar33570922005-01-25 22:26:29 +00001848 hashitem_T *hi;
1849 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001850 int todo;
1851
1852 todo = ht->ht_used;
1853 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1854 {
1855 if (!HASHITEM_EMPTY(hi))
1856 {
1857 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001858 di = HI2DI(hi);
1859 if (empty || di->di_tv.v_type != VAR_STRING
1860 || di->di_tv.vval.v_string != NULL)
1861 list_one_var(di, prefix);
Bram Moolenaara7043832005-01-21 11:56:39 +00001862 }
1863 }
1864}
1865
1866/*
1867 * List global variables.
1868 */
1869 static void
1870list_glob_vars()
1871{
Bram Moolenaar33570922005-01-25 22:26:29 +00001872 list_hashtable_vars(&globvarht, (char_u *)"", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001873}
1874
1875/*
1876 * List buffer variables.
1877 */
1878 static void
1879list_buf_vars()
1880{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001881 char_u numbuf[NUMBUFLEN];
1882
Bram Moolenaar33570922005-01-25 22:26:29 +00001883 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001884
1885 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
1886 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER, numbuf);
Bram Moolenaara7043832005-01-21 11:56:39 +00001887}
1888
1889/*
1890 * List window variables.
1891 */
1892 static void
1893list_win_vars()
1894{
Bram Moolenaar33570922005-01-25 22:26:29 +00001895 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001896}
1897
1898/*
1899 * List Vim variables.
1900 */
1901 static void
1902list_vim_vars()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001903{
Bram Moolenaar33570922005-01-25 22:26:29 +00001904 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001905}
1906
1907/*
1908 * List variables in "arg".
1909 */
1910 static char_u *
1911list_arg_vars(eap, arg)
1912 exarg_T *eap;
1913 char_u *arg;
1914{
1915 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001916 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001917 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001918 char_u *name_start;
1919 char_u *arg_subsc;
1920 char_u *tofree;
1921 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001922
1923 while (!ends_excmd(*arg) && !got_int)
1924 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001925 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001926 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001927 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001928 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
1929 {
1930 emsg_severe = TRUE;
1931 EMSG(_(e_trailing));
1932 break;
1933 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001934 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001935 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001936 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001937 /* get_name_len() takes care of expanding curly braces */
1938 name_start = name = arg;
1939 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1940 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001941 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001942 /* This is mainly to keep test 49 working: when expanding
1943 * curly braces fails overrule the exception error message. */
1944 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001945 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001946 emsg_severe = TRUE;
1947 EMSG2(_(e_invarg2), arg);
1948 break;
1949 }
1950 error = TRUE;
1951 }
1952 else
1953 {
1954 if (tofree != NULL)
1955 name = tofree;
1956 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001957 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001958 else
1959 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001960 /* handle d.key, l[idx], f(expr) */
1961 arg_subsc = arg;
1962 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001963 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001964 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001965 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001966 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001967 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001968 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001969 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001970 case 'g': list_glob_vars(); break;
1971 case 'b': list_buf_vars(); break;
1972 case 'w': list_win_vars(); break;
1973 case 'v': list_vim_vars(); break;
1974 default:
1975 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001976 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001977 }
1978 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001979 {
1980 char_u numbuf[NUMBUFLEN];
1981 char_u *tf;
1982 int c;
1983 char_u *s;
1984
1985 s = echo_string(&tv, &tf, numbuf);
1986 c = *arg;
1987 *arg = NUL;
1988 list_one_var_a((char_u *)"",
1989 arg == arg_subsc ? name : name_start,
1990 tv.v_type, s == NULL ? (char_u *)"" : s);
1991 *arg = c;
1992 vim_free(tf);
1993 }
1994 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001995 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001996 }
1997 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001998
1999 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002000 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002001
2002 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002003 }
2004
2005 return arg;
2006}
2007
2008/*
2009 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2010 * Returns a pointer to the char just after the var name.
2011 * Returns NULL if there is an error.
2012 */
2013 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002014ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002015 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002016 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002017 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002018 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002019 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002020{
2021 int c1;
2022 char_u *name;
2023 char_u *p;
2024 char_u *arg_end = NULL;
2025 int len;
2026 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002027 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002028
2029 /*
2030 * ":let $VAR = expr": Set environment variable.
2031 */
2032 if (*arg == '$')
2033 {
2034 /* Find the end of the name. */
2035 ++arg;
2036 name = arg;
2037 len = get_env_len(&arg);
2038 if (len == 0)
2039 EMSG2(_(e_invarg2), name - 1);
2040 else
2041 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002042 if (op != NULL && (*op == '+' || *op == '-'))
2043 EMSG2(_(e_letwrong), op);
2044 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002045 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002046 EMSG(_(e_letunexp));
2047 else
2048 {
2049 c1 = name[len];
2050 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002051 p = get_tv_string_chk(tv);
2052 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002053 {
2054 int mustfree = FALSE;
2055 char_u *s = vim_getenv(name, &mustfree);
2056
2057 if (s != NULL)
2058 {
2059 p = tofree = concat_str(s, p);
2060 if (mustfree)
2061 vim_free(s);
2062 }
2063 }
2064 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002065 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002066 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002067 if (STRICMP(name, "HOME") == 0)
2068 init_homedir();
2069 else if (didset_vim && STRICMP(name, "VIM") == 0)
2070 didset_vim = FALSE;
2071 else if (didset_vimruntime
2072 && STRICMP(name, "VIMRUNTIME") == 0)
2073 didset_vimruntime = FALSE;
2074 arg_end = arg;
2075 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002076 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002077 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002078 }
2079 }
2080 }
2081
2082 /*
2083 * ":let &option = expr": Set option value.
2084 * ":let &l:option = expr": Set local option value.
2085 * ":let &g:option = expr": Set global option value.
2086 */
2087 else if (*arg == '&')
2088 {
2089 /* Find the end of the name. */
2090 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002091 if (p == NULL || (endchars != NULL
2092 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002093 EMSG(_(e_letunexp));
2094 else
2095 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002096 long n;
2097 int opt_type;
2098 long numval;
2099 char_u *stringval = NULL;
2100 char_u *s;
2101
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002102 c1 = *p;
2103 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002104
2105 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002106 s = get_tv_string_chk(tv); /* != NULL if number or string */
2107 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002108 {
2109 opt_type = get_option_value(arg, &numval,
2110 &stringval, opt_flags);
2111 if ((opt_type == 1 && *op == '.')
2112 || (opt_type == 0 && *op != '.'))
2113 EMSG2(_(e_letwrong), op);
2114 else
2115 {
2116 if (opt_type == 1) /* number */
2117 {
2118 if (*op == '+')
2119 n = numval + n;
2120 else
2121 n = numval - n;
2122 }
2123 else if (opt_type == 0 && stringval != NULL) /* string */
2124 {
2125 s = concat_str(stringval, s);
2126 vim_free(stringval);
2127 stringval = s;
2128 }
2129 }
2130 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002131 if (s != NULL)
2132 {
2133 set_option_value(arg, n, s, opt_flags);
2134 arg_end = p;
2135 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002136 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002137 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002138 }
2139 }
2140
2141 /*
2142 * ":let @r = expr": Set register contents.
2143 */
2144 else if (*arg == '@')
2145 {
2146 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002147 if (op != NULL && (*op == '+' || *op == '-'))
2148 EMSG2(_(e_letwrong), op);
2149 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002150 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002151 EMSG(_(e_letunexp));
2152 else
2153 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002154 char_u *tofree = NULL;
2155 char_u *s;
2156
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002157 p = get_tv_string_chk(tv);
2158 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002159 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002160 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002161 if (s != NULL)
2162 {
2163 p = tofree = concat_str(s, p);
2164 vim_free(s);
2165 }
2166 }
2167 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002168 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002169 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002170 arg_end = arg + 1;
2171 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002172 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002173 }
2174 }
2175
2176 /*
2177 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002178 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002179 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002180 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002181 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002182 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002183
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002184 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002185 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002186 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002187 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2188 EMSG(_(e_letunexp));
2189 else
2190 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002191 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002192 arg_end = p;
2193 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002194 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002195 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002196 }
2197
2198 else
2199 EMSG2(_(e_invarg2), arg);
2200
2201 return arg_end;
2202}
2203
2204/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002205 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2206 */
2207 static int
2208check_changedtick(arg)
2209 char_u *arg;
2210{
2211 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2212 {
2213 EMSG2(_(e_readonlyvar), arg);
2214 return TRUE;
2215 }
2216 return FALSE;
2217}
2218
2219/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002220 * Get an lval: variable, Dict item or List item that can be assigned a value
2221 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2222 * "name.key", "name.key[expr]" etc.
2223 * Indexing only works if "name" is an existing List or Dictionary.
2224 * "name" points to the start of the name.
2225 * If "rettv" is not NULL it points to the value to be assigned.
2226 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2227 * wrong; must end in space or cmd separator.
2228 *
2229 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002230 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002231 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002232 */
2233 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002234get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002235 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002236 typval_T *rettv;
2237 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002238 int unlet;
2239 int skip;
2240 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002241 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002242{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002243 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002244 char_u *expr_start, *expr_end;
2245 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002246 dictitem_T *v;
2247 typval_T var1;
2248 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002249 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002250 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002251 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002252 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002253 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002254
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002255 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002256 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002257
2258 if (skip)
2259 {
2260 /* When skipping just find the end of the name. */
2261 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002262 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002263 }
2264
2265 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002266 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002267 if (expr_start != NULL)
2268 {
2269 /* Don't expand the name when we already know there is an error. */
2270 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2271 && *p != '[' && *p != '.')
2272 {
2273 EMSG(_(e_trailing));
2274 return NULL;
2275 }
2276
2277 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2278 if (lp->ll_exp_name == NULL)
2279 {
2280 /* Report an invalid expression in braces, unless the
2281 * expression evaluation has been cancelled due to an
2282 * aborting error, an interrupt, or an exception. */
2283 if (!aborting() && !quiet)
2284 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002285 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002286 EMSG2(_(e_invarg2), name);
2287 return NULL;
2288 }
2289 }
2290 lp->ll_name = lp->ll_exp_name;
2291 }
2292 else
2293 lp->ll_name = name;
2294
2295 /* Without [idx] or .key we are done. */
2296 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2297 return p;
2298
2299 cc = *p;
2300 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002301 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002302 if (v == NULL && !quiet)
2303 EMSG2(_(e_undefvar), lp->ll_name);
2304 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002305 if (v == NULL)
2306 return NULL;
2307
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002308 /*
2309 * Loop until no more [idx] or .key is following.
2310 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002311 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002312 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002313 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002314 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2315 && !(lp->ll_tv->v_type == VAR_DICT
2316 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002317 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002318 if (!quiet)
2319 EMSG(_("E689: Can only index a List or Dictionary"));
2320 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002321 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002322 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002323 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002324 if (!quiet)
2325 EMSG(_("E708: [:] must come last"));
2326 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002327 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002328
Bram Moolenaar8c711452005-01-14 21:53:12 +00002329 len = -1;
2330 if (*p == '.')
2331 {
2332 key = p + 1;
2333 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2334 ;
2335 if (len == 0)
2336 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002337 if (!quiet)
2338 EMSG(_(e_emptykey));
2339 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002340 }
2341 p = key + len;
2342 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002343 else
2344 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002345 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002346 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002347 if (*p == ':')
2348 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002349 else
2350 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002351 empty1 = FALSE;
2352 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002353 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002354 if (get_tv_string_chk(&var1) == NULL)
2355 {
2356 /* not a number or string */
2357 clear_tv(&var1);
2358 return NULL;
2359 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002360 }
2361
2362 /* Optionally get the second index [ :expr]. */
2363 if (*p == ':')
2364 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002365 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002366 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002367 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002368 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002369 if (!empty1)
2370 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002371 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002372 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002373 if (rettv != NULL && (rettv->v_type != VAR_LIST
2374 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002375 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002376 if (!quiet)
2377 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002378 if (!empty1)
2379 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002380 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002381 }
2382 p = skipwhite(p + 1);
2383 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002384 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002385 else
2386 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002387 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002388 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2389 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002390 if (!empty1)
2391 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002392 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002393 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002394 if (get_tv_string_chk(&var2) == NULL)
2395 {
2396 /* not a number or string */
2397 if (!empty1)
2398 clear_tv(&var1);
2399 clear_tv(&var2);
2400 return NULL;
2401 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002402 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002403 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002404 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002405 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002406 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002407
Bram Moolenaar8c711452005-01-14 21:53:12 +00002408 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002409 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002410 if (!quiet)
2411 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002412 if (!empty1)
2413 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002414 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002415 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002416 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002417 }
2418
2419 /* Skip to past ']'. */
2420 ++p;
2421 }
2422
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002423 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002424 {
2425 if (len == -1)
2426 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002427 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002428 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002429 if (*key == NUL)
2430 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002431 if (!quiet)
2432 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002433 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002434 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002435 }
2436 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002437 lp->ll_list = NULL;
2438 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002439 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002440 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002441 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002442 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002443 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002444 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002445 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002446 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002447 if (len == -1)
2448 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002449 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002450 }
2451 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002452 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002453 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002454 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002455 if (len == -1)
2456 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002457 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002458 p = NULL;
2459 break;
2460 }
2461 if (len == -1)
2462 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002463 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002464 }
2465 else
2466 {
2467 /*
2468 * Get the number and item for the only or first index of the List.
2469 */
2470 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002471 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002472 else
2473 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002474 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002475 clear_tv(&var1);
2476 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002477 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002478 lp->ll_list = lp->ll_tv->vval.v_list;
2479 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2480 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002481 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002482 if (!quiet)
2483 EMSGN(_(e_listidx), lp->ll_n1);
2484 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002485 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002486 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002487 }
2488
2489 /*
2490 * May need to find the item or absolute index for the second
2491 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002492 * When no index given: "lp->ll_empty2" is TRUE.
2493 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002494 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002495 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002496 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002497 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002498 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002499 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002500 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002501 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002502 if (ni == NULL)
2503 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002504 if (!quiet)
2505 EMSGN(_(e_listidx), lp->ll_n2);
2506 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002507 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002508 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002509 }
2510
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002511 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2512 if (lp->ll_n1 < 0)
2513 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2514 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002515 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002516 if (!quiet)
2517 EMSGN(_(e_listidx), lp->ll_n2);
2518 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002519 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002520 }
2521
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002522 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002523 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002524 }
2525
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002526 return p;
2527}
2528
2529/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002530 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002531 */
2532 static void
2533clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002534 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002535{
2536 vim_free(lp->ll_exp_name);
2537 vim_free(lp->ll_newkey);
2538}
2539
2540/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002541 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002542 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002543 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002544 */
2545 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002546set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002547 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002548 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002549 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002550 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002551 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002552{
2553 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002554 listitem_T *ni;
2555 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. */
2606 ni = listitem_alloc();
2607 if (ni == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002608 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002609 ri = NULL;
2610 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002611 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002612 ni->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002613 ni->li_tv.v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002614 ni->li_tv.vval.v_number = 0;
2615 list_append(lp->ll_list, ni);
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002616 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002617 lp->ll_li = lp->ll_li->li_next;
2618 ++lp->ll_n1;
2619 }
2620 if (ri != NULL)
2621 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002622 else if (lp->ll_empty2
2623 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002624 : lp->ll_n1 != lp->ll_n2)
2625 EMSG(_("E711: List value has not enough items"));
2626 }
2627 else
2628 {
2629 /*
2630 * Assign to a List or Dictionary item.
2631 */
2632 if (lp->ll_newkey != NULL)
2633 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002634 if (op != NULL && *op != '=')
2635 {
2636 EMSG2(_(e_letwrong), op);
2637 return;
2638 }
2639
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002640 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002641 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002642 if (di == NULL)
2643 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002644 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2645 {
2646 vim_free(di);
2647 return;
2648 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002649 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002650 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002651 else if (op != NULL && *op != '=')
2652 {
2653 tv_op(lp->ll_tv, rettv, op);
2654 return;
2655 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002656 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002657 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002658
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002659 /*
2660 * Assign the value to the variable or list item.
2661 */
2662 if (copy)
2663 copy_tv(rettv, lp->ll_tv);
2664 else
2665 {
2666 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002667 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002668 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002669 }
2670 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002671}
2672
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002673/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002674 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2675 * Returns OK or FAIL.
2676 */
2677 static int
2678tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002679 typval_T *tv1;
2680 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002681 char_u *op;
2682{
2683 long n;
2684 char_u numbuf[NUMBUFLEN];
2685 char_u *s;
2686
2687 /* Can't do anything with a Funcref or a Dict on the right. */
2688 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2689 {
2690 switch (tv1->v_type)
2691 {
2692 case VAR_DICT:
2693 case VAR_FUNC:
2694 break;
2695
2696 case VAR_LIST:
2697 if (*op != '+' || tv2->v_type != VAR_LIST)
2698 break;
2699 /* List += List */
2700 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2701 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2702 return OK;
2703
2704 case VAR_NUMBER:
2705 case VAR_STRING:
2706 if (tv2->v_type == VAR_LIST)
2707 break;
2708 if (*op == '+' || *op == '-')
2709 {
2710 /* nr += nr or nr -= nr*/
2711 n = get_tv_number(tv1);
2712 if (*op == '+')
2713 n += get_tv_number(tv2);
2714 else
2715 n -= get_tv_number(tv2);
2716 clear_tv(tv1);
2717 tv1->v_type = VAR_NUMBER;
2718 tv1->vval.v_number = n;
2719 }
2720 else
2721 {
2722 /* str .= str */
2723 s = get_tv_string(tv1);
2724 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2725 clear_tv(tv1);
2726 tv1->v_type = VAR_STRING;
2727 tv1->vval.v_string = s;
2728 }
2729 return OK;
2730 }
2731 }
2732
2733 EMSG2(_(e_letwrong), op);
2734 return FAIL;
2735}
2736
2737/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002738 * Add a watcher to a list.
2739 */
2740 static void
2741list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002742 list_T *l;
2743 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002744{
2745 lw->lw_next = l->lv_watch;
2746 l->lv_watch = lw;
2747}
2748
2749/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002750 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002751 * No warning when it isn't found...
2752 */
2753 static void
2754list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002755 list_T *l;
2756 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002757{
Bram Moolenaar33570922005-01-25 22:26:29 +00002758 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002759
2760 lwp = &l->lv_watch;
2761 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2762 {
2763 if (lw == lwrem)
2764 {
2765 *lwp = lw->lw_next;
2766 break;
2767 }
2768 lwp = &lw->lw_next;
2769 }
2770}
2771
2772/*
2773 * Just before removing an item from a list: advance watchers to the next
2774 * item.
2775 */
2776 static void
2777list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002778 list_T *l;
2779 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002780{
Bram Moolenaar33570922005-01-25 22:26:29 +00002781 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002782
2783 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2784 if (lw->lw_item == item)
2785 lw->lw_item = item->li_next;
2786}
2787
2788/*
2789 * Evaluate the expression used in a ":for var in expr" command.
2790 * "arg" points to "var".
2791 * Set "*errp" to TRUE for an error, FALSE otherwise;
2792 * Return a pointer that holds the info. Null when there is an error.
2793 */
2794 void *
2795eval_for_line(arg, errp, nextcmdp, skip)
2796 char_u *arg;
2797 int *errp;
2798 char_u **nextcmdp;
2799 int skip;
2800{
Bram Moolenaar33570922005-01-25 22:26:29 +00002801 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002802 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002803 typval_T tv;
2804 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002805
2806 *errp = TRUE; /* default: there is an error */
2807
Bram Moolenaar33570922005-01-25 22:26:29 +00002808 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002809 if (fi == NULL)
2810 return NULL;
2811
2812 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2813 if (expr == NULL)
2814 return fi;
2815
2816 expr = skipwhite(expr);
2817 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2818 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002819 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002820 return fi;
2821 }
2822
2823 if (skip)
2824 ++emsg_skip;
2825 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2826 {
2827 *errp = FALSE;
2828 if (!skip)
2829 {
2830 l = tv.vval.v_list;
2831 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002832 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002833 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002834 clear_tv(&tv);
2835 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002836 else
2837 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002838 /* No need to increment the refcount, it's already set for the
2839 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002840 fi->fi_list = l;
2841 list_add_watch(l, &fi->fi_lw);
2842 fi->fi_lw.lw_item = l->lv_first;
2843 }
2844 }
2845 }
2846 if (skip)
2847 --emsg_skip;
2848
2849 return fi;
2850}
2851
2852/*
2853 * Use the first item in a ":for" list. Advance to the next.
2854 * Assign the values to the variable (list). "arg" points to the first one.
2855 * Return TRUE when a valid item was found, FALSE when at end of list or
2856 * something wrong.
2857 */
2858 int
2859next_for_item(fi_void, arg)
2860 void *fi_void;
2861 char_u *arg;
2862{
Bram Moolenaar33570922005-01-25 22:26:29 +00002863 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002864 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002865 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002866
2867 item = fi->fi_lw.lw_item;
2868 if (item == NULL)
2869 result = FALSE;
2870 else
2871 {
2872 fi->fi_lw.lw_item = item->li_next;
2873 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2874 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2875 }
2876 return result;
2877}
2878
2879/*
2880 * Free the structure used to store info used by ":for".
2881 */
2882 void
2883free_for_info(fi_void)
2884 void *fi_void;
2885{
Bram Moolenaar33570922005-01-25 22:26:29 +00002886 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002887
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002888 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002889 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002890 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002891 list_unref(fi->fi_list);
2892 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002893 vim_free(fi);
2894}
2895
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2897
2898 void
2899set_context_for_expression(xp, arg, cmdidx)
2900 expand_T *xp;
2901 char_u *arg;
2902 cmdidx_T cmdidx;
2903{
2904 int got_eq = FALSE;
2905 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002906 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002908 if (cmdidx == CMD_let)
2909 {
2910 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002911 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002912 {
2913 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002914 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002915 {
2916 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00002917 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002918 if (vim_iswhite(*p))
2919 break;
2920 }
2921 return;
2922 }
2923 }
2924 else
2925 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2926 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 while ((xp->xp_pattern = vim_strpbrk(arg,
2928 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2929 {
2930 c = *xp->xp_pattern;
2931 if (c == '&')
2932 {
2933 c = xp->xp_pattern[1];
2934 if (c == '&')
2935 {
2936 ++xp->xp_pattern;
2937 xp->xp_context = cmdidx != CMD_let || got_eq
2938 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2939 }
2940 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002941 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002943 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2944 xp->xp_pattern += 2;
2945
2946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 }
2948 else if (c == '$')
2949 {
2950 /* environment variable */
2951 xp->xp_context = EXPAND_ENV_VARS;
2952 }
2953 else if (c == '=')
2954 {
2955 got_eq = TRUE;
2956 xp->xp_context = EXPAND_EXPRESSION;
2957 }
2958 else if (c == '<'
2959 && xp->xp_context == EXPAND_FUNCTIONS
2960 && vim_strchr(xp->xp_pattern, '(') == NULL)
2961 {
2962 /* Function name can start with "<SNR>" */
2963 break;
2964 }
2965 else if (cmdidx != CMD_let || got_eq)
2966 {
2967 if (c == '"') /* string */
2968 {
2969 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2970 if (c == '\\' && xp->xp_pattern[1] != NUL)
2971 ++xp->xp_pattern;
2972 xp->xp_context = EXPAND_NOTHING;
2973 }
2974 else if (c == '\'') /* literal string */
2975 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002976 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2978 /* skip */ ;
2979 xp->xp_context = EXPAND_NOTHING;
2980 }
2981 else if (c == '|')
2982 {
2983 if (xp->xp_pattern[1] == '|')
2984 {
2985 ++xp->xp_pattern;
2986 xp->xp_context = EXPAND_EXPRESSION;
2987 }
2988 else
2989 xp->xp_context = EXPAND_COMMANDS;
2990 }
2991 else
2992 xp->xp_context = EXPAND_EXPRESSION;
2993 }
2994 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002995 /* Doesn't look like something valid, expand as an expression
2996 * anyway. */
2997 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 arg = xp->xp_pattern;
2999 if (*arg != NUL)
3000 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3001 /* skip */ ;
3002 }
3003 xp->xp_pattern = arg;
3004}
3005
3006#endif /* FEAT_CMDL_COMPL */
3007
3008/*
3009 * ":1,25call func(arg1, arg2)" function call.
3010 */
3011 void
3012ex_call(eap)
3013 exarg_T *eap;
3014{
3015 char_u *arg = eap->arg;
3016 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003018 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003020 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 linenr_T lnum;
3022 int doesrange;
3023 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003024 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003026 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
3027 vim_free(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003028 if (tofree == NULL)
3029 return;
3030
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003031 /* Increase refcount on dictionary, it could get deleted when evaluating
3032 * the arguments. */
3033 if (fudi.fd_dict != NULL)
3034 ++fudi.fd_dict->dv_refcount;
3035
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003036 /* If it is the name of a variable of type VAR_FUNC use its contents. */
3037 len = STRLEN(tofree);
3038 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039
Bram Moolenaar532c7802005-01-27 14:44:31 +00003040 /* Skip white space to allow ":call func ()". Not good, but required for
3041 * backward compatibility. */
3042 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003043 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044
3045 if (*startarg != '(')
3046 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003047 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 goto end;
3049 }
3050
3051 /*
3052 * When skipping, evaluate the function once, to find the end of the
3053 * arguments.
3054 * When the function takes a range, this is discovered after the first
3055 * call, and the loop is broken.
3056 */
3057 if (eap->skip)
3058 {
3059 ++emsg_skip;
3060 lnum = eap->line2; /* do it once, also with an invalid range */
3061 }
3062 else
3063 lnum = eap->line1;
3064 for ( ; lnum <= eap->line2; ++lnum)
3065 {
3066 if (!eap->skip && eap->addr_count > 0)
3067 {
3068 curwin->w_cursor.lnum = lnum;
3069 curwin->w_cursor.col = 0;
3070 }
3071 arg = startarg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003072 if (get_func_tv(name, STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003073 eap->line1, eap->line2, &doesrange,
3074 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 {
3076 failed = TRUE;
3077 break;
3078 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003079 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 if (doesrange || eap->skip)
3081 break;
3082 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003083 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003084 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003085 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 if (aborting())
3087 break;
3088 }
3089 if (eap->skip)
3090 --emsg_skip;
3091
3092 if (!failed)
3093 {
3094 /* Check for trailing illegal characters and a following command. */
3095 if (!ends_excmd(*arg))
3096 {
3097 emsg_severe = TRUE;
3098 EMSG(_(e_trailing));
3099 }
3100 else
3101 eap->nextcmd = check_nextcmd(arg);
3102 }
3103
3104end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003105 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003106 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107}
3108
3109/*
3110 * ":unlet[!] var1 ... " command.
3111 */
3112 void
3113ex_unlet(eap)
3114 exarg_T *eap;
3115{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003116 ex_unletlock(eap, eap->arg, 0);
3117}
3118
3119/*
3120 * ":lockvar" and ":unlockvar" commands
3121 */
3122 void
3123ex_lockvar(eap)
3124 exarg_T *eap;
3125{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003127 int deep = 2;
3128
3129 if (eap->forceit)
3130 deep = -1;
3131 else if (vim_isdigit(*arg))
3132 {
3133 deep = getdigits(&arg);
3134 arg = skipwhite(arg);
3135 }
3136
3137 ex_unletlock(eap, arg, deep);
3138}
3139
3140/*
3141 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3142 */
3143 static void
3144ex_unletlock(eap, argstart, deep)
3145 exarg_T *eap;
3146 char_u *argstart;
3147 int deep;
3148{
3149 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003152 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153
3154 do
3155 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003156 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003157 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3158 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003159 if (lv.ll_name == NULL)
3160 error = TRUE; /* error but continue parsing */
3161 if (name_end == NULL || (!vim_iswhite(*name_end)
3162 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003164 if (name_end != NULL)
3165 {
3166 emsg_severe = TRUE;
3167 EMSG(_(e_trailing));
3168 }
3169 if (!(eap->skip || error))
3170 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 break;
3172 }
3173
3174 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003175 {
3176 if (eap->cmdidx == CMD_unlet)
3177 {
3178 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3179 error = TRUE;
3180 }
3181 else
3182 {
3183 if (do_lock_var(&lv, name_end, deep,
3184 eap->cmdidx == CMD_lockvar) == FAIL)
3185 error = TRUE;
3186 }
3187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003189 if (!eap->skip)
3190 clear_lval(&lv);
3191
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 arg = skipwhite(name_end);
3193 } while (!ends_excmd(*arg));
3194
3195 eap->nextcmd = check_nextcmd(arg);
3196}
3197
Bram Moolenaar8c711452005-01-14 21:53:12 +00003198 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003199do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003200 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003201 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003202 int forceit;
3203{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003204 int ret = OK;
3205 int cc;
3206
3207 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003208 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003209 cc = *name_end;
3210 *name_end = NUL;
3211
3212 /* Normal name or expanded name. */
3213 if (check_changedtick(lp->ll_name))
3214 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003215 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003216 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003217 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003218 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003219 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3220 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003221 else if (lp->ll_range)
3222 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003223 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003224
3225 /* Delete a range of List items. */
3226 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3227 {
3228 li = lp->ll_li->li_next;
3229 listitem_remove(lp->ll_list, lp->ll_li);
3230 lp->ll_li = li;
3231 ++lp->ll_n1;
3232 }
3233 }
3234 else
3235 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003236 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003237 /* unlet a List item. */
3238 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003239 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003240 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003241 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003242 }
3243
3244 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003245}
3246
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247/*
3248 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003249 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 */
3251 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003252do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003254 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255{
Bram Moolenaar33570922005-01-25 22:26:29 +00003256 hashtab_T *ht;
3257 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003258 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259
Bram Moolenaar33570922005-01-25 22:26:29 +00003260 ht = find_var_ht(name, &varname);
3261 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003263 hi = hash_find(ht, varname);
3264 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003265 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003266 if (var_check_ro(HI2DI(hi)->di_flags, name))
3267 return FAIL;
3268 delete_var(ht, hi);
3269 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003272 if (forceit)
3273 return OK;
3274 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 return FAIL;
3276}
3277
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003278/*
3279 * Lock or unlock variable indicated by "lp".
3280 * "deep" is the levels to go (-1 for unlimited);
3281 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3282 */
3283 static int
3284do_lock_var(lp, name_end, deep, lock)
3285 lval_T *lp;
3286 char_u *name_end;
3287 int deep;
3288 int lock;
3289{
3290 int ret = OK;
3291 int cc;
3292 dictitem_T *di;
3293
3294 if (deep == 0) /* nothing to do */
3295 return OK;
3296
3297 if (lp->ll_tv == NULL)
3298 {
3299 cc = *name_end;
3300 *name_end = NUL;
3301
3302 /* Normal name or expanded name. */
3303 if (check_changedtick(lp->ll_name))
3304 ret = FAIL;
3305 else
3306 {
3307 di = find_var(lp->ll_name, NULL);
3308 if (di == NULL)
3309 ret = FAIL;
3310 else
3311 {
3312 if (lock)
3313 di->di_flags |= DI_FLAGS_LOCK;
3314 else
3315 di->di_flags &= ~DI_FLAGS_LOCK;
3316 item_lock(&di->di_tv, deep, lock);
3317 }
3318 }
3319 *name_end = cc;
3320 }
3321 else if (lp->ll_range)
3322 {
3323 listitem_T *li = lp->ll_li;
3324
3325 /* (un)lock a range of List items. */
3326 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3327 {
3328 item_lock(&li->li_tv, deep, lock);
3329 li = li->li_next;
3330 ++lp->ll_n1;
3331 }
3332 }
3333 else if (lp->ll_list != NULL)
3334 /* (un)lock a List item. */
3335 item_lock(&lp->ll_li->li_tv, deep, lock);
3336 else
3337 /* un(lock) a Dictionary item. */
3338 item_lock(&lp->ll_di->di_tv, deep, lock);
3339
3340 return ret;
3341}
3342
3343/*
3344 * Lock or unlock an item. "deep" is nr of levels to go.
3345 */
3346 static void
3347item_lock(tv, deep, lock)
3348 typval_T *tv;
3349 int deep;
3350 int lock;
3351{
3352 static int recurse = 0;
3353 list_T *l;
3354 listitem_T *li;
3355 dict_T *d;
3356 hashitem_T *hi;
3357 int todo;
3358
3359 if (recurse >= DICT_MAXNEST)
3360 {
3361 EMSG(_("E743: variable nested too deep for (un)lock"));
3362 return;
3363 }
3364 if (deep == 0)
3365 return;
3366 ++recurse;
3367
3368 /* lock/unlock the item itself */
3369 if (lock)
3370 tv->v_lock |= VAR_LOCKED;
3371 else
3372 tv->v_lock &= ~VAR_LOCKED;
3373
3374 switch (tv->v_type)
3375 {
3376 case VAR_LIST:
3377 if ((l = tv->vval.v_list) != NULL)
3378 {
3379 if (lock)
3380 l->lv_lock |= VAR_LOCKED;
3381 else
3382 l->lv_lock &= ~VAR_LOCKED;
3383 if (deep < 0 || deep > 1)
3384 /* recursive: lock/unlock the items the List contains */
3385 for (li = l->lv_first; li != NULL; li = li->li_next)
3386 item_lock(&li->li_tv, deep - 1, lock);
3387 }
3388 break;
3389 case VAR_DICT:
3390 if ((d = tv->vval.v_dict) != NULL)
3391 {
3392 if (lock)
3393 d->dv_lock |= VAR_LOCKED;
3394 else
3395 d->dv_lock &= ~VAR_LOCKED;
3396 if (deep < 0 || deep > 1)
3397 {
3398 /* recursive: lock/unlock the items the List contains */
3399 todo = d->dv_hashtab.ht_used;
3400 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3401 {
3402 if (!HASHITEM_EMPTY(hi))
3403 {
3404 --todo;
3405 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3406 }
3407 }
3408 }
3409 }
3410 }
3411 --recurse;
3412}
3413
Bram Moolenaara40058a2005-07-11 22:42:07 +00003414/*
3415 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3416 * it refers to a List or Dictionary that is locked.
3417 */
3418 static int
3419tv_islocked(tv)
3420 typval_T *tv;
3421{
3422 return (tv->v_lock & VAR_LOCKED)
3423 || (tv->v_type == VAR_LIST
3424 && tv->vval.v_list != NULL
3425 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3426 || (tv->v_type == VAR_DICT
3427 && tv->vval.v_dict != NULL
3428 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3429}
3430
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3432/*
3433 * Delete all "menutrans_" variables.
3434 */
3435 void
3436del_menutrans_vars()
3437{
Bram Moolenaar33570922005-01-25 22:26:29 +00003438 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003439 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440
Bram Moolenaar33570922005-01-25 22:26:29 +00003441 hash_lock(&globvarht);
3442 todo = globvarht.ht_used;
3443 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003444 {
3445 if (!HASHITEM_EMPTY(hi))
3446 {
3447 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003448 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3449 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003450 }
3451 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003452 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453}
3454#endif
3455
3456#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3457
3458/*
3459 * Local string buffer for the next two functions to store a variable name
3460 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3461 * get_user_var_name().
3462 */
3463
3464static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3465
3466static char_u *varnamebuf = NULL;
3467static int varnamebuflen = 0;
3468
3469/*
3470 * Function to concatenate a prefix and a variable name.
3471 */
3472 static char_u *
3473cat_prefix_varname(prefix, name)
3474 int prefix;
3475 char_u *name;
3476{
3477 int len;
3478
3479 len = (int)STRLEN(name) + 3;
3480 if (len > varnamebuflen)
3481 {
3482 vim_free(varnamebuf);
3483 len += 10; /* some additional space */
3484 varnamebuf = alloc(len);
3485 if (varnamebuf == NULL)
3486 {
3487 varnamebuflen = 0;
3488 return NULL;
3489 }
3490 varnamebuflen = len;
3491 }
3492 *varnamebuf = prefix;
3493 varnamebuf[1] = ':';
3494 STRCPY(varnamebuf + 2, name);
3495 return varnamebuf;
3496}
3497
3498/*
3499 * Function given to ExpandGeneric() to obtain the list of user defined
3500 * (global/buffer/window/built-in) variable names.
3501 */
3502/*ARGSUSED*/
3503 char_u *
3504get_user_var_name(xp, idx)
3505 expand_T *xp;
3506 int idx;
3507{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003508 static long_u gdone;
3509 static long_u bdone;
3510 static long_u wdone;
3511 static int vidx;
3512 static hashitem_T *hi;
3513 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514
3515 if (idx == 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00003516 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00003517
3518 /* Global variables */
3519 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003521 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003522 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003523 else
3524 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003525 while (HASHITEM_EMPTY(hi))
3526 ++hi;
3527 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3528 return cat_prefix_varname('g', hi->hi_key);
3529 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003531
3532 /* b: variables */
3533 ht = &curbuf->b_vars.dv_hashtab;
3534 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003536 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003537 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003538 else
3539 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003540 while (HASHITEM_EMPTY(hi))
3541 ++hi;
3542 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003544 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003546 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 return (char_u *)"b:changedtick";
3548 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003549
3550 /* w: variables */
3551 ht = &curwin->w_vars.dv_hashtab;
3552 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003554 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003555 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003556 else
3557 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003558 while (HASHITEM_EMPTY(hi))
3559 ++hi;
3560 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003562
3563 /* v: variables */
3564 if (vidx < VV_LEN)
3565 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566
3567 vim_free(varnamebuf);
3568 varnamebuf = NULL;
3569 varnamebuflen = 0;
3570 return NULL;
3571}
3572
3573#endif /* FEAT_CMDL_COMPL */
3574
3575/*
3576 * types for expressions.
3577 */
3578typedef enum
3579{
3580 TYPE_UNKNOWN = 0
3581 , TYPE_EQUAL /* == */
3582 , TYPE_NEQUAL /* != */
3583 , TYPE_GREATER /* > */
3584 , TYPE_GEQUAL /* >= */
3585 , TYPE_SMALLER /* < */
3586 , TYPE_SEQUAL /* <= */
3587 , TYPE_MATCH /* =~ */
3588 , TYPE_NOMATCH /* !~ */
3589} exptype_T;
3590
3591/*
3592 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003593 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3595 */
3596
3597/*
3598 * Handle zero level expression.
3599 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003600 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 * Return OK or FAIL.
3602 */
3603 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003604eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003606 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 char_u **nextcmd;
3608 int evaluate;
3609{
3610 int ret;
3611 char_u *p;
3612
3613 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003614 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 if (ret == FAIL || !ends_excmd(*p))
3616 {
3617 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003618 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 /*
3620 * Report the invalid expression unless the expression evaluation has
3621 * been cancelled due to an aborting error, an interrupt, or an
3622 * exception.
3623 */
3624 if (!aborting())
3625 EMSG2(_(e_invexpr2), arg);
3626 ret = FAIL;
3627 }
3628 if (nextcmd != NULL)
3629 *nextcmd = check_nextcmd(p);
3630
3631 return ret;
3632}
3633
3634/*
3635 * Handle top level expression:
3636 * expr1 ? expr0 : expr0
3637 *
3638 * "arg" must point to the first non-white of the expression.
3639 * "arg" is advanced to the next non-white after the recognized expression.
3640 *
3641 * Return OK or FAIL.
3642 */
3643 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003644eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003646 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 int evaluate;
3648{
3649 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003650 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651
3652 /*
3653 * Get the first variable.
3654 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003655 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 return FAIL;
3657
3658 if ((*arg)[0] == '?')
3659 {
3660 result = FALSE;
3661 if (evaluate)
3662 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003663 int error = FALSE;
3664
3665 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003667 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003668 if (error)
3669 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 }
3671
3672 /*
3673 * Get the second variable.
3674 */
3675 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003676 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 return FAIL;
3678
3679 /*
3680 * Check for the ":".
3681 */
3682 if ((*arg)[0] != ':')
3683 {
3684 EMSG(_("E109: Missing ':' after '?'"));
3685 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003686 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 return FAIL;
3688 }
3689
3690 /*
3691 * Get the third variable.
3692 */
3693 *arg = skipwhite(*arg + 1);
3694 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3695 {
3696 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003697 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 return FAIL;
3699 }
3700 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003701 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 }
3703
3704 return OK;
3705}
3706
3707/*
3708 * Handle first level expression:
3709 * expr2 || expr2 || expr2 logical OR
3710 *
3711 * "arg" must point to the first non-white of the expression.
3712 * "arg" is advanced to the next non-white after the recognized expression.
3713 *
3714 * Return OK or FAIL.
3715 */
3716 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003717eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003719 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 int evaluate;
3721{
Bram Moolenaar33570922005-01-25 22:26:29 +00003722 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 long result;
3724 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003725 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726
3727 /*
3728 * Get the first variable.
3729 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003730 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 return FAIL;
3732
3733 /*
3734 * Repeat until there is no following "||".
3735 */
3736 first = TRUE;
3737 result = FALSE;
3738 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3739 {
3740 if (evaluate && first)
3741 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003742 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003744 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003745 if (error)
3746 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 first = FALSE;
3748 }
3749
3750 /*
3751 * Get the second variable.
3752 */
3753 *arg = skipwhite(*arg + 2);
3754 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3755 return FAIL;
3756
3757 /*
3758 * Compute the result.
3759 */
3760 if (evaluate && !result)
3761 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003762 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003764 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003765 if (error)
3766 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 }
3768 if (evaluate)
3769 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003770 rettv->v_type = VAR_NUMBER;
3771 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 }
3773 }
3774
3775 return OK;
3776}
3777
3778/*
3779 * Handle second level expression:
3780 * expr3 && expr3 && expr3 logical AND
3781 *
3782 * "arg" must point to the first non-white of the expression.
3783 * "arg" is advanced to the next non-white after the recognized expression.
3784 *
3785 * Return OK or FAIL.
3786 */
3787 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003788eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003790 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 int evaluate;
3792{
Bram Moolenaar33570922005-01-25 22:26:29 +00003793 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 long result;
3795 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003796 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797
3798 /*
3799 * Get the first variable.
3800 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003801 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 return FAIL;
3803
3804 /*
3805 * Repeat until there is no following "&&".
3806 */
3807 first = TRUE;
3808 result = TRUE;
3809 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3810 {
3811 if (evaluate && first)
3812 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003813 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003815 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003816 if (error)
3817 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 first = FALSE;
3819 }
3820
3821 /*
3822 * Get the second variable.
3823 */
3824 *arg = skipwhite(*arg + 2);
3825 if (eval4(arg, &var2, evaluate && result) == FAIL)
3826 return FAIL;
3827
3828 /*
3829 * Compute the result.
3830 */
3831 if (evaluate && result)
3832 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003833 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003835 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003836 if (error)
3837 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 }
3839 if (evaluate)
3840 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003841 rettv->v_type = VAR_NUMBER;
3842 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 }
3844 }
3845
3846 return OK;
3847}
3848
3849/*
3850 * Handle third level expression:
3851 * var1 == var2
3852 * var1 =~ var2
3853 * var1 != var2
3854 * var1 !~ var2
3855 * var1 > var2
3856 * var1 >= var2
3857 * var1 < var2
3858 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003859 * var1 is var2
3860 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 *
3862 * "arg" must point to the first non-white of the expression.
3863 * "arg" is advanced to the next non-white after the recognized expression.
3864 *
3865 * Return OK or FAIL.
3866 */
3867 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003868eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003870 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 int evaluate;
3872{
Bram Moolenaar33570922005-01-25 22:26:29 +00003873 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 char_u *p;
3875 int i;
3876 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003877 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 int len = 2;
3879 long n1, n2;
3880 char_u *s1, *s2;
3881 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3882 regmatch_T regmatch;
3883 int ic;
3884 char_u *save_cpo;
3885
3886 /*
3887 * Get the first variable.
3888 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003889 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 return FAIL;
3891
3892 p = *arg;
3893 switch (p[0])
3894 {
3895 case '=': if (p[1] == '=')
3896 type = TYPE_EQUAL;
3897 else if (p[1] == '~')
3898 type = TYPE_MATCH;
3899 break;
3900 case '!': if (p[1] == '=')
3901 type = TYPE_NEQUAL;
3902 else if (p[1] == '~')
3903 type = TYPE_NOMATCH;
3904 break;
3905 case '>': if (p[1] != '=')
3906 {
3907 type = TYPE_GREATER;
3908 len = 1;
3909 }
3910 else
3911 type = TYPE_GEQUAL;
3912 break;
3913 case '<': if (p[1] != '=')
3914 {
3915 type = TYPE_SMALLER;
3916 len = 1;
3917 }
3918 else
3919 type = TYPE_SEQUAL;
3920 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003921 case 'i': if (p[1] == 's')
3922 {
3923 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3924 len = 5;
3925 if (!vim_isIDc(p[len]))
3926 {
3927 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3928 type_is = TRUE;
3929 }
3930 }
3931 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 }
3933
3934 /*
3935 * If there is a comparitive operator, use it.
3936 */
3937 if (type != TYPE_UNKNOWN)
3938 {
3939 /* extra question mark appended: ignore case */
3940 if (p[len] == '?')
3941 {
3942 ic = TRUE;
3943 ++len;
3944 }
3945 /* extra '#' appended: match case */
3946 else if (p[len] == '#')
3947 {
3948 ic = FALSE;
3949 ++len;
3950 }
3951 /* nothing appened: use 'ignorecase' */
3952 else
3953 ic = p_ic;
3954
3955 /*
3956 * Get the second variable.
3957 */
3958 *arg = skipwhite(p + len);
3959 if (eval5(arg, &var2, evaluate) == FAIL)
3960 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003961 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 return FAIL;
3963 }
3964
3965 if (evaluate)
3966 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003967 if (type_is && rettv->v_type != var2.v_type)
3968 {
3969 /* For "is" a different type always means FALSE, for "notis"
3970 * it means TRUE. */
3971 n1 = (type == TYPE_NEQUAL);
3972 }
3973 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
3974 {
3975 if (type_is)
3976 {
3977 n1 = (rettv->v_type == var2.v_type
3978 && rettv->vval.v_list == var2.vval.v_list);
3979 if (type == TYPE_NEQUAL)
3980 n1 = !n1;
3981 }
3982 else if (rettv->v_type != var2.v_type
3983 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3984 {
3985 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003986 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003987 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003988 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003989 clear_tv(rettv);
3990 clear_tv(&var2);
3991 return FAIL;
3992 }
3993 else
3994 {
3995 /* Compare two Lists for being equal or unequal. */
3996 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
3997 if (type == TYPE_NEQUAL)
3998 n1 = !n1;
3999 }
4000 }
4001
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004002 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4003 {
4004 if (type_is)
4005 {
4006 n1 = (rettv->v_type == var2.v_type
4007 && rettv->vval.v_dict == var2.vval.v_dict);
4008 if (type == TYPE_NEQUAL)
4009 n1 = !n1;
4010 }
4011 else if (rettv->v_type != var2.v_type
4012 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4013 {
4014 if (rettv->v_type != var2.v_type)
4015 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4016 else
4017 EMSG(_("E736: Invalid operation for Dictionary"));
4018 clear_tv(rettv);
4019 clear_tv(&var2);
4020 return FAIL;
4021 }
4022 else
4023 {
4024 /* Compare two Dictionaries for being equal or unequal. */
4025 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4026 if (type == TYPE_NEQUAL)
4027 n1 = !n1;
4028 }
4029 }
4030
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004031 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4032 {
4033 if (rettv->v_type != var2.v_type
4034 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4035 {
4036 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004037 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004038 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004039 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004040 clear_tv(rettv);
4041 clear_tv(&var2);
4042 return FAIL;
4043 }
4044 else
4045 {
4046 /* Compare two Funcrefs for being equal or unequal. */
4047 if (rettv->vval.v_string == NULL
4048 || var2.vval.v_string == NULL)
4049 n1 = FALSE;
4050 else
4051 n1 = STRCMP(rettv->vval.v_string,
4052 var2.vval.v_string) == 0;
4053 if (type == TYPE_NEQUAL)
4054 n1 = !n1;
4055 }
4056 }
4057
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 /*
4059 * If one of the two variables is a number, compare as a number.
4060 * When using "=~" or "!~", always compare as string.
4061 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004062 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4064 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004065 n1 = get_tv_number(rettv);
4066 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 switch (type)
4068 {
4069 case TYPE_EQUAL: n1 = (n1 == n2); break;
4070 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4071 case TYPE_GREATER: n1 = (n1 > n2); break;
4072 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4073 case TYPE_SMALLER: n1 = (n1 < n2); break;
4074 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4075 case TYPE_UNKNOWN:
4076 case TYPE_MATCH:
4077 case TYPE_NOMATCH: break; /* avoid gcc warning */
4078 }
4079 }
4080 else
4081 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004082 s1 = get_tv_string_buf(rettv, buf1);
4083 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4085 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4086 else
4087 i = 0;
4088 n1 = FALSE;
4089 switch (type)
4090 {
4091 case TYPE_EQUAL: n1 = (i == 0); break;
4092 case TYPE_NEQUAL: n1 = (i != 0); break;
4093 case TYPE_GREATER: n1 = (i > 0); break;
4094 case TYPE_GEQUAL: n1 = (i >= 0); break;
4095 case TYPE_SMALLER: n1 = (i < 0); break;
4096 case TYPE_SEQUAL: n1 = (i <= 0); break;
4097
4098 case TYPE_MATCH:
4099 case TYPE_NOMATCH:
4100 /* avoid 'l' flag in 'cpoptions' */
4101 save_cpo = p_cpo;
4102 p_cpo = (char_u *)"";
4103 regmatch.regprog = vim_regcomp(s2,
4104 RE_MAGIC + RE_STRING);
4105 regmatch.rm_ic = ic;
4106 if (regmatch.regprog != NULL)
4107 {
4108 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4109 vim_free(regmatch.regprog);
4110 if (type == TYPE_NOMATCH)
4111 n1 = !n1;
4112 }
4113 p_cpo = save_cpo;
4114 break;
4115
4116 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4117 }
4118 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004119 clear_tv(rettv);
4120 clear_tv(&var2);
4121 rettv->v_type = VAR_NUMBER;
4122 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 }
4124 }
4125
4126 return OK;
4127}
4128
4129/*
4130 * Handle fourth level expression:
4131 * + number addition
4132 * - number subtraction
4133 * . string concatenation
4134 *
4135 * "arg" must point to the first non-white of the expression.
4136 * "arg" is advanced to the next non-white after the recognized expression.
4137 *
4138 * Return OK or FAIL.
4139 */
4140 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004141eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004143 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 int evaluate;
4145{
Bram Moolenaar33570922005-01-25 22:26:29 +00004146 typval_T var2;
4147 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 int op;
4149 long n1, n2;
4150 char_u *s1, *s2;
4151 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4152 char_u *p;
4153
4154 /*
4155 * Get the first variable.
4156 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004157 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 return FAIL;
4159
4160 /*
4161 * Repeat computing, until no '+', '-' or '.' is following.
4162 */
4163 for (;;)
4164 {
4165 op = **arg;
4166 if (op != '+' && op != '-' && op != '.')
4167 break;
4168
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004169 if (op != '+' || rettv->v_type != VAR_LIST)
4170 {
4171 /* For "list + ...", an illegal use of the first operand as
4172 * a number cannot be determined before evaluating the 2nd
4173 * operand: if this is also a list, all is ok.
4174 * For "something . ...", "something - ..." or "non-list + ...",
4175 * we know that the first operand needs to be a string or number
4176 * without evaluating the 2nd operand. So check before to avoid
4177 * side effects after an error. */
4178 if (evaluate && get_tv_string_chk(rettv) == NULL)
4179 {
4180 clear_tv(rettv);
4181 return FAIL;
4182 }
4183 }
4184
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 /*
4186 * Get the second variable.
4187 */
4188 *arg = skipwhite(*arg + 1);
4189 if (eval6(arg, &var2, evaluate) == FAIL)
4190 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004191 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 return FAIL;
4193 }
4194
4195 if (evaluate)
4196 {
4197 /*
4198 * Compute the result.
4199 */
4200 if (op == '.')
4201 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004202 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4203 s2 = get_tv_string_buf_chk(&var2, buf2);
4204 if (s2 == NULL) /* type error ? */
4205 {
4206 clear_tv(rettv);
4207 clear_tv(&var2);
4208 return FAIL;
4209 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004210 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004211 clear_tv(rettv);
4212 rettv->v_type = VAR_STRING;
4213 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004215 else if (op == '+' && rettv->v_type == VAR_LIST
4216 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004217 {
4218 /* concatenate Lists */
4219 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4220 &var3) == FAIL)
4221 {
4222 clear_tv(rettv);
4223 clear_tv(&var2);
4224 return FAIL;
4225 }
4226 clear_tv(rettv);
4227 *rettv = var3;
4228 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 else
4230 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004231 int error = FALSE;
4232
4233 n1 = get_tv_number_chk(rettv, &error);
4234 if (error)
4235 {
4236 /* This can only happen for "list + non-list".
4237 * For "non-list + ..." or "something - ...", we returned
4238 * before evaluating the 2nd operand. */
4239 clear_tv(rettv);
4240 return FAIL;
4241 }
4242 n2 = get_tv_number_chk(&var2, &error);
4243 if (error)
4244 {
4245 clear_tv(rettv);
4246 clear_tv(&var2);
4247 return FAIL;
4248 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004249 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 if (op == '+')
4251 n1 = n1 + n2;
4252 else
4253 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004254 rettv->v_type = VAR_NUMBER;
4255 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004257 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 }
4259 }
4260 return OK;
4261}
4262
4263/*
4264 * Handle fifth level expression:
4265 * * number multiplication
4266 * / number division
4267 * % number modulo
4268 *
4269 * "arg" must point to the first non-white of the expression.
4270 * "arg" is advanced to the next non-white after the recognized expression.
4271 *
4272 * Return OK or FAIL.
4273 */
4274 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004275eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004277 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 int evaluate;
4279{
Bram Moolenaar33570922005-01-25 22:26:29 +00004280 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 int op;
4282 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004283 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284
4285 /*
4286 * Get the first variable.
4287 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004288 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 return FAIL;
4290
4291 /*
4292 * Repeat computing, until no '*', '/' or '%' is following.
4293 */
4294 for (;;)
4295 {
4296 op = **arg;
4297 if (op != '*' && op != '/' && op != '%')
4298 break;
4299
4300 if (evaluate)
4301 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004302 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004303 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004304 if (error)
4305 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 }
4307 else
4308 n1 = 0;
4309
4310 /*
4311 * Get the second variable.
4312 */
4313 *arg = skipwhite(*arg + 1);
4314 if (eval7(arg, &var2, evaluate) == FAIL)
4315 return FAIL;
4316
4317 if (evaluate)
4318 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004319 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004320 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004321 if (error)
4322 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323
4324 /*
4325 * Compute the result.
4326 */
4327 if (op == '*')
4328 n1 = n1 * n2;
4329 else if (op == '/')
4330 {
4331 if (n2 == 0) /* give an error message? */
4332 n1 = 0x7fffffffL;
4333 else
4334 n1 = n1 / n2;
4335 }
4336 else
4337 {
4338 if (n2 == 0) /* give an error message? */
4339 n1 = 0;
4340 else
4341 n1 = n1 % n2;
4342 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004343 rettv->v_type = VAR_NUMBER;
4344 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 }
4346 }
4347
4348 return OK;
4349}
4350
4351/*
4352 * Handle sixth level expression:
4353 * number number constant
4354 * "string" string contstant
4355 * 'string' literal string contstant
4356 * &option-name option value
4357 * @r register contents
4358 * identifier variable value
4359 * function() function call
4360 * $VAR environment variable
4361 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004362 * [expr, expr] List
4363 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 *
4365 * Also handle:
4366 * ! in front logical NOT
4367 * - in front unary minus
4368 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004369 * trailing [] subscript in String or List
4370 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 *
4372 * "arg" must point to the first non-white of the expression.
4373 * "arg" is advanced to the next non-white after the recognized expression.
4374 *
4375 * Return OK or FAIL.
4376 */
4377 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004378eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004380 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 int evaluate;
4382{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 long n;
4384 int len;
4385 char_u *s;
4386 int val;
4387 char_u *start_leader, *end_leader;
4388 int ret = OK;
4389 char_u *alias;
4390
4391 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004392 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004393 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004395 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396
4397 /*
4398 * Skip '!' and '-' characters. They are handled later.
4399 */
4400 start_leader = *arg;
4401 while (**arg == '!' || **arg == '-' || **arg == '+')
4402 *arg = skipwhite(*arg + 1);
4403 end_leader = *arg;
4404
4405 switch (**arg)
4406 {
4407 /*
4408 * Number constant.
4409 */
4410 case '0':
4411 case '1':
4412 case '2':
4413 case '3':
4414 case '4':
4415 case '5':
4416 case '6':
4417 case '7':
4418 case '8':
4419 case '9':
4420 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4421 *arg += len;
4422 if (evaluate)
4423 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004424 rettv->v_type = VAR_NUMBER;
4425 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 }
4427 break;
4428
4429 /*
4430 * String constant: "string".
4431 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004432 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 break;
4434
4435 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004436 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004438 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004439 break;
4440
4441 /*
4442 * List: [expr, expr]
4443 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004444 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 break;
4446
4447 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004448 * Dictionary: {key: val, key: val}
4449 */
4450 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4451 break;
4452
4453 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004454 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004456 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 break;
4458
4459 /*
4460 * Environment variable: $VAR.
4461 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004462 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 break;
4464
4465 /*
4466 * Register contents: @r.
4467 */
4468 case '@': ++*arg;
4469 if (evaluate)
4470 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004471 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004472 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 }
4474 if (**arg != NUL)
4475 ++*arg;
4476 break;
4477
4478 /*
4479 * nested expression: (expression).
4480 */
4481 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004482 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 if (**arg == ')')
4484 ++*arg;
4485 else if (ret == OK)
4486 {
4487 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004488 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 ret = FAIL;
4490 }
4491 break;
4492
Bram Moolenaar8c711452005-01-14 21:53:12 +00004493 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 break;
4495 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004496
4497 if (ret == NOTDONE)
4498 {
4499 /*
4500 * Must be a variable or function name.
4501 * Can also be a curly-braces kind of name: {expr}.
4502 */
4503 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004504 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004505 if (alias != NULL)
4506 s = alias;
4507
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004508 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004509 ret = FAIL;
4510 else
4511 {
4512 if (**arg == '(') /* recursive! */
4513 {
4514 /* If "s" is the name of a variable of type VAR_FUNC
4515 * use its contents. */
4516 s = deref_func_name(s, &len);
4517
4518 /* Invoke the function. */
4519 ret = get_func_tv(s, len, rettv, arg,
4520 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004521 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004522 /* Stop the expression evaluation when immediately
4523 * aborting on error, or when an interrupt occurred or
4524 * an exception was thrown but not caught. */
4525 if (aborting())
4526 {
4527 if (ret == OK)
4528 clear_tv(rettv);
4529 ret = FAIL;
4530 }
4531 }
4532 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004533 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004534 else
4535 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004536 }
4537
4538 if (alias != NULL)
4539 vim_free(alias);
4540 }
4541
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 *arg = skipwhite(*arg);
4543
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004544 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4545 * expr(expr). */
4546 if (ret == OK)
4547 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548
4549 /*
4550 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4551 */
4552 if (ret == OK && evaluate && end_leader > start_leader)
4553 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004554 int error = FALSE;
4555
4556 val = get_tv_number_chk(rettv, &error);
4557 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004559 clear_tv(rettv);
4560 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004562 else
4563 {
4564 while (end_leader > start_leader)
4565 {
4566 --end_leader;
4567 if (*end_leader == '!')
4568 val = !val;
4569 else if (*end_leader == '-')
4570 val = -val;
4571 }
4572 clear_tv(rettv);
4573 rettv->v_type = VAR_NUMBER;
4574 rettv->vval.v_number = val;
4575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 }
4577
4578 return ret;
4579}
4580
4581/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004582 * Evaluate an "[expr]" or "[expr:expr]" index.
4583 * "*arg" points to the '['.
4584 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4585 */
4586 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004587eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004588 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004589 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004590 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004591 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004592{
4593 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004594 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004595 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004596 long len = -1;
4597 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004598 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004599 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004600
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004601 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004602 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004603 if (verbose)
4604 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004605 return FAIL;
4606 }
4607
Bram Moolenaar8c711452005-01-14 21:53:12 +00004608 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004609 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004610 /*
4611 * dict.name
4612 */
4613 key = *arg + 1;
4614 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4615 ;
4616 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004617 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004618 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004619 }
4620 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004621 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004622 /*
4623 * something[idx]
4624 *
4625 * Get the (first) variable from inside the [].
4626 */
4627 *arg = skipwhite(*arg + 1);
4628 if (**arg == ':')
4629 empty1 = TRUE;
4630 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4631 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004632 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4633 {
4634 /* not a number or string */
4635 clear_tv(&var1);
4636 return FAIL;
4637 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004638
4639 /*
4640 * Get the second variable from inside the [:].
4641 */
4642 if (**arg == ':')
4643 {
4644 range = TRUE;
4645 *arg = skipwhite(*arg + 1);
4646 if (**arg == ']')
4647 empty2 = TRUE;
4648 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4649 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004650 if (!empty1)
4651 clear_tv(&var1);
4652 return FAIL;
4653 }
4654 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4655 {
4656 /* not a number or string */
4657 if (!empty1)
4658 clear_tv(&var1);
4659 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004660 return FAIL;
4661 }
4662 }
4663
4664 /* Check for the ']'. */
4665 if (**arg != ']')
4666 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004667 if (verbose)
4668 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004669 clear_tv(&var1);
4670 if (range)
4671 clear_tv(&var2);
4672 return FAIL;
4673 }
4674 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004675 }
4676
4677 if (evaluate)
4678 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004679 n1 = 0;
4680 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004681 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004682 n1 = get_tv_number(&var1);
4683 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004684 }
4685 if (range)
4686 {
4687 if (empty2)
4688 n2 = -1;
4689 else
4690 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004691 n2 = get_tv_number(&var2);
4692 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004693 }
4694 }
4695
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004696 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004697 {
4698 case VAR_NUMBER:
4699 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004700 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004701 len = (long)STRLEN(s);
4702 if (range)
4703 {
4704 /* The resulting variable is a substring. If the indexes
4705 * are out of range the result is empty. */
4706 if (n1 < 0)
4707 {
4708 n1 = len + n1;
4709 if (n1 < 0)
4710 n1 = 0;
4711 }
4712 if (n2 < 0)
4713 n2 = len + n2;
4714 else if (n2 >= len)
4715 n2 = len;
4716 if (n1 >= len || n2 < 0 || n1 > n2)
4717 s = NULL;
4718 else
4719 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4720 }
4721 else
4722 {
4723 /* The resulting variable is a string of a single
4724 * character. If the index is too big or negative the
4725 * result is empty. */
4726 if (n1 >= len || n1 < 0)
4727 s = NULL;
4728 else
4729 s = vim_strnsave(s + n1, 1);
4730 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004731 clear_tv(rettv);
4732 rettv->v_type = VAR_STRING;
4733 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004734 break;
4735
4736 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004737 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004738 if (n1 < 0)
4739 n1 = len + n1;
4740 if (!empty1 && (n1 < 0 || n1 >= len))
4741 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004742 if (verbose)
4743 EMSGN(_(e_listidx), n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004744 return FAIL;
4745 }
4746 if (range)
4747 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004748 list_T *l;
4749 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004750
4751 if (n2 < 0)
4752 n2 = len + n2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004753 if (!empty2 && (n2 < 0 || n2 >= len || n2 + 1 < n1))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004754 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004755 if (verbose)
4756 EMSGN(_(e_listidx), n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004757 return FAIL;
4758 }
4759 l = list_alloc();
4760 if (l == NULL)
4761 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004762 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004763 n1 <= n2; ++n1)
4764 {
4765 if (list_append_tv(l, &item->li_tv) == FAIL)
4766 {
4767 list_free(l);
4768 return FAIL;
4769 }
4770 item = item->li_next;
4771 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004772 clear_tv(rettv);
4773 rettv->v_type = VAR_LIST;
4774 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004775 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004776 }
4777 else
4778 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004779 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv,
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004780 &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004781 clear_tv(rettv);
4782 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004783 }
4784 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004785
4786 case VAR_DICT:
4787 if (range)
4788 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004789 if (verbose)
4790 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004791 if (len == -1)
4792 clear_tv(&var1);
4793 return FAIL;
4794 }
4795 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004796 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004797
4798 if (len == -1)
4799 {
4800 key = get_tv_string(&var1);
4801 if (*key == NUL)
4802 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004803 if (verbose)
4804 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004805 clear_tv(&var1);
4806 return FAIL;
4807 }
4808 }
4809
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004810 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004811
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004812 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004813 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004814 if (len == -1)
4815 clear_tv(&var1);
4816 if (item == NULL)
4817 return FAIL;
4818
4819 copy_tv(&item->di_tv, &var1);
4820 clear_tv(rettv);
4821 *rettv = var1;
4822 }
4823 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004824 }
4825 }
4826
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004827 return OK;
4828}
4829
4830/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 * Get an option value.
4832 * "arg" points to the '&' or '+' before the option name.
4833 * "arg" is advanced to character after the option name.
4834 * Return OK or FAIL.
4835 */
4836 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004837get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004839 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 int evaluate;
4841{
4842 char_u *option_end;
4843 long numval;
4844 char_u *stringval;
4845 int opt_type;
4846 int c;
4847 int working = (**arg == '+'); /* has("+option") */
4848 int ret = OK;
4849 int opt_flags;
4850
4851 /*
4852 * Isolate the option name and find its value.
4853 */
4854 option_end = find_option_end(arg, &opt_flags);
4855 if (option_end == NULL)
4856 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004857 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 EMSG2(_("E112: Option name missing: %s"), *arg);
4859 return FAIL;
4860 }
4861
4862 if (!evaluate)
4863 {
4864 *arg = option_end;
4865 return OK;
4866 }
4867
4868 c = *option_end;
4869 *option_end = NUL;
4870 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004871 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872
4873 if (opt_type == -3) /* invalid name */
4874 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004875 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 EMSG2(_("E113: Unknown option: %s"), *arg);
4877 ret = FAIL;
4878 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004879 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 {
4881 if (opt_type == -2) /* hidden string option */
4882 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004883 rettv->v_type = VAR_STRING;
4884 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 }
4886 else if (opt_type == -1) /* hidden number option */
4887 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004888 rettv->v_type = VAR_NUMBER;
4889 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 }
4891 else if (opt_type == 1) /* number option */
4892 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004893 rettv->v_type = VAR_NUMBER;
4894 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 }
4896 else /* string option */
4897 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004898 rettv->v_type = VAR_STRING;
4899 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 }
4901 }
4902 else if (working && (opt_type == -2 || opt_type == -1))
4903 ret = FAIL;
4904
4905 *option_end = c; /* put back for error messages */
4906 *arg = option_end;
4907
4908 return ret;
4909}
4910
4911/*
4912 * Allocate a variable for a string constant.
4913 * Return OK or FAIL.
4914 */
4915 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004916get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004918 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 int evaluate;
4920{
4921 char_u *p;
4922 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 int extra = 0;
4924
4925 /*
4926 * Find the end of the string, skipping backslashed characters.
4927 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004928 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 {
4930 if (*p == '\\' && p[1] != NUL)
4931 {
4932 ++p;
4933 /* A "\<x>" form occupies at least 4 characters, and produces up
4934 * to 6 characters: reserve space for 2 extra */
4935 if (*p == '<')
4936 extra += 2;
4937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 }
4939
4940 if (*p != '"')
4941 {
4942 EMSG2(_("E114: Missing quote: %s"), *arg);
4943 return FAIL;
4944 }
4945
4946 /* If only parsing, set *arg and return here */
4947 if (!evaluate)
4948 {
4949 *arg = p + 1;
4950 return OK;
4951 }
4952
4953 /*
4954 * Copy the string into allocated memory, handling backslashed
4955 * characters.
4956 */
4957 name = alloc((unsigned)(p - *arg + extra));
4958 if (name == NULL)
4959 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004960 rettv->v_type = VAR_STRING;
4961 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962
Bram Moolenaar8c711452005-01-14 21:53:12 +00004963 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 {
4965 if (*p == '\\')
4966 {
4967 switch (*++p)
4968 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004969 case 'b': *name++ = BS; ++p; break;
4970 case 'e': *name++ = ESC; ++p; break;
4971 case 'f': *name++ = FF; ++p; break;
4972 case 'n': *name++ = NL; ++p; break;
4973 case 'r': *name++ = CAR; ++p; break;
4974 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975
4976 case 'X': /* hex: "\x1", "\x12" */
4977 case 'x':
4978 case 'u': /* Unicode: "\u0023" */
4979 case 'U':
4980 if (vim_isxdigit(p[1]))
4981 {
4982 int n, nr;
4983 int c = toupper(*p);
4984
4985 if (c == 'X')
4986 n = 2;
4987 else
4988 n = 4;
4989 nr = 0;
4990 while (--n >= 0 && vim_isxdigit(p[1]))
4991 {
4992 ++p;
4993 nr = (nr << 4) + hex2nr(*p);
4994 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004995 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996#ifdef FEAT_MBYTE
4997 /* For "\u" store the number according to
4998 * 'encoding'. */
4999 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005000 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 else
5002#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005003 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 break;
5006
5007 /* octal: "\1", "\12", "\123" */
5008 case '0':
5009 case '1':
5010 case '2':
5011 case '3':
5012 case '4':
5013 case '5':
5014 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005015 case '7': *name = *p++ - '0';
5016 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005018 *name = (*name << 3) + *p++ - '0';
5019 if (*p >= '0' && *p <= '7')
5020 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005022 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 break;
5024
5025 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005026 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 if (extra != 0)
5028 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005029 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 break;
5031 }
5032 /* FALLTHROUGH */
5033
Bram Moolenaar8c711452005-01-14 21:53:12 +00005034 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 break;
5036 }
5037 }
5038 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005039 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005042 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 *arg = p + 1;
5044
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 return OK;
5046}
5047
5048/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005049 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 * Return OK or FAIL.
5051 */
5052 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005053get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005055 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 int evaluate;
5057{
5058 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005059 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005060 int reduce = 0;
5061
5062 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005063 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005064 */
5065 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5066 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005067 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005068 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005069 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005070 break;
5071 ++reduce;
5072 ++p;
5073 }
5074 }
5075
Bram Moolenaar8c711452005-01-14 21:53:12 +00005076 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005077 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005078 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005079 return FAIL;
5080 }
5081
Bram Moolenaar8c711452005-01-14 21:53:12 +00005082 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005083 if (!evaluate)
5084 {
5085 *arg = p + 1;
5086 return OK;
5087 }
5088
5089 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005090 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005091 */
5092 str = alloc((unsigned)((p - *arg) - reduce));
5093 if (str == NULL)
5094 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005095 rettv->v_type = VAR_STRING;
5096 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005097
Bram Moolenaar8c711452005-01-14 21:53:12 +00005098 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005099 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005100 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005101 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005102 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005103 break;
5104 ++p;
5105 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005106 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005107 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005108 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005109 *arg = p + 1;
5110
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005111 return OK;
5112}
5113
5114/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005115 * Allocate a variable for a List and fill it from "*arg".
5116 * Return OK or FAIL.
5117 */
5118 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005119get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005120 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005121 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005122 int evaluate;
5123{
Bram Moolenaar33570922005-01-25 22:26:29 +00005124 list_T *l = NULL;
5125 typval_T tv;
5126 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005127
5128 if (evaluate)
5129 {
5130 l = list_alloc();
5131 if (l == NULL)
5132 return FAIL;
5133 }
5134
5135 *arg = skipwhite(*arg + 1);
5136 while (**arg != ']' && **arg != NUL)
5137 {
5138 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5139 goto failret;
5140 if (evaluate)
5141 {
5142 item = listitem_alloc();
5143 if (item != NULL)
5144 {
5145 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005146 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005147 list_append(l, item);
5148 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005149 else
5150 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005151 }
5152
5153 if (**arg == ']')
5154 break;
5155 if (**arg != ',')
5156 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005157 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005158 goto failret;
5159 }
5160 *arg = skipwhite(*arg + 1);
5161 }
5162
5163 if (**arg != ']')
5164 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005165 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005166failret:
5167 if (evaluate)
5168 list_free(l);
5169 return FAIL;
5170 }
5171
5172 *arg = skipwhite(*arg + 1);
5173 if (evaluate)
5174 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005175 rettv->v_type = VAR_LIST;
5176 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005177 ++l->lv_refcount;
5178 }
5179
5180 return OK;
5181}
5182
5183/*
5184 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005185 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005186 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005187 static list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005188list_alloc()
5189{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005190 list_T *l;
5191
5192 l = (list_T *)alloc_clear(sizeof(list_T));
5193 if (l != NULL)
5194 {
5195 /* Prepend the list to the list of lists for garbage collection. */
5196 if (first_list != NULL)
5197 first_list->lv_used_prev = l;
5198 l->lv_used_prev = NULL;
5199 l->lv_used_next = first_list;
5200 first_list = l;
5201 }
5202 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005203}
5204
5205/*
5206 * Unreference a list: decrement the reference count and free it when it
5207 * becomes zero.
5208 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005209 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005210list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005211 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005212{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005213 if (l != NULL && l->lv_refcount != DEL_REFCOUNT && --l->lv_refcount <= 0)
5214 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005215}
5216
5217/*
5218 * Free a list, including all items it points to.
5219 * Ignores the reference count.
5220 */
5221 static void
5222list_free(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005223 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005224{
Bram Moolenaar33570922005-01-25 22:26:29 +00005225 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005226
Bram Moolenaard9fba312005-06-26 22:34:35 +00005227 /* Avoid that recursive reference to the list frees us again. */
5228 l->lv_refcount = DEL_REFCOUNT;
5229
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005230 /* Remove the list from the list of lists for garbage collection. */
5231 if (l->lv_used_prev == NULL)
5232 first_list = l->lv_used_next;
5233 else
5234 l->lv_used_prev->lv_used_next = l->lv_used_next;
5235 if (l->lv_used_next != NULL)
5236 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5237
Bram Moolenaard9fba312005-06-26 22:34:35 +00005238 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005239 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005240 /* Remove the item before deleting it. */
5241 l->lv_first = item->li_next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005242 listitem_free(item);
5243 }
5244 vim_free(l);
5245}
5246
5247/*
5248 * Allocate a list item.
5249 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005250 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005251listitem_alloc()
5252{
Bram Moolenaar33570922005-01-25 22:26:29 +00005253 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005254}
5255
5256/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005257 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005258 */
5259 static void
5260listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005261 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005262{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005263 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005264 vim_free(item);
5265}
5266
5267/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005268 * Remove a list item from a List and free it. Also clears the value.
5269 */
5270 static void
5271listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005272 list_T *l;
5273 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005274{
5275 list_remove(l, item, item);
5276 listitem_free(item);
5277}
5278
5279/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005280 * Get the number of items in a list.
5281 */
5282 static long
5283list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005284 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005285{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005286 if (l == NULL)
5287 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005288 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005289}
5290
5291/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005292 * Return TRUE when two lists have exactly the same values.
5293 */
5294 static int
5295list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005296 list_T *l1;
5297 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005298 int ic; /* ignore case for strings */
5299{
Bram Moolenaar33570922005-01-25 22:26:29 +00005300 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005301
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005302 if (list_len(l1) != list_len(l2))
5303 return FALSE;
5304
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005305 for (item1 = l1->lv_first, item2 = l2->lv_first;
5306 item1 != NULL && item2 != NULL;
5307 item1 = item1->li_next, item2 = item2->li_next)
5308 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5309 return FALSE;
5310 return item1 == NULL && item2 == NULL;
5311}
5312
5313/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005314 * Return TRUE when two dictionaries have exactly the same key/values.
5315 */
5316 static int
5317dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005318 dict_T *d1;
5319 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005320 int ic; /* ignore case for strings */
5321{
Bram Moolenaar33570922005-01-25 22:26:29 +00005322 hashitem_T *hi;
5323 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005324 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005325
5326 if (dict_len(d1) != dict_len(d2))
5327 return FALSE;
5328
Bram Moolenaar33570922005-01-25 22:26:29 +00005329 todo = d1->dv_hashtab.ht_used;
5330 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005331 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005332 if (!HASHITEM_EMPTY(hi))
5333 {
5334 item2 = dict_find(d2, hi->hi_key, -1);
5335 if (item2 == NULL)
5336 return FALSE;
5337 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5338 return FALSE;
5339 --todo;
5340 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005341 }
5342 return TRUE;
5343}
5344
5345/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005346 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005347 * Compares the items just like "==" would compare them, but strings and
5348 * numbers are different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005349 */
5350 static int
5351tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005352 typval_T *tv1;
5353 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005354 int ic; /* ignore case */
5355{
5356 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005357 char_u *s1, *s2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005358
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005359 if (tv1->v_type != tv2->v_type)
5360 return FALSE;
5361
5362 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005363 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005364 case VAR_LIST:
5365 /* recursive! */
5366 return list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5367
5368 case VAR_DICT:
5369 /* recursive! */
5370 return dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5371
5372 case VAR_FUNC:
5373 return (tv1->vval.v_string != NULL
5374 && tv2->vval.v_string != NULL
5375 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5376
5377 case VAR_NUMBER:
5378 return tv1->vval.v_number == tv2->vval.v_number;
5379
5380 case VAR_STRING:
5381 s1 = get_tv_string_buf(tv1, buf1);
5382 s2 = get_tv_string_buf(tv2, buf2);
5383 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005384 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005385
5386 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005387 return TRUE;
5388}
5389
5390/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005391 * Locate item with index "n" in list "l" and return it.
5392 * A negative index is counted from the end; -1 is the last item.
5393 * Returns NULL when "n" is out of range.
5394 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005395 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005396list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005397 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005398 long n;
5399{
Bram Moolenaar33570922005-01-25 22:26:29 +00005400 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005401 long idx;
5402
5403 if (l == NULL)
5404 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005405
5406 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005407 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005408 n = l->lv_len + n;
5409
5410 /* Check for index out of range. */
5411 if (n < 0 || n >= l->lv_len)
5412 return NULL;
5413
5414 /* When there is a cached index may start search from there. */
5415 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005416 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005417 if (n < l->lv_idx / 2)
5418 {
5419 /* closest to the start of the list */
5420 item = l->lv_first;
5421 idx = 0;
5422 }
5423 else if (n > (l->lv_idx + l->lv_len) / 2)
5424 {
5425 /* closest to the end of the list */
5426 item = l->lv_last;
5427 idx = l->lv_len - 1;
5428 }
5429 else
5430 {
5431 /* closest to the cached index */
5432 item = l->lv_idx_item;
5433 idx = l->lv_idx;
5434 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005435 }
5436 else
5437 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005438 if (n < l->lv_len / 2)
5439 {
5440 /* closest to the start of the list */
5441 item = l->lv_first;
5442 idx = 0;
5443 }
5444 else
5445 {
5446 /* closest to the end of the list */
5447 item = l->lv_last;
5448 idx = l->lv_len - 1;
5449 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005450 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005451
5452 while (n > idx)
5453 {
5454 /* search forward */
5455 item = item->li_next;
5456 ++idx;
5457 }
5458 while (n < idx)
5459 {
5460 /* search backward */
5461 item = item->li_prev;
5462 --idx;
5463 }
5464
5465 /* cache the used index */
5466 l->lv_idx = idx;
5467 l->lv_idx_item = item;
5468
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005469 return item;
5470}
5471
5472/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005473 * Locate "item" list "l" and return its index.
5474 * Returns -1 when "item" is not in the list.
5475 */
5476 static long
5477list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005478 list_T *l;
5479 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005480{
5481 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005482 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005483
5484 if (l == NULL)
5485 return -1;
5486 idx = 0;
5487 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5488 ++idx;
5489 if (li == NULL)
5490 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005491 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005492}
5493
5494/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005495 * Append item "item" to the end of list "l".
5496 */
5497 static void
5498list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005499 list_T *l;
5500 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005501{
5502 if (l->lv_last == NULL)
5503 {
5504 /* empty list */
5505 l->lv_first = item;
5506 l->lv_last = item;
5507 item->li_prev = NULL;
5508 }
5509 else
5510 {
5511 l->lv_last->li_next = item;
5512 item->li_prev = l->lv_last;
5513 l->lv_last = item;
5514 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005515 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005516 item->li_next = NULL;
5517}
5518
5519/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005520 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005521 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005522 */
5523 static int
5524list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005525 list_T *l;
5526 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005527{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005528 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005529
Bram Moolenaar05159a02005-02-26 23:04:13 +00005530 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005531 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005532 copy_tv(tv, &li->li_tv);
5533 list_append(l, li);
5534 return OK;
5535}
5536
5537/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005538 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005539 * Return FAIL when out of memory.
5540 */
5541 int
5542list_append_dict(list, dict)
5543 list_T *list;
5544 dict_T *dict;
5545{
5546 listitem_T *li = listitem_alloc();
5547
5548 if (li == NULL)
5549 return FAIL;
5550 li->li_tv.v_type = VAR_DICT;
5551 li->li_tv.v_lock = 0;
5552 li->li_tv.vval.v_dict = dict;
5553 list_append(list, li);
5554 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005555 return OK;
5556}
5557
5558/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005559 * Make a copy of "str" and append it as an item to list "l".
5560 * Returns FAIL when out of memory.
5561 */
5562 static int
5563list_append_string(l, str)
5564 list_T *l;
5565 char_u *str;
5566{
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;
5574 if ((li->li_tv.vval.v_string = vim_strsave(str)) == NULL)
5575 return FAIL;
5576 return OK;
5577}
5578
5579/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005580 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005581 * If "item" is NULL append at the end.
5582 * Return FAIL when out of memory.
5583 */
5584 static int
5585list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005586 list_T *l;
5587 typval_T *tv;
5588 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005589{
Bram Moolenaar33570922005-01-25 22:26:29 +00005590 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005591
5592 if (ni == NULL)
5593 return FAIL;
5594 copy_tv(tv, &ni->li_tv);
5595 if (item == NULL)
5596 /* Append new item at end of list. */
5597 list_append(l, ni);
5598 else
5599 {
5600 /* Insert new item before existing item. */
5601 ni->li_prev = item->li_prev;
5602 ni->li_next = item;
5603 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005604 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005605 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005606 ++l->lv_idx;
5607 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005608 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005609 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005610 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005611 l->lv_idx_item = NULL;
5612 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005613 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005614 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005615 }
5616 return OK;
5617}
5618
5619/*
5620 * Extend "l1" with "l2".
5621 * If "bef" is NULL append at the end, otherwise insert before this item.
5622 * Returns FAIL when out of memory.
5623 */
5624 static int
5625list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005626 list_T *l1;
5627 list_T *l2;
5628 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005629{
Bram Moolenaar33570922005-01-25 22:26:29 +00005630 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005631
5632 for (item = l2->lv_first; item != NULL; item = item->li_next)
5633 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5634 return FAIL;
5635 return OK;
5636}
5637
5638/*
5639 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5640 * Return FAIL when out of memory.
5641 */
5642 static int
5643list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005644 list_T *l1;
5645 list_T *l2;
5646 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005647{
Bram Moolenaar33570922005-01-25 22:26:29 +00005648 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005649
5650 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005651 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005652 if (l == NULL)
5653 return FAIL;
5654 tv->v_type = VAR_LIST;
5655 tv->vval.v_list = l;
5656
5657 /* append all items from the second list */
5658 return list_extend(l, l2, NULL);
5659}
5660
5661/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005662 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005663 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005664 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005665 * Returns NULL when out of memory.
5666 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005667 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005668list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005669 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005670 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005671 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005672{
Bram Moolenaar33570922005-01-25 22:26:29 +00005673 list_T *copy;
5674 listitem_T *item;
5675 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005676
5677 if (orig == NULL)
5678 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005679
5680 copy = list_alloc();
5681 if (copy != NULL)
5682 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005683 if (copyID != 0)
5684 {
5685 /* Do this before adding the items, because one of the items may
5686 * refer back to this list. */
5687 orig->lv_copyID = copyID;
5688 orig->lv_copylist = copy;
5689 }
5690 for (item = orig->lv_first; item != NULL && !got_int;
5691 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005692 {
5693 ni = listitem_alloc();
5694 if (ni == NULL)
5695 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005696 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005697 {
5698 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5699 {
5700 vim_free(ni);
5701 break;
5702 }
5703 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005704 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005705 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005706 list_append(copy, ni);
5707 }
5708 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005709 if (item != NULL)
5710 {
5711 list_unref(copy);
5712 copy = NULL;
5713 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005714 }
5715
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005716 return copy;
5717}
5718
5719/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005720 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005721 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005722 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005723 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005724list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005725 list_T *l;
5726 listitem_T *item;
5727 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005728{
Bram Moolenaar33570922005-01-25 22:26:29 +00005729 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005730
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005731 /* notify watchers */
5732 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005733 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005734 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005735 list_fix_watch(l, ip);
5736 if (ip == item2)
5737 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005738 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005739
5740 if (item2->li_next == NULL)
5741 l->lv_last = item->li_prev;
5742 else
5743 item2->li_next->li_prev = item->li_prev;
5744 if (item->li_prev == NULL)
5745 l->lv_first = item2->li_next;
5746 else
5747 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005748 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005749}
5750
5751/*
5752 * Return an allocated string with the string representation of a list.
5753 * May return NULL.
5754 */
5755 static char_u *
5756list2string(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005757 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005758{
5759 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005760
5761 if (tv->vval.v_list == NULL)
5762 return NULL;
5763 ga_init2(&ga, (int)sizeof(char), 80);
5764 ga_append(&ga, '[');
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005765 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE) == FAIL)
5766 {
5767 vim_free(ga.ga_data);
5768 return NULL;
5769 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005770 ga_append(&ga, ']');
5771 ga_append(&ga, NUL);
5772 return (char_u *)ga.ga_data;
5773}
5774
5775/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005776 * Join list "l" into a string in "*gap", using separator "sep".
5777 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005778 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005779 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005780 static int
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005781list_join(gap, l, sep, echo)
5782 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00005783 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005784 char_u *sep;
5785 int echo;
5786{
5787 int first = TRUE;
5788 char_u *tofree;
5789 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00005790 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005791 char_u *s;
5792
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005793 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005794 {
5795 if (first)
5796 first = FALSE;
5797 else
5798 ga_concat(gap, sep);
5799
5800 if (echo)
5801 s = echo_string(&item->li_tv, &tofree, numbuf);
5802 else
5803 s = tv2string(&item->li_tv, &tofree, numbuf);
5804 if (s != NULL)
5805 ga_concat(gap, s);
5806 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005807 if (s == NULL)
5808 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005809 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005810 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005811}
5812
5813/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005814 * Garbage collection for lists and dictionaries.
5815 *
5816 * We use reference counts to be able to free most items right away when they
5817 * are no longer used. But for composite items it's possible that it becomes
5818 * unused while the reference count is > 0: When there is a recursive
5819 * reference. Example:
5820 * :let l = [1, 2, 3]
5821 * :let d = {9: l}
5822 * :let l[1] = d
5823 *
5824 * Since this is quite unusual we handle this with garbage collection: every
5825 * once in a while find out which lists and dicts are not referenced from any
5826 * variable.
5827 *
5828 * Here is a good reference text about garbage collection (refers to Python
5829 * but it applies to all reference-counting mechanisms):
5830 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005831 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005832
5833/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005834 * Do garbage collection for lists and dicts.
5835 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005836 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005837 int
5838garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00005839{
5840 dict_T *dd;
5841 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005842 int copyID = ++current_copyID;
5843 buf_T *buf;
5844 win_T *wp;
5845 int i;
5846 funccall_T *fc;
5847 int did_free = FALSE;
5848
5849 /*
5850 * 1. Go through all accessible variables and mark all lists and dicts
5851 * with copyID.
5852 */
5853 /* script-local variables */
5854 for (i = 1; i <= ga_scripts.ga_len; ++i)
5855 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
5856
5857 /* buffer-local variables */
5858 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5859 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
5860
5861 /* window-local variables */
5862 FOR_ALL_WINDOWS(wp)
5863 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
5864
5865 /* global variables */
5866 set_ref_in_ht(&globvarht, copyID);
5867
5868 /* function-local variables */
5869 for (fc = current_funccal; fc != NULL; fc = fc->caller)
5870 {
5871 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
5872 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
5873 }
5874
5875 /*
5876 * 2. Go through the list of dicts and free items without the copyID.
5877 */
5878 for (dd = first_dict; dd != NULL; )
5879 if (dd->dv_copyID != copyID)
5880 {
5881 dict_free(dd);
5882 did_free = TRUE;
5883
5884 /* restart, next dict may also have been freed */
5885 dd = first_dict;
5886 }
5887 else
5888 dd = dd->dv_used_next;
5889
5890 /*
5891 * 3. Go through the list of lists and free items without the copyID.
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00005892 * But don't free a list that has a watcher (used in a for loop), these
5893 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005894 */
5895 for (ll = first_list; ll != NULL; )
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00005896 if (ll->lv_copyID != copyID && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005897 {
5898 list_free(ll);
5899 did_free = TRUE;
5900
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00005901 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005902 ll = first_list;
5903 }
5904 else
5905 ll = ll->lv_used_next;
5906
5907 return did_free;
5908}
5909
5910/*
5911 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
5912 */
5913 static void
5914set_ref_in_ht(ht, copyID)
5915 hashtab_T *ht;
5916 int copyID;
5917{
5918 int todo;
5919 hashitem_T *hi;
5920
5921 todo = ht->ht_used;
5922 for (hi = ht->ht_array; todo > 0; ++hi)
5923 if (!HASHITEM_EMPTY(hi))
5924 {
5925 --todo;
5926 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
5927 }
5928}
5929
5930/*
5931 * Mark all lists and dicts referenced through list "l" with "copyID".
5932 */
5933 static void
5934set_ref_in_list(l, copyID)
5935 list_T *l;
5936 int copyID;
5937{
5938 listitem_T *li;
5939
5940 for (li = l->lv_first; li != NULL; li = li->li_next)
5941 set_ref_in_item(&li->li_tv, copyID);
5942}
5943
5944/*
5945 * Mark all lists and dicts referenced through typval "tv" with "copyID".
5946 */
5947 static void
5948set_ref_in_item(tv, copyID)
5949 typval_T *tv;
5950 int copyID;
5951{
5952 dict_T *dd;
5953 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005954
5955 switch (tv->v_type)
5956 {
5957 case VAR_DICT:
5958 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005959 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005960 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005961 /* Didn't see this dict yet. */
5962 dd->dv_copyID = copyID;
5963 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00005964 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005965 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005966
5967 case VAR_LIST:
5968 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005969 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005970 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005971 /* Didn't see this list yet. */
5972 ll->lv_copyID = copyID;
5973 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00005974 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005975 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005976 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005977 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005978}
5979
5980/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005981 * Allocate an empty header for a dictionary.
5982 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005983 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00005984dict_alloc()
5985{
Bram Moolenaar33570922005-01-25 22:26:29 +00005986 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005987
Bram Moolenaar33570922005-01-25 22:26:29 +00005988 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005989 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005990 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005991 /* Add the list to the hashtable for garbage collection. */
5992 if (first_dict != NULL)
5993 first_dict->dv_used_prev = d;
5994 d->dv_used_next = first_dict;
5995 d->dv_used_prev = NULL;
5996
Bram Moolenaar33570922005-01-25 22:26:29 +00005997 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005998 d->dv_lock = 0;
5999 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006000 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006001 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006002 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006003}
6004
6005/*
6006 * Unreference a Dictionary: decrement the reference count and free it when it
6007 * becomes zero.
6008 */
6009 static void
6010dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006011 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006012{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006013 if (d != NULL && d->dv_refcount != DEL_REFCOUNT && --d->dv_refcount <= 0)
6014 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006015}
6016
6017/*
6018 * Free a Dictionary, including all items it contains.
6019 * Ignores the reference count.
6020 */
6021 static void
6022dict_free(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006023 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006024{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006025 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006026 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006027 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006028
Bram Moolenaard9fba312005-06-26 22:34:35 +00006029 /* Avoid that recursive reference to the dict frees us again. */
6030 d->dv_refcount = DEL_REFCOUNT;
6031
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006032 /* Remove the dict from the list of dicts for garbage collection. */
6033 if (d->dv_used_prev == NULL)
6034 first_dict = d->dv_used_next;
6035 else
6036 d->dv_used_prev->dv_used_next = d->dv_used_next;
6037 if (d->dv_used_next != NULL)
6038 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6039
6040 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006041 hash_lock(&d->dv_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +00006042 todo = d->dv_hashtab.ht_used;
6043 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006044 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006045 if (!HASHITEM_EMPTY(hi))
6046 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006047 /* Remove the item before deleting it, just in case there is
6048 * something recursive causing trouble. */
6049 di = HI2DI(hi);
6050 hash_remove(&d->dv_hashtab, hi);
6051 dictitem_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006052 --todo;
6053 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006054 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006055 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006056 vim_free(d);
6057}
6058
6059/*
6060 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006061 * The "key" is copied to the new item.
6062 * Note that the value of the item "di_tv" still needs to be initialized!
6063 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006064 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006065 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006066dictitem_alloc(key)
6067 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006068{
Bram Moolenaar33570922005-01-25 22:26:29 +00006069 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006070
Bram Moolenaar33570922005-01-25 22:26:29 +00006071 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(key));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006072 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006073 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006074 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006075 di->di_flags = 0;
6076 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006077 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006078}
6079
6080/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006081 * Make a copy of a Dictionary item.
6082 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006083 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006084dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006085 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006086{
Bram Moolenaar33570922005-01-25 22:26:29 +00006087 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006088
Bram Moolenaar33570922005-01-25 22:26:29 +00006089 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(org->di_key));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006090 if (di != NULL)
6091 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006092 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006093 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006094 copy_tv(&org->di_tv, &di->di_tv);
6095 }
6096 return di;
6097}
6098
6099/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006100 * Remove item "item" from Dictionary "dict" and free it.
6101 */
6102 static void
6103dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006104 dict_T *dict;
6105 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006106{
Bram Moolenaar33570922005-01-25 22:26:29 +00006107 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006108
Bram Moolenaar33570922005-01-25 22:26:29 +00006109 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006110 if (HASHITEM_EMPTY(hi))
6111 EMSG2(_(e_intern2), "dictitem_remove()");
6112 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006113 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006114 dictitem_free(item);
6115}
6116
6117/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006118 * Free a dict item. Also clears the value.
6119 */
6120 static void
6121dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006122 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006123{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006124 clear_tv(&item->di_tv);
6125 vim_free(item);
6126}
6127
6128/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006129 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6130 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006131 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006132 * Returns NULL when out of memory.
6133 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006134 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006135dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006136 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006137 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006138 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006139{
Bram Moolenaar33570922005-01-25 22:26:29 +00006140 dict_T *copy;
6141 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006142 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006143 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006144
6145 if (orig == NULL)
6146 return NULL;
6147
6148 copy = dict_alloc();
6149 if (copy != NULL)
6150 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006151 if (copyID != 0)
6152 {
6153 orig->dv_copyID = copyID;
6154 orig->dv_copydict = copy;
6155 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006156 todo = orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006157 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006158 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006159 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006160 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006161 --todo;
6162
6163 di = dictitem_alloc(hi->hi_key);
6164 if (di == NULL)
6165 break;
6166 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006167 {
6168 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6169 copyID) == FAIL)
6170 {
6171 vim_free(di);
6172 break;
6173 }
6174 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006175 else
6176 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6177 if (dict_add(copy, di) == FAIL)
6178 {
6179 dictitem_free(di);
6180 break;
6181 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006182 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006183 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006184
Bram Moolenaare9a41262005-01-15 22:18:47 +00006185 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006186 if (todo > 0)
6187 {
6188 dict_unref(copy);
6189 copy = NULL;
6190 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006191 }
6192
6193 return copy;
6194}
6195
6196/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006197 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006198 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006199 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006200 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006201dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006202 dict_T *d;
6203 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006204{
Bram Moolenaar33570922005-01-25 22:26:29 +00006205 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006206}
6207
Bram Moolenaar8c711452005-01-14 21:53:12 +00006208/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006209 * Add a number or string entry to dictionary "d".
6210 * When "str" is NULL use number "nr", otherwise use "str".
6211 * Returns FAIL when out of memory and when key already exists.
6212 */
6213 int
6214dict_add_nr_str(d, key, nr, str)
6215 dict_T *d;
6216 char *key;
6217 long nr;
6218 char_u *str;
6219{
6220 dictitem_T *item;
6221
6222 item = dictitem_alloc((char_u *)key);
6223 if (item == NULL)
6224 return FAIL;
6225 item->di_tv.v_lock = 0;
6226 if (str == NULL)
6227 {
6228 item->di_tv.v_type = VAR_NUMBER;
6229 item->di_tv.vval.v_number = nr;
6230 }
6231 else
6232 {
6233 item->di_tv.v_type = VAR_STRING;
6234 item->di_tv.vval.v_string = vim_strsave(str);
6235 }
6236 if (dict_add(d, item) == FAIL)
6237 {
6238 dictitem_free(item);
6239 return FAIL;
6240 }
6241 return OK;
6242}
6243
6244/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006245 * Get the number of items in a Dictionary.
6246 */
6247 static long
6248dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006249 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006250{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006251 if (d == NULL)
6252 return 0L;
Bram Moolenaar33570922005-01-25 22:26:29 +00006253 return d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006254}
6255
6256/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006257 * Find item "key[len]" in Dictionary "d".
6258 * If "len" is negative use strlen(key).
6259 * Returns NULL when not found.
6260 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006261 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006262dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006263 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006264 char_u *key;
6265 int len;
6266{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006267#define AKEYLEN 200
6268 char_u buf[AKEYLEN];
6269 char_u *akey;
6270 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006271 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006272
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006273 if (len < 0)
6274 akey = key;
6275 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006276 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006277 tofree = akey = vim_strnsave(key, len);
6278 if (akey == NULL)
6279 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006280 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006281 else
6282 {
6283 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006284 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006285 akey = buf;
6286 }
6287
Bram Moolenaar33570922005-01-25 22:26:29 +00006288 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006289 vim_free(tofree);
6290 if (HASHITEM_EMPTY(hi))
6291 return NULL;
6292 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006293}
6294
6295/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006296 * Get a string item from a dictionary in allocated memory.
6297 * Returns NULL if the entry doesn't exist or out of memory.
6298 */
6299 char_u *
6300get_dict_string(d, key)
6301 dict_T *d;
6302 char_u *key;
6303{
6304 dictitem_T *di;
6305
6306 di = dict_find(d, key, -1);
6307 if (di == NULL)
6308 return NULL;
6309 return vim_strsave(get_tv_string(&di->di_tv));
6310}
6311
6312/*
6313 * Get a number item from a dictionary.
6314 * Returns 0 if the entry doesn't exist or out of memory.
6315 */
6316 long
6317get_dict_number(d, key)
6318 dict_T *d;
6319 char_u *key;
6320{
6321 dictitem_T *di;
6322
6323 di = dict_find(d, key, -1);
6324 if (di == NULL)
6325 return 0;
6326 return get_tv_number(&di->di_tv);
6327}
6328
6329/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006330 * Return an allocated string with the string representation of a Dictionary.
6331 * May return NULL.
6332 */
6333 static char_u *
6334dict2string(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006335 typval_T *tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006336{
6337 garray_T ga;
6338 int first = TRUE;
6339 char_u *tofree;
6340 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006341 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006342 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006343 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006344 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006345
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006346 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006347 return NULL;
6348 ga_init2(&ga, (int)sizeof(char), 80);
6349 ga_append(&ga, '{');
6350
Bram Moolenaar33570922005-01-25 22:26:29 +00006351 todo = d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006352 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006353 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006354 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006355 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006356 --todo;
6357
6358 if (first)
6359 first = FALSE;
6360 else
6361 ga_concat(&ga, (char_u *)", ");
6362
6363 tofree = string_quote(hi->hi_key, FALSE);
6364 if (tofree != NULL)
6365 {
6366 ga_concat(&ga, tofree);
6367 vim_free(tofree);
6368 }
6369 ga_concat(&ga, (char_u *)": ");
6370 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf);
6371 if (s != NULL)
6372 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006373 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006374 if (s == NULL)
6375 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006376 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006377 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006378 if (todo > 0)
6379 {
6380 vim_free(ga.ga_data);
6381 return NULL;
6382 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006383
6384 ga_append(&ga, '}');
6385 ga_append(&ga, NUL);
6386 return (char_u *)ga.ga_data;
6387}
6388
6389/*
6390 * Allocate a variable for a Dictionary and fill it from "*arg".
6391 * Return OK or FAIL. Returns NOTDONE for {expr}.
6392 */
6393 static int
6394get_dict_tv(arg, rettv, evaluate)
6395 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006396 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006397 int evaluate;
6398{
Bram Moolenaar33570922005-01-25 22:26:29 +00006399 dict_T *d = NULL;
6400 typval_T tvkey;
6401 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006402 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00006403 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006404 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006405 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00006406
6407 /*
6408 * First check if it's not a curly-braces thing: {expr}.
6409 * Must do this without evaluating, otherwise a function may be called
6410 * twice. Unfortunately this means we need to call eval1() twice for the
6411 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006412 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006413 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006414 if (*start != '}')
6415 {
6416 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
6417 return FAIL;
6418 if (*start == '}')
6419 return NOTDONE;
6420 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006421
6422 if (evaluate)
6423 {
6424 d = dict_alloc();
6425 if (d == NULL)
6426 return FAIL;
6427 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006428 tvkey.v_type = VAR_UNKNOWN;
6429 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006430
6431 *arg = skipwhite(*arg + 1);
6432 while (**arg != '}' && **arg != NUL)
6433 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006434 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006435 goto failret;
6436 if (**arg != ':')
6437 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006438 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006439 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006440 goto failret;
6441 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006442 key = get_tv_string_buf_chk(&tvkey, buf);
6443 if (key == NULL || *key == NUL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006444 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006445 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6446 if (key != NULL)
6447 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006448 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006449 goto failret;
6450 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006451
6452 *arg = skipwhite(*arg + 1);
6453 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6454 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006455 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006456 goto failret;
6457 }
6458 if (evaluate)
6459 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006460 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006461 if (item != NULL)
6462 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00006463 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006464 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006465 clear_tv(&tv);
6466 goto failret;
6467 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006468 item = dictitem_alloc(key);
6469 clear_tv(&tvkey);
6470 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006471 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006472 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006473 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006474 if (dict_add(d, item) == FAIL)
6475 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006476 }
6477 }
6478
6479 if (**arg == '}')
6480 break;
6481 if (**arg != ',')
6482 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006483 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006484 goto failret;
6485 }
6486 *arg = skipwhite(*arg + 1);
6487 }
6488
6489 if (**arg != '}')
6490 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006491 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006492failret:
6493 if (evaluate)
6494 dict_free(d);
6495 return FAIL;
6496 }
6497
6498 *arg = skipwhite(*arg + 1);
6499 if (evaluate)
6500 {
6501 rettv->v_type = VAR_DICT;
6502 rettv->vval.v_dict = d;
6503 ++d->dv_refcount;
6504 }
6505
6506 return OK;
6507}
6508
Bram Moolenaar8c711452005-01-14 21:53:12 +00006509/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006510 * Return a string with the string representation of a variable.
6511 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006512 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006513 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006514 * May return NULL;
6515 */
6516 static char_u *
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006517echo_string(tv, tofree, numbuf)
Bram Moolenaar33570922005-01-25 22:26:29 +00006518 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006519 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006520 char_u *numbuf;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006521{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006522 static int recurse = 0;
6523 char_u *r = NULL;
6524
Bram Moolenaar33570922005-01-25 22:26:29 +00006525 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006526 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006527 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006528 *tofree = NULL;
6529 return NULL;
6530 }
6531 ++recurse;
6532
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006533 switch (tv->v_type)
6534 {
6535 case VAR_FUNC:
6536 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006537 r = tv->vval.v_string;
6538 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006539 case VAR_LIST:
6540 *tofree = list2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006541 r = *tofree;
6542 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006543 case VAR_DICT:
6544 *tofree = dict2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006545 r = *tofree;
6546 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006547 case VAR_STRING:
6548 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006549 *tofree = NULL;
6550 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006551 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006552 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006553 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006554 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006555 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006556
6557 --recurse;
6558 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006559}
6560
6561/*
6562 * Return a string with the string representation of a variable.
6563 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6564 * "numbuf" is used for a number.
6565 * Puts quotes around strings, so that they can be parsed back by eval().
6566 * May return NULL;
6567 */
6568 static char_u *
6569tv2string(tv, tofree, numbuf)
Bram Moolenaar33570922005-01-25 22:26:29 +00006570 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006571 char_u **tofree;
6572 char_u *numbuf;
6573{
6574 switch (tv->v_type)
6575 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006576 case VAR_FUNC:
6577 *tofree = string_quote(tv->vval.v_string, TRUE);
6578 return *tofree;
6579 case VAR_STRING:
6580 *tofree = string_quote(tv->vval.v_string, FALSE);
6581 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006582 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006583 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006584 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006585 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006586 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006587 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006588 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006589 return echo_string(tv, tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006590}
6591
6592/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006593 * Return string "str" in ' quotes, doubling ' characters.
6594 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006595 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006596 */
6597 static char_u *
6598string_quote(str, function)
6599 char_u *str;
6600 int function;
6601{
Bram Moolenaar33570922005-01-25 22:26:29 +00006602 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006603 char_u *p, *r, *s;
6604
Bram Moolenaar33570922005-01-25 22:26:29 +00006605 len = (function ? 13 : 3);
6606 if (str != NULL)
6607 {
6608 len += STRLEN(str);
6609 for (p = str; *p != NUL; mb_ptr_adv(p))
6610 if (*p == '\'')
6611 ++len;
6612 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006613 s = r = alloc(len);
6614 if (r != NULL)
6615 {
6616 if (function)
6617 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006618 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006619 r += 10;
6620 }
6621 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006622 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006623 if (str != NULL)
6624 for (p = str; *p != NUL; )
6625 {
6626 if (*p == '\'')
6627 *r++ = '\'';
6628 MB_COPY_CHAR(p, r);
6629 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006630 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006631 if (function)
6632 *r++ = ')';
6633 *r++ = NUL;
6634 }
6635 return s;
6636}
6637
6638/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639 * Get the value of an environment variable.
6640 * "arg" is pointing to the '$'. It is advanced to after the name.
6641 * If the environment variable was not set, silently assume it is empty.
6642 * Always return OK.
6643 */
6644 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006645get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006647 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 int evaluate;
6649{
6650 char_u *string = NULL;
6651 int len;
6652 int cc;
6653 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006654 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655
6656 ++*arg;
6657 name = *arg;
6658 len = get_env_len(arg);
6659 if (evaluate)
6660 {
6661 if (len != 0)
6662 {
6663 cc = name[len];
6664 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006665 /* first try vim_getenv(), fast for normal environment vars */
6666 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006668 {
6669 if (!mustfree)
6670 string = vim_strsave(string);
6671 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 else
6673 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00006674 if (mustfree)
6675 vim_free(string);
6676
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677 /* next try expanding things like $VIM and ${HOME} */
6678 string = expand_env_save(name - 1);
6679 if (string != NULL && *string == '$')
6680 {
6681 vim_free(string);
6682 string = NULL;
6683 }
6684 }
6685 name[len] = cc;
6686 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006687 rettv->v_type = VAR_STRING;
6688 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689 }
6690
6691 return OK;
6692}
6693
6694/*
6695 * Array with names and number of arguments of all internal functions
6696 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
6697 */
6698static struct fst
6699{
6700 char *f_name; /* function name */
6701 char f_min_argc; /* minimal number of arguments */
6702 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00006703 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006704 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705} functions[] =
6706{
Bram Moolenaar0d660222005-01-07 21:51:51 +00006707 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708 {"append", 2, 2, f_append},
6709 {"argc", 0, 0, f_argc},
6710 {"argidx", 0, 0, f_argidx},
6711 {"argv", 1, 1, f_argv},
6712 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006713 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714 {"bufexists", 1, 1, f_bufexists},
6715 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
6716 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
6717 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
6718 {"buflisted", 1, 1, f_buflisted},
6719 {"bufloaded", 1, 1, f_bufloaded},
6720 {"bufname", 1, 1, f_bufname},
6721 {"bufnr", 1, 1, f_bufnr},
6722 {"bufwinnr", 1, 1, f_bufwinnr},
6723 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006724 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006725 {"call", 2, 3, f_call},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726 {"char2nr", 1, 1, f_char2nr},
6727 {"cindent", 1, 1, f_cindent},
6728 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00006729#if defined(FEAT_INS_EXPAND)
6730 {"complete_add", 1, 1, f_complete_add},
6731 {"complete_check", 0, 0, f_complete_check},
6732#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006734 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006735 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736 {"cscope_connection",0,3, f_cscope_connection},
6737 {"cursor", 2, 2, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006738 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 {"delete", 1, 1, f_delete},
6740 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00006741 {"diff_filler", 1, 1, f_diff_filler},
6742 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006743 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006745 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746 {"eventhandler", 0, 0, f_eventhandler},
6747 {"executable", 1, 1, f_executable},
6748 {"exists", 1, 1, f_exists},
6749 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006750 {"extend", 2, 3, f_extend},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
6752 {"filereadable", 1, 1, f_filereadable},
6753 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006754 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006755 {"finddir", 1, 3, f_finddir},
6756 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757 {"fnamemodify", 2, 2, f_fnamemodify},
6758 {"foldclosed", 1, 1, f_foldclosed},
6759 {"foldclosedend", 1, 1, f_foldclosedend},
6760 {"foldlevel", 1, 1, f_foldlevel},
6761 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006762 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006764 {"function", 1, 1, f_function},
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006765 {"garbagecollect", 0, 0, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006766 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00006767 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768 {"getbufvar", 2, 2, f_getbufvar},
6769 {"getchar", 0, 1, f_getchar},
6770 {"getcharmod", 0, 0, f_getcharmod},
6771 {"getcmdline", 0, 0, f_getcmdline},
6772 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006773 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006775 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006776 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777 {"getfsize", 1, 1, f_getfsize},
6778 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006779 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006780 {"getline", 1, 2, f_getline},
Bram Moolenaar2641f772005-03-25 21:58:17 +00006781 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006782 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783 {"getregtype", 0, 1, f_getregtype},
6784 {"getwinposx", 0, 0, f_getwinposx},
6785 {"getwinposy", 0, 0, f_getwinposy},
6786 {"getwinvar", 2, 2, f_getwinvar},
6787 {"glob", 1, 1, f_glob},
6788 {"globpath", 2, 2, f_globpath},
6789 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006790 {"has_key", 2, 2, f_has_key},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 {"hasmapto", 1, 2, f_hasmapto},
6792 {"highlightID", 1, 1, f_hlID}, /* obsolete */
6793 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
6794 {"histadd", 2, 2, f_histadd},
6795 {"histdel", 1, 2, f_histdel},
6796 {"histget", 1, 2, f_histget},
6797 {"histnr", 1, 1, f_histnr},
6798 {"hlID", 1, 1, f_hlID},
6799 {"hlexists", 1, 1, f_hlexists},
6800 {"hostname", 0, 0, f_hostname},
6801 {"iconv", 3, 3, f_iconv},
6802 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006803 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006804 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00006806 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 {"inputrestore", 0, 0, f_inputrestore},
6808 {"inputsave", 0, 0, f_inputsave},
6809 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006810 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006812 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006813 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006814 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006815 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006817 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818 {"libcall", 3, 3, f_libcall},
6819 {"libcallnr", 3, 3, f_libcallnr},
6820 {"line", 1, 1, f_line},
6821 {"line2byte", 1, 1, f_line2byte},
6822 {"lispindent", 1, 1, f_lispindent},
6823 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006824 {"map", 2, 2, f_map},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825 {"maparg", 1, 2, f_maparg},
6826 {"mapcheck", 1, 2, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006827 {"match", 2, 4, f_match},
6828 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006829 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006830 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006831 {"max", 1, 1, f_max},
6832 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006833#ifdef vim_mkdir
6834 {"mkdir", 1, 3, f_mkdir},
6835#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 {"mode", 0, 0, f_mode},
6837 {"nextnonblank", 1, 1, f_nextnonblank},
6838 {"nr2char", 1, 1, f_nr2char},
6839 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006840 {"printf", 2, 19, f_printf},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006841 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006842 {"readfile", 1, 3, f_readfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843 {"remote_expr", 2, 3, f_remote_expr},
6844 {"remote_foreground", 1, 1, f_remote_foreground},
6845 {"remote_peek", 1, 2, f_remote_peek},
6846 {"remote_read", 1, 1, f_remote_read},
6847 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006848 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006850 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006852 {"reverse", 1, 1, f_reverse},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853 {"search", 1, 2, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00006854 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855 {"searchpair", 3, 5, f_searchpair},
6856 {"server2client", 2, 2, f_server2client},
6857 {"serverlist", 0, 0, f_serverlist},
6858 {"setbufvar", 3, 3, f_setbufvar},
6859 {"setcmdpos", 1, 1, f_setcmdpos},
6860 {"setline", 2, 2, f_setline},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00006861 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862 {"setreg", 2, 3, f_setreg},
6863 {"setwinvar", 3, 3, f_setwinvar},
6864 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006865 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006866 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006867 {"spellbadword", 0, 0, f_spellbadword},
6868 {"spellsuggest", 1, 2, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006869 {"split", 1, 3, f_split},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870#ifdef HAVE_STRFTIME
6871 {"strftime", 1, 2, f_strftime},
6872#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00006873 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006874 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875 {"strlen", 1, 1, f_strlen},
6876 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00006877 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878 {"strtrans", 1, 1, f_strtrans},
6879 {"submatch", 1, 1, f_submatch},
6880 {"substitute", 4, 4, f_substitute},
6881 {"synID", 3, 3, f_synID},
6882 {"synIDattr", 2, 3, f_synIDattr},
6883 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006884 {"system", 1, 2, f_system},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006885 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006886 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00006888 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 {"tolower", 1, 1, f_tolower},
6890 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00006891 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006893 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894 {"virtcol", 1, 1, f_virtcol},
6895 {"visualmode", 0, 1, f_visualmode},
6896 {"winbufnr", 1, 1, f_winbufnr},
6897 {"wincol", 0, 0, f_wincol},
6898 {"winheight", 1, 1, f_winheight},
6899 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006900 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 {"winrestcmd", 0, 0, f_winrestcmd},
6902 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006903 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904};
6905
6906#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6907
6908/*
6909 * Function given to ExpandGeneric() to obtain the list of internal
6910 * or user defined function names.
6911 */
6912 char_u *
6913get_function_name(xp, idx)
6914 expand_T *xp;
6915 int idx;
6916{
6917 static int intidx = -1;
6918 char_u *name;
6919
6920 if (idx == 0)
6921 intidx = -1;
6922 if (intidx < 0)
6923 {
6924 name = get_user_func_name(xp, idx);
6925 if (name != NULL)
6926 return name;
6927 }
6928 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
6929 {
6930 STRCPY(IObuff, functions[intidx].f_name);
6931 STRCAT(IObuff, "(");
6932 if (functions[intidx].f_max_argc == 0)
6933 STRCAT(IObuff, ")");
6934 return IObuff;
6935 }
6936
6937 return NULL;
6938}
6939
6940/*
6941 * Function given to ExpandGeneric() to obtain the list of internal or
6942 * user defined variable or function names.
6943 */
6944/*ARGSUSED*/
6945 char_u *
6946get_expr_name(xp, idx)
6947 expand_T *xp;
6948 int idx;
6949{
6950 static int intidx = -1;
6951 char_u *name;
6952
6953 if (idx == 0)
6954 intidx = -1;
6955 if (intidx < 0)
6956 {
6957 name = get_function_name(xp, idx);
6958 if (name != NULL)
6959 return name;
6960 }
6961 return get_user_var_name(xp, ++intidx);
6962}
6963
6964#endif /* FEAT_CMDL_COMPL */
6965
6966/*
6967 * Find internal function in table above.
6968 * Return index, or -1 if not found
6969 */
6970 static int
6971find_internal_func(name)
6972 char_u *name; /* name of the function */
6973{
6974 int first = 0;
6975 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
6976 int cmp;
6977 int x;
6978
6979 /*
6980 * Find the function name in the table. Binary search.
6981 */
6982 while (first <= last)
6983 {
6984 x = first + ((unsigned)(last - first) >> 1);
6985 cmp = STRCMP(name, functions[x].f_name);
6986 if (cmp < 0)
6987 last = x - 1;
6988 else if (cmp > 0)
6989 first = x + 1;
6990 else
6991 return x;
6992 }
6993 return -1;
6994}
6995
6996/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006997 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
6998 * name it contains, otherwise return "name".
6999 */
7000 static char_u *
7001deref_func_name(name, lenp)
7002 char_u *name;
7003 int *lenp;
7004{
Bram Moolenaar33570922005-01-25 22:26:29 +00007005 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007006 int cc;
7007
7008 cc = name[*lenp];
7009 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007010 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007011 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007012 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007013 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007014 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007015 {
7016 *lenp = 0;
7017 return (char_u *)""; /* just in case */
7018 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007019 *lenp = STRLEN(v->di_tv.vval.v_string);
7020 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007021 }
7022
7023 return name;
7024}
7025
7026/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 * Allocate a variable for the result of a function.
7028 * Return OK or FAIL.
7029 */
7030 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007031get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7032 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 char_u *name; /* name of the function */
7034 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007035 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036 char_u **arg; /* argument, pointing to the '(' */
7037 linenr_T firstline; /* first line of range */
7038 linenr_T lastline; /* last line of range */
7039 int *doesrange; /* return: function handled range */
7040 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007041 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042{
7043 char_u *argp;
7044 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00007045 typval_T argvars[MAX_FUNC_ARGS]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 int argcount = 0; /* number of arguments found */
7047
7048 /*
7049 * Get the arguments.
7050 */
7051 argp = *arg;
7052 while (argcount < MAX_FUNC_ARGS)
7053 {
7054 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7055 if (*argp == ')' || *argp == ',' || *argp == NUL)
7056 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7058 {
7059 ret = FAIL;
7060 break;
7061 }
7062 ++argcount;
7063 if (*argp != ',')
7064 break;
7065 }
7066 if (*argp == ')')
7067 ++argp;
7068 else
7069 ret = FAIL;
7070
7071 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007072 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007073 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007075 {
7076 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007077 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007078 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007079 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081
7082 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007083 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084
7085 *arg = skipwhite(argp);
7086 return ret;
7087}
7088
7089
7090/*
7091 * Call a function with its resolved parameters
7092 * Return OK or FAIL.
7093 */
7094 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007095call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007096 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097 char_u *name; /* name of the function */
7098 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007099 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 int argcount; /* number of "argvars" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007101 typval_T *argvars; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102 linenr_T firstline; /* first line of range */
7103 linenr_T lastline; /* last line of range */
7104 int *doesrange; /* return: function handled range */
7105 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007106 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107{
7108 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109#define ERROR_UNKNOWN 0
7110#define ERROR_TOOMANY 1
7111#define ERROR_TOOFEW 2
7112#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007113#define ERROR_DICT 4
7114#define ERROR_NONE 5
7115#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116 int error = ERROR_NONE;
7117 int i;
7118 int llen;
7119 ufunc_T *fp;
7120 int cc;
7121#define FLEN_FIXED 40
7122 char_u fname_buf[FLEN_FIXED + 1];
7123 char_u *fname;
7124
7125 /*
7126 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7127 * Change <SNR>123_name() to K_SNR 123_name().
7128 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7129 */
7130 cc = name[len];
7131 name[len] = NUL;
7132 llen = eval_fname_script(name);
7133 if (llen > 0)
7134 {
7135 fname_buf[0] = K_SPECIAL;
7136 fname_buf[1] = KS_EXTRA;
7137 fname_buf[2] = (int)KE_SNR;
7138 i = 3;
7139 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7140 {
7141 if (current_SID <= 0)
7142 error = ERROR_SCRIPT;
7143 else
7144 {
7145 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7146 i = (int)STRLEN(fname_buf);
7147 }
7148 }
7149 if (i + STRLEN(name + llen) < FLEN_FIXED)
7150 {
7151 STRCPY(fname_buf + i, name + llen);
7152 fname = fname_buf;
7153 }
7154 else
7155 {
7156 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7157 if (fname == NULL)
7158 error = ERROR_OTHER;
7159 else
7160 {
7161 mch_memmove(fname, fname_buf, (size_t)i);
7162 STRCPY(fname + i, name + llen);
7163 }
7164 }
7165 }
7166 else
7167 fname = name;
7168
7169 *doesrange = FALSE;
7170
7171
7172 /* execute the function if no errors detected and executing */
7173 if (evaluate && error == ERROR_NONE)
7174 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007175 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 error = ERROR_UNKNOWN;
7177
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007178 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 {
7180 /*
7181 * User defined function.
7182 */
7183 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007184
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007186 /* Trigger FuncUndefined event, may load the function. */
7187 if (fp == NULL
7188 && apply_autocmds(EVENT_FUNCUNDEFINED,
7189 fname, fname, TRUE, NULL)
7190 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007192 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193 fp = find_func(fname);
7194 }
7195#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007196 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007197 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007198 {
7199 /* loaded a package, search for the function again */
7200 fp = find_func(fname);
7201 }
7202
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203 if (fp != NULL)
7204 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007205 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007207 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007209 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007211 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007212 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 else
7214 {
7215 /*
7216 * Call the user function.
7217 * Save and restore search patterns, script variables and
7218 * redo buffer.
7219 */
7220 save_search_patterns();
7221 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007222 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007223 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007224 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007225 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7226 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7227 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007228 /* Function was unreferenced while being used, free it
7229 * now. */
7230 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231 restoreRedobuff();
7232 restore_search_patterns();
7233 error = ERROR_NONE;
7234 }
7235 }
7236 }
7237 else
7238 {
7239 /*
7240 * Find the function name in the table, call its implementation.
7241 */
7242 i = find_internal_func(fname);
7243 if (i >= 0)
7244 {
7245 if (argcount < functions[i].f_min_argc)
7246 error = ERROR_TOOFEW;
7247 else if (argcount > functions[i].f_max_argc)
7248 error = ERROR_TOOMANY;
7249 else
7250 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007251 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007252 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 error = ERROR_NONE;
7254 }
7255 }
7256 }
7257 /*
7258 * The function call (or "FuncUndefined" autocommand sequence) might
7259 * have been aborted by an error, an interrupt, or an explicitly thrown
7260 * exception that has not been caught so far. This situation can be
7261 * tested for by calling aborting(). For an error in an internal
7262 * function or for the "E132" error in call_user_func(), however, the
7263 * throw point at which the "force_abort" flag (temporarily reset by
7264 * emsg()) is normally updated has not been reached yet. We need to
7265 * update that flag first to make aborting() reliable.
7266 */
7267 update_force_abort();
7268 }
7269 if (error == ERROR_NONE)
7270 ret = OK;
7271
7272 /*
7273 * Report an error unless the argument evaluation or function call has been
7274 * cancelled due to an aborting error, an interrupt, or an exception.
7275 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007276 if (!aborting())
7277 {
7278 switch (error)
7279 {
7280 case ERROR_UNKNOWN:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007281 emsg_funcname("E117: Unknown function: %s", name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007282 break;
7283 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007284 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007285 break;
7286 case ERROR_TOOFEW:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007287 emsg_funcname("E119: Not enough arguments for function: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007288 name);
7289 break;
7290 case ERROR_SCRIPT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007291 emsg_funcname("E120: Using <SID> not in a script context: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007292 name);
7293 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007294 case ERROR_DICT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007295 emsg_funcname("E725: Calling dict function without Dictionary: %s",
Bram Moolenaare9a41262005-01-15 22:18:47 +00007296 name);
7297 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007298 }
7299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300
7301 name[len] = cc;
7302 if (fname != name && fname != fname_buf)
7303 vim_free(fname);
7304
7305 return ret;
7306}
7307
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007308/*
7309 * Give an error message with a function name. Handle <SNR> things.
7310 */
7311 static void
7312emsg_funcname(msg, name)
7313 char *msg;
7314 char_u *name;
7315{
7316 char_u *p;
7317
7318 if (*name == K_SPECIAL)
7319 p = concat_str((char_u *)"<SNR>", name + 3);
7320 else
7321 p = name;
7322 EMSG2(_(msg), p);
7323 if (p != name)
7324 vim_free(p);
7325}
7326
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327/*********************************************
7328 * Implementation of the built-in functions
7329 */
7330
7331/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007332 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333 */
7334 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007335f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007336 typval_T *argvars;
7337 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338{
Bram Moolenaar33570922005-01-25 22:26:29 +00007339 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007341 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007342 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007344 if ((l = argvars[0].vval.v_list) != NULL
7345 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7346 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007347 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007348 }
7349 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007350 EMSG(_(e_listreq));
7351}
7352
7353/*
7354 * "append(lnum, string/list)" function
7355 */
7356 static void
7357f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007358 typval_T *argvars;
7359 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007360{
7361 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007362 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007363 list_T *l = NULL;
7364 listitem_T *li = NULL;
7365 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007366 long added = 0;
7367
Bram Moolenaar0d660222005-01-07 21:51:51 +00007368 lnum = get_tv_lnum(argvars);
7369 if (lnum >= 0
7370 && lnum <= curbuf->b_ml.ml_line_count
7371 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007372 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007373 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007374 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007375 l = argvars[1].vval.v_list;
7376 if (l == NULL)
7377 return;
7378 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007379 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007380 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007381 for (;;)
7382 {
7383 if (l == NULL)
7384 tv = &argvars[1]; /* append a string */
7385 else if (li == NULL)
7386 break; /* end of list */
7387 else
7388 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007389 line = get_tv_string_chk(tv);
7390 if (line == NULL) /* type error */
7391 {
7392 rettv->vval.v_number = 1; /* Failed */
7393 break;
7394 }
7395 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007396 ++added;
7397 if (l == NULL)
7398 break;
7399 li = li->li_next;
7400 }
7401
7402 appended_lines_mark(lnum, added);
7403 if (curwin->w_cursor.lnum > lnum)
7404 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007406 else
7407 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408}
7409
7410/*
7411 * "argc()" function
7412 */
7413/* ARGSUSED */
7414 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007415f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007416 typval_T *argvars;
7417 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007419 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420}
7421
7422/*
7423 * "argidx()" function
7424 */
7425/* ARGSUSED */
7426 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007427f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007428 typval_T *argvars;
7429 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007431 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432}
7433
7434/*
7435 * "argv(nr)" function
7436 */
7437 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007438f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007439 typval_T *argvars;
7440 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441{
7442 int idx;
7443
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007444 idx = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445 if (idx >= 0 && idx < ARGCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007446 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007448 rettv->vval.v_string = NULL;
7449 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450}
7451
7452/*
7453 * "browse(save, title, initdir, default)" function
7454 */
7455/* ARGSUSED */
7456 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007457f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007458 typval_T *argvars;
7459 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460{
7461#ifdef FEAT_BROWSE
7462 int save;
7463 char_u *title;
7464 char_u *initdir;
7465 char_u *defname;
7466 char_u buf[NUMBUFLEN];
7467 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007468 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007470 save = get_tv_number_chk(&argvars[0], &error);
7471 title = get_tv_string_chk(&argvars[1]);
7472 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7473 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007475 if (error || title == NULL || initdir == NULL || defname == NULL)
7476 rettv->vval.v_string = NULL;
7477 else
7478 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007479 do_browse(save ? BROWSE_SAVE : 0,
7480 title, defname, NULL, initdir, NULL, curbuf);
7481#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007482 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007483#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007484 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007485}
7486
7487/*
7488 * "browsedir(title, initdir)" function
7489 */
7490/* ARGSUSED */
7491 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007492f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007493 typval_T *argvars;
7494 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007495{
7496#ifdef FEAT_BROWSE
7497 char_u *title;
7498 char_u *initdir;
7499 char_u buf[NUMBUFLEN];
7500
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007501 title = get_tv_string_chk(&argvars[0]);
7502 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007503
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007504 if (title == NULL || initdir == NULL)
7505 rettv->vval.v_string = NULL;
7506 else
7507 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007508 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007510 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007512 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513}
7514
Bram Moolenaar33570922005-01-25 22:26:29 +00007515static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007516
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517/*
7518 * Find a buffer by number or exact name.
7519 */
7520 static buf_T *
7521find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007522 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523{
7524 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007526 if (avar->v_type == VAR_NUMBER)
7527 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007528 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007530 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007531 if (buf == NULL)
7532 {
7533 /* No full path name match, try a match with a URL or a "nofile"
7534 * buffer, these don't use the full path. */
7535 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7536 if (buf->b_fname != NULL
7537 && (path_with_url(buf->b_fname)
7538#ifdef FEAT_QUICKFIX
7539 || bt_nofile(buf)
7540#endif
7541 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007542 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007543 break;
7544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007545 }
7546 return buf;
7547}
7548
7549/*
7550 * "bufexists(expr)" function
7551 */
7552 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007553f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007554 typval_T *argvars;
7555 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007557 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558}
7559
7560/*
7561 * "buflisted(expr)" function
7562 */
7563 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007564f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007565 typval_T *argvars;
7566 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567{
7568 buf_T *buf;
7569
7570 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007571 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572}
7573
7574/*
7575 * "bufloaded(expr)" function
7576 */
7577 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007578f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007579 typval_T *argvars;
7580 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581{
7582 buf_T *buf;
7583
7584 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007585 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586}
7587
Bram Moolenaar33570922005-01-25 22:26:29 +00007588static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007589
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590/*
7591 * Get buffer by number or pattern.
7592 */
7593 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007594get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007595 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007597 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598 int save_magic;
7599 char_u *save_cpo;
7600 buf_T *buf;
7601
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007602 if (tv->v_type == VAR_NUMBER)
7603 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007604 if (tv->v_type != VAR_STRING)
7605 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606 if (name == NULL || *name == NUL)
7607 return curbuf;
7608 if (name[0] == '$' && name[1] == NUL)
7609 return lastbuf;
7610
7611 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7612 save_magic = p_magic;
7613 p_magic = TRUE;
7614 save_cpo = p_cpo;
7615 p_cpo = (char_u *)"";
7616
7617 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
7618 TRUE, FALSE));
7619
7620 p_magic = save_magic;
7621 p_cpo = save_cpo;
7622
7623 /* If not found, try expanding the name, like done for bufexists(). */
7624 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007625 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626
7627 return buf;
7628}
7629
7630/*
7631 * "bufname(expr)" function
7632 */
7633 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007634f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007635 typval_T *argvars;
7636 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637{
7638 buf_T *buf;
7639
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007640 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007642 buf = get_buf_tv(&argvars[0]);
7643 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007645 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007647 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648 --emsg_off;
7649}
7650
7651/*
7652 * "bufnr(expr)" function
7653 */
7654 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007655f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007656 typval_T *argvars;
7657 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658{
7659 buf_T *buf;
7660
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007661 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007663 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007665 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007667 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668 --emsg_off;
7669}
7670
7671/*
7672 * "bufwinnr(nr)" function
7673 */
7674 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007675f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007676 typval_T *argvars;
7677 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678{
7679#ifdef FEAT_WINDOWS
7680 win_T *wp;
7681 int winnr = 0;
7682#endif
7683 buf_T *buf;
7684
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007685 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007687 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688#ifdef FEAT_WINDOWS
7689 for (wp = firstwin; wp; wp = wp->w_next)
7690 {
7691 ++winnr;
7692 if (wp->w_buffer == buf)
7693 break;
7694 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007695 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007697 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698#endif
7699 --emsg_off;
7700}
7701
7702/*
7703 * "byte2line(byte)" function
7704 */
7705/*ARGSUSED*/
7706 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007707f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007708 typval_T *argvars;
7709 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710{
7711#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007712 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713#else
7714 long boff = 0;
7715
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007716 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007718 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007720 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721 (linenr_T)0, &boff);
7722#endif
7723}
7724
7725/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007726 * "byteidx()" function
7727 */
7728/*ARGSUSED*/
7729 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007730f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007731 typval_T *argvars;
7732 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007733{
7734#ifdef FEAT_MBYTE
7735 char_u *t;
7736#endif
7737 char_u *str;
7738 long idx;
7739
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007740 str = get_tv_string_chk(&argvars[0]);
7741 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007742 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007743 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007744 return;
7745
7746#ifdef FEAT_MBYTE
7747 t = str;
7748 for ( ; idx > 0; idx--)
7749 {
7750 if (*t == NUL) /* EOL reached */
7751 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007752 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007753 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007754 rettv->vval.v_number = t - str;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007755#else
7756 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007757 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007758#endif
7759}
7760
7761/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007762 * "call(func, arglist)" function
7763 */
7764 static void
7765f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007766 typval_T *argvars;
7767 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007768{
7769 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00007770 typval_T argv[MAX_FUNC_ARGS];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007771 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007772 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007773 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00007774 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007775
7776 rettv->vval.v_number = 0;
7777 if (argvars[1].v_type != VAR_LIST)
7778 {
7779 EMSG(_(e_listreq));
7780 return;
7781 }
7782 if (argvars[1].vval.v_list == NULL)
7783 return;
7784
7785 if (argvars[0].v_type == VAR_FUNC)
7786 func = argvars[0].vval.v_string;
7787 else
7788 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007789 if (*func == NUL)
7790 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007791
Bram Moolenaare9a41262005-01-15 22:18:47 +00007792 if (argvars[2].v_type != VAR_UNKNOWN)
7793 {
7794 if (argvars[2].v_type != VAR_DICT)
7795 {
7796 EMSG(_(e_dictreq));
7797 return;
7798 }
7799 selfdict = argvars[2].vval.v_dict;
7800 }
7801
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007802 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
7803 item = item->li_next)
7804 {
7805 if (argc == MAX_FUNC_ARGS)
7806 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007807 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007808 break;
7809 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007810 /* Make a copy of each argument. This is needed to be able to set
7811 * v_lock to VAR_FIXED in the copy without changing the original list.
7812 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007813 copy_tv(&item->li_tv, &argv[argc++]);
7814 }
7815
7816 if (item == NULL)
7817 (void)call_func(func, STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007818 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
7819 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007820
7821 /* Free the arguments. */
7822 while (argc > 0)
7823 clear_tv(&argv[--argc]);
7824}
7825
7826/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827 * "char2nr(string)" function
7828 */
7829 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007830f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007831 typval_T *argvars;
7832 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833{
7834#ifdef FEAT_MBYTE
7835 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007836 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837 else
7838#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007839 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840}
7841
7842/*
7843 * "cindent(lnum)" function
7844 */
7845 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007846f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007847 typval_T *argvars;
7848 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849{
7850#ifdef FEAT_CINDENT
7851 pos_T pos;
7852 linenr_T lnum;
7853
7854 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007855 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7857 {
7858 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007859 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860 curwin->w_cursor = pos;
7861 }
7862 else
7863#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007864 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865}
7866
7867/*
7868 * "col(string)" function
7869 */
7870 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007871f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007872 typval_T *argvars;
7873 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874{
7875 colnr_T col = 0;
7876 pos_T *fp;
7877
7878 fp = var2fpos(&argvars[0], FALSE);
7879 if (fp != NULL)
7880 {
7881 if (fp->col == MAXCOL)
7882 {
7883 /* '> can be MAXCOL, get the length of the line then */
7884 if (fp->lnum <= curbuf->b_ml.ml_line_count)
7885 col = STRLEN(ml_get(fp->lnum)) + 1;
7886 else
7887 col = MAXCOL;
7888 }
7889 else
7890 {
7891 col = fp->col + 1;
7892#ifdef FEAT_VIRTUALEDIT
7893 /* col(".") when the cursor is on the NUL at the end of the line
7894 * because of "coladd" can be seen as an extra column. */
7895 if (virtual_active() && fp == &curwin->w_cursor)
7896 {
7897 char_u *p = ml_get_cursor();
7898
7899 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
7900 curwin->w_virtcol - curwin->w_cursor.coladd))
7901 {
7902# ifdef FEAT_MBYTE
7903 int l;
7904
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007905 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906 col += l;
7907# else
7908 if (*p != NUL && p[1] == NUL)
7909 ++col;
7910# endif
7911 }
7912 }
7913#endif
7914 }
7915 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007916 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007917}
7918
Bram Moolenaar572cb562005-08-05 21:35:02 +00007919#if defined(FEAT_INS_EXPAND)
7920/*
7921 * "complete_add()" function
7922 */
7923/*ARGSUSED*/
7924 static void
7925f_complete_add(argvars, rettv)
7926 typval_T *argvars;
7927 typval_T *rettv;
7928{
7929 char_u *s;
7930
7931 s = get_tv_string_chk(&argvars[0]);
7932 if (s != NULL)
7933 rettv->vval.v_number = ins_compl_add(s, -1, NULL, FORWARD, 0);
7934}
7935
7936/*
7937 * "complete_check()" function
7938 */
7939/*ARGSUSED*/
7940 static void
7941f_complete_check(argvars, rettv)
7942 typval_T *argvars;
7943 typval_T *rettv;
7944{
7945 int saved = RedrawingDisabled;
7946
7947 RedrawingDisabled = 0;
7948 ins_compl_check_keys(0);
7949 rettv->vval.v_number = compl_interrupted;
7950 RedrawingDisabled = saved;
7951}
7952#endif
7953
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954/*
7955 * "confirm(message, buttons[, default [, type]])" function
7956 */
7957/*ARGSUSED*/
7958 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007959f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007960 typval_T *argvars;
7961 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962{
7963#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
7964 char_u *message;
7965 char_u *buttons = NULL;
7966 char_u buf[NUMBUFLEN];
7967 char_u buf2[NUMBUFLEN];
7968 int def = 1;
7969 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007970 char_u *typestr;
7971 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007973 message = get_tv_string_chk(&argvars[0]);
7974 if (message == NULL)
7975 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007976 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007978 buttons = get_tv_string_buf_chk(&argvars[1], buf);
7979 if (buttons == NULL)
7980 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007981 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007983 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007984 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007986 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
7987 if (typestr == NULL)
7988 error = TRUE;
7989 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007991 switch (TOUPPER_ASC(*typestr))
7992 {
7993 case 'E': type = VIM_ERROR; break;
7994 case 'Q': type = VIM_QUESTION; break;
7995 case 'I': type = VIM_INFO; break;
7996 case 'W': type = VIM_WARNING; break;
7997 case 'G': type = VIM_GENERIC; break;
7998 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999 }
8000 }
8001 }
8002 }
8003
8004 if (buttons == NULL || *buttons == NUL)
8005 buttons = (char_u *)_("&Ok");
8006
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008007 if (error)
8008 rettv->vval.v_number = 0;
8009 else
8010 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011 def, NULL);
8012#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008013 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014#endif
8015}
8016
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008017/*
8018 * "copy()" function
8019 */
8020 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008021f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008022 typval_T *argvars;
8023 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008024{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008025 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008026}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027
8028/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008029 * "count()" function
8030 */
8031 static void
8032f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008033 typval_T *argvars;
8034 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008035{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008036 long n = 0;
8037 int ic = FALSE;
8038
Bram Moolenaare9a41262005-01-15 22:18:47 +00008039 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008040 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008041 listitem_T *li;
8042 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008043 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008044
Bram Moolenaare9a41262005-01-15 22:18:47 +00008045 if ((l = argvars[0].vval.v_list) != NULL)
8046 {
8047 li = l->lv_first;
8048 if (argvars[2].v_type != VAR_UNKNOWN)
8049 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008050 int error = FALSE;
8051
8052 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008053 if (argvars[3].v_type != VAR_UNKNOWN)
8054 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008055 idx = get_tv_number_chk(&argvars[3], &error);
8056 if (!error)
8057 {
8058 li = list_find(l, idx);
8059 if (li == NULL)
8060 EMSGN(_(e_listidx), idx);
8061 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008062 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008063 if (error)
8064 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008065 }
8066
8067 for ( ; li != NULL; li = li->li_next)
8068 if (tv_equal(&li->li_tv, &argvars[1], ic))
8069 ++n;
8070 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008071 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008072 else if (argvars[0].v_type == VAR_DICT)
8073 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008074 int todo;
8075 dict_T *d;
8076 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008077
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008078 if ((d = argvars[0].vval.v_dict) != NULL)
8079 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008080 int error = FALSE;
8081
Bram Moolenaare9a41262005-01-15 22:18:47 +00008082 if (argvars[2].v_type != VAR_UNKNOWN)
8083 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008084 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008085 if (argvars[3].v_type != VAR_UNKNOWN)
8086 EMSG(_(e_invarg));
8087 }
8088
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008089 todo = error ? 0 : d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008090 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008091 {
8092 if (!HASHITEM_EMPTY(hi))
8093 {
8094 --todo;
8095 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
8096 ++n;
8097 }
8098 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008099 }
8100 }
8101 else
8102 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008103 rettv->vval.v_number = n;
8104}
8105
8106/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8108 *
8109 * Checks the existence of a cscope connection.
8110 */
8111/*ARGSUSED*/
8112 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008113f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008114 typval_T *argvars;
8115 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116{
8117#ifdef FEAT_CSCOPE
8118 int num = 0;
8119 char_u *dbpath = NULL;
8120 char_u *prepend = NULL;
8121 char_u buf[NUMBUFLEN];
8122
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008123 if (argvars[0].v_type != VAR_UNKNOWN
8124 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008126 num = (int)get_tv_number(&argvars[0]);
8127 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008128 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008129 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130 }
8131
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008132 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008134 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135#endif
8136}
8137
8138/*
8139 * "cursor(lnum, col)" function
8140 *
8141 * Moves the cursor to the specified line and column
8142 */
8143/*ARGSUSED*/
8144 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008145f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008146 typval_T *argvars;
8147 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148{
8149 long line, col;
8150
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008151 line = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008152 col = get_tv_number_chk(&argvars[1], NULL);
8153 if (line < 0 || col < 0)
8154 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 if (line > 0)
8156 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 if (col > 0)
8158 curwin->w_cursor.col = col - 1;
8159#ifdef FEAT_VIRTUALEDIT
8160 curwin->w_cursor.coladd = 0;
8161#endif
8162
8163 /* Make sure the cursor is in a valid position. */
8164 check_cursor();
8165#ifdef FEAT_MBYTE
8166 /* Correct cursor for multi-byte character. */
8167 if (has_mbyte)
8168 mb_adjust_cursor();
8169#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008170
8171 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172}
8173
8174/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008175 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176 */
8177 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008178f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008179 typval_T *argvars;
8180 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008182 int noref = 0;
8183
8184 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008185 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008186 if (noref < 0 || noref > 1)
8187 EMSG(_(e_invarg));
8188 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008189 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190}
8191
8192/*
8193 * "delete()" function
8194 */
8195 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008196f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008197 typval_T *argvars;
8198 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199{
8200 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008201 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008203 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204}
8205
8206/*
8207 * "did_filetype()" function
8208 */
8209/*ARGSUSED*/
8210 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008211f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008212 typval_T *argvars;
8213 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214{
8215#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008216 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008218 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219#endif
8220}
8221
8222/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008223 * "diff_filler()" function
8224 */
8225/*ARGSUSED*/
8226 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008227f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008228 typval_T *argvars;
8229 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008230{
8231#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008232 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008233#endif
8234}
8235
8236/*
8237 * "diff_hlID()" function
8238 */
8239/*ARGSUSED*/
8240 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008241f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008242 typval_T *argvars;
8243 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008244{
8245#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008246 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008247 static linenr_T prev_lnum = 0;
8248 static int changedtick = 0;
8249 static int fnum = 0;
8250 static int change_start = 0;
8251 static int change_end = 0;
8252 static enum hlf_value hlID = 0;
8253 int filler_lines;
8254 int col;
8255
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008256 if (lnum < 0) /* ignore type error in {lnum} arg */
8257 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008258 if (lnum != prev_lnum
8259 || changedtick != curbuf->b_changedtick
8260 || fnum != curbuf->b_fnum)
8261 {
8262 /* New line, buffer, change: need to get the values. */
8263 filler_lines = diff_check(curwin, lnum);
8264 if (filler_lines < 0)
8265 {
8266 if (filler_lines == -1)
8267 {
8268 change_start = MAXCOL;
8269 change_end = -1;
8270 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8271 hlID = HLF_ADD; /* added line */
8272 else
8273 hlID = HLF_CHD; /* changed line */
8274 }
8275 else
8276 hlID = HLF_ADD; /* added line */
8277 }
8278 else
8279 hlID = (enum hlf_value)0;
8280 prev_lnum = lnum;
8281 changedtick = curbuf->b_changedtick;
8282 fnum = curbuf->b_fnum;
8283 }
8284
8285 if (hlID == HLF_CHD || hlID == HLF_TXD)
8286 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008287 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008288 if (col >= change_start && col <= change_end)
8289 hlID = HLF_TXD; /* changed text */
8290 else
8291 hlID = HLF_CHD; /* changed line */
8292 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008293 rettv->vval.v_number = hlID == (enum hlf_value)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008294#endif
8295}
8296
8297/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008298 * "empty({expr})" function
8299 */
8300 static void
8301f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008302 typval_T *argvars;
8303 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008304{
8305 int n;
8306
8307 switch (argvars[0].v_type)
8308 {
8309 case VAR_STRING:
8310 case VAR_FUNC:
8311 n = argvars[0].vval.v_string == NULL
8312 || *argvars[0].vval.v_string == NUL;
8313 break;
8314 case VAR_NUMBER:
8315 n = argvars[0].vval.v_number == 0;
8316 break;
8317 case VAR_LIST:
8318 n = argvars[0].vval.v_list == NULL
8319 || argvars[0].vval.v_list->lv_first == NULL;
8320 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008321 case VAR_DICT:
8322 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008323 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008324 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008325 default:
8326 EMSG2(_(e_intern2), "f_empty()");
8327 n = 0;
8328 }
8329
8330 rettv->vval.v_number = n;
8331}
8332
8333/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 * "escape({string}, {chars})" function
8335 */
8336 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008337f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008338 typval_T *argvars;
8339 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340{
8341 char_u buf[NUMBUFLEN];
8342
Bram Moolenaar758711c2005-02-02 23:11:38 +00008343 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8344 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008345 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346}
8347
8348/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008349 * "eval()" function
8350 */
8351/*ARGSUSED*/
8352 static void
8353f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008354 typval_T *argvars;
8355 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008356{
8357 char_u *s;
8358
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008359 s = get_tv_string_chk(&argvars[0]);
8360 if (s != NULL)
8361 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008362
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008363 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8364 {
8365 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008366 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008367 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008368 else if (*s != NUL)
8369 EMSG(_(e_trailing));
8370}
8371
8372/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373 * "eventhandler()" function
8374 */
8375/*ARGSUSED*/
8376 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008377f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008378 typval_T *argvars;
8379 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008381 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382}
8383
8384/*
8385 * "executable()" function
8386 */
8387 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008388f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008389 typval_T *argvars;
8390 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008392 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393}
8394
8395/*
8396 * "exists()" function
8397 */
8398 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008399f_exists(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{
8403 char_u *p;
8404 char_u *name;
8405 int n = FALSE;
8406 int len = 0;
8407
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008408 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409 if (*p == '$') /* environment variable */
8410 {
8411 /* first try "normal" environment variables (fast) */
8412 if (mch_getenv(p + 1) != NULL)
8413 n = TRUE;
8414 else
8415 {
8416 /* try expanding things like $VIM and ${HOME} */
8417 p = expand_env_save(p);
8418 if (p != NULL && *p != '$')
8419 n = TRUE;
8420 vim_free(p);
8421 }
8422 }
8423 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008424 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008425 else if (*p == '*') /* internal or user defined function */
8426 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008427 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428 }
8429 else if (*p == ':')
8430 {
8431 n = cmd_exists(p + 1);
8432 }
8433 else if (*p == '#')
8434 {
8435#ifdef FEAT_AUTOCMD
8436 name = p + 1;
8437 p = vim_strchr(name, '#');
8438 if (p != NULL)
8439 n = au_exists(name, p, p + 1);
8440 else
8441 n = au_exists(name, name + STRLEN(name), NULL);
8442#endif
8443 }
8444 else /* internal variable */
8445 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008446 char_u *tofree;
8447 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008449 /* get_name_len() takes care of expanding curly braces */
8450 name = p;
8451 len = get_name_len(&p, &tofree, TRUE, FALSE);
8452 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008454 if (tofree != NULL)
8455 name = tofree;
8456 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8457 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008459 /* handle d.key, l[idx], f(expr) */
8460 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8461 if (n)
8462 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463 }
8464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008465
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008466 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467 }
8468
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008469 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470}
8471
8472/*
8473 * "expand()" function
8474 */
8475 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008476f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008477 typval_T *argvars;
8478 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479{
8480 char_u *s;
8481 int len;
8482 char_u *errormsg;
8483 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8484 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008485 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008487 rettv->v_type = VAR_STRING;
8488 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489 if (*s == '%' || *s == '#' || *s == '<')
8490 {
8491 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008492 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493 --emsg_off;
8494 }
8495 else
8496 {
8497 /* When the optional second argument is non-zero, don't remove matches
8498 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008499 if (argvars[1].v_type != VAR_UNKNOWN
8500 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008502 if (!error)
8503 {
8504 ExpandInit(&xpc);
8505 xpc.xp_context = EXPAND_FILES;
8506 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
8507 ExpandCleanup(&xpc);
8508 }
8509 else
8510 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511 }
8512}
8513
8514/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008515 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00008516 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008517 */
8518 static void
8519f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008520 typval_T *argvars;
8521 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008522{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008523 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008524 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008525 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008526 list_T *l1, *l2;
8527 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008528 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008529 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008530
Bram Moolenaare9a41262005-01-15 22:18:47 +00008531 l1 = argvars[0].vval.v_list;
8532 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008533 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
8534 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008535 {
8536 if (argvars[2].v_type != VAR_UNKNOWN)
8537 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008538 before = get_tv_number_chk(&argvars[2], &error);
8539 if (error)
8540 return; /* type error; errmsg already given */
8541
Bram Moolenaar758711c2005-02-02 23:11:38 +00008542 if (before == l1->lv_len)
8543 item = NULL;
8544 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008545 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00008546 item = list_find(l1, before);
8547 if (item == NULL)
8548 {
8549 EMSGN(_(e_listidx), before);
8550 return;
8551 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008552 }
8553 }
8554 else
8555 item = NULL;
8556 list_extend(l1, l2, item);
8557
Bram Moolenaare9a41262005-01-15 22:18:47 +00008558 copy_tv(&argvars[0], rettv);
8559 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008560 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008561 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
8562 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008563 dict_T *d1, *d2;
8564 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008565 char_u *action;
8566 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00008567 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008568 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008569
8570 d1 = argvars[0].vval.v_dict;
8571 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008572 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
8573 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008574 {
8575 /* Check the third argument. */
8576 if (argvars[2].v_type != VAR_UNKNOWN)
8577 {
8578 static char *(av[]) = {"keep", "force", "error"};
8579
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008580 action = get_tv_string_chk(&argvars[2]);
8581 if (action == NULL)
8582 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008583 for (i = 0; i < 3; ++i)
8584 if (STRCMP(action, av[i]) == 0)
8585 break;
8586 if (i == 3)
8587 {
8588 EMSGN(_(e_invarg2), action);
8589 return;
8590 }
8591 }
8592 else
8593 action = (char_u *)"force";
8594
8595 /* Go over all entries in the second dict and add them to the
8596 * first dict. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008597 todo = d2->dv_hashtab.ht_used;
8598 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008599 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008600 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008601 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008602 --todo;
8603 di1 = dict_find(d1, hi2->hi_key, -1);
8604 if (di1 == NULL)
8605 {
8606 di1 = dictitem_copy(HI2DI(hi2));
8607 if (di1 != NULL && dict_add(d1, di1) == FAIL)
8608 dictitem_free(di1);
8609 }
8610 else if (*action == 'e')
8611 {
8612 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
8613 break;
8614 }
8615 else if (*action == 'f')
8616 {
8617 clear_tv(&di1->di_tv);
8618 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
8619 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008620 }
8621 }
8622
Bram Moolenaare9a41262005-01-15 22:18:47 +00008623 copy_tv(&argvars[0], rettv);
8624 }
8625 }
8626 else
8627 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008628}
8629
8630/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008631 * "filereadable()" function
8632 */
8633 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008634f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008635 typval_T *argvars;
8636 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637{
8638 FILE *fd;
8639 char_u *p;
8640 int n;
8641
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008642 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
8644 {
8645 n = TRUE;
8646 fclose(fd);
8647 }
8648 else
8649 n = FALSE;
8650
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008651 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652}
8653
8654/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008655 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656 * rights to write into.
8657 */
8658 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008659f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008660 typval_T *argvars;
8661 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008662{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008663 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008664}
8665
Bram Moolenaar33570922005-01-25 22:26:29 +00008666static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008667
8668 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008669findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00008670 typval_T *argvars;
8671 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008672 int dir;
8673{
8674#ifdef FEAT_SEARCHPATH
8675 char_u *fname;
8676 char_u *fresult = NULL;
8677 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
8678 char_u *p;
8679 char_u pathbuf[NUMBUFLEN];
8680 int count = 1;
8681 int first = TRUE;
8682
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008683 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008684
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008685 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008686 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008687 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
8688 if (p == NULL)
8689 count = -1; /* error */
8690 else
8691 {
8692 if (*p != NUL)
8693 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008694
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008695 if (argvars[2].v_type != VAR_UNKNOWN)
8696 count = get_tv_number_chk(&argvars[2], NULL); /* -1: error */
8697 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008698 }
8699
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008700 if (*fname != NUL && count >= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008701 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008702 do
8703 {
8704 vim_free(fresult);
8705 fresult = find_file_in_path_option(first ? fname : NULL,
8706 first ? (int)STRLEN(fname) : 0,
8707 0, first, path, dir, NULL);
8708 first = FALSE;
8709 } while (--count > 0 && fresult != NULL);
8710 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008711
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008712 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008713#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008714 rettv->vval.v_string = NULL;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008715#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008716 rettv->v_type = VAR_STRING;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008717}
8718
Bram Moolenaar33570922005-01-25 22:26:29 +00008719static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
8720static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008721
8722/*
8723 * Implementation of map() and filter().
8724 */
8725 static void
8726filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00008727 typval_T *argvars;
8728 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008729 int map;
8730{
8731 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00008732 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00008733 listitem_T *li, *nli;
8734 list_T *l = NULL;
8735 dictitem_T *di;
8736 hashtab_T *ht;
8737 hashitem_T *hi;
8738 dict_T *d = NULL;
8739 typval_T save_val;
8740 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008741 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008742 int todo;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008743 char_u *msg = map ? (char_u *)"map()" : (char_u *)"filter()";
8744
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008745
8746 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008747 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008748 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008749 if ((l = argvars[0].vval.v_list) == NULL
8750 || (map && tv_check_lock(l->lv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008751 return;
8752 }
8753 else if (argvars[0].v_type == VAR_DICT)
8754 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008755 if ((d = argvars[0].vval.v_dict) == NULL
8756 || (map && tv_check_lock(d->dv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008757 return;
8758 }
8759 else
8760 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008761 EMSG2(_(e_listdictarg), msg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008762 return;
8763 }
8764
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008765 expr = get_tv_string_buf_chk(&argvars[1], buf);
8766 /* On type errors, the preceding call has already displayed an error
8767 * message. Avoid a misleading error message for an empty string that
8768 * was not passed as argument. */
8769 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008770 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008771 prepare_vimvar(VV_VAL, &save_val);
8772 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008773
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008774 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008775 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008776 prepare_vimvar(VV_KEY, &save_key);
8777 vimvars[VV_KEY].vv_type = VAR_STRING;
8778
8779 ht = &d->dv_hashtab;
8780 hash_lock(ht);
8781 todo = ht->ht_used;
8782 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008783 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008784 if (!HASHITEM_EMPTY(hi))
8785 {
8786 --todo;
8787 di = HI2DI(hi);
8788 if (tv_check_lock(di->di_tv.v_lock, msg))
8789 break;
8790 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
8791 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL)
8792 break;
8793 if (!map && rem)
8794 dictitem_remove(d, di);
8795 clear_tv(&vimvars[VV_KEY].vv_tv);
8796 }
8797 }
8798 hash_unlock(ht);
8799
8800 restore_vimvar(VV_KEY, &save_key);
8801 }
8802 else
8803 {
8804 for (li = l->lv_first; li != NULL; li = nli)
8805 {
8806 if (tv_check_lock(li->li_tv.v_lock, msg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008807 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008808 nli = li->li_next;
8809 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008810 break;
8811 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008812 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008813 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008814 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008815
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008816 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008817 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008818
8819 copy_tv(&argvars[0], rettv);
8820}
8821
8822 static int
8823filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00008824 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008825 char_u *expr;
8826 int map;
8827 int *remp;
8828{
Bram Moolenaar33570922005-01-25 22:26:29 +00008829 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008830 char_u *s;
8831
Bram Moolenaar33570922005-01-25 22:26:29 +00008832 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008833 s = expr;
8834 if (eval1(&s, &rettv, TRUE) == FAIL)
8835 return FAIL;
8836 if (*s != NUL) /* check for trailing chars after expr */
8837 {
8838 EMSG2(_(e_invexpr2), s);
8839 return FAIL;
8840 }
8841 if (map)
8842 {
8843 /* map(): replace the list item value */
8844 clear_tv(tv);
8845 *tv = rettv;
8846 }
8847 else
8848 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008849 int error = FALSE;
8850
Bram Moolenaare9a41262005-01-15 22:18:47 +00008851 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008852 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008853 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008854 /* On type error, nothing has been removed; return FAIL to stop the
8855 * loop. The error message was given by get_tv_number_chk(). */
8856 if (error)
8857 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008858 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008859 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008860 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008861}
8862
8863/*
8864 * "filter()" function
8865 */
8866 static void
8867f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008868 typval_T *argvars;
8869 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008870{
8871 filter_map(argvars, rettv, FALSE);
8872}
8873
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008874/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008875 * "finddir({fname}[, {path}[, {count}]])" function
8876 */
8877 static void
8878f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008879 typval_T *argvars;
8880 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008881{
8882 findfilendir(argvars, rettv, TRUE);
8883}
8884
8885/*
8886 * "findfile({fname}[, {path}[, {count}]])" function
8887 */
8888 static void
8889f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008890 typval_T *argvars;
8891 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008892{
8893 findfilendir(argvars, rettv, FALSE);
8894}
8895
8896/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008897 * "fnamemodify({fname}, {mods})" function
8898 */
8899 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008900f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008901 typval_T *argvars;
8902 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008903{
8904 char_u *fname;
8905 char_u *mods;
8906 int usedlen = 0;
8907 int len;
8908 char_u *fbuf = NULL;
8909 char_u buf[NUMBUFLEN];
8910
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008911 fname = get_tv_string_chk(&argvars[0]);
8912 mods = get_tv_string_buf_chk(&argvars[1], buf);
8913 if (fname == NULL || mods == NULL)
8914 fname = NULL;
8915 else
8916 {
8917 len = (int)STRLEN(fname);
8918 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
8919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008921 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008923 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008925 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926 vim_free(fbuf);
8927}
8928
Bram Moolenaar33570922005-01-25 22:26:29 +00008929static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008930
8931/*
8932 * "foldclosed()" function
8933 */
8934 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008935foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00008936 typval_T *argvars;
8937 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008938 int end;
8939{
8940#ifdef FEAT_FOLDING
8941 linenr_T lnum;
8942 linenr_T first, last;
8943
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008944 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8946 {
8947 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
8948 {
8949 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008950 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008952 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953 return;
8954 }
8955 }
8956#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008957 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008958}
8959
8960/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008961 * "foldclosed()" function
8962 */
8963 static void
8964f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008965 typval_T *argvars;
8966 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008967{
8968 foldclosed_both(argvars, rettv, FALSE);
8969}
8970
8971/*
8972 * "foldclosedend()" function
8973 */
8974 static void
8975f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008976 typval_T *argvars;
8977 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008978{
8979 foldclosed_both(argvars, rettv, TRUE);
8980}
8981
8982/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008983 * "foldlevel()" function
8984 */
8985 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008986f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008987 typval_T *argvars;
8988 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008989{
8990#ifdef FEAT_FOLDING
8991 linenr_T lnum;
8992
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008993 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008994 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008995 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008996 else
8997#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008998 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999}
9000
9001/*
9002 * "foldtext()" function
9003 */
9004/*ARGSUSED*/
9005 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009006f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009007 typval_T *argvars;
9008 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009{
9010#ifdef FEAT_FOLDING
9011 linenr_T lnum;
9012 char_u *s;
9013 char_u *r;
9014 int len;
9015 char *txt;
9016#endif
9017
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009018 rettv->v_type = VAR_STRING;
9019 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009020#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00009021 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
9022 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
9023 <= curbuf->b_ml.ml_line_count
9024 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009025 {
9026 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009027 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
9028 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009029 {
9030 if (!linewhite(lnum))
9031 break;
9032 ++lnum;
9033 }
9034
9035 /* Find interesting text in this line. */
9036 s = skipwhite(ml_get(lnum));
9037 /* skip C comment-start */
9038 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009039 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009040 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009041 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00009042 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009043 {
9044 s = skipwhite(ml_get(lnum + 1));
9045 if (*s == '*')
9046 s = skipwhite(s + 1);
9047 }
9048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009049 txt = _("+-%s%3ld lines: ");
9050 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009051 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009052 + 20 /* for %3ld */
9053 + STRLEN(s))); /* concatenated */
9054 if (r != NULL)
9055 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009056 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
9057 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
9058 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009059 len = (int)STRLEN(r);
9060 STRCAT(r, s);
9061 /* remove 'foldmarker' and 'commentstring' */
9062 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009063 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064 }
9065 }
9066#endif
9067}
9068
9069/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009070 * "foldtextresult(lnum)" function
9071 */
9072/*ARGSUSED*/
9073 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009074f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009075 typval_T *argvars;
9076 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009077{
9078#ifdef FEAT_FOLDING
9079 linenr_T lnum;
9080 char_u *text;
9081 char_u buf[51];
9082 foldinfo_T foldinfo;
9083 int fold_count;
9084#endif
9085
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009086 rettv->v_type = VAR_STRING;
9087 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009088#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009089 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009090 /* treat illegal types and illegal string values for {lnum} the same */
9091 if (lnum < 0)
9092 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009093 fold_count = foldedCount(curwin, lnum, &foldinfo);
9094 if (fold_count > 0)
9095 {
9096 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
9097 &foldinfo, buf);
9098 if (text == buf)
9099 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009100 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009101 }
9102#endif
9103}
9104
9105/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106 * "foreground()" function
9107 */
9108/*ARGSUSED*/
9109 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009110f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009111 typval_T *argvars;
9112 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009113{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009114 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009115#ifdef FEAT_GUI
9116 if (gui.in_use)
9117 gui_mch_set_foreground();
9118#else
9119# ifdef WIN32
9120 win32_set_foreground();
9121# endif
9122#endif
9123}
9124
9125/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009126 * "function()" function
9127 */
9128/*ARGSUSED*/
9129 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009130f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009131 typval_T *argvars;
9132 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009133{
9134 char_u *s;
9135
Bram Moolenaara7043832005-01-21 11:56:39 +00009136 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009137 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009138 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009139 EMSG2(_(e_invarg2), s);
9140 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009141 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009142 else
9143 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009144 rettv->vval.v_string = vim_strsave(s);
9145 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009146 }
9147}
9148
9149/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009150 * "garbagecollect()" function
9151 */
9152/*ARGSUSED*/
9153 static void
9154f_garbagecollect(argvars, rettv)
9155 typval_T *argvars;
9156 typval_T *rettv;
9157{
9158 garbage_collect();
9159}
9160
9161/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009162 * "get()" function
9163 */
9164 static void
9165f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009166 typval_T *argvars;
9167 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009168{
Bram Moolenaar33570922005-01-25 22:26:29 +00009169 listitem_T *li;
9170 list_T *l;
9171 dictitem_T *di;
9172 dict_T *d;
9173 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009174
Bram Moolenaare9a41262005-01-15 22:18:47 +00009175 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009176 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009177 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009178 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009179 int error = FALSE;
9180
9181 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9182 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009183 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009184 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009185 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009186 else if (argvars[0].v_type == VAR_DICT)
9187 {
9188 if ((d = argvars[0].vval.v_dict) != NULL)
9189 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009190 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009191 if (di != NULL)
9192 tv = &di->di_tv;
9193 }
9194 }
9195 else
9196 EMSG2(_(e_listdictarg), "get()");
9197
9198 if (tv == NULL)
9199 {
9200 if (argvars[2].v_type == VAR_UNKNOWN)
9201 rettv->vval.v_number = 0;
9202 else
9203 copy_tv(&argvars[2], rettv);
9204 }
9205 else
9206 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009207}
9208
Bram Moolenaar342337a2005-07-21 21:11:17 +00009209static 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 +00009210
9211/*
9212 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +00009213 * Return a range (from start to end) of lines in rettv from the specified
9214 * buffer.
9215 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009216 */
9217 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +00009218get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009219 buf_T *buf;
9220 linenr_T start;
9221 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009222 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009223 typval_T *rettv;
9224{
9225 char_u *p;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009226 list_T *l = NULL;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009227
Bram Moolenaar342337a2005-07-21 21:11:17 +00009228 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009229 {
Bram Moolenaar342337a2005-07-21 21:11:17 +00009230 l = list_alloc();
9231 if (l == NULL)
9232 return;
9233
9234 rettv->vval.v_list = l;
9235 rettv->v_type = VAR_LIST;
9236 ++l->lv_refcount;
9237 }
9238 else
9239 rettv->vval.v_number = 0;
9240
9241 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
9242 return;
9243
9244 if (!retlist)
9245 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009246 if (start >= 1 && start <= buf->b_ml.ml_line_count)
9247 p = ml_get_buf(buf, start, FALSE);
9248 else
9249 p = (char_u *)"";
9250
9251 rettv->v_type = VAR_STRING;
9252 rettv->vval.v_string = vim_strsave(p);
9253 }
9254 else
9255 {
9256 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009257 return;
9258
9259 if (start < 1)
9260 start = 1;
9261 if (end > buf->b_ml.ml_line_count)
9262 end = buf->b_ml.ml_line_count;
9263 while (start <= end)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00009264 if (list_append_string(l, ml_get_buf(buf, start++, FALSE)) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009265 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009266 }
9267}
9268
9269/*
9270 * "getbufline()" function
9271 */
9272 static void
9273f_getbufline(argvars, rettv)
9274 typval_T *argvars;
9275 typval_T *rettv;
9276{
9277 linenr_T lnum;
9278 linenr_T end;
9279 buf_T *buf;
9280
9281 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9282 ++emsg_off;
9283 buf = get_buf_tv(&argvars[0]);
9284 --emsg_off;
9285
Bram Moolenaar661b1822005-07-28 22:36:45 +00009286 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009287 if (argvars[2].v_type == VAR_UNKNOWN)
9288 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009289 else
Bram Moolenaar661b1822005-07-28 22:36:45 +00009290 end = get_tv_lnum_buf(&argvars[2], buf);
9291
Bram Moolenaar342337a2005-07-21 21:11:17 +00009292 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009293}
9294
Bram Moolenaar0d660222005-01-07 21:51:51 +00009295/*
9296 * "getbufvar()" function
9297 */
9298 static void
9299f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009300 typval_T *argvars;
9301 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009302{
9303 buf_T *buf;
9304 buf_T *save_curbuf;
9305 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009306 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009307
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009308 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9309 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009310 ++emsg_off;
9311 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009312
9313 rettv->v_type = VAR_STRING;
9314 rettv->vval.v_string = NULL;
9315
9316 if (buf != NULL && varname != NULL)
9317 {
9318 if (*varname == '&') /* buffer-local-option */
9319 {
9320 /* set curbuf to be our buf, temporarily */
9321 save_curbuf = curbuf;
9322 curbuf = buf;
9323
9324 get_option_tv(&varname, rettv, TRUE);
9325
9326 /* restore previous notion of curbuf */
9327 curbuf = save_curbuf;
9328 }
9329 else
9330 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009331 if (*varname == NUL)
9332 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9333 * scope prefix before the NUL byte is required by
9334 * find_var_in_ht(). */
9335 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009336 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009337 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009338 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009339 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009340 }
9341 }
9342
9343 --emsg_off;
9344}
9345
9346/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009347 * "getchar()" function
9348 */
9349 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009350f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009351 typval_T *argvars;
9352 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009353{
9354 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009355 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009356
9357 ++no_mapping;
9358 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009359 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009360 /* getchar(): blocking wait. */
9361 n = safe_vgetc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009362 else if (get_tv_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009363 /* getchar(1): only check if char avail */
9364 n = vpeekc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009365 else if (error || vpeekc() == NUL)
9366 /* illegal argument or getchar(0) and no char avail: return zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009367 n = 0;
9368 else
9369 /* getchar(0) and char avail: return char */
9370 n = safe_vgetc();
9371 --no_mapping;
9372 --allow_keys;
9373
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009374 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009375 if (IS_SPECIAL(n) || mod_mask != 0)
9376 {
9377 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9378 int i = 0;
9379
9380 /* Turn a special key into three bytes, plus modifier. */
9381 if (mod_mask != 0)
9382 {
9383 temp[i++] = K_SPECIAL;
9384 temp[i++] = KS_MODIFIER;
9385 temp[i++] = mod_mask;
9386 }
9387 if (IS_SPECIAL(n))
9388 {
9389 temp[i++] = K_SPECIAL;
9390 temp[i++] = K_SECOND(n);
9391 temp[i++] = K_THIRD(n);
9392 }
9393#ifdef FEAT_MBYTE
9394 else if (has_mbyte)
9395 i += (*mb_char2bytes)(n, temp + i);
9396#endif
9397 else
9398 temp[i++] = n;
9399 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009400 rettv->v_type = VAR_STRING;
9401 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009402 }
9403}
9404
9405/*
9406 * "getcharmod()" function
9407 */
9408/*ARGSUSED*/
9409 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009410f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009411 typval_T *argvars;
9412 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009413{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009414 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009415}
9416
9417/*
9418 * "getcmdline()" function
9419 */
9420/*ARGSUSED*/
9421 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009422f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009423 typval_T *argvars;
9424 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009425{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009426 rettv->v_type = VAR_STRING;
9427 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009428}
9429
9430/*
9431 * "getcmdpos()" function
9432 */
9433/*ARGSUSED*/
9434 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009435f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009436 typval_T *argvars;
9437 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009438{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009439 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009440}
9441
9442/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00009443 * "getcmdtype()" function
9444 */
9445/*ARGSUSED*/
9446 static void
9447f_getcmdtype(argvars, rettv)
9448 typval_T *argvars;
9449 typval_T *rettv;
9450{
9451 rettv->v_type = VAR_STRING;
9452 rettv->vval.v_string = alloc(2);
9453 if (rettv->vval.v_string != NULL)
9454 {
9455 rettv->vval.v_string[0] = get_cmdline_type();
9456 rettv->vval.v_string[1] = NUL;
9457 }
9458}
9459
9460/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461 * "getcwd()" function
9462 */
9463/*ARGSUSED*/
9464 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009465f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009466 typval_T *argvars;
9467 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009468{
9469 char_u cwd[MAXPATHL];
9470
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009471 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009472 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009473 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009474 else
9475 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009476 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009477#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +00009478 if (rettv->vval.v_string != NULL)
9479 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009480#endif
9481 }
9482}
9483
9484/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009485 * "getfontname()" function
9486 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009487/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009488 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009489f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009490 typval_T *argvars;
9491 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009492{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009493 rettv->v_type = VAR_STRING;
9494 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009495#ifdef FEAT_GUI
9496 if (gui.in_use)
9497 {
9498 GuiFont font;
9499 char_u *name = NULL;
9500
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009501 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009502 {
9503 /* Get the "Normal" font. Either the name saved by
9504 * hl_set_font_name() or from the font ID. */
9505 font = gui.norm_font;
9506 name = hl_get_font_name();
9507 }
9508 else
9509 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009510 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009511 if (STRCMP(name, "*") == 0) /* don't use font dialog */
9512 return;
9513 font = gui_mch_get_font(name, FALSE);
9514 if (font == NOFONT)
9515 return; /* Invalid font name, return empty string. */
9516 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009517 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009518 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009519 gui_mch_free_font(font);
9520 }
9521#endif
9522}
9523
9524/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009525 * "getfperm({fname})" function
9526 */
9527 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009528f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009529 typval_T *argvars;
9530 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009531{
9532 char_u *fname;
9533 struct stat st;
9534 char_u *perm = NULL;
9535 char_u flags[] = "rwx";
9536 int i;
9537
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009538 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009539
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009540 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009541 if (mch_stat((char *)fname, &st) >= 0)
9542 {
9543 perm = vim_strsave((char_u *)"---------");
9544 if (perm != NULL)
9545 {
9546 for (i = 0; i < 9; i++)
9547 {
9548 if (st.st_mode & (1 << (8 - i)))
9549 perm[i] = flags[i % 3];
9550 }
9551 }
9552 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009553 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009554}
9555
9556/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009557 * "getfsize({fname})" function
9558 */
9559 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009560f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009561 typval_T *argvars;
9562 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009563{
9564 char_u *fname;
9565 struct stat st;
9566
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009567 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009568
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009569 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009570
9571 if (mch_stat((char *)fname, &st) >= 0)
9572 {
9573 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009574 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009575 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009576 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009577 }
9578 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009579 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009580}
9581
9582/*
9583 * "getftime({fname})" function
9584 */
9585 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009586f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009587 typval_T *argvars;
9588 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009589{
9590 char_u *fname;
9591 struct stat st;
9592
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009593 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009594
9595 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009596 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009597 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009598 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599}
9600
9601/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009602 * "getftype({fname})" function
9603 */
9604 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009605f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009606 typval_T *argvars;
9607 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009608{
9609 char_u *fname;
9610 struct stat st;
9611 char_u *type = NULL;
9612 char *t;
9613
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009614 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009615
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009616 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009617 if (mch_lstat((char *)fname, &st) >= 0)
9618 {
9619#ifdef S_ISREG
9620 if (S_ISREG(st.st_mode))
9621 t = "file";
9622 else if (S_ISDIR(st.st_mode))
9623 t = "dir";
9624# ifdef S_ISLNK
9625 else if (S_ISLNK(st.st_mode))
9626 t = "link";
9627# endif
9628# ifdef S_ISBLK
9629 else if (S_ISBLK(st.st_mode))
9630 t = "bdev";
9631# endif
9632# ifdef S_ISCHR
9633 else if (S_ISCHR(st.st_mode))
9634 t = "cdev";
9635# endif
9636# ifdef S_ISFIFO
9637 else if (S_ISFIFO(st.st_mode))
9638 t = "fifo";
9639# endif
9640# ifdef S_ISSOCK
9641 else if (S_ISSOCK(st.st_mode))
9642 t = "fifo";
9643# endif
9644 else
9645 t = "other";
9646#else
9647# ifdef S_IFMT
9648 switch (st.st_mode & S_IFMT)
9649 {
9650 case S_IFREG: t = "file"; break;
9651 case S_IFDIR: t = "dir"; break;
9652# ifdef S_IFLNK
9653 case S_IFLNK: t = "link"; break;
9654# endif
9655# ifdef S_IFBLK
9656 case S_IFBLK: t = "bdev"; break;
9657# endif
9658# ifdef S_IFCHR
9659 case S_IFCHR: t = "cdev"; break;
9660# endif
9661# ifdef S_IFIFO
9662 case S_IFIFO: t = "fifo"; break;
9663# endif
9664# ifdef S_IFSOCK
9665 case S_IFSOCK: t = "socket"; break;
9666# endif
9667 default: t = "other";
9668 }
9669# else
9670 if (mch_isdir(fname))
9671 t = "dir";
9672 else
9673 t = "file";
9674# endif
9675#endif
9676 type = vim_strsave((char_u *)t);
9677 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009678 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009679}
9680
9681/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009682 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +00009683 */
9684 static void
9685f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009686 typval_T *argvars;
9687 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009688{
9689 linenr_T lnum;
9690 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009691 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009692
9693 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009694 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009695 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009696 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009697 retlist = FALSE;
9698 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009699 else
Bram Moolenaar342337a2005-07-21 21:11:17 +00009700 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009701 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009702 retlist = TRUE;
9703 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009704
Bram Moolenaar342337a2005-07-21 21:11:17 +00009705 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009706}
9707
9708/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00009709 * "getqflist()" function
9710 */
9711/*ARGSUSED*/
9712 static void
9713f_getqflist(argvars, rettv)
9714 typval_T *argvars;
9715 typval_T *rettv;
9716{
9717#ifdef FEAT_QUICKFIX
9718 list_T *l;
9719#endif
9720
9721 rettv->vval.v_number = FALSE;
9722#ifdef FEAT_QUICKFIX
9723 l = list_alloc();
9724 if (l != NULL)
9725 {
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00009726 rettv->vval.v_list = l;
9727 rettv->v_type = VAR_LIST;
9728 ++l->lv_refcount;
9729 (void)get_errorlist(l);
Bram Moolenaar2641f772005-03-25 21:58:17 +00009730 }
9731#endif
9732}
9733
9734/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009735 * "getreg()" function
9736 */
9737 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009738f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009739 typval_T *argvars;
9740 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009741{
9742 char_u *strregname;
9743 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009744 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009745 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009746
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009747 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009748 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009749 strregname = get_tv_string_chk(&argvars[0]);
9750 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009751 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009752 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009754 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009755 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009756 regname = (strregname == NULL ? '"' : *strregname);
9757 if (regname == 0)
9758 regname = '"';
9759
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009760 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009761 rettv->vval.v_string = error ? NULL :
9762 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763}
9764
9765/*
9766 * "getregtype()" function
9767 */
9768 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009769f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009770 typval_T *argvars;
9771 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009772{
9773 char_u *strregname;
9774 int regname;
9775 char_u buf[NUMBUFLEN + 2];
9776 long reglen = 0;
9777
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009778 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009779 {
9780 strregname = get_tv_string_chk(&argvars[0]);
9781 if (strregname == NULL) /* type error; errmsg already given */
9782 {
9783 rettv->v_type = VAR_STRING;
9784 rettv->vval.v_string = NULL;
9785 return;
9786 }
9787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788 else
9789 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009790 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009791
9792 regname = (strregname == NULL ? '"' : *strregname);
9793 if (regname == 0)
9794 regname = '"';
9795
9796 buf[0] = NUL;
9797 buf[1] = NUL;
9798 switch (get_reg_type(regname, &reglen))
9799 {
9800 case MLINE: buf[0] = 'V'; break;
9801 case MCHAR: buf[0] = 'v'; break;
9802#ifdef FEAT_VISUAL
9803 case MBLOCK:
9804 buf[0] = Ctrl_V;
9805 sprintf((char *)buf + 1, "%ld", reglen + 1);
9806 break;
9807#endif
9808 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009809 rettv->v_type = VAR_STRING;
9810 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009811}
9812
9813/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009814 * "getwinposx()" function
9815 */
9816/*ARGSUSED*/
9817 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009818f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009819 typval_T *argvars;
9820 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009821{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009822 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009823#ifdef FEAT_GUI
9824 if (gui.in_use)
9825 {
9826 int x, y;
9827
9828 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009829 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009830 }
9831#endif
9832}
9833
9834/*
9835 * "getwinposy()" function
9836 */
9837/*ARGSUSED*/
9838 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009839f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009840 typval_T *argvars;
9841 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009842{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009843 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009844#ifdef FEAT_GUI
9845 if (gui.in_use)
9846 {
9847 int x, y;
9848
9849 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009850 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009851 }
9852#endif
9853}
9854
Bram Moolenaara40058a2005-07-11 22:42:07 +00009855static win_T *find_win_by_nr __ARGS((typval_T *vp));
9856
9857 static win_T *
9858find_win_by_nr(vp)
9859 typval_T *vp;
9860{
9861#ifdef FEAT_WINDOWS
9862 win_T *wp;
9863#endif
9864 int nr;
9865
9866 nr = get_tv_number_chk(vp, NULL);
9867
9868#ifdef FEAT_WINDOWS
9869 if (nr < 0)
9870 return NULL;
9871 if (nr == 0)
9872 return curwin;
9873
9874 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9875 if (--nr <= 0)
9876 break;
9877 return wp;
9878#else
9879 if (nr == 0 || nr == 1)
9880 return curwin;
9881 return NULL;
9882#endif
9883}
9884
Bram Moolenaar071d4272004-06-13 20:20:40 +00009885/*
9886 * "getwinvar()" function
9887 */
9888 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009889f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009890 typval_T *argvars;
9891 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009892{
9893 win_T *win, *oldcurwin;
9894 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009895 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009896
Bram Moolenaar071d4272004-06-13 20:20:40 +00009897 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009898 varname = get_tv_string_chk(&argvars[1]);
9899 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009900
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009901 rettv->v_type = VAR_STRING;
9902 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009903
9904 if (win != NULL && varname != NULL)
9905 {
9906 if (*varname == '&') /* window-local-option */
9907 {
Bram Moolenaarc0761132005-03-18 20:30:32 +00009908 /* Set curwin to be our win, temporarily. Also set curbuf, so
9909 * that we can get buffer-local options. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009910 oldcurwin = curwin;
9911 curwin = win;
Bram Moolenaarc0761132005-03-18 20:30:32 +00009912 curbuf = win->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009913
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009914 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009915
9916 /* restore previous notion of curwin */
9917 curwin = oldcurwin;
Bram Moolenaarc0761132005-03-18 20:30:32 +00009918 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009919 }
9920 else
9921 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009922 if (*varname == NUL)
9923 /* let getwinvar({nr}, "") return the "w:" dictionary. The
9924 * scope prefix before the NUL byte is required by
9925 * find_var_in_ht(). */
9926 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009927 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009928 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009929 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009930 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009931 }
9932 }
9933
9934 --emsg_off;
9935}
9936
9937/*
9938 * "glob()" function
9939 */
9940 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009941f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009942 typval_T *argvars;
9943 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009944{
9945 expand_T xpc;
9946
9947 ExpandInit(&xpc);
9948 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009949 rettv->v_type = VAR_STRING;
9950 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009951 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
9952 ExpandCleanup(&xpc);
9953}
9954
9955/*
9956 * "globpath()" function
9957 */
9958 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009959f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009960 typval_T *argvars;
9961 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009962{
9963 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009964 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009965
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009966 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009967 if (file == NULL)
9968 rettv->vval.v_string = NULL;
9969 else
9970 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009971}
9972
9973/*
9974 * "has()" function
9975 */
9976 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009977f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009978 typval_T *argvars;
9979 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009980{
9981 int i;
9982 char_u *name;
9983 int n = FALSE;
9984 static char *(has_list[]) =
9985 {
9986#ifdef AMIGA
9987 "amiga",
9988# ifdef FEAT_ARP
9989 "arp",
9990# endif
9991#endif
9992#ifdef __BEOS__
9993 "beos",
9994#endif
9995#ifdef MSDOS
9996# ifdef DJGPP
9997 "dos32",
9998# else
9999 "dos16",
10000# endif
10001#endif
10002#ifdef MACOS /* TODO: Should we add MACOS_CLASSIC, MACOS_X? (Dany) */
10003 "mac",
10004#endif
10005#if defined(MACOS_X_UNIX)
10006 "macunix",
10007#endif
10008#ifdef OS2
10009 "os2",
10010#endif
10011#ifdef __QNX__
10012 "qnx",
10013#endif
10014#ifdef RISCOS
10015 "riscos",
10016#endif
10017#ifdef UNIX
10018 "unix",
10019#endif
10020#ifdef VMS
10021 "vms",
10022#endif
10023#ifdef WIN16
10024 "win16",
10025#endif
10026#ifdef WIN32
10027 "win32",
10028#endif
10029#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
10030 "win32unix",
10031#endif
10032#ifdef WIN64
10033 "win64",
10034#endif
10035#ifdef EBCDIC
10036 "ebcdic",
10037#endif
10038#ifndef CASE_INSENSITIVE_FILENAME
10039 "fname_case",
10040#endif
10041#ifdef FEAT_ARABIC
10042 "arabic",
10043#endif
10044#ifdef FEAT_AUTOCMD
10045 "autocmd",
10046#endif
10047#ifdef FEAT_BEVAL
10048 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000010049# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
10050 "balloon_multiline",
10051# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010052#endif
10053#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
10054 "builtin_terms",
10055# ifdef ALL_BUILTIN_TCAPS
10056 "all_builtin_terms",
10057# endif
10058#endif
10059#ifdef FEAT_BYTEOFF
10060 "byte_offset",
10061#endif
10062#ifdef FEAT_CINDENT
10063 "cindent",
10064#endif
10065#ifdef FEAT_CLIENTSERVER
10066 "clientserver",
10067#endif
10068#ifdef FEAT_CLIPBOARD
10069 "clipboard",
10070#endif
10071#ifdef FEAT_CMDL_COMPL
10072 "cmdline_compl",
10073#endif
10074#ifdef FEAT_CMDHIST
10075 "cmdline_hist",
10076#endif
10077#ifdef FEAT_COMMENTS
10078 "comments",
10079#endif
10080#ifdef FEAT_CRYPT
10081 "cryptv",
10082#endif
10083#ifdef FEAT_CSCOPE
10084 "cscope",
10085#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010086#ifdef CURSOR_SHAPE
10087 "cursorshape",
10088#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010089#ifdef DEBUG
10090 "debug",
10091#endif
10092#ifdef FEAT_CON_DIALOG
10093 "dialog_con",
10094#endif
10095#ifdef FEAT_GUI_DIALOG
10096 "dialog_gui",
10097#endif
10098#ifdef FEAT_DIFF
10099 "diff",
10100#endif
10101#ifdef FEAT_DIGRAPHS
10102 "digraphs",
10103#endif
10104#ifdef FEAT_DND
10105 "dnd",
10106#endif
10107#ifdef FEAT_EMACS_TAGS
10108 "emacs_tags",
10109#endif
10110 "eval", /* always present, of course! */
10111#ifdef FEAT_EX_EXTRA
10112 "ex_extra",
10113#endif
10114#ifdef FEAT_SEARCH_EXTRA
10115 "extra_search",
10116#endif
10117#ifdef FEAT_FKMAP
10118 "farsi",
10119#endif
10120#ifdef FEAT_SEARCHPATH
10121 "file_in_path",
10122#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010123#if defined(UNIX) && !defined(USE_SYSTEM)
10124 "filterpipe",
10125#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010126#ifdef FEAT_FIND_ID
10127 "find_in_path",
10128#endif
10129#ifdef FEAT_FOLDING
10130 "folding",
10131#endif
10132#ifdef FEAT_FOOTER
10133 "footer",
10134#endif
10135#if !defined(USE_SYSTEM) && defined(UNIX)
10136 "fork",
10137#endif
10138#ifdef FEAT_GETTEXT
10139 "gettext",
10140#endif
10141#ifdef FEAT_GUI
10142 "gui",
10143#endif
10144#ifdef FEAT_GUI_ATHENA
10145# ifdef FEAT_GUI_NEXTAW
10146 "gui_neXtaw",
10147# else
10148 "gui_athena",
10149# endif
10150#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +000010151#ifdef FEAT_GUI_KDE
10152 "gui_kde",
10153#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010154#ifdef FEAT_GUI_GTK
10155 "gui_gtk",
10156# ifdef HAVE_GTK2
10157 "gui_gtk2",
10158# endif
10159#endif
10160#ifdef FEAT_GUI_MAC
10161 "gui_mac",
10162#endif
10163#ifdef FEAT_GUI_MOTIF
10164 "gui_motif",
10165#endif
10166#ifdef FEAT_GUI_PHOTON
10167 "gui_photon",
10168#endif
10169#ifdef FEAT_GUI_W16
10170 "gui_win16",
10171#endif
10172#ifdef FEAT_GUI_W32
10173 "gui_win32",
10174#endif
10175#ifdef FEAT_HANGULIN
10176 "hangul_input",
10177#endif
10178#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10179 "iconv",
10180#endif
10181#ifdef FEAT_INS_EXPAND
10182 "insert_expand",
10183#endif
10184#ifdef FEAT_JUMPLIST
10185 "jumplist",
10186#endif
10187#ifdef FEAT_KEYMAP
10188 "keymap",
10189#endif
10190#ifdef FEAT_LANGMAP
10191 "langmap",
10192#endif
10193#ifdef FEAT_LIBCALL
10194 "libcall",
10195#endif
10196#ifdef FEAT_LINEBREAK
10197 "linebreak",
10198#endif
10199#ifdef FEAT_LISP
10200 "lispindent",
10201#endif
10202#ifdef FEAT_LISTCMDS
10203 "listcmds",
10204#endif
10205#ifdef FEAT_LOCALMAP
10206 "localmap",
10207#endif
10208#ifdef FEAT_MENU
10209 "menu",
10210#endif
10211#ifdef FEAT_SESSION
10212 "mksession",
10213#endif
10214#ifdef FEAT_MODIFY_FNAME
10215 "modify_fname",
10216#endif
10217#ifdef FEAT_MOUSE
10218 "mouse",
10219#endif
10220#ifdef FEAT_MOUSESHAPE
10221 "mouseshape",
10222#endif
10223#if defined(UNIX) || defined(VMS)
10224# ifdef FEAT_MOUSE_DEC
10225 "mouse_dec",
10226# endif
10227# ifdef FEAT_MOUSE_GPM
10228 "mouse_gpm",
10229# endif
10230# ifdef FEAT_MOUSE_JSB
10231 "mouse_jsbterm",
10232# endif
10233# ifdef FEAT_MOUSE_NET
10234 "mouse_netterm",
10235# endif
10236# ifdef FEAT_MOUSE_PTERM
10237 "mouse_pterm",
10238# endif
10239# ifdef FEAT_MOUSE_XTERM
10240 "mouse_xterm",
10241# endif
10242#endif
10243#ifdef FEAT_MBYTE
10244 "multi_byte",
10245#endif
10246#ifdef FEAT_MBYTE_IME
10247 "multi_byte_ime",
10248#endif
10249#ifdef FEAT_MULTI_LANG
10250 "multi_lang",
10251#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010252#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010253#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010254 "mzscheme",
10255#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010256#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010257#ifdef FEAT_OLE
10258 "ole",
10259#endif
10260#ifdef FEAT_OSFILETYPE
10261 "osfiletype",
10262#endif
10263#ifdef FEAT_PATH_EXTRA
10264 "path_extra",
10265#endif
10266#ifdef FEAT_PERL
10267#ifndef DYNAMIC_PERL
10268 "perl",
10269#endif
10270#endif
10271#ifdef FEAT_PYTHON
10272#ifndef DYNAMIC_PYTHON
10273 "python",
10274#endif
10275#endif
10276#ifdef FEAT_POSTSCRIPT
10277 "postscript",
10278#endif
10279#ifdef FEAT_PRINTER
10280 "printer",
10281#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000010282#ifdef FEAT_PROFILE
10283 "profile",
10284#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010285#ifdef FEAT_QUICKFIX
10286 "quickfix",
10287#endif
10288#ifdef FEAT_RIGHTLEFT
10289 "rightleft",
10290#endif
10291#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
10292 "ruby",
10293#endif
10294#ifdef FEAT_SCROLLBIND
10295 "scrollbind",
10296#endif
10297#ifdef FEAT_CMDL_INFO
10298 "showcmd",
10299 "cmdline_info",
10300#endif
10301#ifdef FEAT_SIGNS
10302 "signs",
10303#endif
10304#ifdef FEAT_SMARTINDENT
10305 "smartindent",
10306#endif
10307#ifdef FEAT_SNIFF
10308 "sniff",
10309#endif
10310#ifdef FEAT_STL_OPT
10311 "statusline",
10312#endif
10313#ifdef FEAT_SUN_WORKSHOP
10314 "sun_workshop",
10315#endif
10316#ifdef FEAT_NETBEANS_INTG
10317 "netbeans_intg",
10318#endif
10319#ifdef FEAT_SYN_HL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010320 "spell",
10321#endif
10322#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010323 "syntax",
10324#endif
10325#if defined(USE_SYSTEM) || !defined(UNIX)
10326 "system",
10327#endif
10328#ifdef FEAT_TAG_BINS
10329 "tag_binary",
10330#endif
10331#ifdef FEAT_TAG_OLDSTATIC
10332 "tag_old_static",
10333#endif
10334#ifdef FEAT_TAG_ANYWHITE
10335 "tag_any_white",
10336#endif
10337#ifdef FEAT_TCL
10338# ifndef DYNAMIC_TCL
10339 "tcl",
10340# endif
10341#endif
10342#ifdef TERMINFO
10343 "terminfo",
10344#endif
10345#ifdef FEAT_TERMRESPONSE
10346 "termresponse",
10347#endif
10348#ifdef FEAT_TEXTOBJ
10349 "textobjects",
10350#endif
10351#ifdef HAVE_TGETENT
10352 "tgetent",
10353#endif
10354#ifdef FEAT_TITLE
10355 "title",
10356#endif
10357#ifdef FEAT_TOOLBAR
10358 "toolbar",
10359#endif
10360#ifdef FEAT_USR_CMDS
10361 "user-commands", /* was accidentally included in 5.4 */
10362 "user_commands",
10363#endif
10364#ifdef FEAT_VIMINFO
10365 "viminfo",
10366#endif
10367#ifdef FEAT_VERTSPLIT
10368 "vertsplit",
10369#endif
10370#ifdef FEAT_VIRTUALEDIT
10371 "virtualedit",
10372#endif
10373#ifdef FEAT_VISUAL
10374 "visual",
10375#endif
10376#ifdef FEAT_VISUALEXTRA
10377 "visualextra",
10378#endif
10379#ifdef FEAT_VREPLACE
10380 "vreplace",
10381#endif
10382#ifdef FEAT_WILDIGN
10383 "wildignore",
10384#endif
10385#ifdef FEAT_WILDMENU
10386 "wildmenu",
10387#endif
10388#ifdef FEAT_WINDOWS
10389 "windows",
10390#endif
10391#ifdef FEAT_WAK
10392 "winaltkeys",
10393#endif
10394#ifdef FEAT_WRITEBACKUP
10395 "writebackup",
10396#endif
10397#ifdef FEAT_XIM
10398 "xim",
10399#endif
10400#ifdef FEAT_XFONTSET
10401 "xfontset",
10402#endif
10403#ifdef USE_XSMP
10404 "xsmp",
10405#endif
10406#ifdef USE_XSMP_INTERACT
10407 "xsmp_interact",
10408#endif
10409#ifdef FEAT_XCLIPBOARD
10410 "xterm_clipboard",
10411#endif
10412#ifdef FEAT_XTERM_SAVE
10413 "xterm_save",
10414#endif
10415#if defined(UNIX) && defined(FEAT_X11)
10416 "X11",
10417#endif
10418 NULL
10419 };
10420
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010421 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010422 for (i = 0; has_list[i] != NULL; ++i)
10423 if (STRICMP(name, has_list[i]) == 0)
10424 {
10425 n = TRUE;
10426 break;
10427 }
10428
10429 if (n == FALSE)
10430 {
10431 if (STRNICMP(name, "patch", 5) == 0)
10432 n = has_patch(atoi((char *)name + 5));
10433 else if (STRICMP(name, "vim_starting") == 0)
10434 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010435#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
10436 else if (STRICMP(name, "balloon_multiline") == 0)
10437 n = multiline_balloon_available();
10438#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010439#ifdef DYNAMIC_TCL
10440 else if (STRICMP(name, "tcl") == 0)
10441 n = tcl_enabled(FALSE);
10442#endif
10443#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
10444 else if (STRICMP(name, "iconv") == 0)
10445 n = iconv_enabled(FALSE);
10446#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010447#ifdef DYNAMIC_MZSCHEME
10448 else if (STRICMP(name, "mzscheme") == 0)
10449 n = mzscheme_enabled(FALSE);
10450#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010451#ifdef DYNAMIC_RUBY
10452 else if (STRICMP(name, "ruby") == 0)
10453 n = ruby_enabled(FALSE);
10454#endif
10455#ifdef DYNAMIC_PYTHON
10456 else if (STRICMP(name, "python") == 0)
10457 n = python_enabled(FALSE);
10458#endif
10459#ifdef DYNAMIC_PERL
10460 else if (STRICMP(name, "perl") == 0)
10461 n = perl_enabled(FALSE);
10462#endif
10463#ifdef FEAT_GUI
10464 else if (STRICMP(name, "gui_running") == 0)
10465 n = (gui.in_use || gui.starting);
10466# ifdef FEAT_GUI_W32
10467 else if (STRICMP(name, "gui_win32s") == 0)
10468 n = gui_is_win32s();
10469# endif
10470# ifdef FEAT_BROWSE
10471 else if (STRICMP(name, "browse") == 0)
10472 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
10473# endif
10474#endif
10475#ifdef FEAT_SYN_HL
10476 else if (STRICMP(name, "syntax_items") == 0)
10477 n = syntax_present(curbuf);
10478#endif
10479#if defined(WIN3264)
10480 else if (STRICMP(name, "win95") == 0)
10481 n = mch_windows95();
10482#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000010483#ifdef FEAT_NETBEANS_INTG
10484 else if (STRICMP(name, "netbeans_enabled") == 0)
10485 n = usingNetbeans;
10486#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010487 }
10488
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010489 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010490}
10491
10492/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000010493 * "has_key()" function
10494 */
10495 static void
10496f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010497 typval_T *argvars;
10498 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010499{
10500 rettv->vval.v_number = 0;
10501 if (argvars[0].v_type != VAR_DICT)
10502 {
10503 EMSG(_(e_dictreq));
10504 return;
10505 }
10506 if (argvars[0].vval.v_dict == NULL)
10507 return;
10508
10509 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010510 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010511}
10512
10513/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010514 * "hasmapto()" function
10515 */
10516 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010517f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010518 typval_T *argvars;
10519 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010520{
10521 char_u *name;
10522 char_u *mode;
10523 char_u buf[NUMBUFLEN];
10524
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010525 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010526 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010527 mode = (char_u *)"nvo";
10528 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010529 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010530
10531 if (map_to_exists(name, mode))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010532 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010533 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010534 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010535}
10536
10537/*
10538 * "histadd()" function
10539 */
10540/*ARGSUSED*/
10541 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010542f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010543 typval_T *argvars;
10544 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010545{
10546#ifdef FEAT_CMDHIST
10547 int histype;
10548 char_u *str;
10549 char_u buf[NUMBUFLEN];
10550#endif
10551
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010552 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010553 if (check_restricted() || check_secure())
10554 return;
10555#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010556 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10557 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010558 if (histype >= 0)
10559 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010560 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010561 if (*str != NUL)
10562 {
10563 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010564 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010565 return;
10566 }
10567 }
10568#endif
10569}
10570
10571/*
10572 * "histdel()" function
10573 */
10574/*ARGSUSED*/
10575 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010576f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010577 typval_T *argvars;
10578 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010579{
10580#ifdef FEAT_CMDHIST
10581 int n;
10582 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010583 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010584
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010585 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10586 if (str == NULL)
10587 n = 0;
10588 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010589 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010590 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010591 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010592 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010593 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010594 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010595 else
10596 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010597 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010598 get_tv_string_buf(&argvars[1], buf));
10599 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010600#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010601 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010602#endif
10603}
10604
10605/*
10606 * "histget()" function
10607 */
10608/*ARGSUSED*/
10609 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010610f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010611 typval_T *argvars;
10612 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010613{
10614#ifdef FEAT_CMDHIST
10615 int type;
10616 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010617 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010618
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010619 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10620 if (str == NULL)
10621 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010622 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010623 {
10624 type = get_histtype(str);
10625 if (argvars[1].v_type == VAR_UNKNOWN)
10626 idx = get_history_idx(type);
10627 else
10628 idx = (int)get_tv_number_chk(&argvars[1], NULL);
10629 /* -1 on type error */
10630 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
10631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010632#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010633 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010634#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010635 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010636}
10637
10638/*
10639 * "histnr()" function
10640 */
10641/*ARGSUSED*/
10642 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010643f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010644 typval_T *argvars;
10645 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010646{
10647 int i;
10648
10649#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010650 char_u *history = get_tv_string_chk(&argvars[0]);
10651
10652 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010653 if (i >= HIST_CMD && i < HIST_COUNT)
10654 i = get_history_idx(i);
10655 else
10656#endif
10657 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010658 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010659}
10660
10661/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010662 * "highlightID(name)" function
10663 */
10664 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010665f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010666 typval_T *argvars;
10667 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010668{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010669 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010670}
10671
10672/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010673 * "highlight_exists()" function
10674 */
10675 static void
10676f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010677 typval_T *argvars;
10678 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010679{
10680 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
10681}
10682
10683/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010684 * "hostname()" function
10685 */
10686/*ARGSUSED*/
10687 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010688f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010689 typval_T *argvars;
10690 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010691{
10692 char_u hostname[256];
10693
10694 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010695 rettv->v_type = VAR_STRING;
10696 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010697}
10698
10699/*
10700 * iconv() function
10701 */
10702/*ARGSUSED*/
10703 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010704f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010705 typval_T *argvars;
10706 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010707{
10708#ifdef FEAT_MBYTE
10709 char_u buf1[NUMBUFLEN];
10710 char_u buf2[NUMBUFLEN];
10711 char_u *from, *to, *str;
10712 vimconv_T vimconv;
10713#endif
10714
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010715 rettv->v_type = VAR_STRING;
10716 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010717
10718#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010719 str = get_tv_string(&argvars[0]);
10720 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
10721 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010722 vimconv.vc_type = CONV_NONE;
10723 convert_setup(&vimconv, from, to);
10724
10725 /* If the encodings are equal, no conversion needed. */
10726 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010727 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010728 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010729 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010730
10731 convert_setup(&vimconv, NULL, NULL);
10732 vim_free(from);
10733 vim_free(to);
10734#endif
10735}
10736
10737/*
10738 * "indent()" function
10739 */
10740 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010741f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010742 typval_T *argvars;
10743 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010744{
10745 linenr_T lnum;
10746
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010747 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010748 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010749 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010750 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010751 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010752}
10753
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010754/*
10755 * "index()" function
10756 */
10757 static void
10758f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010759 typval_T *argvars;
10760 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010761{
Bram Moolenaar33570922005-01-25 22:26:29 +000010762 list_T *l;
10763 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010764 long idx = 0;
10765 int ic = FALSE;
10766
10767 rettv->vval.v_number = -1;
10768 if (argvars[0].v_type != VAR_LIST)
10769 {
10770 EMSG(_(e_listreq));
10771 return;
10772 }
10773 l = argvars[0].vval.v_list;
10774 if (l != NULL)
10775 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010776 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010777 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010778 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010779 int error = FALSE;
10780
Bram Moolenaar758711c2005-02-02 23:11:38 +000010781 /* Start at specified item. Use the cached index that list_find()
10782 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010783 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000010784 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010785 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010786 ic = get_tv_number_chk(&argvars[3], &error);
10787 if (error)
10788 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010789 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010790
Bram Moolenaar758711c2005-02-02 23:11:38 +000010791 for ( ; item != NULL; item = item->li_next, ++idx)
10792 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010793 {
10794 rettv->vval.v_number = idx;
10795 break;
10796 }
10797 }
10798}
10799
Bram Moolenaar071d4272004-06-13 20:20:40 +000010800static int inputsecret_flag = 0;
10801
10802/*
10803 * "input()" function
10804 * Also handles inputsecret() when inputsecret is set.
10805 */
10806 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010807f_input(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010808 typval_T *argvars;
10809 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010810{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010811 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010812 char_u *p = NULL;
10813 int c;
10814 char_u buf[NUMBUFLEN];
10815 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010816 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010817 int xp_type = EXPAND_NOTHING;
10818 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010819
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010820 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010821
10822#ifdef NO_CONSOLE_INPUT
10823 /* While starting up, there is no place to enter text. */
10824 if (no_console_input())
10825 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010826 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010827 return;
10828 }
10829#endif
10830
10831 cmd_silent = FALSE; /* Want to see the prompt. */
10832 if (prompt != NULL)
10833 {
10834 /* Only the part of the message after the last NL is considered as
10835 * prompt for the command line */
10836 p = vim_strrchr(prompt, '\n');
10837 if (p == NULL)
10838 p = prompt;
10839 else
10840 {
10841 ++p;
10842 c = *p;
10843 *p = NUL;
10844 msg_start();
10845 msg_clr_eos();
10846 msg_puts_attr(prompt, echo_attr);
10847 msg_didout = FALSE;
10848 msg_starthere();
10849 *p = c;
10850 }
10851 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010852
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010853 if (argvars[1].v_type != VAR_UNKNOWN)
10854 {
10855 defstr = get_tv_string_buf_chk(&argvars[1], buf);
10856 if (defstr != NULL)
10857 stuffReadbuffSpec(defstr);
10858 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010859
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010860 if (argvars[2].v_type != VAR_UNKNOWN)
10861 {
10862 char_u *xp_name;
10863 int xp_namelen;
10864 long argt;
10865
10866 rettv->vval.v_string = NULL;
10867
10868 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
10869 if (xp_name == NULL)
10870 return;
10871
10872 xp_namelen = STRLEN(xp_name);
10873
10874 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt, &xp_arg)
10875 == FAIL)
10876 return;
10877 }
10878
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010879 if (defstr != NULL)
10880 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010881 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
10882 xp_type, xp_arg);
10883
10884 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010885
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010886 /* since the user typed this, no need to wait for return */
10887 need_wait_return = FALSE;
10888 msg_didout = FALSE;
10889 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010890 cmd_silent = cmd_silent_save;
10891}
10892
10893/*
10894 * "inputdialog()" function
10895 */
10896 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010897f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010898 typval_T *argvars;
10899 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010900{
10901#if defined(FEAT_GUI_TEXTDIALOG)
10902 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
10903 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
10904 {
10905 char_u *message;
10906 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010907 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000010908
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010909 message = get_tv_string_chk(&argvars[0]);
10910 if (argvars[1].v_type != VAR_UNKNOWN
10911 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000010912 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010913 else
10914 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010915 if (message != NULL && defstr != NULL
10916 && do_dialog(VIM_QUESTION, NULL, message,
10917 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010918 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010919 else
10920 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010921 if (message != NULL && defstr != NULL
10922 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010923 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010924 rettv->vval.v_string = vim_strsave(
10925 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010926 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010927 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010928 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010929 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010930 }
10931 else
10932#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010933 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010934}
10935
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000010936/*
10937 * "inputlist()" function
10938 */
10939 static void
10940f_inputlist(argvars, rettv)
10941 typval_T *argvars;
10942 typval_T *rettv;
10943{
10944 listitem_T *li;
10945 int selected;
10946 int mouse_used;
10947
10948 rettv->vval.v_number = 0;
10949#ifdef NO_CONSOLE_INPUT
10950 /* While starting up, there is no place to enter text. */
10951 if (no_console_input())
10952 return;
10953#endif
10954 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
10955 {
10956 EMSG2(_(e_listarg), "inputlist()");
10957 return;
10958 }
10959
10960 msg_start();
10961 lines_left = Rows; /* avoid more prompt */
10962 msg_scroll = TRUE;
10963 msg_clr_eos();
10964
10965 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
10966 {
10967 msg_puts(get_tv_string(&li->li_tv));
10968 msg_putchar('\n');
10969 }
10970
10971 /* Ask for choice. */
10972 selected = prompt_for_number(&mouse_used);
10973 if (mouse_used)
10974 selected -= lines_left;
10975
10976 rettv->vval.v_number = selected;
10977}
10978
10979
Bram Moolenaar071d4272004-06-13 20:20:40 +000010980static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
10981
10982/*
10983 * "inputrestore()" function
10984 */
10985/*ARGSUSED*/
10986 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010987f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010988 typval_T *argvars;
10989 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010990{
10991 if (ga_userinput.ga_len > 0)
10992 {
10993 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010994 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
10995 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010996 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010997 }
10998 else if (p_verbose > 1)
10999 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000011000 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011001 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011002 }
11003}
11004
11005/*
11006 * "inputsave()" function
11007 */
11008/*ARGSUSED*/
11009 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011010f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011011 typval_T *argvars;
11012 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011013{
11014 /* Add an entry to the stack of typehead storage. */
11015 if (ga_grow(&ga_userinput, 1) == OK)
11016 {
11017 save_typeahead((tasave_T *)(ga_userinput.ga_data)
11018 + ga_userinput.ga_len);
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
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011023 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011024}
11025
11026/*
11027 * "inputsecret()" function
11028 */
11029 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011030f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011031 typval_T *argvars;
11032 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011033{
11034 ++cmdline_star;
11035 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011036 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011037 --cmdline_star;
11038 --inputsecret_flag;
11039}
11040
11041/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011042 * "insert()" function
11043 */
11044 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011045f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011046 typval_T *argvars;
11047 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011048{
11049 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011050 listitem_T *item;
11051 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011052 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011053
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011054 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011055 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011056 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011057 else if ((l = argvars[0].vval.v_list) != NULL
11058 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011059 {
11060 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011061 before = get_tv_number_chk(&argvars[2], &error);
11062 if (error)
11063 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011064
Bram Moolenaar758711c2005-02-02 23:11:38 +000011065 if (before == l->lv_len)
11066 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011067 else
11068 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011069 item = list_find(l, before);
11070 if (item == NULL)
11071 {
11072 EMSGN(_(e_listidx), before);
11073 l = NULL;
11074 }
11075 }
11076 if (l != NULL)
11077 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011078 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011079 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011080 }
11081 }
11082}
11083
11084/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011085 * "isdirectory()" function
11086 */
11087 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011088f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011089 typval_T *argvars;
11090 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011091{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011092 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011093}
11094
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011095/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011096 * "islocked()" function
11097 */
11098 static void
11099f_islocked(argvars, rettv)
11100 typval_T *argvars;
11101 typval_T *rettv;
11102{
11103 lval_T lv;
11104 char_u *end;
11105 dictitem_T *di;
11106
11107 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000011108 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
11109 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011110 if (end != NULL && lv.ll_name != NULL)
11111 {
11112 if (*end != NUL)
11113 EMSG(_(e_trailing));
11114 else
11115 {
11116 if (lv.ll_tv == NULL)
11117 {
11118 if (check_changedtick(lv.ll_name))
11119 rettv->vval.v_number = 1; /* always locked */
11120 else
11121 {
11122 di = find_var(lv.ll_name, NULL);
11123 if (di != NULL)
11124 {
11125 /* Consider a variable locked when:
11126 * 1. the variable itself is locked
11127 * 2. the value of the variable is locked.
11128 * 3. the List or Dict value is locked.
11129 */
11130 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
11131 || tv_islocked(&di->di_tv));
11132 }
11133 }
11134 }
11135 else if (lv.ll_range)
11136 EMSG(_("E745: Range not allowed"));
11137 else if (lv.ll_newkey != NULL)
11138 EMSG2(_(e_dictkey), lv.ll_newkey);
11139 else if (lv.ll_list != NULL)
11140 /* List item. */
11141 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
11142 else
11143 /* Dictionary item. */
11144 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
11145 }
11146 }
11147
11148 clear_lval(&lv);
11149}
11150
Bram Moolenaar33570922005-01-25 22:26:29 +000011151static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011152
11153/*
11154 * Turn a dict into a list:
11155 * "what" == 0: list of keys
11156 * "what" == 1: list of values
11157 * "what" == 2: list of items
11158 */
11159 static void
11160dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000011161 typval_T *argvars;
11162 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011163 int what;
11164{
Bram Moolenaar33570922005-01-25 22:26:29 +000011165 list_T *l;
11166 list_T *l2;
11167 dictitem_T *di;
11168 hashitem_T *hi;
11169 listitem_T *li;
11170 listitem_T *li2;
11171 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011172 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011173
11174 rettv->vval.v_number = 0;
11175 if (argvars[0].v_type != VAR_DICT)
11176 {
11177 EMSG(_(e_dictreq));
11178 return;
11179 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011180 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011181 return;
11182
11183 l = list_alloc();
11184 if (l == NULL)
11185 return;
11186 rettv->v_type = VAR_LIST;
11187 rettv->vval.v_list = l;
11188 ++l->lv_refcount;
11189
Bram Moolenaar33570922005-01-25 22:26:29 +000011190 todo = d->dv_hashtab.ht_used;
11191 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011192 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011193 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011194 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011195 --todo;
11196 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011197
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011198 li = listitem_alloc();
11199 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011200 break;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011201 list_append(l, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011202
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011203 if (what == 0)
11204 {
11205 /* keys() */
11206 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011207 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011208 li->li_tv.vval.v_string = vim_strsave(di->di_key);
11209 }
11210 else if (what == 1)
11211 {
11212 /* values() */
11213 copy_tv(&di->di_tv, &li->li_tv);
11214 }
11215 else
11216 {
11217 /* items() */
11218 l2 = list_alloc();
11219 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011220 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011221 li->li_tv.vval.v_list = l2;
11222 if (l2 == NULL)
11223 break;
11224 ++l2->lv_refcount;
11225
11226 li2 = listitem_alloc();
11227 if (li2 == NULL)
11228 break;
11229 list_append(l2, li2);
11230 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011231 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011232 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
11233
11234 li2 = listitem_alloc();
11235 if (li2 == NULL)
11236 break;
11237 list_append(l2, li2);
11238 copy_tv(&di->di_tv, &li2->li_tv);
11239 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011240 }
11241 }
11242}
11243
11244/*
11245 * "items(dict)" function
11246 */
11247 static void
11248f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011249 typval_T *argvars;
11250 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011251{
11252 dict_list(argvars, rettv, 2);
11253}
11254
Bram Moolenaar071d4272004-06-13 20:20:40 +000011255/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011256 * "join()" function
11257 */
11258 static void
11259f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011260 typval_T *argvars;
11261 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011262{
11263 garray_T ga;
11264 char_u *sep;
11265
11266 rettv->vval.v_number = 0;
11267 if (argvars[0].v_type != VAR_LIST)
11268 {
11269 EMSG(_(e_listreq));
11270 return;
11271 }
11272 if (argvars[0].vval.v_list == NULL)
11273 return;
11274 if (argvars[1].v_type == VAR_UNKNOWN)
11275 sep = (char_u *)" ";
11276 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011277 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011278
11279 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011280
11281 if (sep != NULL)
11282 {
11283 ga_init2(&ga, (int)sizeof(char), 80);
11284 list_join(&ga, argvars[0].vval.v_list, sep, TRUE);
11285 ga_append(&ga, NUL);
11286 rettv->vval.v_string = (char_u *)ga.ga_data;
11287 }
11288 else
11289 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011290}
11291
11292/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011293 * "keys()" function
11294 */
11295 static void
11296f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011297 typval_T *argvars;
11298 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011299{
11300 dict_list(argvars, rettv, 0);
11301}
11302
11303/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011304 * "last_buffer_nr()" function.
11305 */
11306/*ARGSUSED*/
11307 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011308f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011309 typval_T *argvars;
11310 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011311{
11312 int n = 0;
11313 buf_T *buf;
11314
11315 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
11316 if (n < buf->b_fnum)
11317 n = buf->b_fnum;
11318
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011319 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011320}
11321
11322/*
11323 * "len()" function
11324 */
11325 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011326f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011327 typval_T *argvars;
11328 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011329{
11330 switch (argvars[0].v_type)
11331 {
11332 case VAR_STRING:
11333 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011334 rettv->vval.v_number = (varnumber_T)STRLEN(
11335 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011336 break;
11337 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011338 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011339 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011340 case VAR_DICT:
11341 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
11342 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011343 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011344 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011345 break;
11346 }
11347}
11348
Bram Moolenaar33570922005-01-25 22:26:29 +000011349static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011350
11351 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011352libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011353 typval_T *argvars;
11354 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011355 int type;
11356{
11357#ifdef FEAT_LIBCALL
11358 char_u *string_in;
11359 char_u **string_result;
11360 int nr_result;
11361#endif
11362
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011363 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011364 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011365 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011366 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011367 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011368
11369 if (check_restricted() || check_secure())
11370 return;
11371
11372#ifdef FEAT_LIBCALL
11373 /* The first two args must be strings, otherwise its meaningless */
11374 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
11375 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011376 string_in = NULL;
11377 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011378 string_in = argvars[2].vval.v_string;
11379 if (type == VAR_NUMBER)
11380 string_result = NULL;
11381 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011382 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011383 if (mch_libcall(argvars[0].vval.v_string,
11384 argvars[1].vval.v_string,
11385 string_in,
11386 argvars[2].vval.v_number,
11387 string_result,
11388 &nr_result) == OK
11389 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011390 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011391 }
11392#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011393}
11394
11395/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011396 * "libcall()" function
11397 */
11398 static void
11399f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011400 typval_T *argvars;
11401 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011402{
11403 libcall_common(argvars, rettv, VAR_STRING);
11404}
11405
11406/*
11407 * "libcallnr()" function
11408 */
11409 static void
11410f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011411 typval_T *argvars;
11412 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011413{
11414 libcall_common(argvars, rettv, VAR_NUMBER);
11415}
11416
11417/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011418 * "line(string)" function
11419 */
11420 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011421f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011422 typval_T *argvars;
11423 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011424{
11425 linenr_T lnum = 0;
11426 pos_T *fp;
11427
11428 fp = var2fpos(&argvars[0], TRUE);
11429 if (fp != NULL)
11430 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011431 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011432}
11433
11434/*
11435 * "line2byte(lnum)" function
11436 */
11437/*ARGSUSED*/
11438 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011439f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011440 typval_T *argvars;
11441 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011442{
11443#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011444 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011445#else
11446 linenr_T lnum;
11447
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011448 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011449 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011450 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011451 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011452 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
11453 if (rettv->vval.v_number >= 0)
11454 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011455#endif
11456}
11457
11458/*
11459 * "lispindent(lnum)" function
11460 */
11461 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011462f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011463 typval_T *argvars;
11464 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011465{
11466#ifdef FEAT_LISP
11467 pos_T pos;
11468 linenr_T lnum;
11469
11470 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011471 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011472 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11473 {
11474 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011475 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011476 curwin->w_cursor = pos;
11477 }
11478 else
11479#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011480 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011481}
11482
11483/*
11484 * "localtime()" function
11485 */
11486/*ARGSUSED*/
11487 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011488f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011489 typval_T *argvars;
11490 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011491{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011492 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011493}
11494
Bram Moolenaar33570922005-01-25 22:26:29 +000011495static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011496
11497 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011498get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000011499 typval_T *argvars;
11500 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011501 int exact;
11502{
11503 char_u *keys;
11504 char_u *which;
11505 char_u buf[NUMBUFLEN];
11506 char_u *keys_buf = NULL;
11507 char_u *rhs;
11508 int mode;
11509 garray_T ga;
11510
11511 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011512 rettv->v_type = VAR_STRING;
11513 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011514
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011515 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011516 if (*keys == NUL)
11517 return;
11518
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011519 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011520 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011521 else
11522 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011523 if (which == NULL)
11524 return;
11525
Bram Moolenaar071d4272004-06-13 20:20:40 +000011526 mode = get_map_mode(&which, 0);
11527
11528 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000011529 rhs = check_map(keys, mode, exact, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011530 vim_free(keys_buf);
11531 if (rhs != NULL)
11532 {
11533 ga_init(&ga);
11534 ga.ga_itemsize = 1;
11535 ga.ga_growsize = 40;
11536
11537 while (*rhs != NUL)
11538 ga_concat(&ga, str2special(&rhs, FALSE));
11539
11540 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011541 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011542 }
11543}
11544
11545/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011546 * "map()" function
11547 */
11548 static void
11549f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011550 typval_T *argvars;
11551 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011552{
11553 filter_map(argvars, rettv, TRUE);
11554}
11555
11556/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011557 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011558 */
11559 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011560f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011561 typval_T *argvars;
11562 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011563{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011564 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011565}
11566
11567/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011568 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011569 */
11570 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011571f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011572 typval_T *argvars;
11573 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011574{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011575 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011576}
11577
Bram Moolenaar33570922005-01-25 22:26:29 +000011578static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011579
11580 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011581find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011582 typval_T *argvars;
11583 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011584 int type;
11585{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011586 char_u *str = NULL;
11587 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011588 char_u *pat;
11589 regmatch_T regmatch;
11590 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011591 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011592 char_u *save_cpo;
11593 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011594 long nth = 1;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011595 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011596 list_T *l = NULL;
11597 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011598 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011599 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011600
11601 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11602 save_cpo = p_cpo;
11603 p_cpo = (char_u *)"";
11604
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011605 rettv->vval.v_number = -1;
11606 if (type == 3)
11607 {
11608 /* return empty list when there are no matches */
11609 if ((rettv->vval.v_list = list_alloc()) == NULL)
11610 goto theend;
11611 rettv->v_type = VAR_LIST;
11612 ++rettv->vval.v_list->lv_refcount;
11613 }
11614 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011615 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011616 rettv->v_type = VAR_STRING;
11617 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011618 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011619
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011620 if (argvars[0].v_type == VAR_LIST)
11621 {
11622 if ((l = argvars[0].vval.v_list) == NULL)
11623 goto theend;
11624 li = l->lv_first;
11625 }
11626 else
11627 expr = str = get_tv_string(&argvars[0]);
11628
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011629 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11630 if (pat == NULL)
11631 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011632
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011633 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011634 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011635 int error = FALSE;
11636
11637 start = get_tv_number_chk(&argvars[2], &error);
11638 if (error)
11639 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011640 if (l != NULL)
11641 {
11642 li = list_find(l, start);
11643 if (li == NULL)
11644 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000011645 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011646 }
11647 else
11648 {
11649 if (start < 0)
11650 start = 0;
11651 if (start > (long)STRLEN(str))
11652 goto theend;
11653 str += start;
11654 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011655
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011656 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011657 nth = get_tv_number_chk(&argvars[3], &error);
11658 if (error)
11659 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011660 }
11661
11662 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11663 if (regmatch.regprog != NULL)
11664 {
11665 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011666
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000011667 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011668 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011669 if (l != NULL)
11670 {
11671 if (li == NULL)
11672 {
11673 match = FALSE;
11674 break;
11675 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011676 vim_free(tofree);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011677 str = echo_string(&li->li_tv, &tofree, strbuf);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011678 if (str == NULL)
11679 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011680 }
11681
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011682 match = vim_regexec_nl(&regmatch, str, (colnr_T)0);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011683
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011684 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011685 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011686 if (l == NULL && !match)
11687 break;
11688
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011689 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011690 if (l != NULL)
11691 {
11692 li = li->li_next;
11693 ++idx;
11694 }
11695 else
11696 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011697#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011698 str = regmatch.startp[0] + (*mb_ptr2len)(regmatch.startp[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011699#else
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011700 str = regmatch.startp[0] + 1;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011701#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011702 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011703 }
11704
11705 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011706 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011707 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011708 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011709 int i;
11710
11711 /* return list with matched string and submatches */
11712 for (i = 0; i < NSUBEXP; ++i)
11713 {
11714 if (regmatch.endp[i] == NULL)
11715 break;
11716 li = listitem_alloc();
11717 if (li == NULL)
11718 break;
11719 li->li_tv.v_type = VAR_STRING;
11720 li->li_tv.v_lock = 0;
11721 li->li_tv.vval.v_string = vim_strnsave(regmatch.startp[i],
11722 (int)(regmatch.endp[i] - regmatch.startp[i]));
11723 list_append(rettv->vval.v_list, li);
11724 }
11725 }
11726 else if (type == 2)
11727 {
11728 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011729 if (l != NULL)
11730 copy_tv(&li->li_tv, rettv);
11731 else
11732 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000011733 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011734 }
11735 else if (l != NULL)
11736 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011737 else
11738 {
11739 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011740 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011741 (varnumber_T)(regmatch.startp[0] - str);
11742 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011743 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011744 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011745 rettv->vval.v_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011746 }
11747 }
11748 vim_free(regmatch.regprog);
11749 }
11750
11751theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011752 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011753 p_cpo = save_cpo;
11754}
11755
11756/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011757 * "match()" function
11758 */
11759 static void
11760f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011761 typval_T *argvars;
11762 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011763{
11764 find_some_match(argvars, rettv, 1);
11765}
11766
11767/*
11768 * "matchend()" function
11769 */
11770 static void
11771f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011772 typval_T *argvars;
11773 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011774{
11775 find_some_match(argvars, rettv, 0);
11776}
11777
11778/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011779 * "matchlist()" function
11780 */
11781 static void
11782f_matchlist(argvars, rettv)
11783 typval_T *argvars;
11784 typval_T *rettv;
11785{
11786 find_some_match(argvars, rettv, 3);
11787}
11788
11789/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011790 * "matchstr()" function
11791 */
11792 static void
11793f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011794 typval_T *argvars;
11795 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011796{
11797 find_some_match(argvars, rettv, 2);
11798}
11799
Bram Moolenaar33570922005-01-25 22:26:29 +000011800static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011801
11802 static void
11803max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000011804 typval_T *argvars;
11805 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011806 int domax;
11807{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011808 long n = 0;
11809 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011810 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011811
11812 if (argvars[0].v_type == VAR_LIST)
11813 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011814 list_T *l;
11815 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011816
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011817 l = argvars[0].vval.v_list;
11818 if (l != NULL)
11819 {
11820 li = l->lv_first;
11821 if (li != NULL)
11822 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011823 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000011824 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011825 {
11826 li = li->li_next;
11827 if (li == NULL)
11828 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011829 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011830 if (domax ? i > n : i < n)
11831 n = i;
11832 }
11833 }
11834 }
11835 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011836 else if (argvars[0].v_type == VAR_DICT)
11837 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011838 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011839 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000011840 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011841 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011842
11843 d = argvars[0].vval.v_dict;
11844 if (d != NULL)
11845 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011846 todo = d->dv_hashtab.ht_used;
11847 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011848 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011849 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011850 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011851 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011852 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011853 if (first)
11854 {
11855 n = i;
11856 first = FALSE;
11857 }
11858 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011859 n = i;
11860 }
11861 }
11862 }
11863 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011864 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000011865 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011866 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011867}
11868
11869/*
11870 * "max()" function
11871 */
11872 static void
11873f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011874 typval_T *argvars;
11875 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011876{
11877 max_min(argvars, rettv, TRUE);
11878}
11879
11880/*
11881 * "min()" function
11882 */
11883 static void
11884f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011885 typval_T *argvars;
11886 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011887{
11888 max_min(argvars, rettv, FALSE);
11889}
11890
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011891static int mkdir_recurse __ARGS((char_u *dir, int prot));
11892
11893/*
11894 * Create the directory in which "dir" is located, and higher levels when
11895 * needed.
11896 */
11897 static int
11898mkdir_recurse(dir, prot)
11899 char_u *dir;
11900 int prot;
11901{
11902 char_u *p;
11903 char_u *updir;
11904 int r = FAIL;
11905
11906 /* Get end of directory name in "dir".
11907 * We're done when it's "/" or "c:/". */
11908 p = gettail_sep(dir);
11909 if (p <= get_past_head(dir))
11910 return OK;
11911
11912 /* If the directory exists we're done. Otherwise: create it.*/
11913 updir = vim_strnsave(dir, (int)(p - dir));
11914 if (updir == NULL)
11915 return FAIL;
11916 if (mch_isdir(updir))
11917 r = OK;
11918 else if (mkdir_recurse(updir, prot) == OK)
11919 r = vim_mkdir_emsg(updir, prot);
11920 vim_free(updir);
11921 return r;
11922}
11923
11924#ifdef vim_mkdir
11925/*
11926 * "mkdir()" function
11927 */
11928 static void
11929f_mkdir(argvars, rettv)
11930 typval_T *argvars;
11931 typval_T *rettv;
11932{
11933 char_u *dir;
11934 char_u buf[NUMBUFLEN];
11935 int prot = 0755;
11936
11937 rettv->vval.v_number = FAIL;
11938 if (check_restricted() || check_secure())
11939 return;
11940
11941 dir = get_tv_string_buf(&argvars[0], buf);
11942 if (argvars[1].v_type != VAR_UNKNOWN)
11943 {
11944 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011945 prot = get_tv_number_chk(&argvars[2], NULL);
11946 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011947 mkdir_recurse(dir, prot);
11948 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011949 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011950}
11951#endif
11952
Bram Moolenaar0d660222005-01-07 21:51:51 +000011953/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011954 * "mode()" function
11955 */
11956/*ARGSUSED*/
11957 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011958f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011959 typval_T *argvars;
11960 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011961{
11962 char_u buf[2];
11963
11964#ifdef FEAT_VISUAL
11965 if (VIsual_active)
11966 {
11967 if (VIsual_select)
11968 buf[0] = VIsual_mode + 's' - 'v';
11969 else
11970 buf[0] = VIsual_mode;
11971 }
11972 else
11973#endif
11974 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
11975 buf[0] = 'r';
11976 else if (State & INSERT)
11977 {
11978 if (State & REPLACE_FLAG)
11979 buf[0] = 'R';
11980 else
11981 buf[0] = 'i';
11982 }
11983 else if (State & CMDLINE)
11984 buf[0] = 'c';
11985 else
11986 buf[0] = 'n';
11987
11988 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011989 rettv->vval.v_string = vim_strsave(buf);
11990 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011991}
11992
11993/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011994 * "nextnonblank()" function
11995 */
11996 static void
11997f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011998 typval_T *argvars;
11999 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012000{
12001 linenr_T lnum;
12002
12003 for (lnum = get_tv_lnum(argvars); ; ++lnum)
12004 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012005 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012006 {
12007 lnum = 0;
12008 break;
12009 }
12010 if (*skipwhite(ml_get(lnum)) != NUL)
12011 break;
12012 }
12013 rettv->vval.v_number = lnum;
12014}
12015
12016/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012017 * "nr2char()" function
12018 */
12019 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012020f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012021 typval_T *argvars;
12022 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012023{
12024 char_u buf[NUMBUFLEN];
12025
12026#ifdef FEAT_MBYTE
12027 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012028 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012029 else
12030#endif
12031 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012032 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012033 buf[1] = NUL;
12034 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012035 rettv->v_type = VAR_STRING;
12036 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012037}
12038
12039/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012040 * "prevnonblank()" function
12041 */
12042 static void
12043f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012044 typval_T *argvars;
12045 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012046{
12047 linenr_T lnum;
12048
12049 lnum = get_tv_lnum(argvars);
12050 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
12051 lnum = 0;
12052 else
12053 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
12054 --lnum;
12055 rettv->vval.v_number = lnum;
12056}
12057
Bram Moolenaara6c840d2005-08-22 22:59:46 +000012058#ifdef HAVE_STDARG_H
12059/* This dummy va_list is here because:
12060 * - passing a NULL pointer doesn't work when va_list isn't a pointer
12061 * - locally in the function results in a "used before set" warning
12062 * - using va_start() to initialize it gives "function with fixed args" error */
12063static va_list ap;
12064#endif
12065
Bram Moolenaar8c711452005-01-14 21:53:12 +000012066/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012067 * "printf()" function
12068 */
12069 static void
12070f_printf(argvars, rettv)
12071 typval_T *argvars;
12072 typval_T *rettv;
12073{
12074 rettv->v_type = VAR_STRING;
12075 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000012076#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012077 {
12078 char_u buf[NUMBUFLEN];
12079 int len;
12080 char_u *s;
12081 int saved_did_emsg = did_emsg;
12082 char *fmt;
12083
12084 /* Get the required length, allocate the buffer and do it for real. */
12085 did_emsg = FALSE;
12086 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012087 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012088 if (!did_emsg)
12089 {
12090 s = alloc(len + 1);
12091 if (s != NULL)
12092 {
12093 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012094 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012095 }
12096 }
12097 did_emsg |= saved_did_emsg;
12098 }
12099#endif
12100}
12101
12102/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012103 * "range()" function
12104 */
12105 static void
12106f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012107 typval_T *argvars;
12108 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012109{
12110 long start;
12111 long end;
12112 long stride = 1;
12113 long i;
Bram Moolenaar33570922005-01-25 22:26:29 +000012114 list_T *l;
12115 listitem_T *li;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012116 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012117
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012118 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012119 if (argvars[1].v_type == VAR_UNKNOWN)
12120 {
12121 end = start - 1;
12122 start = 0;
12123 }
12124 else
12125 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012126 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012127 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012128 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012129 }
12130
12131 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012132 if (error)
12133 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000012134 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012135 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000012136 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012137 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012138 else
12139 {
12140 l = list_alloc();
12141 if (l != NULL)
12142 {
12143 rettv->v_type = VAR_LIST;
12144 rettv->vval.v_list = l;
12145 ++l->lv_refcount;
12146
12147 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
12148 {
12149 li = listitem_alloc();
12150 if (li == NULL)
12151 break;
12152 li->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012153 li->li_tv.v_lock = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012154 li->li_tv.vval.v_number = i;
12155 list_append(l, li);
12156 }
12157 }
12158 }
12159}
12160
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012161/*
12162 * "readfile()" function
12163 */
12164 static void
12165f_readfile(argvars, rettv)
12166 typval_T *argvars;
12167 typval_T *rettv;
12168{
12169 int binary = FALSE;
12170 char_u *fname;
12171 FILE *fd;
12172 list_T *l;
12173 listitem_T *li;
12174#define FREAD_SIZE 200 /* optimized for text lines */
12175 char_u buf[FREAD_SIZE];
12176 int readlen; /* size of last fread() */
12177 int buflen; /* nr of valid chars in buf[] */
12178 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
12179 int tolist; /* first byte in buf[] still to be put in list */
12180 int chop; /* how many CR to chop off */
12181 char_u *prev = NULL; /* previously read bytes, if any */
12182 int prevlen = 0; /* length of "prev" if not NULL */
12183 char_u *s;
12184 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012185 long maxline = MAXLNUM;
12186 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012187
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012188 if (argvars[1].v_type != VAR_UNKNOWN)
12189 {
12190 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
12191 binary = TRUE;
12192 if (argvars[2].v_type != VAR_UNKNOWN)
12193 maxline = get_tv_number(&argvars[2]);
12194 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012195
12196 l = list_alloc();
12197 if (l == NULL)
12198 return;
12199 rettv->v_type = VAR_LIST;
12200 rettv->vval.v_list = l;
12201 l->lv_refcount = 1;
12202
12203 /* Always open the file in binary mode, library functions have a mind of
12204 * their own about CR-LF conversion. */
12205 fname = get_tv_string(&argvars[0]);
12206 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
12207 {
12208 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
12209 return;
12210 }
12211
12212 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012213 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012214 {
12215 readlen = fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
12216 buflen = filtd + readlen;
12217 tolist = 0;
12218 for ( ; filtd < buflen || readlen <= 0; ++filtd)
12219 {
12220 if (buf[filtd] == '\n' || readlen <= 0)
12221 {
12222 /* Only when in binary mode add an empty list item when the
12223 * last line ends in a '\n'. */
12224 if (!binary && readlen == 0 && filtd == 0)
12225 break;
12226
12227 /* Found end-of-line or end-of-file: add a text line to the
12228 * list. */
12229 chop = 0;
12230 if (!binary)
12231 while (filtd - chop - 1 >= tolist
12232 && buf[filtd - chop - 1] == '\r')
12233 ++chop;
12234 len = filtd - tolist - chop;
12235 if (prev == NULL)
12236 s = vim_strnsave(buf + tolist, len);
12237 else
12238 {
12239 s = alloc((unsigned)(prevlen + len + 1));
12240 if (s != NULL)
12241 {
12242 mch_memmove(s, prev, prevlen);
12243 vim_free(prev);
12244 prev = NULL;
12245 mch_memmove(s + prevlen, buf + tolist, len);
12246 s[prevlen + len] = NUL;
12247 }
12248 }
12249 tolist = filtd + 1;
12250
12251 li = listitem_alloc();
12252 if (li == NULL)
12253 {
12254 vim_free(s);
12255 break;
12256 }
12257 li->li_tv.v_type = VAR_STRING;
12258 li->li_tv.v_lock = 0;
12259 li->li_tv.vval.v_string = s;
12260 list_append(l, li);
12261
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012262 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012263 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012264 if (readlen <= 0)
12265 break;
12266 }
12267 else if (buf[filtd] == NUL)
12268 buf[filtd] = '\n';
12269 }
12270 if (readlen <= 0)
12271 break;
12272
12273 if (tolist == 0)
12274 {
12275 /* "buf" is full, need to move text to an allocated buffer */
12276 if (prev == NULL)
12277 {
12278 prev = vim_strnsave(buf, buflen);
12279 prevlen = buflen;
12280 }
12281 else
12282 {
12283 s = alloc((unsigned)(prevlen + buflen));
12284 if (s != NULL)
12285 {
12286 mch_memmove(s, prev, prevlen);
12287 mch_memmove(s + prevlen, buf, buflen);
12288 vim_free(prev);
12289 prev = s;
12290 prevlen += buflen;
12291 }
12292 }
12293 filtd = 0;
12294 }
12295 else
12296 {
12297 mch_memmove(buf, buf + tolist, buflen - tolist);
12298 filtd -= tolist;
12299 }
12300 }
12301
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012302 /*
12303 * For a negative line count use only the lines at the end of the file,
12304 * free the rest.
12305 */
12306 if (maxline < 0)
12307 while (cnt > -maxline)
12308 {
12309 listitem_remove(l, l->lv_first);
12310 --cnt;
12311 }
12312
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012313 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012314 fclose(fd);
12315}
12316
12317
Bram Moolenaar0d660222005-01-07 21:51:51 +000012318#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
12319static void make_connection __ARGS((void));
12320static int check_connection __ARGS((void));
12321
12322 static void
12323make_connection()
12324{
12325 if (X_DISPLAY == NULL
12326# ifdef FEAT_GUI
12327 && !gui.in_use
12328# endif
12329 )
12330 {
12331 x_force_connect = TRUE;
12332 setup_term_clip();
12333 x_force_connect = FALSE;
12334 }
12335}
12336
12337 static int
12338check_connection()
12339{
12340 make_connection();
12341 if (X_DISPLAY == NULL)
12342 {
12343 EMSG(_("E240: No connection to Vim server"));
12344 return FAIL;
12345 }
12346 return OK;
12347}
12348#endif
12349
12350#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012351static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012352
12353 static void
12354remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000012355 typval_T *argvars;
12356 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012357 int expr;
12358{
12359 char_u *server_name;
12360 char_u *keys;
12361 char_u *r = NULL;
12362 char_u buf[NUMBUFLEN];
12363# ifdef WIN32
12364 HWND w;
12365# else
12366 Window w;
12367# endif
12368
12369 if (check_restricted() || check_secure())
12370 return;
12371
12372# ifdef FEAT_X11
12373 if (check_connection() == FAIL)
12374 return;
12375# endif
12376
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012377 server_name = get_tv_string_chk(&argvars[0]);
12378 if (server_name == NULL)
12379 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012380 keys = get_tv_string_buf(&argvars[1], buf);
12381# ifdef WIN32
12382 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
12383# else
12384 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
12385 < 0)
12386# endif
12387 {
12388 if (r != NULL)
12389 EMSG(r); /* sending worked but evaluation failed */
12390 else
12391 EMSG2(_("E241: Unable to send to %s"), server_name);
12392 return;
12393 }
12394
12395 rettv->vval.v_string = r;
12396
12397 if (argvars[2].v_type != VAR_UNKNOWN)
12398 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012399 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000012400 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012401 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012402
12403 sprintf((char *)str, "0x%x", (unsigned int)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000012404 v.di_tv.v_type = VAR_STRING;
12405 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012406 idvar = get_tv_string_chk(&argvars[2]);
12407 if (idvar != NULL)
12408 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012409 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012410 }
12411}
12412#endif
12413
12414/*
12415 * "remote_expr()" function
12416 */
12417/*ARGSUSED*/
12418 static void
12419f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012420 typval_T *argvars;
12421 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012422{
12423 rettv->v_type = VAR_STRING;
12424 rettv->vval.v_string = NULL;
12425#ifdef FEAT_CLIENTSERVER
12426 remote_common(argvars, rettv, TRUE);
12427#endif
12428}
12429
12430/*
12431 * "remote_foreground()" function
12432 */
12433/*ARGSUSED*/
12434 static void
12435f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012436 typval_T *argvars;
12437 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012438{
12439 rettv->vval.v_number = 0;
12440#ifdef FEAT_CLIENTSERVER
12441# ifdef WIN32
12442 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012443 {
12444 char_u *server_name = get_tv_string_chk(&argvars[0]);
12445
12446 if (server_name != NULL)
12447 serverForeground(server_name);
12448 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012449# else
12450 /* Send a foreground() expression to the server. */
12451 argvars[1].v_type = VAR_STRING;
12452 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
12453 argvars[2].v_type = VAR_UNKNOWN;
12454 remote_common(argvars, rettv, TRUE);
12455 vim_free(argvars[1].vval.v_string);
12456# endif
12457#endif
12458}
12459
12460/*ARGSUSED*/
12461 static void
12462f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012463 typval_T *argvars;
12464 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012465{
12466#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012467 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012468 char_u *s = NULL;
12469# ifdef WIN32
12470 int n = 0;
12471# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012472 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012473
12474 if (check_restricted() || check_secure())
12475 {
12476 rettv->vval.v_number = -1;
12477 return;
12478 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012479 serverid = get_tv_string_chk(&argvars[0]);
12480 if (serverid == NULL)
12481 {
12482 rettv->vval.v_number = -1;
12483 return; /* type error; errmsg already given */
12484 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012485# ifdef WIN32
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012486 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012487 if (n == 0)
12488 rettv->vval.v_number = -1;
12489 else
12490 {
12491 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
12492 rettv->vval.v_number = (s != NULL);
12493 }
12494# else
12495 rettv->vval.v_number = 0;
12496 if (check_connection() == FAIL)
12497 return;
12498
12499 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012500 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012501# endif
12502
12503 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
12504 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012505 char_u *retvar;
12506
Bram Moolenaar33570922005-01-25 22:26:29 +000012507 v.di_tv.v_type = VAR_STRING;
12508 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012509 retvar = get_tv_string_chk(&argvars[1]);
12510 if (retvar != NULL)
12511 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012512 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012513 }
12514#else
12515 rettv->vval.v_number = -1;
12516#endif
12517}
12518
12519/*ARGSUSED*/
12520 static void
12521f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012522 typval_T *argvars;
12523 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012524{
12525 char_u *r = NULL;
12526
12527#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012528 char_u *serverid = get_tv_string_chk(&argvars[0]);
12529
12530 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000012531 {
12532# ifdef WIN32
12533 /* The server's HWND is encoded in the 'id' parameter */
12534 int n = 0;
12535
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012536 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012537 if (n != 0)
12538 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
12539 if (r == NULL)
12540# else
12541 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012542 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012543# endif
12544 EMSG(_("E277: Unable to read a server reply"));
12545 }
12546#endif
12547 rettv->v_type = VAR_STRING;
12548 rettv->vval.v_string = r;
12549}
12550
12551/*
12552 * "remote_send()" function
12553 */
12554/*ARGSUSED*/
12555 static void
12556f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012557 typval_T *argvars;
12558 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012559{
12560 rettv->v_type = VAR_STRING;
12561 rettv->vval.v_string = NULL;
12562#ifdef FEAT_CLIENTSERVER
12563 remote_common(argvars, rettv, FALSE);
12564#endif
12565}
12566
12567/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012568 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012569 */
12570 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012571f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012572 typval_T *argvars;
12573 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012574{
Bram Moolenaar33570922005-01-25 22:26:29 +000012575 list_T *l;
12576 listitem_T *item, *item2;
12577 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012578 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012579 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012580 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000012581 dict_T *d;
12582 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012583
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012584 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012585 if (argvars[0].v_type == VAR_DICT)
12586 {
12587 if (argvars[2].v_type != VAR_UNKNOWN)
12588 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012589 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar758711c2005-02-02 23:11:38 +000012590 && !tv_check_lock(d->dv_lock, (char_u *)"remove()"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000012591 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012592 key = get_tv_string_chk(&argvars[1]);
12593 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012594 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012595 di = dict_find(d, key, -1);
12596 if (di == NULL)
12597 EMSG2(_(e_dictkey), key);
12598 else
12599 {
12600 *rettv = di->di_tv;
12601 init_tv(&di->di_tv);
12602 dictitem_remove(d, di);
12603 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012604 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012605 }
12606 }
12607 else if (argvars[0].v_type != VAR_LIST)
12608 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012609 else if ((l = argvars[0].vval.v_list) != NULL
12610 && !tv_check_lock(l->lv_lock, (char_u *)"remove()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012611 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012612 int error = FALSE;
12613
12614 idx = get_tv_number_chk(&argvars[1], &error);
12615 if (error)
12616 ; /* type error: do nothing, errmsg already given */
12617 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012618 EMSGN(_(e_listidx), idx);
12619 else
12620 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012621 if (argvars[2].v_type == VAR_UNKNOWN)
12622 {
12623 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012624 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012625 *rettv = item->li_tv;
12626 vim_free(item);
12627 }
12628 else
12629 {
12630 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012631 end = get_tv_number_chk(&argvars[2], &error);
12632 if (error)
12633 ; /* type error: do nothing */
12634 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012635 EMSGN(_(e_listidx), end);
12636 else
12637 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012638 int cnt = 0;
12639
12640 for (li = item; li != NULL; li = li->li_next)
12641 {
12642 ++cnt;
12643 if (li == item2)
12644 break;
12645 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012646 if (li == NULL) /* didn't find "item2" after "item" */
12647 EMSG(_(e_invrange));
12648 else
12649 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012650 list_remove(l, item, item2);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012651 l = list_alloc();
12652 if (l != NULL)
12653 {
12654 rettv->v_type = VAR_LIST;
12655 rettv->vval.v_list = l;
12656 l->lv_first = item;
12657 l->lv_last = item2;
12658 l->lv_refcount = 1;
12659 item->li_prev = NULL;
12660 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012661 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012662 }
12663 }
12664 }
12665 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012666 }
12667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012668}
12669
12670/*
12671 * "rename({from}, {to})" function
12672 */
12673 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012674f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012675 typval_T *argvars;
12676 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012677{
12678 char_u buf[NUMBUFLEN];
12679
12680 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012681 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012682 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012683 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
12684 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012685}
12686
12687/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012688 * "repeat()" function
12689 */
12690/*ARGSUSED*/
12691 static void
12692f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012693 typval_T *argvars;
12694 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012695{
12696 char_u *p;
12697 int n;
12698 int slen;
12699 int len;
12700 char_u *r;
12701 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000012702 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012703
12704 n = get_tv_number(&argvars[1]);
12705 if (argvars[0].v_type == VAR_LIST)
12706 {
12707 l = list_alloc();
12708 if (l != NULL && argvars[0].vval.v_list != NULL)
12709 {
12710 l->lv_refcount = 1;
12711 while (n-- > 0)
12712 if (list_extend(l, argvars[0].vval.v_list, NULL) == FAIL)
12713 break;
12714 }
12715 rettv->v_type = VAR_LIST;
12716 rettv->vval.v_list = l;
12717 }
12718 else
12719 {
12720 p = get_tv_string(&argvars[0]);
12721 rettv->v_type = VAR_STRING;
12722 rettv->vval.v_string = NULL;
12723
12724 slen = (int)STRLEN(p);
12725 len = slen * n;
12726 if (len <= 0)
12727 return;
12728
12729 r = alloc(len + 1);
12730 if (r != NULL)
12731 {
12732 for (i = 0; i < n; i++)
12733 mch_memmove(r + i * slen, p, (size_t)slen);
12734 r[len] = NUL;
12735 }
12736
12737 rettv->vval.v_string = r;
12738 }
12739}
12740
12741/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012742 * "resolve()" function
12743 */
12744 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012745f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012746 typval_T *argvars;
12747 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012748{
12749 char_u *p;
12750
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012751 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012752#ifdef FEAT_SHORTCUT
12753 {
12754 char_u *v = NULL;
12755
12756 v = mch_resolve_shortcut(p);
12757 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012758 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012759 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012760 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012761 }
12762#else
12763# ifdef HAVE_READLINK
12764 {
12765 char_u buf[MAXPATHL + 1];
12766 char_u *cpy;
12767 int len;
12768 char_u *remain = NULL;
12769 char_u *q;
12770 int is_relative_to_current = FALSE;
12771 int has_trailing_pathsep = FALSE;
12772 int limit = 100;
12773
12774 p = vim_strsave(p);
12775
12776 if (p[0] == '.' && (vim_ispathsep(p[1])
12777 || (p[1] == '.' && (vim_ispathsep(p[2])))))
12778 is_relative_to_current = TRUE;
12779
12780 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000012781 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012782 has_trailing_pathsep = TRUE;
12783
12784 q = getnextcomp(p);
12785 if (*q != NUL)
12786 {
12787 /* Separate the first path component in "p", and keep the
12788 * remainder (beginning with the path separator). */
12789 remain = vim_strsave(q - 1);
12790 q[-1] = NUL;
12791 }
12792
12793 for (;;)
12794 {
12795 for (;;)
12796 {
12797 len = readlink((char *)p, (char *)buf, MAXPATHL);
12798 if (len <= 0)
12799 break;
12800 buf[len] = NUL;
12801
12802 if (limit-- == 0)
12803 {
12804 vim_free(p);
12805 vim_free(remain);
12806 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012807 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012808 goto fail;
12809 }
12810
12811 /* Ensure that the result will have a trailing path separator
12812 * if the argument has one. */
12813 if (remain == NULL && has_trailing_pathsep)
12814 add_pathsep(buf);
12815
12816 /* Separate the first path component in the link value and
12817 * concatenate the remainders. */
12818 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
12819 if (*q != NUL)
12820 {
12821 if (remain == NULL)
12822 remain = vim_strsave(q - 1);
12823 else
12824 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012825 cpy = vim_strnsave(q-1, STRLEN(q-1) + STRLEN(remain));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012826 if (cpy != NULL)
12827 {
12828 STRCAT(cpy, remain);
12829 vim_free(remain);
12830 remain = cpy;
12831 }
12832 }
12833 q[-1] = NUL;
12834 }
12835
12836 q = gettail(p);
12837 if (q > p && *q == NUL)
12838 {
12839 /* Ignore trailing path separator. */
12840 q[-1] = NUL;
12841 q = gettail(p);
12842 }
12843 if (q > p && !mch_isFullName(buf))
12844 {
12845 /* symlink is relative to directory of argument */
12846 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
12847 if (cpy != NULL)
12848 {
12849 STRCPY(cpy, p);
12850 STRCPY(gettail(cpy), buf);
12851 vim_free(p);
12852 p = cpy;
12853 }
12854 }
12855 else
12856 {
12857 vim_free(p);
12858 p = vim_strsave(buf);
12859 }
12860 }
12861
12862 if (remain == NULL)
12863 break;
12864
12865 /* Append the first path component of "remain" to "p". */
12866 q = getnextcomp(remain + 1);
12867 len = q - remain - (*q != NUL);
12868 cpy = vim_strnsave(p, STRLEN(p) + len);
12869 if (cpy != NULL)
12870 {
12871 STRNCAT(cpy, remain, len);
12872 vim_free(p);
12873 p = cpy;
12874 }
12875 /* Shorten "remain". */
12876 if (*q != NUL)
12877 STRCPY(remain, q - 1);
12878 else
12879 {
12880 vim_free(remain);
12881 remain = NULL;
12882 }
12883 }
12884
12885 /* If the result is a relative path name, make it explicitly relative to
12886 * the current directory if and only if the argument had this form. */
12887 if (!vim_ispathsep(*p))
12888 {
12889 if (is_relative_to_current
12890 && *p != NUL
12891 && !(p[0] == '.'
12892 && (p[1] == NUL
12893 || vim_ispathsep(p[1])
12894 || (p[1] == '.'
12895 && (p[2] == NUL
12896 || vim_ispathsep(p[2]))))))
12897 {
12898 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012899 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012900 if (cpy != NULL)
12901 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012902 vim_free(p);
12903 p = cpy;
12904 }
12905 }
12906 else if (!is_relative_to_current)
12907 {
12908 /* Strip leading "./". */
12909 q = p;
12910 while (q[0] == '.' && vim_ispathsep(q[1]))
12911 q += 2;
12912 if (q > p)
12913 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
12914 }
12915 }
12916
12917 /* Ensure that the result will have no trailing path separator
12918 * if the argument had none. But keep "/" or "//". */
12919 if (!has_trailing_pathsep)
12920 {
12921 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000012922 if (after_pathsep(p, q))
12923 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012924 }
12925
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012926 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012927 }
12928# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012929 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012930# endif
12931#endif
12932
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012933 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012934
12935#ifdef HAVE_READLINK
12936fail:
12937#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012938 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012939}
12940
12941/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012942 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012943 */
12944 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012945f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012946 typval_T *argvars;
12947 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012948{
Bram Moolenaar33570922005-01-25 22:26:29 +000012949 list_T *l;
12950 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012951
Bram Moolenaar0d660222005-01-07 21:51:51 +000012952 rettv->vval.v_number = 0;
12953 if (argvars[0].v_type != VAR_LIST)
12954 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012955 else if ((l = argvars[0].vval.v_list) != NULL
12956 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000012957 {
12958 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012959 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012960 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012961 while (li != NULL)
12962 {
12963 ni = li->li_prev;
12964 list_append(l, li);
12965 li = ni;
12966 }
12967 rettv->vval.v_list = l;
12968 rettv->v_type = VAR_LIST;
12969 ++l->lv_refcount;
12970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012971}
12972
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012973#define SP_NOMOVE 1 /* don't move cursor */
12974#define SP_REPEAT 2 /* repeat to find outer pair */
12975#define SP_RETCOUNT 4 /* return matchcount */
Bram Moolenaar231334e2005-07-25 20:46:57 +000012976#define SP_SETPCMARK 8 /* set previous context mark */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012977
Bram Moolenaar33570922005-01-25 22:26:29 +000012978static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012979
12980/*
12981 * Get flags for a search function.
12982 * Possibly sets "p_ws".
12983 * Returns BACKWARD, FORWARD or zero (for an error).
12984 */
12985 static int
12986get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000012987 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012988 int *flagsp;
12989{
12990 int dir = FORWARD;
12991 char_u *flags;
12992 char_u nbuf[NUMBUFLEN];
12993 int mask;
12994
12995 if (varp->v_type != VAR_UNKNOWN)
12996 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012997 flags = get_tv_string_buf_chk(varp, nbuf);
12998 if (flags == NULL)
12999 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013000 while (*flags != NUL)
13001 {
13002 switch (*flags)
13003 {
13004 case 'b': dir = BACKWARD; break;
13005 case 'w': p_ws = TRUE; break;
13006 case 'W': p_ws = FALSE; break;
13007 default: mask = 0;
13008 if (flagsp != NULL)
13009 switch (*flags)
13010 {
13011 case 'n': mask = SP_NOMOVE; break;
13012 case 'r': mask = SP_REPEAT; break;
13013 case 'm': mask = SP_RETCOUNT; break;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013014 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013015 }
13016 if (mask == 0)
13017 {
13018 EMSG2(_(e_invarg2), flags);
13019 dir = 0;
13020 }
13021 else
13022 *flagsp |= mask;
13023 }
13024 if (dir == 0)
13025 break;
13026 ++flags;
13027 }
13028 }
13029 return dir;
13030}
13031
Bram Moolenaar071d4272004-06-13 20:20:40 +000013032/*
13033 * "search()" function
13034 */
13035 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013036f_search(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013037 typval_T *argvars;
13038 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013039{
13040 char_u *pat;
13041 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013042 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013043 int save_p_ws = p_ws;
13044 int dir;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013045 int flags = 0;
13046
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013047 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013048
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013049 pat = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013050 dir = get_search_arg(&argvars[1], &flags); /* may set p_ws */
13051 if (dir == 0)
13052 goto theend;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013053 /*
13054 * This function accepts only SP_NOMOVE and SP_SETPCMARK flags.
13055 * Check to make sure only those flags are set.
13056 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
13057 * flags cannot be set. Check for that condition also.
13058 */
13059 if (((flags & ~(SP_NOMOVE | SP_SETPCMARK)) != 0) ||
13060 ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013061 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013062 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013063 goto theend;
13064 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013065
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013066 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013067 if (searchit(curwin, curbuf, &pos, dir, pat, 1L,
13068 SEARCH_KEEP, RE_SEARCH) != FAIL)
13069 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013070 rettv->vval.v_number = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013071 if (flags & SP_SETPCMARK)
13072 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013073 curwin->w_cursor = pos;
13074 /* "/$" will put the cursor after the end of the line, may need to
13075 * correct that here */
13076 check_cursor();
13077 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013078
13079 /* If 'n' flag is used: restore cursor position. */
13080 if (flags & SP_NOMOVE)
13081 curwin->w_cursor = save_cursor;
13082theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000013083 p_ws = save_p_ws;
13084}
13085
Bram Moolenaar071d4272004-06-13 20:20:40 +000013086/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013087 * "searchdecl()" function
13088 */
13089 static void
13090f_searchdecl(argvars, rettv)
13091 typval_T *argvars;
13092 typval_T *rettv;
13093{
13094 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013095 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013096 int error = FALSE;
13097 char_u *name;
13098
13099 rettv->vval.v_number = 1; /* default: FAIL */
13100
13101 name = get_tv_string_chk(&argvars[0]);
13102 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000013103 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013104 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013105 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13106 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
13107 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013108 if (!error && name != NULL)
13109 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000013110 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013111}
13112
13113/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013114 * "searchpair()" function
13115 */
13116 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013117f_searchpair(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013118 typval_T *argvars;
13119 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013120{
13121 char_u *spat, *mpat, *epat;
13122 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013123 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013124 int dir;
13125 int flags = 0;
13126 char_u nbuf1[NUMBUFLEN];
13127 char_u nbuf2[NUMBUFLEN];
13128 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013129
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013130 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013131
Bram Moolenaar071d4272004-06-13 20:20:40 +000013132 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013133 spat = get_tv_string_chk(&argvars[0]);
13134 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
13135 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
13136 if (spat == NULL || mpat == NULL || epat == NULL)
13137 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013138
Bram Moolenaar071d4272004-06-13 20:20:40 +000013139 /* Handle the optional fourth argument: flags */
13140 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013141 if (dir == 0)
13142 goto theend;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013143 /*
13144 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
13145 */
13146 if ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK))
13147 {
13148 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
13149 goto theend;
13150 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013151
13152 /* Optional fifth argument: skip expresion */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013153 if (argvars[3].v_type == VAR_UNKNOWN
13154 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013155 skip = (char_u *)"";
13156 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013157 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
13158 if (skip == NULL)
13159 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013160
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013161 rettv->vval.v_number = do_searchpair(spat, mpat, epat, dir, skip, flags);
13162
13163theend:
13164 p_ws = save_p_ws;
13165}
13166
13167/*
13168 * Search for a start/middle/end thing.
13169 * Used by searchpair(), see its documentation for the details.
13170 * Returns 0 or -1 for no match,
13171 */
13172 long
13173do_searchpair(spat, mpat, epat, dir, skip, flags)
13174 char_u *spat; /* start pattern */
13175 char_u *mpat; /* middle pattern */
13176 char_u *epat; /* end pattern */
13177 int dir; /* BACKWARD or FORWARD */
13178 char_u *skip; /* skip expression */
13179 int flags; /* SP_RETCOUNT, SP_REPEAT, SP_NOMOVE */
13180{
13181 char_u *save_cpo;
13182 char_u *pat, *pat2 = NULL, *pat3 = NULL;
13183 long retval = 0;
13184 pos_T pos;
13185 pos_T firstpos;
13186 pos_T foundpos;
13187 pos_T save_cursor;
13188 pos_T save_pos;
13189 int n;
13190 int r;
13191 int nest = 1;
13192 int err;
13193
13194 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13195 save_cpo = p_cpo;
13196 p_cpo = (char_u *)"";
13197
13198 /* Make two search patterns: start/end (pat2, for in nested pairs) and
13199 * start/middle/end (pat3, for the top pair). */
13200 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
13201 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
13202 if (pat2 == NULL || pat3 == NULL)
13203 goto theend;
13204 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
13205 if (*mpat == NUL)
13206 STRCPY(pat3, pat2);
13207 else
13208 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
13209 spat, epat, mpat);
13210
Bram Moolenaar071d4272004-06-13 20:20:40 +000013211 save_cursor = curwin->w_cursor;
13212 pos = curwin->w_cursor;
13213 firstpos.lnum = 0;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000013214 foundpos.lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013215 pat = pat3;
13216 for (;;)
13217 {
13218 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
13219 SEARCH_KEEP, RE_SEARCH);
13220 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
13221 /* didn't find it or found the first match again: FAIL */
13222 break;
13223
13224 if (firstpos.lnum == 0)
13225 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000013226 if (equalpos(pos, foundpos))
13227 {
13228 /* Found the same position again. Can happen with a pattern that
13229 * has "\zs" at the end and searching backwards. Advance one
13230 * character and try again. */
13231 if (dir == BACKWARD)
13232 decl(&pos);
13233 else
13234 incl(&pos);
13235 }
13236 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013237
13238 /* If the skip pattern matches, ignore this match. */
13239 if (*skip != NUL)
13240 {
13241 save_pos = curwin->w_cursor;
13242 curwin->w_cursor = pos;
13243 r = eval_to_bool(skip, &err, NULL, FALSE);
13244 curwin->w_cursor = save_pos;
13245 if (err)
13246 {
13247 /* Evaluating {skip} caused an error, break here. */
13248 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013249 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013250 break;
13251 }
13252 if (r)
13253 continue;
13254 }
13255
13256 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
13257 {
13258 /* Found end when searching backwards or start when searching
13259 * forward: nested pair. */
13260 ++nest;
13261 pat = pat2; /* nested, don't search for middle */
13262 }
13263 else
13264 {
13265 /* Found end when searching forward or start when searching
13266 * backward: end of (nested) pair; or found middle in outer pair. */
13267 if (--nest == 1)
13268 pat = pat3; /* outer level, search for middle */
13269 }
13270
13271 if (nest == 0)
13272 {
13273 /* Found the match: return matchcount or line number. */
13274 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013275 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013276 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013277 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013278 if (flags & SP_SETPCMARK)
13279 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013280 curwin->w_cursor = pos;
13281 if (!(flags & SP_REPEAT))
13282 break;
13283 nest = 1; /* search for next unmatched */
13284 }
13285 }
13286
13287 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013288 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013289 curwin->w_cursor = save_cursor;
13290
13291theend:
13292 vim_free(pat2);
13293 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013294 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013295
13296 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013297}
13298
Bram Moolenaar0d660222005-01-07 21:51:51 +000013299/*ARGSUSED*/
13300 static void
13301f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013302 typval_T *argvars;
13303 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013304{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013305#ifdef FEAT_CLIENTSERVER
13306 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013307 char_u *server = get_tv_string_chk(&argvars[0]);
13308 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013309
Bram Moolenaar0d660222005-01-07 21:51:51 +000013310 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013311 if (server == NULL || reply == NULL)
13312 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013313 if (check_restricted() || check_secure())
13314 return;
13315# ifdef FEAT_X11
13316 if (check_connection() == FAIL)
13317 return;
13318# endif
13319
13320 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013321 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013322 EMSG(_("E258: Unable to send to client"));
13323 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013324 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013325 rettv->vval.v_number = 0;
13326#else
13327 rettv->vval.v_number = -1;
13328#endif
13329}
13330
13331/*ARGSUSED*/
13332 static void
13333f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013334 typval_T *argvars;
13335 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013336{
13337 char_u *r = NULL;
13338
13339#ifdef FEAT_CLIENTSERVER
13340# ifdef WIN32
13341 r = serverGetVimNames();
13342# else
13343 make_connection();
13344 if (X_DISPLAY != NULL)
13345 r = serverGetVimNames(X_DISPLAY);
13346# endif
13347#endif
13348 rettv->v_type = VAR_STRING;
13349 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013350}
13351
13352/*
13353 * "setbufvar()" function
13354 */
13355/*ARGSUSED*/
13356 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013357f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013358 typval_T *argvars;
13359 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013360{
13361 buf_T *buf;
13362#ifdef FEAT_AUTOCMD
13363 aco_save_T aco;
13364#else
13365 buf_T *save_curbuf;
13366#endif
13367 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013368 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013369 char_u nbuf[NUMBUFLEN];
13370
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013371 rettv->vval.v_number = 0;
13372
Bram Moolenaar071d4272004-06-13 20:20:40 +000013373 if (check_restricted() || check_secure())
13374 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013375 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
13376 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013377 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013378 varp = &argvars[2];
13379
13380 if (buf != NULL && varname != NULL && varp != NULL)
13381 {
13382 /* set curbuf to be our buf, temporarily */
13383#ifdef FEAT_AUTOCMD
13384 aucmd_prepbuf(&aco, buf);
13385#else
13386 save_curbuf = curbuf;
13387 curbuf = buf;
13388#endif
13389
13390 if (*varname == '&')
13391 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013392 long numval;
13393 char_u *strval;
13394 int error = FALSE;
13395
Bram Moolenaar071d4272004-06-13 20:20:40 +000013396 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013397 numval = get_tv_number_chk(varp, &error);
13398 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013399 if (!error && strval != NULL)
13400 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013401 }
13402 else
13403 {
13404 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
13405 if (bufvarname != NULL)
13406 {
13407 STRCPY(bufvarname, "b:");
13408 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013409 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013410 vim_free(bufvarname);
13411 }
13412 }
13413
13414 /* reset notion of buffer */
13415#ifdef FEAT_AUTOCMD
13416 aucmd_restbuf(&aco);
13417#else
13418 curbuf = save_curbuf;
13419#endif
13420 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013421}
13422
13423/*
13424 * "setcmdpos()" function
13425 */
13426 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013427f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013428 typval_T *argvars;
13429 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013430{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013431 int pos = (int)get_tv_number(&argvars[0]) - 1;
13432
13433 if (pos >= 0)
13434 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013435}
13436
13437/*
13438 * "setline()" function
13439 */
13440 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013441f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013442 typval_T *argvars;
13443 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013444{
13445 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000013446 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013447 list_T *l = NULL;
13448 listitem_T *li = NULL;
13449 long added = 0;
13450 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013451
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013452 lnum = get_tv_lnum(&argvars[0]);
13453 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013454 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013455 l = argvars[1].vval.v_list;
13456 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013457 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013458 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013459 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013460
13461 rettv->vval.v_number = 0; /* OK */
13462 for (;;)
13463 {
13464 if (l != NULL)
13465 {
13466 /* list argument, get next string */
13467 if (li == NULL)
13468 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013469 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013470 li = li->li_next;
13471 }
13472
13473 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013474 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013475 break;
13476 if (lnum <= curbuf->b_ml.ml_line_count)
13477 {
13478 /* existing line, replace it */
13479 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
13480 {
13481 changed_bytes(lnum, 0);
13482 check_cursor_col();
13483 rettv->vval.v_number = 0; /* OK */
13484 }
13485 }
13486 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
13487 {
13488 /* lnum is one past the last line, append the line */
13489 ++added;
13490 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
13491 rettv->vval.v_number = 0; /* OK */
13492 }
13493
13494 if (l == NULL) /* only one string argument */
13495 break;
13496 ++lnum;
13497 }
13498
13499 if (added > 0)
13500 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013501}
13502
13503/*
Bram Moolenaar2641f772005-03-25 21:58:17 +000013504 * "setqflist()" function
13505 */
13506/*ARGSUSED*/
13507 static void
13508f_setqflist(argvars, rettv)
13509 typval_T *argvars;
13510 typval_T *rettv;
13511{
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013512 char_u *act;
13513 int action = ' ';
13514
Bram Moolenaar2641f772005-03-25 21:58:17 +000013515 rettv->vval.v_number = -1;
13516
13517#ifdef FEAT_QUICKFIX
13518 if (argvars[0].v_type != VAR_LIST)
13519 EMSG(_(e_listreq));
13520 else
13521 {
13522 list_T *l = argvars[0].vval.v_list;
13523
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013524 if (argvars[1].v_type == VAR_STRING)
13525 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013526 act = get_tv_string_chk(&argvars[1]);
13527 if (act == NULL)
13528 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013529 if (*act == 'a' || *act == 'r')
13530 action = *act;
13531 }
13532
13533 if (l != NULL && set_errorlist(l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013534 rettv->vval.v_number = 0;
13535 }
13536#endif
13537}
13538
13539/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013540 * "setreg()" function
13541 */
13542 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013543f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013544 typval_T *argvars;
13545 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013546{
13547 int regname;
13548 char_u *strregname;
13549 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013550 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013551 int append;
13552 char_u yank_type;
13553 long block_len;
13554
13555 block_len = -1;
13556 yank_type = MAUTO;
13557 append = FALSE;
13558
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013559 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013560 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013561
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013562 if (strregname == NULL)
13563 return; /* type error; errmsg already given */
13564 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013565 if (regname == 0 || regname == '@')
13566 regname = '"';
13567 else if (regname == '=')
13568 return;
13569
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013570 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013571 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013572 stropt = get_tv_string_chk(&argvars[2]);
13573 if (stropt == NULL)
13574 return; /* type error */
13575 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013576 switch (*stropt)
13577 {
13578 case 'a': case 'A': /* append */
13579 append = TRUE;
13580 break;
13581 case 'v': case 'c': /* character-wise selection */
13582 yank_type = MCHAR;
13583 break;
13584 case 'V': case 'l': /* line-wise selection */
13585 yank_type = MLINE;
13586 break;
13587#ifdef FEAT_VISUAL
13588 case 'b': case Ctrl_V: /* block-wise selection */
13589 yank_type = MBLOCK;
13590 if (VIM_ISDIGIT(stropt[1]))
13591 {
13592 ++stropt;
13593 block_len = getdigits(&stropt) - 1;
13594 --stropt;
13595 }
13596 break;
13597#endif
13598 }
13599 }
13600
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013601 strval = get_tv_string_chk(&argvars[1]);
13602 if (strval != NULL)
13603 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000013604 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013605 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013606}
13607
13608
13609/*
13610 * "setwinvar(expr)" function
13611 */
13612/*ARGSUSED*/
13613 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013614f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013615 typval_T *argvars;
13616 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013617{
13618 win_T *win;
13619#ifdef FEAT_WINDOWS
13620 win_T *save_curwin;
13621#endif
13622 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013623 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013624 char_u nbuf[NUMBUFLEN];
13625
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013626 rettv->vval.v_number = 0;
13627
Bram Moolenaar071d4272004-06-13 20:20:40 +000013628 if (check_restricted() || check_secure())
13629 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013630 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013631 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013632 varp = &argvars[2];
13633
13634 if (win != NULL && varname != NULL && varp != NULL)
13635 {
13636#ifdef FEAT_WINDOWS
13637 /* set curwin to be our win, temporarily */
13638 save_curwin = curwin;
13639 curwin = win;
13640 curbuf = curwin->w_buffer;
13641#endif
13642
13643 if (*varname == '&')
13644 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013645 long numval;
13646 char_u *strval;
13647 int error = FALSE;
13648
Bram Moolenaar071d4272004-06-13 20:20:40 +000013649 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013650 numval = get_tv_number_chk(varp, &error);
13651 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013652 if (!error && strval != NULL)
13653 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013654 }
13655 else
13656 {
13657 winvarname = alloc((unsigned)STRLEN(varname) + 3);
13658 if (winvarname != NULL)
13659 {
13660 STRCPY(winvarname, "w:");
13661 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013662 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013663 vim_free(winvarname);
13664 }
13665 }
13666
13667#ifdef FEAT_WINDOWS
13668 /* Restore current window, if it's still valid (autocomands can make
13669 * it invalid). */
13670 if (win_valid(save_curwin))
13671 {
13672 curwin = save_curwin;
13673 curbuf = curwin->w_buffer;
13674 }
13675#endif
13676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013677}
13678
13679/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013680 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013681 */
13682 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013683f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013684 typval_T *argvars;
13685 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013686{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013687 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013688
Bram Moolenaar0d660222005-01-07 21:51:51 +000013689 p = get_tv_string(&argvars[0]);
13690 rettv->vval.v_string = vim_strsave(p);
13691 simplify_filename(rettv->vval.v_string); /* simplify in place */
13692 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013693}
13694
Bram Moolenaar0d660222005-01-07 21:51:51 +000013695static int
13696#ifdef __BORLANDC__
13697 _RTLENTRYF
13698#endif
13699 item_compare __ARGS((const void *s1, const void *s2));
13700static int
13701#ifdef __BORLANDC__
13702 _RTLENTRYF
13703#endif
13704 item_compare2 __ARGS((const void *s1, const void *s2));
13705
13706static int item_compare_ic;
13707static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013708static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013709#define ITEM_COMPARE_FAIL 999
13710
Bram Moolenaar071d4272004-06-13 20:20:40 +000013711/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013712 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013713 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013714 static int
13715#ifdef __BORLANDC__
13716_RTLENTRYF
13717#endif
13718item_compare(s1, s2)
13719 const void *s1;
13720 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013721{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013722 char_u *p1, *p2;
13723 char_u *tofree1, *tofree2;
13724 int res;
13725 char_u numbuf1[NUMBUFLEN];
13726 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013727
Bram Moolenaar33570922005-01-25 22:26:29 +000013728 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1);
13729 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013730 if (item_compare_ic)
13731 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013732 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000013733 res = STRCMP(p1, p2);
13734 vim_free(tofree1);
13735 vim_free(tofree2);
13736 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013737}
13738
13739 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000013740#ifdef __BORLANDC__
13741_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000013742#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000013743item_compare2(s1, s2)
13744 const void *s1;
13745 const void *s2;
13746{
13747 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000013748 typval_T rettv;
13749 typval_T argv[2];
Bram Moolenaar0d660222005-01-07 21:51:51 +000013750 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013751
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013752 /* shortcut after failure in previous call; compare all items equal */
13753 if (item_compare_func_err)
13754 return 0;
13755
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013756 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
13757 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013758 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
13759 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013760
13761 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
13762 res = call_func(item_compare_func, STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000013763 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013764 clear_tv(&argv[0]);
13765 clear_tv(&argv[1]);
13766
13767 if (res == FAIL)
13768 res = ITEM_COMPARE_FAIL;
13769 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013770 /* return value has wrong type */
13771 res = get_tv_number_chk(&rettv, &item_compare_func_err);
13772 if (item_compare_func_err)
13773 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013774 clear_tv(&rettv);
13775 return res;
13776}
13777
13778/*
13779 * "sort({list})" function
13780 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013781 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013782f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013783 typval_T *argvars;
13784 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013785{
Bram Moolenaar33570922005-01-25 22:26:29 +000013786 list_T *l;
13787 listitem_T *li;
13788 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013789 long len;
13790 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013791
Bram Moolenaar0d660222005-01-07 21:51:51 +000013792 rettv->vval.v_number = 0;
13793 if (argvars[0].v_type != VAR_LIST)
13794 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000013795 else
13796 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013797 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013798 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013799 return;
13800 rettv->vval.v_list = l;
13801 rettv->v_type = VAR_LIST;
13802 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013803
Bram Moolenaar0d660222005-01-07 21:51:51 +000013804 len = list_len(l);
13805 if (len <= 1)
13806 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013807
Bram Moolenaar0d660222005-01-07 21:51:51 +000013808 item_compare_ic = FALSE;
13809 item_compare_func = NULL;
13810 if (argvars[1].v_type != VAR_UNKNOWN)
13811 {
13812 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013813 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013814 else
13815 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013816 int error = FALSE;
13817
13818 i = get_tv_number_chk(&argvars[1], &error);
13819 if (error)
13820 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013821 if (i == 1)
13822 item_compare_ic = TRUE;
13823 else
13824 item_compare_func = get_tv_string(&argvars[1]);
13825 }
13826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013827
Bram Moolenaar0d660222005-01-07 21:51:51 +000013828 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013829 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013830 if (ptrs == NULL)
13831 return;
13832 i = 0;
13833 for (li = l->lv_first; li != NULL; li = li->li_next)
13834 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013835
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013836 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013837 /* test the compare function */
13838 if (item_compare_func != NULL
13839 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
13840 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013841 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013842 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000013843 {
13844 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013845 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000013846 item_compare_func == NULL ? item_compare : item_compare2);
13847
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013848 if (!item_compare_func_err)
13849 {
13850 /* Clear the List and append the items in the sorted order. */
13851 l->lv_first = l->lv_last = NULL;
13852 l->lv_len = 0;
13853 for (i = 0; i < len; ++i)
13854 list_append(l, ptrs[i]);
13855 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013856 }
13857
13858 vim_free(ptrs);
13859 }
13860}
13861
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013862/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000013863 * "soundfold({word})" function
13864 */
13865 static void
13866f_soundfold(argvars, rettv)
13867 typval_T *argvars;
13868 typval_T *rettv;
13869{
13870 char_u *s;
13871
13872 rettv->v_type = VAR_STRING;
13873 s = get_tv_string(&argvars[0]);
13874#ifdef FEAT_SYN_HL
13875 rettv->vval.v_string = eval_soundfold(s);
13876#else
13877 rettv->vval.v_string = vim_strsave(s);
13878#endif
13879}
13880
13881/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013882 * "spellbadword()" function
13883 */
13884/* ARGSUSED */
13885 static void
13886f_spellbadword(argvars, rettv)
13887 typval_T *argvars;
13888 typval_T *rettv;
13889{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013890 int len;
13891
13892 rettv->vval.v_string = NULL;
13893 rettv->v_type = VAR_STRING;
13894
13895#ifdef FEAT_SYN_HL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +000013896 /* Find the start and length of the badly spelled word. */
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +000013897 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +000013898 if (len != 0)
13899 rettv->vval.v_string = vim_strnsave(ml_get_cursor(), len);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013900#endif
13901}
13902
13903/*
13904 * "spellsuggest()" function
13905 */
13906 static void
13907f_spellsuggest(argvars, rettv)
13908 typval_T *argvars;
13909 typval_T *rettv;
13910{
13911 char_u *str;
13912 int maxcount;
13913 garray_T ga;
13914 list_T *l;
13915 listitem_T *li;
13916 int i;
13917
13918 l = list_alloc();
13919 if (l == NULL)
13920 return;
13921 rettv->v_type = VAR_LIST;
13922 rettv->vval.v_list = l;
13923 ++l->lv_refcount;
13924
13925#ifdef FEAT_SYN_HL
13926 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
13927 {
13928 str = get_tv_string(&argvars[0]);
13929 if (argvars[1].v_type != VAR_UNKNOWN)
13930 {
13931 maxcount = get_tv_number(&argvars[1]);
13932 if (maxcount <= 0)
13933 return;
13934 }
13935 else
13936 maxcount = 25;
13937
Bram Moolenaar8c45cdf2005-08-11 20:11:38 +000013938 spell_suggest_list(&ga, str, maxcount, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013939
13940 for (i = 0; i < ga.ga_len; ++i)
13941 {
13942 str = ((char_u **)ga.ga_data)[i];
13943
13944 li = listitem_alloc();
13945 if (li == NULL)
13946 vim_free(str);
13947 else
13948 {
13949 li->li_tv.v_type = VAR_STRING;
13950 li->li_tv.v_lock = 0;
13951 li->li_tv.vval.v_string = str;
13952 list_append(l, li);
13953 }
13954 }
13955 ga_clear(&ga);
13956 }
13957#endif
13958}
13959
Bram Moolenaar0d660222005-01-07 21:51:51 +000013960 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013961f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013962 typval_T *argvars;
13963 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013964{
13965 char_u *str;
13966 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013967 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013968 regmatch_T regmatch;
13969 char_u patbuf[NUMBUFLEN];
13970 char_u *save_cpo;
13971 int match;
Bram Moolenaar33570922005-01-25 22:26:29 +000013972 listitem_T *ni;
13973 list_T *l;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013974 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013975 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013976 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013977
13978 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13979 save_cpo = p_cpo;
13980 p_cpo = (char_u *)"";
13981
13982 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013983 if (argvars[1].v_type != VAR_UNKNOWN)
13984 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013985 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13986 if (pat == NULL)
13987 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013988 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013989 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013990 }
13991 if (pat == NULL || *pat == NUL)
13992 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000013993
13994 l = list_alloc();
13995 if (l == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013996 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013997 rettv->v_type = VAR_LIST;
13998 rettv->vval.v_list = l;
13999 ++l->lv_refcount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014000 if (typeerr)
14001 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014002
Bram Moolenaar0d660222005-01-07 21:51:51 +000014003 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14004 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014005 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014006 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014007 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014008 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014009 if (*str == NUL)
14010 match = FALSE; /* empty item at the end */
14011 else
14012 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014013 if (match)
14014 end = regmatch.startp[0];
14015 else
14016 end = str + STRLEN(str);
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014017 if (keepempty || end > str || (l->lv_len > 0 && *str != NUL
14018 && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014019 {
14020 ni = listitem_alloc();
14021 if (ni == NULL)
14022 break;
14023 ni->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014024 ni->li_tv.v_lock = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014025 ni->li_tv.vval.v_string = vim_strnsave(str, end - str);
14026 list_append(l, ni);
14027 }
14028 if (!match)
14029 break;
14030 /* Advance to just after the match. */
14031 if (regmatch.endp[0] > str)
14032 col = 0;
14033 else
14034 {
14035 /* Don't get stuck at the same match. */
14036#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014037 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014038#else
14039 col = 1;
14040#endif
14041 }
14042 str = regmatch.endp[0];
14043 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014044
Bram Moolenaar0d660222005-01-07 21:51:51 +000014045 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014047
Bram Moolenaar0d660222005-01-07 21:51:51 +000014048 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014049}
14050
14051#ifdef HAVE_STRFTIME
14052/*
14053 * "strftime({format}[, {time}])" function
14054 */
14055 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014056f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014057 typval_T *argvars;
14058 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014059{
14060 char_u result_buf[256];
14061 struct tm *curtime;
14062 time_t seconds;
14063 char_u *p;
14064
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014065 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014066
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014067 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014068 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014069 seconds = time(NULL);
14070 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014071 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014072 curtime = localtime(&seconds);
14073 /* MSVC returns NULL for an invalid value of seconds. */
14074 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014075 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014076 else
14077 {
14078# ifdef FEAT_MBYTE
14079 vimconv_T conv;
14080 char_u *enc;
14081
14082 conv.vc_type = CONV_NONE;
14083 enc = enc_locale();
14084 convert_setup(&conv, p_enc, enc);
14085 if (conv.vc_type != CONV_NONE)
14086 p = string_convert(&conv, p, NULL);
14087# endif
14088 if (p != NULL)
14089 (void)strftime((char *)result_buf, sizeof(result_buf),
14090 (char *)p, curtime);
14091 else
14092 result_buf[0] = NUL;
14093
14094# ifdef FEAT_MBYTE
14095 if (conv.vc_type != CONV_NONE)
14096 vim_free(p);
14097 convert_setup(&conv, enc, p_enc);
14098 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014099 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014100 else
14101# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014102 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014103
14104# ifdef FEAT_MBYTE
14105 /* Release conversion descriptors */
14106 convert_setup(&conv, NULL, NULL);
14107 vim_free(enc);
14108# endif
14109 }
14110}
14111#endif
14112
14113/*
14114 * "stridx()" function
14115 */
14116 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014117f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014118 typval_T *argvars;
14119 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014120{
14121 char_u buf[NUMBUFLEN];
14122 char_u *needle;
14123 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000014124 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014125 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000014126 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014127
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014128 needle = get_tv_string_chk(&argvars[1]);
14129 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000014130 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014131 if (needle == NULL || haystack == NULL)
14132 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014133
Bram Moolenaar33570922005-01-25 22:26:29 +000014134 if (argvars[2].v_type != VAR_UNKNOWN)
14135 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014136 int error = FALSE;
14137
14138 start_idx = get_tv_number_chk(&argvars[2], &error);
14139 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000014140 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014141 if (start_idx >= 0)
14142 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000014143 }
14144
14145 pos = (char_u *)strstr((char *)haystack, (char *)needle);
14146 if (pos != NULL)
14147 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014148}
14149
14150/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014151 * "string()" function
14152 */
14153 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014154f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014155 typval_T *argvars;
14156 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014157{
14158 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014159 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014160
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014161 rettv->v_type = VAR_STRING;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014162 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014163 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014164 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014165}
14166
14167/*
14168 * "strlen()" function
14169 */
14170 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014171f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014172 typval_T *argvars;
14173 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014174{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014175 rettv->vval.v_number = (varnumber_T)(STRLEN(
14176 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014177}
14178
14179/*
14180 * "strpart()" function
14181 */
14182 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014183f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014184 typval_T *argvars;
14185 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014186{
14187 char_u *p;
14188 int n;
14189 int len;
14190 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014191 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014192
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014193 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014194 slen = (int)STRLEN(p);
14195
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014196 n = get_tv_number_chk(&argvars[1], &error);
14197 if (error)
14198 len = 0;
14199 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014200 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014201 else
14202 len = slen - n; /* default len: all bytes that are available. */
14203
14204 /*
14205 * Only return the overlap between the specified part and the actual
14206 * string.
14207 */
14208 if (n < 0)
14209 {
14210 len += n;
14211 n = 0;
14212 }
14213 else if (n > slen)
14214 n = slen;
14215 if (len < 0)
14216 len = 0;
14217 else if (n + len > slen)
14218 len = slen - n;
14219
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014220 rettv->v_type = VAR_STRING;
14221 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014222}
14223
14224/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014225 * "strridx()" function
14226 */
14227 static void
14228f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014229 typval_T *argvars;
14230 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014231{
14232 char_u buf[NUMBUFLEN];
14233 char_u *needle;
14234 char_u *haystack;
14235 char_u *rest;
14236 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014237 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014238
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014239 needle = get_tv_string_chk(&argvars[1]);
14240 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar532c7802005-01-27 14:44:31 +000014241 haystack_len = STRLEN(haystack);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014242
14243 rettv->vval.v_number = -1;
14244 if (needle == NULL || haystack == NULL)
14245 return; /* type error; errmsg already given */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014246 if (argvars[2].v_type != VAR_UNKNOWN)
14247 {
14248 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014249 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000014250 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014251 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014252 }
14253 else
14254 end_idx = haystack_len;
14255
Bram Moolenaar0d660222005-01-07 21:51:51 +000014256 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000014257 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014258 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014259 lastmatch = haystack + end_idx;
14260 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014261 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000014262 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014263 for (rest = haystack; *rest != '\0'; ++rest)
14264 {
14265 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000014266 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014267 break;
14268 lastmatch = rest;
14269 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000014270 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014271
14272 if (lastmatch == NULL)
14273 rettv->vval.v_number = -1;
14274 else
14275 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
14276}
14277
14278/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014279 * "strtrans()" function
14280 */
14281 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014282f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014283 typval_T *argvars;
14284 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014285{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014286 rettv->v_type = VAR_STRING;
14287 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014288}
14289
14290/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014291 * "submatch()" function
14292 */
14293 static void
14294f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014295 typval_T *argvars;
14296 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014297{
14298 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014299 rettv->vval.v_string =
14300 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014301}
14302
14303/*
14304 * "substitute()" function
14305 */
14306 static void
14307f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014308 typval_T *argvars;
14309 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014310{
14311 char_u patbuf[NUMBUFLEN];
14312 char_u subbuf[NUMBUFLEN];
14313 char_u flagsbuf[NUMBUFLEN];
14314
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014315 char_u *str = get_tv_string_chk(&argvars[0]);
14316 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14317 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
14318 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
14319
Bram Moolenaar0d660222005-01-07 21:51:51 +000014320 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014321 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
14322 rettv->vval.v_string = NULL;
14323 else
14324 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014325}
14326
14327/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014328 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014329 */
14330/*ARGSUSED*/
14331 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014332f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014333 typval_T *argvars;
14334 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014335{
14336 int id = 0;
14337#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014338 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014339 long col;
14340 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000014341 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014342
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014343 lnum = get_tv_lnum(argvars); /* -1 on type error */
14344 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
14345 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014346
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014347 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014348 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +000014349 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014350#endif
14351
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014352 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014353}
14354
14355/*
14356 * "synIDattr(id, what [, mode])" function
14357 */
14358/*ARGSUSED*/
14359 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014360f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014361 typval_T *argvars;
14362 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014363{
14364 char_u *p = NULL;
14365#ifdef FEAT_SYN_HL
14366 int id;
14367 char_u *what;
14368 char_u *mode;
14369 char_u modebuf[NUMBUFLEN];
14370 int modec;
14371
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014372 id = get_tv_number(&argvars[0]);
14373 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014374 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014375 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014376 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014377 modec = TOLOWER_ASC(mode[0]);
14378 if (modec != 't' && modec != 'c'
14379#ifdef FEAT_GUI
14380 && modec != 'g'
14381#endif
14382 )
14383 modec = 0; /* replace invalid with current */
14384 }
14385 else
14386 {
14387#ifdef FEAT_GUI
14388 if (gui.in_use)
14389 modec = 'g';
14390 else
14391#endif
14392 if (t_colors > 1)
14393 modec = 'c';
14394 else
14395 modec = 't';
14396 }
14397
14398
14399 switch (TOLOWER_ASC(what[0]))
14400 {
14401 case 'b':
14402 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
14403 p = highlight_color(id, what, modec);
14404 else /* bold */
14405 p = highlight_has_attr(id, HL_BOLD, modec);
14406 break;
14407
14408 case 'f': /* fg[#] */
14409 p = highlight_color(id, what, modec);
14410 break;
14411
14412 case 'i':
14413 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
14414 p = highlight_has_attr(id, HL_INVERSE, modec);
14415 else /* italic */
14416 p = highlight_has_attr(id, HL_ITALIC, modec);
14417 break;
14418
14419 case 'n': /* name */
14420 p = get_highlight_name(NULL, id - 1);
14421 break;
14422
14423 case 'r': /* reverse */
14424 p = highlight_has_attr(id, HL_INVERSE, modec);
14425 break;
14426
14427 case 's': /* standout */
14428 p = highlight_has_attr(id, HL_STANDOUT, modec);
14429 break;
14430
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000014431 case 'u':
14432 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
14433 /* underline */
14434 p = highlight_has_attr(id, HL_UNDERLINE, modec);
14435 else
14436 /* undercurl */
14437 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014438 break;
14439 }
14440
14441 if (p != NULL)
14442 p = vim_strsave(p);
14443#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014444 rettv->v_type = VAR_STRING;
14445 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014446}
14447
14448/*
14449 * "synIDtrans(id)" function
14450 */
14451/*ARGSUSED*/
14452 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014453f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014454 typval_T *argvars;
14455 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014456{
14457 int id;
14458
14459#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014460 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014461
14462 if (id > 0)
14463 id = syn_get_final_id(id);
14464 else
14465#endif
14466 id = 0;
14467
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014468 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014469}
14470
14471/*
14472 * "system()" function
14473 */
14474 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014475f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014476 typval_T *argvars;
14477 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014478{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014479 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014480 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014481 char_u *infile = NULL;
14482 char_u buf[NUMBUFLEN];
14483 int err = FALSE;
14484 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014485
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014486 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014487 {
14488 /*
14489 * Write the string to a temp file, to be used for input of the shell
14490 * command.
14491 */
14492 if ((infile = vim_tempname('i')) == NULL)
14493 {
14494 EMSG(_(e_notmp));
14495 return;
14496 }
14497
14498 fd = mch_fopen((char *)infile, WRITEBIN);
14499 if (fd == NULL)
14500 {
14501 EMSG2(_(e_notopen), infile);
14502 goto done;
14503 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014504 p = get_tv_string_buf_chk(&argvars[1], buf);
14505 if (p == NULL)
14506 goto done; /* type error; errmsg already given */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014507 if (fwrite(p, STRLEN(p), 1, fd) != 1)
14508 err = TRUE;
14509 if (fclose(fd) != 0)
14510 err = TRUE;
14511 if (err)
14512 {
14513 EMSG(_("E677: Error writing temp file"));
14514 goto done;
14515 }
14516 }
14517
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014518 res = get_cmd_output(get_tv_string(&argvars[0]), infile, SHELL_SILENT);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014519
Bram Moolenaar071d4272004-06-13 20:20:40 +000014520#ifdef USE_CR
14521 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014522 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014523 {
14524 char_u *s;
14525
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014526 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014527 {
14528 if (*s == CAR)
14529 *s = NL;
14530 }
14531 }
14532#else
14533# ifdef USE_CRNL
14534 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014535 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014536 {
14537 char_u *s, *d;
14538
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014539 d = res;
14540 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014541 {
14542 if (s[0] == CAR && s[1] == NL)
14543 ++s;
14544 *d++ = *s;
14545 }
14546 *d = NUL;
14547 }
14548# endif
14549#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014550
14551done:
14552 if (infile != NULL)
14553 {
14554 mch_remove(infile);
14555 vim_free(infile);
14556 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014557 rettv->v_type = VAR_STRING;
14558 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014559}
14560
14561/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000014562 * "tagfiles()" function
14563 */
14564/*ARGSUSED*/
14565 static void
14566f_tagfiles(argvars, rettv)
14567 typval_T *argvars;
14568 typval_T *rettv;
14569{
14570 char_u fname[MAXPATHL + 1];
14571 list_T *l;
14572
14573 l = list_alloc();
14574 if (l == NULL)
14575 {
14576 rettv->vval.v_number = 0;
14577 return;
14578 }
14579 rettv->vval.v_list = l;
14580 rettv->v_type = VAR_LIST;
14581 ++l->lv_refcount;
14582
14583 get_tagfname(TRUE, NULL);
14584 for (;;)
14585 if (get_tagfname(FALSE, fname) == FAIL
14586 || list_append_string(l, fname) == FAIL)
14587 break;
14588}
14589
14590/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000014591 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014592 */
14593 static void
14594f_taglist(argvars, rettv)
14595 typval_T *argvars;
14596 typval_T *rettv;
14597{
14598 char_u *tag_pattern;
14599 list_T *l;
14600
14601 tag_pattern = get_tv_string(&argvars[0]);
14602
14603 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014604 if (*tag_pattern == NUL)
14605 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014606
14607 l = list_alloc();
14608 if (l != NULL)
14609 {
14610 if (get_tags(l, tag_pattern) != FAIL)
14611 {
14612 rettv->vval.v_list = l;
14613 rettv->v_type = VAR_LIST;
14614 ++l->lv_refcount;
14615 }
14616 else
14617 list_free(l);
14618 }
14619}
14620
14621/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014622 * "tempname()" function
14623 */
14624/*ARGSUSED*/
14625 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014626f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014627 typval_T *argvars;
14628 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014629{
14630 static int x = 'A';
14631
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014632 rettv->v_type = VAR_STRING;
14633 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014634
14635 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
14636 * names. Skip 'I' and 'O', they are used for shell redirection. */
14637 do
14638 {
14639 if (x == 'Z')
14640 x = '0';
14641 else if (x == '9')
14642 x = 'A';
14643 else
14644 {
14645#ifdef EBCDIC
14646 if (x == 'I')
14647 x = 'J';
14648 else if (x == 'R')
14649 x = 'S';
14650 else
14651#endif
14652 ++x;
14653 }
14654 } while (x == 'I' || x == 'O');
14655}
14656
14657/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000014658 * "test(list)" function: Just checking the walls...
14659 */
14660/*ARGSUSED*/
14661 static void
14662f_test(argvars, rettv)
14663 typval_T *argvars;
14664 typval_T *rettv;
14665{
14666 /* Used for unit testing. Change the code below to your liking. */
14667#if 0
14668 listitem_T *li;
14669 list_T *l;
14670 char_u *bad, *good;
14671
14672 if (argvars[0].v_type != VAR_LIST)
14673 return;
14674 l = argvars[0].vval.v_list;
14675 if (l == NULL)
14676 return;
14677 li = l->lv_first;
14678 if (li == NULL)
14679 return;
14680 bad = get_tv_string(&li->li_tv);
14681 li = li->li_next;
14682 if (li == NULL)
14683 return;
14684 good = get_tv_string(&li->li_tv);
14685 rettv->vval.v_number = test_edit_score(bad, good);
14686#endif
14687}
14688
14689/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014690 * "tolower(string)" function
14691 */
14692 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014693f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014694 typval_T *argvars;
14695 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014696{
14697 char_u *p;
14698
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014699 p = vim_strsave(get_tv_string(&argvars[0]));
14700 rettv->v_type = VAR_STRING;
14701 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014702
14703 if (p != NULL)
14704 while (*p != NUL)
14705 {
14706#ifdef FEAT_MBYTE
14707 int l;
14708
14709 if (enc_utf8)
14710 {
14711 int c, lc;
14712
14713 c = utf_ptr2char(p);
14714 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014715 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014716 /* TODO: reallocate string when byte count changes. */
14717 if (utf_char2len(lc) == l)
14718 utf_char2bytes(lc, p);
14719 p += l;
14720 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014721 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014722 p += l; /* skip multi-byte character */
14723 else
14724#endif
14725 {
14726 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
14727 ++p;
14728 }
14729 }
14730}
14731
14732/*
14733 * "toupper(string)" function
14734 */
14735 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014736f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014737 typval_T *argvars;
14738 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014739{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014740 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014741 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014742}
14743
14744/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000014745 * "tr(string, fromstr, tostr)" function
14746 */
14747 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014748f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014749 typval_T *argvars;
14750 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014751{
14752 char_u *instr;
14753 char_u *fromstr;
14754 char_u *tostr;
14755 char_u *p;
14756#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000014757 int inlen;
14758 int fromlen;
14759 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014760 int idx;
14761 char_u *cpstr;
14762 int cplen;
14763 int first = TRUE;
14764#endif
14765 char_u buf[NUMBUFLEN];
14766 char_u buf2[NUMBUFLEN];
14767 garray_T ga;
14768
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014769 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014770 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
14771 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014772
14773 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014774 rettv->v_type = VAR_STRING;
14775 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014776 if (fromstr == NULL || tostr == NULL)
14777 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000014778 ga_init2(&ga, (int)sizeof(char), 80);
14779
14780#ifdef FEAT_MBYTE
14781 if (!has_mbyte)
14782#endif
14783 /* not multi-byte: fromstr and tostr must be the same length */
14784 if (STRLEN(fromstr) != STRLEN(tostr))
14785 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014786#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000014787error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014788#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000014789 EMSG2(_(e_invarg2), fromstr);
14790 ga_clear(&ga);
14791 return;
14792 }
14793
14794 /* fromstr and tostr have to contain the same number of chars */
14795 while (*instr != NUL)
14796 {
14797#ifdef FEAT_MBYTE
14798 if (has_mbyte)
14799 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014800 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014801 cpstr = instr;
14802 cplen = inlen;
14803 idx = 0;
14804 for (p = fromstr; *p != NUL; p += fromlen)
14805 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014806 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014807 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
14808 {
14809 for (p = tostr; *p != NUL; p += tolen)
14810 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014811 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014812 if (idx-- == 0)
14813 {
14814 cplen = tolen;
14815 cpstr = p;
14816 break;
14817 }
14818 }
14819 if (*p == NUL) /* tostr is shorter than fromstr */
14820 goto error;
14821 break;
14822 }
14823 ++idx;
14824 }
14825
14826 if (first && cpstr == instr)
14827 {
14828 /* Check that fromstr and tostr have the same number of
14829 * (multi-byte) characters. Done only once when a character
14830 * of instr doesn't appear in fromstr. */
14831 first = FALSE;
14832 for (p = tostr; *p != NUL; p += tolen)
14833 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014834 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014835 --idx;
14836 }
14837 if (idx != 0)
14838 goto error;
14839 }
14840
14841 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000014842 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014843 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014844
14845 instr += inlen;
14846 }
14847 else
14848#endif
14849 {
14850 /* When not using multi-byte chars we can do it faster. */
14851 p = vim_strchr(fromstr, *instr);
14852 if (p != NULL)
14853 ga_append(&ga, tostr[p - fromstr]);
14854 else
14855 ga_append(&ga, *instr);
14856 ++instr;
14857 }
14858 }
14859
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014860 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014861}
14862
14863/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014864 * "type(expr)" function
14865 */
14866 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014867f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014868 typval_T *argvars;
14869 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014870{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014871 int n;
14872
14873 switch (argvars[0].v_type)
14874 {
14875 case VAR_NUMBER: n = 0; break;
14876 case VAR_STRING: n = 1; break;
14877 case VAR_FUNC: n = 2; break;
14878 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014879 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014880 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
14881 }
14882 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014883}
14884
14885/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014886 * "values(dict)" function
14887 */
14888 static void
14889f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014890 typval_T *argvars;
14891 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014892{
14893 dict_list(argvars, rettv, 1);
14894}
14895
14896/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014897 * "virtcol(string)" function
14898 */
14899 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014900f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014901 typval_T *argvars;
14902 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014903{
14904 colnr_T vcol = 0;
14905 pos_T *fp;
14906
14907 fp = var2fpos(&argvars[0], FALSE);
14908 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count)
14909 {
14910 getvvcol(curwin, fp, NULL, NULL, &vcol);
14911 ++vcol;
14912 }
14913
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014914 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014915}
14916
14917/*
14918 * "visualmode()" function
14919 */
14920/*ARGSUSED*/
14921 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014922f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014923 typval_T *argvars;
14924 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014925{
14926#ifdef FEAT_VISUAL
14927 char_u str[2];
14928
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014929 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014930 str[0] = curbuf->b_visual_mode_eval;
14931 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014932 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014933
14934 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014935 if ((argvars[0].v_type == VAR_NUMBER
14936 && argvars[0].vval.v_number != 0)
14937 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014938 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014939 curbuf->b_visual_mode_eval = NUL;
14940#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014941 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014942#endif
14943}
14944
14945/*
14946 * "winbufnr(nr)" function
14947 */
14948 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014949f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014950 typval_T *argvars;
14951 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014952{
14953 win_T *wp;
14954
14955 wp = find_win_by_nr(&argvars[0]);
14956 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014957 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014958 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014959 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014960}
14961
14962/*
14963 * "wincol()" function
14964 */
14965/*ARGSUSED*/
14966 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014967f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014968 typval_T *argvars;
14969 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014970{
14971 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014972 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014973}
14974
14975/*
14976 * "winheight(nr)" function
14977 */
14978 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014979f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014980 typval_T *argvars;
14981 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014982{
14983 win_T *wp;
14984
14985 wp = find_win_by_nr(&argvars[0]);
14986 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014987 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014988 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014989 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014990}
14991
14992/*
14993 * "winline()" function
14994 */
14995/*ARGSUSED*/
14996 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014997f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014998 typval_T *argvars;
14999 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015000{
15001 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015002 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015003}
15004
15005/*
15006 * "winnr()" function
15007 */
15008/* ARGSUSED */
15009 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015010f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015011 typval_T *argvars;
15012 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015013{
15014 int nr = 1;
15015#ifdef FEAT_WINDOWS
15016 win_T *wp;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015017 win_T *twin = curwin;
15018 char_u *arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015019
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015020 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015021 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015022 arg = get_tv_string_chk(&argvars[0]);
15023 if (arg == NULL)
15024 nr = 0; /* type error; errmsg already given */
15025 else if (STRCMP(arg, "$") == 0)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015026 twin = lastwin;
15027 else if (STRCMP(arg, "#") == 0)
15028 {
15029 twin = prevwin;
15030 if (prevwin == NULL)
15031 nr = 0;
15032 }
15033 else
15034 {
15035 EMSG2(_(e_invexpr2), arg);
15036 nr = 0;
15037 }
15038 }
15039
15040 if (nr > 0)
15041 for (wp = firstwin; wp != twin; wp = wp->w_next)
15042 ++nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015043#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015044 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015045}
15046
15047/*
15048 * "winrestcmd()" function
15049 */
15050/* ARGSUSED */
15051 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015052f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015053 typval_T *argvars;
15054 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015055{
15056#ifdef FEAT_WINDOWS
15057 win_T *wp;
15058 int winnr = 1;
15059 garray_T ga;
15060 char_u buf[50];
15061
15062 ga_init2(&ga, (int)sizeof(char), 70);
15063 for (wp = firstwin; wp != NULL; wp = wp->w_next)
15064 {
15065 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
15066 ga_concat(&ga, buf);
15067# ifdef FEAT_VERTSPLIT
15068 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
15069 ga_concat(&ga, buf);
15070# endif
15071 ++winnr;
15072 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000015073 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015074
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015075 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015076#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015077 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015078#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015079 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015080}
15081
15082/*
15083 * "winwidth(nr)" function
15084 */
15085 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015086f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015087 typval_T *argvars;
15088 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015089{
15090 win_T *wp;
15091
15092 wp = find_win_by_nr(&argvars[0]);
15093 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015094 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015095 else
15096#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015097 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015098#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015099 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015100#endif
15101}
15102
Bram Moolenaar071d4272004-06-13 20:20:40 +000015103/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015104 * "writefile()" function
15105 */
15106 static void
15107f_writefile(argvars, rettv)
15108 typval_T *argvars;
15109 typval_T *rettv;
15110{
15111 int binary = FALSE;
15112 char_u *fname;
15113 FILE *fd;
15114 listitem_T *li;
15115 char_u *s;
15116 int ret = 0;
15117 int c;
15118
15119 if (argvars[0].v_type != VAR_LIST)
15120 {
15121 EMSG2(_(e_listarg), "writefile()");
15122 return;
15123 }
15124 if (argvars[0].vval.v_list == NULL)
15125 return;
15126
15127 if (argvars[2].v_type != VAR_UNKNOWN
15128 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
15129 binary = TRUE;
15130
15131 /* Always open the file in binary mode, library functions have a mind of
15132 * their own about CR-LF conversion. */
15133 fname = get_tv_string(&argvars[1]);
15134 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
15135 {
15136 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
15137 ret = -1;
15138 }
15139 else
15140 {
15141 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
15142 li = li->li_next)
15143 {
15144 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
15145 {
15146 if (*s == '\n')
15147 c = putc(NUL, fd);
15148 else
15149 c = putc(*s, fd);
15150 if (c == EOF)
15151 {
15152 ret = -1;
15153 break;
15154 }
15155 }
15156 if (!binary || li->li_next != NULL)
15157 if (putc('\n', fd) == EOF)
15158 {
15159 ret = -1;
15160 break;
15161 }
15162 if (ret < 0)
15163 {
15164 EMSG(_(e_write));
15165 break;
15166 }
15167 }
15168 fclose(fd);
15169 }
15170
15171 rettv->vval.v_number = ret;
15172}
15173
15174/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015175 * Translate a String variable into a position.
15176 */
15177 static pos_T *
15178var2fpos(varp, lnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000015179 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015180 int lnum; /* TRUE when $ is last line */
15181{
15182 char_u *name;
15183 static pos_T pos;
15184 pos_T *pp;
15185
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015186 name = get_tv_string_chk(varp);
15187 if (name == NULL)
15188 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015189 if (name[0] == '.') /* cursor */
15190 return &curwin->w_cursor;
15191 if (name[0] == '\'') /* mark */
15192 {
15193 pp = getmark(name[1], FALSE);
15194 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
15195 return NULL;
15196 return pp;
15197 }
15198 if (name[0] == '$') /* last column or line */
15199 {
15200 if (lnum)
15201 {
15202 pos.lnum = curbuf->b_ml.ml_line_count;
15203 pos.col = 0;
15204 }
15205 else
15206 {
15207 pos.lnum = curwin->w_cursor.lnum;
15208 pos.col = (colnr_T)STRLEN(ml_get_curline());
15209 }
15210 return &pos;
15211 }
15212 return NULL;
15213}
15214
15215/*
15216 * Get the length of an environment variable name.
15217 * Advance "arg" to the first character after the name.
15218 * Return 0 for error.
15219 */
15220 static int
15221get_env_len(arg)
15222 char_u **arg;
15223{
15224 char_u *p;
15225 int len;
15226
15227 for (p = *arg; vim_isIDc(*p); ++p)
15228 ;
15229 if (p == *arg) /* no name found */
15230 return 0;
15231
15232 len = (int)(p - *arg);
15233 *arg = p;
15234 return len;
15235}
15236
15237/*
15238 * Get the length of the name of a function or internal variable.
15239 * "arg" is advanced to the first non-white character after the name.
15240 * Return 0 if something is wrong.
15241 */
15242 static int
15243get_id_len(arg)
15244 char_u **arg;
15245{
15246 char_u *p;
15247 int len;
15248
15249 /* Find the end of the name. */
15250 for (p = *arg; eval_isnamec(*p); ++p)
15251 ;
15252 if (p == *arg) /* no name found */
15253 return 0;
15254
15255 len = (int)(p - *arg);
15256 *arg = skipwhite(p);
15257
15258 return len;
15259}
15260
15261/*
Bram Moolenaara7043832005-01-21 11:56:39 +000015262 * Get the length of the name of a variable or function.
15263 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000015264 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015265 * Return -1 if curly braces expansion failed.
15266 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015267 * If the name contains 'magic' {}'s, expand them and return the
15268 * expanded name in an allocated string via 'alias' - caller must free.
15269 */
15270 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015271get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015272 char_u **arg;
15273 char_u **alias;
15274 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015275 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015276{
15277 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015278 char_u *p;
15279 char_u *expr_start;
15280 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015281
15282 *alias = NULL; /* default to no alias */
15283
15284 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
15285 && (*arg)[2] == (int)KE_SNR)
15286 {
15287 /* hard coded <SNR>, already translated */
15288 *arg += 3;
15289 return get_id_len(arg) + 3;
15290 }
15291 len = eval_fname_script(*arg);
15292 if (len > 0)
15293 {
15294 /* literal "<SID>", "s:" or "<SNR>" */
15295 *arg += len;
15296 }
15297
Bram Moolenaar071d4272004-06-13 20:20:40 +000015298 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015299 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015300 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015301 p = find_name_end(*arg, &expr_start, &expr_end,
15302 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015303 if (expr_start != NULL)
15304 {
15305 char_u *temp_string;
15306
15307 if (!evaluate)
15308 {
15309 len += (int)(p - *arg);
15310 *arg = skipwhite(p);
15311 return len;
15312 }
15313
15314 /*
15315 * Include any <SID> etc in the expanded string:
15316 * Thus the -len here.
15317 */
15318 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
15319 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015320 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015321 *alias = temp_string;
15322 *arg = skipwhite(p);
15323 return (int)STRLEN(temp_string);
15324 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015325
15326 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015327 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015328 EMSG2(_(e_invexpr2), *arg);
15329
15330 return len;
15331}
15332
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015333/*
15334 * Find the end of a variable or function name, taking care of magic braces.
15335 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
15336 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015337 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015338 * Return a pointer to just after the name. Equal to "arg" if there is no
15339 * valid name.
15340 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015341 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015342find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015343 char_u *arg;
15344 char_u **expr_start;
15345 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015346 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015347{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015348 int mb_nest = 0;
15349 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015350 char_u *p;
15351
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015352 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015353 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015354 *expr_start = NULL;
15355 *expr_end = NULL;
15356 }
15357
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015358 /* Quick check for valid starting character. */
15359 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
15360 return arg;
15361
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015362 for (p = arg; *p != NUL
15363 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015364 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015365 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015366 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000015367 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015368 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000015369 if (*p == '\'')
15370 {
15371 /* skip over 'string' to avoid counting [ and ] inside it. */
15372 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
15373 ;
15374 if (*p == NUL)
15375 break;
15376 }
15377 else if (*p == '"')
15378 {
15379 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
15380 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
15381 if (*p == '\\' && p[1] != NUL)
15382 ++p;
15383 if (*p == NUL)
15384 break;
15385 }
15386
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015387 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015388 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015389 if (*p == '[')
15390 ++br_nest;
15391 else if (*p == ']')
15392 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015393 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000015394
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015395 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015396 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015397 if (*p == '{')
15398 {
15399 mb_nest++;
15400 if (expr_start != NULL && *expr_start == NULL)
15401 *expr_start = p;
15402 }
15403 else if (*p == '}')
15404 {
15405 mb_nest--;
15406 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
15407 *expr_end = p;
15408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015410 }
15411
15412 return p;
15413}
15414
15415/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015416 * Expands out the 'magic' {}'s in a variable/function name.
15417 * Note that this can call itself recursively, to deal with
15418 * constructs like foo{bar}{baz}{bam}
15419 * The four pointer arguments point to "foo{expre}ss{ion}bar"
15420 * "in_start" ^
15421 * "expr_start" ^
15422 * "expr_end" ^
15423 * "in_end" ^
15424 *
15425 * Returns a new allocated string, which the caller must free.
15426 * Returns NULL for failure.
15427 */
15428 static char_u *
15429make_expanded_name(in_start, expr_start, expr_end, in_end)
15430 char_u *in_start;
15431 char_u *expr_start;
15432 char_u *expr_end;
15433 char_u *in_end;
15434{
15435 char_u c1;
15436 char_u *retval = NULL;
15437 char_u *temp_result;
15438 char_u *nextcmd = NULL;
15439
15440 if (expr_end == NULL || in_end == NULL)
15441 return NULL;
15442 *expr_start = NUL;
15443 *expr_end = NUL;
15444 c1 = *in_end;
15445 *in_end = NUL;
15446
15447 temp_result = eval_to_string(expr_start + 1, &nextcmd);
15448 if (temp_result != NULL && nextcmd == NULL)
15449 {
15450 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
15451 + (in_end - expr_end) + 1));
15452 if (retval != NULL)
15453 {
15454 STRCPY(retval, in_start);
15455 STRCAT(retval, temp_result);
15456 STRCAT(retval, expr_end + 1);
15457 }
15458 }
15459 vim_free(temp_result);
15460
15461 *in_end = c1; /* put char back for error messages */
15462 *expr_start = '{';
15463 *expr_end = '}';
15464
15465 if (retval != NULL)
15466 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015467 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015468 if (expr_start != NULL)
15469 {
15470 /* Further expansion! */
15471 temp_result = make_expanded_name(retval, expr_start,
15472 expr_end, temp_result);
15473 vim_free(retval);
15474 retval = temp_result;
15475 }
15476 }
15477
15478 return retval;
15479}
15480
15481/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015482 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000015483 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015484 */
15485 static int
15486eval_isnamec(c)
15487 int c;
15488{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015489 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
15490}
15491
15492/*
15493 * Return TRUE if character "c" can be used as the first character in a
15494 * variable or function name (excluding '{' and '}').
15495 */
15496 static int
15497eval_isnamec1(c)
15498 int c;
15499{
15500 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000015501}
15502
15503/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015504 * Set number v: variable to "val".
15505 */
15506 void
15507set_vim_var_nr(idx, val)
15508 int idx;
15509 long val;
15510{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015511 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015512}
15513
15514/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015515 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015516 */
15517 long
15518get_vim_var_nr(idx)
15519 int idx;
15520{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015521 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015522}
15523
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015524#if defined(FEAT_AUTOCMD) || defined(PROTO)
15525/*
15526 * Get string v: variable value. Uses a static buffer, can only be used once.
15527 */
15528 char_u *
15529get_vim_var_str(idx)
15530 int idx;
15531{
15532 return get_tv_string(&vimvars[idx].vv_tv);
15533}
15534#endif
15535
Bram Moolenaar071d4272004-06-13 20:20:40 +000015536/*
15537 * Set v:count, v:count1 and v:prevcount.
15538 */
15539 void
15540set_vcount(count, count1)
15541 long count;
15542 long count1;
15543{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015544 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
15545 vimvars[VV_COUNT].vv_nr = count;
15546 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015547}
15548
15549/*
15550 * Set string v: variable to a copy of "val".
15551 */
15552 void
15553set_vim_var_string(idx, val, len)
15554 int idx;
15555 char_u *val;
15556 int len; /* length of "val" to use or -1 (whole string) */
15557{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015558 /* Need to do this (at least) once, since we can't initialize a union.
15559 * Will always be invoked when "v:progname" is set. */
15560 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
15561
Bram Moolenaare9a41262005-01-15 22:18:47 +000015562 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015563 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015564 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015565 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015566 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015567 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000015568 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015569}
15570
15571/*
15572 * Set v:register if needed.
15573 */
15574 void
15575set_reg_var(c)
15576 int c;
15577{
15578 char_u regname;
15579
15580 if (c == 0 || c == ' ')
15581 regname = '"';
15582 else
15583 regname = c;
15584 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000015585 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015586 set_vim_var_string(VV_REG, &regname, 1);
15587}
15588
15589/*
15590 * Get or set v:exception. If "oldval" == NULL, return the current value.
15591 * Otherwise, restore the value to "oldval" and return NULL.
15592 * Must always be called in pairs to save and restore v:exception! Does not
15593 * take care of memory allocations.
15594 */
15595 char_u *
15596v_exception(oldval)
15597 char_u *oldval;
15598{
15599 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015600 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015601
Bram Moolenaare9a41262005-01-15 22:18:47 +000015602 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015603 return NULL;
15604}
15605
15606/*
15607 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
15608 * Otherwise, restore the value to "oldval" and return NULL.
15609 * Must always be called in pairs to save and restore v:throwpoint! Does not
15610 * take care of memory allocations.
15611 */
15612 char_u *
15613v_throwpoint(oldval)
15614 char_u *oldval;
15615{
15616 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015617 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015618
Bram Moolenaare9a41262005-01-15 22:18:47 +000015619 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015620 return NULL;
15621}
15622
15623#if defined(FEAT_AUTOCMD) || defined(PROTO)
15624/*
15625 * Set v:cmdarg.
15626 * If "eap" != NULL, use "eap" to generate the value and return the old value.
15627 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
15628 * Must always be called in pairs!
15629 */
15630 char_u *
15631set_cmdarg(eap, oldarg)
15632 exarg_T *eap;
15633 char_u *oldarg;
15634{
15635 char_u *oldval;
15636 char_u *newval;
15637 unsigned len;
15638
Bram Moolenaare9a41262005-01-15 22:18:47 +000015639 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015640 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015641 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015642 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000015643 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015644 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015645 }
15646
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015647 if (eap->force_bin == FORCE_BIN)
15648 len = 6;
15649 else if (eap->force_bin == FORCE_NOBIN)
15650 len = 8;
15651 else
15652 len = 0;
15653 if (eap->force_ff != 0)
15654 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
15655# ifdef FEAT_MBYTE
15656 if (eap->force_enc != 0)
15657 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
15658# endif
15659
15660 newval = alloc(len + 1);
15661 if (newval == NULL)
15662 return NULL;
15663
15664 if (eap->force_bin == FORCE_BIN)
15665 sprintf((char *)newval, " ++bin");
15666 else if (eap->force_bin == FORCE_NOBIN)
15667 sprintf((char *)newval, " ++nobin");
15668 else
15669 *newval = NUL;
15670 if (eap->force_ff != 0)
15671 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
15672 eap->cmd + eap->force_ff);
15673# ifdef FEAT_MBYTE
15674 if (eap->force_enc != 0)
15675 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
15676 eap->cmd + eap->force_enc);
15677# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000015678 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015679 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015680}
15681#endif
15682
15683/*
15684 * Get the value of internal variable "name".
15685 * Return OK or FAIL.
15686 */
15687 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015688get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015689 char_u *name;
15690 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000015691 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015692 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015693{
15694 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000015695 typval_T *tv = NULL;
15696 typval_T atv;
15697 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015698 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015699
15700 /* truncate the name, so that we can use strcmp() */
15701 cc = name[len];
15702 name[len] = NUL;
15703
15704 /*
15705 * Check for "b:changedtick".
15706 */
15707 if (STRCMP(name, "b:changedtick") == 0)
15708 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000015709 atv.v_type = VAR_NUMBER;
15710 atv.vval.v_number = curbuf->b_changedtick;
15711 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015712 }
15713
15714 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015715 * Check for user-defined variables.
15716 */
15717 else
15718 {
Bram Moolenaara7043832005-01-21 11:56:39 +000015719 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015720 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000015721 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015722 }
15723
Bram Moolenaare9a41262005-01-15 22:18:47 +000015724 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015725 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015726 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015727 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015728 ret = FAIL;
15729 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015730 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015731 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015732
15733 name[len] = cc;
15734
15735 return ret;
15736}
15737
15738/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015739 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
15740 * Also handle function call with Funcref variable: func(expr)
15741 * Can all be combined: dict.func(expr)[idx]['func'](expr)
15742 */
15743 static int
15744handle_subscript(arg, rettv, evaluate, verbose)
15745 char_u **arg;
15746 typval_T *rettv;
15747 int evaluate; /* do more than finding the end */
15748 int verbose; /* give error messages */
15749{
15750 int ret = OK;
15751 dict_T *selfdict = NULL;
15752 char_u *s;
15753 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000015754 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015755
15756 while (ret == OK
15757 && (**arg == '['
15758 || (**arg == '.' && rettv->v_type == VAR_DICT)
15759 || (**arg == '(' && rettv->v_type == VAR_FUNC))
15760 && !vim_iswhite(*(*arg - 1)))
15761 {
15762 if (**arg == '(')
15763 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000015764 /* need to copy the funcref so that we can clear rettv */
15765 functv = *rettv;
15766 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015767
15768 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000015769 s = functv.vval.v_string;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015770 ret = get_func_tv(s, STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000015771 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
15772 &len, evaluate, selfdict);
15773
15774 /* Clear the funcref afterwards, so that deleting it while
15775 * evaluating the arguments is possible (see test55). */
15776 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015777
15778 /* Stop the expression evaluation when immediately aborting on
15779 * error, or when an interrupt occurred or an exception was thrown
15780 * but not caught. */
15781 if (aborting())
15782 {
15783 if (ret == OK)
15784 clear_tv(rettv);
15785 ret = FAIL;
15786 }
15787 dict_unref(selfdict);
15788 selfdict = NULL;
15789 }
15790 else /* **arg == '[' || **arg == '.' */
15791 {
15792 dict_unref(selfdict);
15793 if (rettv->v_type == VAR_DICT)
15794 {
15795 selfdict = rettv->vval.v_dict;
15796 if (selfdict != NULL)
15797 ++selfdict->dv_refcount;
15798 }
15799 else
15800 selfdict = NULL;
15801 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
15802 {
15803 clear_tv(rettv);
15804 ret = FAIL;
15805 }
15806 }
15807 }
15808 dict_unref(selfdict);
15809 return ret;
15810}
15811
15812/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015813 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
15814 * value).
15815 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015816 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015817alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015818{
Bram Moolenaar33570922005-01-25 22:26:29 +000015819 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015820}
15821
15822/*
15823 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015824 * The string "s" must have been allocated, it is consumed.
15825 * Return NULL for out of memory, the variable otherwise.
15826 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015827 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015828alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015829 char_u *s;
15830{
Bram Moolenaar33570922005-01-25 22:26:29 +000015831 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015832
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015833 rettv = alloc_tv();
15834 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015835 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015836 rettv->v_type = VAR_STRING;
15837 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015838 }
15839 else
15840 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015841 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015842}
15843
15844/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015845 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015846 */
15847 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015848free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015849 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015850{
15851 if (varp != NULL)
15852 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015853 switch (varp->v_type)
15854 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015855 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015856 func_unref(varp->vval.v_string);
15857 /*FALLTHROUGH*/
15858 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015859 vim_free(varp->vval.v_string);
15860 break;
15861 case VAR_LIST:
15862 list_unref(varp->vval.v_list);
15863 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015864 case VAR_DICT:
15865 dict_unref(varp->vval.v_dict);
15866 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015867 case VAR_NUMBER:
15868 case VAR_UNKNOWN:
15869 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015870 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000015871 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015872 break;
15873 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015874 vim_free(varp);
15875 }
15876}
15877
15878/*
15879 * Free the memory for a variable value and set the value to NULL or 0.
15880 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000015881 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015882clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015883 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015884{
15885 if (varp != NULL)
15886 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015887 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015888 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015889 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015890 func_unref(varp->vval.v_string);
15891 /*FALLTHROUGH*/
15892 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015893 vim_free(varp->vval.v_string);
15894 varp->vval.v_string = NULL;
15895 break;
15896 case VAR_LIST:
15897 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015898 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015899 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015900 case VAR_DICT:
15901 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015902 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015903 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015904 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015905 varp->vval.v_number = 0;
15906 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015907 case VAR_UNKNOWN:
15908 break;
15909 default:
15910 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000015911 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015912 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015913 }
15914}
15915
15916/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015917 * Set the value of a variable to NULL without freeing items.
15918 */
15919 static void
15920init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015921 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015922{
15923 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000015924 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015925}
15926
15927/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015928 * Get the number value of a variable.
15929 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015930 * For incompatible types, return 0.
15931 * get_tv_number_chk() is similar to get_tv_number(), but informs the
15932 * caller of incompatible types: it sets *denote to TRUE if "denote"
15933 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015934 */
15935 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015936get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015937 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015938{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015939 int error = FALSE;
15940
15941 return get_tv_number_chk(varp, &error); /* return 0L on error */
15942}
15943
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015944 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015945get_tv_number_chk(varp, denote)
15946 typval_T *varp;
15947 int *denote;
15948{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015949 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015950
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015951 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015952 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015953 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015954 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015955 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015956 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015957 break;
15958 case VAR_STRING:
15959 if (varp->vval.v_string != NULL)
15960 vim_str2nr(varp->vval.v_string, NULL, NULL,
15961 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015962 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015963 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000015964 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015965 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015966 case VAR_DICT:
15967 EMSG(_("E728: Using a Dictionary as a number"));
15968 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015969 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015970 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015971 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015972 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015973 if (denote == NULL) /* useful for values that must be unsigned */
15974 n = -1;
15975 else
15976 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015977 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015978}
15979
15980/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000015981 * Get the lnum from the first argument.
15982 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015983 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015984 */
15985 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015986get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000015987 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015988{
Bram Moolenaar33570922005-01-25 22:26:29 +000015989 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015990 linenr_T lnum;
15991
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015992 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015993 if (lnum == 0) /* no valid number, try using line() */
15994 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015995 rettv.v_type = VAR_NUMBER;
15996 f_line(argvars, &rettv);
15997 lnum = rettv.vval.v_number;
15998 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015999 }
16000 return lnum;
16001}
16002
16003/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000016004 * Get the lnum from the first argument.
16005 * Also accepts "$", then "buf" is used.
16006 * Returns 0 on error.
16007 */
16008 static linenr_T
16009get_tv_lnum_buf(argvars, buf)
16010 typval_T *argvars;
16011 buf_T *buf;
16012{
16013 if (argvars[0].v_type == VAR_STRING
16014 && argvars[0].vval.v_string != NULL
16015 && argvars[0].vval.v_string[0] == '$'
16016 && buf != NULL)
16017 return buf->b_ml.ml_line_count;
16018 return get_tv_number_chk(&argvars[0], NULL);
16019}
16020
16021/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016022 * Get the string value of a variable.
16023 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000016024 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
16025 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016026 * If the String variable has never been set, return an empty string.
16027 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016028 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
16029 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016030 */
16031 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016032get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016033 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016034{
16035 static char_u mybuf[NUMBUFLEN];
16036
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016037 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016038}
16039
16040 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016041get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000016042 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016043 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016044{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016045 char_u *res = get_tv_string_buf_chk(varp, buf);
16046
16047 return res != NULL ? res : (char_u *)"";
16048}
16049
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016050 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016051get_tv_string_chk(varp)
16052 typval_T *varp;
16053{
16054 static char_u mybuf[NUMBUFLEN];
16055
16056 return get_tv_string_buf_chk(varp, mybuf);
16057}
16058
16059 static char_u *
16060get_tv_string_buf_chk(varp, buf)
16061 typval_T *varp;
16062 char_u *buf;
16063{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016064 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016065 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016066 case VAR_NUMBER:
16067 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
16068 return buf;
16069 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016070 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016071 break;
16072 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016073 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016074 break;
16075 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016076 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016077 break;
16078 case VAR_STRING:
16079 if (varp->vval.v_string != NULL)
16080 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016081 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016082 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016083 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016084 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016085 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016086 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016087}
16088
16089/*
16090 * Find variable "name" in the list of variables.
16091 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016092 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000016093 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000016094 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016095 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016096 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000016097find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016098 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000016099 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016100{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016101 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016102 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016103
Bram Moolenaara7043832005-01-21 11:56:39 +000016104 ht = find_var_ht(name, &varname);
16105 if (htp != NULL)
16106 *htp = ht;
16107 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016108 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016109 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016110}
16111
16112/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016113 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000016114 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016115 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016116 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016117find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000016118 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000016119 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016120 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000016121{
Bram Moolenaar33570922005-01-25 22:26:29 +000016122 hashitem_T *hi;
16123
16124 if (*varname == NUL)
16125 {
16126 /* Must be something like "s:", otherwise "ht" would be NULL. */
16127 switch (varname[-2])
16128 {
16129 case 's': return &SCRIPT_SV(current_SID).sv_var;
16130 case 'g': return &globvars_var;
16131 case 'v': return &vimvars_var;
16132 case 'b': return &curbuf->b_bufvar;
16133 case 'w': return &curwin->w_winvar;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016134 case 'l': return current_funccal == NULL
16135 ? NULL : &current_funccal->l_vars_var;
16136 case 'a': return current_funccal == NULL
16137 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000016138 }
16139 return NULL;
16140 }
Bram Moolenaara7043832005-01-21 11:56:39 +000016141
16142 hi = hash_find(ht, varname);
16143 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016144 {
16145 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000016146 * worked find the variable again. Don't auto-load a script if it was
16147 * loaded already, otherwise it would be loaded every time when
16148 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016149 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000016150 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016151 hi = hash_find(ht, varname);
16152 if (HASHITEM_EMPTY(hi))
16153 return NULL;
16154 }
Bram Moolenaar33570922005-01-25 22:26:29 +000016155 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000016156}
16157
16158/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016159 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000016160 * Set "varname" to the start of name without ':'.
16161 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016162 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000016163find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016164 char_u *name;
16165 char_u **varname;
16166{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000016167 hashitem_T *hi;
16168
Bram Moolenaar071d4272004-06-13 20:20:40 +000016169 if (name[1] != ':')
16170 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016171 /* The name must not start with a colon or #. */
16172 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016173 return NULL;
16174 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016175
16176 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000016177 hi = hash_find(&compat_hashtab, name);
16178 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000016179 return &compat_hashtab;
16180
Bram Moolenaar071d4272004-06-13 20:20:40 +000016181 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016182 return &globvarht; /* global variable */
16183 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016184 }
16185 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016186 if (*name == 'g') /* global variable */
16187 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016188 /* There must be no ':' or '#' in the rest of the name, unless g: is used
16189 */
16190 if (vim_strchr(name + 2, ':') != NULL
16191 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016192 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016193 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000016194 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016195 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000016196 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000016197 if (*name == 'v') /* v: variable */
16198 return &vimvarht;
16199 if (*name == 'a' && current_funccal != NULL) /* function argument */
16200 return &current_funccal->l_avars.dv_hashtab;
16201 if (*name == 'l' && current_funccal != NULL) /* local function variable */
16202 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016203 if (*name == 's' /* script variable */
16204 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
16205 return &SCRIPT_VARS(current_SID);
16206 return NULL;
16207}
16208
16209/*
16210 * Get the string value of a (global/local) variable.
16211 * Returns NULL when it doesn't exist.
16212 */
16213 char_u *
16214get_var_value(name)
16215 char_u *name;
16216{
Bram Moolenaar33570922005-01-25 22:26:29 +000016217 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016218
Bram Moolenaara7043832005-01-21 11:56:39 +000016219 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016220 if (v == NULL)
16221 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000016222 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016223}
16224
16225/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016226 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000016227 * sourcing this script and when executing functions defined in the script.
16228 */
16229 void
16230new_script_vars(id)
16231 scid_T id;
16232{
Bram Moolenaara7043832005-01-21 11:56:39 +000016233 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000016234 hashtab_T *ht;
16235 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000016236
Bram Moolenaar071d4272004-06-13 20:20:40 +000016237 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
16238 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016239 /* Re-allocating ga_data means that an ht_array pointing to
16240 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000016241 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000016242 for (i = 1; i <= ga_scripts.ga_len; ++i)
16243 {
16244 ht = &SCRIPT_VARS(i);
16245 if (ht->ht_mask == HT_INIT_SIZE - 1)
16246 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000016247 sv = &SCRIPT_SV(i);
16248 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000016249 }
16250
Bram Moolenaar071d4272004-06-13 20:20:40 +000016251 while (ga_scripts.ga_len < id)
16252 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016253 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
16254 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016255 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016256 }
16257 }
16258}
16259
16260/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016261 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
16262 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016263 */
16264 void
Bram Moolenaar33570922005-01-25 22:26:29 +000016265init_var_dict(dict, dict_var)
16266 dict_T *dict;
16267 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016268{
Bram Moolenaar33570922005-01-25 22:26:29 +000016269 hash_init(&dict->dv_hashtab);
16270 dict->dv_refcount = 99999;
16271 dict_var->di_tv.vval.v_dict = dict;
16272 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016273 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016274 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
16275 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016276}
16277
16278/*
16279 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000016280 * Frees all allocated variables and the value they contain.
16281 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016282 */
16283 void
Bram Moolenaara7043832005-01-21 11:56:39 +000016284vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000016285 hashtab_T *ht;
16286{
16287 vars_clear_ext(ht, TRUE);
16288}
16289
16290/*
16291 * Like vars_clear(), but only free the value if "free_val" is TRUE.
16292 */
16293 static void
16294vars_clear_ext(ht, free_val)
16295 hashtab_T *ht;
16296 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016297{
Bram Moolenaara7043832005-01-21 11:56:39 +000016298 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000016299 hashitem_T *hi;
16300 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016301
Bram Moolenaar33570922005-01-25 22:26:29 +000016302 hash_lock(ht);
Bram Moolenaara7043832005-01-21 11:56:39 +000016303 todo = ht->ht_used;
16304 for (hi = ht->ht_array; todo > 0; ++hi)
16305 {
16306 if (!HASHITEM_EMPTY(hi))
16307 {
16308 --todo;
16309
Bram Moolenaar33570922005-01-25 22:26:29 +000016310 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000016311 * ht_array might change then. hash_clear() takes care of it
16312 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016313 v = HI2DI(hi);
16314 if (free_val)
16315 clear_tv(&v->di_tv);
16316 if ((v->di_flags & DI_FLAGS_FIX) == 0)
16317 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000016318 }
16319 }
16320 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016321 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016322}
16323
Bram Moolenaara7043832005-01-21 11:56:39 +000016324/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016325 * Delete a variable from hashtab "ht" at item "hi".
16326 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000016327 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016328 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000016329delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000016330 hashtab_T *ht;
16331 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016332{
Bram Moolenaar33570922005-01-25 22:26:29 +000016333 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000016334
16335 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000016336 clear_tv(&di->di_tv);
16337 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016338}
16339
16340/*
16341 * List the value of one internal variable.
16342 */
16343 static void
16344list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000016345 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016346 char_u *prefix;
16347{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016348 char_u *tofree;
16349 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016350 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016351
Bram Moolenaar33570922005-01-25 22:26:29 +000016352 s = echo_string(&v->di_tv, &tofree, numbuf);
16353 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016354 s == NULL ? (char_u *)"" : s);
16355 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016356}
16357
Bram Moolenaar071d4272004-06-13 20:20:40 +000016358 static void
16359list_one_var_a(prefix, name, type, string)
16360 char_u *prefix;
16361 char_u *name;
16362 int type;
16363 char_u *string;
16364{
16365 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
16366 if (name != NULL) /* "a:" vars don't have a name stored */
16367 msg_puts(name);
16368 msg_putchar(' ');
16369 msg_advance(22);
16370 if (type == VAR_NUMBER)
16371 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016372 else if (type == VAR_FUNC)
16373 msg_putchar('*');
16374 else if (type == VAR_LIST)
16375 {
16376 msg_putchar('[');
16377 if (*string == '[')
16378 ++string;
16379 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016380 else if (type == VAR_DICT)
16381 {
16382 msg_putchar('{');
16383 if (*string == '{')
16384 ++string;
16385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016386 else
16387 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016388
Bram Moolenaar071d4272004-06-13 20:20:40 +000016389 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016390
16391 if (type == VAR_FUNC)
16392 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016393}
16394
16395/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016396 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000016397 * If the variable already exists, the value is updated.
16398 * Otherwise the variable is created.
16399 */
16400 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016401set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016402 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000016403 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016404 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016405{
Bram Moolenaar33570922005-01-25 22:26:29 +000016406 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016407 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016408 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000016409 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016410
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016411 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016412 {
16413 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
16414 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
16415 ? name[2] : name[0]))
16416 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016417 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016418 return;
16419 }
16420 if (function_exists(name))
16421 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016422 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016423 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016424 return;
16425 }
16426 }
16427
Bram Moolenaara7043832005-01-21 11:56:39 +000016428 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000016429 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000016430 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000016431 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000016432 return;
16433 }
16434
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016435 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016436 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016437 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016438 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016439 if (var_check_ro(v->di_flags, name)
16440 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000016441 return;
16442 if (v->di_tv.v_type != tv->v_type
16443 && !((v->di_tv.v_type == VAR_STRING
16444 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016445 && (tv->v_type == VAR_STRING
16446 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016447 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016448 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016449 return;
16450 }
Bram Moolenaar33570922005-01-25 22:26:29 +000016451
16452 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000016453 * Handle setting internal v: variables separately: we don't change
16454 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000016455 */
16456 if (ht == &vimvarht)
16457 {
16458 if (v->di_tv.v_type == VAR_STRING)
16459 {
16460 vim_free(v->di_tv.vval.v_string);
16461 if (copy || tv->v_type != VAR_STRING)
16462 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
16463 else
16464 {
16465 /* Take over the string to avoid an extra alloc/free. */
16466 v->di_tv.vval.v_string = tv->vval.v_string;
16467 tv->vval.v_string = NULL;
16468 }
16469 }
16470 else if (v->di_tv.v_type != VAR_NUMBER)
16471 EMSG2(_(e_intern2), "set_var()");
16472 else
16473 v->di_tv.vval.v_number = get_tv_number(tv);
16474 return;
16475 }
16476
16477 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016478 }
16479 else /* add a new variable */
16480 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000016481 /* Make sure the variable name is valid. */
16482 for (p = varname; *p != NUL; ++p)
16483 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p)))
16484 {
16485 EMSG2(_(e_illvar), varname);
16486 return;
16487 }
16488
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016489 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
16490 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000016491 if (v == NULL)
16492 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000016493 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000016494 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016495 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016496 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016497 return;
16498 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016499 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016500 }
Bram Moolenaara7043832005-01-21 11:56:39 +000016501
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016502 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000016503 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016504 else
16505 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016506 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016507 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016508 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016510}
16511
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016512/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016513 * Return TRUE if di_flags "flags" indicate read-only variable "name".
16514 * Also give an error message.
16515 */
16516 static int
16517var_check_ro(flags, name)
16518 int flags;
16519 char_u *name;
16520{
16521 if (flags & DI_FLAGS_RO)
16522 {
16523 EMSG2(_(e_readonlyvar), name);
16524 return TRUE;
16525 }
16526 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
16527 {
16528 EMSG2(_(e_readonlysbx), name);
16529 return TRUE;
16530 }
16531 return FALSE;
16532}
16533
16534/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016535 * Return TRUE if typeval "tv" is set to be locked (immutable).
16536 * Also give an error message, using "name".
16537 */
16538 static int
16539tv_check_lock(lock, name)
16540 int lock;
16541 char_u *name;
16542{
16543 if (lock & VAR_LOCKED)
16544 {
16545 EMSG2(_("E741: Value is locked: %s"),
16546 name == NULL ? (char_u *)_("Unknown") : name);
16547 return TRUE;
16548 }
16549 if (lock & VAR_FIXED)
16550 {
16551 EMSG2(_("E742: Cannot change value of %s"),
16552 name == NULL ? (char_u *)_("Unknown") : name);
16553 return TRUE;
16554 }
16555 return FALSE;
16556}
16557
16558/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016559 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016560 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016561 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016562 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016563 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016564copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000016565 typval_T *from;
16566 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016567{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016568 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016569 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016570 switch (from->v_type)
16571 {
16572 case VAR_NUMBER:
16573 to->vval.v_number = from->vval.v_number;
16574 break;
16575 case VAR_STRING:
16576 case VAR_FUNC:
16577 if (from->vval.v_string == NULL)
16578 to->vval.v_string = NULL;
16579 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016580 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016581 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016582 if (from->v_type == VAR_FUNC)
16583 func_ref(to->vval.v_string);
16584 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016585 break;
16586 case VAR_LIST:
16587 if (from->vval.v_list == NULL)
16588 to->vval.v_list = NULL;
16589 else
16590 {
16591 to->vval.v_list = from->vval.v_list;
16592 ++to->vval.v_list->lv_refcount;
16593 }
16594 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016595 case VAR_DICT:
16596 if (from->vval.v_dict == NULL)
16597 to->vval.v_dict = NULL;
16598 else
16599 {
16600 to->vval.v_dict = from->vval.v_dict;
16601 ++to->vval.v_dict->dv_refcount;
16602 }
16603 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016604 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016605 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016606 break;
16607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016608}
16609
16610/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000016611 * Make a copy of an item.
16612 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016613 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
16614 * reference to an already copied list/dict can be used.
16615 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016616 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016617 static int
16618item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000016619 typval_T *from;
16620 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016621 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016622 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016623{
16624 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016625 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016626
Bram Moolenaar33570922005-01-25 22:26:29 +000016627 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016628 {
16629 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016630 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016631 }
16632 ++recurse;
16633
16634 switch (from->v_type)
16635 {
16636 case VAR_NUMBER:
16637 case VAR_STRING:
16638 case VAR_FUNC:
16639 copy_tv(from, to);
16640 break;
16641 case VAR_LIST:
16642 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016643 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016644 if (from->vval.v_list == NULL)
16645 to->vval.v_list = NULL;
16646 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
16647 {
16648 /* use the copy made earlier */
16649 to->vval.v_list = from->vval.v_list->lv_copylist;
16650 ++to->vval.v_list->lv_refcount;
16651 }
16652 else
16653 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
16654 if (to->vval.v_list == NULL)
16655 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016656 break;
16657 case VAR_DICT:
16658 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016659 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016660 if (from->vval.v_dict == NULL)
16661 to->vval.v_dict = NULL;
16662 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
16663 {
16664 /* use the copy made earlier */
16665 to->vval.v_dict = from->vval.v_dict->dv_copydict;
16666 ++to->vval.v_dict->dv_refcount;
16667 }
16668 else
16669 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
16670 if (to->vval.v_dict == NULL)
16671 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016672 break;
16673 default:
16674 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016675 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016676 }
16677 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016678 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016679}
16680
16681/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016682 * ":echo expr1 ..." print each argument separated with a space, add a
16683 * newline at the end.
16684 * ":echon expr1 ..." print each argument plain.
16685 */
16686 void
16687ex_echo(eap)
16688 exarg_T *eap;
16689{
16690 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000016691 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016692 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016693 char_u *p;
16694 int needclr = TRUE;
16695 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016696 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016697
16698 if (eap->skip)
16699 ++emsg_skip;
16700 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
16701 {
16702 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016703 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016704 {
16705 /*
16706 * Report the invalid expression unless the expression evaluation
16707 * has been cancelled due to an aborting error, an interrupt, or an
16708 * exception.
16709 */
16710 if (!aborting())
16711 EMSG2(_(e_invexpr2), p);
16712 break;
16713 }
16714 if (!eap->skip)
16715 {
16716 if (atstart)
16717 {
16718 atstart = FALSE;
16719 /* Call msg_start() after eval1(), evaluating the expression
16720 * may cause a message to appear. */
16721 if (eap->cmdidx == CMD_echo)
16722 msg_start();
16723 }
16724 else if (eap->cmdidx == CMD_echo)
16725 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016726 p = echo_string(&rettv, &tofree, numbuf);
16727 if (p != NULL)
16728 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016729 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016730 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016731 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016732 if (*p != TAB && needclr)
16733 {
16734 /* remove any text still there from the command */
16735 msg_clr_eos();
16736 needclr = FALSE;
16737 }
16738 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016739 }
16740 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016741 {
16742#ifdef FEAT_MBYTE
16743 if (has_mbyte)
16744 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016745 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016746
16747 (void)msg_outtrans_len_attr(p, i, echo_attr);
16748 p += i - 1;
16749 }
16750 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000016751#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016752 (void)msg_outtrans_len_attr(p, 1, echo_attr);
16753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016754 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016755 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016756 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016757 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016758 arg = skipwhite(arg);
16759 }
16760 eap->nextcmd = check_nextcmd(arg);
16761
16762 if (eap->skip)
16763 --emsg_skip;
16764 else
16765 {
16766 /* remove text that may still be there from the command */
16767 if (needclr)
16768 msg_clr_eos();
16769 if (eap->cmdidx == CMD_echo)
16770 msg_end();
16771 }
16772}
16773
16774/*
16775 * ":echohl {name}".
16776 */
16777 void
16778ex_echohl(eap)
16779 exarg_T *eap;
16780{
16781 int id;
16782
16783 id = syn_name2id(eap->arg);
16784 if (id == 0)
16785 echo_attr = 0;
16786 else
16787 echo_attr = syn_id2attr(id);
16788}
16789
16790/*
16791 * ":execute expr1 ..." execute the result of an expression.
16792 * ":echomsg expr1 ..." Print a message
16793 * ":echoerr expr1 ..." Print an error
16794 * Each gets spaces around each argument and a newline at the end for
16795 * echo commands
16796 */
16797 void
16798ex_execute(eap)
16799 exarg_T *eap;
16800{
16801 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000016802 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016803 int ret = OK;
16804 char_u *p;
16805 garray_T ga;
16806 int len;
16807 int save_did_emsg;
16808
16809 ga_init2(&ga, 1, 80);
16810
16811 if (eap->skip)
16812 ++emsg_skip;
16813 while (*arg != NUL && *arg != '|' && *arg != '\n')
16814 {
16815 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016816 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016817 {
16818 /*
16819 * Report the invalid expression unless the expression evaluation
16820 * has been cancelled due to an aborting error, an interrupt, or an
16821 * exception.
16822 */
16823 if (!aborting())
16824 EMSG2(_(e_invexpr2), p);
16825 ret = FAIL;
16826 break;
16827 }
16828
16829 if (!eap->skip)
16830 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016831 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016832 len = (int)STRLEN(p);
16833 if (ga_grow(&ga, len + 2) == FAIL)
16834 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016835 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016836 ret = FAIL;
16837 break;
16838 }
16839 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016840 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016841 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016842 ga.ga_len += len;
16843 }
16844
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016845 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016846 arg = skipwhite(arg);
16847 }
16848
16849 if (ret != FAIL && ga.ga_data != NULL)
16850 {
16851 if (eap->cmdidx == CMD_echomsg)
16852 MSG_ATTR(ga.ga_data, echo_attr);
16853 else if (eap->cmdidx == CMD_echoerr)
16854 {
16855 /* We don't want to abort following commands, restore did_emsg. */
16856 save_did_emsg = did_emsg;
16857 EMSG((char_u *)ga.ga_data);
16858 if (!force_abort)
16859 did_emsg = save_did_emsg;
16860 }
16861 else if (eap->cmdidx == CMD_execute)
16862 do_cmdline((char_u *)ga.ga_data,
16863 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
16864 }
16865
16866 ga_clear(&ga);
16867
16868 if (eap->skip)
16869 --emsg_skip;
16870
16871 eap->nextcmd = check_nextcmd(arg);
16872}
16873
16874/*
16875 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
16876 * "arg" points to the "&" or '+' when called, to "option" when returning.
16877 * Returns NULL when no option name found. Otherwise pointer to the char
16878 * after the option name.
16879 */
16880 static char_u *
16881find_option_end(arg, opt_flags)
16882 char_u **arg;
16883 int *opt_flags;
16884{
16885 char_u *p = *arg;
16886
16887 ++p;
16888 if (*p == 'g' && p[1] == ':')
16889 {
16890 *opt_flags = OPT_GLOBAL;
16891 p += 2;
16892 }
16893 else if (*p == 'l' && p[1] == ':')
16894 {
16895 *opt_flags = OPT_LOCAL;
16896 p += 2;
16897 }
16898 else
16899 *opt_flags = 0;
16900
16901 if (!ASCII_ISALPHA(*p))
16902 return NULL;
16903 *arg = p;
16904
16905 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
16906 p += 4; /* termcap option */
16907 else
16908 while (ASCII_ISALPHA(*p))
16909 ++p;
16910 return p;
16911}
16912
16913/*
16914 * ":function"
16915 */
16916 void
16917ex_function(eap)
16918 exarg_T *eap;
16919{
16920 char_u *theline;
16921 int j;
16922 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016923 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016924 char_u *name = NULL;
16925 char_u *p;
16926 char_u *arg;
16927 garray_T newargs;
16928 garray_T newlines;
16929 int varargs = FALSE;
16930 int mustend = FALSE;
16931 int flags = 0;
16932 ufunc_T *fp;
16933 int indent;
16934 int nesting;
16935 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000016936 dictitem_T *v;
16937 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016938 static int func_nr = 0; /* number for nameless function */
16939 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016940 hashtab_T *ht;
16941 int todo;
16942 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016943
16944 /*
16945 * ":function" without argument: list functions.
16946 */
16947 if (ends_excmd(*eap->arg))
16948 {
16949 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016950 {
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000016951 todo = func_hashtab.ht_used;
16952 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016953 {
16954 if (!HASHITEM_EMPTY(hi))
16955 {
16956 --todo;
16957 fp = HI2UF(hi);
16958 if (!isdigit(*fp->uf_name))
16959 list_func_head(fp, FALSE);
16960 }
16961 }
16962 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016963 eap->nextcmd = check_nextcmd(eap->arg);
16964 return;
16965 }
16966
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016967 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016968 * ":function /pat": list functions matching pattern.
16969 */
16970 if (*eap->arg == '/')
16971 {
16972 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
16973 if (!eap->skip)
16974 {
16975 regmatch_T regmatch;
16976
16977 c = *p;
16978 *p = NUL;
16979 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
16980 *p = c;
16981 if (regmatch.regprog != NULL)
16982 {
16983 regmatch.rm_ic = p_ic;
16984
16985 todo = func_hashtab.ht_used;
16986 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
16987 {
16988 if (!HASHITEM_EMPTY(hi))
16989 {
16990 --todo;
16991 fp = HI2UF(hi);
16992 if (!isdigit(*fp->uf_name)
16993 && vim_regexec(&regmatch, fp->uf_name, 0))
16994 list_func_head(fp, FALSE);
16995 }
16996 }
16997 }
16998 }
16999 if (*p == '/')
17000 ++p;
17001 eap->nextcmd = check_nextcmd(p);
17002 return;
17003 }
17004
17005 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017006 * Get the function name. There are these situations:
17007 * func normal function name
17008 * "name" == func, "fudi.fd_dict" == NULL
17009 * dict.func new dictionary entry
17010 * "name" == NULL, "fudi.fd_dict" set,
17011 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
17012 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017013 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017014 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
17015 * dict.func existing dict entry that's not a Funcref
17016 * "name" == NULL, "fudi.fd_dict" set,
17017 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
17018 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017019 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017020 name = trans_function_name(&p, eap->skip, 0, &fudi);
17021 paren = (vim_strchr(p, '(') != NULL);
17022 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017023 {
17024 /*
17025 * Return on an invalid expression in braces, unless the expression
17026 * evaluation has been cancelled due to an aborting error, an
17027 * interrupt, or an exception.
17028 */
17029 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017030 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017031 if (!eap->skip && fudi.fd_newkey != NULL)
17032 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017033 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017034 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017036 else
17037 eap->skip = TRUE;
17038 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017039 /* An error in a function call during evaluation of an expression in magic
17040 * braces should not cause the function not to be defined. */
17041 saved_did_emsg = did_emsg;
17042 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017043
17044 /*
17045 * ":function func" with only function name: list function.
17046 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017047 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017048 {
17049 if (!ends_excmd(*skipwhite(p)))
17050 {
17051 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017052 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017053 }
17054 eap->nextcmd = check_nextcmd(p);
17055 if (eap->nextcmd != NULL)
17056 *p = NUL;
17057 if (!eap->skip && !got_int)
17058 {
17059 fp = find_func(name);
17060 if (fp != NULL)
17061 {
17062 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017063 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017064 {
17065 msg_putchar('\n');
17066 msg_outnum((long)(j + 1));
17067 if (j < 9)
17068 msg_putchar(' ');
17069 if (j < 99)
17070 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017071 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017072 out_flush(); /* show a line at a time */
17073 ui_breakcheck();
17074 }
17075 if (!got_int)
17076 {
17077 msg_putchar('\n');
17078 msg_puts((char_u *)" endfunction");
17079 }
17080 }
17081 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017082 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017083 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017084 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017085 }
17086
17087 /*
17088 * ":function name(arg1, arg2)" Define function.
17089 */
17090 p = skipwhite(p);
17091 if (*p != '(')
17092 {
17093 if (!eap->skip)
17094 {
17095 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017096 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017097 }
17098 /* attempt to continue by skipping some text */
17099 if (vim_strchr(p, '(') != NULL)
17100 p = vim_strchr(p, '(');
17101 }
17102 p = skipwhite(p + 1);
17103
17104 ga_init2(&newargs, (int)sizeof(char_u *), 3);
17105 ga_init2(&newlines, (int)sizeof(char_u *), 3);
17106
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017107 if (!eap->skip)
17108 {
17109 /* Check the name of the function. */
17110 if (name != NULL)
17111 arg = name;
17112 else
17113 arg = fudi.fd_newkey;
17114 if (arg != NULL)
17115 {
17116 if (*arg == K_SPECIAL)
17117 j = 3;
17118 else
17119 j = 0;
17120 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
17121 : eval_isnamec(arg[j])))
17122 ++j;
17123 if (arg[j] != NUL)
17124 emsg_funcname(_(e_invarg2), arg);
17125 }
17126 }
17127
Bram Moolenaar071d4272004-06-13 20:20:40 +000017128 /*
17129 * Isolate the arguments: "arg1, arg2, ...)"
17130 */
17131 while (*p != ')')
17132 {
17133 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
17134 {
17135 varargs = TRUE;
17136 p += 3;
17137 mustend = TRUE;
17138 }
17139 else
17140 {
17141 arg = p;
17142 while (ASCII_ISALNUM(*p) || *p == '_')
17143 ++p;
17144 if (arg == p || isdigit(*arg)
17145 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
17146 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
17147 {
17148 if (!eap->skip)
17149 EMSG2(_("E125: Illegal argument: %s"), arg);
17150 break;
17151 }
17152 if (ga_grow(&newargs, 1) == FAIL)
17153 goto erret;
17154 c = *p;
17155 *p = NUL;
17156 arg = vim_strsave(arg);
17157 if (arg == NULL)
17158 goto erret;
17159 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
17160 *p = c;
17161 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017162 if (*p == ',')
17163 ++p;
17164 else
17165 mustend = TRUE;
17166 }
17167 p = skipwhite(p);
17168 if (mustend && *p != ')')
17169 {
17170 if (!eap->skip)
17171 EMSG2(_(e_invarg2), eap->arg);
17172 break;
17173 }
17174 }
17175 ++p; /* skip the ')' */
17176
Bram Moolenaare9a41262005-01-15 22:18:47 +000017177 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017178 for (;;)
17179 {
17180 p = skipwhite(p);
17181 if (STRNCMP(p, "range", 5) == 0)
17182 {
17183 flags |= FC_RANGE;
17184 p += 5;
17185 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000017186 else if (STRNCMP(p, "dict", 4) == 0)
17187 {
17188 flags |= FC_DICT;
17189 p += 4;
17190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017191 else if (STRNCMP(p, "abort", 5) == 0)
17192 {
17193 flags |= FC_ABORT;
17194 p += 5;
17195 }
17196 else
17197 break;
17198 }
17199
17200 if (*p != NUL && *p != '"' && *p != '\n' && !eap->skip && !did_emsg)
17201 EMSG(_(e_trailing));
17202
17203 /*
17204 * Read the body of the function, until ":endfunction" is found.
17205 */
17206 if (KeyTyped)
17207 {
17208 /* Check if the function already exists, don't let the user type the
17209 * whole function before telling him it doesn't work! For a script we
17210 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017211 if (!eap->skip && !eap->forceit)
17212 {
17213 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
17214 EMSG(_(e_funcdict));
17215 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017216 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017218
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017219 if (!eap->skip && did_emsg)
17220 goto erret;
17221
Bram Moolenaar071d4272004-06-13 20:20:40 +000017222 msg_putchar('\n'); /* don't overwrite the function name */
17223 cmdline_row = msg_row;
17224 }
17225
17226 indent = 2;
17227 nesting = 0;
17228 for (;;)
17229 {
17230 msg_scroll = TRUE;
17231 need_wait_return = FALSE;
17232 if (eap->getline == NULL)
17233 theline = getcmdline(':', 0L, indent);
17234 else
17235 theline = eap->getline(':', eap->cookie, indent);
17236 if (KeyTyped)
17237 lines_left = Rows - 1;
17238 if (theline == NULL)
17239 {
17240 EMSG(_("E126: Missing :endfunction"));
17241 goto erret;
17242 }
17243
17244 if (skip_until != NULL)
17245 {
17246 /* between ":append" and "." and between ":python <<EOF" and "EOF"
17247 * don't check for ":endfunc". */
17248 if (STRCMP(theline, skip_until) == 0)
17249 {
17250 vim_free(skip_until);
17251 skip_until = NULL;
17252 }
17253 }
17254 else
17255 {
17256 /* skip ':' and blanks*/
17257 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
17258 ;
17259
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017260 /* Check for "endfunction". */
17261 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017262 {
17263 vim_free(theline);
17264 break;
17265 }
17266
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017267 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000017268 * at "end". */
17269 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
17270 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017271 else if (STRNCMP(p, "if", 2) == 0
17272 || STRNCMP(p, "wh", 2) == 0
17273 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000017274 || STRNCMP(p, "try", 3) == 0)
17275 indent += 2;
17276
17277 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017278 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017279 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017280 if (*p == '!')
17281 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017282 p += eval_fname_script(p);
17283 if (ASCII_ISALPHA(*p))
17284 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017285 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017286 if (*skipwhite(p) == '(')
17287 {
17288 ++nesting;
17289 indent += 2;
17290 }
17291 }
17292 }
17293
17294 /* Check for ":append" or ":insert". */
17295 p = skip_range(p, NULL);
17296 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
17297 || (p[0] == 'i'
17298 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
17299 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
17300 skip_until = vim_strsave((char_u *)".");
17301
17302 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
17303 arg = skipwhite(skiptowhite(p));
17304 if (arg[0] == '<' && arg[1] =='<'
17305 && ((p[0] == 'p' && p[1] == 'y'
17306 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
17307 || (p[0] == 'p' && p[1] == 'e'
17308 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
17309 || (p[0] == 't' && p[1] == 'c'
17310 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
17311 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
17312 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000017313 || (p[0] == 'm' && p[1] == 'z'
17314 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017315 ))
17316 {
17317 /* ":python <<" continues until a dot, like ":append" */
17318 p = skipwhite(arg + 2);
17319 if (*p == NUL)
17320 skip_until = vim_strsave((char_u *)".");
17321 else
17322 skip_until = vim_strsave(p);
17323 }
17324 }
17325
17326 /* Add the line to the function. */
17327 if (ga_grow(&newlines, 1) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017328 {
17329 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017330 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017331 }
17332
17333 /* Copy the line to newly allocated memory. get_one_sourceline()
17334 * allocates 250 bytes per line, this saves 80% on average. The cost
17335 * is an extra alloc/free. */
17336 p = vim_strsave(theline);
17337 if (p != NULL)
17338 {
17339 vim_free(theline);
17340 theline = p;
17341 }
17342
Bram Moolenaar071d4272004-06-13 20:20:40 +000017343 ((char_u **)(newlines.ga_data))[newlines.ga_len] = theline;
17344 newlines.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017345 }
17346
17347 /* Don't define the function when skipping commands or when an error was
17348 * detected. */
17349 if (eap->skip || did_emsg)
17350 goto erret;
17351
17352 /*
17353 * If there are no errors, add the function
17354 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017355 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017356 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017357 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000017358 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017359 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017360 emsg_funcname("E707: Function name conflicts with variable: %s",
17361 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017362 goto erret;
17363 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017364
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017365 fp = find_func(name);
17366 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017367 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017368 if (!eap->forceit)
17369 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017370 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017371 goto erret;
17372 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017373 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017374 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017375 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017376 name);
17377 goto erret;
17378 }
17379 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017380 ga_clear_strings(&(fp->uf_args));
17381 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017382 vim_free(name);
17383 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017385 }
17386 else
17387 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017388 char numbuf[20];
17389
17390 fp = NULL;
17391 if (fudi.fd_newkey == NULL && !eap->forceit)
17392 {
17393 EMSG(_(e_funcdict));
17394 goto erret;
17395 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000017396 if (fudi.fd_di == NULL)
17397 {
17398 /* Can't add a function to a locked dictionary */
17399 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
17400 goto erret;
17401 }
17402 /* Can't change an existing function if it is locked */
17403 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
17404 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017405
17406 /* Give the function a sequential number. Can only be used with a
17407 * Funcref! */
17408 vim_free(name);
17409 sprintf(numbuf, "%d", ++func_nr);
17410 name = vim_strsave((char_u *)numbuf);
17411 if (name == NULL)
17412 goto erret;
17413 }
17414
17415 if (fp == NULL)
17416 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017417 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017418 {
17419 int slen, plen;
17420 char_u *scriptname;
17421
17422 /* Check that the autoload name matches the script name. */
17423 j = FAIL;
17424 if (sourcing_name != NULL)
17425 {
17426 scriptname = autoload_name(name);
17427 if (scriptname != NULL)
17428 {
17429 p = vim_strchr(scriptname, '/');
17430 plen = STRLEN(p);
17431 slen = STRLEN(sourcing_name);
17432 if (slen > plen && fnamecmp(p,
17433 sourcing_name + slen - plen) == 0)
17434 j = OK;
17435 vim_free(scriptname);
17436 }
17437 }
17438 if (j == FAIL)
17439 {
17440 EMSG2(_("E746: Function name does not match script file name: %s"), name);
17441 goto erret;
17442 }
17443 }
17444
17445 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017446 if (fp == NULL)
17447 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017448
17449 if (fudi.fd_dict != NULL)
17450 {
17451 if (fudi.fd_di == NULL)
17452 {
17453 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017454 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017455 if (fudi.fd_di == NULL)
17456 {
17457 vim_free(fp);
17458 goto erret;
17459 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017460 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
17461 {
17462 vim_free(fudi.fd_di);
17463 goto erret;
17464 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017465 }
17466 else
17467 /* overwrite existing dict entry */
17468 clear_tv(&fudi.fd_di->di_tv);
17469 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017470 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017471 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017472 fp->uf_refcount = 1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017473 }
17474
Bram Moolenaar071d4272004-06-13 20:20:40 +000017475 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017476 STRCPY(fp->uf_name, name);
17477 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017478 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017479 fp->uf_args = newargs;
17480 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017481#ifdef FEAT_PROFILE
17482 fp->uf_tml_count = NULL;
17483 fp->uf_tml_total = NULL;
17484 fp->uf_tml_self = NULL;
17485 fp->uf_profiling = FALSE;
17486 if (prof_def_func())
17487 func_do_profile(fp);
17488#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017489 fp->uf_varargs = varargs;
17490 fp->uf_flags = flags;
17491 fp->uf_calls = 0;
17492 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017493 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017494
17495erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017496 ga_clear_strings(&newargs);
17497 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017498ret_free:
17499 vim_free(skip_until);
17500 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017501 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017502 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017503}
17504
17505/*
17506 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000017507 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017508 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017509 * flags:
17510 * TFN_INT: internal function name OK
17511 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000017512 * Advances "pp" to just after the function name (if no error).
17513 */
17514 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017515trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017516 char_u **pp;
17517 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017518 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000017519 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017520{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017521 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017522 char_u *start;
17523 char_u *end;
17524 int lead;
17525 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017526 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000017527 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017528
17529 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017530 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017531 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000017532
17533 /* Check for hard coded <SNR>: already translated function ID (from a user
17534 * command). */
17535 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
17536 && (*pp)[2] == (int)KE_SNR)
17537 {
17538 *pp += 3;
17539 len = get_id_len(pp) + 3;
17540 return vim_strnsave(start, len);
17541 }
17542
17543 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
17544 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017545 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000017546 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017547 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017548
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017549 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
17550 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017551 if (end == start)
17552 {
17553 if (!skip)
17554 EMSG(_("E129: Function name required"));
17555 goto theend;
17556 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017557 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017558 {
17559 /*
17560 * Report an invalid expression in braces, unless the expression
17561 * evaluation has been cancelled due to an aborting error, an
17562 * interrupt, or an exception.
17563 */
17564 if (!aborting())
17565 {
17566 if (end != NULL)
17567 EMSG2(_(e_invarg2), start);
17568 }
17569 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017570 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017571 goto theend;
17572 }
17573
17574 if (lv.ll_tv != NULL)
17575 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017576 if (fdp != NULL)
17577 {
17578 fdp->fd_dict = lv.ll_dict;
17579 fdp->fd_newkey = lv.ll_newkey;
17580 lv.ll_newkey = NULL;
17581 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017582 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017583 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
17584 {
17585 name = vim_strsave(lv.ll_tv->vval.v_string);
17586 *pp = end;
17587 }
17588 else
17589 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017590 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
17591 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017592 EMSG(_(e_funcref));
17593 else
17594 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017595 name = NULL;
17596 }
17597 goto theend;
17598 }
17599
17600 if (lv.ll_name == NULL)
17601 {
17602 /* Error found, but continue after the function name. */
17603 *pp = end;
17604 goto theend;
17605 }
17606
17607 if (lv.ll_exp_name != NULL)
17608 len = STRLEN(lv.ll_exp_name);
17609 else
Bram Moolenaara7043832005-01-21 11:56:39 +000017610 {
17611 if (lead == 2) /* skip over "s:" */
17612 lv.ll_name += 2;
17613 len = (int)(end - lv.ll_name);
17614 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017615
17616 /*
17617 * Copy the function name to allocated memory.
17618 * Accept <SID>name() inside a script, translate into <SNR>123_name().
17619 * Accept <SNR>123_name() outside a script.
17620 */
17621 if (skip)
17622 lead = 0; /* do nothing */
17623 else if (lead > 0)
17624 {
17625 lead = 3;
17626 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
17627 {
17628 if (current_SID <= 0)
17629 {
17630 EMSG(_(e_usingsid));
17631 goto theend;
17632 }
17633 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
17634 lead += (int)STRLEN(sid_buf);
17635 }
17636 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017637 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017638 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017639 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017640 goto theend;
17641 }
17642 name = alloc((unsigned)(len + lead + 1));
17643 if (name != NULL)
17644 {
17645 if (lead > 0)
17646 {
17647 name[0] = K_SPECIAL;
17648 name[1] = KS_EXTRA;
17649 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000017650 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017651 STRCPY(name + 3, sid_buf);
17652 }
17653 mch_memmove(name + lead, lv.ll_name, (size_t)len);
17654 name[len + lead] = NUL;
17655 }
17656 *pp = end;
17657
17658theend:
17659 clear_lval(&lv);
17660 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017661}
17662
17663/*
17664 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
17665 * Return 2 if "p" starts with "s:".
17666 * Return 0 otherwise.
17667 */
17668 static int
17669eval_fname_script(p)
17670 char_u *p;
17671{
17672 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
17673 || STRNICMP(p + 1, "SNR>", 4) == 0))
17674 return 5;
17675 if (p[0] == 's' && p[1] == ':')
17676 return 2;
17677 return 0;
17678}
17679
17680/*
17681 * Return TRUE if "p" starts with "<SID>" or "s:".
17682 * Only works if eval_fname_script() returned non-zero for "p"!
17683 */
17684 static int
17685eval_fname_sid(p)
17686 char_u *p;
17687{
17688 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
17689}
17690
17691/*
17692 * List the head of the function: "name(arg1, arg2)".
17693 */
17694 static void
17695list_func_head(fp, indent)
17696 ufunc_T *fp;
17697 int indent;
17698{
17699 int j;
17700
17701 msg_start();
17702 if (indent)
17703 MSG_PUTS(" ");
17704 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017705 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017706 {
17707 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017708 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017709 }
17710 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017711 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017712 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017713 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017714 {
17715 if (j)
17716 MSG_PUTS(", ");
17717 msg_puts(FUNCARG(fp, j));
17718 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017719 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017720 {
17721 if (j)
17722 MSG_PUTS(", ");
17723 MSG_PUTS("...");
17724 }
17725 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000017726 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000017727 if (p_verbose > 0)
17728 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017729}
17730
17731/*
17732 * Find a function by name, return pointer to it in ufuncs.
17733 * Return NULL for unknown function.
17734 */
17735 static ufunc_T *
17736find_func(name)
17737 char_u *name;
17738{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017739 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017740
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017741 hi = hash_find(&func_hashtab, name);
17742 if (!HASHITEM_EMPTY(hi))
17743 return HI2UF(hi);
17744 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017745}
17746
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017747#if defined(EXITFREE) || defined(PROTO)
17748 void
17749free_all_functions()
17750{
17751 hashitem_T *hi;
17752
17753 /* Need to start all over every time, because func_free() may change the
17754 * hash table. */
17755 while (func_hashtab.ht_used > 0)
17756 for (hi = func_hashtab.ht_array; ; ++hi)
17757 if (!HASHITEM_EMPTY(hi))
17758 {
17759 func_free(HI2UF(hi));
17760 break;
17761 }
17762}
17763#endif
17764
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017765/*
17766 * Return TRUE if a function "name" exists.
17767 */
17768 static int
17769function_exists(name)
17770 char_u *name;
17771{
17772 char_u *p = name;
17773 int n = FALSE;
17774
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017775 p = trans_function_name(&p, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017776 if (p != NULL)
17777 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017778 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017779 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017780 else
17781 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017782 vim_free(p);
17783 }
17784 return n;
17785}
17786
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017787/*
17788 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017789 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017790 */
17791 static int
17792builtin_function(name)
17793 char_u *name;
17794{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017795 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
17796 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017797}
17798
Bram Moolenaar05159a02005-02-26 23:04:13 +000017799#if defined(FEAT_PROFILE) || defined(PROTO)
17800/*
17801 * Start profiling function "fp".
17802 */
17803 static void
17804func_do_profile(fp)
17805 ufunc_T *fp;
17806{
17807 fp->uf_tm_count = 0;
17808 profile_zero(&fp->uf_tm_self);
17809 profile_zero(&fp->uf_tm_total);
17810 if (fp->uf_tml_count == NULL)
17811 fp->uf_tml_count = (int *)alloc_clear((unsigned)
17812 (sizeof(int) * fp->uf_lines.ga_len));
17813 if (fp->uf_tml_total == NULL)
17814 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
17815 (sizeof(proftime_T) * fp->uf_lines.ga_len));
17816 if (fp->uf_tml_self == NULL)
17817 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
17818 (sizeof(proftime_T) * fp->uf_lines.ga_len));
17819 fp->uf_tml_idx = -1;
17820 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
17821 || fp->uf_tml_self == NULL)
17822 return; /* out of memory */
17823
17824 fp->uf_profiling = TRUE;
17825}
17826
17827/*
17828 * Dump the profiling results for all functions in file "fd".
17829 */
17830 void
17831func_dump_profile(fd)
17832 FILE *fd;
17833{
17834 hashitem_T *hi;
17835 int todo;
17836 ufunc_T *fp;
17837 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000017838 ufunc_T **sorttab;
17839 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017840
17841 todo = func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000017842 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
17843
Bram Moolenaar05159a02005-02-26 23:04:13 +000017844 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
17845 {
17846 if (!HASHITEM_EMPTY(hi))
17847 {
17848 --todo;
17849 fp = HI2UF(hi);
17850 if (fp->uf_profiling)
17851 {
Bram Moolenaar73830342005-02-28 22:48:19 +000017852 if (sorttab != NULL)
17853 sorttab[st_len++] = fp;
17854
Bram Moolenaar05159a02005-02-26 23:04:13 +000017855 if (fp->uf_name[0] == K_SPECIAL)
17856 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
17857 else
17858 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
17859 if (fp->uf_tm_count == 1)
17860 fprintf(fd, "Called 1 time\n");
17861 else
17862 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
17863 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
17864 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
17865 fprintf(fd, "\n");
17866 fprintf(fd, "count total (s) self (s)\n");
17867
17868 for (i = 0; i < fp->uf_lines.ga_len; ++i)
17869 {
Bram Moolenaar73830342005-02-28 22:48:19 +000017870 prof_func_line(fd, fp->uf_tml_count[i],
17871 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017872 fprintf(fd, "%s\n", FUNCLINE(fp, i));
17873 }
17874 fprintf(fd, "\n");
17875 }
17876 }
17877 }
Bram Moolenaar73830342005-02-28 22:48:19 +000017878
17879 if (sorttab != NULL && st_len > 0)
17880 {
17881 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
17882 prof_total_cmp);
17883 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
17884 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
17885 prof_self_cmp);
17886 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
17887 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000017888}
Bram Moolenaar73830342005-02-28 22:48:19 +000017889
17890 static void
17891prof_sort_list(fd, sorttab, st_len, title, prefer_self)
17892 FILE *fd;
17893 ufunc_T **sorttab;
17894 int st_len;
17895 char *title;
17896 int prefer_self; /* when equal print only self time */
17897{
17898 int i;
17899 ufunc_T *fp;
17900
17901 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
17902 fprintf(fd, "count total (s) self (s) function\n");
17903 for (i = 0; i < 20 && i < st_len; ++i)
17904 {
17905 fp = sorttab[i];
17906 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
17907 prefer_self);
17908 if (fp->uf_name[0] == K_SPECIAL)
17909 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
17910 else
17911 fprintf(fd, " %s()\n", fp->uf_name);
17912 }
17913 fprintf(fd, "\n");
17914}
17915
17916/*
17917 * Print the count and times for one function or function line.
17918 */
17919 static void
17920prof_func_line(fd, count, total, self, prefer_self)
17921 FILE *fd;
17922 int count;
17923 proftime_T *total;
17924 proftime_T *self;
17925 int prefer_self; /* when equal print only self time */
17926{
17927 if (count > 0)
17928 {
17929 fprintf(fd, "%5d ", count);
17930 if (prefer_self && profile_equal(total, self))
17931 fprintf(fd, " ");
17932 else
17933 fprintf(fd, "%s ", profile_msg(total));
17934 if (!prefer_self && profile_equal(total, self))
17935 fprintf(fd, " ");
17936 else
17937 fprintf(fd, "%s ", profile_msg(self));
17938 }
17939 else
17940 fprintf(fd, " ");
17941}
17942
17943/*
17944 * Compare function for total time sorting.
17945 */
17946 static int
17947#ifdef __BORLANDC__
17948_RTLENTRYF
17949#endif
17950prof_total_cmp(s1, s2)
17951 const void *s1;
17952 const void *s2;
17953{
17954 ufunc_T *p1, *p2;
17955
17956 p1 = *(ufunc_T **)s1;
17957 p2 = *(ufunc_T **)s2;
17958 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
17959}
17960
17961/*
17962 * Compare function for self time sorting.
17963 */
17964 static int
17965#ifdef __BORLANDC__
17966_RTLENTRYF
17967#endif
17968prof_self_cmp(s1, s2)
17969 const void *s1;
17970 const void *s2;
17971{
17972 ufunc_T *p1, *p2;
17973
17974 p1 = *(ufunc_T **)s1;
17975 p2 = *(ufunc_T **)s2;
17976 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
17977}
17978
Bram Moolenaar05159a02005-02-26 23:04:13 +000017979#endif
17980
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017981/* The names of packages that once were loaded is remembered. */
17982static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
17983
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017984/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017985 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017986 * Return TRUE if a package was loaded.
17987 */
17988 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017989script_autoload(name, reload)
17990 char_u *name;
17991 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017992{
17993 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017994 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017995 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017996 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017997
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017998 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017999 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018000 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018001 return FALSE;
18002
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018003 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018004
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018005 /* Find the name in the list of previously loaded package names. Skip
18006 * "autoload/", it's always the same. */
18007 for (i = 0; i < ga_loaded.ga_len; ++i)
18008 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
18009 break;
18010 if (!reload && i < ga_loaded.ga_len)
18011 ret = FALSE; /* was loaded already */
18012 else
18013 {
18014 /* Remember the name if it wasn't loaded already. */
18015 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
18016 {
18017 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
18018 tofree = NULL;
18019 }
18020
18021 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000018022 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018023 ret = TRUE;
18024 }
18025
18026 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018027 return ret;
18028}
18029
18030/*
18031 * Return the autoload script name for a function or variable name.
18032 * Returns NULL when out of memory.
18033 */
18034 static char_u *
18035autoload_name(name)
18036 char_u *name;
18037{
18038 char_u *p;
18039 char_u *scriptname;
18040
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018041 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018042 scriptname = alloc((unsigned)(STRLEN(name) + 14));
18043 if (scriptname == NULL)
18044 return FALSE;
18045 STRCPY(scriptname, "autoload/");
18046 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018047 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018048 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018049 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018050 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018051 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018052}
18053
Bram Moolenaar071d4272004-06-13 20:20:40 +000018054#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
18055
18056/*
18057 * Function given to ExpandGeneric() to obtain the list of user defined
18058 * function names.
18059 */
18060 char_u *
18061get_user_func_name(xp, idx)
18062 expand_T *xp;
18063 int idx;
18064{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018065 static long_u done;
18066 static hashitem_T *hi;
18067 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018068
18069 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018070 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018071 done = 0;
18072 hi = func_hashtab.ht_array;
18073 }
18074 if (done < func_hashtab.ht_used)
18075 {
18076 if (done++ > 0)
18077 ++hi;
18078 while (HASHITEM_EMPTY(hi))
18079 ++hi;
18080 fp = HI2UF(hi);
18081
18082 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
18083 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018084
18085 cat_func_name(IObuff, fp);
18086 if (xp->xp_context != EXPAND_USER_FUNC)
18087 {
18088 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018089 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018090 STRCAT(IObuff, ")");
18091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018092 return IObuff;
18093 }
18094 return NULL;
18095}
18096
18097#endif /* FEAT_CMDL_COMPL */
18098
18099/*
18100 * Copy the function name of "fp" to buffer "buf".
18101 * "buf" must be able to hold the function name plus three bytes.
18102 * Takes care of script-local function names.
18103 */
18104 static void
18105cat_func_name(buf, fp)
18106 char_u *buf;
18107 ufunc_T *fp;
18108{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018109 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018110 {
18111 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018112 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018113 }
18114 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018115 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018116}
18117
18118/*
18119 * ":delfunction {name}"
18120 */
18121 void
18122ex_delfunction(eap)
18123 exarg_T *eap;
18124{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018125 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018126 char_u *p;
18127 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000018128 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018129
18130 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018131 name = trans_function_name(&p, eap->skip, 0, &fudi);
18132 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018133 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018134 {
18135 if (fudi.fd_dict != NULL && !eap->skip)
18136 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018137 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018139 if (!ends_excmd(*skipwhite(p)))
18140 {
18141 vim_free(name);
18142 EMSG(_(e_trailing));
18143 return;
18144 }
18145 eap->nextcmd = check_nextcmd(p);
18146 if (eap->nextcmd != NULL)
18147 *p = NUL;
18148
18149 if (!eap->skip)
18150 fp = find_func(name);
18151 vim_free(name);
18152
18153 if (!eap->skip)
18154 {
18155 if (fp == NULL)
18156 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000018157 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018158 return;
18159 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018160 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018161 {
18162 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
18163 return;
18164 }
18165
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018166 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018167 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018168 /* Delete the dict item that refers to the function, it will
18169 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018170 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018171 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018172 else
18173 func_free(fp);
18174 }
18175}
18176
18177/*
18178 * Free a function and remove it from the list of functions.
18179 */
18180 static void
18181func_free(fp)
18182 ufunc_T *fp;
18183{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018184 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018185
18186 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018187 ga_clear_strings(&(fp->uf_args));
18188 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000018189#ifdef FEAT_PROFILE
18190 vim_free(fp->uf_tml_count);
18191 vim_free(fp->uf_tml_total);
18192 vim_free(fp->uf_tml_self);
18193#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018194
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018195 /* remove the function from the function hashtable */
18196 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
18197 if (HASHITEM_EMPTY(hi))
18198 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018199 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018200 hash_remove(&func_hashtab, hi);
18201
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018202 vim_free(fp);
18203}
18204
18205/*
18206 * Unreference a Function: decrement the reference count and free it when it
18207 * becomes zero. Only for numbered functions.
18208 */
18209 static void
18210func_unref(name)
18211 char_u *name;
18212{
18213 ufunc_T *fp;
18214
18215 if (name != NULL && isdigit(*name))
18216 {
18217 fp = find_func(name);
18218 if (fp == NULL)
18219 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018220 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018221 {
18222 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018223 * when "uf_calls" becomes zero. */
18224 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018225 func_free(fp);
18226 }
18227 }
18228}
18229
18230/*
18231 * Count a reference to a Function.
18232 */
18233 static void
18234func_ref(name)
18235 char_u *name;
18236{
18237 ufunc_T *fp;
18238
18239 if (name != NULL && isdigit(*name))
18240 {
18241 fp = find_func(name);
18242 if (fp == NULL)
18243 EMSG2(_(e_intern2), "func_ref()");
18244 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018245 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018246 }
18247}
18248
18249/*
18250 * Call a user function.
18251 */
18252 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000018253call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018254 ufunc_T *fp; /* pointer to function */
18255 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000018256 typval_T *argvars; /* arguments */
18257 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018258 linenr_T firstline; /* first line of range */
18259 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000018260 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018261{
Bram Moolenaar33570922005-01-25 22:26:29 +000018262 char_u *save_sourcing_name;
18263 linenr_T save_sourcing_lnum;
18264 scid_T save_current_SID;
18265 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000018266 int save_did_emsg;
18267 static int depth = 0;
18268 dictitem_T *v;
18269 int fixvar_idx = 0; /* index in fixvar[] */
18270 int i;
18271 int ai;
18272 char_u numbuf[NUMBUFLEN];
18273 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018274#ifdef FEAT_PROFILE
18275 proftime_T wait_start;
18276#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018277
18278 /* If depth of calling is getting too high, don't execute the function */
18279 if (depth >= p_mfd)
18280 {
18281 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018282 rettv->v_type = VAR_NUMBER;
18283 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018284 return;
18285 }
18286 ++depth;
18287
18288 line_breakcheck(); /* check for CTRL-C hit */
18289
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018290 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000018291 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018292 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018293 fc.rettv = rettv;
18294 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018295 fc.linenr = 0;
18296 fc.returned = FALSE;
18297 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018298 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018299 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018300 fc.dbg_tick = debug_tick;
18301
Bram Moolenaar33570922005-01-25 22:26:29 +000018302 /*
18303 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
18304 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
18305 * each argument variable and saves a lot of time.
18306 */
18307 /*
18308 * Init l: variables.
18309 */
18310 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000018311 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018312 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018313 /* Set l:self to "selfdict". */
18314 v = &fc.fixvar[fixvar_idx++].var;
18315 STRCPY(v->di_key, "self");
18316 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
18317 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
18318 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018319 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000018320 v->di_tv.vval.v_dict = selfdict;
18321 ++selfdict->dv_refcount;
18322 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000018323
Bram Moolenaar33570922005-01-25 22:26:29 +000018324 /*
18325 * Init a: variables.
18326 * Set a:0 to "argcount".
18327 * Set a:000 to a list with room for the "..." arguments.
18328 */
18329 init_var_dict(&fc.l_avars, &fc.l_avars_var);
18330 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018331 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000018332 v = &fc.fixvar[fixvar_idx++].var;
18333 STRCPY(v->di_key, "000");
18334 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18335 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
18336 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018337 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018338 v->di_tv.vval.v_list = &fc.l_varlist;
18339 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
18340 fc.l_varlist.lv_refcount = 99999;
18341
18342 /*
18343 * Set a:firstline to "firstline" and a:lastline to "lastline".
18344 * Set a:name to named arguments.
18345 * Set a:N to the "..." arguments.
18346 */
18347 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
18348 (varnumber_T)firstline);
18349 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
18350 (varnumber_T)lastline);
18351 for (i = 0; i < argcount; ++i)
18352 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018353 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000018354 if (ai < 0)
18355 /* named argument a:name */
18356 name = FUNCARG(fp, i);
18357 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000018358 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018359 /* "..." argument a:1, a:2, etc. */
18360 sprintf((char *)numbuf, "%d", ai + 1);
18361 name = numbuf;
18362 }
18363 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
18364 {
18365 v = &fc.fixvar[fixvar_idx++].var;
18366 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18367 }
18368 else
18369 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018370 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
18371 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000018372 if (v == NULL)
18373 break;
18374 v->di_flags = DI_FLAGS_RO;
18375 }
18376 STRCPY(v->di_key, name);
18377 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
18378
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018379 /* Note: the values are copied directly to avoid alloc/free.
18380 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000018381 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018382 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018383
18384 if (ai >= 0 && ai < MAX_FUNC_ARGS)
18385 {
18386 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
18387 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018388 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018389 }
18390 }
18391
Bram Moolenaar071d4272004-06-13 20:20:40 +000018392 /* Don't redraw while executing the function. */
18393 ++RedrawingDisabled;
18394 save_sourcing_name = sourcing_name;
18395 save_sourcing_lnum = sourcing_lnum;
18396 sourcing_lnum = 1;
18397 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018398 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018399 if (sourcing_name != NULL)
18400 {
18401 if (save_sourcing_name != NULL
18402 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
18403 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
18404 else
18405 STRCPY(sourcing_name, "function ");
18406 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
18407
18408 if (p_verbose >= 12)
18409 {
18410 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018411 verbose_enter_scroll();
18412
Bram Moolenaar555b2802005-05-19 21:08:39 +000018413 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018414 if (p_verbose >= 14)
18415 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018416 char_u buf[MSG_BUF_LEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000018417 char_u numbuf[NUMBUFLEN];
18418 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018419
18420 msg_puts((char_u *)"(");
18421 for (i = 0; i < argcount; ++i)
18422 {
18423 if (i > 0)
18424 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018425 if (argvars[i].v_type == VAR_NUMBER)
18426 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018427 else
18428 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000018429 trunc_string(tv2string(&argvars[i], &tofree, numbuf),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018430 buf, MSG_BUF_CLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018431 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000018432 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018433 }
18434 }
18435 msg_puts((char_u *)")");
18436 }
18437 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018438
18439 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018440 --no_wait_return;
18441 }
18442 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000018443#ifdef FEAT_PROFILE
18444 if (do_profiling)
18445 {
18446 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
18447 func_do_profile(fp);
18448 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018449 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000018450 {
18451 ++fp->uf_tm_count;
18452 profile_start(&fp->uf_tm_start);
18453 profile_zero(&fp->uf_tm_children);
18454 }
18455 script_prof_save(&wait_start);
18456 }
18457#endif
18458
Bram Moolenaar071d4272004-06-13 20:20:40 +000018459 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018460 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018461 save_did_emsg = did_emsg;
18462 did_emsg = FALSE;
18463
18464 /* call do_cmdline() to execute the lines */
18465 do_cmdline(NULL, get_func_line, (void *)&fc,
18466 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
18467
18468 --RedrawingDisabled;
18469
18470 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018471 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018472 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018473 clear_tv(rettv);
18474 rettv->v_type = VAR_NUMBER;
18475 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018476 }
18477
Bram Moolenaar05159a02005-02-26 23:04:13 +000018478#ifdef FEAT_PROFILE
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018479 if (fp->uf_profiling || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000018480 {
18481 profile_end(&fp->uf_tm_start);
18482 profile_sub_wait(&wait_start, &fp->uf_tm_start);
18483 profile_add(&fp->uf_tm_total, &fp->uf_tm_start);
18484 profile_add(&fp->uf_tm_self, &fp->uf_tm_start);
18485 profile_sub(&fp->uf_tm_self, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018486 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018487 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018488 profile_add(&fc.caller->func->uf_tm_children, &fp->uf_tm_start);
18489 profile_add(&fc.caller->func->uf_tml_children, &fp->uf_tm_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018490 }
18491 }
18492#endif
18493
Bram Moolenaar071d4272004-06-13 20:20:40 +000018494 /* when being verbose, mention the return value */
18495 if (p_verbose >= 12)
18496 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018497 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018498 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018499
Bram Moolenaar071d4272004-06-13 20:20:40 +000018500 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000018501 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018502 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000018503 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
18504 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000018505 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000018506 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000018507 char_u buf[MSG_BUF_LEN];
18508 char_u numbuf[NUMBUFLEN];
18509 char_u *tofree;
18510
Bram Moolenaar555b2802005-05-19 21:08:39 +000018511 /* The value may be very long. Skip the middle part, so that we
18512 * have some idea how it starts and ends. smsg() would always
18513 * truncate it at the end. */
Bram Moolenaar758711c2005-02-02 23:11:38 +000018514 trunc_string(tv2string(fc.rettv, &tofree, numbuf),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018515 buf, MSG_BUF_CLEN);
Bram Moolenaar555b2802005-05-19 21:08:39 +000018516 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000018517 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018518 }
18519 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018520
18521 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018522 --no_wait_return;
18523 }
18524
18525 vim_free(sourcing_name);
18526 sourcing_name = save_sourcing_name;
18527 sourcing_lnum = save_sourcing_lnum;
18528 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018529#ifdef FEAT_PROFILE
18530 if (do_profiling)
18531 script_prof_restore(&wait_start);
18532#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018533
18534 if (p_verbose >= 12 && sourcing_name != NULL)
18535 {
18536 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018537 verbose_enter_scroll();
18538
Bram Moolenaar555b2802005-05-19 21:08:39 +000018539 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018540 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018541
18542 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018543 --no_wait_return;
18544 }
18545
18546 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018547 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018548
Bram Moolenaar33570922005-01-25 22:26:29 +000018549 /* The a: variables typevals were not alloced, only free the allocated
18550 * variables. */
18551 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
18552
18553 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018554 --depth;
18555}
18556
18557/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018558 * Add a number variable "name" to dict "dp" with value "nr".
18559 */
18560 static void
18561add_nr_var(dp, v, name, nr)
18562 dict_T *dp;
18563 dictitem_T *v;
18564 char *name;
18565 varnumber_T nr;
18566{
18567 STRCPY(v->di_key, name);
18568 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18569 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
18570 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018571 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018572 v->di_tv.vval.v_number = nr;
18573}
18574
18575/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018576 * ":return [expr]"
18577 */
18578 void
18579ex_return(eap)
18580 exarg_T *eap;
18581{
18582 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018583 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018584 int returning = FALSE;
18585
18586 if (current_funccal == NULL)
18587 {
18588 EMSG(_("E133: :return not inside a function"));
18589 return;
18590 }
18591
18592 if (eap->skip)
18593 ++emsg_skip;
18594
18595 eap->nextcmd = NULL;
18596 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018597 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018598 {
18599 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018600 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018601 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018602 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018603 }
18604 /* It's safer to return also on error. */
18605 else if (!eap->skip)
18606 {
18607 /*
18608 * Return unless the expression evaluation has been cancelled due to an
18609 * aborting error, an interrupt, or an exception.
18610 */
18611 if (!aborting())
18612 returning = do_return(eap, FALSE, TRUE, NULL);
18613 }
18614
18615 /* When skipping or the return gets pending, advance to the next command
18616 * in this line (!returning). Otherwise, ignore the rest of the line.
18617 * Following lines will be ignored by get_func_line(). */
18618 if (returning)
18619 eap->nextcmd = NULL;
18620 else if (eap->nextcmd == NULL) /* no argument */
18621 eap->nextcmd = check_nextcmd(arg);
18622
18623 if (eap->skip)
18624 --emsg_skip;
18625}
18626
18627/*
18628 * Return from a function. Possibly makes the return pending. Also called
18629 * for a pending return at the ":endtry" or after returning from an extra
18630 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000018631 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018632 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018633 * FALSE when the return gets pending.
18634 */
18635 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018636do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018637 exarg_T *eap;
18638 int reanimate;
18639 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018640 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018641{
18642 int idx;
18643 struct condstack *cstack = eap->cstack;
18644
18645 if (reanimate)
18646 /* Undo the return. */
18647 current_funccal->returned = FALSE;
18648
18649 /*
18650 * Cleanup (and inactivate) conditionals, but stop when a try conditional
18651 * not in its finally clause (which then is to be executed next) is found.
18652 * In this case, make the ":return" pending for execution at the ":endtry".
18653 * Otherwise, return normally.
18654 */
18655 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
18656 if (idx >= 0)
18657 {
18658 cstack->cs_pending[idx] = CSTP_RETURN;
18659
18660 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018661 /* A pending return again gets pending. "rettv" points to an
18662 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000018663 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018664 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018665 else
18666 {
18667 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018668 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018669 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018670 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018671
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018672 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018673 {
18674 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018675 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018676 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018677 else
18678 EMSG(_(e_outofmem));
18679 }
18680 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018681 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018682
18683 if (reanimate)
18684 {
18685 /* The pending return value could be overwritten by a ":return"
18686 * without argument in a finally clause; reset the default
18687 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018688 current_funccal->rettv->v_type = VAR_NUMBER;
18689 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018690 }
18691 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018692 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018693 }
18694 else
18695 {
18696 current_funccal->returned = TRUE;
18697
18698 /* If the return is carried out now, store the return value. For
18699 * a return immediately after reanimation, the value is already
18700 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018701 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018702 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018703 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000018704 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018705 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018706 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018707 }
18708 }
18709
18710 return idx < 0;
18711}
18712
18713/*
18714 * Free the variable with a pending return value.
18715 */
18716 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018717discard_pending_return(rettv)
18718 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018719{
Bram Moolenaar33570922005-01-25 22:26:29 +000018720 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018721}
18722
18723/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018724 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000018725 * is an allocated string. Used by report_pending() for verbose messages.
18726 */
18727 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018728get_return_cmd(rettv)
18729 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018730{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018731 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018732 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018733 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018734
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018735 if (rettv != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018736 s = echo_string((typval_T *)rettv, &tofree, numbuf);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018737 if (s == NULL)
18738 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018739
18740 STRCPY(IObuff, ":return ");
18741 STRNCPY(IObuff + 8, s, IOSIZE - 8);
18742 if (STRLEN(s) + 8 >= IOSIZE)
18743 STRCPY(IObuff + IOSIZE - 4, "...");
18744 vim_free(tofree);
18745 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018746}
18747
18748/*
18749 * Get next function line.
18750 * Called by do_cmdline() to get the next line.
18751 * Returns allocated string, or NULL for end of function.
18752 */
18753/* ARGSUSED */
18754 char_u *
18755get_func_line(c, cookie, indent)
18756 int c; /* not used */
18757 void *cookie;
18758 int indent; /* not used */
18759{
Bram Moolenaar33570922005-01-25 22:26:29 +000018760 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018761 ufunc_T *fp = fcp->func;
18762 char_u *retval;
18763 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018764
18765 /* If breakpoints have been added/deleted need to check for it. */
18766 if (fcp->dbg_tick != debug_tick)
18767 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000018768 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018769 sourcing_lnum);
18770 fcp->dbg_tick = debug_tick;
18771 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000018772#ifdef FEAT_PROFILE
18773 if (do_profiling)
18774 func_line_end(cookie);
18775#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018776
Bram Moolenaar05159a02005-02-26 23:04:13 +000018777 gap = &fp->uf_lines;
18778 if ((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000018779 retval = NULL;
18780 else if (fcp->returned || fcp->linenr >= gap->ga_len)
18781 retval = NULL;
18782 else
18783 {
18784 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
18785 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018786#ifdef FEAT_PROFILE
18787 if (do_profiling)
18788 func_line_start(cookie);
18789#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018790 }
18791
18792 /* Did we encounter a breakpoint? */
18793 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
18794 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000018795 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018796 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018797 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018798 sourcing_lnum);
18799 fcp->dbg_tick = debug_tick;
18800 }
18801
18802 return retval;
18803}
18804
Bram Moolenaar05159a02005-02-26 23:04:13 +000018805#if defined(FEAT_PROFILE) || defined(PROTO)
18806/*
18807 * Called when starting to read a function line.
18808 * "sourcing_lnum" must be correct!
18809 * When skipping lines it may not actually be executed, but we won't find out
18810 * until later and we need to store the time now.
18811 */
18812 void
18813func_line_start(cookie)
18814 void *cookie;
18815{
18816 funccall_T *fcp = (funccall_T *)cookie;
18817 ufunc_T *fp = fcp->func;
18818
18819 if (fp->uf_profiling && sourcing_lnum >= 1
18820 && sourcing_lnum <= fp->uf_lines.ga_len)
18821 {
18822 fp->uf_tml_idx = sourcing_lnum - 1;
18823 fp->uf_tml_execed = FALSE;
18824 profile_start(&fp->uf_tml_start);
18825 profile_zero(&fp->uf_tml_children);
18826 profile_get_wait(&fp->uf_tml_wait);
18827 }
18828}
18829
18830/*
18831 * Called when actually executing a function line.
18832 */
18833 void
18834func_line_exec(cookie)
18835 void *cookie;
18836{
18837 funccall_T *fcp = (funccall_T *)cookie;
18838 ufunc_T *fp = fcp->func;
18839
18840 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
18841 fp->uf_tml_execed = TRUE;
18842}
18843
18844/*
18845 * Called when done with a function line.
18846 */
18847 void
18848func_line_end(cookie)
18849 void *cookie;
18850{
18851 funccall_T *fcp = (funccall_T *)cookie;
18852 ufunc_T *fp = fcp->func;
18853
18854 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
18855 {
18856 if (fp->uf_tml_execed)
18857 {
18858 ++fp->uf_tml_count[fp->uf_tml_idx];
18859 profile_end(&fp->uf_tml_start);
18860 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
18861 profile_add(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start);
18862 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
18863 profile_sub(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_children);
18864 }
18865 fp->uf_tml_idx = -1;
18866 }
18867}
18868#endif
18869
Bram Moolenaar071d4272004-06-13 20:20:40 +000018870/*
18871 * Return TRUE if the currently active function should be ended, because a
18872 * return was encountered or an error occured. Used inside a ":while".
18873 */
18874 int
18875func_has_ended(cookie)
18876 void *cookie;
18877{
Bram Moolenaar33570922005-01-25 22:26:29 +000018878 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018879
18880 /* Ignore the "abort" flag if the abortion behavior has been changed due to
18881 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018882 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000018883 || fcp->returned);
18884}
18885
18886/*
18887 * return TRUE if cookie indicates a function which "abort"s on errors.
18888 */
18889 int
18890func_has_abort(cookie)
18891 void *cookie;
18892{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018893 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018894}
18895
18896#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
18897typedef enum
18898{
18899 VAR_FLAVOUR_DEFAULT,
18900 VAR_FLAVOUR_SESSION,
18901 VAR_FLAVOUR_VIMINFO
18902} var_flavour_T;
18903
18904static var_flavour_T var_flavour __ARGS((char_u *varname));
18905
18906 static var_flavour_T
18907var_flavour(varname)
18908 char_u *varname;
18909{
18910 char_u *p = varname;
18911
18912 if (ASCII_ISUPPER(*p))
18913 {
18914 while (*(++p))
18915 if (ASCII_ISLOWER(*p))
18916 return VAR_FLAVOUR_SESSION;
18917 return VAR_FLAVOUR_VIMINFO;
18918 }
18919 else
18920 return VAR_FLAVOUR_DEFAULT;
18921}
18922#endif
18923
18924#if defined(FEAT_VIMINFO) || defined(PROTO)
18925/*
18926 * Restore global vars that start with a capital from the viminfo file
18927 */
18928 int
18929read_viminfo_varlist(virp, writing)
18930 vir_T *virp;
18931 int writing;
18932{
18933 char_u *tab;
18934 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000018935 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018936
18937 if (!writing && (find_viminfo_parameter('!') != NULL))
18938 {
18939 tab = vim_strchr(virp->vir_line + 1, '\t');
18940 if (tab != NULL)
18941 {
18942 *tab++ = '\0'; /* isolate the variable name */
18943 if (*tab == 'S') /* string var */
18944 is_string = TRUE;
18945
18946 tab = vim_strchr(tab, '\t');
18947 if (tab != NULL)
18948 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018949 if (is_string)
18950 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000018951 tv.v_type = VAR_STRING;
18952 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018953 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018954 }
18955 else
18956 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000018957 tv.v_type = VAR_NUMBER;
18958 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018959 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000018960 set_var(virp->vir_line + 1, &tv, FALSE);
18961 if (is_string)
18962 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018963 }
18964 }
18965 }
18966
18967 return viminfo_readline(virp);
18968}
18969
18970/*
18971 * Write global vars that start with a capital to the viminfo file
18972 */
18973 void
18974write_viminfo_varlist(fp)
18975 FILE *fp;
18976{
Bram Moolenaar33570922005-01-25 22:26:29 +000018977 hashitem_T *hi;
18978 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000018979 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018980 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018981 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018982 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018983 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018984
18985 if (find_viminfo_parameter('!') == NULL)
18986 return;
18987
18988 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000018989
Bram Moolenaar33570922005-01-25 22:26:29 +000018990 todo = globvarht.ht_used;
18991 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018992 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018993 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018994 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018995 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000018996 this_var = HI2DI(hi);
18997 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018998 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018999 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000019000 {
19001 case VAR_STRING: s = "STR"; break;
19002 case VAR_NUMBER: s = "NUM"; break;
19003 default: continue;
19004 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019005 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019006 p = echo_string(&this_var->di_tv, &tofree, numbuf);
19007 if (p != NULL)
19008 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000019009 vim_free(tofree);
19010 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019011 }
19012 }
19013}
19014#endif
19015
19016#if defined(FEAT_SESSION) || defined(PROTO)
19017 int
19018store_session_globals(fd)
19019 FILE *fd;
19020{
Bram Moolenaar33570922005-01-25 22:26:29 +000019021 hashitem_T *hi;
19022 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000019023 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019024 char_u *p, *t;
19025
Bram Moolenaar33570922005-01-25 22:26:29 +000019026 todo = globvarht.ht_used;
19027 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019028 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019029 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019030 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019031 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019032 this_var = HI2DI(hi);
19033 if ((this_var->di_tv.v_type == VAR_NUMBER
19034 || this_var->di_tv.v_type == VAR_STRING)
19035 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019036 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019037 /* Escape special characters with a backslash. Turn a LF and
19038 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019039 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000019040 (char_u *)"\\\"\n\r");
19041 if (p == NULL) /* out of memory */
19042 break;
19043 for (t = p; *t != NUL; ++t)
19044 if (*t == '\n')
19045 *t = 'n';
19046 else if (*t == '\r')
19047 *t = 'r';
19048 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000019049 this_var->di_key,
19050 (this_var->di_tv.v_type == VAR_STRING) ? '"'
19051 : ' ',
19052 p,
19053 (this_var->di_tv.v_type == VAR_STRING) ? '"'
19054 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000019055 || put_eol(fd) == FAIL)
19056 {
19057 vim_free(p);
19058 return FAIL;
19059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019060 vim_free(p);
19061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019062 }
19063 }
19064 return OK;
19065}
19066#endif
19067
Bram Moolenaar661b1822005-07-28 22:36:45 +000019068/*
19069 * Display script name where an item was last set.
19070 * Should only be invoked when 'verbose' is non-zero.
19071 */
19072 void
19073last_set_msg(scriptID)
19074 scid_T scriptID;
19075{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019076 char_u *p;
19077
Bram Moolenaar661b1822005-07-28 22:36:45 +000019078 if (scriptID != 0)
19079 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019080 p = home_replace_save(NULL, get_scriptname(scriptID));
19081 if (p != NULL)
19082 {
19083 verbose_enter();
19084 MSG_PUTS(_("\n\tLast set from "));
19085 MSG_PUTS(p);
19086 vim_free(p);
19087 verbose_leave();
19088 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000019089 }
19090}
19091
Bram Moolenaar071d4272004-06-13 20:20:40 +000019092#endif /* FEAT_EVAL */
19093
19094#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
19095
19096
19097#ifdef WIN3264
19098/*
19099 * Functions for ":8" filename modifier: get 8.3 version of a filename.
19100 */
19101static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
19102static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
19103static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
19104
19105/*
19106 * Get the short pathname of a file.
19107 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
19108 */
19109 static int
19110get_short_pathname(fnamep, bufp, fnamelen)
19111 char_u **fnamep;
19112 char_u **bufp;
19113 int *fnamelen;
19114{
19115 int l,len;
19116 char_u *newbuf;
19117
19118 len = *fnamelen;
19119
19120 l = GetShortPathName(*fnamep, *fnamep, len);
19121 if (l > len - 1)
19122 {
19123 /* If that doesn't work (not enough space), then save the string
19124 * and try again with a new buffer big enough
19125 */
19126 newbuf = vim_strnsave(*fnamep, l);
19127 if (newbuf == NULL)
19128 return 0;
19129
19130 vim_free(*bufp);
19131 *fnamep = *bufp = newbuf;
19132
19133 l = GetShortPathName(*fnamep,*fnamep,l+1);
19134
19135 /* Really should always succeed, as the buffer is big enough */
19136 }
19137
19138 *fnamelen = l;
19139 return 1;
19140}
19141
19142/*
19143 * Create a short path name. Returns the length of the buffer it needs.
19144 * Doesn't copy over the end of the buffer passed in.
19145 */
19146 static int
19147shortpath_for_invalid_fname(fname, bufp, fnamelen)
19148 char_u **fname;
19149 char_u **bufp;
19150 int *fnamelen;
19151{
19152 char_u *s, *p, *pbuf2, *pbuf3;
19153 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019154 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019155
19156 /* Make a copy */
19157 len2 = *fnamelen;
19158 pbuf2 = vim_strnsave(*fname, len2);
19159 pbuf3 = NULL;
19160
19161 s = pbuf2 + len2 - 1; /* Find the end */
19162 slen = 1;
19163 plen = len2;
19164
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019165 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019166 {
19167 --s;
19168 ++slen;
19169 --plen;
19170 }
19171
19172 do
19173 {
19174 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019175 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019176 {
19177 --s;
19178 ++slen;
19179 --plen;
19180 }
19181 if (s <= pbuf2)
19182 break;
19183
19184 /* Remeber the character that is about to be blatted */
19185 ch = *s;
19186 *s = 0; /* get_short_pathname requires a null-terminated string */
19187
19188 /* Try it in situ */
19189 p = pbuf2;
19190 if (!get_short_pathname(&p, &pbuf3, &plen))
19191 {
19192 vim_free(pbuf2);
19193 return -1;
19194 }
19195 *s = ch; /* Preserve the string */
19196 } while (plen == 0);
19197
19198 if (plen > 0)
19199 {
19200 /* Remeber the length of the new string. */
19201 *fnamelen = len = plen + slen;
19202 vim_free(*bufp);
19203 if (len > len2)
19204 {
19205 /* If there's not enough space in the currently allocated string,
19206 * then copy it to a buffer big enough.
19207 */
19208 *fname= *bufp = vim_strnsave(p, len);
19209 if (*fname == NULL)
19210 return -1;
19211 }
19212 else
19213 {
19214 /* Transfer pbuf2 to being the main buffer (it's big enough) */
19215 *fname = *bufp = pbuf2;
19216 if (p != pbuf2)
19217 strncpy(*fname, p, plen);
19218 pbuf2 = NULL;
19219 }
19220 /* Concat the next bit */
19221 strncpy(*fname + plen, s, slen);
19222 (*fname)[len] = '\0';
19223 }
19224 vim_free(pbuf3);
19225 vim_free(pbuf2);
19226 return 0;
19227}
19228
19229/*
19230 * Get a pathname for a partial path.
19231 */
19232 static int
19233shortpath_for_partial(fnamep, bufp, fnamelen)
19234 char_u **fnamep;
19235 char_u **bufp;
19236 int *fnamelen;
19237{
19238 int sepcount, len, tflen;
19239 char_u *p;
19240 char_u *pbuf, *tfname;
19241 int hasTilde;
19242
19243 /* Count up the path seperators from the RHS.. so we know which part
19244 * of the path to return.
19245 */
19246 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019247 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019248 if (vim_ispathsep(*p))
19249 ++sepcount;
19250
19251 /* Need full path first (use expand_env() to remove a "~/") */
19252 hasTilde = (**fnamep == '~');
19253 if (hasTilde)
19254 pbuf = tfname = expand_env_save(*fnamep);
19255 else
19256 pbuf = tfname = FullName_save(*fnamep, FALSE);
19257
19258 len = tflen = STRLEN(tfname);
19259
19260 if (!get_short_pathname(&tfname, &pbuf, &len))
19261 return -1;
19262
19263 if (len == 0)
19264 {
19265 /* Don't have a valid filename, so shorten the rest of the
19266 * path if we can. This CAN give us invalid 8.3 filenames, but
19267 * there's not a lot of point in guessing what it might be.
19268 */
19269 len = tflen;
19270 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
19271 return -1;
19272 }
19273
19274 /* Count the paths backward to find the beginning of the desired string. */
19275 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019276 {
19277#ifdef FEAT_MBYTE
19278 if (has_mbyte)
19279 p -= mb_head_off(tfname, p);
19280#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019281 if (vim_ispathsep(*p))
19282 {
19283 if (sepcount == 0 || (hasTilde && sepcount == 1))
19284 break;
19285 else
19286 sepcount --;
19287 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019289 if (hasTilde)
19290 {
19291 --p;
19292 if (p >= tfname)
19293 *p = '~';
19294 else
19295 return -1;
19296 }
19297 else
19298 ++p;
19299
19300 /* Copy in the string - p indexes into tfname - allocated at pbuf */
19301 vim_free(*bufp);
19302 *fnamelen = (int)STRLEN(p);
19303 *bufp = pbuf;
19304 *fnamep = p;
19305
19306 return 0;
19307}
19308#endif /* WIN3264 */
19309
19310/*
19311 * Adjust a filename, according to a string of modifiers.
19312 * *fnamep must be NUL terminated when called. When returning, the length is
19313 * determined by *fnamelen.
19314 * Returns valid flags.
19315 * When there is an error, *fnamep is set to NULL.
19316 */
19317 int
19318modify_fname(src, usedlen, fnamep, bufp, fnamelen)
19319 char_u *src; /* string with modifiers */
19320 int *usedlen; /* characters after src that are used */
19321 char_u **fnamep; /* file name so far */
19322 char_u **bufp; /* buffer for allocated file name or NULL */
19323 int *fnamelen; /* length of fnamep */
19324{
19325 int valid = 0;
19326 char_u *tail;
19327 char_u *s, *p, *pbuf;
19328 char_u dirname[MAXPATHL];
19329 int c;
19330 int has_fullname = 0;
19331#ifdef WIN3264
19332 int has_shortname = 0;
19333#endif
19334
19335repeat:
19336 /* ":p" - full path/file_name */
19337 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
19338 {
19339 has_fullname = 1;
19340
19341 valid |= VALID_PATH;
19342 *usedlen += 2;
19343
19344 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
19345 if ((*fnamep)[0] == '~'
19346#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
19347 && ((*fnamep)[1] == '/'
19348# ifdef BACKSLASH_IN_FILENAME
19349 || (*fnamep)[1] == '\\'
19350# endif
19351 || (*fnamep)[1] == NUL)
19352
19353#endif
19354 )
19355 {
19356 *fnamep = expand_env_save(*fnamep);
19357 vim_free(*bufp); /* free any allocated file name */
19358 *bufp = *fnamep;
19359 if (*fnamep == NULL)
19360 return -1;
19361 }
19362
19363 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019364 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019365 {
19366 if (vim_ispathsep(*p)
19367 && p[1] == '.'
19368 && (p[2] == NUL
19369 || vim_ispathsep(p[2])
19370 || (p[2] == '.'
19371 && (p[3] == NUL || vim_ispathsep(p[3])))))
19372 break;
19373 }
19374
19375 /* FullName_save() is slow, don't use it when not needed. */
19376 if (*p != NUL || !vim_isAbsName(*fnamep))
19377 {
19378 *fnamep = FullName_save(*fnamep, *p != NUL);
19379 vim_free(*bufp); /* free any allocated file name */
19380 *bufp = *fnamep;
19381 if (*fnamep == NULL)
19382 return -1;
19383 }
19384
19385 /* Append a path separator to a directory. */
19386 if (mch_isdir(*fnamep))
19387 {
19388 /* Make room for one or two extra characters. */
19389 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
19390 vim_free(*bufp); /* free any allocated file name */
19391 *bufp = *fnamep;
19392 if (*fnamep == NULL)
19393 return -1;
19394 add_pathsep(*fnamep);
19395 }
19396 }
19397
19398 /* ":." - path relative to the current directory */
19399 /* ":~" - path relative to the home directory */
19400 /* ":8" - shortname path - postponed till after */
19401 while (src[*usedlen] == ':'
19402 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
19403 {
19404 *usedlen += 2;
19405 if (c == '8')
19406 {
19407#ifdef WIN3264
19408 has_shortname = 1; /* Postpone this. */
19409#endif
19410 continue;
19411 }
19412 pbuf = NULL;
19413 /* Need full path first (use expand_env() to remove a "~/") */
19414 if (!has_fullname)
19415 {
19416 if (c == '.' && **fnamep == '~')
19417 p = pbuf = expand_env_save(*fnamep);
19418 else
19419 p = pbuf = FullName_save(*fnamep, FALSE);
19420 }
19421 else
19422 p = *fnamep;
19423
19424 has_fullname = 0;
19425
19426 if (p != NULL)
19427 {
19428 if (c == '.')
19429 {
19430 mch_dirname(dirname, MAXPATHL);
19431 s = shorten_fname(p, dirname);
19432 if (s != NULL)
19433 {
19434 *fnamep = s;
19435 if (pbuf != NULL)
19436 {
19437 vim_free(*bufp); /* free any allocated file name */
19438 *bufp = pbuf;
19439 pbuf = NULL;
19440 }
19441 }
19442 }
19443 else
19444 {
19445 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
19446 /* Only replace it when it starts with '~' */
19447 if (*dirname == '~')
19448 {
19449 s = vim_strsave(dirname);
19450 if (s != NULL)
19451 {
19452 *fnamep = s;
19453 vim_free(*bufp);
19454 *bufp = s;
19455 }
19456 }
19457 }
19458 vim_free(pbuf);
19459 }
19460 }
19461
19462 tail = gettail(*fnamep);
19463 *fnamelen = (int)STRLEN(*fnamep);
19464
19465 /* ":h" - head, remove "/file_name", can be repeated */
19466 /* Don't remove the first "/" or "c:\" */
19467 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
19468 {
19469 valid |= VALID_HEAD;
19470 *usedlen += 2;
19471 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019472 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019473 --tail;
19474 *fnamelen = (int)(tail - *fnamep);
19475#ifdef VMS
19476 if (*fnamelen > 0)
19477 *fnamelen += 1; /* the path separator is part of the path */
19478#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019479 while (tail > s && !after_pathsep(s, tail))
19480 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019481 }
19482
19483 /* ":8" - shortname */
19484 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
19485 {
19486 *usedlen += 2;
19487#ifdef WIN3264
19488 has_shortname = 1;
19489#endif
19490 }
19491
19492#ifdef WIN3264
19493 /* Check shortname after we have done 'heads' and before we do 'tails'
19494 */
19495 if (has_shortname)
19496 {
19497 pbuf = NULL;
19498 /* Copy the string if it is shortened by :h */
19499 if (*fnamelen < (int)STRLEN(*fnamep))
19500 {
19501 p = vim_strnsave(*fnamep, *fnamelen);
19502 if (p == 0)
19503 return -1;
19504 vim_free(*bufp);
19505 *bufp = *fnamep = p;
19506 }
19507
19508 /* Split into two implementations - makes it easier. First is where
19509 * there isn't a full name already, second is where there is.
19510 */
19511 if (!has_fullname && !vim_isAbsName(*fnamep))
19512 {
19513 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
19514 return -1;
19515 }
19516 else
19517 {
19518 int l;
19519
19520 /* Simple case, already have the full-name
19521 * Nearly always shorter, so try first time. */
19522 l = *fnamelen;
19523 if (!get_short_pathname(fnamep, bufp, &l))
19524 return -1;
19525
19526 if (l == 0)
19527 {
19528 /* Couldn't find the filename.. search the paths.
19529 */
19530 l = *fnamelen;
19531 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
19532 return -1;
19533 }
19534 *fnamelen = l;
19535 }
19536 }
19537#endif /* WIN3264 */
19538
19539 /* ":t" - tail, just the basename */
19540 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
19541 {
19542 *usedlen += 2;
19543 *fnamelen -= (int)(tail - *fnamep);
19544 *fnamep = tail;
19545 }
19546
19547 /* ":e" - extension, can be repeated */
19548 /* ":r" - root, without extension, can be repeated */
19549 while (src[*usedlen] == ':'
19550 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
19551 {
19552 /* find a '.' in the tail:
19553 * - for second :e: before the current fname
19554 * - otherwise: The last '.'
19555 */
19556 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
19557 s = *fnamep - 2;
19558 else
19559 s = *fnamep + *fnamelen - 1;
19560 for ( ; s > tail; --s)
19561 if (s[0] == '.')
19562 break;
19563 if (src[*usedlen + 1] == 'e') /* :e */
19564 {
19565 if (s > tail)
19566 {
19567 *fnamelen += (int)(*fnamep - (s + 1));
19568 *fnamep = s + 1;
19569#ifdef VMS
19570 /* cut version from the extension */
19571 s = *fnamep + *fnamelen - 1;
19572 for ( ; s > *fnamep; --s)
19573 if (s[0] == ';')
19574 break;
19575 if (s > *fnamep)
19576 *fnamelen = s - *fnamep;
19577#endif
19578 }
19579 else if (*fnamep <= tail)
19580 *fnamelen = 0;
19581 }
19582 else /* :r */
19583 {
19584 if (s > tail) /* remove one extension */
19585 *fnamelen = (int)(s - *fnamep);
19586 }
19587 *usedlen += 2;
19588 }
19589
19590 /* ":s?pat?foo?" - substitute */
19591 /* ":gs?pat?foo?" - global substitute */
19592 if (src[*usedlen] == ':'
19593 && (src[*usedlen + 1] == 's'
19594 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
19595 {
19596 char_u *str;
19597 char_u *pat;
19598 char_u *sub;
19599 int sep;
19600 char_u *flags;
19601 int didit = FALSE;
19602
19603 flags = (char_u *)"";
19604 s = src + *usedlen + 2;
19605 if (src[*usedlen + 1] == 'g')
19606 {
19607 flags = (char_u *)"g";
19608 ++s;
19609 }
19610
19611 sep = *s++;
19612 if (sep)
19613 {
19614 /* find end of pattern */
19615 p = vim_strchr(s, sep);
19616 if (p != NULL)
19617 {
19618 pat = vim_strnsave(s, (int)(p - s));
19619 if (pat != NULL)
19620 {
19621 s = p + 1;
19622 /* find end of substitution */
19623 p = vim_strchr(s, sep);
19624 if (p != NULL)
19625 {
19626 sub = vim_strnsave(s, (int)(p - s));
19627 str = vim_strnsave(*fnamep, *fnamelen);
19628 if (sub != NULL && str != NULL)
19629 {
19630 *usedlen = (int)(p + 1 - src);
19631 s = do_string_sub(str, pat, sub, flags);
19632 if (s != NULL)
19633 {
19634 *fnamep = s;
19635 *fnamelen = (int)STRLEN(s);
19636 vim_free(*bufp);
19637 *bufp = s;
19638 didit = TRUE;
19639 }
19640 }
19641 vim_free(sub);
19642 vim_free(str);
19643 }
19644 vim_free(pat);
19645 }
19646 }
19647 /* after using ":s", repeat all the modifiers */
19648 if (didit)
19649 goto repeat;
19650 }
19651 }
19652
19653 return valid;
19654}
19655
19656/*
19657 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
19658 * "flags" can be "g" to do a global substitute.
19659 * Returns an allocated string, NULL for error.
19660 */
19661 char_u *
19662do_string_sub(str, pat, sub, flags)
19663 char_u *str;
19664 char_u *pat;
19665 char_u *sub;
19666 char_u *flags;
19667{
19668 int sublen;
19669 regmatch_T regmatch;
19670 int i;
19671 int do_all;
19672 char_u *tail;
19673 garray_T ga;
19674 char_u *ret;
19675 char_u *save_cpo;
19676
19677 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
19678 save_cpo = p_cpo;
19679 p_cpo = (char_u *)"";
19680
19681 ga_init2(&ga, 1, 200);
19682
19683 do_all = (flags[0] == 'g');
19684
19685 regmatch.rm_ic = p_ic;
19686 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19687 if (regmatch.regprog != NULL)
19688 {
19689 tail = str;
19690 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
19691 {
19692 /*
19693 * Get some space for a temporary buffer to do the substitution
19694 * into. It will contain:
19695 * - The text up to where the match is.
19696 * - The substituted text.
19697 * - The text after the match.
19698 */
19699 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
19700 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
19701 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
19702 {
19703 ga_clear(&ga);
19704 break;
19705 }
19706
19707 /* copy the text up to where the match is */
19708 i = (int)(regmatch.startp[0] - tail);
19709 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
19710 /* add the substituted text */
19711 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
19712 + ga.ga_len + i, TRUE, TRUE, FALSE);
19713 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019714 /* avoid getting stuck on a match with an empty string */
19715 if (tail == regmatch.endp[0])
19716 {
19717 if (*tail == NUL)
19718 break;
19719 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
19720 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019721 }
19722 else
19723 {
19724 tail = regmatch.endp[0];
19725 if (*tail == NUL)
19726 break;
19727 }
19728 if (!do_all)
19729 break;
19730 }
19731
19732 if (ga.ga_data != NULL)
19733 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
19734
19735 vim_free(regmatch.regprog);
19736 }
19737
19738 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
19739 ga_clear(&ga);
19740 p_cpo = save_cpo;
19741
19742 return ret;
19743}
19744
19745#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */