blob: 7c8babab9f32be16422b8141be8cd3ba69d86fc4 [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)
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014# include "vimio.h" /* for mch_open(), must be before vim.h */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015#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
194/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000195 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000197static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000199/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000200static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
201
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000202/* list heads for garbage collection */
203static dict_T *first_dict = NULL; /* list of all dicts */
204static list_T *first_list = NULL; /* list of all lists */
205
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000206/* From user function to hashitem and back. */
207static ufunc_T dumuf;
208#define UF2HIKEY(fp) ((fp)->uf_name)
209#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
210#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
211
212#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
213#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214
Bram Moolenaar33570922005-01-25 22:26:29 +0000215#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
216#define VAR_SHORT_LEN 20 /* short variable name length */
217#define FIXVAR_CNT 12 /* number of fixed variables */
218
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000220typedef struct funccall_S funccall_T;
221
222struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223{
224 ufunc_T *func; /* function being called */
225 int linenr; /* next line to be executed */
226 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000227 struct /* fixed variables for arguments */
228 {
229 dictitem_T var; /* variable (without room for name) */
230 char_u room[VAR_SHORT_LEN]; /* room for the name */
231 } fixvar[FIXVAR_CNT];
232 dict_T l_vars; /* l: local function variables */
233 dictitem_T l_vars_var; /* variable for l: scope */
234 dict_T l_avars; /* a: argument variables */
235 dictitem_T l_avars_var; /* variable for a: scope */
236 list_T l_varlist; /* list for a:000 */
237 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
238 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239 linenr_T breakpoint; /* next line with breakpoint or zero */
240 int dbg_tick; /* debug_tick when breakpoint was set */
241 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000242#ifdef FEAT_PROFILE
243 proftime_T prof_child; /* time spent in a child */
244#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000245 funccall_T *caller; /* calling function or NULL */
246};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247
248/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000249 * Info used by a ":for" loop.
250 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000251typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000252{
253 int fi_semicolon; /* TRUE if ending in '; var]' */
254 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000255 listwatch_T fi_lw; /* keep an eye on the item used. */
256 list_T *fi_list; /* list being used */
257} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000258
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000259/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000260 * Struct used by trans_function_name()
261 */
262typedef struct
263{
Bram Moolenaar33570922005-01-25 22:26:29 +0000264 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000265 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000266 dictitem_T *fd_di; /* Dictionary item used */
267} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000268
Bram Moolenaara7043832005-01-21 11:56:39 +0000269
270/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000271 * Array to hold the value of v: variables.
272 * The value is in a dictitem, so that it can also be used in the v: scope.
273 * The reason to use this table anyway is for very quick access to the
274 * variables with the VV_ defines.
275 */
276#include "version.h"
277
278/* values for vv_flags: */
279#define VV_COMPAT 1 /* compatible, also used without "v:" */
280#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000281#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000282
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000283#define VV_NAME(s, t) s, {{t}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000284
285static struct vimvar
286{
287 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000288 dictitem_T vv_di; /* value and name for key */
289 char vv_filler[16]; /* space for LONGEST name below!!! */
290 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
291} vimvars[VV_LEN] =
292{
293 /*
294 * The order here must match the VV_ defines in vim.h!
295 * Initializing a union does not work, leave tv.vval empty to get zero's.
296 */
297 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
298 {VV_NAME("count1", VAR_NUMBER), VV_RO},
299 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
300 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
301 {VV_NAME("warningmsg", VAR_STRING), 0},
302 {VV_NAME("statusmsg", VAR_STRING), 0},
303 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
304 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
305 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
306 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
307 {VV_NAME("termresponse", VAR_STRING), VV_RO},
308 {VV_NAME("fname", VAR_STRING), VV_RO},
309 {VV_NAME("lang", VAR_STRING), VV_RO},
310 {VV_NAME("lc_time", VAR_STRING), VV_RO},
311 {VV_NAME("ctype", VAR_STRING), VV_RO},
312 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
313 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
314 {VV_NAME("fname_in", VAR_STRING), VV_RO},
315 {VV_NAME("fname_out", VAR_STRING), VV_RO},
316 {VV_NAME("fname_new", VAR_STRING), VV_RO},
317 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
318 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
319 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
320 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
321 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
322 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
323 {VV_NAME("progname", VAR_STRING), VV_RO},
324 {VV_NAME("servername", VAR_STRING), VV_RO},
325 {VV_NAME("dying", VAR_NUMBER), VV_RO},
326 {VV_NAME("exception", VAR_STRING), VV_RO},
327 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
328 {VV_NAME("register", VAR_STRING), VV_RO},
329 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
330 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000331 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
332 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000333 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000334 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
335 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000336 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
337 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
338 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
339 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
340 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000341 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000342 {VV_NAME("swapname", VAR_STRING), VV_RO},
343 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000344 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000345 {VV_NAME("char", VAR_STRING), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000346};
347
348/* shorthand */
349#define vv_type vv_di.di_tv.v_type
350#define vv_nr vv_di.di_tv.vval.v_number
351#define vv_str vv_di.di_tv.vval.v_string
352#define vv_tv vv_di.di_tv
353
354/*
355 * The v: variables are stored in dictionary "vimvardict".
356 * "vimvars_var" is the variable that is used for the "l:" scope.
357 */
358static dict_T vimvardict;
359static dictitem_T vimvars_var;
360#define vimvarht vimvardict.dv_hashtab
361
Bram Moolenaara40058a2005-07-11 22:42:07 +0000362static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
363static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
364#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
365static int call_vim_function __ARGS((char_u *func, int argc, char_u **argv, int safe, typval_T *rettv));
366#endif
367static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
368static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
369static char_u *skip_var_one __ARGS((char_u *arg));
370static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty));
371static void list_glob_vars __ARGS((void));
372static void list_buf_vars __ARGS((void));
373static void list_win_vars __ARGS((void));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000374#ifdef FEAT_WINDOWS
375static void list_tab_vars __ARGS((void));
376#endif
Bram Moolenaara40058a2005-07-11 22:42:07 +0000377static void list_vim_vars __ARGS((void));
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000378static void list_script_vars __ARGS((void));
379static void list_func_vars __ARGS((void));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000380static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg));
381static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
382static int check_changedtick __ARGS((char_u *arg));
383static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
384static void clear_lval __ARGS((lval_T *lp));
385static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
386static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
387static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
388static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
389static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
390static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
391static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
392static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
393static void item_lock __ARGS((typval_T *tv, int deep, int lock));
394static int tv_islocked __ARGS((typval_T *tv));
395
Bram Moolenaar33570922005-01-25 22:26:29 +0000396static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
397static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
398static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
399static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
400static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
401static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
402static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
403static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000404
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000405static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000406static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
407static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
408static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
409static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000410static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000411static listitem_T *listitem_alloc __ARGS((void));
412static void listitem_free __ARGS((listitem_T *item));
413static void listitem_remove __ARGS((list_T *l, listitem_T *item));
414static long list_len __ARGS((list_T *l));
415static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
416static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
417static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
Bram Moolenaar33570922005-01-25 22:26:29 +0000418static listitem_T *list_find __ARGS((list_T *l, long n));
Bram Moolenaara5525202006-03-02 22:52:09 +0000419static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000420static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000421static void list_append __ARGS((list_T *l, listitem_T *item));
422static int list_append_tv __ARGS((list_T *l, typval_T *tv));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000423static int list_append_string __ARGS((list_T *l, char_u *str, int len));
424static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000425static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
426static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
427static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000428static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000429static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000430static char_u *list2string __ARGS((typval_T *tv, int copyID));
431static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000432static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
433static void set_ref_in_list __ARGS((list_T *l, int copyID));
434static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000435static void dict_unref __ARGS((dict_T *d));
Bram Moolenaar685295c2006-10-15 20:37:38 +0000436static void dict_free __ARGS((dict_T *d, int recurse));
Bram Moolenaar33570922005-01-25 22:26:29 +0000437static dictitem_T *dictitem_alloc __ARGS((char_u *key));
438static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
439static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
440static void dictitem_free __ARGS((dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000441static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000442static int dict_add __ARGS((dict_T *d, dictitem_T *item));
443static long dict_len __ARGS((dict_T *d));
444static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000445static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000446static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000447static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
448static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000449static char_u *string_quote __ARGS((char_u *str, int function));
450static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
451static int find_internal_func __ARGS((char_u *name));
452static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
453static 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));
454static 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 Moolenaar89d40322006-08-29 15:30:07 +0000455static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000456
457static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
458static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
459static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
460static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
463static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
464static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
465static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
467static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
468static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
469static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
470static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000473static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000474static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000477#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000478static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000479static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
481#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000482static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
487static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000500static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000501static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
503static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000514static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000515static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000516static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000517static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000522static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000523static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000530static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000531static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000532static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000534static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000535static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000555static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000556static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000561static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000562static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000577static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000578static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000579static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000580static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000583#ifdef vim_mkdir
584static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
585#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000586static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000589static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000590static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000591static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000592static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000593static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000594static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000595static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000597static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000608static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000609static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000610static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000612static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000617static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000618static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000619static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000620static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000621static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000622static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000623static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000624static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
625static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000626static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000627static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
628static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000629static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2c932302006-03-18 21:42:09 +0000630static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000631#ifdef HAVE_STRFTIME
632static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
633#endif
634static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
635static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
636static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
637static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
638static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
639static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
640static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
641static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
642static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
643static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
644static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
645static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000646static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000647static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000648static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000649static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000650static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000651static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000652static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000653static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
654static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
655static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
656static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
657static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
658static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
659static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
660static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
661static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
662static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
664static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
665static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000666static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
667static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000668static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000669static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000670
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000671static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
672static pos_T *var2fpos __ARGS((typval_T *varp, int lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000673static int get_env_len __ARGS((char_u **arg));
674static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000675static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000676static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
677#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
678#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
679 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000680static 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 +0000681static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000682static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000683static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
684static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000685static typval_T *alloc_tv __ARGS((void));
686static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000687static void init_tv __ARGS((typval_T *varp));
688static long get_tv_number __ARGS((typval_T *varp));
689static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000690static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000691static char_u *get_tv_string __ARGS((typval_T *varp));
692static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000693static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000694static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000695static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000696static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
697static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
698static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
699static void list_one_var __ARGS((dictitem_T *v, char_u *prefix));
700static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
701static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
702static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000703static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000704static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000705static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000706static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000707static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
708static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
709static int eval_fname_script __ARGS((char_u *p));
710static int eval_fname_sid __ARGS((char_u *p));
711static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000712static ufunc_T *find_func __ARGS((char_u *name));
713static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000714static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000715#ifdef FEAT_PROFILE
716static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000717static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
718static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
719static int
720# ifdef __BORLANDC__
721 _RTLENTRYF
722# endif
723 prof_total_cmp __ARGS((const void *s1, const void *s2));
724static int
725# ifdef __BORLANDC__
726 _RTLENTRYF
727# endif
728 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000729#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000730static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000731static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000732static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000733static void func_free __ARGS((ufunc_T *fp));
734static void func_unref __ARGS((char_u *name));
735static void func_ref __ARGS((char_u *name));
736static 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));
737static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000738static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
739static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000740static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000741static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000742static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000743
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000744/* Character used as separated in autoload function/variable names. */
745#define AUTOLOAD_CHAR '#'
746
Bram Moolenaar33570922005-01-25 22:26:29 +0000747/*
748 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000749 */
750 void
751eval_init()
752{
Bram Moolenaar33570922005-01-25 22:26:29 +0000753 int i;
754 struct vimvar *p;
755
756 init_var_dict(&globvardict, &globvars_var);
757 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000758 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000759 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000760
761 for (i = 0; i < VV_LEN; ++i)
762 {
763 p = &vimvars[i];
764 STRCPY(p->vv_di.di_key, p->vv_name);
765 if (p->vv_flags & VV_RO)
766 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
767 else if (p->vv_flags & VV_RO_SBX)
768 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
769 else
770 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000771
772 /* add to v: scope dict, unless the value is not always available */
773 if (p->vv_type != VAR_UNKNOWN)
774 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000775 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000776 /* add to compat scope dict */
777 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000778 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000779}
780
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000781#if defined(EXITFREE) || defined(PROTO)
782 void
783eval_clear()
784{
785 int i;
786 struct vimvar *p;
787
788 for (i = 0; i < VV_LEN; ++i)
789 {
790 p = &vimvars[i];
791 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000792 {
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000793 vim_free(p->vv_di.di_tv.vval.v_string);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000794 p->vv_di.di_tv.vval.v_string = NULL;
795 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000796 }
797 hash_clear(&vimvarht);
798 hash_clear(&compat_hashtab);
799
800 /* script-local variables */
801 for (i = 1; i <= ga_scripts.ga_len; ++i)
802 vars_clear(&SCRIPT_VARS(i));
803 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000804 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000805
806 /* global variables */
807 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000808
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000809 /* functions */
Bram Moolenaard9fba312005-06-26 22:34:35 +0000810 free_all_functions();
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000811 hash_clear(&func_hashtab);
812
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000813 /* autoloaded script names */
814 ga_clear_strings(&ga_loaded);
815
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000816 /* unreferenced lists and dicts */
817 (void)garbage_collect();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000818}
819#endif
820
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000821/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 * Return the name of the executed function.
823 */
824 char_u *
825func_name(cookie)
826 void *cookie;
827{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000828 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829}
830
831/*
832 * Return the address holding the next breakpoint line for a funccall cookie.
833 */
834 linenr_T *
835func_breakpoint(cookie)
836 void *cookie;
837{
Bram Moolenaar33570922005-01-25 22:26:29 +0000838 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839}
840
841/*
842 * Return the address holding the debug tick for a funccall cookie.
843 */
844 int *
845func_dbg_tick(cookie)
846 void *cookie;
847{
Bram Moolenaar33570922005-01-25 22:26:29 +0000848 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849}
850
851/*
852 * Return the nesting level for a funccall cookie.
853 */
854 int
855func_level(cookie)
856 void *cookie;
857{
Bram Moolenaar33570922005-01-25 22:26:29 +0000858 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859}
860
861/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000862funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863
864/*
865 * Return TRUE when a function was ended by a ":return" command.
866 */
867 int
868current_func_returned()
869{
870 return current_funccal->returned;
871}
872
873
874/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 * Set an internal variable to a string value. Creates the variable if it does
876 * not already exist.
877 */
878 void
879set_internal_string_var(name, value)
880 char_u *name;
881 char_u *value;
882{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000883 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000884 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885
886 val = vim_strsave(value);
887 if (val != NULL)
888 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000889 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000890 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000892 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000893 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 }
895 }
896}
897
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000898static lval_T *redir_lval = NULL;
899static char_u *redir_endp = NULL;
900static char_u *redir_varname = NULL;
901
902/*
903 * Start recording command output to a variable
904 * Returns OK if successfully completed the setup. FAIL otherwise.
905 */
906 int
907var_redir_start(name, append)
908 char_u *name;
909 int append; /* append to an existing variable */
910{
911 int save_emsg;
912 int err;
913 typval_T tv;
914
915 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000916 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000917 {
918 EMSG(_(e_invarg));
919 return FAIL;
920 }
921
922 redir_varname = vim_strsave(name);
923 if (redir_varname == NULL)
924 return FAIL;
925
926 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
927 if (redir_lval == NULL)
928 {
929 var_redir_stop();
930 return FAIL;
931 }
932
933 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000934 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
935 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000936 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
937 {
938 if (redir_endp != NULL && *redir_endp != NUL)
939 /* Trailing characters are present after the variable name */
940 EMSG(_(e_trailing));
941 else
942 EMSG(_(e_invarg));
943 var_redir_stop();
944 return FAIL;
945 }
946
947 /* check if we can write to the variable: set it to or append an empty
948 * string */
949 save_emsg = did_emsg;
950 did_emsg = FALSE;
951 tv.v_type = VAR_STRING;
952 tv.vval.v_string = (char_u *)"";
953 if (append)
954 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
955 else
956 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
957 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000958 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000959 if (err)
960 {
961 var_redir_stop();
962 return FAIL;
963 }
964 if (redir_lval->ll_newkey != NULL)
965 {
966 /* Dictionary item was created, don't do it again. */
967 vim_free(redir_lval->ll_newkey);
968 redir_lval->ll_newkey = NULL;
969 }
970
971 return OK;
972}
973
974/*
975 * Append "value[len]" to the variable set by var_redir_start().
976 */
977 void
978var_redir_str(value, len)
979 char_u *value;
980 int len;
981{
982 char_u *val;
983 typval_T tv;
984 int save_emsg;
985 int err;
986
987 if (redir_lval == NULL)
988 return;
989
990 if (len == -1)
991 /* Append the entire string */
992 val = vim_strsave(value);
993 else
994 /* Append only the specified number of characters */
995 val = vim_strnsave(value, len);
996 if (val == NULL)
997 return;
998
999 tv.v_type = VAR_STRING;
1000 tv.vval.v_string = val;
1001
1002 save_emsg = did_emsg;
1003 did_emsg = FALSE;
1004 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1005 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001006 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001007 if (err)
1008 var_redir_stop();
1009
1010 vim_free(tv.vval.v_string);
1011}
1012
1013/*
1014 * Stop redirecting command output to a variable.
1015 */
1016 void
1017var_redir_stop()
1018{
1019 if (redir_lval != NULL)
1020 {
1021 clear_lval(redir_lval);
1022 vim_free(redir_lval);
1023 redir_lval = NULL;
1024 }
1025 vim_free(redir_varname);
1026 redir_varname = NULL;
1027}
1028
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029# if defined(FEAT_MBYTE) || defined(PROTO)
1030 int
1031eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1032 char_u *enc_from;
1033 char_u *enc_to;
1034 char_u *fname_from;
1035 char_u *fname_to;
1036{
1037 int err = FALSE;
1038
1039 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1040 set_vim_var_string(VV_CC_TO, enc_to, -1);
1041 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1042 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1043 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1044 err = TRUE;
1045 set_vim_var_string(VV_CC_FROM, NULL, -1);
1046 set_vim_var_string(VV_CC_TO, NULL, -1);
1047 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1048 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1049
1050 if (err)
1051 return FAIL;
1052 return OK;
1053}
1054# endif
1055
1056# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1057 int
1058eval_printexpr(fname, args)
1059 char_u *fname;
1060 char_u *args;
1061{
1062 int err = FALSE;
1063
1064 set_vim_var_string(VV_FNAME_IN, fname, -1);
1065 set_vim_var_string(VV_CMDARG, args, -1);
1066 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1067 err = TRUE;
1068 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1069 set_vim_var_string(VV_CMDARG, NULL, -1);
1070
1071 if (err)
1072 {
1073 mch_remove(fname);
1074 return FAIL;
1075 }
1076 return OK;
1077}
1078# endif
1079
1080# if defined(FEAT_DIFF) || defined(PROTO)
1081 void
1082eval_diff(origfile, newfile, outfile)
1083 char_u *origfile;
1084 char_u *newfile;
1085 char_u *outfile;
1086{
1087 int err = FALSE;
1088
1089 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1090 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1091 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1092 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1093 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1094 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1095 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1096}
1097
1098 void
1099eval_patch(origfile, difffile, outfile)
1100 char_u *origfile;
1101 char_u *difffile;
1102 char_u *outfile;
1103{
1104 int err;
1105
1106 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1107 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1108 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1109 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1110 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1111 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1112 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1113}
1114# endif
1115
1116/*
1117 * Top level evaluation function, returning a boolean.
1118 * Sets "error" to TRUE if there was an error.
1119 * Return TRUE or FALSE.
1120 */
1121 int
1122eval_to_bool(arg, error, nextcmd, skip)
1123 char_u *arg;
1124 int *error;
1125 char_u **nextcmd;
1126 int skip; /* only parse, don't execute */
1127{
Bram Moolenaar33570922005-01-25 22:26:29 +00001128 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 int retval = FALSE;
1130
1131 if (skip)
1132 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001133 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 else
1136 {
1137 *error = FALSE;
1138 if (!skip)
1139 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001140 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001141 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 }
1143 }
1144 if (skip)
1145 --emsg_skip;
1146
1147 return retval;
1148}
1149
1150/*
1151 * Top level evaluation function, returning a string. If "skip" is TRUE,
1152 * only parsing to "nextcmd" is done, without reporting errors. Return
1153 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1154 */
1155 char_u *
1156eval_to_string_skip(arg, nextcmd, skip)
1157 char_u *arg;
1158 char_u **nextcmd;
1159 int skip; /* only parse, don't execute */
1160{
Bram Moolenaar33570922005-01-25 22:26:29 +00001161 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 char_u *retval;
1163
1164 if (skip)
1165 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001166 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 retval = NULL;
1168 else
1169 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001170 retval = vim_strsave(get_tv_string(&tv));
1171 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 }
1173 if (skip)
1174 --emsg_skip;
1175
1176 return retval;
1177}
1178
1179/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001180 * Skip over an expression at "*pp".
1181 * Return FAIL for an error, OK otherwise.
1182 */
1183 int
1184skip_expr(pp)
1185 char_u **pp;
1186{
Bram Moolenaar33570922005-01-25 22:26:29 +00001187 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001188
1189 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001190 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001191}
1192
1193/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 * Top level evaluation function, returning a string.
1195 * Return pointer to allocated memory, or NULL for failure.
1196 */
1197 char_u *
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001198eval_to_string(arg, nextcmd, dolist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 char_u *arg;
1200 char_u **nextcmd;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001201 int dolist; /* turn List into sequence of lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202{
Bram Moolenaar33570922005-01-25 22:26:29 +00001203 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001205 garray_T ga;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001207 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 retval = NULL;
1209 else
1210 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001211 if (dolist && tv.v_type == VAR_LIST)
1212 {
1213 ga_init2(&ga, (int)sizeof(char), 80);
1214 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
1215 ga_append(&ga, NUL);
1216 retval = (char_u *)ga.ga_data;
1217 }
1218 else
1219 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001220 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 }
1222
1223 return retval;
1224}
1225
1226/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001227 * Call eval_to_string() without using current local variables and using
1228 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 */
1230 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001231eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 char_u *arg;
1233 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001234 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235{
1236 char_u *retval;
1237 void *save_funccalp;
1238
1239 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001240 if (use_sandbox)
1241 ++sandbox;
1242 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001243 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001244 if (use_sandbox)
1245 --sandbox;
1246 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 restore_funccal(save_funccalp);
1248 return retval;
1249}
1250
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251/*
1252 * Top level evaluation function, returning a number.
1253 * Evaluates "expr" silently.
1254 * Returns -1 for an error.
1255 */
1256 int
1257eval_to_number(expr)
1258 char_u *expr;
1259{
Bram Moolenaar33570922005-01-25 22:26:29 +00001260 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001262 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263
1264 ++emsg_off;
1265
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001266 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 retval = -1;
1268 else
1269 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001270 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001271 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 }
1273 --emsg_off;
1274
1275 return retval;
1276}
1277
Bram Moolenaara40058a2005-07-11 22:42:07 +00001278/*
1279 * Prepare v: variable "idx" to be used.
1280 * Save the current typeval in "save_tv".
1281 * When not used yet add the variable to the v: hashtable.
1282 */
1283 static void
1284prepare_vimvar(idx, save_tv)
1285 int idx;
1286 typval_T *save_tv;
1287{
1288 *save_tv = vimvars[idx].vv_tv;
1289 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1290 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1291}
1292
1293/*
1294 * Restore v: variable "idx" to typeval "save_tv".
1295 * When no longer defined, remove the variable from the v: hashtable.
1296 */
1297 static void
1298restore_vimvar(idx, save_tv)
1299 int idx;
1300 typval_T *save_tv;
1301{
1302 hashitem_T *hi;
1303
1304 clear_tv(&vimvars[idx].vv_tv);
1305 vimvars[idx].vv_tv = *save_tv;
1306 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1307 {
1308 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1309 if (HASHITEM_EMPTY(hi))
1310 EMSG2(_(e_intern2), "restore_vimvar()");
1311 else
1312 hash_remove(&vimvarht, hi);
1313 }
1314}
1315
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001316#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001317/*
1318 * Evaluate an expression to a list with suggestions.
1319 * For the "expr:" part of 'spellsuggest'.
1320 */
1321 list_T *
1322eval_spell_expr(badword, expr)
1323 char_u *badword;
1324 char_u *expr;
1325{
1326 typval_T save_val;
1327 typval_T rettv;
1328 list_T *list = NULL;
1329 char_u *p = skipwhite(expr);
1330
1331 /* Set "v:val" to the bad word. */
1332 prepare_vimvar(VV_VAL, &save_val);
1333 vimvars[VV_VAL].vv_type = VAR_STRING;
1334 vimvars[VV_VAL].vv_str = badword;
1335 if (p_verbose == 0)
1336 ++emsg_off;
1337
1338 if (eval1(&p, &rettv, TRUE) == OK)
1339 {
1340 if (rettv.v_type != VAR_LIST)
1341 clear_tv(&rettv);
1342 else
1343 list = rettv.vval.v_list;
1344 }
1345
1346 if (p_verbose == 0)
1347 --emsg_off;
1348 vimvars[VV_VAL].vv_str = NULL;
1349 restore_vimvar(VV_VAL, &save_val);
1350
1351 return list;
1352}
1353
1354/*
1355 * "list" is supposed to contain two items: a word and a number. Return the
1356 * word in "pp" and the number as the return value.
1357 * Return -1 if anything isn't right.
1358 * Used to get the good word and score from the eval_spell_expr() result.
1359 */
1360 int
1361get_spellword(list, pp)
1362 list_T *list;
1363 char_u **pp;
1364{
1365 listitem_T *li;
1366
1367 li = list->lv_first;
1368 if (li == NULL)
1369 return -1;
1370 *pp = get_tv_string(&li->li_tv);
1371
1372 li = li->li_next;
1373 if (li == NULL)
1374 return -1;
1375 return get_tv_number(&li->li_tv);
1376}
1377#endif
1378
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001379/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001380 * Top level evaluation function.
1381 * Returns an allocated typval_T with the result.
1382 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001383 */
1384 typval_T *
1385eval_expr(arg, nextcmd)
1386 char_u *arg;
1387 char_u **nextcmd;
1388{
1389 typval_T *tv;
1390
1391 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001392 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001393 {
1394 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001395 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001396 }
1397
1398 return tv;
1399}
1400
1401
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1403/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001404 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 * Uses argv[argc] for the function arguments.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001406 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001408 static int
1409call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 char_u *func;
1411 int argc;
1412 char_u **argv;
1413 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001414 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415{
Bram Moolenaar33570922005-01-25 22:26:29 +00001416 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 long n;
1418 int len;
1419 int i;
1420 int doesrange;
1421 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001422 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001424 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001426 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427
1428 for (i = 0; i < argc; i++)
1429 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001430 /* Pass a NULL or empty argument as an empty string */
1431 if (argv[i] == NULL || *argv[i] == NUL)
1432 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001433 argvars[i].v_type = VAR_STRING;
1434 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001435 continue;
1436 }
1437
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 /* Recognize a number argument, the others must be strings. */
1439 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1440 if (len != 0 && len == (int)STRLEN(argv[i]))
1441 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001442 argvars[i].v_type = VAR_NUMBER;
1443 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 }
1445 else
1446 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001447 argvars[i].v_type = VAR_STRING;
1448 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 }
1450 }
1451
1452 if (safe)
1453 {
1454 save_funccalp = save_funccal();
1455 ++sandbox;
1456 }
1457
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001458 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1459 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001461 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 if (safe)
1463 {
1464 --sandbox;
1465 restore_funccal(save_funccalp);
1466 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001467 vim_free(argvars);
1468
1469 if (ret == FAIL)
1470 clear_tv(rettv);
1471
1472 return ret;
1473}
1474
1475/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001476 * Call vimL function "func" and return the result as a string.
1477 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001478 * Uses argv[argc] for the function arguments.
1479 */
1480 void *
1481call_func_retstr(func, argc, argv, safe)
1482 char_u *func;
1483 int argc;
1484 char_u **argv;
1485 int safe; /* use the sandbox */
1486{
1487 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001488 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001489
1490 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1491 return NULL;
1492
1493 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001494 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 return retval;
1496}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001497
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001498#if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001499/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001500 * Call vimL function "func" and return the result as a number.
1501 * Returns -1 when calling the function fails.
1502 * Uses argv[argc] for the function arguments.
1503 */
1504 long
1505call_func_retnr(func, argc, argv, safe)
1506 char_u *func;
1507 int argc;
1508 char_u **argv;
1509 int safe; /* use the sandbox */
1510{
1511 typval_T rettv;
1512 long retval;
1513
1514 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1515 return -1;
1516
1517 retval = get_tv_number_chk(&rettv, NULL);
1518 clear_tv(&rettv);
1519 return retval;
1520}
1521#endif
1522
1523/*
1524 * Call vimL function "func" and return the result as a list
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001525 * Uses argv[argc] for the function arguments.
1526 */
1527 void *
1528call_func_retlist(func, argc, argv, safe)
1529 char_u *func;
1530 int argc;
1531 char_u **argv;
1532 int safe; /* use the sandbox */
1533{
1534 typval_T rettv;
1535
1536 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1537 return NULL;
1538
1539 if (rettv.v_type != VAR_LIST)
1540 {
1541 clear_tv(&rettv);
1542 return NULL;
1543 }
1544
1545 return rettv.vval.v_list;
1546}
1547
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548#endif
1549
1550/*
1551 * Save the current function call pointer, and set it to NULL.
1552 * Used when executing autocommands and for ":source".
1553 */
1554 void *
1555save_funccal()
1556{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001557 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 current_funccal = NULL;
1560 return (void *)fc;
1561}
1562
1563 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001564restore_funccal(vfc)
1565 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001567 funccall_T *fc = (funccall_T *)vfc;
1568
1569 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570}
1571
Bram Moolenaar05159a02005-02-26 23:04:13 +00001572#if defined(FEAT_PROFILE) || defined(PROTO)
1573/*
1574 * Prepare profiling for entering a child or something else that is not
1575 * counted for the script/function itself.
1576 * Should always be called in pair with prof_child_exit().
1577 */
1578 void
1579prof_child_enter(tm)
1580 proftime_T *tm; /* place to store waittime */
1581{
1582 funccall_T *fc = current_funccal;
1583
1584 if (fc != NULL && fc->func->uf_profiling)
1585 profile_start(&fc->prof_child);
1586 script_prof_save(tm);
1587}
1588
1589/*
1590 * Take care of time spent in a child.
1591 * Should always be called after prof_child_enter().
1592 */
1593 void
1594prof_child_exit(tm)
1595 proftime_T *tm; /* where waittime was stored */
1596{
1597 funccall_T *fc = current_funccal;
1598
1599 if (fc != NULL && fc->func->uf_profiling)
1600 {
1601 profile_end(&fc->prof_child);
1602 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1603 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1604 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1605 }
1606 script_prof_restore(tm);
1607}
1608#endif
1609
1610
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611#ifdef FEAT_FOLDING
1612/*
1613 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1614 * it in "*cp". Doesn't give error messages.
1615 */
1616 int
1617eval_foldexpr(arg, cp)
1618 char_u *arg;
1619 int *cp;
1620{
Bram Moolenaar33570922005-01-25 22:26:29 +00001621 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 int retval;
1623 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001624 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1625 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626
1627 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001628 if (use_sandbox)
1629 ++sandbox;
1630 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001632 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 retval = 0;
1634 else
1635 {
1636 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001637 if (tv.v_type == VAR_NUMBER)
1638 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001639 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 retval = 0;
1641 else
1642 {
1643 /* If the result is a string, check if there is a non-digit before
1644 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001645 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 if (!VIM_ISDIGIT(*s) && *s != '-')
1647 *cp = *s++;
1648 retval = atol((char *)s);
1649 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001650 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 }
1652 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001653 if (use_sandbox)
1654 --sandbox;
1655 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656
1657 return retval;
1658}
1659#endif
1660
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001662 * ":let" list all variable values
1663 * ":let var1 var2" list variable values
1664 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001665 * ":let var += expr" assignment command.
1666 * ":let var -= expr" assignment command.
1667 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001668 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 */
1670 void
1671ex_let(eap)
1672 exarg_T *eap;
1673{
1674 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001675 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001676 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001678 int var_count = 0;
1679 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001680 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001681 char_u *argend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682
Bram Moolenaardb552d602006-03-23 22:59:57 +00001683 argend = skip_var_list(arg, &var_count, &semicolon);
1684 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001685 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001686 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1687 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001688 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001689 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001691 /*
1692 * ":let" without "=": list variables
1693 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001694 if (*arg == '[')
1695 EMSG(_(e_invarg));
1696 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001697 /* ":let var1 var2" */
1698 arg = list_arg_vars(eap, arg);
1699 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001700 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001701 /* ":let" */
Bram Moolenaara7043832005-01-21 11:56:39 +00001702 list_glob_vars();
1703 list_buf_vars();
1704 list_win_vars();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001705#ifdef FEAT_WINDOWS
1706 list_tab_vars();
1707#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001708 list_script_vars();
1709 list_func_vars();
Bram Moolenaara7043832005-01-21 11:56:39 +00001710 list_vim_vars();
1711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 eap->nextcmd = check_nextcmd(arg);
1713 }
1714 else
1715 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001716 op[0] = '=';
1717 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001718 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001719 {
1720 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1721 op[0] = expr[-1]; /* +=, -= or .= */
1722 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001723 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001724
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 if (eap->skip)
1726 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001727 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 if (eap->skip)
1729 {
1730 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001731 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 --emsg_skip;
1733 }
1734 else if (i != FAIL)
1735 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001736 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001737 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001738 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 }
1740 }
1741}
1742
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001743/*
1744 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1745 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001746 * When "nextchars" is not NULL it points to a string with characters that
1747 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1748 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001749 * Returns OK or FAIL;
1750 */
1751 static int
1752ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1753 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001754 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001755 int copy; /* copy values from "tv", don't move */
1756 int semicolon; /* from skip_var_list() */
1757 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001758 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001759{
1760 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001761 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001762 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001763 listitem_T *item;
1764 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001765
1766 if (*arg != '[')
1767 {
1768 /*
1769 * ":let var = expr" or ":for var in list"
1770 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001771 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001772 return FAIL;
1773 return OK;
1774 }
1775
1776 /*
1777 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1778 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001779 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001780 {
1781 EMSG(_(e_listreq));
1782 return FAIL;
1783 }
1784
1785 i = list_len(l);
1786 if (semicolon == 0 && var_count < i)
1787 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001788 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001789 return FAIL;
1790 }
1791 if (var_count - semicolon > i)
1792 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001793 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001794 return FAIL;
1795 }
1796
1797 item = l->lv_first;
1798 while (*arg != ']')
1799 {
1800 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001801 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001802 item = item->li_next;
1803 if (arg == NULL)
1804 return FAIL;
1805
1806 arg = skipwhite(arg);
1807 if (*arg == ';')
1808 {
1809 /* Put the rest of the list (may be empty) in the var after ';'.
1810 * Create a new list for this. */
1811 l = list_alloc();
1812 if (l == NULL)
1813 return FAIL;
1814 while (item != NULL)
1815 {
1816 list_append_tv(l, &item->li_tv);
1817 item = item->li_next;
1818 }
1819
1820 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001821 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001822 ltv.vval.v_list = l;
1823 l->lv_refcount = 1;
1824
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001825 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1826 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001827 clear_tv(&ltv);
1828 if (arg == NULL)
1829 return FAIL;
1830 break;
1831 }
1832 else if (*arg != ',' && *arg != ']')
1833 {
1834 EMSG2(_(e_intern2), "ex_let_vars()");
1835 return FAIL;
1836 }
1837 }
1838
1839 return OK;
1840}
1841
1842/*
1843 * Skip over assignable variable "var" or list of variables "[var, var]".
1844 * Used for ":let varvar = expr" and ":for varvar in expr".
1845 * For "[var, var]" increment "*var_count" for each variable.
1846 * for "[var, var; var]" set "semicolon".
1847 * Return NULL for an error.
1848 */
1849 static char_u *
1850skip_var_list(arg, var_count, semicolon)
1851 char_u *arg;
1852 int *var_count;
1853 int *semicolon;
1854{
1855 char_u *p, *s;
1856
1857 if (*arg == '[')
1858 {
1859 /* "[var, var]": find the matching ']'. */
1860 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001861 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001862 {
1863 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1864 s = skip_var_one(p);
1865 if (s == p)
1866 {
1867 EMSG2(_(e_invarg2), p);
1868 return NULL;
1869 }
1870 ++*var_count;
1871
1872 p = skipwhite(s);
1873 if (*p == ']')
1874 break;
1875 else if (*p == ';')
1876 {
1877 if (*semicolon == 1)
1878 {
1879 EMSG(_("Double ; in list of variables"));
1880 return NULL;
1881 }
1882 *semicolon = 1;
1883 }
1884 else if (*p != ',')
1885 {
1886 EMSG2(_(e_invarg2), p);
1887 return NULL;
1888 }
1889 }
1890 return p + 1;
1891 }
1892 else
1893 return skip_var_one(arg);
1894}
1895
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001896/*
Bram Moolenaar92124a32005-06-17 22:03:40 +00001897 * Skip one (assignable) variable name, includig @r, $VAR, &option, d.key,
1898 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001899 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001900 static char_u *
1901skip_var_one(arg)
1902 char_u *arg;
1903{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001904 if (*arg == '@' && arg[1] != NUL)
1905 return arg + 2;
1906 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1907 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001908}
1909
Bram Moolenaara7043832005-01-21 11:56:39 +00001910/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001911 * List variables for hashtab "ht" with prefix "prefix".
1912 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001913 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001914 static void
Bram Moolenaar33570922005-01-25 22:26:29 +00001915list_hashtable_vars(ht, prefix, empty)
1916 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001917 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001918 int empty;
Bram Moolenaara7043832005-01-21 11:56:39 +00001919{
Bram Moolenaar33570922005-01-25 22:26:29 +00001920 hashitem_T *hi;
1921 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001922 int todo;
1923
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001924 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001925 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1926 {
1927 if (!HASHITEM_EMPTY(hi))
1928 {
1929 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001930 di = HI2DI(hi);
1931 if (empty || di->di_tv.v_type != VAR_STRING
1932 || di->di_tv.vval.v_string != NULL)
1933 list_one_var(di, prefix);
Bram Moolenaara7043832005-01-21 11:56:39 +00001934 }
1935 }
1936}
1937
1938/*
1939 * List global variables.
1940 */
1941 static void
1942list_glob_vars()
1943{
Bram Moolenaar33570922005-01-25 22:26:29 +00001944 list_hashtable_vars(&globvarht, (char_u *)"", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001945}
1946
1947/*
1948 * List buffer variables.
1949 */
1950 static void
1951list_buf_vars()
1952{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001953 char_u numbuf[NUMBUFLEN];
1954
Bram Moolenaar33570922005-01-25 22:26:29 +00001955 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001956
1957 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
1958 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER, numbuf);
Bram Moolenaara7043832005-01-21 11:56:39 +00001959}
1960
1961/*
1962 * List window variables.
1963 */
1964 static void
1965list_win_vars()
1966{
Bram Moolenaar33570922005-01-25 22:26:29 +00001967 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001968}
1969
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001970#ifdef FEAT_WINDOWS
1971/*
1972 * List tab page variables.
1973 */
1974 static void
1975list_tab_vars()
1976{
1977 list_hashtable_vars(&curtab->tp_vars.dv_hashtab, (char_u *)"t:", TRUE);
1978}
1979#endif
1980
Bram Moolenaara7043832005-01-21 11:56:39 +00001981/*
1982 * List Vim variables.
1983 */
1984 static void
1985list_vim_vars()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001986{
Bram Moolenaar33570922005-01-25 22:26:29 +00001987 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001988}
1989
1990/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001991 * List script-local variables, if there is a script.
1992 */
1993 static void
1994list_script_vars()
1995{
1996 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
1997 list_hashtable_vars(&SCRIPT_VARS(current_SID), (char_u *)"s:", FALSE);
1998}
1999
2000/*
2001 * List function variables, if there is a function.
2002 */
2003 static void
2004list_func_vars()
2005{
2006 if (current_funccal != NULL)
2007 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
2008 (char_u *)"l:", FALSE);
2009}
2010
2011/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002012 * List variables in "arg".
2013 */
2014 static char_u *
2015list_arg_vars(eap, arg)
2016 exarg_T *eap;
2017 char_u *arg;
2018{
2019 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002020 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002021 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002022 char_u *name_start;
2023 char_u *arg_subsc;
2024 char_u *tofree;
2025 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002026
2027 while (!ends_excmd(*arg) && !got_int)
2028 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002029 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002030 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002031 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002032 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2033 {
2034 emsg_severe = TRUE;
2035 EMSG(_(e_trailing));
2036 break;
2037 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002038 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002039 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002040 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002041 /* get_name_len() takes care of expanding curly braces */
2042 name_start = name = arg;
2043 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2044 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002045 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002046 /* This is mainly to keep test 49 working: when expanding
2047 * curly braces fails overrule the exception error message. */
2048 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002049 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002050 emsg_severe = TRUE;
2051 EMSG2(_(e_invarg2), arg);
2052 break;
2053 }
2054 error = TRUE;
2055 }
2056 else
2057 {
2058 if (tofree != NULL)
2059 name = tofree;
2060 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002061 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002062 else
2063 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002064 /* handle d.key, l[idx], f(expr) */
2065 arg_subsc = arg;
2066 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002067 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002068 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002069 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002070 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002071 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002072 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002073 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002074 case 'g': list_glob_vars(); break;
2075 case 'b': list_buf_vars(); break;
2076 case 'w': list_win_vars(); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002077#ifdef FEAT_WINDOWS
2078 case 't': list_tab_vars(); break;
2079#endif
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002080 case 'v': list_vim_vars(); break;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002081 case 's': list_script_vars(); break;
2082 case 'l': list_func_vars(); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002083 default:
2084 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002085 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002086 }
2087 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002088 {
2089 char_u numbuf[NUMBUFLEN];
2090 char_u *tf;
2091 int c;
2092 char_u *s;
2093
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002094 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002095 c = *arg;
2096 *arg = NUL;
2097 list_one_var_a((char_u *)"",
2098 arg == arg_subsc ? name : name_start,
2099 tv.v_type, s == NULL ? (char_u *)"" : s);
2100 *arg = c;
2101 vim_free(tf);
2102 }
2103 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002104 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002105 }
2106 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002107
2108 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002109 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002110
2111 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002112 }
2113
2114 return arg;
2115}
2116
2117/*
2118 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2119 * Returns a pointer to the char just after the var name.
2120 * Returns NULL if there is an error.
2121 */
2122 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002123ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002124 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002125 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002126 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002127 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002128 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002129{
2130 int c1;
2131 char_u *name;
2132 char_u *p;
2133 char_u *arg_end = NULL;
2134 int len;
2135 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002136 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002137
2138 /*
2139 * ":let $VAR = expr": Set environment variable.
2140 */
2141 if (*arg == '$')
2142 {
2143 /* Find the end of the name. */
2144 ++arg;
2145 name = arg;
2146 len = get_env_len(&arg);
2147 if (len == 0)
2148 EMSG2(_(e_invarg2), name - 1);
2149 else
2150 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002151 if (op != NULL && (*op == '+' || *op == '-'))
2152 EMSG2(_(e_letwrong), op);
2153 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002154 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002155 EMSG(_(e_letunexp));
2156 else
2157 {
2158 c1 = name[len];
2159 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002160 p = get_tv_string_chk(tv);
2161 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002162 {
2163 int mustfree = FALSE;
2164 char_u *s = vim_getenv(name, &mustfree);
2165
2166 if (s != NULL)
2167 {
2168 p = tofree = concat_str(s, p);
2169 if (mustfree)
2170 vim_free(s);
2171 }
2172 }
2173 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002174 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002175 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002176 if (STRICMP(name, "HOME") == 0)
2177 init_homedir();
2178 else if (didset_vim && STRICMP(name, "VIM") == 0)
2179 didset_vim = FALSE;
2180 else if (didset_vimruntime
2181 && STRICMP(name, "VIMRUNTIME") == 0)
2182 didset_vimruntime = FALSE;
2183 arg_end = arg;
2184 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002185 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002186 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002187 }
2188 }
2189 }
2190
2191 /*
2192 * ":let &option = expr": Set option value.
2193 * ":let &l:option = expr": Set local option value.
2194 * ":let &g:option = expr": Set global option value.
2195 */
2196 else if (*arg == '&')
2197 {
2198 /* Find the end of the name. */
2199 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002200 if (p == NULL || (endchars != NULL
2201 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002202 EMSG(_(e_letunexp));
2203 else
2204 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002205 long n;
2206 int opt_type;
2207 long numval;
2208 char_u *stringval = NULL;
2209 char_u *s;
2210
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002211 c1 = *p;
2212 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002213
2214 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002215 s = get_tv_string_chk(tv); /* != NULL if number or string */
2216 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002217 {
2218 opt_type = get_option_value(arg, &numval,
2219 &stringval, opt_flags);
2220 if ((opt_type == 1 && *op == '.')
2221 || (opt_type == 0 && *op != '.'))
2222 EMSG2(_(e_letwrong), op);
2223 else
2224 {
2225 if (opt_type == 1) /* number */
2226 {
2227 if (*op == '+')
2228 n = numval + n;
2229 else
2230 n = numval - n;
2231 }
2232 else if (opt_type == 0 && stringval != NULL) /* string */
2233 {
2234 s = concat_str(stringval, s);
2235 vim_free(stringval);
2236 stringval = s;
2237 }
2238 }
2239 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002240 if (s != NULL)
2241 {
2242 set_option_value(arg, n, s, opt_flags);
2243 arg_end = p;
2244 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002245 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002246 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002247 }
2248 }
2249
2250 /*
2251 * ":let @r = expr": Set register contents.
2252 */
2253 else if (*arg == '@')
2254 {
2255 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002256 if (op != NULL && (*op == '+' || *op == '-'))
2257 EMSG2(_(e_letwrong), op);
2258 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002259 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002260 EMSG(_(e_letunexp));
2261 else
2262 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002263 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002264 char_u *s;
2265
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002266 p = get_tv_string_chk(tv);
2267 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002268 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002269 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002270 if (s != NULL)
2271 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002272 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002273 vim_free(s);
2274 }
2275 }
2276 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002277 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002278 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002279 arg_end = arg + 1;
2280 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002281 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002282 }
2283 }
2284
2285 /*
2286 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002287 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002288 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002289 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002290 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002291 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002292
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002293 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002294 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002295 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002296 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2297 EMSG(_(e_letunexp));
2298 else
2299 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002300 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002301 arg_end = p;
2302 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002303 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002304 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002305 }
2306
2307 else
2308 EMSG2(_(e_invarg2), arg);
2309
2310 return arg_end;
2311}
2312
2313/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002314 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2315 */
2316 static int
2317check_changedtick(arg)
2318 char_u *arg;
2319{
2320 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2321 {
2322 EMSG2(_(e_readonlyvar), arg);
2323 return TRUE;
2324 }
2325 return FALSE;
2326}
2327
2328/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002329 * Get an lval: variable, Dict item or List item that can be assigned a value
2330 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2331 * "name.key", "name.key[expr]" etc.
2332 * Indexing only works if "name" is an existing List or Dictionary.
2333 * "name" points to the start of the name.
2334 * If "rettv" is not NULL it points to the value to be assigned.
2335 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2336 * wrong; must end in space or cmd separator.
2337 *
2338 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002339 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002340 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002341 */
2342 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002343get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002344 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002345 typval_T *rettv;
2346 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002347 int unlet;
2348 int skip;
2349 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002350 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002351{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002352 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002353 char_u *expr_start, *expr_end;
2354 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002355 dictitem_T *v;
2356 typval_T var1;
2357 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002358 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002359 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002360 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002361 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002362 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002363
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002364 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002365 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002366
2367 if (skip)
2368 {
2369 /* When skipping just find the end of the name. */
2370 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002371 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002372 }
2373
2374 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002375 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002376 if (expr_start != NULL)
2377 {
2378 /* Don't expand the name when we already know there is an error. */
2379 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2380 && *p != '[' && *p != '.')
2381 {
2382 EMSG(_(e_trailing));
2383 return NULL;
2384 }
2385
2386 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2387 if (lp->ll_exp_name == NULL)
2388 {
2389 /* Report an invalid expression in braces, unless the
2390 * expression evaluation has been cancelled due to an
2391 * aborting error, an interrupt, or an exception. */
2392 if (!aborting() && !quiet)
2393 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002394 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002395 EMSG2(_(e_invarg2), name);
2396 return NULL;
2397 }
2398 }
2399 lp->ll_name = lp->ll_exp_name;
2400 }
2401 else
2402 lp->ll_name = name;
2403
2404 /* Without [idx] or .key we are done. */
2405 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2406 return p;
2407
2408 cc = *p;
2409 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002410 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002411 if (v == NULL && !quiet)
2412 EMSG2(_(e_undefvar), lp->ll_name);
2413 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002414 if (v == NULL)
2415 return NULL;
2416
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002417 /*
2418 * Loop until no more [idx] or .key is following.
2419 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002420 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002421 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002422 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002423 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2424 && !(lp->ll_tv->v_type == VAR_DICT
2425 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002426 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002427 if (!quiet)
2428 EMSG(_("E689: Can only index a List or Dictionary"));
2429 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002430 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002431 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002432 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002433 if (!quiet)
2434 EMSG(_("E708: [:] must come last"));
2435 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002436 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002437
Bram Moolenaar8c711452005-01-14 21:53:12 +00002438 len = -1;
2439 if (*p == '.')
2440 {
2441 key = p + 1;
2442 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2443 ;
2444 if (len == 0)
2445 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002446 if (!quiet)
2447 EMSG(_(e_emptykey));
2448 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002449 }
2450 p = key + len;
2451 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002452 else
2453 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002454 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002455 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002456 if (*p == ':')
2457 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002458 else
2459 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002460 empty1 = FALSE;
2461 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002462 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002463 if (get_tv_string_chk(&var1) == NULL)
2464 {
2465 /* not a number or string */
2466 clear_tv(&var1);
2467 return NULL;
2468 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002469 }
2470
2471 /* Optionally get the second index [ :expr]. */
2472 if (*p == ':')
2473 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002474 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002475 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002476 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002477 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002478 if (!empty1)
2479 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002480 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002481 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002482 if (rettv != NULL && (rettv->v_type != VAR_LIST
2483 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002484 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002485 if (!quiet)
2486 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002487 if (!empty1)
2488 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002489 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002490 }
2491 p = skipwhite(p + 1);
2492 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002493 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002494 else
2495 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002496 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002497 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2498 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002499 if (!empty1)
2500 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002501 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002502 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002503 if (get_tv_string_chk(&var2) == NULL)
2504 {
2505 /* not a number or string */
2506 if (!empty1)
2507 clear_tv(&var1);
2508 clear_tv(&var2);
2509 return NULL;
2510 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002511 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002512 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002513 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002514 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002515 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002516
Bram Moolenaar8c711452005-01-14 21:53:12 +00002517 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002518 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002519 if (!quiet)
2520 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002521 if (!empty1)
2522 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002523 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002524 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002525 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002526 }
2527
2528 /* Skip to past ']'. */
2529 ++p;
2530 }
2531
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002532 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002533 {
2534 if (len == -1)
2535 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002536 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002537 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002538 if (*key == NUL)
2539 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002540 if (!quiet)
2541 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002542 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002543 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002544 }
2545 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002546 lp->ll_list = NULL;
2547 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002548 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002549 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002550 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002551 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002552 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002553 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002554 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002555 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002556 if (len == -1)
2557 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002558 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002559 }
2560 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002561 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002562 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002563 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002564 if (len == -1)
2565 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002566 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002567 p = NULL;
2568 break;
2569 }
2570 if (len == -1)
2571 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002572 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002573 }
2574 else
2575 {
2576 /*
2577 * Get the number and item for the only or first index of the List.
2578 */
2579 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002580 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002581 else
2582 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002583 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002584 clear_tv(&var1);
2585 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002586 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002587 lp->ll_list = lp->ll_tv->vval.v_list;
2588 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2589 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002590 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002591 if (lp->ll_n1 < 0)
2592 {
2593 lp->ll_n1 = 0;
2594 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2595 }
2596 }
2597 if (lp->ll_li == NULL)
2598 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002599 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002600 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002601 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002602 }
2603
2604 /*
2605 * May need to find the item or absolute index for the second
2606 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002607 * When no index given: "lp->ll_empty2" is TRUE.
2608 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002609 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002610 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002611 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002612 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002613 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002614 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002615 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002616 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002617 if (ni == NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002618 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002619 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002620 }
2621
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002622 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2623 if (lp->ll_n1 < 0)
2624 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2625 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002626 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002627 }
2628
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002629 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002630 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002631 }
2632
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002633 return p;
2634}
2635
2636/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002637 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002638 */
2639 static void
2640clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002641 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002642{
2643 vim_free(lp->ll_exp_name);
2644 vim_free(lp->ll_newkey);
2645}
2646
2647/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002648 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002649 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002650 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002651 */
2652 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002653set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002654 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002655 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002656 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002657 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002658 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002659{
2660 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002661 listitem_T *ri;
2662 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002663
2664 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002665 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002666 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002667 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002668 cc = *endp;
2669 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002670 if (op != NULL && *op != '=')
2671 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002672 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002673
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002674 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002675 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002676 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002677 {
2678 if (tv_op(&tv, rettv, op) == OK)
2679 set_var(lp->ll_name, &tv, FALSE);
2680 clear_tv(&tv);
2681 }
2682 }
2683 else
2684 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002685 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002686 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002687 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002688 else if (tv_check_lock(lp->ll_newkey == NULL
2689 ? lp->ll_tv->v_lock
2690 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2691 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002692 else if (lp->ll_range)
2693 {
2694 /*
2695 * Assign the List values to the list items.
2696 */
2697 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002698 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002699 if (op != NULL && *op != '=')
2700 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2701 else
2702 {
2703 clear_tv(&lp->ll_li->li_tv);
2704 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2705 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002706 ri = ri->li_next;
2707 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2708 break;
2709 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002710 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002711 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002712 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002713 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002714 ri = NULL;
2715 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002716 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002717 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002718 lp->ll_li = lp->ll_li->li_next;
2719 ++lp->ll_n1;
2720 }
2721 if (ri != NULL)
2722 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002723 else if (lp->ll_empty2
2724 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002725 : lp->ll_n1 != lp->ll_n2)
2726 EMSG(_("E711: List value has not enough items"));
2727 }
2728 else
2729 {
2730 /*
2731 * Assign to a List or Dictionary item.
2732 */
2733 if (lp->ll_newkey != NULL)
2734 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002735 if (op != NULL && *op != '=')
2736 {
2737 EMSG2(_(e_letwrong), op);
2738 return;
2739 }
2740
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002741 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002742 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002743 if (di == NULL)
2744 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002745 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2746 {
2747 vim_free(di);
2748 return;
2749 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002750 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002751 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002752 else if (op != NULL && *op != '=')
2753 {
2754 tv_op(lp->ll_tv, rettv, op);
2755 return;
2756 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002757 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002758 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002759
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002760 /*
2761 * Assign the value to the variable or list item.
2762 */
2763 if (copy)
2764 copy_tv(rettv, lp->ll_tv);
2765 else
2766 {
2767 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002768 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002769 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002770 }
2771 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002772}
2773
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002774/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002775 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2776 * Returns OK or FAIL.
2777 */
2778 static int
2779tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002780 typval_T *tv1;
2781 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002782 char_u *op;
2783{
2784 long n;
2785 char_u numbuf[NUMBUFLEN];
2786 char_u *s;
2787
2788 /* Can't do anything with a Funcref or a Dict on the right. */
2789 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2790 {
2791 switch (tv1->v_type)
2792 {
2793 case VAR_DICT:
2794 case VAR_FUNC:
2795 break;
2796
2797 case VAR_LIST:
2798 if (*op != '+' || tv2->v_type != VAR_LIST)
2799 break;
2800 /* List += List */
2801 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2802 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2803 return OK;
2804
2805 case VAR_NUMBER:
2806 case VAR_STRING:
2807 if (tv2->v_type == VAR_LIST)
2808 break;
2809 if (*op == '+' || *op == '-')
2810 {
2811 /* nr += nr or nr -= nr*/
2812 n = get_tv_number(tv1);
2813 if (*op == '+')
2814 n += get_tv_number(tv2);
2815 else
2816 n -= get_tv_number(tv2);
2817 clear_tv(tv1);
2818 tv1->v_type = VAR_NUMBER;
2819 tv1->vval.v_number = n;
2820 }
2821 else
2822 {
2823 /* str .= str */
2824 s = get_tv_string(tv1);
2825 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2826 clear_tv(tv1);
2827 tv1->v_type = VAR_STRING;
2828 tv1->vval.v_string = s;
2829 }
2830 return OK;
2831 }
2832 }
2833
2834 EMSG2(_(e_letwrong), op);
2835 return FAIL;
2836}
2837
2838/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002839 * Add a watcher to a list.
2840 */
2841 static void
2842list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002843 list_T *l;
2844 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002845{
2846 lw->lw_next = l->lv_watch;
2847 l->lv_watch = lw;
2848}
2849
2850/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002851 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002852 * No warning when it isn't found...
2853 */
2854 static void
2855list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002856 list_T *l;
2857 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002858{
Bram Moolenaar33570922005-01-25 22:26:29 +00002859 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002860
2861 lwp = &l->lv_watch;
2862 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2863 {
2864 if (lw == lwrem)
2865 {
2866 *lwp = lw->lw_next;
2867 break;
2868 }
2869 lwp = &lw->lw_next;
2870 }
2871}
2872
2873/*
2874 * Just before removing an item from a list: advance watchers to the next
2875 * item.
2876 */
2877 static void
2878list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002879 list_T *l;
2880 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002881{
Bram Moolenaar33570922005-01-25 22:26:29 +00002882 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002883
2884 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2885 if (lw->lw_item == item)
2886 lw->lw_item = item->li_next;
2887}
2888
2889/*
2890 * Evaluate the expression used in a ":for var in expr" command.
2891 * "arg" points to "var".
2892 * Set "*errp" to TRUE for an error, FALSE otherwise;
2893 * Return a pointer that holds the info. Null when there is an error.
2894 */
2895 void *
2896eval_for_line(arg, errp, nextcmdp, skip)
2897 char_u *arg;
2898 int *errp;
2899 char_u **nextcmdp;
2900 int skip;
2901{
Bram Moolenaar33570922005-01-25 22:26:29 +00002902 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002903 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002904 typval_T tv;
2905 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002906
2907 *errp = TRUE; /* default: there is an error */
2908
Bram Moolenaar33570922005-01-25 22:26:29 +00002909 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002910 if (fi == NULL)
2911 return NULL;
2912
2913 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2914 if (expr == NULL)
2915 return fi;
2916
2917 expr = skipwhite(expr);
2918 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2919 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002920 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002921 return fi;
2922 }
2923
2924 if (skip)
2925 ++emsg_skip;
2926 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2927 {
2928 *errp = FALSE;
2929 if (!skip)
2930 {
2931 l = tv.vval.v_list;
2932 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002933 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002934 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002935 clear_tv(&tv);
2936 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002937 else
2938 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002939 /* No need to increment the refcount, it's already set for the
2940 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002941 fi->fi_list = l;
2942 list_add_watch(l, &fi->fi_lw);
2943 fi->fi_lw.lw_item = l->lv_first;
2944 }
2945 }
2946 }
2947 if (skip)
2948 --emsg_skip;
2949
2950 return fi;
2951}
2952
2953/*
2954 * Use the first item in a ":for" list. Advance to the next.
2955 * Assign the values to the variable (list). "arg" points to the first one.
2956 * Return TRUE when a valid item was found, FALSE when at end of list or
2957 * something wrong.
2958 */
2959 int
2960next_for_item(fi_void, arg)
2961 void *fi_void;
2962 char_u *arg;
2963{
Bram Moolenaar33570922005-01-25 22:26:29 +00002964 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002965 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002966 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002967
2968 item = fi->fi_lw.lw_item;
2969 if (item == NULL)
2970 result = FALSE;
2971 else
2972 {
2973 fi->fi_lw.lw_item = item->li_next;
2974 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2975 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2976 }
2977 return result;
2978}
2979
2980/*
2981 * Free the structure used to store info used by ":for".
2982 */
2983 void
2984free_for_info(fi_void)
2985 void *fi_void;
2986{
Bram Moolenaar33570922005-01-25 22:26:29 +00002987 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002988
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002989 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002990 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002991 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002992 list_unref(fi->fi_list);
2993 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002994 vim_free(fi);
2995}
2996
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2998
2999 void
3000set_context_for_expression(xp, arg, cmdidx)
3001 expand_T *xp;
3002 char_u *arg;
3003 cmdidx_T cmdidx;
3004{
3005 int got_eq = FALSE;
3006 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003007 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003009 if (cmdidx == CMD_let)
3010 {
3011 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003012 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003013 {
3014 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003015 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003016 {
3017 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003018 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003019 if (vim_iswhite(*p))
3020 break;
3021 }
3022 return;
3023 }
3024 }
3025 else
3026 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3027 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 while ((xp->xp_pattern = vim_strpbrk(arg,
3029 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3030 {
3031 c = *xp->xp_pattern;
3032 if (c == '&')
3033 {
3034 c = xp->xp_pattern[1];
3035 if (c == '&')
3036 {
3037 ++xp->xp_pattern;
3038 xp->xp_context = cmdidx != CMD_let || got_eq
3039 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3040 }
3041 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003042 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003044 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3045 xp->xp_pattern += 2;
3046
3047 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 }
3049 else if (c == '$')
3050 {
3051 /* environment variable */
3052 xp->xp_context = EXPAND_ENV_VARS;
3053 }
3054 else if (c == '=')
3055 {
3056 got_eq = TRUE;
3057 xp->xp_context = EXPAND_EXPRESSION;
3058 }
3059 else if (c == '<'
3060 && xp->xp_context == EXPAND_FUNCTIONS
3061 && vim_strchr(xp->xp_pattern, '(') == NULL)
3062 {
3063 /* Function name can start with "<SNR>" */
3064 break;
3065 }
3066 else if (cmdidx != CMD_let || got_eq)
3067 {
3068 if (c == '"') /* string */
3069 {
3070 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3071 if (c == '\\' && xp->xp_pattern[1] != NUL)
3072 ++xp->xp_pattern;
3073 xp->xp_context = EXPAND_NOTHING;
3074 }
3075 else if (c == '\'') /* literal string */
3076 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003077 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3079 /* skip */ ;
3080 xp->xp_context = EXPAND_NOTHING;
3081 }
3082 else if (c == '|')
3083 {
3084 if (xp->xp_pattern[1] == '|')
3085 {
3086 ++xp->xp_pattern;
3087 xp->xp_context = EXPAND_EXPRESSION;
3088 }
3089 else
3090 xp->xp_context = EXPAND_COMMANDS;
3091 }
3092 else
3093 xp->xp_context = EXPAND_EXPRESSION;
3094 }
3095 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003096 /* Doesn't look like something valid, expand as an expression
3097 * anyway. */
3098 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 arg = xp->xp_pattern;
3100 if (*arg != NUL)
3101 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3102 /* skip */ ;
3103 }
3104 xp->xp_pattern = arg;
3105}
3106
3107#endif /* FEAT_CMDL_COMPL */
3108
3109/*
3110 * ":1,25call func(arg1, arg2)" function call.
3111 */
3112 void
3113ex_call(eap)
3114 exarg_T *eap;
3115{
3116 char_u *arg = eap->arg;
3117 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003119 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003121 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 linenr_T lnum;
3123 int doesrange;
3124 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003125 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003127 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003128 if (fudi.fd_newkey != NULL)
3129 {
3130 /* Still need to give an error message for missing key. */
3131 EMSG2(_(e_dictkey), fudi.fd_newkey);
3132 vim_free(fudi.fd_newkey);
3133 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003134 if (tofree == NULL)
3135 return;
3136
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003137 /* Increase refcount on dictionary, it could get deleted when evaluating
3138 * the arguments. */
3139 if (fudi.fd_dict != NULL)
3140 ++fudi.fd_dict->dv_refcount;
3141
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003142 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003143 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003144 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145
Bram Moolenaar532c7802005-01-27 14:44:31 +00003146 /* Skip white space to allow ":call func ()". Not good, but required for
3147 * backward compatibility. */
3148 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003149 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150
3151 if (*startarg != '(')
3152 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003153 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 goto end;
3155 }
3156
3157 /*
3158 * When skipping, evaluate the function once, to find the end of the
3159 * arguments.
3160 * When the function takes a range, this is discovered after the first
3161 * call, and the loop is broken.
3162 */
3163 if (eap->skip)
3164 {
3165 ++emsg_skip;
3166 lnum = eap->line2; /* do it once, also with an invalid range */
3167 }
3168 else
3169 lnum = eap->line1;
3170 for ( ; lnum <= eap->line2; ++lnum)
3171 {
3172 if (!eap->skip && eap->addr_count > 0)
3173 {
3174 curwin->w_cursor.lnum = lnum;
3175 curwin->w_cursor.col = 0;
3176 }
3177 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003178 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003179 eap->line1, eap->line2, &doesrange,
3180 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 {
3182 failed = TRUE;
3183 break;
3184 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003185 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 if (doesrange || eap->skip)
3187 break;
3188 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003189 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003190 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003191 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 if (aborting())
3193 break;
3194 }
3195 if (eap->skip)
3196 --emsg_skip;
3197
3198 if (!failed)
3199 {
3200 /* Check for trailing illegal characters and a following command. */
3201 if (!ends_excmd(*arg))
3202 {
3203 emsg_severe = TRUE;
3204 EMSG(_(e_trailing));
3205 }
3206 else
3207 eap->nextcmd = check_nextcmd(arg);
3208 }
3209
3210end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003211 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003212 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213}
3214
3215/*
3216 * ":unlet[!] var1 ... " command.
3217 */
3218 void
3219ex_unlet(eap)
3220 exarg_T *eap;
3221{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003222 ex_unletlock(eap, eap->arg, 0);
3223}
3224
3225/*
3226 * ":lockvar" and ":unlockvar" commands
3227 */
3228 void
3229ex_lockvar(eap)
3230 exarg_T *eap;
3231{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003233 int deep = 2;
3234
3235 if (eap->forceit)
3236 deep = -1;
3237 else if (vim_isdigit(*arg))
3238 {
3239 deep = getdigits(&arg);
3240 arg = skipwhite(arg);
3241 }
3242
3243 ex_unletlock(eap, arg, deep);
3244}
3245
3246/*
3247 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3248 */
3249 static void
3250ex_unletlock(eap, argstart, deep)
3251 exarg_T *eap;
3252 char_u *argstart;
3253 int deep;
3254{
3255 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003258 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259
3260 do
3261 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003262 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003263 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3264 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003265 if (lv.ll_name == NULL)
3266 error = TRUE; /* error but continue parsing */
3267 if (name_end == NULL || (!vim_iswhite(*name_end)
3268 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003270 if (name_end != NULL)
3271 {
3272 emsg_severe = TRUE;
3273 EMSG(_(e_trailing));
3274 }
3275 if (!(eap->skip || error))
3276 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 break;
3278 }
3279
3280 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003281 {
3282 if (eap->cmdidx == CMD_unlet)
3283 {
3284 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3285 error = TRUE;
3286 }
3287 else
3288 {
3289 if (do_lock_var(&lv, name_end, deep,
3290 eap->cmdidx == CMD_lockvar) == FAIL)
3291 error = TRUE;
3292 }
3293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003295 if (!eap->skip)
3296 clear_lval(&lv);
3297
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 arg = skipwhite(name_end);
3299 } while (!ends_excmd(*arg));
3300
3301 eap->nextcmd = check_nextcmd(arg);
3302}
3303
Bram Moolenaar8c711452005-01-14 21:53:12 +00003304 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003305do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003306 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003307 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003308 int forceit;
3309{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003310 int ret = OK;
3311 int cc;
3312
3313 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003314 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003315 cc = *name_end;
3316 *name_end = NUL;
3317
3318 /* Normal name or expanded name. */
3319 if (check_changedtick(lp->ll_name))
3320 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003321 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003322 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003323 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003324 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003325 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3326 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003327 else if (lp->ll_range)
3328 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003329 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003330
3331 /* Delete a range of List items. */
3332 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3333 {
3334 li = lp->ll_li->li_next;
3335 listitem_remove(lp->ll_list, lp->ll_li);
3336 lp->ll_li = li;
3337 ++lp->ll_n1;
3338 }
3339 }
3340 else
3341 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003342 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003343 /* unlet a List item. */
3344 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003345 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003346 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003347 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003348 }
3349
3350 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003351}
3352
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353/*
3354 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003355 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 */
3357 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003358do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003360 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361{
Bram Moolenaar33570922005-01-25 22:26:29 +00003362 hashtab_T *ht;
3363 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003364 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365
Bram Moolenaar33570922005-01-25 22:26:29 +00003366 ht = find_var_ht(name, &varname);
3367 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003369 hi = hash_find(ht, varname);
3370 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003371 {
Bram Moolenaar4e957af2006-09-02 11:41:07 +00003372 if (var_check_fixed(HI2DI(hi)->di_flags, name))
3373 return FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003374 if (var_check_ro(HI2DI(hi)->di_flags, name))
3375 return FAIL;
3376 delete_var(ht, hi);
3377 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003380 if (forceit)
3381 return OK;
3382 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 return FAIL;
3384}
3385
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003386/*
3387 * Lock or unlock variable indicated by "lp".
3388 * "deep" is the levels to go (-1 for unlimited);
3389 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3390 */
3391 static int
3392do_lock_var(lp, name_end, deep, lock)
3393 lval_T *lp;
3394 char_u *name_end;
3395 int deep;
3396 int lock;
3397{
3398 int ret = OK;
3399 int cc;
3400 dictitem_T *di;
3401
3402 if (deep == 0) /* nothing to do */
3403 return OK;
3404
3405 if (lp->ll_tv == NULL)
3406 {
3407 cc = *name_end;
3408 *name_end = NUL;
3409
3410 /* Normal name or expanded name. */
3411 if (check_changedtick(lp->ll_name))
3412 ret = FAIL;
3413 else
3414 {
3415 di = find_var(lp->ll_name, NULL);
3416 if (di == NULL)
3417 ret = FAIL;
3418 else
3419 {
3420 if (lock)
3421 di->di_flags |= DI_FLAGS_LOCK;
3422 else
3423 di->di_flags &= ~DI_FLAGS_LOCK;
3424 item_lock(&di->di_tv, deep, lock);
3425 }
3426 }
3427 *name_end = cc;
3428 }
3429 else if (lp->ll_range)
3430 {
3431 listitem_T *li = lp->ll_li;
3432
3433 /* (un)lock a range of List items. */
3434 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3435 {
3436 item_lock(&li->li_tv, deep, lock);
3437 li = li->li_next;
3438 ++lp->ll_n1;
3439 }
3440 }
3441 else if (lp->ll_list != NULL)
3442 /* (un)lock a List item. */
3443 item_lock(&lp->ll_li->li_tv, deep, lock);
3444 else
3445 /* un(lock) a Dictionary item. */
3446 item_lock(&lp->ll_di->di_tv, deep, lock);
3447
3448 return ret;
3449}
3450
3451/*
3452 * Lock or unlock an item. "deep" is nr of levels to go.
3453 */
3454 static void
3455item_lock(tv, deep, lock)
3456 typval_T *tv;
3457 int deep;
3458 int lock;
3459{
3460 static int recurse = 0;
3461 list_T *l;
3462 listitem_T *li;
3463 dict_T *d;
3464 hashitem_T *hi;
3465 int todo;
3466
3467 if (recurse >= DICT_MAXNEST)
3468 {
3469 EMSG(_("E743: variable nested too deep for (un)lock"));
3470 return;
3471 }
3472 if (deep == 0)
3473 return;
3474 ++recurse;
3475
3476 /* lock/unlock the item itself */
3477 if (lock)
3478 tv->v_lock |= VAR_LOCKED;
3479 else
3480 tv->v_lock &= ~VAR_LOCKED;
3481
3482 switch (tv->v_type)
3483 {
3484 case VAR_LIST:
3485 if ((l = tv->vval.v_list) != NULL)
3486 {
3487 if (lock)
3488 l->lv_lock |= VAR_LOCKED;
3489 else
3490 l->lv_lock &= ~VAR_LOCKED;
3491 if (deep < 0 || deep > 1)
3492 /* recursive: lock/unlock the items the List contains */
3493 for (li = l->lv_first; li != NULL; li = li->li_next)
3494 item_lock(&li->li_tv, deep - 1, lock);
3495 }
3496 break;
3497 case VAR_DICT:
3498 if ((d = tv->vval.v_dict) != NULL)
3499 {
3500 if (lock)
3501 d->dv_lock |= VAR_LOCKED;
3502 else
3503 d->dv_lock &= ~VAR_LOCKED;
3504 if (deep < 0 || deep > 1)
3505 {
3506 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003507 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003508 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3509 {
3510 if (!HASHITEM_EMPTY(hi))
3511 {
3512 --todo;
3513 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3514 }
3515 }
3516 }
3517 }
3518 }
3519 --recurse;
3520}
3521
Bram Moolenaara40058a2005-07-11 22:42:07 +00003522/*
3523 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3524 * it refers to a List or Dictionary that is locked.
3525 */
3526 static int
3527tv_islocked(tv)
3528 typval_T *tv;
3529{
3530 return (tv->v_lock & VAR_LOCKED)
3531 || (tv->v_type == VAR_LIST
3532 && tv->vval.v_list != NULL
3533 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3534 || (tv->v_type == VAR_DICT
3535 && tv->vval.v_dict != NULL
3536 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3537}
3538
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3540/*
3541 * Delete all "menutrans_" variables.
3542 */
3543 void
3544del_menutrans_vars()
3545{
Bram Moolenaar33570922005-01-25 22:26:29 +00003546 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003547 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548
Bram Moolenaar33570922005-01-25 22:26:29 +00003549 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003550 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003551 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003552 {
3553 if (!HASHITEM_EMPTY(hi))
3554 {
3555 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003556 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3557 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003558 }
3559 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003560 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561}
3562#endif
3563
3564#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3565
3566/*
3567 * Local string buffer for the next two functions to store a variable name
3568 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3569 * get_user_var_name().
3570 */
3571
3572static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3573
3574static char_u *varnamebuf = NULL;
3575static int varnamebuflen = 0;
3576
3577/*
3578 * Function to concatenate a prefix and a variable name.
3579 */
3580 static char_u *
3581cat_prefix_varname(prefix, name)
3582 int prefix;
3583 char_u *name;
3584{
3585 int len;
3586
3587 len = (int)STRLEN(name) + 3;
3588 if (len > varnamebuflen)
3589 {
3590 vim_free(varnamebuf);
3591 len += 10; /* some additional space */
3592 varnamebuf = alloc(len);
3593 if (varnamebuf == NULL)
3594 {
3595 varnamebuflen = 0;
3596 return NULL;
3597 }
3598 varnamebuflen = len;
3599 }
3600 *varnamebuf = prefix;
3601 varnamebuf[1] = ':';
3602 STRCPY(varnamebuf + 2, name);
3603 return varnamebuf;
3604}
3605
3606/*
3607 * Function given to ExpandGeneric() to obtain the list of user defined
3608 * (global/buffer/window/built-in) variable names.
3609 */
3610/*ARGSUSED*/
3611 char_u *
3612get_user_var_name(xp, idx)
3613 expand_T *xp;
3614 int idx;
3615{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003616 static long_u gdone;
3617 static long_u bdone;
3618 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003619#ifdef FEAT_WINDOWS
3620 static long_u tdone;
3621#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003622 static int vidx;
3623 static hashitem_T *hi;
3624 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625
3626 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003627 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003628 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003629#ifdef FEAT_WINDOWS
3630 tdone = 0;
3631#endif
3632 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003633
3634 /* Global variables */
3635 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003637 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003638 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003639 else
3640 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003641 while (HASHITEM_EMPTY(hi))
3642 ++hi;
3643 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3644 return cat_prefix_varname('g', hi->hi_key);
3645 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003647
3648 /* b: variables */
3649 ht = &curbuf->b_vars.dv_hashtab;
3650 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003652 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003653 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003654 else
3655 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003656 while (HASHITEM_EMPTY(hi))
3657 ++hi;
3658 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003660 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003662 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 return (char_u *)"b:changedtick";
3664 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003665
3666 /* w: variables */
3667 ht = &curwin->w_vars.dv_hashtab;
3668 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003670 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003671 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003672 else
3673 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003674 while (HASHITEM_EMPTY(hi))
3675 ++hi;
3676 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003678
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003679#ifdef FEAT_WINDOWS
3680 /* t: variables */
3681 ht = &curtab->tp_vars.dv_hashtab;
3682 if (tdone < ht->ht_used)
3683 {
3684 if (tdone++ == 0)
3685 hi = ht->ht_array;
3686 else
3687 ++hi;
3688 while (HASHITEM_EMPTY(hi))
3689 ++hi;
3690 return cat_prefix_varname('t', hi->hi_key);
3691 }
3692#endif
3693
Bram Moolenaar33570922005-01-25 22:26:29 +00003694 /* v: variables */
3695 if (vidx < VV_LEN)
3696 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697
3698 vim_free(varnamebuf);
3699 varnamebuf = NULL;
3700 varnamebuflen = 0;
3701 return NULL;
3702}
3703
3704#endif /* FEAT_CMDL_COMPL */
3705
3706/*
3707 * types for expressions.
3708 */
3709typedef enum
3710{
3711 TYPE_UNKNOWN = 0
3712 , TYPE_EQUAL /* == */
3713 , TYPE_NEQUAL /* != */
3714 , TYPE_GREATER /* > */
3715 , TYPE_GEQUAL /* >= */
3716 , TYPE_SMALLER /* < */
3717 , TYPE_SEQUAL /* <= */
3718 , TYPE_MATCH /* =~ */
3719 , TYPE_NOMATCH /* !~ */
3720} exptype_T;
3721
3722/*
3723 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003724 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3726 */
3727
3728/*
3729 * Handle zero level expression.
3730 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003731 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003732 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 * Return OK or FAIL.
3734 */
3735 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003736eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003738 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 char_u **nextcmd;
3740 int evaluate;
3741{
3742 int ret;
3743 char_u *p;
3744
3745 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003746 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 if (ret == FAIL || !ends_excmd(*p))
3748 {
3749 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003750 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 /*
3752 * Report the invalid expression unless the expression evaluation has
3753 * been cancelled due to an aborting error, an interrupt, or an
3754 * exception.
3755 */
3756 if (!aborting())
3757 EMSG2(_(e_invexpr2), arg);
3758 ret = FAIL;
3759 }
3760 if (nextcmd != NULL)
3761 *nextcmd = check_nextcmd(p);
3762
3763 return ret;
3764}
3765
3766/*
3767 * Handle top level expression:
3768 * expr1 ? expr0 : expr0
3769 *
3770 * "arg" must point to the first non-white of the expression.
3771 * "arg" is advanced to the next non-white after the recognized expression.
3772 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003773 * Note: "rettv.v_lock" is not set.
3774 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 * Return OK or FAIL.
3776 */
3777 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003778eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003780 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 int evaluate;
3782{
3783 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003784 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785
3786 /*
3787 * Get the first variable.
3788 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003789 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 return FAIL;
3791
3792 if ((*arg)[0] == '?')
3793 {
3794 result = FALSE;
3795 if (evaluate)
3796 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003797 int error = FALSE;
3798
3799 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003801 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003802 if (error)
3803 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 }
3805
3806 /*
3807 * Get the second variable.
3808 */
3809 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003810 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 return FAIL;
3812
3813 /*
3814 * Check for the ":".
3815 */
3816 if ((*arg)[0] != ':')
3817 {
3818 EMSG(_("E109: Missing ':' after '?'"));
3819 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003820 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 return FAIL;
3822 }
3823
3824 /*
3825 * Get the third variable.
3826 */
3827 *arg = skipwhite(*arg + 1);
3828 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3829 {
3830 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003831 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 return FAIL;
3833 }
3834 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003835 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 }
3837
3838 return OK;
3839}
3840
3841/*
3842 * Handle first level expression:
3843 * expr2 || expr2 || expr2 logical OR
3844 *
3845 * "arg" must point to the first non-white of the expression.
3846 * "arg" is advanced to the next non-white after the recognized expression.
3847 *
3848 * Return OK or FAIL.
3849 */
3850 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003851eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003853 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 int evaluate;
3855{
Bram Moolenaar33570922005-01-25 22:26:29 +00003856 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 long result;
3858 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003859 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860
3861 /*
3862 * Get the first variable.
3863 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003864 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 return FAIL;
3866
3867 /*
3868 * Repeat until there is no following "||".
3869 */
3870 first = TRUE;
3871 result = FALSE;
3872 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3873 {
3874 if (evaluate && first)
3875 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003876 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003878 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003879 if (error)
3880 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 first = FALSE;
3882 }
3883
3884 /*
3885 * Get the second variable.
3886 */
3887 *arg = skipwhite(*arg + 2);
3888 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3889 return FAIL;
3890
3891 /*
3892 * Compute the result.
3893 */
3894 if (evaluate && !result)
3895 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003896 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003898 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003899 if (error)
3900 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 }
3902 if (evaluate)
3903 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003904 rettv->v_type = VAR_NUMBER;
3905 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 }
3907 }
3908
3909 return OK;
3910}
3911
3912/*
3913 * Handle second level expression:
3914 * expr3 && expr3 && expr3 logical AND
3915 *
3916 * "arg" must point to the first non-white of the expression.
3917 * "arg" is advanced to the next non-white after the recognized expression.
3918 *
3919 * Return OK or FAIL.
3920 */
3921 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003922eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003924 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 int evaluate;
3926{
Bram Moolenaar33570922005-01-25 22:26:29 +00003927 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 long result;
3929 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003930 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931
3932 /*
3933 * Get the first variable.
3934 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003935 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 return FAIL;
3937
3938 /*
3939 * Repeat until there is no following "&&".
3940 */
3941 first = TRUE;
3942 result = TRUE;
3943 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3944 {
3945 if (evaluate && first)
3946 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003947 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003949 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003950 if (error)
3951 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 first = FALSE;
3953 }
3954
3955 /*
3956 * Get the second variable.
3957 */
3958 *arg = skipwhite(*arg + 2);
3959 if (eval4(arg, &var2, evaluate && result) == FAIL)
3960 return FAIL;
3961
3962 /*
3963 * Compute the result.
3964 */
3965 if (evaluate && result)
3966 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003967 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003969 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003970 if (error)
3971 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 }
3973 if (evaluate)
3974 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003975 rettv->v_type = VAR_NUMBER;
3976 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 }
3978 }
3979
3980 return OK;
3981}
3982
3983/*
3984 * Handle third level expression:
3985 * var1 == var2
3986 * var1 =~ var2
3987 * var1 != var2
3988 * var1 !~ var2
3989 * var1 > var2
3990 * var1 >= var2
3991 * var1 < var2
3992 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003993 * var1 is var2
3994 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 *
3996 * "arg" must point to the first non-white of the expression.
3997 * "arg" is advanced to the next non-white after the recognized expression.
3998 *
3999 * Return OK or FAIL.
4000 */
4001 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004002eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004004 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 int evaluate;
4006{
Bram Moolenaar33570922005-01-25 22:26:29 +00004007 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 char_u *p;
4009 int i;
4010 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004011 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 int len = 2;
4013 long n1, n2;
4014 char_u *s1, *s2;
4015 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4016 regmatch_T regmatch;
4017 int ic;
4018 char_u *save_cpo;
4019
4020 /*
4021 * Get the first variable.
4022 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004023 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 return FAIL;
4025
4026 p = *arg;
4027 switch (p[0])
4028 {
4029 case '=': if (p[1] == '=')
4030 type = TYPE_EQUAL;
4031 else if (p[1] == '~')
4032 type = TYPE_MATCH;
4033 break;
4034 case '!': if (p[1] == '=')
4035 type = TYPE_NEQUAL;
4036 else if (p[1] == '~')
4037 type = TYPE_NOMATCH;
4038 break;
4039 case '>': if (p[1] != '=')
4040 {
4041 type = TYPE_GREATER;
4042 len = 1;
4043 }
4044 else
4045 type = TYPE_GEQUAL;
4046 break;
4047 case '<': if (p[1] != '=')
4048 {
4049 type = TYPE_SMALLER;
4050 len = 1;
4051 }
4052 else
4053 type = TYPE_SEQUAL;
4054 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004055 case 'i': if (p[1] == 's')
4056 {
4057 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4058 len = 5;
4059 if (!vim_isIDc(p[len]))
4060 {
4061 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4062 type_is = TRUE;
4063 }
4064 }
4065 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 }
4067
4068 /*
4069 * If there is a comparitive operator, use it.
4070 */
4071 if (type != TYPE_UNKNOWN)
4072 {
4073 /* extra question mark appended: ignore case */
4074 if (p[len] == '?')
4075 {
4076 ic = TRUE;
4077 ++len;
4078 }
4079 /* extra '#' appended: match case */
4080 else if (p[len] == '#')
4081 {
4082 ic = FALSE;
4083 ++len;
4084 }
4085 /* nothing appened: use 'ignorecase' */
4086 else
4087 ic = p_ic;
4088
4089 /*
4090 * Get the second variable.
4091 */
4092 *arg = skipwhite(p + len);
4093 if (eval5(arg, &var2, evaluate) == FAIL)
4094 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004095 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 return FAIL;
4097 }
4098
4099 if (evaluate)
4100 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004101 if (type_is && rettv->v_type != var2.v_type)
4102 {
4103 /* For "is" a different type always means FALSE, for "notis"
4104 * it means TRUE. */
4105 n1 = (type == TYPE_NEQUAL);
4106 }
4107 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4108 {
4109 if (type_is)
4110 {
4111 n1 = (rettv->v_type == var2.v_type
4112 && rettv->vval.v_list == var2.vval.v_list);
4113 if (type == TYPE_NEQUAL)
4114 n1 = !n1;
4115 }
4116 else if (rettv->v_type != var2.v_type
4117 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4118 {
4119 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004120 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004121 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004122 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004123 clear_tv(rettv);
4124 clear_tv(&var2);
4125 return FAIL;
4126 }
4127 else
4128 {
4129 /* Compare two Lists for being equal or unequal. */
4130 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4131 if (type == TYPE_NEQUAL)
4132 n1 = !n1;
4133 }
4134 }
4135
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004136 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4137 {
4138 if (type_is)
4139 {
4140 n1 = (rettv->v_type == var2.v_type
4141 && rettv->vval.v_dict == var2.vval.v_dict);
4142 if (type == TYPE_NEQUAL)
4143 n1 = !n1;
4144 }
4145 else if (rettv->v_type != var2.v_type
4146 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4147 {
4148 if (rettv->v_type != var2.v_type)
4149 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4150 else
4151 EMSG(_("E736: Invalid operation for Dictionary"));
4152 clear_tv(rettv);
4153 clear_tv(&var2);
4154 return FAIL;
4155 }
4156 else
4157 {
4158 /* Compare two Dictionaries for being equal or unequal. */
4159 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4160 if (type == TYPE_NEQUAL)
4161 n1 = !n1;
4162 }
4163 }
4164
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004165 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4166 {
4167 if (rettv->v_type != var2.v_type
4168 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4169 {
4170 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004171 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004172 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004173 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004174 clear_tv(rettv);
4175 clear_tv(&var2);
4176 return FAIL;
4177 }
4178 else
4179 {
4180 /* Compare two Funcrefs for being equal or unequal. */
4181 if (rettv->vval.v_string == NULL
4182 || var2.vval.v_string == NULL)
4183 n1 = FALSE;
4184 else
4185 n1 = STRCMP(rettv->vval.v_string,
4186 var2.vval.v_string) == 0;
4187 if (type == TYPE_NEQUAL)
4188 n1 = !n1;
4189 }
4190 }
4191
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 /*
4193 * If one of the two variables is a number, compare as a number.
4194 * When using "=~" or "!~", always compare as string.
4195 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004196 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4198 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004199 n1 = get_tv_number(rettv);
4200 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 switch (type)
4202 {
4203 case TYPE_EQUAL: n1 = (n1 == n2); break;
4204 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4205 case TYPE_GREATER: n1 = (n1 > n2); break;
4206 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4207 case TYPE_SMALLER: n1 = (n1 < n2); break;
4208 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4209 case TYPE_UNKNOWN:
4210 case TYPE_MATCH:
4211 case TYPE_NOMATCH: break; /* avoid gcc warning */
4212 }
4213 }
4214 else
4215 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004216 s1 = get_tv_string_buf(rettv, buf1);
4217 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4219 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4220 else
4221 i = 0;
4222 n1 = FALSE;
4223 switch (type)
4224 {
4225 case TYPE_EQUAL: n1 = (i == 0); break;
4226 case TYPE_NEQUAL: n1 = (i != 0); break;
4227 case TYPE_GREATER: n1 = (i > 0); break;
4228 case TYPE_GEQUAL: n1 = (i >= 0); break;
4229 case TYPE_SMALLER: n1 = (i < 0); break;
4230 case TYPE_SEQUAL: n1 = (i <= 0); break;
4231
4232 case TYPE_MATCH:
4233 case TYPE_NOMATCH:
4234 /* avoid 'l' flag in 'cpoptions' */
4235 save_cpo = p_cpo;
4236 p_cpo = (char_u *)"";
4237 regmatch.regprog = vim_regcomp(s2,
4238 RE_MAGIC + RE_STRING);
4239 regmatch.rm_ic = ic;
4240 if (regmatch.regprog != NULL)
4241 {
4242 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4243 vim_free(regmatch.regprog);
4244 if (type == TYPE_NOMATCH)
4245 n1 = !n1;
4246 }
4247 p_cpo = save_cpo;
4248 break;
4249
4250 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4251 }
4252 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004253 clear_tv(rettv);
4254 clear_tv(&var2);
4255 rettv->v_type = VAR_NUMBER;
4256 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 }
4258 }
4259
4260 return OK;
4261}
4262
4263/*
4264 * Handle fourth level expression:
4265 * + number addition
4266 * - number subtraction
4267 * . string concatenation
4268 *
4269 * "arg" must point to the first non-white of the expression.
4270 * "arg" is advanced to the next non-white after the recognized expression.
4271 *
4272 * Return OK or FAIL.
4273 */
4274 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004275eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004277 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 int evaluate;
4279{
Bram Moolenaar33570922005-01-25 22:26:29 +00004280 typval_T var2;
4281 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 int op;
4283 long n1, n2;
4284 char_u *s1, *s2;
4285 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4286 char_u *p;
4287
4288 /*
4289 * Get the first variable.
4290 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004291 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 return FAIL;
4293
4294 /*
4295 * Repeat computing, until no '+', '-' or '.' is following.
4296 */
4297 for (;;)
4298 {
4299 op = **arg;
4300 if (op != '+' && op != '-' && op != '.')
4301 break;
4302
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004303 if (op != '+' || rettv->v_type != VAR_LIST)
4304 {
4305 /* For "list + ...", an illegal use of the first operand as
4306 * a number cannot be determined before evaluating the 2nd
4307 * operand: if this is also a list, all is ok.
4308 * For "something . ...", "something - ..." or "non-list + ...",
4309 * we know that the first operand needs to be a string or number
4310 * without evaluating the 2nd operand. So check before to avoid
4311 * side effects after an error. */
4312 if (evaluate && get_tv_string_chk(rettv) == NULL)
4313 {
4314 clear_tv(rettv);
4315 return FAIL;
4316 }
4317 }
4318
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 /*
4320 * Get the second variable.
4321 */
4322 *arg = skipwhite(*arg + 1);
4323 if (eval6(arg, &var2, evaluate) == FAIL)
4324 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004325 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 return FAIL;
4327 }
4328
4329 if (evaluate)
4330 {
4331 /*
4332 * Compute the result.
4333 */
4334 if (op == '.')
4335 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004336 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4337 s2 = get_tv_string_buf_chk(&var2, buf2);
4338 if (s2 == NULL) /* type error ? */
4339 {
4340 clear_tv(rettv);
4341 clear_tv(&var2);
4342 return FAIL;
4343 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004344 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004345 clear_tv(rettv);
4346 rettv->v_type = VAR_STRING;
4347 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004349 else if (op == '+' && rettv->v_type == VAR_LIST
4350 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004351 {
4352 /* concatenate Lists */
4353 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4354 &var3) == FAIL)
4355 {
4356 clear_tv(rettv);
4357 clear_tv(&var2);
4358 return FAIL;
4359 }
4360 clear_tv(rettv);
4361 *rettv = var3;
4362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 else
4364 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004365 int error = FALSE;
4366
4367 n1 = get_tv_number_chk(rettv, &error);
4368 if (error)
4369 {
4370 /* This can only happen for "list + non-list".
4371 * For "non-list + ..." or "something - ...", we returned
4372 * before evaluating the 2nd operand. */
4373 clear_tv(rettv);
4374 return FAIL;
4375 }
4376 n2 = get_tv_number_chk(&var2, &error);
4377 if (error)
4378 {
4379 clear_tv(rettv);
4380 clear_tv(&var2);
4381 return FAIL;
4382 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004383 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 if (op == '+')
4385 n1 = n1 + n2;
4386 else
4387 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004388 rettv->v_type = VAR_NUMBER;
4389 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004391 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 }
4393 }
4394 return OK;
4395}
4396
4397/*
4398 * Handle fifth level expression:
4399 * * number multiplication
4400 * / number division
4401 * % number modulo
4402 *
4403 * "arg" must point to the first non-white of the expression.
4404 * "arg" is advanced to the next non-white after the recognized expression.
4405 *
4406 * Return OK or FAIL.
4407 */
4408 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004409eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004411 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 int evaluate;
4413{
Bram Moolenaar33570922005-01-25 22:26:29 +00004414 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 int op;
4416 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004417 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418
4419 /*
4420 * Get the first variable.
4421 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004422 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 return FAIL;
4424
4425 /*
4426 * Repeat computing, until no '*', '/' or '%' is following.
4427 */
4428 for (;;)
4429 {
4430 op = **arg;
4431 if (op != '*' && op != '/' && op != '%')
4432 break;
4433
4434 if (evaluate)
4435 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004436 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004437 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004438 if (error)
4439 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 }
4441 else
4442 n1 = 0;
4443
4444 /*
4445 * Get the second variable.
4446 */
4447 *arg = skipwhite(*arg + 1);
4448 if (eval7(arg, &var2, evaluate) == FAIL)
4449 return FAIL;
4450
4451 if (evaluate)
4452 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004453 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004454 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004455 if (error)
4456 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457
4458 /*
4459 * Compute the result.
4460 */
4461 if (op == '*')
4462 n1 = n1 * n2;
4463 else if (op == '/')
4464 {
4465 if (n2 == 0) /* give an error message? */
4466 n1 = 0x7fffffffL;
4467 else
4468 n1 = n1 / n2;
4469 }
4470 else
4471 {
4472 if (n2 == 0) /* give an error message? */
4473 n1 = 0;
4474 else
4475 n1 = n1 % n2;
4476 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004477 rettv->v_type = VAR_NUMBER;
4478 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 }
4480 }
4481
4482 return OK;
4483}
4484
4485/*
4486 * Handle sixth level expression:
4487 * number number constant
4488 * "string" string contstant
4489 * 'string' literal string contstant
4490 * &option-name option value
4491 * @r register contents
4492 * identifier variable value
4493 * function() function call
4494 * $VAR environment variable
4495 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004496 * [expr, expr] List
4497 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 *
4499 * Also handle:
4500 * ! in front logical NOT
4501 * - in front unary minus
4502 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004503 * trailing [] subscript in String or List
4504 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 *
4506 * "arg" must point to the first non-white of the expression.
4507 * "arg" is advanced to the next non-white after the recognized expression.
4508 *
4509 * Return OK or FAIL.
4510 */
4511 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004512eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004514 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 int evaluate;
4516{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 long n;
4518 int len;
4519 char_u *s;
4520 int val;
4521 char_u *start_leader, *end_leader;
4522 int ret = OK;
4523 char_u *alias;
4524
4525 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004526 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004527 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004529 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530
4531 /*
4532 * Skip '!' and '-' characters. They are handled later.
4533 */
4534 start_leader = *arg;
4535 while (**arg == '!' || **arg == '-' || **arg == '+')
4536 *arg = skipwhite(*arg + 1);
4537 end_leader = *arg;
4538
4539 switch (**arg)
4540 {
4541 /*
4542 * Number constant.
4543 */
4544 case '0':
4545 case '1':
4546 case '2':
4547 case '3':
4548 case '4':
4549 case '5':
4550 case '6':
4551 case '7':
4552 case '8':
4553 case '9':
4554 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4555 *arg += len;
4556 if (evaluate)
4557 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004558 rettv->v_type = VAR_NUMBER;
4559 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 }
4561 break;
4562
4563 /*
4564 * String constant: "string".
4565 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004566 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 break;
4568
4569 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004570 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004572 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004573 break;
4574
4575 /*
4576 * List: [expr, expr]
4577 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004578 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 break;
4580
4581 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004582 * Dictionary: {key: val, key: val}
4583 */
4584 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4585 break;
4586
4587 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004588 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004590 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 break;
4592
4593 /*
4594 * Environment variable: $VAR.
4595 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004596 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 break;
4598
4599 /*
4600 * Register contents: @r.
4601 */
4602 case '@': ++*arg;
4603 if (evaluate)
4604 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004605 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004606 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 }
4608 if (**arg != NUL)
4609 ++*arg;
4610 break;
4611
4612 /*
4613 * nested expression: (expression).
4614 */
4615 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004616 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 if (**arg == ')')
4618 ++*arg;
4619 else if (ret == OK)
4620 {
4621 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004622 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 ret = FAIL;
4624 }
4625 break;
4626
Bram Moolenaar8c711452005-01-14 21:53:12 +00004627 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 break;
4629 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004630
4631 if (ret == NOTDONE)
4632 {
4633 /*
4634 * Must be a variable or function name.
4635 * Can also be a curly-braces kind of name: {expr}.
4636 */
4637 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004638 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004639 if (alias != NULL)
4640 s = alias;
4641
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004642 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004643 ret = FAIL;
4644 else
4645 {
4646 if (**arg == '(') /* recursive! */
4647 {
4648 /* If "s" is the name of a variable of type VAR_FUNC
4649 * use its contents. */
4650 s = deref_func_name(s, &len);
4651
4652 /* Invoke the function. */
4653 ret = get_func_tv(s, len, rettv, arg,
4654 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004655 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004656 /* Stop the expression evaluation when immediately
4657 * aborting on error, or when an interrupt occurred or
4658 * an exception was thrown but not caught. */
4659 if (aborting())
4660 {
4661 if (ret == OK)
4662 clear_tv(rettv);
4663 ret = FAIL;
4664 }
4665 }
4666 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004667 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004668 else
4669 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004670 }
4671
4672 if (alias != NULL)
4673 vim_free(alias);
4674 }
4675
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 *arg = skipwhite(*arg);
4677
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004678 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4679 * expr(expr). */
4680 if (ret == OK)
4681 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682
4683 /*
4684 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4685 */
4686 if (ret == OK && evaluate && end_leader > start_leader)
4687 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004688 int error = FALSE;
4689
4690 val = get_tv_number_chk(rettv, &error);
4691 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004693 clear_tv(rettv);
4694 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004696 else
4697 {
4698 while (end_leader > start_leader)
4699 {
4700 --end_leader;
4701 if (*end_leader == '!')
4702 val = !val;
4703 else if (*end_leader == '-')
4704 val = -val;
4705 }
4706 clear_tv(rettv);
4707 rettv->v_type = VAR_NUMBER;
4708 rettv->vval.v_number = val;
4709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 }
4711
4712 return ret;
4713}
4714
4715/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004716 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4717 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004718 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4719 */
4720 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004721eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004722 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004723 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004724 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004725 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004726{
4727 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004728 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004729 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004730 long len = -1;
4731 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004732 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004733 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004734
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004735 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004736 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004737 if (verbose)
4738 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004739 return FAIL;
4740 }
4741
Bram Moolenaar8c711452005-01-14 21:53:12 +00004742 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004743 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004744 /*
4745 * dict.name
4746 */
4747 key = *arg + 1;
4748 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4749 ;
4750 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004751 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004752 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004753 }
4754 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004755 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004756 /*
4757 * something[idx]
4758 *
4759 * Get the (first) variable from inside the [].
4760 */
4761 *arg = skipwhite(*arg + 1);
4762 if (**arg == ':')
4763 empty1 = TRUE;
4764 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4765 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004766 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4767 {
4768 /* not a number or string */
4769 clear_tv(&var1);
4770 return FAIL;
4771 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004772
4773 /*
4774 * Get the second variable from inside the [:].
4775 */
4776 if (**arg == ':')
4777 {
4778 range = TRUE;
4779 *arg = skipwhite(*arg + 1);
4780 if (**arg == ']')
4781 empty2 = TRUE;
4782 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4783 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004784 if (!empty1)
4785 clear_tv(&var1);
4786 return FAIL;
4787 }
4788 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4789 {
4790 /* not a number or string */
4791 if (!empty1)
4792 clear_tv(&var1);
4793 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004794 return FAIL;
4795 }
4796 }
4797
4798 /* Check for the ']'. */
4799 if (**arg != ']')
4800 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004801 if (verbose)
4802 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004803 clear_tv(&var1);
4804 if (range)
4805 clear_tv(&var2);
4806 return FAIL;
4807 }
4808 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004809 }
4810
4811 if (evaluate)
4812 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004813 n1 = 0;
4814 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004815 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004816 n1 = get_tv_number(&var1);
4817 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004818 }
4819 if (range)
4820 {
4821 if (empty2)
4822 n2 = -1;
4823 else
4824 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004825 n2 = get_tv_number(&var2);
4826 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004827 }
4828 }
4829
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004830 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004831 {
4832 case VAR_NUMBER:
4833 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004834 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004835 len = (long)STRLEN(s);
4836 if (range)
4837 {
4838 /* The resulting variable is a substring. If the indexes
4839 * are out of range the result is empty. */
4840 if (n1 < 0)
4841 {
4842 n1 = len + n1;
4843 if (n1 < 0)
4844 n1 = 0;
4845 }
4846 if (n2 < 0)
4847 n2 = len + n2;
4848 else if (n2 >= len)
4849 n2 = len;
4850 if (n1 >= len || n2 < 0 || n1 > n2)
4851 s = NULL;
4852 else
4853 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4854 }
4855 else
4856 {
4857 /* The resulting variable is a string of a single
4858 * character. If the index is too big or negative the
4859 * result is empty. */
4860 if (n1 >= len || n1 < 0)
4861 s = NULL;
4862 else
4863 s = vim_strnsave(s + n1, 1);
4864 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004865 clear_tv(rettv);
4866 rettv->v_type = VAR_STRING;
4867 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004868 break;
4869
4870 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004871 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004872 if (n1 < 0)
4873 n1 = len + n1;
4874 if (!empty1 && (n1 < 0 || n1 >= len))
4875 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004876 /* For a range we allow invalid values and return an empty
4877 * list. A list index out of range is an error. */
4878 if (!range)
4879 {
4880 if (verbose)
4881 EMSGN(_(e_listidx), n1);
4882 return FAIL;
4883 }
4884 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004885 }
4886 if (range)
4887 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004888 list_T *l;
4889 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004890
4891 if (n2 < 0)
4892 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004893 else if (n2 >= len)
4894 n2 = len - 1;
4895 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004896 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004897 l = list_alloc();
4898 if (l == NULL)
4899 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004900 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004901 n1 <= n2; ++n1)
4902 {
4903 if (list_append_tv(l, &item->li_tv) == FAIL)
4904 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00004905 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004906 return FAIL;
4907 }
4908 item = item->li_next;
4909 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004910 clear_tv(rettv);
4911 rettv->v_type = VAR_LIST;
4912 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004913 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004914 }
4915 else
4916 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004917 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004918 clear_tv(rettv);
4919 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004920 }
4921 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004922
4923 case VAR_DICT:
4924 if (range)
4925 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004926 if (verbose)
4927 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004928 if (len == -1)
4929 clear_tv(&var1);
4930 return FAIL;
4931 }
4932 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004933 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004934
4935 if (len == -1)
4936 {
4937 key = get_tv_string(&var1);
4938 if (*key == NUL)
4939 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004940 if (verbose)
4941 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004942 clear_tv(&var1);
4943 return FAIL;
4944 }
4945 }
4946
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004947 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004948
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004949 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004950 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004951 if (len == -1)
4952 clear_tv(&var1);
4953 if (item == NULL)
4954 return FAIL;
4955
4956 copy_tv(&item->di_tv, &var1);
4957 clear_tv(rettv);
4958 *rettv = var1;
4959 }
4960 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004961 }
4962 }
4963
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004964 return OK;
4965}
4966
4967/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 * Get an option value.
4969 * "arg" points to the '&' or '+' before the option name.
4970 * "arg" is advanced to character after the option name.
4971 * Return OK or FAIL.
4972 */
4973 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004974get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004976 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 int evaluate;
4978{
4979 char_u *option_end;
4980 long numval;
4981 char_u *stringval;
4982 int opt_type;
4983 int c;
4984 int working = (**arg == '+'); /* has("+option") */
4985 int ret = OK;
4986 int opt_flags;
4987
4988 /*
4989 * Isolate the option name and find its value.
4990 */
4991 option_end = find_option_end(arg, &opt_flags);
4992 if (option_end == NULL)
4993 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004994 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 EMSG2(_("E112: Option name missing: %s"), *arg);
4996 return FAIL;
4997 }
4998
4999 if (!evaluate)
5000 {
5001 *arg = option_end;
5002 return OK;
5003 }
5004
5005 c = *option_end;
5006 *option_end = NUL;
5007 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005008 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009
5010 if (opt_type == -3) /* invalid name */
5011 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005012 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 EMSG2(_("E113: Unknown option: %s"), *arg);
5014 ret = FAIL;
5015 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005016 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 {
5018 if (opt_type == -2) /* hidden string option */
5019 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005020 rettv->v_type = VAR_STRING;
5021 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 }
5023 else if (opt_type == -1) /* hidden number option */
5024 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005025 rettv->v_type = VAR_NUMBER;
5026 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 }
5028 else if (opt_type == 1) /* number option */
5029 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005030 rettv->v_type = VAR_NUMBER;
5031 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 }
5033 else /* string option */
5034 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005035 rettv->v_type = VAR_STRING;
5036 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 }
5038 }
5039 else if (working && (opt_type == -2 || opt_type == -1))
5040 ret = FAIL;
5041
5042 *option_end = c; /* put back for error messages */
5043 *arg = option_end;
5044
5045 return ret;
5046}
5047
5048/*
5049 * Allocate a variable for a string constant.
5050 * Return OK or FAIL.
5051 */
5052 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005053get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005055 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 int evaluate;
5057{
5058 char_u *p;
5059 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 int extra = 0;
5061
5062 /*
5063 * Find the end of the string, skipping backslashed characters.
5064 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005065 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 {
5067 if (*p == '\\' && p[1] != NUL)
5068 {
5069 ++p;
5070 /* A "\<x>" form occupies at least 4 characters, and produces up
5071 * to 6 characters: reserve space for 2 extra */
5072 if (*p == '<')
5073 extra += 2;
5074 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 }
5076
5077 if (*p != '"')
5078 {
5079 EMSG2(_("E114: Missing quote: %s"), *arg);
5080 return FAIL;
5081 }
5082
5083 /* If only parsing, set *arg and return here */
5084 if (!evaluate)
5085 {
5086 *arg = p + 1;
5087 return OK;
5088 }
5089
5090 /*
5091 * Copy the string into allocated memory, handling backslashed
5092 * characters.
5093 */
5094 name = alloc((unsigned)(p - *arg + extra));
5095 if (name == NULL)
5096 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005097 rettv->v_type = VAR_STRING;
5098 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099
Bram Moolenaar8c711452005-01-14 21:53:12 +00005100 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 {
5102 if (*p == '\\')
5103 {
5104 switch (*++p)
5105 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005106 case 'b': *name++ = BS; ++p; break;
5107 case 'e': *name++ = ESC; ++p; break;
5108 case 'f': *name++ = FF; ++p; break;
5109 case 'n': *name++ = NL; ++p; break;
5110 case 'r': *name++ = CAR; ++p; break;
5111 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112
5113 case 'X': /* hex: "\x1", "\x12" */
5114 case 'x':
5115 case 'u': /* Unicode: "\u0023" */
5116 case 'U':
5117 if (vim_isxdigit(p[1]))
5118 {
5119 int n, nr;
5120 int c = toupper(*p);
5121
5122 if (c == 'X')
5123 n = 2;
5124 else
5125 n = 4;
5126 nr = 0;
5127 while (--n >= 0 && vim_isxdigit(p[1]))
5128 {
5129 ++p;
5130 nr = (nr << 4) + hex2nr(*p);
5131 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005132 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133#ifdef FEAT_MBYTE
5134 /* For "\u" store the number according to
5135 * 'encoding'. */
5136 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005137 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 else
5139#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005140 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 break;
5143
5144 /* octal: "\1", "\12", "\123" */
5145 case '0':
5146 case '1':
5147 case '2':
5148 case '3':
5149 case '4':
5150 case '5':
5151 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005152 case '7': *name = *p++ - '0';
5153 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005155 *name = (*name << 3) + *p++ - '0';
5156 if (*p >= '0' && *p <= '7')
5157 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005159 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 break;
5161
5162 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005163 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 if (extra != 0)
5165 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005166 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 break;
5168 }
5169 /* FALLTHROUGH */
5170
Bram Moolenaar8c711452005-01-14 21:53:12 +00005171 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 break;
5173 }
5174 }
5175 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005176 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005179 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 *arg = p + 1;
5181
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 return OK;
5183}
5184
5185/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005186 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 * Return OK or FAIL.
5188 */
5189 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005190get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005192 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 int evaluate;
5194{
5195 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005196 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005197 int reduce = 0;
5198
5199 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005200 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005201 */
5202 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5203 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005204 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005205 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005206 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005207 break;
5208 ++reduce;
5209 ++p;
5210 }
5211 }
5212
Bram Moolenaar8c711452005-01-14 21:53:12 +00005213 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005214 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005215 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005216 return FAIL;
5217 }
5218
Bram Moolenaar8c711452005-01-14 21:53:12 +00005219 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005220 if (!evaluate)
5221 {
5222 *arg = p + 1;
5223 return OK;
5224 }
5225
5226 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005227 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005228 */
5229 str = alloc((unsigned)((p - *arg) - reduce));
5230 if (str == NULL)
5231 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005232 rettv->v_type = VAR_STRING;
5233 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005234
Bram Moolenaar8c711452005-01-14 21:53:12 +00005235 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005236 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005237 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005238 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005239 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005240 break;
5241 ++p;
5242 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005243 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005244 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005245 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005246 *arg = p + 1;
5247
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005248 return OK;
5249}
5250
5251/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005252 * Allocate a variable for a List and fill it from "*arg".
5253 * Return OK or FAIL.
5254 */
5255 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005256get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005257 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005258 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005259 int evaluate;
5260{
Bram Moolenaar33570922005-01-25 22:26:29 +00005261 list_T *l = NULL;
5262 typval_T tv;
5263 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005264
5265 if (evaluate)
5266 {
5267 l = list_alloc();
5268 if (l == NULL)
5269 return FAIL;
5270 }
5271
5272 *arg = skipwhite(*arg + 1);
5273 while (**arg != ']' && **arg != NUL)
5274 {
5275 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5276 goto failret;
5277 if (evaluate)
5278 {
5279 item = listitem_alloc();
5280 if (item != NULL)
5281 {
5282 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005283 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005284 list_append(l, item);
5285 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005286 else
5287 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005288 }
5289
5290 if (**arg == ']')
5291 break;
5292 if (**arg != ',')
5293 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005294 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005295 goto failret;
5296 }
5297 *arg = skipwhite(*arg + 1);
5298 }
5299
5300 if (**arg != ']')
5301 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005302 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005303failret:
5304 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005305 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005306 return FAIL;
5307 }
5308
5309 *arg = skipwhite(*arg + 1);
5310 if (evaluate)
5311 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005312 rettv->v_type = VAR_LIST;
5313 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005314 ++l->lv_refcount;
5315 }
5316
5317 return OK;
5318}
5319
5320/*
5321 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005322 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005323 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005324 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005325list_alloc()
5326{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005327 list_T *l;
5328
5329 l = (list_T *)alloc_clear(sizeof(list_T));
5330 if (l != NULL)
5331 {
5332 /* Prepend the list to the list of lists for garbage collection. */
5333 if (first_list != NULL)
5334 first_list->lv_used_prev = l;
5335 l->lv_used_prev = NULL;
5336 l->lv_used_next = first_list;
5337 first_list = l;
5338 }
5339 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005340}
5341
5342/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005343 * Allocate an empty list for a return value.
5344 * Returns OK or FAIL.
5345 */
5346 static int
5347rettv_list_alloc(rettv)
5348 typval_T *rettv;
5349{
5350 list_T *l = list_alloc();
5351
5352 if (l == NULL)
5353 return FAIL;
5354
5355 rettv->vval.v_list = l;
5356 rettv->v_type = VAR_LIST;
5357 ++l->lv_refcount;
5358 return OK;
5359}
5360
5361/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005362 * Unreference a list: decrement the reference count and free it when it
5363 * becomes zero.
5364 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005365 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005366list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005367 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005368{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005369 if (l != NULL && --l->lv_refcount <= 0)
5370 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005371}
5372
5373/*
5374 * Free a list, including all items it points to.
5375 * Ignores the reference count.
5376 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005377 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005378list_free(l, recurse)
5379 list_T *l;
5380 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005381{
Bram Moolenaar33570922005-01-25 22:26:29 +00005382 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005383
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005384 /* Remove the list from the list of lists for garbage collection. */
5385 if (l->lv_used_prev == NULL)
5386 first_list = l->lv_used_next;
5387 else
5388 l->lv_used_prev->lv_used_next = l->lv_used_next;
5389 if (l->lv_used_next != NULL)
5390 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5391
Bram Moolenaard9fba312005-06-26 22:34:35 +00005392 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005393 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005394 /* Remove the item before deleting it. */
5395 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005396 if (recurse || (item->li_tv.v_type != VAR_LIST
5397 && item->li_tv.v_type != VAR_DICT))
5398 clear_tv(&item->li_tv);
5399 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005400 }
5401 vim_free(l);
5402}
5403
5404/*
5405 * Allocate a list item.
5406 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005407 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005408listitem_alloc()
5409{
Bram Moolenaar33570922005-01-25 22:26:29 +00005410 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005411}
5412
5413/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005414 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005415 */
5416 static void
5417listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005418 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005419{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005420 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005421 vim_free(item);
5422}
5423
5424/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005425 * Remove a list item from a List and free it. Also clears the value.
5426 */
5427 static void
5428listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005429 list_T *l;
5430 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005431{
5432 list_remove(l, item, item);
5433 listitem_free(item);
5434}
5435
5436/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005437 * Get the number of items in a list.
5438 */
5439 static long
5440list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005441 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005442{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005443 if (l == NULL)
5444 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005445 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005446}
5447
5448/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005449 * Return TRUE when two lists have exactly the same values.
5450 */
5451 static int
5452list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005453 list_T *l1;
5454 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005455 int ic; /* ignore case for strings */
5456{
Bram Moolenaar33570922005-01-25 22:26:29 +00005457 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005458
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005459 if (l1 == l2)
5460 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005461 if (list_len(l1) != list_len(l2))
5462 return FALSE;
5463
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005464 for (item1 = l1->lv_first, item2 = l2->lv_first;
5465 item1 != NULL && item2 != NULL;
5466 item1 = item1->li_next, item2 = item2->li_next)
5467 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5468 return FALSE;
5469 return item1 == NULL && item2 == NULL;
5470}
5471
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005472#if defined(FEAT_PYTHON) || defined(PROTO)
5473/*
5474 * Return the dictitem that an entry in a hashtable points to.
5475 */
5476 dictitem_T *
5477dict_lookup(hi)
5478 hashitem_T *hi;
5479{
5480 return HI2DI(hi);
5481}
5482#endif
5483
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005484/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005485 * Return TRUE when two dictionaries have exactly the same key/values.
5486 */
5487 static int
5488dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005489 dict_T *d1;
5490 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005491 int ic; /* ignore case for strings */
5492{
Bram Moolenaar33570922005-01-25 22:26:29 +00005493 hashitem_T *hi;
5494 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005495 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005496
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005497 if (d1 == d2)
5498 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005499 if (dict_len(d1) != dict_len(d2))
5500 return FALSE;
5501
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005502 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00005503 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005504 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005505 if (!HASHITEM_EMPTY(hi))
5506 {
5507 item2 = dict_find(d2, hi->hi_key, -1);
5508 if (item2 == NULL)
5509 return FALSE;
5510 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5511 return FALSE;
5512 --todo;
5513 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005514 }
5515 return TRUE;
5516}
5517
5518/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005519 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005520 * Compares the items just like "==" would compare them, but strings and
5521 * numbers are different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005522 */
5523 static int
5524tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005525 typval_T *tv1;
5526 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005527 int ic; /* ignore case */
5528{
5529 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005530 char_u *s1, *s2;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005531 static int recursive = 0; /* cach recursive loops */
5532 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005533
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005534 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005535 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005536 /* Catch lists and dicts that have an endless loop by limiting
5537 * recursiveness to 1000. We guess they are equal then. */
5538 if (recursive >= 1000)
5539 return TRUE;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005540
5541 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005542 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005543 case VAR_LIST:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005544 ++recursive;
5545 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5546 --recursive;
5547 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005548
5549 case VAR_DICT:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005550 ++recursive;
5551 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5552 --recursive;
5553 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005554
5555 case VAR_FUNC:
5556 return (tv1->vval.v_string != NULL
5557 && tv2->vval.v_string != NULL
5558 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5559
5560 case VAR_NUMBER:
5561 return tv1->vval.v_number == tv2->vval.v_number;
5562
5563 case VAR_STRING:
5564 s1 = get_tv_string_buf(tv1, buf1);
5565 s2 = get_tv_string_buf(tv2, buf2);
5566 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005567 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005568
5569 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005570 return TRUE;
5571}
5572
5573/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005574 * Locate item with index "n" in list "l" and return it.
5575 * A negative index is counted from the end; -1 is the last item.
5576 * Returns NULL when "n" is out of range.
5577 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005578 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005579list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005580 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005581 long n;
5582{
Bram Moolenaar33570922005-01-25 22:26:29 +00005583 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005584 long idx;
5585
5586 if (l == NULL)
5587 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005588
5589 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005590 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005591 n = l->lv_len + n;
5592
5593 /* Check for index out of range. */
5594 if (n < 0 || n >= l->lv_len)
5595 return NULL;
5596
5597 /* When there is a cached index may start search from there. */
5598 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005599 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005600 if (n < l->lv_idx / 2)
5601 {
5602 /* closest to the start of the list */
5603 item = l->lv_first;
5604 idx = 0;
5605 }
5606 else if (n > (l->lv_idx + l->lv_len) / 2)
5607 {
5608 /* closest to the end of the list */
5609 item = l->lv_last;
5610 idx = l->lv_len - 1;
5611 }
5612 else
5613 {
5614 /* closest to the cached index */
5615 item = l->lv_idx_item;
5616 idx = l->lv_idx;
5617 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005618 }
5619 else
5620 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005621 if (n < l->lv_len / 2)
5622 {
5623 /* closest to the start of the list */
5624 item = l->lv_first;
5625 idx = 0;
5626 }
5627 else
5628 {
5629 /* closest to the end of the list */
5630 item = l->lv_last;
5631 idx = l->lv_len - 1;
5632 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005633 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005634
5635 while (n > idx)
5636 {
5637 /* search forward */
5638 item = item->li_next;
5639 ++idx;
5640 }
5641 while (n < idx)
5642 {
5643 /* search backward */
5644 item = item->li_prev;
5645 --idx;
5646 }
5647
5648 /* cache the used index */
5649 l->lv_idx = idx;
5650 l->lv_idx_item = item;
5651
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005652 return item;
5653}
5654
5655/*
Bram Moolenaara5525202006-03-02 22:52:09 +00005656 * Get list item "l[idx]" as a number.
5657 */
5658 static long
5659list_find_nr(l, idx, errorp)
5660 list_T *l;
5661 long idx;
5662 int *errorp; /* set to TRUE when something wrong */
5663{
5664 listitem_T *li;
5665
5666 li = list_find(l, idx);
5667 if (li == NULL)
5668 {
5669 if (errorp != NULL)
5670 *errorp = TRUE;
5671 return -1L;
5672 }
5673 return get_tv_number_chk(&li->li_tv, errorp);
5674}
5675
5676/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005677 * Locate "item" list "l" and return its index.
5678 * Returns -1 when "item" is not in the list.
5679 */
5680 static long
5681list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005682 list_T *l;
5683 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005684{
5685 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005686 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005687
5688 if (l == NULL)
5689 return -1;
5690 idx = 0;
5691 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5692 ++idx;
5693 if (li == NULL)
5694 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005695 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005696}
5697
5698/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005699 * Append item "item" to the end of list "l".
5700 */
5701 static void
5702list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005703 list_T *l;
5704 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005705{
5706 if (l->lv_last == NULL)
5707 {
5708 /* empty list */
5709 l->lv_first = item;
5710 l->lv_last = item;
5711 item->li_prev = NULL;
5712 }
5713 else
5714 {
5715 l->lv_last->li_next = item;
5716 item->li_prev = l->lv_last;
5717 l->lv_last = item;
5718 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005719 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005720 item->li_next = NULL;
5721}
5722
5723/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005724 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005725 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005726 */
5727 static int
5728list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005729 list_T *l;
5730 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005731{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005732 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005733
Bram Moolenaar05159a02005-02-26 23:04:13 +00005734 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005735 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005736 copy_tv(tv, &li->li_tv);
5737 list_append(l, li);
5738 return OK;
5739}
5740
5741/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005742 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005743 * Return FAIL when out of memory.
5744 */
5745 int
5746list_append_dict(list, dict)
5747 list_T *list;
5748 dict_T *dict;
5749{
5750 listitem_T *li = listitem_alloc();
5751
5752 if (li == NULL)
5753 return FAIL;
5754 li->li_tv.v_type = VAR_DICT;
5755 li->li_tv.v_lock = 0;
5756 li->li_tv.vval.v_dict = dict;
5757 list_append(list, li);
5758 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005759 return OK;
5760}
5761
5762/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005763 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00005764 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005765 * Returns FAIL when out of memory.
5766 */
5767 static int
Bram Moolenaar4463f292005-09-25 22:20:24 +00005768list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005769 list_T *l;
5770 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00005771 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005772{
5773 listitem_T *li = listitem_alloc();
5774
5775 if (li == NULL)
5776 return FAIL;
5777 list_append(l, li);
5778 li->li_tv.v_type = VAR_STRING;
5779 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005780 if (str == NULL)
5781 li->li_tv.vval.v_string = NULL;
5782 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005783 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005784 return FAIL;
5785 return OK;
5786}
5787
5788/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00005789 * Append "n" to list "l".
5790 * Returns FAIL when out of memory.
5791 */
5792 static int
5793list_append_number(l, n)
5794 list_T *l;
5795 varnumber_T n;
5796{
5797 listitem_T *li;
5798
5799 li = listitem_alloc();
5800 if (li == NULL)
5801 return FAIL;
5802 li->li_tv.v_type = VAR_NUMBER;
5803 li->li_tv.v_lock = 0;
5804 li->li_tv.vval.v_number = n;
5805 list_append(l, li);
5806 return OK;
5807}
5808
5809/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005810 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005811 * If "item" is NULL append at the end.
5812 * Return FAIL when out of memory.
5813 */
5814 static int
5815list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005816 list_T *l;
5817 typval_T *tv;
5818 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005819{
Bram Moolenaar33570922005-01-25 22:26:29 +00005820 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005821
5822 if (ni == NULL)
5823 return FAIL;
5824 copy_tv(tv, &ni->li_tv);
5825 if (item == NULL)
5826 /* Append new item at end of list. */
5827 list_append(l, ni);
5828 else
5829 {
5830 /* Insert new item before existing item. */
5831 ni->li_prev = item->li_prev;
5832 ni->li_next = item;
5833 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005834 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005835 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005836 ++l->lv_idx;
5837 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005838 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005839 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005840 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005841 l->lv_idx_item = NULL;
5842 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005843 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005844 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005845 }
5846 return OK;
5847}
5848
5849/*
5850 * Extend "l1" with "l2".
5851 * If "bef" is NULL append at the end, otherwise insert before this item.
5852 * Returns FAIL when out of memory.
5853 */
5854 static int
5855list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005856 list_T *l1;
5857 list_T *l2;
5858 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005859{
Bram Moolenaar33570922005-01-25 22:26:29 +00005860 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005861
5862 for (item = l2->lv_first; item != NULL; item = item->li_next)
5863 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5864 return FAIL;
5865 return OK;
5866}
5867
5868/*
5869 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5870 * Return FAIL when out of memory.
5871 */
5872 static int
5873list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005874 list_T *l1;
5875 list_T *l2;
5876 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005877{
Bram Moolenaar33570922005-01-25 22:26:29 +00005878 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005879
5880 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005881 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005882 if (l == NULL)
5883 return FAIL;
5884 tv->v_type = VAR_LIST;
5885 tv->vval.v_list = l;
5886
5887 /* append all items from the second list */
5888 return list_extend(l, l2, NULL);
5889}
5890
5891/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005892 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005893 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005894 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005895 * Returns NULL when out of memory.
5896 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005897 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005898list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005899 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005900 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005901 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005902{
Bram Moolenaar33570922005-01-25 22:26:29 +00005903 list_T *copy;
5904 listitem_T *item;
5905 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005906
5907 if (orig == NULL)
5908 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005909
5910 copy = list_alloc();
5911 if (copy != NULL)
5912 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005913 if (copyID != 0)
5914 {
5915 /* Do this before adding the items, because one of the items may
5916 * refer back to this list. */
5917 orig->lv_copyID = copyID;
5918 orig->lv_copylist = copy;
5919 }
5920 for (item = orig->lv_first; item != NULL && !got_int;
5921 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005922 {
5923 ni = listitem_alloc();
5924 if (ni == NULL)
5925 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005926 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005927 {
5928 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5929 {
5930 vim_free(ni);
5931 break;
5932 }
5933 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005934 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005935 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005936 list_append(copy, ni);
5937 }
5938 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005939 if (item != NULL)
5940 {
5941 list_unref(copy);
5942 copy = NULL;
5943 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005944 }
5945
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005946 return copy;
5947}
5948
5949/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005950 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005951 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005952 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005953 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005954list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005955 list_T *l;
5956 listitem_T *item;
5957 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005958{
Bram Moolenaar33570922005-01-25 22:26:29 +00005959 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005960
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005961 /* notify watchers */
5962 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005963 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005964 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005965 list_fix_watch(l, ip);
5966 if (ip == item2)
5967 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005968 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005969
5970 if (item2->li_next == NULL)
5971 l->lv_last = item->li_prev;
5972 else
5973 item2->li_next->li_prev = item->li_prev;
5974 if (item->li_prev == NULL)
5975 l->lv_first = item2->li_next;
5976 else
5977 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005978 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005979}
5980
5981/*
5982 * Return an allocated string with the string representation of a list.
5983 * May return NULL.
5984 */
5985 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005986list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005987 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005988 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005989{
5990 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005991
5992 if (tv->vval.v_list == NULL)
5993 return NULL;
5994 ga_init2(&ga, (int)sizeof(char), 80);
5995 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005996 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005997 {
5998 vim_free(ga.ga_data);
5999 return NULL;
6000 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006001 ga_append(&ga, ']');
6002 ga_append(&ga, NUL);
6003 return (char_u *)ga.ga_data;
6004}
6005
6006/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006007 * Join list "l" into a string in "*gap", using separator "sep".
6008 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006009 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006010 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006011 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006012list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006013 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006014 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006015 char_u *sep;
6016 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006017 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006018{
6019 int first = TRUE;
6020 char_u *tofree;
6021 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006022 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006023 char_u *s;
6024
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006025 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006026 {
6027 if (first)
6028 first = FALSE;
6029 else
6030 ga_concat(gap, sep);
6031
6032 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006033 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006034 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006035 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006036 if (s != NULL)
6037 ga_concat(gap, s);
6038 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006039 if (s == NULL)
6040 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006041 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006042 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006043}
6044
6045/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006046 * Garbage collection for lists and dictionaries.
6047 *
6048 * We use reference counts to be able to free most items right away when they
6049 * are no longer used. But for composite items it's possible that it becomes
6050 * unused while the reference count is > 0: When there is a recursive
6051 * reference. Example:
6052 * :let l = [1, 2, 3]
6053 * :let d = {9: l}
6054 * :let l[1] = d
6055 *
6056 * Since this is quite unusual we handle this with garbage collection: every
6057 * once in a while find out which lists and dicts are not referenced from any
6058 * variable.
6059 *
6060 * Here is a good reference text about garbage collection (refers to Python
6061 * but it applies to all reference-counting mechanisms):
6062 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006063 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006064
6065/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006066 * Do garbage collection for lists and dicts.
6067 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006068 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006069 int
6070garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006071{
6072 dict_T *dd;
6073 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006074 int copyID = ++current_copyID;
6075 buf_T *buf;
6076 win_T *wp;
6077 int i;
6078 funccall_T *fc;
6079 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006080#ifdef FEAT_WINDOWS
6081 tabpage_T *tp;
6082#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006083
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006084 /* Only do this once. */
6085 want_garbage_collect = FALSE;
6086 may_garbage_collect = FALSE;
6087
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006088 /*
6089 * 1. Go through all accessible variables and mark all lists and dicts
6090 * with copyID.
6091 */
6092 /* script-local variables */
6093 for (i = 1; i <= ga_scripts.ga_len; ++i)
6094 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6095
6096 /* buffer-local variables */
6097 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6098 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6099
6100 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006101 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006102 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6103
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006104#ifdef FEAT_WINDOWS
6105 /* tabpage-local variables */
6106 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6107 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6108#endif
6109
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006110 /* global variables */
6111 set_ref_in_ht(&globvarht, copyID);
6112
6113 /* function-local variables */
6114 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6115 {
6116 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6117 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6118 }
6119
6120 /*
6121 * 2. Go through the list of dicts and free items without the copyID.
6122 */
6123 for (dd = first_dict; dd != NULL; )
6124 if (dd->dv_copyID != copyID)
6125 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006126 /* Free the Dictionary and ordinary items it contains, but don't
6127 * recurse into Lists and Dictionaries, they will be in the list
6128 * of dicts or list of lists. */
6129 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006130 did_free = TRUE;
6131
6132 /* restart, next dict may also have been freed */
6133 dd = first_dict;
6134 }
6135 else
6136 dd = dd->dv_used_next;
6137
6138 /*
6139 * 3. Go through the list of lists and free items without the copyID.
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006140 * But don't free a list that has a watcher (used in a for loop), these
6141 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006142 */
6143 for (ll = first_list; ll != NULL; )
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006144 if (ll->lv_copyID != copyID && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006145 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006146 /* Free the List and ordinary items it contains, but don't recurse
6147 * into Lists and Dictionaries, they will be in the list of dicts
6148 * or list of lists. */
6149 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006150 did_free = TRUE;
6151
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006152 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006153 ll = first_list;
6154 }
6155 else
6156 ll = ll->lv_used_next;
6157
6158 return did_free;
6159}
6160
6161/*
6162 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6163 */
6164 static void
6165set_ref_in_ht(ht, copyID)
6166 hashtab_T *ht;
6167 int copyID;
6168{
6169 int todo;
6170 hashitem_T *hi;
6171
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006172 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006173 for (hi = ht->ht_array; todo > 0; ++hi)
6174 if (!HASHITEM_EMPTY(hi))
6175 {
6176 --todo;
6177 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6178 }
6179}
6180
6181/*
6182 * Mark all lists and dicts referenced through list "l" with "copyID".
6183 */
6184 static void
6185set_ref_in_list(l, copyID)
6186 list_T *l;
6187 int copyID;
6188{
6189 listitem_T *li;
6190
6191 for (li = l->lv_first; li != NULL; li = li->li_next)
6192 set_ref_in_item(&li->li_tv, copyID);
6193}
6194
6195/*
6196 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6197 */
6198 static void
6199set_ref_in_item(tv, copyID)
6200 typval_T *tv;
6201 int copyID;
6202{
6203 dict_T *dd;
6204 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006205
6206 switch (tv->v_type)
6207 {
6208 case VAR_DICT:
6209 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006210 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006211 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006212 /* Didn't see this dict yet. */
6213 dd->dv_copyID = copyID;
6214 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006215 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006216 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006217
6218 case VAR_LIST:
6219 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006220 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006221 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006222 /* Didn't see this list yet. */
6223 ll->lv_copyID = copyID;
6224 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006225 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006226 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006227 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006228 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006229}
6230
6231/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006232 * Allocate an empty header for a dictionary.
6233 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006234 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006235dict_alloc()
6236{
Bram Moolenaar33570922005-01-25 22:26:29 +00006237 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006238
Bram Moolenaar33570922005-01-25 22:26:29 +00006239 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006240 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006241 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006242 /* Add the list to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006243 if (first_dict != NULL)
6244 first_dict->dv_used_prev = d;
6245 d->dv_used_next = first_dict;
6246 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006247 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006248
Bram Moolenaar33570922005-01-25 22:26:29 +00006249 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006250 d->dv_lock = 0;
6251 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006252 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006253 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006254 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006255}
6256
6257/*
6258 * Unreference a Dictionary: decrement the reference count and free it when it
6259 * becomes zero.
6260 */
6261 static void
6262dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006263 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006264{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006265 if (d != NULL && --d->dv_refcount <= 0)
6266 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006267}
6268
6269/*
6270 * Free a Dictionary, including all items it contains.
6271 * Ignores the reference count.
6272 */
6273 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006274dict_free(d, recurse)
6275 dict_T *d;
6276 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006277{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006278 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006279 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006280 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006281
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006282 /* Remove the dict from the list of dicts for garbage collection. */
6283 if (d->dv_used_prev == NULL)
6284 first_dict = d->dv_used_next;
6285 else
6286 d->dv_used_prev->dv_used_next = d->dv_used_next;
6287 if (d->dv_used_next != NULL)
6288 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6289
6290 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006291 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006292 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006293 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006294 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006295 if (!HASHITEM_EMPTY(hi))
6296 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006297 /* Remove the item before deleting it, just in case there is
6298 * something recursive causing trouble. */
6299 di = HI2DI(hi);
6300 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00006301 if (recurse || (di->di_tv.v_type != VAR_LIST
6302 && di->di_tv.v_type != VAR_DICT))
6303 clear_tv(&di->di_tv);
6304 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006305 --todo;
6306 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006307 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006308 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006309 vim_free(d);
6310}
6311
6312/*
6313 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006314 * The "key" is copied to the new item.
6315 * Note that the value of the item "di_tv" still needs to be initialized!
6316 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006317 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006318 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006319dictitem_alloc(key)
6320 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006321{
Bram Moolenaar33570922005-01-25 22:26:29 +00006322 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006323
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006324 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006325 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006326 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006327 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006328 di->di_flags = 0;
6329 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006330 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006331}
6332
6333/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006334 * Make a copy of a Dictionary item.
6335 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006336 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006337dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006338 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006339{
Bram Moolenaar33570922005-01-25 22:26:29 +00006340 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006341
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006342 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
6343 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006344 if (di != NULL)
6345 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006346 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006347 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006348 copy_tv(&org->di_tv, &di->di_tv);
6349 }
6350 return di;
6351}
6352
6353/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006354 * Remove item "item" from Dictionary "dict" and free it.
6355 */
6356 static void
6357dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006358 dict_T *dict;
6359 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006360{
Bram Moolenaar33570922005-01-25 22:26:29 +00006361 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006362
Bram Moolenaar33570922005-01-25 22:26:29 +00006363 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006364 if (HASHITEM_EMPTY(hi))
6365 EMSG2(_(e_intern2), "dictitem_remove()");
6366 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006367 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006368 dictitem_free(item);
6369}
6370
6371/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006372 * Free a dict item. Also clears the value.
6373 */
6374 static void
6375dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006376 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006377{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006378 clear_tv(&item->di_tv);
6379 vim_free(item);
6380}
6381
6382/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006383 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6384 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006385 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006386 * Returns NULL when out of memory.
6387 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006388 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006389dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006390 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006391 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006392 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006393{
Bram Moolenaar33570922005-01-25 22:26:29 +00006394 dict_T *copy;
6395 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006396 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006397 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006398
6399 if (orig == NULL)
6400 return NULL;
6401
6402 copy = dict_alloc();
6403 if (copy != NULL)
6404 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006405 if (copyID != 0)
6406 {
6407 orig->dv_copyID = copyID;
6408 orig->dv_copydict = copy;
6409 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006410 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006411 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006412 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006413 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006414 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006415 --todo;
6416
6417 di = dictitem_alloc(hi->hi_key);
6418 if (di == NULL)
6419 break;
6420 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006421 {
6422 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6423 copyID) == FAIL)
6424 {
6425 vim_free(di);
6426 break;
6427 }
6428 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006429 else
6430 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6431 if (dict_add(copy, di) == FAIL)
6432 {
6433 dictitem_free(di);
6434 break;
6435 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006436 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006437 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006438
Bram Moolenaare9a41262005-01-15 22:18:47 +00006439 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006440 if (todo > 0)
6441 {
6442 dict_unref(copy);
6443 copy = NULL;
6444 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006445 }
6446
6447 return copy;
6448}
6449
6450/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006451 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006452 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006453 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006454 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006455dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006456 dict_T *d;
6457 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006458{
Bram Moolenaar33570922005-01-25 22:26:29 +00006459 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006460}
6461
Bram Moolenaar8c711452005-01-14 21:53:12 +00006462/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006463 * Add a number or string entry to dictionary "d".
6464 * When "str" is NULL use number "nr", otherwise use "str".
6465 * Returns FAIL when out of memory and when key already exists.
6466 */
6467 int
6468dict_add_nr_str(d, key, nr, str)
6469 dict_T *d;
6470 char *key;
6471 long nr;
6472 char_u *str;
6473{
6474 dictitem_T *item;
6475
6476 item = dictitem_alloc((char_u *)key);
6477 if (item == NULL)
6478 return FAIL;
6479 item->di_tv.v_lock = 0;
6480 if (str == NULL)
6481 {
6482 item->di_tv.v_type = VAR_NUMBER;
6483 item->di_tv.vval.v_number = nr;
6484 }
6485 else
6486 {
6487 item->di_tv.v_type = VAR_STRING;
6488 item->di_tv.vval.v_string = vim_strsave(str);
6489 }
6490 if (dict_add(d, item) == FAIL)
6491 {
6492 dictitem_free(item);
6493 return FAIL;
6494 }
6495 return OK;
6496}
6497
6498/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006499 * Get the number of items in a Dictionary.
6500 */
6501 static long
6502dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006503 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006504{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006505 if (d == NULL)
6506 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006507 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006508}
6509
6510/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006511 * Find item "key[len]" in Dictionary "d".
6512 * If "len" is negative use strlen(key).
6513 * Returns NULL when not found.
6514 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006515 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006516dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006517 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006518 char_u *key;
6519 int len;
6520{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006521#define AKEYLEN 200
6522 char_u buf[AKEYLEN];
6523 char_u *akey;
6524 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006525 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006526
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006527 if (len < 0)
6528 akey = key;
6529 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006530 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006531 tofree = akey = vim_strnsave(key, len);
6532 if (akey == NULL)
6533 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006534 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006535 else
6536 {
6537 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006538 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006539 akey = buf;
6540 }
6541
Bram Moolenaar33570922005-01-25 22:26:29 +00006542 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006543 vim_free(tofree);
6544 if (HASHITEM_EMPTY(hi))
6545 return NULL;
6546 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006547}
6548
6549/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006550 * Get a string item from a dictionary.
6551 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00006552 * Returns NULL if the entry doesn't exist or out of memory.
6553 */
6554 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006555get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00006556 dict_T *d;
6557 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006558 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006559{
6560 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006561 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006562
6563 di = dict_find(d, key, -1);
6564 if (di == NULL)
6565 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006566 s = get_tv_string(&di->di_tv);
6567 if (save && s != NULL)
6568 s = vim_strsave(s);
6569 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006570}
6571
6572/*
6573 * Get a number item from a dictionary.
6574 * Returns 0 if the entry doesn't exist or out of memory.
6575 */
6576 long
6577get_dict_number(d, key)
6578 dict_T *d;
6579 char_u *key;
6580{
6581 dictitem_T *di;
6582
6583 di = dict_find(d, key, -1);
6584 if (di == NULL)
6585 return 0;
6586 return get_tv_number(&di->di_tv);
6587}
6588
6589/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006590 * Return an allocated string with the string representation of a Dictionary.
6591 * May return NULL.
6592 */
6593 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006594dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006595 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006596 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006597{
6598 garray_T ga;
6599 int first = TRUE;
6600 char_u *tofree;
6601 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006602 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006603 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006604 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006605 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006606
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006607 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006608 return NULL;
6609 ga_init2(&ga, (int)sizeof(char), 80);
6610 ga_append(&ga, '{');
6611
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006612 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006613 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006614 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006615 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006616 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006617 --todo;
6618
6619 if (first)
6620 first = FALSE;
6621 else
6622 ga_concat(&ga, (char_u *)", ");
6623
6624 tofree = string_quote(hi->hi_key, FALSE);
6625 if (tofree != NULL)
6626 {
6627 ga_concat(&ga, tofree);
6628 vim_free(tofree);
6629 }
6630 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006631 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006632 if (s != NULL)
6633 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006634 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006635 if (s == NULL)
6636 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006637 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006638 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006639 if (todo > 0)
6640 {
6641 vim_free(ga.ga_data);
6642 return NULL;
6643 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006644
6645 ga_append(&ga, '}');
6646 ga_append(&ga, NUL);
6647 return (char_u *)ga.ga_data;
6648}
6649
6650/*
6651 * Allocate a variable for a Dictionary and fill it from "*arg".
6652 * Return OK or FAIL. Returns NOTDONE for {expr}.
6653 */
6654 static int
6655get_dict_tv(arg, rettv, evaluate)
6656 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006657 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006658 int evaluate;
6659{
Bram Moolenaar33570922005-01-25 22:26:29 +00006660 dict_T *d = NULL;
6661 typval_T tvkey;
6662 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006663 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00006664 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006665 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006666 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00006667
6668 /*
6669 * First check if it's not a curly-braces thing: {expr}.
6670 * Must do this without evaluating, otherwise a function may be called
6671 * twice. Unfortunately this means we need to call eval1() twice for the
6672 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006673 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006674 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006675 if (*start != '}')
6676 {
6677 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
6678 return FAIL;
6679 if (*start == '}')
6680 return NOTDONE;
6681 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006682
6683 if (evaluate)
6684 {
6685 d = dict_alloc();
6686 if (d == NULL)
6687 return FAIL;
6688 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006689 tvkey.v_type = VAR_UNKNOWN;
6690 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006691
6692 *arg = skipwhite(*arg + 1);
6693 while (**arg != '}' && **arg != NUL)
6694 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006695 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006696 goto failret;
6697 if (**arg != ':')
6698 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006699 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006700 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006701 goto failret;
6702 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006703 key = get_tv_string_buf_chk(&tvkey, buf);
6704 if (key == NULL || *key == NUL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006705 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006706 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6707 if (key != NULL)
6708 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006709 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006710 goto failret;
6711 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006712
6713 *arg = skipwhite(*arg + 1);
6714 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6715 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006716 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006717 goto failret;
6718 }
6719 if (evaluate)
6720 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006721 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006722 if (item != NULL)
6723 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00006724 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006725 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006726 clear_tv(&tv);
6727 goto failret;
6728 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006729 item = dictitem_alloc(key);
6730 clear_tv(&tvkey);
6731 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006732 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006733 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006734 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006735 if (dict_add(d, item) == FAIL)
6736 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006737 }
6738 }
6739
6740 if (**arg == '}')
6741 break;
6742 if (**arg != ',')
6743 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006744 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006745 goto failret;
6746 }
6747 *arg = skipwhite(*arg + 1);
6748 }
6749
6750 if (**arg != '}')
6751 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006752 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006753failret:
6754 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00006755 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006756 return FAIL;
6757 }
6758
6759 *arg = skipwhite(*arg + 1);
6760 if (evaluate)
6761 {
6762 rettv->v_type = VAR_DICT;
6763 rettv->vval.v_dict = d;
6764 ++d->dv_refcount;
6765 }
6766
6767 return OK;
6768}
6769
Bram Moolenaar8c711452005-01-14 21:53:12 +00006770/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006771 * Return a string with the string representation of a variable.
6772 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006773 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006774 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006775 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006776 * May return NULL;
6777 */
6778 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006779echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006780 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006781 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006782 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006783 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006784{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006785 static int recurse = 0;
6786 char_u *r = NULL;
6787
Bram Moolenaar33570922005-01-25 22:26:29 +00006788 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006789 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006790 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006791 *tofree = NULL;
6792 return NULL;
6793 }
6794 ++recurse;
6795
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006796 switch (tv->v_type)
6797 {
6798 case VAR_FUNC:
6799 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006800 r = tv->vval.v_string;
6801 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006802
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006803 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006804 if (tv->vval.v_list == NULL)
6805 {
6806 *tofree = NULL;
6807 r = NULL;
6808 }
6809 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
6810 {
6811 *tofree = NULL;
6812 r = (char_u *)"[...]";
6813 }
6814 else
6815 {
6816 tv->vval.v_list->lv_copyID = copyID;
6817 *tofree = list2string(tv, copyID);
6818 r = *tofree;
6819 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006820 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006821
Bram Moolenaar8c711452005-01-14 21:53:12 +00006822 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006823 if (tv->vval.v_dict == NULL)
6824 {
6825 *tofree = NULL;
6826 r = NULL;
6827 }
6828 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
6829 {
6830 *tofree = NULL;
6831 r = (char_u *)"{...}";
6832 }
6833 else
6834 {
6835 tv->vval.v_dict->dv_copyID = copyID;
6836 *tofree = dict2string(tv, copyID);
6837 r = *tofree;
6838 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006839 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006840
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006841 case VAR_STRING:
6842 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006843 *tofree = NULL;
6844 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006845 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006846
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006847 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006848 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006849 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006850 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006851
6852 --recurse;
6853 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006854}
6855
6856/*
6857 * Return a string with the string representation of a variable.
6858 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6859 * "numbuf" is used for a number.
6860 * Puts quotes around strings, so that they can be parsed back by eval().
6861 * May return NULL;
6862 */
6863 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006864tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006865 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006866 char_u **tofree;
6867 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006868 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006869{
6870 switch (tv->v_type)
6871 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006872 case VAR_FUNC:
6873 *tofree = string_quote(tv->vval.v_string, TRUE);
6874 return *tofree;
6875 case VAR_STRING:
6876 *tofree = string_quote(tv->vval.v_string, FALSE);
6877 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006878 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006879 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006880 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006881 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006882 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006883 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006884 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006885 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006886}
6887
6888/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006889 * Return string "str" in ' quotes, doubling ' characters.
6890 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006891 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006892 */
6893 static char_u *
6894string_quote(str, function)
6895 char_u *str;
6896 int function;
6897{
Bram Moolenaar33570922005-01-25 22:26:29 +00006898 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006899 char_u *p, *r, *s;
6900
Bram Moolenaar33570922005-01-25 22:26:29 +00006901 len = (function ? 13 : 3);
6902 if (str != NULL)
6903 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006904 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00006905 for (p = str; *p != NUL; mb_ptr_adv(p))
6906 if (*p == '\'')
6907 ++len;
6908 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006909 s = r = alloc(len);
6910 if (r != NULL)
6911 {
6912 if (function)
6913 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006914 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006915 r += 10;
6916 }
6917 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006918 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006919 if (str != NULL)
6920 for (p = str; *p != NUL; )
6921 {
6922 if (*p == '\'')
6923 *r++ = '\'';
6924 MB_COPY_CHAR(p, r);
6925 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006926 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006927 if (function)
6928 *r++ = ')';
6929 *r++ = NUL;
6930 }
6931 return s;
6932}
6933
6934/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935 * Get the value of an environment variable.
6936 * "arg" is pointing to the '$'. It is advanced to after the name.
6937 * If the environment variable was not set, silently assume it is empty.
6938 * Always return OK.
6939 */
6940 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006941get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006943 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944 int evaluate;
6945{
6946 char_u *string = NULL;
6947 int len;
6948 int cc;
6949 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006950 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951
6952 ++*arg;
6953 name = *arg;
6954 len = get_env_len(arg);
6955 if (evaluate)
6956 {
6957 if (len != 0)
6958 {
6959 cc = name[len];
6960 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006961 /* first try vim_getenv(), fast for normal environment vars */
6962 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006964 {
6965 if (!mustfree)
6966 string = vim_strsave(string);
6967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 else
6969 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00006970 if (mustfree)
6971 vim_free(string);
6972
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 /* next try expanding things like $VIM and ${HOME} */
6974 string = expand_env_save(name - 1);
6975 if (string != NULL && *string == '$')
6976 {
6977 vim_free(string);
6978 string = NULL;
6979 }
6980 }
6981 name[len] = cc;
6982 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006983 rettv->v_type = VAR_STRING;
6984 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985 }
6986
6987 return OK;
6988}
6989
6990/*
6991 * Array with names and number of arguments of all internal functions
6992 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
6993 */
6994static struct fst
6995{
6996 char *f_name; /* function name */
6997 char f_min_argc; /* minimal number of arguments */
6998 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00006999 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007000 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001} functions[] =
7002{
Bram Moolenaar0d660222005-01-07 21:51:51 +00007003 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 {"append", 2, 2, f_append},
7005 {"argc", 0, 0, f_argc},
7006 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007007 {"argv", 0, 1, f_argv},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007009 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 {"bufexists", 1, 1, f_bufexists},
7011 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7012 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7013 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7014 {"buflisted", 1, 1, f_buflisted},
7015 {"bufloaded", 1, 1, f_bufloaded},
7016 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007017 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 {"bufwinnr", 1, 1, f_bufwinnr},
7019 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007020 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007021 {"call", 2, 3, f_call},
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007022 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023 {"char2nr", 1, 1, f_char2nr},
7024 {"cindent", 1, 1, f_cindent},
7025 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007026#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007027 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007028 {"complete_add", 1, 1, f_complete_add},
7029 {"complete_check", 0, 0, f_complete_check},
7030#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007032 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007033 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007035 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007036 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 {"delete", 1, 1, f_delete},
7038 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007039 {"diff_filler", 1, 1, f_diff_filler},
7040 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007041 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007043 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 {"eventhandler", 0, 0, f_eventhandler},
7045 {"executable", 1, 1, f_executable},
7046 {"exists", 1, 1, f_exists},
7047 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007048 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007049 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7051 {"filereadable", 1, 1, f_filereadable},
7052 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007053 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007054 {"finddir", 1, 3, f_finddir},
7055 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 {"fnamemodify", 2, 2, f_fnamemodify},
7057 {"foldclosed", 1, 1, f_foldclosed},
7058 {"foldclosedend", 1, 1, f_foldclosedend},
7059 {"foldlevel", 1, 1, f_foldlevel},
7060 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007061 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007063 {"function", 1, 1, f_function},
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007064 {"garbagecollect", 0, 0, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007065 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007066 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 {"getbufvar", 2, 2, f_getbufvar},
7068 {"getchar", 0, 1, f_getchar},
7069 {"getcharmod", 0, 0, f_getcharmod},
7070 {"getcmdline", 0, 0, f_getcmdline},
7071 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007072 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007074 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007075 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076 {"getfsize", 1, 1, f_getfsize},
7077 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007078 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007079 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007080 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaara5525202006-03-02 22:52:09 +00007081 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007082 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007083 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007085 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086 {"getwinposx", 0, 0, f_getwinposx},
7087 {"getwinposy", 0, 0, f_getwinposy},
7088 {"getwinvar", 2, 2, f_getwinvar},
7089 {"glob", 1, 1, f_glob},
7090 {"globpath", 2, 2, f_globpath},
7091 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007092 {"has_key", 2, 2, f_has_key},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007093 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7095 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7096 {"histadd", 2, 2, f_histadd},
7097 {"histdel", 1, 2, f_histdel},
7098 {"histget", 1, 2, f_histget},
7099 {"histnr", 1, 1, f_histnr},
7100 {"hlID", 1, 1, f_hlID},
7101 {"hlexists", 1, 1, f_hlexists},
7102 {"hostname", 0, 0, f_hostname},
7103 {"iconv", 3, 3, f_iconv},
7104 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007105 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007106 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007108 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 {"inputrestore", 0, 0, f_inputrestore},
7110 {"inputsave", 0, 0, f_inputsave},
7111 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007112 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007114 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007115 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007116 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007117 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007119 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 {"libcall", 3, 3, f_libcall},
7121 {"libcallnr", 3, 3, f_libcallnr},
7122 {"line", 1, 1, f_line},
7123 {"line2byte", 1, 1, f_line2byte},
7124 {"lispindent", 1, 1, f_lispindent},
7125 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007126 {"map", 2, 2, f_map},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007127 {"maparg", 1, 3, f_maparg},
7128 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007129 {"match", 2, 4, f_match},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007130 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007131 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007132 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007133 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007134 {"max", 1, 1, f_max},
7135 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007136#ifdef vim_mkdir
7137 {"mkdir", 1, 3, f_mkdir},
7138#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 {"mode", 0, 0, f_mode},
7140 {"nextnonblank", 1, 1, f_nextnonblank},
7141 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007142 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007144 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007145 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007146 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007147 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007148 {"reltime", 0, 2, f_reltime},
7149 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150 {"remote_expr", 2, 3, f_remote_expr},
7151 {"remote_foreground", 1, 1, f_remote_foreground},
7152 {"remote_peek", 1, 2, f_remote_peek},
7153 {"remote_read", 1, 1, f_remote_read},
7154 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007155 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007157 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007159 {"reverse", 1, 1, f_reverse},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00007160 {"search", 1, 3, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007161 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00007162 {"searchpair", 3, 6, f_searchpair},
7163 {"searchpairpos", 3, 6, f_searchpairpos},
7164 {"searchpos", 1, 3, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165 {"server2client", 2, 2, f_server2client},
7166 {"serverlist", 0, 0, f_serverlist},
7167 {"setbufvar", 3, 3, f_setbufvar},
7168 {"setcmdpos", 1, 1, f_setcmdpos},
7169 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007170 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007171 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007172 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173 {"setreg", 2, 3, f_setreg},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007174 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar60a495f2006-10-03 12:44:42 +00007176 {"shellescape", 1, 1, f_shellescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007178 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007179 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007180 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007181 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007182 {"split", 1, 3, f_split},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007183 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184#ifdef HAVE_STRFTIME
7185 {"strftime", 1, 2, f_strftime},
7186#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007187 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007188 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189 {"strlen", 1, 1, f_strlen},
7190 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007191 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 {"strtrans", 1, 1, f_strtrans},
7193 {"submatch", 1, 1, f_submatch},
7194 {"substitute", 4, 4, f_substitute},
7195 {"synID", 3, 3, f_synID},
7196 {"synIDattr", 2, 3, f_synIDattr},
7197 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007198 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007199 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007200 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007201 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007202 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007203 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007205 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206 {"tolower", 1, 1, f_tolower},
7207 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007208 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007210 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211 {"virtcol", 1, 1, f_virtcol},
7212 {"visualmode", 0, 1, f_visualmode},
7213 {"winbufnr", 1, 1, f_winbufnr},
7214 {"wincol", 0, 0, f_wincol},
7215 {"winheight", 1, 1, f_winheight},
7216 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007217 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007219 {"winrestview", 1, 1, f_winrestview},
7220 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007222 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223};
7224
7225#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7226
7227/*
7228 * Function given to ExpandGeneric() to obtain the list of internal
7229 * or user defined function names.
7230 */
7231 char_u *
7232get_function_name(xp, idx)
7233 expand_T *xp;
7234 int idx;
7235{
7236 static int intidx = -1;
7237 char_u *name;
7238
7239 if (idx == 0)
7240 intidx = -1;
7241 if (intidx < 0)
7242 {
7243 name = get_user_func_name(xp, idx);
7244 if (name != NULL)
7245 return name;
7246 }
7247 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7248 {
7249 STRCPY(IObuff, functions[intidx].f_name);
7250 STRCAT(IObuff, "(");
7251 if (functions[intidx].f_max_argc == 0)
7252 STRCAT(IObuff, ")");
7253 return IObuff;
7254 }
7255
7256 return NULL;
7257}
7258
7259/*
7260 * Function given to ExpandGeneric() to obtain the list of internal or
7261 * user defined variable or function names.
7262 */
7263/*ARGSUSED*/
7264 char_u *
7265get_expr_name(xp, idx)
7266 expand_T *xp;
7267 int idx;
7268{
7269 static int intidx = -1;
7270 char_u *name;
7271
7272 if (idx == 0)
7273 intidx = -1;
7274 if (intidx < 0)
7275 {
7276 name = get_function_name(xp, idx);
7277 if (name != NULL)
7278 return name;
7279 }
7280 return get_user_var_name(xp, ++intidx);
7281}
7282
7283#endif /* FEAT_CMDL_COMPL */
7284
7285/*
7286 * Find internal function in table above.
7287 * Return index, or -1 if not found
7288 */
7289 static int
7290find_internal_func(name)
7291 char_u *name; /* name of the function */
7292{
7293 int first = 0;
7294 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7295 int cmp;
7296 int x;
7297
7298 /*
7299 * Find the function name in the table. Binary search.
7300 */
7301 while (first <= last)
7302 {
7303 x = first + ((unsigned)(last - first) >> 1);
7304 cmp = STRCMP(name, functions[x].f_name);
7305 if (cmp < 0)
7306 last = x - 1;
7307 else if (cmp > 0)
7308 first = x + 1;
7309 else
7310 return x;
7311 }
7312 return -1;
7313}
7314
7315/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007316 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7317 * name it contains, otherwise return "name".
7318 */
7319 static char_u *
7320deref_func_name(name, lenp)
7321 char_u *name;
7322 int *lenp;
7323{
Bram Moolenaar33570922005-01-25 22:26:29 +00007324 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007325 int cc;
7326
7327 cc = name[*lenp];
7328 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007329 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007330 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007331 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007332 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007333 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007334 {
7335 *lenp = 0;
7336 return (char_u *)""; /* just in case */
7337 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007338 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00007339 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007340 }
7341
7342 return name;
7343}
7344
7345/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346 * Allocate a variable for the result of a function.
7347 * Return OK or FAIL.
7348 */
7349 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007350get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7351 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352 char_u *name; /* name of the function */
7353 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007354 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 char_u **arg; /* argument, pointing to the '(' */
7356 linenr_T firstline; /* first line of range */
7357 linenr_T lastline; /* last line of range */
7358 int *doesrange; /* return: function handled range */
7359 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007360 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361{
7362 char_u *argp;
7363 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007364 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 int argcount = 0; /* number of arguments found */
7366
7367 /*
7368 * Get the arguments.
7369 */
7370 argp = *arg;
7371 while (argcount < MAX_FUNC_ARGS)
7372 {
7373 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7374 if (*argp == ')' || *argp == ',' || *argp == NUL)
7375 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7377 {
7378 ret = FAIL;
7379 break;
7380 }
7381 ++argcount;
7382 if (*argp != ',')
7383 break;
7384 }
7385 if (*argp == ')')
7386 ++argp;
7387 else
7388 ret = FAIL;
7389
7390 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007391 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007392 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007394 {
7395 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007396 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007397 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007398 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007399 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400
7401 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007402 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403
7404 *arg = skipwhite(argp);
7405 return ret;
7406}
7407
7408
7409/*
7410 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00007411 * Return OK when the function can't be called, FAIL otherwise.
7412 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413 */
7414 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007415call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007416 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 char_u *name; /* name of the function */
7418 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007419 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007421 typval_T *argvars; /* vars for arguments, must have "argcount"
7422 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423 linenr_T firstline; /* first line of range */
7424 linenr_T lastline; /* last line of range */
7425 int *doesrange; /* return: function handled range */
7426 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007427 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428{
7429 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430#define ERROR_UNKNOWN 0
7431#define ERROR_TOOMANY 1
7432#define ERROR_TOOFEW 2
7433#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007434#define ERROR_DICT 4
7435#define ERROR_NONE 5
7436#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437 int error = ERROR_NONE;
7438 int i;
7439 int llen;
7440 ufunc_T *fp;
7441 int cc;
7442#define FLEN_FIXED 40
7443 char_u fname_buf[FLEN_FIXED + 1];
7444 char_u *fname;
7445
7446 /*
7447 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7448 * Change <SNR>123_name() to K_SNR 123_name().
7449 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7450 */
7451 cc = name[len];
7452 name[len] = NUL;
7453 llen = eval_fname_script(name);
7454 if (llen > 0)
7455 {
7456 fname_buf[0] = K_SPECIAL;
7457 fname_buf[1] = KS_EXTRA;
7458 fname_buf[2] = (int)KE_SNR;
7459 i = 3;
7460 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7461 {
7462 if (current_SID <= 0)
7463 error = ERROR_SCRIPT;
7464 else
7465 {
7466 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7467 i = (int)STRLEN(fname_buf);
7468 }
7469 }
7470 if (i + STRLEN(name + llen) < FLEN_FIXED)
7471 {
7472 STRCPY(fname_buf + i, name + llen);
7473 fname = fname_buf;
7474 }
7475 else
7476 {
7477 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7478 if (fname == NULL)
7479 error = ERROR_OTHER;
7480 else
7481 {
7482 mch_memmove(fname, fname_buf, (size_t)i);
7483 STRCPY(fname + i, name + llen);
7484 }
7485 }
7486 }
7487 else
7488 fname = name;
7489
7490 *doesrange = FALSE;
7491
7492
7493 /* execute the function if no errors detected and executing */
7494 if (evaluate && error == ERROR_NONE)
7495 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007496 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497 error = ERROR_UNKNOWN;
7498
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007499 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500 {
7501 /*
7502 * User defined function.
7503 */
7504 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007505
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007507 /* Trigger FuncUndefined event, may load the function. */
7508 if (fp == NULL
7509 && apply_autocmds(EVENT_FUNCUNDEFINED,
7510 fname, fname, TRUE, NULL)
7511 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007513 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514 fp = find_func(fname);
7515 }
7516#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007517 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007518 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007519 {
7520 /* loaded a package, search for the function again */
7521 fp = find_func(fname);
7522 }
7523
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524 if (fp != NULL)
7525 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007526 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007528 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007530 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007532 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007533 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534 else
7535 {
7536 /*
7537 * Call the user function.
7538 * Save and restore search patterns, script variables and
7539 * redo buffer.
7540 */
7541 save_search_patterns();
7542 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007543 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007544 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007545 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007546 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7547 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7548 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007549 /* Function was unreferenced while being used, free it
7550 * now. */
7551 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552 restoreRedobuff();
7553 restore_search_patterns();
7554 error = ERROR_NONE;
7555 }
7556 }
7557 }
7558 else
7559 {
7560 /*
7561 * Find the function name in the table, call its implementation.
7562 */
7563 i = find_internal_func(fname);
7564 if (i >= 0)
7565 {
7566 if (argcount < functions[i].f_min_argc)
7567 error = ERROR_TOOFEW;
7568 else if (argcount > functions[i].f_max_argc)
7569 error = ERROR_TOOMANY;
7570 else
7571 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007572 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007573 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 error = ERROR_NONE;
7575 }
7576 }
7577 }
7578 /*
7579 * The function call (or "FuncUndefined" autocommand sequence) might
7580 * have been aborted by an error, an interrupt, or an explicitly thrown
7581 * exception that has not been caught so far. This situation can be
7582 * tested for by calling aborting(). For an error in an internal
7583 * function or for the "E132" error in call_user_func(), however, the
7584 * throw point at which the "force_abort" flag (temporarily reset by
7585 * emsg()) is normally updated has not been reached yet. We need to
7586 * update that flag first to make aborting() reliable.
7587 */
7588 update_force_abort();
7589 }
7590 if (error == ERROR_NONE)
7591 ret = OK;
7592
7593 /*
7594 * Report an error unless the argument evaluation or function call has been
7595 * cancelled due to an aborting error, an interrupt, or an exception.
7596 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007597 if (!aborting())
7598 {
7599 switch (error)
7600 {
7601 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007602 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007603 break;
7604 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007605 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007606 break;
7607 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007608 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00007609 name);
7610 break;
7611 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007612 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00007613 name);
7614 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007615 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007616 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00007617 name);
7618 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007619 }
7620 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621
7622 name[len] = cc;
7623 if (fname != name && fname != fname_buf)
7624 vim_free(fname);
7625
7626 return ret;
7627}
7628
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007629/*
7630 * Give an error message with a function name. Handle <SNR> things.
7631 */
7632 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00007633emsg_funcname(ermsg, name)
7634 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007635 char_u *name;
7636{
7637 char_u *p;
7638
7639 if (*name == K_SPECIAL)
7640 p = concat_str((char_u *)"<SNR>", name + 3);
7641 else
7642 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00007643 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007644 if (p != name)
7645 vim_free(p);
7646}
7647
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648/*********************************************
7649 * Implementation of the built-in functions
7650 */
7651
7652/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007653 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654 */
7655 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007656f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007657 typval_T *argvars;
7658 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659{
Bram Moolenaar33570922005-01-25 22:26:29 +00007660 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007662 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007663 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007665 if ((l = argvars[0].vval.v_list) != NULL
7666 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7667 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007668 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007669 }
7670 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007671 EMSG(_(e_listreq));
7672}
7673
7674/*
7675 * "append(lnum, string/list)" function
7676 */
7677 static void
7678f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007679 typval_T *argvars;
7680 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007681{
7682 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007683 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007684 list_T *l = NULL;
7685 listitem_T *li = NULL;
7686 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007687 long added = 0;
7688
Bram Moolenaar0d660222005-01-07 21:51:51 +00007689 lnum = get_tv_lnum(argvars);
7690 if (lnum >= 0
7691 && lnum <= curbuf->b_ml.ml_line_count
7692 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007693 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007694 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007695 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007696 l = argvars[1].vval.v_list;
7697 if (l == NULL)
7698 return;
7699 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007700 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007701 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007702 for (;;)
7703 {
7704 if (l == NULL)
7705 tv = &argvars[1]; /* append a string */
7706 else if (li == NULL)
7707 break; /* end of list */
7708 else
7709 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007710 line = get_tv_string_chk(tv);
7711 if (line == NULL) /* type error */
7712 {
7713 rettv->vval.v_number = 1; /* Failed */
7714 break;
7715 }
7716 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007717 ++added;
7718 if (l == NULL)
7719 break;
7720 li = li->li_next;
7721 }
7722
7723 appended_lines_mark(lnum, added);
7724 if (curwin->w_cursor.lnum > lnum)
7725 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007727 else
7728 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729}
7730
7731/*
7732 * "argc()" function
7733 */
7734/* ARGSUSED */
7735 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007736f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007737 typval_T *argvars;
7738 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007740 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741}
7742
7743/*
7744 * "argidx()" function
7745 */
7746/* ARGSUSED */
7747 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007748f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007749 typval_T *argvars;
7750 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007752 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753}
7754
7755/*
7756 * "argv(nr)" function
7757 */
7758 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007759f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007760 typval_T *argvars;
7761 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762{
7763 int idx;
7764
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007765 if (argvars[0].v_type != VAR_UNKNOWN)
7766 {
7767 idx = get_tv_number_chk(&argvars[0], NULL);
7768 if (idx >= 0 && idx < ARGCOUNT)
7769 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
7770 else
7771 rettv->vval.v_string = NULL;
7772 rettv->v_type = VAR_STRING;
7773 }
7774 else if (rettv_list_alloc(rettv) == OK)
7775 for (idx = 0; idx < ARGCOUNT; ++idx)
7776 list_append_string(rettv->vval.v_list,
7777 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778}
7779
7780/*
7781 * "browse(save, title, initdir, default)" function
7782 */
7783/* ARGSUSED */
7784 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007785f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007786 typval_T *argvars;
7787 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788{
7789#ifdef FEAT_BROWSE
7790 int save;
7791 char_u *title;
7792 char_u *initdir;
7793 char_u *defname;
7794 char_u buf[NUMBUFLEN];
7795 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007796 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007798 save = get_tv_number_chk(&argvars[0], &error);
7799 title = get_tv_string_chk(&argvars[1]);
7800 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7801 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007803 if (error || title == NULL || initdir == NULL || defname == NULL)
7804 rettv->vval.v_string = NULL;
7805 else
7806 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007807 do_browse(save ? BROWSE_SAVE : 0,
7808 title, defname, NULL, initdir, NULL, curbuf);
7809#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007810 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007811#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007812 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007813}
7814
7815/*
7816 * "browsedir(title, initdir)" function
7817 */
7818/* ARGSUSED */
7819 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007820f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007821 typval_T *argvars;
7822 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007823{
7824#ifdef FEAT_BROWSE
7825 char_u *title;
7826 char_u *initdir;
7827 char_u buf[NUMBUFLEN];
7828
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007829 title = get_tv_string_chk(&argvars[0]);
7830 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007831
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007832 if (title == NULL || initdir == NULL)
7833 rettv->vval.v_string = NULL;
7834 else
7835 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007836 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007838 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007840 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841}
7842
Bram Moolenaar33570922005-01-25 22:26:29 +00007843static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007844
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845/*
7846 * Find a buffer by number or exact name.
7847 */
7848 static buf_T *
7849find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007850 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851{
7852 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007854 if (avar->v_type == VAR_NUMBER)
7855 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007856 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007858 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007859 if (buf == NULL)
7860 {
7861 /* No full path name match, try a match with a URL or a "nofile"
7862 * buffer, these don't use the full path. */
7863 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7864 if (buf->b_fname != NULL
7865 && (path_with_url(buf->b_fname)
7866#ifdef FEAT_QUICKFIX
7867 || bt_nofile(buf)
7868#endif
7869 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007870 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007871 break;
7872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873 }
7874 return buf;
7875}
7876
7877/*
7878 * "bufexists(expr)" function
7879 */
7880 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007881f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007882 typval_T *argvars;
7883 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007885 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886}
7887
7888/*
7889 * "buflisted(expr)" function
7890 */
7891 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007892f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007893 typval_T *argvars;
7894 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895{
7896 buf_T *buf;
7897
7898 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007899 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900}
7901
7902/*
7903 * "bufloaded(expr)" function
7904 */
7905 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007906f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007907 typval_T *argvars;
7908 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909{
7910 buf_T *buf;
7911
7912 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007913 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914}
7915
Bram Moolenaar33570922005-01-25 22:26:29 +00007916static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007917
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918/*
7919 * Get buffer by number or pattern.
7920 */
7921 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007922get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007923 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007925 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926 int save_magic;
7927 char_u *save_cpo;
7928 buf_T *buf;
7929
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007930 if (tv->v_type == VAR_NUMBER)
7931 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007932 if (tv->v_type != VAR_STRING)
7933 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934 if (name == NULL || *name == NUL)
7935 return curbuf;
7936 if (name[0] == '$' && name[1] == NUL)
7937 return lastbuf;
7938
7939 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7940 save_magic = p_magic;
7941 p_magic = TRUE;
7942 save_cpo = p_cpo;
7943 p_cpo = (char_u *)"";
7944
7945 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
7946 TRUE, FALSE));
7947
7948 p_magic = save_magic;
7949 p_cpo = save_cpo;
7950
7951 /* If not found, try expanding the name, like done for bufexists(). */
7952 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007953 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954
7955 return buf;
7956}
7957
7958/*
7959 * "bufname(expr)" function
7960 */
7961 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007962f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007963 typval_T *argvars;
7964 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965{
7966 buf_T *buf;
7967
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007968 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007970 buf = get_buf_tv(&argvars[0]);
7971 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007973 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007975 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976 --emsg_off;
7977}
7978
7979/*
7980 * "bufnr(expr)" function
7981 */
7982 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007983f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007984 typval_T *argvars;
7985 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986{
7987 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007988 int error = FALSE;
7989 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007991 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007993 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007994 --emsg_off;
7995
7996 /* If the buffer isn't found and the second argument is not zero create a
7997 * new buffer. */
7998 if (buf == NULL
7999 && argvars[1].v_type != VAR_UNKNOWN
8000 && get_tv_number_chk(&argvars[1], &error) != 0
8001 && !error
8002 && (name = get_tv_string_chk(&argvars[0])) != NULL
8003 && !error)
8004 buf = buflist_new(name, NULL, (linenr_T)1, 0);
8005
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008007 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008009 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010}
8011
8012/*
8013 * "bufwinnr(nr)" function
8014 */
8015 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008016f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008017 typval_T *argvars;
8018 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019{
8020#ifdef FEAT_WINDOWS
8021 win_T *wp;
8022 int winnr = 0;
8023#endif
8024 buf_T *buf;
8025
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008026 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008028 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029#ifdef FEAT_WINDOWS
8030 for (wp = firstwin; wp; wp = wp->w_next)
8031 {
8032 ++winnr;
8033 if (wp->w_buffer == buf)
8034 break;
8035 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008036 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008038 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039#endif
8040 --emsg_off;
8041}
8042
8043/*
8044 * "byte2line(byte)" function
8045 */
8046/*ARGSUSED*/
8047 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008048f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008049 typval_T *argvars;
8050 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051{
8052#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008053 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008054#else
8055 long boff = 0;
8056
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008057 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008059 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008061 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062 (linenr_T)0, &boff);
8063#endif
8064}
8065
8066/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008067 * "byteidx()" function
8068 */
8069/*ARGSUSED*/
8070 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008071f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008072 typval_T *argvars;
8073 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008074{
8075#ifdef FEAT_MBYTE
8076 char_u *t;
8077#endif
8078 char_u *str;
8079 long idx;
8080
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008081 str = get_tv_string_chk(&argvars[0]);
8082 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008083 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008084 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008085 return;
8086
8087#ifdef FEAT_MBYTE
8088 t = str;
8089 for ( ; idx > 0; idx--)
8090 {
8091 if (*t == NUL) /* EOL reached */
8092 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008093 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008094 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008095 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008096#else
8097 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008098 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008099#endif
8100}
8101
8102/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008103 * "call(func, arglist)" function
8104 */
8105 static void
8106f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008107 typval_T *argvars;
8108 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008109{
8110 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008111 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008112 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008113 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008114 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00008115 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008116
8117 rettv->vval.v_number = 0;
8118 if (argvars[1].v_type != VAR_LIST)
8119 {
8120 EMSG(_(e_listreq));
8121 return;
8122 }
8123 if (argvars[1].vval.v_list == NULL)
8124 return;
8125
8126 if (argvars[0].v_type == VAR_FUNC)
8127 func = argvars[0].vval.v_string;
8128 else
8129 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008130 if (*func == NUL)
8131 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008132
Bram Moolenaare9a41262005-01-15 22:18:47 +00008133 if (argvars[2].v_type != VAR_UNKNOWN)
8134 {
8135 if (argvars[2].v_type != VAR_DICT)
8136 {
8137 EMSG(_(e_dictreq));
8138 return;
8139 }
8140 selfdict = argvars[2].vval.v_dict;
8141 }
8142
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008143 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
8144 item = item->li_next)
8145 {
8146 if (argc == MAX_FUNC_ARGS)
8147 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008148 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008149 break;
8150 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008151 /* Make a copy of each argument. This is needed to be able to set
8152 * v_lock to VAR_FIXED in the copy without changing the original list.
8153 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008154 copy_tv(&item->li_tv, &argv[argc++]);
8155 }
8156
8157 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008158 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008159 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
8160 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008161
8162 /* Free the arguments. */
8163 while (argc > 0)
8164 clear_tv(&argv[--argc]);
8165}
8166
8167/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008168 * "changenr()" function
8169 */
8170/*ARGSUSED*/
8171 static void
8172f_changenr(argvars, rettv)
8173 typval_T *argvars;
8174 typval_T *rettv;
8175{
8176 rettv->vval.v_number = curbuf->b_u_seq_cur;
8177}
8178
8179/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180 * "char2nr(string)" function
8181 */
8182 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008183f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008184 typval_T *argvars;
8185 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186{
8187#ifdef FEAT_MBYTE
8188 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008189 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 else
8191#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008192 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193}
8194
8195/*
8196 * "cindent(lnum)" function
8197 */
8198 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008199f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008200 typval_T *argvars;
8201 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202{
8203#ifdef FEAT_CINDENT
8204 pos_T pos;
8205 linenr_T lnum;
8206
8207 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008208 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8210 {
8211 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008212 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213 curwin->w_cursor = pos;
8214 }
8215 else
8216#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008217 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218}
8219
8220/*
8221 * "col(string)" function
8222 */
8223 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008224f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008225 typval_T *argvars;
8226 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227{
8228 colnr_T col = 0;
8229 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008230 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008232 fp = var2fpos(&argvars[0], FALSE, &fnum);
8233 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234 {
8235 if (fp->col == MAXCOL)
8236 {
8237 /* '> can be MAXCOL, get the length of the line then */
8238 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008239 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240 else
8241 col = MAXCOL;
8242 }
8243 else
8244 {
8245 col = fp->col + 1;
8246#ifdef FEAT_VIRTUALEDIT
8247 /* col(".") when the cursor is on the NUL at the end of the line
8248 * because of "coladd" can be seen as an extra column. */
8249 if (virtual_active() && fp == &curwin->w_cursor)
8250 {
8251 char_u *p = ml_get_cursor();
8252
8253 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
8254 curwin->w_virtcol - curwin->w_cursor.coladd))
8255 {
8256# ifdef FEAT_MBYTE
8257 int l;
8258
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008259 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260 col += l;
8261# else
8262 if (*p != NUL && p[1] == NUL)
8263 ++col;
8264# endif
8265 }
8266 }
8267#endif
8268 }
8269 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008270 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271}
8272
Bram Moolenaar572cb562005-08-05 21:35:02 +00008273#if defined(FEAT_INS_EXPAND)
8274/*
Bram Moolenaarade00832006-03-10 21:46:58 +00008275 * "complete()" function
8276 */
8277/*ARGSUSED*/
8278 static void
8279f_complete(argvars, rettv)
8280 typval_T *argvars;
8281 typval_T *rettv;
8282{
8283 int startcol;
8284
8285 if ((State & INSERT) == 0)
8286 {
8287 EMSG(_("E785: complete() can only be used in Insert mode"));
8288 return;
8289 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00008290
8291 /* Check for undo allowed here, because if something was already inserted
8292 * the line was already saved for undo and this check isn't done. */
8293 if (!undo_allowed())
8294 return;
8295
Bram Moolenaarade00832006-03-10 21:46:58 +00008296 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
8297 {
8298 EMSG(_(e_invarg));
8299 return;
8300 }
8301
8302 startcol = get_tv_number_chk(&argvars[0], NULL);
8303 if (startcol <= 0)
8304 return;
8305
8306 set_completion(startcol - 1, argvars[1].vval.v_list);
8307}
8308
8309/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00008310 * "complete_add()" function
8311 */
8312/*ARGSUSED*/
8313 static void
8314f_complete_add(argvars, rettv)
8315 typval_T *argvars;
8316 typval_T *rettv;
8317{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00008318 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00008319}
8320
8321/*
8322 * "complete_check()" function
8323 */
8324/*ARGSUSED*/
8325 static void
8326f_complete_check(argvars, rettv)
8327 typval_T *argvars;
8328 typval_T *rettv;
8329{
8330 int saved = RedrawingDisabled;
8331
8332 RedrawingDisabled = 0;
8333 ins_compl_check_keys(0);
8334 rettv->vval.v_number = compl_interrupted;
8335 RedrawingDisabled = saved;
8336}
8337#endif
8338
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339/*
8340 * "confirm(message, buttons[, default [, type]])" function
8341 */
8342/*ARGSUSED*/
8343 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008344f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008345 typval_T *argvars;
8346 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347{
8348#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
8349 char_u *message;
8350 char_u *buttons = NULL;
8351 char_u buf[NUMBUFLEN];
8352 char_u buf2[NUMBUFLEN];
8353 int def = 1;
8354 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008355 char_u *typestr;
8356 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008358 message = get_tv_string_chk(&argvars[0]);
8359 if (message == NULL)
8360 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008361 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008363 buttons = get_tv_string_buf_chk(&argvars[1], buf);
8364 if (buttons == NULL)
8365 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008366 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008368 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008369 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008371 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
8372 if (typestr == NULL)
8373 error = TRUE;
8374 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008376 switch (TOUPPER_ASC(*typestr))
8377 {
8378 case 'E': type = VIM_ERROR; break;
8379 case 'Q': type = VIM_QUESTION; break;
8380 case 'I': type = VIM_INFO; break;
8381 case 'W': type = VIM_WARNING; break;
8382 case 'G': type = VIM_GENERIC; break;
8383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384 }
8385 }
8386 }
8387 }
8388
8389 if (buttons == NULL || *buttons == NUL)
8390 buttons = (char_u *)_("&Ok");
8391
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008392 if (error)
8393 rettv->vval.v_number = 0;
8394 else
8395 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396 def, NULL);
8397#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008398 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399#endif
8400}
8401
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008402/*
8403 * "copy()" function
8404 */
8405 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008406f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008407 typval_T *argvars;
8408 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008409{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008410 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008411}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412
8413/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008414 * "count()" function
8415 */
8416 static void
8417f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008418 typval_T *argvars;
8419 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008420{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008421 long n = 0;
8422 int ic = FALSE;
8423
Bram Moolenaare9a41262005-01-15 22:18:47 +00008424 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008425 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008426 listitem_T *li;
8427 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008428 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008429
Bram Moolenaare9a41262005-01-15 22:18:47 +00008430 if ((l = argvars[0].vval.v_list) != NULL)
8431 {
8432 li = l->lv_first;
8433 if (argvars[2].v_type != VAR_UNKNOWN)
8434 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008435 int error = FALSE;
8436
8437 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008438 if (argvars[3].v_type != VAR_UNKNOWN)
8439 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008440 idx = get_tv_number_chk(&argvars[3], &error);
8441 if (!error)
8442 {
8443 li = list_find(l, idx);
8444 if (li == NULL)
8445 EMSGN(_(e_listidx), idx);
8446 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008447 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008448 if (error)
8449 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008450 }
8451
8452 for ( ; li != NULL; li = li->li_next)
8453 if (tv_equal(&li->li_tv, &argvars[1], ic))
8454 ++n;
8455 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008456 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008457 else if (argvars[0].v_type == VAR_DICT)
8458 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008459 int todo;
8460 dict_T *d;
8461 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008462
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008463 if ((d = argvars[0].vval.v_dict) != NULL)
8464 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008465 int error = FALSE;
8466
Bram Moolenaare9a41262005-01-15 22:18:47 +00008467 if (argvars[2].v_type != VAR_UNKNOWN)
8468 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008469 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008470 if (argvars[3].v_type != VAR_UNKNOWN)
8471 EMSG(_(e_invarg));
8472 }
8473
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008474 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008475 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008476 {
8477 if (!HASHITEM_EMPTY(hi))
8478 {
8479 --todo;
8480 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
8481 ++n;
8482 }
8483 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008484 }
8485 }
8486 else
8487 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008488 rettv->vval.v_number = n;
8489}
8490
8491/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8493 *
8494 * Checks the existence of a cscope connection.
8495 */
8496/*ARGSUSED*/
8497 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008498f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008499 typval_T *argvars;
8500 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501{
8502#ifdef FEAT_CSCOPE
8503 int num = 0;
8504 char_u *dbpath = NULL;
8505 char_u *prepend = NULL;
8506 char_u buf[NUMBUFLEN];
8507
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008508 if (argvars[0].v_type != VAR_UNKNOWN
8509 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008511 num = (int)get_tv_number(&argvars[0]);
8512 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008513 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008514 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515 }
8516
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008517 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008519 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520#endif
8521}
8522
8523/*
8524 * "cursor(lnum, col)" function
8525 *
8526 * Moves the cursor to the specified line and column
8527 */
8528/*ARGSUSED*/
8529 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008530f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008531 typval_T *argvars;
8532 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533{
8534 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008535#ifdef FEAT_VIRTUALEDIT
8536 long coladd = 0;
8537#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538
Bram Moolenaara5525202006-03-02 22:52:09 +00008539 if (argvars[1].v_type == VAR_UNKNOWN)
8540 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008541 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00008542
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008543 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00008544 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008545 line = pos.lnum;
8546 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008547#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008548 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00008549#endif
8550 }
8551 else
8552 {
8553 line = get_tv_lnum(argvars);
8554 col = get_tv_number_chk(&argvars[1], NULL);
8555#ifdef FEAT_VIRTUALEDIT
8556 if (argvars[2].v_type != VAR_UNKNOWN)
8557 coladd = get_tv_number_chk(&argvars[2], NULL);
8558#endif
8559 }
8560 if (line < 0 || col < 0
8561#ifdef FEAT_VIRTUALEDIT
8562 || coladd < 0
8563#endif
8564 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008565 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566 if (line > 0)
8567 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568 if (col > 0)
8569 curwin->w_cursor.col = col - 1;
8570#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00008571 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008572#endif
8573
8574 /* Make sure the cursor is in a valid position. */
8575 check_cursor();
8576#ifdef FEAT_MBYTE
8577 /* Correct cursor for multi-byte character. */
8578 if (has_mbyte)
8579 mb_adjust_cursor();
8580#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008581
8582 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008583}
8584
8585/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008586 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587 */
8588 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008589f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008590 typval_T *argvars;
8591 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008593 int noref = 0;
8594
8595 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008596 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008597 if (noref < 0 || noref > 1)
8598 EMSG(_(e_invarg));
8599 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008600 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008601}
8602
8603/*
8604 * "delete()" function
8605 */
8606 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008607f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008608 typval_T *argvars;
8609 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610{
8611 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008612 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008614 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615}
8616
8617/*
8618 * "did_filetype()" function
8619 */
8620/*ARGSUSED*/
8621 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008622f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008623 typval_T *argvars;
8624 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625{
8626#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008627 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008629 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630#endif
8631}
8632
8633/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008634 * "diff_filler()" function
8635 */
8636/*ARGSUSED*/
8637 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008638f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008639 typval_T *argvars;
8640 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008641{
8642#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008643 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008644#endif
8645}
8646
8647/*
8648 * "diff_hlID()" function
8649 */
8650/*ARGSUSED*/
8651 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008652f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008653 typval_T *argvars;
8654 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008655{
8656#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008657 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008658 static linenr_T prev_lnum = 0;
8659 static int changedtick = 0;
8660 static int fnum = 0;
8661 static int change_start = 0;
8662 static int change_end = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008663 static hlf_T hlID = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008664 int filler_lines;
8665 int col;
8666
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008667 if (lnum < 0) /* ignore type error in {lnum} arg */
8668 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008669 if (lnum != prev_lnum
8670 || changedtick != curbuf->b_changedtick
8671 || fnum != curbuf->b_fnum)
8672 {
8673 /* New line, buffer, change: need to get the values. */
8674 filler_lines = diff_check(curwin, lnum);
8675 if (filler_lines < 0)
8676 {
8677 if (filler_lines == -1)
8678 {
8679 change_start = MAXCOL;
8680 change_end = -1;
8681 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8682 hlID = HLF_ADD; /* added line */
8683 else
8684 hlID = HLF_CHD; /* changed line */
8685 }
8686 else
8687 hlID = HLF_ADD; /* added line */
8688 }
8689 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008690 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008691 prev_lnum = lnum;
8692 changedtick = curbuf->b_changedtick;
8693 fnum = curbuf->b_fnum;
8694 }
8695
8696 if (hlID == HLF_CHD || hlID == HLF_TXD)
8697 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008698 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008699 if (col >= change_start && col <= change_end)
8700 hlID = HLF_TXD; /* changed text */
8701 else
8702 hlID = HLF_CHD; /* changed line */
8703 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008704 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008705#endif
8706}
8707
8708/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008709 * "empty({expr})" function
8710 */
8711 static void
8712f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008713 typval_T *argvars;
8714 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008715{
8716 int n;
8717
8718 switch (argvars[0].v_type)
8719 {
8720 case VAR_STRING:
8721 case VAR_FUNC:
8722 n = argvars[0].vval.v_string == NULL
8723 || *argvars[0].vval.v_string == NUL;
8724 break;
8725 case VAR_NUMBER:
8726 n = argvars[0].vval.v_number == 0;
8727 break;
8728 case VAR_LIST:
8729 n = argvars[0].vval.v_list == NULL
8730 || argvars[0].vval.v_list->lv_first == NULL;
8731 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008732 case VAR_DICT:
8733 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008734 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008735 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008736 default:
8737 EMSG2(_(e_intern2), "f_empty()");
8738 n = 0;
8739 }
8740
8741 rettv->vval.v_number = n;
8742}
8743
8744/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745 * "escape({string}, {chars})" function
8746 */
8747 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008748f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008749 typval_T *argvars;
8750 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751{
8752 char_u buf[NUMBUFLEN];
8753
Bram Moolenaar758711c2005-02-02 23:11:38 +00008754 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8755 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008756 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008757}
8758
8759/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008760 * "eval()" function
8761 */
8762/*ARGSUSED*/
8763 static void
8764f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008765 typval_T *argvars;
8766 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008767{
8768 char_u *s;
8769
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008770 s = get_tv_string_chk(&argvars[0]);
8771 if (s != NULL)
8772 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008773
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008774 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8775 {
8776 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008777 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008778 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008779 else if (*s != NUL)
8780 EMSG(_(e_trailing));
8781}
8782
8783/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008784 * "eventhandler()" function
8785 */
8786/*ARGSUSED*/
8787 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008788f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008789 typval_T *argvars;
8790 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008791{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008792 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793}
8794
8795/*
8796 * "executable()" function
8797 */
8798 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008799f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008800 typval_T *argvars;
8801 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008802{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008803 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804}
8805
8806/*
8807 * "exists()" function
8808 */
8809 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008810f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008811 typval_T *argvars;
8812 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813{
8814 char_u *p;
8815 char_u *name;
8816 int n = FALSE;
8817 int len = 0;
8818
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008819 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820 if (*p == '$') /* environment variable */
8821 {
8822 /* first try "normal" environment variables (fast) */
8823 if (mch_getenv(p + 1) != NULL)
8824 n = TRUE;
8825 else
8826 {
8827 /* try expanding things like $VIM and ${HOME} */
8828 p = expand_env_save(p);
8829 if (p != NULL && *p != '$')
8830 n = TRUE;
8831 vim_free(p);
8832 }
8833 }
8834 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00008835 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008836 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00008837 if (*skipwhite(p) != NUL)
8838 n = FALSE; /* trailing garbage */
8839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840 else if (*p == '*') /* internal or user defined function */
8841 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008842 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843 }
8844 else if (*p == ':')
8845 {
8846 n = cmd_exists(p + 1);
8847 }
8848 else if (*p == '#')
8849 {
8850#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00008851 if (p[1] == '#')
8852 n = autocmd_supported(p + 2);
8853 else
8854 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855#endif
8856 }
8857 else /* internal variable */
8858 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008859 char_u *tofree;
8860 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008862 /* get_name_len() takes care of expanding curly braces */
8863 name = p;
8864 len = get_name_len(&p, &tofree, TRUE, FALSE);
8865 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008867 if (tofree != NULL)
8868 name = tofree;
8869 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8870 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008872 /* handle d.key, l[idx], f(expr) */
8873 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8874 if (n)
8875 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876 }
8877 }
Bram Moolenaar79783442006-05-05 21:18:03 +00008878 if (*p != NUL)
8879 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008881 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882 }
8883
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008884 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885}
8886
8887/*
8888 * "expand()" function
8889 */
8890 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008891f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008892 typval_T *argvars;
8893 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894{
8895 char_u *s;
8896 int len;
8897 char_u *errormsg;
8898 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8899 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008900 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008902 rettv->v_type = VAR_STRING;
8903 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904 if (*s == '%' || *s == '#' || *s == '<')
8905 {
8906 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008907 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908 --emsg_off;
8909 }
8910 else
8911 {
8912 /* When the optional second argument is non-zero, don't remove matches
8913 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008914 if (argvars[1].v_type != VAR_UNKNOWN
8915 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008917 if (!error)
8918 {
8919 ExpandInit(&xpc);
8920 xpc.xp_context = EXPAND_FILES;
8921 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008922 }
8923 else
8924 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925 }
8926}
8927
8928/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008929 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00008930 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008931 */
8932 static void
8933f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008934 typval_T *argvars;
8935 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008936{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008937 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008938 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008939 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008940 list_T *l1, *l2;
8941 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008942 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008943 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008944
Bram Moolenaare9a41262005-01-15 22:18:47 +00008945 l1 = argvars[0].vval.v_list;
8946 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008947 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
8948 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008949 {
8950 if (argvars[2].v_type != VAR_UNKNOWN)
8951 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008952 before = get_tv_number_chk(&argvars[2], &error);
8953 if (error)
8954 return; /* type error; errmsg already given */
8955
Bram Moolenaar758711c2005-02-02 23:11:38 +00008956 if (before == l1->lv_len)
8957 item = NULL;
8958 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008959 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00008960 item = list_find(l1, before);
8961 if (item == NULL)
8962 {
8963 EMSGN(_(e_listidx), before);
8964 return;
8965 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008966 }
8967 }
8968 else
8969 item = NULL;
8970 list_extend(l1, l2, item);
8971
Bram Moolenaare9a41262005-01-15 22:18:47 +00008972 copy_tv(&argvars[0], rettv);
8973 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008974 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008975 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
8976 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008977 dict_T *d1, *d2;
8978 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008979 char_u *action;
8980 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00008981 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008982 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008983
8984 d1 = argvars[0].vval.v_dict;
8985 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008986 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
8987 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008988 {
8989 /* Check the third argument. */
8990 if (argvars[2].v_type != VAR_UNKNOWN)
8991 {
8992 static char *(av[]) = {"keep", "force", "error"};
8993
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008994 action = get_tv_string_chk(&argvars[2]);
8995 if (action == NULL)
8996 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008997 for (i = 0; i < 3; ++i)
8998 if (STRCMP(action, av[i]) == 0)
8999 break;
9000 if (i == 3)
9001 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009002 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009003 return;
9004 }
9005 }
9006 else
9007 action = (char_u *)"force";
9008
9009 /* Go over all entries in the second dict and add them to the
9010 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009011 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009012 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009013 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009014 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009015 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009016 --todo;
9017 di1 = dict_find(d1, hi2->hi_key, -1);
9018 if (di1 == NULL)
9019 {
9020 di1 = dictitem_copy(HI2DI(hi2));
9021 if (di1 != NULL && dict_add(d1, di1) == FAIL)
9022 dictitem_free(di1);
9023 }
9024 else if (*action == 'e')
9025 {
9026 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
9027 break;
9028 }
9029 else if (*action == 'f')
9030 {
9031 clear_tv(&di1->di_tv);
9032 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
9033 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009034 }
9035 }
9036
Bram Moolenaare9a41262005-01-15 22:18:47 +00009037 copy_tv(&argvars[0], rettv);
9038 }
9039 }
9040 else
9041 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009042}
9043
9044/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009045 * "feedkeys()" function
9046 */
9047/*ARGSUSED*/
9048 static void
9049f_feedkeys(argvars, rettv)
9050 typval_T *argvars;
9051 typval_T *rettv;
9052{
9053 int remap = TRUE;
9054 char_u *keys, *flags;
9055 char_u nbuf[NUMBUFLEN];
9056 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009057 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009058
9059 rettv->vval.v_number = 0;
9060 keys = get_tv_string(&argvars[0]);
9061 if (*keys != NUL)
9062 {
9063 if (argvars[1].v_type != VAR_UNKNOWN)
9064 {
9065 flags = get_tv_string_buf(&argvars[1], nbuf);
9066 for ( ; *flags != NUL; ++flags)
9067 {
9068 switch (*flags)
9069 {
9070 case 'n': remap = FALSE; break;
9071 case 'm': remap = TRUE; break;
9072 case 't': typed = TRUE; break;
9073 }
9074 }
9075 }
9076
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009077 /* Need to escape K_SPECIAL and CSI before putting the string in the
9078 * typeahead buffer. */
9079 keys_esc = vim_strsave_escape_csi(keys);
9080 if (keys_esc != NULL)
9081 {
9082 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009083 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009084 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009085 if (vgetc_busy)
9086 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009087 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009088 }
9089}
9090
9091/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009092 * "filereadable()" function
9093 */
9094 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009095f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009096 typval_T *argvars;
9097 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009098{
9099 FILE *fd;
9100 char_u *p;
9101 int n;
9102
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009103 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009104 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
9105 {
9106 n = TRUE;
9107 fclose(fd);
9108 }
9109 else
9110 n = FALSE;
9111
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009112 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009113}
9114
9115/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009116 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00009117 * rights to write into.
9118 */
9119 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009120f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009121 typval_T *argvars;
9122 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009123{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009124 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009125}
9126
Bram Moolenaar33570922005-01-25 22:26:29 +00009127static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009128
9129 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00009130findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00009131 typval_T *argvars;
9132 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009133 int dir;
9134{
9135#ifdef FEAT_SEARCHPATH
9136 char_u *fname;
9137 char_u *fresult = NULL;
9138 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
9139 char_u *p;
9140 char_u pathbuf[NUMBUFLEN];
9141 int count = 1;
9142 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009143 int error = FALSE;
9144#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009145
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009146 rettv->vval.v_string = NULL;
9147 rettv->v_type = VAR_STRING;
9148
9149#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009150 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009151
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009152 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009153 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009154 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
9155 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009156 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009157 else
9158 {
9159 if (*p != NUL)
9160 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009161
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009162 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009163 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009164 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009165 }
9166
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009167 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
9168 error = TRUE;
9169
9170 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009171 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009172 do
9173 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009174 if (rettv->v_type == VAR_STRING)
9175 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009176 fresult = find_file_in_path_option(first ? fname : NULL,
9177 first ? (int)STRLEN(fname) : 0,
Bram Moolenaare580b0c2006-03-21 21:33:03 +00009178 0, first, path, dir, NULL,
9179 dir ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009180 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009181
9182 if (fresult != NULL && rettv->v_type == VAR_LIST)
9183 list_append_string(rettv->vval.v_list, fresult, -1);
9184
9185 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009186 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009187
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009188 if (rettv->v_type == VAR_STRING)
9189 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009190#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009191}
9192
Bram Moolenaar33570922005-01-25 22:26:29 +00009193static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
9194static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009195
9196/*
9197 * Implementation of map() and filter().
9198 */
9199 static void
9200filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00009201 typval_T *argvars;
9202 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009203 int map;
9204{
9205 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00009206 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00009207 listitem_T *li, *nli;
9208 list_T *l = NULL;
9209 dictitem_T *di;
9210 hashtab_T *ht;
9211 hashitem_T *hi;
9212 dict_T *d = NULL;
9213 typval_T save_val;
9214 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009215 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009216 int todo;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009217 char_u *ermsg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009218 int save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009219
9220 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009221 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009222 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009223 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009224 || (map && tv_check_lock(l->lv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009225 return;
9226 }
9227 else if (argvars[0].v_type == VAR_DICT)
9228 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009229 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009230 || (map && tv_check_lock(d->dv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009231 return;
9232 }
9233 else
9234 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009235 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009236 return;
9237 }
9238
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009239 expr = get_tv_string_buf_chk(&argvars[1], buf);
9240 /* On type errors, the preceding call has already displayed an error
9241 * message. Avoid a misleading error message for an empty string that
9242 * was not passed as argument. */
9243 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009244 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009245 prepare_vimvar(VV_VAL, &save_val);
9246 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009247
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009248 /* We reset "did_emsg" to be able to detect whether an error
9249 * occurred during evaluation of the expression. */
9250 save_did_emsg = did_emsg;
9251 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009252
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009253 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009254 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009255 prepare_vimvar(VV_KEY, &save_key);
9256 vimvars[VV_KEY].vv_type = VAR_STRING;
9257
9258 ht = &d->dv_hashtab;
9259 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009260 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009261 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009262 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009263 if (!HASHITEM_EMPTY(hi))
9264 {
9265 --todo;
9266 di = HI2DI(hi);
Bram Moolenaar89d40322006-08-29 15:30:07 +00009267 if (tv_check_lock(di->di_tv.v_lock, ermsg))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009268 break;
9269 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009270 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009271 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009272 break;
9273 if (!map && rem)
9274 dictitem_remove(d, di);
9275 clear_tv(&vimvars[VV_KEY].vv_tv);
9276 }
9277 }
9278 hash_unlock(ht);
9279
9280 restore_vimvar(VV_KEY, &save_key);
9281 }
9282 else
9283 {
9284 for (li = l->lv_first; li != NULL; li = nli)
9285 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009286 if (tv_check_lock(li->li_tv.v_lock, ermsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009287 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009288 nli = li->li_next;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009289 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009290 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009291 break;
9292 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009293 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009294 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009295 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009296
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009297 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009298
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009299 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009300 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009301
9302 copy_tv(&argvars[0], rettv);
9303}
9304
9305 static int
9306filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009307 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009308 char_u *expr;
9309 int map;
9310 int *remp;
9311{
Bram Moolenaar33570922005-01-25 22:26:29 +00009312 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009313 char_u *s;
9314
Bram Moolenaar33570922005-01-25 22:26:29 +00009315 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009316 s = expr;
9317 if (eval1(&s, &rettv, TRUE) == FAIL)
9318 return FAIL;
9319 if (*s != NUL) /* check for trailing chars after expr */
9320 {
9321 EMSG2(_(e_invexpr2), s);
9322 return FAIL;
9323 }
9324 if (map)
9325 {
9326 /* map(): replace the list item value */
9327 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +00009328 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009329 *tv = rettv;
9330 }
9331 else
9332 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009333 int error = FALSE;
9334
Bram Moolenaare9a41262005-01-15 22:18:47 +00009335 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009336 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009337 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009338 /* On type error, nothing has been removed; return FAIL to stop the
9339 * loop. The error message was given by get_tv_number_chk(). */
9340 if (error)
9341 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009342 }
Bram Moolenaar33570922005-01-25 22:26:29 +00009343 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009344 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009345}
9346
9347/*
9348 * "filter()" function
9349 */
9350 static void
9351f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009352 typval_T *argvars;
9353 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009354{
9355 filter_map(argvars, rettv, FALSE);
9356}
9357
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009358/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009359 * "finddir({fname}[, {path}[, {count}]])" function
9360 */
9361 static void
9362f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009363 typval_T *argvars;
9364 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009365{
9366 findfilendir(argvars, rettv, TRUE);
9367}
9368
9369/*
9370 * "findfile({fname}[, {path}[, {count}]])" function
9371 */
9372 static void
9373f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009374 typval_T *argvars;
9375 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009376{
9377 findfilendir(argvars, rettv, FALSE);
9378}
9379
9380/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009381 * "fnamemodify({fname}, {mods})" function
9382 */
9383 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009384f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009385 typval_T *argvars;
9386 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009387{
9388 char_u *fname;
9389 char_u *mods;
9390 int usedlen = 0;
9391 int len;
9392 char_u *fbuf = NULL;
9393 char_u buf[NUMBUFLEN];
9394
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009395 fname = get_tv_string_chk(&argvars[0]);
9396 mods = get_tv_string_buf_chk(&argvars[1], buf);
9397 if (fname == NULL || mods == NULL)
9398 fname = NULL;
9399 else
9400 {
9401 len = (int)STRLEN(fname);
9402 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
9403 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009404
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009405 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009406 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009407 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009408 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009409 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009410 vim_free(fbuf);
9411}
9412
Bram Moolenaar33570922005-01-25 22:26:29 +00009413static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009414
9415/*
9416 * "foldclosed()" function
9417 */
9418 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009419foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00009420 typval_T *argvars;
9421 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009422 int end;
9423{
9424#ifdef FEAT_FOLDING
9425 linenr_T lnum;
9426 linenr_T first, last;
9427
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009428 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009429 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9430 {
9431 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
9432 {
9433 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009434 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009435 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009436 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009437 return;
9438 }
9439 }
9440#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009441 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009442}
9443
9444/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009445 * "foldclosed()" function
9446 */
9447 static void
9448f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009449 typval_T *argvars;
9450 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009451{
9452 foldclosed_both(argvars, rettv, FALSE);
9453}
9454
9455/*
9456 * "foldclosedend()" function
9457 */
9458 static void
9459f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009460 typval_T *argvars;
9461 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009462{
9463 foldclosed_both(argvars, rettv, TRUE);
9464}
9465
9466/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009467 * "foldlevel()" function
9468 */
9469 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009470f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009471 typval_T *argvars;
9472 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009473{
9474#ifdef FEAT_FOLDING
9475 linenr_T lnum;
9476
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009477 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009478 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009479 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009480 else
9481#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009482 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483}
9484
9485/*
9486 * "foldtext()" function
9487 */
9488/*ARGSUSED*/
9489 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009490f_foldtext(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#ifdef FEAT_FOLDING
9495 linenr_T lnum;
9496 char_u *s;
9497 char_u *r;
9498 int len;
9499 char *txt;
9500#endif
9501
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009502 rettv->v_type = VAR_STRING;
9503 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009504#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00009505 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
9506 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
9507 <= curbuf->b_ml.ml_line_count
9508 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009509 {
9510 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009511 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
9512 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009513 {
9514 if (!linewhite(lnum))
9515 break;
9516 ++lnum;
9517 }
9518
9519 /* Find interesting text in this line. */
9520 s = skipwhite(ml_get(lnum));
9521 /* skip C comment-start */
9522 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009523 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009525 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00009526 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009527 {
9528 s = skipwhite(ml_get(lnum + 1));
9529 if (*s == '*')
9530 s = skipwhite(s + 1);
9531 }
9532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009533 txt = _("+-%s%3ld lines: ");
9534 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009535 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009536 + 20 /* for %3ld */
9537 + STRLEN(s))); /* concatenated */
9538 if (r != NULL)
9539 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009540 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
9541 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
9542 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009543 len = (int)STRLEN(r);
9544 STRCAT(r, s);
9545 /* remove 'foldmarker' and 'commentstring' */
9546 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009547 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009548 }
9549 }
9550#endif
9551}
9552
9553/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009554 * "foldtextresult(lnum)" function
9555 */
9556/*ARGSUSED*/
9557 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009558f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009559 typval_T *argvars;
9560 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009561{
9562#ifdef FEAT_FOLDING
9563 linenr_T lnum;
9564 char_u *text;
9565 char_u buf[51];
9566 foldinfo_T foldinfo;
9567 int fold_count;
9568#endif
9569
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009570 rettv->v_type = VAR_STRING;
9571 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009572#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009573 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009574 /* treat illegal types and illegal string values for {lnum} the same */
9575 if (lnum < 0)
9576 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009577 fold_count = foldedCount(curwin, lnum, &foldinfo);
9578 if (fold_count > 0)
9579 {
9580 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
9581 &foldinfo, buf);
9582 if (text == buf)
9583 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009584 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009585 }
9586#endif
9587}
9588
9589/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009590 * "foreground()" function
9591 */
9592/*ARGSUSED*/
9593 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009594f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009595 typval_T *argvars;
9596 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009597{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009598 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599#ifdef FEAT_GUI
9600 if (gui.in_use)
9601 gui_mch_set_foreground();
9602#else
9603# ifdef WIN32
9604 win32_set_foreground();
9605# endif
9606#endif
9607}
9608
9609/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009610 * "function()" function
9611 */
9612/*ARGSUSED*/
9613 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009614f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009615 typval_T *argvars;
9616 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009617{
9618 char_u *s;
9619
Bram Moolenaara7043832005-01-21 11:56:39 +00009620 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009621 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009622 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009623 EMSG2(_(e_invarg2), s);
9624 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009625 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009626 else
9627 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009628 rettv->vval.v_string = vim_strsave(s);
9629 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009630 }
9631}
9632
9633/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009634 * "garbagecollect()" function
9635 */
9636/*ARGSUSED*/
9637 static void
9638f_garbagecollect(argvars, rettv)
9639 typval_T *argvars;
9640 typval_T *rettv;
9641{
Bram Moolenaar9fecb462006-09-05 10:59:47 +00009642 /* This is postponed until we are back at the toplevel, because we may be
9643 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
9644 want_garbage_collect = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009645}
9646
9647/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009648 * "get()" function
9649 */
9650 static void
9651f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009652 typval_T *argvars;
9653 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009654{
Bram Moolenaar33570922005-01-25 22:26:29 +00009655 listitem_T *li;
9656 list_T *l;
9657 dictitem_T *di;
9658 dict_T *d;
9659 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009660
Bram Moolenaare9a41262005-01-15 22:18:47 +00009661 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009662 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009663 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009664 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009665 int error = FALSE;
9666
9667 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9668 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009669 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009670 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009671 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009672 else if (argvars[0].v_type == VAR_DICT)
9673 {
9674 if ((d = argvars[0].vval.v_dict) != NULL)
9675 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009676 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009677 if (di != NULL)
9678 tv = &di->di_tv;
9679 }
9680 }
9681 else
9682 EMSG2(_(e_listdictarg), "get()");
9683
9684 if (tv == NULL)
9685 {
9686 if (argvars[2].v_type == VAR_UNKNOWN)
9687 rettv->vval.v_number = 0;
9688 else
9689 copy_tv(&argvars[2], rettv);
9690 }
9691 else
9692 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009693}
9694
Bram Moolenaar342337a2005-07-21 21:11:17 +00009695static 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 +00009696
9697/*
9698 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +00009699 * Return a range (from start to end) of lines in rettv from the specified
9700 * buffer.
9701 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009702 */
9703 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +00009704get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009705 buf_T *buf;
9706 linenr_T start;
9707 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009708 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009709 typval_T *rettv;
9710{
9711 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009712
Bram Moolenaar342337a2005-07-21 21:11:17 +00009713 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009714 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009715 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009716 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009717 }
9718 else
9719 rettv->vval.v_number = 0;
9720
9721 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
9722 return;
9723
9724 if (!retlist)
9725 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009726 if (start >= 1 && start <= buf->b_ml.ml_line_count)
9727 p = ml_get_buf(buf, start, FALSE);
9728 else
9729 p = (char_u *)"";
9730
9731 rettv->v_type = VAR_STRING;
9732 rettv->vval.v_string = vim_strsave(p);
9733 }
9734 else
9735 {
9736 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009737 return;
9738
9739 if (start < 1)
9740 start = 1;
9741 if (end > buf->b_ml.ml_line_count)
9742 end = buf->b_ml.ml_line_count;
9743 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009744 if (list_append_string(rettv->vval.v_list,
9745 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009746 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009747 }
9748}
9749
9750/*
9751 * "getbufline()" function
9752 */
9753 static void
9754f_getbufline(argvars, rettv)
9755 typval_T *argvars;
9756 typval_T *rettv;
9757{
9758 linenr_T lnum;
9759 linenr_T end;
9760 buf_T *buf;
9761
9762 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9763 ++emsg_off;
9764 buf = get_buf_tv(&argvars[0]);
9765 --emsg_off;
9766
Bram Moolenaar661b1822005-07-28 22:36:45 +00009767 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009768 if (argvars[2].v_type == VAR_UNKNOWN)
9769 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009770 else
Bram Moolenaar661b1822005-07-28 22:36:45 +00009771 end = get_tv_lnum_buf(&argvars[2], buf);
9772
Bram Moolenaar342337a2005-07-21 21:11:17 +00009773 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009774}
9775
Bram Moolenaar0d660222005-01-07 21:51:51 +00009776/*
9777 * "getbufvar()" function
9778 */
9779 static void
9780f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009781 typval_T *argvars;
9782 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009783{
9784 buf_T *buf;
9785 buf_T *save_curbuf;
9786 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009787 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009788
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009789 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9790 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009791 ++emsg_off;
9792 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009793
9794 rettv->v_type = VAR_STRING;
9795 rettv->vval.v_string = NULL;
9796
9797 if (buf != NULL && varname != NULL)
9798 {
9799 if (*varname == '&') /* buffer-local-option */
9800 {
9801 /* set curbuf to be our buf, temporarily */
9802 save_curbuf = curbuf;
9803 curbuf = buf;
9804
9805 get_option_tv(&varname, rettv, TRUE);
9806
9807 /* restore previous notion of curbuf */
9808 curbuf = save_curbuf;
9809 }
9810 else
9811 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009812 if (*varname == NUL)
9813 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9814 * scope prefix before the NUL byte is required by
9815 * find_var_in_ht(). */
9816 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009817 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009818 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009819 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009820 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009821 }
9822 }
9823
9824 --emsg_off;
9825}
9826
9827/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009828 * "getchar()" function
9829 */
9830 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009831f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009832 typval_T *argvars;
9833 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009834{
9835 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009836 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009837
Bram Moolenaar4015b2c2006-06-22 19:01:34 +00009838 /* Position the cursor. Needed after a message that ends in a space. */
9839 windgoto(msg_row, msg_col);
9840
Bram Moolenaar071d4272004-06-13 20:20:40 +00009841 ++no_mapping;
9842 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009843 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009844 /* getchar(): blocking wait. */
9845 n = safe_vgetc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009846 else if (get_tv_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009847 /* getchar(1): only check if char avail */
9848 n = vpeekc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009849 else if (error || vpeekc() == NUL)
9850 /* illegal argument or getchar(0) and no char avail: return zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009851 n = 0;
9852 else
9853 /* getchar(0) and char avail: return char */
9854 n = safe_vgetc();
9855 --no_mapping;
9856 --allow_keys;
9857
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009858 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009859 if (IS_SPECIAL(n) || mod_mask != 0)
9860 {
9861 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9862 int i = 0;
9863
9864 /* Turn a special key into three bytes, plus modifier. */
9865 if (mod_mask != 0)
9866 {
9867 temp[i++] = K_SPECIAL;
9868 temp[i++] = KS_MODIFIER;
9869 temp[i++] = mod_mask;
9870 }
9871 if (IS_SPECIAL(n))
9872 {
9873 temp[i++] = K_SPECIAL;
9874 temp[i++] = K_SECOND(n);
9875 temp[i++] = K_THIRD(n);
9876 }
9877#ifdef FEAT_MBYTE
9878 else if (has_mbyte)
9879 i += (*mb_char2bytes)(n, temp + i);
9880#endif
9881 else
9882 temp[i++] = n;
9883 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009884 rettv->v_type = VAR_STRING;
9885 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009886 }
9887}
9888
9889/*
9890 * "getcharmod()" function
9891 */
9892/*ARGSUSED*/
9893 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009894f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009895 typval_T *argvars;
9896 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009897{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009898 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009899}
9900
9901/*
9902 * "getcmdline()" function
9903 */
9904/*ARGSUSED*/
9905 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009906f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009907 typval_T *argvars;
9908 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009909{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009910 rettv->v_type = VAR_STRING;
9911 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009912}
9913
9914/*
9915 * "getcmdpos()" function
9916 */
9917/*ARGSUSED*/
9918 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009919f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009920 typval_T *argvars;
9921 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009922{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009923 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924}
9925
9926/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00009927 * "getcmdtype()" function
9928 */
9929/*ARGSUSED*/
9930 static void
9931f_getcmdtype(argvars, rettv)
9932 typval_T *argvars;
9933 typval_T *rettv;
9934{
9935 rettv->v_type = VAR_STRING;
9936 rettv->vval.v_string = alloc(2);
9937 if (rettv->vval.v_string != NULL)
9938 {
9939 rettv->vval.v_string[0] = get_cmdline_type();
9940 rettv->vval.v_string[1] = NUL;
9941 }
9942}
9943
9944/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009945 * "getcwd()" function
9946 */
9947/*ARGSUSED*/
9948 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009949f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009950 typval_T *argvars;
9951 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009952{
9953 char_u cwd[MAXPATHL];
9954
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009955 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009956 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009957 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009958 else
9959 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009960 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009961#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +00009962 if (rettv->vval.v_string != NULL)
9963 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009964#endif
9965 }
9966}
9967
9968/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009969 * "getfontname()" function
9970 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009971/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009972 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009973f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009974 typval_T *argvars;
9975 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009976{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009977 rettv->v_type = VAR_STRING;
9978 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009979#ifdef FEAT_GUI
9980 if (gui.in_use)
9981 {
9982 GuiFont font;
9983 char_u *name = NULL;
9984
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009985 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009986 {
9987 /* Get the "Normal" font. Either the name saved by
9988 * hl_set_font_name() or from the font ID. */
9989 font = gui.norm_font;
9990 name = hl_get_font_name();
9991 }
9992 else
9993 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009994 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009995 if (STRCMP(name, "*") == 0) /* don't use font dialog */
9996 return;
9997 font = gui_mch_get_font(name, FALSE);
9998 if (font == NOFONT)
9999 return; /* Invalid font name, return empty string. */
10000 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010001 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010002 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010003 gui_mch_free_font(font);
10004 }
10005#endif
10006}
10007
10008/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010009 * "getfperm({fname})" function
10010 */
10011 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010012f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010013 typval_T *argvars;
10014 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010015{
10016 char_u *fname;
10017 struct stat st;
10018 char_u *perm = NULL;
10019 char_u flags[] = "rwx";
10020 int i;
10021
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010022 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010023
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010024 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010025 if (mch_stat((char *)fname, &st) >= 0)
10026 {
10027 perm = vim_strsave((char_u *)"---------");
10028 if (perm != NULL)
10029 {
10030 for (i = 0; i < 9; i++)
10031 {
10032 if (st.st_mode & (1 << (8 - i)))
10033 perm[i] = flags[i % 3];
10034 }
10035 }
10036 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010037 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010038}
10039
10040/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010041 * "getfsize({fname})" function
10042 */
10043 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010044f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010045 typval_T *argvars;
10046 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010047{
10048 char_u *fname;
10049 struct stat st;
10050
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010051 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010052
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010053 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010054
10055 if (mch_stat((char *)fname, &st) >= 0)
10056 {
10057 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010058 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010059 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010060 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010061 }
10062 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010063 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010064}
10065
10066/*
10067 * "getftime({fname})" function
10068 */
10069 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010070f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010071 typval_T *argvars;
10072 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010073{
10074 char_u *fname;
10075 struct stat st;
10076
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010077 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010078
10079 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010080 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010081 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010082 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010083}
10084
10085/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010086 * "getftype({fname})" function
10087 */
10088 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010089f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010090 typval_T *argvars;
10091 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010092{
10093 char_u *fname;
10094 struct stat st;
10095 char_u *type = NULL;
10096 char *t;
10097
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010098 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010099
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010100 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010101 if (mch_lstat((char *)fname, &st) >= 0)
10102 {
10103#ifdef S_ISREG
10104 if (S_ISREG(st.st_mode))
10105 t = "file";
10106 else if (S_ISDIR(st.st_mode))
10107 t = "dir";
10108# ifdef S_ISLNK
10109 else if (S_ISLNK(st.st_mode))
10110 t = "link";
10111# endif
10112# ifdef S_ISBLK
10113 else if (S_ISBLK(st.st_mode))
10114 t = "bdev";
10115# endif
10116# ifdef S_ISCHR
10117 else if (S_ISCHR(st.st_mode))
10118 t = "cdev";
10119# endif
10120# ifdef S_ISFIFO
10121 else if (S_ISFIFO(st.st_mode))
10122 t = "fifo";
10123# endif
10124# ifdef S_ISSOCK
10125 else if (S_ISSOCK(st.st_mode))
10126 t = "fifo";
10127# endif
10128 else
10129 t = "other";
10130#else
10131# ifdef S_IFMT
10132 switch (st.st_mode & S_IFMT)
10133 {
10134 case S_IFREG: t = "file"; break;
10135 case S_IFDIR: t = "dir"; break;
10136# ifdef S_IFLNK
10137 case S_IFLNK: t = "link"; break;
10138# endif
10139# ifdef S_IFBLK
10140 case S_IFBLK: t = "bdev"; break;
10141# endif
10142# ifdef S_IFCHR
10143 case S_IFCHR: t = "cdev"; break;
10144# endif
10145# ifdef S_IFIFO
10146 case S_IFIFO: t = "fifo"; break;
10147# endif
10148# ifdef S_IFSOCK
10149 case S_IFSOCK: t = "socket"; break;
10150# endif
10151 default: t = "other";
10152 }
10153# else
10154 if (mch_isdir(fname))
10155 t = "dir";
10156 else
10157 t = "file";
10158# endif
10159#endif
10160 type = vim_strsave((char_u *)t);
10161 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010162 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010163}
10164
10165/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010166 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000010167 */
10168 static void
10169f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010170 typval_T *argvars;
10171 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010172{
10173 linenr_T lnum;
10174 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010175 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010176
10177 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010178 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010179 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010180 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010181 retlist = FALSE;
10182 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010183 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000010184 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000010185 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010186 retlist = TRUE;
10187 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010188
Bram Moolenaar342337a2005-07-21 21:11:17 +000010189 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010190}
10191
10192/*
Bram Moolenaara5525202006-03-02 22:52:09 +000010193 * "getpos(string)" function
10194 */
10195 static void
10196f_getpos(argvars, rettv)
10197 typval_T *argvars;
10198 typval_T *rettv;
10199{
10200 pos_T *fp;
10201 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010202 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010203
10204 if (rettv_list_alloc(rettv) == OK)
10205 {
10206 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010207 fp = var2fpos(&argvars[0], TRUE, &fnum);
10208 if (fnum != -1)
10209 list_append_number(l, (varnumber_T)fnum);
10210 else
10211 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000010212 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
10213 : (varnumber_T)0);
10214 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->col + 1
10215 : (varnumber_T)0);
10216 list_append_number(l,
10217#ifdef FEAT_VIRTUALEDIT
10218 (fp != NULL) ? (varnumber_T)fp->coladd :
10219#endif
10220 (varnumber_T)0);
10221 }
10222 else
10223 rettv->vval.v_number = FALSE;
10224}
10225
10226/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000010227 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000010228 */
Bram Moolenaar280f1262006-01-30 00:14:18 +000010229/*ARGSUSED*/
Bram Moolenaar2641f772005-03-25 21:58:17 +000010230 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000010231f_getqflist(argvars, rettv)
10232 typval_T *argvars;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010233 typval_T *rettv;
10234{
10235#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000010236 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010237#endif
10238
10239 rettv->vval.v_number = FALSE;
10240#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010241 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000010242 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000010243 wp = NULL;
10244 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
10245 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010246 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010247 if (wp == NULL)
10248 return;
10249 }
10250
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010251 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000010252 }
10253#endif
10254}
10255
10256/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010257 * "getreg()" function
10258 */
10259 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010260f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010261 typval_T *argvars;
10262 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010263{
10264 char_u *strregname;
10265 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010266 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010267 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010268
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010269 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010270 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010271 strregname = get_tv_string_chk(&argvars[0]);
10272 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010273 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010274 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010276 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010277 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010278 regname = (strregname == NULL ? '"' : *strregname);
10279 if (regname == 0)
10280 regname = '"';
10281
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010282 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010283 rettv->vval.v_string = error ? NULL :
10284 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010285}
10286
10287/*
10288 * "getregtype()" function
10289 */
10290 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010291f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010292 typval_T *argvars;
10293 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010294{
10295 char_u *strregname;
10296 int regname;
10297 char_u buf[NUMBUFLEN + 2];
10298 long reglen = 0;
10299
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010300 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010301 {
10302 strregname = get_tv_string_chk(&argvars[0]);
10303 if (strregname == NULL) /* type error; errmsg already given */
10304 {
10305 rettv->v_type = VAR_STRING;
10306 rettv->vval.v_string = NULL;
10307 return;
10308 }
10309 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010310 else
10311 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010312 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010313
10314 regname = (strregname == NULL ? '"' : *strregname);
10315 if (regname == 0)
10316 regname = '"';
10317
10318 buf[0] = NUL;
10319 buf[1] = NUL;
10320 switch (get_reg_type(regname, &reglen))
10321 {
10322 case MLINE: buf[0] = 'V'; break;
10323 case MCHAR: buf[0] = 'v'; break;
10324#ifdef FEAT_VISUAL
10325 case MBLOCK:
10326 buf[0] = Ctrl_V;
10327 sprintf((char *)buf + 1, "%ld", reglen + 1);
10328 break;
10329#endif
10330 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010331 rettv->v_type = VAR_STRING;
10332 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010333}
10334
10335/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010336 * "gettabwinvar()" function
10337 */
10338 static void
10339f_gettabwinvar(argvars, rettv)
10340 typval_T *argvars;
10341 typval_T *rettv;
10342{
10343 getwinvar(argvars, rettv, 1);
10344}
10345
10346/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010347 * "getwinposx()" function
10348 */
10349/*ARGSUSED*/
10350 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010351f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010352 typval_T *argvars;
10353 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010354{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010355 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010356#ifdef FEAT_GUI
10357 if (gui.in_use)
10358 {
10359 int x, y;
10360
10361 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010362 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010363 }
10364#endif
10365}
10366
10367/*
10368 * "getwinposy()" function
10369 */
10370/*ARGSUSED*/
10371 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010372f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010373 typval_T *argvars;
10374 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010375{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010376 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010377#ifdef FEAT_GUI
10378 if (gui.in_use)
10379 {
10380 int x, y;
10381
10382 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010383 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010384 }
10385#endif
10386}
10387
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010388/*
10389 * Find window specifed by "vp" in tabpage "tp".
10390 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000010391 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010392find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000010393 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010394 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000010395{
10396#ifdef FEAT_WINDOWS
10397 win_T *wp;
10398#endif
10399 int nr;
10400
10401 nr = get_tv_number_chk(vp, NULL);
10402
10403#ifdef FEAT_WINDOWS
10404 if (nr < 0)
10405 return NULL;
10406 if (nr == 0)
10407 return curwin;
10408
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010409 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
10410 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000010411 if (--nr <= 0)
10412 break;
10413 return wp;
10414#else
10415 if (nr == 0 || nr == 1)
10416 return curwin;
10417 return NULL;
10418#endif
10419}
10420
Bram Moolenaar071d4272004-06-13 20:20:40 +000010421/*
10422 * "getwinvar()" function
10423 */
10424 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010425f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010426 typval_T *argvars;
10427 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010428{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010429 getwinvar(argvars, rettv, 0);
10430}
10431
10432/*
10433 * getwinvar() and gettabwinvar()
10434 */
10435 static void
10436getwinvar(argvars, rettv, off)
10437 typval_T *argvars;
10438 typval_T *rettv;
10439 int off; /* 1 for gettabwinvar() */
10440{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010441 win_T *win, *oldcurwin;
10442 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010443 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010444 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010445
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010446#ifdef FEAT_WINDOWS
10447 if (off == 1)
10448 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
10449 else
10450 tp = curtab;
10451#endif
10452 win = find_win_by_nr(&argvars[off], tp);
10453 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010454 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010455
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010456 rettv->v_type = VAR_STRING;
10457 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010458
10459 if (win != NULL && varname != NULL)
10460 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000010461 /* Set curwin to be our win, temporarily. Also set curbuf, so
10462 * that we can get buffer-local options. */
10463 oldcurwin = curwin;
10464 curwin = win;
10465 curbuf = win->w_buffer;
10466
Bram Moolenaar071d4272004-06-13 20:20:40 +000010467 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010468 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010469 else
10470 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010471 if (*varname == NUL)
10472 /* let getwinvar({nr}, "") return the "w:" dictionary. The
10473 * scope prefix before the NUL byte is required by
10474 * find_var_in_ht(). */
10475 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010476 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010477 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010478 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010479 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010480 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000010481
10482 /* restore previous notion of curwin */
10483 curwin = oldcurwin;
10484 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010485 }
10486
10487 --emsg_off;
10488}
10489
10490/*
10491 * "glob()" function
10492 */
10493 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010494f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010495 typval_T *argvars;
10496 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010497{
10498 expand_T xpc;
10499
10500 ExpandInit(&xpc);
10501 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010502 rettv->v_type = VAR_STRING;
10503 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010504 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010505}
10506
10507/*
10508 * "globpath()" function
10509 */
10510 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010511f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010512 typval_T *argvars;
10513 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010514{
10515 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010516 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010517
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010518 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010519 if (file == NULL)
10520 rettv->vval.v_string = NULL;
10521 else
10522 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010523}
10524
10525/*
10526 * "has()" function
10527 */
10528 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010529f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010530 typval_T *argvars;
10531 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010532{
10533 int i;
10534 char_u *name;
10535 int n = FALSE;
10536 static char *(has_list[]) =
10537 {
10538#ifdef AMIGA
10539 "amiga",
10540# ifdef FEAT_ARP
10541 "arp",
10542# endif
10543#endif
10544#ifdef __BEOS__
10545 "beos",
10546#endif
10547#ifdef MSDOS
10548# ifdef DJGPP
10549 "dos32",
10550# else
10551 "dos16",
10552# endif
10553#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000010554#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010555 "mac",
10556#endif
10557#if defined(MACOS_X_UNIX)
10558 "macunix",
10559#endif
10560#ifdef OS2
10561 "os2",
10562#endif
10563#ifdef __QNX__
10564 "qnx",
10565#endif
10566#ifdef RISCOS
10567 "riscos",
10568#endif
10569#ifdef UNIX
10570 "unix",
10571#endif
10572#ifdef VMS
10573 "vms",
10574#endif
10575#ifdef WIN16
10576 "win16",
10577#endif
10578#ifdef WIN32
10579 "win32",
10580#endif
10581#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
10582 "win32unix",
10583#endif
10584#ifdef WIN64
10585 "win64",
10586#endif
10587#ifdef EBCDIC
10588 "ebcdic",
10589#endif
10590#ifndef CASE_INSENSITIVE_FILENAME
10591 "fname_case",
10592#endif
10593#ifdef FEAT_ARABIC
10594 "arabic",
10595#endif
10596#ifdef FEAT_AUTOCMD
10597 "autocmd",
10598#endif
10599#ifdef FEAT_BEVAL
10600 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000010601# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
10602 "balloon_multiline",
10603# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010604#endif
10605#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
10606 "builtin_terms",
10607# ifdef ALL_BUILTIN_TCAPS
10608 "all_builtin_terms",
10609# endif
10610#endif
10611#ifdef FEAT_BYTEOFF
10612 "byte_offset",
10613#endif
10614#ifdef FEAT_CINDENT
10615 "cindent",
10616#endif
10617#ifdef FEAT_CLIENTSERVER
10618 "clientserver",
10619#endif
10620#ifdef FEAT_CLIPBOARD
10621 "clipboard",
10622#endif
10623#ifdef FEAT_CMDL_COMPL
10624 "cmdline_compl",
10625#endif
10626#ifdef FEAT_CMDHIST
10627 "cmdline_hist",
10628#endif
10629#ifdef FEAT_COMMENTS
10630 "comments",
10631#endif
10632#ifdef FEAT_CRYPT
10633 "cryptv",
10634#endif
10635#ifdef FEAT_CSCOPE
10636 "cscope",
10637#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010638#ifdef CURSOR_SHAPE
10639 "cursorshape",
10640#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010641#ifdef DEBUG
10642 "debug",
10643#endif
10644#ifdef FEAT_CON_DIALOG
10645 "dialog_con",
10646#endif
10647#ifdef FEAT_GUI_DIALOG
10648 "dialog_gui",
10649#endif
10650#ifdef FEAT_DIFF
10651 "diff",
10652#endif
10653#ifdef FEAT_DIGRAPHS
10654 "digraphs",
10655#endif
10656#ifdef FEAT_DND
10657 "dnd",
10658#endif
10659#ifdef FEAT_EMACS_TAGS
10660 "emacs_tags",
10661#endif
10662 "eval", /* always present, of course! */
10663#ifdef FEAT_EX_EXTRA
10664 "ex_extra",
10665#endif
10666#ifdef FEAT_SEARCH_EXTRA
10667 "extra_search",
10668#endif
10669#ifdef FEAT_FKMAP
10670 "farsi",
10671#endif
10672#ifdef FEAT_SEARCHPATH
10673 "file_in_path",
10674#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010675#if defined(UNIX) && !defined(USE_SYSTEM)
10676 "filterpipe",
10677#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010678#ifdef FEAT_FIND_ID
10679 "find_in_path",
10680#endif
10681#ifdef FEAT_FOLDING
10682 "folding",
10683#endif
10684#ifdef FEAT_FOOTER
10685 "footer",
10686#endif
10687#if !defined(USE_SYSTEM) && defined(UNIX)
10688 "fork",
10689#endif
10690#ifdef FEAT_GETTEXT
10691 "gettext",
10692#endif
10693#ifdef FEAT_GUI
10694 "gui",
10695#endif
10696#ifdef FEAT_GUI_ATHENA
10697# ifdef FEAT_GUI_NEXTAW
10698 "gui_neXtaw",
10699# else
10700 "gui_athena",
10701# endif
10702#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010703#ifdef FEAT_GUI_GTK
10704 "gui_gtk",
10705# ifdef HAVE_GTK2
10706 "gui_gtk2",
10707# endif
10708#endif
10709#ifdef FEAT_GUI_MAC
10710 "gui_mac",
10711#endif
10712#ifdef FEAT_GUI_MOTIF
10713 "gui_motif",
10714#endif
10715#ifdef FEAT_GUI_PHOTON
10716 "gui_photon",
10717#endif
10718#ifdef FEAT_GUI_W16
10719 "gui_win16",
10720#endif
10721#ifdef FEAT_GUI_W32
10722 "gui_win32",
10723#endif
10724#ifdef FEAT_HANGULIN
10725 "hangul_input",
10726#endif
10727#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10728 "iconv",
10729#endif
10730#ifdef FEAT_INS_EXPAND
10731 "insert_expand",
10732#endif
10733#ifdef FEAT_JUMPLIST
10734 "jumplist",
10735#endif
10736#ifdef FEAT_KEYMAP
10737 "keymap",
10738#endif
10739#ifdef FEAT_LANGMAP
10740 "langmap",
10741#endif
10742#ifdef FEAT_LIBCALL
10743 "libcall",
10744#endif
10745#ifdef FEAT_LINEBREAK
10746 "linebreak",
10747#endif
10748#ifdef FEAT_LISP
10749 "lispindent",
10750#endif
10751#ifdef FEAT_LISTCMDS
10752 "listcmds",
10753#endif
10754#ifdef FEAT_LOCALMAP
10755 "localmap",
10756#endif
10757#ifdef FEAT_MENU
10758 "menu",
10759#endif
10760#ifdef FEAT_SESSION
10761 "mksession",
10762#endif
10763#ifdef FEAT_MODIFY_FNAME
10764 "modify_fname",
10765#endif
10766#ifdef FEAT_MOUSE
10767 "mouse",
10768#endif
10769#ifdef FEAT_MOUSESHAPE
10770 "mouseshape",
10771#endif
10772#if defined(UNIX) || defined(VMS)
10773# ifdef FEAT_MOUSE_DEC
10774 "mouse_dec",
10775# endif
10776# ifdef FEAT_MOUSE_GPM
10777 "mouse_gpm",
10778# endif
10779# ifdef FEAT_MOUSE_JSB
10780 "mouse_jsbterm",
10781# endif
10782# ifdef FEAT_MOUSE_NET
10783 "mouse_netterm",
10784# endif
10785# ifdef FEAT_MOUSE_PTERM
10786 "mouse_pterm",
10787# endif
10788# ifdef FEAT_MOUSE_XTERM
10789 "mouse_xterm",
10790# endif
10791#endif
10792#ifdef FEAT_MBYTE
10793 "multi_byte",
10794#endif
10795#ifdef FEAT_MBYTE_IME
10796 "multi_byte_ime",
10797#endif
10798#ifdef FEAT_MULTI_LANG
10799 "multi_lang",
10800#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010801#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010802#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010803 "mzscheme",
10804#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010805#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010806#ifdef FEAT_OLE
10807 "ole",
10808#endif
10809#ifdef FEAT_OSFILETYPE
10810 "osfiletype",
10811#endif
10812#ifdef FEAT_PATH_EXTRA
10813 "path_extra",
10814#endif
10815#ifdef FEAT_PERL
10816#ifndef DYNAMIC_PERL
10817 "perl",
10818#endif
10819#endif
10820#ifdef FEAT_PYTHON
10821#ifndef DYNAMIC_PYTHON
10822 "python",
10823#endif
10824#endif
10825#ifdef FEAT_POSTSCRIPT
10826 "postscript",
10827#endif
10828#ifdef FEAT_PRINTER
10829 "printer",
10830#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000010831#ifdef FEAT_PROFILE
10832 "profile",
10833#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000010834#ifdef FEAT_RELTIME
10835 "reltime",
10836#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010837#ifdef FEAT_QUICKFIX
10838 "quickfix",
10839#endif
10840#ifdef FEAT_RIGHTLEFT
10841 "rightleft",
10842#endif
10843#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
10844 "ruby",
10845#endif
10846#ifdef FEAT_SCROLLBIND
10847 "scrollbind",
10848#endif
10849#ifdef FEAT_CMDL_INFO
10850 "showcmd",
10851 "cmdline_info",
10852#endif
10853#ifdef FEAT_SIGNS
10854 "signs",
10855#endif
10856#ifdef FEAT_SMARTINDENT
10857 "smartindent",
10858#endif
10859#ifdef FEAT_SNIFF
10860 "sniff",
10861#endif
10862#ifdef FEAT_STL_OPT
10863 "statusline",
10864#endif
10865#ifdef FEAT_SUN_WORKSHOP
10866 "sun_workshop",
10867#endif
10868#ifdef FEAT_NETBEANS_INTG
10869 "netbeans_intg",
10870#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000010871#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010872 "spell",
10873#endif
10874#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010875 "syntax",
10876#endif
10877#if defined(USE_SYSTEM) || !defined(UNIX)
10878 "system",
10879#endif
10880#ifdef FEAT_TAG_BINS
10881 "tag_binary",
10882#endif
10883#ifdef FEAT_TAG_OLDSTATIC
10884 "tag_old_static",
10885#endif
10886#ifdef FEAT_TAG_ANYWHITE
10887 "tag_any_white",
10888#endif
10889#ifdef FEAT_TCL
10890# ifndef DYNAMIC_TCL
10891 "tcl",
10892# endif
10893#endif
10894#ifdef TERMINFO
10895 "terminfo",
10896#endif
10897#ifdef FEAT_TERMRESPONSE
10898 "termresponse",
10899#endif
10900#ifdef FEAT_TEXTOBJ
10901 "textobjects",
10902#endif
10903#ifdef HAVE_TGETENT
10904 "tgetent",
10905#endif
10906#ifdef FEAT_TITLE
10907 "title",
10908#endif
10909#ifdef FEAT_TOOLBAR
10910 "toolbar",
10911#endif
10912#ifdef FEAT_USR_CMDS
10913 "user-commands", /* was accidentally included in 5.4 */
10914 "user_commands",
10915#endif
10916#ifdef FEAT_VIMINFO
10917 "viminfo",
10918#endif
10919#ifdef FEAT_VERTSPLIT
10920 "vertsplit",
10921#endif
10922#ifdef FEAT_VIRTUALEDIT
10923 "virtualedit",
10924#endif
10925#ifdef FEAT_VISUAL
10926 "visual",
10927#endif
10928#ifdef FEAT_VISUALEXTRA
10929 "visualextra",
10930#endif
10931#ifdef FEAT_VREPLACE
10932 "vreplace",
10933#endif
10934#ifdef FEAT_WILDIGN
10935 "wildignore",
10936#endif
10937#ifdef FEAT_WILDMENU
10938 "wildmenu",
10939#endif
10940#ifdef FEAT_WINDOWS
10941 "windows",
10942#endif
10943#ifdef FEAT_WAK
10944 "winaltkeys",
10945#endif
10946#ifdef FEAT_WRITEBACKUP
10947 "writebackup",
10948#endif
10949#ifdef FEAT_XIM
10950 "xim",
10951#endif
10952#ifdef FEAT_XFONTSET
10953 "xfontset",
10954#endif
10955#ifdef USE_XSMP
10956 "xsmp",
10957#endif
10958#ifdef USE_XSMP_INTERACT
10959 "xsmp_interact",
10960#endif
10961#ifdef FEAT_XCLIPBOARD
10962 "xterm_clipboard",
10963#endif
10964#ifdef FEAT_XTERM_SAVE
10965 "xterm_save",
10966#endif
10967#if defined(UNIX) && defined(FEAT_X11)
10968 "X11",
10969#endif
10970 NULL
10971 };
10972
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010973 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010974 for (i = 0; has_list[i] != NULL; ++i)
10975 if (STRICMP(name, has_list[i]) == 0)
10976 {
10977 n = TRUE;
10978 break;
10979 }
10980
10981 if (n == FALSE)
10982 {
10983 if (STRNICMP(name, "patch", 5) == 0)
10984 n = has_patch(atoi((char *)name + 5));
10985 else if (STRICMP(name, "vim_starting") == 0)
10986 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010987#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
10988 else if (STRICMP(name, "balloon_multiline") == 0)
10989 n = multiline_balloon_available();
10990#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010991#ifdef DYNAMIC_TCL
10992 else if (STRICMP(name, "tcl") == 0)
10993 n = tcl_enabled(FALSE);
10994#endif
10995#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
10996 else if (STRICMP(name, "iconv") == 0)
10997 n = iconv_enabled(FALSE);
10998#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010999#ifdef DYNAMIC_MZSCHEME
11000 else if (STRICMP(name, "mzscheme") == 0)
11001 n = mzscheme_enabled(FALSE);
11002#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011003#ifdef DYNAMIC_RUBY
11004 else if (STRICMP(name, "ruby") == 0)
11005 n = ruby_enabled(FALSE);
11006#endif
11007#ifdef DYNAMIC_PYTHON
11008 else if (STRICMP(name, "python") == 0)
11009 n = python_enabled(FALSE);
11010#endif
11011#ifdef DYNAMIC_PERL
11012 else if (STRICMP(name, "perl") == 0)
11013 n = perl_enabled(FALSE);
11014#endif
11015#ifdef FEAT_GUI
11016 else if (STRICMP(name, "gui_running") == 0)
11017 n = (gui.in_use || gui.starting);
11018# ifdef FEAT_GUI_W32
11019 else if (STRICMP(name, "gui_win32s") == 0)
11020 n = gui_is_win32s();
11021# endif
11022# ifdef FEAT_BROWSE
11023 else if (STRICMP(name, "browse") == 0)
11024 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
11025# endif
11026#endif
11027#ifdef FEAT_SYN_HL
11028 else if (STRICMP(name, "syntax_items") == 0)
11029 n = syntax_present(curbuf);
11030#endif
11031#if defined(WIN3264)
11032 else if (STRICMP(name, "win95") == 0)
11033 n = mch_windows95();
11034#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000011035#ifdef FEAT_NETBEANS_INTG
11036 else if (STRICMP(name, "netbeans_enabled") == 0)
11037 n = usingNetbeans;
11038#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011039 }
11040
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011041 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011042}
11043
11044/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000011045 * "has_key()" function
11046 */
11047 static void
11048f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011049 typval_T *argvars;
11050 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011051{
11052 rettv->vval.v_number = 0;
11053 if (argvars[0].v_type != VAR_DICT)
11054 {
11055 EMSG(_(e_dictreq));
11056 return;
11057 }
11058 if (argvars[0].vval.v_dict == NULL)
11059 return;
11060
11061 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011062 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011063}
11064
11065/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011066 * "hasmapto()" function
11067 */
11068 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011069f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011070 typval_T *argvars;
11071 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011072{
11073 char_u *name;
11074 char_u *mode;
11075 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000011076 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011077
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011078 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011079 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011080 mode = (char_u *)"nvo";
11081 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000011082 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011083 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000011084 if (argvars[2].v_type != VAR_UNKNOWN)
11085 abbr = get_tv_number(&argvars[2]);
11086 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011087
Bram Moolenaar2c932302006-03-18 21:42:09 +000011088 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011089 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011090 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011091 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011092}
11093
11094/*
11095 * "histadd()" function
11096 */
11097/*ARGSUSED*/
11098 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011099f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011100 typval_T *argvars;
11101 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011102{
11103#ifdef FEAT_CMDHIST
11104 int histype;
11105 char_u *str;
11106 char_u buf[NUMBUFLEN];
11107#endif
11108
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011109 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011110 if (check_restricted() || check_secure())
11111 return;
11112#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011113 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11114 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011115 if (histype >= 0)
11116 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011117 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011118 if (*str != NUL)
11119 {
11120 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011121 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011122 return;
11123 }
11124 }
11125#endif
11126}
11127
11128/*
11129 * "histdel()" function
11130 */
11131/*ARGSUSED*/
11132 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011133f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011134 typval_T *argvars;
11135 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011136{
11137#ifdef FEAT_CMDHIST
11138 int n;
11139 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011140 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011141
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011142 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11143 if (str == NULL)
11144 n = 0;
11145 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011146 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011147 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011148 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011149 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011150 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011151 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011152 else
11153 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011154 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011155 get_tv_string_buf(&argvars[1], buf));
11156 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011157#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011158 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011159#endif
11160}
11161
11162/*
11163 * "histget()" function
11164 */
11165/*ARGSUSED*/
11166 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011167f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011168 typval_T *argvars;
11169 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011170{
11171#ifdef FEAT_CMDHIST
11172 int type;
11173 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011174 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011175
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011176 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11177 if (str == NULL)
11178 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011179 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011180 {
11181 type = get_histtype(str);
11182 if (argvars[1].v_type == VAR_UNKNOWN)
11183 idx = get_history_idx(type);
11184 else
11185 idx = (int)get_tv_number_chk(&argvars[1], NULL);
11186 /* -1 on type error */
11187 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
11188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011189#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011190 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011191#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011192 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011193}
11194
11195/*
11196 * "histnr()" function
11197 */
11198/*ARGSUSED*/
11199 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011200f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011201 typval_T *argvars;
11202 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011203{
11204 int i;
11205
11206#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011207 char_u *history = get_tv_string_chk(&argvars[0]);
11208
11209 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011210 if (i >= HIST_CMD && i < HIST_COUNT)
11211 i = get_history_idx(i);
11212 else
11213#endif
11214 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011215 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011216}
11217
11218/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011219 * "highlightID(name)" function
11220 */
11221 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011222f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011223 typval_T *argvars;
11224 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011225{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011226 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011227}
11228
11229/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011230 * "highlight_exists()" function
11231 */
11232 static void
11233f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011234 typval_T *argvars;
11235 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011236{
11237 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
11238}
11239
11240/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011241 * "hostname()" function
11242 */
11243/*ARGSUSED*/
11244 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011245f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011246 typval_T *argvars;
11247 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011248{
11249 char_u hostname[256];
11250
11251 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011252 rettv->v_type = VAR_STRING;
11253 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011254}
11255
11256/*
11257 * iconv() function
11258 */
11259/*ARGSUSED*/
11260 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011261f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011262 typval_T *argvars;
11263 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011264{
11265#ifdef FEAT_MBYTE
11266 char_u buf1[NUMBUFLEN];
11267 char_u buf2[NUMBUFLEN];
11268 char_u *from, *to, *str;
11269 vimconv_T vimconv;
11270#endif
11271
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011272 rettv->v_type = VAR_STRING;
11273 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011274
11275#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011276 str = get_tv_string(&argvars[0]);
11277 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
11278 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011279 vimconv.vc_type = CONV_NONE;
11280 convert_setup(&vimconv, from, to);
11281
11282 /* If the encodings are equal, no conversion needed. */
11283 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011284 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011285 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011286 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011287
11288 convert_setup(&vimconv, NULL, NULL);
11289 vim_free(from);
11290 vim_free(to);
11291#endif
11292}
11293
11294/*
11295 * "indent()" function
11296 */
11297 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011298f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011299 typval_T *argvars;
11300 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011301{
11302 linenr_T lnum;
11303
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011304 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011305 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011306 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011307 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011308 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011309}
11310
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011311/*
11312 * "index()" function
11313 */
11314 static void
11315f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011316 typval_T *argvars;
11317 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011318{
Bram Moolenaar33570922005-01-25 22:26:29 +000011319 list_T *l;
11320 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011321 long idx = 0;
11322 int ic = FALSE;
11323
11324 rettv->vval.v_number = -1;
11325 if (argvars[0].v_type != VAR_LIST)
11326 {
11327 EMSG(_(e_listreq));
11328 return;
11329 }
11330 l = argvars[0].vval.v_list;
11331 if (l != NULL)
11332 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011333 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011334 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011335 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011336 int error = FALSE;
11337
Bram Moolenaar758711c2005-02-02 23:11:38 +000011338 /* Start at specified item. Use the cached index that list_find()
11339 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011340 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000011341 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011342 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011343 ic = get_tv_number_chk(&argvars[3], &error);
11344 if (error)
11345 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011346 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011347
Bram Moolenaar758711c2005-02-02 23:11:38 +000011348 for ( ; item != NULL; item = item->li_next, ++idx)
11349 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011350 {
11351 rettv->vval.v_number = idx;
11352 break;
11353 }
11354 }
11355}
11356
Bram Moolenaar071d4272004-06-13 20:20:40 +000011357static int inputsecret_flag = 0;
11358
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011359static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
11360
Bram Moolenaar071d4272004-06-13 20:20:40 +000011361/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011362 * This function is used by f_input() and f_inputdialog() functions. The third
11363 * argument to f_input() specifies the type of completion to use at the
11364 * prompt. The third argument to f_inputdialog() specifies the value to return
11365 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011366 */
11367 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011368get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000011369 typval_T *argvars;
11370 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011371 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011372{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011373 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011374 char_u *p = NULL;
11375 int c;
11376 char_u buf[NUMBUFLEN];
11377 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011378 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011379 int xp_type = EXPAND_NOTHING;
11380 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011381
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011382 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011383
11384#ifdef NO_CONSOLE_INPUT
11385 /* While starting up, there is no place to enter text. */
11386 if (no_console_input())
11387 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011388 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011389 return;
11390 }
11391#endif
11392
11393 cmd_silent = FALSE; /* Want to see the prompt. */
11394 if (prompt != NULL)
11395 {
11396 /* Only the part of the message after the last NL is considered as
11397 * prompt for the command line */
11398 p = vim_strrchr(prompt, '\n');
11399 if (p == NULL)
11400 p = prompt;
11401 else
11402 {
11403 ++p;
11404 c = *p;
11405 *p = NUL;
11406 msg_start();
11407 msg_clr_eos();
11408 msg_puts_attr(prompt, echo_attr);
11409 msg_didout = FALSE;
11410 msg_starthere();
11411 *p = c;
11412 }
11413 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011414
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011415 if (argvars[1].v_type != VAR_UNKNOWN)
11416 {
11417 defstr = get_tv_string_buf_chk(&argvars[1], buf);
11418 if (defstr != NULL)
11419 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011420
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011421 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000011422 {
11423 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000011424 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000011425 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011426
Bram Moolenaar4463f292005-09-25 22:20:24 +000011427 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011428
Bram Moolenaar4463f292005-09-25 22:20:24 +000011429 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
11430 if (xp_name == NULL)
11431 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011432
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011433 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011434
Bram Moolenaar4463f292005-09-25 22:20:24 +000011435 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
11436 &xp_arg) == FAIL)
11437 return;
11438 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011439 }
11440
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011441 if (defstr != NULL)
11442 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011443 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
11444 xp_type, xp_arg);
11445
11446 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011447
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011448 /* since the user typed this, no need to wait for return */
11449 need_wait_return = FALSE;
11450 msg_didout = FALSE;
11451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011452 cmd_silent = cmd_silent_save;
11453}
11454
11455/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011456 * "input()" function
11457 * Also handles inputsecret() when inputsecret is set.
11458 */
11459 static void
11460f_input(argvars, rettv)
11461 typval_T *argvars;
11462 typval_T *rettv;
11463{
11464 get_user_input(argvars, rettv, FALSE);
11465}
11466
11467/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011468 * "inputdialog()" function
11469 */
11470 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011471f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011472 typval_T *argvars;
11473 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011474{
11475#if defined(FEAT_GUI_TEXTDIALOG)
11476 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
11477 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
11478 {
11479 char_u *message;
11480 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011481 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000011482
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011483 message = get_tv_string_chk(&argvars[0]);
11484 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000011485 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000011486 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011487 else
11488 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011489 if (message != NULL && defstr != NULL
11490 && do_dialog(VIM_QUESTION, NULL, message,
11491 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011492 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011493 else
11494 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011495 if (message != NULL && defstr != NULL
11496 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011497 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011498 rettv->vval.v_string = vim_strsave(
11499 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011500 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011501 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011502 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011503 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011504 }
11505 else
11506#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011507 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011508}
11509
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011510/*
11511 * "inputlist()" function
11512 */
11513 static void
11514f_inputlist(argvars, rettv)
11515 typval_T *argvars;
11516 typval_T *rettv;
11517{
11518 listitem_T *li;
11519 int selected;
11520 int mouse_used;
11521
11522 rettv->vval.v_number = 0;
11523#ifdef NO_CONSOLE_INPUT
11524 /* While starting up, there is no place to enter text. */
11525 if (no_console_input())
11526 return;
11527#endif
11528 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
11529 {
11530 EMSG2(_(e_listarg), "inputlist()");
11531 return;
11532 }
11533
11534 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000011535 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011536 lines_left = Rows; /* avoid more prompt */
11537 msg_scroll = TRUE;
11538 msg_clr_eos();
11539
11540 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
11541 {
11542 msg_puts(get_tv_string(&li->li_tv));
11543 msg_putchar('\n');
11544 }
11545
11546 /* Ask for choice. */
11547 selected = prompt_for_number(&mouse_used);
11548 if (mouse_used)
11549 selected -= lines_left;
11550
11551 rettv->vval.v_number = selected;
11552}
11553
11554
Bram Moolenaar071d4272004-06-13 20:20:40 +000011555static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
11556
11557/*
11558 * "inputrestore()" function
11559 */
11560/*ARGSUSED*/
11561 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011562f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011563 typval_T *argvars;
11564 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011565{
11566 if (ga_userinput.ga_len > 0)
11567 {
11568 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011569 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
11570 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011571 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011572 }
11573 else if (p_verbose > 1)
11574 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000011575 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011576 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011577 }
11578}
11579
11580/*
11581 * "inputsave()" function
11582 */
11583/*ARGSUSED*/
11584 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011585f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011586 typval_T *argvars;
11587 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011588{
11589 /* Add an entry to the stack of typehead storage. */
11590 if (ga_grow(&ga_userinput, 1) == OK)
11591 {
11592 save_typeahead((tasave_T *)(ga_userinput.ga_data)
11593 + ga_userinput.ga_len);
11594 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011595 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011596 }
11597 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011598 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011599}
11600
11601/*
11602 * "inputsecret()" function
11603 */
11604 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011605f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011606 typval_T *argvars;
11607 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011608{
11609 ++cmdline_star;
11610 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011611 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011612 --cmdline_star;
11613 --inputsecret_flag;
11614}
11615
11616/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011617 * "insert()" function
11618 */
11619 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011620f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011621 typval_T *argvars;
11622 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011623{
11624 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011625 listitem_T *item;
11626 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011627 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011628
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011629 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011630 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011631 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011632 else if ((l = argvars[0].vval.v_list) != NULL
11633 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011634 {
11635 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011636 before = get_tv_number_chk(&argvars[2], &error);
11637 if (error)
11638 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011639
Bram Moolenaar758711c2005-02-02 23:11:38 +000011640 if (before == l->lv_len)
11641 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011642 else
11643 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011644 item = list_find(l, before);
11645 if (item == NULL)
11646 {
11647 EMSGN(_(e_listidx), before);
11648 l = NULL;
11649 }
11650 }
11651 if (l != NULL)
11652 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011653 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011654 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011655 }
11656 }
11657}
11658
11659/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011660 * "isdirectory()" function
11661 */
11662 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011663f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011664 typval_T *argvars;
11665 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011666{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011667 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011668}
11669
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011670/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011671 * "islocked()" function
11672 */
11673 static void
11674f_islocked(argvars, rettv)
11675 typval_T *argvars;
11676 typval_T *rettv;
11677{
11678 lval_T lv;
11679 char_u *end;
11680 dictitem_T *di;
11681
11682 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000011683 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
11684 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011685 if (end != NULL && lv.ll_name != NULL)
11686 {
11687 if (*end != NUL)
11688 EMSG(_(e_trailing));
11689 else
11690 {
11691 if (lv.ll_tv == NULL)
11692 {
11693 if (check_changedtick(lv.ll_name))
11694 rettv->vval.v_number = 1; /* always locked */
11695 else
11696 {
11697 di = find_var(lv.ll_name, NULL);
11698 if (di != NULL)
11699 {
11700 /* Consider a variable locked when:
11701 * 1. the variable itself is locked
11702 * 2. the value of the variable is locked.
11703 * 3. the List or Dict value is locked.
11704 */
11705 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
11706 || tv_islocked(&di->di_tv));
11707 }
11708 }
11709 }
11710 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011711 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011712 else if (lv.ll_newkey != NULL)
11713 EMSG2(_(e_dictkey), lv.ll_newkey);
11714 else if (lv.ll_list != NULL)
11715 /* List item. */
11716 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
11717 else
11718 /* Dictionary item. */
11719 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
11720 }
11721 }
11722
11723 clear_lval(&lv);
11724}
11725
Bram Moolenaar33570922005-01-25 22:26:29 +000011726static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011727
11728/*
11729 * Turn a dict into a list:
11730 * "what" == 0: list of keys
11731 * "what" == 1: list of values
11732 * "what" == 2: list of items
11733 */
11734 static void
11735dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000011736 typval_T *argvars;
11737 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011738 int what;
11739{
Bram Moolenaar33570922005-01-25 22:26:29 +000011740 list_T *l2;
11741 dictitem_T *di;
11742 hashitem_T *hi;
11743 listitem_T *li;
11744 listitem_T *li2;
11745 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011746 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011747
11748 rettv->vval.v_number = 0;
11749 if (argvars[0].v_type != VAR_DICT)
11750 {
11751 EMSG(_(e_dictreq));
11752 return;
11753 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011754 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011755 return;
11756
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011757 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011758 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011759
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011760 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000011761 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011762 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011763 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011764 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011765 --todo;
11766 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011767
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011768 li = listitem_alloc();
11769 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011770 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011771 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011772
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011773 if (what == 0)
11774 {
11775 /* keys() */
11776 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011777 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011778 li->li_tv.vval.v_string = vim_strsave(di->di_key);
11779 }
11780 else if (what == 1)
11781 {
11782 /* values() */
11783 copy_tv(&di->di_tv, &li->li_tv);
11784 }
11785 else
11786 {
11787 /* items() */
11788 l2 = list_alloc();
11789 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011790 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011791 li->li_tv.vval.v_list = l2;
11792 if (l2 == NULL)
11793 break;
11794 ++l2->lv_refcount;
11795
11796 li2 = listitem_alloc();
11797 if (li2 == NULL)
11798 break;
11799 list_append(l2, li2);
11800 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011801 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011802 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
11803
11804 li2 = listitem_alloc();
11805 if (li2 == NULL)
11806 break;
11807 list_append(l2, li2);
11808 copy_tv(&di->di_tv, &li2->li_tv);
11809 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011810 }
11811 }
11812}
11813
11814/*
11815 * "items(dict)" function
11816 */
11817 static void
11818f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011819 typval_T *argvars;
11820 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011821{
11822 dict_list(argvars, rettv, 2);
11823}
11824
Bram Moolenaar071d4272004-06-13 20:20:40 +000011825/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011826 * "join()" function
11827 */
11828 static void
11829f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011830 typval_T *argvars;
11831 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011832{
11833 garray_T ga;
11834 char_u *sep;
11835
11836 rettv->vval.v_number = 0;
11837 if (argvars[0].v_type != VAR_LIST)
11838 {
11839 EMSG(_(e_listreq));
11840 return;
11841 }
11842 if (argvars[0].vval.v_list == NULL)
11843 return;
11844 if (argvars[1].v_type == VAR_UNKNOWN)
11845 sep = (char_u *)" ";
11846 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011847 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011848
11849 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011850
11851 if (sep != NULL)
11852 {
11853 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000011854 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011855 ga_append(&ga, NUL);
11856 rettv->vval.v_string = (char_u *)ga.ga_data;
11857 }
11858 else
11859 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011860}
11861
11862/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011863 * "keys()" function
11864 */
11865 static void
11866f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011867 typval_T *argvars;
11868 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011869{
11870 dict_list(argvars, rettv, 0);
11871}
11872
11873/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011874 * "last_buffer_nr()" function.
11875 */
11876/*ARGSUSED*/
11877 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011878f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011879 typval_T *argvars;
11880 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011881{
11882 int n = 0;
11883 buf_T *buf;
11884
11885 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
11886 if (n < buf->b_fnum)
11887 n = buf->b_fnum;
11888
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011889 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011890}
11891
11892/*
11893 * "len()" function
11894 */
11895 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011896f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011897 typval_T *argvars;
11898 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011899{
11900 switch (argvars[0].v_type)
11901 {
11902 case VAR_STRING:
11903 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011904 rettv->vval.v_number = (varnumber_T)STRLEN(
11905 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011906 break;
11907 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011908 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011909 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011910 case VAR_DICT:
11911 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
11912 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011913 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011914 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011915 break;
11916 }
11917}
11918
Bram Moolenaar33570922005-01-25 22:26:29 +000011919static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011920
11921 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011922libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011923 typval_T *argvars;
11924 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011925 int type;
11926{
11927#ifdef FEAT_LIBCALL
11928 char_u *string_in;
11929 char_u **string_result;
11930 int nr_result;
11931#endif
11932
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011933 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011934 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011935 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011936 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011937 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011938
11939 if (check_restricted() || check_secure())
11940 return;
11941
11942#ifdef FEAT_LIBCALL
11943 /* The first two args must be strings, otherwise its meaningless */
11944 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
11945 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011946 string_in = NULL;
11947 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011948 string_in = argvars[2].vval.v_string;
11949 if (type == VAR_NUMBER)
11950 string_result = NULL;
11951 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011952 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011953 if (mch_libcall(argvars[0].vval.v_string,
11954 argvars[1].vval.v_string,
11955 string_in,
11956 argvars[2].vval.v_number,
11957 string_result,
11958 &nr_result) == OK
11959 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011960 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011961 }
11962#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011963}
11964
11965/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011966 * "libcall()" function
11967 */
11968 static void
11969f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011970 typval_T *argvars;
11971 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011972{
11973 libcall_common(argvars, rettv, VAR_STRING);
11974}
11975
11976/*
11977 * "libcallnr()" function
11978 */
11979 static void
11980f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011981 typval_T *argvars;
11982 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011983{
11984 libcall_common(argvars, rettv, VAR_NUMBER);
11985}
11986
11987/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011988 * "line(string)" function
11989 */
11990 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011991f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011992 typval_T *argvars;
11993 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011994{
11995 linenr_T lnum = 0;
11996 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011997 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011998
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011999 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012000 if (fp != NULL)
12001 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012002 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012003}
12004
12005/*
12006 * "line2byte(lnum)" function
12007 */
12008/*ARGSUSED*/
12009 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012010f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012011 typval_T *argvars;
12012 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012013{
12014#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012015 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012016#else
12017 linenr_T lnum;
12018
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012019 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012020 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012021 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012022 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012023 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
12024 if (rettv->vval.v_number >= 0)
12025 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012026#endif
12027}
12028
12029/*
12030 * "lispindent(lnum)" function
12031 */
12032 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012033f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012034 typval_T *argvars;
12035 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012036{
12037#ifdef FEAT_LISP
12038 pos_T pos;
12039 linenr_T lnum;
12040
12041 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012042 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012043 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12044 {
12045 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012046 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012047 curwin->w_cursor = pos;
12048 }
12049 else
12050#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012051 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012052}
12053
12054/*
12055 * "localtime()" function
12056 */
12057/*ARGSUSED*/
12058 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012059f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012060 typval_T *argvars;
12061 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012062{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012063 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012064}
12065
Bram Moolenaar33570922005-01-25 22:26:29 +000012066static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012067
12068 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012069get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000012070 typval_T *argvars;
12071 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012072 int exact;
12073{
12074 char_u *keys;
12075 char_u *which;
12076 char_u buf[NUMBUFLEN];
12077 char_u *keys_buf = NULL;
12078 char_u *rhs;
12079 int mode;
12080 garray_T ga;
Bram Moolenaar2c932302006-03-18 21:42:09 +000012081 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012082
12083 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012084 rettv->v_type = VAR_STRING;
12085 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012086
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012087 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012088 if (*keys == NUL)
12089 return;
12090
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012091 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000012092 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012093 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012094 if (argvars[2].v_type != VAR_UNKNOWN)
12095 abbr = get_tv_number(&argvars[2]);
12096 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012097 else
12098 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012099 if (which == NULL)
12100 return;
12101
Bram Moolenaar071d4272004-06-13 20:20:40 +000012102 mode = get_map_mode(&which, 0);
12103
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000012104 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012105 rhs = check_map(keys, mode, exact, FALSE, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012106 vim_free(keys_buf);
12107 if (rhs != NULL)
12108 {
12109 ga_init(&ga);
12110 ga.ga_itemsize = 1;
12111 ga.ga_growsize = 40;
12112
12113 while (*rhs != NUL)
12114 ga_concat(&ga, str2special(&rhs, FALSE));
12115
12116 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012117 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012118 }
12119}
12120
12121/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012122 * "map()" function
12123 */
12124 static void
12125f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012126 typval_T *argvars;
12127 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012128{
12129 filter_map(argvars, rettv, TRUE);
12130}
12131
12132/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012133 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012134 */
12135 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012136f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012137 typval_T *argvars;
12138 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012139{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012140 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012141}
12142
12143/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012144 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012145 */
12146 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012147f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012148 typval_T *argvars;
12149 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012150{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012151 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012152}
12153
Bram Moolenaar33570922005-01-25 22:26:29 +000012154static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012155
12156 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012157find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000012158 typval_T *argvars;
12159 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012160 int type;
12161{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012162 char_u *str = NULL;
12163 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012164 char_u *pat;
12165 regmatch_T regmatch;
12166 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012167 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000012168 char_u *save_cpo;
12169 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012170 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012171 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012172 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012173 list_T *l = NULL;
12174 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012175 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012176 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012177
12178 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12179 save_cpo = p_cpo;
12180 p_cpo = (char_u *)"";
12181
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012182 rettv->vval.v_number = -1;
12183 if (type == 3)
12184 {
12185 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012186 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012187 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012188 }
12189 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012190 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012191 rettv->v_type = VAR_STRING;
12192 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012193 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012194
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012195 if (argvars[0].v_type == VAR_LIST)
12196 {
12197 if ((l = argvars[0].vval.v_list) == NULL)
12198 goto theend;
12199 li = l->lv_first;
12200 }
12201 else
12202 expr = str = get_tv_string(&argvars[0]);
12203
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012204 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
12205 if (pat == NULL)
12206 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012207
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012208 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012209 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012210 int error = FALSE;
12211
12212 start = get_tv_number_chk(&argvars[2], &error);
12213 if (error)
12214 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012215 if (l != NULL)
12216 {
12217 li = list_find(l, start);
12218 if (li == NULL)
12219 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012220 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012221 }
12222 else
12223 {
12224 if (start < 0)
12225 start = 0;
12226 if (start > (long)STRLEN(str))
12227 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012228 /* When "count" argument is there ignore matches before "start",
12229 * otherwise skip part of the string. Differs when pattern is "^"
12230 * or "\<". */
12231 if (argvars[3].v_type != VAR_UNKNOWN)
12232 startcol = start;
12233 else
12234 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012235 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012236
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012237 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012238 nth = get_tv_number_chk(&argvars[3], &error);
12239 if (error)
12240 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012241 }
12242
12243 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12244 if (regmatch.regprog != NULL)
12245 {
12246 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012247
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012248 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012249 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012250 if (l != NULL)
12251 {
12252 if (li == NULL)
12253 {
12254 match = FALSE;
12255 break;
12256 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012257 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012258 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012259 if (str == NULL)
12260 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012261 }
12262
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012263 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012264
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012265 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012266 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012267 if (l == NULL && !match)
12268 break;
12269
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012270 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012271 if (l != NULL)
12272 {
12273 li = li->li_next;
12274 ++idx;
12275 }
12276 else
12277 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012278#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012279 startcol = (colnr_T)(regmatch.startp[0]
12280 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012281#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012282 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012283#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012284 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012285 }
12286
12287 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012288 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012289 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012290 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012291 int i;
12292
12293 /* return list with matched string and submatches */
12294 for (i = 0; i < NSUBEXP; ++i)
12295 {
12296 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000012297 {
12298 if (list_append_string(rettv->vval.v_list,
12299 (char_u *)"", 0) == FAIL)
12300 break;
12301 }
12302 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000012303 regmatch.startp[i],
12304 (int)(regmatch.endp[i] - regmatch.startp[i]))
12305 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012306 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012307 }
12308 }
12309 else if (type == 2)
12310 {
12311 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012312 if (l != NULL)
12313 copy_tv(&li->li_tv, rettv);
12314 else
12315 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000012316 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012317 }
12318 else if (l != NULL)
12319 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012320 else
12321 {
12322 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012323 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012324 (varnumber_T)(regmatch.startp[0] - str);
12325 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012326 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012327 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012328 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012329 }
12330 }
12331 vim_free(regmatch.regprog);
12332 }
12333
12334theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012335 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012336 p_cpo = save_cpo;
12337}
12338
12339/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012340 * "match()" function
12341 */
12342 static void
12343f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012344 typval_T *argvars;
12345 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012346{
12347 find_some_match(argvars, rettv, 1);
12348}
12349
12350/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012351 * "matcharg()" function
12352 */
12353 static void
12354f_matcharg(argvars, rettv)
12355 typval_T *argvars;
12356 typval_T *rettv;
12357{
12358 if (rettv_list_alloc(rettv) == OK)
12359 {
12360#ifdef FEAT_SEARCH_EXTRA
12361 int mi = get_tv_number(&argvars[0]);
12362
12363 if (mi >= 1 && mi <= 3)
12364 {
12365 list_append_string(rettv->vval.v_list,
12366 syn_id2name(curwin->w_match_id[mi - 1]), -1);
12367 list_append_string(rettv->vval.v_list,
12368 curwin->w_match_pat[mi - 1], -1);
12369 }
12370#endif
12371 }
12372}
12373
12374/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012375 * "matchend()" function
12376 */
12377 static void
12378f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012379 typval_T *argvars;
12380 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012381{
12382 find_some_match(argvars, rettv, 0);
12383}
12384
12385/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012386 * "matchlist()" function
12387 */
12388 static void
12389f_matchlist(argvars, rettv)
12390 typval_T *argvars;
12391 typval_T *rettv;
12392{
12393 find_some_match(argvars, rettv, 3);
12394}
12395
12396/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012397 * "matchstr()" function
12398 */
12399 static void
12400f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012401 typval_T *argvars;
12402 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012403{
12404 find_some_match(argvars, rettv, 2);
12405}
12406
Bram Moolenaar33570922005-01-25 22:26:29 +000012407static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012408
12409 static void
12410max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000012411 typval_T *argvars;
12412 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012413 int domax;
12414{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012415 long n = 0;
12416 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012417 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012418
12419 if (argvars[0].v_type == VAR_LIST)
12420 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012421 list_T *l;
12422 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012423
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012424 l = argvars[0].vval.v_list;
12425 if (l != NULL)
12426 {
12427 li = l->lv_first;
12428 if (li != NULL)
12429 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012430 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012431 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012432 {
12433 li = li->li_next;
12434 if (li == NULL)
12435 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012436 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012437 if (domax ? i > n : i < n)
12438 n = i;
12439 }
12440 }
12441 }
12442 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012443 else if (argvars[0].v_type == VAR_DICT)
12444 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012445 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012446 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000012447 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012448 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012449
12450 d = argvars[0].vval.v_dict;
12451 if (d != NULL)
12452 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012453 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000012454 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012455 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012456 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000012457 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012458 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012459 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012460 if (first)
12461 {
12462 n = i;
12463 first = FALSE;
12464 }
12465 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012466 n = i;
12467 }
12468 }
12469 }
12470 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012471 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000012472 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012473 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012474}
12475
12476/*
12477 * "max()" function
12478 */
12479 static void
12480f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012481 typval_T *argvars;
12482 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012483{
12484 max_min(argvars, rettv, TRUE);
12485}
12486
12487/*
12488 * "min()" function
12489 */
12490 static void
12491f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012492 typval_T *argvars;
12493 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012494{
12495 max_min(argvars, rettv, FALSE);
12496}
12497
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012498static int mkdir_recurse __ARGS((char_u *dir, int prot));
12499
12500/*
12501 * Create the directory in which "dir" is located, and higher levels when
12502 * needed.
12503 */
12504 static int
12505mkdir_recurse(dir, prot)
12506 char_u *dir;
12507 int prot;
12508{
12509 char_u *p;
12510 char_u *updir;
12511 int r = FAIL;
12512
12513 /* Get end of directory name in "dir".
12514 * We're done when it's "/" or "c:/". */
12515 p = gettail_sep(dir);
12516 if (p <= get_past_head(dir))
12517 return OK;
12518
12519 /* If the directory exists we're done. Otherwise: create it.*/
12520 updir = vim_strnsave(dir, (int)(p - dir));
12521 if (updir == NULL)
12522 return FAIL;
12523 if (mch_isdir(updir))
12524 r = OK;
12525 else if (mkdir_recurse(updir, prot) == OK)
12526 r = vim_mkdir_emsg(updir, prot);
12527 vim_free(updir);
12528 return r;
12529}
12530
12531#ifdef vim_mkdir
12532/*
12533 * "mkdir()" function
12534 */
12535 static void
12536f_mkdir(argvars, rettv)
12537 typval_T *argvars;
12538 typval_T *rettv;
12539{
12540 char_u *dir;
12541 char_u buf[NUMBUFLEN];
12542 int prot = 0755;
12543
12544 rettv->vval.v_number = FAIL;
12545 if (check_restricted() || check_secure())
12546 return;
12547
12548 dir = get_tv_string_buf(&argvars[0], buf);
12549 if (argvars[1].v_type != VAR_UNKNOWN)
12550 {
12551 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012552 prot = get_tv_number_chk(&argvars[2], NULL);
12553 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012554 mkdir_recurse(dir, prot);
12555 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012556 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012557}
12558#endif
12559
Bram Moolenaar0d660222005-01-07 21:51:51 +000012560/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012561 * "mode()" function
12562 */
12563/*ARGSUSED*/
12564 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012565f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012566 typval_T *argvars;
12567 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012568{
12569 char_u buf[2];
12570
12571#ifdef FEAT_VISUAL
12572 if (VIsual_active)
12573 {
12574 if (VIsual_select)
12575 buf[0] = VIsual_mode + 's' - 'v';
12576 else
12577 buf[0] = VIsual_mode;
12578 }
12579 else
12580#endif
12581 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
12582 buf[0] = 'r';
12583 else if (State & INSERT)
12584 {
12585 if (State & REPLACE_FLAG)
12586 buf[0] = 'R';
12587 else
12588 buf[0] = 'i';
12589 }
12590 else if (State & CMDLINE)
12591 buf[0] = 'c';
12592 else
12593 buf[0] = 'n';
12594
12595 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012596 rettv->vval.v_string = vim_strsave(buf);
12597 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012598}
12599
12600/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012601 * "nextnonblank()" function
12602 */
12603 static void
12604f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012605 typval_T *argvars;
12606 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012607{
12608 linenr_T lnum;
12609
12610 for (lnum = get_tv_lnum(argvars); ; ++lnum)
12611 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012612 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012613 {
12614 lnum = 0;
12615 break;
12616 }
12617 if (*skipwhite(ml_get(lnum)) != NUL)
12618 break;
12619 }
12620 rettv->vval.v_number = lnum;
12621}
12622
12623/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012624 * "nr2char()" function
12625 */
12626 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012627f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012628 typval_T *argvars;
12629 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012630{
12631 char_u buf[NUMBUFLEN];
12632
12633#ifdef FEAT_MBYTE
12634 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012635 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012636 else
12637#endif
12638 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012639 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012640 buf[1] = NUL;
12641 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012642 rettv->v_type = VAR_STRING;
12643 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012644}
12645
12646/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012647 * "pathshorten()" function
12648 */
12649 static void
12650f_pathshorten(argvars, rettv)
12651 typval_T *argvars;
12652 typval_T *rettv;
12653{
12654 char_u *p;
12655
12656 rettv->v_type = VAR_STRING;
12657 p = get_tv_string_chk(&argvars[0]);
12658 if (p == NULL)
12659 rettv->vval.v_string = NULL;
12660 else
12661 {
12662 p = vim_strsave(p);
12663 rettv->vval.v_string = p;
12664 if (p != NULL)
12665 shorten_dir(p);
12666 }
12667}
12668
12669/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012670 * "prevnonblank()" function
12671 */
12672 static void
12673f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012674 typval_T *argvars;
12675 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012676{
12677 linenr_T lnum;
12678
12679 lnum = get_tv_lnum(argvars);
12680 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
12681 lnum = 0;
12682 else
12683 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
12684 --lnum;
12685 rettv->vval.v_number = lnum;
12686}
12687
Bram Moolenaara6c840d2005-08-22 22:59:46 +000012688#ifdef HAVE_STDARG_H
12689/* This dummy va_list is here because:
12690 * - passing a NULL pointer doesn't work when va_list isn't a pointer
12691 * - locally in the function results in a "used before set" warning
12692 * - using va_start() to initialize it gives "function with fixed args" error */
12693static va_list ap;
12694#endif
12695
Bram Moolenaar8c711452005-01-14 21:53:12 +000012696/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012697 * "printf()" function
12698 */
12699 static void
12700f_printf(argvars, rettv)
12701 typval_T *argvars;
12702 typval_T *rettv;
12703{
12704 rettv->v_type = VAR_STRING;
12705 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000012706#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012707 {
12708 char_u buf[NUMBUFLEN];
12709 int len;
12710 char_u *s;
12711 int saved_did_emsg = did_emsg;
12712 char *fmt;
12713
12714 /* Get the required length, allocate the buffer and do it for real. */
12715 did_emsg = FALSE;
12716 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012717 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012718 if (!did_emsg)
12719 {
12720 s = alloc(len + 1);
12721 if (s != NULL)
12722 {
12723 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012724 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012725 }
12726 }
12727 did_emsg |= saved_did_emsg;
12728 }
12729#endif
12730}
12731
12732/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000012733 * "pumvisible()" function
12734 */
12735/*ARGSUSED*/
12736 static void
12737f_pumvisible(argvars, rettv)
12738 typval_T *argvars;
12739 typval_T *rettv;
12740{
12741 rettv->vval.v_number = 0;
12742#ifdef FEAT_INS_EXPAND
12743 if (pum_visible())
12744 rettv->vval.v_number = 1;
12745#endif
12746}
12747
12748/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012749 * "range()" function
12750 */
12751 static void
12752f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012753 typval_T *argvars;
12754 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012755{
12756 long start;
12757 long end;
12758 long stride = 1;
12759 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012760 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012761
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012762 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012763 if (argvars[1].v_type == VAR_UNKNOWN)
12764 {
12765 end = start - 1;
12766 start = 0;
12767 }
12768 else
12769 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012770 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012771 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012772 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012773 }
12774
12775 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012776 if (error)
12777 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000012778 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012779 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000012780 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012781 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012782 else
12783 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012784 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012785 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012786 if (list_append_number(rettv->vval.v_list,
12787 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012788 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012789 }
12790}
12791
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012792/*
12793 * "readfile()" function
12794 */
12795 static void
12796f_readfile(argvars, rettv)
12797 typval_T *argvars;
12798 typval_T *rettv;
12799{
12800 int binary = FALSE;
12801 char_u *fname;
12802 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012803 listitem_T *li;
12804#define FREAD_SIZE 200 /* optimized for text lines */
12805 char_u buf[FREAD_SIZE];
12806 int readlen; /* size of last fread() */
12807 int buflen; /* nr of valid chars in buf[] */
12808 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
12809 int tolist; /* first byte in buf[] still to be put in list */
12810 int chop; /* how many CR to chop off */
12811 char_u *prev = NULL; /* previously read bytes, if any */
12812 int prevlen = 0; /* length of "prev" if not NULL */
12813 char_u *s;
12814 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012815 long maxline = MAXLNUM;
12816 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012817
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012818 if (argvars[1].v_type != VAR_UNKNOWN)
12819 {
12820 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
12821 binary = TRUE;
12822 if (argvars[2].v_type != VAR_UNKNOWN)
12823 maxline = get_tv_number(&argvars[2]);
12824 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012825
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012826 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012827 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012828
12829 /* Always open the file in binary mode, library functions have a mind of
12830 * their own about CR-LF conversion. */
12831 fname = get_tv_string(&argvars[0]);
12832 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
12833 {
12834 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
12835 return;
12836 }
12837
12838 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012839 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012840 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012841 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012842 buflen = filtd + readlen;
12843 tolist = 0;
12844 for ( ; filtd < buflen || readlen <= 0; ++filtd)
12845 {
12846 if (buf[filtd] == '\n' || readlen <= 0)
12847 {
12848 /* Only when in binary mode add an empty list item when the
12849 * last line ends in a '\n'. */
12850 if (!binary && readlen == 0 && filtd == 0)
12851 break;
12852
12853 /* Found end-of-line or end-of-file: add a text line to the
12854 * list. */
12855 chop = 0;
12856 if (!binary)
12857 while (filtd - chop - 1 >= tolist
12858 && buf[filtd - chop - 1] == '\r')
12859 ++chop;
12860 len = filtd - tolist - chop;
12861 if (prev == NULL)
12862 s = vim_strnsave(buf + tolist, len);
12863 else
12864 {
12865 s = alloc((unsigned)(prevlen + len + 1));
12866 if (s != NULL)
12867 {
12868 mch_memmove(s, prev, prevlen);
12869 vim_free(prev);
12870 prev = NULL;
12871 mch_memmove(s + prevlen, buf + tolist, len);
12872 s[prevlen + len] = NUL;
12873 }
12874 }
12875 tolist = filtd + 1;
12876
12877 li = listitem_alloc();
12878 if (li == NULL)
12879 {
12880 vim_free(s);
12881 break;
12882 }
12883 li->li_tv.v_type = VAR_STRING;
12884 li->li_tv.v_lock = 0;
12885 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012886 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012887
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012888 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012889 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012890 if (readlen <= 0)
12891 break;
12892 }
12893 else if (buf[filtd] == NUL)
12894 buf[filtd] = '\n';
12895 }
12896 if (readlen <= 0)
12897 break;
12898
12899 if (tolist == 0)
12900 {
12901 /* "buf" is full, need to move text to an allocated buffer */
12902 if (prev == NULL)
12903 {
12904 prev = vim_strnsave(buf, buflen);
12905 prevlen = buflen;
12906 }
12907 else
12908 {
12909 s = alloc((unsigned)(prevlen + buflen));
12910 if (s != NULL)
12911 {
12912 mch_memmove(s, prev, prevlen);
12913 mch_memmove(s + prevlen, buf, buflen);
12914 vim_free(prev);
12915 prev = s;
12916 prevlen += buflen;
12917 }
12918 }
12919 filtd = 0;
12920 }
12921 else
12922 {
12923 mch_memmove(buf, buf + tolist, buflen - tolist);
12924 filtd -= tolist;
12925 }
12926 }
12927
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012928 /*
12929 * For a negative line count use only the lines at the end of the file,
12930 * free the rest.
12931 */
12932 if (maxline < 0)
12933 while (cnt > -maxline)
12934 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012935 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012936 --cnt;
12937 }
12938
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012939 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012940 fclose(fd);
12941}
12942
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012943#if defined(FEAT_RELTIME)
12944static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
12945
12946/*
12947 * Convert a List to proftime_T.
12948 * Return FAIL when there is something wrong.
12949 */
12950 static int
12951list2proftime(arg, tm)
12952 typval_T *arg;
12953 proftime_T *tm;
12954{
12955 long n1, n2;
12956 int error = FALSE;
12957
12958 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
12959 || arg->vval.v_list->lv_len != 2)
12960 return FAIL;
12961 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
12962 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
12963# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000012964 tm->HighPart = n1;
12965 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012966# else
12967 tm->tv_sec = n1;
12968 tm->tv_usec = n2;
12969# endif
12970 return error ? FAIL : OK;
12971}
12972#endif /* FEAT_RELTIME */
12973
12974/*
12975 * "reltime()" function
12976 */
12977 static void
12978f_reltime(argvars, rettv)
12979 typval_T *argvars;
12980 typval_T *rettv;
12981{
12982#ifdef FEAT_RELTIME
12983 proftime_T res;
12984 proftime_T start;
12985
12986 if (argvars[0].v_type == VAR_UNKNOWN)
12987 {
12988 /* No arguments: get current time. */
12989 profile_start(&res);
12990 }
12991 else if (argvars[1].v_type == VAR_UNKNOWN)
12992 {
12993 if (list2proftime(&argvars[0], &res) == FAIL)
12994 return;
12995 profile_end(&res);
12996 }
12997 else
12998 {
12999 /* Two arguments: compute the difference. */
13000 if (list2proftime(&argvars[0], &start) == FAIL
13001 || list2proftime(&argvars[1], &res) == FAIL)
13002 return;
13003 profile_sub(&res, &start);
13004 }
13005
13006 if (rettv_list_alloc(rettv) == OK)
13007 {
13008 long n1, n2;
13009
13010# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000013011 n1 = res.HighPart;
13012 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013013# else
13014 n1 = res.tv_sec;
13015 n2 = res.tv_usec;
13016# endif
13017 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
13018 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
13019 }
13020#endif
13021}
13022
13023/*
13024 * "reltimestr()" function
13025 */
13026 static void
13027f_reltimestr(argvars, rettv)
13028 typval_T *argvars;
13029 typval_T *rettv;
13030{
13031#ifdef FEAT_RELTIME
13032 proftime_T tm;
13033#endif
13034
13035 rettv->v_type = VAR_STRING;
13036 rettv->vval.v_string = NULL;
13037#ifdef FEAT_RELTIME
13038 if (list2proftime(&argvars[0], &tm) == OK)
13039 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
13040#endif
13041}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013042
Bram Moolenaar0d660222005-01-07 21:51:51 +000013043#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
13044static void make_connection __ARGS((void));
13045static int check_connection __ARGS((void));
13046
13047 static void
13048make_connection()
13049{
13050 if (X_DISPLAY == NULL
13051# ifdef FEAT_GUI
13052 && !gui.in_use
13053# endif
13054 )
13055 {
13056 x_force_connect = TRUE;
13057 setup_term_clip();
13058 x_force_connect = FALSE;
13059 }
13060}
13061
13062 static int
13063check_connection()
13064{
13065 make_connection();
13066 if (X_DISPLAY == NULL)
13067 {
13068 EMSG(_("E240: No connection to Vim server"));
13069 return FAIL;
13070 }
13071 return OK;
13072}
13073#endif
13074
13075#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000013076static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013077
13078 static void
13079remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000013080 typval_T *argvars;
13081 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013082 int expr;
13083{
13084 char_u *server_name;
13085 char_u *keys;
13086 char_u *r = NULL;
13087 char_u buf[NUMBUFLEN];
13088# ifdef WIN32
13089 HWND w;
13090# else
13091 Window w;
13092# endif
13093
13094 if (check_restricted() || check_secure())
13095 return;
13096
13097# ifdef FEAT_X11
13098 if (check_connection() == FAIL)
13099 return;
13100# endif
13101
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013102 server_name = get_tv_string_chk(&argvars[0]);
13103 if (server_name == NULL)
13104 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013105 keys = get_tv_string_buf(&argvars[1], buf);
13106# ifdef WIN32
13107 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
13108# else
13109 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
13110 < 0)
13111# endif
13112 {
13113 if (r != NULL)
13114 EMSG(r); /* sending worked but evaluation failed */
13115 else
13116 EMSG2(_("E241: Unable to send to %s"), server_name);
13117 return;
13118 }
13119
13120 rettv->vval.v_string = r;
13121
13122 if (argvars[2].v_type != VAR_UNKNOWN)
13123 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013124 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000013125 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013126 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013127
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013128 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000013129 v.di_tv.v_type = VAR_STRING;
13130 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013131 idvar = get_tv_string_chk(&argvars[2]);
13132 if (idvar != NULL)
13133 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000013134 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013135 }
13136}
13137#endif
13138
13139/*
13140 * "remote_expr()" function
13141 */
13142/*ARGSUSED*/
13143 static void
13144f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013145 typval_T *argvars;
13146 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013147{
13148 rettv->v_type = VAR_STRING;
13149 rettv->vval.v_string = NULL;
13150#ifdef FEAT_CLIENTSERVER
13151 remote_common(argvars, rettv, TRUE);
13152#endif
13153}
13154
13155/*
13156 * "remote_foreground()" function
13157 */
13158/*ARGSUSED*/
13159 static void
13160f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013161 typval_T *argvars;
13162 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013163{
13164 rettv->vval.v_number = 0;
13165#ifdef FEAT_CLIENTSERVER
13166# ifdef WIN32
13167 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013168 {
13169 char_u *server_name = get_tv_string_chk(&argvars[0]);
13170
13171 if (server_name != NULL)
13172 serverForeground(server_name);
13173 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013174# else
13175 /* Send a foreground() expression to the server. */
13176 argvars[1].v_type = VAR_STRING;
13177 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
13178 argvars[2].v_type = VAR_UNKNOWN;
13179 remote_common(argvars, rettv, TRUE);
13180 vim_free(argvars[1].vval.v_string);
13181# endif
13182#endif
13183}
13184
13185/*ARGSUSED*/
13186 static void
13187f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013188 typval_T *argvars;
13189 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013190{
13191#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000013192 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013193 char_u *s = NULL;
13194# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013195 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013196# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013197 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013198
13199 if (check_restricted() || check_secure())
13200 {
13201 rettv->vval.v_number = -1;
13202 return;
13203 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013204 serverid = get_tv_string_chk(&argvars[0]);
13205 if (serverid == NULL)
13206 {
13207 rettv->vval.v_number = -1;
13208 return; /* type error; errmsg already given */
13209 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013210# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013211 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013212 if (n == 0)
13213 rettv->vval.v_number = -1;
13214 else
13215 {
13216 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
13217 rettv->vval.v_number = (s != NULL);
13218 }
13219# else
13220 rettv->vval.v_number = 0;
13221 if (check_connection() == FAIL)
13222 return;
13223
13224 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013225 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013226# endif
13227
13228 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
13229 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013230 char_u *retvar;
13231
Bram Moolenaar33570922005-01-25 22:26:29 +000013232 v.di_tv.v_type = VAR_STRING;
13233 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013234 retvar = get_tv_string_chk(&argvars[1]);
13235 if (retvar != NULL)
13236 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000013237 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013238 }
13239#else
13240 rettv->vval.v_number = -1;
13241#endif
13242}
13243
13244/*ARGSUSED*/
13245 static void
13246f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013247 typval_T *argvars;
13248 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013249{
13250 char_u *r = NULL;
13251
13252#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013253 char_u *serverid = get_tv_string_chk(&argvars[0]);
13254
13255 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000013256 {
13257# ifdef WIN32
13258 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013259 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013260
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013261 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013262 if (n != 0)
13263 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
13264 if (r == NULL)
13265# else
13266 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013267 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013268# endif
13269 EMSG(_("E277: Unable to read a server reply"));
13270 }
13271#endif
13272 rettv->v_type = VAR_STRING;
13273 rettv->vval.v_string = r;
13274}
13275
13276/*
13277 * "remote_send()" function
13278 */
13279/*ARGSUSED*/
13280 static void
13281f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013282 typval_T *argvars;
13283 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013284{
13285 rettv->v_type = VAR_STRING;
13286 rettv->vval.v_string = NULL;
13287#ifdef FEAT_CLIENTSERVER
13288 remote_common(argvars, rettv, FALSE);
13289#endif
13290}
13291
13292/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013293 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013294 */
13295 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013296f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013297 typval_T *argvars;
13298 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013299{
Bram Moolenaar33570922005-01-25 22:26:29 +000013300 list_T *l;
13301 listitem_T *item, *item2;
13302 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013303 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013304 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013305 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000013306 dict_T *d;
13307 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013308
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013309 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013310 if (argvars[0].v_type == VAR_DICT)
13311 {
13312 if (argvars[2].v_type != VAR_UNKNOWN)
13313 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013314 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000013315 && !tv_check_lock(d->dv_lock, (char_u *)"remove() argument"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013316 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013317 key = get_tv_string_chk(&argvars[1]);
13318 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013319 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013320 di = dict_find(d, key, -1);
13321 if (di == NULL)
13322 EMSG2(_(e_dictkey), key);
13323 else
13324 {
13325 *rettv = di->di_tv;
13326 init_tv(&di->di_tv);
13327 dictitem_remove(d, di);
13328 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013329 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013330 }
13331 }
13332 else if (argvars[0].v_type != VAR_LIST)
13333 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013334 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000013335 && !tv_check_lock(l->lv_lock, (char_u *)"remove() argument"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013336 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013337 int error = FALSE;
13338
13339 idx = get_tv_number_chk(&argvars[1], &error);
13340 if (error)
13341 ; /* type error: do nothing, errmsg already given */
13342 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013343 EMSGN(_(e_listidx), idx);
13344 else
13345 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013346 if (argvars[2].v_type == VAR_UNKNOWN)
13347 {
13348 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013349 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013350 *rettv = item->li_tv;
13351 vim_free(item);
13352 }
13353 else
13354 {
13355 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013356 end = get_tv_number_chk(&argvars[2], &error);
13357 if (error)
13358 ; /* type error: do nothing */
13359 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013360 EMSGN(_(e_listidx), end);
13361 else
13362 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013363 int cnt = 0;
13364
13365 for (li = item; li != NULL; li = li->li_next)
13366 {
13367 ++cnt;
13368 if (li == item2)
13369 break;
13370 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013371 if (li == NULL) /* didn't find "item2" after "item" */
13372 EMSG(_(e_invrange));
13373 else
13374 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013375 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013376 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013377 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013378 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013379 l->lv_first = item;
13380 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013381 item->li_prev = NULL;
13382 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013383 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013384 }
13385 }
13386 }
13387 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013388 }
13389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013390}
13391
13392/*
13393 * "rename({from}, {to})" function
13394 */
13395 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013396f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013397 typval_T *argvars;
13398 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013399{
13400 char_u buf[NUMBUFLEN];
13401
13402 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013403 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013404 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013405 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
13406 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013407}
13408
13409/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013410 * "repeat()" function
13411 */
13412/*ARGSUSED*/
13413 static void
13414f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013415 typval_T *argvars;
13416 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013417{
13418 char_u *p;
13419 int n;
13420 int slen;
13421 int len;
13422 char_u *r;
13423 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013424
13425 n = get_tv_number(&argvars[1]);
13426 if (argvars[0].v_type == VAR_LIST)
13427 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013428 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013429 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013430 if (list_extend(rettv->vval.v_list,
13431 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013432 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013433 }
13434 else
13435 {
13436 p = get_tv_string(&argvars[0]);
13437 rettv->v_type = VAR_STRING;
13438 rettv->vval.v_string = NULL;
13439
13440 slen = (int)STRLEN(p);
13441 len = slen * n;
13442 if (len <= 0)
13443 return;
13444
13445 r = alloc(len + 1);
13446 if (r != NULL)
13447 {
13448 for (i = 0; i < n; i++)
13449 mch_memmove(r + i * slen, p, (size_t)slen);
13450 r[len] = NUL;
13451 }
13452
13453 rettv->vval.v_string = r;
13454 }
13455}
13456
13457/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013458 * "resolve()" function
13459 */
13460 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013461f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013462 typval_T *argvars;
13463 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013464{
13465 char_u *p;
13466
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013467 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013468#ifdef FEAT_SHORTCUT
13469 {
13470 char_u *v = NULL;
13471
13472 v = mch_resolve_shortcut(p);
13473 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013474 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013475 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013476 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013477 }
13478#else
13479# ifdef HAVE_READLINK
13480 {
13481 char_u buf[MAXPATHL + 1];
13482 char_u *cpy;
13483 int len;
13484 char_u *remain = NULL;
13485 char_u *q;
13486 int is_relative_to_current = FALSE;
13487 int has_trailing_pathsep = FALSE;
13488 int limit = 100;
13489
13490 p = vim_strsave(p);
13491
13492 if (p[0] == '.' && (vim_ispathsep(p[1])
13493 || (p[1] == '.' && (vim_ispathsep(p[2])))))
13494 is_relative_to_current = TRUE;
13495
13496 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013497 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013498 has_trailing_pathsep = TRUE;
13499
13500 q = getnextcomp(p);
13501 if (*q != NUL)
13502 {
13503 /* Separate the first path component in "p", and keep the
13504 * remainder (beginning with the path separator). */
13505 remain = vim_strsave(q - 1);
13506 q[-1] = NUL;
13507 }
13508
13509 for (;;)
13510 {
13511 for (;;)
13512 {
13513 len = readlink((char *)p, (char *)buf, MAXPATHL);
13514 if (len <= 0)
13515 break;
13516 buf[len] = NUL;
13517
13518 if (limit-- == 0)
13519 {
13520 vim_free(p);
13521 vim_free(remain);
13522 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013523 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013524 goto fail;
13525 }
13526
13527 /* Ensure that the result will have a trailing path separator
13528 * if the argument has one. */
13529 if (remain == NULL && has_trailing_pathsep)
13530 add_pathsep(buf);
13531
13532 /* Separate the first path component in the link value and
13533 * concatenate the remainders. */
13534 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
13535 if (*q != NUL)
13536 {
13537 if (remain == NULL)
13538 remain = vim_strsave(q - 1);
13539 else
13540 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000013541 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013542 if (cpy != NULL)
13543 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013544 vim_free(remain);
13545 remain = cpy;
13546 }
13547 }
13548 q[-1] = NUL;
13549 }
13550
13551 q = gettail(p);
13552 if (q > p && *q == NUL)
13553 {
13554 /* Ignore trailing path separator. */
13555 q[-1] = NUL;
13556 q = gettail(p);
13557 }
13558 if (q > p && !mch_isFullName(buf))
13559 {
13560 /* symlink is relative to directory of argument */
13561 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
13562 if (cpy != NULL)
13563 {
13564 STRCPY(cpy, p);
13565 STRCPY(gettail(cpy), buf);
13566 vim_free(p);
13567 p = cpy;
13568 }
13569 }
13570 else
13571 {
13572 vim_free(p);
13573 p = vim_strsave(buf);
13574 }
13575 }
13576
13577 if (remain == NULL)
13578 break;
13579
13580 /* Append the first path component of "remain" to "p". */
13581 q = getnextcomp(remain + 1);
13582 len = q - remain - (*q != NUL);
13583 cpy = vim_strnsave(p, STRLEN(p) + len);
13584 if (cpy != NULL)
13585 {
13586 STRNCAT(cpy, remain, len);
13587 vim_free(p);
13588 p = cpy;
13589 }
13590 /* Shorten "remain". */
13591 if (*q != NUL)
13592 STRCPY(remain, q - 1);
13593 else
13594 {
13595 vim_free(remain);
13596 remain = NULL;
13597 }
13598 }
13599
13600 /* If the result is a relative path name, make it explicitly relative to
13601 * the current directory if and only if the argument had this form. */
13602 if (!vim_ispathsep(*p))
13603 {
13604 if (is_relative_to_current
13605 && *p != NUL
13606 && !(p[0] == '.'
13607 && (p[1] == NUL
13608 || vim_ispathsep(p[1])
13609 || (p[1] == '.'
13610 && (p[2] == NUL
13611 || vim_ispathsep(p[2]))))))
13612 {
13613 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013614 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013615 if (cpy != NULL)
13616 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013617 vim_free(p);
13618 p = cpy;
13619 }
13620 }
13621 else if (!is_relative_to_current)
13622 {
13623 /* Strip leading "./". */
13624 q = p;
13625 while (q[0] == '.' && vim_ispathsep(q[1]))
13626 q += 2;
13627 if (q > p)
13628 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
13629 }
13630 }
13631
13632 /* Ensure that the result will have no trailing path separator
13633 * if the argument had none. But keep "/" or "//". */
13634 if (!has_trailing_pathsep)
13635 {
13636 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013637 if (after_pathsep(p, q))
13638 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013639 }
13640
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013641 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013642 }
13643# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013644 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013645# endif
13646#endif
13647
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013648 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013649
13650#ifdef HAVE_READLINK
13651fail:
13652#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013653 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013654}
13655
13656/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013657 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013658 */
13659 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013660f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013661 typval_T *argvars;
13662 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013663{
Bram Moolenaar33570922005-01-25 22:26:29 +000013664 list_T *l;
13665 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013666
Bram Moolenaar0d660222005-01-07 21:51:51 +000013667 rettv->vval.v_number = 0;
13668 if (argvars[0].v_type != VAR_LIST)
13669 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013670 else if ((l = argvars[0].vval.v_list) != NULL
13671 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013672 {
13673 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013674 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013675 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013676 while (li != NULL)
13677 {
13678 ni = li->li_prev;
13679 list_append(l, li);
13680 li = ni;
13681 }
13682 rettv->vval.v_list = l;
13683 rettv->v_type = VAR_LIST;
13684 ++l->lv_refcount;
13685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013686}
13687
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013688#define SP_NOMOVE 0x01 /* don't move cursor */
13689#define SP_REPEAT 0x02 /* repeat to find outer pair */
13690#define SP_RETCOUNT 0x04 /* return matchcount */
13691#define SP_SETPCMARK 0x08 /* set previous context mark */
13692#define SP_START 0x10 /* accept match at start position */
13693#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
13694#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013695
Bram Moolenaar33570922005-01-25 22:26:29 +000013696static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013697
13698/*
13699 * Get flags for a search function.
13700 * Possibly sets "p_ws".
13701 * Returns BACKWARD, FORWARD or zero (for an error).
13702 */
13703 static int
13704get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013705 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013706 int *flagsp;
13707{
13708 int dir = FORWARD;
13709 char_u *flags;
13710 char_u nbuf[NUMBUFLEN];
13711 int mask;
13712
13713 if (varp->v_type != VAR_UNKNOWN)
13714 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013715 flags = get_tv_string_buf_chk(varp, nbuf);
13716 if (flags == NULL)
13717 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013718 while (*flags != NUL)
13719 {
13720 switch (*flags)
13721 {
13722 case 'b': dir = BACKWARD; break;
13723 case 'w': p_ws = TRUE; break;
13724 case 'W': p_ws = FALSE; break;
13725 default: mask = 0;
13726 if (flagsp != NULL)
13727 switch (*flags)
13728 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013729 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013730 case 'e': mask = SP_END; break;
13731 case 'm': mask = SP_RETCOUNT; break;
13732 case 'n': mask = SP_NOMOVE; break;
13733 case 'p': mask = SP_SUBPAT; break;
13734 case 'r': mask = SP_REPEAT; break;
13735 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013736 }
13737 if (mask == 0)
13738 {
13739 EMSG2(_(e_invarg2), flags);
13740 dir = 0;
13741 }
13742 else
13743 *flagsp |= mask;
13744 }
13745 if (dir == 0)
13746 break;
13747 ++flags;
13748 }
13749 }
13750 return dir;
13751}
13752
Bram Moolenaar071d4272004-06-13 20:20:40 +000013753/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013754 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000013755 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013756 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013757search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013758 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013759 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013760 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013761{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013762 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013763 char_u *pat;
13764 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013765 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013766 int save_p_ws = p_ws;
13767 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013768 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013769 long lnum_stop = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013770 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013771 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013772
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013773 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013774 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013775 if (dir == 0)
13776 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013777 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013778 if (flags & SP_START)
13779 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013780 if (flags & SP_END)
13781 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013782
13783 /* Optional extra argument: line number to stop searching. */
13784 if (argvars[1].v_type != VAR_UNKNOWN
13785 && argvars[2].v_type != VAR_UNKNOWN)
13786 {
13787 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
13788 if (lnum_stop < 0)
13789 goto theend;
13790 }
13791
Bram Moolenaar231334e2005-07-25 20:46:57 +000013792 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013793 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000013794 * Check to make sure only those flags are set.
13795 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
13796 * flags cannot be set. Check for that condition also.
13797 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013798 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013799 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013800 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013801 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013802 goto theend;
13803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013804
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013805 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013806 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
13807 options, RE_SEARCH, (linenr_T)lnum_stop);
13808 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013809 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013810 if (flags & SP_SUBPAT)
13811 retval = subpatnum;
13812 else
13813 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013814 if (flags & SP_SETPCMARK)
13815 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013816 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013817 if (match_pos != NULL)
13818 {
13819 /* Store the match cursor position */
13820 match_pos->lnum = pos.lnum;
13821 match_pos->col = pos.col + 1;
13822 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013823 /* "/$" will put the cursor after the end of the line, may need to
13824 * correct that here */
13825 check_cursor();
13826 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013827
13828 /* If 'n' flag is used: restore cursor position. */
13829 if (flags & SP_NOMOVE)
13830 curwin->w_cursor = save_cursor;
13831theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000013832 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013833
13834 return retval;
13835}
13836
13837/*
13838 * "search()" function
13839 */
13840 static void
13841f_search(argvars, rettv)
13842 typval_T *argvars;
13843 typval_T *rettv;
13844{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013845 int flags = 0;
13846
13847 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013848}
13849
Bram Moolenaar071d4272004-06-13 20:20:40 +000013850/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013851 * "searchdecl()" function
13852 */
13853 static void
13854f_searchdecl(argvars, rettv)
13855 typval_T *argvars;
13856 typval_T *rettv;
13857{
13858 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013859 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013860 int error = FALSE;
13861 char_u *name;
13862
13863 rettv->vval.v_number = 1; /* default: FAIL */
13864
13865 name = get_tv_string_chk(&argvars[0]);
13866 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000013867 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013868 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013869 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13870 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
13871 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013872 if (!error && name != NULL)
13873 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000013874 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013875}
13876
13877/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013878 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000013879 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013880 static int
13881searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000013882 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013883 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013884{
13885 char_u *spat, *mpat, *epat;
13886 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013887 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013888 int dir;
13889 int flags = 0;
13890 char_u nbuf1[NUMBUFLEN];
13891 char_u nbuf2[NUMBUFLEN];
13892 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013893 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013894 long lnum_stop = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013895
Bram Moolenaar071d4272004-06-13 20:20:40 +000013896 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013897 spat = get_tv_string_chk(&argvars[0]);
13898 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
13899 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
13900 if (spat == NULL || mpat == NULL || epat == NULL)
13901 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013902
Bram Moolenaar071d4272004-06-13 20:20:40 +000013903 /* Handle the optional fourth argument: flags */
13904 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013905 if (dir == 0)
13906 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013907
13908 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000013909 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
13910 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013911 if ((flags & (SP_END | SP_SUBPAT)) != 0
13912 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000013913 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013914 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000013915 goto theend;
13916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013917
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013918 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013919 if (argvars[3].v_type == VAR_UNKNOWN
13920 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013921 skip = (char_u *)"";
13922 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013923 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013924 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013925 if (argvars[5].v_type != VAR_UNKNOWN)
13926 {
13927 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
13928 if (lnum_stop < 0)
13929 goto theend;
13930 }
13931 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013932 if (skip == NULL)
13933 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013934
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013935 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
13936 match_pos, lnum_stop);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013937
13938theend:
13939 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013940
13941 return retval;
13942}
13943
13944/*
13945 * "searchpair()" function
13946 */
13947 static void
13948f_searchpair(argvars, rettv)
13949 typval_T *argvars;
13950 typval_T *rettv;
13951{
13952 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
13953}
13954
13955/*
13956 * "searchpairpos()" function
13957 */
13958 static void
13959f_searchpairpos(argvars, rettv)
13960 typval_T *argvars;
13961 typval_T *rettv;
13962{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013963 pos_T match_pos;
13964 int lnum = 0;
13965 int col = 0;
13966
13967 rettv->vval.v_number = 0;
13968
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013969 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013970 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013971
13972 if (searchpair_cmn(argvars, &match_pos) > 0)
13973 {
13974 lnum = match_pos.lnum;
13975 col = match_pos.col;
13976 }
13977
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013978 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
13979 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013980}
13981
13982/*
13983 * Search for a start/middle/end thing.
13984 * Used by searchpair(), see its documentation for the details.
13985 * Returns 0 or -1 for no match,
13986 */
13987 long
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013988do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos, lnum_stop)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013989 char_u *spat; /* start pattern */
13990 char_u *mpat; /* middle pattern */
13991 char_u *epat; /* end pattern */
13992 int dir; /* BACKWARD or FORWARD */
13993 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013994 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013995 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013996 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013997{
13998 char_u *save_cpo;
13999 char_u *pat, *pat2 = NULL, *pat3 = NULL;
14000 long retval = 0;
14001 pos_T pos;
14002 pos_T firstpos;
14003 pos_T foundpos;
14004 pos_T save_cursor;
14005 pos_T save_pos;
14006 int n;
14007 int r;
14008 int nest = 1;
14009 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014010 int options = SEARCH_KEEP;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014011
14012 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14013 save_cpo = p_cpo;
14014 p_cpo = (char_u *)"";
14015
14016 /* Make two search patterns: start/end (pat2, for in nested pairs) and
14017 * start/middle/end (pat3, for the top pair). */
14018 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
14019 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
14020 if (pat2 == NULL || pat3 == NULL)
14021 goto theend;
14022 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
14023 if (*mpat == NUL)
14024 STRCPY(pat3, pat2);
14025 else
14026 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
14027 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014028 if (flags & SP_START)
14029 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014030
Bram Moolenaar071d4272004-06-13 20:20:40 +000014031 save_cursor = curwin->w_cursor;
14032 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000014033 clearpos(&firstpos);
14034 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014035 pat = pat3;
14036 for (;;)
14037 {
14038 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014039 options, RE_SEARCH, lnum_stop);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014040 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
14041 /* didn't find it or found the first match again: FAIL */
14042 break;
14043
14044 if (firstpos.lnum == 0)
14045 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000014046 if (equalpos(pos, foundpos))
14047 {
14048 /* Found the same position again. Can happen with a pattern that
14049 * has "\zs" at the end and searching backwards. Advance one
14050 * character and try again. */
14051 if (dir == BACKWARD)
14052 decl(&pos);
14053 else
14054 incl(&pos);
14055 }
14056 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014057
14058 /* If the skip pattern matches, ignore this match. */
14059 if (*skip != NUL)
14060 {
14061 save_pos = curwin->w_cursor;
14062 curwin->w_cursor = pos;
14063 r = eval_to_bool(skip, &err, NULL, FALSE);
14064 curwin->w_cursor = save_pos;
14065 if (err)
14066 {
14067 /* Evaluating {skip} caused an error, break here. */
14068 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014069 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014070 break;
14071 }
14072 if (r)
14073 continue;
14074 }
14075
14076 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
14077 {
14078 /* Found end when searching backwards or start when searching
14079 * forward: nested pair. */
14080 ++nest;
14081 pat = pat2; /* nested, don't search for middle */
14082 }
14083 else
14084 {
14085 /* Found end when searching forward or start when searching
14086 * backward: end of (nested) pair; or found middle in outer pair. */
14087 if (--nest == 1)
14088 pat = pat3; /* outer level, search for middle */
14089 }
14090
14091 if (nest == 0)
14092 {
14093 /* Found the match: return matchcount or line number. */
14094 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014095 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014096 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014097 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000014098 if (flags & SP_SETPCMARK)
14099 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014100 curwin->w_cursor = pos;
14101 if (!(flags & SP_REPEAT))
14102 break;
14103 nest = 1; /* search for next unmatched */
14104 }
14105 }
14106
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014107 if (match_pos != NULL)
14108 {
14109 /* Store the match cursor position */
14110 match_pos->lnum = curwin->w_cursor.lnum;
14111 match_pos->col = curwin->w_cursor.col + 1;
14112 }
14113
Bram Moolenaar071d4272004-06-13 20:20:40 +000014114 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014115 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014116 curwin->w_cursor = save_cursor;
14117
14118theend:
14119 vim_free(pat2);
14120 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014121 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014122
14123 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014124}
14125
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014126/*
14127 * "searchpos()" function
14128 */
14129 static void
14130f_searchpos(argvars, rettv)
14131 typval_T *argvars;
14132 typval_T *rettv;
14133{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014134 pos_T match_pos;
14135 int lnum = 0;
14136 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014137 int n;
14138 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014139
14140 rettv->vval.v_number = 0;
14141
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014142 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014143 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014144
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014145 n = search_cmn(argvars, &match_pos, &flags);
14146 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014147 {
14148 lnum = match_pos.lnum;
14149 col = match_pos.col;
14150 }
14151
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014152 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
14153 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014154 if (flags & SP_SUBPAT)
14155 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014156}
14157
14158
Bram Moolenaar0d660222005-01-07 21:51:51 +000014159/*ARGSUSED*/
14160 static void
14161f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014162 typval_T *argvars;
14163 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014164{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014165#ifdef FEAT_CLIENTSERVER
14166 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014167 char_u *server = get_tv_string_chk(&argvars[0]);
14168 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014169
Bram Moolenaar0d660222005-01-07 21:51:51 +000014170 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014171 if (server == NULL || reply == NULL)
14172 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014173 if (check_restricted() || check_secure())
14174 return;
14175# ifdef FEAT_X11
14176 if (check_connection() == FAIL)
14177 return;
14178# endif
14179
14180 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014181 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014182 EMSG(_("E258: Unable to send to client"));
14183 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014184 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014185 rettv->vval.v_number = 0;
14186#else
14187 rettv->vval.v_number = -1;
14188#endif
14189}
14190
14191/*ARGSUSED*/
14192 static void
14193f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014194 typval_T *argvars;
14195 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014196{
14197 char_u *r = NULL;
14198
14199#ifdef FEAT_CLIENTSERVER
14200# ifdef WIN32
14201 r = serverGetVimNames();
14202# else
14203 make_connection();
14204 if (X_DISPLAY != NULL)
14205 r = serverGetVimNames(X_DISPLAY);
14206# endif
14207#endif
14208 rettv->v_type = VAR_STRING;
14209 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014210}
14211
14212/*
14213 * "setbufvar()" function
14214 */
14215/*ARGSUSED*/
14216 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014217f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014218 typval_T *argvars;
14219 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014220{
14221 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014222 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014223 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014224 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014225 char_u nbuf[NUMBUFLEN];
14226
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014227 rettv->vval.v_number = 0;
14228
Bram Moolenaar071d4272004-06-13 20:20:40 +000014229 if (check_restricted() || check_secure())
14230 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014231 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
14232 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014233 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014234 varp = &argvars[2];
14235
14236 if (buf != NULL && varname != NULL && varp != NULL)
14237 {
14238 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014239 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014240
14241 if (*varname == '&')
14242 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014243 long numval;
14244 char_u *strval;
14245 int error = FALSE;
14246
Bram Moolenaar071d4272004-06-13 20:20:40 +000014247 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014248 numval = get_tv_number_chk(varp, &error);
14249 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014250 if (!error && strval != NULL)
14251 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014252 }
14253 else
14254 {
14255 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
14256 if (bufvarname != NULL)
14257 {
14258 STRCPY(bufvarname, "b:");
14259 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014260 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014261 vim_free(bufvarname);
14262 }
14263 }
14264
14265 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014266 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014267 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014268}
14269
14270/*
14271 * "setcmdpos()" function
14272 */
14273 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014274f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014275 typval_T *argvars;
14276 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014277{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014278 int pos = (int)get_tv_number(&argvars[0]) - 1;
14279
14280 if (pos >= 0)
14281 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014282}
14283
14284/*
14285 * "setline()" function
14286 */
14287 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014288f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014289 typval_T *argvars;
14290 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014291{
14292 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000014293 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014294 list_T *l = NULL;
14295 listitem_T *li = NULL;
14296 long added = 0;
14297 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014298
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014299 lnum = get_tv_lnum(&argvars[0]);
14300 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014301 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014302 l = argvars[1].vval.v_list;
14303 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014304 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014305 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014306 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014307
14308 rettv->vval.v_number = 0; /* OK */
14309 for (;;)
14310 {
14311 if (l != NULL)
14312 {
14313 /* list argument, get next string */
14314 if (li == NULL)
14315 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014316 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014317 li = li->li_next;
14318 }
14319
14320 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014321 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014322 break;
14323 if (lnum <= curbuf->b_ml.ml_line_count)
14324 {
14325 /* existing line, replace it */
14326 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
14327 {
14328 changed_bytes(lnum, 0);
14329 check_cursor_col();
14330 rettv->vval.v_number = 0; /* OK */
14331 }
14332 }
14333 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
14334 {
14335 /* lnum is one past the last line, append the line */
14336 ++added;
14337 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
14338 rettv->vval.v_number = 0; /* OK */
14339 }
14340
14341 if (l == NULL) /* only one string argument */
14342 break;
14343 ++lnum;
14344 }
14345
14346 if (added > 0)
14347 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014348}
14349
14350/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014351 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000014352 */
14353/*ARGSUSED*/
14354 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014355set_qf_ll_list(wp, list_arg, action_arg, rettv)
14356 win_T *wp;
14357 typval_T *list_arg;
14358 typval_T *action_arg;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014359 typval_T *rettv;
14360{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014361#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014362 char_u *act;
14363 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014364#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014365
Bram Moolenaar2641f772005-03-25 21:58:17 +000014366 rettv->vval.v_number = -1;
14367
14368#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014369 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014370 EMSG(_(e_listreq));
14371 else
14372 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014373 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014374
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014375 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014376 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014377 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014378 if (act == NULL)
14379 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014380 if (*act == 'a' || *act == 'r')
14381 action = *act;
14382 }
14383
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014384 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014385 rettv->vval.v_number = 0;
14386 }
14387#endif
14388}
14389
14390/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014391 * "setloclist()" function
14392 */
14393/*ARGSUSED*/
14394 static void
14395f_setloclist(argvars, rettv)
14396 typval_T *argvars;
14397 typval_T *rettv;
14398{
14399 win_T *win;
14400
14401 rettv->vval.v_number = -1;
14402
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014403 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014404 if (win != NULL)
14405 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
14406}
14407
14408/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014409 * "setpos()" function
14410 */
14411/*ARGSUSED*/
14412 static void
14413f_setpos(argvars, rettv)
14414 typval_T *argvars;
14415 typval_T *rettv;
14416{
14417 pos_T pos;
14418 int fnum;
14419 char_u *name;
14420
14421 name = get_tv_string_chk(argvars);
14422 if (name != NULL)
14423 {
14424 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
14425 {
14426 --pos.col;
14427 if (name[0] == '.') /* cursor */
14428 {
14429 if (fnum == curbuf->b_fnum)
14430 {
14431 curwin->w_cursor = pos;
14432 check_cursor();
14433 }
14434 else
14435 EMSG(_(e_invarg));
14436 }
14437 else if (name[0] == '\'') /* mark */
14438 (void)setmark_pos(name[1], &pos, fnum);
14439 else
14440 EMSG(_(e_invarg));
14441 }
14442 }
14443}
14444
14445/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014446 * "setqflist()" function
14447 */
14448/*ARGSUSED*/
14449 static void
14450f_setqflist(argvars, rettv)
14451 typval_T *argvars;
14452 typval_T *rettv;
14453{
14454 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
14455}
14456
14457/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014458 * "setreg()" function
14459 */
14460 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014461f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014462 typval_T *argvars;
14463 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014464{
14465 int regname;
14466 char_u *strregname;
14467 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014468 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014469 int append;
14470 char_u yank_type;
14471 long block_len;
14472
14473 block_len = -1;
14474 yank_type = MAUTO;
14475 append = FALSE;
14476
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014477 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014478 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014479
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014480 if (strregname == NULL)
14481 return; /* type error; errmsg already given */
14482 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014483 if (regname == 0 || regname == '@')
14484 regname = '"';
14485 else if (regname == '=')
14486 return;
14487
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014488 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014489 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014490 stropt = get_tv_string_chk(&argvars[2]);
14491 if (stropt == NULL)
14492 return; /* type error */
14493 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014494 switch (*stropt)
14495 {
14496 case 'a': case 'A': /* append */
14497 append = TRUE;
14498 break;
14499 case 'v': case 'c': /* character-wise selection */
14500 yank_type = MCHAR;
14501 break;
14502 case 'V': case 'l': /* line-wise selection */
14503 yank_type = MLINE;
14504 break;
14505#ifdef FEAT_VISUAL
14506 case 'b': case Ctrl_V: /* block-wise selection */
14507 yank_type = MBLOCK;
14508 if (VIM_ISDIGIT(stropt[1]))
14509 {
14510 ++stropt;
14511 block_len = getdigits(&stropt) - 1;
14512 --stropt;
14513 }
14514 break;
14515#endif
14516 }
14517 }
14518
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014519 strval = get_tv_string_chk(&argvars[1]);
14520 if (strval != NULL)
14521 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000014522 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014523 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014524}
14525
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014526/*
14527 * "settabwinvar()" function
14528 */
14529 static void
14530f_settabwinvar(argvars, rettv)
14531 typval_T *argvars;
14532 typval_T *rettv;
14533{
14534 setwinvar(argvars, rettv, 1);
14535}
Bram Moolenaar071d4272004-06-13 20:20:40 +000014536
14537/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014538 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014539 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014540 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014541f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014542 typval_T *argvars;
14543 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014544{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014545 setwinvar(argvars, rettv, 0);
14546}
14547
14548/*
14549 * "setwinvar()" and "settabwinvar()" functions
14550 */
14551 static void
14552setwinvar(argvars, rettv, off)
14553 typval_T *argvars;
14554 typval_T *rettv;
14555 int off;
14556{
Bram Moolenaar071d4272004-06-13 20:20:40 +000014557 win_T *win;
14558#ifdef FEAT_WINDOWS
14559 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014560 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014561#endif
14562 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014563 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014564 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014565 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014566
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014567 rettv->vval.v_number = 0;
14568
Bram Moolenaar071d4272004-06-13 20:20:40 +000014569 if (check_restricted() || check_secure())
14570 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014571
14572#ifdef FEAT_WINDOWS
14573 if (off == 1)
14574 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
14575 else
14576 tp = curtab;
14577#endif
14578 win = find_win_by_nr(&argvars[off], tp);
14579 varname = get_tv_string_chk(&argvars[off + 1]);
14580 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014581
14582 if (win != NULL && varname != NULL && varp != NULL)
14583 {
14584#ifdef FEAT_WINDOWS
14585 /* set curwin to be our win, temporarily */
14586 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014587 save_curtab = curtab;
14588 goto_tabpage_tp(tp);
14589 if (!win_valid(win))
14590 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014591 curwin = win;
14592 curbuf = curwin->w_buffer;
14593#endif
14594
14595 if (*varname == '&')
14596 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014597 long numval;
14598 char_u *strval;
14599 int error = FALSE;
14600
Bram Moolenaar071d4272004-06-13 20:20:40 +000014601 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014602 numval = get_tv_number_chk(varp, &error);
14603 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014604 if (!error && strval != NULL)
14605 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014606 }
14607 else
14608 {
14609 winvarname = alloc((unsigned)STRLEN(varname) + 3);
14610 if (winvarname != NULL)
14611 {
14612 STRCPY(winvarname, "w:");
14613 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014614 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014615 vim_free(winvarname);
14616 }
14617 }
14618
14619#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014620 /* Restore current tabpage and window, if still valid (autocomands can
14621 * make them invalid). */
14622 if (valid_tabpage(save_curtab))
14623 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014624 if (win_valid(save_curwin))
14625 {
14626 curwin = save_curwin;
14627 curbuf = curwin->w_buffer;
14628 }
14629#endif
14630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014631}
14632
14633/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000014634 * "shellescape({string})" function
14635 */
14636 static void
14637f_shellescape(argvars, rettv)
14638 typval_T *argvars;
14639 typval_T *rettv;
14640{
14641 rettv->vval.v_string = vim_strsave_shellescape(get_tv_string(&argvars[0]));
14642 rettv->v_type = VAR_STRING;
14643}
14644
14645/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014646 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014647 */
14648 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014649f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014650 typval_T *argvars;
14651 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014652{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014653 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014654
Bram Moolenaar0d660222005-01-07 21:51:51 +000014655 p = get_tv_string(&argvars[0]);
14656 rettv->vval.v_string = vim_strsave(p);
14657 simplify_filename(rettv->vval.v_string); /* simplify in place */
14658 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014659}
14660
Bram Moolenaar0d660222005-01-07 21:51:51 +000014661static int
14662#ifdef __BORLANDC__
14663 _RTLENTRYF
14664#endif
14665 item_compare __ARGS((const void *s1, const void *s2));
14666static int
14667#ifdef __BORLANDC__
14668 _RTLENTRYF
14669#endif
14670 item_compare2 __ARGS((const void *s1, const void *s2));
14671
14672static int item_compare_ic;
14673static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014674static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014675#define ITEM_COMPARE_FAIL 999
14676
Bram Moolenaar071d4272004-06-13 20:20:40 +000014677/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014678 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014679 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014680 static int
14681#ifdef __BORLANDC__
14682_RTLENTRYF
14683#endif
14684item_compare(s1, s2)
14685 const void *s1;
14686 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014687{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014688 char_u *p1, *p2;
14689 char_u *tofree1, *tofree2;
14690 int res;
14691 char_u numbuf1[NUMBUFLEN];
14692 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014693
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014694 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
14695 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014696 if (item_compare_ic)
14697 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014698 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014699 res = STRCMP(p1, p2);
14700 vim_free(tofree1);
14701 vim_free(tofree2);
14702 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014703}
14704
14705 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000014706#ifdef __BORLANDC__
14707_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014708#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000014709item_compare2(s1, s2)
14710 const void *s1;
14711 const void *s2;
14712{
14713 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000014714 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014715 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000014716 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014717
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014718 /* shortcut after failure in previous call; compare all items equal */
14719 if (item_compare_func_err)
14720 return 0;
14721
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014722 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
14723 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014724 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
14725 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014726
14727 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014728 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000014729 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014730 clear_tv(&argv[0]);
14731 clear_tv(&argv[1]);
14732
14733 if (res == FAIL)
14734 res = ITEM_COMPARE_FAIL;
14735 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014736 /* return value has wrong type */
14737 res = get_tv_number_chk(&rettv, &item_compare_func_err);
14738 if (item_compare_func_err)
14739 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014740 clear_tv(&rettv);
14741 return res;
14742}
14743
14744/*
14745 * "sort({list})" function
14746 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014747 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014748f_sort(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{
Bram Moolenaar33570922005-01-25 22:26:29 +000014752 list_T *l;
14753 listitem_T *li;
14754 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014755 long len;
14756 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014757
Bram Moolenaar0d660222005-01-07 21:51:51 +000014758 rettv->vval.v_number = 0;
14759 if (argvars[0].v_type != VAR_LIST)
14760 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000014761 else
14762 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014763 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014764 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014765 return;
14766 rettv->vval.v_list = l;
14767 rettv->v_type = VAR_LIST;
14768 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014769
Bram Moolenaar0d660222005-01-07 21:51:51 +000014770 len = list_len(l);
14771 if (len <= 1)
14772 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014773
Bram Moolenaar0d660222005-01-07 21:51:51 +000014774 item_compare_ic = FALSE;
14775 item_compare_func = NULL;
14776 if (argvars[1].v_type != VAR_UNKNOWN)
14777 {
14778 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014779 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014780 else
14781 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014782 int error = FALSE;
14783
14784 i = get_tv_number_chk(&argvars[1], &error);
14785 if (error)
14786 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014787 if (i == 1)
14788 item_compare_ic = TRUE;
14789 else
14790 item_compare_func = get_tv_string(&argvars[1]);
14791 }
14792 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014793
Bram Moolenaar0d660222005-01-07 21:51:51 +000014794 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014795 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014796 if (ptrs == NULL)
14797 return;
14798 i = 0;
14799 for (li = l->lv_first; li != NULL; li = li->li_next)
14800 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014801
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014802 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014803 /* test the compare function */
14804 if (item_compare_func != NULL
14805 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
14806 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014807 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014808 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014809 {
14810 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014811 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000014812 item_compare_func == NULL ? item_compare : item_compare2);
14813
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014814 if (!item_compare_func_err)
14815 {
14816 /* Clear the List and append the items in the sorted order. */
14817 l->lv_first = l->lv_last = NULL;
14818 l->lv_len = 0;
14819 for (i = 0; i < len; ++i)
14820 list_append(l, ptrs[i]);
14821 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014822 }
14823
14824 vim_free(ptrs);
14825 }
14826}
14827
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014828/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000014829 * "soundfold({word})" function
14830 */
14831 static void
14832f_soundfold(argvars, rettv)
14833 typval_T *argvars;
14834 typval_T *rettv;
14835{
14836 char_u *s;
14837
14838 rettv->v_type = VAR_STRING;
14839 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014840#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000014841 rettv->vval.v_string = eval_soundfold(s);
14842#else
14843 rettv->vval.v_string = vim_strsave(s);
14844#endif
14845}
14846
14847/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014848 * "spellbadword()" function
14849 */
14850/* ARGSUSED */
14851 static void
14852f_spellbadword(argvars, rettv)
14853 typval_T *argvars;
14854 typval_T *rettv;
14855{
Bram Moolenaar4463f292005-09-25 22:20:24 +000014856 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014857 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014858 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014859
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014860 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014861 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014862
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014863#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000014864 if (argvars[0].v_type == VAR_UNKNOWN)
14865 {
14866 /* Find the start and length of the badly spelled word. */
14867 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
14868 if (len != 0)
14869 word = ml_get_cursor();
14870 }
14871 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14872 {
14873 char_u *str = get_tv_string_chk(&argvars[0]);
14874 int capcol = -1;
14875
14876 if (str != NULL)
14877 {
14878 /* Check the argument for spelling. */
14879 while (*str != NUL)
14880 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014881 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014882 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014883 {
14884 word = str;
14885 break;
14886 }
14887 str += len;
14888 }
14889 }
14890 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014891#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000014892
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014893 list_append_string(rettv->vval.v_list, word, len);
14894 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014895 attr == HLF_SPB ? "bad" :
14896 attr == HLF_SPR ? "rare" :
14897 attr == HLF_SPL ? "local" :
14898 attr == HLF_SPC ? "caps" :
14899 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014900}
14901
14902/*
14903 * "spellsuggest()" function
14904 */
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014905/*ARGSUSED*/
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014906 static void
14907f_spellsuggest(argvars, rettv)
14908 typval_T *argvars;
14909 typval_T *rettv;
14910{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014911#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014912 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014913 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014914 int maxcount;
14915 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014916 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014917 listitem_T *li;
14918 int need_capital = FALSE;
14919#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014920
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014921 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014922 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014923
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014924#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014925 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14926 {
14927 str = get_tv_string(&argvars[0]);
14928 if (argvars[1].v_type != VAR_UNKNOWN)
14929 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014930 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014931 if (maxcount <= 0)
14932 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014933 if (argvars[2].v_type != VAR_UNKNOWN)
14934 {
14935 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
14936 if (typeerr)
14937 return;
14938 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014939 }
14940 else
14941 maxcount = 25;
14942
Bram Moolenaar4770d092006-01-12 23:22:24 +000014943 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014944
14945 for (i = 0; i < ga.ga_len; ++i)
14946 {
14947 str = ((char_u **)ga.ga_data)[i];
14948
14949 li = listitem_alloc();
14950 if (li == NULL)
14951 vim_free(str);
14952 else
14953 {
14954 li->li_tv.v_type = VAR_STRING;
14955 li->li_tv.v_lock = 0;
14956 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014957 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014958 }
14959 }
14960 ga_clear(&ga);
14961 }
14962#endif
14963}
14964
Bram Moolenaar0d660222005-01-07 21:51:51 +000014965 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014966f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014967 typval_T *argvars;
14968 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014969{
14970 char_u *str;
14971 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014972 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014973 regmatch_T regmatch;
14974 char_u patbuf[NUMBUFLEN];
14975 char_u *save_cpo;
14976 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014977 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014978 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014979 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014980
14981 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14982 save_cpo = p_cpo;
14983 p_cpo = (char_u *)"";
14984
14985 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014986 if (argvars[1].v_type != VAR_UNKNOWN)
14987 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014988 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14989 if (pat == NULL)
14990 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014991 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014992 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014993 }
14994 if (pat == NULL || *pat == NUL)
14995 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000014996
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014997 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014998 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014999 if (typeerr)
15000 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015001
Bram Moolenaar0d660222005-01-07 21:51:51 +000015002 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15003 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015004 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015005 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015006 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015007 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015008 if (*str == NUL)
15009 match = FALSE; /* empty item at the end */
15010 else
15011 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015012 if (match)
15013 end = regmatch.startp[0];
15014 else
15015 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015016 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
15017 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015018 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015019 if (list_append_string(rettv->vval.v_list, str,
15020 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015021 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015022 }
15023 if (!match)
15024 break;
15025 /* Advance to just after the match. */
15026 if (regmatch.endp[0] > str)
15027 col = 0;
15028 else
15029 {
15030 /* Don't get stuck at the same match. */
15031#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015032 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015033#else
15034 col = 1;
15035#endif
15036 }
15037 str = regmatch.endp[0];
15038 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015039
Bram Moolenaar0d660222005-01-07 21:51:51 +000015040 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015042
Bram Moolenaar0d660222005-01-07 21:51:51 +000015043 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015044}
15045
Bram Moolenaar2c932302006-03-18 21:42:09 +000015046/*
15047 * "str2nr()" function
15048 */
15049 static void
15050f_str2nr(argvars, rettv)
15051 typval_T *argvars;
15052 typval_T *rettv;
15053{
15054 int base = 10;
15055 char_u *p;
15056 long n;
15057
15058 if (argvars[1].v_type != VAR_UNKNOWN)
15059 {
15060 base = get_tv_number(&argvars[1]);
15061 if (base != 8 && base != 10 && base != 16)
15062 {
15063 EMSG(_(e_invarg));
15064 return;
15065 }
15066 }
15067
15068 p = skipwhite(get_tv_string(&argvars[0]));
15069 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
15070 rettv->vval.v_number = n;
15071}
15072
Bram Moolenaar071d4272004-06-13 20:20:40 +000015073#ifdef HAVE_STRFTIME
15074/*
15075 * "strftime({format}[, {time}])" function
15076 */
15077 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015078f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015079 typval_T *argvars;
15080 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015081{
15082 char_u result_buf[256];
15083 struct tm *curtime;
15084 time_t seconds;
15085 char_u *p;
15086
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015087 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015088
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015089 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015090 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015091 seconds = time(NULL);
15092 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015093 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015094 curtime = localtime(&seconds);
15095 /* MSVC returns NULL for an invalid value of seconds. */
15096 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015097 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015098 else
15099 {
15100# ifdef FEAT_MBYTE
15101 vimconv_T conv;
15102 char_u *enc;
15103
15104 conv.vc_type = CONV_NONE;
15105 enc = enc_locale();
15106 convert_setup(&conv, p_enc, enc);
15107 if (conv.vc_type != CONV_NONE)
15108 p = string_convert(&conv, p, NULL);
15109# endif
15110 if (p != NULL)
15111 (void)strftime((char *)result_buf, sizeof(result_buf),
15112 (char *)p, curtime);
15113 else
15114 result_buf[0] = NUL;
15115
15116# ifdef FEAT_MBYTE
15117 if (conv.vc_type != CONV_NONE)
15118 vim_free(p);
15119 convert_setup(&conv, enc, p_enc);
15120 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015121 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015122 else
15123# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015124 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015125
15126# ifdef FEAT_MBYTE
15127 /* Release conversion descriptors */
15128 convert_setup(&conv, NULL, NULL);
15129 vim_free(enc);
15130# endif
15131 }
15132}
15133#endif
15134
15135/*
15136 * "stridx()" function
15137 */
15138 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015139f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015140 typval_T *argvars;
15141 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015142{
15143 char_u buf[NUMBUFLEN];
15144 char_u *needle;
15145 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000015146 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015147 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000015148 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015149
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015150 needle = get_tv_string_chk(&argvars[1]);
15151 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000015152 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015153 if (needle == NULL || haystack == NULL)
15154 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015155
Bram Moolenaar33570922005-01-25 22:26:29 +000015156 if (argvars[2].v_type != VAR_UNKNOWN)
15157 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015158 int error = FALSE;
15159
15160 start_idx = get_tv_number_chk(&argvars[2], &error);
15161 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000015162 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015163 if (start_idx >= 0)
15164 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000015165 }
15166
15167 pos = (char_u *)strstr((char *)haystack, (char *)needle);
15168 if (pos != NULL)
15169 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015170}
15171
15172/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015173 * "string()" function
15174 */
15175 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015176f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015177 typval_T *argvars;
15178 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015179{
15180 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015181 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015182
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015183 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015184 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015185 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015186 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015187}
15188
15189/*
15190 * "strlen()" function
15191 */
15192 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015193f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015194 typval_T *argvars;
15195 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015196{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015197 rettv->vval.v_number = (varnumber_T)(STRLEN(
15198 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015199}
15200
15201/*
15202 * "strpart()" function
15203 */
15204 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015205f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015206 typval_T *argvars;
15207 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015208{
15209 char_u *p;
15210 int n;
15211 int len;
15212 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015213 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015214
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015215 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015216 slen = (int)STRLEN(p);
15217
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015218 n = get_tv_number_chk(&argvars[1], &error);
15219 if (error)
15220 len = 0;
15221 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015222 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015223 else
15224 len = slen - n; /* default len: all bytes that are available. */
15225
15226 /*
15227 * Only return the overlap between the specified part and the actual
15228 * string.
15229 */
15230 if (n < 0)
15231 {
15232 len += n;
15233 n = 0;
15234 }
15235 else if (n > slen)
15236 n = slen;
15237 if (len < 0)
15238 len = 0;
15239 else if (n + len > slen)
15240 len = slen - n;
15241
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015242 rettv->v_type = VAR_STRING;
15243 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015244}
15245
15246/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015247 * "strridx()" function
15248 */
15249 static void
15250f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015251 typval_T *argvars;
15252 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015253{
15254 char_u buf[NUMBUFLEN];
15255 char_u *needle;
15256 char_u *haystack;
15257 char_u *rest;
15258 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015259 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015260
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015261 needle = get_tv_string_chk(&argvars[1]);
15262 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015263
15264 rettv->vval.v_number = -1;
15265 if (needle == NULL || haystack == NULL)
15266 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015267
15268 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000015269 if (argvars[2].v_type != VAR_UNKNOWN)
15270 {
15271 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015272 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000015273 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015274 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000015275 }
15276 else
15277 end_idx = haystack_len;
15278
Bram Moolenaar0d660222005-01-07 21:51:51 +000015279 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000015280 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015281 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000015282 lastmatch = haystack + end_idx;
15283 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015284 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000015285 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015286 for (rest = haystack; *rest != '\0'; ++rest)
15287 {
15288 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000015289 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015290 break;
15291 lastmatch = rest;
15292 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000015293 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015294
15295 if (lastmatch == NULL)
15296 rettv->vval.v_number = -1;
15297 else
15298 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
15299}
15300
15301/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015302 * "strtrans()" function
15303 */
15304 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015305f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015306 typval_T *argvars;
15307 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015308{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015309 rettv->v_type = VAR_STRING;
15310 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015311}
15312
15313/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015314 * "submatch()" function
15315 */
15316 static void
15317f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015318 typval_T *argvars;
15319 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015320{
15321 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015322 rettv->vval.v_string =
15323 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015324}
15325
15326/*
15327 * "substitute()" function
15328 */
15329 static void
15330f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015331 typval_T *argvars;
15332 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015333{
15334 char_u patbuf[NUMBUFLEN];
15335 char_u subbuf[NUMBUFLEN];
15336 char_u flagsbuf[NUMBUFLEN];
15337
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015338 char_u *str = get_tv_string_chk(&argvars[0]);
15339 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15340 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
15341 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
15342
Bram Moolenaar0d660222005-01-07 21:51:51 +000015343 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015344 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
15345 rettv->vval.v_string = NULL;
15346 else
15347 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015348}
15349
15350/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015351 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015352 */
15353/*ARGSUSED*/
15354 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015355f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015356 typval_T *argvars;
15357 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015358{
15359 int id = 0;
15360#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015361 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015362 long col;
15363 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000015364 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015365
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015366 lnum = get_tv_lnum(argvars); /* -1 on type error */
15367 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
15368 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015369
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015370 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015371 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +000015372 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015373#endif
15374
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015375 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015376}
15377
15378/*
15379 * "synIDattr(id, what [, mode])" function
15380 */
15381/*ARGSUSED*/
15382 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015383f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015384 typval_T *argvars;
15385 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015386{
15387 char_u *p = NULL;
15388#ifdef FEAT_SYN_HL
15389 int id;
15390 char_u *what;
15391 char_u *mode;
15392 char_u modebuf[NUMBUFLEN];
15393 int modec;
15394
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015395 id = get_tv_number(&argvars[0]);
15396 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015397 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015398 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015399 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015400 modec = TOLOWER_ASC(mode[0]);
15401 if (modec != 't' && modec != 'c'
15402#ifdef FEAT_GUI
15403 && modec != 'g'
15404#endif
15405 )
15406 modec = 0; /* replace invalid with current */
15407 }
15408 else
15409 {
15410#ifdef FEAT_GUI
15411 if (gui.in_use)
15412 modec = 'g';
15413 else
15414#endif
15415 if (t_colors > 1)
15416 modec = 'c';
15417 else
15418 modec = 't';
15419 }
15420
15421
15422 switch (TOLOWER_ASC(what[0]))
15423 {
15424 case 'b':
15425 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
15426 p = highlight_color(id, what, modec);
15427 else /* bold */
15428 p = highlight_has_attr(id, HL_BOLD, modec);
15429 break;
15430
15431 case 'f': /* fg[#] */
15432 p = highlight_color(id, what, modec);
15433 break;
15434
15435 case 'i':
15436 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
15437 p = highlight_has_attr(id, HL_INVERSE, modec);
15438 else /* italic */
15439 p = highlight_has_attr(id, HL_ITALIC, modec);
15440 break;
15441
15442 case 'n': /* name */
15443 p = get_highlight_name(NULL, id - 1);
15444 break;
15445
15446 case 'r': /* reverse */
15447 p = highlight_has_attr(id, HL_INVERSE, modec);
15448 break;
15449
15450 case 's': /* standout */
15451 p = highlight_has_attr(id, HL_STANDOUT, modec);
15452 break;
15453
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000015454 case 'u':
15455 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
15456 /* underline */
15457 p = highlight_has_attr(id, HL_UNDERLINE, modec);
15458 else
15459 /* undercurl */
15460 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015461 break;
15462 }
15463
15464 if (p != NULL)
15465 p = vim_strsave(p);
15466#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015467 rettv->v_type = VAR_STRING;
15468 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015469}
15470
15471/*
15472 * "synIDtrans(id)" function
15473 */
15474/*ARGSUSED*/
15475 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015476f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015477 typval_T *argvars;
15478 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015479{
15480 int id;
15481
15482#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015483 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015484
15485 if (id > 0)
15486 id = syn_get_final_id(id);
15487 else
15488#endif
15489 id = 0;
15490
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015491 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015492}
15493
15494/*
15495 * "system()" function
15496 */
15497 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015498f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015499 typval_T *argvars;
15500 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015501{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015502 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015503 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015504 char_u *infile = NULL;
15505 char_u buf[NUMBUFLEN];
15506 int err = FALSE;
15507 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015508
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015509 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015510 {
15511 /*
15512 * Write the string to a temp file, to be used for input of the shell
15513 * command.
15514 */
15515 if ((infile = vim_tempname('i')) == NULL)
15516 {
15517 EMSG(_(e_notmp));
15518 return;
15519 }
15520
15521 fd = mch_fopen((char *)infile, WRITEBIN);
15522 if (fd == NULL)
15523 {
15524 EMSG2(_(e_notopen), infile);
15525 goto done;
15526 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015527 p = get_tv_string_buf_chk(&argvars[1], buf);
15528 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015529 {
15530 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015531 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015532 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015533 if (fwrite(p, STRLEN(p), 1, fd) != 1)
15534 err = TRUE;
15535 if (fclose(fd) != 0)
15536 err = TRUE;
15537 if (err)
15538 {
15539 EMSG(_("E677: Error writing temp file"));
15540 goto done;
15541 }
15542 }
15543
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015544 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
15545 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015546
Bram Moolenaar071d4272004-06-13 20:20:40 +000015547#ifdef USE_CR
15548 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015549 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015550 {
15551 char_u *s;
15552
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015553 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015554 {
15555 if (*s == CAR)
15556 *s = NL;
15557 }
15558 }
15559#else
15560# ifdef USE_CRNL
15561 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015562 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015563 {
15564 char_u *s, *d;
15565
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015566 d = res;
15567 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015568 {
15569 if (s[0] == CAR && s[1] == NL)
15570 ++s;
15571 *d++ = *s;
15572 }
15573 *d = NUL;
15574 }
15575# endif
15576#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015577
15578done:
15579 if (infile != NULL)
15580 {
15581 mch_remove(infile);
15582 vim_free(infile);
15583 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015584 rettv->v_type = VAR_STRING;
15585 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015586}
15587
15588/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015589 * "tabpagebuflist()" function
15590 */
15591/* ARGSUSED */
15592 static void
15593f_tabpagebuflist(argvars, rettv)
15594 typval_T *argvars;
15595 typval_T *rettv;
15596{
15597#ifndef FEAT_WINDOWS
15598 rettv->vval.v_number = 0;
15599#else
15600 tabpage_T *tp;
15601 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015602
15603 if (argvars[0].v_type == VAR_UNKNOWN)
15604 wp = firstwin;
15605 else
15606 {
15607 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15608 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000015609 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015610 }
15611 if (wp == NULL)
15612 rettv->vval.v_number = 0;
15613 else
15614 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015615 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015616 rettv->vval.v_number = 0;
15617 else
15618 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015619 for (; wp != NULL; wp = wp->w_next)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015620 if (list_append_number(rettv->vval.v_list,
15621 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015622 break;
15623 }
15624 }
15625#endif
15626}
15627
15628
15629/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015630 * "tabpagenr()" function
15631 */
15632/* ARGSUSED */
15633 static void
15634f_tabpagenr(argvars, rettv)
15635 typval_T *argvars;
15636 typval_T *rettv;
15637{
15638 int nr = 1;
15639#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015640 char_u *arg;
15641
15642 if (argvars[0].v_type != VAR_UNKNOWN)
15643 {
15644 arg = get_tv_string_chk(&argvars[0]);
15645 nr = 0;
15646 if (arg != NULL)
15647 {
15648 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000015649 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015650 else
15651 EMSG2(_(e_invexpr2), arg);
15652 }
15653 }
15654 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015655 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015656#endif
15657 rettv->vval.v_number = nr;
15658}
15659
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015660
15661#ifdef FEAT_WINDOWS
15662static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
15663
15664/*
15665 * Common code for tabpagewinnr() and winnr().
15666 */
15667 static int
15668get_winnr(tp, argvar)
15669 tabpage_T *tp;
15670 typval_T *argvar;
15671{
15672 win_T *twin;
15673 int nr = 1;
15674 win_T *wp;
15675 char_u *arg;
15676
15677 twin = (tp == curtab) ? curwin : tp->tp_curwin;
15678 if (argvar->v_type != VAR_UNKNOWN)
15679 {
15680 arg = get_tv_string_chk(argvar);
15681 if (arg == NULL)
15682 nr = 0; /* type error; errmsg already given */
15683 else if (STRCMP(arg, "$") == 0)
15684 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
15685 else if (STRCMP(arg, "#") == 0)
15686 {
15687 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
15688 if (twin == NULL)
15689 nr = 0;
15690 }
15691 else
15692 {
15693 EMSG2(_(e_invexpr2), arg);
15694 nr = 0;
15695 }
15696 }
15697
15698 if (nr > 0)
15699 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
15700 wp != twin; wp = wp->w_next)
15701 ++nr;
15702 return nr;
15703}
15704#endif
15705
15706/*
15707 * "tabpagewinnr()" function
15708 */
15709/* ARGSUSED */
15710 static void
15711f_tabpagewinnr(argvars, rettv)
15712 typval_T *argvars;
15713 typval_T *rettv;
15714{
15715 int nr = 1;
15716#ifdef FEAT_WINDOWS
15717 tabpage_T *tp;
15718
15719 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15720 if (tp == NULL)
15721 nr = 0;
15722 else
15723 nr = get_winnr(tp, &argvars[1]);
15724#endif
15725 rettv->vval.v_number = nr;
15726}
15727
15728
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015729/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015730 * "tagfiles()" function
15731 */
15732/*ARGSUSED*/
15733 static void
15734f_tagfiles(argvars, rettv)
15735 typval_T *argvars;
15736 typval_T *rettv;
15737{
15738 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015739 tagname_T tn;
15740 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015741
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015742 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015743 {
15744 rettv->vval.v_number = 0;
15745 return;
15746 }
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015747
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015748 for (first = TRUE; ; first = FALSE)
15749 if (get_tagfname(&tn, first, fname) == FAIL
15750 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015751 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015752 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015753}
15754
15755/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000015756 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015757 */
15758 static void
15759f_taglist(argvars, rettv)
15760 typval_T *argvars;
15761 typval_T *rettv;
15762{
15763 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015764
15765 tag_pattern = get_tv_string(&argvars[0]);
15766
15767 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015768 if (*tag_pattern == NUL)
15769 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015770
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015771 if (rettv_list_alloc(rettv) == OK)
15772 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015773}
15774
15775/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015776 * "tempname()" function
15777 */
15778/*ARGSUSED*/
15779 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015780f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015781 typval_T *argvars;
15782 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015783{
15784 static int x = 'A';
15785
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015786 rettv->v_type = VAR_STRING;
15787 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015788
15789 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
15790 * names. Skip 'I' and 'O', they are used for shell redirection. */
15791 do
15792 {
15793 if (x == 'Z')
15794 x = '0';
15795 else if (x == '9')
15796 x = 'A';
15797 else
15798 {
15799#ifdef EBCDIC
15800 if (x == 'I')
15801 x = 'J';
15802 else if (x == 'R')
15803 x = 'S';
15804 else
15805#endif
15806 ++x;
15807 }
15808 } while (x == 'I' || x == 'O');
15809}
15810
15811/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000015812 * "test(list)" function: Just checking the walls...
15813 */
15814/*ARGSUSED*/
15815 static void
15816f_test(argvars, rettv)
15817 typval_T *argvars;
15818 typval_T *rettv;
15819{
15820 /* Used for unit testing. Change the code below to your liking. */
15821#if 0
15822 listitem_T *li;
15823 list_T *l;
15824 char_u *bad, *good;
15825
15826 if (argvars[0].v_type != VAR_LIST)
15827 return;
15828 l = argvars[0].vval.v_list;
15829 if (l == NULL)
15830 return;
15831 li = l->lv_first;
15832 if (li == NULL)
15833 return;
15834 bad = get_tv_string(&li->li_tv);
15835 li = li->li_next;
15836 if (li == NULL)
15837 return;
15838 good = get_tv_string(&li->li_tv);
15839 rettv->vval.v_number = test_edit_score(bad, good);
15840#endif
15841}
15842
15843/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015844 * "tolower(string)" function
15845 */
15846 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015847f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015848 typval_T *argvars;
15849 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015850{
15851 char_u *p;
15852
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015853 p = vim_strsave(get_tv_string(&argvars[0]));
15854 rettv->v_type = VAR_STRING;
15855 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015856
15857 if (p != NULL)
15858 while (*p != NUL)
15859 {
15860#ifdef FEAT_MBYTE
15861 int l;
15862
15863 if (enc_utf8)
15864 {
15865 int c, lc;
15866
15867 c = utf_ptr2char(p);
15868 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015869 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015870 /* TODO: reallocate string when byte count changes. */
15871 if (utf_char2len(lc) == l)
15872 utf_char2bytes(lc, p);
15873 p += l;
15874 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015875 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015876 p += l; /* skip multi-byte character */
15877 else
15878#endif
15879 {
15880 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
15881 ++p;
15882 }
15883 }
15884}
15885
15886/*
15887 * "toupper(string)" function
15888 */
15889 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015890f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015891 typval_T *argvars;
15892 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015893{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015894 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015895 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015896}
15897
15898/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000015899 * "tr(string, fromstr, tostr)" function
15900 */
15901 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015902f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015903 typval_T *argvars;
15904 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015905{
15906 char_u *instr;
15907 char_u *fromstr;
15908 char_u *tostr;
15909 char_u *p;
15910#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000015911 int inlen;
15912 int fromlen;
15913 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015914 int idx;
15915 char_u *cpstr;
15916 int cplen;
15917 int first = TRUE;
15918#endif
15919 char_u buf[NUMBUFLEN];
15920 char_u buf2[NUMBUFLEN];
15921 garray_T ga;
15922
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015923 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015924 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
15925 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015926
15927 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015928 rettv->v_type = VAR_STRING;
15929 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015930 if (fromstr == NULL || tostr == NULL)
15931 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000015932 ga_init2(&ga, (int)sizeof(char), 80);
15933
15934#ifdef FEAT_MBYTE
15935 if (!has_mbyte)
15936#endif
15937 /* not multi-byte: fromstr and tostr must be the same length */
15938 if (STRLEN(fromstr) != STRLEN(tostr))
15939 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015940#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000015941error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015942#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000015943 EMSG2(_(e_invarg2), fromstr);
15944 ga_clear(&ga);
15945 return;
15946 }
15947
15948 /* fromstr and tostr have to contain the same number of chars */
15949 while (*instr != NUL)
15950 {
15951#ifdef FEAT_MBYTE
15952 if (has_mbyte)
15953 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015954 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015955 cpstr = instr;
15956 cplen = inlen;
15957 idx = 0;
15958 for (p = fromstr; *p != NUL; p += fromlen)
15959 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015960 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015961 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
15962 {
15963 for (p = tostr; *p != NUL; p += tolen)
15964 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015965 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015966 if (idx-- == 0)
15967 {
15968 cplen = tolen;
15969 cpstr = p;
15970 break;
15971 }
15972 }
15973 if (*p == NUL) /* tostr is shorter than fromstr */
15974 goto error;
15975 break;
15976 }
15977 ++idx;
15978 }
15979
15980 if (first && cpstr == instr)
15981 {
15982 /* Check that fromstr and tostr have the same number of
15983 * (multi-byte) characters. Done only once when a character
15984 * of instr doesn't appear in fromstr. */
15985 first = FALSE;
15986 for (p = tostr; *p != NUL; p += tolen)
15987 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015988 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015989 --idx;
15990 }
15991 if (idx != 0)
15992 goto error;
15993 }
15994
15995 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000015996 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015997 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015998
15999 instr += inlen;
16000 }
16001 else
16002#endif
16003 {
16004 /* When not using multi-byte chars we can do it faster. */
16005 p = vim_strchr(fromstr, *instr);
16006 if (p != NULL)
16007 ga_append(&ga, tostr[p - fromstr]);
16008 else
16009 ga_append(&ga, *instr);
16010 ++instr;
16011 }
16012 }
16013
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016014 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000016015}
16016
16017/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016018 * "type(expr)" function
16019 */
16020 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016021f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016022 typval_T *argvars;
16023 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016024{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016025 int n;
16026
16027 switch (argvars[0].v_type)
16028 {
16029 case VAR_NUMBER: n = 0; break;
16030 case VAR_STRING: n = 1; break;
16031 case VAR_FUNC: n = 2; break;
16032 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016033 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016034 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
16035 }
16036 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016037}
16038
16039/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016040 * "values(dict)" function
16041 */
16042 static void
16043f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016044 typval_T *argvars;
16045 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016046{
16047 dict_list(argvars, rettv, 1);
16048}
16049
16050/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016051 * "virtcol(string)" function
16052 */
16053 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016054f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016055 typval_T *argvars;
16056 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016057{
16058 colnr_T vcol = 0;
16059 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016060 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016061
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016062 fp = var2fpos(&argvars[0], FALSE, &fnum);
16063 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
16064 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016065 {
16066 getvvcol(curwin, fp, NULL, NULL, &vcol);
16067 ++vcol;
16068 }
16069
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016070 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016071}
16072
16073/*
16074 * "visualmode()" function
16075 */
16076/*ARGSUSED*/
16077 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016078f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016079 typval_T *argvars;
16080 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016081{
16082#ifdef FEAT_VISUAL
16083 char_u str[2];
16084
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016085 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016086 str[0] = curbuf->b_visual_mode_eval;
16087 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016088 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016089
16090 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016091 if ((argvars[0].v_type == VAR_NUMBER
16092 && argvars[0].vval.v_number != 0)
16093 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016094 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016095 curbuf->b_visual_mode_eval = NUL;
16096#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016097 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016098#endif
16099}
16100
16101/*
16102 * "winbufnr(nr)" function
16103 */
16104 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016105f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016106 typval_T *argvars;
16107 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016108{
16109 win_T *wp;
16110
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016111 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016112 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016113 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016114 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016115 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016116}
16117
16118/*
16119 * "wincol()" function
16120 */
16121/*ARGSUSED*/
16122 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016123f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016124 typval_T *argvars;
16125 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016126{
16127 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016128 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016129}
16130
16131/*
16132 * "winheight(nr)" function
16133 */
16134 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016135f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016136 typval_T *argvars;
16137 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016138{
16139 win_T *wp;
16140
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016141 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016142 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016143 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016144 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016145 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016146}
16147
16148/*
16149 * "winline()" function
16150 */
16151/*ARGSUSED*/
16152 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016153f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016154 typval_T *argvars;
16155 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016156{
16157 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016158 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016159}
16160
16161/*
16162 * "winnr()" function
16163 */
16164/* ARGSUSED */
16165 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016166f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016167 typval_T *argvars;
16168 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016169{
16170 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016171
Bram Moolenaar071d4272004-06-13 20:20:40 +000016172#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016173 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016174#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016175 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016176}
16177
16178/*
16179 * "winrestcmd()" function
16180 */
16181/* ARGSUSED */
16182 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016183f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016184 typval_T *argvars;
16185 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016186{
16187#ifdef FEAT_WINDOWS
16188 win_T *wp;
16189 int winnr = 1;
16190 garray_T ga;
16191 char_u buf[50];
16192
16193 ga_init2(&ga, (int)sizeof(char), 70);
16194 for (wp = firstwin; wp != NULL; wp = wp->w_next)
16195 {
16196 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
16197 ga_concat(&ga, buf);
16198# ifdef FEAT_VERTSPLIT
16199 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
16200 ga_concat(&ga, buf);
16201# endif
16202 ++winnr;
16203 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000016204 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016205
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016206 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016207#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016208 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016209#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016210 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016211}
16212
16213/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016214 * "winrestview()" function
16215 */
16216/* ARGSUSED */
16217 static void
16218f_winrestview(argvars, rettv)
16219 typval_T *argvars;
16220 typval_T *rettv;
16221{
16222 dict_T *dict;
16223
16224 if (argvars[0].v_type != VAR_DICT
16225 || (dict = argvars[0].vval.v_dict) == NULL)
16226 EMSG(_(e_invarg));
16227 else
16228 {
16229 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
16230 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
16231#ifdef FEAT_VIRTUALEDIT
16232 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
16233#endif
16234 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016235 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016236
Bram Moolenaar6f11a412006-09-06 20:16:42 +000016237 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016238#ifdef FEAT_DIFF
16239 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
16240#endif
16241 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
16242 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
16243
16244 check_cursor();
16245 changed_cline_bef_curs();
16246 invalidate_botline();
16247 redraw_later(VALID);
16248
16249 if (curwin->w_topline == 0)
16250 curwin->w_topline = 1;
16251 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
16252 curwin->w_topline = curbuf->b_ml.ml_line_count;
16253#ifdef FEAT_DIFF
16254 check_topfill(curwin, TRUE);
16255#endif
16256 }
16257}
16258
16259/*
16260 * "winsaveview()" function
16261 */
16262/* ARGSUSED */
16263 static void
16264f_winsaveview(argvars, rettv)
16265 typval_T *argvars;
16266 typval_T *rettv;
16267{
16268 dict_T *dict;
16269
16270 dict = dict_alloc();
16271 if (dict == NULL)
16272 return;
16273 rettv->v_type = VAR_DICT;
16274 rettv->vval.v_dict = dict;
16275 ++dict->dv_refcount;
16276
16277 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
16278 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
16279#ifdef FEAT_VIRTUALEDIT
16280 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
16281#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000016282 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016283 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
16284
16285 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
16286#ifdef FEAT_DIFF
16287 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
16288#endif
16289 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
16290 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
16291}
16292
16293/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016294 * "winwidth(nr)" function
16295 */
16296 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016297f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016298 typval_T *argvars;
16299 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016300{
16301 win_T *wp;
16302
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016303 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016304 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016305 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016306 else
16307#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016308 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016309#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016310 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016311#endif
16312}
16313
Bram Moolenaar071d4272004-06-13 20:20:40 +000016314/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016315 * "writefile()" function
16316 */
16317 static void
16318f_writefile(argvars, rettv)
16319 typval_T *argvars;
16320 typval_T *rettv;
16321{
16322 int binary = FALSE;
16323 char_u *fname;
16324 FILE *fd;
16325 listitem_T *li;
16326 char_u *s;
16327 int ret = 0;
16328 int c;
16329
16330 if (argvars[0].v_type != VAR_LIST)
16331 {
16332 EMSG2(_(e_listarg), "writefile()");
16333 return;
16334 }
16335 if (argvars[0].vval.v_list == NULL)
16336 return;
16337
16338 if (argvars[2].v_type != VAR_UNKNOWN
16339 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
16340 binary = TRUE;
16341
16342 /* Always open the file in binary mode, library functions have a mind of
16343 * their own about CR-LF conversion. */
16344 fname = get_tv_string(&argvars[1]);
16345 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
16346 {
16347 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
16348 ret = -1;
16349 }
16350 else
16351 {
16352 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
16353 li = li->li_next)
16354 {
16355 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
16356 {
16357 if (*s == '\n')
16358 c = putc(NUL, fd);
16359 else
16360 c = putc(*s, fd);
16361 if (c == EOF)
16362 {
16363 ret = -1;
16364 break;
16365 }
16366 }
16367 if (!binary || li->li_next != NULL)
16368 if (putc('\n', fd) == EOF)
16369 {
16370 ret = -1;
16371 break;
16372 }
16373 if (ret < 0)
16374 {
16375 EMSG(_(e_write));
16376 break;
16377 }
16378 }
16379 fclose(fd);
16380 }
16381
16382 rettv->vval.v_number = ret;
16383}
16384
16385/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016386 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016387 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016388 */
16389 static pos_T *
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016390var2fpos(varp, lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000016391 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016392 int lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016393 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016394{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016395 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016396 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016397 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016398
Bram Moolenaara5525202006-03-02 22:52:09 +000016399 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016400 if (varp->v_type == VAR_LIST)
16401 {
16402 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016403 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000016404 int error = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016405
16406 l = varp->vval.v_list;
16407 if (l == NULL)
16408 return NULL;
16409
16410 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016411 pos.lnum = list_find_nr(l, 0L, &error);
16412 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016413 return NULL; /* invalid line number */
16414
16415 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016416 pos.col = list_find_nr(l, 1L, &error);
16417 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016418 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016419 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaara5525202006-03-02 22:52:09 +000016420 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000016421 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016422 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016423 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016424
Bram Moolenaara5525202006-03-02 22:52:09 +000016425#ifdef FEAT_VIRTUALEDIT
16426 /* Get the virtual offset. Defaults to zero. */
16427 pos.coladd = list_find_nr(l, 2L, &error);
16428 if (error)
16429 pos.coladd = 0;
16430#endif
16431
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016432 return &pos;
16433 }
16434
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016435 name = get_tv_string_chk(varp);
16436 if (name == NULL)
16437 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016438 if (name[0] == '.') /* cursor */
16439 return &curwin->w_cursor;
16440 if (name[0] == '\'') /* mark */
16441 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016442 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016443 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
16444 return NULL;
16445 return pp;
16446 }
Bram Moolenaara5525202006-03-02 22:52:09 +000016447
16448#ifdef FEAT_VIRTUALEDIT
16449 pos.coladd = 0;
16450#endif
16451
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016452 if (name[0] == 'w' && lnum)
16453 {
16454 pos.col = 0;
16455 if (name[1] == '0') /* "w0": first visible line */
16456 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016457 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016458 pos.lnum = curwin->w_topline;
16459 return &pos;
16460 }
16461 else if (name[1] == '$') /* "w$": last visible line */
16462 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016463 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016464 pos.lnum = curwin->w_botline - 1;
16465 return &pos;
16466 }
16467 }
16468 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016469 {
16470 if (lnum)
16471 {
16472 pos.lnum = curbuf->b_ml.ml_line_count;
16473 pos.col = 0;
16474 }
16475 else
16476 {
16477 pos.lnum = curwin->w_cursor.lnum;
16478 pos.col = (colnr_T)STRLEN(ml_get_curline());
16479 }
16480 return &pos;
16481 }
16482 return NULL;
16483}
16484
16485/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016486 * Convert list in "arg" into a position and optional file number.
16487 * When "fnump" is NULL there is no file number, only 3 items.
16488 * Note that the column is passed on as-is, the caller may want to decrement
16489 * it to use 1 for the first column.
16490 * Return FAIL when conversion is not possible, doesn't check the position for
16491 * validity.
16492 */
16493 static int
16494list2fpos(arg, posp, fnump)
16495 typval_T *arg;
16496 pos_T *posp;
16497 int *fnump;
16498{
16499 list_T *l = arg->vval.v_list;
16500 long i = 0;
16501 long n;
16502
Bram Moolenaarbde35262006-07-23 20:12:24 +000016503 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
16504 * when "fnump" isn't NULL and "coladd" is optional. */
16505 if (arg->v_type != VAR_LIST
16506 || l == NULL
16507 || l->lv_len < (fnump == NULL ? 2 : 3)
16508 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016509 return FAIL;
16510
16511 if (fnump != NULL)
16512 {
16513 n = list_find_nr(l, i++, NULL); /* fnum */
16514 if (n < 0)
16515 return FAIL;
16516 if (n == 0)
16517 n = curbuf->b_fnum; /* current buffer */
16518 *fnump = n;
16519 }
16520
16521 n = list_find_nr(l, i++, NULL); /* lnum */
16522 if (n < 0)
16523 return FAIL;
16524 posp->lnum = n;
16525
16526 n = list_find_nr(l, i++, NULL); /* col */
16527 if (n < 0)
16528 return FAIL;
16529 posp->col = n;
16530
16531#ifdef FEAT_VIRTUALEDIT
16532 n = list_find_nr(l, i, NULL);
16533 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000016534 posp->coladd = 0;
16535 else
16536 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016537#endif
16538
16539 return OK;
16540}
16541
16542/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016543 * Get the length of an environment variable name.
16544 * Advance "arg" to the first character after the name.
16545 * Return 0 for error.
16546 */
16547 static int
16548get_env_len(arg)
16549 char_u **arg;
16550{
16551 char_u *p;
16552 int len;
16553
16554 for (p = *arg; vim_isIDc(*p); ++p)
16555 ;
16556 if (p == *arg) /* no name found */
16557 return 0;
16558
16559 len = (int)(p - *arg);
16560 *arg = p;
16561 return len;
16562}
16563
16564/*
16565 * Get the length of the name of a function or internal variable.
16566 * "arg" is advanced to the first non-white character after the name.
16567 * Return 0 if something is wrong.
16568 */
16569 static int
16570get_id_len(arg)
16571 char_u **arg;
16572{
16573 char_u *p;
16574 int len;
16575
16576 /* Find the end of the name. */
16577 for (p = *arg; eval_isnamec(*p); ++p)
16578 ;
16579 if (p == *arg) /* no name found */
16580 return 0;
16581
16582 len = (int)(p - *arg);
16583 *arg = skipwhite(p);
16584
16585 return len;
16586}
16587
16588/*
Bram Moolenaara7043832005-01-21 11:56:39 +000016589 * Get the length of the name of a variable or function.
16590 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000016591 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016592 * Return -1 if curly braces expansion failed.
16593 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016594 * If the name contains 'magic' {}'s, expand them and return the
16595 * expanded name in an allocated string via 'alias' - caller must free.
16596 */
16597 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016598get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016599 char_u **arg;
16600 char_u **alias;
16601 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016602 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016603{
16604 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016605 char_u *p;
16606 char_u *expr_start;
16607 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016608
16609 *alias = NULL; /* default to no alias */
16610
16611 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
16612 && (*arg)[2] == (int)KE_SNR)
16613 {
16614 /* hard coded <SNR>, already translated */
16615 *arg += 3;
16616 return get_id_len(arg) + 3;
16617 }
16618 len = eval_fname_script(*arg);
16619 if (len > 0)
16620 {
16621 /* literal "<SID>", "s:" or "<SNR>" */
16622 *arg += len;
16623 }
16624
Bram Moolenaar071d4272004-06-13 20:20:40 +000016625 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016626 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016627 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016628 p = find_name_end(*arg, &expr_start, &expr_end,
16629 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016630 if (expr_start != NULL)
16631 {
16632 char_u *temp_string;
16633
16634 if (!evaluate)
16635 {
16636 len += (int)(p - *arg);
16637 *arg = skipwhite(p);
16638 return len;
16639 }
16640
16641 /*
16642 * Include any <SID> etc in the expanded string:
16643 * Thus the -len here.
16644 */
16645 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
16646 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016647 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016648 *alias = temp_string;
16649 *arg = skipwhite(p);
16650 return (int)STRLEN(temp_string);
16651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016652
16653 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016654 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016655 EMSG2(_(e_invexpr2), *arg);
16656
16657 return len;
16658}
16659
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016660/*
16661 * Find the end of a variable or function name, taking care of magic braces.
16662 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
16663 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016664 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016665 * Return a pointer to just after the name. Equal to "arg" if there is no
16666 * valid name.
16667 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016668 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016669find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016670 char_u *arg;
16671 char_u **expr_start;
16672 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016673 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016674{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016675 int mb_nest = 0;
16676 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016677 char_u *p;
16678
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016679 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016680 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016681 *expr_start = NULL;
16682 *expr_end = NULL;
16683 }
16684
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016685 /* Quick check for valid starting character. */
16686 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
16687 return arg;
16688
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016689 for (p = arg; *p != NUL
16690 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016691 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016692 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016693 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000016694 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016695 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000016696 if (*p == '\'')
16697 {
16698 /* skip over 'string' to avoid counting [ and ] inside it. */
16699 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
16700 ;
16701 if (*p == NUL)
16702 break;
16703 }
16704 else if (*p == '"')
16705 {
16706 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
16707 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
16708 if (*p == '\\' && p[1] != NUL)
16709 ++p;
16710 if (*p == NUL)
16711 break;
16712 }
16713
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016714 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016715 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016716 if (*p == '[')
16717 ++br_nest;
16718 else if (*p == ']')
16719 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016720 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000016721
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016722 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016723 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016724 if (*p == '{')
16725 {
16726 mb_nest++;
16727 if (expr_start != NULL && *expr_start == NULL)
16728 *expr_start = p;
16729 }
16730 else if (*p == '}')
16731 {
16732 mb_nest--;
16733 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
16734 *expr_end = p;
16735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016737 }
16738
16739 return p;
16740}
16741
16742/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016743 * Expands out the 'magic' {}'s in a variable/function name.
16744 * Note that this can call itself recursively, to deal with
16745 * constructs like foo{bar}{baz}{bam}
16746 * The four pointer arguments point to "foo{expre}ss{ion}bar"
16747 * "in_start" ^
16748 * "expr_start" ^
16749 * "expr_end" ^
16750 * "in_end" ^
16751 *
16752 * Returns a new allocated string, which the caller must free.
16753 * Returns NULL for failure.
16754 */
16755 static char_u *
16756make_expanded_name(in_start, expr_start, expr_end, in_end)
16757 char_u *in_start;
16758 char_u *expr_start;
16759 char_u *expr_end;
16760 char_u *in_end;
16761{
16762 char_u c1;
16763 char_u *retval = NULL;
16764 char_u *temp_result;
16765 char_u *nextcmd = NULL;
16766
16767 if (expr_end == NULL || in_end == NULL)
16768 return NULL;
16769 *expr_start = NUL;
16770 *expr_end = NUL;
16771 c1 = *in_end;
16772 *in_end = NUL;
16773
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016774 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016775 if (temp_result != NULL && nextcmd == NULL)
16776 {
16777 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
16778 + (in_end - expr_end) + 1));
16779 if (retval != NULL)
16780 {
16781 STRCPY(retval, in_start);
16782 STRCAT(retval, temp_result);
16783 STRCAT(retval, expr_end + 1);
16784 }
16785 }
16786 vim_free(temp_result);
16787
16788 *in_end = c1; /* put char back for error messages */
16789 *expr_start = '{';
16790 *expr_end = '}';
16791
16792 if (retval != NULL)
16793 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016794 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016795 if (expr_start != NULL)
16796 {
16797 /* Further expansion! */
16798 temp_result = make_expanded_name(retval, expr_start,
16799 expr_end, temp_result);
16800 vim_free(retval);
16801 retval = temp_result;
16802 }
16803 }
16804
16805 return retval;
16806}
16807
16808/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016809 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016810 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016811 */
16812 static int
16813eval_isnamec(c)
16814 int c;
16815{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016816 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
16817}
16818
16819/*
16820 * Return TRUE if character "c" can be used as the first character in a
16821 * variable or function name (excluding '{' and '}').
16822 */
16823 static int
16824eval_isnamec1(c)
16825 int c;
16826{
16827 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000016828}
16829
16830/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016831 * Set number v: variable to "val".
16832 */
16833 void
16834set_vim_var_nr(idx, val)
16835 int idx;
16836 long val;
16837{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016838 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016839}
16840
16841/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016842 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016843 */
16844 long
16845get_vim_var_nr(idx)
16846 int idx;
16847{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016848 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016849}
16850
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016851#if defined(FEAT_AUTOCMD) || defined(PROTO)
16852/*
16853 * Get string v: variable value. Uses a static buffer, can only be used once.
16854 */
16855 char_u *
16856get_vim_var_str(idx)
16857 int idx;
16858{
16859 return get_tv_string(&vimvars[idx].vv_tv);
16860}
16861#endif
16862
Bram Moolenaar071d4272004-06-13 20:20:40 +000016863/*
16864 * Set v:count, v:count1 and v:prevcount.
16865 */
16866 void
16867set_vcount(count, count1)
16868 long count;
16869 long count1;
16870{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016871 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
16872 vimvars[VV_COUNT].vv_nr = count;
16873 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016874}
16875
16876/*
16877 * Set string v: variable to a copy of "val".
16878 */
16879 void
16880set_vim_var_string(idx, val, len)
16881 int idx;
16882 char_u *val;
16883 int len; /* length of "val" to use or -1 (whole string) */
16884{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016885 /* Need to do this (at least) once, since we can't initialize a union.
16886 * Will always be invoked when "v:progname" is set. */
16887 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
16888
Bram Moolenaare9a41262005-01-15 22:18:47 +000016889 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016890 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016891 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016892 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016893 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016894 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000016895 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016896}
16897
16898/*
16899 * Set v:register if needed.
16900 */
16901 void
16902set_reg_var(c)
16903 int c;
16904{
16905 char_u regname;
16906
16907 if (c == 0 || c == ' ')
16908 regname = '"';
16909 else
16910 regname = c;
16911 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000016912 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016913 set_vim_var_string(VV_REG, &regname, 1);
16914}
16915
16916/*
16917 * Get or set v:exception. If "oldval" == NULL, return the current value.
16918 * Otherwise, restore the value to "oldval" and return NULL.
16919 * Must always be called in pairs to save and restore v:exception! Does not
16920 * take care of memory allocations.
16921 */
16922 char_u *
16923v_exception(oldval)
16924 char_u *oldval;
16925{
16926 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016927 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016928
Bram Moolenaare9a41262005-01-15 22:18:47 +000016929 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016930 return NULL;
16931}
16932
16933/*
16934 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
16935 * Otherwise, restore the value to "oldval" and return NULL.
16936 * Must always be called in pairs to save and restore v:throwpoint! Does not
16937 * take care of memory allocations.
16938 */
16939 char_u *
16940v_throwpoint(oldval)
16941 char_u *oldval;
16942{
16943 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016944 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016945
Bram Moolenaare9a41262005-01-15 22:18:47 +000016946 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016947 return NULL;
16948}
16949
16950#if defined(FEAT_AUTOCMD) || defined(PROTO)
16951/*
16952 * Set v:cmdarg.
16953 * If "eap" != NULL, use "eap" to generate the value and return the old value.
16954 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
16955 * Must always be called in pairs!
16956 */
16957 char_u *
16958set_cmdarg(eap, oldarg)
16959 exarg_T *eap;
16960 char_u *oldarg;
16961{
16962 char_u *oldval;
16963 char_u *newval;
16964 unsigned len;
16965
Bram Moolenaare9a41262005-01-15 22:18:47 +000016966 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016967 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016968 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016969 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000016970 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016971 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016972 }
16973
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016974 if (eap->force_bin == FORCE_BIN)
16975 len = 6;
16976 else if (eap->force_bin == FORCE_NOBIN)
16977 len = 8;
16978 else
16979 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016980
16981 if (eap->read_edit)
16982 len += 7;
16983
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016984 if (eap->force_ff != 0)
16985 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
16986# ifdef FEAT_MBYTE
16987 if (eap->force_enc != 0)
16988 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000016989 if (eap->bad_char != 0)
16990 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016991# endif
16992
16993 newval = alloc(len + 1);
16994 if (newval == NULL)
16995 return NULL;
16996
16997 if (eap->force_bin == FORCE_BIN)
16998 sprintf((char *)newval, " ++bin");
16999 else if (eap->force_bin == FORCE_NOBIN)
17000 sprintf((char *)newval, " ++nobin");
17001 else
17002 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017003
17004 if (eap->read_edit)
17005 STRCAT(newval, " ++edit");
17006
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017007 if (eap->force_ff != 0)
17008 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
17009 eap->cmd + eap->force_ff);
17010# ifdef FEAT_MBYTE
17011 if (eap->force_enc != 0)
17012 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
17013 eap->cmd + eap->force_enc);
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000017014 if (eap->bad_char != 0)
17015 sprintf((char *)newval + STRLEN(newval), " ++bad=%s",
17016 eap->cmd + eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017017# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000017018 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017019 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017020}
17021#endif
17022
17023/*
17024 * Get the value of internal variable "name".
17025 * Return OK or FAIL.
17026 */
17027 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017028get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017029 char_u *name;
17030 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000017031 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017032 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017033{
17034 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000017035 typval_T *tv = NULL;
17036 typval_T atv;
17037 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017038 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017039
17040 /* truncate the name, so that we can use strcmp() */
17041 cc = name[len];
17042 name[len] = NUL;
17043
17044 /*
17045 * Check for "b:changedtick".
17046 */
17047 if (STRCMP(name, "b:changedtick") == 0)
17048 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000017049 atv.v_type = VAR_NUMBER;
17050 atv.vval.v_number = curbuf->b_changedtick;
17051 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017052 }
17053
17054 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017055 * Check for user-defined variables.
17056 */
17057 else
17058 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017059 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017060 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017061 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017062 }
17063
Bram Moolenaare9a41262005-01-15 22:18:47 +000017064 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017065 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017066 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017067 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017068 ret = FAIL;
17069 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017070 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017071 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017072
17073 name[len] = cc;
17074
17075 return ret;
17076}
17077
17078/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017079 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
17080 * Also handle function call with Funcref variable: func(expr)
17081 * Can all be combined: dict.func(expr)[idx]['func'](expr)
17082 */
17083 static int
17084handle_subscript(arg, rettv, evaluate, verbose)
17085 char_u **arg;
17086 typval_T *rettv;
17087 int evaluate; /* do more than finding the end */
17088 int verbose; /* give error messages */
17089{
17090 int ret = OK;
17091 dict_T *selfdict = NULL;
17092 char_u *s;
17093 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000017094 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017095
17096 while (ret == OK
17097 && (**arg == '['
17098 || (**arg == '.' && rettv->v_type == VAR_DICT)
17099 || (**arg == '(' && rettv->v_type == VAR_FUNC))
17100 && !vim_iswhite(*(*arg - 1)))
17101 {
17102 if (**arg == '(')
17103 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000017104 /* need to copy the funcref so that we can clear rettv */
17105 functv = *rettv;
17106 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017107
17108 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000017109 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017110 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000017111 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
17112 &len, evaluate, selfdict);
17113
17114 /* Clear the funcref afterwards, so that deleting it while
17115 * evaluating the arguments is possible (see test55). */
17116 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017117
17118 /* Stop the expression evaluation when immediately aborting on
17119 * error, or when an interrupt occurred or an exception was thrown
17120 * but not caught. */
17121 if (aborting())
17122 {
17123 if (ret == OK)
17124 clear_tv(rettv);
17125 ret = FAIL;
17126 }
17127 dict_unref(selfdict);
17128 selfdict = NULL;
17129 }
17130 else /* **arg == '[' || **arg == '.' */
17131 {
17132 dict_unref(selfdict);
17133 if (rettv->v_type == VAR_DICT)
17134 {
17135 selfdict = rettv->vval.v_dict;
17136 if (selfdict != NULL)
17137 ++selfdict->dv_refcount;
17138 }
17139 else
17140 selfdict = NULL;
17141 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
17142 {
17143 clear_tv(rettv);
17144 ret = FAIL;
17145 }
17146 }
17147 }
17148 dict_unref(selfdict);
17149 return ret;
17150}
17151
17152/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017153 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
17154 * value).
17155 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017156 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017157alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017158{
Bram Moolenaar33570922005-01-25 22:26:29 +000017159 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017160}
17161
17162/*
17163 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017164 * The string "s" must have been allocated, it is consumed.
17165 * Return NULL for out of memory, the variable otherwise.
17166 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017167 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017168alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017169 char_u *s;
17170{
Bram Moolenaar33570922005-01-25 22:26:29 +000017171 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017172
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017173 rettv = alloc_tv();
17174 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017175 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017176 rettv->v_type = VAR_STRING;
17177 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017178 }
17179 else
17180 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017181 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017182}
17183
17184/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017185 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017186 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000017187 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017188free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017189 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017190{
17191 if (varp != NULL)
17192 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017193 switch (varp->v_type)
17194 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017195 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017196 func_unref(varp->vval.v_string);
17197 /*FALLTHROUGH*/
17198 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017199 vim_free(varp->vval.v_string);
17200 break;
17201 case VAR_LIST:
17202 list_unref(varp->vval.v_list);
17203 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017204 case VAR_DICT:
17205 dict_unref(varp->vval.v_dict);
17206 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017207 case VAR_NUMBER:
17208 case VAR_UNKNOWN:
17209 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017210 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000017211 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017212 break;
17213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017214 vim_free(varp);
17215 }
17216}
17217
17218/*
17219 * Free the memory for a variable value and set the value to NULL or 0.
17220 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017221 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017222clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017223 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017224{
17225 if (varp != NULL)
17226 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017227 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017228 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017229 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017230 func_unref(varp->vval.v_string);
17231 /*FALLTHROUGH*/
17232 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017233 vim_free(varp->vval.v_string);
17234 varp->vval.v_string = NULL;
17235 break;
17236 case VAR_LIST:
17237 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017238 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017239 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017240 case VAR_DICT:
17241 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017242 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017243 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017244 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017245 varp->vval.v_number = 0;
17246 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017247 case VAR_UNKNOWN:
17248 break;
17249 default:
17250 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017251 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017252 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017253 }
17254}
17255
17256/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017257 * Set the value of a variable to NULL without freeing items.
17258 */
17259 static void
17260init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017261 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017262{
17263 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017264 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017265}
17266
17267/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017268 * Get the number value of a variable.
17269 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017270 * For incompatible types, return 0.
17271 * get_tv_number_chk() is similar to get_tv_number(), but informs the
17272 * caller of incompatible types: it sets *denote to TRUE if "denote"
17273 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017274 */
17275 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017276get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017277 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017278{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017279 int error = FALSE;
17280
17281 return get_tv_number_chk(varp, &error); /* return 0L on error */
17282}
17283
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017284 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017285get_tv_number_chk(varp, denote)
17286 typval_T *varp;
17287 int *denote;
17288{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017289 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017290
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017291 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017292 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017293 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017294 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017295 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017296 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017297 break;
17298 case VAR_STRING:
17299 if (varp->vval.v_string != NULL)
17300 vim_str2nr(varp->vval.v_string, NULL, NULL,
17301 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017302 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017303 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000017304 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017305 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017306 case VAR_DICT:
17307 EMSG(_("E728: Using a Dictionary as a number"));
17308 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017309 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017310 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017311 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017312 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017313 if (denote == NULL) /* useful for values that must be unsigned */
17314 n = -1;
17315 else
17316 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017317 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017318}
17319
17320/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000017321 * Get the lnum from the first argument.
17322 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017323 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017324 */
17325 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017326get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000017327 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017328{
Bram Moolenaar33570922005-01-25 22:26:29 +000017329 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017330 linenr_T lnum;
17331
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017332 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017333 if (lnum == 0) /* no valid number, try using line() */
17334 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017335 rettv.v_type = VAR_NUMBER;
17336 f_line(argvars, &rettv);
17337 lnum = rettv.vval.v_number;
17338 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017339 }
17340 return lnum;
17341}
17342
17343/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000017344 * Get the lnum from the first argument.
17345 * Also accepts "$", then "buf" is used.
17346 * Returns 0 on error.
17347 */
17348 static linenr_T
17349get_tv_lnum_buf(argvars, buf)
17350 typval_T *argvars;
17351 buf_T *buf;
17352{
17353 if (argvars[0].v_type == VAR_STRING
17354 && argvars[0].vval.v_string != NULL
17355 && argvars[0].vval.v_string[0] == '$'
17356 && buf != NULL)
17357 return buf->b_ml.ml_line_count;
17358 return get_tv_number_chk(&argvars[0], NULL);
17359}
17360
17361/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017362 * Get the string value of a variable.
17363 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000017364 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
17365 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017366 * If the String variable has never been set, return an empty string.
17367 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017368 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
17369 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017370 */
17371 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017372get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017373 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017374{
17375 static char_u mybuf[NUMBUFLEN];
17376
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017377 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017378}
17379
17380 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017381get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000017382 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017383 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017384{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017385 char_u *res = get_tv_string_buf_chk(varp, buf);
17386
17387 return res != NULL ? res : (char_u *)"";
17388}
17389
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017390 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017391get_tv_string_chk(varp)
17392 typval_T *varp;
17393{
17394 static char_u mybuf[NUMBUFLEN];
17395
17396 return get_tv_string_buf_chk(varp, mybuf);
17397}
17398
17399 static char_u *
17400get_tv_string_buf_chk(varp, buf)
17401 typval_T *varp;
17402 char_u *buf;
17403{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017404 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017405 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017406 case VAR_NUMBER:
17407 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
17408 return buf;
17409 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017410 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017411 break;
17412 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017413 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000017414 break;
17415 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017416 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017417 break;
17418 case VAR_STRING:
17419 if (varp->vval.v_string != NULL)
17420 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017421 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017422 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017423 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017424 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017425 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017426 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017427}
17428
17429/*
17430 * Find variable "name" in the list of variables.
17431 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017432 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017433 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000017434 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017435 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017436 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017437find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017438 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017439 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017440{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017441 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017442 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017443
Bram Moolenaara7043832005-01-21 11:56:39 +000017444 ht = find_var_ht(name, &varname);
17445 if (htp != NULL)
17446 *htp = ht;
17447 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017448 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017449 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017450}
17451
17452/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017453 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000017454 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017455 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017456 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017457find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000017458 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000017459 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017460 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000017461{
Bram Moolenaar33570922005-01-25 22:26:29 +000017462 hashitem_T *hi;
17463
17464 if (*varname == NUL)
17465 {
17466 /* Must be something like "s:", otherwise "ht" would be NULL. */
17467 switch (varname[-2])
17468 {
17469 case 's': return &SCRIPT_SV(current_SID).sv_var;
17470 case 'g': return &globvars_var;
17471 case 'v': return &vimvars_var;
17472 case 'b': return &curbuf->b_bufvar;
17473 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017474#ifdef FEAT_WINDOWS
17475 case 't': return &curtab->tp_winvar;
17476#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017477 case 'l': return current_funccal == NULL
17478 ? NULL : &current_funccal->l_vars_var;
17479 case 'a': return current_funccal == NULL
17480 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000017481 }
17482 return NULL;
17483 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017484
17485 hi = hash_find(ht, varname);
17486 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017487 {
17488 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017489 * worked find the variable again. Don't auto-load a script if it was
17490 * loaded already, otherwise it would be loaded every time when
17491 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017492 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017493 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017494 hi = hash_find(ht, varname);
17495 if (HASHITEM_EMPTY(hi))
17496 return NULL;
17497 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017498 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017499}
17500
17501/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017502 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017503 * Set "varname" to the start of name without ':'.
17504 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017505 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017506find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017507 char_u *name;
17508 char_u **varname;
17509{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017510 hashitem_T *hi;
17511
Bram Moolenaar071d4272004-06-13 20:20:40 +000017512 if (name[1] != ':')
17513 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017514 /* The name must not start with a colon or #. */
17515 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017516 return NULL;
17517 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017518
17519 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017520 hi = hash_find(&compat_hashtab, name);
17521 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000017522 return &compat_hashtab;
17523
Bram Moolenaar071d4272004-06-13 20:20:40 +000017524 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017525 return &globvarht; /* global variable */
17526 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017527 }
17528 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017529 if (*name == 'g') /* global variable */
17530 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017531 /* There must be no ':' or '#' in the rest of the name, unless g: is used
17532 */
17533 if (vim_strchr(name + 2, ':') != NULL
17534 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017535 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017536 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017537 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017538 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017539 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017540#ifdef FEAT_WINDOWS
17541 if (*name == 't') /* tab page variable */
17542 return &curtab->tp_vars.dv_hashtab;
17543#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000017544 if (*name == 'v') /* v: variable */
17545 return &vimvarht;
17546 if (*name == 'a' && current_funccal != NULL) /* function argument */
17547 return &current_funccal->l_avars.dv_hashtab;
17548 if (*name == 'l' && current_funccal != NULL) /* local function variable */
17549 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017550 if (*name == 's' /* script variable */
17551 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
17552 return &SCRIPT_VARS(current_SID);
17553 return NULL;
17554}
17555
17556/*
17557 * Get the string value of a (global/local) variable.
17558 * Returns NULL when it doesn't exist.
17559 */
17560 char_u *
17561get_var_value(name)
17562 char_u *name;
17563{
Bram Moolenaar33570922005-01-25 22:26:29 +000017564 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017565
Bram Moolenaara7043832005-01-21 11:56:39 +000017566 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017567 if (v == NULL)
17568 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000017569 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017570}
17571
17572/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017573 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000017574 * sourcing this script and when executing functions defined in the script.
17575 */
17576 void
17577new_script_vars(id)
17578 scid_T id;
17579{
Bram Moolenaara7043832005-01-21 11:56:39 +000017580 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000017581 hashtab_T *ht;
17582 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000017583
Bram Moolenaar071d4272004-06-13 20:20:40 +000017584 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
17585 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017586 /* Re-allocating ga_data means that an ht_array pointing to
17587 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000017588 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000017589 for (i = 1; i <= ga_scripts.ga_len; ++i)
17590 {
17591 ht = &SCRIPT_VARS(i);
17592 if (ht->ht_mask == HT_INIT_SIZE - 1)
17593 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000017594 sv = &SCRIPT_SV(i);
17595 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000017596 }
17597
Bram Moolenaar071d4272004-06-13 20:20:40 +000017598 while (ga_scripts.ga_len < id)
17599 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017600 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
17601 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017602 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017603 }
17604 }
17605}
17606
17607/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017608 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
17609 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017610 */
17611 void
Bram Moolenaar33570922005-01-25 22:26:29 +000017612init_var_dict(dict, dict_var)
17613 dict_T *dict;
17614 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017615{
Bram Moolenaar33570922005-01-25 22:26:29 +000017616 hash_init(&dict->dv_hashtab);
17617 dict->dv_refcount = 99999;
17618 dict_var->di_tv.vval.v_dict = dict;
17619 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017620 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017621 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
17622 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017623}
17624
17625/*
17626 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000017627 * Frees all allocated variables and the value they contain.
17628 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017629 */
17630 void
Bram Moolenaara7043832005-01-21 11:56:39 +000017631vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000017632 hashtab_T *ht;
17633{
17634 vars_clear_ext(ht, TRUE);
17635}
17636
17637/*
17638 * Like vars_clear(), but only free the value if "free_val" is TRUE.
17639 */
17640 static void
17641vars_clear_ext(ht, free_val)
17642 hashtab_T *ht;
17643 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017644{
Bram Moolenaara7043832005-01-21 11:56:39 +000017645 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000017646 hashitem_T *hi;
17647 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017648
Bram Moolenaar33570922005-01-25 22:26:29 +000017649 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017650 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000017651 for (hi = ht->ht_array; todo > 0; ++hi)
17652 {
17653 if (!HASHITEM_EMPTY(hi))
17654 {
17655 --todo;
17656
Bram Moolenaar33570922005-01-25 22:26:29 +000017657 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000017658 * ht_array might change then. hash_clear() takes care of it
17659 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017660 v = HI2DI(hi);
17661 if (free_val)
17662 clear_tv(&v->di_tv);
17663 if ((v->di_flags & DI_FLAGS_FIX) == 0)
17664 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000017665 }
17666 }
17667 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017668 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017669}
17670
Bram Moolenaara7043832005-01-21 11:56:39 +000017671/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017672 * Delete a variable from hashtab "ht" at item "hi".
17673 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000017674 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017675 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000017676delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000017677 hashtab_T *ht;
17678 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017679{
Bram Moolenaar33570922005-01-25 22:26:29 +000017680 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017681
17682 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000017683 clear_tv(&di->di_tv);
17684 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017685}
17686
17687/*
17688 * List the value of one internal variable.
17689 */
17690 static void
17691list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000017692 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017693 char_u *prefix;
17694{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017695 char_u *tofree;
17696 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017697 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017698
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017699 s = echo_string(&v->di_tv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000017700 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017701 s == NULL ? (char_u *)"" : s);
17702 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017703}
17704
Bram Moolenaar071d4272004-06-13 20:20:40 +000017705 static void
17706list_one_var_a(prefix, name, type, string)
17707 char_u *prefix;
17708 char_u *name;
17709 int type;
17710 char_u *string;
17711{
17712 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
17713 if (name != NULL) /* "a:" vars don't have a name stored */
17714 msg_puts(name);
17715 msg_putchar(' ');
17716 msg_advance(22);
17717 if (type == VAR_NUMBER)
17718 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017719 else if (type == VAR_FUNC)
17720 msg_putchar('*');
17721 else if (type == VAR_LIST)
17722 {
17723 msg_putchar('[');
17724 if (*string == '[')
17725 ++string;
17726 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017727 else if (type == VAR_DICT)
17728 {
17729 msg_putchar('{');
17730 if (*string == '{')
17731 ++string;
17732 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017733 else
17734 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017735
Bram Moolenaar071d4272004-06-13 20:20:40 +000017736 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017737
17738 if (type == VAR_FUNC)
17739 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017740}
17741
17742/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017743 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000017744 * If the variable already exists, the value is updated.
17745 * Otherwise the variable is created.
17746 */
17747 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017748set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017749 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017750 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017751 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017752{
Bram Moolenaar33570922005-01-25 22:26:29 +000017753 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017754 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017755 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017756 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017757
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017758 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017759 {
17760 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
17761 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
17762 ? name[2] : name[0]))
17763 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017764 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017765 return;
17766 }
17767 if (function_exists(name))
17768 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017769 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017770 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017771 return;
17772 }
17773 }
17774
Bram Moolenaara7043832005-01-21 11:56:39 +000017775 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000017776 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000017777 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000017778 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000017779 return;
17780 }
17781
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017782 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017783 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017784 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017785 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017786 if (var_check_ro(v->di_flags, name)
17787 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000017788 return;
17789 if (v->di_tv.v_type != tv->v_type
17790 && !((v->di_tv.v_type == VAR_STRING
17791 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017792 && (tv->v_type == VAR_STRING
17793 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017794 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017795 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017796 return;
17797 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017798
17799 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000017800 * Handle setting internal v: variables separately: we don't change
17801 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000017802 */
17803 if (ht == &vimvarht)
17804 {
17805 if (v->di_tv.v_type == VAR_STRING)
17806 {
17807 vim_free(v->di_tv.vval.v_string);
17808 if (copy || tv->v_type != VAR_STRING)
17809 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
17810 else
17811 {
17812 /* Take over the string to avoid an extra alloc/free. */
17813 v->di_tv.vval.v_string = tv->vval.v_string;
17814 tv->vval.v_string = NULL;
17815 }
17816 }
17817 else if (v->di_tv.v_type != VAR_NUMBER)
17818 EMSG2(_(e_intern2), "set_var()");
17819 else
17820 v->di_tv.vval.v_number = get_tv_number(tv);
17821 return;
17822 }
17823
17824 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017825 }
17826 else /* add a new variable */
17827 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000017828 /* Can't add "v:" variable. */
17829 if (ht == &vimvarht)
17830 {
17831 EMSG2(_(e_illvar), name);
17832 return;
17833 }
17834
Bram Moolenaar92124a32005-06-17 22:03:40 +000017835 /* Make sure the variable name is valid. */
17836 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000017837 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
17838 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000017839 {
17840 EMSG2(_(e_illvar), varname);
17841 return;
17842 }
17843
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017844 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
17845 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000017846 if (v == NULL)
17847 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000017848 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000017849 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017850 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017851 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017852 return;
17853 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017854 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017855 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017856
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017857 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000017858 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017859 else
17860 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017861 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017862 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017863 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017865}
17866
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017867/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000017868 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000017869 * Also give an error message.
17870 */
17871 static int
17872var_check_ro(flags, name)
17873 int flags;
17874 char_u *name;
17875{
17876 if (flags & DI_FLAGS_RO)
17877 {
17878 EMSG2(_(e_readonlyvar), name);
17879 return TRUE;
17880 }
17881 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
17882 {
17883 EMSG2(_(e_readonlysbx), name);
17884 return TRUE;
17885 }
17886 return FALSE;
17887}
17888
17889/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000017890 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
17891 * Also give an error message.
17892 */
17893 static int
17894var_check_fixed(flags, name)
17895 int flags;
17896 char_u *name;
17897{
17898 if (flags & DI_FLAGS_FIX)
17899 {
17900 EMSG2(_("E795: Cannot delete variable %s"), name);
17901 return TRUE;
17902 }
17903 return FALSE;
17904}
17905
17906/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017907 * Return TRUE if typeval "tv" is set to be locked (immutable).
17908 * Also give an error message, using "name".
17909 */
17910 static int
17911tv_check_lock(lock, name)
17912 int lock;
17913 char_u *name;
17914{
17915 if (lock & VAR_LOCKED)
17916 {
17917 EMSG2(_("E741: Value is locked: %s"),
17918 name == NULL ? (char_u *)_("Unknown") : name);
17919 return TRUE;
17920 }
17921 if (lock & VAR_FIXED)
17922 {
17923 EMSG2(_("E742: Cannot change value of %s"),
17924 name == NULL ? (char_u *)_("Unknown") : name);
17925 return TRUE;
17926 }
17927 return FALSE;
17928}
17929
17930/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017931 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017932 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017933 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017934 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017935 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017936copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000017937 typval_T *from;
17938 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017939{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017940 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017941 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017942 switch (from->v_type)
17943 {
17944 case VAR_NUMBER:
17945 to->vval.v_number = from->vval.v_number;
17946 break;
17947 case VAR_STRING:
17948 case VAR_FUNC:
17949 if (from->vval.v_string == NULL)
17950 to->vval.v_string = NULL;
17951 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017952 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017953 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017954 if (from->v_type == VAR_FUNC)
17955 func_ref(to->vval.v_string);
17956 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017957 break;
17958 case VAR_LIST:
17959 if (from->vval.v_list == NULL)
17960 to->vval.v_list = NULL;
17961 else
17962 {
17963 to->vval.v_list = from->vval.v_list;
17964 ++to->vval.v_list->lv_refcount;
17965 }
17966 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017967 case VAR_DICT:
17968 if (from->vval.v_dict == NULL)
17969 to->vval.v_dict = NULL;
17970 else
17971 {
17972 to->vval.v_dict = from->vval.v_dict;
17973 ++to->vval.v_dict->dv_refcount;
17974 }
17975 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017976 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017977 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017978 break;
17979 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017980}
17981
17982/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000017983 * Make a copy of an item.
17984 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017985 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
17986 * reference to an already copied list/dict can be used.
17987 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017988 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017989 static int
17990item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000017991 typval_T *from;
17992 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017993 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017994 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017995{
17996 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017997 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017998
Bram Moolenaar33570922005-01-25 22:26:29 +000017999 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018000 {
18001 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018002 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018003 }
18004 ++recurse;
18005
18006 switch (from->v_type)
18007 {
18008 case VAR_NUMBER:
18009 case VAR_STRING:
18010 case VAR_FUNC:
18011 copy_tv(from, to);
18012 break;
18013 case VAR_LIST:
18014 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018015 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018016 if (from->vval.v_list == NULL)
18017 to->vval.v_list = NULL;
18018 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
18019 {
18020 /* use the copy made earlier */
18021 to->vval.v_list = from->vval.v_list->lv_copylist;
18022 ++to->vval.v_list->lv_refcount;
18023 }
18024 else
18025 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
18026 if (to->vval.v_list == NULL)
18027 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018028 break;
18029 case VAR_DICT:
18030 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018031 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018032 if (from->vval.v_dict == NULL)
18033 to->vval.v_dict = NULL;
18034 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
18035 {
18036 /* use the copy made earlier */
18037 to->vval.v_dict = from->vval.v_dict->dv_copydict;
18038 ++to->vval.v_dict->dv_refcount;
18039 }
18040 else
18041 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
18042 if (to->vval.v_dict == NULL)
18043 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018044 break;
18045 default:
18046 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018047 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018048 }
18049 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018050 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018051}
18052
18053/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018054 * ":echo expr1 ..." print each argument separated with a space, add a
18055 * newline at the end.
18056 * ":echon expr1 ..." print each argument plain.
18057 */
18058 void
18059ex_echo(eap)
18060 exarg_T *eap;
18061{
18062 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018063 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018064 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018065 char_u *p;
18066 int needclr = TRUE;
18067 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018068 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018069
18070 if (eap->skip)
18071 ++emsg_skip;
18072 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
18073 {
18074 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018075 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018076 {
18077 /*
18078 * Report the invalid expression unless the expression evaluation
18079 * has been cancelled due to an aborting error, an interrupt, or an
18080 * exception.
18081 */
18082 if (!aborting())
18083 EMSG2(_(e_invexpr2), p);
18084 break;
18085 }
18086 if (!eap->skip)
18087 {
18088 if (atstart)
18089 {
18090 atstart = FALSE;
18091 /* Call msg_start() after eval1(), evaluating the expression
18092 * may cause a message to appear. */
18093 if (eap->cmdidx == CMD_echo)
18094 msg_start();
18095 }
18096 else if (eap->cmdidx == CMD_echo)
18097 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018098 p = echo_string(&rettv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018099 if (p != NULL)
18100 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018101 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018102 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018103 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018104 if (*p != TAB && needclr)
18105 {
18106 /* remove any text still there from the command */
18107 msg_clr_eos();
18108 needclr = FALSE;
18109 }
18110 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018111 }
18112 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018113 {
18114#ifdef FEAT_MBYTE
18115 if (has_mbyte)
18116 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018117 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018118
18119 (void)msg_outtrans_len_attr(p, i, echo_attr);
18120 p += i - 1;
18121 }
18122 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000018123#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018124 (void)msg_outtrans_len_attr(p, 1, echo_attr);
18125 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018126 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018127 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018128 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018129 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018130 arg = skipwhite(arg);
18131 }
18132 eap->nextcmd = check_nextcmd(arg);
18133
18134 if (eap->skip)
18135 --emsg_skip;
18136 else
18137 {
18138 /* remove text that may still be there from the command */
18139 if (needclr)
18140 msg_clr_eos();
18141 if (eap->cmdidx == CMD_echo)
18142 msg_end();
18143 }
18144}
18145
18146/*
18147 * ":echohl {name}".
18148 */
18149 void
18150ex_echohl(eap)
18151 exarg_T *eap;
18152{
18153 int id;
18154
18155 id = syn_name2id(eap->arg);
18156 if (id == 0)
18157 echo_attr = 0;
18158 else
18159 echo_attr = syn_id2attr(id);
18160}
18161
18162/*
18163 * ":execute expr1 ..." execute the result of an expression.
18164 * ":echomsg expr1 ..." Print a message
18165 * ":echoerr expr1 ..." Print an error
18166 * Each gets spaces around each argument and a newline at the end for
18167 * echo commands
18168 */
18169 void
18170ex_execute(eap)
18171 exarg_T *eap;
18172{
18173 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018174 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018175 int ret = OK;
18176 char_u *p;
18177 garray_T ga;
18178 int len;
18179 int save_did_emsg;
18180
18181 ga_init2(&ga, 1, 80);
18182
18183 if (eap->skip)
18184 ++emsg_skip;
18185 while (*arg != NUL && *arg != '|' && *arg != '\n')
18186 {
18187 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018188 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018189 {
18190 /*
18191 * Report the invalid expression unless the expression evaluation
18192 * has been cancelled due to an aborting error, an interrupt, or an
18193 * exception.
18194 */
18195 if (!aborting())
18196 EMSG2(_(e_invexpr2), p);
18197 ret = FAIL;
18198 break;
18199 }
18200
18201 if (!eap->skip)
18202 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018203 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018204 len = (int)STRLEN(p);
18205 if (ga_grow(&ga, len + 2) == FAIL)
18206 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018207 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018208 ret = FAIL;
18209 break;
18210 }
18211 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018212 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018213 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018214 ga.ga_len += len;
18215 }
18216
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018217 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018218 arg = skipwhite(arg);
18219 }
18220
18221 if (ret != FAIL && ga.ga_data != NULL)
18222 {
18223 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000018224 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018225 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000018226 out_flush();
18227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018228 else if (eap->cmdidx == CMD_echoerr)
18229 {
18230 /* We don't want to abort following commands, restore did_emsg. */
18231 save_did_emsg = did_emsg;
18232 EMSG((char_u *)ga.ga_data);
18233 if (!force_abort)
18234 did_emsg = save_did_emsg;
18235 }
18236 else if (eap->cmdidx == CMD_execute)
18237 do_cmdline((char_u *)ga.ga_data,
18238 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
18239 }
18240
18241 ga_clear(&ga);
18242
18243 if (eap->skip)
18244 --emsg_skip;
18245
18246 eap->nextcmd = check_nextcmd(arg);
18247}
18248
18249/*
18250 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
18251 * "arg" points to the "&" or '+' when called, to "option" when returning.
18252 * Returns NULL when no option name found. Otherwise pointer to the char
18253 * after the option name.
18254 */
18255 static char_u *
18256find_option_end(arg, opt_flags)
18257 char_u **arg;
18258 int *opt_flags;
18259{
18260 char_u *p = *arg;
18261
18262 ++p;
18263 if (*p == 'g' && p[1] == ':')
18264 {
18265 *opt_flags = OPT_GLOBAL;
18266 p += 2;
18267 }
18268 else if (*p == 'l' && p[1] == ':')
18269 {
18270 *opt_flags = OPT_LOCAL;
18271 p += 2;
18272 }
18273 else
18274 *opt_flags = 0;
18275
18276 if (!ASCII_ISALPHA(*p))
18277 return NULL;
18278 *arg = p;
18279
18280 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
18281 p += 4; /* termcap option */
18282 else
18283 while (ASCII_ISALPHA(*p))
18284 ++p;
18285 return p;
18286}
18287
18288/*
18289 * ":function"
18290 */
18291 void
18292ex_function(eap)
18293 exarg_T *eap;
18294{
18295 char_u *theline;
18296 int j;
18297 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018298 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018299 char_u *name = NULL;
18300 char_u *p;
18301 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018302 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018303 garray_T newargs;
18304 garray_T newlines;
18305 int varargs = FALSE;
18306 int mustend = FALSE;
18307 int flags = 0;
18308 ufunc_T *fp;
18309 int indent;
18310 int nesting;
18311 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000018312 dictitem_T *v;
18313 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018314 static int func_nr = 0; /* number for nameless function */
18315 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018316 hashtab_T *ht;
18317 int todo;
18318 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018319 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018320
18321 /*
18322 * ":function" without argument: list functions.
18323 */
18324 if (ends_excmd(*eap->arg))
18325 {
18326 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018327 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018328 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000018329 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018330 {
18331 if (!HASHITEM_EMPTY(hi))
18332 {
18333 --todo;
18334 fp = HI2UF(hi);
18335 if (!isdigit(*fp->uf_name))
18336 list_func_head(fp, FALSE);
18337 }
18338 }
18339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018340 eap->nextcmd = check_nextcmd(eap->arg);
18341 return;
18342 }
18343
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018344 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018345 * ":function /pat": list functions matching pattern.
18346 */
18347 if (*eap->arg == '/')
18348 {
18349 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
18350 if (!eap->skip)
18351 {
18352 regmatch_T regmatch;
18353
18354 c = *p;
18355 *p = NUL;
18356 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
18357 *p = c;
18358 if (regmatch.regprog != NULL)
18359 {
18360 regmatch.rm_ic = p_ic;
18361
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018362 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018363 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
18364 {
18365 if (!HASHITEM_EMPTY(hi))
18366 {
18367 --todo;
18368 fp = HI2UF(hi);
18369 if (!isdigit(*fp->uf_name)
18370 && vim_regexec(&regmatch, fp->uf_name, 0))
18371 list_func_head(fp, FALSE);
18372 }
18373 }
18374 }
18375 }
18376 if (*p == '/')
18377 ++p;
18378 eap->nextcmd = check_nextcmd(p);
18379 return;
18380 }
18381
18382 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018383 * Get the function name. There are these situations:
18384 * func normal function name
18385 * "name" == func, "fudi.fd_dict" == NULL
18386 * dict.func new dictionary entry
18387 * "name" == NULL, "fudi.fd_dict" set,
18388 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
18389 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018390 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018391 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18392 * dict.func existing dict entry that's not a Funcref
18393 * "name" == NULL, "fudi.fd_dict" set,
18394 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18395 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018396 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018397 name = trans_function_name(&p, eap->skip, 0, &fudi);
18398 paren = (vim_strchr(p, '(') != NULL);
18399 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018400 {
18401 /*
18402 * Return on an invalid expression in braces, unless the expression
18403 * evaluation has been cancelled due to an aborting error, an
18404 * interrupt, or an exception.
18405 */
18406 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018407 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018408 if (!eap->skip && fudi.fd_newkey != NULL)
18409 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018410 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018411 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018413 else
18414 eap->skip = TRUE;
18415 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000018416
Bram Moolenaar071d4272004-06-13 20:20:40 +000018417 /* An error in a function call during evaluation of an expression in magic
18418 * braces should not cause the function not to be defined. */
18419 saved_did_emsg = did_emsg;
18420 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018421
18422 /*
18423 * ":function func" with only function name: list function.
18424 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018425 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018426 {
18427 if (!ends_excmd(*skipwhite(p)))
18428 {
18429 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018430 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018431 }
18432 eap->nextcmd = check_nextcmd(p);
18433 if (eap->nextcmd != NULL)
18434 *p = NUL;
18435 if (!eap->skip && !got_int)
18436 {
18437 fp = find_func(name);
18438 if (fp != NULL)
18439 {
18440 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018441 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018442 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018443 if (FUNCLINE(fp, j) == NULL)
18444 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018445 msg_putchar('\n');
18446 msg_outnum((long)(j + 1));
18447 if (j < 9)
18448 msg_putchar(' ');
18449 if (j < 99)
18450 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018451 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018452 out_flush(); /* show a line at a time */
18453 ui_breakcheck();
18454 }
18455 if (!got_int)
18456 {
18457 msg_putchar('\n');
18458 msg_puts((char_u *)" endfunction");
18459 }
18460 }
18461 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018462 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018463 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018464 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018465 }
18466
18467 /*
18468 * ":function name(arg1, arg2)" Define function.
18469 */
18470 p = skipwhite(p);
18471 if (*p != '(')
18472 {
18473 if (!eap->skip)
18474 {
18475 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018476 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018477 }
18478 /* attempt to continue by skipping some text */
18479 if (vim_strchr(p, '(') != NULL)
18480 p = vim_strchr(p, '(');
18481 }
18482 p = skipwhite(p + 1);
18483
18484 ga_init2(&newargs, (int)sizeof(char_u *), 3);
18485 ga_init2(&newlines, (int)sizeof(char_u *), 3);
18486
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018487 if (!eap->skip)
18488 {
18489 /* Check the name of the function. */
18490 if (name != NULL)
18491 arg = name;
18492 else
18493 arg = fudi.fd_newkey;
18494 if (arg != NULL)
18495 {
18496 if (*arg == K_SPECIAL)
18497 j = 3;
18498 else
18499 j = 0;
18500 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
18501 : eval_isnamec(arg[j])))
18502 ++j;
18503 if (arg[j] != NUL)
18504 emsg_funcname(_(e_invarg2), arg);
18505 }
18506 }
18507
Bram Moolenaar071d4272004-06-13 20:20:40 +000018508 /*
18509 * Isolate the arguments: "arg1, arg2, ...)"
18510 */
18511 while (*p != ')')
18512 {
18513 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
18514 {
18515 varargs = TRUE;
18516 p += 3;
18517 mustend = TRUE;
18518 }
18519 else
18520 {
18521 arg = p;
18522 while (ASCII_ISALNUM(*p) || *p == '_')
18523 ++p;
18524 if (arg == p || isdigit(*arg)
18525 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
18526 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
18527 {
18528 if (!eap->skip)
18529 EMSG2(_("E125: Illegal argument: %s"), arg);
18530 break;
18531 }
18532 if (ga_grow(&newargs, 1) == FAIL)
18533 goto erret;
18534 c = *p;
18535 *p = NUL;
18536 arg = vim_strsave(arg);
18537 if (arg == NULL)
18538 goto erret;
18539 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
18540 *p = c;
18541 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018542 if (*p == ',')
18543 ++p;
18544 else
18545 mustend = TRUE;
18546 }
18547 p = skipwhite(p);
18548 if (mustend && *p != ')')
18549 {
18550 if (!eap->skip)
18551 EMSG2(_(e_invarg2), eap->arg);
18552 break;
18553 }
18554 }
18555 ++p; /* skip the ')' */
18556
Bram Moolenaare9a41262005-01-15 22:18:47 +000018557 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018558 for (;;)
18559 {
18560 p = skipwhite(p);
18561 if (STRNCMP(p, "range", 5) == 0)
18562 {
18563 flags |= FC_RANGE;
18564 p += 5;
18565 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000018566 else if (STRNCMP(p, "dict", 4) == 0)
18567 {
18568 flags |= FC_DICT;
18569 p += 4;
18570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018571 else if (STRNCMP(p, "abort", 5) == 0)
18572 {
18573 flags |= FC_ABORT;
18574 p += 5;
18575 }
18576 else
18577 break;
18578 }
18579
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018580 /* When there is a line break use what follows for the function body.
18581 * Makes 'exe "func Test()\n...\nendfunc"' work. */
18582 if (*p == '\n')
18583 line_arg = p + 1;
18584 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018585 EMSG(_(e_trailing));
18586
18587 /*
18588 * Read the body of the function, until ":endfunction" is found.
18589 */
18590 if (KeyTyped)
18591 {
18592 /* Check if the function already exists, don't let the user type the
18593 * whole function before telling him it doesn't work! For a script we
18594 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018595 if (!eap->skip && !eap->forceit)
18596 {
18597 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
18598 EMSG(_(e_funcdict));
18599 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018600 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018602
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018603 if (!eap->skip && did_emsg)
18604 goto erret;
18605
Bram Moolenaar071d4272004-06-13 20:20:40 +000018606 msg_putchar('\n'); /* don't overwrite the function name */
18607 cmdline_row = msg_row;
18608 }
18609
18610 indent = 2;
18611 nesting = 0;
18612 for (;;)
18613 {
18614 msg_scroll = TRUE;
18615 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018616 sourcing_lnum_off = sourcing_lnum;
18617
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018618 if (line_arg != NULL)
18619 {
18620 /* Use eap->arg, split up in parts by line breaks. */
18621 theline = line_arg;
18622 p = vim_strchr(theline, '\n');
18623 if (p == NULL)
18624 line_arg += STRLEN(line_arg);
18625 else
18626 {
18627 *p = NUL;
18628 line_arg = p + 1;
18629 }
18630 }
18631 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018632 theline = getcmdline(':', 0L, indent);
18633 else
18634 theline = eap->getline(':', eap->cookie, indent);
18635 if (KeyTyped)
18636 lines_left = Rows - 1;
18637 if (theline == NULL)
18638 {
18639 EMSG(_("E126: Missing :endfunction"));
18640 goto erret;
18641 }
18642
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018643 /* Detect line continuation: sourcing_lnum increased more than one. */
18644 if (sourcing_lnum > sourcing_lnum_off + 1)
18645 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
18646 else
18647 sourcing_lnum_off = 0;
18648
Bram Moolenaar071d4272004-06-13 20:20:40 +000018649 if (skip_until != NULL)
18650 {
18651 /* between ":append" and "." and between ":python <<EOF" and "EOF"
18652 * don't check for ":endfunc". */
18653 if (STRCMP(theline, skip_until) == 0)
18654 {
18655 vim_free(skip_until);
18656 skip_until = NULL;
18657 }
18658 }
18659 else
18660 {
18661 /* skip ':' and blanks*/
18662 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
18663 ;
18664
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018665 /* Check for "endfunction". */
18666 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018667 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018668 if (line_arg == NULL)
18669 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018670 break;
18671 }
18672
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018673 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000018674 * at "end". */
18675 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
18676 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018677 else if (STRNCMP(p, "if", 2) == 0
18678 || STRNCMP(p, "wh", 2) == 0
18679 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000018680 || STRNCMP(p, "try", 3) == 0)
18681 indent += 2;
18682
18683 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018684 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018685 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018686 if (*p == '!')
18687 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018688 p += eval_fname_script(p);
18689 if (ASCII_ISALPHA(*p))
18690 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018691 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018692 if (*skipwhite(p) == '(')
18693 {
18694 ++nesting;
18695 indent += 2;
18696 }
18697 }
18698 }
18699
18700 /* Check for ":append" or ":insert". */
18701 p = skip_range(p, NULL);
18702 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
18703 || (p[0] == 'i'
18704 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
18705 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
18706 skip_until = vim_strsave((char_u *)".");
18707
18708 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
18709 arg = skipwhite(skiptowhite(p));
18710 if (arg[0] == '<' && arg[1] =='<'
18711 && ((p[0] == 'p' && p[1] == 'y'
18712 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
18713 || (p[0] == 'p' && p[1] == 'e'
18714 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
18715 || (p[0] == 't' && p[1] == 'c'
18716 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
18717 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
18718 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000018719 || (p[0] == 'm' && p[1] == 'z'
18720 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018721 ))
18722 {
18723 /* ":python <<" continues until a dot, like ":append" */
18724 p = skipwhite(arg + 2);
18725 if (*p == NUL)
18726 skip_until = vim_strsave((char_u *)".");
18727 else
18728 skip_until = vim_strsave(p);
18729 }
18730 }
18731
18732 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018733 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018734 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018735 if (line_arg == NULL)
18736 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018737 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018738 }
18739
18740 /* Copy the line to newly allocated memory. get_one_sourceline()
18741 * allocates 250 bytes per line, this saves 80% on average. The cost
18742 * is an extra alloc/free. */
18743 p = vim_strsave(theline);
18744 if (p != NULL)
18745 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018746 if (line_arg == NULL)
18747 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018748 theline = p;
18749 }
18750
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018751 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
18752
18753 /* Add NULL lines for continuation lines, so that the line count is
18754 * equal to the index in the growarray. */
18755 while (sourcing_lnum_off-- > 0)
18756 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018757
18758 /* Check for end of eap->arg. */
18759 if (line_arg != NULL && *line_arg == NUL)
18760 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018761 }
18762
18763 /* Don't define the function when skipping commands or when an error was
18764 * detected. */
18765 if (eap->skip || did_emsg)
18766 goto erret;
18767
18768 /*
18769 * If there are no errors, add the function
18770 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018771 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018772 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018773 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000018774 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018775 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018776 emsg_funcname("E707: Function name conflicts with variable: %s",
18777 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018778 goto erret;
18779 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018780
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018781 fp = find_func(name);
18782 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018783 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018784 if (!eap->forceit)
18785 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018786 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018787 goto erret;
18788 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018789 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018790 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018791 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018792 name);
18793 goto erret;
18794 }
18795 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018796 ga_clear_strings(&(fp->uf_args));
18797 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018798 vim_free(name);
18799 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018801 }
18802 else
18803 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018804 char numbuf[20];
18805
18806 fp = NULL;
18807 if (fudi.fd_newkey == NULL && !eap->forceit)
18808 {
18809 EMSG(_(e_funcdict));
18810 goto erret;
18811 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000018812 if (fudi.fd_di == NULL)
18813 {
18814 /* Can't add a function to a locked dictionary */
18815 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
18816 goto erret;
18817 }
18818 /* Can't change an existing function if it is locked */
18819 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
18820 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018821
18822 /* Give the function a sequential number. Can only be used with a
18823 * Funcref! */
18824 vim_free(name);
18825 sprintf(numbuf, "%d", ++func_nr);
18826 name = vim_strsave((char_u *)numbuf);
18827 if (name == NULL)
18828 goto erret;
18829 }
18830
18831 if (fp == NULL)
18832 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018833 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018834 {
18835 int slen, plen;
18836 char_u *scriptname;
18837
18838 /* Check that the autoload name matches the script name. */
18839 j = FAIL;
18840 if (sourcing_name != NULL)
18841 {
18842 scriptname = autoload_name(name);
18843 if (scriptname != NULL)
18844 {
18845 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018846 plen = (int)STRLEN(p);
18847 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018848 if (slen > plen && fnamecmp(p,
18849 sourcing_name + slen - plen) == 0)
18850 j = OK;
18851 vim_free(scriptname);
18852 }
18853 }
18854 if (j == FAIL)
18855 {
18856 EMSG2(_("E746: Function name does not match script file name: %s"), name);
18857 goto erret;
18858 }
18859 }
18860
18861 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018862 if (fp == NULL)
18863 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018864
18865 if (fudi.fd_dict != NULL)
18866 {
18867 if (fudi.fd_di == NULL)
18868 {
18869 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018870 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018871 if (fudi.fd_di == NULL)
18872 {
18873 vim_free(fp);
18874 goto erret;
18875 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018876 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
18877 {
18878 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000018879 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018880 goto erret;
18881 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018882 }
18883 else
18884 /* overwrite existing dict entry */
18885 clear_tv(&fudi.fd_di->di_tv);
18886 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018887 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018888 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018889 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018890
18891 /* behave like "dict" was used */
18892 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018893 }
18894
Bram Moolenaar071d4272004-06-13 20:20:40 +000018895 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018896 STRCPY(fp->uf_name, name);
18897 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018898 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018899 fp->uf_args = newargs;
18900 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018901#ifdef FEAT_PROFILE
18902 fp->uf_tml_count = NULL;
18903 fp->uf_tml_total = NULL;
18904 fp->uf_tml_self = NULL;
18905 fp->uf_profiling = FALSE;
18906 if (prof_def_func())
18907 func_do_profile(fp);
18908#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018909 fp->uf_varargs = varargs;
18910 fp->uf_flags = flags;
18911 fp->uf_calls = 0;
18912 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018913 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018914
18915erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000018916 ga_clear_strings(&newargs);
18917 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018918ret_free:
18919 vim_free(skip_until);
18920 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018921 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018922 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018923}
18924
18925/*
18926 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000018927 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018928 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018929 * flags:
18930 * TFN_INT: internal function name OK
18931 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000018932 * Advances "pp" to just after the function name (if no error).
18933 */
18934 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018935trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018936 char_u **pp;
18937 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018938 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000018939 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018940{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018941 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018942 char_u *start;
18943 char_u *end;
18944 int lead;
18945 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018946 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000018947 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018948
18949 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018950 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018951 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000018952
18953 /* Check for hard coded <SNR>: already translated function ID (from a user
18954 * command). */
18955 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
18956 && (*pp)[2] == (int)KE_SNR)
18957 {
18958 *pp += 3;
18959 len = get_id_len(pp) + 3;
18960 return vim_strnsave(start, len);
18961 }
18962
18963 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
18964 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018965 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000018966 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018967 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018968
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018969 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
18970 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018971 if (end == start)
18972 {
18973 if (!skip)
18974 EMSG(_("E129: Function name required"));
18975 goto theend;
18976 }
Bram Moolenaara7043832005-01-21 11:56:39 +000018977 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018978 {
18979 /*
18980 * Report an invalid expression in braces, unless the expression
18981 * evaluation has been cancelled due to an aborting error, an
18982 * interrupt, or an exception.
18983 */
18984 if (!aborting())
18985 {
18986 if (end != NULL)
18987 EMSG2(_(e_invarg2), start);
18988 }
18989 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018990 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018991 goto theend;
18992 }
18993
18994 if (lv.ll_tv != NULL)
18995 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018996 if (fdp != NULL)
18997 {
18998 fdp->fd_dict = lv.ll_dict;
18999 fdp->fd_newkey = lv.ll_newkey;
19000 lv.ll_newkey = NULL;
19001 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019002 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019003 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
19004 {
19005 name = vim_strsave(lv.ll_tv->vval.v_string);
19006 *pp = end;
19007 }
19008 else
19009 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019010 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
19011 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019012 EMSG(_(e_funcref));
19013 else
19014 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019015 name = NULL;
19016 }
19017 goto theend;
19018 }
19019
19020 if (lv.ll_name == NULL)
19021 {
19022 /* Error found, but continue after the function name. */
19023 *pp = end;
19024 goto theend;
19025 }
19026
19027 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000019028 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019029 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000019030 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
19031 && STRNCMP(lv.ll_name, "s:", 2) == 0)
19032 {
19033 /* When there was "s:" already or the name expanded to get a
19034 * leading "s:" then remove it. */
19035 lv.ll_name += 2;
19036 len -= 2;
19037 lead = 2;
19038 }
19039 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019040 else
Bram Moolenaara7043832005-01-21 11:56:39 +000019041 {
19042 if (lead == 2) /* skip over "s:" */
19043 lv.ll_name += 2;
19044 len = (int)(end - lv.ll_name);
19045 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019046
19047 /*
19048 * Copy the function name to allocated memory.
19049 * Accept <SID>name() inside a script, translate into <SNR>123_name().
19050 * Accept <SNR>123_name() outside a script.
19051 */
19052 if (skip)
19053 lead = 0; /* do nothing */
19054 else if (lead > 0)
19055 {
19056 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000019057 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
19058 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019059 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000019060 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019061 if (current_SID <= 0)
19062 {
19063 EMSG(_(e_usingsid));
19064 goto theend;
19065 }
19066 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
19067 lead += (int)STRLEN(sid_buf);
19068 }
19069 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019070 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019071 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019072 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019073 goto theend;
19074 }
19075 name = alloc((unsigned)(len + lead + 1));
19076 if (name != NULL)
19077 {
19078 if (lead > 0)
19079 {
19080 name[0] = K_SPECIAL;
19081 name[1] = KS_EXTRA;
19082 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000019083 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019084 STRCPY(name + 3, sid_buf);
19085 }
19086 mch_memmove(name + lead, lv.ll_name, (size_t)len);
19087 name[len + lead] = NUL;
19088 }
19089 *pp = end;
19090
19091theend:
19092 clear_lval(&lv);
19093 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019094}
19095
19096/*
19097 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
19098 * Return 2 if "p" starts with "s:".
19099 * Return 0 otherwise.
19100 */
19101 static int
19102eval_fname_script(p)
19103 char_u *p;
19104{
19105 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
19106 || STRNICMP(p + 1, "SNR>", 4) == 0))
19107 return 5;
19108 if (p[0] == 's' && p[1] == ':')
19109 return 2;
19110 return 0;
19111}
19112
19113/*
19114 * Return TRUE if "p" starts with "<SID>" or "s:".
19115 * Only works if eval_fname_script() returned non-zero for "p"!
19116 */
19117 static int
19118eval_fname_sid(p)
19119 char_u *p;
19120{
19121 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
19122}
19123
19124/*
19125 * List the head of the function: "name(arg1, arg2)".
19126 */
19127 static void
19128list_func_head(fp, indent)
19129 ufunc_T *fp;
19130 int indent;
19131{
19132 int j;
19133
19134 msg_start();
19135 if (indent)
19136 MSG_PUTS(" ");
19137 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019138 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019139 {
19140 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019141 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019142 }
19143 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019144 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019145 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019146 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019147 {
19148 if (j)
19149 MSG_PUTS(", ");
19150 msg_puts(FUNCARG(fp, j));
19151 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019152 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019153 {
19154 if (j)
19155 MSG_PUTS(", ");
19156 MSG_PUTS("...");
19157 }
19158 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019159 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000019160 if (p_verbose > 0)
19161 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019162}
19163
19164/*
19165 * Find a function by name, return pointer to it in ufuncs.
19166 * Return NULL for unknown function.
19167 */
19168 static ufunc_T *
19169find_func(name)
19170 char_u *name;
19171{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019172 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019173
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019174 hi = hash_find(&func_hashtab, name);
19175 if (!HASHITEM_EMPTY(hi))
19176 return HI2UF(hi);
19177 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019178}
19179
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019180#if defined(EXITFREE) || defined(PROTO)
19181 void
19182free_all_functions()
19183{
19184 hashitem_T *hi;
19185
19186 /* Need to start all over every time, because func_free() may change the
19187 * hash table. */
19188 while (func_hashtab.ht_used > 0)
19189 for (hi = func_hashtab.ht_array; ; ++hi)
19190 if (!HASHITEM_EMPTY(hi))
19191 {
19192 func_free(HI2UF(hi));
19193 break;
19194 }
19195}
19196#endif
19197
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019198/*
19199 * Return TRUE if a function "name" exists.
19200 */
19201 static int
19202function_exists(name)
19203 char_u *name;
19204{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000019205 char_u *nm = name;
19206 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019207 int n = FALSE;
19208
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000019209 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000019210 nm = skipwhite(nm);
19211
19212 /* Only accept "funcname", "funcname ", "funcname (..." and
19213 * "funcname(...", not "funcname!...". */
19214 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019215 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019216 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019217 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019218 else
19219 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019220 }
Bram Moolenaar79783442006-05-05 21:18:03 +000019221 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019222 return n;
19223}
19224
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019225/*
19226 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019227 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019228 */
19229 static int
19230builtin_function(name)
19231 char_u *name;
19232{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019233 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
19234 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019235}
19236
Bram Moolenaar05159a02005-02-26 23:04:13 +000019237#if defined(FEAT_PROFILE) || defined(PROTO)
19238/*
19239 * Start profiling function "fp".
19240 */
19241 static void
19242func_do_profile(fp)
19243 ufunc_T *fp;
19244{
19245 fp->uf_tm_count = 0;
19246 profile_zero(&fp->uf_tm_self);
19247 profile_zero(&fp->uf_tm_total);
19248 if (fp->uf_tml_count == NULL)
19249 fp->uf_tml_count = (int *)alloc_clear((unsigned)
19250 (sizeof(int) * fp->uf_lines.ga_len));
19251 if (fp->uf_tml_total == NULL)
19252 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
19253 (sizeof(proftime_T) * fp->uf_lines.ga_len));
19254 if (fp->uf_tml_self == NULL)
19255 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
19256 (sizeof(proftime_T) * fp->uf_lines.ga_len));
19257 fp->uf_tml_idx = -1;
19258 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
19259 || fp->uf_tml_self == NULL)
19260 return; /* out of memory */
19261
19262 fp->uf_profiling = TRUE;
19263}
19264
19265/*
19266 * Dump the profiling results for all functions in file "fd".
19267 */
19268 void
19269func_dump_profile(fd)
19270 FILE *fd;
19271{
19272 hashitem_T *hi;
19273 int todo;
19274 ufunc_T *fp;
19275 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000019276 ufunc_T **sorttab;
19277 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019278
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019279 todo = (int)func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000019280 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
19281
Bram Moolenaar05159a02005-02-26 23:04:13 +000019282 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
19283 {
19284 if (!HASHITEM_EMPTY(hi))
19285 {
19286 --todo;
19287 fp = HI2UF(hi);
19288 if (fp->uf_profiling)
19289 {
Bram Moolenaar73830342005-02-28 22:48:19 +000019290 if (sorttab != NULL)
19291 sorttab[st_len++] = fp;
19292
Bram Moolenaar05159a02005-02-26 23:04:13 +000019293 if (fp->uf_name[0] == K_SPECIAL)
19294 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
19295 else
19296 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
19297 if (fp->uf_tm_count == 1)
19298 fprintf(fd, "Called 1 time\n");
19299 else
19300 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
19301 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
19302 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
19303 fprintf(fd, "\n");
19304 fprintf(fd, "count total (s) self (s)\n");
19305
19306 for (i = 0; i < fp->uf_lines.ga_len; ++i)
19307 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019308 if (FUNCLINE(fp, i) == NULL)
19309 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000019310 prof_func_line(fd, fp->uf_tml_count[i],
19311 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019312 fprintf(fd, "%s\n", FUNCLINE(fp, i));
19313 }
19314 fprintf(fd, "\n");
19315 }
19316 }
19317 }
Bram Moolenaar73830342005-02-28 22:48:19 +000019318
19319 if (sorttab != NULL && st_len > 0)
19320 {
19321 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
19322 prof_total_cmp);
19323 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
19324 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
19325 prof_self_cmp);
19326 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
19327 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019328}
Bram Moolenaar73830342005-02-28 22:48:19 +000019329
19330 static void
19331prof_sort_list(fd, sorttab, st_len, title, prefer_self)
19332 FILE *fd;
19333 ufunc_T **sorttab;
19334 int st_len;
19335 char *title;
19336 int prefer_self; /* when equal print only self time */
19337{
19338 int i;
19339 ufunc_T *fp;
19340
19341 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
19342 fprintf(fd, "count total (s) self (s) function\n");
19343 for (i = 0; i < 20 && i < st_len; ++i)
19344 {
19345 fp = sorttab[i];
19346 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
19347 prefer_self);
19348 if (fp->uf_name[0] == K_SPECIAL)
19349 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
19350 else
19351 fprintf(fd, " %s()\n", fp->uf_name);
19352 }
19353 fprintf(fd, "\n");
19354}
19355
19356/*
19357 * Print the count and times for one function or function line.
19358 */
19359 static void
19360prof_func_line(fd, count, total, self, prefer_self)
19361 FILE *fd;
19362 int count;
19363 proftime_T *total;
19364 proftime_T *self;
19365 int prefer_self; /* when equal print only self time */
19366{
19367 if (count > 0)
19368 {
19369 fprintf(fd, "%5d ", count);
19370 if (prefer_self && profile_equal(total, self))
19371 fprintf(fd, " ");
19372 else
19373 fprintf(fd, "%s ", profile_msg(total));
19374 if (!prefer_self && profile_equal(total, self))
19375 fprintf(fd, " ");
19376 else
19377 fprintf(fd, "%s ", profile_msg(self));
19378 }
19379 else
19380 fprintf(fd, " ");
19381}
19382
19383/*
19384 * Compare function for total time sorting.
19385 */
19386 static int
19387#ifdef __BORLANDC__
19388_RTLENTRYF
19389#endif
19390prof_total_cmp(s1, s2)
19391 const void *s1;
19392 const void *s2;
19393{
19394 ufunc_T *p1, *p2;
19395
19396 p1 = *(ufunc_T **)s1;
19397 p2 = *(ufunc_T **)s2;
19398 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
19399}
19400
19401/*
19402 * Compare function for self time sorting.
19403 */
19404 static int
19405#ifdef __BORLANDC__
19406_RTLENTRYF
19407#endif
19408prof_self_cmp(s1, s2)
19409 const void *s1;
19410 const void *s2;
19411{
19412 ufunc_T *p1, *p2;
19413
19414 p1 = *(ufunc_T **)s1;
19415 p2 = *(ufunc_T **)s2;
19416 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
19417}
19418
Bram Moolenaar05159a02005-02-26 23:04:13 +000019419#endif
19420
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019421/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019422 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019423 * Return TRUE if a package was loaded.
19424 */
19425 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019426script_autoload(name, reload)
19427 char_u *name;
19428 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019429{
19430 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019431 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019432 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019433 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019434
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019435 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019436 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019437 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019438 return FALSE;
19439
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019440 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019441
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019442 /* Find the name in the list of previously loaded package names. Skip
19443 * "autoload/", it's always the same. */
19444 for (i = 0; i < ga_loaded.ga_len; ++i)
19445 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
19446 break;
19447 if (!reload && i < ga_loaded.ga_len)
19448 ret = FALSE; /* was loaded already */
19449 else
19450 {
19451 /* Remember the name if it wasn't loaded already. */
19452 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
19453 {
19454 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
19455 tofree = NULL;
19456 }
19457
19458 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000019459 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019460 ret = TRUE;
19461 }
19462
19463 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019464 return ret;
19465}
19466
19467/*
19468 * Return the autoload script name for a function or variable name.
19469 * Returns NULL when out of memory.
19470 */
19471 static char_u *
19472autoload_name(name)
19473 char_u *name;
19474{
19475 char_u *p;
19476 char_u *scriptname;
19477
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019478 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019479 scriptname = alloc((unsigned)(STRLEN(name) + 14));
19480 if (scriptname == NULL)
19481 return FALSE;
19482 STRCPY(scriptname, "autoload/");
19483 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019484 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019485 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019486 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019487 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019488 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019489}
19490
Bram Moolenaar071d4272004-06-13 20:20:40 +000019491#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
19492
19493/*
19494 * Function given to ExpandGeneric() to obtain the list of user defined
19495 * function names.
19496 */
19497 char_u *
19498get_user_func_name(xp, idx)
19499 expand_T *xp;
19500 int idx;
19501{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019502 static long_u done;
19503 static hashitem_T *hi;
19504 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019505
19506 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019507 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019508 done = 0;
19509 hi = func_hashtab.ht_array;
19510 }
19511 if (done < func_hashtab.ht_used)
19512 {
19513 if (done++ > 0)
19514 ++hi;
19515 while (HASHITEM_EMPTY(hi))
19516 ++hi;
19517 fp = HI2UF(hi);
19518
19519 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
19520 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019521
19522 cat_func_name(IObuff, fp);
19523 if (xp->xp_context != EXPAND_USER_FUNC)
19524 {
19525 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019526 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019527 STRCAT(IObuff, ")");
19528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019529 return IObuff;
19530 }
19531 return NULL;
19532}
19533
19534#endif /* FEAT_CMDL_COMPL */
19535
19536/*
19537 * Copy the function name of "fp" to buffer "buf".
19538 * "buf" must be able to hold the function name plus three bytes.
19539 * Takes care of script-local function names.
19540 */
19541 static void
19542cat_func_name(buf, fp)
19543 char_u *buf;
19544 ufunc_T *fp;
19545{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019546 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019547 {
19548 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019549 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019550 }
19551 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019552 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019553}
19554
19555/*
19556 * ":delfunction {name}"
19557 */
19558 void
19559ex_delfunction(eap)
19560 exarg_T *eap;
19561{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019562 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019563 char_u *p;
19564 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019565 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019566
19567 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019568 name = trans_function_name(&p, eap->skip, 0, &fudi);
19569 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019570 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019571 {
19572 if (fudi.fd_dict != NULL && !eap->skip)
19573 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019574 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019576 if (!ends_excmd(*skipwhite(p)))
19577 {
19578 vim_free(name);
19579 EMSG(_(e_trailing));
19580 return;
19581 }
19582 eap->nextcmd = check_nextcmd(p);
19583 if (eap->nextcmd != NULL)
19584 *p = NUL;
19585
19586 if (!eap->skip)
19587 fp = find_func(name);
19588 vim_free(name);
19589
19590 if (!eap->skip)
19591 {
19592 if (fp == NULL)
19593 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019594 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019595 return;
19596 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019597 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019598 {
19599 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
19600 return;
19601 }
19602
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019603 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019604 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019605 /* Delete the dict item that refers to the function, it will
19606 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019607 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019608 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019609 else
19610 func_free(fp);
19611 }
19612}
19613
19614/*
19615 * Free a function and remove it from the list of functions.
19616 */
19617 static void
19618func_free(fp)
19619 ufunc_T *fp;
19620{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019621 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019622
19623 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019624 ga_clear_strings(&(fp->uf_args));
19625 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000019626#ifdef FEAT_PROFILE
19627 vim_free(fp->uf_tml_count);
19628 vim_free(fp->uf_tml_total);
19629 vim_free(fp->uf_tml_self);
19630#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019631
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019632 /* remove the function from the function hashtable */
19633 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
19634 if (HASHITEM_EMPTY(hi))
19635 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019636 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019637 hash_remove(&func_hashtab, hi);
19638
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019639 vim_free(fp);
19640}
19641
19642/*
19643 * Unreference a Function: decrement the reference count and free it when it
19644 * becomes zero. Only for numbered functions.
19645 */
19646 static void
19647func_unref(name)
19648 char_u *name;
19649{
19650 ufunc_T *fp;
19651
19652 if (name != NULL && isdigit(*name))
19653 {
19654 fp = find_func(name);
19655 if (fp == NULL)
19656 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019657 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019658 {
19659 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019660 * when "uf_calls" becomes zero. */
19661 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019662 func_free(fp);
19663 }
19664 }
19665}
19666
19667/*
19668 * Count a reference to a Function.
19669 */
19670 static void
19671func_ref(name)
19672 char_u *name;
19673{
19674 ufunc_T *fp;
19675
19676 if (name != NULL && isdigit(*name))
19677 {
19678 fp = find_func(name);
19679 if (fp == NULL)
19680 EMSG2(_(e_intern2), "func_ref()");
19681 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019682 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019683 }
19684}
19685
19686/*
19687 * Call a user function.
19688 */
19689 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000019690call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019691 ufunc_T *fp; /* pointer to function */
19692 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000019693 typval_T *argvars; /* arguments */
19694 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019695 linenr_T firstline; /* first line of range */
19696 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000019697 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019698{
Bram Moolenaar33570922005-01-25 22:26:29 +000019699 char_u *save_sourcing_name;
19700 linenr_T save_sourcing_lnum;
19701 scid_T save_current_SID;
19702 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000019703 int save_did_emsg;
19704 static int depth = 0;
19705 dictitem_T *v;
19706 int fixvar_idx = 0; /* index in fixvar[] */
19707 int i;
19708 int ai;
19709 char_u numbuf[NUMBUFLEN];
19710 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019711#ifdef FEAT_PROFILE
19712 proftime_T wait_start;
19713#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019714
19715 /* If depth of calling is getting too high, don't execute the function */
19716 if (depth >= p_mfd)
19717 {
19718 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019719 rettv->v_type = VAR_NUMBER;
19720 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019721 return;
19722 }
19723 ++depth;
19724
19725 line_breakcheck(); /* check for CTRL-C hit */
19726
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019727 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000019728 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019729 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019730 fc.rettv = rettv;
19731 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019732 fc.linenr = 0;
19733 fc.returned = FALSE;
19734 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019735 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019736 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019737 fc.dbg_tick = debug_tick;
19738
Bram Moolenaar33570922005-01-25 22:26:29 +000019739 /*
19740 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
19741 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
19742 * each argument variable and saves a lot of time.
19743 */
19744 /*
19745 * Init l: variables.
19746 */
19747 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000019748 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019749 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000019750 /* Set l:self to "selfdict". Use "name" to avoid a warning from
19751 * some compiler that checks the destination size. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019752 v = &fc.fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000019753 name = v->di_key;
19754 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000019755 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
19756 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
19757 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019758 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000019759 v->di_tv.vval.v_dict = selfdict;
19760 ++selfdict->dv_refcount;
19761 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000019762
Bram Moolenaar33570922005-01-25 22:26:29 +000019763 /*
19764 * Init a: variables.
19765 * Set a:0 to "argcount".
19766 * Set a:000 to a list with room for the "..." arguments.
19767 */
19768 init_var_dict(&fc.l_avars, &fc.l_avars_var);
19769 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019770 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000019771 v = &fc.fixvar[fixvar_idx++].var;
19772 STRCPY(v->di_key, "000");
19773 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19774 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
19775 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019776 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019777 v->di_tv.vval.v_list = &fc.l_varlist;
19778 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
19779 fc.l_varlist.lv_refcount = 99999;
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000019780 fc.l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019781
19782 /*
19783 * Set a:firstline to "firstline" and a:lastline to "lastline".
19784 * Set a:name to named arguments.
19785 * Set a:N to the "..." arguments.
19786 */
19787 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
19788 (varnumber_T)firstline);
19789 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
19790 (varnumber_T)lastline);
19791 for (i = 0; i < argcount; ++i)
19792 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019793 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000019794 if (ai < 0)
19795 /* named argument a:name */
19796 name = FUNCARG(fp, i);
19797 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019798 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019799 /* "..." argument a:1, a:2, etc. */
19800 sprintf((char *)numbuf, "%d", ai + 1);
19801 name = numbuf;
19802 }
19803 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
19804 {
19805 v = &fc.fixvar[fixvar_idx++].var;
19806 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19807 }
19808 else
19809 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019810 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19811 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000019812 if (v == NULL)
19813 break;
19814 v->di_flags = DI_FLAGS_RO;
19815 }
19816 STRCPY(v->di_key, name);
19817 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
19818
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019819 /* Note: the values are copied directly to avoid alloc/free.
19820 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019821 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019822 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019823
19824 if (ai >= 0 && ai < MAX_FUNC_ARGS)
19825 {
19826 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
19827 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019828 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019829 }
19830 }
19831
Bram Moolenaar071d4272004-06-13 20:20:40 +000019832 /* Don't redraw while executing the function. */
19833 ++RedrawingDisabled;
19834 save_sourcing_name = sourcing_name;
19835 save_sourcing_lnum = sourcing_lnum;
19836 sourcing_lnum = 1;
19837 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019838 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019839 if (sourcing_name != NULL)
19840 {
19841 if (save_sourcing_name != NULL
19842 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
19843 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
19844 else
19845 STRCPY(sourcing_name, "function ");
19846 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
19847
19848 if (p_verbose >= 12)
19849 {
19850 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019851 verbose_enter_scroll();
19852
Bram Moolenaar555b2802005-05-19 21:08:39 +000019853 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019854 if (p_verbose >= 14)
19855 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019856 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000019857 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000019858 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019859
19860 msg_puts((char_u *)"(");
19861 for (i = 0; i < argcount; ++i)
19862 {
19863 if (i > 0)
19864 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019865 if (argvars[i].v_type == VAR_NUMBER)
19866 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019867 else
19868 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000019869 trunc_string(tv2string(&argvars[i], &tofree,
19870 numbuf2, 0), buf, MSG_BUF_CLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019871 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019872 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019873 }
19874 }
19875 msg_puts((char_u *)")");
19876 }
19877 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019878
19879 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019880 --no_wait_return;
19881 }
19882 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019883#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000019884 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019885 {
19886 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
19887 func_do_profile(fp);
19888 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019889 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000019890 {
19891 ++fp->uf_tm_count;
19892 profile_start(&fp->uf_tm_start);
19893 profile_zero(&fp->uf_tm_children);
19894 }
19895 script_prof_save(&wait_start);
19896 }
19897#endif
19898
Bram Moolenaar071d4272004-06-13 20:20:40 +000019899 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019900 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019901 save_did_emsg = did_emsg;
19902 did_emsg = FALSE;
19903
19904 /* call do_cmdline() to execute the lines */
19905 do_cmdline(NULL, get_func_line, (void *)&fc,
19906 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
19907
19908 --RedrawingDisabled;
19909
19910 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019911 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019912 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019913 clear_tv(rettv);
19914 rettv->v_type = VAR_NUMBER;
19915 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019916 }
19917
Bram Moolenaar05159a02005-02-26 23:04:13 +000019918#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000019919 if (do_profiling == PROF_YES && (fp->uf_profiling
19920 || (fc.caller != NULL && &fc.caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000019921 {
19922 profile_end(&fp->uf_tm_start);
19923 profile_sub_wait(&wait_start, &fp->uf_tm_start);
19924 profile_add(&fp->uf_tm_total, &fp->uf_tm_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000019925 profile_self(&fp->uf_tm_self, &fp->uf_tm_start, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019926 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019927 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019928 profile_add(&fc.caller->func->uf_tm_children, &fp->uf_tm_start);
19929 profile_add(&fc.caller->func->uf_tml_children, &fp->uf_tm_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019930 }
19931 }
19932#endif
19933
Bram Moolenaar071d4272004-06-13 20:20:40 +000019934 /* when being verbose, mention the return value */
19935 if (p_verbose >= 12)
19936 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019937 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019938 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019939
Bram Moolenaar071d4272004-06-13 20:20:40 +000019940 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000019941 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019942 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000019943 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
19944 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019945 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000019946 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000019947 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000019948 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000019949 char_u *tofree;
19950
Bram Moolenaar555b2802005-05-19 21:08:39 +000019951 /* The value may be very long. Skip the middle part, so that we
19952 * have some idea how it starts and ends. smsg() would always
19953 * truncate it at the end. */
Bram Moolenaar89d40322006-08-29 15:30:07 +000019954 trunc_string(tv2string(fc.rettv, &tofree, numbuf2, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019955 buf, MSG_BUF_CLEN);
Bram Moolenaar555b2802005-05-19 21:08:39 +000019956 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019957 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019958 }
19959 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019960
19961 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019962 --no_wait_return;
19963 }
19964
19965 vim_free(sourcing_name);
19966 sourcing_name = save_sourcing_name;
19967 sourcing_lnum = save_sourcing_lnum;
19968 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019969#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000019970 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019971 script_prof_restore(&wait_start);
19972#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019973
19974 if (p_verbose >= 12 && sourcing_name != NULL)
19975 {
19976 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019977 verbose_enter_scroll();
19978
Bram Moolenaar555b2802005-05-19 21:08:39 +000019979 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019980 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019981
19982 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019983 --no_wait_return;
19984 }
19985
19986 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019987 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019988
Bram Moolenaar33570922005-01-25 22:26:29 +000019989 /* The a: variables typevals were not alloced, only free the allocated
19990 * variables. */
19991 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
19992
19993 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019994 --depth;
19995}
19996
19997/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019998 * Add a number variable "name" to dict "dp" with value "nr".
19999 */
20000 static void
20001add_nr_var(dp, v, name, nr)
20002 dict_T *dp;
20003 dictitem_T *v;
20004 char *name;
20005 varnumber_T nr;
20006{
20007 STRCPY(v->di_key, name);
20008 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20009 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
20010 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020011 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020012 v->di_tv.vval.v_number = nr;
20013}
20014
20015/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020016 * ":return [expr]"
20017 */
20018 void
20019ex_return(eap)
20020 exarg_T *eap;
20021{
20022 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020023 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020024 int returning = FALSE;
20025
20026 if (current_funccal == NULL)
20027 {
20028 EMSG(_("E133: :return not inside a function"));
20029 return;
20030 }
20031
20032 if (eap->skip)
20033 ++emsg_skip;
20034
20035 eap->nextcmd = NULL;
20036 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020037 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020038 {
20039 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020040 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020041 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020042 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020043 }
20044 /* It's safer to return also on error. */
20045 else if (!eap->skip)
20046 {
20047 /*
20048 * Return unless the expression evaluation has been cancelled due to an
20049 * aborting error, an interrupt, or an exception.
20050 */
20051 if (!aborting())
20052 returning = do_return(eap, FALSE, TRUE, NULL);
20053 }
20054
20055 /* When skipping or the return gets pending, advance to the next command
20056 * in this line (!returning). Otherwise, ignore the rest of the line.
20057 * Following lines will be ignored by get_func_line(). */
20058 if (returning)
20059 eap->nextcmd = NULL;
20060 else if (eap->nextcmd == NULL) /* no argument */
20061 eap->nextcmd = check_nextcmd(arg);
20062
20063 if (eap->skip)
20064 --emsg_skip;
20065}
20066
20067/*
20068 * Return from a function. Possibly makes the return pending. Also called
20069 * for a pending return at the ":endtry" or after returning from an extra
20070 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000020071 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020072 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020073 * FALSE when the return gets pending.
20074 */
20075 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020076do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020077 exarg_T *eap;
20078 int reanimate;
20079 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020080 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020081{
20082 int idx;
20083 struct condstack *cstack = eap->cstack;
20084
20085 if (reanimate)
20086 /* Undo the return. */
20087 current_funccal->returned = FALSE;
20088
20089 /*
20090 * Cleanup (and inactivate) conditionals, but stop when a try conditional
20091 * not in its finally clause (which then is to be executed next) is found.
20092 * In this case, make the ":return" pending for execution at the ":endtry".
20093 * Otherwise, return normally.
20094 */
20095 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
20096 if (idx >= 0)
20097 {
20098 cstack->cs_pending[idx] = CSTP_RETURN;
20099
20100 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020101 /* A pending return again gets pending. "rettv" points to an
20102 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000020103 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020104 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020105 else
20106 {
20107 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020108 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020109 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020110 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020111
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020112 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020113 {
20114 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020115 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020116 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020117 else
20118 EMSG(_(e_outofmem));
20119 }
20120 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020121 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020122
20123 if (reanimate)
20124 {
20125 /* The pending return value could be overwritten by a ":return"
20126 * without argument in a finally clause; reset the default
20127 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020128 current_funccal->rettv->v_type = VAR_NUMBER;
20129 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020130 }
20131 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020132 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020133 }
20134 else
20135 {
20136 current_funccal->returned = TRUE;
20137
20138 /* If the return is carried out now, store the return value. For
20139 * a return immediately after reanimation, the value is already
20140 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020141 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020142 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020143 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000020144 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020145 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020146 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020147 }
20148 }
20149
20150 return idx < 0;
20151}
20152
20153/*
20154 * Free the variable with a pending return value.
20155 */
20156 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020157discard_pending_return(rettv)
20158 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020159{
Bram Moolenaar33570922005-01-25 22:26:29 +000020160 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020161}
20162
20163/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020164 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000020165 * is an allocated string. Used by report_pending() for verbose messages.
20166 */
20167 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020168get_return_cmd(rettv)
20169 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020170{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020171 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020172 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020173 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020174
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020175 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000020176 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020177 if (s == NULL)
20178 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020179
20180 STRCPY(IObuff, ":return ");
20181 STRNCPY(IObuff + 8, s, IOSIZE - 8);
20182 if (STRLEN(s) + 8 >= IOSIZE)
20183 STRCPY(IObuff + IOSIZE - 4, "...");
20184 vim_free(tofree);
20185 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020186}
20187
20188/*
20189 * Get next function line.
20190 * Called by do_cmdline() to get the next line.
20191 * Returns allocated string, or NULL for end of function.
20192 */
20193/* ARGSUSED */
20194 char_u *
20195get_func_line(c, cookie, indent)
20196 int c; /* not used */
20197 void *cookie;
20198 int indent; /* not used */
20199{
Bram Moolenaar33570922005-01-25 22:26:29 +000020200 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020201 ufunc_T *fp = fcp->func;
20202 char_u *retval;
20203 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020204
20205 /* If breakpoints have been added/deleted need to check for it. */
20206 if (fcp->dbg_tick != debug_tick)
20207 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020208 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020209 sourcing_lnum);
20210 fcp->dbg_tick = debug_tick;
20211 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000020212#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020213 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020214 func_line_end(cookie);
20215#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020216
Bram Moolenaar05159a02005-02-26 23:04:13 +000020217 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020218 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
20219 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020220 retval = NULL;
20221 else
20222 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020223 /* Skip NULL lines (continuation lines). */
20224 while (fcp->linenr < gap->ga_len
20225 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
20226 ++fcp->linenr;
20227 if (fcp->linenr >= gap->ga_len)
20228 retval = NULL;
20229 else
20230 {
20231 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
20232 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020233#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020234 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020235 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020236#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020238 }
20239
20240 /* Did we encounter a breakpoint? */
20241 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
20242 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020243 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020244 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020245 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020246 sourcing_lnum);
20247 fcp->dbg_tick = debug_tick;
20248 }
20249
20250 return retval;
20251}
20252
Bram Moolenaar05159a02005-02-26 23:04:13 +000020253#if defined(FEAT_PROFILE) || defined(PROTO)
20254/*
20255 * Called when starting to read a function line.
20256 * "sourcing_lnum" must be correct!
20257 * When skipping lines it may not actually be executed, but we won't find out
20258 * until later and we need to store the time now.
20259 */
20260 void
20261func_line_start(cookie)
20262 void *cookie;
20263{
20264 funccall_T *fcp = (funccall_T *)cookie;
20265 ufunc_T *fp = fcp->func;
20266
20267 if (fp->uf_profiling && sourcing_lnum >= 1
20268 && sourcing_lnum <= fp->uf_lines.ga_len)
20269 {
20270 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020271 /* Skip continuation lines. */
20272 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
20273 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020274 fp->uf_tml_execed = FALSE;
20275 profile_start(&fp->uf_tml_start);
20276 profile_zero(&fp->uf_tml_children);
20277 profile_get_wait(&fp->uf_tml_wait);
20278 }
20279}
20280
20281/*
20282 * Called when actually executing a function line.
20283 */
20284 void
20285func_line_exec(cookie)
20286 void *cookie;
20287{
20288 funccall_T *fcp = (funccall_T *)cookie;
20289 ufunc_T *fp = fcp->func;
20290
20291 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
20292 fp->uf_tml_execed = TRUE;
20293}
20294
20295/*
20296 * Called when done with a function line.
20297 */
20298 void
20299func_line_end(cookie)
20300 void *cookie;
20301{
20302 funccall_T *fcp = (funccall_T *)cookie;
20303 ufunc_T *fp = fcp->func;
20304
20305 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
20306 {
20307 if (fp->uf_tml_execed)
20308 {
20309 ++fp->uf_tml_count[fp->uf_tml_idx];
20310 profile_end(&fp->uf_tml_start);
20311 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020312 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000020313 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
20314 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020315 }
20316 fp->uf_tml_idx = -1;
20317 }
20318}
20319#endif
20320
Bram Moolenaar071d4272004-06-13 20:20:40 +000020321/*
20322 * Return TRUE if the currently active function should be ended, because a
20323 * return was encountered or an error occured. Used inside a ":while".
20324 */
20325 int
20326func_has_ended(cookie)
20327 void *cookie;
20328{
Bram Moolenaar33570922005-01-25 22:26:29 +000020329 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020330
20331 /* Ignore the "abort" flag if the abortion behavior has been changed due to
20332 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020333 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000020334 || fcp->returned);
20335}
20336
20337/*
20338 * return TRUE if cookie indicates a function which "abort"s on errors.
20339 */
20340 int
20341func_has_abort(cookie)
20342 void *cookie;
20343{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020344 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020345}
20346
20347#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
20348typedef enum
20349{
20350 VAR_FLAVOUR_DEFAULT,
20351 VAR_FLAVOUR_SESSION,
20352 VAR_FLAVOUR_VIMINFO
20353} var_flavour_T;
20354
20355static var_flavour_T var_flavour __ARGS((char_u *varname));
20356
20357 static var_flavour_T
20358var_flavour(varname)
20359 char_u *varname;
20360{
20361 char_u *p = varname;
20362
20363 if (ASCII_ISUPPER(*p))
20364 {
20365 while (*(++p))
20366 if (ASCII_ISLOWER(*p))
20367 return VAR_FLAVOUR_SESSION;
20368 return VAR_FLAVOUR_VIMINFO;
20369 }
20370 else
20371 return VAR_FLAVOUR_DEFAULT;
20372}
20373#endif
20374
20375#if defined(FEAT_VIMINFO) || defined(PROTO)
20376/*
20377 * Restore global vars that start with a capital from the viminfo file
20378 */
20379 int
20380read_viminfo_varlist(virp, writing)
20381 vir_T *virp;
20382 int writing;
20383{
20384 char_u *tab;
20385 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000020386 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020387
20388 if (!writing && (find_viminfo_parameter('!') != NULL))
20389 {
20390 tab = vim_strchr(virp->vir_line + 1, '\t');
20391 if (tab != NULL)
20392 {
20393 *tab++ = '\0'; /* isolate the variable name */
20394 if (*tab == 'S') /* string var */
20395 is_string = TRUE;
20396
20397 tab = vim_strchr(tab, '\t');
20398 if (tab != NULL)
20399 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020400 if (is_string)
20401 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020402 tv.v_type = VAR_STRING;
20403 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020404 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020405 }
20406 else
20407 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020408 tv.v_type = VAR_NUMBER;
20409 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020410 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020411 set_var(virp->vir_line + 1, &tv, FALSE);
20412 if (is_string)
20413 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020414 }
20415 }
20416 }
20417
20418 return viminfo_readline(virp);
20419}
20420
20421/*
20422 * Write global vars that start with a capital to the viminfo file
20423 */
20424 void
20425write_viminfo_varlist(fp)
20426 FILE *fp;
20427{
Bram Moolenaar33570922005-01-25 22:26:29 +000020428 hashitem_T *hi;
20429 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020430 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020431 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020432 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020433 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020434 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020435
20436 if (find_viminfo_parameter('!') == NULL)
20437 return;
20438
20439 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000020440
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020441 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000020442 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020443 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020444 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020445 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020446 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020447 this_var = HI2DI(hi);
20448 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020449 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020450 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000020451 {
20452 case VAR_STRING: s = "STR"; break;
20453 case VAR_NUMBER: s = "NUM"; break;
20454 default: continue;
20455 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020456 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000020457 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020458 if (p != NULL)
20459 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000020460 vim_free(tofree);
20461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020462 }
20463 }
20464}
20465#endif
20466
20467#if defined(FEAT_SESSION) || defined(PROTO)
20468 int
20469store_session_globals(fd)
20470 FILE *fd;
20471{
Bram Moolenaar33570922005-01-25 22:26:29 +000020472 hashitem_T *hi;
20473 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020474 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020475 char_u *p, *t;
20476
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020477 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000020478 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020479 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020480 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020481 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020482 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020483 this_var = HI2DI(hi);
20484 if ((this_var->di_tv.v_type == VAR_NUMBER
20485 || this_var->di_tv.v_type == VAR_STRING)
20486 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020487 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020488 /* Escape special characters with a backslash. Turn a LF and
20489 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020490 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000020491 (char_u *)"\\\"\n\r");
20492 if (p == NULL) /* out of memory */
20493 break;
20494 for (t = p; *t != NUL; ++t)
20495 if (*t == '\n')
20496 *t = 'n';
20497 else if (*t == '\r')
20498 *t = 'r';
20499 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000020500 this_var->di_key,
20501 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20502 : ' ',
20503 p,
20504 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20505 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000020506 || put_eol(fd) == FAIL)
20507 {
20508 vim_free(p);
20509 return FAIL;
20510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020511 vim_free(p);
20512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020513 }
20514 }
20515 return OK;
20516}
20517#endif
20518
Bram Moolenaar661b1822005-07-28 22:36:45 +000020519/*
20520 * Display script name where an item was last set.
20521 * Should only be invoked when 'verbose' is non-zero.
20522 */
20523 void
20524last_set_msg(scriptID)
20525 scid_T scriptID;
20526{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020527 char_u *p;
20528
Bram Moolenaar661b1822005-07-28 22:36:45 +000020529 if (scriptID != 0)
20530 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020531 p = home_replace_save(NULL, get_scriptname(scriptID));
20532 if (p != NULL)
20533 {
20534 verbose_enter();
20535 MSG_PUTS(_("\n\tLast set from "));
20536 MSG_PUTS(p);
20537 vim_free(p);
20538 verbose_leave();
20539 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000020540 }
20541}
20542
Bram Moolenaar071d4272004-06-13 20:20:40 +000020543#endif /* FEAT_EVAL */
20544
20545#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
20546
20547
20548#ifdef WIN3264
20549/*
20550 * Functions for ":8" filename modifier: get 8.3 version of a filename.
20551 */
20552static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
20553static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
20554static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
20555
20556/*
20557 * Get the short pathname of a file.
20558 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
20559 */
20560 static int
20561get_short_pathname(fnamep, bufp, fnamelen)
20562 char_u **fnamep;
20563 char_u **bufp;
20564 int *fnamelen;
20565{
20566 int l,len;
20567 char_u *newbuf;
20568
20569 len = *fnamelen;
20570
20571 l = GetShortPathName(*fnamep, *fnamep, len);
20572 if (l > len - 1)
20573 {
20574 /* If that doesn't work (not enough space), then save the string
20575 * and try again with a new buffer big enough
20576 */
20577 newbuf = vim_strnsave(*fnamep, l);
20578 if (newbuf == NULL)
20579 return 0;
20580
20581 vim_free(*bufp);
20582 *fnamep = *bufp = newbuf;
20583
20584 l = GetShortPathName(*fnamep,*fnamep,l+1);
20585
20586 /* Really should always succeed, as the buffer is big enough */
20587 }
20588
20589 *fnamelen = l;
20590 return 1;
20591}
20592
20593/*
20594 * Create a short path name. Returns the length of the buffer it needs.
20595 * Doesn't copy over the end of the buffer passed in.
20596 */
20597 static int
20598shortpath_for_invalid_fname(fname, bufp, fnamelen)
20599 char_u **fname;
20600 char_u **bufp;
20601 int *fnamelen;
20602{
20603 char_u *s, *p, *pbuf2, *pbuf3;
20604 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020605 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020606
20607 /* Make a copy */
20608 len2 = *fnamelen;
20609 pbuf2 = vim_strnsave(*fname, len2);
20610 pbuf3 = NULL;
20611
20612 s = pbuf2 + len2 - 1; /* Find the end */
20613 slen = 1;
20614 plen = len2;
20615
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020616 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020617 {
20618 --s;
20619 ++slen;
20620 --plen;
20621 }
20622
20623 do
20624 {
20625 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020626 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020627 {
20628 --s;
20629 ++slen;
20630 --plen;
20631 }
20632 if (s <= pbuf2)
20633 break;
20634
20635 /* Remeber the character that is about to be blatted */
20636 ch = *s;
20637 *s = 0; /* get_short_pathname requires a null-terminated string */
20638
20639 /* Try it in situ */
20640 p = pbuf2;
20641 if (!get_short_pathname(&p, &pbuf3, &plen))
20642 {
20643 vim_free(pbuf2);
20644 return -1;
20645 }
20646 *s = ch; /* Preserve the string */
20647 } while (plen == 0);
20648
20649 if (plen > 0)
20650 {
20651 /* Remeber the length of the new string. */
20652 *fnamelen = len = plen + slen;
20653 vim_free(*bufp);
20654 if (len > len2)
20655 {
20656 /* If there's not enough space in the currently allocated string,
20657 * then copy it to a buffer big enough.
20658 */
20659 *fname= *bufp = vim_strnsave(p, len);
20660 if (*fname == NULL)
20661 return -1;
20662 }
20663 else
20664 {
20665 /* Transfer pbuf2 to being the main buffer (it's big enough) */
20666 *fname = *bufp = pbuf2;
20667 if (p != pbuf2)
20668 strncpy(*fname, p, plen);
20669 pbuf2 = NULL;
20670 }
20671 /* Concat the next bit */
20672 strncpy(*fname + plen, s, slen);
20673 (*fname)[len] = '\0';
20674 }
20675 vim_free(pbuf3);
20676 vim_free(pbuf2);
20677 return 0;
20678}
20679
20680/*
20681 * Get a pathname for a partial path.
20682 */
20683 static int
20684shortpath_for_partial(fnamep, bufp, fnamelen)
20685 char_u **fnamep;
20686 char_u **bufp;
20687 int *fnamelen;
20688{
20689 int sepcount, len, tflen;
20690 char_u *p;
20691 char_u *pbuf, *tfname;
20692 int hasTilde;
20693
20694 /* Count up the path seperators from the RHS.. so we know which part
20695 * of the path to return.
20696 */
20697 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020698 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020699 if (vim_ispathsep(*p))
20700 ++sepcount;
20701
20702 /* Need full path first (use expand_env() to remove a "~/") */
20703 hasTilde = (**fnamep == '~');
20704 if (hasTilde)
20705 pbuf = tfname = expand_env_save(*fnamep);
20706 else
20707 pbuf = tfname = FullName_save(*fnamep, FALSE);
20708
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020709 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020710
20711 if (!get_short_pathname(&tfname, &pbuf, &len))
20712 return -1;
20713
20714 if (len == 0)
20715 {
20716 /* Don't have a valid filename, so shorten the rest of the
20717 * path if we can. This CAN give us invalid 8.3 filenames, but
20718 * there's not a lot of point in guessing what it might be.
20719 */
20720 len = tflen;
20721 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
20722 return -1;
20723 }
20724
20725 /* Count the paths backward to find the beginning of the desired string. */
20726 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020727 {
20728#ifdef FEAT_MBYTE
20729 if (has_mbyte)
20730 p -= mb_head_off(tfname, p);
20731#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020732 if (vim_ispathsep(*p))
20733 {
20734 if (sepcount == 0 || (hasTilde && sepcount == 1))
20735 break;
20736 else
20737 sepcount --;
20738 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020740 if (hasTilde)
20741 {
20742 --p;
20743 if (p >= tfname)
20744 *p = '~';
20745 else
20746 return -1;
20747 }
20748 else
20749 ++p;
20750
20751 /* Copy in the string - p indexes into tfname - allocated at pbuf */
20752 vim_free(*bufp);
20753 *fnamelen = (int)STRLEN(p);
20754 *bufp = pbuf;
20755 *fnamep = p;
20756
20757 return 0;
20758}
20759#endif /* WIN3264 */
20760
20761/*
20762 * Adjust a filename, according to a string of modifiers.
20763 * *fnamep must be NUL terminated when called. When returning, the length is
20764 * determined by *fnamelen.
20765 * Returns valid flags.
20766 * When there is an error, *fnamep is set to NULL.
20767 */
20768 int
20769modify_fname(src, usedlen, fnamep, bufp, fnamelen)
20770 char_u *src; /* string with modifiers */
20771 int *usedlen; /* characters after src that are used */
20772 char_u **fnamep; /* file name so far */
20773 char_u **bufp; /* buffer for allocated file name or NULL */
20774 int *fnamelen; /* length of fnamep */
20775{
20776 int valid = 0;
20777 char_u *tail;
20778 char_u *s, *p, *pbuf;
20779 char_u dirname[MAXPATHL];
20780 int c;
20781 int has_fullname = 0;
20782#ifdef WIN3264
20783 int has_shortname = 0;
20784#endif
20785
20786repeat:
20787 /* ":p" - full path/file_name */
20788 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
20789 {
20790 has_fullname = 1;
20791
20792 valid |= VALID_PATH;
20793 *usedlen += 2;
20794
20795 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
20796 if ((*fnamep)[0] == '~'
20797#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
20798 && ((*fnamep)[1] == '/'
20799# ifdef BACKSLASH_IN_FILENAME
20800 || (*fnamep)[1] == '\\'
20801# endif
20802 || (*fnamep)[1] == NUL)
20803
20804#endif
20805 )
20806 {
20807 *fnamep = expand_env_save(*fnamep);
20808 vim_free(*bufp); /* free any allocated file name */
20809 *bufp = *fnamep;
20810 if (*fnamep == NULL)
20811 return -1;
20812 }
20813
20814 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020815 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020816 {
20817 if (vim_ispathsep(*p)
20818 && p[1] == '.'
20819 && (p[2] == NUL
20820 || vim_ispathsep(p[2])
20821 || (p[2] == '.'
20822 && (p[3] == NUL || vim_ispathsep(p[3])))))
20823 break;
20824 }
20825
20826 /* FullName_save() is slow, don't use it when not needed. */
20827 if (*p != NUL || !vim_isAbsName(*fnamep))
20828 {
20829 *fnamep = FullName_save(*fnamep, *p != NUL);
20830 vim_free(*bufp); /* free any allocated file name */
20831 *bufp = *fnamep;
20832 if (*fnamep == NULL)
20833 return -1;
20834 }
20835
20836 /* Append a path separator to a directory. */
20837 if (mch_isdir(*fnamep))
20838 {
20839 /* Make room for one or two extra characters. */
20840 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
20841 vim_free(*bufp); /* free any allocated file name */
20842 *bufp = *fnamep;
20843 if (*fnamep == NULL)
20844 return -1;
20845 add_pathsep(*fnamep);
20846 }
20847 }
20848
20849 /* ":." - path relative to the current directory */
20850 /* ":~" - path relative to the home directory */
20851 /* ":8" - shortname path - postponed till after */
20852 while (src[*usedlen] == ':'
20853 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
20854 {
20855 *usedlen += 2;
20856 if (c == '8')
20857 {
20858#ifdef WIN3264
20859 has_shortname = 1; /* Postpone this. */
20860#endif
20861 continue;
20862 }
20863 pbuf = NULL;
20864 /* Need full path first (use expand_env() to remove a "~/") */
20865 if (!has_fullname)
20866 {
20867 if (c == '.' && **fnamep == '~')
20868 p = pbuf = expand_env_save(*fnamep);
20869 else
20870 p = pbuf = FullName_save(*fnamep, FALSE);
20871 }
20872 else
20873 p = *fnamep;
20874
20875 has_fullname = 0;
20876
20877 if (p != NULL)
20878 {
20879 if (c == '.')
20880 {
20881 mch_dirname(dirname, MAXPATHL);
20882 s = shorten_fname(p, dirname);
20883 if (s != NULL)
20884 {
20885 *fnamep = s;
20886 if (pbuf != NULL)
20887 {
20888 vim_free(*bufp); /* free any allocated file name */
20889 *bufp = pbuf;
20890 pbuf = NULL;
20891 }
20892 }
20893 }
20894 else
20895 {
20896 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
20897 /* Only replace it when it starts with '~' */
20898 if (*dirname == '~')
20899 {
20900 s = vim_strsave(dirname);
20901 if (s != NULL)
20902 {
20903 *fnamep = s;
20904 vim_free(*bufp);
20905 *bufp = s;
20906 }
20907 }
20908 }
20909 vim_free(pbuf);
20910 }
20911 }
20912
20913 tail = gettail(*fnamep);
20914 *fnamelen = (int)STRLEN(*fnamep);
20915
20916 /* ":h" - head, remove "/file_name", can be repeated */
20917 /* Don't remove the first "/" or "c:\" */
20918 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
20919 {
20920 valid |= VALID_HEAD;
20921 *usedlen += 2;
20922 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020923 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020924 --tail;
20925 *fnamelen = (int)(tail - *fnamep);
20926#ifdef VMS
20927 if (*fnamelen > 0)
20928 *fnamelen += 1; /* the path separator is part of the path */
20929#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020930 while (tail > s && !after_pathsep(s, tail))
20931 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020932 }
20933
20934 /* ":8" - shortname */
20935 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
20936 {
20937 *usedlen += 2;
20938#ifdef WIN3264
20939 has_shortname = 1;
20940#endif
20941 }
20942
20943#ifdef WIN3264
20944 /* Check shortname after we have done 'heads' and before we do 'tails'
20945 */
20946 if (has_shortname)
20947 {
20948 pbuf = NULL;
20949 /* Copy the string if it is shortened by :h */
20950 if (*fnamelen < (int)STRLEN(*fnamep))
20951 {
20952 p = vim_strnsave(*fnamep, *fnamelen);
20953 if (p == 0)
20954 return -1;
20955 vim_free(*bufp);
20956 *bufp = *fnamep = p;
20957 }
20958
20959 /* Split into two implementations - makes it easier. First is where
20960 * there isn't a full name already, second is where there is.
20961 */
20962 if (!has_fullname && !vim_isAbsName(*fnamep))
20963 {
20964 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
20965 return -1;
20966 }
20967 else
20968 {
20969 int l;
20970
20971 /* Simple case, already have the full-name
20972 * Nearly always shorter, so try first time. */
20973 l = *fnamelen;
20974 if (!get_short_pathname(fnamep, bufp, &l))
20975 return -1;
20976
20977 if (l == 0)
20978 {
20979 /* Couldn't find the filename.. search the paths.
20980 */
20981 l = *fnamelen;
20982 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
20983 return -1;
20984 }
20985 *fnamelen = l;
20986 }
20987 }
20988#endif /* WIN3264 */
20989
20990 /* ":t" - tail, just the basename */
20991 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
20992 {
20993 *usedlen += 2;
20994 *fnamelen -= (int)(tail - *fnamep);
20995 *fnamep = tail;
20996 }
20997
20998 /* ":e" - extension, can be repeated */
20999 /* ":r" - root, without extension, can be repeated */
21000 while (src[*usedlen] == ':'
21001 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
21002 {
21003 /* find a '.' in the tail:
21004 * - for second :e: before the current fname
21005 * - otherwise: The last '.'
21006 */
21007 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
21008 s = *fnamep - 2;
21009 else
21010 s = *fnamep + *fnamelen - 1;
21011 for ( ; s > tail; --s)
21012 if (s[0] == '.')
21013 break;
21014 if (src[*usedlen + 1] == 'e') /* :e */
21015 {
21016 if (s > tail)
21017 {
21018 *fnamelen += (int)(*fnamep - (s + 1));
21019 *fnamep = s + 1;
21020#ifdef VMS
21021 /* cut version from the extension */
21022 s = *fnamep + *fnamelen - 1;
21023 for ( ; s > *fnamep; --s)
21024 if (s[0] == ';')
21025 break;
21026 if (s > *fnamep)
21027 *fnamelen = s - *fnamep;
21028#endif
21029 }
21030 else if (*fnamep <= tail)
21031 *fnamelen = 0;
21032 }
21033 else /* :r */
21034 {
21035 if (s > tail) /* remove one extension */
21036 *fnamelen = (int)(s - *fnamep);
21037 }
21038 *usedlen += 2;
21039 }
21040
21041 /* ":s?pat?foo?" - substitute */
21042 /* ":gs?pat?foo?" - global substitute */
21043 if (src[*usedlen] == ':'
21044 && (src[*usedlen + 1] == 's'
21045 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
21046 {
21047 char_u *str;
21048 char_u *pat;
21049 char_u *sub;
21050 int sep;
21051 char_u *flags;
21052 int didit = FALSE;
21053
21054 flags = (char_u *)"";
21055 s = src + *usedlen + 2;
21056 if (src[*usedlen + 1] == 'g')
21057 {
21058 flags = (char_u *)"g";
21059 ++s;
21060 }
21061
21062 sep = *s++;
21063 if (sep)
21064 {
21065 /* find end of pattern */
21066 p = vim_strchr(s, sep);
21067 if (p != NULL)
21068 {
21069 pat = vim_strnsave(s, (int)(p - s));
21070 if (pat != NULL)
21071 {
21072 s = p + 1;
21073 /* find end of substitution */
21074 p = vim_strchr(s, sep);
21075 if (p != NULL)
21076 {
21077 sub = vim_strnsave(s, (int)(p - s));
21078 str = vim_strnsave(*fnamep, *fnamelen);
21079 if (sub != NULL && str != NULL)
21080 {
21081 *usedlen = (int)(p + 1 - src);
21082 s = do_string_sub(str, pat, sub, flags);
21083 if (s != NULL)
21084 {
21085 *fnamep = s;
21086 *fnamelen = (int)STRLEN(s);
21087 vim_free(*bufp);
21088 *bufp = s;
21089 didit = TRUE;
21090 }
21091 }
21092 vim_free(sub);
21093 vim_free(str);
21094 }
21095 vim_free(pat);
21096 }
21097 }
21098 /* after using ":s", repeat all the modifiers */
21099 if (didit)
21100 goto repeat;
21101 }
21102 }
21103
21104 return valid;
21105}
21106
21107/*
21108 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
21109 * "flags" can be "g" to do a global substitute.
21110 * Returns an allocated string, NULL for error.
21111 */
21112 char_u *
21113do_string_sub(str, pat, sub, flags)
21114 char_u *str;
21115 char_u *pat;
21116 char_u *sub;
21117 char_u *flags;
21118{
21119 int sublen;
21120 regmatch_T regmatch;
21121 int i;
21122 int do_all;
21123 char_u *tail;
21124 garray_T ga;
21125 char_u *ret;
21126 char_u *save_cpo;
21127
21128 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
21129 save_cpo = p_cpo;
21130 p_cpo = (char_u *)"";
21131
21132 ga_init2(&ga, 1, 200);
21133
21134 do_all = (flags[0] == 'g');
21135
21136 regmatch.rm_ic = p_ic;
21137 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
21138 if (regmatch.regprog != NULL)
21139 {
21140 tail = str;
21141 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
21142 {
21143 /*
21144 * Get some space for a temporary buffer to do the substitution
21145 * into. It will contain:
21146 * - The text up to where the match is.
21147 * - The substituted text.
21148 * - The text after the match.
21149 */
21150 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
21151 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
21152 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
21153 {
21154 ga_clear(&ga);
21155 break;
21156 }
21157
21158 /* copy the text up to where the match is */
21159 i = (int)(regmatch.startp[0] - tail);
21160 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
21161 /* add the substituted text */
21162 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
21163 + ga.ga_len + i, TRUE, TRUE, FALSE);
21164 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021165 /* avoid getting stuck on a match with an empty string */
21166 if (tail == regmatch.endp[0])
21167 {
21168 if (*tail == NUL)
21169 break;
21170 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
21171 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021172 }
21173 else
21174 {
21175 tail = regmatch.endp[0];
21176 if (*tail == NUL)
21177 break;
21178 }
21179 if (!do_all)
21180 break;
21181 }
21182
21183 if (ga.ga_data != NULL)
21184 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
21185
21186 vim_free(regmatch.regprog);
21187 }
21188
21189 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
21190 ga_clear(&ga);
21191 p_cpo = save_cpo;
21192
21193 return ret;
21194}
21195
21196#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */