blob: d1271987e70a81ee10837c2f3e6395a23f08cd44 [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 Moolenaar761b1132005-10-03 22:05:45 +0000340 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaar33570922005-01-25 22:26:29 +0000341};
342
343/* shorthand */
344#define vv_type vv_di.di_tv.v_type
345#define vv_nr vv_di.di_tv.vval.v_number
346#define vv_str vv_di.di_tv.vval.v_string
347#define vv_tv vv_di.di_tv
348
349/*
350 * The v: variables are stored in dictionary "vimvardict".
351 * "vimvars_var" is the variable that is used for the "l:" scope.
352 */
353static dict_T vimvardict;
354static dictitem_T vimvars_var;
355#define vimvarht vimvardict.dv_hashtab
356
Bram Moolenaara40058a2005-07-11 22:42:07 +0000357static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
358static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
359#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
360static int call_vim_function __ARGS((char_u *func, int argc, char_u **argv, int safe, typval_T *rettv));
361#endif
362static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
363static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
364static char_u *skip_var_one __ARGS((char_u *arg));
365static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty));
366static void list_glob_vars __ARGS((void));
367static void list_buf_vars __ARGS((void));
368static void list_win_vars __ARGS((void));
369static void list_vim_vars __ARGS((void));
370static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg));
371static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
372static int check_changedtick __ARGS((char_u *arg));
373static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
374static void clear_lval __ARGS((lval_T *lp));
375static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
376static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
377static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
378static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
379static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
380static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
381static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
382static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
383static void item_lock __ARGS((typval_T *tv, int deep, int lock));
384static int tv_islocked __ARGS((typval_T *tv));
385
Bram Moolenaar33570922005-01-25 22:26:29 +0000386static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
387static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
388static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
389static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
390static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
391static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
392static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
393static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000394
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000395static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000396static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
397static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
398static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
399static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
400static list_T *list_alloc __ARGS((void));
Bram Moolenaar33570922005-01-25 22:26:29 +0000401static void list_free __ARGS((list_T *l));
402static listitem_T *listitem_alloc __ARGS((void));
403static void listitem_free __ARGS((listitem_T *item));
404static void listitem_remove __ARGS((list_T *l, listitem_T *item));
405static long list_len __ARGS((list_T *l));
406static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
407static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
408static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
Bram Moolenaar33570922005-01-25 22:26:29 +0000409static listitem_T *list_find __ARGS((list_T *l, long n));
410static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000411static void list_append __ARGS((list_T *l, listitem_T *item));
412static int list_append_tv __ARGS((list_T *l, typval_T *tv));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000413static int list_append_string __ARGS((list_T *l, char_u *str, int len));
414static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000415static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
416static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
417static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000418static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000419static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
420static char_u *list2string __ARGS((typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000421static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000422static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
423static void set_ref_in_list __ARGS((list_T *l, int copyID));
424static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000425static void dict_unref __ARGS((dict_T *d));
426static void dict_free __ARGS((dict_T *d));
427static dictitem_T *dictitem_alloc __ARGS((char_u *key));
428static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
429static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
430static void dictitem_free __ARGS((dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000431static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000432static int dict_add __ARGS((dict_T *d, dictitem_T *item));
433static long dict_len __ARGS((dict_T *d));
434static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
435static char_u *dict2string __ARGS((typval_T *tv));
436static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar33570922005-01-25 22:26:29 +0000437static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf));
438static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf));
439static char_u *string_quote __ARGS((char_u *str, int function));
440static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
441static int find_internal_func __ARGS((char_u *name));
442static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
443static 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));
444static 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 +0000445static void emsg_funcname __ARGS((char *msg, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000446
447static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
448static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
449static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
450static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
451static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
452static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
453static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
454static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
455static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
456static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
457static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
458static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
459static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
460static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
463static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
464static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
465static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000466#if defined(FEAT_INS_EXPAND)
467static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
468static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
469#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000470static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
475static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000501static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000502static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000503static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000504static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000509static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000510static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000517static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000518static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
534static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000540static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000541static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000546static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000547static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000563static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000564static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000567#ifdef vim_mkdir
568static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
569#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000570static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000574static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000575static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000576static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000577static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000588static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000589static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000595static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000596static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000600static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000601static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000603static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
604#ifdef HAVE_STRFTIME
605static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
606#endif
607static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000619static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000620static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000621static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000622static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000623static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
624static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
625static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
626static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
627static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
628static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
629static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
630static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
631static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
632static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
633static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
634static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
635static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
636static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000637static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000638
Bram Moolenaar33570922005-01-25 22:26:29 +0000639static pos_T *var2fpos __ARGS((typval_T *varp, int lnum));
640static int get_env_len __ARGS((char_u **arg));
641static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000642static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000643static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
644#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
645#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
646 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000647static 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 +0000648static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000649static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000650static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
651static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000652static typval_T *alloc_tv __ARGS((void));
653static typval_T *alloc_string_tv __ARGS((char_u *string));
654static void free_tv __ARGS((typval_T *varp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000655static void init_tv __ARGS((typval_T *varp));
656static long get_tv_number __ARGS((typval_T *varp));
657static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000658static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000659static char_u *get_tv_string __ARGS((typval_T *varp));
660static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000661static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000662static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000663static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000664static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
665static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
666static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
667static void list_one_var __ARGS((dictitem_T *v, char_u *prefix));
668static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
669static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
670static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000671static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000672static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000673static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000674static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
675static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
676static int eval_fname_script __ARGS((char_u *p));
677static int eval_fname_sid __ARGS((char_u *p));
678static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000679static ufunc_T *find_func __ARGS((char_u *name));
680static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000681static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000682#ifdef FEAT_PROFILE
683static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000684static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
685static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
686static int
687# ifdef __BORLANDC__
688 _RTLENTRYF
689# endif
690 prof_total_cmp __ARGS((const void *s1, const void *s2));
691static int
692# ifdef __BORLANDC__
693 _RTLENTRYF
694# endif
695 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000696#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000697static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000698static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000699static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000700static void func_free __ARGS((ufunc_T *fp));
701static void func_unref __ARGS((char_u *name));
702static void func_ref __ARGS((char_u *name));
703static 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));
704static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
705
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000706/* Character used as separated in autoload function/variable names. */
707#define AUTOLOAD_CHAR '#'
708
Bram Moolenaar33570922005-01-25 22:26:29 +0000709/*
710 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000711 */
712 void
713eval_init()
714{
Bram Moolenaar33570922005-01-25 22:26:29 +0000715 int i;
716 struct vimvar *p;
717
718 init_var_dict(&globvardict, &globvars_var);
719 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000720 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000721 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000722
723 for (i = 0; i < VV_LEN; ++i)
724 {
725 p = &vimvars[i];
726 STRCPY(p->vv_di.di_key, p->vv_name);
727 if (p->vv_flags & VV_RO)
728 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
729 else if (p->vv_flags & VV_RO_SBX)
730 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
731 else
732 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000733
734 /* add to v: scope dict, unless the value is not always available */
735 if (p->vv_type != VAR_UNKNOWN)
736 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000737 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000738 /* add to compat scope dict */
739 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000740 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000741}
742
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000743#if defined(EXITFREE) || defined(PROTO)
744 void
745eval_clear()
746{
747 int i;
748 struct vimvar *p;
749
750 for (i = 0; i < VV_LEN; ++i)
751 {
752 p = &vimvars[i];
753 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000754 {
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000755 vim_free(p->vv_di.di_tv.vval.v_string);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000756 p->vv_di.di_tv.vval.v_string = NULL;
757 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000758 }
759 hash_clear(&vimvarht);
760 hash_clear(&compat_hashtab);
761
762 /* script-local variables */
763 for (i = 1; i <= ga_scripts.ga_len; ++i)
764 vars_clear(&SCRIPT_VARS(i));
765 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000766 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000767
768 /* global variables */
769 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000770
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000771 /* functions */
Bram Moolenaard9fba312005-06-26 22:34:35 +0000772 free_all_functions();
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000773 hash_clear(&func_hashtab);
774
775 /* unreferenced lists and dicts */
776 (void)garbage_collect();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000777}
778#endif
779
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000780/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 * Return the name of the executed function.
782 */
783 char_u *
784func_name(cookie)
785 void *cookie;
786{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000787 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788}
789
790/*
791 * Return the address holding the next breakpoint line for a funccall cookie.
792 */
793 linenr_T *
794func_breakpoint(cookie)
795 void *cookie;
796{
Bram Moolenaar33570922005-01-25 22:26:29 +0000797 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798}
799
800/*
801 * Return the address holding the debug tick for a funccall cookie.
802 */
803 int *
804func_dbg_tick(cookie)
805 void *cookie;
806{
Bram Moolenaar33570922005-01-25 22:26:29 +0000807 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808}
809
810/*
811 * Return the nesting level for a funccall cookie.
812 */
813 int
814func_level(cookie)
815 void *cookie;
816{
Bram Moolenaar33570922005-01-25 22:26:29 +0000817 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818}
819
820/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000821funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822
823/*
824 * Return TRUE when a function was ended by a ":return" command.
825 */
826 int
827current_func_returned()
828{
829 return current_funccal->returned;
830}
831
832
833/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 * Set an internal variable to a string value. Creates the variable if it does
835 * not already exist.
836 */
837 void
838set_internal_string_var(name, value)
839 char_u *name;
840 char_u *value;
841{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000842 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000843 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844
845 val = vim_strsave(value);
846 if (val != NULL)
847 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000848 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000849 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000851 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000852 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 }
854 }
855}
856
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000857static lval_T *redir_lval = NULL;
858static char_u *redir_endp = NULL;
859static char_u *redir_varname = NULL;
860
861/*
862 * Start recording command output to a variable
863 * Returns OK if successfully completed the setup. FAIL otherwise.
864 */
865 int
866var_redir_start(name, append)
867 char_u *name;
868 int append; /* append to an existing variable */
869{
870 int save_emsg;
871 int err;
872 typval_T tv;
873
874 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000875 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000876 {
877 EMSG(_(e_invarg));
878 return FAIL;
879 }
880
881 redir_varname = vim_strsave(name);
882 if (redir_varname == NULL)
883 return FAIL;
884
885 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
886 if (redir_lval == NULL)
887 {
888 var_redir_stop();
889 return FAIL;
890 }
891
892 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000893 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
894 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000895 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
896 {
897 if (redir_endp != NULL && *redir_endp != NUL)
898 /* Trailing characters are present after the variable name */
899 EMSG(_(e_trailing));
900 else
901 EMSG(_(e_invarg));
902 var_redir_stop();
903 return FAIL;
904 }
905
906 /* check if we can write to the variable: set it to or append an empty
907 * string */
908 save_emsg = did_emsg;
909 did_emsg = FALSE;
910 tv.v_type = VAR_STRING;
911 tv.vval.v_string = (char_u *)"";
912 if (append)
913 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
914 else
915 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
916 err = did_emsg;
917 did_emsg += save_emsg;
918 if (err)
919 {
920 var_redir_stop();
921 return FAIL;
922 }
923 if (redir_lval->ll_newkey != NULL)
924 {
925 /* Dictionary item was created, don't do it again. */
926 vim_free(redir_lval->ll_newkey);
927 redir_lval->ll_newkey = NULL;
928 }
929
930 return OK;
931}
932
933/*
934 * Append "value[len]" to the variable set by var_redir_start().
935 */
936 void
937var_redir_str(value, len)
938 char_u *value;
939 int len;
940{
941 char_u *val;
942 typval_T tv;
943 int save_emsg;
944 int err;
945
946 if (redir_lval == NULL)
947 return;
948
949 if (len == -1)
950 /* Append the entire string */
951 val = vim_strsave(value);
952 else
953 /* Append only the specified number of characters */
954 val = vim_strnsave(value, len);
955 if (val == NULL)
956 return;
957
958 tv.v_type = VAR_STRING;
959 tv.vval.v_string = val;
960
961 save_emsg = did_emsg;
962 did_emsg = FALSE;
963 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
964 err = did_emsg;
965 did_emsg += save_emsg;
966 if (err)
967 var_redir_stop();
968
969 vim_free(tv.vval.v_string);
970}
971
972/*
973 * Stop redirecting command output to a variable.
974 */
975 void
976var_redir_stop()
977{
978 if (redir_lval != NULL)
979 {
980 clear_lval(redir_lval);
981 vim_free(redir_lval);
982 redir_lval = NULL;
983 }
984 vim_free(redir_varname);
985 redir_varname = NULL;
986}
987
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988# if defined(FEAT_MBYTE) || defined(PROTO)
989 int
990eval_charconvert(enc_from, enc_to, fname_from, fname_to)
991 char_u *enc_from;
992 char_u *enc_to;
993 char_u *fname_from;
994 char_u *fname_to;
995{
996 int err = FALSE;
997
998 set_vim_var_string(VV_CC_FROM, enc_from, -1);
999 set_vim_var_string(VV_CC_TO, enc_to, -1);
1000 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1001 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1002 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1003 err = TRUE;
1004 set_vim_var_string(VV_CC_FROM, NULL, -1);
1005 set_vim_var_string(VV_CC_TO, NULL, -1);
1006 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1007 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1008
1009 if (err)
1010 return FAIL;
1011 return OK;
1012}
1013# endif
1014
1015# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1016 int
1017eval_printexpr(fname, args)
1018 char_u *fname;
1019 char_u *args;
1020{
1021 int err = FALSE;
1022
1023 set_vim_var_string(VV_FNAME_IN, fname, -1);
1024 set_vim_var_string(VV_CMDARG, args, -1);
1025 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1026 err = TRUE;
1027 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1028 set_vim_var_string(VV_CMDARG, NULL, -1);
1029
1030 if (err)
1031 {
1032 mch_remove(fname);
1033 return FAIL;
1034 }
1035 return OK;
1036}
1037# endif
1038
1039# if defined(FEAT_DIFF) || defined(PROTO)
1040 void
1041eval_diff(origfile, newfile, outfile)
1042 char_u *origfile;
1043 char_u *newfile;
1044 char_u *outfile;
1045{
1046 int err = FALSE;
1047
1048 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1049 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1050 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1051 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1052 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1053 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1054 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1055}
1056
1057 void
1058eval_patch(origfile, difffile, outfile)
1059 char_u *origfile;
1060 char_u *difffile;
1061 char_u *outfile;
1062{
1063 int err;
1064
1065 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1066 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1067 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1068 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1069 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1070 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1071 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1072}
1073# endif
1074
1075/*
1076 * Top level evaluation function, returning a boolean.
1077 * Sets "error" to TRUE if there was an error.
1078 * Return TRUE or FALSE.
1079 */
1080 int
1081eval_to_bool(arg, error, nextcmd, skip)
1082 char_u *arg;
1083 int *error;
1084 char_u **nextcmd;
1085 int skip; /* only parse, don't execute */
1086{
Bram Moolenaar33570922005-01-25 22:26:29 +00001087 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 int retval = FALSE;
1089
1090 if (skip)
1091 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001092 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 else
1095 {
1096 *error = FALSE;
1097 if (!skip)
1098 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001099 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001100 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 }
1102 }
1103 if (skip)
1104 --emsg_skip;
1105
1106 return retval;
1107}
1108
1109/*
1110 * Top level evaluation function, returning a string. If "skip" is TRUE,
1111 * only parsing to "nextcmd" is done, without reporting errors. Return
1112 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1113 */
1114 char_u *
1115eval_to_string_skip(arg, nextcmd, skip)
1116 char_u *arg;
1117 char_u **nextcmd;
1118 int skip; /* only parse, don't execute */
1119{
Bram Moolenaar33570922005-01-25 22:26:29 +00001120 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 char_u *retval;
1122
1123 if (skip)
1124 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001125 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 retval = NULL;
1127 else
1128 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001129 retval = vim_strsave(get_tv_string(&tv));
1130 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 }
1132 if (skip)
1133 --emsg_skip;
1134
1135 return retval;
1136}
1137
1138/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001139 * Skip over an expression at "*pp".
1140 * Return FAIL for an error, OK otherwise.
1141 */
1142 int
1143skip_expr(pp)
1144 char_u **pp;
1145{
Bram Moolenaar33570922005-01-25 22:26:29 +00001146 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001147
1148 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001149 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001150}
1151
1152/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 * Top level evaluation function, returning a string.
1154 * Return pointer to allocated memory, or NULL for failure.
1155 */
1156 char_u *
1157eval_to_string(arg, nextcmd)
1158 char_u *arg;
1159 char_u **nextcmd;
1160{
Bram Moolenaar33570922005-01-25 22:26:29 +00001161 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 char_u *retval;
1163
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001164 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 retval = NULL;
1166 else
1167 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001168 retval = vim_strsave(get_tv_string(&tv));
1169 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 }
1171
1172 return retval;
1173}
1174
1175/*
1176 * Call eval_to_string() with "sandbox" set and not using local variables.
1177 */
1178 char_u *
1179eval_to_string_safe(arg, nextcmd)
1180 char_u *arg;
1181 char_u **nextcmd;
1182{
1183 char_u *retval;
1184 void *save_funccalp;
1185
1186 save_funccalp = save_funccal();
1187 ++sandbox;
1188 retval = eval_to_string(arg, nextcmd);
1189 --sandbox;
1190 restore_funccal(save_funccalp);
1191 return retval;
1192}
1193
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194/*
1195 * Top level evaluation function, returning a number.
1196 * Evaluates "expr" silently.
1197 * Returns -1 for an error.
1198 */
1199 int
1200eval_to_number(expr)
1201 char_u *expr;
1202{
Bram Moolenaar33570922005-01-25 22:26:29 +00001203 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001205 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206
1207 ++emsg_off;
1208
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001209 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 retval = -1;
1211 else
1212 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001213 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001214 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 }
1216 --emsg_off;
1217
1218 return retval;
1219}
1220
Bram Moolenaara40058a2005-07-11 22:42:07 +00001221/*
1222 * Prepare v: variable "idx" to be used.
1223 * Save the current typeval in "save_tv".
1224 * When not used yet add the variable to the v: hashtable.
1225 */
1226 static void
1227prepare_vimvar(idx, save_tv)
1228 int idx;
1229 typval_T *save_tv;
1230{
1231 *save_tv = vimvars[idx].vv_tv;
1232 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1233 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1234}
1235
1236/*
1237 * Restore v: variable "idx" to typeval "save_tv".
1238 * When no longer defined, remove the variable from the v: hashtable.
1239 */
1240 static void
1241restore_vimvar(idx, save_tv)
1242 int idx;
1243 typval_T *save_tv;
1244{
1245 hashitem_T *hi;
1246
1247 clear_tv(&vimvars[idx].vv_tv);
1248 vimvars[idx].vv_tv = *save_tv;
1249 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1250 {
1251 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1252 if (HASHITEM_EMPTY(hi))
1253 EMSG2(_(e_intern2), "restore_vimvar()");
1254 else
1255 hash_remove(&vimvarht, hi);
1256 }
1257}
1258
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001259#if defined(FEAT_SYN_HL) || defined(PROTO)
1260/*
1261 * Evaluate an expression to a list with suggestions.
1262 * For the "expr:" part of 'spellsuggest'.
1263 */
1264 list_T *
1265eval_spell_expr(badword, expr)
1266 char_u *badword;
1267 char_u *expr;
1268{
1269 typval_T save_val;
1270 typval_T rettv;
1271 list_T *list = NULL;
1272 char_u *p = skipwhite(expr);
1273
1274 /* Set "v:val" to the bad word. */
1275 prepare_vimvar(VV_VAL, &save_val);
1276 vimvars[VV_VAL].vv_type = VAR_STRING;
1277 vimvars[VV_VAL].vv_str = badword;
1278 if (p_verbose == 0)
1279 ++emsg_off;
1280
1281 if (eval1(&p, &rettv, TRUE) == OK)
1282 {
1283 if (rettv.v_type != VAR_LIST)
1284 clear_tv(&rettv);
1285 else
1286 list = rettv.vval.v_list;
1287 }
1288
1289 if (p_verbose == 0)
1290 --emsg_off;
1291 vimvars[VV_VAL].vv_str = NULL;
1292 restore_vimvar(VV_VAL, &save_val);
1293
1294 return list;
1295}
1296
1297/*
1298 * "list" is supposed to contain two items: a word and a number. Return the
1299 * word in "pp" and the number as the return value.
1300 * Return -1 if anything isn't right.
1301 * Used to get the good word and score from the eval_spell_expr() result.
1302 */
1303 int
1304get_spellword(list, pp)
1305 list_T *list;
1306 char_u **pp;
1307{
1308 listitem_T *li;
1309
1310 li = list->lv_first;
1311 if (li == NULL)
1312 return -1;
1313 *pp = get_tv_string(&li->li_tv);
1314
1315 li = li->li_next;
1316 if (li == NULL)
1317 return -1;
1318 return get_tv_number(&li->li_tv);
1319}
1320#endif
1321
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001322/*
1323 * Top level evaluation function,
1324 */
1325 typval_T *
1326eval_expr(arg, nextcmd)
1327 char_u *arg;
1328 char_u **nextcmd;
1329{
1330 typval_T *tv;
1331
1332 tv = (typval_T *)alloc(sizeof(typval_T));
1333 if (!tv)
1334 return NULL;
1335
1336 if (eval0(arg, tv, nextcmd, TRUE) == FAIL)
1337 {
1338 vim_free(tv);
1339 return NULL;
1340 }
1341
1342 return tv;
1343}
1344
1345
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1347/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001348 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 * Uses argv[argc] for the function arguments.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001350 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001352 static int
1353call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 char_u *func;
1355 int argc;
1356 char_u **argv;
1357 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001358 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359{
Bram Moolenaar33570922005-01-25 22:26:29 +00001360 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 long n;
1362 int len;
1363 int i;
1364 int doesrange;
1365 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001366 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367
Bram Moolenaar33570922005-01-25 22:26:29 +00001368 argvars = (typval_T *)alloc((unsigned)(argc * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001370 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371
1372 for (i = 0; i < argc; i++)
1373 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001374 /* Pass a NULL or empty argument as an empty string */
1375 if (argv[i] == NULL || *argv[i] == NUL)
1376 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001377 argvars[i].v_type = VAR_STRING;
1378 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001379 continue;
1380 }
1381
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 /* Recognize a number argument, the others must be strings. */
1383 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1384 if (len != 0 && len == (int)STRLEN(argv[i]))
1385 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001386 argvars[i].v_type = VAR_NUMBER;
1387 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 }
1389 else
1390 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001391 argvars[i].v_type = VAR_STRING;
1392 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 }
1394 }
1395
1396 if (safe)
1397 {
1398 save_funccalp = save_funccal();
1399 ++sandbox;
1400 }
1401
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001402 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1403 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001405 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 if (safe)
1407 {
1408 --sandbox;
1409 restore_funccal(save_funccalp);
1410 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001411 vim_free(argvars);
1412
1413 if (ret == FAIL)
1414 clear_tv(rettv);
1415
1416 return ret;
1417}
1418
1419/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001420 * Call vimL function "func" and return the result as a string.
1421 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001422 * Uses argv[argc] for the function arguments.
1423 */
1424 void *
1425call_func_retstr(func, argc, argv, safe)
1426 char_u *func;
1427 int argc;
1428 char_u **argv;
1429 int safe; /* use the sandbox */
1430{
1431 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001432 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001433
1434 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1435 return NULL;
1436
1437 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001438 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 return retval;
1440}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001441
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001442#if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001443/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001444 * Call vimL function "func" and return the result as a number.
1445 * Returns -1 when calling the function fails.
1446 * Uses argv[argc] for the function arguments.
1447 */
1448 long
1449call_func_retnr(func, argc, argv, safe)
1450 char_u *func;
1451 int argc;
1452 char_u **argv;
1453 int safe; /* use the sandbox */
1454{
1455 typval_T rettv;
1456 long retval;
1457
1458 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1459 return -1;
1460
1461 retval = get_tv_number_chk(&rettv, NULL);
1462 clear_tv(&rettv);
1463 return retval;
1464}
1465#endif
1466
1467/*
1468 * Call vimL function "func" and return the result as a list
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001469 * Uses argv[argc] for the function arguments.
1470 */
1471 void *
1472call_func_retlist(func, argc, argv, safe)
1473 char_u *func;
1474 int argc;
1475 char_u **argv;
1476 int safe; /* use the sandbox */
1477{
1478 typval_T rettv;
1479
1480 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1481 return NULL;
1482
1483 if (rettv.v_type != VAR_LIST)
1484 {
1485 clear_tv(&rettv);
1486 return NULL;
1487 }
1488
1489 return rettv.vval.v_list;
1490}
1491
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492#endif
1493
1494/*
1495 * Save the current function call pointer, and set it to NULL.
1496 * Used when executing autocommands and for ":source".
1497 */
1498 void *
1499save_funccal()
1500{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001501 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 current_funccal = NULL;
1504 return (void *)fc;
1505}
1506
1507 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001508restore_funccal(vfc)
1509 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001511 funccall_T *fc = (funccall_T *)vfc;
1512
1513 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514}
1515
Bram Moolenaar05159a02005-02-26 23:04:13 +00001516#if defined(FEAT_PROFILE) || defined(PROTO)
1517/*
1518 * Prepare profiling for entering a child or something else that is not
1519 * counted for the script/function itself.
1520 * Should always be called in pair with prof_child_exit().
1521 */
1522 void
1523prof_child_enter(tm)
1524 proftime_T *tm; /* place to store waittime */
1525{
1526 funccall_T *fc = current_funccal;
1527
1528 if (fc != NULL && fc->func->uf_profiling)
1529 profile_start(&fc->prof_child);
1530 script_prof_save(tm);
1531}
1532
1533/*
1534 * Take care of time spent in a child.
1535 * Should always be called after prof_child_enter().
1536 */
1537 void
1538prof_child_exit(tm)
1539 proftime_T *tm; /* where waittime was stored */
1540{
1541 funccall_T *fc = current_funccal;
1542
1543 if (fc != NULL && fc->func->uf_profiling)
1544 {
1545 profile_end(&fc->prof_child);
1546 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1547 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1548 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1549 }
1550 script_prof_restore(tm);
1551}
1552#endif
1553
1554
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555#ifdef FEAT_FOLDING
1556/*
1557 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1558 * it in "*cp". Doesn't give error messages.
1559 */
1560 int
1561eval_foldexpr(arg, cp)
1562 char_u *arg;
1563 int *cp;
1564{
Bram Moolenaar33570922005-01-25 22:26:29 +00001565 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 int retval;
1567 char_u *s;
1568
1569 ++emsg_off;
1570 ++sandbox;
1571 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001572 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 retval = 0;
1574 else
1575 {
1576 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001577 if (tv.v_type == VAR_NUMBER)
1578 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001579 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 retval = 0;
1581 else
1582 {
1583 /* If the result is a string, check if there is a non-digit before
1584 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001585 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 if (!VIM_ISDIGIT(*s) && *s != '-')
1587 *cp = *s++;
1588 retval = atol((char *)s);
1589 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001590 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 }
1592 --emsg_off;
1593 --sandbox;
1594
1595 return retval;
1596}
1597#endif
1598
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001600 * ":let" list all variable values
1601 * ":let var1 var2" list variable values
1602 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001603 * ":let var += expr" assignment command.
1604 * ":let var -= expr" assignment command.
1605 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001606 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 */
1608 void
1609ex_let(eap)
1610 exarg_T *eap;
1611{
1612 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001613 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001614 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001616 int var_count = 0;
1617 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001618 char_u op[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001620 expr = skip_var_list(arg, &var_count, &semicolon);
1621 if (expr == NULL)
1622 return;
1623 expr = vim_strchr(expr, '=');
1624 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001626 /*
1627 * ":let" without "=": list variables
1628 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001629 if (*arg == '[')
1630 EMSG(_(e_invarg));
1631 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001632 /* ":let var1 var2" */
1633 arg = list_arg_vars(eap, arg);
1634 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001635 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001636 /* ":let" */
Bram Moolenaara7043832005-01-21 11:56:39 +00001637 list_glob_vars();
1638 list_buf_vars();
1639 list_win_vars();
1640 list_vim_vars();
1641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 eap->nextcmd = check_nextcmd(arg);
1643 }
1644 else
1645 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001646 op[0] = '=';
1647 op[1] = NUL;
1648 if (expr > arg)
1649 {
1650 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1651 op[0] = expr[-1]; /* +=, -= or .= */
1652 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001653 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001654
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 if (eap->skip)
1656 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001657 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 if (eap->skip)
1659 {
1660 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001661 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 --emsg_skip;
1663 }
1664 else if (i != FAIL)
1665 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001666 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001667 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001668 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 }
1670 }
1671}
1672
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001673/*
1674 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1675 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001676 * When "nextchars" is not NULL it points to a string with characters that
1677 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1678 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001679 * Returns OK or FAIL;
1680 */
1681 static int
1682ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1683 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001684 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001685 int copy; /* copy values from "tv", don't move */
1686 int semicolon; /* from skip_var_list() */
1687 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001688 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001689{
1690 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001691 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001692 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001693 listitem_T *item;
1694 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001695
1696 if (*arg != '[')
1697 {
1698 /*
1699 * ":let var = expr" or ":for var in list"
1700 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001701 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001702 return FAIL;
1703 return OK;
1704 }
1705
1706 /*
1707 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1708 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001709 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001710 {
1711 EMSG(_(e_listreq));
1712 return FAIL;
1713 }
1714
1715 i = list_len(l);
1716 if (semicolon == 0 && var_count < i)
1717 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001718 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001719 return FAIL;
1720 }
1721 if (var_count - semicolon > i)
1722 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001723 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001724 return FAIL;
1725 }
1726
1727 item = l->lv_first;
1728 while (*arg != ']')
1729 {
1730 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001731 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001732 item = item->li_next;
1733 if (arg == NULL)
1734 return FAIL;
1735
1736 arg = skipwhite(arg);
1737 if (*arg == ';')
1738 {
1739 /* Put the rest of the list (may be empty) in the var after ';'.
1740 * Create a new list for this. */
1741 l = list_alloc();
1742 if (l == NULL)
1743 return FAIL;
1744 while (item != NULL)
1745 {
1746 list_append_tv(l, &item->li_tv);
1747 item = item->li_next;
1748 }
1749
1750 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001751 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001752 ltv.vval.v_list = l;
1753 l->lv_refcount = 1;
1754
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001755 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1756 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001757 clear_tv(&ltv);
1758 if (arg == NULL)
1759 return FAIL;
1760 break;
1761 }
1762 else if (*arg != ',' && *arg != ']')
1763 {
1764 EMSG2(_(e_intern2), "ex_let_vars()");
1765 return FAIL;
1766 }
1767 }
1768
1769 return OK;
1770}
1771
1772/*
1773 * Skip over assignable variable "var" or list of variables "[var, var]".
1774 * Used for ":let varvar = expr" and ":for varvar in expr".
1775 * For "[var, var]" increment "*var_count" for each variable.
1776 * for "[var, var; var]" set "semicolon".
1777 * Return NULL for an error.
1778 */
1779 static char_u *
1780skip_var_list(arg, var_count, semicolon)
1781 char_u *arg;
1782 int *var_count;
1783 int *semicolon;
1784{
1785 char_u *p, *s;
1786
1787 if (*arg == '[')
1788 {
1789 /* "[var, var]": find the matching ']'. */
1790 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001791 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001792 {
1793 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1794 s = skip_var_one(p);
1795 if (s == p)
1796 {
1797 EMSG2(_(e_invarg2), p);
1798 return NULL;
1799 }
1800 ++*var_count;
1801
1802 p = skipwhite(s);
1803 if (*p == ']')
1804 break;
1805 else if (*p == ';')
1806 {
1807 if (*semicolon == 1)
1808 {
1809 EMSG(_("Double ; in list of variables"));
1810 return NULL;
1811 }
1812 *semicolon = 1;
1813 }
1814 else if (*p != ',')
1815 {
1816 EMSG2(_(e_invarg2), p);
1817 return NULL;
1818 }
1819 }
1820 return p + 1;
1821 }
1822 else
1823 return skip_var_one(arg);
1824}
1825
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001826/*
Bram Moolenaar92124a32005-06-17 22:03:40 +00001827 * Skip one (assignable) variable name, includig @r, $VAR, &option, d.key,
1828 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001829 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001830 static char_u *
1831skip_var_one(arg)
1832 char_u *arg;
1833{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001834 if (*arg == '@' && arg[1] != NUL)
1835 return arg + 2;
1836 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1837 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001838}
1839
Bram Moolenaara7043832005-01-21 11:56:39 +00001840/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001841 * List variables for hashtab "ht" with prefix "prefix".
1842 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001843 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001844 static void
Bram Moolenaar33570922005-01-25 22:26:29 +00001845list_hashtable_vars(ht, prefix, empty)
1846 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001847 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001848 int empty;
Bram Moolenaara7043832005-01-21 11:56:39 +00001849{
Bram Moolenaar33570922005-01-25 22:26:29 +00001850 hashitem_T *hi;
1851 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001852 int todo;
1853
1854 todo = ht->ht_used;
1855 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1856 {
1857 if (!HASHITEM_EMPTY(hi))
1858 {
1859 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001860 di = HI2DI(hi);
1861 if (empty || di->di_tv.v_type != VAR_STRING
1862 || di->di_tv.vval.v_string != NULL)
1863 list_one_var(di, prefix);
Bram Moolenaara7043832005-01-21 11:56:39 +00001864 }
1865 }
1866}
1867
1868/*
1869 * List global variables.
1870 */
1871 static void
1872list_glob_vars()
1873{
Bram Moolenaar33570922005-01-25 22:26:29 +00001874 list_hashtable_vars(&globvarht, (char_u *)"", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001875}
1876
1877/*
1878 * List buffer variables.
1879 */
1880 static void
1881list_buf_vars()
1882{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001883 char_u numbuf[NUMBUFLEN];
1884
Bram Moolenaar33570922005-01-25 22:26:29 +00001885 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001886
1887 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
1888 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER, numbuf);
Bram Moolenaara7043832005-01-21 11:56:39 +00001889}
1890
1891/*
1892 * List window variables.
1893 */
1894 static void
1895list_win_vars()
1896{
Bram Moolenaar33570922005-01-25 22:26:29 +00001897 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001898}
1899
1900/*
1901 * List Vim variables.
1902 */
1903 static void
1904list_vim_vars()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001905{
Bram Moolenaar33570922005-01-25 22:26:29 +00001906 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001907}
1908
1909/*
1910 * List variables in "arg".
1911 */
1912 static char_u *
1913list_arg_vars(eap, arg)
1914 exarg_T *eap;
1915 char_u *arg;
1916{
1917 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001918 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001919 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001920 char_u *name_start;
1921 char_u *arg_subsc;
1922 char_u *tofree;
1923 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001924
1925 while (!ends_excmd(*arg) && !got_int)
1926 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001927 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001928 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001929 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001930 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
1931 {
1932 emsg_severe = TRUE;
1933 EMSG(_(e_trailing));
1934 break;
1935 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001936 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001937 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001938 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001939 /* get_name_len() takes care of expanding curly braces */
1940 name_start = name = arg;
1941 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1942 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001943 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001944 /* This is mainly to keep test 49 working: when expanding
1945 * curly braces fails overrule the exception error message. */
1946 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001947 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001948 emsg_severe = TRUE;
1949 EMSG2(_(e_invarg2), arg);
1950 break;
1951 }
1952 error = TRUE;
1953 }
1954 else
1955 {
1956 if (tofree != NULL)
1957 name = tofree;
1958 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001959 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001960 else
1961 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001962 /* handle d.key, l[idx], f(expr) */
1963 arg_subsc = arg;
1964 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001965 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001966 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001967 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001968 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001969 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001970 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001971 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001972 case 'g': list_glob_vars(); break;
1973 case 'b': list_buf_vars(); break;
1974 case 'w': list_win_vars(); break;
1975 case 'v': list_vim_vars(); break;
1976 default:
1977 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001978 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001979 }
1980 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001981 {
1982 char_u numbuf[NUMBUFLEN];
1983 char_u *tf;
1984 int c;
1985 char_u *s;
1986
1987 s = echo_string(&tv, &tf, numbuf);
1988 c = *arg;
1989 *arg = NUL;
1990 list_one_var_a((char_u *)"",
1991 arg == arg_subsc ? name : name_start,
1992 tv.v_type, s == NULL ? (char_u *)"" : s);
1993 *arg = c;
1994 vim_free(tf);
1995 }
1996 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001997 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001998 }
1999 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002000
2001 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002002 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002003
2004 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002005 }
2006
2007 return arg;
2008}
2009
2010/*
2011 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2012 * Returns a pointer to the char just after the var name.
2013 * Returns NULL if there is an error.
2014 */
2015 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002016ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002017 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002018 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002019 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002020 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002021 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002022{
2023 int c1;
2024 char_u *name;
2025 char_u *p;
2026 char_u *arg_end = NULL;
2027 int len;
2028 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002029 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002030
2031 /*
2032 * ":let $VAR = expr": Set environment variable.
2033 */
2034 if (*arg == '$')
2035 {
2036 /* Find the end of the name. */
2037 ++arg;
2038 name = arg;
2039 len = get_env_len(&arg);
2040 if (len == 0)
2041 EMSG2(_(e_invarg2), name - 1);
2042 else
2043 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002044 if (op != NULL && (*op == '+' || *op == '-'))
2045 EMSG2(_(e_letwrong), op);
2046 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002047 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002048 EMSG(_(e_letunexp));
2049 else
2050 {
2051 c1 = name[len];
2052 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002053 p = get_tv_string_chk(tv);
2054 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002055 {
2056 int mustfree = FALSE;
2057 char_u *s = vim_getenv(name, &mustfree);
2058
2059 if (s != NULL)
2060 {
2061 p = tofree = concat_str(s, p);
2062 if (mustfree)
2063 vim_free(s);
2064 }
2065 }
2066 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002067 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002068 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002069 if (STRICMP(name, "HOME") == 0)
2070 init_homedir();
2071 else if (didset_vim && STRICMP(name, "VIM") == 0)
2072 didset_vim = FALSE;
2073 else if (didset_vimruntime
2074 && STRICMP(name, "VIMRUNTIME") == 0)
2075 didset_vimruntime = FALSE;
2076 arg_end = arg;
2077 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002078 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002079 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002080 }
2081 }
2082 }
2083
2084 /*
2085 * ":let &option = expr": Set option value.
2086 * ":let &l:option = expr": Set local option value.
2087 * ":let &g:option = expr": Set global option value.
2088 */
2089 else if (*arg == '&')
2090 {
2091 /* Find the end of the name. */
2092 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002093 if (p == NULL || (endchars != NULL
2094 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002095 EMSG(_(e_letunexp));
2096 else
2097 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002098 long n;
2099 int opt_type;
2100 long numval;
2101 char_u *stringval = NULL;
2102 char_u *s;
2103
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002104 c1 = *p;
2105 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002106
2107 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002108 s = get_tv_string_chk(tv); /* != NULL if number or string */
2109 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002110 {
2111 opt_type = get_option_value(arg, &numval,
2112 &stringval, opt_flags);
2113 if ((opt_type == 1 && *op == '.')
2114 || (opt_type == 0 && *op != '.'))
2115 EMSG2(_(e_letwrong), op);
2116 else
2117 {
2118 if (opt_type == 1) /* number */
2119 {
2120 if (*op == '+')
2121 n = numval + n;
2122 else
2123 n = numval - n;
2124 }
2125 else if (opt_type == 0 && stringval != NULL) /* string */
2126 {
2127 s = concat_str(stringval, s);
2128 vim_free(stringval);
2129 stringval = s;
2130 }
2131 }
2132 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002133 if (s != NULL)
2134 {
2135 set_option_value(arg, n, s, opt_flags);
2136 arg_end = p;
2137 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002138 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002139 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002140 }
2141 }
2142
2143 /*
2144 * ":let @r = expr": Set register contents.
2145 */
2146 else if (*arg == '@')
2147 {
2148 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002149 if (op != NULL && (*op == '+' || *op == '-'))
2150 EMSG2(_(e_letwrong), op);
2151 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002152 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002153 EMSG(_(e_letunexp));
2154 else
2155 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002156 char_u *tofree = NULL;
2157 char_u *s;
2158
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002159 p = get_tv_string_chk(tv);
2160 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002161 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002162 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002163 if (s != NULL)
2164 {
2165 p = tofree = concat_str(s, p);
2166 vim_free(s);
2167 }
2168 }
2169 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002170 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002171 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002172 arg_end = arg + 1;
2173 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002174 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002175 }
2176 }
2177
2178 /*
2179 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002180 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002181 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002182 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002183 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002184 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002185
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002186 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002187 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002188 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002189 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2190 EMSG(_(e_letunexp));
2191 else
2192 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002193 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002194 arg_end = p;
2195 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002196 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002197 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002198 }
2199
2200 else
2201 EMSG2(_(e_invarg2), arg);
2202
2203 return arg_end;
2204}
2205
2206/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002207 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2208 */
2209 static int
2210check_changedtick(arg)
2211 char_u *arg;
2212{
2213 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2214 {
2215 EMSG2(_(e_readonlyvar), arg);
2216 return TRUE;
2217 }
2218 return FALSE;
2219}
2220
2221/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002222 * Get an lval: variable, Dict item or List item that can be assigned a value
2223 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2224 * "name.key", "name.key[expr]" etc.
2225 * Indexing only works if "name" is an existing List or Dictionary.
2226 * "name" points to the start of the name.
2227 * If "rettv" is not NULL it points to the value to be assigned.
2228 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2229 * wrong; must end in space or cmd separator.
2230 *
2231 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002232 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002233 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002234 */
2235 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002236get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002237 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002238 typval_T *rettv;
2239 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002240 int unlet;
2241 int skip;
2242 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002243 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002244{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002245 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002246 char_u *expr_start, *expr_end;
2247 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002248 dictitem_T *v;
2249 typval_T var1;
2250 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002251 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002252 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002253 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002254 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002255 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002256
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002257 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002258 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002259
2260 if (skip)
2261 {
2262 /* When skipping just find the end of the name. */
2263 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002264 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002265 }
2266
2267 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002268 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002269 if (expr_start != NULL)
2270 {
2271 /* Don't expand the name when we already know there is an error. */
2272 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2273 && *p != '[' && *p != '.')
2274 {
2275 EMSG(_(e_trailing));
2276 return NULL;
2277 }
2278
2279 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2280 if (lp->ll_exp_name == NULL)
2281 {
2282 /* Report an invalid expression in braces, unless the
2283 * expression evaluation has been cancelled due to an
2284 * aborting error, an interrupt, or an exception. */
2285 if (!aborting() && !quiet)
2286 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002287 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002288 EMSG2(_(e_invarg2), name);
2289 return NULL;
2290 }
2291 }
2292 lp->ll_name = lp->ll_exp_name;
2293 }
2294 else
2295 lp->ll_name = name;
2296
2297 /* Without [idx] or .key we are done. */
2298 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2299 return p;
2300
2301 cc = *p;
2302 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002303 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002304 if (v == NULL && !quiet)
2305 EMSG2(_(e_undefvar), lp->ll_name);
2306 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002307 if (v == NULL)
2308 return NULL;
2309
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002310 /*
2311 * Loop until no more [idx] or .key is following.
2312 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002313 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002314 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002315 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002316 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2317 && !(lp->ll_tv->v_type == VAR_DICT
2318 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002319 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002320 if (!quiet)
2321 EMSG(_("E689: Can only index a List or Dictionary"));
2322 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002323 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002324 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002325 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002326 if (!quiet)
2327 EMSG(_("E708: [:] must come last"));
2328 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002329 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002330
Bram Moolenaar8c711452005-01-14 21:53:12 +00002331 len = -1;
2332 if (*p == '.')
2333 {
2334 key = p + 1;
2335 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2336 ;
2337 if (len == 0)
2338 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002339 if (!quiet)
2340 EMSG(_(e_emptykey));
2341 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002342 }
2343 p = key + len;
2344 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002345 else
2346 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002347 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002348 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002349 if (*p == ':')
2350 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002351 else
2352 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002353 empty1 = FALSE;
2354 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002355 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002356 if (get_tv_string_chk(&var1) == NULL)
2357 {
2358 /* not a number or string */
2359 clear_tv(&var1);
2360 return NULL;
2361 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002362 }
2363
2364 /* Optionally get the second index [ :expr]. */
2365 if (*p == ':')
2366 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002367 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002368 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002369 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002370 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002371 if (!empty1)
2372 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002373 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002374 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002375 if (rettv != NULL && (rettv->v_type != VAR_LIST
2376 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002377 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002378 if (!quiet)
2379 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002380 if (!empty1)
2381 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002382 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002383 }
2384 p = skipwhite(p + 1);
2385 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002386 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002387 else
2388 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002389 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002390 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2391 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002392 if (!empty1)
2393 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002394 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002395 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002396 if (get_tv_string_chk(&var2) == NULL)
2397 {
2398 /* not a number or string */
2399 if (!empty1)
2400 clear_tv(&var1);
2401 clear_tv(&var2);
2402 return NULL;
2403 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002404 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002405 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002406 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002407 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002408 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002409
Bram Moolenaar8c711452005-01-14 21:53:12 +00002410 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002411 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002412 if (!quiet)
2413 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002414 if (!empty1)
2415 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002416 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002417 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002418 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002419 }
2420
2421 /* Skip to past ']'. */
2422 ++p;
2423 }
2424
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002425 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002426 {
2427 if (len == -1)
2428 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002429 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002430 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002431 if (*key == NUL)
2432 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002433 if (!quiet)
2434 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002435 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002436 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002437 }
2438 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002439 lp->ll_list = NULL;
2440 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002441 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002442 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002443 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002444 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002445 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002446 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002447 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002448 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002449 if (len == -1)
2450 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002451 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002452 }
2453 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002454 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002455 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002456 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002457 if (len == -1)
2458 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002459 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002460 p = NULL;
2461 break;
2462 }
2463 if (len == -1)
2464 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002465 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002466 }
2467 else
2468 {
2469 /*
2470 * Get the number and item for the only or first index of the List.
2471 */
2472 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002473 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002474 else
2475 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002476 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002477 clear_tv(&var1);
2478 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002479 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002480 lp->ll_list = lp->ll_tv->vval.v_list;
2481 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2482 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002483 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002484 if (!quiet)
2485 EMSGN(_(e_listidx), lp->ll_n1);
2486 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002487 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002488 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002489 }
2490
2491 /*
2492 * May need to find the item or absolute index for the second
2493 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002494 * When no index given: "lp->ll_empty2" is TRUE.
2495 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002496 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002497 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002498 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002499 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002500 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002501 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002502 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002503 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002504 if (ni == NULL)
2505 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002506 if (!quiet)
2507 EMSGN(_(e_listidx), lp->ll_n2);
2508 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002509 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002510 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002511 }
2512
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002513 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2514 if (lp->ll_n1 < 0)
2515 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2516 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002517 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002518 if (!quiet)
2519 EMSGN(_(e_listidx), lp->ll_n2);
2520 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002521 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002522 }
2523
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002524 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002525 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002526 }
2527
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002528 return p;
2529}
2530
2531/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002532 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002533 */
2534 static void
2535clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002536 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002537{
2538 vim_free(lp->ll_exp_name);
2539 vim_free(lp->ll_newkey);
2540}
2541
2542/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002543 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002544 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002545 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002546 */
2547 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002548set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002549 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002550 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002551 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002552 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002553 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002554{
2555 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002556 listitem_T *ri;
2557 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002558
2559 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002560 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002561 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002562 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002563 cc = *endp;
2564 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002565 if (op != NULL && *op != '=')
2566 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002567 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002568
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002569 /* handle +=, -= and .= */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002570 if (get_var_tv(lp->ll_name, STRLEN(lp->ll_name),
2571 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002572 {
2573 if (tv_op(&tv, rettv, op) == OK)
2574 set_var(lp->ll_name, &tv, FALSE);
2575 clear_tv(&tv);
2576 }
2577 }
2578 else
2579 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002580 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002581 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002582 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002583 else if (tv_check_lock(lp->ll_newkey == NULL
2584 ? lp->ll_tv->v_lock
2585 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2586 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002587 else if (lp->ll_range)
2588 {
2589 /*
2590 * Assign the List values to the list items.
2591 */
2592 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002593 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002594 if (op != NULL && *op != '=')
2595 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2596 else
2597 {
2598 clear_tv(&lp->ll_li->li_tv);
2599 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2600 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002601 ri = ri->li_next;
2602 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2603 break;
2604 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002605 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002606 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002607 if (list_append_number(lp->ll_list, 0) == FAIL)
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 Moolenaar6cc16192005-01-08 21:49:45 +00002612 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002613 lp->ll_li = lp->ll_li->li_next;
2614 ++lp->ll_n1;
2615 }
2616 if (ri != NULL)
2617 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002618 else if (lp->ll_empty2
2619 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002620 : lp->ll_n1 != lp->ll_n2)
2621 EMSG(_("E711: List value has not enough items"));
2622 }
2623 else
2624 {
2625 /*
2626 * Assign to a List or Dictionary item.
2627 */
2628 if (lp->ll_newkey != NULL)
2629 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002630 if (op != NULL && *op != '=')
2631 {
2632 EMSG2(_(e_letwrong), op);
2633 return;
2634 }
2635
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002636 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002637 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002638 if (di == NULL)
2639 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002640 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2641 {
2642 vim_free(di);
2643 return;
2644 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002645 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002646 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002647 else if (op != NULL && *op != '=')
2648 {
2649 tv_op(lp->ll_tv, rettv, op);
2650 return;
2651 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002652 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002654
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002655 /*
2656 * Assign the value to the variable or list item.
2657 */
2658 if (copy)
2659 copy_tv(rettv, lp->ll_tv);
2660 else
2661 {
2662 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002663 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002664 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002665 }
2666 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002667}
2668
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002669/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002670 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2671 * Returns OK or FAIL.
2672 */
2673 static int
2674tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002675 typval_T *tv1;
2676 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002677 char_u *op;
2678{
2679 long n;
2680 char_u numbuf[NUMBUFLEN];
2681 char_u *s;
2682
2683 /* Can't do anything with a Funcref or a Dict on the right. */
2684 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2685 {
2686 switch (tv1->v_type)
2687 {
2688 case VAR_DICT:
2689 case VAR_FUNC:
2690 break;
2691
2692 case VAR_LIST:
2693 if (*op != '+' || tv2->v_type != VAR_LIST)
2694 break;
2695 /* List += List */
2696 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2697 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2698 return OK;
2699
2700 case VAR_NUMBER:
2701 case VAR_STRING:
2702 if (tv2->v_type == VAR_LIST)
2703 break;
2704 if (*op == '+' || *op == '-')
2705 {
2706 /* nr += nr or nr -= nr*/
2707 n = get_tv_number(tv1);
2708 if (*op == '+')
2709 n += get_tv_number(tv2);
2710 else
2711 n -= get_tv_number(tv2);
2712 clear_tv(tv1);
2713 tv1->v_type = VAR_NUMBER;
2714 tv1->vval.v_number = n;
2715 }
2716 else
2717 {
2718 /* str .= str */
2719 s = get_tv_string(tv1);
2720 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2721 clear_tv(tv1);
2722 tv1->v_type = VAR_STRING;
2723 tv1->vval.v_string = s;
2724 }
2725 return OK;
2726 }
2727 }
2728
2729 EMSG2(_(e_letwrong), op);
2730 return FAIL;
2731}
2732
2733/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002734 * Add a watcher to a list.
2735 */
2736 static void
2737list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002738 list_T *l;
2739 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002740{
2741 lw->lw_next = l->lv_watch;
2742 l->lv_watch = lw;
2743}
2744
2745/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002746 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002747 * No warning when it isn't found...
2748 */
2749 static void
2750list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002751 list_T *l;
2752 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002753{
Bram Moolenaar33570922005-01-25 22:26:29 +00002754 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002755
2756 lwp = &l->lv_watch;
2757 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2758 {
2759 if (lw == lwrem)
2760 {
2761 *lwp = lw->lw_next;
2762 break;
2763 }
2764 lwp = &lw->lw_next;
2765 }
2766}
2767
2768/*
2769 * Just before removing an item from a list: advance watchers to the next
2770 * item.
2771 */
2772 static void
2773list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002774 list_T *l;
2775 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002776{
Bram Moolenaar33570922005-01-25 22:26:29 +00002777 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002778
2779 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2780 if (lw->lw_item == item)
2781 lw->lw_item = item->li_next;
2782}
2783
2784/*
2785 * Evaluate the expression used in a ":for var in expr" command.
2786 * "arg" points to "var".
2787 * Set "*errp" to TRUE for an error, FALSE otherwise;
2788 * Return a pointer that holds the info. Null when there is an error.
2789 */
2790 void *
2791eval_for_line(arg, errp, nextcmdp, skip)
2792 char_u *arg;
2793 int *errp;
2794 char_u **nextcmdp;
2795 int skip;
2796{
Bram Moolenaar33570922005-01-25 22:26:29 +00002797 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002798 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002799 typval_T tv;
2800 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002801
2802 *errp = TRUE; /* default: there is an error */
2803
Bram Moolenaar33570922005-01-25 22:26:29 +00002804 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002805 if (fi == NULL)
2806 return NULL;
2807
2808 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2809 if (expr == NULL)
2810 return fi;
2811
2812 expr = skipwhite(expr);
2813 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2814 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002815 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002816 return fi;
2817 }
2818
2819 if (skip)
2820 ++emsg_skip;
2821 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2822 {
2823 *errp = FALSE;
2824 if (!skip)
2825 {
2826 l = tv.vval.v_list;
2827 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002828 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002829 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002830 clear_tv(&tv);
2831 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002832 else
2833 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002834 /* No need to increment the refcount, it's already set for the
2835 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002836 fi->fi_list = l;
2837 list_add_watch(l, &fi->fi_lw);
2838 fi->fi_lw.lw_item = l->lv_first;
2839 }
2840 }
2841 }
2842 if (skip)
2843 --emsg_skip;
2844
2845 return fi;
2846}
2847
2848/*
2849 * Use the first item in a ":for" list. Advance to the next.
2850 * Assign the values to the variable (list). "arg" points to the first one.
2851 * Return TRUE when a valid item was found, FALSE when at end of list or
2852 * something wrong.
2853 */
2854 int
2855next_for_item(fi_void, arg)
2856 void *fi_void;
2857 char_u *arg;
2858{
Bram Moolenaar33570922005-01-25 22:26:29 +00002859 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002860 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002861 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002862
2863 item = fi->fi_lw.lw_item;
2864 if (item == NULL)
2865 result = FALSE;
2866 else
2867 {
2868 fi->fi_lw.lw_item = item->li_next;
2869 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2870 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2871 }
2872 return result;
2873}
2874
2875/*
2876 * Free the structure used to store info used by ":for".
2877 */
2878 void
2879free_for_info(fi_void)
2880 void *fi_void;
2881{
Bram Moolenaar33570922005-01-25 22:26:29 +00002882 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002883
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002884 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002885 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002886 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002887 list_unref(fi->fi_list);
2888 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002889 vim_free(fi);
2890}
2891
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2893
2894 void
2895set_context_for_expression(xp, arg, cmdidx)
2896 expand_T *xp;
2897 char_u *arg;
2898 cmdidx_T cmdidx;
2899{
2900 int got_eq = FALSE;
2901 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002902 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002904 if (cmdidx == CMD_let)
2905 {
2906 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002907 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002908 {
2909 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002910 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002911 {
2912 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00002913 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002914 if (vim_iswhite(*p))
2915 break;
2916 }
2917 return;
2918 }
2919 }
2920 else
2921 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2922 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 while ((xp->xp_pattern = vim_strpbrk(arg,
2924 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2925 {
2926 c = *xp->xp_pattern;
2927 if (c == '&')
2928 {
2929 c = xp->xp_pattern[1];
2930 if (c == '&')
2931 {
2932 ++xp->xp_pattern;
2933 xp->xp_context = cmdidx != CMD_let || got_eq
2934 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2935 }
2936 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002937 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002939 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2940 xp->xp_pattern += 2;
2941
2942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 }
2944 else if (c == '$')
2945 {
2946 /* environment variable */
2947 xp->xp_context = EXPAND_ENV_VARS;
2948 }
2949 else if (c == '=')
2950 {
2951 got_eq = TRUE;
2952 xp->xp_context = EXPAND_EXPRESSION;
2953 }
2954 else if (c == '<'
2955 && xp->xp_context == EXPAND_FUNCTIONS
2956 && vim_strchr(xp->xp_pattern, '(') == NULL)
2957 {
2958 /* Function name can start with "<SNR>" */
2959 break;
2960 }
2961 else if (cmdidx != CMD_let || got_eq)
2962 {
2963 if (c == '"') /* string */
2964 {
2965 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2966 if (c == '\\' && xp->xp_pattern[1] != NUL)
2967 ++xp->xp_pattern;
2968 xp->xp_context = EXPAND_NOTHING;
2969 }
2970 else if (c == '\'') /* literal string */
2971 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002972 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2974 /* skip */ ;
2975 xp->xp_context = EXPAND_NOTHING;
2976 }
2977 else if (c == '|')
2978 {
2979 if (xp->xp_pattern[1] == '|')
2980 {
2981 ++xp->xp_pattern;
2982 xp->xp_context = EXPAND_EXPRESSION;
2983 }
2984 else
2985 xp->xp_context = EXPAND_COMMANDS;
2986 }
2987 else
2988 xp->xp_context = EXPAND_EXPRESSION;
2989 }
2990 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002991 /* Doesn't look like something valid, expand as an expression
2992 * anyway. */
2993 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 arg = xp->xp_pattern;
2995 if (*arg != NUL)
2996 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2997 /* skip */ ;
2998 }
2999 xp->xp_pattern = arg;
3000}
3001
3002#endif /* FEAT_CMDL_COMPL */
3003
3004/*
3005 * ":1,25call func(arg1, arg2)" function call.
3006 */
3007 void
3008ex_call(eap)
3009 exarg_T *eap;
3010{
3011 char_u *arg = eap->arg;
3012 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003014 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003016 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 linenr_T lnum;
3018 int doesrange;
3019 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003020 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003022 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
3023 vim_free(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003024 if (tofree == NULL)
3025 return;
3026
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003027 /* Increase refcount on dictionary, it could get deleted when evaluating
3028 * the arguments. */
3029 if (fudi.fd_dict != NULL)
3030 ++fudi.fd_dict->dv_refcount;
3031
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003032 /* If it is the name of a variable of type VAR_FUNC use its contents. */
3033 len = STRLEN(tofree);
3034 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035
Bram Moolenaar532c7802005-01-27 14:44:31 +00003036 /* Skip white space to allow ":call func ()". Not good, but required for
3037 * backward compatibility. */
3038 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003039 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040
3041 if (*startarg != '(')
3042 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003043 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 goto end;
3045 }
3046
3047 /*
3048 * When skipping, evaluate the function once, to find the end of the
3049 * arguments.
3050 * When the function takes a range, this is discovered after the first
3051 * call, and the loop is broken.
3052 */
3053 if (eap->skip)
3054 {
3055 ++emsg_skip;
3056 lnum = eap->line2; /* do it once, also with an invalid range */
3057 }
3058 else
3059 lnum = eap->line1;
3060 for ( ; lnum <= eap->line2; ++lnum)
3061 {
3062 if (!eap->skip && eap->addr_count > 0)
3063 {
3064 curwin->w_cursor.lnum = lnum;
3065 curwin->w_cursor.col = 0;
3066 }
3067 arg = startarg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003068 if (get_func_tv(name, STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003069 eap->line1, eap->line2, &doesrange,
3070 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 {
3072 failed = TRUE;
3073 break;
3074 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003075 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 if (doesrange || eap->skip)
3077 break;
3078 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003079 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003080 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003081 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 if (aborting())
3083 break;
3084 }
3085 if (eap->skip)
3086 --emsg_skip;
3087
3088 if (!failed)
3089 {
3090 /* Check for trailing illegal characters and a following command. */
3091 if (!ends_excmd(*arg))
3092 {
3093 emsg_severe = TRUE;
3094 EMSG(_(e_trailing));
3095 }
3096 else
3097 eap->nextcmd = check_nextcmd(arg);
3098 }
3099
3100end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003101 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003102 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103}
3104
3105/*
3106 * ":unlet[!] var1 ... " command.
3107 */
3108 void
3109ex_unlet(eap)
3110 exarg_T *eap;
3111{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003112 ex_unletlock(eap, eap->arg, 0);
3113}
3114
3115/*
3116 * ":lockvar" and ":unlockvar" commands
3117 */
3118 void
3119ex_lockvar(eap)
3120 exarg_T *eap;
3121{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003123 int deep = 2;
3124
3125 if (eap->forceit)
3126 deep = -1;
3127 else if (vim_isdigit(*arg))
3128 {
3129 deep = getdigits(&arg);
3130 arg = skipwhite(arg);
3131 }
3132
3133 ex_unletlock(eap, arg, deep);
3134}
3135
3136/*
3137 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3138 */
3139 static void
3140ex_unletlock(eap, argstart, deep)
3141 exarg_T *eap;
3142 char_u *argstart;
3143 int deep;
3144{
3145 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003148 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149
3150 do
3151 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003152 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003153 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3154 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003155 if (lv.ll_name == NULL)
3156 error = TRUE; /* error but continue parsing */
3157 if (name_end == NULL || (!vim_iswhite(*name_end)
3158 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003160 if (name_end != NULL)
3161 {
3162 emsg_severe = TRUE;
3163 EMSG(_(e_trailing));
3164 }
3165 if (!(eap->skip || error))
3166 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 break;
3168 }
3169
3170 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003171 {
3172 if (eap->cmdidx == CMD_unlet)
3173 {
3174 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3175 error = TRUE;
3176 }
3177 else
3178 {
3179 if (do_lock_var(&lv, name_end, deep,
3180 eap->cmdidx == CMD_lockvar) == FAIL)
3181 error = TRUE;
3182 }
3183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003185 if (!eap->skip)
3186 clear_lval(&lv);
3187
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 arg = skipwhite(name_end);
3189 } while (!ends_excmd(*arg));
3190
3191 eap->nextcmd = check_nextcmd(arg);
3192}
3193
Bram Moolenaar8c711452005-01-14 21:53:12 +00003194 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003195do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003196 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003197 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003198 int forceit;
3199{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003200 int ret = OK;
3201 int cc;
3202
3203 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003204 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003205 cc = *name_end;
3206 *name_end = NUL;
3207
3208 /* Normal name or expanded name. */
3209 if (check_changedtick(lp->ll_name))
3210 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003211 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003212 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003213 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003214 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003215 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3216 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003217 else if (lp->ll_range)
3218 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003219 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003220
3221 /* Delete a range of List items. */
3222 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3223 {
3224 li = lp->ll_li->li_next;
3225 listitem_remove(lp->ll_list, lp->ll_li);
3226 lp->ll_li = li;
3227 ++lp->ll_n1;
3228 }
3229 }
3230 else
3231 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003232 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003233 /* unlet a List item. */
3234 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003235 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003236 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003237 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003238 }
3239
3240 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003241}
3242
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243/*
3244 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003245 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 */
3247 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003248do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003250 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251{
Bram Moolenaar33570922005-01-25 22:26:29 +00003252 hashtab_T *ht;
3253 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003254 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255
Bram Moolenaar33570922005-01-25 22:26:29 +00003256 ht = find_var_ht(name, &varname);
3257 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003259 hi = hash_find(ht, varname);
3260 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003261 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003262 if (var_check_ro(HI2DI(hi)->di_flags, name))
3263 return FAIL;
3264 delete_var(ht, hi);
3265 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003268 if (forceit)
3269 return OK;
3270 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 return FAIL;
3272}
3273
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003274/*
3275 * Lock or unlock variable indicated by "lp".
3276 * "deep" is the levels to go (-1 for unlimited);
3277 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3278 */
3279 static int
3280do_lock_var(lp, name_end, deep, lock)
3281 lval_T *lp;
3282 char_u *name_end;
3283 int deep;
3284 int lock;
3285{
3286 int ret = OK;
3287 int cc;
3288 dictitem_T *di;
3289
3290 if (deep == 0) /* nothing to do */
3291 return OK;
3292
3293 if (lp->ll_tv == NULL)
3294 {
3295 cc = *name_end;
3296 *name_end = NUL;
3297
3298 /* Normal name or expanded name. */
3299 if (check_changedtick(lp->ll_name))
3300 ret = FAIL;
3301 else
3302 {
3303 di = find_var(lp->ll_name, NULL);
3304 if (di == NULL)
3305 ret = FAIL;
3306 else
3307 {
3308 if (lock)
3309 di->di_flags |= DI_FLAGS_LOCK;
3310 else
3311 di->di_flags &= ~DI_FLAGS_LOCK;
3312 item_lock(&di->di_tv, deep, lock);
3313 }
3314 }
3315 *name_end = cc;
3316 }
3317 else if (lp->ll_range)
3318 {
3319 listitem_T *li = lp->ll_li;
3320
3321 /* (un)lock a range of List items. */
3322 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3323 {
3324 item_lock(&li->li_tv, deep, lock);
3325 li = li->li_next;
3326 ++lp->ll_n1;
3327 }
3328 }
3329 else if (lp->ll_list != NULL)
3330 /* (un)lock a List item. */
3331 item_lock(&lp->ll_li->li_tv, deep, lock);
3332 else
3333 /* un(lock) a Dictionary item. */
3334 item_lock(&lp->ll_di->di_tv, deep, lock);
3335
3336 return ret;
3337}
3338
3339/*
3340 * Lock or unlock an item. "deep" is nr of levels to go.
3341 */
3342 static void
3343item_lock(tv, deep, lock)
3344 typval_T *tv;
3345 int deep;
3346 int lock;
3347{
3348 static int recurse = 0;
3349 list_T *l;
3350 listitem_T *li;
3351 dict_T *d;
3352 hashitem_T *hi;
3353 int todo;
3354
3355 if (recurse >= DICT_MAXNEST)
3356 {
3357 EMSG(_("E743: variable nested too deep for (un)lock"));
3358 return;
3359 }
3360 if (deep == 0)
3361 return;
3362 ++recurse;
3363
3364 /* lock/unlock the item itself */
3365 if (lock)
3366 tv->v_lock |= VAR_LOCKED;
3367 else
3368 tv->v_lock &= ~VAR_LOCKED;
3369
3370 switch (tv->v_type)
3371 {
3372 case VAR_LIST:
3373 if ((l = tv->vval.v_list) != NULL)
3374 {
3375 if (lock)
3376 l->lv_lock |= VAR_LOCKED;
3377 else
3378 l->lv_lock &= ~VAR_LOCKED;
3379 if (deep < 0 || deep > 1)
3380 /* recursive: lock/unlock the items the List contains */
3381 for (li = l->lv_first; li != NULL; li = li->li_next)
3382 item_lock(&li->li_tv, deep - 1, lock);
3383 }
3384 break;
3385 case VAR_DICT:
3386 if ((d = tv->vval.v_dict) != NULL)
3387 {
3388 if (lock)
3389 d->dv_lock |= VAR_LOCKED;
3390 else
3391 d->dv_lock &= ~VAR_LOCKED;
3392 if (deep < 0 || deep > 1)
3393 {
3394 /* recursive: lock/unlock the items the List contains */
3395 todo = d->dv_hashtab.ht_used;
3396 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3397 {
3398 if (!HASHITEM_EMPTY(hi))
3399 {
3400 --todo;
3401 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3402 }
3403 }
3404 }
3405 }
3406 }
3407 --recurse;
3408}
3409
Bram Moolenaara40058a2005-07-11 22:42:07 +00003410/*
3411 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3412 * it refers to a List or Dictionary that is locked.
3413 */
3414 static int
3415tv_islocked(tv)
3416 typval_T *tv;
3417{
3418 return (tv->v_lock & VAR_LOCKED)
3419 || (tv->v_type == VAR_LIST
3420 && tv->vval.v_list != NULL
3421 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3422 || (tv->v_type == VAR_DICT
3423 && tv->vval.v_dict != NULL
3424 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3425}
3426
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3428/*
3429 * Delete all "menutrans_" variables.
3430 */
3431 void
3432del_menutrans_vars()
3433{
Bram Moolenaar33570922005-01-25 22:26:29 +00003434 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003435 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436
Bram Moolenaar33570922005-01-25 22:26:29 +00003437 hash_lock(&globvarht);
3438 todo = globvarht.ht_used;
3439 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003440 {
3441 if (!HASHITEM_EMPTY(hi))
3442 {
3443 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003444 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3445 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003446 }
3447 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003448 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449}
3450#endif
3451
3452#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3453
3454/*
3455 * Local string buffer for the next two functions to store a variable name
3456 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3457 * get_user_var_name().
3458 */
3459
3460static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3461
3462static char_u *varnamebuf = NULL;
3463static int varnamebuflen = 0;
3464
3465/*
3466 * Function to concatenate a prefix and a variable name.
3467 */
3468 static char_u *
3469cat_prefix_varname(prefix, name)
3470 int prefix;
3471 char_u *name;
3472{
3473 int len;
3474
3475 len = (int)STRLEN(name) + 3;
3476 if (len > varnamebuflen)
3477 {
3478 vim_free(varnamebuf);
3479 len += 10; /* some additional space */
3480 varnamebuf = alloc(len);
3481 if (varnamebuf == NULL)
3482 {
3483 varnamebuflen = 0;
3484 return NULL;
3485 }
3486 varnamebuflen = len;
3487 }
3488 *varnamebuf = prefix;
3489 varnamebuf[1] = ':';
3490 STRCPY(varnamebuf + 2, name);
3491 return varnamebuf;
3492}
3493
3494/*
3495 * Function given to ExpandGeneric() to obtain the list of user defined
3496 * (global/buffer/window/built-in) variable names.
3497 */
3498/*ARGSUSED*/
3499 char_u *
3500get_user_var_name(xp, idx)
3501 expand_T *xp;
3502 int idx;
3503{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003504 static long_u gdone;
3505 static long_u bdone;
3506 static long_u wdone;
3507 static int vidx;
3508 static hashitem_T *hi;
3509 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510
3511 if (idx == 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00003512 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00003513
3514 /* Global variables */
3515 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003517 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003518 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003519 else
3520 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003521 while (HASHITEM_EMPTY(hi))
3522 ++hi;
3523 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3524 return cat_prefix_varname('g', hi->hi_key);
3525 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003527
3528 /* b: variables */
3529 ht = &curbuf->b_vars.dv_hashtab;
3530 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003532 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003533 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003534 else
3535 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003536 while (HASHITEM_EMPTY(hi))
3537 ++hi;
3538 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003540 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003542 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 return (char_u *)"b:changedtick";
3544 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003545
3546 /* w: variables */
3547 ht = &curwin->w_vars.dv_hashtab;
3548 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003550 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003551 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003552 else
3553 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003554 while (HASHITEM_EMPTY(hi))
3555 ++hi;
3556 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003558
3559 /* v: variables */
3560 if (vidx < VV_LEN)
3561 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562
3563 vim_free(varnamebuf);
3564 varnamebuf = NULL;
3565 varnamebuflen = 0;
3566 return NULL;
3567}
3568
3569#endif /* FEAT_CMDL_COMPL */
3570
3571/*
3572 * types for expressions.
3573 */
3574typedef enum
3575{
3576 TYPE_UNKNOWN = 0
3577 , TYPE_EQUAL /* == */
3578 , TYPE_NEQUAL /* != */
3579 , TYPE_GREATER /* > */
3580 , TYPE_GEQUAL /* >= */
3581 , TYPE_SMALLER /* < */
3582 , TYPE_SEQUAL /* <= */
3583 , TYPE_MATCH /* =~ */
3584 , TYPE_NOMATCH /* !~ */
3585} exptype_T;
3586
3587/*
3588 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003589 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3591 */
3592
3593/*
3594 * Handle zero level expression.
3595 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003596 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003597 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 * Return OK or FAIL.
3599 */
3600 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003601eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003603 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 char_u **nextcmd;
3605 int evaluate;
3606{
3607 int ret;
3608 char_u *p;
3609
3610 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003611 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 if (ret == FAIL || !ends_excmd(*p))
3613 {
3614 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003615 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 /*
3617 * Report the invalid expression unless the expression evaluation has
3618 * been cancelled due to an aborting error, an interrupt, or an
3619 * exception.
3620 */
3621 if (!aborting())
3622 EMSG2(_(e_invexpr2), arg);
3623 ret = FAIL;
3624 }
3625 if (nextcmd != NULL)
3626 *nextcmd = check_nextcmd(p);
3627
3628 return ret;
3629}
3630
3631/*
3632 * Handle top level expression:
3633 * expr1 ? expr0 : expr0
3634 *
3635 * "arg" must point to the first non-white of the expression.
3636 * "arg" is advanced to the next non-white after the recognized expression.
3637 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003638 * Note: "rettv.v_lock" is not set.
3639 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 * Return OK or FAIL.
3641 */
3642 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003643eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003645 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 int evaluate;
3647{
3648 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003649 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650
3651 /*
3652 * Get the first variable.
3653 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003654 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 return FAIL;
3656
3657 if ((*arg)[0] == '?')
3658 {
3659 result = FALSE;
3660 if (evaluate)
3661 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003662 int error = FALSE;
3663
3664 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003666 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003667 if (error)
3668 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 }
3670
3671 /*
3672 * Get the second variable.
3673 */
3674 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003675 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 return FAIL;
3677
3678 /*
3679 * Check for the ":".
3680 */
3681 if ((*arg)[0] != ':')
3682 {
3683 EMSG(_("E109: Missing ':' after '?'"));
3684 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003685 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 return FAIL;
3687 }
3688
3689 /*
3690 * Get the third variable.
3691 */
3692 *arg = skipwhite(*arg + 1);
3693 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3694 {
3695 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003696 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 return FAIL;
3698 }
3699 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003700 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 }
3702
3703 return OK;
3704}
3705
3706/*
3707 * Handle first level expression:
3708 * expr2 || expr2 || expr2 logical OR
3709 *
3710 * "arg" must point to the first non-white of the expression.
3711 * "arg" is advanced to the next non-white after the recognized expression.
3712 *
3713 * Return OK or FAIL.
3714 */
3715 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003716eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003718 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 int evaluate;
3720{
Bram Moolenaar33570922005-01-25 22:26:29 +00003721 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 long result;
3723 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003724 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725
3726 /*
3727 * Get the first variable.
3728 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003729 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 return FAIL;
3731
3732 /*
3733 * Repeat until there is no following "||".
3734 */
3735 first = TRUE;
3736 result = FALSE;
3737 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3738 {
3739 if (evaluate && first)
3740 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003741 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003743 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003744 if (error)
3745 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 first = FALSE;
3747 }
3748
3749 /*
3750 * Get the second variable.
3751 */
3752 *arg = skipwhite(*arg + 2);
3753 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3754 return FAIL;
3755
3756 /*
3757 * Compute the result.
3758 */
3759 if (evaluate && !result)
3760 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003761 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003763 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003764 if (error)
3765 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 }
3767 if (evaluate)
3768 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003769 rettv->v_type = VAR_NUMBER;
3770 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 }
3772 }
3773
3774 return OK;
3775}
3776
3777/*
3778 * Handle second level expression:
3779 * expr3 && expr3 && expr3 logical AND
3780 *
3781 * "arg" must point to the first non-white of the expression.
3782 * "arg" is advanced to the next non-white after the recognized expression.
3783 *
3784 * Return OK or FAIL.
3785 */
3786 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003787eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003789 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 int evaluate;
3791{
Bram Moolenaar33570922005-01-25 22:26:29 +00003792 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 long result;
3794 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003795 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796
3797 /*
3798 * Get the first variable.
3799 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003800 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 return FAIL;
3802
3803 /*
3804 * Repeat until there is no following "&&".
3805 */
3806 first = TRUE;
3807 result = TRUE;
3808 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3809 {
3810 if (evaluate && first)
3811 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003812 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003814 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003815 if (error)
3816 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 first = FALSE;
3818 }
3819
3820 /*
3821 * Get the second variable.
3822 */
3823 *arg = skipwhite(*arg + 2);
3824 if (eval4(arg, &var2, evaluate && result) == FAIL)
3825 return FAIL;
3826
3827 /*
3828 * Compute the result.
3829 */
3830 if (evaluate && result)
3831 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003832 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003834 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003835 if (error)
3836 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 }
3838 if (evaluate)
3839 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003840 rettv->v_type = VAR_NUMBER;
3841 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 }
3843 }
3844
3845 return OK;
3846}
3847
3848/*
3849 * Handle third level expression:
3850 * var1 == var2
3851 * var1 =~ var2
3852 * var1 != var2
3853 * var1 !~ var2
3854 * var1 > var2
3855 * var1 >= var2
3856 * var1 < var2
3857 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003858 * var1 is var2
3859 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 *
3861 * "arg" must point to the first non-white of the expression.
3862 * "arg" is advanced to the next non-white after the recognized expression.
3863 *
3864 * Return OK or FAIL.
3865 */
3866 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003867eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003869 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 int evaluate;
3871{
Bram Moolenaar33570922005-01-25 22:26:29 +00003872 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 char_u *p;
3874 int i;
3875 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003876 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 int len = 2;
3878 long n1, n2;
3879 char_u *s1, *s2;
3880 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3881 regmatch_T regmatch;
3882 int ic;
3883 char_u *save_cpo;
3884
3885 /*
3886 * Get the first variable.
3887 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003888 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 return FAIL;
3890
3891 p = *arg;
3892 switch (p[0])
3893 {
3894 case '=': if (p[1] == '=')
3895 type = TYPE_EQUAL;
3896 else if (p[1] == '~')
3897 type = TYPE_MATCH;
3898 break;
3899 case '!': if (p[1] == '=')
3900 type = TYPE_NEQUAL;
3901 else if (p[1] == '~')
3902 type = TYPE_NOMATCH;
3903 break;
3904 case '>': if (p[1] != '=')
3905 {
3906 type = TYPE_GREATER;
3907 len = 1;
3908 }
3909 else
3910 type = TYPE_GEQUAL;
3911 break;
3912 case '<': if (p[1] != '=')
3913 {
3914 type = TYPE_SMALLER;
3915 len = 1;
3916 }
3917 else
3918 type = TYPE_SEQUAL;
3919 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003920 case 'i': if (p[1] == 's')
3921 {
3922 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3923 len = 5;
3924 if (!vim_isIDc(p[len]))
3925 {
3926 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3927 type_is = TRUE;
3928 }
3929 }
3930 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 }
3932
3933 /*
3934 * If there is a comparitive operator, use it.
3935 */
3936 if (type != TYPE_UNKNOWN)
3937 {
3938 /* extra question mark appended: ignore case */
3939 if (p[len] == '?')
3940 {
3941 ic = TRUE;
3942 ++len;
3943 }
3944 /* extra '#' appended: match case */
3945 else if (p[len] == '#')
3946 {
3947 ic = FALSE;
3948 ++len;
3949 }
3950 /* nothing appened: use 'ignorecase' */
3951 else
3952 ic = p_ic;
3953
3954 /*
3955 * Get the second variable.
3956 */
3957 *arg = skipwhite(p + len);
3958 if (eval5(arg, &var2, evaluate) == FAIL)
3959 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003960 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 return FAIL;
3962 }
3963
3964 if (evaluate)
3965 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003966 if (type_is && rettv->v_type != var2.v_type)
3967 {
3968 /* For "is" a different type always means FALSE, for "notis"
3969 * it means TRUE. */
3970 n1 = (type == TYPE_NEQUAL);
3971 }
3972 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
3973 {
3974 if (type_is)
3975 {
3976 n1 = (rettv->v_type == var2.v_type
3977 && rettv->vval.v_list == var2.vval.v_list);
3978 if (type == TYPE_NEQUAL)
3979 n1 = !n1;
3980 }
3981 else if (rettv->v_type != var2.v_type
3982 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3983 {
3984 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003985 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003986 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003987 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003988 clear_tv(rettv);
3989 clear_tv(&var2);
3990 return FAIL;
3991 }
3992 else
3993 {
3994 /* Compare two Lists for being equal or unequal. */
3995 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
3996 if (type == TYPE_NEQUAL)
3997 n1 = !n1;
3998 }
3999 }
4000
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004001 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4002 {
4003 if (type_is)
4004 {
4005 n1 = (rettv->v_type == var2.v_type
4006 && rettv->vval.v_dict == var2.vval.v_dict);
4007 if (type == TYPE_NEQUAL)
4008 n1 = !n1;
4009 }
4010 else if (rettv->v_type != var2.v_type
4011 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4012 {
4013 if (rettv->v_type != var2.v_type)
4014 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4015 else
4016 EMSG(_("E736: Invalid operation for Dictionary"));
4017 clear_tv(rettv);
4018 clear_tv(&var2);
4019 return FAIL;
4020 }
4021 else
4022 {
4023 /* Compare two Dictionaries for being equal or unequal. */
4024 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4025 if (type == TYPE_NEQUAL)
4026 n1 = !n1;
4027 }
4028 }
4029
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004030 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4031 {
4032 if (rettv->v_type != var2.v_type
4033 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4034 {
4035 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004036 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004037 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004038 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004039 clear_tv(rettv);
4040 clear_tv(&var2);
4041 return FAIL;
4042 }
4043 else
4044 {
4045 /* Compare two Funcrefs for being equal or unequal. */
4046 if (rettv->vval.v_string == NULL
4047 || var2.vval.v_string == NULL)
4048 n1 = FALSE;
4049 else
4050 n1 = STRCMP(rettv->vval.v_string,
4051 var2.vval.v_string) == 0;
4052 if (type == TYPE_NEQUAL)
4053 n1 = !n1;
4054 }
4055 }
4056
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 /*
4058 * If one of the two variables is a number, compare as a number.
4059 * When using "=~" or "!~", always compare as string.
4060 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004061 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4063 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004064 n1 = get_tv_number(rettv);
4065 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 switch (type)
4067 {
4068 case TYPE_EQUAL: n1 = (n1 == n2); break;
4069 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4070 case TYPE_GREATER: n1 = (n1 > n2); break;
4071 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4072 case TYPE_SMALLER: n1 = (n1 < n2); break;
4073 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4074 case TYPE_UNKNOWN:
4075 case TYPE_MATCH:
4076 case TYPE_NOMATCH: break; /* avoid gcc warning */
4077 }
4078 }
4079 else
4080 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004081 s1 = get_tv_string_buf(rettv, buf1);
4082 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4084 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4085 else
4086 i = 0;
4087 n1 = FALSE;
4088 switch (type)
4089 {
4090 case TYPE_EQUAL: n1 = (i == 0); break;
4091 case TYPE_NEQUAL: n1 = (i != 0); break;
4092 case TYPE_GREATER: n1 = (i > 0); break;
4093 case TYPE_GEQUAL: n1 = (i >= 0); break;
4094 case TYPE_SMALLER: n1 = (i < 0); break;
4095 case TYPE_SEQUAL: n1 = (i <= 0); break;
4096
4097 case TYPE_MATCH:
4098 case TYPE_NOMATCH:
4099 /* avoid 'l' flag in 'cpoptions' */
4100 save_cpo = p_cpo;
4101 p_cpo = (char_u *)"";
4102 regmatch.regprog = vim_regcomp(s2,
4103 RE_MAGIC + RE_STRING);
4104 regmatch.rm_ic = ic;
4105 if (regmatch.regprog != NULL)
4106 {
4107 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4108 vim_free(regmatch.regprog);
4109 if (type == TYPE_NOMATCH)
4110 n1 = !n1;
4111 }
4112 p_cpo = save_cpo;
4113 break;
4114
4115 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4116 }
4117 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004118 clear_tv(rettv);
4119 clear_tv(&var2);
4120 rettv->v_type = VAR_NUMBER;
4121 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 }
4123 }
4124
4125 return OK;
4126}
4127
4128/*
4129 * Handle fourth level expression:
4130 * + number addition
4131 * - number subtraction
4132 * . string concatenation
4133 *
4134 * "arg" must point to the first non-white of the expression.
4135 * "arg" is advanced to the next non-white after the recognized expression.
4136 *
4137 * Return OK or FAIL.
4138 */
4139 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004140eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004142 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 int evaluate;
4144{
Bram Moolenaar33570922005-01-25 22:26:29 +00004145 typval_T var2;
4146 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 int op;
4148 long n1, n2;
4149 char_u *s1, *s2;
4150 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4151 char_u *p;
4152
4153 /*
4154 * Get the first variable.
4155 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004156 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 return FAIL;
4158
4159 /*
4160 * Repeat computing, until no '+', '-' or '.' is following.
4161 */
4162 for (;;)
4163 {
4164 op = **arg;
4165 if (op != '+' && op != '-' && op != '.')
4166 break;
4167
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004168 if (op != '+' || rettv->v_type != VAR_LIST)
4169 {
4170 /* For "list + ...", an illegal use of the first operand as
4171 * a number cannot be determined before evaluating the 2nd
4172 * operand: if this is also a list, all is ok.
4173 * For "something . ...", "something - ..." or "non-list + ...",
4174 * we know that the first operand needs to be a string or number
4175 * without evaluating the 2nd operand. So check before to avoid
4176 * side effects after an error. */
4177 if (evaluate && get_tv_string_chk(rettv) == NULL)
4178 {
4179 clear_tv(rettv);
4180 return FAIL;
4181 }
4182 }
4183
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 /*
4185 * Get the second variable.
4186 */
4187 *arg = skipwhite(*arg + 1);
4188 if (eval6(arg, &var2, evaluate) == FAIL)
4189 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004190 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 return FAIL;
4192 }
4193
4194 if (evaluate)
4195 {
4196 /*
4197 * Compute the result.
4198 */
4199 if (op == '.')
4200 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004201 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4202 s2 = get_tv_string_buf_chk(&var2, buf2);
4203 if (s2 == NULL) /* type error ? */
4204 {
4205 clear_tv(rettv);
4206 clear_tv(&var2);
4207 return FAIL;
4208 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004209 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004210 clear_tv(rettv);
4211 rettv->v_type = VAR_STRING;
4212 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004214 else if (op == '+' && rettv->v_type == VAR_LIST
4215 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004216 {
4217 /* concatenate Lists */
4218 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4219 &var3) == FAIL)
4220 {
4221 clear_tv(rettv);
4222 clear_tv(&var2);
4223 return FAIL;
4224 }
4225 clear_tv(rettv);
4226 *rettv = var3;
4227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 else
4229 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004230 int error = FALSE;
4231
4232 n1 = get_tv_number_chk(rettv, &error);
4233 if (error)
4234 {
4235 /* This can only happen for "list + non-list".
4236 * For "non-list + ..." or "something - ...", we returned
4237 * before evaluating the 2nd operand. */
4238 clear_tv(rettv);
4239 return FAIL;
4240 }
4241 n2 = get_tv_number_chk(&var2, &error);
4242 if (error)
4243 {
4244 clear_tv(rettv);
4245 clear_tv(&var2);
4246 return FAIL;
4247 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004248 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 if (op == '+')
4250 n1 = n1 + n2;
4251 else
4252 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004253 rettv->v_type = VAR_NUMBER;
4254 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004256 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 }
4258 }
4259 return OK;
4260}
4261
4262/*
4263 * Handle fifth level expression:
4264 * * number multiplication
4265 * / number division
4266 * % number modulo
4267 *
4268 * "arg" must point to the first non-white of the expression.
4269 * "arg" is advanced to the next non-white after the recognized expression.
4270 *
4271 * Return OK or FAIL.
4272 */
4273 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004274eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004276 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 int evaluate;
4278{
Bram Moolenaar33570922005-01-25 22:26:29 +00004279 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 int op;
4281 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004282 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283
4284 /*
4285 * Get the first variable.
4286 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004287 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 return FAIL;
4289
4290 /*
4291 * Repeat computing, until no '*', '/' or '%' is following.
4292 */
4293 for (;;)
4294 {
4295 op = **arg;
4296 if (op != '*' && op != '/' && op != '%')
4297 break;
4298
4299 if (evaluate)
4300 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004301 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004302 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004303 if (error)
4304 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 }
4306 else
4307 n1 = 0;
4308
4309 /*
4310 * Get the second variable.
4311 */
4312 *arg = skipwhite(*arg + 1);
4313 if (eval7(arg, &var2, evaluate) == FAIL)
4314 return FAIL;
4315
4316 if (evaluate)
4317 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004318 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004319 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004320 if (error)
4321 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322
4323 /*
4324 * Compute the result.
4325 */
4326 if (op == '*')
4327 n1 = n1 * n2;
4328 else if (op == '/')
4329 {
4330 if (n2 == 0) /* give an error message? */
4331 n1 = 0x7fffffffL;
4332 else
4333 n1 = n1 / n2;
4334 }
4335 else
4336 {
4337 if (n2 == 0) /* give an error message? */
4338 n1 = 0;
4339 else
4340 n1 = n1 % n2;
4341 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004342 rettv->v_type = VAR_NUMBER;
4343 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 }
4345 }
4346
4347 return OK;
4348}
4349
4350/*
4351 * Handle sixth level expression:
4352 * number number constant
4353 * "string" string contstant
4354 * 'string' literal string contstant
4355 * &option-name option value
4356 * @r register contents
4357 * identifier variable value
4358 * function() function call
4359 * $VAR environment variable
4360 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004361 * [expr, expr] List
4362 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 *
4364 * Also handle:
4365 * ! in front logical NOT
4366 * - in front unary minus
4367 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004368 * trailing [] subscript in String or List
4369 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 *
4371 * "arg" must point to the first non-white of the expression.
4372 * "arg" is advanced to the next non-white after the recognized expression.
4373 *
4374 * Return OK or FAIL.
4375 */
4376 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004377eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004379 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 int evaluate;
4381{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 long n;
4383 int len;
4384 char_u *s;
4385 int val;
4386 char_u *start_leader, *end_leader;
4387 int ret = OK;
4388 char_u *alias;
4389
4390 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004391 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004392 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004394 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395
4396 /*
4397 * Skip '!' and '-' characters. They are handled later.
4398 */
4399 start_leader = *arg;
4400 while (**arg == '!' || **arg == '-' || **arg == '+')
4401 *arg = skipwhite(*arg + 1);
4402 end_leader = *arg;
4403
4404 switch (**arg)
4405 {
4406 /*
4407 * Number constant.
4408 */
4409 case '0':
4410 case '1':
4411 case '2':
4412 case '3':
4413 case '4':
4414 case '5':
4415 case '6':
4416 case '7':
4417 case '8':
4418 case '9':
4419 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4420 *arg += len;
4421 if (evaluate)
4422 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004423 rettv->v_type = VAR_NUMBER;
4424 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 }
4426 break;
4427
4428 /*
4429 * String constant: "string".
4430 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004431 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 break;
4433
4434 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004435 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004437 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004438 break;
4439
4440 /*
4441 * List: [expr, expr]
4442 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004443 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 break;
4445
4446 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004447 * Dictionary: {key: val, key: val}
4448 */
4449 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4450 break;
4451
4452 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004453 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004455 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 break;
4457
4458 /*
4459 * Environment variable: $VAR.
4460 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004461 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 break;
4463
4464 /*
4465 * Register contents: @r.
4466 */
4467 case '@': ++*arg;
4468 if (evaluate)
4469 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004470 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004471 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 }
4473 if (**arg != NUL)
4474 ++*arg;
4475 break;
4476
4477 /*
4478 * nested expression: (expression).
4479 */
4480 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004481 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 if (**arg == ')')
4483 ++*arg;
4484 else if (ret == OK)
4485 {
4486 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004487 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 ret = FAIL;
4489 }
4490 break;
4491
Bram Moolenaar8c711452005-01-14 21:53:12 +00004492 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 break;
4494 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004495
4496 if (ret == NOTDONE)
4497 {
4498 /*
4499 * Must be a variable or function name.
4500 * Can also be a curly-braces kind of name: {expr}.
4501 */
4502 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004503 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004504 if (alias != NULL)
4505 s = alias;
4506
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004507 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004508 ret = FAIL;
4509 else
4510 {
4511 if (**arg == '(') /* recursive! */
4512 {
4513 /* If "s" is the name of a variable of type VAR_FUNC
4514 * use its contents. */
4515 s = deref_func_name(s, &len);
4516
4517 /* Invoke the function. */
4518 ret = get_func_tv(s, len, rettv, arg,
4519 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004520 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004521 /* Stop the expression evaluation when immediately
4522 * aborting on error, or when an interrupt occurred or
4523 * an exception was thrown but not caught. */
4524 if (aborting())
4525 {
4526 if (ret == OK)
4527 clear_tv(rettv);
4528 ret = FAIL;
4529 }
4530 }
4531 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004532 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004533 else
4534 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004535 }
4536
4537 if (alias != NULL)
4538 vim_free(alias);
4539 }
4540
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 *arg = skipwhite(*arg);
4542
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004543 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4544 * expr(expr). */
4545 if (ret == OK)
4546 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547
4548 /*
4549 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4550 */
4551 if (ret == OK && evaluate && end_leader > start_leader)
4552 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004553 int error = FALSE;
4554
4555 val = get_tv_number_chk(rettv, &error);
4556 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004558 clear_tv(rettv);
4559 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004561 else
4562 {
4563 while (end_leader > start_leader)
4564 {
4565 --end_leader;
4566 if (*end_leader == '!')
4567 val = !val;
4568 else if (*end_leader == '-')
4569 val = -val;
4570 }
4571 clear_tv(rettv);
4572 rettv->v_type = VAR_NUMBER;
4573 rettv->vval.v_number = val;
4574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 }
4576
4577 return ret;
4578}
4579
4580/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004581 * Evaluate an "[expr]" or "[expr:expr]" index.
4582 * "*arg" points to the '['.
4583 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4584 */
4585 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004586eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004587 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004588 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004589 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004590 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004591{
4592 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004593 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004594 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004595 long len = -1;
4596 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004597 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004598 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004599
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004600 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004601 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004602 if (verbose)
4603 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004604 return FAIL;
4605 }
4606
Bram Moolenaar8c711452005-01-14 21:53:12 +00004607 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004608 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004609 /*
4610 * dict.name
4611 */
4612 key = *arg + 1;
4613 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4614 ;
4615 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004616 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004617 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004618 }
4619 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004620 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004621 /*
4622 * something[idx]
4623 *
4624 * Get the (first) variable from inside the [].
4625 */
4626 *arg = skipwhite(*arg + 1);
4627 if (**arg == ':')
4628 empty1 = TRUE;
4629 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4630 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004631 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4632 {
4633 /* not a number or string */
4634 clear_tv(&var1);
4635 return FAIL;
4636 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004637
4638 /*
4639 * Get the second variable from inside the [:].
4640 */
4641 if (**arg == ':')
4642 {
4643 range = TRUE;
4644 *arg = skipwhite(*arg + 1);
4645 if (**arg == ']')
4646 empty2 = TRUE;
4647 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4648 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004649 if (!empty1)
4650 clear_tv(&var1);
4651 return FAIL;
4652 }
4653 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4654 {
4655 /* not a number or string */
4656 if (!empty1)
4657 clear_tv(&var1);
4658 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004659 return FAIL;
4660 }
4661 }
4662
4663 /* Check for the ']'. */
4664 if (**arg != ']')
4665 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004666 if (verbose)
4667 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004668 clear_tv(&var1);
4669 if (range)
4670 clear_tv(&var2);
4671 return FAIL;
4672 }
4673 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004674 }
4675
4676 if (evaluate)
4677 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004678 n1 = 0;
4679 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004680 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004681 n1 = get_tv_number(&var1);
4682 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004683 }
4684 if (range)
4685 {
4686 if (empty2)
4687 n2 = -1;
4688 else
4689 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004690 n2 = get_tv_number(&var2);
4691 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004692 }
4693 }
4694
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004695 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004696 {
4697 case VAR_NUMBER:
4698 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004699 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004700 len = (long)STRLEN(s);
4701 if (range)
4702 {
4703 /* The resulting variable is a substring. If the indexes
4704 * are out of range the result is empty. */
4705 if (n1 < 0)
4706 {
4707 n1 = len + n1;
4708 if (n1 < 0)
4709 n1 = 0;
4710 }
4711 if (n2 < 0)
4712 n2 = len + n2;
4713 else if (n2 >= len)
4714 n2 = len;
4715 if (n1 >= len || n2 < 0 || n1 > n2)
4716 s = NULL;
4717 else
4718 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4719 }
4720 else
4721 {
4722 /* The resulting variable is a string of a single
4723 * character. If the index is too big or negative the
4724 * result is empty. */
4725 if (n1 >= len || n1 < 0)
4726 s = NULL;
4727 else
4728 s = vim_strnsave(s + n1, 1);
4729 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004730 clear_tv(rettv);
4731 rettv->v_type = VAR_STRING;
4732 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004733 break;
4734
4735 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004736 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004737 if (n1 < 0)
4738 n1 = len + n1;
4739 if (!empty1 && (n1 < 0 || n1 >= len))
4740 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004741 if (verbose)
4742 EMSGN(_(e_listidx), n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004743 return FAIL;
4744 }
4745 if (range)
4746 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004747 list_T *l;
4748 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004749
4750 if (n2 < 0)
4751 n2 = len + n2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004752 if (!empty2 && (n2 < 0 || n2 >= len || n2 + 1 < n1))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004753 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004754 if (verbose)
4755 EMSGN(_(e_listidx), n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004756 return FAIL;
4757 }
4758 l = list_alloc();
4759 if (l == NULL)
4760 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004761 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004762 n1 <= n2; ++n1)
4763 {
4764 if (list_append_tv(l, &item->li_tv) == FAIL)
4765 {
4766 list_free(l);
4767 return FAIL;
4768 }
4769 item = item->li_next;
4770 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004771 clear_tv(rettv);
4772 rettv->v_type = VAR_LIST;
4773 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004774 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004775 }
4776 else
4777 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004778 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv,
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004779 &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004780 clear_tv(rettv);
4781 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004782 }
4783 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004784
4785 case VAR_DICT:
4786 if (range)
4787 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004788 if (verbose)
4789 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004790 if (len == -1)
4791 clear_tv(&var1);
4792 return FAIL;
4793 }
4794 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004795 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004796
4797 if (len == -1)
4798 {
4799 key = get_tv_string(&var1);
4800 if (*key == NUL)
4801 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004802 if (verbose)
4803 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004804 clear_tv(&var1);
4805 return FAIL;
4806 }
4807 }
4808
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004809 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004810
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004811 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004812 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004813 if (len == -1)
4814 clear_tv(&var1);
4815 if (item == NULL)
4816 return FAIL;
4817
4818 copy_tv(&item->di_tv, &var1);
4819 clear_tv(rettv);
4820 *rettv = var1;
4821 }
4822 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004823 }
4824 }
4825
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004826 return OK;
4827}
4828
4829/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 * Get an option value.
4831 * "arg" points to the '&' or '+' before the option name.
4832 * "arg" is advanced to character after the option name.
4833 * Return OK or FAIL.
4834 */
4835 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004836get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004838 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 int evaluate;
4840{
4841 char_u *option_end;
4842 long numval;
4843 char_u *stringval;
4844 int opt_type;
4845 int c;
4846 int working = (**arg == '+'); /* has("+option") */
4847 int ret = OK;
4848 int opt_flags;
4849
4850 /*
4851 * Isolate the option name and find its value.
4852 */
4853 option_end = find_option_end(arg, &opt_flags);
4854 if (option_end == NULL)
4855 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004856 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 EMSG2(_("E112: Option name missing: %s"), *arg);
4858 return FAIL;
4859 }
4860
4861 if (!evaluate)
4862 {
4863 *arg = option_end;
4864 return OK;
4865 }
4866
4867 c = *option_end;
4868 *option_end = NUL;
4869 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004870 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871
4872 if (opt_type == -3) /* invalid name */
4873 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004874 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 EMSG2(_("E113: Unknown option: %s"), *arg);
4876 ret = FAIL;
4877 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004878 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 {
4880 if (opt_type == -2) /* hidden string option */
4881 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004882 rettv->v_type = VAR_STRING;
4883 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 }
4885 else if (opt_type == -1) /* hidden number option */
4886 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004887 rettv->v_type = VAR_NUMBER;
4888 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 }
4890 else if (opt_type == 1) /* number option */
4891 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004892 rettv->v_type = VAR_NUMBER;
4893 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 }
4895 else /* string option */
4896 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004897 rettv->v_type = VAR_STRING;
4898 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 }
4900 }
4901 else if (working && (opt_type == -2 || opt_type == -1))
4902 ret = FAIL;
4903
4904 *option_end = c; /* put back for error messages */
4905 *arg = option_end;
4906
4907 return ret;
4908}
4909
4910/*
4911 * Allocate a variable for a string constant.
4912 * Return OK or FAIL.
4913 */
4914 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004915get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004917 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 int evaluate;
4919{
4920 char_u *p;
4921 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 int extra = 0;
4923
4924 /*
4925 * Find the end of the string, skipping backslashed characters.
4926 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004927 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 {
4929 if (*p == '\\' && p[1] != NUL)
4930 {
4931 ++p;
4932 /* A "\<x>" form occupies at least 4 characters, and produces up
4933 * to 6 characters: reserve space for 2 extra */
4934 if (*p == '<')
4935 extra += 2;
4936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 }
4938
4939 if (*p != '"')
4940 {
4941 EMSG2(_("E114: Missing quote: %s"), *arg);
4942 return FAIL;
4943 }
4944
4945 /* If only parsing, set *arg and return here */
4946 if (!evaluate)
4947 {
4948 *arg = p + 1;
4949 return OK;
4950 }
4951
4952 /*
4953 * Copy the string into allocated memory, handling backslashed
4954 * characters.
4955 */
4956 name = alloc((unsigned)(p - *arg + extra));
4957 if (name == NULL)
4958 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004959 rettv->v_type = VAR_STRING;
4960 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961
Bram Moolenaar8c711452005-01-14 21:53:12 +00004962 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 {
4964 if (*p == '\\')
4965 {
4966 switch (*++p)
4967 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004968 case 'b': *name++ = BS; ++p; break;
4969 case 'e': *name++ = ESC; ++p; break;
4970 case 'f': *name++ = FF; ++p; break;
4971 case 'n': *name++ = NL; ++p; break;
4972 case 'r': *name++ = CAR; ++p; break;
4973 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974
4975 case 'X': /* hex: "\x1", "\x12" */
4976 case 'x':
4977 case 'u': /* Unicode: "\u0023" */
4978 case 'U':
4979 if (vim_isxdigit(p[1]))
4980 {
4981 int n, nr;
4982 int c = toupper(*p);
4983
4984 if (c == 'X')
4985 n = 2;
4986 else
4987 n = 4;
4988 nr = 0;
4989 while (--n >= 0 && vim_isxdigit(p[1]))
4990 {
4991 ++p;
4992 nr = (nr << 4) + hex2nr(*p);
4993 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004994 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995#ifdef FEAT_MBYTE
4996 /* For "\u" store the number according to
4997 * 'encoding'. */
4998 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00004999 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 else
5001#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005002 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 break;
5005
5006 /* octal: "\1", "\12", "\123" */
5007 case '0':
5008 case '1':
5009 case '2':
5010 case '3':
5011 case '4':
5012 case '5':
5013 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005014 case '7': *name = *p++ - '0';
5015 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005017 *name = (*name << 3) + *p++ - '0';
5018 if (*p >= '0' && *p <= '7')
5019 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005021 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 break;
5023
5024 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005025 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 if (extra != 0)
5027 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005028 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 break;
5030 }
5031 /* FALLTHROUGH */
5032
Bram Moolenaar8c711452005-01-14 21:53:12 +00005033 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 break;
5035 }
5036 }
5037 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005038 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005041 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 *arg = p + 1;
5043
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 return OK;
5045}
5046
5047/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005048 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 * Return OK or FAIL.
5050 */
5051 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005052get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005054 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 int evaluate;
5056{
5057 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005058 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005059 int reduce = 0;
5060
5061 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005062 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005063 */
5064 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5065 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005066 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005067 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005068 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005069 break;
5070 ++reduce;
5071 ++p;
5072 }
5073 }
5074
Bram Moolenaar8c711452005-01-14 21:53:12 +00005075 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005076 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005077 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005078 return FAIL;
5079 }
5080
Bram Moolenaar8c711452005-01-14 21:53:12 +00005081 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005082 if (!evaluate)
5083 {
5084 *arg = p + 1;
5085 return OK;
5086 }
5087
5088 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005089 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005090 */
5091 str = alloc((unsigned)((p - *arg) - reduce));
5092 if (str == NULL)
5093 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005094 rettv->v_type = VAR_STRING;
5095 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005096
Bram Moolenaar8c711452005-01-14 21:53:12 +00005097 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005098 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005099 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005100 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005101 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005102 break;
5103 ++p;
5104 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005105 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005106 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005107 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005108 *arg = p + 1;
5109
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005110 return OK;
5111}
5112
5113/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005114 * Allocate a variable for a List and fill it from "*arg".
5115 * Return OK or FAIL.
5116 */
5117 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005118get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005119 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005120 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005121 int evaluate;
5122{
Bram Moolenaar33570922005-01-25 22:26:29 +00005123 list_T *l = NULL;
5124 typval_T tv;
5125 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005126
5127 if (evaluate)
5128 {
5129 l = list_alloc();
5130 if (l == NULL)
5131 return FAIL;
5132 }
5133
5134 *arg = skipwhite(*arg + 1);
5135 while (**arg != ']' && **arg != NUL)
5136 {
5137 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5138 goto failret;
5139 if (evaluate)
5140 {
5141 item = listitem_alloc();
5142 if (item != NULL)
5143 {
5144 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005145 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005146 list_append(l, item);
5147 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005148 else
5149 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005150 }
5151
5152 if (**arg == ']')
5153 break;
5154 if (**arg != ',')
5155 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005156 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005157 goto failret;
5158 }
5159 *arg = skipwhite(*arg + 1);
5160 }
5161
5162 if (**arg != ']')
5163 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005164 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005165failret:
5166 if (evaluate)
5167 list_free(l);
5168 return FAIL;
5169 }
5170
5171 *arg = skipwhite(*arg + 1);
5172 if (evaluate)
5173 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005174 rettv->v_type = VAR_LIST;
5175 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005176 ++l->lv_refcount;
5177 }
5178
5179 return OK;
5180}
5181
5182/*
5183 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005184 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005185 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005186 static list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005187list_alloc()
5188{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005189 list_T *l;
5190
5191 l = (list_T *)alloc_clear(sizeof(list_T));
5192 if (l != NULL)
5193 {
5194 /* Prepend the list to the list of lists for garbage collection. */
5195 if (first_list != NULL)
5196 first_list->lv_used_prev = l;
5197 l->lv_used_prev = NULL;
5198 l->lv_used_next = first_list;
5199 first_list = l;
5200 }
5201 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005202}
5203
5204/*
5205 * Unreference a list: decrement the reference count and free it when it
5206 * becomes zero.
5207 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005208 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005209list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005210 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005211{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005212 if (l != NULL && l->lv_refcount != DEL_REFCOUNT && --l->lv_refcount <= 0)
5213 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005214}
5215
5216/*
5217 * Free a list, including all items it points to.
5218 * Ignores the reference count.
5219 */
5220 static void
5221list_free(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005222 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005223{
Bram Moolenaar33570922005-01-25 22:26:29 +00005224 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005225
Bram Moolenaard9fba312005-06-26 22:34:35 +00005226 /* Avoid that recursive reference to the list frees us again. */
5227 l->lv_refcount = DEL_REFCOUNT;
5228
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005229 /* Remove the list from the list of lists for garbage collection. */
5230 if (l->lv_used_prev == NULL)
5231 first_list = l->lv_used_next;
5232 else
5233 l->lv_used_prev->lv_used_next = l->lv_used_next;
5234 if (l->lv_used_next != NULL)
5235 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5236
Bram Moolenaard9fba312005-06-26 22:34:35 +00005237 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005238 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005239 /* Remove the item before deleting it. */
5240 l->lv_first = item->li_next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005241 listitem_free(item);
5242 }
5243 vim_free(l);
5244}
5245
5246/*
5247 * Allocate a list item.
5248 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005249 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005250listitem_alloc()
5251{
Bram Moolenaar33570922005-01-25 22:26:29 +00005252 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005253}
5254
5255/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005256 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005257 */
5258 static void
5259listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005260 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005261{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005262 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005263 vim_free(item);
5264}
5265
5266/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005267 * Remove a list item from a List and free it. Also clears the value.
5268 */
5269 static void
5270listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005271 list_T *l;
5272 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005273{
5274 list_remove(l, item, item);
5275 listitem_free(item);
5276}
5277
5278/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005279 * Get the number of items in a list.
5280 */
5281 static long
5282list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005283 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005284{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005285 if (l == NULL)
5286 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005287 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005288}
5289
5290/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005291 * Return TRUE when two lists have exactly the same values.
5292 */
5293 static int
5294list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005295 list_T *l1;
5296 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005297 int ic; /* ignore case for strings */
5298{
Bram Moolenaar33570922005-01-25 22:26:29 +00005299 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005300
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005301 if (list_len(l1) != list_len(l2))
5302 return FALSE;
5303
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005304 for (item1 = l1->lv_first, item2 = l2->lv_first;
5305 item1 != NULL && item2 != NULL;
5306 item1 = item1->li_next, item2 = item2->li_next)
5307 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5308 return FALSE;
5309 return item1 == NULL && item2 == NULL;
5310}
5311
5312/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005313 * Return TRUE when two dictionaries have exactly the same key/values.
5314 */
5315 static int
5316dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005317 dict_T *d1;
5318 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005319 int ic; /* ignore case for strings */
5320{
Bram Moolenaar33570922005-01-25 22:26:29 +00005321 hashitem_T *hi;
5322 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005323 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005324
5325 if (dict_len(d1) != dict_len(d2))
5326 return FALSE;
5327
Bram Moolenaar33570922005-01-25 22:26:29 +00005328 todo = d1->dv_hashtab.ht_used;
5329 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005330 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005331 if (!HASHITEM_EMPTY(hi))
5332 {
5333 item2 = dict_find(d2, hi->hi_key, -1);
5334 if (item2 == NULL)
5335 return FALSE;
5336 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5337 return FALSE;
5338 --todo;
5339 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005340 }
5341 return TRUE;
5342}
5343
5344/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005345 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005346 * Compares the items just like "==" would compare them, but strings and
5347 * numbers are different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005348 */
5349 static int
5350tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005351 typval_T *tv1;
5352 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005353 int ic; /* ignore case */
5354{
5355 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005356 char_u *s1, *s2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005357
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005358 if (tv1->v_type != tv2->v_type)
5359 return FALSE;
5360
5361 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005362 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005363 case VAR_LIST:
5364 /* recursive! */
5365 return list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5366
5367 case VAR_DICT:
5368 /* recursive! */
5369 return dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5370
5371 case VAR_FUNC:
5372 return (tv1->vval.v_string != NULL
5373 && tv2->vval.v_string != NULL
5374 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5375
5376 case VAR_NUMBER:
5377 return tv1->vval.v_number == tv2->vval.v_number;
5378
5379 case VAR_STRING:
5380 s1 = get_tv_string_buf(tv1, buf1);
5381 s2 = get_tv_string_buf(tv2, buf2);
5382 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005383 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005384
5385 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005386 return TRUE;
5387}
5388
5389/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005390 * Locate item with index "n" in list "l" and return it.
5391 * A negative index is counted from the end; -1 is the last item.
5392 * Returns NULL when "n" is out of range.
5393 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005394 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005395list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005396 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005397 long n;
5398{
Bram Moolenaar33570922005-01-25 22:26:29 +00005399 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005400 long idx;
5401
5402 if (l == NULL)
5403 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005404
5405 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005406 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005407 n = l->lv_len + n;
5408
5409 /* Check for index out of range. */
5410 if (n < 0 || n >= l->lv_len)
5411 return NULL;
5412
5413 /* When there is a cached index may start search from there. */
5414 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005415 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005416 if (n < l->lv_idx / 2)
5417 {
5418 /* closest to the start of the list */
5419 item = l->lv_first;
5420 idx = 0;
5421 }
5422 else if (n > (l->lv_idx + l->lv_len) / 2)
5423 {
5424 /* closest to the end of the list */
5425 item = l->lv_last;
5426 idx = l->lv_len - 1;
5427 }
5428 else
5429 {
5430 /* closest to the cached index */
5431 item = l->lv_idx_item;
5432 idx = l->lv_idx;
5433 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005434 }
5435 else
5436 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005437 if (n < l->lv_len / 2)
5438 {
5439 /* closest to the start of the list */
5440 item = l->lv_first;
5441 idx = 0;
5442 }
5443 else
5444 {
5445 /* closest to the end of the list */
5446 item = l->lv_last;
5447 idx = l->lv_len - 1;
5448 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005449 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005450
5451 while (n > idx)
5452 {
5453 /* search forward */
5454 item = item->li_next;
5455 ++idx;
5456 }
5457 while (n < idx)
5458 {
5459 /* search backward */
5460 item = item->li_prev;
5461 --idx;
5462 }
5463
5464 /* cache the used index */
5465 l->lv_idx = idx;
5466 l->lv_idx_item = item;
5467
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005468 return item;
5469}
5470
5471/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005472 * Locate "item" list "l" and return its index.
5473 * Returns -1 when "item" is not in the list.
5474 */
5475 static long
5476list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005477 list_T *l;
5478 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005479{
5480 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005481 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005482
5483 if (l == NULL)
5484 return -1;
5485 idx = 0;
5486 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5487 ++idx;
5488 if (li == NULL)
5489 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005490 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005491}
5492
5493/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005494 * Append item "item" to the end of list "l".
5495 */
5496 static void
5497list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005498 list_T *l;
5499 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005500{
5501 if (l->lv_last == NULL)
5502 {
5503 /* empty list */
5504 l->lv_first = item;
5505 l->lv_last = item;
5506 item->li_prev = NULL;
5507 }
5508 else
5509 {
5510 l->lv_last->li_next = item;
5511 item->li_prev = l->lv_last;
5512 l->lv_last = item;
5513 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005514 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005515 item->li_next = NULL;
5516}
5517
5518/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005519 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005520 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005521 */
5522 static int
5523list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005524 list_T *l;
5525 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005526{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005527 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005528
Bram Moolenaar05159a02005-02-26 23:04:13 +00005529 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005530 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005531 copy_tv(tv, &li->li_tv);
5532 list_append(l, li);
5533 return OK;
5534}
5535
5536/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005537 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005538 * Return FAIL when out of memory.
5539 */
5540 int
5541list_append_dict(list, dict)
5542 list_T *list;
5543 dict_T *dict;
5544{
5545 listitem_T *li = listitem_alloc();
5546
5547 if (li == NULL)
5548 return FAIL;
5549 li->li_tv.v_type = VAR_DICT;
5550 li->li_tv.v_lock = 0;
5551 li->li_tv.vval.v_dict = dict;
5552 list_append(list, li);
5553 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005554 return OK;
5555}
5556
5557/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005558 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00005559 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005560 * Returns FAIL when out of memory.
5561 */
5562 static int
Bram Moolenaar4463f292005-09-25 22:20:24 +00005563list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005564 list_T *l;
5565 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00005566 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005567{
5568 listitem_T *li = listitem_alloc();
5569
5570 if (li == NULL)
5571 return FAIL;
5572 list_append(l, li);
5573 li->li_tv.v_type = VAR_STRING;
5574 li->li_tv.v_lock = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005575 if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
5576 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005577 return FAIL;
5578 return OK;
5579}
5580
5581/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00005582 * Append "n" to list "l".
5583 * Returns FAIL when out of memory.
5584 */
5585 static int
5586list_append_number(l, n)
5587 list_T *l;
5588 varnumber_T n;
5589{
5590 listitem_T *li;
5591
5592 li = listitem_alloc();
5593 if (li == NULL)
5594 return FAIL;
5595 li->li_tv.v_type = VAR_NUMBER;
5596 li->li_tv.v_lock = 0;
5597 li->li_tv.vval.v_number = n;
5598 list_append(l, li);
5599 return OK;
5600}
5601
5602/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005603 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005604 * If "item" is NULL append at the end.
5605 * Return FAIL when out of memory.
5606 */
5607 static int
5608list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005609 list_T *l;
5610 typval_T *tv;
5611 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005612{
Bram Moolenaar33570922005-01-25 22:26:29 +00005613 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005614
5615 if (ni == NULL)
5616 return FAIL;
5617 copy_tv(tv, &ni->li_tv);
5618 if (item == NULL)
5619 /* Append new item at end of list. */
5620 list_append(l, ni);
5621 else
5622 {
5623 /* Insert new item before existing item. */
5624 ni->li_prev = item->li_prev;
5625 ni->li_next = item;
5626 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005627 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005628 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005629 ++l->lv_idx;
5630 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005631 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005632 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005633 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005634 l->lv_idx_item = NULL;
5635 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005636 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005637 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005638 }
5639 return OK;
5640}
5641
5642/*
5643 * Extend "l1" with "l2".
5644 * If "bef" is NULL append at the end, otherwise insert before this item.
5645 * Returns FAIL when out of memory.
5646 */
5647 static int
5648list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005649 list_T *l1;
5650 list_T *l2;
5651 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005652{
Bram Moolenaar33570922005-01-25 22:26:29 +00005653 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005654
5655 for (item = l2->lv_first; item != NULL; item = item->li_next)
5656 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5657 return FAIL;
5658 return OK;
5659}
5660
5661/*
5662 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5663 * Return FAIL when out of memory.
5664 */
5665 static int
5666list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005667 list_T *l1;
5668 list_T *l2;
5669 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005670{
Bram Moolenaar33570922005-01-25 22:26:29 +00005671 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005672
5673 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005674 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005675 if (l == NULL)
5676 return FAIL;
5677 tv->v_type = VAR_LIST;
5678 tv->vval.v_list = l;
5679
5680 /* append all items from the second list */
5681 return list_extend(l, l2, NULL);
5682}
5683
5684/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005685 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005686 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005687 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005688 * Returns NULL when out of memory.
5689 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005690 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005691list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005692 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005693 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005694 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005695{
Bram Moolenaar33570922005-01-25 22:26:29 +00005696 list_T *copy;
5697 listitem_T *item;
5698 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005699
5700 if (orig == NULL)
5701 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005702
5703 copy = list_alloc();
5704 if (copy != NULL)
5705 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005706 if (copyID != 0)
5707 {
5708 /* Do this before adding the items, because one of the items may
5709 * refer back to this list. */
5710 orig->lv_copyID = copyID;
5711 orig->lv_copylist = copy;
5712 }
5713 for (item = orig->lv_first; item != NULL && !got_int;
5714 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005715 {
5716 ni = listitem_alloc();
5717 if (ni == NULL)
5718 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005719 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005720 {
5721 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5722 {
5723 vim_free(ni);
5724 break;
5725 }
5726 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005727 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005728 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005729 list_append(copy, ni);
5730 }
5731 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005732 if (item != NULL)
5733 {
5734 list_unref(copy);
5735 copy = NULL;
5736 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005737 }
5738
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005739 return copy;
5740}
5741
5742/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005743 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005744 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005745 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005746 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005747list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005748 list_T *l;
5749 listitem_T *item;
5750 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005751{
Bram Moolenaar33570922005-01-25 22:26:29 +00005752 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005753
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005754 /* notify watchers */
5755 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005756 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005757 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005758 list_fix_watch(l, ip);
5759 if (ip == item2)
5760 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005761 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005762
5763 if (item2->li_next == NULL)
5764 l->lv_last = item->li_prev;
5765 else
5766 item2->li_next->li_prev = item->li_prev;
5767 if (item->li_prev == NULL)
5768 l->lv_first = item2->li_next;
5769 else
5770 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005771 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005772}
5773
5774/*
5775 * Return an allocated string with the string representation of a list.
5776 * May return NULL.
5777 */
5778 static char_u *
5779list2string(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005780 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005781{
5782 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005783
5784 if (tv->vval.v_list == NULL)
5785 return NULL;
5786 ga_init2(&ga, (int)sizeof(char), 80);
5787 ga_append(&ga, '[');
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005788 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE) == FAIL)
5789 {
5790 vim_free(ga.ga_data);
5791 return NULL;
5792 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005793 ga_append(&ga, ']');
5794 ga_append(&ga, NUL);
5795 return (char_u *)ga.ga_data;
5796}
5797
5798/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005799 * Join list "l" into a string in "*gap", using separator "sep".
5800 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005801 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005802 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005803 static int
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005804list_join(gap, l, sep, echo)
5805 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00005806 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005807 char_u *sep;
5808 int echo;
5809{
5810 int first = TRUE;
5811 char_u *tofree;
5812 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00005813 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005814 char_u *s;
5815
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005816 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005817 {
5818 if (first)
5819 first = FALSE;
5820 else
5821 ga_concat(gap, sep);
5822
5823 if (echo)
5824 s = echo_string(&item->li_tv, &tofree, numbuf);
5825 else
5826 s = tv2string(&item->li_tv, &tofree, numbuf);
5827 if (s != NULL)
5828 ga_concat(gap, s);
5829 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005830 if (s == NULL)
5831 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005832 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005833 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005834}
5835
5836/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005837 * Garbage collection for lists and dictionaries.
5838 *
5839 * We use reference counts to be able to free most items right away when they
5840 * are no longer used. But for composite items it's possible that it becomes
5841 * unused while the reference count is > 0: When there is a recursive
5842 * reference. Example:
5843 * :let l = [1, 2, 3]
5844 * :let d = {9: l}
5845 * :let l[1] = d
5846 *
5847 * Since this is quite unusual we handle this with garbage collection: every
5848 * once in a while find out which lists and dicts are not referenced from any
5849 * variable.
5850 *
5851 * Here is a good reference text about garbage collection (refers to Python
5852 * but it applies to all reference-counting mechanisms):
5853 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005854 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005855
5856/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005857 * Do garbage collection for lists and dicts.
5858 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005859 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005860 int
5861garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00005862{
5863 dict_T *dd;
5864 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005865 int copyID = ++current_copyID;
5866 buf_T *buf;
5867 win_T *wp;
5868 int i;
5869 funccall_T *fc;
5870 int did_free = FALSE;
5871
5872 /*
5873 * 1. Go through all accessible variables and mark all lists and dicts
5874 * with copyID.
5875 */
5876 /* script-local variables */
5877 for (i = 1; i <= ga_scripts.ga_len; ++i)
5878 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
5879
5880 /* buffer-local variables */
5881 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5882 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
5883
5884 /* window-local variables */
5885 FOR_ALL_WINDOWS(wp)
5886 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
5887
5888 /* global variables */
5889 set_ref_in_ht(&globvarht, copyID);
5890
5891 /* function-local variables */
5892 for (fc = current_funccal; fc != NULL; fc = fc->caller)
5893 {
5894 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
5895 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
5896 }
5897
5898 /*
5899 * 2. Go through the list of dicts and free items without the copyID.
5900 */
5901 for (dd = first_dict; dd != NULL; )
5902 if (dd->dv_copyID != copyID)
5903 {
5904 dict_free(dd);
5905 did_free = TRUE;
5906
5907 /* restart, next dict may also have been freed */
5908 dd = first_dict;
5909 }
5910 else
5911 dd = dd->dv_used_next;
5912
5913 /*
5914 * 3. Go through the list of lists and free items without the copyID.
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00005915 * But don't free a list that has a watcher (used in a for loop), these
5916 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005917 */
5918 for (ll = first_list; ll != NULL; )
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00005919 if (ll->lv_copyID != copyID && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005920 {
5921 list_free(ll);
5922 did_free = TRUE;
5923
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00005924 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005925 ll = first_list;
5926 }
5927 else
5928 ll = ll->lv_used_next;
5929
5930 return did_free;
5931}
5932
5933/*
5934 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
5935 */
5936 static void
5937set_ref_in_ht(ht, copyID)
5938 hashtab_T *ht;
5939 int copyID;
5940{
5941 int todo;
5942 hashitem_T *hi;
5943
5944 todo = ht->ht_used;
5945 for (hi = ht->ht_array; todo > 0; ++hi)
5946 if (!HASHITEM_EMPTY(hi))
5947 {
5948 --todo;
5949 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
5950 }
5951}
5952
5953/*
5954 * Mark all lists and dicts referenced through list "l" with "copyID".
5955 */
5956 static void
5957set_ref_in_list(l, copyID)
5958 list_T *l;
5959 int copyID;
5960{
5961 listitem_T *li;
5962
5963 for (li = l->lv_first; li != NULL; li = li->li_next)
5964 set_ref_in_item(&li->li_tv, copyID);
5965}
5966
5967/*
5968 * Mark all lists and dicts referenced through typval "tv" with "copyID".
5969 */
5970 static void
5971set_ref_in_item(tv, copyID)
5972 typval_T *tv;
5973 int copyID;
5974{
5975 dict_T *dd;
5976 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005977
5978 switch (tv->v_type)
5979 {
5980 case VAR_DICT:
5981 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005982 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005983 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005984 /* Didn't see this dict yet. */
5985 dd->dv_copyID = copyID;
5986 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00005987 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005988 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005989
5990 case VAR_LIST:
5991 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005992 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005993 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005994 /* Didn't see this list yet. */
5995 ll->lv_copyID = copyID;
5996 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00005997 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005998 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005999 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006000 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006001}
6002
6003/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006004 * Allocate an empty header for a dictionary.
6005 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006006 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006007dict_alloc()
6008{
Bram Moolenaar33570922005-01-25 22:26:29 +00006009 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006010
Bram Moolenaar33570922005-01-25 22:26:29 +00006011 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006012 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006013 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006014 /* Add the list to the hashtable for garbage collection. */
6015 if (first_dict != NULL)
6016 first_dict->dv_used_prev = d;
6017 d->dv_used_next = first_dict;
6018 d->dv_used_prev = NULL;
6019
Bram Moolenaar33570922005-01-25 22:26:29 +00006020 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006021 d->dv_lock = 0;
6022 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006023 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006024 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006025 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006026}
6027
6028/*
6029 * Unreference a Dictionary: decrement the reference count and free it when it
6030 * becomes zero.
6031 */
6032 static void
6033dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006034 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006035{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006036 if (d != NULL && d->dv_refcount != DEL_REFCOUNT && --d->dv_refcount <= 0)
6037 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006038}
6039
6040/*
6041 * Free a Dictionary, including all items it contains.
6042 * Ignores the reference count.
6043 */
6044 static void
6045dict_free(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006046 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006047{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006048 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006049 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006050 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006051
Bram Moolenaard9fba312005-06-26 22:34:35 +00006052 /* Avoid that recursive reference to the dict frees us again. */
6053 d->dv_refcount = DEL_REFCOUNT;
6054
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006055 /* Remove the dict from the list of dicts for garbage collection. */
6056 if (d->dv_used_prev == NULL)
6057 first_dict = d->dv_used_next;
6058 else
6059 d->dv_used_prev->dv_used_next = d->dv_used_next;
6060 if (d->dv_used_next != NULL)
6061 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6062
6063 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006064 hash_lock(&d->dv_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +00006065 todo = d->dv_hashtab.ht_used;
6066 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006067 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006068 if (!HASHITEM_EMPTY(hi))
6069 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006070 /* Remove the item before deleting it, just in case there is
6071 * something recursive causing trouble. */
6072 di = HI2DI(hi);
6073 hash_remove(&d->dv_hashtab, hi);
6074 dictitem_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006075 --todo;
6076 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006077 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006078 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006079 vim_free(d);
6080}
6081
6082/*
6083 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006084 * The "key" is copied to the new item.
6085 * Note that the value of the item "di_tv" still needs to be initialized!
6086 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006087 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006088 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006089dictitem_alloc(key)
6090 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006091{
Bram Moolenaar33570922005-01-25 22:26:29 +00006092 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006093
Bram Moolenaar33570922005-01-25 22:26:29 +00006094 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(key));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006095 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006096 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006097 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006098 di->di_flags = 0;
6099 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006100 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006101}
6102
6103/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006104 * Make a copy of a Dictionary item.
6105 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006106 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006107dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006108 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006109{
Bram Moolenaar33570922005-01-25 22:26:29 +00006110 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006111
Bram Moolenaar33570922005-01-25 22:26:29 +00006112 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(org->di_key));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006113 if (di != NULL)
6114 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006115 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006116 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006117 copy_tv(&org->di_tv, &di->di_tv);
6118 }
6119 return di;
6120}
6121
6122/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006123 * Remove item "item" from Dictionary "dict" and free it.
6124 */
6125 static void
6126dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006127 dict_T *dict;
6128 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006129{
Bram Moolenaar33570922005-01-25 22:26:29 +00006130 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006131
Bram Moolenaar33570922005-01-25 22:26:29 +00006132 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006133 if (HASHITEM_EMPTY(hi))
6134 EMSG2(_(e_intern2), "dictitem_remove()");
6135 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006136 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006137 dictitem_free(item);
6138}
6139
6140/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006141 * Free a dict item. Also clears the value.
6142 */
6143 static void
6144dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006145 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006146{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006147 clear_tv(&item->di_tv);
6148 vim_free(item);
6149}
6150
6151/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006152 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6153 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006154 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006155 * Returns NULL when out of memory.
6156 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006157 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006158dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006159 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006160 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006161 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006162{
Bram Moolenaar33570922005-01-25 22:26:29 +00006163 dict_T *copy;
6164 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006165 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006166 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006167
6168 if (orig == NULL)
6169 return NULL;
6170
6171 copy = dict_alloc();
6172 if (copy != NULL)
6173 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006174 if (copyID != 0)
6175 {
6176 orig->dv_copyID = copyID;
6177 orig->dv_copydict = copy;
6178 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006179 todo = orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006180 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006181 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006182 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006183 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006184 --todo;
6185
6186 di = dictitem_alloc(hi->hi_key);
6187 if (di == NULL)
6188 break;
6189 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006190 {
6191 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6192 copyID) == FAIL)
6193 {
6194 vim_free(di);
6195 break;
6196 }
6197 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006198 else
6199 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6200 if (dict_add(copy, di) == FAIL)
6201 {
6202 dictitem_free(di);
6203 break;
6204 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006205 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006206 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006207
Bram Moolenaare9a41262005-01-15 22:18:47 +00006208 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006209 if (todo > 0)
6210 {
6211 dict_unref(copy);
6212 copy = NULL;
6213 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006214 }
6215
6216 return copy;
6217}
6218
6219/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006220 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006221 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006222 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006223 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006224dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006225 dict_T *d;
6226 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006227{
Bram Moolenaar33570922005-01-25 22:26:29 +00006228 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006229}
6230
Bram Moolenaar8c711452005-01-14 21:53:12 +00006231/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006232 * Add a number or string entry to dictionary "d".
6233 * When "str" is NULL use number "nr", otherwise use "str".
6234 * Returns FAIL when out of memory and when key already exists.
6235 */
6236 int
6237dict_add_nr_str(d, key, nr, str)
6238 dict_T *d;
6239 char *key;
6240 long nr;
6241 char_u *str;
6242{
6243 dictitem_T *item;
6244
6245 item = dictitem_alloc((char_u *)key);
6246 if (item == NULL)
6247 return FAIL;
6248 item->di_tv.v_lock = 0;
6249 if (str == NULL)
6250 {
6251 item->di_tv.v_type = VAR_NUMBER;
6252 item->di_tv.vval.v_number = nr;
6253 }
6254 else
6255 {
6256 item->di_tv.v_type = VAR_STRING;
6257 item->di_tv.vval.v_string = vim_strsave(str);
6258 }
6259 if (dict_add(d, item) == FAIL)
6260 {
6261 dictitem_free(item);
6262 return FAIL;
6263 }
6264 return OK;
6265}
6266
6267/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006268 * Get the number of items in a Dictionary.
6269 */
6270 static long
6271dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006272 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006273{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006274 if (d == NULL)
6275 return 0L;
Bram Moolenaar33570922005-01-25 22:26:29 +00006276 return d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006277}
6278
6279/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006280 * Find item "key[len]" in Dictionary "d".
6281 * If "len" is negative use strlen(key).
6282 * Returns NULL when not found.
6283 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006284 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006285dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006286 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006287 char_u *key;
6288 int len;
6289{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006290#define AKEYLEN 200
6291 char_u buf[AKEYLEN];
6292 char_u *akey;
6293 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006294 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006295
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006296 if (len < 0)
6297 akey = key;
6298 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006299 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006300 tofree = akey = vim_strnsave(key, len);
6301 if (akey == NULL)
6302 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006303 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006304 else
6305 {
6306 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006307 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006308 akey = buf;
6309 }
6310
Bram Moolenaar33570922005-01-25 22:26:29 +00006311 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006312 vim_free(tofree);
6313 if (HASHITEM_EMPTY(hi))
6314 return NULL;
6315 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006316}
6317
6318/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006319 * Get a string item from a dictionary in allocated memory.
6320 * Returns NULL if the entry doesn't exist or out of memory.
6321 */
6322 char_u *
6323get_dict_string(d, key)
6324 dict_T *d;
6325 char_u *key;
6326{
6327 dictitem_T *di;
6328
6329 di = dict_find(d, key, -1);
6330 if (di == NULL)
6331 return NULL;
6332 return vim_strsave(get_tv_string(&di->di_tv));
6333}
6334
6335/*
6336 * Get a number item from a dictionary.
6337 * Returns 0 if the entry doesn't exist or out of memory.
6338 */
6339 long
6340get_dict_number(d, key)
6341 dict_T *d;
6342 char_u *key;
6343{
6344 dictitem_T *di;
6345
6346 di = dict_find(d, key, -1);
6347 if (di == NULL)
6348 return 0;
6349 return get_tv_number(&di->di_tv);
6350}
6351
6352/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006353 * Return an allocated string with the string representation of a Dictionary.
6354 * May return NULL.
6355 */
6356 static char_u *
6357dict2string(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006358 typval_T *tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006359{
6360 garray_T ga;
6361 int first = TRUE;
6362 char_u *tofree;
6363 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006364 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006365 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006366 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006367 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006368
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006369 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006370 return NULL;
6371 ga_init2(&ga, (int)sizeof(char), 80);
6372 ga_append(&ga, '{');
6373
Bram Moolenaar33570922005-01-25 22:26:29 +00006374 todo = d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006375 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006376 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006377 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006378 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006379 --todo;
6380
6381 if (first)
6382 first = FALSE;
6383 else
6384 ga_concat(&ga, (char_u *)", ");
6385
6386 tofree = string_quote(hi->hi_key, FALSE);
6387 if (tofree != NULL)
6388 {
6389 ga_concat(&ga, tofree);
6390 vim_free(tofree);
6391 }
6392 ga_concat(&ga, (char_u *)": ");
6393 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf);
6394 if (s != NULL)
6395 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006396 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006397 if (s == NULL)
6398 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006399 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006400 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006401 if (todo > 0)
6402 {
6403 vim_free(ga.ga_data);
6404 return NULL;
6405 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006406
6407 ga_append(&ga, '}');
6408 ga_append(&ga, NUL);
6409 return (char_u *)ga.ga_data;
6410}
6411
6412/*
6413 * Allocate a variable for a Dictionary and fill it from "*arg".
6414 * Return OK or FAIL. Returns NOTDONE for {expr}.
6415 */
6416 static int
6417get_dict_tv(arg, rettv, evaluate)
6418 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006419 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006420 int evaluate;
6421{
Bram Moolenaar33570922005-01-25 22:26:29 +00006422 dict_T *d = NULL;
6423 typval_T tvkey;
6424 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006425 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00006426 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006427 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006428 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00006429
6430 /*
6431 * First check if it's not a curly-braces thing: {expr}.
6432 * Must do this without evaluating, otherwise a function may be called
6433 * twice. Unfortunately this means we need to call eval1() twice for the
6434 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006435 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006436 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006437 if (*start != '}')
6438 {
6439 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
6440 return FAIL;
6441 if (*start == '}')
6442 return NOTDONE;
6443 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006444
6445 if (evaluate)
6446 {
6447 d = dict_alloc();
6448 if (d == NULL)
6449 return FAIL;
6450 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006451 tvkey.v_type = VAR_UNKNOWN;
6452 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006453
6454 *arg = skipwhite(*arg + 1);
6455 while (**arg != '}' && **arg != NUL)
6456 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006457 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006458 goto failret;
6459 if (**arg != ':')
6460 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006461 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006462 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006463 goto failret;
6464 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006465 key = get_tv_string_buf_chk(&tvkey, buf);
6466 if (key == NULL || *key == NUL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006467 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006468 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6469 if (key != NULL)
6470 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006471 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006472 goto failret;
6473 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006474
6475 *arg = skipwhite(*arg + 1);
6476 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6477 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006478 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006479 goto failret;
6480 }
6481 if (evaluate)
6482 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006483 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006484 if (item != NULL)
6485 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00006486 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006487 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006488 clear_tv(&tv);
6489 goto failret;
6490 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006491 item = dictitem_alloc(key);
6492 clear_tv(&tvkey);
6493 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006494 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006495 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006496 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006497 if (dict_add(d, item) == FAIL)
6498 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006499 }
6500 }
6501
6502 if (**arg == '}')
6503 break;
6504 if (**arg != ',')
6505 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006506 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006507 goto failret;
6508 }
6509 *arg = skipwhite(*arg + 1);
6510 }
6511
6512 if (**arg != '}')
6513 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006514 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006515failret:
6516 if (evaluate)
6517 dict_free(d);
6518 return FAIL;
6519 }
6520
6521 *arg = skipwhite(*arg + 1);
6522 if (evaluate)
6523 {
6524 rettv->v_type = VAR_DICT;
6525 rettv->vval.v_dict = d;
6526 ++d->dv_refcount;
6527 }
6528
6529 return OK;
6530}
6531
Bram Moolenaar8c711452005-01-14 21:53:12 +00006532/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006533 * Return a string with the string representation of a variable.
6534 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006535 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006536 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006537 * May return NULL;
6538 */
6539 static char_u *
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006540echo_string(tv, tofree, numbuf)
Bram Moolenaar33570922005-01-25 22:26:29 +00006541 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006542 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006543 char_u *numbuf;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006544{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006545 static int recurse = 0;
6546 char_u *r = NULL;
6547
Bram Moolenaar33570922005-01-25 22:26:29 +00006548 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006549 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006550 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006551 *tofree = NULL;
6552 return NULL;
6553 }
6554 ++recurse;
6555
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006556 switch (tv->v_type)
6557 {
6558 case VAR_FUNC:
6559 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006560 r = tv->vval.v_string;
6561 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006562 case VAR_LIST:
6563 *tofree = list2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006564 r = *tofree;
6565 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006566 case VAR_DICT:
6567 *tofree = dict2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006568 r = *tofree;
6569 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006570 case VAR_STRING:
6571 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006572 *tofree = NULL;
6573 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006574 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006575 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006576 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006577 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006578 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006579
6580 --recurse;
6581 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006582}
6583
6584/*
6585 * Return a string with the string representation of a variable.
6586 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6587 * "numbuf" is used for a number.
6588 * Puts quotes around strings, so that they can be parsed back by eval().
6589 * May return NULL;
6590 */
6591 static char_u *
6592tv2string(tv, tofree, numbuf)
Bram Moolenaar33570922005-01-25 22:26:29 +00006593 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006594 char_u **tofree;
6595 char_u *numbuf;
6596{
6597 switch (tv->v_type)
6598 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006599 case VAR_FUNC:
6600 *tofree = string_quote(tv->vval.v_string, TRUE);
6601 return *tofree;
6602 case VAR_STRING:
6603 *tofree = string_quote(tv->vval.v_string, FALSE);
6604 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006605 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006606 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006607 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006608 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006609 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006610 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006611 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006612 return echo_string(tv, tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006613}
6614
6615/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006616 * Return string "str" in ' quotes, doubling ' characters.
6617 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006618 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006619 */
6620 static char_u *
6621string_quote(str, function)
6622 char_u *str;
6623 int function;
6624{
Bram Moolenaar33570922005-01-25 22:26:29 +00006625 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006626 char_u *p, *r, *s;
6627
Bram Moolenaar33570922005-01-25 22:26:29 +00006628 len = (function ? 13 : 3);
6629 if (str != NULL)
6630 {
6631 len += STRLEN(str);
6632 for (p = str; *p != NUL; mb_ptr_adv(p))
6633 if (*p == '\'')
6634 ++len;
6635 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006636 s = r = alloc(len);
6637 if (r != NULL)
6638 {
6639 if (function)
6640 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006641 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006642 r += 10;
6643 }
6644 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006645 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006646 if (str != NULL)
6647 for (p = str; *p != NUL; )
6648 {
6649 if (*p == '\'')
6650 *r++ = '\'';
6651 MB_COPY_CHAR(p, r);
6652 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006653 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006654 if (function)
6655 *r++ = ')';
6656 *r++ = NUL;
6657 }
6658 return s;
6659}
6660
6661/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662 * Get the value of an environment variable.
6663 * "arg" is pointing to the '$'. It is advanced to after the name.
6664 * If the environment variable was not set, silently assume it is empty.
6665 * Always return OK.
6666 */
6667 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006668get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006670 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671 int evaluate;
6672{
6673 char_u *string = NULL;
6674 int len;
6675 int cc;
6676 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006677 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678
6679 ++*arg;
6680 name = *arg;
6681 len = get_env_len(arg);
6682 if (evaluate)
6683 {
6684 if (len != 0)
6685 {
6686 cc = name[len];
6687 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006688 /* first try vim_getenv(), fast for normal environment vars */
6689 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006691 {
6692 if (!mustfree)
6693 string = vim_strsave(string);
6694 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695 else
6696 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00006697 if (mustfree)
6698 vim_free(string);
6699
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 /* next try expanding things like $VIM and ${HOME} */
6701 string = expand_env_save(name - 1);
6702 if (string != NULL && *string == '$')
6703 {
6704 vim_free(string);
6705 string = NULL;
6706 }
6707 }
6708 name[len] = cc;
6709 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006710 rettv->v_type = VAR_STRING;
6711 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712 }
6713
6714 return OK;
6715}
6716
6717/*
6718 * Array with names and number of arguments of all internal functions
6719 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
6720 */
6721static struct fst
6722{
6723 char *f_name; /* function name */
6724 char f_min_argc; /* minimal number of arguments */
6725 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00006726 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006727 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728} functions[] =
6729{
Bram Moolenaar0d660222005-01-07 21:51:51 +00006730 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731 {"append", 2, 2, f_append},
6732 {"argc", 0, 0, f_argc},
6733 {"argidx", 0, 0, f_argidx},
6734 {"argv", 1, 1, f_argv},
6735 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006736 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 {"bufexists", 1, 1, f_bufexists},
6738 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
6739 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
6740 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
6741 {"buflisted", 1, 1, f_buflisted},
6742 {"bufloaded", 1, 1, f_bufloaded},
6743 {"bufname", 1, 1, f_bufname},
6744 {"bufnr", 1, 1, f_bufnr},
6745 {"bufwinnr", 1, 1, f_bufwinnr},
6746 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006747 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006748 {"call", 2, 3, f_call},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749 {"char2nr", 1, 1, f_char2nr},
6750 {"cindent", 1, 1, f_cindent},
6751 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00006752#if defined(FEAT_INS_EXPAND)
6753 {"complete_add", 1, 1, f_complete_add},
6754 {"complete_check", 0, 0, f_complete_check},
6755#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006757 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006758 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759 {"cscope_connection",0,3, f_cscope_connection},
6760 {"cursor", 2, 2, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006761 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 {"delete", 1, 1, f_delete},
6763 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00006764 {"diff_filler", 1, 1, f_diff_filler},
6765 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006766 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006768 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769 {"eventhandler", 0, 0, f_eventhandler},
6770 {"executable", 1, 1, f_executable},
6771 {"exists", 1, 1, f_exists},
6772 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006773 {"extend", 2, 3, f_extend},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
6775 {"filereadable", 1, 1, f_filereadable},
6776 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006777 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006778 {"finddir", 1, 3, f_finddir},
6779 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780 {"fnamemodify", 2, 2, f_fnamemodify},
6781 {"foldclosed", 1, 1, f_foldclosed},
6782 {"foldclosedend", 1, 1, f_foldclosedend},
6783 {"foldlevel", 1, 1, f_foldlevel},
6784 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006785 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006787 {"function", 1, 1, f_function},
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006788 {"garbagecollect", 0, 0, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006789 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00006790 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 {"getbufvar", 2, 2, f_getbufvar},
6792 {"getchar", 0, 1, f_getchar},
6793 {"getcharmod", 0, 0, f_getcharmod},
6794 {"getcmdline", 0, 0, f_getcmdline},
6795 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006796 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006798 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006799 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 {"getfsize", 1, 1, f_getfsize},
6801 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006802 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006803 {"getline", 1, 2, f_getline},
Bram Moolenaar2641f772005-03-25 21:58:17 +00006804 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006805 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 {"getregtype", 0, 1, f_getregtype},
6807 {"getwinposx", 0, 0, f_getwinposx},
6808 {"getwinposy", 0, 0, f_getwinposy},
6809 {"getwinvar", 2, 2, f_getwinvar},
6810 {"glob", 1, 1, f_glob},
6811 {"globpath", 2, 2, f_globpath},
6812 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006813 {"has_key", 2, 2, f_has_key},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814 {"hasmapto", 1, 2, f_hasmapto},
6815 {"highlightID", 1, 1, f_hlID}, /* obsolete */
6816 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
6817 {"histadd", 2, 2, f_histadd},
6818 {"histdel", 1, 2, f_histdel},
6819 {"histget", 1, 2, f_histget},
6820 {"histnr", 1, 1, f_histnr},
6821 {"hlID", 1, 1, f_hlID},
6822 {"hlexists", 1, 1, f_hlexists},
6823 {"hostname", 0, 0, f_hostname},
6824 {"iconv", 3, 3, f_iconv},
6825 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006826 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006827 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00006829 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 {"inputrestore", 0, 0, f_inputrestore},
6831 {"inputsave", 0, 0, f_inputsave},
6832 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006833 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006835 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006836 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006837 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006838 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006840 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841 {"libcall", 3, 3, f_libcall},
6842 {"libcallnr", 3, 3, f_libcallnr},
6843 {"line", 1, 1, f_line},
6844 {"line2byte", 1, 1, f_line2byte},
6845 {"lispindent", 1, 1, f_lispindent},
6846 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006847 {"map", 2, 2, f_map},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848 {"maparg", 1, 2, f_maparg},
6849 {"mapcheck", 1, 2, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006850 {"match", 2, 4, f_match},
6851 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006852 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006853 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006854 {"max", 1, 1, f_max},
6855 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006856#ifdef vim_mkdir
6857 {"mkdir", 1, 3, f_mkdir},
6858#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859 {"mode", 0, 0, f_mode},
6860 {"nextnonblank", 1, 1, f_nextnonblank},
6861 {"nr2char", 1, 1, f_nr2char},
6862 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006863 {"printf", 2, 19, f_printf},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006864 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006865 {"readfile", 1, 3, f_readfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866 {"remote_expr", 2, 3, f_remote_expr},
6867 {"remote_foreground", 1, 1, f_remote_foreground},
6868 {"remote_peek", 1, 2, f_remote_peek},
6869 {"remote_read", 1, 1, f_remote_read},
6870 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006871 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006873 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006875 {"reverse", 1, 1, f_reverse},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876 {"search", 1, 2, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00006877 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878 {"searchpair", 3, 5, f_searchpair},
6879 {"server2client", 2, 2, f_server2client},
6880 {"serverlist", 0, 0, f_serverlist},
6881 {"setbufvar", 3, 3, f_setbufvar},
6882 {"setcmdpos", 1, 1, f_setcmdpos},
6883 {"setline", 2, 2, f_setline},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00006884 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 {"setreg", 2, 3, f_setreg},
6886 {"setwinvar", 3, 3, f_setwinvar},
6887 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006888 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006889 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00006890 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00006891 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006892 {"split", 1, 3, f_split},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893#ifdef HAVE_STRFTIME
6894 {"strftime", 1, 2, f_strftime},
6895#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00006896 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006897 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898 {"strlen", 1, 1, f_strlen},
6899 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00006900 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 {"strtrans", 1, 1, f_strtrans},
6902 {"submatch", 1, 1, f_submatch},
6903 {"substitute", 4, 4, f_substitute},
6904 {"synID", 3, 3, f_synID},
6905 {"synIDattr", 2, 3, f_synIDattr},
6906 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006907 {"system", 1, 2, f_system},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006908 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006909 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00006911 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912 {"tolower", 1, 1, f_tolower},
6913 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00006914 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006916 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 {"virtcol", 1, 1, f_virtcol},
6918 {"visualmode", 0, 1, f_visualmode},
6919 {"winbufnr", 1, 1, f_winbufnr},
6920 {"wincol", 0, 0, f_wincol},
6921 {"winheight", 1, 1, f_winheight},
6922 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006923 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924 {"winrestcmd", 0, 0, f_winrestcmd},
6925 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006926 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927};
6928
6929#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6930
6931/*
6932 * Function given to ExpandGeneric() to obtain the list of internal
6933 * or user defined function names.
6934 */
6935 char_u *
6936get_function_name(xp, idx)
6937 expand_T *xp;
6938 int idx;
6939{
6940 static int intidx = -1;
6941 char_u *name;
6942
6943 if (idx == 0)
6944 intidx = -1;
6945 if (intidx < 0)
6946 {
6947 name = get_user_func_name(xp, idx);
6948 if (name != NULL)
6949 return name;
6950 }
6951 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
6952 {
6953 STRCPY(IObuff, functions[intidx].f_name);
6954 STRCAT(IObuff, "(");
6955 if (functions[intidx].f_max_argc == 0)
6956 STRCAT(IObuff, ")");
6957 return IObuff;
6958 }
6959
6960 return NULL;
6961}
6962
6963/*
6964 * Function given to ExpandGeneric() to obtain the list of internal or
6965 * user defined variable or function names.
6966 */
6967/*ARGSUSED*/
6968 char_u *
6969get_expr_name(xp, idx)
6970 expand_T *xp;
6971 int idx;
6972{
6973 static int intidx = -1;
6974 char_u *name;
6975
6976 if (idx == 0)
6977 intidx = -1;
6978 if (intidx < 0)
6979 {
6980 name = get_function_name(xp, idx);
6981 if (name != NULL)
6982 return name;
6983 }
6984 return get_user_var_name(xp, ++intidx);
6985}
6986
6987#endif /* FEAT_CMDL_COMPL */
6988
6989/*
6990 * Find internal function in table above.
6991 * Return index, or -1 if not found
6992 */
6993 static int
6994find_internal_func(name)
6995 char_u *name; /* name of the function */
6996{
6997 int first = 0;
6998 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
6999 int cmp;
7000 int x;
7001
7002 /*
7003 * Find the function name in the table. Binary search.
7004 */
7005 while (first <= last)
7006 {
7007 x = first + ((unsigned)(last - first) >> 1);
7008 cmp = STRCMP(name, functions[x].f_name);
7009 if (cmp < 0)
7010 last = x - 1;
7011 else if (cmp > 0)
7012 first = x + 1;
7013 else
7014 return x;
7015 }
7016 return -1;
7017}
7018
7019/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007020 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7021 * name it contains, otherwise return "name".
7022 */
7023 static char_u *
7024deref_func_name(name, lenp)
7025 char_u *name;
7026 int *lenp;
7027{
Bram Moolenaar33570922005-01-25 22:26:29 +00007028 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007029 int cc;
7030
7031 cc = name[*lenp];
7032 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007033 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007034 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007035 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007036 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007037 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007038 {
7039 *lenp = 0;
7040 return (char_u *)""; /* just in case */
7041 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007042 *lenp = STRLEN(v->di_tv.vval.v_string);
7043 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007044 }
7045
7046 return name;
7047}
7048
7049/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 * Allocate a variable for the result of a function.
7051 * Return OK or FAIL.
7052 */
7053 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007054get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7055 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 char_u *name; /* name of the function */
7057 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007058 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 char_u **arg; /* argument, pointing to the '(' */
7060 linenr_T firstline; /* first line of range */
7061 linenr_T lastline; /* last line of range */
7062 int *doesrange; /* return: function handled range */
7063 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007064 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065{
7066 char_u *argp;
7067 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00007068 typval_T argvars[MAX_FUNC_ARGS]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069 int argcount = 0; /* number of arguments found */
7070
7071 /*
7072 * Get the arguments.
7073 */
7074 argp = *arg;
7075 while (argcount < MAX_FUNC_ARGS)
7076 {
7077 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7078 if (*argp == ')' || *argp == ',' || *argp == NUL)
7079 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7081 {
7082 ret = FAIL;
7083 break;
7084 }
7085 ++argcount;
7086 if (*argp != ',')
7087 break;
7088 }
7089 if (*argp == ')')
7090 ++argp;
7091 else
7092 ret = FAIL;
7093
7094 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007095 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007096 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007098 {
7099 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007100 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007101 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007102 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104
7105 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007106 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107
7108 *arg = skipwhite(argp);
7109 return ret;
7110}
7111
7112
7113/*
7114 * Call a function with its resolved parameters
7115 * Return OK or FAIL.
7116 */
7117 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007118call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007119 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 char_u *name; /* name of the function */
7121 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007122 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123 int argcount; /* number of "argvars" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007124 typval_T *argvars; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125 linenr_T firstline; /* first line of range */
7126 linenr_T lastline; /* last line of range */
7127 int *doesrange; /* return: function handled range */
7128 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007129 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130{
7131 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132#define ERROR_UNKNOWN 0
7133#define ERROR_TOOMANY 1
7134#define ERROR_TOOFEW 2
7135#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007136#define ERROR_DICT 4
7137#define ERROR_NONE 5
7138#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 int error = ERROR_NONE;
7140 int i;
7141 int llen;
7142 ufunc_T *fp;
7143 int cc;
7144#define FLEN_FIXED 40
7145 char_u fname_buf[FLEN_FIXED + 1];
7146 char_u *fname;
7147
7148 /*
7149 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7150 * Change <SNR>123_name() to K_SNR 123_name().
7151 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7152 */
7153 cc = name[len];
7154 name[len] = NUL;
7155 llen = eval_fname_script(name);
7156 if (llen > 0)
7157 {
7158 fname_buf[0] = K_SPECIAL;
7159 fname_buf[1] = KS_EXTRA;
7160 fname_buf[2] = (int)KE_SNR;
7161 i = 3;
7162 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7163 {
7164 if (current_SID <= 0)
7165 error = ERROR_SCRIPT;
7166 else
7167 {
7168 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7169 i = (int)STRLEN(fname_buf);
7170 }
7171 }
7172 if (i + STRLEN(name + llen) < FLEN_FIXED)
7173 {
7174 STRCPY(fname_buf + i, name + llen);
7175 fname = fname_buf;
7176 }
7177 else
7178 {
7179 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7180 if (fname == NULL)
7181 error = ERROR_OTHER;
7182 else
7183 {
7184 mch_memmove(fname, fname_buf, (size_t)i);
7185 STRCPY(fname + i, name + llen);
7186 }
7187 }
7188 }
7189 else
7190 fname = name;
7191
7192 *doesrange = FALSE;
7193
7194
7195 /* execute the function if no errors detected and executing */
7196 if (evaluate && error == ERROR_NONE)
7197 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007198 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 error = ERROR_UNKNOWN;
7200
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007201 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 {
7203 /*
7204 * User defined function.
7205 */
7206 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007207
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007209 /* Trigger FuncUndefined event, may load the function. */
7210 if (fp == NULL
7211 && apply_autocmds(EVENT_FUNCUNDEFINED,
7212 fname, fname, TRUE, NULL)
7213 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007215 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 fp = find_func(fname);
7217 }
7218#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007219 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007220 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007221 {
7222 /* loaded a package, search for the function again */
7223 fp = find_func(fname);
7224 }
7225
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 if (fp != NULL)
7227 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007228 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007230 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007232 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007234 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007235 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236 else
7237 {
7238 /*
7239 * Call the user function.
7240 * Save and restore search patterns, script variables and
7241 * redo buffer.
7242 */
7243 save_search_patterns();
7244 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007245 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007246 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007247 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007248 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7249 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7250 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007251 /* Function was unreferenced while being used, free it
7252 * now. */
7253 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 restoreRedobuff();
7255 restore_search_patterns();
7256 error = ERROR_NONE;
7257 }
7258 }
7259 }
7260 else
7261 {
7262 /*
7263 * Find the function name in the table, call its implementation.
7264 */
7265 i = find_internal_func(fname);
7266 if (i >= 0)
7267 {
7268 if (argcount < functions[i].f_min_argc)
7269 error = ERROR_TOOFEW;
7270 else if (argcount > functions[i].f_max_argc)
7271 error = ERROR_TOOMANY;
7272 else
7273 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007274 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007275 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276 error = ERROR_NONE;
7277 }
7278 }
7279 }
7280 /*
7281 * The function call (or "FuncUndefined" autocommand sequence) might
7282 * have been aborted by an error, an interrupt, or an explicitly thrown
7283 * exception that has not been caught so far. This situation can be
7284 * tested for by calling aborting(). For an error in an internal
7285 * function or for the "E132" error in call_user_func(), however, the
7286 * throw point at which the "force_abort" flag (temporarily reset by
7287 * emsg()) is normally updated has not been reached yet. We need to
7288 * update that flag first to make aborting() reliable.
7289 */
7290 update_force_abort();
7291 }
7292 if (error == ERROR_NONE)
7293 ret = OK;
7294
7295 /*
7296 * Report an error unless the argument evaluation or function call has been
7297 * cancelled due to an aborting error, an interrupt, or an exception.
7298 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007299 if (!aborting())
7300 {
7301 switch (error)
7302 {
7303 case ERROR_UNKNOWN:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007304 emsg_funcname("E117: Unknown function: %s", name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007305 break;
7306 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007307 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007308 break;
7309 case ERROR_TOOFEW:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007310 emsg_funcname("E119: Not enough arguments for function: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007311 name);
7312 break;
7313 case ERROR_SCRIPT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007314 emsg_funcname("E120: Using <SID> not in a script context: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007315 name);
7316 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007317 case ERROR_DICT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007318 emsg_funcname("E725: Calling dict function without Dictionary: %s",
Bram Moolenaare9a41262005-01-15 22:18:47 +00007319 name);
7320 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007321 }
7322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323
7324 name[len] = cc;
7325 if (fname != name && fname != fname_buf)
7326 vim_free(fname);
7327
7328 return ret;
7329}
7330
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007331/*
7332 * Give an error message with a function name. Handle <SNR> things.
7333 */
7334 static void
7335emsg_funcname(msg, name)
7336 char *msg;
7337 char_u *name;
7338{
7339 char_u *p;
7340
7341 if (*name == K_SPECIAL)
7342 p = concat_str((char_u *)"<SNR>", name + 3);
7343 else
7344 p = name;
7345 EMSG2(_(msg), p);
7346 if (p != name)
7347 vim_free(p);
7348}
7349
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350/*********************************************
7351 * Implementation of the built-in functions
7352 */
7353
7354/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007355 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356 */
7357 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007358f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007359 typval_T *argvars;
7360 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361{
Bram Moolenaar33570922005-01-25 22:26:29 +00007362 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007364 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007365 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007367 if ((l = argvars[0].vval.v_list) != NULL
7368 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7369 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007370 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007371 }
7372 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007373 EMSG(_(e_listreq));
7374}
7375
7376/*
7377 * "append(lnum, string/list)" function
7378 */
7379 static void
7380f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007381 typval_T *argvars;
7382 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007383{
7384 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007385 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007386 list_T *l = NULL;
7387 listitem_T *li = NULL;
7388 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007389 long added = 0;
7390
Bram Moolenaar0d660222005-01-07 21:51:51 +00007391 lnum = get_tv_lnum(argvars);
7392 if (lnum >= 0
7393 && lnum <= curbuf->b_ml.ml_line_count
7394 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007395 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007396 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007397 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007398 l = argvars[1].vval.v_list;
7399 if (l == NULL)
7400 return;
7401 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007402 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007403 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007404 for (;;)
7405 {
7406 if (l == NULL)
7407 tv = &argvars[1]; /* append a string */
7408 else if (li == NULL)
7409 break; /* end of list */
7410 else
7411 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007412 line = get_tv_string_chk(tv);
7413 if (line == NULL) /* type error */
7414 {
7415 rettv->vval.v_number = 1; /* Failed */
7416 break;
7417 }
7418 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007419 ++added;
7420 if (l == NULL)
7421 break;
7422 li = li->li_next;
7423 }
7424
7425 appended_lines_mark(lnum, added);
7426 if (curwin->w_cursor.lnum > lnum)
7427 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007429 else
7430 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431}
7432
7433/*
7434 * "argc()" function
7435 */
7436/* ARGSUSED */
7437 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007438f_argc(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{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007442 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443}
7444
7445/*
7446 * "argidx()" function
7447 */
7448/* ARGSUSED */
7449 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007450f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007451 typval_T *argvars;
7452 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007454 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455}
7456
7457/*
7458 * "argv(nr)" function
7459 */
7460 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007461f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007462 typval_T *argvars;
7463 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464{
7465 int idx;
7466
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007467 idx = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468 if (idx >= 0 && idx < ARGCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007469 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007471 rettv->vval.v_string = NULL;
7472 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473}
7474
7475/*
7476 * "browse(save, title, initdir, default)" function
7477 */
7478/* ARGSUSED */
7479 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007480f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007481 typval_T *argvars;
7482 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483{
7484#ifdef FEAT_BROWSE
7485 int save;
7486 char_u *title;
7487 char_u *initdir;
7488 char_u *defname;
7489 char_u buf[NUMBUFLEN];
7490 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007491 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007493 save = get_tv_number_chk(&argvars[0], &error);
7494 title = get_tv_string_chk(&argvars[1]);
7495 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7496 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007498 if (error || title == NULL || initdir == NULL || defname == NULL)
7499 rettv->vval.v_string = NULL;
7500 else
7501 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007502 do_browse(save ? BROWSE_SAVE : 0,
7503 title, defname, NULL, initdir, NULL, curbuf);
7504#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007505 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007506#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007507 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007508}
7509
7510/*
7511 * "browsedir(title, initdir)" function
7512 */
7513/* ARGSUSED */
7514 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007515f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007516 typval_T *argvars;
7517 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007518{
7519#ifdef FEAT_BROWSE
7520 char_u *title;
7521 char_u *initdir;
7522 char_u buf[NUMBUFLEN];
7523
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007524 title = get_tv_string_chk(&argvars[0]);
7525 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007526
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007527 if (title == NULL || initdir == NULL)
7528 rettv->vval.v_string = NULL;
7529 else
7530 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007531 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007533 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007535 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536}
7537
Bram Moolenaar33570922005-01-25 22:26:29 +00007538static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007539
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540/*
7541 * Find a buffer by number or exact name.
7542 */
7543 static buf_T *
7544find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007545 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546{
7547 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007549 if (avar->v_type == VAR_NUMBER)
7550 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007551 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007553 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007554 if (buf == NULL)
7555 {
7556 /* No full path name match, try a match with a URL or a "nofile"
7557 * buffer, these don't use the full path. */
7558 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7559 if (buf->b_fname != NULL
7560 && (path_with_url(buf->b_fname)
7561#ifdef FEAT_QUICKFIX
7562 || bt_nofile(buf)
7563#endif
7564 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007565 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007566 break;
7567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568 }
7569 return buf;
7570}
7571
7572/*
7573 * "bufexists(expr)" function
7574 */
7575 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007576f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007577 typval_T *argvars;
7578 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007580 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581}
7582
7583/*
7584 * "buflisted(expr)" function
7585 */
7586 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007587f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007588 typval_T *argvars;
7589 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590{
7591 buf_T *buf;
7592
7593 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007594 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595}
7596
7597/*
7598 * "bufloaded(expr)" function
7599 */
7600 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007601f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007602 typval_T *argvars;
7603 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604{
7605 buf_T *buf;
7606
7607 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007608 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609}
7610
Bram Moolenaar33570922005-01-25 22:26:29 +00007611static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007612
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613/*
7614 * Get buffer by number or pattern.
7615 */
7616 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007617get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007618 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007620 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621 int save_magic;
7622 char_u *save_cpo;
7623 buf_T *buf;
7624
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007625 if (tv->v_type == VAR_NUMBER)
7626 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007627 if (tv->v_type != VAR_STRING)
7628 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629 if (name == NULL || *name == NUL)
7630 return curbuf;
7631 if (name[0] == '$' && name[1] == NUL)
7632 return lastbuf;
7633
7634 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7635 save_magic = p_magic;
7636 p_magic = TRUE;
7637 save_cpo = p_cpo;
7638 p_cpo = (char_u *)"";
7639
7640 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
7641 TRUE, FALSE));
7642
7643 p_magic = save_magic;
7644 p_cpo = save_cpo;
7645
7646 /* If not found, try expanding the name, like done for bufexists(). */
7647 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007648 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649
7650 return buf;
7651}
7652
7653/*
7654 * "bufname(expr)" function
7655 */
7656 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007657f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007658 typval_T *argvars;
7659 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660{
7661 buf_T *buf;
7662
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007663 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007665 buf = get_buf_tv(&argvars[0]);
7666 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007668 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007670 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671 --emsg_off;
7672}
7673
7674/*
7675 * "bufnr(expr)" function
7676 */
7677 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007678f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007679 typval_T *argvars;
7680 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681{
7682 buf_T *buf;
7683
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007684 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007686 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007688 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007690 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691 --emsg_off;
7692}
7693
7694/*
7695 * "bufwinnr(nr)" function
7696 */
7697 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007698f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007699 typval_T *argvars;
7700 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701{
7702#ifdef FEAT_WINDOWS
7703 win_T *wp;
7704 int winnr = 0;
7705#endif
7706 buf_T *buf;
7707
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007708 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007710 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711#ifdef FEAT_WINDOWS
7712 for (wp = firstwin; wp; wp = wp->w_next)
7713 {
7714 ++winnr;
7715 if (wp->w_buffer == buf)
7716 break;
7717 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007718 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007720 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721#endif
7722 --emsg_off;
7723}
7724
7725/*
7726 * "byte2line(byte)" function
7727 */
7728/*ARGSUSED*/
7729 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007730f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007731 typval_T *argvars;
7732 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733{
7734#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007735 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736#else
7737 long boff = 0;
7738
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007739 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007741 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007743 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744 (linenr_T)0, &boff);
7745#endif
7746}
7747
7748/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007749 * "byteidx()" function
7750 */
7751/*ARGSUSED*/
7752 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007753f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007754 typval_T *argvars;
7755 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007756{
7757#ifdef FEAT_MBYTE
7758 char_u *t;
7759#endif
7760 char_u *str;
7761 long idx;
7762
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007763 str = get_tv_string_chk(&argvars[0]);
7764 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007765 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007766 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007767 return;
7768
7769#ifdef FEAT_MBYTE
7770 t = str;
7771 for ( ; idx > 0; idx--)
7772 {
7773 if (*t == NUL) /* EOL reached */
7774 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007775 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007776 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007777 rettv->vval.v_number = t - str;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007778#else
7779 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007780 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007781#endif
7782}
7783
7784/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007785 * "call(func, arglist)" function
7786 */
7787 static void
7788f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007789 typval_T *argvars;
7790 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007791{
7792 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00007793 typval_T argv[MAX_FUNC_ARGS];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007794 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007795 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007796 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00007797 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007798
7799 rettv->vval.v_number = 0;
7800 if (argvars[1].v_type != VAR_LIST)
7801 {
7802 EMSG(_(e_listreq));
7803 return;
7804 }
7805 if (argvars[1].vval.v_list == NULL)
7806 return;
7807
7808 if (argvars[0].v_type == VAR_FUNC)
7809 func = argvars[0].vval.v_string;
7810 else
7811 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007812 if (*func == NUL)
7813 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007814
Bram Moolenaare9a41262005-01-15 22:18:47 +00007815 if (argvars[2].v_type != VAR_UNKNOWN)
7816 {
7817 if (argvars[2].v_type != VAR_DICT)
7818 {
7819 EMSG(_(e_dictreq));
7820 return;
7821 }
7822 selfdict = argvars[2].vval.v_dict;
7823 }
7824
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007825 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
7826 item = item->li_next)
7827 {
7828 if (argc == MAX_FUNC_ARGS)
7829 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007830 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007831 break;
7832 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007833 /* Make a copy of each argument. This is needed to be able to set
7834 * v_lock to VAR_FIXED in the copy without changing the original list.
7835 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007836 copy_tv(&item->li_tv, &argv[argc++]);
7837 }
7838
7839 if (item == NULL)
7840 (void)call_func(func, STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007841 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
7842 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007843
7844 /* Free the arguments. */
7845 while (argc > 0)
7846 clear_tv(&argv[--argc]);
7847}
7848
7849/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850 * "char2nr(string)" function
7851 */
7852 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007853f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007854 typval_T *argvars;
7855 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856{
7857#ifdef FEAT_MBYTE
7858 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007859 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860 else
7861#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007862 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863}
7864
7865/*
7866 * "cindent(lnum)" function
7867 */
7868 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007869f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007870 typval_T *argvars;
7871 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872{
7873#ifdef FEAT_CINDENT
7874 pos_T pos;
7875 linenr_T lnum;
7876
7877 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007878 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7880 {
7881 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007882 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883 curwin->w_cursor = pos;
7884 }
7885 else
7886#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007887 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888}
7889
7890/*
7891 * "col(string)" function
7892 */
7893 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007894f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007895 typval_T *argvars;
7896 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897{
7898 colnr_T col = 0;
7899 pos_T *fp;
7900
7901 fp = var2fpos(&argvars[0], FALSE);
7902 if (fp != NULL)
7903 {
7904 if (fp->col == MAXCOL)
7905 {
7906 /* '> can be MAXCOL, get the length of the line then */
7907 if (fp->lnum <= curbuf->b_ml.ml_line_count)
7908 col = STRLEN(ml_get(fp->lnum)) + 1;
7909 else
7910 col = MAXCOL;
7911 }
7912 else
7913 {
7914 col = fp->col + 1;
7915#ifdef FEAT_VIRTUALEDIT
7916 /* col(".") when the cursor is on the NUL at the end of the line
7917 * because of "coladd" can be seen as an extra column. */
7918 if (virtual_active() && fp == &curwin->w_cursor)
7919 {
7920 char_u *p = ml_get_cursor();
7921
7922 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
7923 curwin->w_virtcol - curwin->w_cursor.coladd))
7924 {
7925# ifdef FEAT_MBYTE
7926 int l;
7927
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007928 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929 col += l;
7930# else
7931 if (*p != NUL && p[1] == NUL)
7932 ++col;
7933# endif
7934 }
7935 }
7936#endif
7937 }
7938 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007939 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940}
7941
Bram Moolenaar572cb562005-08-05 21:35:02 +00007942#if defined(FEAT_INS_EXPAND)
7943/*
7944 * "complete_add()" function
7945 */
7946/*ARGSUSED*/
7947 static void
7948f_complete_add(argvars, rettv)
7949 typval_T *argvars;
7950 typval_T *rettv;
7951{
7952 char_u *s;
7953
7954 s = get_tv_string_chk(&argvars[0]);
7955 if (s != NULL)
7956 rettv->vval.v_number = ins_compl_add(s, -1, NULL, FORWARD, 0);
7957}
7958
7959/*
7960 * "complete_check()" function
7961 */
7962/*ARGSUSED*/
7963 static void
7964f_complete_check(argvars, rettv)
7965 typval_T *argvars;
7966 typval_T *rettv;
7967{
7968 int saved = RedrawingDisabled;
7969
7970 RedrawingDisabled = 0;
7971 ins_compl_check_keys(0);
7972 rettv->vval.v_number = compl_interrupted;
7973 RedrawingDisabled = saved;
7974}
7975#endif
7976
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977/*
7978 * "confirm(message, buttons[, default [, type]])" function
7979 */
7980/*ARGSUSED*/
7981 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007982f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007983 typval_T *argvars;
7984 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985{
7986#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
7987 char_u *message;
7988 char_u *buttons = NULL;
7989 char_u buf[NUMBUFLEN];
7990 char_u buf2[NUMBUFLEN];
7991 int def = 1;
7992 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007993 char_u *typestr;
7994 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007996 message = get_tv_string_chk(&argvars[0]);
7997 if (message == NULL)
7998 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007999 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008001 buttons = get_tv_string_buf_chk(&argvars[1], buf);
8002 if (buttons == NULL)
8003 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008004 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008006 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008007 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008009 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
8010 if (typestr == NULL)
8011 error = TRUE;
8012 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008014 switch (TOUPPER_ASC(*typestr))
8015 {
8016 case 'E': type = VIM_ERROR; break;
8017 case 'Q': type = VIM_QUESTION; break;
8018 case 'I': type = VIM_INFO; break;
8019 case 'W': type = VIM_WARNING; break;
8020 case 'G': type = VIM_GENERIC; break;
8021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022 }
8023 }
8024 }
8025 }
8026
8027 if (buttons == NULL || *buttons == NUL)
8028 buttons = (char_u *)_("&Ok");
8029
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008030 if (error)
8031 rettv->vval.v_number = 0;
8032 else
8033 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034 def, NULL);
8035#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008036 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037#endif
8038}
8039
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008040/*
8041 * "copy()" function
8042 */
8043 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008044f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008045 typval_T *argvars;
8046 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008047{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008048 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008049}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050
8051/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008052 * "count()" function
8053 */
8054 static void
8055f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008056 typval_T *argvars;
8057 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008058{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008059 long n = 0;
8060 int ic = FALSE;
8061
Bram Moolenaare9a41262005-01-15 22:18:47 +00008062 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008063 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008064 listitem_T *li;
8065 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008066 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008067
Bram Moolenaare9a41262005-01-15 22:18:47 +00008068 if ((l = argvars[0].vval.v_list) != NULL)
8069 {
8070 li = l->lv_first;
8071 if (argvars[2].v_type != VAR_UNKNOWN)
8072 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008073 int error = FALSE;
8074
8075 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008076 if (argvars[3].v_type != VAR_UNKNOWN)
8077 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008078 idx = get_tv_number_chk(&argvars[3], &error);
8079 if (!error)
8080 {
8081 li = list_find(l, idx);
8082 if (li == NULL)
8083 EMSGN(_(e_listidx), idx);
8084 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008085 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008086 if (error)
8087 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008088 }
8089
8090 for ( ; li != NULL; li = li->li_next)
8091 if (tv_equal(&li->li_tv, &argvars[1], ic))
8092 ++n;
8093 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008094 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008095 else if (argvars[0].v_type == VAR_DICT)
8096 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008097 int todo;
8098 dict_T *d;
8099 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008100
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008101 if ((d = argvars[0].vval.v_dict) != NULL)
8102 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008103 int error = FALSE;
8104
Bram Moolenaare9a41262005-01-15 22:18:47 +00008105 if (argvars[2].v_type != VAR_UNKNOWN)
8106 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008107 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008108 if (argvars[3].v_type != VAR_UNKNOWN)
8109 EMSG(_(e_invarg));
8110 }
8111
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008112 todo = error ? 0 : d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008113 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008114 {
8115 if (!HASHITEM_EMPTY(hi))
8116 {
8117 --todo;
8118 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
8119 ++n;
8120 }
8121 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008122 }
8123 }
8124 else
8125 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008126 rettv->vval.v_number = n;
8127}
8128
8129/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8131 *
8132 * Checks the existence of a cscope connection.
8133 */
8134/*ARGSUSED*/
8135 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008136f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008137 typval_T *argvars;
8138 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139{
8140#ifdef FEAT_CSCOPE
8141 int num = 0;
8142 char_u *dbpath = NULL;
8143 char_u *prepend = NULL;
8144 char_u buf[NUMBUFLEN];
8145
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008146 if (argvars[0].v_type != VAR_UNKNOWN
8147 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008149 num = (int)get_tv_number(&argvars[0]);
8150 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008151 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008152 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153 }
8154
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008155 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008157 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158#endif
8159}
8160
8161/*
8162 * "cursor(lnum, col)" function
8163 *
8164 * Moves the cursor to the specified line and column
8165 */
8166/*ARGSUSED*/
8167 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008168f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008169 typval_T *argvars;
8170 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171{
8172 long line, col;
8173
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008174 line = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008175 col = get_tv_number_chk(&argvars[1], NULL);
8176 if (line < 0 || col < 0)
8177 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178 if (line > 0)
8179 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180 if (col > 0)
8181 curwin->w_cursor.col = col - 1;
8182#ifdef FEAT_VIRTUALEDIT
8183 curwin->w_cursor.coladd = 0;
8184#endif
8185
8186 /* Make sure the cursor is in a valid position. */
8187 check_cursor();
8188#ifdef FEAT_MBYTE
8189 /* Correct cursor for multi-byte character. */
8190 if (has_mbyte)
8191 mb_adjust_cursor();
8192#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008193
8194 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195}
8196
8197/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008198 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 */
8200 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008201f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008202 typval_T *argvars;
8203 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008205 int noref = 0;
8206
8207 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008208 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008209 if (noref < 0 || noref > 1)
8210 EMSG(_(e_invarg));
8211 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008212 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213}
8214
8215/*
8216 * "delete()" function
8217 */
8218 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008219f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008220 typval_T *argvars;
8221 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222{
8223 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008224 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008226 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227}
8228
8229/*
8230 * "did_filetype()" function
8231 */
8232/*ARGSUSED*/
8233 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008234f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008235 typval_T *argvars;
8236 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237{
8238#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008239 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008241 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242#endif
8243}
8244
8245/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008246 * "diff_filler()" function
8247 */
8248/*ARGSUSED*/
8249 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008250f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008251 typval_T *argvars;
8252 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008253{
8254#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008255 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008256#endif
8257}
8258
8259/*
8260 * "diff_hlID()" function
8261 */
8262/*ARGSUSED*/
8263 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008264f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008265 typval_T *argvars;
8266 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008267{
8268#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008269 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008270 static linenr_T prev_lnum = 0;
8271 static int changedtick = 0;
8272 static int fnum = 0;
8273 static int change_start = 0;
8274 static int change_end = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008275 static hlf_T hlID = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008276 int filler_lines;
8277 int col;
8278
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008279 if (lnum < 0) /* ignore type error in {lnum} arg */
8280 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008281 if (lnum != prev_lnum
8282 || changedtick != curbuf->b_changedtick
8283 || fnum != curbuf->b_fnum)
8284 {
8285 /* New line, buffer, change: need to get the values. */
8286 filler_lines = diff_check(curwin, lnum);
8287 if (filler_lines < 0)
8288 {
8289 if (filler_lines == -1)
8290 {
8291 change_start = MAXCOL;
8292 change_end = -1;
8293 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8294 hlID = HLF_ADD; /* added line */
8295 else
8296 hlID = HLF_CHD; /* changed line */
8297 }
8298 else
8299 hlID = HLF_ADD; /* added line */
8300 }
8301 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008302 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008303 prev_lnum = lnum;
8304 changedtick = curbuf->b_changedtick;
8305 fnum = curbuf->b_fnum;
8306 }
8307
8308 if (hlID == HLF_CHD || hlID == HLF_TXD)
8309 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008310 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008311 if (col >= change_start && col <= change_end)
8312 hlID = HLF_TXD; /* changed text */
8313 else
8314 hlID = HLF_CHD; /* changed line */
8315 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008316 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008317#endif
8318}
8319
8320/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008321 * "empty({expr})" function
8322 */
8323 static void
8324f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008325 typval_T *argvars;
8326 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008327{
8328 int n;
8329
8330 switch (argvars[0].v_type)
8331 {
8332 case VAR_STRING:
8333 case VAR_FUNC:
8334 n = argvars[0].vval.v_string == NULL
8335 || *argvars[0].vval.v_string == NUL;
8336 break;
8337 case VAR_NUMBER:
8338 n = argvars[0].vval.v_number == 0;
8339 break;
8340 case VAR_LIST:
8341 n = argvars[0].vval.v_list == NULL
8342 || argvars[0].vval.v_list->lv_first == NULL;
8343 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008344 case VAR_DICT:
8345 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008346 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008347 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008348 default:
8349 EMSG2(_(e_intern2), "f_empty()");
8350 n = 0;
8351 }
8352
8353 rettv->vval.v_number = n;
8354}
8355
8356/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357 * "escape({string}, {chars})" function
8358 */
8359 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008360f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008361 typval_T *argvars;
8362 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363{
8364 char_u buf[NUMBUFLEN];
8365
Bram Moolenaar758711c2005-02-02 23:11:38 +00008366 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8367 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008368 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369}
8370
8371/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008372 * "eval()" function
8373 */
8374/*ARGSUSED*/
8375 static void
8376f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008377 typval_T *argvars;
8378 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008379{
8380 char_u *s;
8381
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008382 s = get_tv_string_chk(&argvars[0]);
8383 if (s != NULL)
8384 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008385
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008386 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8387 {
8388 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008389 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008390 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008391 else if (*s != NUL)
8392 EMSG(_(e_trailing));
8393}
8394
8395/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396 * "eventhandler()" function
8397 */
8398/*ARGSUSED*/
8399 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008400f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008401 typval_T *argvars;
8402 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008404 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405}
8406
8407/*
8408 * "executable()" function
8409 */
8410 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008411f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008412 typval_T *argvars;
8413 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008415 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416}
8417
8418/*
8419 * "exists()" function
8420 */
8421 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008422f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008423 typval_T *argvars;
8424 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008425{
8426 char_u *p;
8427 char_u *name;
8428 int n = FALSE;
8429 int len = 0;
8430
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008431 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 if (*p == '$') /* environment variable */
8433 {
8434 /* first try "normal" environment variables (fast) */
8435 if (mch_getenv(p + 1) != NULL)
8436 n = TRUE;
8437 else
8438 {
8439 /* try expanding things like $VIM and ${HOME} */
8440 p = expand_env_save(p);
8441 if (p != NULL && *p != '$')
8442 n = TRUE;
8443 vim_free(p);
8444 }
8445 }
8446 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008447 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448 else if (*p == '*') /* internal or user defined function */
8449 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008450 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451 }
8452 else if (*p == ':')
8453 {
8454 n = cmd_exists(p + 1);
8455 }
8456 else if (*p == '#')
8457 {
8458#ifdef FEAT_AUTOCMD
8459 name = p + 1;
8460 p = vim_strchr(name, '#');
8461 if (p != NULL)
8462 n = au_exists(name, p, p + 1);
8463 else
8464 n = au_exists(name, name + STRLEN(name), NULL);
8465#endif
8466 }
8467 else /* internal variable */
8468 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008469 char_u *tofree;
8470 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008472 /* get_name_len() takes care of expanding curly braces */
8473 name = p;
8474 len = get_name_len(&p, &tofree, TRUE, FALSE);
8475 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008477 if (tofree != NULL)
8478 name = tofree;
8479 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8480 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008482 /* handle d.key, l[idx], f(expr) */
8483 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8484 if (n)
8485 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486 }
8487 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008489 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008490 }
8491
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008492 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493}
8494
8495/*
8496 * "expand()" function
8497 */
8498 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008499f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008500 typval_T *argvars;
8501 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502{
8503 char_u *s;
8504 int len;
8505 char_u *errormsg;
8506 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8507 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008508 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008510 rettv->v_type = VAR_STRING;
8511 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512 if (*s == '%' || *s == '#' || *s == '<')
8513 {
8514 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008515 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516 --emsg_off;
8517 }
8518 else
8519 {
8520 /* When the optional second argument is non-zero, don't remove matches
8521 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008522 if (argvars[1].v_type != VAR_UNKNOWN
8523 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008525 if (!error)
8526 {
8527 ExpandInit(&xpc);
8528 xpc.xp_context = EXPAND_FILES;
8529 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
8530 ExpandCleanup(&xpc);
8531 }
8532 else
8533 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534 }
8535}
8536
8537/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008538 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00008539 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008540 */
8541 static void
8542f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008543 typval_T *argvars;
8544 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008545{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008546 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008547 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008548 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008549 list_T *l1, *l2;
8550 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008551 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008552 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008553
Bram Moolenaare9a41262005-01-15 22:18:47 +00008554 l1 = argvars[0].vval.v_list;
8555 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008556 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
8557 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008558 {
8559 if (argvars[2].v_type != VAR_UNKNOWN)
8560 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008561 before = get_tv_number_chk(&argvars[2], &error);
8562 if (error)
8563 return; /* type error; errmsg already given */
8564
Bram Moolenaar758711c2005-02-02 23:11:38 +00008565 if (before == l1->lv_len)
8566 item = NULL;
8567 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008568 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00008569 item = list_find(l1, before);
8570 if (item == NULL)
8571 {
8572 EMSGN(_(e_listidx), before);
8573 return;
8574 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008575 }
8576 }
8577 else
8578 item = NULL;
8579 list_extend(l1, l2, item);
8580
Bram Moolenaare9a41262005-01-15 22:18:47 +00008581 copy_tv(&argvars[0], rettv);
8582 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008583 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008584 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
8585 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008586 dict_T *d1, *d2;
8587 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008588 char_u *action;
8589 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00008590 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008591 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008592
8593 d1 = argvars[0].vval.v_dict;
8594 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008595 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
8596 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008597 {
8598 /* Check the third argument. */
8599 if (argvars[2].v_type != VAR_UNKNOWN)
8600 {
8601 static char *(av[]) = {"keep", "force", "error"};
8602
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008603 action = get_tv_string_chk(&argvars[2]);
8604 if (action == NULL)
8605 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008606 for (i = 0; i < 3; ++i)
8607 if (STRCMP(action, av[i]) == 0)
8608 break;
8609 if (i == 3)
8610 {
8611 EMSGN(_(e_invarg2), action);
8612 return;
8613 }
8614 }
8615 else
8616 action = (char_u *)"force";
8617
8618 /* Go over all entries in the second dict and add them to the
8619 * first dict. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008620 todo = d2->dv_hashtab.ht_used;
8621 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008622 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008623 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008624 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008625 --todo;
8626 di1 = dict_find(d1, hi2->hi_key, -1);
8627 if (di1 == NULL)
8628 {
8629 di1 = dictitem_copy(HI2DI(hi2));
8630 if (di1 != NULL && dict_add(d1, di1) == FAIL)
8631 dictitem_free(di1);
8632 }
8633 else if (*action == 'e')
8634 {
8635 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
8636 break;
8637 }
8638 else if (*action == 'f')
8639 {
8640 clear_tv(&di1->di_tv);
8641 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
8642 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008643 }
8644 }
8645
Bram Moolenaare9a41262005-01-15 22:18:47 +00008646 copy_tv(&argvars[0], rettv);
8647 }
8648 }
8649 else
8650 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008651}
8652
8653/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654 * "filereadable()" function
8655 */
8656 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008657f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008658 typval_T *argvars;
8659 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660{
8661 FILE *fd;
8662 char_u *p;
8663 int n;
8664
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008665 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008666 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
8667 {
8668 n = TRUE;
8669 fclose(fd);
8670 }
8671 else
8672 n = FALSE;
8673
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008674 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675}
8676
8677/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008678 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00008679 * rights to write into.
8680 */
8681 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008682f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008683 typval_T *argvars;
8684 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008686 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008687}
8688
Bram Moolenaar33570922005-01-25 22:26:29 +00008689static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008690
8691 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008692findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00008693 typval_T *argvars;
8694 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008695 int dir;
8696{
8697#ifdef FEAT_SEARCHPATH
8698 char_u *fname;
8699 char_u *fresult = NULL;
8700 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
8701 char_u *p;
8702 char_u pathbuf[NUMBUFLEN];
8703 int count = 1;
8704 int first = TRUE;
8705
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008706 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008707
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008708 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008709 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008710 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
8711 if (p == NULL)
8712 count = -1; /* error */
8713 else
8714 {
8715 if (*p != NUL)
8716 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008717
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008718 if (argvars[2].v_type != VAR_UNKNOWN)
8719 count = get_tv_number_chk(&argvars[2], NULL); /* -1: error */
8720 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008721 }
8722
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008723 if (*fname != NUL && count >= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008724 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008725 do
8726 {
8727 vim_free(fresult);
8728 fresult = find_file_in_path_option(first ? fname : NULL,
8729 first ? (int)STRLEN(fname) : 0,
8730 0, first, path, dir, NULL);
8731 first = FALSE;
8732 } while (--count > 0 && fresult != NULL);
8733 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008734
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008735 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008736#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008737 rettv->vval.v_string = NULL;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008738#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008739 rettv->v_type = VAR_STRING;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008740}
8741
Bram Moolenaar33570922005-01-25 22:26:29 +00008742static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
8743static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008744
8745/*
8746 * Implementation of map() and filter().
8747 */
8748 static void
8749filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00008750 typval_T *argvars;
8751 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008752 int map;
8753{
8754 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00008755 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00008756 listitem_T *li, *nli;
8757 list_T *l = NULL;
8758 dictitem_T *di;
8759 hashtab_T *ht;
8760 hashitem_T *hi;
8761 dict_T *d = NULL;
8762 typval_T save_val;
8763 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008764 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008765 int todo;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008766 char_u *msg = map ? (char_u *)"map()" : (char_u *)"filter()";
8767
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008768
8769 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008770 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008771 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008772 if ((l = argvars[0].vval.v_list) == NULL
8773 || (map && tv_check_lock(l->lv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008774 return;
8775 }
8776 else if (argvars[0].v_type == VAR_DICT)
8777 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008778 if ((d = argvars[0].vval.v_dict) == NULL
8779 || (map && tv_check_lock(d->dv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008780 return;
8781 }
8782 else
8783 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008784 EMSG2(_(e_listdictarg), msg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008785 return;
8786 }
8787
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008788 expr = get_tv_string_buf_chk(&argvars[1], buf);
8789 /* On type errors, the preceding call has already displayed an error
8790 * message. Avoid a misleading error message for an empty string that
8791 * was not passed as argument. */
8792 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008793 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008794 prepare_vimvar(VV_VAL, &save_val);
8795 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008796
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008797 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008798 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008799 prepare_vimvar(VV_KEY, &save_key);
8800 vimvars[VV_KEY].vv_type = VAR_STRING;
8801
8802 ht = &d->dv_hashtab;
8803 hash_lock(ht);
8804 todo = ht->ht_used;
8805 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008806 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008807 if (!HASHITEM_EMPTY(hi))
8808 {
8809 --todo;
8810 di = HI2DI(hi);
8811 if (tv_check_lock(di->di_tv.v_lock, msg))
8812 break;
8813 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
8814 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL)
8815 break;
8816 if (!map && rem)
8817 dictitem_remove(d, di);
8818 clear_tv(&vimvars[VV_KEY].vv_tv);
8819 }
8820 }
8821 hash_unlock(ht);
8822
8823 restore_vimvar(VV_KEY, &save_key);
8824 }
8825 else
8826 {
8827 for (li = l->lv_first; li != NULL; li = nli)
8828 {
8829 if (tv_check_lock(li->li_tv.v_lock, msg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008830 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008831 nli = li->li_next;
8832 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008833 break;
8834 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008835 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008836 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008837 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008838
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008839 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008840 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008841
8842 copy_tv(&argvars[0], rettv);
8843}
8844
8845 static int
8846filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00008847 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008848 char_u *expr;
8849 int map;
8850 int *remp;
8851{
Bram Moolenaar33570922005-01-25 22:26:29 +00008852 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008853 char_u *s;
8854
Bram Moolenaar33570922005-01-25 22:26:29 +00008855 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008856 s = expr;
8857 if (eval1(&s, &rettv, TRUE) == FAIL)
8858 return FAIL;
8859 if (*s != NUL) /* check for trailing chars after expr */
8860 {
8861 EMSG2(_(e_invexpr2), s);
8862 return FAIL;
8863 }
8864 if (map)
8865 {
8866 /* map(): replace the list item value */
8867 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +00008868 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008869 *tv = rettv;
8870 }
8871 else
8872 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008873 int error = FALSE;
8874
Bram Moolenaare9a41262005-01-15 22:18:47 +00008875 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008876 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008877 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008878 /* On type error, nothing has been removed; return FAIL to stop the
8879 * loop. The error message was given by get_tv_number_chk(). */
8880 if (error)
8881 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008882 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008883 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008884 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008885}
8886
8887/*
8888 * "filter()" function
8889 */
8890 static void
8891f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008892 typval_T *argvars;
8893 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008894{
8895 filter_map(argvars, rettv, FALSE);
8896}
8897
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008898/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008899 * "finddir({fname}[, {path}[, {count}]])" function
8900 */
8901 static void
8902f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008903 typval_T *argvars;
8904 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008905{
8906 findfilendir(argvars, rettv, TRUE);
8907}
8908
8909/*
8910 * "findfile({fname}[, {path}[, {count}]])" function
8911 */
8912 static void
8913f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008914 typval_T *argvars;
8915 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008916{
8917 findfilendir(argvars, rettv, FALSE);
8918}
8919
8920/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921 * "fnamemodify({fname}, {mods})" function
8922 */
8923 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008924f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008925 typval_T *argvars;
8926 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927{
8928 char_u *fname;
8929 char_u *mods;
8930 int usedlen = 0;
8931 int len;
8932 char_u *fbuf = NULL;
8933 char_u buf[NUMBUFLEN];
8934
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008935 fname = get_tv_string_chk(&argvars[0]);
8936 mods = get_tv_string_buf_chk(&argvars[1], buf);
8937 if (fname == NULL || mods == NULL)
8938 fname = NULL;
8939 else
8940 {
8941 len = (int)STRLEN(fname);
8942 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
8943 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008944
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008945 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008947 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008949 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950 vim_free(fbuf);
8951}
8952
Bram Moolenaar33570922005-01-25 22:26:29 +00008953static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008954
8955/*
8956 * "foldclosed()" function
8957 */
8958 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008959foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00008960 typval_T *argvars;
8961 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962 int end;
8963{
8964#ifdef FEAT_FOLDING
8965 linenr_T lnum;
8966 linenr_T first, last;
8967
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008968 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8970 {
8971 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
8972 {
8973 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008974 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008975 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008976 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977 return;
8978 }
8979 }
8980#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008981 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982}
8983
8984/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008985 * "foldclosed()" function
8986 */
8987 static void
8988f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008989 typval_T *argvars;
8990 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008991{
8992 foldclosed_both(argvars, rettv, FALSE);
8993}
8994
8995/*
8996 * "foldclosedend()" function
8997 */
8998 static void
8999f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009000 typval_T *argvars;
9001 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009002{
9003 foldclosed_both(argvars, rettv, TRUE);
9004}
9005
9006/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009007 * "foldlevel()" function
9008 */
9009 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009010f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009011 typval_T *argvars;
9012 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009013{
9014#ifdef FEAT_FOLDING
9015 linenr_T lnum;
9016
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009017 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009018 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009019 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009020 else
9021#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009022 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009023}
9024
9025/*
9026 * "foldtext()" function
9027 */
9028/*ARGSUSED*/
9029 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009030f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009031 typval_T *argvars;
9032 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009033{
9034#ifdef FEAT_FOLDING
9035 linenr_T lnum;
9036 char_u *s;
9037 char_u *r;
9038 int len;
9039 char *txt;
9040#endif
9041
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009042 rettv->v_type = VAR_STRING;
9043 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009044#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00009045 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
9046 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
9047 <= curbuf->b_ml.ml_line_count
9048 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009049 {
9050 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009051 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
9052 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053 {
9054 if (!linewhite(lnum))
9055 break;
9056 ++lnum;
9057 }
9058
9059 /* Find interesting text in this line. */
9060 s = skipwhite(ml_get(lnum));
9061 /* skip C comment-start */
9062 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009063 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009065 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00009066 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009067 {
9068 s = skipwhite(ml_get(lnum + 1));
9069 if (*s == '*')
9070 s = skipwhite(s + 1);
9071 }
9072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009073 txt = _("+-%s%3ld lines: ");
9074 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009075 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009076 + 20 /* for %3ld */
9077 + STRLEN(s))); /* concatenated */
9078 if (r != NULL)
9079 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009080 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
9081 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
9082 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083 len = (int)STRLEN(r);
9084 STRCAT(r, s);
9085 /* remove 'foldmarker' and 'commentstring' */
9086 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009087 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009088 }
9089 }
9090#endif
9091}
9092
9093/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009094 * "foldtextresult(lnum)" function
9095 */
9096/*ARGSUSED*/
9097 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009098f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009099 typval_T *argvars;
9100 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009101{
9102#ifdef FEAT_FOLDING
9103 linenr_T lnum;
9104 char_u *text;
9105 char_u buf[51];
9106 foldinfo_T foldinfo;
9107 int fold_count;
9108#endif
9109
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009110 rettv->v_type = VAR_STRING;
9111 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009112#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009113 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009114 /* treat illegal types and illegal string values for {lnum} the same */
9115 if (lnum < 0)
9116 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009117 fold_count = foldedCount(curwin, lnum, &foldinfo);
9118 if (fold_count > 0)
9119 {
9120 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
9121 &foldinfo, buf);
9122 if (text == buf)
9123 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009124 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009125 }
9126#endif
9127}
9128
9129/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009130 * "foreground()" function
9131 */
9132/*ARGSUSED*/
9133 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009134f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009135 typval_T *argvars;
9136 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009137{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009138 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009139#ifdef FEAT_GUI
9140 if (gui.in_use)
9141 gui_mch_set_foreground();
9142#else
9143# ifdef WIN32
9144 win32_set_foreground();
9145# endif
9146#endif
9147}
9148
9149/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009150 * "function()" function
9151 */
9152/*ARGSUSED*/
9153 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009154f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009155 typval_T *argvars;
9156 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009157{
9158 char_u *s;
9159
Bram Moolenaara7043832005-01-21 11:56:39 +00009160 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009161 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009162 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009163 EMSG2(_(e_invarg2), s);
9164 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009165 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009166 else
9167 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009168 rettv->vval.v_string = vim_strsave(s);
9169 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009170 }
9171}
9172
9173/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009174 * "garbagecollect()" function
9175 */
9176/*ARGSUSED*/
9177 static void
9178f_garbagecollect(argvars, rettv)
9179 typval_T *argvars;
9180 typval_T *rettv;
9181{
9182 garbage_collect();
9183}
9184
9185/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009186 * "get()" function
9187 */
9188 static void
9189f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009190 typval_T *argvars;
9191 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009192{
Bram Moolenaar33570922005-01-25 22:26:29 +00009193 listitem_T *li;
9194 list_T *l;
9195 dictitem_T *di;
9196 dict_T *d;
9197 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009198
Bram Moolenaare9a41262005-01-15 22:18:47 +00009199 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009200 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009201 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009202 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009203 int error = FALSE;
9204
9205 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9206 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009207 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009208 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009209 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009210 else if (argvars[0].v_type == VAR_DICT)
9211 {
9212 if ((d = argvars[0].vval.v_dict) != NULL)
9213 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009214 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009215 if (di != NULL)
9216 tv = &di->di_tv;
9217 }
9218 }
9219 else
9220 EMSG2(_(e_listdictarg), "get()");
9221
9222 if (tv == NULL)
9223 {
9224 if (argvars[2].v_type == VAR_UNKNOWN)
9225 rettv->vval.v_number = 0;
9226 else
9227 copy_tv(&argvars[2], rettv);
9228 }
9229 else
9230 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009231}
9232
Bram Moolenaar342337a2005-07-21 21:11:17 +00009233static 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 +00009234
9235/*
9236 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +00009237 * Return a range (from start to end) of lines in rettv from the specified
9238 * buffer.
9239 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009240 */
9241 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +00009242get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009243 buf_T *buf;
9244 linenr_T start;
9245 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009246 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009247 typval_T *rettv;
9248{
9249 char_u *p;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009250 list_T *l = NULL;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009251
Bram Moolenaar342337a2005-07-21 21:11:17 +00009252 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009253 {
Bram Moolenaar342337a2005-07-21 21:11:17 +00009254 l = list_alloc();
9255 if (l == NULL)
9256 return;
9257
9258 rettv->vval.v_list = l;
9259 rettv->v_type = VAR_LIST;
9260 ++l->lv_refcount;
9261 }
9262 else
9263 rettv->vval.v_number = 0;
9264
9265 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
9266 return;
9267
9268 if (!retlist)
9269 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009270 if (start >= 1 && start <= buf->b_ml.ml_line_count)
9271 p = ml_get_buf(buf, start, FALSE);
9272 else
9273 p = (char_u *)"";
9274
9275 rettv->v_type = VAR_STRING;
9276 rettv->vval.v_string = vim_strsave(p);
9277 }
9278 else
9279 {
9280 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009281 return;
9282
9283 if (start < 1)
9284 start = 1;
9285 if (end > buf->b_ml.ml_line_count)
9286 end = buf->b_ml.ml_line_count;
9287 while (start <= end)
Bram Moolenaar4463f292005-09-25 22:20:24 +00009288 if (list_append_string(l, ml_get_buf(buf, start++, FALSE), -1)
9289 == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009290 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009291 }
9292}
9293
9294/*
9295 * "getbufline()" function
9296 */
9297 static void
9298f_getbufline(argvars, rettv)
9299 typval_T *argvars;
9300 typval_T *rettv;
9301{
9302 linenr_T lnum;
9303 linenr_T end;
9304 buf_T *buf;
9305
9306 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9307 ++emsg_off;
9308 buf = get_buf_tv(&argvars[0]);
9309 --emsg_off;
9310
Bram Moolenaar661b1822005-07-28 22:36:45 +00009311 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009312 if (argvars[2].v_type == VAR_UNKNOWN)
9313 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009314 else
Bram Moolenaar661b1822005-07-28 22:36:45 +00009315 end = get_tv_lnum_buf(&argvars[2], buf);
9316
Bram Moolenaar342337a2005-07-21 21:11:17 +00009317 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009318}
9319
Bram Moolenaar0d660222005-01-07 21:51:51 +00009320/*
9321 * "getbufvar()" function
9322 */
9323 static void
9324f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009325 typval_T *argvars;
9326 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009327{
9328 buf_T *buf;
9329 buf_T *save_curbuf;
9330 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009331 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009332
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009333 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9334 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009335 ++emsg_off;
9336 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009337
9338 rettv->v_type = VAR_STRING;
9339 rettv->vval.v_string = NULL;
9340
9341 if (buf != NULL && varname != NULL)
9342 {
9343 if (*varname == '&') /* buffer-local-option */
9344 {
9345 /* set curbuf to be our buf, temporarily */
9346 save_curbuf = curbuf;
9347 curbuf = buf;
9348
9349 get_option_tv(&varname, rettv, TRUE);
9350
9351 /* restore previous notion of curbuf */
9352 curbuf = save_curbuf;
9353 }
9354 else
9355 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009356 if (*varname == NUL)
9357 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9358 * scope prefix before the NUL byte is required by
9359 * find_var_in_ht(). */
9360 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009361 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009362 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009363 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009364 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009365 }
9366 }
9367
9368 --emsg_off;
9369}
9370
9371/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009372 * "getchar()" function
9373 */
9374 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009375f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009376 typval_T *argvars;
9377 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009378{
9379 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009380 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009381
9382 ++no_mapping;
9383 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009384 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009385 /* getchar(): blocking wait. */
9386 n = safe_vgetc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009387 else if (get_tv_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388 /* getchar(1): only check if char avail */
9389 n = vpeekc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009390 else if (error || vpeekc() == NUL)
9391 /* illegal argument or getchar(0) and no char avail: return zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009392 n = 0;
9393 else
9394 /* getchar(0) and char avail: return char */
9395 n = safe_vgetc();
9396 --no_mapping;
9397 --allow_keys;
9398
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009399 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009400 if (IS_SPECIAL(n) || mod_mask != 0)
9401 {
9402 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9403 int i = 0;
9404
9405 /* Turn a special key into three bytes, plus modifier. */
9406 if (mod_mask != 0)
9407 {
9408 temp[i++] = K_SPECIAL;
9409 temp[i++] = KS_MODIFIER;
9410 temp[i++] = mod_mask;
9411 }
9412 if (IS_SPECIAL(n))
9413 {
9414 temp[i++] = K_SPECIAL;
9415 temp[i++] = K_SECOND(n);
9416 temp[i++] = K_THIRD(n);
9417 }
9418#ifdef FEAT_MBYTE
9419 else if (has_mbyte)
9420 i += (*mb_char2bytes)(n, temp + i);
9421#endif
9422 else
9423 temp[i++] = n;
9424 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009425 rettv->v_type = VAR_STRING;
9426 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009427 }
9428}
9429
9430/*
9431 * "getcharmod()" function
9432 */
9433/*ARGSUSED*/
9434 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009435f_getcharmod(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 = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009440}
9441
9442/*
9443 * "getcmdline()" function
9444 */
9445/*ARGSUSED*/
9446 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009447f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009448 typval_T *argvars;
9449 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009450{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009451 rettv->v_type = VAR_STRING;
9452 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453}
9454
9455/*
9456 * "getcmdpos()" function
9457 */
9458/*ARGSUSED*/
9459 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009460f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009461 typval_T *argvars;
9462 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009463{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009464 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009465}
9466
9467/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00009468 * "getcmdtype()" function
9469 */
9470/*ARGSUSED*/
9471 static void
9472f_getcmdtype(argvars, rettv)
9473 typval_T *argvars;
9474 typval_T *rettv;
9475{
9476 rettv->v_type = VAR_STRING;
9477 rettv->vval.v_string = alloc(2);
9478 if (rettv->vval.v_string != NULL)
9479 {
9480 rettv->vval.v_string[0] = get_cmdline_type();
9481 rettv->vval.v_string[1] = NUL;
9482 }
9483}
9484
9485/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009486 * "getcwd()" function
9487 */
9488/*ARGSUSED*/
9489 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009490f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009491 typval_T *argvars;
9492 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009493{
9494 char_u cwd[MAXPATHL];
9495
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009496 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009498 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009499 else
9500 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009501 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009502#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +00009503 if (rettv->vval.v_string != NULL)
9504 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505#endif
9506 }
9507}
9508
9509/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009510 * "getfontname()" function
9511 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009512/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009513 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009514f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009515 typval_T *argvars;
9516 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009517{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009518 rettv->v_type = VAR_STRING;
9519 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009520#ifdef FEAT_GUI
9521 if (gui.in_use)
9522 {
9523 GuiFont font;
9524 char_u *name = NULL;
9525
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009526 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009527 {
9528 /* Get the "Normal" font. Either the name saved by
9529 * hl_set_font_name() or from the font ID. */
9530 font = gui.norm_font;
9531 name = hl_get_font_name();
9532 }
9533 else
9534 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009535 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009536 if (STRCMP(name, "*") == 0) /* don't use font dialog */
9537 return;
9538 font = gui_mch_get_font(name, FALSE);
9539 if (font == NOFONT)
9540 return; /* Invalid font name, return empty string. */
9541 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009542 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009543 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009544 gui_mch_free_font(font);
9545 }
9546#endif
9547}
9548
9549/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009550 * "getfperm({fname})" function
9551 */
9552 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009553f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009554 typval_T *argvars;
9555 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009556{
9557 char_u *fname;
9558 struct stat st;
9559 char_u *perm = NULL;
9560 char_u flags[] = "rwx";
9561 int i;
9562
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009563 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009564
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009565 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009566 if (mch_stat((char *)fname, &st) >= 0)
9567 {
9568 perm = vim_strsave((char_u *)"---------");
9569 if (perm != NULL)
9570 {
9571 for (i = 0; i < 9; i++)
9572 {
9573 if (st.st_mode & (1 << (8 - i)))
9574 perm[i] = flags[i % 3];
9575 }
9576 }
9577 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009578 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009579}
9580
9581/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009582 * "getfsize({fname})" function
9583 */
9584 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009585f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009586 typval_T *argvars;
9587 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009588{
9589 char_u *fname;
9590 struct stat st;
9591
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009592 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009593
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009594 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595
9596 if (mch_stat((char *)fname, &st) >= 0)
9597 {
9598 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009599 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009600 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009601 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009602 }
9603 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009604 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009605}
9606
9607/*
9608 * "getftime({fname})" function
9609 */
9610 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009611f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009612 typval_T *argvars;
9613 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009614{
9615 char_u *fname;
9616 struct stat st;
9617
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009618 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009619
9620 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009621 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009622 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009623 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009624}
9625
9626/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009627 * "getftype({fname})" function
9628 */
9629 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009630f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009631 typval_T *argvars;
9632 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009633{
9634 char_u *fname;
9635 struct stat st;
9636 char_u *type = NULL;
9637 char *t;
9638
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009639 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009640
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009641 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009642 if (mch_lstat((char *)fname, &st) >= 0)
9643 {
9644#ifdef S_ISREG
9645 if (S_ISREG(st.st_mode))
9646 t = "file";
9647 else if (S_ISDIR(st.st_mode))
9648 t = "dir";
9649# ifdef S_ISLNK
9650 else if (S_ISLNK(st.st_mode))
9651 t = "link";
9652# endif
9653# ifdef S_ISBLK
9654 else if (S_ISBLK(st.st_mode))
9655 t = "bdev";
9656# endif
9657# ifdef S_ISCHR
9658 else if (S_ISCHR(st.st_mode))
9659 t = "cdev";
9660# endif
9661# ifdef S_ISFIFO
9662 else if (S_ISFIFO(st.st_mode))
9663 t = "fifo";
9664# endif
9665# ifdef S_ISSOCK
9666 else if (S_ISSOCK(st.st_mode))
9667 t = "fifo";
9668# endif
9669 else
9670 t = "other";
9671#else
9672# ifdef S_IFMT
9673 switch (st.st_mode & S_IFMT)
9674 {
9675 case S_IFREG: t = "file"; break;
9676 case S_IFDIR: t = "dir"; break;
9677# ifdef S_IFLNK
9678 case S_IFLNK: t = "link"; break;
9679# endif
9680# ifdef S_IFBLK
9681 case S_IFBLK: t = "bdev"; break;
9682# endif
9683# ifdef S_IFCHR
9684 case S_IFCHR: t = "cdev"; break;
9685# endif
9686# ifdef S_IFIFO
9687 case S_IFIFO: t = "fifo"; break;
9688# endif
9689# ifdef S_IFSOCK
9690 case S_IFSOCK: t = "socket"; break;
9691# endif
9692 default: t = "other";
9693 }
9694# else
9695 if (mch_isdir(fname))
9696 t = "dir";
9697 else
9698 t = "file";
9699# endif
9700#endif
9701 type = vim_strsave((char_u *)t);
9702 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009703 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009704}
9705
9706/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009707 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +00009708 */
9709 static void
9710f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009711 typval_T *argvars;
9712 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009713{
9714 linenr_T lnum;
9715 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009716 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009717
9718 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009719 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009720 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009721 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009722 retlist = FALSE;
9723 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009724 else
Bram Moolenaar342337a2005-07-21 21:11:17 +00009725 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009726 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009727 retlist = TRUE;
9728 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009729
Bram Moolenaar342337a2005-07-21 21:11:17 +00009730 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009731}
9732
9733/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00009734 * "getqflist()" function
9735 */
9736/*ARGSUSED*/
9737 static void
9738f_getqflist(argvars, rettv)
9739 typval_T *argvars;
9740 typval_T *rettv;
9741{
9742#ifdef FEAT_QUICKFIX
9743 list_T *l;
9744#endif
9745
9746 rettv->vval.v_number = FALSE;
9747#ifdef FEAT_QUICKFIX
9748 l = list_alloc();
9749 if (l != NULL)
9750 {
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00009751 rettv->vval.v_list = l;
9752 rettv->v_type = VAR_LIST;
9753 ++l->lv_refcount;
9754 (void)get_errorlist(l);
Bram Moolenaar2641f772005-03-25 21:58:17 +00009755 }
9756#endif
9757}
9758
9759/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009760 * "getreg()" function
9761 */
9762 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009763f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009764 typval_T *argvars;
9765 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766{
9767 char_u *strregname;
9768 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009769 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009770 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009771
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009772 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009773 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009774 strregname = get_tv_string_chk(&argvars[0]);
9775 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009776 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009777 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009779 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009780 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009781 regname = (strregname == NULL ? '"' : *strregname);
9782 if (regname == 0)
9783 regname = '"';
9784
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009785 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009786 rettv->vval.v_string = error ? NULL :
9787 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788}
9789
9790/*
9791 * "getregtype()" function
9792 */
9793 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009794f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009795 typval_T *argvars;
9796 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009797{
9798 char_u *strregname;
9799 int regname;
9800 char_u buf[NUMBUFLEN + 2];
9801 long reglen = 0;
9802
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009803 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009804 {
9805 strregname = get_tv_string_chk(&argvars[0]);
9806 if (strregname == NULL) /* type error; errmsg already given */
9807 {
9808 rettv->v_type = VAR_STRING;
9809 rettv->vval.v_string = NULL;
9810 return;
9811 }
9812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009813 else
9814 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009815 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009816
9817 regname = (strregname == NULL ? '"' : *strregname);
9818 if (regname == 0)
9819 regname = '"';
9820
9821 buf[0] = NUL;
9822 buf[1] = NUL;
9823 switch (get_reg_type(regname, &reglen))
9824 {
9825 case MLINE: buf[0] = 'V'; break;
9826 case MCHAR: buf[0] = 'v'; break;
9827#ifdef FEAT_VISUAL
9828 case MBLOCK:
9829 buf[0] = Ctrl_V;
9830 sprintf((char *)buf + 1, "%ld", reglen + 1);
9831 break;
9832#endif
9833 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009834 rettv->v_type = VAR_STRING;
9835 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009836}
9837
9838/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009839 * "getwinposx()" function
9840 */
9841/*ARGSUSED*/
9842 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009843f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009844 typval_T *argvars;
9845 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009846{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009847 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009848#ifdef FEAT_GUI
9849 if (gui.in_use)
9850 {
9851 int x, y;
9852
9853 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009854 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009855 }
9856#endif
9857}
9858
9859/*
9860 * "getwinposy()" function
9861 */
9862/*ARGSUSED*/
9863 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009864f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009865 typval_T *argvars;
9866 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009867{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009868 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009869#ifdef FEAT_GUI
9870 if (gui.in_use)
9871 {
9872 int x, y;
9873
9874 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009875 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009876 }
9877#endif
9878}
9879
Bram Moolenaara40058a2005-07-11 22:42:07 +00009880static win_T *find_win_by_nr __ARGS((typval_T *vp));
9881
9882 static win_T *
9883find_win_by_nr(vp)
9884 typval_T *vp;
9885{
9886#ifdef FEAT_WINDOWS
9887 win_T *wp;
9888#endif
9889 int nr;
9890
9891 nr = get_tv_number_chk(vp, NULL);
9892
9893#ifdef FEAT_WINDOWS
9894 if (nr < 0)
9895 return NULL;
9896 if (nr == 0)
9897 return curwin;
9898
9899 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9900 if (--nr <= 0)
9901 break;
9902 return wp;
9903#else
9904 if (nr == 0 || nr == 1)
9905 return curwin;
9906 return NULL;
9907#endif
9908}
9909
Bram Moolenaar071d4272004-06-13 20:20:40 +00009910/*
9911 * "getwinvar()" function
9912 */
9913 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009914f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009915 typval_T *argvars;
9916 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009917{
9918 win_T *win, *oldcurwin;
9919 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009920 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009921
Bram Moolenaar071d4272004-06-13 20:20:40 +00009922 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009923 varname = get_tv_string_chk(&argvars[1]);
9924 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009925
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009926 rettv->v_type = VAR_STRING;
9927 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009928
9929 if (win != NULL && varname != NULL)
9930 {
9931 if (*varname == '&') /* window-local-option */
9932 {
Bram Moolenaarc0761132005-03-18 20:30:32 +00009933 /* Set curwin to be our win, temporarily. Also set curbuf, so
9934 * that we can get buffer-local options. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009935 oldcurwin = curwin;
9936 curwin = win;
Bram Moolenaarc0761132005-03-18 20:30:32 +00009937 curbuf = win->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009938
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009939 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009940
9941 /* restore previous notion of curwin */
9942 curwin = oldcurwin;
Bram Moolenaarc0761132005-03-18 20:30:32 +00009943 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009944 }
9945 else
9946 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009947 if (*varname == NUL)
9948 /* let getwinvar({nr}, "") return the "w:" dictionary. The
9949 * scope prefix before the NUL byte is required by
9950 * find_var_in_ht(). */
9951 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009952 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009953 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009954 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009955 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009956 }
9957 }
9958
9959 --emsg_off;
9960}
9961
9962/*
9963 * "glob()" function
9964 */
9965 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009966f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009967 typval_T *argvars;
9968 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009969{
9970 expand_T xpc;
9971
9972 ExpandInit(&xpc);
9973 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009974 rettv->v_type = VAR_STRING;
9975 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009976 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
9977 ExpandCleanup(&xpc);
9978}
9979
9980/*
9981 * "globpath()" function
9982 */
9983 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009984f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009985 typval_T *argvars;
9986 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009987{
9988 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009989 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009990
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009991 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009992 if (file == NULL)
9993 rettv->vval.v_string = NULL;
9994 else
9995 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009996}
9997
9998/*
9999 * "has()" function
10000 */
10001 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010002f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010003 typval_T *argvars;
10004 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010005{
10006 int i;
10007 char_u *name;
10008 int n = FALSE;
10009 static char *(has_list[]) =
10010 {
10011#ifdef AMIGA
10012 "amiga",
10013# ifdef FEAT_ARP
10014 "arp",
10015# endif
10016#endif
10017#ifdef __BEOS__
10018 "beos",
10019#endif
10020#ifdef MSDOS
10021# ifdef DJGPP
10022 "dos32",
10023# else
10024 "dos16",
10025# endif
10026#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000010027#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010028 "mac",
10029#endif
10030#if defined(MACOS_X_UNIX)
10031 "macunix",
10032#endif
10033#ifdef OS2
10034 "os2",
10035#endif
10036#ifdef __QNX__
10037 "qnx",
10038#endif
10039#ifdef RISCOS
10040 "riscos",
10041#endif
10042#ifdef UNIX
10043 "unix",
10044#endif
10045#ifdef VMS
10046 "vms",
10047#endif
10048#ifdef WIN16
10049 "win16",
10050#endif
10051#ifdef WIN32
10052 "win32",
10053#endif
10054#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
10055 "win32unix",
10056#endif
10057#ifdef WIN64
10058 "win64",
10059#endif
10060#ifdef EBCDIC
10061 "ebcdic",
10062#endif
10063#ifndef CASE_INSENSITIVE_FILENAME
10064 "fname_case",
10065#endif
10066#ifdef FEAT_ARABIC
10067 "arabic",
10068#endif
10069#ifdef FEAT_AUTOCMD
10070 "autocmd",
10071#endif
10072#ifdef FEAT_BEVAL
10073 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000010074# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
10075 "balloon_multiline",
10076# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010077#endif
10078#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
10079 "builtin_terms",
10080# ifdef ALL_BUILTIN_TCAPS
10081 "all_builtin_terms",
10082# endif
10083#endif
10084#ifdef FEAT_BYTEOFF
10085 "byte_offset",
10086#endif
10087#ifdef FEAT_CINDENT
10088 "cindent",
10089#endif
10090#ifdef FEAT_CLIENTSERVER
10091 "clientserver",
10092#endif
10093#ifdef FEAT_CLIPBOARD
10094 "clipboard",
10095#endif
10096#ifdef FEAT_CMDL_COMPL
10097 "cmdline_compl",
10098#endif
10099#ifdef FEAT_CMDHIST
10100 "cmdline_hist",
10101#endif
10102#ifdef FEAT_COMMENTS
10103 "comments",
10104#endif
10105#ifdef FEAT_CRYPT
10106 "cryptv",
10107#endif
10108#ifdef FEAT_CSCOPE
10109 "cscope",
10110#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010111#ifdef CURSOR_SHAPE
10112 "cursorshape",
10113#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010114#ifdef DEBUG
10115 "debug",
10116#endif
10117#ifdef FEAT_CON_DIALOG
10118 "dialog_con",
10119#endif
10120#ifdef FEAT_GUI_DIALOG
10121 "dialog_gui",
10122#endif
10123#ifdef FEAT_DIFF
10124 "diff",
10125#endif
10126#ifdef FEAT_DIGRAPHS
10127 "digraphs",
10128#endif
10129#ifdef FEAT_DND
10130 "dnd",
10131#endif
10132#ifdef FEAT_EMACS_TAGS
10133 "emacs_tags",
10134#endif
10135 "eval", /* always present, of course! */
10136#ifdef FEAT_EX_EXTRA
10137 "ex_extra",
10138#endif
10139#ifdef FEAT_SEARCH_EXTRA
10140 "extra_search",
10141#endif
10142#ifdef FEAT_FKMAP
10143 "farsi",
10144#endif
10145#ifdef FEAT_SEARCHPATH
10146 "file_in_path",
10147#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010148#if defined(UNIX) && !defined(USE_SYSTEM)
10149 "filterpipe",
10150#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010151#ifdef FEAT_FIND_ID
10152 "find_in_path",
10153#endif
10154#ifdef FEAT_FOLDING
10155 "folding",
10156#endif
10157#ifdef FEAT_FOOTER
10158 "footer",
10159#endif
10160#if !defined(USE_SYSTEM) && defined(UNIX)
10161 "fork",
10162#endif
10163#ifdef FEAT_GETTEXT
10164 "gettext",
10165#endif
10166#ifdef FEAT_GUI
10167 "gui",
10168#endif
10169#ifdef FEAT_GUI_ATHENA
10170# ifdef FEAT_GUI_NEXTAW
10171 "gui_neXtaw",
10172# else
10173 "gui_athena",
10174# endif
10175#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010176#ifdef FEAT_GUI_GTK
10177 "gui_gtk",
10178# ifdef HAVE_GTK2
10179 "gui_gtk2",
10180# endif
10181#endif
10182#ifdef FEAT_GUI_MAC
10183 "gui_mac",
10184#endif
10185#ifdef FEAT_GUI_MOTIF
10186 "gui_motif",
10187#endif
10188#ifdef FEAT_GUI_PHOTON
10189 "gui_photon",
10190#endif
10191#ifdef FEAT_GUI_W16
10192 "gui_win16",
10193#endif
10194#ifdef FEAT_GUI_W32
10195 "gui_win32",
10196#endif
10197#ifdef FEAT_HANGULIN
10198 "hangul_input",
10199#endif
10200#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10201 "iconv",
10202#endif
10203#ifdef FEAT_INS_EXPAND
10204 "insert_expand",
10205#endif
10206#ifdef FEAT_JUMPLIST
10207 "jumplist",
10208#endif
10209#ifdef FEAT_KEYMAP
10210 "keymap",
10211#endif
10212#ifdef FEAT_LANGMAP
10213 "langmap",
10214#endif
10215#ifdef FEAT_LIBCALL
10216 "libcall",
10217#endif
10218#ifdef FEAT_LINEBREAK
10219 "linebreak",
10220#endif
10221#ifdef FEAT_LISP
10222 "lispindent",
10223#endif
10224#ifdef FEAT_LISTCMDS
10225 "listcmds",
10226#endif
10227#ifdef FEAT_LOCALMAP
10228 "localmap",
10229#endif
10230#ifdef FEAT_MENU
10231 "menu",
10232#endif
10233#ifdef FEAT_SESSION
10234 "mksession",
10235#endif
10236#ifdef FEAT_MODIFY_FNAME
10237 "modify_fname",
10238#endif
10239#ifdef FEAT_MOUSE
10240 "mouse",
10241#endif
10242#ifdef FEAT_MOUSESHAPE
10243 "mouseshape",
10244#endif
10245#if defined(UNIX) || defined(VMS)
10246# ifdef FEAT_MOUSE_DEC
10247 "mouse_dec",
10248# endif
10249# ifdef FEAT_MOUSE_GPM
10250 "mouse_gpm",
10251# endif
10252# ifdef FEAT_MOUSE_JSB
10253 "mouse_jsbterm",
10254# endif
10255# ifdef FEAT_MOUSE_NET
10256 "mouse_netterm",
10257# endif
10258# ifdef FEAT_MOUSE_PTERM
10259 "mouse_pterm",
10260# endif
10261# ifdef FEAT_MOUSE_XTERM
10262 "mouse_xterm",
10263# endif
10264#endif
10265#ifdef FEAT_MBYTE
10266 "multi_byte",
10267#endif
10268#ifdef FEAT_MBYTE_IME
10269 "multi_byte_ime",
10270#endif
10271#ifdef FEAT_MULTI_LANG
10272 "multi_lang",
10273#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010274#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010275#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010276 "mzscheme",
10277#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010278#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010279#ifdef FEAT_OLE
10280 "ole",
10281#endif
10282#ifdef FEAT_OSFILETYPE
10283 "osfiletype",
10284#endif
10285#ifdef FEAT_PATH_EXTRA
10286 "path_extra",
10287#endif
10288#ifdef FEAT_PERL
10289#ifndef DYNAMIC_PERL
10290 "perl",
10291#endif
10292#endif
10293#ifdef FEAT_PYTHON
10294#ifndef DYNAMIC_PYTHON
10295 "python",
10296#endif
10297#endif
10298#ifdef FEAT_POSTSCRIPT
10299 "postscript",
10300#endif
10301#ifdef FEAT_PRINTER
10302 "printer",
10303#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000010304#ifdef FEAT_PROFILE
10305 "profile",
10306#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010307#ifdef FEAT_QUICKFIX
10308 "quickfix",
10309#endif
10310#ifdef FEAT_RIGHTLEFT
10311 "rightleft",
10312#endif
10313#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
10314 "ruby",
10315#endif
10316#ifdef FEAT_SCROLLBIND
10317 "scrollbind",
10318#endif
10319#ifdef FEAT_CMDL_INFO
10320 "showcmd",
10321 "cmdline_info",
10322#endif
10323#ifdef FEAT_SIGNS
10324 "signs",
10325#endif
10326#ifdef FEAT_SMARTINDENT
10327 "smartindent",
10328#endif
10329#ifdef FEAT_SNIFF
10330 "sniff",
10331#endif
10332#ifdef FEAT_STL_OPT
10333 "statusline",
10334#endif
10335#ifdef FEAT_SUN_WORKSHOP
10336 "sun_workshop",
10337#endif
10338#ifdef FEAT_NETBEANS_INTG
10339 "netbeans_intg",
10340#endif
10341#ifdef FEAT_SYN_HL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010342 "spell",
10343#endif
10344#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010345 "syntax",
10346#endif
10347#if defined(USE_SYSTEM) || !defined(UNIX)
10348 "system",
10349#endif
10350#ifdef FEAT_TAG_BINS
10351 "tag_binary",
10352#endif
10353#ifdef FEAT_TAG_OLDSTATIC
10354 "tag_old_static",
10355#endif
10356#ifdef FEAT_TAG_ANYWHITE
10357 "tag_any_white",
10358#endif
10359#ifdef FEAT_TCL
10360# ifndef DYNAMIC_TCL
10361 "tcl",
10362# endif
10363#endif
10364#ifdef TERMINFO
10365 "terminfo",
10366#endif
10367#ifdef FEAT_TERMRESPONSE
10368 "termresponse",
10369#endif
10370#ifdef FEAT_TEXTOBJ
10371 "textobjects",
10372#endif
10373#ifdef HAVE_TGETENT
10374 "tgetent",
10375#endif
10376#ifdef FEAT_TITLE
10377 "title",
10378#endif
10379#ifdef FEAT_TOOLBAR
10380 "toolbar",
10381#endif
10382#ifdef FEAT_USR_CMDS
10383 "user-commands", /* was accidentally included in 5.4 */
10384 "user_commands",
10385#endif
10386#ifdef FEAT_VIMINFO
10387 "viminfo",
10388#endif
10389#ifdef FEAT_VERTSPLIT
10390 "vertsplit",
10391#endif
10392#ifdef FEAT_VIRTUALEDIT
10393 "virtualedit",
10394#endif
10395#ifdef FEAT_VISUAL
10396 "visual",
10397#endif
10398#ifdef FEAT_VISUALEXTRA
10399 "visualextra",
10400#endif
10401#ifdef FEAT_VREPLACE
10402 "vreplace",
10403#endif
10404#ifdef FEAT_WILDIGN
10405 "wildignore",
10406#endif
10407#ifdef FEAT_WILDMENU
10408 "wildmenu",
10409#endif
10410#ifdef FEAT_WINDOWS
10411 "windows",
10412#endif
10413#ifdef FEAT_WAK
10414 "winaltkeys",
10415#endif
10416#ifdef FEAT_WRITEBACKUP
10417 "writebackup",
10418#endif
10419#ifdef FEAT_XIM
10420 "xim",
10421#endif
10422#ifdef FEAT_XFONTSET
10423 "xfontset",
10424#endif
10425#ifdef USE_XSMP
10426 "xsmp",
10427#endif
10428#ifdef USE_XSMP_INTERACT
10429 "xsmp_interact",
10430#endif
10431#ifdef FEAT_XCLIPBOARD
10432 "xterm_clipboard",
10433#endif
10434#ifdef FEAT_XTERM_SAVE
10435 "xterm_save",
10436#endif
10437#if defined(UNIX) && defined(FEAT_X11)
10438 "X11",
10439#endif
10440 NULL
10441 };
10442
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010443 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010444 for (i = 0; has_list[i] != NULL; ++i)
10445 if (STRICMP(name, has_list[i]) == 0)
10446 {
10447 n = TRUE;
10448 break;
10449 }
10450
10451 if (n == FALSE)
10452 {
10453 if (STRNICMP(name, "patch", 5) == 0)
10454 n = has_patch(atoi((char *)name + 5));
10455 else if (STRICMP(name, "vim_starting") == 0)
10456 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010457#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
10458 else if (STRICMP(name, "balloon_multiline") == 0)
10459 n = multiline_balloon_available();
10460#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010461#ifdef DYNAMIC_TCL
10462 else if (STRICMP(name, "tcl") == 0)
10463 n = tcl_enabled(FALSE);
10464#endif
10465#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
10466 else if (STRICMP(name, "iconv") == 0)
10467 n = iconv_enabled(FALSE);
10468#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010469#ifdef DYNAMIC_MZSCHEME
10470 else if (STRICMP(name, "mzscheme") == 0)
10471 n = mzscheme_enabled(FALSE);
10472#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010473#ifdef DYNAMIC_RUBY
10474 else if (STRICMP(name, "ruby") == 0)
10475 n = ruby_enabled(FALSE);
10476#endif
10477#ifdef DYNAMIC_PYTHON
10478 else if (STRICMP(name, "python") == 0)
10479 n = python_enabled(FALSE);
10480#endif
10481#ifdef DYNAMIC_PERL
10482 else if (STRICMP(name, "perl") == 0)
10483 n = perl_enabled(FALSE);
10484#endif
10485#ifdef FEAT_GUI
10486 else if (STRICMP(name, "gui_running") == 0)
10487 n = (gui.in_use || gui.starting);
10488# ifdef FEAT_GUI_W32
10489 else if (STRICMP(name, "gui_win32s") == 0)
10490 n = gui_is_win32s();
10491# endif
10492# ifdef FEAT_BROWSE
10493 else if (STRICMP(name, "browse") == 0)
10494 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
10495# endif
10496#endif
10497#ifdef FEAT_SYN_HL
10498 else if (STRICMP(name, "syntax_items") == 0)
10499 n = syntax_present(curbuf);
10500#endif
10501#if defined(WIN3264)
10502 else if (STRICMP(name, "win95") == 0)
10503 n = mch_windows95();
10504#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000010505#ifdef FEAT_NETBEANS_INTG
10506 else if (STRICMP(name, "netbeans_enabled") == 0)
10507 n = usingNetbeans;
10508#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010509 }
10510
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010511 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010512}
10513
10514/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000010515 * "has_key()" function
10516 */
10517 static void
10518f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010519 typval_T *argvars;
10520 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010521{
10522 rettv->vval.v_number = 0;
10523 if (argvars[0].v_type != VAR_DICT)
10524 {
10525 EMSG(_(e_dictreq));
10526 return;
10527 }
10528 if (argvars[0].vval.v_dict == NULL)
10529 return;
10530
10531 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010532 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010533}
10534
10535/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010536 * "hasmapto()" function
10537 */
10538 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010539f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010540 typval_T *argvars;
10541 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010542{
10543 char_u *name;
10544 char_u *mode;
10545 char_u buf[NUMBUFLEN];
10546
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010547 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010548 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010549 mode = (char_u *)"nvo";
10550 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010551 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010552
10553 if (map_to_exists(name, mode))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010554 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010555 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010556 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010557}
10558
10559/*
10560 * "histadd()" function
10561 */
10562/*ARGSUSED*/
10563 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010564f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010565 typval_T *argvars;
10566 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010567{
10568#ifdef FEAT_CMDHIST
10569 int histype;
10570 char_u *str;
10571 char_u buf[NUMBUFLEN];
10572#endif
10573
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010574 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010575 if (check_restricted() || check_secure())
10576 return;
10577#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010578 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10579 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010580 if (histype >= 0)
10581 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010582 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010583 if (*str != NUL)
10584 {
10585 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010586 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010587 return;
10588 }
10589 }
10590#endif
10591}
10592
10593/*
10594 * "histdel()" function
10595 */
10596/*ARGSUSED*/
10597 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010598f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010599 typval_T *argvars;
10600 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010601{
10602#ifdef FEAT_CMDHIST
10603 int n;
10604 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010605 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010606
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010607 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10608 if (str == NULL)
10609 n = 0;
10610 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010611 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010612 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010613 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010614 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010615 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010616 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010617 else
10618 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010619 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010620 get_tv_string_buf(&argvars[1], buf));
10621 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010622#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010623 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010624#endif
10625}
10626
10627/*
10628 * "histget()" function
10629 */
10630/*ARGSUSED*/
10631 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010632f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010633 typval_T *argvars;
10634 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010635{
10636#ifdef FEAT_CMDHIST
10637 int type;
10638 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010639 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010640
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010641 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10642 if (str == NULL)
10643 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010644 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010645 {
10646 type = get_histtype(str);
10647 if (argvars[1].v_type == VAR_UNKNOWN)
10648 idx = get_history_idx(type);
10649 else
10650 idx = (int)get_tv_number_chk(&argvars[1], NULL);
10651 /* -1 on type error */
10652 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
10653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010654#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010655 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010656#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010657 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010658}
10659
10660/*
10661 * "histnr()" function
10662 */
10663/*ARGSUSED*/
10664 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010665f_histnr(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{
10669 int i;
10670
10671#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010672 char_u *history = get_tv_string_chk(&argvars[0]);
10673
10674 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010675 if (i >= HIST_CMD && i < HIST_COUNT)
10676 i = get_history_idx(i);
10677 else
10678#endif
10679 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010680 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010681}
10682
10683/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010684 * "highlightID(name)" function
10685 */
10686 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010687f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010688 typval_T *argvars;
10689 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010690{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010691 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010692}
10693
10694/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010695 * "highlight_exists()" function
10696 */
10697 static void
10698f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010699 typval_T *argvars;
10700 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010701{
10702 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
10703}
10704
10705/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010706 * "hostname()" function
10707 */
10708/*ARGSUSED*/
10709 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010710f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010711 typval_T *argvars;
10712 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010713{
10714 char_u hostname[256];
10715
10716 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010717 rettv->v_type = VAR_STRING;
10718 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010719}
10720
10721/*
10722 * iconv() function
10723 */
10724/*ARGSUSED*/
10725 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010726f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010727 typval_T *argvars;
10728 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010729{
10730#ifdef FEAT_MBYTE
10731 char_u buf1[NUMBUFLEN];
10732 char_u buf2[NUMBUFLEN];
10733 char_u *from, *to, *str;
10734 vimconv_T vimconv;
10735#endif
10736
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010737 rettv->v_type = VAR_STRING;
10738 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010739
10740#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010741 str = get_tv_string(&argvars[0]);
10742 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
10743 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010744 vimconv.vc_type = CONV_NONE;
10745 convert_setup(&vimconv, from, to);
10746
10747 /* If the encodings are equal, no conversion needed. */
10748 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010749 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010750 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010751 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010752
10753 convert_setup(&vimconv, NULL, NULL);
10754 vim_free(from);
10755 vim_free(to);
10756#endif
10757}
10758
10759/*
10760 * "indent()" function
10761 */
10762 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010763f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010764 typval_T *argvars;
10765 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010766{
10767 linenr_T lnum;
10768
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010769 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010770 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010771 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010772 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010773 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010774}
10775
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010776/*
10777 * "index()" function
10778 */
10779 static void
10780f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010781 typval_T *argvars;
10782 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010783{
Bram Moolenaar33570922005-01-25 22:26:29 +000010784 list_T *l;
10785 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010786 long idx = 0;
10787 int ic = FALSE;
10788
10789 rettv->vval.v_number = -1;
10790 if (argvars[0].v_type != VAR_LIST)
10791 {
10792 EMSG(_(e_listreq));
10793 return;
10794 }
10795 l = argvars[0].vval.v_list;
10796 if (l != NULL)
10797 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010798 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010799 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010800 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010801 int error = FALSE;
10802
Bram Moolenaar758711c2005-02-02 23:11:38 +000010803 /* Start at specified item. Use the cached index that list_find()
10804 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010805 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000010806 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010807 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010808 ic = get_tv_number_chk(&argvars[3], &error);
10809 if (error)
10810 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010811 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010812
Bram Moolenaar758711c2005-02-02 23:11:38 +000010813 for ( ; item != NULL; item = item->li_next, ++idx)
10814 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010815 {
10816 rettv->vval.v_number = idx;
10817 break;
10818 }
10819 }
10820}
10821
Bram Moolenaar071d4272004-06-13 20:20:40 +000010822static int inputsecret_flag = 0;
10823
10824/*
10825 * "input()" function
10826 * Also handles inputsecret() when inputsecret is set.
10827 */
10828 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010829f_input(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010830 typval_T *argvars;
10831 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010832{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010833 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010834 char_u *p = NULL;
10835 int c;
10836 char_u buf[NUMBUFLEN];
10837 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010838 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010839 int xp_type = EXPAND_NOTHING;
10840 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010841
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010842 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010843
10844#ifdef NO_CONSOLE_INPUT
10845 /* While starting up, there is no place to enter text. */
10846 if (no_console_input())
10847 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010848 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010849 return;
10850 }
10851#endif
10852
10853 cmd_silent = FALSE; /* Want to see the prompt. */
10854 if (prompt != NULL)
10855 {
10856 /* Only the part of the message after the last NL is considered as
10857 * prompt for the command line */
10858 p = vim_strrchr(prompt, '\n');
10859 if (p == NULL)
10860 p = prompt;
10861 else
10862 {
10863 ++p;
10864 c = *p;
10865 *p = NUL;
10866 msg_start();
10867 msg_clr_eos();
10868 msg_puts_attr(prompt, echo_attr);
10869 msg_didout = FALSE;
10870 msg_starthere();
10871 *p = c;
10872 }
10873 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010874
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010875 if (argvars[1].v_type != VAR_UNKNOWN)
10876 {
10877 defstr = get_tv_string_buf_chk(&argvars[1], buf);
10878 if (defstr != NULL)
10879 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010880
Bram Moolenaar4463f292005-09-25 22:20:24 +000010881 if (argvars[2].v_type != VAR_UNKNOWN)
10882 {
10883 char_u *xp_name;
10884 int xp_namelen;
10885 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010886
Bram Moolenaar4463f292005-09-25 22:20:24 +000010887 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010888
Bram Moolenaar4463f292005-09-25 22:20:24 +000010889 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
10890 if (xp_name == NULL)
10891 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010892
Bram Moolenaar4463f292005-09-25 22:20:24 +000010893 xp_namelen = STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010894
Bram Moolenaar4463f292005-09-25 22:20:24 +000010895 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
10896 &xp_arg) == FAIL)
10897 return;
10898 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010899 }
10900
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010901 if (defstr != NULL)
10902 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010903 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
10904 xp_type, xp_arg);
10905
10906 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010907
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010908 /* since the user typed this, no need to wait for return */
10909 need_wait_return = FALSE;
10910 msg_didout = FALSE;
10911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010912 cmd_silent = cmd_silent_save;
10913}
10914
10915/*
10916 * "inputdialog()" function
10917 */
10918 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010919f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010920 typval_T *argvars;
10921 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010922{
10923#if defined(FEAT_GUI_TEXTDIALOG)
10924 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
10925 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
10926 {
10927 char_u *message;
10928 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010929 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000010930
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010931 message = get_tv_string_chk(&argvars[0]);
10932 if (argvars[1].v_type != VAR_UNKNOWN
10933 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000010934 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010935 else
10936 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010937 if (message != NULL && defstr != NULL
10938 && do_dialog(VIM_QUESTION, NULL, message,
10939 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010940 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010941 else
10942 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010943 if (message != NULL && defstr != NULL
10944 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010945 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010946 rettv->vval.v_string = vim_strsave(
10947 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010948 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010949 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010950 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010951 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010952 }
10953 else
10954#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010955 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010956}
10957
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000010958/*
10959 * "inputlist()" function
10960 */
10961 static void
10962f_inputlist(argvars, rettv)
10963 typval_T *argvars;
10964 typval_T *rettv;
10965{
10966 listitem_T *li;
10967 int selected;
10968 int mouse_used;
10969
10970 rettv->vval.v_number = 0;
10971#ifdef NO_CONSOLE_INPUT
10972 /* While starting up, there is no place to enter text. */
10973 if (no_console_input())
10974 return;
10975#endif
10976 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
10977 {
10978 EMSG2(_(e_listarg), "inputlist()");
10979 return;
10980 }
10981
10982 msg_start();
10983 lines_left = Rows; /* avoid more prompt */
10984 msg_scroll = TRUE;
10985 msg_clr_eos();
10986
10987 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
10988 {
10989 msg_puts(get_tv_string(&li->li_tv));
10990 msg_putchar('\n');
10991 }
10992
10993 /* Ask for choice. */
10994 selected = prompt_for_number(&mouse_used);
10995 if (mouse_used)
10996 selected -= lines_left;
10997
10998 rettv->vval.v_number = selected;
10999}
11000
11001
Bram Moolenaar071d4272004-06-13 20:20:40 +000011002static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
11003
11004/*
11005 * "inputrestore()" function
11006 */
11007/*ARGSUSED*/
11008 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011009f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011010 typval_T *argvars;
11011 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011012{
11013 if (ga_userinput.ga_len > 0)
11014 {
11015 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011016 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
11017 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011018 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011019 }
11020 else if (p_verbose > 1)
11021 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000011022 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
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/*
11028 * "inputsave()" function
11029 */
11030/*ARGSUSED*/
11031 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011032f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011033 typval_T *argvars;
11034 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011035{
11036 /* Add an entry to the stack of typehead storage. */
11037 if (ga_grow(&ga_userinput, 1) == OK)
11038 {
11039 save_typeahead((tasave_T *)(ga_userinput.ga_data)
11040 + ga_userinput.ga_len);
11041 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011042 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011043 }
11044 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011045 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011046}
11047
11048/*
11049 * "inputsecret()" function
11050 */
11051 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011052f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011053 typval_T *argvars;
11054 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011055{
11056 ++cmdline_star;
11057 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011058 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011059 --cmdline_star;
11060 --inputsecret_flag;
11061}
11062
11063/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011064 * "insert()" function
11065 */
11066 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011067f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011068 typval_T *argvars;
11069 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011070{
11071 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011072 listitem_T *item;
11073 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011074 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011075
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011076 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011077 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011078 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011079 else if ((l = argvars[0].vval.v_list) != NULL
11080 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011081 {
11082 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011083 before = get_tv_number_chk(&argvars[2], &error);
11084 if (error)
11085 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011086
Bram Moolenaar758711c2005-02-02 23:11:38 +000011087 if (before == l->lv_len)
11088 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011089 else
11090 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011091 item = list_find(l, before);
11092 if (item == NULL)
11093 {
11094 EMSGN(_(e_listidx), before);
11095 l = NULL;
11096 }
11097 }
11098 if (l != NULL)
11099 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011100 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011101 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011102 }
11103 }
11104}
11105
11106/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011107 * "isdirectory()" function
11108 */
11109 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011110f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011111 typval_T *argvars;
11112 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011113{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011114 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011115}
11116
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011117/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011118 * "islocked()" function
11119 */
11120 static void
11121f_islocked(argvars, rettv)
11122 typval_T *argvars;
11123 typval_T *rettv;
11124{
11125 lval_T lv;
11126 char_u *end;
11127 dictitem_T *di;
11128
11129 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000011130 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
11131 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011132 if (end != NULL && lv.ll_name != NULL)
11133 {
11134 if (*end != NUL)
11135 EMSG(_(e_trailing));
11136 else
11137 {
11138 if (lv.ll_tv == NULL)
11139 {
11140 if (check_changedtick(lv.ll_name))
11141 rettv->vval.v_number = 1; /* always locked */
11142 else
11143 {
11144 di = find_var(lv.ll_name, NULL);
11145 if (di != NULL)
11146 {
11147 /* Consider a variable locked when:
11148 * 1. the variable itself is locked
11149 * 2. the value of the variable is locked.
11150 * 3. the List or Dict value is locked.
11151 */
11152 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
11153 || tv_islocked(&di->di_tv));
11154 }
11155 }
11156 }
11157 else if (lv.ll_range)
11158 EMSG(_("E745: Range not allowed"));
11159 else if (lv.ll_newkey != NULL)
11160 EMSG2(_(e_dictkey), lv.ll_newkey);
11161 else if (lv.ll_list != NULL)
11162 /* List item. */
11163 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
11164 else
11165 /* Dictionary item. */
11166 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
11167 }
11168 }
11169
11170 clear_lval(&lv);
11171}
11172
Bram Moolenaar33570922005-01-25 22:26:29 +000011173static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011174
11175/*
11176 * Turn a dict into a list:
11177 * "what" == 0: list of keys
11178 * "what" == 1: list of values
11179 * "what" == 2: list of items
11180 */
11181 static void
11182dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000011183 typval_T *argvars;
11184 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011185 int what;
11186{
Bram Moolenaar33570922005-01-25 22:26:29 +000011187 list_T *l;
11188 list_T *l2;
11189 dictitem_T *di;
11190 hashitem_T *hi;
11191 listitem_T *li;
11192 listitem_T *li2;
11193 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011194 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011195
11196 rettv->vval.v_number = 0;
11197 if (argvars[0].v_type != VAR_DICT)
11198 {
11199 EMSG(_(e_dictreq));
11200 return;
11201 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011202 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011203 return;
11204
11205 l = list_alloc();
11206 if (l == NULL)
11207 return;
11208 rettv->v_type = VAR_LIST;
11209 rettv->vval.v_list = l;
11210 ++l->lv_refcount;
11211
Bram Moolenaar33570922005-01-25 22:26:29 +000011212 todo = d->dv_hashtab.ht_used;
11213 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011214 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011215 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011216 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011217 --todo;
11218 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011219
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011220 li = listitem_alloc();
11221 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011222 break;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011223 list_append(l, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011224
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011225 if (what == 0)
11226 {
11227 /* keys() */
11228 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011229 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011230 li->li_tv.vval.v_string = vim_strsave(di->di_key);
11231 }
11232 else if (what == 1)
11233 {
11234 /* values() */
11235 copy_tv(&di->di_tv, &li->li_tv);
11236 }
11237 else
11238 {
11239 /* items() */
11240 l2 = list_alloc();
11241 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011242 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011243 li->li_tv.vval.v_list = l2;
11244 if (l2 == NULL)
11245 break;
11246 ++l2->lv_refcount;
11247
11248 li2 = listitem_alloc();
11249 if (li2 == NULL)
11250 break;
11251 list_append(l2, li2);
11252 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011253 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011254 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
11255
11256 li2 = listitem_alloc();
11257 if (li2 == NULL)
11258 break;
11259 list_append(l2, li2);
11260 copy_tv(&di->di_tv, &li2->li_tv);
11261 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011262 }
11263 }
11264}
11265
11266/*
11267 * "items(dict)" function
11268 */
11269 static void
11270f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011271 typval_T *argvars;
11272 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011273{
11274 dict_list(argvars, rettv, 2);
11275}
11276
Bram Moolenaar071d4272004-06-13 20:20:40 +000011277/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011278 * "join()" function
11279 */
11280 static void
11281f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011282 typval_T *argvars;
11283 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011284{
11285 garray_T ga;
11286 char_u *sep;
11287
11288 rettv->vval.v_number = 0;
11289 if (argvars[0].v_type != VAR_LIST)
11290 {
11291 EMSG(_(e_listreq));
11292 return;
11293 }
11294 if (argvars[0].vval.v_list == NULL)
11295 return;
11296 if (argvars[1].v_type == VAR_UNKNOWN)
11297 sep = (char_u *)" ";
11298 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011299 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011300
11301 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011302
11303 if (sep != NULL)
11304 {
11305 ga_init2(&ga, (int)sizeof(char), 80);
11306 list_join(&ga, argvars[0].vval.v_list, sep, TRUE);
11307 ga_append(&ga, NUL);
11308 rettv->vval.v_string = (char_u *)ga.ga_data;
11309 }
11310 else
11311 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011312}
11313
11314/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011315 * "keys()" function
11316 */
11317 static void
11318f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011319 typval_T *argvars;
11320 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011321{
11322 dict_list(argvars, rettv, 0);
11323}
11324
11325/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011326 * "last_buffer_nr()" function.
11327 */
11328/*ARGSUSED*/
11329 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011330f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011331 typval_T *argvars;
11332 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011333{
11334 int n = 0;
11335 buf_T *buf;
11336
11337 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
11338 if (n < buf->b_fnum)
11339 n = buf->b_fnum;
11340
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011341 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011342}
11343
11344/*
11345 * "len()" function
11346 */
11347 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011348f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011349 typval_T *argvars;
11350 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011351{
11352 switch (argvars[0].v_type)
11353 {
11354 case VAR_STRING:
11355 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011356 rettv->vval.v_number = (varnumber_T)STRLEN(
11357 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011358 break;
11359 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011360 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011361 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011362 case VAR_DICT:
11363 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
11364 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011365 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011366 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011367 break;
11368 }
11369}
11370
Bram Moolenaar33570922005-01-25 22:26:29 +000011371static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011372
11373 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011374libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011375 typval_T *argvars;
11376 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011377 int type;
11378{
11379#ifdef FEAT_LIBCALL
11380 char_u *string_in;
11381 char_u **string_result;
11382 int nr_result;
11383#endif
11384
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011385 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011386 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011387 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011388 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011389 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011390
11391 if (check_restricted() || check_secure())
11392 return;
11393
11394#ifdef FEAT_LIBCALL
11395 /* The first two args must be strings, otherwise its meaningless */
11396 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
11397 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011398 string_in = NULL;
11399 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011400 string_in = argvars[2].vval.v_string;
11401 if (type == VAR_NUMBER)
11402 string_result = NULL;
11403 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011404 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011405 if (mch_libcall(argvars[0].vval.v_string,
11406 argvars[1].vval.v_string,
11407 string_in,
11408 argvars[2].vval.v_number,
11409 string_result,
11410 &nr_result) == OK
11411 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011412 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011413 }
11414#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011415}
11416
11417/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011418 * "libcall()" function
11419 */
11420 static void
11421f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011422 typval_T *argvars;
11423 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011424{
11425 libcall_common(argvars, rettv, VAR_STRING);
11426}
11427
11428/*
11429 * "libcallnr()" function
11430 */
11431 static void
11432f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011433 typval_T *argvars;
11434 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011435{
11436 libcall_common(argvars, rettv, VAR_NUMBER);
11437}
11438
11439/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011440 * "line(string)" function
11441 */
11442 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011443f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011444 typval_T *argvars;
11445 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011446{
11447 linenr_T lnum = 0;
11448 pos_T *fp;
11449
11450 fp = var2fpos(&argvars[0], TRUE);
11451 if (fp != NULL)
11452 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011453 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011454}
11455
11456/*
11457 * "line2byte(lnum)" function
11458 */
11459/*ARGSUSED*/
11460 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011461f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011462 typval_T *argvars;
11463 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011464{
11465#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011466 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011467#else
11468 linenr_T lnum;
11469
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011470 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011471 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011472 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011473 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011474 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
11475 if (rettv->vval.v_number >= 0)
11476 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011477#endif
11478}
11479
11480/*
11481 * "lispindent(lnum)" function
11482 */
11483 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011484f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011485 typval_T *argvars;
11486 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011487{
11488#ifdef FEAT_LISP
11489 pos_T pos;
11490 linenr_T lnum;
11491
11492 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011493 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011494 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11495 {
11496 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011497 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011498 curwin->w_cursor = pos;
11499 }
11500 else
11501#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011502 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011503}
11504
11505/*
11506 * "localtime()" function
11507 */
11508/*ARGSUSED*/
11509 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011510f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011511 typval_T *argvars;
11512 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011513{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011514 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011515}
11516
Bram Moolenaar33570922005-01-25 22:26:29 +000011517static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011518
11519 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011520get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000011521 typval_T *argvars;
11522 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011523 int exact;
11524{
11525 char_u *keys;
11526 char_u *which;
11527 char_u buf[NUMBUFLEN];
11528 char_u *keys_buf = NULL;
11529 char_u *rhs;
11530 int mode;
11531 garray_T ga;
11532
11533 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011534 rettv->v_type = VAR_STRING;
11535 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011536
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011537 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011538 if (*keys == NUL)
11539 return;
11540
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011541 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011542 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011543 else
11544 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011545 if (which == NULL)
11546 return;
11547
Bram Moolenaar071d4272004-06-13 20:20:40 +000011548 mode = get_map_mode(&which, 0);
11549
11550 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000011551 rhs = check_map(keys, mode, exact, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011552 vim_free(keys_buf);
11553 if (rhs != NULL)
11554 {
11555 ga_init(&ga);
11556 ga.ga_itemsize = 1;
11557 ga.ga_growsize = 40;
11558
11559 while (*rhs != NUL)
11560 ga_concat(&ga, str2special(&rhs, FALSE));
11561
11562 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011563 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011564 }
11565}
11566
11567/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011568 * "map()" function
11569 */
11570 static void
11571f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011572 typval_T *argvars;
11573 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011574{
11575 filter_map(argvars, rettv, TRUE);
11576}
11577
11578/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011579 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011580 */
11581 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011582f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011583 typval_T *argvars;
11584 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011585{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011586 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011587}
11588
11589/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011590 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011591 */
11592 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011593f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011594 typval_T *argvars;
11595 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011596{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011597 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011598}
11599
Bram Moolenaar33570922005-01-25 22:26:29 +000011600static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011601
11602 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011603find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011604 typval_T *argvars;
11605 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011606 int type;
11607{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011608 char_u *str = NULL;
11609 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011610 char_u *pat;
11611 regmatch_T regmatch;
11612 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011613 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011614 char_u *save_cpo;
11615 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011616 long nth = 1;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011617 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011618 list_T *l = NULL;
11619 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011620 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011621 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011622
11623 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11624 save_cpo = p_cpo;
11625 p_cpo = (char_u *)"";
11626
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011627 rettv->vval.v_number = -1;
11628 if (type == 3)
11629 {
11630 /* return empty list when there are no matches */
11631 if ((rettv->vval.v_list = list_alloc()) == NULL)
11632 goto theend;
11633 rettv->v_type = VAR_LIST;
11634 ++rettv->vval.v_list->lv_refcount;
11635 }
11636 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011637 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011638 rettv->v_type = VAR_STRING;
11639 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011641
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011642 if (argvars[0].v_type == VAR_LIST)
11643 {
11644 if ((l = argvars[0].vval.v_list) == NULL)
11645 goto theend;
11646 li = l->lv_first;
11647 }
11648 else
11649 expr = str = get_tv_string(&argvars[0]);
11650
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011651 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11652 if (pat == NULL)
11653 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011654
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011655 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011656 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011657 int error = FALSE;
11658
11659 start = get_tv_number_chk(&argvars[2], &error);
11660 if (error)
11661 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011662 if (l != NULL)
11663 {
11664 li = list_find(l, start);
11665 if (li == NULL)
11666 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000011667 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011668 }
11669 else
11670 {
11671 if (start < 0)
11672 start = 0;
11673 if (start > (long)STRLEN(str))
11674 goto theend;
11675 str += start;
11676 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011677
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011678 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011679 nth = get_tv_number_chk(&argvars[3], &error);
11680 if (error)
11681 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011682 }
11683
11684 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11685 if (regmatch.regprog != NULL)
11686 {
11687 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011688
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000011689 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011690 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011691 if (l != NULL)
11692 {
11693 if (li == NULL)
11694 {
11695 match = FALSE;
11696 break;
11697 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011698 vim_free(tofree);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011699 str = echo_string(&li->li_tv, &tofree, strbuf);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011700 if (str == NULL)
11701 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011702 }
11703
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011704 match = vim_regexec_nl(&regmatch, str, (colnr_T)0);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011705
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011706 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011707 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011708 if (l == NULL && !match)
11709 break;
11710
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011711 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011712 if (l != NULL)
11713 {
11714 li = li->li_next;
11715 ++idx;
11716 }
11717 else
11718 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011719#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011720 str = regmatch.startp[0] + (*mb_ptr2len)(regmatch.startp[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011721#else
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011722 str = regmatch.startp[0] + 1;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011723#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011724 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011725 }
11726
11727 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011728 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011729 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011730 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011731 int i;
11732
11733 /* return list with matched string and submatches */
11734 for (i = 0; i < NSUBEXP; ++i)
11735 {
11736 if (regmatch.endp[i] == NULL)
11737 break;
Bram Moolenaar4463f292005-09-25 22:20:24 +000011738 if (list_append_string(rettv->vval.v_list,
11739 regmatch.startp[i],
11740 (int)(regmatch.endp[i] - regmatch.startp[i]))
11741 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011742 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011743 }
11744 }
11745 else if (type == 2)
11746 {
11747 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011748 if (l != NULL)
11749 copy_tv(&li->li_tv, rettv);
11750 else
11751 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000011752 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011753 }
11754 else if (l != NULL)
11755 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011756 else
11757 {
11758 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011759 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011760 (varnumber_T)(regmatch.startp[0] - str);
11761 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011762 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011763 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011764 rettv->vval.v_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011765 }
11766 }
11767 vim_free(regmatch.regprog);
11768 }
11769
11770theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011771 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011772 p_cpo = save_cpo;
11773}
11774
11775/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011776 * "match()" function
11777 */
11778 static void
11779f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011780 typval_T *argvars;
11781 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011782{
11783 find_some_match(argvars, rettv, 1);
11784}
11785
11786/*
11787 * "matchend()" function
11788 */
11789 static void
11790f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011791 typval_T *argvars;
11792 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011793{
11794 find_some_match(argvars, rettv, 0);
11795}
11796
11797/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011798 * "matchlist()" function
11799 */
11800 static void
11801f_matchlist(argvars, rettv)
11802 typval_T *argvars;
11803 typval_T *rettv;
11804{
11805 find_some_match(argvars, rettv, 3);
11806}
11807
11808/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011809 * "matchstr()" function
11810 */
11811 static void
11812f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011813 typval_T *argvars;
11814 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011815{
11816 find_some_match(argvars, rettv, 2);
11817}
11818
Bram Moolenaar33570922005-01-25 22:26:29 +000011819static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011820
11821 static void
11822max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000011823 typval_T *argvars;
11824 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011825 int domax;
11826{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011827 long n = 0;
11828 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011829 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011830
11831 if (argvars[0].v_type == VAR_LIST)
11832 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011833 list_T *l;
11834 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011835
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011836 l = argvars[0].vval.v_list;
11837 if (l != NULL)
11838 {
11839 li = l->lv_first;
11840 if (li != NULL)
11841 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011842 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000011843 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011844 {
11845 li = li->li_next;
11846 if (li == NULL)
11847 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011848 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011849 if (domax ? i > n : i < n)
11850 n = i;
11851 }
11852 }
11853 }
11854 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011855 else if (argvars[0].v_type == VAR_DICT)
11856 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011857 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011858 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000011859 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011860 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011861
11862 d = argvars[0].vval.v_dict;
11863 if (d != NULL)
11864 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011865 todo = d->dv_hashtab.ht_used;
11866 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011867 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011868 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011869 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011870 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011871 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011872 if (first)
11873 {
11874 n = i;
11875 first = FALSE;
11876 }
11877 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011878 n = i;
11879 }
11880 }
11881 }
11882 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011883 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000011884 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011885 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011886}
11887
11888/*
11889 * "max()" function
11890 */
11891 static void
11892f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011893 typval_T *argvars;
11894 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011895{
11896 max_min(argvars, rettv, TRUE);
11897}
11898
11899/*
11900 * "min()" function
11901 */
11902 static void
11903f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011904 typval_T *argvars;
11905 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011906{
11907 max_min(argvars, rettv, FALSE);
11908}
11909
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011910static int mkdir_recurse __ARGS((char_u *dir, int prot));
11911
11912/*
11913 * Create the directory in which "dir" is located, and higher levels when
11914 * needed.
11915 */
11916 static int
11917mkdir_recurse(dir, prot)
11918 char_u *dir;
11919 int prot;
11920{
11921 char_u *p;
11922 char_u *updir;
11923 int r = FAIL;
11924
11925 /* Get end of directory name in "dir".
11926 * We're done when it's "/" or "c:/". */
11927 p = gettail_sep(dir);
11928 if (p <= get_past_head(dir))
11929 return OK;
11930
11931 /* If the directory exists we're done. Otherwise: create it.*/
11932 updir = vim_strnsave(dir, (int)(p - dir));
11933 if (updir == NULL)
11934 return FAIL;
11935 if (mch_isdir(updir))
11936 r = OK;
11937 else if (mkdir_recurse(updir, prot) == OK)
11938 r = vim_mkdir_emsg(updir, prot);
11939 vim_free(updir);
11940 return r;
11941}
11942
11943#ifdef vim_mkdir
11944/*
11945 * "mkdir()" function
11946 */
11947 static void
11948f_mkdir(argvars, rettv)
11949 typval_T *argvars;
11950 typval_T *rettv;
11951{
11952 char_u *dir;
11953 char_u buf[NUMBUFLEN];
11954 int prot = 0755;
11955
11956 rettv->vval.v_number = FAIL;
11957 if (check_restricted() || check_secure())
11958 return;
11959
11960 dir = get_tv_string_buf(&argvars[0], buf);
11961 if (argvars[1].v_type != VAR_UNKNOWN)
11962 {
11963 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011964 prot = get_tv_number_chk(&argvars[2], NULL);
11965 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011966 mkdir_recurse(dir, prot);
11967 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011968 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011969}
11970#endif
11971
Bram Moolenaar0d660222005-01-07 21:51:51 +000011972/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011973 * "mode()" function
11974 */
11975/*ARGSUSED*/
11976 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011977f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011978 typval_T *argvars;
11979 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011980{
11981 char_u buf[2];
11982
11983#ifdef FEAT_VISUAL
11984 if (VIsual_active)
11985 {
11986 if (VIsual_select)
11987 buf[0] = VIsual_mode + 's' - 'v';
11988 else
11989 buf[0] = VIsual_mode;
11990 }
11991 else
11992#endif
11993 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
11994 buf[0] = 'r';
11995 else if (State & INSERT)
11996 {
11997 if (State & REPLACE_FLAG)
11998 buf[0] = 'R';
11999 else
12000 buf[0] = 'i';
12001 }
12002 else if (State & CMDLINE)
12003 buf[0] = 'c';
12004 else
12005 buf[0] = 'n';
12006
12007 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012008 rettv->vval.v_string = vim_strsave(buf);
12009 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012010}
12011
12012/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012013 * "nextnonblank()" function
12014 */
12015 static void
12016f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012017 typval_T *argvars;
12018 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012019{
12020 linenr_T lnum;
12021
12022 for (lnum = get_tv_lnum(argvars); ; ++lnum)
12023 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012024 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012025 {
12026 lnum = 0;
12027 break;
12028 }
12029 if (*skipwhite(ml_get(lnum)) != NUL)
12030 break;
12031 }
12032 rettv->vval.v_number = lnum;
12033}
12034
12035/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012036 * "nr2char()" function
12037 */
12038 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012039f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012040 typval_T *argvars;
12041 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012042{
12043 char_u buf[NUMBUFLEN];
12044
12045#ifdef FEAT_MBYTE
12046 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012047 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012048 else
12049#endif
12050 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012051 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012052 buf[1] = NUL;
12053 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012054 rettv->v_type = VAR_STRING;
12055 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012056}
12057
12058/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012059 * "prevnonblank()" function
12060 */
12061 static void
12062f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012063 typval_T *argvars;
12064 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012065{
12066 linenr_T lnum;
12067
12068 lnum = get_tv_lnum(argvars);
12069 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
12070 lnum = 0;
12071 else
12072 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
12073 --lnum;
12074 rettv->vval.v_number = lnum;
12075}
12076
Bram Moolenaara6c840d2005-08-22 22:59:46 +000012077#ifdef HAVE_STDARG_H
12078/* This dummy va_list is here because:
12079 * - passing a NULL pointer doesn't work when va_list isn't a pointer
12080 * - locally in the function results in a "used before set" warning
12081 * - using va_start() to initialize it gives "function with fixed args" error */
12082static va_list ap;
12083#endif
12084
Bram Moolenaar8c711452005-01-14 21:53:12 +000012085/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012086 * "printf()" function
12087 */
12088 static void
12089f_printf(argvars, rettv)
12090 typval_T *argvars;
12091 typval_T *rettv;
12092{
12093 rettv->v_type = VAR_STRING;
12094 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000012095#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012096 {
12097 char_u buf[NUMBUFLEN];
12098 int len;
12099 char_u *s;
12100 int saved_did_emsg = did_emsg;
12101 char *fmt;
12102
12103 /* Get the required length, allocate the buffer and do it for real. */
12104 did_emsg = FALSE;
12105 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012106 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012107 if (!did_emsg)
12108 {
12109 s = alloc(len + 1);
12110 if (s != NULL)
12111 {
12112 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012113 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012114 }
12115 }
12116 did_emsg |= saved_did_emsg;
12117 }
12118#endif
12119}
12120
12121/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012122 * "range()" function
12123 */
12124 static void
12125f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012126 typval_T *argvars;
12127 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012128{
12129 long start;
12130 long end;
12131 long stride = 1;
12132 long i;
Bram Moolenaar33570922005-01-25 22:26:29 +000012133 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012134 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012135
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012136 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012137 if (argvars[1].v_type == VAR_UNKNOWN)
12138 {
12139 end = start - 1;
12140 start = 0;
12141 }
12142 else
12143 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012144 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012145 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012146 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012147 }
12148
12149 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012150 if (error)
12151 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000012152 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012153 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000012154 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012155 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012156 else
12157 {
12158 l = list_alloc();
12159 if (l != NULL)
12160 {
12161 rettv->v_type = VAR_LIST;
12162 rettv->vval.v_list = l;
12163 ++l->lv_refcount;
12164
12165 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012166 if (list_append_number(l, (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012167 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012168 }
12169 }
12170}
12171
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012172/*
12173 * "readfile()" function
12174 */
12175 static void
12176f_readfile(argvars, rettv)
12177 typval_T *argvars;
12178 typval_T *rettv;
12179{
12180 int binary = FALSE;
12181 char_u *fname;
12182 FILE *fd;
12183 list_T *l;
12184 listitem_T *li;
12185#define FREAD_SIZE 200 /* optimized for text lines */
12186 char_u buf[FREAD_SIZE];
12187 int readlen; /* size of last fread() */
12188 int buflen; /* nr of valid chars in buf[] */
12189 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
12190 int tolist; /* first byte in buf[] still to be put in list */
12191 int chop; /* how many CR to chop off */
12192 char_u *prev = NULL; /* previously read bytes, if any */
12193 int prevlen = 0; /* length of "prev" if not NULL */
12194 char_u *s;
12195 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012196 long maxline = MAXLNUM;
12197 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012198
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012199 if (argvars[1].v_type != VAR_UNKNOWN)
12200 {
12201 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
12202 binary = TRUE;
12203 if (argvars[2].v_type != VAR_UNKNOWN)
12204 maxline = get_tv_number(&argvars[2]);
12205 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012206
12207 l = list_alloc();
12208 if (l == NULL)
12209 return;
12210 rettv->v_type = VAR_LIST;
12211 rettv->vval.v_list = l;
12212 l->lv_refcount = 1;
12213
12214 /* Always open the file in binary mode, library functions have a mind of
12215 * their own about CR-LF conversion. */
12216 fname = get_tv_string(&argvars[0]);
12217 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
12218 {
12219 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
12220 return;
12221 }
12222
12223 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012224 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012225 {
12226 readlen = fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
12227 buflen = filtd + readlen;
12228 tolist = 0;
12229 for ( ; filtd < buflen || readlen <= 0; ++filtd)
12230 {
12231 if (buf[filtd] == '\n' || readlen <= 0)
12232 {
12233 /* Only when in binary mode add an empty list item when the
12234 * last line ends in a '\n'. */
12235 if (!binary && readlen == 0 && filtd == 0)
12236 break;
12237
12238 /* Found end-of-line or end-of-file: add a text line to the
12239 * list. */
12240 chop = 0;
12241 if (!binary)
12242 while (filtd - chop - 1 >= tolist
12243 && buf[filtd - chop - 1] == '\r')
12244 ++chop;
12245 len = filtd - tolist - chop;
12246 if (prev == NULL)
12247 s = vim_strnsave(buf + tolist, len);
12248 else
12249 {
12250 s = alloc((unsigned)(prevlen + len + 1));
12251 if (s != NULL)
12252 {
12253 mch_memmove(s, prev, prevlen);
12254 vim_free(prev);
12255 prev = NULL;
12256 mch_memmove(s + prevlen, buf + tolist, len);
12257 s[prevlen + len] = NUL;
12258 }
12259 }
12260 tolist = filtd + 1;
12261
12262 li = listitem_alloc();
12263 if (li == NULL)
12264 {
12265 vim_free(s);
12266 break;
12267 }
12268 li->li_tv.v_type = VAR_STRING;
12269 li->li_tv.v_lock = 0;
12270 li->li_tv.vval.v_string = s;
12271 list_append(l, li);
12272
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012273 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012274 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012275 if (readlen <= 0)
12276 break;
12277 }
12278 else if (buf[filtd] == NUL)
12279 buf[filtd] = '\n';
12280 }
12281 if (readlen <= 0)
12282 break;
12283
12284 if (tolist == 0)
12285 {
12286 /* "buf" is full, need to move text to an allocated buffer */
12287 if (prev == NULL)
12288 {
12289 prev = vim_strnsave(buf, buflen);
12290 prevlen = buflen;
12291 }
12292 else
12293 {
12294 s = alloc((unsigned)(prevlen + buflen));
12295 if (s != NULL)
12296 {
12297 mch_memmove(s, prev, prevlen);
12298 mch_memmove(s + prevlen, buf, buflen);
12299 vim_free(prev);
12300 prev = s;
12301 prevlen += buflen;
12302 }
12303 }
12304 filtd = 0;
12305 }
12306 else
12307 {
12308 mch_memmove(buf, buf + tolist, buflen - tolist);
12309 filtd -= tolist;
12310 }
12311 }
12312
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012313 /*
12314 * For a negative line count use only the lines at the end of the file,
12315 * free the rest.
12316 */
12317 if (maxline < 0)
12318 while (cnt > -maxline)
12319 {
12320 listitem_remove(l, l->lv_first);
12321 --cnt;
12322 }
12323
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012324 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012325 fclose(fd);
12326}
12327
12328
Bram Moolenaar0d660222005-01-07 21:51:51 +000012329#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
12330static void make_connection __ARGS((void));
12331static int check_connection __ARGS((void));
12332
12333 static void
12334make_connection()
12335{
12336 if (X_DISPLAY == NULL
12337# ifdef FEAT_GUI
12338 && !gui.in_use
12339# endif
12340 )
12341 {
12342 x_force_connect = TRUE;
12343 setup_term_clip();
12344 x_force_connect = FALSE;
12345 }
12346}
12347
12348 static int
12349check_connection()
12350{
12351 make_connection();
12352 if (X_DISPLAY == NULL)
12353 {
12354 EMSG(_("E240: No connection to Vim server"));
12355 return FAIL;
12356 }
12357 return OK;
12358}
12359#endif
12360
12361#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012362static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012363
12364 static void
12365remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000012366 typval_T *argvars;
12367 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012368 int expr;
12369{
12370 char_u *server_name;
12371 char_u *keys;
12372 char_u *r = NULL;
12373 char_u buf[NUMBUFLEN];
12374# ifdef WIN32
12375 HWND w;
12376# else
12377 Window w;
12378# endif
12379
12380 if (check_restricted() || check_secure())
12381 return;
12382
12383# ifdef FEAT_X11
12384 if (check_connection() == FAIL)
12385 return;
12386# endif
12387
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012388 server_name = get_tv_string_chk(&argvars[0]);
12389 if (server_name == NULL)
12390 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012391 keys = get_tv_string_buf(&argvars[1], buf);
12392# ifdef WIN32
12393 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
12394# else
12395 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
12396 < 0)
12397# endif
12398 {
12399 if (r != NULL)
12400 EMSG(r); /* sending worked but evaluation failed */
12401 else
12402 EMSG2(_("E241: Unable to send to %s"), server_name);
12403 return;
12404 }
12405
12406 rettv->vval.v_string = r;
12407
12408 if (argvars[2].v_type != VAR_UNKNOWN)
12409 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012410 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000012411 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012412 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012413
12414 sprintf((char *)str, "0x%x", (unsigned int)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000012415 v.di_tv.v_type = VAR_STRING;
12416 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012417 idvar = get_tv_string_chk(&argvars[2]);
12418 if (idvar != NULL)
12419 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012420 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012421 }
12422}
12423#endif
12424
12425/*
12426 * "remote_expr()" function
12427 */
12428/*ARGSUSED*/
12429 static void
12430f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012431 typval_T *argvars;
12432 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012433{
12434 rettv->v_type = VAR_STRING;
12435 rettv->vval.v_string = NULL;
12436#ifdef FEAT_CLIENTSERVER
12437 remote_common(argvars, rettv, TRUE);
12438#endif
12439}
12440
12441/*
12442 * "remote_foreground()" function
12443 */
12444/*ARGSUSED*/
12445 static void
12446f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012447 typval_T *argvars;
12448 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012449{
12450 rettv->vval.v_number = 0;
12451#ifdef FEAT_CLIENTSERVER
12452# ifdef WIN32
12453 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012454 {
12455 char_u *server_name = get_tv_string_chk(&argvars[0]);
12456
12457 if (server_name != NULL)
12458 serverForeground(server_name);
12459 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012460# else
12461 /* Send a foreground() expression to the server. */
12462 argvars[1].v_type = VAR_STRING;
12463 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
12464 argvars[2].v_type = VAR_UNKNOWN;
12465 remote_common(argvars, rettv, TRUE);
12466 vim_free(argvars[1].vval.v_string);
12467# endif
12468#endif
12469}
12470
12471/*ARGSUSED*/
12472 static void
12473f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012474 typval_T *argvars;
12475 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012476{
12477#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012478 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012479 char_u *s = NULL;
12480# ifdef WIN32
12481 int n = 0;
12482# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012483 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012484
12485 if (check_restricted() || check_secure())
12486 {
12487 rettv->vval.v_number = -1;
12488 return;
12489 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012490 serverid = get_tv_string_chk(&argvars[0]);
12491 if (serverid == NULL)
12492 {
12493 rettv->vval.v_number = -1;
12494 return; /* type error; errmsg already given */
12495 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012496# ifdef WIN32
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012497 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012498 if (n == 0)
12499 rettv->vval.v_number = -1;
12500 else
12501 {
12502 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
12503 rettv->vval.v_number = (s != NULL);
12504 }
12505# else
12506 rettv->vval.v_number = 0;
12507 if (check_connection() == FAIL)
12508 return;
12509
12510 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012511 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012512# endif
12513
12514 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
12515 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012516 char_u *retvar;
12517
Bram Moolenaar33570922005-01-25 22:26:29 +000012518 v.di_tv.v_type = VAR_STRING;
12519 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012520 retvar = get_tv_string_chk(&argvars[1]);
12521 if (retvar != NULL)
12522 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012523 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012524 }
12525#else
12526 rettv->vval.v_number = -1;
12527#endif
12528}
12529
12530/*ARGSUSED*/
12531 static void
12532f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012533 typval_T *argvars;
12534 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012535{
12536 char_u *r = NULL;
12537
12538#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012539 char_u *serverid = get_tv_string_chk(&argvars[0]);
12540
12541 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000012542 {
12543# ifdef WIN32
12544 /* The server's HWND is encoded in the 'id' parameter */
12545 int n = 0;
12546
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012547 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012548 if (n != 0)
12549 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
12550 if (r == NULL)
12551# else
12552 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012553 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012554# endif
12555 EMSG(_("E277: Unable to read a server reply"));
12556 }
12557#endif
12558 rettv->v_type = VAR_STRING;
12559 rettv->vval.v_string = r;
12560}
12561
12562/*
12563 * "remote_send()" function
12564 */
12565/*ARGSUSED*/
12566 static void
12567f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012568 typval_T *argvars;
12569 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012570{
12571 rettv->v_type = VAR_STRING;
12572 rettv->vval.v_string = NULL;
12573#ifdef FEAT_CLIENTSERVER
12574 remote_common(argvars, rettv, FALSE);
12575#endif
12576}
12577
12578/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012579 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012580 */
12581 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012582f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012583 typval_T *argvars;
12584 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012585{
Bram Moolenaar33570922005-01-25 22:26:29 +000012586 list_T *l;
12587 listitem_T *item, *item2;
12588 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012589 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012590 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012591 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000012592 dict_T *d;
12593 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012594
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012595 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012596 if (argvars[0].v_type == VAR_DICT)
12597 {
12598 if (argvars[2].v_type != VAR_UNKNOWN)
12599 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012600 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar758711c2005-02-02 23:11:38 +000012601 && !tv_check_lock(d->dv_lock, (char_u *)"remove()"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000012602 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012603 key = get_tv_string_chk(&argvars[1]);
12604 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012605 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012606 di = dict_find(d, key, -1);
12607 if (di == NULL)
12608 EMSG2(_(e_dictkey), key);
12609 else
12610 {
12611 *rettv = di->di_tv;
12612 init_tv(&di->di_tv);
12613 dictitem_remove(d, di);
12614 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012615 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012616 }
12617 }
12618 else if (argvars[0].v_type != VAR_LIST)
12619 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012620 else if ((l = argvars[0].vval.v_list) != NULL
12621 && !tv_check_lock(l->lv_lock, (char_u *)"remove()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012622 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012623 int error = FALSE;
12624
12625 idx = get_tv_number_chk(&argvars[1], &error);
12626 if (error)
12627 ; /* type error: do nothing, errmsg already given */
12628 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012629 EMSGN(_(e_listidx), idx);
12630 else
12631 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012632 if (argvars[2].v_type == VAR_UNKNOWN)
12633 {
12634 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012635 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012636 *rettv = item->li_tv;
12637 vim_free(item);
12638 }
12639 else
12640 {
12641 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012642 end = get_tv_number_chk(&argvars[2], &error);
12643 if (error)
12644 ; /* type error: do nothing */
12645 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012646 EMSGN(_(e_listidx), end);
12647 else
12648 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012649 int cnt = 0;
12650
12651 for (li = item; li != NULL; li = li->li_next)
12652 {
12653 ++cnt;
12654 if (li == item2)
12655 break;
12656 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012657 if (li == NULL) /* didn't find "item2" after "item" */
12658 EMSG(_(e_invrange));
12659 else
12660 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012661 list_remove(l, item, item2);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012662 l = list_alloc();
12663 if (l != NULL)
12664 {
12665 rettv->v_type = VAR_LIST;
12666 rettv->vval.v_list = l;
12667 l->lv_first = item;
12668 l->lv_last = item2;
12669 l->lv_refcount = 1;
12670 item->li_prev = NULL;
12671 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012672 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012673 }
12674 }
12675 }
12676 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012677 }
12678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012679}
12680
12681/*
12682 * "rename({from}, {to})" function
12683 */
12684 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012685f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012686 typval_T *argvars;
12687 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012688{
12689 char_u buf[NUMBUFLEN];
12690
12691 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012692 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012693 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012694 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
12695 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012696}
12697
12698/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012699 * "repeat()" function
12700 */
12701/*ARGSUSED*/
12702 static void
12703f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012704 typval_T *argvars;
12705 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012706{
12707 char_u *p;
12708 int n;
12709 int slen;
12710 int len;
12711 char_u *r;
12712 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000012713 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012714
12715 n = get_tv_number(&argvars[1]);
12716 if (argvars[0].v_type == VAR_LIST)
12717 {
12718 l = list_alloc();
12719 if (l != NULL && argvars[0].vval.v_list != NULL)
12720 {
12721 l->lv_refcount = 1;
12722 while (n-- > 0)
12723 if (list_extend(l, argvars[0].vval.v_list, NULL) == FAIL)
12724 break;
12725 }
12726 rettv->v_type = VAR_LIST;
12727 rettv->vval.v_list = l;
12728 }
12729 else
12730 {
12731 p = get_tv_string(&argvars[0]);
12732 rettv->v_type = VAR_STRING;
12733 rettv->vval.v_string = NULL;
12734
12735 slen = (int)STRLEN(p);
12736 len = slen * n;
12737 if (len <= 0)
12738 return;
12739
12740 r = alloc(len + 1);
12741 if (r != NULL)
12742 {
12743 for (i = 0; i < n; i++)
12744 mch_memmove(r + i * slen, p, (size_t)slen);
12745 r[len] = NUL;
12746 }
12747
12748 rettv->vval.v_string = r;
12749 }
12750}
12751
12752/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012753 * "resolve()" function
12754 */
12755 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012756f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012757 typval_T *argvars;
12758 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012759{
12760 char_u *p;
12761
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012762 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012763#ifdef FEAT_SHORTCUT
12764 {
12765 char_u *v = NULL;
12766
12767 v = mch_resolve_shortcut(p);
12768 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012769 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012770 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012771 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012772 }
12773#else
12774# ifdef HAVE_READLINK
12775 {
12776 char_u buf[MAXPATHL + 1];
12777 char_u *cpy;
12778 int len;
12779 char_u *remain = NULL;
12780 char_u *q;
12781 int is_relative_to_current = FALSE;
12782 int has_trailing_pathsep = FALSE;
12783 int limit = 100;
12784
12785 p = vim_strsave(p);
12786
12787 if (p[0] == '.' && (vim_ispathsep(p[1])
12788 || (p[1] == '.' && (vim_ispathsep(p[2])))))
12789 is_relative_to_current = TRUE;
12790
12791 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000012792 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012793 has_trailing_pathsep = TRUE;
12794
12795 q = getnextcomp(p);
12796 if (*q != NUL)
12797 {
12798 /* Separate the first path component in "p", and keep the
12799 * remainder (beginning with the path separator). */
12800 remain = vim_strsave(q - 1);
12801 q[-1] = NUL;
12802 }
12803
12804 for (;;)
12805 {
12806 for (;;)
12807 {
12808 len = readlink((char *)p, (char *)buf, MAXPATHL);
12809 if (len <= 0)
12810 break;
12811 buf[len] = NUL;
12812
12813 if (limit-- == 0)
12814 {
12815 vim_free(p);
12816 vim_free(remain);
12817 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012818 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012819 goto fail;
12820 }
12821
12822 /* Ensure that the result will have a trailing path separator
12823 * if the argument has one. */
12824 if (remain == NULL && has_trailing_pathsep)
12825 add_pathsep(buf);
12826
12827 /* Separate the first path component in the link value and
12828 * concatenate the remainders. */
12829 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
12830 if (*q != NUL)
12831 {
12832 if (remain == NULL)
12833 remain = vim_strsave(q - 1);
12834 else
12835 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012836 cpy = vim_strnsave(q-1, STRLEN(q-1) + STRLEN(remain));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012837 if (cpy != NULL)
12838 {
12839 STRCAT(cpy, remain);
12840 vim_free(remain);
12841 remain = cpy;
12842 }
12843 }
12844 q[-1] = NUL;
12845 }
12846
12847 q = gettail(p);
12848 if (q > p && *q == NUL)
12849 {
12850 /* Ignore trailing path separator. */
12851 q[-1] = NUL;
12852 q = gettail(p);
12853 }
12854 if (q > p && !mch_isFullName(buf))
12855 {
12856 /* symlink is relative to directory of argument */
12857 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
12858 if (cpy != NULL)
12859 {
12860 STRCPY(cpy, p);
12861 STRCPY(gettail(cpy), buf);
12862 vim_free(p);
12863 p = cpy;
12864 }
12865 }
12866 else
12867 {
12868 vim_free(p);
12869 p = vim_strsave(buf);
12870 }
12871 }
12872
12873 if (remain == NULL)
12874 break;
12875
12876 /* Append the first path component of "remain" to "p". */
12877 q = getnextcomp(remain + 1);
12878 len = q - remain - (*q != NUL);
12879 cpy = vim_strnsave(p, STRLEN(p) + len);
12880 if (cpy != NULL)
12881 {
12882 STRNCAT(cpy, remain, len);
12883 vim_free(p);
12884 p = cpy;
12885 }
12886 /* Shorten "remain". */
12887 if (*q != NUL)
12888 STRCPY(remain, q - 1);
12889 else
12890 {
12891 vim_free(remain);
12892 remain = NULL;
12893 }
12894 }
12895
12896 /* If the result is a relative path name, make it explicitly relative to
12897 * the current directory if and only if the argument had this form. */
12898 if (!vim_ispathsep(*p))
12899 {
12900 if (is_relative_to_current
12901 && *p != NUL
12902 && !(p[0] == '.'
12903 && (p[1] == NUL
12904 || vim_ispathsep(p[1])
12905 || (p[1] == '.'
12906 && (p[2] == NUL
12907 || vim_ispathsep(p[2]))))))
12908 {
12909 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012910 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012911 if (cpy != NULL)
12912 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012913 vim_free(p);
12914 p = cpy;
12915 }
12916 }
12917 else if (!is_relative_to_current)
12918 {
12919 /* Strip leading "./". */
12920 q = p;
12921 while (q[0] == '.' && vim_ispathsep(q[1]))
12922 q += 2;
12923 if (q > p)
12924 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
12925 }
12926 }
12927
12928 /* Ensure that the result will have no trailing path separator
12929 * if the argument had none. But keep "/" or "//". */
12930 if (!has_trailing_pathsep)
12931 {
12932 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000012933 if (after_pathsep(p, q))
12934 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012935 }
12936
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012937 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012938 }
12939# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012940 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012941# endif
12942#endif
12943
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012944 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012945
12946#ifdef HAVE_READLINK
12947fail:
12948#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012949 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012950}
12951
12952/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012953 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012954 */
12955 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012956f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012957 typval_T *argvars;
12958 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012959{
Bram Moolenaar33570922005-01-25 22:26:29 +000012960 list_T *l;
12961 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012962
Bram Moolenaar0d660222005-01-07 21:51:51 +000012963 rettv->vval.v_number = 0;
12964 if (argvars[0].v_type != VAR_LIST)
12965 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012966 else if ((l = argvars[0].vval.v_list) != NULL
12967 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000012968 {
12969 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012970 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012971 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012972 while (li != NULL)
12973 {
12974 ni = li->li_prev;
12975 list_append(l, li);
12976 li = ni;
12977 }
12978 rettv->vval.v_list = l;
12979 rettv->v_type = VAR_LIST;
12980 ++l->lv_refcount;
12981 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012982}
12983
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012984#define SP_NOMOVE 1 /* don't move cursor */
12985#define SP_REPEAT 2 /* repeat to find outer pair */
12986#define SP_RETCOUNT 4 /* return matchcount */
Bram Moolenaar231334e2005-07-25 20:46:57 +000012987#define SP_SETPCMARK 8 /* set previous context mark */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012988
Bram Moolenaar33570922005-01-25 22:26:29 +000012989static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012990
12991/*
12992 * Get flags for a search function.
12993 * Possibly sets "p_ws".
12994 * Returns BACKWARD, FORWARD or zero (for an error).
12995 */
12996 static int
12997get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000012998 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012999 int *flagsp;
13000{
13001 int dir = FORWARD;
13002 char_u *flags;
13003 char_u nbuf[NUMBUFLEN];
13004 int mask;
13005
13006 if (varp->v_type != VAR_UNKNOWN)
13007 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013008 flags = get_tv_string_buf_chk(varp, nbuf);
13009 if (flags == NULL)
13010 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013011 while (*flags != NUL)
13012 {
13013 switch (*flags)
13014 {
13015 case 'b': dir = BACKWARD; break;
13016 case 'w': p_ws = TRUE; break;
13017 case 'W': p_ws = FALSE; break;
13018 default: mask = 0;
13019 if (flagsp != NULL)
13020 switch (*flags)
13021 {
13022 case 'n': mask = SP_NOMOVE; break;
13023 case 'r': mask = SP_REPEAT; break;
13024 case 'm': mask = SP_RETCOUNT; break;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013025 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013026 }
13027 if (mask == 0)
13028 {
13029 EMSG2(_(e_invarg2), flags);
13030 dir = 0;
13031 }
13032 else
13033 *flagsp |= mask;
13034 }
13035 if (dir == 0)
13036 break;
13037 ++flags;
13038 }
13039 }
13040 return dir;
13041}
13042
Bram Moolenaar071d4272004-06-13 20:20:40 +000013043/*
13044 * "search()" function
13045 */
13046 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013047f_search(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013048 typval_T *argvars;
13049 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013050{
13051 char_u *pat;
13052 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013053 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013054 int save_p_ws = p_ws;
13055 int dir;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013056 int flags = 0;
13057
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013058 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013059
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013060 pat = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013061 dir = get_search_arg(&argvars[1], &flags); /* may set p_ws */
13062 if (dir == 0)
13063 goto theend;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013064 /*
13065 * This function accepts only SP_NOMOVE and SP_SETPCMARK flags.
13066 * Check to make sure only those flags are set.
13067 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
13068 * flags cannot be set. Check for that condition also.
13069 */
13070 if (((flags & ~(SP_NOMOVE | SP_SETPCMARK)) != 0) ||
13071 ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013072 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013073 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013074 goto theend;
13075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013076
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013077 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013078 if (searchit(curwin, curbuf, &pos, dir, pat, 1L,
13079 SEARCH_KEEP, RE_SEARCH) != FAIL)
13080 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013081 rettv->vval.v_number = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013082 if (flags & SP_SETPCMARK)
13083 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013084 curwin->w_cursor = pos;
13085 /* "/$" will put the cursor after the end of the line, may need to
13086 * correct that here */
13087 check_cursor();
13088 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013089
13090 /* If 'n' flag is used: restore cursor position. */
13091 if (flags & SP_NOMOVE)
13092 curwin->w_cursor = save_cursor;
13093theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000013094 p_ws = save_p_ws;
13095}
13096
Bram Moolenaar071d4272004-06-13 20:20:40 +000013097/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013098 * "searchdecl()" function
13099 */
13100 static void
13101f_searchdecl(argvars, rettv)
13102 typval_T *argvars;
13103 typval_T *rettv;
13104{
13105 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013106 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013107 int error = FALSE;
13108 char_u *name;
13109
13110 rettv->vval.v_number = 1; /* default: FAIL */
13111
13112 name = get_tv_string_chk(&argvars[0]);
13113 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000013114 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013115 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013116 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13117 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
13118 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013119 if (!error && name != NULL)
13120 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000013121 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013122}
13123
13124/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013125 * "searchpair()" function
13126 */
13127 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013128f_searchpair(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013129 typval_T *argvars;
13130 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013131{
13132 char_u *spat, *mpat, *epat;
13133 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013134 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013135 int dir;
13136 int flags = 0;
13137 char_u nbuf1[NUMBUFLEN];
13138 char_u nbuf2[NUMBUFLEN];
13139 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013140
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013141 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013142
Bram Moolenaar071d4272004-06-13 20:20:40 +000013143 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013144 spat = get_tv_string_chk(&argvars[0]);
13145 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
13146 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
13147 if (spat == NULL || mpat == NULL || epat == NULL)
13148 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013149
Bram Moolenaar071d4272004-06-13 20:20:40 +000013150 /* Handle the optional fourth argument: flags */
13151 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013152 if (dir == 0)
13153 goto theend;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013154 /*
13155 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
13156 */
13157 if ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK))
13158 {
13159 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
13160 goto theend;
13161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013162
13163 /* Optional fifth argument: skip expresion */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013164 if (argvars[3].v_type == VAR_UNKNOWN
13165 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013166 skip = (char_u *)"";
13167 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013168 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
13169 if (skip == NULL)
13170 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013171
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013172 rettv->vval.v_number = do_searchpair(spat, mpat, epat, dir, skip, flags);
13173
13174theend:
13175 p_ws = save_p_ws;
13176}
13177
13178/*
13179 * Search for a start/middle/end thing.
13180 * Used by searchpair(), see its documentation for the details.
13181 * Returns 0 or -1 for no match,
13182 */
13183 long
13184do_searchpair(spat, mpat, epat, dir, skip, flags)
13185 char_u *spat; /* start pattern */
13186 char_u *mpat; /* middle pattern */
13187 char_u *epat; /* end pattern */
13188 int dir; /* BACKWARD or FORWARD */
13189 char_u *skip; /* skip expression */
13190 int flags; /* SP_RETCOUNT, SP_REPEAT, SP_NOMOVE */
13191{
13192 char_u *save_cpo;
13193 char_u *pat, *pat2 = NULL, *pat3 = NULL;
13194 long retval = 0;
13195 pos_T pos;
13196 pos_T firstpos;
13197 pos_T foundpos;
13198 pos_T save_cursor;
13199 pos_T save_pos;
13200 int n;
13201 int r;
13202 int nest = 1;
13203 int err;
13204
13205 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13206 save_cpo = p_cpo;
13207 p_cpo = (char_u *)"";
13208
13209 /* Make two search patterns: start/end (pat2, for in nested pairs) and
13210 * start/middle/end (pat3, for the top pair). */
13211 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
13212 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
13213 if (pat2 == NULL || pat3 == NULL)
13214 goto theend;
13215 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
13216 if (*mpat == NUL)
13217 STRCPY(pat3, pat2);
13218 else
13219 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
13220 spat, epat, mpat);
13221
Bram Moolenaar071d4272004-06-13 20:20:40 +000013222 save_cursor = curwin->w_cursor;
13223 pos = curwin->w_cursor;
13224 firstpos.lnum = 0;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000013225 foundpos.lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013226 pat = pat3;
13227 for (;;)
13228 {
13229 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
13230 SEARCH_KEEP, RE_SEARCH);
13231 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
13232 /* didn't find it or found the first match again: FAIL */
13233 break;
13234
13235 if (firstpos.lnum == 0)
13236 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000013237 if (equalpos(pos, foundpos))
13238 {
13239 /* Found the same position again. Can happen with a pattern that
13240 * has "\zs" at the end and searching backwards. Advance one
13241 * character and try again. */
13242 if (dir == BACKWARD)
13243 decl(&pos);
13244 else
13245 incl(&pos);
13246 }
13247 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013248
13249 /* If the skip pattern matches, ignore this match. */
13250 if (*skip != NUL)
13251 {
13252 save_pos = curwin->w_cursor;
13253 curwin->w_cursor = pos;
13254 r = eval_to_bool(skip, &err, NULL, FALSE);
13255 curwin->w_cursor = save_pos;
13256 if (err)
13257 {
13258 /* Evaluating {skip} caused an error, break here. */
13259 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013260 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013261 break;
13262 }
13263 if (r)
13264 continue;
13265 }
13266
13267 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
13268 {
13269 /* Found end when searching backwards or start when searching
13270 * forward: nested pair. */
13271 ++nest;
13272 pat = pat2; /* nested, don't search for middle */
13273 }
13274 else
13275 {
13276 /* Found end when searching forward or start when searching
13277 * backward: end of (nested) pair; or found middle in outer pair. */
13278 if (--nest == 1)
13279 pat = pat3; /* outer level, search for middle */
13280 }
13281
13282 if (nest == 0)
13283 {
13284 /* Found the match: return matchcount or line number. */
13285 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013286 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013287 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013288 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013289 if (flags & SP_SETPCMARK)
13290 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013291 curwin->w_cursor = pos;
13292 if (!(flags & SP_REPEAT))
13293 break;
13294 nest = 1; /* search for next unmatched */
13295 }
13296 }
13297
13298 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013299 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013300 curwin->w_cursor = save_cursor;
13301
13302theend:
13303 vim_free(pat2);
13304 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013305 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013306
13307 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013308}
13309
Bram Moolenaar0d660222005-01-07 21:51:51 +000013310/*ARGSUSED*/
13311 static void
13312f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013313 typval_T *argvars;
13314 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013315{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013316#ifdef FEAT_CLIENTSERVER
13317 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013318 char_u *server = get_tv_string_chk(&argvars[0]);
13319 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013320
Bram Moolenaar0d660222005-01-07 21:51:51 +000013321 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013322 if (server == NULL || reply == NULL)
13323 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013324 if (check_restricted() || check_secure())
13325 return;
13326# ifdef FEAT_X11
13327 if (check_connection() == FAIL)
13328 return;
13329# endif
13330
13331 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013332 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013333 EMSG(_("E258: Unable to send to client"));
13334 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013335 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013336 rettv->vval.v_number = 0;
13337#else
13338 rettv->vval.v_number = -1;
13339#endif
13340}
13341
13342/*ARGSUSED*/
13343 static void
13344f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013345 typval_T *argvars;
13346 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013347{
13348 char_u *r = NULL;
13349
13350#ifdef FEAT_CLIENTSERVER
13351# ifdef WIN32
13352 r = serverGetVimNames();
13353# else
13354 make_connection();
13355 if (X_DISPLAY != NULL)
13356 r = serverGetVimNames(X_DISPLAY);
13357# endif
13358#endif
13359 rettv->v_type = VAR_STRING;
13360 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013361}
13362
13363/*
13364 * "setbufvar()" function
13365 */
13366/*ARGSUSED*/
13367 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013368f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013369 typval_T *argvars;
13370 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013371{
13372 buf_T *buf;
13373#ifdef FEAT_AUTOCMD
13374 aco_save_T aco;
13375#else
13376 buf_T *save_curbuf;
13377#endif
13378 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013379 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013380 char_u nbuf[NUMBUFLEN];
13381
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013382 rettv->vval.v_number = 0;
13383
Bram Moolenaar071d4272004-06-13 20:20:40 +000013384 if (check_restricted() || check_secure())
13385 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013386 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
13387 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013388 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013389 varp = &argvars[2];
13390
13391 if (buf != NULL && varname != NULL && varp != NULL)
13392 {
13393 /* set curbuf to be our buf, temporarily */
13394#ifdef FEAT_AUTOCMD
13395 aucmd_prepbuf(&aco, buf);
13396#else
13397 save_curbuf = curbuf;
13398 curbuf = buf;
13399#endif
13400
13401 if (*varname == '&')
13402 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013403 long numval;
13404 char_u *strval;
13405 int error = FALSE;
13406
Bram Moolenaar071d4272004-06-13 20:20:40 +000013407 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013408 numval = get_tv_number_chk(varp, &error);
13409 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013410 if (!error && strval != NULL)
13411 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013412 }
13413 else
13414 {
13415 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
13416 if (bufvarname != NULL)
13417 {
13418 STRCPY(bufvarname, "b:");
13419 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013420 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013421 vim_free(bufvarname);
13422 }
13423 }
13424
13425 /* reset notion of buffer */
13426#ifdef FEAT_AUTOCMD
13427 aucmd_restbuf(&aco);
13428#else
13429 curbuf = save_curbuf;
13430#endif
13431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013432}
13433
13434/*
13435 * "setcmdpos()" function
13436 */
13437 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013438f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013439 typval_T *argvars;
13440 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013441{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013442 int pos = (int)get_tv_number(&argvars[0]) - 1;
13443
13444 if (pos >= 0)
13445 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013446}
13447
13448/*
13449 * "setline()" function
13450 */
13451 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013452f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013453 typval_T *argvars;
13454 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013455{
13456 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000013457 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013458 list_T *l = NULL;
13459 listitem_T *li = NULL;
13460 long added = 0;
13461 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013462
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013463 lnum = get_tv_lnum(&argvars[0]);
13464 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013465 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013466 l = argvars[1].vval.v_list;
13467 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013468 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013469 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013470 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013471
13472 rettv->vval.v_number = 0; /* OK */
13473 for (;;)
13474 {
13475 if (l != NULL)
13476 {
13477 /* list argument, get next string */
13478 if (li == NULL)
13479 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013480 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013481 li = li->li_next;
13482 }
13483
13484 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013485 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013486 break;
13487 if (lnum <= curbuf->b_ml.ml_line_count)
13488 {
13489 /* existing line, replace it */
13490 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
13491 {
13492 changed_bytes(lnum, 0);
13493 check_cursor_col();
13494 rettv->vval.v_number = 0; /* OK */
13495 }
13496 }
13497 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
13498 {
13499 /* lnum is one past the last line, append the line */
13500 ++added;
13501 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
13502 rettv->vval.v_number = 0; /* OK */
13503 }
13504
13505 if (l == NULL) /* only one string argument */
13506 break;
13507 ++lnum;
13508 }
13509
13510 if (added > 0)
13511 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013512}
13513
13514/*
Bram Moolenaar2641f772005-03-25 21:58:17 +000013515 * "setqflist()" function
13516 */
13517/*ARGSUSED*/
13518 static void
13519f_setqflist(argvars, rettv)
13520 typval_T *argvars;
13521 typval_T *rettv;
13522{
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013523 char_u *act;
13524 int action = ' ';
13525
Bram Moolenaar2641f772005-03-25 21:58:17 +000013526 rettv->vval.v_number = -1;
13527
13528#ifdef FEAT_QUICKFIX
13529 if (argvars[0].v_type != VAR_LIST)
13530 EMSG(_(e_listreq));
13531 else
13532 {
13533 list_T *l = argvars[0].vval.v_list;
13534
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013535 if (argvars[1].v_type == VAR_STRING)
13536 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013537 act = get_tv_string_chk(&argvars[1]);
13538 if (act == NULL)
13539 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013540 if (*act == 'a' || *act == 'r')
13541 action = *act;
13542 }
13543
13544 if (l != NULL && set_errorlist(l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013545 rettv->vval.v_number = 0;
13546 }
13547#endif
13548}
13549
13550/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013551 * "setreg()" function
13552 */
13553 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013554f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013555 typval_T *argvars;
13556 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013557{
13558 int regname;
13559 char_u *strregname;
13560 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013561 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013562 int append;
13563 char_u yank_type;
13564 long block_len;
13565
13566 block_len = -1;
13567 yank_type = MAUTO;
13568 append = FALSE;
13569
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013570 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013571 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013572
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013573 if (strregname == NULL)
13574 return; /* type error; errmsg already given */
13575 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013576 if (regname == 0 || regname == '@')
13577 regname = '"';
13578 else if (regname == '=')
13579 return;
13580
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013581 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013582 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013583 stropt = get_tv_string_chk(&argvars[2]);
13584 if (stropt == NULL)
13585 return; /* type error */
13586 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013587 switch (*stropt)
13588 {
13589 case 'a': case 'A': /* append */
13590 append = TRUE;
13591 break;
13592 case 'v': case 'c': /* character-wise selection */
13593 yank_type = MCHAR;
13594 break;
13595 case 'V': case 'l': /* line-wise selection */
13596 yank_type = MLINE;
13597 break;
13598#ifdef FEAT_VISUAL
13599 case 'b': case Ctrl_V: /* block-wise selection */
13600 yank_type = MBLOCK;
13601 if (VIM_ISDIGIT(stropt[1]))
13602 {
13603 ++stropt;
13604 block_len = getdigits(&stropt) - 1;
13605 --stropt;
13606 }
13607 break;
13608#endif
13609 }
13610 }
13611
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013612 strval = get_tv_string_chk(&argvars[1]);
13613 if (strval != NULL)
13614 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000013615 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013616 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013617}
13618
13619
13620/*
13621 * "setwinvar(expr)" function
13622 */
13623/*ARGSUSED*/
13624 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013625f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013626 typval_T *argvars;
13627 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013628{
13629 win_T *win;
13630#ifdef FEAT_WINDOWS
13631 win_T *save_curwin;
13632#endif
13633 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013634 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013635 char_u nbuf[NUMBUFLEN];
13636
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013637 rettv->vval.v_number = 0;
13638
Bram Moolenaar071d4272004-06-13 20:20:40 +000013639 if (check_restricted() || check_secure())
13640 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013641 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013642 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013643 varp = &argvars[2];
13644
13645 if (win != NULL && varname != NULL && varp != NULL)
13646 {
13647#ifdef FEAT_WINDOWS
13648 /* set curwin to be our win, temporarily */
13649 save_curwin = curwin;
13650 curwin = win;
13651 curbuf = curwin->w_buffer;
13652#endif
13653
13654 if (*varname == '&')
13655 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013656 long numval;
13657 char_u *strval;
13658 int error = FALSE;
13659
Bram Moolenaar071d4272004-06-13 20:20:40 +000013660 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013661 numval = get_tv_number_chk(varp, &error);
13662 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013663 if (!error && strval != NULL)
13664 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013665 }
13666 else
13667 {
13668 winvarname = alloc((unsigned)STRLEN(varname) + 3);
13669 if (winvarname != NULL)
13670 {
13671 STRCPY(winvarname, "w:");
13672 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013673 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013674 vim_free(winvarname);
13675 }
13676 }
13677
13678#ifdef FEAT_WINDOWS
13679 /* Restore current window, if it's still valid (autocomands can make
13680 * it invalid). */
13681 if (win_valid(save_curwin))
13682 {
13683 curwin = save_curwin;
13684 curbuf = curwin->w_buffer;
13685 }
13686#endif
13687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013688}
13689
13690/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013691 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013692 */
13693 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013694f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013695 typval_T *argvars;
13696 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013697{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013698 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013699
Bram Moolenaar0d660222005-01-07 21:51:51 +000013700 p = get_tv_string(&argvars[0]);
13701 rettv->vval.v_string = vim_strsave(p);
13702 simplify_filename(rettv->vval.v_string); /* simplify in place */
13703 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013704}
13705
Bram Moolenaar0d660222005-01-07 21:51:51 +000013706static int
13707#ifdef __BORLANDC__
13708 _RTLENTRYF
13709#endif
13710 item_compare __ARGS((const void *s1, const void *s2));
13711static int
13712#ifdef __BORLANDC__
13713 _RTLENTRYF
13714#endif
13715 item_compare2 __ARGS((const void *s1, const void *s2));
13716
13717static int item_compare_ic;
13718static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013719static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013720#define ITEM_COMPARE_FAIL 999
13721
Bram Moolenaar071d4272004-06-13 20:20:40 +000013722/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013723 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013724 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013725 static int
13726#ifdef __BORLANDC__
13727_RTLENTRYF
13728#endif
13729item_compare(s1, s2)
13730 const void *s1;
13731 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013732{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013733 char_u *p1, *p2;
13734 char_u *tofree1, *tofree2;
13735 int res;
13736 char_u numbuf1[NUMBUFLEN];
13737 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013738
Bram Moolenaar33570922005-01-25 22:26:29 +000013739 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1);
13740 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013741 if (item_compare_ic)
13742 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013743 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000013744 res = STRCMP(p1, p2);
13745 vim_free(tofree1);
13746 vim_free(tofree2);
13747 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013748}
13749
13750 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000013751#ifdef __BORLANDC__
13752_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000013753#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000013754item_compare2(s1, s2)
13755 const void *s1;
13756 const void *s2;
13757{
13758 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000013759 typval_T rettv;
13760 typval_T argv[2];
Bram Moolenaar0d660222005-01-07 21:51:51 +000013761 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013762
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013763 /* shortcut after failure in previous call; compare all items equal */
13764 if (item_compare_func_err)
13765 return 0;
13766
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013767 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
13768 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013769 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
13770 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013771
13772 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
13773 res = call_func(item_compare_func, STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000013774 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013775 clear_tv(&argv[0]);
13776 clear_tv(&argv[1]);
13777
13778 if (res == FAIL)
13779 res = ITEM_COMPARE_FAIL;
13780 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013781 /* return value has wrong type */
13782 res = get_tv_number_chk(&rettv, &item_compare_func_err);
13783 if (item_compare_func_err)
13784 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013785 clear_tv(&rettv);
13786 return res;
13787}
13788
13789/*
13790 * "sort({list})" function
13791 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013792 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013793f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013794 typval_T *argvars;
13795 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013796{
Bram Moolenaar33570922005-01-25 22:26:29 +000013797 list_T *l;
13798 listitem_T *li;
13799 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013800 long len;
13801 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013802
Bram Moolenaar0d660222005-01-07 21:51:51 +000013803 rettv->vval.v_number = 0;
13804 if (argvars[0].v_type != VAR_LIST)
13805 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000013806 else
13807 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013808 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013809 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013810 return;
13811 rettv->vval.v_list = l;
13812 rettv->v_type = VAR_LIST;
13813 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013814
Bram Moolenaar0d660222005-01-07 21:51:51 +000013815 len = list_len(l);
13816 if (len <= 1)
13817 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013818
Bram Moolenaar0d660222005-01-07 21:51:51 +000013819 item_compare_ic = FALSE;
13820 item_compare_func = NULL;
13821 if (argvars[1].v_type != VAR_UNKNOWN)
13822 {
13823 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013824 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013825 else
13826 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013827 int error = FALSE;
13828
13829 i = get_tv_number_chk(&argvars[1], &error);
13830 if (error)
13831 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013832 if (i == 1)
13833 item_compare_ic = TRUE;
13834 else
13835 item_compare_func = get_tv_string(&argvars[1]);
13836 }
13837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013838
Bram Moolenaar0d660222005-01-07 21:51:51 +000013839 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013840 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013841 if (ptrs == NULL)
13842 return;
13843 i = 0;
13844 for (li = l->lv_first; li != NULL; li = li->li_next)
13845 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013846
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013847 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013848 /* test the compare function */
13849 if (item_compare_func != NULL
13850 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
13851 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013852 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013853 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000013854 {
13855 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013856 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000013857 item_compare_func == NULL ? item_compare : item_compare2);
13858
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013859 if (!item_compare_func_err)
13860 {
13861 /* Clear the List and append the items in the sorted order. */
13862 l->lv_first = l->lv_last = NULL;
13863 l->lv_len = 0;
13864 for (i = 0; i < len; ++i)
13865 list_append(l, ptrs[i]);
13866 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013867 }
13868
13869 vim_free(ptrs);
13870 }
13871}
13872
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013873/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000013874 * "soundfold({word})" function
13875 */
13876 static void
13877f_soundfold(argvars, rettv)
13878 typval_T *argvars;
13879 typval_T *rettv;
13880{
13881 char_u *s;
13882
13883 rettv->v_type = VAR_STRING;
13884 s = get_tv_string(&argvars[0]);
13885#ifdef FEAT_SYN_HL
13886 rettv->vval.v_string = eval_soundfold(s);
13887#else
13888 rettv->vval.v_string = vim_strsave(s);
13889#endif
13890}
13891
13892/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013893 * "spellbadword()" function
13894 */
13895/* ARGSUSED */
13896 static void
13897f_spellbadword(argvars, rettv)
13898 typval_T *argvars;
13899 typval_T *rettv;
13900{
Bram Moolenaar4463f292005-09-25 22:20:24 +000013901 char_u *word = (char_u *)"";
13902#ifdef FEAT_SYN_HL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013903 int len = 0;
13904 hlf_T attr = HLF_COUNT;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013905 list_T *l;
13906#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013907
Bram Moolenaar4463f292005-09-25 22:20:24 +000013908 l = list_alloc();
13909 if (l == NULL)
13910 return;
13911 rettv->v_type = VAR_LIST;
13912 rettv->vval.v_list = l;
13913 ++l->lv_refcount;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013914
13915#ifdef FEAT_SYN_HL
Bram Moolenaar4463f292005-09-25 22:20:24 +000013916 if (argvars[0].v_type == VAR_UNKNOWN)
13917 {
13918 /* Find the start and length of the badly spelled word. */
13919 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
13920 if (len != 0)
13921 word = ml_get_cursor();
13922 }
13923 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
13924 {
13925 char_u *str = get_tv_string_chk(&argvars[0]);
13926 int capcol = -1;
13927
13928 if (str != NULL)
13929 {
13930 /* Check the argument for spelling. */
13931 while (*str != NUL)
13932 {
13933 len = spell_check(curwin, str, &attr, &capcol);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013934 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013935 {
13936 word = str;
13937 break;
13938 }
13939 str += len;
13940 }
13941 }
13942 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013943#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000013944
13945 list_append_string(l, word, len);
13946 list_append_string(l, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013947 attr == HLF_SPB ? "bad" :
13948 attr == HLF_SPR ? "rare" :
13949 attr == HLF_SPL ? "local" :
13950 attr == HLF_SPC ? "caps" :
13951 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013952}
13953
13954/*
13955 * "spellsuggest()" function
13956 */
13957 static void
13958f_spellsuggest(argvars, rettv)
13959 typval_T *argvars;
13960 typval_T *rettv;
13961{
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000013962 list_T *l;
13963#ifdef FEAT_SYN_HL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013964 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000013965 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013966 int maxcount;
13967 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013968 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000013969 listitem_T *li;
13970 int need_capital = FALSE;
13971#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013972
13973 l = list_alloc();
13974 if (l == NULL)
13975 return;
13976 rettv->v_type = VAR_LIST;
13977 rettv->vval.v_list = l;
13978 ++l->lv_refcount;
13979
13980#ifdef FEAT_SYN_HL
13981 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
13982 {
13983 str = get_tv_string(&argvars[0]);
13984 if (argvars[1].v_type != VAR_UNKNOWN)
13985 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000013986 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013987 if (maxcount <= 0)
13988 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000013989 if (argvars[2].v_type != VAR_UNKNOWN)
13990 {
13991 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
13992 if (typeerr)
13993 return;
13994 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013995 }
13996 else
13997 maxcount = 25;
13998
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000013999 spell_suggest_list(&ga, str, maxcount, need_capital);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014000
14001 for (i = 0; i < ga.ga_len; ++i)
14002 {
14003 str = ((char_u **)ga.ga_data)[i];
14004
14005 li = listitem_alloc();
14006 if (li == NULL)
14007 vim_free(str);
14008 else
14009 {
14010 li->li_tv.v_type = VAR_STRING;
14011 li->li_tv.v_lock = 0;
14012 li->li_tv.vval.v_string = str;
14013 list_append(l, li);
14014 }
14015 }
14016 ga_clear(&ga);
14017 }
14018#endif
14019}
14020
Bram Moolenaar0d660222005-01-07 21:51:51 +000014021 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014022f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014023 typval_T *argvars;
14024 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014025{
14026 char_u *str;
14027 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014028 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014029 regmatch_T regmatch;
14030 char_u patbuf[NUMBUFLEN];
14031 char_u *save_cpo;
14032 int match;
Bram Moolenaar33570922005-01-25 22:26:29 +000014033 list_T *l;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014034 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014035 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014036 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014037
14038 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14039 save_cpo = p_cpo;
14040 p_cpo = (char_u *)"";
14041
14042 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014043 if (argvars[1].v_type != VAR_UNKNOWN)
14044 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014045 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14046 if (pat == NULL)
14047 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014048 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014049 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014050 }
14051 if (pat == NULL || *pat == NUL)
14052 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000014053
14054 l = list_alloc();
14055 if (l == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014056 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014057 rettv->v_type = VAR_LIST;
14058 rettv->vval.v_list = l;
14059 ++l->lv_refcount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014060 if (typeerr)
14061 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014062
Bram Moolenaar0d660222005-01-07 21:51:51 +000014063 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14064 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014065 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014066 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014067 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014068 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014069 if (*str == NUL)
14070 match = FALSE; /* empty item at the end */
14071 else
14072 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014073 if (match)
14074 end = regmatch.startp[0];
14075 else
14076 end = str + STRLEN(str);
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014077 if (keepempty || end > str || (l->lv_len > 0 && *str != NUL
14078 && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014079 {
Bram Moolenaar4463f292005-09-25 22:20:24 +000014080 if (list_append_string(l, str, (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014081 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014082 }
14083 if (!match)
14084 break;
14085 /* Advance to just after the match. */
14086 if (regmatch.endp[0] > str)
14087 col = 0;
14088 else
14089 {
14090 /* Don't get stuck at the same match. */
14091#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014092 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014093#else
14094 col = 1;
14095#endif
14096 }
14097 str = regmatch.endp[0];
14098 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014099
Bram Moolenaar0d660222005-01-07 21:51:51 +000014100 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014101 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014102
Bram Moolenaar0d660222005-01-07 21:51:51 +000014103 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014104}
14105
14106#ifdef HAVE_STRFTIME
14107/*
14108 * "strftime({format}[, {time}])" function
14109 */
14110 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014111f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014112 typval_T *argvars;
14113 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014114{
14115 char_u result_buf[256];
14116 struct tm *curtime;
14117 time_t seconds;
14118 char_u *p;
14119
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014120 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014121
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014122 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014123 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014124 seconds = time(NULL);
14125 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014126 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014127 curtime = localtime(&seconds);
14128 /* MSVC returns NULL for an invalid value of seconds. */
14129 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014130 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014131 else
14132 {
14133# ifdef FEAT_MBYTE
14134 vimconv_T conv;
14135 char_u *enc;
14136
14137 conv.vc_type = CONV_NONE;
14138 enc = enc_locale();
14139 convert_setup(&conv, p_enc, enc);
14140 if (conv.vc_type != CONV_NONE)
14141 p = string_convert(&conv, p, NULL);
14142# endif
14143 if (p != NULL)
14144 (void)strftime((char *)result_buf, sizeof(result_buf),
14145 (char *)p, curtime);
14146 else
14147 result_buf[0] = NUL;
14148
14149# ifdef FEAT_MBYTE
14150 if (conv.vc_type != CONV_NONE)
14151 vim_free(p);
14152 convert_setup(&conv, enc, p_enc);
14153 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014154 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014155 else
14156# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014157 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014158
14159# ifdef FEAT_MBYTE
14160 /* Release conversion descriptors */
14161 convert_setup(&conv, NULL, NULL);
14162 vim_free(enc);
14163# endif
14164 }
14165}
14166#endif
14167
14168/*
14169 * "stridx()" function
14170 */
14171 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014172f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014173 typval_T *argvars;
14174 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014175{
14176 char_u buf[NUMBUFLEN];
14177 char_u *needle;
14178 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000014179 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014180 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000014181 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014182
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014183 needle = get_tv_string_chk(&argvars[1]);
14184 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000014185 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014186 if (needle == NULL || haystack == NULL)
14187 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014188
Bram Moolenaar33570922005-01-25 22:26:29 +000014189 if (argvars[2].v_type != VAR_UNKNOWN)
14190 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014191 int error = FALSE;
14192
14193 start_idx = get_tv_number_chk(&argvars[2], &error);
14194 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000014195 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014196 if (start_idx >= 0)
14197 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000014198 }
14199
14200 pos = (char_u *)strstr((char *)haystack, (char *)needle);
14201 if (pos != NULL)
14202 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014203}
14204
14205/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014206 * "string()" function
14207 */
14208 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014209f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014210 typval_T *argvars;
14211 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014212{
14213 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014214 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014215
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014216 rettv->v_type = VAR_STRING;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014217 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014218 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014219 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014220}
14221
14222/*
14223 * "strlen()" function
14224 */
14225 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014226f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014227 typval_T *argvars;
14228 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014229{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014230 rettv->vval.v_number = (varnumber_T)(STRLEN(
14231 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014232}
14233
14234/*
14235 * "strpart()" function
14236 */
14237 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014238f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014239 typval_T *argvars;
14240 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014241{
14242 char_u *p;
14243 int n;
14244 int len;
14245 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014246 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014247
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014248 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014249 slen = (int)STRLEN(p);
14250
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014251 n = get_tv_number_chk(&argvars[1], &error);
14252 if (error)
14253 len = 0;
14254 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014255 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014256 else
14257 len = slen - n; /* default len: all bytes that are available. */
14258
14259 /*
14260 * Only return the overlap between the specified part and the actual
14261 * string.
14262 */
14263 if (n < 0)
14264 {
14265 len += n;
14266 n = 0;
14267 }
14268 else if (n > slen)
14269 n = slen;
14270 if (len < 0)
14271 len = 0;
14272 else if (n + len > slen)
14273 len = slen - n;
14274
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014275 rettv->v_type = VAR_STRING;
14276 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014277}
14278
14279/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014280 * "strridx()" function
14281 */
14282 static void
14283f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014284 typval_T *argvars;
14285 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014286{
14287 char_u buf[NUMBUFLEN];
14288 char_u *needle;
14289 char_u *haystack;
14290 char_u *rest;
14291 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014292 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014293
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014294 needle = get_tv_string_chk(&argvars[1]);
14295 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar532c7802005-01-27 14:44:31 +000014296 haystack_len = STRLEN(haystack);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014297
14298 rettv->vval.v_number = -1;
14299 if (needle == NULL || haystack == NULL)
14300 return; /* type error; errmsg already given */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014301 if (argvars[2].v_type != VAR_UNKNOWN)
14302 {
14303 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014304 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000014305 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014306 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014307 }
14308 else
14309 end_idx = haystack_len;
14310
Bram Moolenaar0d660222005-01-07 21:51:51 +000014311 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000014312 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014313 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014314 lastmatch = haystack + end_idx;
14315 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014316 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000014317 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014318 for (rest = haystack; *rest != '\0'; ++rest)
14319 {
14320 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000014321 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014322 break;
14323 lastmatch = rest;
14324 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000014325 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014326
14327 if (lastmatch == NULL)
14328 rettv->vval.v_number = -1;
14329 else
14330 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
14331}
14332
14333/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014334 * "strtrans()" function
14335 */
14336 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014337f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014338 typval_T *argvars;
14339 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014340{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014341 rettv->v_type = VAR_STRING;
14342 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014343}
14344
14345/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014346 * "submatch()" function
14347 */
14348 static void
14349f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014350 typval_T *argvars;
14351 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014352{
14353 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014354 rettv->vval.v_string =
14355 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014356}
14357
14358/*
14359 * "substitute()" function
14360 */
14361 static void
14362f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014363 typval_T *argvars;
14364 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014365{
14366 char_u patbuf[NUMBUFLEN];
14367 char_u subbuf[NUMBUFLEN];
14368 char_u flagsbuf[NUMBUFLEN];
14369
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014370 char_u *str = get_tv_string_chk(&argvars[0]);
14371 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14372 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
14373 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
14374
Bram Moolenaar0d660222005-01-07 21:51:51 +000014375 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014376 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
14377 rettv->vval.v_string = NULL;
14378 else
14379 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014380}
14381
14382/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014383 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014384 */
14385/*ARGSUSED*/
14386 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014387f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014388 typval_T *argvars;
14389 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014390{
14391 int id = 0;
14392#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014393 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014394 long col;
14395 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000014396 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014397
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014398 lnum = get_tv_lnum(argvars); /* -1 on type error */
14399 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
14400 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014401
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014402 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014403 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +000014404 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014405#endif
14406
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014407 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014408}
14409
14410/*
14411 * "synIDattr(id, what [, mode])" function
14412 */
14413/*ARGSUSED*/
14414 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014415f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014416 typval_T *argvars;
14417 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014418{
14419 char_u *p = NULL;
14420#ifdef FEAT_SYN_HL
14421 int id;
14422 char_u *what;
14423 char_u *mode;
14424 char_u modebuf[NUMBUFLEN];
14425 int modec;
14426
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014427 id = get_tv_number(&argvars[0]);
14428 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014429 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014430 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014431 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014432 modec = TOLOWER_ASC(mode[0]);
14433 if (modec != 't' && modec != 'c'
14434#ifdef FEAT_GUI
14435 && modec != 'g'
14436#endif
14437 )
14438 modec = 0; /* replace invalid with current */
14439 }
14440 else
14441 {
14442#ifdef FEAT_GUI
14443 if (gui.in_use)
14444 modec = 'g';
14445 else
14446#endif
14447 if (t_colors > 1)
14448 modec = 'c';
14449 else
14450 modec = 't';
14451 }
14452
14453
14454 switch (TOLOWER_ASC(what[0]))
14455 {
14456 case 'b':
14457 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
14458 p = highlight_color(id, what, modec);
14459 else /* bold */
14460 p = highlight_has_attr(id, HL_BOLD, modec);
14461 break;
14462
14463 case 'f': /* fg[#] */
14464 p = highlight_color(id, what, modec);
14465 break;
14466
14467 case 'i':
14468 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
14469 p = highlight_has_attr(id, HL_INVERSE, modec);
14470 else /* italic */
14471 p = highlight_has_attr(id, HL_ITALIC, modec);
14472 break;
14473
14474 case 'n': /* name */
14475 p = get_highlight_name(NULL, id - 1);
14476 break;
14477
14478 case 'r': /* reverse */
14479 p = highlight_has_attr(id, HL_INVERSE, modec);
14480 break;
14481
14482 case 's': /* standout */
14483 p = highlight_has_attr(id, HL_STANDOUT, modec);
14484 break;
14485
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000014486 case 'u':
14487 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
14488 /* underline */
14489 p = highlight_has_attr(id, HL_UNDERLINE, modec);
14490 else
14491 /* undercurl */
14492 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014493 break;
14494 }
14495
14496 if (p != NULL)
14497 p = vim_strsave(p);
14498#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014499 rettv->v_type = VAR_STRING;
14500 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014501}
14502
14503/*
14504 * "synIDtrans(id)" function
14505 */
14506/*ARGSUSED*/
14507 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014508f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014509 typval_T *argvars;
14510 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014511{
14512 int id;
14513
14514#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014515 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014516
14517 if (id > 0)
14518 id = syn_get_final_id(id);
14519 else
14520#endif
14521 id = 0;
14522
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014523 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014524}
14525
14526/*
14527 * "system()" function
14528 */
14529 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014530f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014531 typval_T *argvars;
14532 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014533{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014534 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014535 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014536 char_u *infile = NULL;
14537 char_u buf[NUMBUFLEN];
14538 int err = FALSE;
14539 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014540
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014541 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014542 {
14543 /*
14544 * Write the string to a temp file, to be used for input of the shell
14545 * command.
14546 */
14547 if ((infile = vim_tempname('i')) == NULL)
14548 {
14549 EMSG(_(e_notmp));
14550 return;
14551 }
14552
14553 fd = mch_fopen((char *)infile, WRITEBIN);
14554 if (fd == NULL)
14555 {
14556 EMSG2(_(e_notopen), infile);
14557 goto done;
14558 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014559 p = get_tv_string_buf_chk(&argvars[1], buf);
14560 if (p == NULL)
14561 goto done; /* type error; errmsg already given */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014562 if (fwrite(p, STRLEN(p), 1, fd) != 1)
14563 err = TRUE;
14564 if (fclose(fd) != 0)
14565 err = TRUE;
14566 if (err)
14567 {
14568 EMSG(_("E677: Error writing temp file"));
14569 goto done;
14570 }
14571 }
14572
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014573 res = get_cmd_output(get_tv_string(&argvars[0]), infile, SHELL_SILENT);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014574
Bram Moolenaar071d4272004-06-13 20:20:40 +000014575#ifdef USE_CR
14576 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014577 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014578 {
14579 char_u *s;
14580
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014581 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014582 {
14583 if (*s == CAR)
14584 *s = NL;
14585 }
14586 }
14587#else
14588# ifdef USE_CRNL
14589 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014590 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014591 {
14592 char_u *s, *d;
14593
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014594 d = res;
14595 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014596 {
14597 if (s[0] == CAR && s[1] == NL)
14598 ++s;
14599 *d++ = *s;
14600 }
14601 *d = NUL;
14602 }
14603# endif
14604#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014605
14606done:
14607 if (infile != NULL)
14608 {
14609 mch_remove(infile);
14610 vim_free(infile);
14611 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014612 rettv->v_type = VAR_STRING;
14613 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014614}
14615
14616/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000014617 * "tagfiles()" function
14618 */
14619/*ARGSUSED*/
14620 static void
14621f_tagfiles(argvars, rettv)
14622 typval_T *argvars;
14623 typval_T *rettv;
14624{
14625 char_u fname[MAXPATHL + 1];
14626 list_T *l;
14627
14628 l = list_alloc();
14629 if (l == NULL)
14630 {
14631 rettv->vval.v_number = 0;
14632 return;
14633 }
14634 rettv->vval.v_list = l;
14635 rettv->v_type = VAR_LIST;
14636 ++l->lv_refcount;
14637
14638 get_tagfname(TRUE, NULL);
14639 for (;;)
14640 if (get_tagfname(FALSE, fname) == FAIL
Bram Moolenaar4463f292005-09-25 22:20:24 +000014641 || list_append_string(l, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000014642 break;
14643}
14644
14645/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000014646 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014647 */
14648 static void
14649f_taglist(argvars, rettv)
14650 typval_T *argvars;
14651 typval_T *rettv;
14652{
14653 char_u *tag_pattern;
14654 list_T *l;
14655
14656 tag_pattern = get_tv_string(&argvars[0]);
14657
14658 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014659 if (*tag_pattern == NUL)
14660 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014661
14662 l = list_alloc();
14663 if (l != NULL)
14664 {
14665 if (get_tags(l, tag_pattern) != FAIL)
14666 {
14667 rettv->vval.v_list = l;
14668 rettv->v_type = VAR_LIST;
14669 ++l->lv_refcount;
14670 }
14671 else
14672 list_free(l);
14673 }
14674}
14675
14676/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014677 * "tempname()" function
14678 */
14679/*ARGSUSED*/
14680 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014681f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014682 typval_T *argvars;
14683 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014684{
14685 static int x = 'A';
14686
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014687 rettv->v_type = VAR_STRING;
14688 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014689
14690 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
14691 * names. Skip 'I' and 'O', they are used for shell redirection. */
14692 do
14693 {
14694 if (x == 'Z')
14695 x = '0';
14696 else if (x == '9')
14697 x = 'A';
14698 else
14699 {
14700#ifdef EBCDIC
14701 if (x == 'I')
14702 x = 'J';
14703 else if (x == 'R')
14704 x = 'S';
14705 else
14706#endif
14707 ++x;
14708 }
14709 } while (x == 'I' || x == 'O');
14710}
14711
14712/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000014713 * "test(list)" function: Just checking the walls...
14714 */
14715/*ARGSUSED*/
14716 static void
14717f_test(argvars, rettv)
14718 typval_T *argvars;
14719 typval_T *rettv;
14720{
14721 /* Used for unit testing. Change the code below to your liking. */
14722#if 0
14723 listitem_T *li;
14724 list_T *l;
14725 char_u *bad, *good;
14726
14727 if (argvars[0].v_type != VAR_LIST)
14728 return;
14729 l = argvars[0].vval.v_list;
14730 if (l == NULL)
14731 return;
14732 li = l->lv_first;
14733 if (li == NULL)
14734 return;
14735 bad = get_tv_string(&li->li_tv);
14736 li = li->li_next;
14737 if (li == NULL)
14738 return;
14739 good = get_tv_string(&li->li_tv);
14740 rettv->vval.v_number = test_edit_score(bad, good);
14741#endif
14742}
14743
14744/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014745 * "tolower(string)" function
14746 */
14747 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014748f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014749 typval_T *argvars;
14750 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014751{
14752 char_u *p;
14753
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014754 p = vim_strsave(get_tv_string(&argvars[0]));
14755 rettv->v_type = VAR_STRING;
14756 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014757
14758 if (p != NULL)
14759 while (*p != NUL)
14760 {
14761#ifdef FEAT_MBYTE
14762 int l;
14763
14764 if (enc_utf8)
14765 {
14766 int c, lc;
14767
14768 c = utf_ptr2char(p);
14769 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014770 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014771 /* TODO: reallocate string when byte count changes. */
14772 if (utf_char2len(lc) == l)
14773 utf_char2bytes(lc, p);
14774 p += l;
14775 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014776 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014777 p += l; /* skip multi-byte character */
14778 else
14779#endif
14780 {
14781 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
14782 ++p;
14783 }
14784 }
14785}
14786
14787/*
14788 * "toupper(string)" function
14789 */
14790 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014791f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014792 typval_T *argvars;
14793 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014794{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014795 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014796 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014797}
14798
14799/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000014800 * "tr(string, fromstr, tostr)" function
14801 */
14802 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014803f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014804 typval_T *argvars;
14805 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014806{
14807 char_u *instr;
14808 char_u *fromstr;
14809 char_u *tostr;
14810 char_u *p;
14811#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000014812 int inlen;
14813 int fromlen;
14814 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014815 int idx;
14816 char_u *cpstr;
14817 int cplen;
14818 int first = TRUE;
14819#endif
14820 char_u buf[NUMBUFLEN];
14821 char_u buf2[NUMBUFLEN];
14822 garray_T ga;
14823
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014824 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014825 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
14826 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014827
14828 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014829 rettv->v_type = VAR_STRING;
14830 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014831 if (fromstr == NULL || tostr == NULL)
14832 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000014833 ga_init2(&ga, (int)sizeof(char), 80);
14834
14835#ifdef FEAT_MBYTE
14836 if (!has_mbyte)
14837#endif
14838 /* not multi-byte: fromstr and tostr must be the same length */
14839 if (STRLEN(fromstr) != STRLEN(tostr))
14840 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014841#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000014842error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014843#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000014844 EMSG2(_(e_invarg2), fromstr);
14845 ga_clear(&ga);
14846 return;
14847 }
14848
14849 /* fromstr and tostr have to contain the same number of chars */
14850 while (*instr != NUL)
14851 {
14852#ifdef FEAT_MBYTE
14853 if (has_mbyte)
14854 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014855 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014856 cpstr = instr;
14857 cplen = inlen;
14858 idx = 0;
14859 for (p = fromstr; *p != NUL; p += fromlen)
14860 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014861 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014862 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
14863 {
14864 for (p = tostr; *p != NUL; p += tolen)
14865 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014866 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014867 if (idx-- == 0)
14868 {
14869 cplen = tolen;
14870 cpstr = p;
14871 break;
14872 }
14873 }
14874 if (*p == NUL) /* tostr is shorter than fromstr */
14875 goto error;
14876 break;
14877 }
14878 ++idx;
14879 }
14880
14881 if (first && cpstr == instr)
14882 {
14883 /* Check that fromstr and tostr have the same number of
14884 * (multi-byte) characters. Done only once when a character
14885 * of instr doesn't appear in fromstr. */
14886 first = FALSE;
14887 for (p = tostr; *p != NUL; p += tolen)
14888 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014889 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014890 --idx;
14891 }
14892 if (idx != 0)
14893 goto error;
14894 }
14895
14896 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000014897 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014898 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014899
14900 instr += inlen;
14901 }
14902 else
14903#endif
14904 {
14905 /* When not using multi-byte chars we can do it faster. */
14906 p = vim_strchr(fromstr, *instr);
14907 if (p != NULL)
14908 ga_append(&ga, tostr[p - fromstr]);
14909 else
14910 ga_append(&ga, *instr);
14911 ++instr;
14912 }
14913 }
14914
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014915 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014916}
14917
14918/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014919 * "type(expr)" function
14920 */
14921 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014922f_type(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{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014926 int n;
14927
14928 switch (argvars[0].v_type)
14929 {
14930 case VAR_NUMBER: n = 0; break;
14931 case VAR_STRING: n = 1; break;
14932 case VAR_FUNC: n = 2; break;
14933 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014934 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014935 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
14936 }
14937 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014938}
14939
14940/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014941 * "values(dict)" function
14942 */
14943 static void
14944f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014945 typval_T *argvars;
14946 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014947{
14948 dict_list(argvars, rettv, 1);
14949}
14950
14951/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014952 * "virtcol(string)" function
14953 */
14954 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014955f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014956 typval_T *argvars;
14957 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014958{
14959 colnr_T vcol = 0;
14960 pos_T *fp;
14961
14962 fp = var2fpos(&argvars[0], FALSE);
14963 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count)
14964 {
14965 getvvcol(curwin, fp, NULL, NULL, &vcol);
14966 ++vcol;
14967 }
14968
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014969 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014970}
14971
14972/*
14973 * "visualmode()" function
14974 */
14975/*ARGSUSED*/
14976 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014977f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014978 typval_T *argvars;
14979 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014980{
14981#ifdef FEAT_VISUAL
14982 char_u str[2];
14983
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014984 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014985 str[0] = curbuf->b_visual_mode_eval;
14986 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014987 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014988
14989 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014990 if ((argvars[0].v_type == VAR_NUMBER
14991 && argvars[0].vval.v_number != 0)
14992 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014993 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014994 curbuf->b_visual_mode_eval = NUL;
14995#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014996 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014997#endif
14998}
14999
15000/*
15001 * "winbufnr(nr)" function
15002 */
15003 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015004f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015005 typval_T *argvars;
15006 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015007{
15008 win_T *wp;
15009
15010 wp = find_win_by_nr(&argvars[0]);
15011 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015012 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015013 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015014 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015015}
15016
15017/*
15018 * "wincol()" function
15019 */
15020/*ARGSUSED*/
15021 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015022f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015023 typval_T *argvars;
15024 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015025{
15026 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015027 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015028}
15029
15030/*
15031 * "winheight(nr)" function
15032 */
15033 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015034f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015035 typval_T *argvars;
15036 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015037{
15038 win_T *wp;
15039
15040 wp = find_win_by_nr(&argvars[0]);
15041 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015042 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015043 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015044 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015045}
15046
15047/*
15048 * "winline()" function
15049 */
15050/*ARGSUSED*/
15051 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015052f_winline(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 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015057 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015058}
15059
15060/*
15061 * "winnr()" function
15062 */
15063/* ARGSUSED */
15064 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015065f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015066 typval_T *argvars;
15067 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015068{
15069 int nr = 1;
15070#ifdef FEAT_WINDOWS
15071 win_T *wp;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015072 win_T *twin = curwin;
15073 char_u *arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015074
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015075 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015076 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015077 arg = get_tv_string_chk(&argvars[0]);
15078 if (arg == NULL)
15079 nr = 0; /* type error; errmsg already given */
15080 else if (STRCMP(arg, "$") == 0)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015081 twin = lastwin;
15082 else if (STRCMP(arg, "#") == 0)
15083 {
15084 twin = prevwin;
15085 if (prevwin == NULL)
15086 nr = 0;
15087 }
15088 else
15089 {
15090 EMSG2(_(e_invexpr2), arg);
15091 nr = 0;
15092 }
15093 }
15094
15095 if (nr > 0)
15096 for (wp = firstwin; wp != twin; wp = wp->w_next)
15097 ++nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015098#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015099 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015100}
15101
15102/*
15103 * "winrestcmd()" function
15104 */
15105/* ARGSUSED */
15106 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015107f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015108 typval_T *argvars;
15109 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015110{
15111#ifdef FEAT_WINDOWS
15112 win_T *wp;
15113 int winnr = 1;
15114 garray_T ga;
15115 char_u buf[50];
15116
15117 ga_init2(&ga, (int)sizeof(char), 70);
15118 for (wp = firstwin; wp != NULL; wp = wp->w_next)
15119 {
15120 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
15121 ga_concat(&ga, buf);
15122# ifdef FEAT_VERTSPLIT
15123 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
15124 ga_concat(&ga, buf);
15125# endif
15126 ++winnr;
15127 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000015128 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015129
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015130 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015131#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015132 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015133#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015134 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015135}
15136
15137/*
15138 * "winwidth(nr)" function
15139 */
15140 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015141f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015142 typval_T *argvars;
15143 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015144{
15145 win_T *wp;
15146
15147 wp = find_win_by_nr(&argvars[0]);
15148 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015149 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015150 else
15151#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015152 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015153#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015154 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015155#endif
15156}
15157
Bram Moolenaar071d4272004-06-13 20:20:40 +000015158/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015159 * "writefile()" function
15160 */
15161 static void
15162f_writefile(argvars, rettv)
15163 typval_T *argvars;
15164 typval_T *rettv;
15165{
15166 int binary = FALSE;
15167 char_u *fname;
15168 FILE *fd;
15169 listitem_T *li;
15170 char_u *s;
15171 int ret = 0;
15172 int c;
15173
15174 if (argvars[0].v_type != VAR_LIST)
15175 {
15176 EMSG2(_(e_listarg), "writefile()");
15177 return;
15178 }
15179 if (argvars[0].vval.v_list == NULL)
15180 return;
15181
15182 if (argvars[2].v_type != VAR_UNKNOWN
15183 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
15184 binary = TRUE;
15185
15186 /* Always open the file in binary mode, library functions have a mind of
15187 * their own about CR-LF conversion. */
15188 fname = get_tv_string(&argvars[1]);
15189 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
15190 {
15191 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
15192 ret = -1;
15193 }
15194 else
15195 {
15196 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
15197 li = li->li_next)
15198 {
15199 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
15200 {
15201 if (*s == '\n')
15202 c = putc(NUL, fd);
15203 else
15204 c = putc(*s, fd);
15205 if (c == EOF)
15206 {
15207 ret = -1;
15208 break;
15209 }
15210 }
15211 if (!binary || li->li_next != NULL)
15212 if (putc('\n', fd) == EOF)
15213 {
15214 ret = -1;
15215 break;
15216 }
15217 if (ret < 0)
15218 {
15219 EMSG(_(e_write));
15220 break;
15221 }
15222 }
15223 fclose(fd);
15224 }
15225
15226 rettv->vval.v_number = ret;
15227}
15228
15229/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015230 * Translate a String variable into a position.
15231 */
15232 static pos_T *
15233var2fpos(varp, lnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000015234 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015235 int lnum; /* TRUE when $ is last line */
15236{
15237 char_u *name;
15238 static pos_T pos;
15239 pos_T *pp;
15240
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015241 name = get_tv_string_chk(varp);
15242 if (name == NULL)
15243 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015244 if (name[0] == '.') /* cursor */
15245 return &curwin->w_cursor;
15246 if (name[0] == '\'') /* mark */
15247 {
15248 pp = getmark(name[1], FALSE);
15249 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
15250 return NULL;
15251 return pp;
15252 }
15253 if (name[0] == '$') /* last column or line */
15254 {
15255 if (lnum)
15256 {
15257 pos.lnum = curbuf->b_ml.ml_line_count;
15258 pos.col = 0;
15259 }
15260 else
15261 {
15262 pos.lnum = curwin->w_cursor.lnum;
15263 pos.col = (colnr_T)STRLEN(ml_get_curline());
15264 }
15265 return &pos;
15266 }
15267 return NULL;
15268}
15269
15270/*
15271 * Get the length of an environment variable name.
15272 * Advance "arg" to the first character after the name.
15273 * Return 0 for error.
15274 */
15275 static int
15276get_env_len(arg)
15277 char_u **arg;
15278{
15279 char_u *p;
15280 int len;
15281
15282 for (p = *arg; vim_isIDc(*p); ++p)
15283 ;
15284 if (p == *arg) /* no name found */
15285 return 0;
15286
15287 len = (int)(p - *arg);
15288 *arg = p;
15289 return len;
15290}
15291
15292/*
15293 * Get the length of the name of a function or internal variable.
15294 * "arg" is advanced to the first non-white character after the name.
15295 * Return 0 if something is wrong.
15296 */
15297 static int
15298get_id_len(arg)
15299 char_u **arg;
15300{
15301 char_u *p;
15302 int len;
15303
15304 /* Find the end of the name. */
15305 for (p = *arg; eval_isnamec(*p); ++p)
15306 ;
15307 if (p == *arg) /* no name found */
15308 return 0;
15309
15310 len = (int)(p - *arg);
15311 *arg = skipwhite(p);
15312
15313 return len;
15314}
15315
15316/*
Bram Moolenaara7043832005-01-21 11:56:39 +000015317 * Get the length of the name of a variable or function.
15318 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000015319 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015320 * Return -1 if curly braces expansion failed.
15321 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015322 * If the name contains 'magic' {}'s, expand them and return the
15323 * expanded name in an allocated string via 'alias' - caller must free.
15324 */
15325 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015326get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015327 char_u **arg;
15328 char_u **alias;
15329 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015330 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015331{
15332 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015333 char_u *p;
15334 char_u *expr_start;
15335 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015336
15337 *alias = NULL; /* default to no alias */
15338
15339 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
15340 && (*arg)[2] == (int)KE_SNR)
15341 {
15342 /* hard coded <SNR>, already translated */
15343 *arg += 3;
15344 return get_id_len(arg) + 3;
15345 }
15346 len = eval_fname_script(*arg);
15347 if (len > 0)
15348 {
15349 /* literal "<SID>", "s:" or "<SNR>" */
15350 *arg += len;
15351 }
15352
Bram Moolenaar071d4272004-06-13 20:20:40 +000015353 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015354 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015355 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015356 p = find_name_end(*arg, &expr_start, &expr_end,
15357 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015358 if (expr_start != NULL)
15359 {
15360 char_u *temp_string;
15361
15362 if (!evaluate)
15363 {
15364 len += (int)(p - *arg);
15365 *arg = skipwhite(p);
15366 return len;
15367 }
15368
15369 /*
15370 * Include any <SID> etc in the expanded string:
15371 * Thus the -len here.
15372 */
15373 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
15374 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015375 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015376 *alias = temp_string;
15377 *arg = skipwhite(p);
15378 return (int)STRLEN(temp_string);
15379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015380
15381 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015382 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015383 EMSG2(_(e_invexpr2), *arg);
15384
15385 return len;
15386}
15387
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015388/*
15389 * Find the end of a variable or function name, taking care of magic braces.
15390 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
15391 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015392 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015393 * Return a pointer to just after the name. Equal to "arg" if there is no
15394 * valid name.
15395 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015396 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015397find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015398 char_u *arg;
15399 char_u **expr_start;
15400 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015401 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015402{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015403 int mb_nest = 0;
15404 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015405 char_u *p;
15406
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015407 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015408 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015409 *expr_start = NULL;
15410 *expr_end = NULL;
15411 }
15412
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015413 /* Quick check for valid starting character. */
15414 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
15415 return arg;
15416
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015417 for (p = arg; *p != NUL
15418 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015419 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015420 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015421 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000015422 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015423 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000015424 if (*p == '\'')
15425 {
15426 /* skip over 'string' to avoid counting [ and ] inside it. */
15427 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
15428 ;
15429 if (*p == NUL)
15430 break;
15431 }
15432 else if (*p == '"')
15433 {
15434 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
15435 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
15436 if (*p == '\\' && p[1] != NUL)
15437 ++p;
15438 if (*p == NUL)
15439 break;
15440 }
15441
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015442 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015443 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015444 if (*p == '[')
15445 ++br_nest;
15446 else if (*p == ']')
15447 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015448 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000015449
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015450 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015451 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015452 if (*p == '{')
15453 {
15454 mb_nest++;
15455 if (expr_start != NULL && *expr_start == NULL)
15456 *expr_start = p;
15457 }
15458 else if (*p == '}')
15459 {
15460 mb_nest--;
15461 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
15462 *expr_end = p;
15463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015465 }
15466
15467 return p;
15468}
15469
15470/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015471 * Expands out the 'magic' {}'s in a variable/function name.
15472 * Note that this can call itself recursively, to deal with
15473 * constructs like foo{bar}{baz}{bam}
15474 * The four pointer arguments point to "foo{expre}ss{ion}bar"
15475 * "in_start" ^
15476 * "expr_start" ^
15477 * "expr_end" ^
15478 * "in_end" ^
15479 *
15480 * Returns a new allocated string, which the caller must free.
15481 * Returns NULL for failure.
15482 */
15483 static char_u *
15484make_expanded_name(in_start, expr_start, expr_end, in_end)
15485 char_u *in_start;
15486 char_u *expr_start;
15487 char_u *expr_end;
15488 char_u *in_end;
15489{
15490 char_u c1;
15491 char_u *retval = NULL;
15492 char_u *temp_result;
15493 char_u *nextcmd = NULL;
15494
15495 if (expr_end == NULL || in_end == NULL)
15496 return NULL;
15497 *expr_start = NUL;
15498 *expr_end = NUL;
15499 c1 = *in_end;
15500 *in_end = NUL;
15501
15502 temp_result = eval_to_string(expr_start + 1, &nextcmd);
15503 if (temp_result != NULL && nextcmd == NULL)
15504 {
15505 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
15506 + (in_end - expr_end) + 1));
15507 if (retval != NULL)
15508 {
15509 STRCPY(retval, in_start);
15510 STRCAT(retval, temp_result);
15511 STRCAT(retval, expr_end + 1);
15512 }
15513 }
15514 vim_free(temp_result);
15515
15516 *in_end = c1; /* put char back for error messages */
15517 *expr_start = '{';
15518 *expr_end = '}';
15519
15520 if (retval != NULL)
15521 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015522 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015523 if (expr_start != NULL)
15524 {
15525 /* Further expansion! */
15526 temp_result = make_expanded_name(retval, expr_start,
15527 expr_end, temp_result);
15528 vim_free(retval);
15529 retval = temp_result;
15530 }
15531 }
15532
15533 return retval;
15534}
15535
15536/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015537 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000015538 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015539 */
15540 static int
15541eval_isnamec(c)
15542 int c;
15543{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015544 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
15545}
15546
15547/*
15548 * Return TRUE if character "c" can be used as the first character in a
15549 * variable or function name (excluding '{' and '}').
15550 */
15551 static int
15552eval_isnamec1(c)
15553 int c;
15554{
15555 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000015556}
15557
15558/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015559 * Set number v: variable to "val".
15560 */
15561 void
15562set_vim_var_nr(idx, val)
15563 int idx;
15564 long val;
15565{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015566 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015567}
15568
15569/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015570 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015571 */
15572 long
15573get_vim_var_nr(idx)
15574 int idx;
15575{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015576 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015577}
15578
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015579#if defined(FEAT_AUTOCMD) || defined(PROTO)
15580/*
15581 * Get string v: variable value. Uses a static buffer, can only be used once.
15582 */
15583 char_u *
15584get_vim_var_str(idx)
15585 int idx;
15586{
15587 return get_tv_string(&vimvars[idx].vv_tv);
15588}
15589#endif
15590
Bram Moolenaar071d4272004-06-13 20:20:40 +000015591/*
15592 * Set v:count, v:count1 and v:prevcount.
15593 */
15594 void
15595set_vcount(count, count1)
15596 long count;
15597 long count1;
15598{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015599 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
15600 vimvars[VV_COUNT].vv_nr = count;
15601 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015602}
15603
15604/*
15605 * Set string v: variable to a copy of "val".
15606 */
15607 void
15608set_vim_var_string(idx, val, len)
15609 int idx;
15610 char_u *val;
15611 int len; /* length of "val" to use or -1 (whole string) */
15612{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015613 /* Need to do this (at least) once, since we can't initialize a union.
15614 * Will always be invoked when "v:progname" is set. */
15615 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
15616
Bram Moolenaare9a41262005-01-15 22:18:47 +000015617 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015618 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015619 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015620 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015621 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015622 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000015623 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015624}
15625
15626/*
15627 * Set v:register if needed.
15628 */
15629 void
15630set_reg_var(c)
15631 int c;
15632{
15633 char_u regname;
15634
15635 if (c == 0 || c == ' ')
15636 regname = '"';
15637 else
15638 regname = c;
15639 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000015640 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015641 set_vim_var_string(VV_REG, &regname, 1);
15642}
15643
15644/*
15645 * Get or set v:exception. If "oldval" == NULL, return the current value.
15646 * Otherwise, restore the value to "oldval" and return NULL.
15647 * Must always be called in pairs to save and restore v:exception! Does not
15648 * take care of memory allocations.
15649 */
15650 char_u *
15651v_exception(oldval)
15652 char_u *oldval;
15653{
15654 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015655 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015656
Bram Moolenaare9a41262005-01-15 22:18:47 +000015657 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015658 return NULL;
15659}
15660
15661/*
15662 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
15663 * Otherwise, restore the value to "oldval" and return NULL.
15664 * Must always be called in pairs to save and restore v:throwpoint! Does not
15665 * take care of memory allocations.
15666 */
15667 char_u *
15668v_throwpoint(oldval)
15669 char_u *oldval;
15670{
15671 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015672 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015673
Bram Moolenaare9a41262005-01-15 22:18:47 +000015674 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015675 return NULL;
15676}
15677
15678#if defined(FEAT_AUTOCMD) || defined(PROTO)
15679/*
15680 * Set v:cmdarg.
15681 * If "eap" != NULL, use "eap" to generate the value and return the old value.
15682 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
15683 * Must always be called in pairs!
15684 */
15685 char_u *
15686set_cmdarg(eap, oldarg)
15687 exarg_T *eap;
15688 char_u *oldarg;
15689{
15690 char_u *oldval;
15691 char_u *newval;
15692 unsigned len;
15693
Bram Moolenaare9a41262005-01-15 22:18:47 +000015694 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015695 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015696 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015697 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000015698 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015699 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015700 }
15701
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015702 if (eap->force_bin == FORCE_BIN)
15703 len = 6;
15704 else if (eap->force_bin == FORCE_NOBIN)
15705 len = 8;
15706 else
15707 len = 0;
15708 if (eap->force_ff != 0)
15709 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
15710# ifdef FEAT_MBYTE
15711 if (eap->force_enc != 0)
15712 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
15713# endif
15714
15715 newval = alloc(len + 1);
15716 if (newval == NULL)
15717 return NULL;
15718
15719 if (eap->force_bin == FORCE_BIN)
15720 sprintf((char *)newval, " ++bin");
15721 else if (eap->force_bin == FORCE_NOBIN)
15722 sprintf((char *)newval, " ++nobin");
15723 else
15724 *newval = NUL;
15725 if (eap->force_ff != 0)
15726 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
15727 eap->cmd + eap->force_ff);
15728# ifdef FEAT_MBYTE
15729 if (eap->force_enc != 0)
15730 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
15731 eap->cmd + eap->force_enc);
15732# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000015733 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015734 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015735}
15736#endif
15737
15738/*
15739 * Get the value of internal variable "name".
15740 * Return OK or FAIL.
15741 */
15742 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015743get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015744 char_u *name;
15745 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000015746 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015747 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015748{
15749 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000015750 typval_T *tv = NULL;
15751 typval_T atv;
15752 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015753 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015754
15755 /* truncate the name, so that we can use strcmp() */
15756 cc = name[len];
15757 name[len] = NUL;
15758
15759 /*
15760 * Check for "b:changedtick".
15761 */
15762 if (STRCMP(name, "b:changedtick") == 0)
15763 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000015764 atv.v_type = VAR_NUMBER;
15765 atv.vval.v_number = curbuf->b_changedtick;
15766 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015767 }
15768
15769 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015770 * Check for user-defined variables.
15771 */
15772 else
15773 {
Bram Moolenaara7043832005-01-21 11:56:39 +000015774 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015775 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000015776 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015777 }
15778
Bram Moolenaare9a41262005-01-15 22:18:47 +000015779 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015780 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015781 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015782 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015783 ret = FAIL;
15784 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015785 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015786 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015787
15788 name[len] = cc;
15789
15790 return ret;
15791}
15792
15793/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015794 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
15795 * Also handle function call with Funcref variable: func(expr)
15796 * Can all be combined: dict.func(expr)[idx]['func'](expr)
15797 */
15798 static int
15799handle_subscript(arg, rettv, evaluate, verbose)
15800 char_u **arg;
15801 typval_T *rettv;
15802 int evaluate; /* do more than finding the end */
15803 int verbose; /* give error messages */
15804{
15805 int ret = OK;
15806 dict_T *selfdict = NULL;
15807 char_u *s;
15808 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000015809 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015810
15811 while (ret == OK
15812 && (**arg == '['
15813 || (**arg == '.' && rettv->v_type == VAR_DICT)
15814 || (**arg == '(' && rettv->v_type == VAR_FUNC))
15815 && !vim_iswhite(*(*arg - 1)))
15816 {
15817 if (**arg == '(')
15818 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000015819 /* need to copy the funcref so that we can clear rettv */
15820 functv = *rettv;
15821 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015822
15823 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000015824 s = functv.vval.v_string;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015825 ret = get_func_tv(s, STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000015826 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
15827 &len, evaluate, selfdict);
15828
15829 /* Clear the funcref afterwards, so that deleting it while
15830 * evaluating the arguments is possible (see test55). */
15831 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015832
15833 /* Stop the expression evaluation when immediately aborting on
15834 * error, or when an interrupt occurred or an exception was thrown
15835 * but not caught. */
15836 if (aborting())
15837 {
15838 if (ret == OK)
15839 clear_tv(rettv);
15840 ret = FAIL;
15841 }
15842 dict_unref(selfdict);
15843 selfdict = NULL;
15844 }
15845 else /* **arg == '[' || **arg == '.' */
15846 {
15847 dict_unref(selfdict);
15848 if (rettv->v_type == VAR_DICT)
15849 {
15850 selfdict = rettv->vval.v_dict;
15851 if (selfdict != NULL)
15852 ++selfdict->dv_refcount;
15853 }
15854 else
15855 selfdict = NULL;
15856 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
15857 {
15858 clear_tv(rettv);
15859 ret = FAIL;
15860 }
15861 }
15862 }
15863 dict_unref(selfdict);
15864 return ret;
15865}
15866
15867/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015868 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
15869 * value).
15870 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015871 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015872alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015873{
Bram Moolenaar33570922005-01-25 22:26:29 +000015874 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015875}
15876
15877/*
15878 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015879 * The string "s" must have been allocated, it is consumed.
15880 * Return NULL for out of memory, the variable otherwise.
15881 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015882 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015883alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015884 char_u *s;
15885{
Bram Moolenaar33570922005-01-25 22:26:29 +000015886 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015887
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015888 rettv = alloc_tv();
15889 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015890 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015891 rettv->v_type = VAR_STRING;
15892 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015893 }
15894 else
15895 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015896 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015897}
15898
15899/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015900 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015901 */
15902 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015903free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015904 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015905{
15906 if (varp != NULL)
15907 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015908 switch (varp->v_type)
15909 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015910 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015911 func_unref(varp->vval.v_string);
15912 /*FALLTHROUGH*/
15913 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015914 vim_free(varp->vval.v_string);
15915 break;
15916 case VAR_LIST:
15917 list_unref(varp->vval.v_list);
15918 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015919 case VAR_DICT:
15920 dict_unref(varp->vval.v_dict);
15921 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015922 case VAR_NUMBER:
15923 case VAR_UNKNOWN:
15924 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015925 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000015926 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015927 break;
15928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015929 vim_free(varp);
15930 }
15931}
15932
15933/*
15934 * Free the memory for a variable value and set the value to NULL or 0.
15935 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000015936 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015937clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015938 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015939{
15940 if (varp != NULL)
15941 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015942 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015943 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015944 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015945 func_unref(varp->vval.v_string);
15946 /*FALLTHROUGH*/
15947 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015948 vim_free(varp->vval.v_string);
15949 varp->vval.v_string = NULL;
15950 break;
15951 case VAR_LIST:
15952 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015953 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015954 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015955 case VAR_DICT:
15956 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015957 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015958 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015959 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015960 varp->vval.v_number = 0;
15961 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015962 case VAR_UNKNOWN:
15963 break;
15964 default:
15965 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000015966 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015967 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015968 }
15969}
15970
15971/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015972 * Set the value of a variable to NULL without freeing items.
15973 */
15974 static void
15975init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015976 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015977{
15978 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000015979 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015980}
15981
15982/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015983 * Get the number value of a variable.
15984 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015985 * For incompatible types, return 0.
15986 * get_tv_number_chk() is similar to get_tv_number(), but informs the
15987 * caller of incompatible types: it sets *denote to TRUE if "denote"
15988 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015989 */
15990 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015991get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015992 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015993{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015994 int error = FALSE;
15995
15996 return get_tv_number_chk(varp, &error); /* return 0L on error */
15997}
15998
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015999 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016000get_tv_number_chk(varp, denote)
16001 typval_T *varp;
16002 int *denote;
16003{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016004 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016005
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016006 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016007 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016008 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016009 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016010 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016011 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016012 break;
16013 case VAR_STRING:
16014 if (varp->vval.v_string != NULL)
16015 vim_str2nr(varp->vval.v_string, NULL, NULL,
16016 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016017 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016018 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000016019 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016020 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016021 case VAR_DICT:
16022 EMSG(_("E728: Using a Dictionary as a number"));
16023 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016024 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016025 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016026 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016027 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016028 if (denote == NULL) /* useful for values that must be unsigned */
16029 n = -1;
16030 else
16031 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016032 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016033}
16034
16035/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000016036 * Get the lnum from the first argument.
16037 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016038 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016039 */
16040 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016041get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000016042 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016043{
Bram Moolenaar33570922005-01-25 22:26:29 +000016044 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016045 linenr_T lnum;
16046
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016047 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016048 if (lnum == 0) /* no valid number, try using line() */
16049 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016050 rettv.v_type = VAR_NUMBER;
16051 f_line(argvars, &rettv);
16052 lnum = rettv.vval.v_number;
16053 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016054 }
16055 return lnum;
16056}
16057
16058/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000016059 * Get the lnum from the first argument.
16060 * Also accepts "$", then "buf" is used.
16061 * Returns 0 on error.
16062 */
16063 static linenr_T
16064get_tv_lnum_buf(argvars, buf)
16065 typval_T *argvars;
16066 buf_T *buf;
16067{
16068 if (argvars[0].v_type == VAR_STRING
16069 && argvars[0].vval.v_string != NULL
16070 && argvars[0].vval.v_string[0] == '$'
16071 && buf != NULL)
16072 return buf->b_ml.ml_line_count;
16073 return get_tv_number_chk(&argvars[0], NULL);
16074}
16075
16076/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016077 * Get the string value of a variable.
16078 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000016079 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
16080 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016081 * If the String variable has never been set, return an empty string.
16082 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016083 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
16084 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016085 */
16086 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016087get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016088 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016089{
16090 static char_u mybuf[NUMBUFLEN];
16091
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016092 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016093}
16094
16095 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016096get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000016097 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016098 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016099{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016100 char_u *res = get_tv_string_buf_chk(varp, buf);
16101
16102 return res != NULL ? res : (char_u *)"";
16103}
16104
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016105 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016106get_tv_string_chk(varp)
16107 typval_T *varp;
16108{
16109 static char_u mybuf[NUMBUFLEN];
16110
16111 return get_tv_string_buf_chk(varp, mybuf);
16112}
16113
16114 static char_u *
16115get_tv_string_buf_chk(varp, buf)
16116 typval_T *varp;
16117 char_u *buf;
16118{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016119 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016120 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016121 case VAR_NUMBER:
16122 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
16123 return buf;
16124 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016125 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016126 break;
16127 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016128 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016129 break;
16130 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016131 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016132 break;
16133 case VAR_STRING:
16134 if (varp->vval.v_string != NULL)
16135 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016136 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016137 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016138 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016139 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016140 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016141 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016142}
16143
16144/*
16145 * Find variable "name" in the list of variables.
16146 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016147 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000016148 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000016149 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016150 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016151 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000016152find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016153 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000016154 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016155{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016156 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016157 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016158
Bram Moolenaara7043832005-01-21 11:56:39 +000016159 ht = find_var_ht(name, &varname);
16160 if (htp != NULL)
16161 *htp = ht;
16162 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016163 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016164 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016165}
16166
16167/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016168 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000016169 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016170 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016171 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016172find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000016173 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000016174 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016175 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000016176{
Bram Moolenaar33570922005-01-25 22:26:29 +000016177 hashitem_T *hi;
16178
16179 if (*varname == NUL)
16180 {
16181 /* Must be something like "s:", otherwise "ht" would be NULL. */
16182 switch (varname[-2])
16183 {
16184 case 's': return &SCRIPT_SV(current_SID).sv_var;
16185 case 'g': return &globvars_var;
16186 case 'v': return &vimvars_var;
16187 case 'b': return &curbuf->b_bufvar;
16188 case 'w': return &curwin->w_winvar;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016189 case 'l': return current_funccal == NULL
16190 ? NULL : &current_funccal->l_vars_var;
16191 case 'a': return current_funccal == NULL
16192 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000016193 }
16194 return NULL;
16195 }
Bram Moolenaara7043832005-01-21 11:56:39 +000016196
16197 hi = hash_find(ht, varname);
16198 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016199 {
16200 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000016201 * worked find the variable again. Don't auto-load a script if it was
16202 * loaded already, otherwise it would be loaded every time when
16203 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016204 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000016205 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016206 hi = hash_find(ht, varname);
16207 if (HASHITEM_EMPTY(hi))
16208 return NULL;
16209 }
Bram Moolenaar33570922005-01-25 22:26:29 +000016210 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000016211}
16212
16213/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016214 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000016215 * Set "varname" to the start of name without ':'.
16216 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016217 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000016218find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016219 char_u *name;
16220 char_u **varname;
16221{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000016222 hashitem_T *hi;
16223
Bram Moolenaar071d4272004-06-13 20:20:40 +000016224 if (name[1] != ':')
16225 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016226 /* The name must not start with a colon or #. */
16227 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016228 return NULL;
16229 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016230
16231 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000016232 hi = hash_find(&compat_hashtab, name);
16233 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000016234 return &compat_hashtab;
16235
Bram Moolenaar071d4272004-06-13 20:20:40 +000016236 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016237 return &globvarht; /* global variable */
16238 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016239 }
16240 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016241 if (*name == 'g') /* global variable */
16242 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016243 /* There must be no ':' or '#' in the rest of the name, unless g: is used
16244 */
16245 if (vim_strchr(name + 2, ':') != NULL
16246 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016247 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016248 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000016249 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016250 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000016251 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000016252 if (*name == 'v') /* v: variable */
16253 return &vimvarht;
16254 if (*name == 'a' && current_funccal != NULL) /* function argument */
16255 return &current_funccal->l_avars.dv_hashtab;
16256 if (*name == 'l' && current_funccal != NULL) /* local function variable */
16257 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016258 if (*name == 's' /* script variable */
16259 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
16260 return &SCRIPT_VARS(current_SID);
16261 return NULL;
16262}
16263
16264/*
16265 * Get the string value of a (global/local) variable.
16266 * Returns NULL when it doesn't exist.
16267 */
16268 char_u *
16269get_var_value(name)
16270 char_u *name;
16271{
Bram Moolenaar33570922005-01-25 22:26:29 +000016272 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016273
Bram Moolenaara7043832005-01-21 11:56:39 +000016274 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016275 if (v == NULL)
16276 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000016277 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016278}
16279
16280/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016281 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000016282 * sourcing this script and when executing functions defined in the script.
16283 */
16284 void
16285new_script_vars(id)
16286 scid_T id;
16287{
Bram Moolenaara7043832005-01-21 11:56:39 +000016288 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000016289 hashtab_T *ht;
16290 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000016291
Bram Moolenaar071d4272004-06-13 20:20:40 +000016292 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
16293 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016294 /* Re-allocating ga_data means that an ht_array pointing to
16295 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000016296 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000016297 for (i = 1; i <= ga_scripts.ga_len; ++i)
16298 {
16299 ht = &SCRIPT_VARS(i);
16300 if (ht->ht_mask == HT_INIT_SIZE - 1)
16301 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000016302 sv = &SCRIPT_SV(i);
16303 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000016304 }
16305
Bram Moolenaar071d4272004-06-13 20:20:40 +000016306 while (ga_scripts.ga_len < id)
16307 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016308 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
16309 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016310 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016311 }
16312 }
16313}
16314
16315/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016316 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
16317 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016318 */
16319 void
Bram Moolenaar33570922005-01-25 22:26:29 +000016320init_var_dict(dict, dict_var)
16321 dict_T *dict;
16322 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016323{
Bram Moolenaar33570922005-01-25 22:26:29 +000016324 hash_init(&dict->dv_hashtab);
16325 dict->dv_refcount = 99999;
16326 dict_var->di_tv.vval.v_dict = dict;
16327 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016328 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016329 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
16330 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016331}
16332
16333/*
16334 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000016335 * Frees all allocated variables and the value they contain.
16336 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016337 */
16338 void
Bram Moolenaara7043832005-01-21 11:56:39 +000016339vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000016340 hashtab_T *ht;
16341{
16342 vars_clear_ext(ht, TRUE);
16343}
16344
16345/*
16346 * Like vars_clear(), but only free the value if "free_val" is TRUE.
16347 */
16348 static void
16349vars_clear_ext(ht, free_val)
16350 hashtab_T *ht;
16351 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016352{
Bram Moolenaara7043832005-01-21 11:56:39 +000016353 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000016354 hashitem_T *hi;
16355 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016356
Bram Moolenaar33570922005-01-25 22:26:29 +000016357 hash_lock(ht);
Bram Moolenaara7043832005-01-21 11:56:39 +000016358 todo = ht->ht_used;
16359 for (hi = ht->ht_array; todo > 0; ++hi)
16360 {
16361 if (!HASHITEM_EMPTY(hi))
16362 {
16363 --todo;
16364
Bram Moolenaar33570922005-01-25 22:26:29 +000016365 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000016366 * ht_array might change then. hash_clear() takes care of it
16367 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016368 v = HI2DI(hi);
16369 if (free_val)
16370 clear_tv(&v->di_tv);
16371 if ((v->di_flags & DI_FLAGS_FIX) == 0)
16372 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000016373 }
16374 }
16375 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016376 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016377}
16378
Bram Moolenaara7043832005-01-21 11:56:39 +000016379/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016380 * Delete a variable from hashtab "ht" at item "hi".
16381 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000016382 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016383 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000016384delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000016385 hashtab_T *ht;
16386 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016387{
Bram Moolenaar33570922005-01-25 22:26:29 +000016388 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000016389
16390 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000016391 clear_tv(&di->di_tv);
16392 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016393}
16394
16395/*
16396 * List the value of one internal variable.
16397 */
16398 static void
16399list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000016400 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016401 char_u *prefix;
16402{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016403 char_u *tofree;
16404 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016405 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016406
Bram Moolenaar33570922005-01-25 22:26:29 +000016407 s = echo_string(&v->di_tv, &tofree, numbuf);
16408 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016409 s == NULL ? (char_u *)"" : s);
16410 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016411}
16412
Bram Moolenaar071d4272004-06-13 20:20:40 +000016413 static void
16414list_one_var_a(prefix, name, type, string)
16415 char_u *prefix;
16416 char_u *name;
16417 int type;
16418 char_u *string;
16419{
16420 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
16421 if (name != NULL) /* "a:" vars don't have a name stored */
16422 msg_puts(name);
16423 msg_putchar(' ');
16424 msg_advance(22);
16425 if (type == VAR_NUMBER)
16426 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016427 else if (type == VAR_FUNC)
16428 msg_putchar('*');
16429 else if (type == VAR_LIST)
16430 {
16431 msg_putchar('[');
16432 if (*string == '[')
16433 ++string;
16434 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016435 else if (type == VAR_DICT)
16436 {
16437 msg_putchar('{');
16438 if (*string == '{')
16439 ++string;
16440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016441 else
16442 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016443
Bram Moolenaar071d4272004-06-13 20:20:40 +000016444 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016445
16446 if (type == VAR_FUNC)
16447 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016448}
16449
16450/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016451 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000016452 * If the variable already exists, the value is updated.
16453 * Otherwise the variable is created.
16454 */
16455 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016456set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016457 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000016458 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016459 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016460{
Bram Moolenaar33570922005-01-25 22:26:29 +000016461 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016462 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016463 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000016464 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016465
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016466 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016467 {
16468 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
16469 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
16470 ? name[2] : name[0]))
16471 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016472 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016473 return;
16474 }
16475 if (function_exists(name))
16476 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016477 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016478 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016479 return;
16480 }
16481 }
16482
Bram Moolenaara7043832005-01-21 11:56:39 +000016483 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000016484 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000016485 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000016486 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000016487 return;
16488 }
16489
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016490 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016491 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016492 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016493 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016494 if (var_check_ro(v->di_flags, name)
16495 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000016496 return;
16497 if (v->di_tv.v_type != tv->v_type
16498 && !((v->di_tv.v_type == VAR_STRING
16499 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016500 && (tv->v_type == VAR_STRING
16501 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016502 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016503 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016504 return;
16505 }
Bram Moolenaar33570922005-01-25 22:26:29 +000016506
16507 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000016508 * Handle setting internal v: variables separately: we don't change
16509 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000016510 */
16511 if (ht == &vimvarht)
16512 {
16513 if (v->di_tv.v_type == VAR_STRING)
16514 {
16515 vim_free(v->di_tv.vval.v_string);
16516 if (copy || tv->v_type != VAR_STRING)
16517 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
16518 else
16519 {
16520 /* Take over the string to avoid an extra alloc/free. */
16521 v->di_tv.vval.v_string = tv->vval.v_string;
16522 tv->vval.v_string = NULL;
16523 }
16524 }
16525 else if (v->di_tv.v_type != VAR_NUMBER)
16526 EMSG2(_(e_intern2), "set_var()");
16527 else
16528 v->di_tv.vval.v_number = get_tv_number(tv);
16529 return;
16530 }
16531
16532 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016533 }
16534 else /* add a new variable */
16535 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000016536 /* Make sure the variable name is valid. */
16537 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000016538 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
16539 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000016540 {
16541 EMSG2(_(e_illvar), varname);
16542 return;
16543 }
16544
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016545 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
16546 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000016547 if (v == NULL)
16548 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000016549 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000016550 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016551 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016552 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016553 return;
16554 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016555 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016556 }
Bram Moolenaara7043832005-01-21 11:56:39 +000016557
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016558 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000016559 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016560 else
16561 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016562 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016563 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016564 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016565 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016566}
16567
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016568/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016569 * Return TRUE if di_flags "flags" indicate read-only variable "name".
16570 * Also give an error message.
16571 */
16572 static int
16573var_check_ro(flags, name)
16574 int flags;
16575 char_u *name;
16576{
16577 if (flags & DI_FLAGS_RO)
16578 {
16579 EMSG2(_(e_readonlyvar), name);
16580 return TRUE;
16581 }
16582 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
16583 {
16584 EMSG2(_(e_readonlysbx), name);
16585 return TRUE;
16586 }
16587 return FALSE;
16588}
16589
16590/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016591 * Return TRUE if typeval "tv" is set to be locked (immutable).
16592 * Also give an error message, using "name".
16593 */
16594 static int
16595tv_check_lock(lock, name)
16596 int lock;
16597 char_u *name;
16598{
16599 if (lock & VAR_LOCKED)
16600 {
16601 EMSG2(_("E741: Value is locked: %s"),
16602 name == NULL ? (char_u *)_("Unknown") : name);
16603 return TRUE;
16604 }
16605 if (lock & VAR_FIXED)
16606 {
16607 EMSG2(_("E742: Cannot change value of %s"),
16608 name == NULL ? (char_u *)_("Unknown") : name);
16609 return TRUE;
16610 }
16611 return FALSE;
16612}
16613
16614/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016615 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016616 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016617 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016618 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016619 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016620copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000016621 typval_T *from;
16622 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016623{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016624 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016625 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016626 switch (from->v_type)
16627 {
16628 case VAR_NUMBER:
16629 to->vval.v_number = from->vval.v_number;
16630 break;
16631 case VAR_STRING:
16632 case VAR_FUNC:
16633 if (from->vval.v_string == NULL)
16634 to->vval.v_string = NULL;
16635 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016636 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016637 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016638 if (from->v_type == VAR_FUNC)
16639 func_ref(to->vval.v_string);
16640 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016641 break;
16642 case VAR_LIST:
16643 if (from->vval.v_list == NULL)
16644 to->vval.v_list = NULL;
16645 else
16646 {
16647 to->vval.v_list = from->vval.v_list;
16648 ++to->vval.v_list->lv_refcount;
16649 }
16650 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016651 case VAR_DICT:
16652 if (from->vval.v_dict == NULL)
16653 to->vval.v_dict = NULL;
16654 else
16655 {
16656 to->vval.v_dict = from->vval.v_dict;
16657 ++to->vval.v_dict->dv_refcount;
16658 }
16659 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016660 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016661 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016662 break;
16663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016664}
16665
16666/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000016667 * Make a copy of an item.
16668 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016669 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
16670 * reference to an already copied list/dict can be used.
16671 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016672 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016673 static int
16674item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000016675 typval_T *from;
16676 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016677 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016678 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016679{
16680 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016681 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016682
Bram Moolenaar33570922005-01-25 22:26:29 +000016683 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016684 {
16685 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016686 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016687 }
16688 ++recurse;
16689
16690 switch (from->v_type)
16691 {
16692 case VAR_NUMBER:
16693 case VAR_STRING:
16694 case VAR_FUNC:
16695 copy_tv(from, to);
16696 break;
16697 case VAR_LIST:
16698 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016699 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016700 if (from->vval.v_list == NULL)
16701 to->vval.v_list = NULL;
16702 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
16703 {
16704 /* use the copy made earlier */
16705 to->vval.v_list = from->vval.v_list->lv_copylist;
16706 ++to->vval.v_list->lv_refcount;
16707 }
16708 else
16709 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
16710 if (to->vval.v_list == NULL)
16711 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016712 break;
16713 case VAR_DICT:
16714 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016715 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016716 if (from->vval.v_dict == NULL)
16717 to->vval.v_dict = NULL;
16718 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
16719 {
16720 /* use the copy made earlier */
16721 to->vval.v_dict = from->vval.v_dict->dv_copydict;
16722 ++to->vval.v_dict->dv_refcount;
16723 }
16724 else
16725 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
16726 if (to->vval.v_dict == NULL)
16727 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016728 break;
16729 default:
16730 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016731 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016732 }
16733 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016734 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016735}
16736
16737/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016738 * ":echo expr1 ..." print each argument separated with a space, add a
16739 * newline at the end.
16740 * ":echon expr1 ..." print each argument plain.
16741 */
16742 void
16743ex_echo(eap)
16744 exarg_T *eap;
16745{
16746 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000016747 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016748 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016749 char_u *p;
16750 int needclr = TRUE;
16751 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016752 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016753
16754 if (eap->skip)
16755 ++emsg_skip;
16756 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
16757 {
16758 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016759 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016760 {
16761 /*
16762 * Report the invalid expression unless the expression evaluation
16763 * has been cancelled due to an aborting error, an interrupt, or an
16764 * exception.
16765 */
16766 if (!aborting())
16767 EMSG2(_(e_invexpr2), p);
16768 break;
16769 }
16770 if (!eap->skip)
16771 {
16772 if (atstart)
16773 {
16774 atstart = FALSE;
16775 /* Call msg_start() after eval1(), evaluating the expression
16776 * may cause a message to appear. */
16777 if (eap->cmdidx == CMD_echo)
16778 msg_start();
16779 }
16780 else if (eap->cmdidx == CMD_echo)
16781 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016782 p = echo_string(&rettv, &tofree, numbuf);
16783 if (p != NULL)
16784 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016785 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016786 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016787 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016788 if (*p != TAB && needclr)
16789 {
16790 /* remove any text still there from the command */
16791 msg_clr_eos();
16792 needclr = FALSE;
16793 }
16794 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016795 }
16796 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016797 {
16798#ifdef FEAT_MBYTE
16799 if (has_mbyte)
16800 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016801 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016802
16803 (void)msg_outtrans_len_attr(p, i, echo_attr);
16804 p += i - 1;
16805 }
16806 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000016807#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016808 (void)msg_outtrans_len_attr(p, 1, echo_attr);
16809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016810 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016811 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016812 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016813 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016814 arg = skipwhite(arg);
16815 }
16816 eap->nextcmd = check_nextcmd(arg);
16817
16818 if (eap->skip)
16819 --emsg_skip;
16820 else
16821 {
16822 /* remove text that may still be there from the command */
16823 if (needclr)
16824 msg_clr_eos();
16825 if (eap->cmdidx == CMD_echo)
16826 msg_end();
16827 }
16828}
16829
16830/*
16831 * ":echohl {name}".
16832 */
16833 void
16834ex_echohl(eap)
16835 exarg_T *eap;
16836{
16837 int id;
16838
16839 id = syn_name2id(eap->arg);
16840 if (id == 0)
16841 echo_attr = 0;
16842 else
16843 echo_attr = syn_id2attr(id);
16844}
16845
16846/*
16847 * ":execute expr1 ..." execute the result of an expression.
16848 * ":echomsg expr1 ..." Print a message
16849 * ":echoerr expr1 ..." Print an error
16850 * Each gets spaces around each argument and a newline at the end for
16851 * echo commands
16852 */
16853 void
16854ex_execute(eap)
16855 exarg_T *eap;
16856{
16857 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000016858 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016859 int ret = OK;
16860 char_u *p;
16861 garray_T ga;
16862 int len;
16863 int save_did_emsg;
16864
16865 ga_init2(&ga, 1, 80);
16866
16867 if (eap->skip)
16868 ++emsg_skip;
16869 while (*arg != NUL && *arg != '|' && *arg != '\n')
16870 {
16871 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016872 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016873 {
16874 /*
16875 * Report the invalid expression unless the expression evaluation
16876 * has been cancelled due to an aborting error, an interrupt, or an
16877 * exception.
16878 */
16879 if (!aborting())
16880 EMSG2(_(e_invexpr2), p);
16881 ret = FAIL;
16882 break;
16883 }
16884
16885 if (!eap->skip)
16886 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016887 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016888 len = (int)STRLEN(p);
16889 if (ga_grow(&ga, len + 2) == FAIL)
16890 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016891 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016892 ret = FAIL;
16893 break;
16894 }
16895 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016896 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016897 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016898 ga.ga_len += len;
16899 }
16900
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016901 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016902 arg = skipwhite(arg);
16903 }
16904
16905 if (ret != FAIL && ga.ga_data != NULL)
16906 {
16907 if (eap->cmdidx == CMD_echomsg)
16908 MSG_ATTR(ga.ga_data, echo_attr);
16909 else if (eap->cmdidx == CMD_echoerr)
16910 {
16911 /* We don't want to abort following commands, restore did_emsg. */
16912 save_did_emsg = did_emsg;
16913 EMSG((char_u *)ga.ga_data);
16914 if (!force_abort)
16915 did_emsg = save_did_emsg;
16916 }
16917 else if (eap->cmdidx == CMD_execute)
16918 do_cmdline((char_u *)ga.ga_data,
16919 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
16920 }
16921
16922 ga_clear(&ga);
16923
16924 if (eap->skip)
16925 --emsg_skip;
16926
16927 eap->nextcmd = check_nextcmd(arg);
16928}
16929
16930/*
16931 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
16932 * "arg" points to the "&" or '+' when called, to "option" when returning.
16933 * Returns NULL when no option name found. Otherwise pointer to the char
16934 * after the option name.
16935 */
16936 static char_u *
16937find_option_end(arg, opt_flags)
16938 char_u **arg;
16939 int *opt_flags;
16940{
16941 char_u *p = *arg;
16942
16943 ++p;
16944 if (*p == 'g' && p[1] == ':')
16945 {
16946 *opt_flags = OPT_GLOBAL;
16947 p += 2;
16948 }
16949 else if (*p == 'l' && p[1] == ':')
16950 {
16951 *opt_flags = OPT_LOCAL;
16952 p += 2;
16953 }
16954 else
16955 *opt_flags = 0;
16956
16957 if (!ASCII_ISALPHA(*p))
16958 return NULL;
16959 *arg = p;
16960
16961 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
16962 p += 4; /* termcap option */
16963 else
16964 while (ASCII_ISALPHA(*p))
16965 ++p;
16966 return p;
16967}
16968
16969/*
16970 * ":function"
16971 */
16972 void
16973ex_function(eap)
16974 exarg_T *eap;
16975{
16976 char_u *theline;
16977 int j;
16978 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016979 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016980 char_u *name = NULL;
16981 char_u *p;
16982 char_u *arg;
16983 garray_T newargs;
16984 garray_T newlines;
16985 int varargs = FALSE;
16986 int mustend = FALSE;
16987 int flags = 0;
16988 ufunc_T *fp;
16989 int indent;
16990 int nesting;
16991 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000016992 dictitem_T *v;
16993 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016994 static int func_nr = 0; /* number for nameless function */
16995 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016996 hashtab_T *ht;
16997 int todo;
16998 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016999
17000 /*
17001 * ":function" without argument: list functions.
17002 */
17003 if (ends_excmd(*eap->arg))
17004 {
17005 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017006 {
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000017007 todo = func_hashtab.ht_used;
17008 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017009 {
17010 if (!HASHITEM_EMPTY(hi))
17011 {
17012 --todo;
17013 fp = HI2UF(hi);
17014 if (!isdigit(*fp->uf_name))
17015 list_func_head(fp, FALSE);
17016 }
17017 }
17018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017019 eap->nextcmd = check_nextcmd(eap->arg);
17020 return;
17021 }
17022
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017023 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017024 * ":function /pat": list functions matching pattern.
17025 */
17026 if (*eap->arg == '/')
17027 {
17028 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
17029 if (!eap->skip)
17030 {
17031 regmatch_T regmatch;
17032
17033 c = *p;
17034 *p = NUL;
17035 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
17036 *p = c;
17037 if (regmatch.regprog != NULL)
17038 {
17039 regmatch.rm_ic = p_ic;
17040
17041 todo = func_hashtab.ht_used;
17042 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
17043 {
17044 if (!HASHITEM_EMPTY(hi))
17045 {
17046 --todo;
17047 fp = HI2UF(hi);
17048 if (!isdigit(*fp->uf_name)
17049 && vim_regexec(&regmatch, fp->uf_name, 0))
17050 list_func_head(fp, FALSE);
17051 }
17052 }
17053 }
17054 }
17055 if (*p == '/')
17056 ++p;
17057 eap->nextcmd = check_nextcmd(p);
17058 return;
17059 }
17060
17061 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017062 * Get the function name. There are these situations:
17063 * func normal function name
17064 * "name" == func, "fudi.fd_dict" == NULL
17065 * dict.func new dictionary entry
17066 * "name" == NULL, "fudi.fd_dict" set,
17067 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
17068 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017069 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017070 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
17071 * dict.func existing dict entry that's not a Funcref
17072 * "name" == NULL, "fudi.fd_dict" set,
17073 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
17074 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017075 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017076 name = trans_function_name(&p, eap->skip, 0, &fudi);
17077 paren = (vim_strchr(p, '(') != NULL);
17078 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017079 {
17080 /*
17081 * Return on an invalid expression in braces, unless the expression
17082 * evaluation has been cancelled due to an aborting error, an
17083 * interrupt, or an exception.
17084 */
17085 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017086 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017087 if (!eap->skip && fudi.fd_newkey != NULL)
17088 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017089 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017090 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017092 else
17093 eap->skip = TRUE;
17094 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017095 /* An error in a function call during evaluation of an expression in magic
17096 * braces should not cause the function not to be defined. */
17097 saved_did_emsg = did_emsg;
17098 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017099
17100 /*
17101 * ":function func" with only function name: list function.
17102 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017103 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017104 {
17105 if (!ends_excmd(*skipwhite(p)))
17106 {
17107 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017108 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017109 }
17110 eap->nextcmd = check_nextcmd(p);
17111 if (eap->nextcmd != NULL)
17112 *p = NUL;
17113 if (!eap->skip && !got_int)
17114 {
17115 fp = find_func(name);
17116 if (fp != NULL)
17117 {
17118 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017119 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017120 {
17121 msg_putchar('\n');
17122 msg_outnum((long)(j + 1));
17123 if (j < 9)
17124 msg_putchar(' ');
17125 if (j < 99)
17126 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017127 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017128 out_flush(); /* show a line at a time */
17129 ui_breakcheck();
17130 }
17131 if (!got_int)
17132 {
17133 msg_putchar('\n');
17134 msg_puts((char_u *)" endfunction");
17135 }
17136 }
17137 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017138 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017139 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017140 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017141 }
17142
17143 /*
17144 * ":function name(arg1, arg2)" Define function.
17145 */
17146 p = skipwhite(p);
17147 if (*p != '(')
17148 {
17149 if (!eap->skip)
17150 {
17151 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017152 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017153 }
17154 /* attempt to continue by skipping some text */
17155 if (vim_strchr(p, '(') != NULL)
17156 p = vim_strchr(p, '(');
17157 }
17158 p = skipwhite(p + 1);
17159
17160 ga_init2(&newargs, (int)sizeof(char_u *), 3);
17161 ga_init2(&newlines, (int)sizeof(char_u *), 3);
17162
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017163 if (!eap->skip)
17164 {
17165 /* Check the name of the function. */
17166 if (name != NULL)
17167 arg = name;
17168 else
17169 arg = fudi.fd_newkey;
17170 if (arg != NULL)
17171 {
17172 if (*arg == K_SPECIAL)
17173 j = 3;
17174 else
17175 j = 0;
17176 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
17177 : eval_isnamec(arg[j])))
17178 ++j;
17179 if (arg[j] != NUL)
17180 emsg_funcname(_(e_invarg2), arg);
17181 }
17182 }
17183
Bram Moolenaar071d4272004-06-13 20:20:40 +000017184 /*
17185 * Isolate the arguments: "arg1, arg2, ...)"
17186 */
17187 while (*p != ')')
17188 {
17189 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
17190 {
17191 varargs = TRUE;
17192 p += 3;
17193 mustend = TRUE;
17194 }
17195 else
17196 {
17197 arg = p;
17198 while (ASCII_ISALNUM(*p) || *p == '_')
17199 ++p;
17200 if (arg == p || isdigit(*arg)
17201 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
17202 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
17203 {
17204 if (!eap->skip)
17205 EMSG2(_("E125: Illegal argument: %s"), arg);
17206 break;
17207 }
17208 if (ga_grow(&newargs, 1) == FAIL)
17209 goto erret;
17210 c = *p;
17211 *p = NUL;
17212 arg = vim_strsave(arg);
17213 if (arg == NULL)
17214 goto erret;
17215 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
17216 *p = c;
17217 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017218 if (*p == ',')
17219 ++p;
17220 else
17221 mustend = TRUE;
17222 }
17223 p = skipwhite(p);
17224 if (mustend && *p != ')')
17225 {
17226 if (!eap->skip)
17227 EMSG2(_(e_invarg2), eap->arg);
17228 break;
17229 }
17230 }
17231 ++p; /* skip the ')' */
17232
Bram Moolenaare9a41262005-01-15 22:18:47 +000017233 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017234 for (;;)
17235 {
17236 p = skipwhite(p);
17237 if (STRNCMP(p, "range", 5) == 0)
17238 {
17239 flags |= FC_RANGE;
17240 p += 5;
17241 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000017242 else if (STRNCMP(p, "dict", 4) == 0)
17243 {
17244 flags |= FC_DICT;
17245 p += 4;
17246 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017247 else if (STRNCMP(p, "abort", 5) == 0)
17248 {
17249 flags |= FC_ABORT;
17250 p += 5;
17251 }
17252 else
17253 break;
17254 }
17255
17256 if (*p != NUL && *p != '"' && *p != '\n' && !eap->skip && !did_emsg)
17257 EMSG(_(e_trailing));
17258
17259 /*
17260 * Read the body of the function, until ":endfunction" is found.
17261 */
17262 if (KeyTyped)
17263 {
17264 /* Check if the function already exists, don't let the user type the
17265 * whole function before telling him it doesn't work! For a script we
17266 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017267 if (!eap->skip && !eap->forceit)
17268 {
17269 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
17270 EMSG(_(e_funcdict));
17271 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017272 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017274
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017275 if (!eap->skip && did_emsg)
17276 goto erret;
17277
Bram Moolenaar071d4272004-06-13 20:20:40 +000017278 msg_putchar('\n'); /* don't overwrite the function name */
17279 cmdline_row = msg_row;
17280 }
17281
17282 indent = 2;
17283 nesting = 0;
17284 for (;;)
17285 {
17286 msg_scroll = TRUE;
17287 need_wait_return = FALSE;
17288 if (eap->getline == NULL)
17289 theline = getcmdline(':', 0L, indent);
17290 else
17291 theline = eap->getline(':', eap->cookie, indent);
17292 if (KeyTyped)
17293 lines_left = Rows - 1;
17294 if (theline == NULL)
17295 {
17296 EMSG(_("E126: Missing :endfunction"));
17297 goto erret;
17298 }
17299
17300 if (skip_until != NULL)
17301 {
17302 /* between ":append" and "." and between ":python <<EOF" and "EOF"
17303 * don't check for ":endfunc". */
17304 if (STRCMP(theline, skip_until) == 0)
17305 {
17306 vim_free(skip_until);
17307 skip_until = NULL;
17308 }
17309 }
17310 else
17311 {
17312 /* skip ':' and blanks*/
17313 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
17314 ;
17315
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017316 /* Check for "endfunction". */
17317 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017318 {
17319 vim_free(theline);
17320 break;
17321 }
17322
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017323 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000017324 * at "end". */
17325 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
17326 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017327 else if (STRNCMP(p, "if", 2) == 0
17328 || STRNCMP(p, "wh", 2) == 0
17329 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000017330 || STRNCMP(p, "try", 3) == 0)
17331 indent += 2;
17332
17333 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017334 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017335 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017336 if (*p == '!')
17337 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017338 p += eval_fname_script(p);
17339 if (ASCII_ISALPHA(*p))
17340 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017341 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017342 if (*skipwhite(p) == '(')
17343 {
17344 ++nesting;
17345 indent += 2;
17346 }
17347 }
17348 }
17349
17350 /* Check for ":append" or ":insert". */
17351 p = skip_range(p, NULL);
17352 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
17353 || (p[0] == 'i'
17354 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
17355 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
17356 skip_until = vim_strsave((char_u *)".");
17357
17358 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
17359 arg = skipwhite(skiptowhite(p));
17360 if (arg[0] == '<' && arg[1] =='<'
17361 && ((p[0] == 'p' && p[1] == 'y'
17362 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
17363 || (p[0] == 'p' && p[1] == 'e'
17364 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
17365 || (p[0] == 't' && p[1] == 'c'
17366 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
17367 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
17368 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000017369 || (p[0] == 'm' && p[1] == 'z'
17370 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017371 ))
17372 {
17373 /* ":python <<" continues until a dot, like ":append" */
17374 p = skipwhite(arg + 2);
17375 if (*p == NUL)
17376 skip_until = vim_strsave((char_u *)".");
17377 else
17378 skip_until = vim_strsave(p);
17379 }
17380 }
17381
17382 /* Add the line to the function. */
17383 if (ga_grow(&newlines, 1) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017384 {
17385 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017386 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017387 }
17388
17389 /* Copy the line to newly allocated memory. get_one_sourceline()
17390 * allocates 250 bytes per line, this saves 80% on average. The cost
17391 * is an extra alloc/free. */
17392 p = vim_strsave(theline);
17393 if (p != NULL)
17394 {
17395 vim_free(theline);
17396 theline = p;
17397 }
17398
Bram Moolenaar071d4272004-06-13 20:20:40 +000017399 ((char_u **)(newlines.ga_data))[newlines.ga_len] = theline;
17400 newlines.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017401 }
17402
17403 /* Don't define the function when skipping commands or when an error was
17404 * detected. */
17405 if (eap->skip || did_emsg)
17406 goto erret;
17407
17408 /*
17409 * If there are no errors, add the function
17410 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017411 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017412 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017413 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000017414 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017415 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017416 emsg_funcname("E707: Function name conflicts with variable: %s",
17417 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017418 goto erret;
17419 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017420
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017421 fp = find_func(name);
17422 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017423 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017424 if (!eap->forceit)
17425 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017426 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017427 goto erret;
17428 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017429 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017430 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017431 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017432 name);
17433 goto erret;
17434 }
17435 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017436 ga_clear_strings(&(fp->uf_args));
17437 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017438 vim_free(name);
17439 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017441 }
17442 else
17443 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017444 char numbuf[20];
17445
17446 fp = NULL;
17447 if (fudi.fd_newkey == NULL && !eap->forceit)
17448 {
17449 EMSG(_(e_funcdict));
17450 goto erret;
17451 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000017452 if (fudi.fd_di == NULL)
17453 {
17454 /* Can't add a function to a locked dictionary */
17455 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
17456 goto erret;
17457 }
17458 /* Can't change an existing function if it is locked */
17459 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
17460 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017461
17462 /* Give the function a sequential number. Can only be used with a
17463 * Funcref! */
17464 vim_free(name);
17465 sprintf(numbuf, "%d", ++func_nr);
17466 name = vim_strsave((char_u *)numbuf);
17467 if (name == NULL)
17468 goto erret;
17469 }
17470
17471 if (fp == NULL)
17472 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017473 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017474 {
17475 int slen, plen;
17476 char_u *scriptname;
17477
17478 /* Check that the autoload name matches the script name. */
17479 j = FAIL;
17480 if (sourcing_name != NULL)
17481 {
17482 scriptname = autoload_name(name);
17483 if (scriptname != NULL)
17484 {
17485 p = vim_strchr(scriptname, '/');
17486 plen = STRLEN(p);
17487 slen = STRLEN(sourcing_name);
17488 if (slen > plen && fnamecmp(p,
17489 sourcing_name + slen - plen) == 0)
17490 j = OK;
17491 vim_free(scriptname);
17492 }
17493 }
17494 if (j == FAIL)
17495 {
17496 EMSG2(_("E746: Function name does not match script file name: %s"), name);
17497 goto erret;
17498 }
17499 }
17500
17501 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017502 if (fp == NULL)
17503 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017504
17505 if (fudi.fd_dict != NULL)
17506 {
17507 if (fudi.fd_di == NULL)
17508 {
17509 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017510 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017511 if (fudi.fd_di == NULL)
17512 {
17513 vim_free(fp);
17514 goto erret;
17515 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017516 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
17517 {
17518 vim_free(fudi.fd_di);
17519 goto erret;
17520 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017521 }
17522 else
17523 /* overwrite existing dict entry */
17524 clear_tv(&fudi.fd_di->di_tv);
17525 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017526 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017527 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017528 fp->uf_refcount = 1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017529 }
17530
Bram Moolenaar071d4272004-06-13 20:20:40 +000017531 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017532 STRCPY(fp->uf_name, name);
17533 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017534 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017535 fp->uf_args = newargs;
17536 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017537#ifdef FEAT_PROFILE
17538 fp->uf_tml_count = NULL;
17539 fp->uf_tml_total = NULL;
17540 fp->uf_tml_self = NULL;
17541 fp->uf_profiling = FALSE;
17542 if (prof_def_func())
17543 func_do_profile(fp);
17544#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017545 fp->uf_varargs = varargs;
17546 fp->uf_flags = flags;
17547 fp->uf_calls = 0;
17548 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017549 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017550
17551erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017552 ga_clear_strings(&newargs);
17553 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017554ret_free:
17555 vim_free(skip_until);
17556 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017557 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017558 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017559}
17560
17561/*
17562 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000017563 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017564 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017565 * flags:
17566 * TFN_INT: internal function name OK
17567 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000017568 * Advances "pp" to just after the function name (if no error).
17569 */
17570 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017571trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017572 char_u **pp;
17573 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017574 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000017575 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017576{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017577 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017578 char_u *start;
17579 char_u *end;
17580 int lead;
17581 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017582 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000017583 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017584
17585 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017586 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017587 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000017588
17589 /* Check for hard coded <SNR>: already translated function ID (from a user
17590 * command). */
17591 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
17592 && (*pp)[2] == (int)KE_SNR)
17593 {
17594 *pp += 3;
17595 len = get_id_len(pp) + 3;
17596 return vim_strnsave(start, len);
17597 }
17598
17599 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
17600 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017601 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000017602 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017603 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017604
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017605 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
17606 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017607 if (end == start)
17608 {
17609 if (!skip)
17610 EMSG(_("E129: Function name required"));
17611 goto theend;
17612 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017613 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017614 {
17615 /*
17616 * Report an invalid expression in braces, unless the expression
17617 * evaluation has been cancelled due to an aborting error, an
17618 * interrupt, or an exception.
17619 */
17620 if (!aborting())
17621 {
17622 if (end != NULL)
17623 EMSG2(_(e_invarg2), start);
17624 }
17625 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017626 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017627 goto theend;
17628 }
17629
17630 if (lv.ll_tv != NULL)
17631 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017632 if (fdp != NULL)
17633 {
17634 fdp->fd_dict = lv.ll_dict;
17635 fdp->fd_newkey = lv.ll_newkey;
17636 lv.ll_newkey = NULL;
17637 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017638 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017639 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
17640 {
17641 name = vim_strsave(lv.ll_tv->vval.v_string);
17642 *pp = end;
17643 }
17644 else
17645 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017646 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
17647 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017648 EMSG(_(e_funcref));
17649 else
17650 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017651 name = NULL;
17652 }
17653 goto theend;
17654 }
17655
17656 if (lv.ll_name == NULL)
17657 {
17658 /* Error found, but continue after the function name. */
17659 *pp = end;
17660 goto theend;
17661 }
17662
17663 if (lv.ll_exp_name != NULL)
17664 len = STRLEN(lv.ll_exp_name);
17665 else
Bram Moolenaara7043832005-01-21 11:56:39 +000017666 {
17667 if (lead == 2) /* skip over "s:" */
17668 lv.ll_name += 2;
17669 len = (int)(end - lv.ll_name);
17670 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017671
17672 /*
17673 * Copy the function name to allocated memory.
17674 * Accept <SID>name() inside a script, translate into <SNR>123_name().
17675 * Accept <SNR>123_name() outside a script.
17676 */
17677 if (skip)
17678 lead = 0; /* do nothing */
17679 else if (lead > 0)
17680 {
17681 lead = 3;
17682 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
17683 {
17684 if (current_SID <= 0)
17685 {
17686 EMSG(_(e_usingsid));
17687 goto theend;
17688 }
17689 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
17690 lead += (int)STRLEN(sid_buf);
17691 }
17692 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017693 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017694 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017695 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017696 goto theend;
17697 }
17698 name = alloc((unsigned)(len + lead + 1));
17699 if (name != NULL)
17700 {
17701 if (lead > 0)
17702 {
17703 name[0] = K_SPECIAL;
17704 name[1] = KS_EXTRA;
17705 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000017706 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017707 STRCPY(name + 3, sid_buf);
17708 }
17709 mch_memmove(name + lead, lv.ll_name, (size_t)len);
17710 name[len + lead] = NUL;
17711 }
17712 *pp = end;
17713
17714theend:
17715 clear_lval(&lv);
17716 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017717}
17718
17719/*
17720 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
17721 * Return 2 if "p" starts with "s:".
17722 * Return 0 otherwise.
17723 */
17724 static int
17725eval_fname_script(p)
17726 char_u *p;
17727{
17728 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
17729 || STRNICMP(p + 1, "SNR>", 4) == 0))
17730 return 5;
17731 if (p[0] == 's' && p[1] == ':')
17732 return 2;
17733 return 0;
17734}
17735
17736/*
17737 * Return TRUE if "p" starts with "<SID>" or "s:".
17738 * Only works if eval_fname_script() returned non-zero for "p"!
17739 */
17740 static int
17741eval_fname_sid(p)
17742 char_u *p;
17743{
17744 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
17745}
17746
17747/*
17748 * List the head of the function: "name(arg1, arg2)".
17749 */
17750 static void
17751list_func_head(fp, indent)
17752 ufunc_T *fp;
17753 int indent;
17754{
17755 int j;
17756
17757 msg_start();
17758 if (indent)
17759 MSG_PUTS(" ");
17760 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017761 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017762 {
17763 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017764 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017765 }
17766 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017767 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017768 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017769 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017770 {
17771 if (j)
17772 MSG_PUTS(", ");
17773 msg_puts(FUNCARG(fp, j));
17774 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017775 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017776 {
17777 if (j)
17778 MSG_PUTS(", ");
17779 MSG_PUTS("...");
17780 }
17781 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000017782 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000017783 if (p_verbose > 0)
17784 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017785}
17786
17787/*
17788 * Find a function by name, return pointer to it in ufuncs.
17789 * Return NULL for unknown function.
17790 */
17791 static ufunc_T *
17792find_func(name)
17793 char_u *name;
17794{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017795 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017796
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017797 hi = hash_find(&func_hashtab, name);
17798 if (!HASHITEM_EMPTY(hi))
17799 return HI2UF(hi);
17800 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017801}
17802
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017803#if defined(EXITFREE) || defined(PROTO)
17804 void
17805free_all_functions()
17806{
17807 hashitem_T *hi;
17808
17809 /* Need to start all over every time, because func_free() may change the
17810 * hash table. */
17811 while (func_hashtab.ht_used > 0)
17812 for (hi = func_hashtab.ht_array; ; ++hi)
17813 if (!HASHITEM_EMPTY(hi))
17814 {
17815 func_free(HI2UF(hi));
17816 break;
17817 }
17818}
17819#endif
17820
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017821/*
17822 * Return TRUE if a function "name" exists.
17823 */
17824 static int
17825function_exists(name)
17826 char_u *name;
17827{
17828 char_u *p = name;
17829 int n = FALSE;
17830
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017831 p = trans_function_name(&p, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017832 if (p != NULL)
17833 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017834 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017835 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017836 else
17837 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017838 vim_free(p);
17839 }
17840 return n;
17841}
17842
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017843/*
17844 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017845 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017846 */
17847 static int
17848builtin_function(name)
17849 char_u *name;
17850{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017851 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
17852 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017853}
17854
Bram Moolenaar05159a02005-02-26 23:04:13 +000017855#if defined(FEAT_PROFILE) || defined(PROTO)
17856/*
17857 * Start profiling function "fp".
17858 */
17859 static void
17860func_do_profile(fp)
17861 ufunc_T *fp;
17862{
17863 fp->uf_tm_count = 0;
17864 profile_zero(&fp->uf_tm_self);
17865 profile_zero(&fp->uf_tm_total);
17866 if (fp->uf_tml_count == NULL)
17867 fp->uf_tml_count = (int *)alloc_clear((unsigned)
17868 (sizeof(int) * fp->uf_lines.ga_len));
17869 if (fp->uf_tml_total == NULL)
17870 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
17871 (sizeof(proftime_T) * fp->uf_lines.ga_len));
17872 if (fp->uf_tml_self == NULL)
17873 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
17874 (sizeof(proftime_T) * fp->uf_lines.ga_len));
17875 fp->uf_tml_idx = -1;
17876 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
17877 || fp->uf_tml_self == NULL)
17878 return; /* out of memory */
17879
17880 fp->uf_profiling = TRUE;
17881}
17882
17883/*
17884 * Dump the profiling results for all functions in file "fd".
17885 */
17886 void
17887func_dump_profile(fd)
17888 FILE *fd;
17889{
17890 hashitem_T *hi;
17891 int todo;
17892 ufunc_T *fp;
17893 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000017894 ufunc_T **sorttab;
17895 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017896
17897 todo = func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000017898 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
17899
Bram Moolenaar05159a02005-02-26 23:04:13 +000017900 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
17901 {
17902 if (!HASHITEM_EMPTY(hi))
17903 {
17904 --todo;
17905 fp = HI2UF(hi);
17906 if (fp->uf_profiling)
17907 {
Bram Moolenaar73830342005-02-28 22:48:19 +000017908 if (sorttab != NULL)
17909 sorttab[st_len++] = fp;
17910
Bram Moolenaar05159a02005-02-26 23:04:13 +000017911 if (fp->uf_name[0] == K_SPECIAL)
17912 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
17913 else
17914 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
17915 if (fp->uf_tm_count == 1)
17916 fprintf(fd, "Called 1 time\n");
17917 else
17918 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
17919 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
17920 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
17921 fprintf(fd, "\n");
17922 fprintf(fd, "count total (s) self (s)\n");
17923
17924 for (i = 0; i < fp->uf_lines.ga_len; ++i)
17925 {
Bram Moolenaar73830342005-02-28 22:48:19 +000017926 prof_func_line(fd, fp->uf_tml_count[i],
17927 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017928 fprintf(fd, "%s\n", FUNCLINE(fp, i));
17929 }
17930 fprintf(fd, "\n");
17931 }
17932 }
17933 }
Bram Moolenaar73830342005-02-28 22:48:19 +000017934
17935 if (sorttab != NULL && st_len > 0)
17936 {
17937 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
17938 prof_total_cmp);
17939 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
17940 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
17941 prof_self_cmp);
17942 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
17943 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000017944}
Bram Moolenaar73830342005-02-28 22:48:19 +000017945
17946 static void
17947prof_sort_list(fd, sorttab, st_len, title, prefer_self)
17948 FILE *fd;
17949 ufunc_T **sorttab;
17950 int st_len;
17951 char *title;
17952 int prefer_self; /* when equal print only self time */
17953{
17954 int i;
17955 ufunc_T *fp;
17956
17957 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
17958 fprintf(fd, "count total (s) self (s) function\n");
17959 for (i = 0; i < 20 && i < st_len; ++i)
17960 {
17961 fp = sorttab[i];
17962 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
17963 prefer_self);
17964 if (fp->uf_name[0] == K_SPECIAL)
17965 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
17966 else
17967 fprintf(fd, " %s()\n", fp->uf_name);
17968 }
17969 fprintf(fd, "\n");
17970}
17971
17972/*
17973 * Print the count and times for one function or function line.
17974 */
17975 static void
17976prof_func_line(fd, count, total, self, prefer_self)
17977 FILE *fd;
17978 int count;
17979 proftime_T *total;
17980 proftime_T *self;
17981 int prefer_self; /* when equal print only self time */
17982{
17983 if (count > 0)
17984 {
17985 fprintf(fd, "%5d ", count);
17986 if (prefer_self && profile_equal(total, self))
17987 fprintf(fd, " ");
17988 else
17989 fprintf(fd, "%s ", profile_msg(total));
17990 if (!prefer_self && profile_equal(total, self))
17991 fprintf(fd, " ");
17992 else
17993 fprintf(fd, "%s ", profile_msg(self));
17994 }
17995 else
17996 fprintf(fd, " ");
17997}
17998
17999/*
18000 * Compare function for total time sorting.
18001 */
18002 static int
18003#ifdef __BORLANDC__
18004_RTLENTRYF
18005#endif
18006prof_total_cmp(s1, s2)
18007 const void *s1;
18008 const void *s2;
18009{
18010 ufunc_T *p1, *p2;
18011
18012 p1 = *(ufunc_T **)s1;
18013 p2 = *(ufunc_T **)s2;
18014 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
18015}
18016
18017/*
18018 * Compare function for self time sorting.
18019 */
18020 static int
18021#ifdef __BORLANDC__
18022_RTLENTRYF
18023#endif
18024prof_self_cmp(s1, s2)
18025 const void *s1;
18026 const void *s2;
18027{
18028 ufunc_T *p1, *p2;
18029
18030 p1 = *(ufunc_T **)s1;
18031 p2 = *(ufunc_T **)s2;
18032 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
18033}
18034
Bram Moolenaar05159a02005-02-26 23:04:13 +000018035#endif
18036
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018037/* The names of packages that once were loaded is remembered. */
18038static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
18039
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018040/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018041 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018042 * Return TRUE if a package was loaded.
18043 */
18044 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018045script_autoload(name, reload)
18046 char_u *name;
18047 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018048{
18049 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018050 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018051 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018052 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018053
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018054 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018055 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018056 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018057 return FALSE;
18058
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018059 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018060
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018061 /* Find the name in the list of previously loaded package names. Skip
18062 * "autoload/", it's always the same. */
18063 for (i = 0; i < ga_loaded.ga_len; ++i)
18064 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
18065 break;
18066 if (!reload && i < ga_loaded.ga_len)
18067 ret = FALSE; /* was loaded already */
18068 else
18069 {
18070 /* Remember the name if it wasn't loaded already. */
18071 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
18072 {
18073 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
18074 tofree = NULL;
18075 }
18076
18077 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000018078 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018079 ret = TRUE;
18080 }
18081
18082 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018083 return ret;
18084}
18085
18086/*
18087 * Return the autoload script name for a function or variable name.
18088 * Returns NULL when out of memory.
18089 */
18090 static char_u *
18091autoload_name(name)
18092 char_u *name;
18093{
18094 char_u *p;
18095 char_u *scriptname;
18096
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018097 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018098 scriptname = alloc((unsigned)(STRLEN(name) + 14));
18099 if (scriptname == NULL)
18100 return FALSE;
18101 STRCPY(scriptname, "autoload/");
18102 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018103 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018104 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018105 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018106 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018107 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018108}
18109
Bram Moolenaar071d4272004-06-13 20:20:40 +000018110#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
18111
18112/*
18113 * Function given to ExpandGeneric() to obtain the list of user defined
18114 * function names.
18115 */
18116 char_u *
18117get_user_func_name(xp, idx)
18118 expand_T *xp;
18119 int idx;
18120{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018121 static long_u done;
18122 static hashitem_T *hi;
18123 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018124
18125 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018126 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018127 done = 0;
18128 hi = func_hashtab.ht_array;
18129 }
18130 if (done < func_hashtab.ht_used)
18131 {
18132 if (done++ > 0)
18133 ++hi;
18134 while (HASHITEM_EMPTY(hi))
18135 ++hi;
18136 fp = HI2UF(hi);
18137
18138 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
18139 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018140
18141 cat_func_name(IObuff, fp);
18142 if (xp->xp_context != EXPAND_USER_FUNC)
18143 {
18144 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018145 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018146 STRCAT(IObuff, ")");
18147 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018148 return IObuff;
18149 }
18150 return NULL;
18151}
18152
18153#endif /* FEAT_CMDL_COMPL */
18154
18155/*
18156 * Copy the function name of "fp" to buffer "buf".
18157 * "buf" must be able to hold the function name plus three bytes.
18158 * Takes care of script-local function names.
18159 */
18160 static void
18161cat_func_name(buf, fp)
18162 char_u *buf;
18163 ufunc_T *fp;
18164{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018165 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018166 {
18167 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018168 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018169 }
18170 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018171 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018172}
18173
18174/*
18175 * ":delfunction {name}"
18176 */
18177 void
18178ex_delfunction(eap)
18179 exarg_T *eap;
18180{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018181 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018182 char_u *p;
18183 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000018184 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018185
18186 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018187 name = trans_function_name(&p, eap->skip, 0, &fudi);
18188 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018189 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018190 {
18191 if (fudi.fd_dict != NULL && !eap->skip)
18192 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018193 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018194 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018195 if (!ends_excmd(*skipwhite(p)))
18196 {
18197 vim_free(name);
18198 EMSG(_(e_trailing));
18199 return;
18200 }
18201 eap->nextcmd = check_nextcmd(p);
18202 if (eap->nextcmd != NULL)
18203 *p = NUL;
18204
18205 if (!eap->skip)
18206 fp = find_func(name);
18207 vim_free(name);
18208
18209 if (!eap->skip)
18210 {
18211 if (fp == NULL)
18212 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000018213 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018214 return;
18215 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018216 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018217 {
18218 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
18219 return;
18220 }
18221
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018222 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018223 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018224 /* Delete the dict item that refers to the function, it will
18225 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018226 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018227 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018228 else
18229 func_free(fp);
18230 }
18231}
18232
18233/*
18234 * Free a function and remove it from the list of functions.
18235 */
18236 static void
18237func_free(fp)
18238 ufunc_T *fp;
18239{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018240 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018241
18242 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018243 ga_clear_strings(&(fp->uf_args));
18244 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000018245#ifdef FEAT_PROFILE
18246 vim_free(fp->uf_tml_count);
18247 vim_free(fp->uf_tml_total);
18248 vim_free(fp->uf_tml_self);
18249#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018250
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018251 /* remove the function from the function hashtable */
18252 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
18253 if (HASHITEM_EMPTY(hi))
18254 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018255 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018256 hash_remove(&func_hashtab, hi);
18257
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018258 vim_free(fp);
18259}
18260
18261/*
18262 * Unreference a Function: decrement the reference count and free it when it
18263 * becomes zero. Only for numbered functions.
18264 */
18265 static void
18266func_unref(name)
18267 char_u *name;
18268{
18269 ufunc_T *fp;
18270
18271 if (name != NULL && isdigit(*name))
18272 {
18273 fp = find_func(name);
18274 if (fp == NULL)
18275 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018276 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018277 {
18278 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018279 * when "uf_calls" becomes zero. */
18280 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018281 func_free(fp);
18282 }
18283 }
18284}
18285
18286/*
18287 * Count a reference to a Function.
18288 */
18289 static void
18290func_ref(name)
18291 char_u *name;
18292{
18293 ufunc_T *fp;
18294
18295 if (name != NULL && isdigit(*name))
18296 {
18297 fp = find_func(name);
18298 if (fp == NULL)
18299 EMSG2(_(e_intern2), "func_ref()");
18300 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018301 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018302 }
18303}
18304
18305/*
18306 * Call a user function.
18307 */
18308 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000018309call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018310 ufunc_T *fp; /* pointer to function */
18311 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000018312 typval_T *argvars; /* arguments */
18313 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018314 linenr_T firstline; /* first line of range */
18315 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000018316 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018317{
Bram Moolenaar33570922005-01-25 22:26:29 +000018318 char_u *save_sourcing_name;
18319 linenr_T save_sourcing_lnum;
18320 scid_T save_current_SID;
18321 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000018322 int save_did_emsg;
18323 static int depth = 0;
18324 dictitem_T *v;
18325 int fixvar_idx = 0; /* index in fixvar[] */
18326 int i;
18327 int ai;
18328 char_u numbuf[NUMBUFLEN];
18329 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018330#ifdef FEAT_PROFILE
18331 proftime_T wait_start;
18332#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018333
18334 /* If depth of calling is getting too high, don't execute the function */
18335 if (depth >= p_mfd)
18336 {
18337 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018338 rettv->v_type = VAR_NUMBER;
18339 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018340 return;
18341 }
18342 ++depth;
18343
18344 line_breakcheck(); /* check for CTRL-C hit */
18345
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018346 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000018347 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018348 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018349 fc.rettv = rettv;
18350 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018351 fc.linenr = 0;
18352 fc.returned = FALSE;
18353 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018354 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018355 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018356 fc.dbg_tick = debug_tick;
18357
Bram Moolenaar33570922005-01-25 22:26:29 +000018358 /*
18359 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
18360 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
18361 * each argument variable and saves a lot of time.
18362 */
18363 /*
18364 * Init l: variables.
18365 */
18366 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000018367 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018368 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018369 /* Set l:self to "selfdict". */
18370 v = &fc.fixvar[fixvar_idx++].var;
18371 STRCPY(v->di_key, "self");
18372 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
18373 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
18374 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018375 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000018376 v->di_tv.vval.v_dict = selfdict;
18377 ++selfdict->dv_refcount;
18378 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000018379
Bram Moolenaar33570922005-01-25 22:26:29 +000018380 /*
18381 * Init a: variables.
18382 * Set a:0 to "argcount".
18383 * Set a:000 to a list with room for the "..." arguments.
18384 */
18385 init_var_dict(&fc.l_avars, &fc.l_avars_var);
18386 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018387 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000018388 v = &fc.fixvar[fixvar_idx++].var;
18389 STRCPY(v->di_key, "000");
18390 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18391 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
18392 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018393 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018394 v->di_tv.vval.v_list = &fc.l_varlist;
18395 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
18396 fc.l_varlist.lv_refcount = 99999;
18397
18398 /*
18399 * Set a:firstline to "firstline" and a:lastline to "lastline".
18400 * Set a:name to named arguments.
18401 * Set a:N to the "..." arguments.
18402 */
18403 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
18404 (varnumber_T)firstline);
18405 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
18406 (varnumber_T)lastline);
18407 for (i = 0; i < argcount; ++i)
18408 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018409 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000018410 if (ai < 0)
18411 /* named argument a:name */
18412 name = FUNCARG(fp, i);
18413 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000018414 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018415 /* "..." argument a:1, a:2, etc. */
18416 sprintf((char *)numbuf, "%d", ai + 1);
18417 name = numbuf;
18418 }
18419 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
18420 {
18421 v = &fc.fixvar[fixvar_idx++].var;
18422 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18423 }
18424 else
18425 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018426 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
18427 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000018428 if (v == NULL)
18429 break;
18430 v->di_flags = DI_FLAGS_RO;
18431 }
18432 STRCPY(v->di_key, name);
18433 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
18434
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018435 /* Note: the values are copied directly to avoid alloc/free.
18436 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000018437 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018438 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018439
18440 if (ai >= 0 && ai < MAX_FUNC_ARGS)
18441 {
18442 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
18443 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018444 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018445 }
18446 }
18447
Bram Moolenaar071d4272004-06-13 20:20:40 +000018448 /* Don't redraw while executing the function. */
18449 ++RedrawingDisabled;
18450 save_sourcing_name = sourcing_name;
18451 save_sourcing_lnum = sourcing_lnum;
18452 sourcing_lnum = 1;
18453 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018454 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018455 if (sourcing_name != NULL)
18456 {
18457 if (save_sourcing_name != NULL
18458 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
18459 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
18460 else
18461 STRCPY(sourcing_name, "function ");
18462 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
18463
18464 if (p_verbose >= 12)
18465 {
18466 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018467 verbose_enter_scroll();
18468
Bram Moolenaar555b2802005-05-19 21:08:39 +000018469 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018470 if (p_verbose >= 14)
18471 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018472 char_u buf[MSG_BUF_LEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000018473 char_u numbuf[NUMBUFLEN];
18474 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018475
18476 msg_puts((char_u *)"(");
18477 for (i = 0; i < argcount; ++i)
18478 {
18479 if (i > 0)
18480 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018481 if (argvars[i].v_type == VAR_NUMBER)
18482 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018483 else
18484 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000018485 trunc_string(tv2string(&argvars[i], &tofree, numbuf),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018486 buf, MSG_BUF_CLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018487 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000018488 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018489 }
18490 }
18491 msg_puts((char_u *)")");
18492 }
18493 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018494
18495 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018496 --no_wait_return;
18497 }
18498 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000018499#ifdef FEAT_PROFILE
18500 if (do_profiling)
18501 {
18502 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
18503 func_do_profile(fp);
18504 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018505 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000018506 {
18507 ++fp->uf_tm_count;
18508 profile_start(&fp->uf_tm_start);
18509 profile_zero(&fp->uf_tm_children);
18510 }
18511 script_prof_save(&wait_start);
18512 }
18513#endif
18514
Bram Moolenaar071d4272004-06-13 20:20:40 +000018515 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018516 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018517 save_did_emsg = did_emsg;
18518 did_emsg = FALSE;
18519
18520 /* call do_cmdline() to execute the lines */
18521 do_cmdline(NULL, get_func_line, (void *)&fc,
18522 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
18523
18524 --RedrawingDisabled;
18525
18526 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018527 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018528 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018529 clear_tv(rettv);
18530 rettv->v_type = VAR_NUMBER;
18531 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018532 }
18533
Bram Moolenaar05159a02005-02-26 23:04:13 +000018534#ifdef FEAT_PROFILE
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018535 if (fp->uf_profiling || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000018536 {
18537 profile_end(&fp->uf_tm_start);
18538 profile_sub_wait(&wait_start, &fp->uf_tm_start);
18539 profile_add(&fp->uf_tm_total, &fp->uf_tm_start);
18540 profile_add(&fp->uf_tm_self, &fp->uf_tm_start);
18541 profile_sub(&fp->uf_tm_self, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018542 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018543 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018544 profile_add(&fc.caller->func->uf_tm_children, &fp->uf_tm_start);
18545 profile_add(&fc.caller->func->uf_tml_children, &fp->uf_tm_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018546 }
18547 }
18548#endif
18549
Bram Moolenaar071d4272004-06-13 20:20:40 +000018550 /* when being verbose, mention the return value */
18551 if (p_verbose >= 12)
18552 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018553 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018554 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018555
Bram Moolenaar071d4272004-06-13 20:20:40 +000018556 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000018557 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018558 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000018559 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
18560 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000018561 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000018562 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000018563 char_u buf[MSG_BUF_LEN];
18564 char_u numbuf[NUMBUFLEN];
18565 char_u *tofree;
18566
Bram Moolenaar555b2802005-05-19 21:08:39 +000018567 /* The value may be very long. Skip the middle part, so that we
18568 * have some idea how it starts and ends. smsg() would always
18569 * truncate it at the end. */
Bram Moolenaar758711c2005-02-02 23:11:38 +000018570 trunc_string(tv2string(fc.rettv, &tofree, numbuf),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018571 buf, MSG_BUF_CLEN);
Bram Moolenaar555b2802005-05-19 21:08:39 +000018572 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000018573 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018574 }
18575 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018576
18577 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018578 --no_wait_return;
18579 }
18580
18581 vim_free(sourcing_name);
18582 sourcing_name = save_sourcing_name;
18583 sourcing_lnum = save_sourcing_lnum;
18584 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018585#ifdef FEAT_PROFILE
18586 if (do_profiling)
18587 script_prof_restore(&wait_start);
18588#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018589
18590 if (p_verbose >= 12 && sourcing_name != NULL)
18591 {
18592 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018593 verbose_enter_scroll();
18594
Bram Moolenaar555b2802005-05-19 21:08:39 +000018595 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018596 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018597
18598 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018599 --no_wait_return;
18600 }
18601
18602 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018603 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018604
Bram Moolenaar33570922005-01-25 22:26:29 +000018605 /* The a: variables typevals were not alloced, only free the allocated
18606 * variables. */
18607 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
18608
18609 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018610 --depth;
18611}
18612
18613/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018614 * Add a number variable "name" to dict "dp" with value "nr".
18615 */
18616 static void
18617add_nr_var(dp, v, name, nr)
18618 dict_T *dp;
18619 dictitem_T *v;
18620 char *name;
18621 varnumber_T nr;
18622{
18623 STRCPY(v->di_key, name);
18624 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18625 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
18626 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018627 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018628 v->di_tv.vval.v_number = nr;
18629}
18630
18631/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018632 * ":return [expr]"
18633 */
18634 void
18635ex_return(eap)
18636 exarg_T *eap;
18637{
18638 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018639 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018640 int returning = FALSE;
18641
18642 if (current_funccal == NULL)
18643 {
18644 EMSG(_("E133: :return not inside a function"));
18645 return;
18646 }
18647
18648 if (eap->skip)
18649 ++emsg_skip;
18650
18651 eap->nextcmd = NULL;
18652 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018653 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018654 {
18655 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018656 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018657 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018658 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018659 }
18660 /* It's safer to return also on error. */
18661 else if (!eap->skip)
18662 {
18663 /*
18664 * Return unless the expression evaluation has been cancelled due to an
18665 * aborting error, an interrupt, or an exception.
18666 */
18667 if (!aborting())
18668 returning = do_return(eap, FALSE, TRUE, NULL);
18669 }
18670
18671 /* When skipping or the return gets pending, advance to the next command
18672 * in this line (!returning). Otherwise, ignore the rest of the line.
18673 * Following lines will be ignored by get_func_line(). */
18674 if (returning)
18675 eap->nextcmd = NULL;
18676 else if (eap->nextcmd == NULL) /* no argument */
18677 eap->nextcmd = check_nextcmd(arg);
18678
18679 if (eap->skip)
18680 --emsg_skip;
18681}
18682
18683/*
18684 * Return from a function. Possibly makes the return pending. Also called
18685 * for a pending return at the ":endtry" or after returning from an extra
18686 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000018687 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018688 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018689 * FALSE when the return gets pending.
18690 */
18691 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018692do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018693 exarg_T *eap;
18694 int reanimate;
18695 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018696 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018697{
18698 int idx;
18699 struct condstack *cstack = eap->cstack;
18700
18701 if (reanimate)
18702 /* Undo the return. */
18703 current_funccal->returned = FALSE;
18704
18705 /*
18706 * Cleanup (and inactivate) conditionals, but stop when a try conditional
18707 * not in its finally clause (which then is to be executed next) is found.
18708 * In this case, make the ":return" pending for execution at the ":endtry".
18709 * Otherwise, return normally.
18710 */
18711 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
18712 if (idx >= 0)
18713 {
18714 cstack->cs_pending[idx] = CSTP_RETURN;
18715
18716 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018717 /* A pending return again gets pending. "rettv" points to an
18718 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000018719 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018720 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018721 else
18722 {
18723 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018724 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018725 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018726 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018727
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018728 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018729 {
18730 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018731 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018732 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018733 else
18734 EMSG(_(e_outofmem));
18735 }
18736 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018737 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018738
18739 if (reanimate)
18740 {
18741 /* The pending return value could be overwritten by a ":return"
18742 * without argument in a finally clause; reset the default
18743 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018744 current_funccal->rettv->v_type = VAR_NUMBER;
18745 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018746 }
18747 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018748 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018749 }
18750 else
18751 {
18752 current_funccal->returned = TRUE;
18753
18754 /* If the return is carried out now, store the return value. For
18755 * a return immediately after reanimation, the value is already
18756 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018757 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018758 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018759 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000018760 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018761 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018762 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018763 }
18764 }
18765
18766 return idx < 0;
18767}
18768
18769/*
18770 * Free the variable with a pending return value.
18771 */
18772 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018773discard_pending_return(rettv)
18774 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018775{
Bram Moolenaar33570922005-01-25 22:26:29 +000018776 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018777}
18778
18779/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018780 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000018781 * is an allocated string. Used by report_pending() for verbose messages.
18782 */
18783 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018784get_return_cmd(rettv)
18785 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018786{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018787 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018788 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018789 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018790
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018791 if (rettv != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018792 s = echo_string((typval_T *)rettv, &tofree, numbuf);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018793 if (s == NULL)
18794 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018795
18796 STRCPY(IObuff, ":return ");
18797 STRNCPY(IObuff + 8, s, IOSIZE - 8);
18798 if (STRLEN(s) + 8 >= IOSIZE)
18799 STRCPY(IObuff + IOSIZE - 4, "...");
18800 vim_free(tofree);
18801 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018802}
18803
18804/*
18805 * Get next function line.
18806 * Called by do_cmdline() to get the next line.
18807 * Returns allocated string, or NULL for end of function.
18808 */
18809/* ARGSUSED */
18810 char_u *
18811get_func_line(c, cookie, indent)
18812 int c; /* not used */
18813 void *cookie;
18814 int indent; /* not used */
18815{
Bram Moolenaar33570922005-01-25 22:26:29 +000018816 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018817 ufunc_T *fp = fcp->func;
18818 char_u *retval;
18819 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018820
18821 /* If breakpoints have been added/deleted need to check for it. */
18822 if (fcp->dbg_tick != debug_tick)
18823 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000018824 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018825 sourcing_lnum);
18826 fcp->dbg_tick = debug_tick;
18827 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000018828#ifdef FEAT_PROFILE
18829 if (do_profiling)
18830 func_line_end(cookie);
18831#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018832
Bram Moolenaar05159a02005-02-26 23:04:13 +000018833 gap = &fp->uf_lines;
18834 if ((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000018835 retval = NULL;
18836 else if (fcp->returned || fcp->linenr >= gap->ga_len)
18837 retval = NULL;
18838 else
18839 {
18840 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
18841 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018842#ifdef FEAT_PROFILE
18843 if (do_profiling)
18844 func_line_start(cookie);
18845#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018846 }
18847
18848 /* Did we encounter a breakpoint? */
18849 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
18850 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000018851 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018852 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018853 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018854 sourcing_lnum);
18855 fcp->dbg_tick = debug_tick;
18856 }
18857
18858 return retval;
18859}
18860
Bram Moolenaar05159a02005-02-26 23:04:13 +000018861#if defined(FEAT_PROFILE) || defined(PROTO)
18862/*
18863 * Called when starting to read a function line.
18864 * "sourcing_lnum" must be correct!
18865 * When skipping lines it may not actually be executed, but we won't find out
18866 * until later and we need to store the time now.
18867 */
18868 void
18869func_line_start(cookie)
18870 void *cookie;
18871{
18872 funccall_T *fcp = (funccall_T *)cookie;
18873 ufunc_T *fp = fcp->func;
18874
18875 if (fp->uf_profiling && sourcing_lnum >= 1
18876 && sourcing_lnum <= fp->uf_lines.ga_len)
18877 {
18878 fp->uf_tml_idx = sourcing_lnum - 1;
18879 fp->uf_tml_execed = FALSE;
18880 profile_start(&fp->uf_tml_start);
18881 profile_zero(&fp->uf_tml_children);
18882 profile_get_wait(&fp->uf_tml_wait);
18883 }
18884}
18885
18886/*
18887 * Called when actually executing a function line.
18888 */
18889 void
18890func_line_exec(cookie)
18891 void *cookie;
18892{
18893 funccall_T *fcp = (funccall_T *)cookie;
18894 ufunc_T *fp = fcp->func;
18895
18896 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
18897 fp->uf_tml_execed = TRUE;
18898}
18899
18900/*
18901 * Called when done with a function line.
18902 */
18903 void
18904func_line_end(cookie)
18905 void *cookie;
18906{
18907 funccall_T *fcp = (funccall_T *)cookie;
18908 ufunc_T *fp = fcp->func;
18909
18910 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
18911 {
18912 if (fp->uf_tml_execed)
18913 {
18914 ++fp->uf_tml_count[fp->uf_tml_idx];
18915 profile_end(&fp->uf_tml_start);
18916 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
18917 profile_add(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start);
18918 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
18919 profile_sub(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_children);
18920 }
18921 fp->uf_tml_idx = -1;
18922 }
18923}
18924#endif
18925
Bram Moolenaar071d4272004-06-13 20:20:40 +000018926/*
18927 * Return TRUE if the currently active function should be ended, because a
18928 * return was encountered or an error occured. Used inside a ":while".
18929 */
18930 int
18931func_has_ended(cookie)
18932 void *cookie;
18933{
Bram Moolenaar33570922005-01-25 22:26:29 +000018934 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018935
18936 /* Ignore the "abort" flag if the abortion behavior has been changed due to
18937 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018938 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000018939 || fcp->returned);
18940}
18941
18942/*
18943 * return TRUE if cookie indicates a function which "abort"s on errors.
18944 */
18945 int
18946func_has_abort(cookie)
18947 void *cookie;
18948{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018949 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018950}
18951
18952#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
18953typedef enum
18954{
18955 VAR_FLAVOUR_DEFAULT,
18956 VAR_FLAVOUR_SESSION,
18957 VAR_FLAVOUR_VIMINFO
18958} var_flavour_T;
18959
18960static var_flavour_T var_flavour __ARGS((char_u *varname));
18961
18962 static var_flavour_T
18963var_flavour(varname)
18964 char_u *varname;
18965{
18966 char_u *p = varname;
18967
18968 if (ASCII_ISUPPER(*p))
18969 {
18970 while (*(++p))
18971 if (ASCII_ISLOWER(*p))
18972 return VAR_FLAVOUR_SESSION;
18973 return VAR_FLAVOUR_VIMINFO;
18974 }
18975 else
18976 return VAR_FLAVOUR_DEFAULT;
18977}
18978#endif
18979
18980#if defined(FEAT_VIMINFO) || defined(PROTO)
18981/*
18982 * Restore global vars that start with a capital from the viminfo file
18983 */
18984 int
18985read_viminfo_varlist(virp, writing)
18986 vir_T *virp;
18987 int writing;
18988{
18989 char_u *tab;
18990 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000018991 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018992
18993 if (!writing && (find_viminfo_parameter('!') != NULL))
18994 {
18995 tab = vim_strchr(virp->vir_line + 1, '\t');
18996 if (tab != NULL)
18997 {
18998 *tab++ = '\0'; /* isolate the variable name */
18999 if (*tab == 'S') /* string var */
19000 is_string = TRUE;
19001
19002 tab = vim_strchr(tab, '\t');
19003 if (tab != NULL)
19004 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019005 if (is_string)
19006 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019007 tv.v_type = VAR_STRING;
19008 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019009 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019010 }
19011 else
19012 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019013 tv.v_type = VAR_NUMBER;
19014 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019015 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019016 set_var(virp->vir_line + 1, &tv, FALSE);
19017 if (is_string)
19018 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019019 }
19020 }
19021 }
19022
19023 return viminfo_readline(virp);
19024}
19025
19026/*
19027 * Write global vars that start with a capital to the viminfo file
19028 */
19029 void
19030write_viminfo_varlist(fp)
19031 FILE *fp;
19032{
Bram Moolenaar33570922005-01-25 22:26:29 +000019033 hashitem_T *hi;
19034 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000019035 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019036 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019037 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019038 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019039 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019040
19041 if (find_viminfo_parameter('!') == NULL)
19042 return;
19043
19044 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000019045
Bram Moolenaar33570922005-01-25 22:26:29 +000019046 todo = globvarht.ht_used;
19047 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019048 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019049 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019050 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019051 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019052 this_var = HI2DI(hi);
19053 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019054 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019055 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000019056 {
19057 case VAR_STRING: s = "STR"; break;
19058 case VAR_NUMBER: s = "NUM"; break;
19059 default: continue;
19060 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019061 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019062 p = echo_string(&this_var->di_tv, &tofree, numbuf);
19063 if (p != NULL)
19064 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000019065 vim_free(tofree);
19066 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019067 }
19068 }
19069}
19070#endif
19071
19072#if defined(FEAT_SESSION) || defined(PROTO)
19073 int
19074store_session_globals(fd)
19075 FILE *fd;
19076{
Bram Moolenaar33570922005-01-25 22:26:29 +000019077 hashitem_T *hi;
19078 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000019079 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019080 char_u *p, *t;
19081
Bram Moolenaar33570922005-01-25 22:26:29 +000019082 todo = globvarht.ht_used;
19083 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019084 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019085 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019086 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019087 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019088 this_var = HI2DI(hi);
19089 if ((this_var->di_tv.v_type == VAR_NUMBER
19090 || this_var->di_tv.v_type == VAR_STRING)
19091 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019092 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019093 /* Escape special characters with a backslash. Turn a LF and
19094 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019095 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000019096 (char_u *)"\\\"\n\r");
19097 if (p == NULL) /* out of memory */
19098 break;
19099 for (t = p; *t != NUL; ++t)
19100 if (*t == '\n')
19101 *t = 'n';
19102 else if (*t == '\r')
19103 *t = 'r';
19104 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000019105 this_var->di_key,
19106 (this_var->di_tv.v_type == VAR_STRING) ? '"'
19107 : ' ',
19108 p,
19109 (this_var->di_tv.v_type == VAR_STRING) ? '"'
19110 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000019111 || put_eol(fd) == FAIL)
19112 {
19113 vim_free(p);
19114 return FAIL;
19115 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019116 vim_free(p);
19117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019118 }
19119 }
19120 return OK;
19121}
19122#endif
19123
Bram Moolenaar661b1822005-07-28 22:36:45 +000019124/*
19125 * Display script name where an item was last set.
19126 * Should only be invoked when 'verbose' is non-zero.
19127 */
19128 void
19129last_set_msg(scriptID)
19130 scid_T scriptID;
19131{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019132 char_u *p;
19133
Bram Moolenaar661b1822005-07-28 22:36:45 +000019134 if (scriptID != 0)
19135 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019136 p = home_replace_save(NULL, get_scriptname(scriptID));
19137 if (p != NULL)
19138 {
19139 verbose_enter();
19140 MSG_PUTS(_("\n\tLast set from "));
19141 MSG_PUTS(p);
19142 vim_free(p);
19143 verbose_leave();
19144 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000019145 }
19146}
19147
Bram Moolenaar071d4272004-06-13 20:20:40 +000019148#endif /* FEAT_EVAL */
19149
19150#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
19151
19152
19153#ifdef WIN3264
19154/*
19155 * Functions for ":8" filename modifier: get 8.3 version of a filename.
19156 */
19157static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
19158static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
19159static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
19160
19161/*
19162 * Get the short pathname of a file.
19163 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
19164 */
19165 static int
19166get_short_pathname(fnamep, bufp, fnamelen)
19167 char_u **fnamep;
19168 char_u **bufp;
19169 int *fnamelen;
19170{
19171 int l,len;
19172 char_u *newbuf;
19173
19174 len = *fnamelen;
19175
19176 l = GetShortPathName(*fnamep, *fnamep, len);
19177 if (l > len - 1)
19178 {
19179 /* If that doesn't work (not enough space), then save the string
19180 * and try again with a new buffer big enough
19181 */
19182 newbuf = vim_strnsave(*fnamep, l);
19183 if (newbuf == NULL)
19184 return 0;
19185
19186 vim_free(*bufp);
19187 *fnamep = *bufp = newbuf;
19188
19189 l = GetShortPathName(*fnamep,*fnamep,l+1);
19190
19191 /* Really should always succeed, as the buffer is big enough */
19192 }
19193
19194 *fnamelen = l;
19195 return 1;
19196}
19197
19198/*
19199 * Create a short path name. Returns the length of the buffer it needs.
19200 * Doesn't copy over the end of the buffer passed in.
19201 */
19202 static int
19203shortpath_for_invalid_fname(fname, bufp, fnamelen)
19204 char_u **fname;
19205 char_u **bufp;
19206 int *fnamelen;
19207{
19208 char_u *s, *p, *pbuf2, *pbuf3;
19209 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019210 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019211
19212 /* Make a copy */
19213 len2 = *fnamelen;
19214 pbuf2 = vim_strnsave(*fname, len2);
19215 pbuf3 = NULL;
19216
19217 s = pbuf2 + len2 - 1; /* Find the end */
19218 slen = 1;
19219 plen = len2;
19220
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019221 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019222 {
19223 --s;
19224 ++slen;
19225 --plen;
19226 }
19227
19228 do
19229 {
19230 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019231 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019232 {
19233 --s;
19234 ++slen;
19235 --plen;
19236 }
19237 if (s <= pbuf2)
19238 break;
19239
19240 /* Remeber the character that is about to be blatted */
19241 ch = *s;
19242 *s = 0; /* get_short_pathname requires a null-terminated string */
19243
19244 /* Try it in situ */
19245 p = pbuf2;
19246 if (!get_short_pathname(&p, &pbuf3, &plen))
19247 {
19248 vim_free(pbuf2);
19249 return -1;
19250 }
19251 *s = ch; /* Preserve the string */
19252 } while (plen == 0);
19253
19254 if (plen > 0)
19255 {
19256 /* Remeber the length of the new string. */
19257 *fnamelen = len = plen + slen;
19258 vim_free(*bufp);
19259 if (len > len2)
19260 {
19261 /* If there's not enough space in the currently allocated string,
19262 * then copy it to a buffer big enough.
19263 */
19264 *fname= *bufp = vim_strnsave(p, len);
19265 if (*fname == NULL)
19266 return -1;
19267 }
19268 else
19269 {
19270 /* Transfer pbuf2 to being the main buffer (it's big enough) */
19271 *fname = *bufp = pbuf2;
19272 if (p != pbuf2)
19273 strncpy(*fname, p, plen);
19274 pbuf2 = NULL;
19275 }
19276 /* Concat the next bit */
19277 strncpy(*fname + plen, s, slen);
19278 (*fname)[len] = '\0';
19279 }
19280 vim_free(pbuf3);
19281 vim_free(pbuf2);
19282 return 0;
19283}
19284
19285/*
19286 * Get a pathname for a partial path.
19287 */
19288 static int
19289shortpath_for_partial(fnamep, bufp, fnamelen)
19290 char_u **fnamep;
19291 char_u **bufp;
19292 int *fnamelen;
19293{
19294 int sepcount, len, tflen;
19295 char_u *p;
19296 char_u *pbuf, *tfname;
19297 int hasTilde;
19298
19299 /* Count up the path seperators from the RHS.. so we know which part
19300 * of the path to return.
19301 */
19302 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019303 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019304 if (vim_ispathsep(*p))
19305 ++sepcount;
19306
19307 /* Need full path first (use expand_env() to remove a "~/") */
19308 hasTilde = (**fnamep == '~');
19309 if (hasTilde)
19310 pbuf = tfname = expand_env_save(*fnamep);
19311 else
19312 pbuf = tfname = FullName_save(*fnamep, FALSE);
19313
19314 len = tflen = STRLEN(tfname);
19315
19316 if (!get_short_pathname(&tfname, &pbuf, &len))
19317 return -1;
19318
19319 if (len == 0)
19320 {
19321 /* Don't have a valid filename, so shorten the rest of the
19322 * path if we can. This CAN give us invalid 8.3 filenames, but
19323 * there's not a lot of point in guessing what it might be.
19324 */
19325 len = tflen;
19326 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
19327 return -1;
19328 }
19329
19330 /* Count the paths backward to find the beginning of the desired string. */
19331 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019332 {
19333#ifdef FEAT_MBYTE
19334 if (has_mbyte)
19335 p -= mb_head_off(tfname, p);
19336#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019337 if (vim_ispathsep(*p))
19338 {
19339 if (sepcount == 0 || (hasTilde && sepcount == 1))
19340 break;
19341 else
19342 sepcount --;
19343 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019345 if (hasTilde)
19346 {
19347 --p;
19348 if (p >= tfname)
19349 *p = '~';
19350 else
19351 return -1;
19352 }
19353 else
19354 ++p;
19355
19356 /* Copy in the string - p indexes into tfname - allocated at pbuf */
19357 vim_free(*bufp);
19358 *fnamelen = (int)STRLEN(p);
19359 *bufp = pbuf;
19360 *fnamep = p;
19361
19362 return 0;
19363}
19364#endif /* WIN3264 */
19365
19366/*
19367 * Adjust a filename, according to a string of modifiers.
19368 * *fnamep must be NUL terminated when called. When returning, the length is
19369 * determined by *fnamelen.
19370 * Returns valid flags.
19371 * When there is an error, *fnamep is set to NULL.
19372 */
19373 int
19374modify_fname(src, usedlen, fnamep, bufp, fnamelen)
19375 char_u *src; /* string with modifiers */
19376 int *usedlen; /* characters after src that are used */
19377 char_u **fnamep; /* file name so far */
19378 char_u **bufp; /* buffer for allocated file name or NULL */
19379 int *fnamelen; /* length of fnamep */
19380{
19381 int valid = 0;
19382 char_u *tail;
19383 char_u *s, *p, *pbuf;
19384 char_u dirname[MAXPATHL];
19385 int c;
19386 int has_fullname = 0;
19387#ifdef WIN3264
19388 int has_shortname = 0;
19389#endif
19390
19391repeat:
19392 /* ":p" - full path/file_name */
19393 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
19394 {
19395 has_fullname = 1;
19396
19397 valid |= VALID_PATH;
19398 *usedlen += 2;
19399
19400 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
19401 if ((*fnamep)[0] == '~'
19402#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
19403 && ((*fnamep)[1] == '/'
19404# ifdef BACKSLASH_IN_FILENAME
19405 || (*fnamep)[1] == '\\'
19406# endif
19407 || (*fnamep)[1] == NUL)
19408
19409#endif
19410 )
19411 {
19412 *fnamep = expand_env_save(*fnamep);
19413 vim_free(*bufp); /* free any allocated file name */
19414 *bufp = *fnamep;
19415 if (*fnamep == NULL)
19416 return -1;
19417 }
19418
19419 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019420 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019421 {
19422 if (vim_ispathsep(*p)
19423 && p[1] == '.'
19424 && (p[2] == NUL
19425 || vim_ispathsep(p[2])
19426 || (p[2] == '.'
19427 && (p[3] == NUL || vim_ispathsep(p[3])))))
19428 break;
19429 }
19430
19431 /* FullName_save() is slow, don't use it when not needed. */
19432 if (*p != NUL || !vim_isAbsName(*fnamep))
19433 {
19434 *fnamep = FullName_save(*fnamep, *p != NUL);
19435 vim_free(*bufp); /* free any allocated file name */
19436 *bufp = *fnamep;
19437 if (*fnamep == NULL)
19438 return -1;
19439 }
19440
19441 /* Append a path separator to a directory. */
19442 if (mch_isdir(*fnamep))
19443 {
19444 /* Make room for one or two extra characters. */
19445 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
19446 vim_free(*bufp); /* free any allocated file name */
19447 *bufp = *fnamep;
19448 if (*fnamep == NULL)
19449 return -1;
19450 add_pathsep(*fnamep);
19451 }
19452 }
19453
19454 /* ":." - path relative to the current directory */
19455 /* ":~" - path relative to the home directory */
19456 /* ":8" - shortname path - postponed till after */
19457 while (src[*usedlen] == ':'
19458 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
19459 {
19460 *usedlen += 2;
19461 if (c == '8')
19462 {
19463#ifdef WIN3264
19464 has_shortname = 1; /* Postpone this. */
19465#endif
19466 continue;
19467 }
19468 pbuf = NULL;
19469 /* Need full path first (use expand_env() to remove a "~/") */
19470 if (!has_fullname)
19471 {
19472 if (c == '.' && **fnamep == '~')
19473 p = pbuf = expand_env_save(*fnamep);
19474 else
19475 p = pbuf = FullName_save(*fnamep, FALSE);
19476 }
19477 else
19478 p = *fnamep;
19479
19480 has_fullname = 0;
19481
19482 if (p != NULL)
19483 {
19484 if (c == '.')
19485 {
19486 mch_dirname(dirname, MAXPATHL);
19487 s = shorten_fname(p, dirname);
19488 if (s != NULL)
19489 {
19490 *fnamep = s;
19491 if (pbuf != NULL)
19492 {
19493 vim_free(*bufp); /* free any allocated file name */
19494 *bufp = pbuf;
19495 pbuf = NULL;
19496 }
19497 }
19498 }
19499 else
19500 {
19501 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
19502 /* Only replace it when it starts with '~' */
19503 if (*dirname == '~')
19504 {
19505 s = vim_strsave(dirname);
19506 if (s != NULL)
19507 {
19508 *fnamep = s;
19509 vim_free(*bufp);
19510 *bufp = s;
19511 }
19512 }
19513 }
19514 vim_free(pbuf);
19515 }
19516 }
19517
19518 tail = gettail(*fnamep);
19519 *fnamelen = (int)STRLEN(*fnamep);
19520
19521 /* ":h" - head, remove "/file_name", can be repeated */
19522 /* Don't remove the first "/" or "c:\" */
19523 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
19524 {
19525 valid |= VALID_HEAD;
19526 *usedlen += 2;
19527 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019528 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019529 --tail;
19530 *fnamelen = (int)(tail - *fnamep);
19531#ifdef VMS
19532 if (*fnamelen > 0)
19533 *fnamelen += 1; /* the path separator is part of the path */
19534#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019535 while (tail > s && !after_pathsep(s, tail))
19536 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019537 }
19538
19539 /* ":8" - shortname */
19540 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
19541 {
19542 *usedlen += 2;
19543#ifdef WIN3264
19544 has_shortname = 1;
19545#endif
19546 }
19547
19548#ifdef WIN3264
19549 /* Check shortname after we have done 'heads' and before we do 'tails'
19550 */
19551 if (has_shortname)
19552 {
19553 pbuf = NULL;
19554 /* Copy the string if it is shortened by :h */
19555 if (*fnamelen < (int)STRLEN(*fnamep))
19556 {
19557 p = vim_strnsave(*fnamep, *fnamelen);
19558 if (p == 0)
19559 return -1;
19560 vim_free(*bufp);
19561 *bufp = *fnamep = p;
19562 }
19563
19564 /* Split into two implementations - makes it easier. First is where
19565 * there isn't a full name already, second is where there is.
19566 */
19567 if (!has_fullname && !vim_isAbsName(*fnamep))
19568 {
19569 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
19570 return -1;
19571 }
19572 else
19573 {
19574 int l;
19575
19576 /* Simple case, already have the full-name
19577 * Nearly always shorter, so try first time. */
19578 l = *fnamelen;
19579 if (!get_short_pathname(fnamep, bufp, &l))
19580 return -1;
19581
19582 if (l == 0)
19583 {
19584 /* Couldn't find the filename.. search the paths.
19585 */
19586 l = *fnamelen;
19587 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
19588 return -1;
19589 }
19590 *fnamelen = l;
19591 }
19592 }
19593#endif /* WIN3264 */
19594
19595 /* ":t" - tail, just the basename */
19596 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
19597 {
19598 *usedlen += 2;
19599 *fnamelen -= (int)(tail - *fnamep);
19600 *fnamep = tail;
19601 }
19602
19603 /* ":e" - extension, can be repeated */
19604 /* ":r" - root, without extension, can be repeated */
19605 while (src[*usedlen] == ':'
19606 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
19607 {
19608 /* find a '.' in the tail:
19609 * - for second :e: before the current fname
19610 * - otherwise: The last '.'
19611 */
19612 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
19613 s = *fnamep - 2;
19614 else
19615 s = *fnamep + *fnamelen - 1;
19616 for ( ; s > tail; --s)
19617 if (s[0] == '.')
19618 break;
19619 if (src[*usedlen + 1] == 'e') /* :e */
19620 {
19621 if (s > tail)
19622 {
19623 *fnamelen += (int)(*fnamep - (s + 1));
19624 *fnamep = s + 1;
19625#ifdef VMS
19626 /* cut version from the extension */
19627 s = *fnamep + *fnamelen - 1;
19628 for ( ; s > *fnamep; --s)
19629 if (s[0] == ';')
19630 break;
19631 if (s > *fnamep)
19632 *fnamelen = s - *fnamep;
19633#endif
19634 }
19635 else if (*fnamep <= tail)
19636 *fnamelen = 0;
19637 }
19638 else /* :r */
19639 {
19640 if (s > tail) /* remove one extension */
19641 *fnamelen = (int)(s - *fnamep);
19642 }
19643 *usedlen += 2;
19644 }
19645
19646 /* ":s?pat?foo?" - substitute */
19647 /* ":gs?pat?foo?" - global substitute */
19648 if (src[*usedlen] == ':'
19649 && (src[*usedlen + 1] == 's'
19650 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
19651 {
19652 char_u *str;
19653 char_u *pat;
19654 char_u *sub;
19655 int sep;
19656 char_u *flags;
19657 int didit = FALSE;
19658
19659 flags = (char_u *)"";
19660 s = src + *usedlen + 2;
19661 if (src[*usedlen + 1] == 'g')
19662 {
19663 flags = (char_u *)"g";
19664 ++s;
19665 }
19666
19667 sep = *s++;
19668 if (sep)
19669 {
19670 /* find end of pattern */
19671 p = vim_strchr(s, sep);
19672 if (p != NULL)
19673 {
19674 pat = vim_strnsave(s, (int)(p - s));
19675 if (pat != NULL)
19676 {
19677 s = p + 1;
19678 /* find end of substitution */
19679 p = vim_strchr(s, sep);
19680 if (p != NULL)
19681 {
19682 sub = vim_strnsave(s, (int)(p - s));
19683 str = vim_strnsave(*fnamep, *fnamelen);
19684 if (sub != NULL && str != NULL)
19685 {
19686 *usedlen = (int)(p + 1 - src);
19687 s = do_string_sub(str, pat, sub, flags);
19688 if (s != NULL)
19689 {
19690 *fnamep = s;
19691 *fnamelen = (int)STRLEN(s);
19692 vim_free(*bufp);
19693 *bufp = s;
19694 didit = TRUE;
19695 }
19696 }
19697 vim_free(sub);
19698 vim_free(str);
19699 }
19700 vim_free(pat);
19701 }
19702 }
19703 /* after using ":s", repeat all the modifiers */
19704 if (didit)
19705 goto repeat;
19706 }
19707 }
19708
19709 return valid;
19710}
19711
19712/*
19713 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
19714 * "flags" can be "g" to do a global substitute.
19715 * Returns an allocated string, NULL for error.
19716 */
19717 char_u *
19718do_string_sub(str, pat, sub, flags)
19719 char_u *str;
19720 char_u *pat;
19721 char_u *sub;
19722 char_u *flags;
19723{
19724 int sublen;
19725 regmatch_T regmatch;
19726 int i;
19727 int do_all;
19728 char_u *tail;
19729 garray_T ga;
19730 char_u *ret;
19731 char_u *save_cpo;
19732
19733 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
19734 save_cpo = p_cpo;
19735 p_cpo = (char_u *)"";
19736
19737 ga_init2(&ga, 1, 200);
19738
19739 do_all = (flags[0] == 'g');
19740
19741 regmatch.rm_ic = p_ic;
19742 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19743 if (regmatch.regprog != NULL)
19744 {
19745 tail = str;
19746 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
19747 {
19748 /*
19749 * Get some space for a temporary buffer to do the substitution
19750 * into. It will contain:
19751 * - The text up to where the match is.
19752 * - The substituted text.
19753 * - The text after the match.
19754 */
19755 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
19756 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
19757 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
19758 {
19759 ga_clear(&ga);
19760 break;
19761 }
19762
19763 /* copy the text up to where the match is */
19764 i = (int)(regmatch.startp[0] - tail);
19765 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
19766 /* add the substituted text */
19767 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
19768 + ga.ga_len + i, TRUE, TRUE, FALSE);
19769 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019770 /* avoid getting stuck on a match with an empty string */
19771 if (tail == regmatch.endp[0])
19772 {
19773 if (*tail == NUL)
19774 break;
19775 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
19776 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019777 }
19778 else
19779 {
19780 tail = regmatch.endp[0];
19781 if (*tail == NUL)
19782 break;
19783 }
19784 if (!do_all)
19785 break;
19786 }
19787
19788 if (ga.ga_data != NULL)
19789 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
19790
19791 vim_free(regmatch.regprog);
19792 }
19793
19794 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
19795 ga_clear(&ga);
19796 p_cpo = save_cpo;
19797
19798 return ret;
19799}
19800
19801#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */