blob: 5ff523e549b5a0078144ab36d550ee600f22ac69 [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 Moolenaar219b8702006-11-01 14:32:36 +0000346 {VV_NAME("mouse_win", VAR_NUMBER), 0},
347 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
348 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar33570922005-01-25 22:26:29 +0000349};
350
351/* shorthand */
352#define vv_type vv_di.di_tv.v_type
353#define vv_nr vv_di.di_tv.vval.v_number
354#define vv_str vv_di.di_tv.vval.v_string
355#define vv_tv vv_di.di_tv
356
357/*
358 * The v: variables are stored in dictionary "vimvardict".
359 * "vimvars_var" is the variable that is used for the "l:" scope.
360 */
361static dict_T vimvardict;
362static dictitem_T vimvars_var;
363#define vimvarht vimvardict.dv_hashtab
364
Bram Moolenaara40058a2005-07-11 22:42:07 +0000365static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
366static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
367#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
368static int call_vim_function __ARGS((char_u *func, int argc, char_u **argv, int safe, typval_T *rettv));
369#endif
370static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
371static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
372static char_u *skip_var_one __ARGS((char_u *arg));
373static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty));
374static void list_glob_vars __ARGS((void));
375static void list_buf_vars __ARGS((void));
376static void list_win_vars __ARGS((void));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000377#ifdef FEAT_WINDOWS
378static void list_tab_vars __ARGS((void));
379#endif
Bram Moolenaara40058a2005-07-11 22:42:07 +0000380static void list_vim_vars __ARGS((void));
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000381static void list_script_vars __ARGS((void));
382static void list_func_vars __ARGS((void));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000383static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg));
384static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
385static int check_changedtick __ARGS((char_u *arg));
386static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
387static void clear_lval __ARGS((lval_T *lp));
388static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
389static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
390static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
391static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
392static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
393static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
394static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
395static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
396static void item_lock __ARGS((typval_T *tv, int deep, int lock));
397static int tv_islocked __ARGS((typval_T *tv));
398
Bram Moolenaar33570922005-01-25 22:26:29 +0000399static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
400static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
401static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
402static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
403static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
404static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
405static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
406static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000407
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000408static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000409static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
410static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
411static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
412static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000413static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000414static listitem_T *listitem_alloc __ARGS((void));
415static void listitem_free __ARGS((listitem_T *item));
416static void listitem_remove __ARGS((list_T *l, listitem_T *item));
417static long list_len __ARGS((list_T *l));
418static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
419static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
420static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
Bram Moolenaar33570922005-01-25 22:26:29 +0000421static listitem_T *list_find __ARGS((list_T *l, long n));
Bram Moolenaara5525202006-03-02 22:52:09 +0000422static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000423static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000424static void list_append __ARGS((list_T *l, listitem_T *item));
425static int list_append_tv __ARGS((list_T *l, typval_T *tv));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000426static int list_append_string __ARGS((list_T *l, char_u *str, int len));
427static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000428static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
429static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
430static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000431static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000432static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000433static char_u *list2string __ARGS((typval_T *tv, int copyID));
434static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000435static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
436static void set_ref_in_list __ARGS((list_T *l, int copyID));
437static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000438static void dict_unref __ARGS((dict_T *d));
Bram Moolenaar685295c2006-10-15 20:37:38 +0000439static void dict_free __ARGS((dict_T *d, int recurse));
Bram Moolenaar33570922005-01-25 22:26:29 +0000440static dictitem_T *dictitem_alloc __ARGS((char_u *key));
441static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
442static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
443static void dictitem_free __ARGS((dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000444static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000445static int dict_add __ARGS((dict_T *d, dictitem_T *item));
446static long dict_len __ARGS((dict_T *d));
447static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000448static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000449static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000450static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
451static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000452static char_u *string_quote __ARGS((char_u *str, int function));
453static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
454static int find_internal_func __ARGS((char_u *name));
455static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
456static 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));
457static 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 +0000458static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000459
460static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
463static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
464static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
465static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
467static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
468static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
469static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
470static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000476static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000477static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000480#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000481static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000482static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
484#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000485static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
490static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000503static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000504static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000517static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000518static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000519static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000520static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000525static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000526static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000533static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000534static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000535static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000537static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000538static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000558static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000559static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000564static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000565static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000580static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000581static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000582static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000583static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000586#ifdef vim_mkdir
587static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
588#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000589static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000592static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000593static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000594static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000595static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000596static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000597static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000598static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000600static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000611static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000612static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000613static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000615static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
619static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000620static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000621static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000622static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000623static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000624static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000625static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000626static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000627static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
628static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000629static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000630static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
631static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000632static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2c932302006-03-18 21:42:09 +0000633static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000634#ifdef HAVE_STRFTIME
635static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
636#endif
637static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
638static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
639static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
640static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
641static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
642static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
643static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
644static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
645static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
646static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
647static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
648static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000649static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000650static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000651static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000652static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000653static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000654static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000655static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000656static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
657static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
658static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
659static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
660static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
661static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
662static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
664static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
665static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
666static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
667static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
668static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000669static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
670static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000671static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000672static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000673
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000674static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
675static pos_T *var2fpos __ARGS((typval_T *varp, int lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000676static int get_env_len __ARGS((char_u **arg));
677static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000678static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000679static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
680#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
681#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
682 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000683static 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 +0000684static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000685static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000686static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
687static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000688static typval_T *alloc_tv __ARGS((void));
689static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000690static void init_tv __ARGS((typval_T *varp));
691static long get_tv_number __ARGS((typval_T *varp));
692static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000693static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000694static char_u *get_tv_string __ARGS((typval_T *varp));
695static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000696static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000697static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000698static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000699static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
700static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
701static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
702static void list_one_var __ARGS((dictitem_T *v, char_u *prefix));
703static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
704static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
705static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000706static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000707static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000708static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000709static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000710static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
711static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
712static int eval_fname_script __ARGS((char_u *p));
713static int eval_fname_sid __ARGS((char_u *p));
714static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000715static ufunc_T *find_func __ARGS((char_u *name));
716static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000717static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000718#ifdef FEAT_PROFILE
719static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000720static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
721static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
722static int
723# ifdef __BORLANDC__
724 _RTLENTRYF
725# endif
726 prof_total_cmp __ARGS((const void *s1, const void *s2));
727static int
728# ifdef __BORLANDC__
729 _RTLENTRYF
730# endif
731 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000732#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000733static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000734static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000735static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000736static void func_free __ARGS((ufunc_T *fp));
737static void func_unref __ARGS((char_u *name));
738static void func_ref __ARGS((char_u *name));
739static 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));
740static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000741static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
742static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000743static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000744static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000745static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000746
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000747/* Character used as separated in autoload function/variable names. */
748#define AUTOLOAD_CHAR '#'
749
Bram Moolenaar33570922005-01-25 22:26:29 +0000750/*
751 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000752 */
753 void
754eval_init()
755{
Bram Moolenaar33570922005-01-25 22:26:29 +0000756 int i;
757 struct vimvar *p;
758
759 init_var_dict(&globvardict, &globvars_var);
760 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000761 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000762 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000763
764 for (i = 0; i < VV_LEN; ++i)
765 {
766 p = &vimvars[i];
767 STRCPY(p->vv_di.di_key, p->vv_name);
768 if (p->vv_flags & VV_RO)
769 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
770 else if (p->vv_flags & VV_RO_SBX)
771 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
772 else
773 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000774
775 /* add to v: scope dict, unless the value is not always available */
776 if (p->vv_type != VAR_UNKNOWN)
777 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000778 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000779 /* add to compat scope dict */
780 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000781 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000782}
783
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000784#if defined(EXITFREE) || defined(PROTO)
785 void
786eval_clear()
787{
788 int i;
789 struct vimvar *p;
790
791 for (i = 0; i < VV_LEN; ++i)
792 {
793 p = &vimvars[i];
794 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000795 {
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000796 vim_free(p->vv_di.di_tv.vval.v_string);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000797 p->vv_di.di_tv.vval.v_string = NULL;
798 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000799 }
800 hash_clear(&vimvarht);
801 hash_clear(&compat_hashtab);
802
803 /* script-local variables */
804 for (i = 1; i <= ga_scripts.ga_len; ++i)
805 vars_clear(&SCRIPT_VARS(i));
806 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000807 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000808
809 /* global variables */
810 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000811
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000812 /* functions */
Bram Moolenaard9fba312005-06-26 22:34:35 +0000813 free_all_functions();
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000814 hash_clear(&func_hashtab);
815
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000816 /* autoloaded script names */
817 ga_clear_strings(&ga_loaded);
818
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000819 /* unreferenced lists and dicts */
820 (void)garbage_collect();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000821}
822#endif
823
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000824/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 * Return the name of the executed function.
826 */
827 char_u *
828func_name(cookie)
829 void *cookie;
830{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000831 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832}
833
834/*
835 * Return the address holding the next breakpoint line for a funccall cookie.
836 */
837 linenr_T *
838func_breakpoint(cookie)
839 void *cookie;
840{
Bram Moolenaar33570922005-01-25 22:26:29 +0000841 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842}
843
844/*
845 * Return the address holding the debug tick for a funccall cookie.
846 */
847 int *
848func_dbg_tick(cookie)
849 void *cookie;
850{
Bram Moolenaar33570922005-01-25 22:26:29 +0000851 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852}
853
854/*
855 * Return the nesting level for a funccall cookie.
856 */
857 int
858func_level(cookie)
859 void *cookie;
860{
Bram Moolenaar33570922005-01-25 22:26:29 +0000861 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862}
863
864/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000865funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866
867/*
868 * Return TRUE when a function was ended by a ":return" command.
869 */
870 int
871current_func_returned()
872{
873 return current_funccal->returned;
874}
875
876
877/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 * Set an internal variable to a string value. Creates the variable if it does
879 * not already exist.
880 */
881 void
882set_internal_string_var(name, value)
883 char_u *name;
884 char_u *value;
885{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000886 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000887 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888
889 val = vim_strsave(value);
890 if (val != NULL)
891 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000892 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000893 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000895 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000896 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 }
898 }
899}
900
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000901static lval_T *redir_lval = NULL;
902static char_u *redir_endp = NULL;
903static char_u *redir_varname = NULL;
904
905/*
906 * Start recording command output to a variable
907 * Returns OK if successfully completed the setup. FAIL otherwise.
908 */
909 int
910var_redir_start(name, append)
911 char_u *name;
912 int append; /* append to an existing variable */
913{
914 int save_emsg;
915 int err;
916 typval_T tv;
917
918 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000919 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000920 {
921 EMSG(_(e_invarg));
922 return FAIL;
923 }
924
925 redir_varname = vim_strsave(name);
926 if (redir_varname == NULL)
927 return FAIL;
928
929 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
930 if (redir_lval == NULL)
931 {
932 var_redir_stop();
933 return FAIL;
934 }
935
936 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000937 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
938 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000939 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
940 {
941 if (redir_endp != NULL && *redir_endp != NUL)
942 /* Trailing characters are present after the variable name */
943 EMSG(_(e_trailing));
944 else
945 EMSG(_(e_invarg));
946 var_redir_stop();
947 return FAIL;
948 }
949
950 /* check if we can write to the variable: set it to or append an empty
951 * string */
952 save_emsg = did_emsg;
953 did_emsg = FALSE;
954 tv.v_type = VAR_STRING;
955 tv.vval.v_string = (char_u *)"";
956 if (append)
957 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
958 else
959 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
960 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000961 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000962 if (err)
963 {
964 var_redir_stop();
965 return FAIL;
966 }
967 if (redir_lval->ll_newkey != NULL)
968 {
969 /* Dictionary item was created, don't do it again. */
970 vim_free(redir_lval->ll_newkey);
971 redir_lval->ll_newkey = NULL;
972 }
973
974 return OK;
975}
976
977/*
978 * Append "value[len]" to the variable set by var_redir_start().
979 */
980 void
981var_redir_str(value, len)
982 char_u *value;
983 int len;
984{
985 char_u *val;
986 typval_T tv;
987 int save_emsg;
988 int err;
989
990 if (redir_lval == NULL)
991 return;
992
993 if (len == -1)
994 /* Append the entire string */
995 val = vim_strsave(value);
996 else
997 /* Append only the specified number of characters */
998 val = vim_strnsave(value, len);
999 if (val == NULL)
1000 return;
1001
1002 tv.v_type = VAR_STRING;
1003 tv.vval.v_string = val;
1004
1005 save_emsg = did_emsg;
1006 did_emsg = FALSE;
1007 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1008 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001009 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001010 if (err)
1011 var_redir_stop();
1012
1013 vim_free(tv.vval.v_string);
1014}
1015
1016/*
1017 * Stop redirecting command output to a variable.
1018 */
1019 void
1020var_redir_stop()
1021{
1022 if (redir_lval != NULL)
1023 {
1024 clear_lval(redir_lval);
1025 vim_free(redir_lval);
1026 redir_lval = NULL;
1027 }
1028 vim_free(redir_varname);
1029 redir_varname = NULL;
1030}
1031
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032# if defined(FEAT_MBYTE) || defined(PROTO)
1033 int
1034eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1035 char_u *enc_from;
1036 char_u *enc_to;
1037 char_u *fname_from;
1038 char_u *fname_to;
1039{
1040 int err = FALSE;
1041
1042 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1043 set_vim_var_string(VV_CC_TO, enc_to, -1);
1044 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1045 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1046 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1047 err = TRUE;
1048 set_vim_var_string(VV_CC_FROM, NULL, -1);
1049 set_vim_var_string(VV_CC_TO, NULL, -1);
1050 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1051 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1052
1053 if (err)
1054 return FAIL;
1055 return OK;
1056}
1057# endif
1058
1059# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1060 int
1061eval_printexpr(fname, args)
1062 char_u *fname;
1063 char_u *args;
1064{
1065 int err = FALSE;
1066
1067 set_vim_var_string(VV_FNAME_IN, fname, -1);
1068 set_vim_var_string(VV_CMDARG, args, -1);
1069 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1070 err = TRUE;
1071 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1072 set_vim_var_string(VV_CMDARG, NULL, -1);
1073
1074 if (err)
1075 {
1076 mch_remove(fname);
1077 return FAIL;
1078 }
1079 return OK;
1080}
1081# endif
1082
1083# if defined(FEAT_DIFF) || defined(PROTO)
1084 void
1085eval_diff(origfile, newfile, outfile)
1086 char_u *origfile;
1087 char_u *newfile;
1088 char_u *outfile;
1089{
1090 int err = FALSE;
1091
1092 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1093 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1094 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1095 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1096 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1097 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1098 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1099}
1100
1101 void
1102eval_patch(origfile, difffile, outfile)
1103 char_u *origfile;
1104 char_u *difffile;
1105 char_u *outfile;
1106{
1107 int err;
1108
1109 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1110 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1111 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1112 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1113 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1114 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1115 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1116}
1117# endif
1118
1119/*
1120 * Top level evaluation function, returning a boolean.
1121 * Sets "error" to TRUE if there was an error.
1122 * Return TRUE or FALSE.
1123 */
1124 int
1125eval_to_bool(arg, error, nextcmd, skip)
1126 char_u *arg;
1127 int *error;
1128 char_u **nextcmd;
1129 int skip; /* only parse, don't execute */
1130{
Bram Moolenaar33570922005-01-25 22:26:29 +00001131 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 int retval = FALSE;
1133
1134 if (skip)
1135 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001136 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 else
1139 {
1140 *error = FALSE;
1141 if (!skip)
1142 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001143 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001144 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 }
1146 }
1147 if (skip)
1148 --emsg_skip;
1149
1150 return retval;
1151}
1152
1153/*
1154 * Top level evaluation function, returning a string. If "skip" is TRUE,
1155 * only parsing to "nextcmd" is done, without reporting errors. Return
1156 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1157 */
1158 char_u *
1159eval_to_string_skip(arg, nextcmd, skip)
1160 char_u *arg;
1161 char_u **nextcmd;
1162 int skip; /* only parse, don't execute */
1163{
Bram Moolenaar33570922005-01-25 22:26:29 +00001164 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 char_u *retval;
1166
1167 if (skip)
1168 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001169 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 retval = NULL;
1171 else
1172 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001173 retval = vim_strsave(get_tv_string(&tv));
1174 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 }
1176 if (skip)
1177 --emsg_skip;
1178
1179 return retval;
1180}
1181
1182/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001183 * Skip over an expression at "*pp".
1184 * Return FAIL for an error, OK otherwise.
1185 */
1186 int
1187skip_expr(pp)
1188 char_u **pp;
1189{
Bram Moolenaar33570922005-01-25 22:26:29 +00001190 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001191
1192 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001193 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001194}
1195
1196/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 * Top level evaluation function, returning a string.
1198 * Return pointer to allocated memory, or NULL for failure.
1199 */
1200 char_u *
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001201eval_to_string(arg, nextcmd, dolist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 char_u *arg;
1203 char_u **nextcmd;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001204 int dolist; /* turn List into sequence of lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205{
Bram Moolenaar33570922005-01-25 22:26:29 +00001206 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001208 garray_T ga;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001210 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 retval = NULL;
1212 else
1213 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001214 if (dolist && tv.v_type == VAR_LIST)
1215 {
1216 ga_init2(&ga, (int)sizeof(char), 80);
1217 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
1218 ga_append(&ga, NUL);
1219 retval = (char_u *)ga.ga_data;
1220 }
1221 else
1222 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001223 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 }
1225
1226 return retval;
1227}
1228
1229/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001230 * Call eval_to_string() without using current local variables and using
1231 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 */
1233 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001234eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 char_u *arg;
1236 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001237 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238{
1239 char_u *retval;
1240 void *save_funccalp;
1241
1242 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001243 if (use_sandbox)
1244 ++sandbox;
1245 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001246 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001247 if (use_sandbox)
1248 --sandbox;
1249 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 restore_funccal(save_funccalp);
1251 return retval;
1252}
1253
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254/*
1255 * Top level evaluation function, returning a number.
1256 * Evaluates "expr" silently.
1257 * Returns -1 for an error.
1258 */
1259 int
1260eval_to_number(expr)
1261 char_u *expr;
1262{
Bram Moolenaar33570922005-01-25 22:26:29 +00001263 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001265 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266
1267 ++emsg_off;
1268
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001269 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 retval = -1;
1271 else
1272 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001273 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001274 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 }
1276 --emsg_off;
1277
1278 return retval;
1279}
1280
Bram Moolenaara40058a2005-07-11 22:42:07 +00001281/*
1282 * Prepare v: variable "idx" to be used.
1283 * Save the current typeval in "save_tv".
1284 * When not used yet add the variable to the v: hashtable.
1285 */
1286 static void
1287prepare_vimvar(idx, save_tv)
1288 int idx;
1289 typval_T *save_tv;
1290{
1291 *save_tv = vimvars[idx].vv_tv;
1292 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1293 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1294}
1295
1296/*
1297 * Restore v: variable "idx" to typeval "save_tv".
1298 * When no longer defined, remove the variable from the v: hashtable.
1299 */
1300 static void
1301restore_vimvar(idx, save_tv)
1302 int idx;
1303 typval_T *save_tv;
1304{
1305 hashitem_T *hi;
1306
1307 clear_tv(&vimvars[idx].vv_tv);
1308 vimvars[idx].vv_tv = *save_tv;
1309 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1310 {
1311 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1312 if (HASHITEM_EMPTY(hi))
1313 EMSG2(_(e_intern2), "restore_vimvar()");
1314 else
1315 hash_remove(&vimvarht, hi);
1316 }
1317}
1318
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001319#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001320/*
1321 * Evaluate an expression to a list with suggestions.
1322 * For the "expr:" part of 'spellsuggest'.
1323 */
1324 list_T *
1325eval_spell_expr(badword, expr)
1326 char_u *badword;
1327 char_u *expr;
1328{
1329 typval_T save_val;
1330 typval_T rettv;
1331 list_T *list = NULL;
1332 char_u *p = skipwhite(expr);
1333
1334 /* Set "v:val" to the bad word. */
1335 prepare_vimvar(VV_VAL, &save_val);
1336 vimvars[VV_VAL].vv_type = VAR_STRING;
1337 vimvars[VV_VAL].vv_str = badword;
1338 if (p_verbose == 0)
1339 ++emsg_off;
1340
1341 if (eval1(&p, &rettv, TRUE) == OK)
1342 {
1343 if (rettv.v_type != VAR_LIST)
1344 clear_tv(&rettv);
1345 else
1346 list = rettv.vval.v_list;
1347 }
1348
1349 if (p_verbose == 0)
1350 --emsg_off;
1351 vimvars[VV_VAL].vv_str = NULL;
1352 restore_vimvar(VV_VAL, &save_val);
1353
1354 return list;
1355}
1356
1357/*
1358 * "list" is supposed to contain two items: a word and a number. Return the
1359 * word in "pp" and the number as the return value.
1360 * Return -1 if anything isn't right.
1361 * Used to get the good word and score from the eval_spell_expr() result.
1362 */
1363 int
1364get_spellword(list, pp)
1365 list_T *list;
1366 char_u **pp;
1367{
1368 listitem_T *li;
1369
1370 li = list->lv_first;
1371 if (li == NULL)
1372 return -1;
1373 *pp = get_tv_string(&li->li_tv);
1374
1375 li = li->li_next;
1376 if (li == NULL)
1377 return -1;
1378 return get_tv_number(&li->li_tv);
1379}
1380#endif
1381
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001382/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001383 * Top level evaluation function.
1384 * Returns an allocated typval_T with the result.
1385 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001386 */
1387 typval_T *
1388eval_expr(arg, nextcmd)
1389 char_u *arg;
1390 char_u **nextcmd;
1391{
1392 typval_T *tv;
1393
1394 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001395 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001396 {
1397 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001398 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001399 }
1400
1401 return tv;
1402}
1403
1404
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1406/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001407 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 * Uses argv[argc] for the function arguments.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001409 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001411 static int
1412call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 char_u *func;
1414 int argc;
1415 char_u **argv;
1416 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001417 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418{
Bram Moolenaar33570922005-01-25 22:26:29 +00001419 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 long n;
1421 int len;
1422 int i;
1423 int doesrange;
1424 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001425 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001427 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001429 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430
1431 for (i = 0; i < argc; i++)
1432 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001433 /* Pass a NULL or empty argument as an empty string */
1434 if (argv[i] == NULL || *argv[i] == NUL)
1435 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001436 argvars[i].v_type = VAR_STRING;
1437 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001438 continue;
1439 }
1440
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 /* Recognize a number argument, the others must be strings. */
1442 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1443 if (len != 0 && len == (int)STRLEN(argv[i]))
1444 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001445 argvars[i].v_type = VAR_NUMBER;
1446 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 }
1448 else
1449 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001450 argvars[i].v_type = VAR_STRING;
1451 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 }
1453 }
1454
1455 if (safe)
1456 {
1457 save_funccalp = save_funccal();
1458 ++sandbox;
1459 }
1460
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001461 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1462 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001464 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 if (safe)
1466 {
1467 --sandbox;
1468 restore_funccal(save_funccalp);
1469 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001470 vim_free(argvars);
1471
1472 if (ret == FAIL)
1473 clear_tv(rettv);
1474
1475 return ret;
1476}
1477
1478/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001479 * Call vimL function "func" and return the result as a string.
1480 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001481 * Uses argv[argc] for the function arguments.
1482 */
1483 void *
1484call_func_retstr(func, argc, argv, safe)
1485 char_u *func;
1486 int argc;
1487 char_u **argv;
1488 int safe; /* use the sandbox */
1489{
1490 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001491 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001492
1493 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1494 return NULL;
1495
1496 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001497 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 return retval;
1499}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001500
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001501#if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001502/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001503 * Call vimL function "func" and return the result as a number.
1504 * Returns -1 when calling the function fails.
1505 * Uses argv[argc] for the function arguments.
1506 */
1507 long
1508call_func_retnr(func, argc, argv, safe)
1509 char_u *func;
1510 int argc;
1511 char_u **argv;
1512 int safe; /* use the sandbox */
1513{
1514 typval_T rettv;
1515 long retval;
1516
1517 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1518 return -1;
1519
1520 retval = get_tv_number_chk(&rettv, NULL);
1521 clear_tv(&rettv);
1522 return retval;
1523}
1524#endif
1525
1526/*
1527 * Call vimL function "func" and return the result as a list
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001528 * Uses argv[argc] for the function arguments.
1529 */
1530 void *
1531call_func_retlist(func, argc, argv, safe)
1532 char_u *func;
1533 int argc;
1534 char_u **argv;
1535 int safe; /* use the sandbox */
1536{
1537 typval_T rettv;
1538
1539 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1540 return NULL;
1541
1542 if (rettv.v_type != VAR_LIST)
1543 {
1544 clear_tv(&rettv);
1545 return NULL;
1546 }
1547
1548 return rettv.vval.v_list;
1549}
1550
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551#endif
1552
1553/*
1554 * Save the current function call pointer, and set it to NULL.
1555 * Used when executing autocommands and for ":source".
1556 */
1557 void *
1558save_funccal()
1559{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001560 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 current_funccal = NULL;
1563 return (void *)fc;
1564}
1565
1566 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001567restore_funccal(vfc)
1568 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001570 funccall_T *fc = (funccall_T *)vfc;
1571
1572 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573}
1574
Bram Moolenaar05159a02005-02-26 23:04:13 +00001575#if defined(FEAT_PROFILE) || defined(PROTO)
1576/*
1577 * Prepare profiling for entering a child or something else that is not
1578 * counted for the script/function itself.
1579 * Should always be called in pair with prof_child_exit().
1580 */
1581 void
1582prof_child_enter(tm)
1583 proftime_T *tm; /* place to store waittime */
1584{
1585 funccall_T *fc = current_funccal;
1586
1587 if (fc != NULL && fc->func->uf_profiling)
1588 profile_start(&fc->prof_child);
1589 script_prof_save(tm);
1590}
1591
1592/*
1593 * Take care of time spent in a child.
1594 * Should always be called after prof_child_enter().
1595 */
1596 void
1597prof_child_exit(tm)
1598 proftime_T *tm; /* where waittime was stored */
1599{
1600 funccall_T *fc = current_funccal;
1601
1602 if (fc != NULL && fc->func->uf_profiling)
1603 {
1604 profile_end(&fc->prof_child);
1605 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1606 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1607 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1608 }
1609 script_prof_restore(tm);
1610}
1611#endif
1612
1613
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614#ifdef FEAT_FOLDING
1615/*
1616 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1617 * it in "*cp". Doesn't give error messages.
1618 */
1619 int
1620eval_foldexpr(arg, cp)
1621 char_u *arg;
1622 int *cp;
1623{
Bram Moolenaar33570922005-01-25 22:26:29 +00001624 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 int retval;
1626 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001627 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1628 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629
1630 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001631 if (use_sandbox)
1632 ++sandbox;
1633 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001635 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 retval = 0;
1637 else
1638 {
1639 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001640 if (tv.v_type == VAR_NUMBER)
1641 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001642 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 retval = 0;
1644 else
1645 {
1646 /* If the result is a string, check if there is a non-digit before
1647 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001648 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 if (!VIM_ISDIGIT(*s) && *s != '-')
1650 *cp = *s++;
1651 retval = atol((char *)s);
1652 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001653 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 }
1655 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001656 if (use_sandbox)
1657 --sandbox;
1658 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659
1660 return retval;
1661}
1662#endif
1663
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001665 * ":let" list all variable values
1666 * ":let var1 var2" list variable values
1667 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001668 * ":let var += expr" assignment command.
1669 * ":let var -= expr" assignment command.
1670 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001671 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 */
1673 void
1674ex_let(eap)
1675 exarg_T *eap;
1676{
1677 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001678 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001679 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001681 int var_count = 0;
1682 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001683 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001684 char_u *argend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685
Bram Moolenaardb552d602006-03-23 22:59:57 +00001686 argend = skip_var_list(arg, &var_count, &semicolon);
1687 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001688 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001689 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1690 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001691 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001692 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001694 /*
1695 * ":let" without "=": list variables
1696 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001697 if (*arg == '[')
1698 EMSG(_(e_invarg));
1699 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001700 /* ":let var1 var2" */
1701 arg = list_arg_vars(eap, arg);
1702 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001703 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001704 /* ":let" */
Bram Moolenaara7043832005-01-21 11:56:39 +00001705 list_glob_vars();
1706 list_buf_vars();
1707 list_win_vars();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001708#ifdef FEAT_WINDOWS
1709 list_tab_vars();
1710#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001711 list_script_vars();
1712 list_func_vars();
Bram Moolenaara7043832005-01-21 11:56:39 +00001713 list_vim_vars();
1714 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 eap->nextcmd = check_nextcmd(arg);
1716 }
1717 else
1718 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001719 op[0] = '=';
1720 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001721 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001722 {
1723 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1724 op[0] = expr[-1]; /* +=, -= or .= */
1725 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001726 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001727
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 if (eap->skip)
1729 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001730 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 if (eap->skip)
1732 {
1733 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001734 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 --emsg_skip;
1736 }
1737 else if (i != FAIL)
1738 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001739 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001740 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001741 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 }
1743 }
1744}
1745
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001746/*
1747 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1748 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001749 * When "nextchars" is not NULL it points to a string with characters that
1750 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1751 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001752 * Returns OK or FAIL;
1753 */
1754 static int
1755ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1756 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001757 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001758 int copy; /* copy values from "tv", don't move */
1759 int semicolon; /* from skip_var_list() */
1760 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001761 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001762{
1763 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001764 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001765 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001766 listitem_T *item;
1767 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001768
1769 if (*arg != '[')
1770 {
1771 /*
1772 * ":let var = expr" or ":for var in list"
1773 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001774 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001775 return FAIL;
1776 return OK;
1777 }
1778
1779 /*
1780 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1781 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001782 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001783 {
1784 EMSG(_(e_listreq));
1785 return FAIL;
1786 }
1787
1788 i = list_len(l);
1789 if (semicolon == 0 && var_count < i)
1790 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001791 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001792 return FAIL;
1793 }
1794 if (var_count - semicolon > i)
1795 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001796 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001797 return FAIL;
1798 }
1799
1800 item = l->lv_first;
1801 while (*arg != ']')
1802 {
1803 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001804 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001805 item = item->li_next;
1806 if (arg == NULL)
1807 return FAIL;
1808
1809 arg = skipwhite(arg);
1810 if (*arg == ';')
1811 {
1812 /* Put the rest of the list (may be empty) in the var after ';'.
1813 * Create a new list for this. */
1814 l = list_alloc();
1815 if (l == NULL)
1816 return FAIL;
1817 while (item != NULL)
1818 {
1819 list_append_tv(l, &item->li_tv);
1820 item = item->li_next;
1821 }
1822
1823 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001824 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001825 ltv.vval.v_list = l;
1826 l->lv_refcount = 1;
1827
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001828 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1829 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001830 clear_tv(&ltv);
1831 if (arg == NULL)
1832 return FAIL;
1833 break;
1834 }
1835 else if (*arg != ',' && *arg != ']')
1836 {
1837 EMSG2(_(e_intern2), "ex_let_vars()");
1838 return FAIL;
1839 }
1840 }
1841
1842 return OK;
1843}
1844
1845/*
1846 * Skip over assignable variable "var" or list of variables "[var, var]".
1847 * Used for ":let varvar = expr" and ":for varvar in expr".
1848 * For "[var, var]" increment "*var_count" for each variable.
1849 * for "[var, var; var]" set "semicolon".
1850 * Return NULL for an error.
1851 */
1852 static char_u *
1853skip_var_list(arg, var_count, semicolon)
1854 char_u *arg;
1855 int *var_count;
1856 int *semicolon;
1857{
1858 char_u *p, *s;
1859
1860 if (*arg == '[')
1861 {
1862 /* "[var, var]": find the matching ']'. */
1863 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001864 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001865 {
1866 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1867 s = skip_var_one(p);
1868 if (s == p)
1869 {
1870 EMSG2(_(e_invarg2), p);
1871 return NULL;
1872 }
1873 ++*var_count;
1874
1875 p = skipwhite(s);
1876 if (*p == ']')
1877 break;
1878 else if (*p == ';')
1879 {
1880 if (*semicolon == 1)
1881 {
1882 EMSG(_("Double ; in list of variables"));
1883 return NULL;
1884 }
1885 *semicolon = 1;
1886 }
1887 else if (*p != ',')
1888 {
1889 EMSG2(_(e_invarg2), p);
1890 return NULL;
1891 }
1892 }
1893 return p + 1;
1894 }
1895 else
1896 return skip_var_one(arg);
1897}
1898
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001899/*
Bram Moolenaar92124a32005-06-17 22:03:40 +00001900 * Skip one (assignable) variable name, includig @r, $VAR, &option, d.key,
1901 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001902 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001903 static char_u *
1904skip_var_one(arg)
1905 char_u *arg;
1906{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001907 if (*arg == '@' && arg[1] != NUL)
1908 return arg + 2;
1909 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1910 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001911}
1912
Bram Moolenaara7043832005-01-21 11:56:39 +00001913/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001914 * List variables for hashtab "ht" with prefix "prefix".
1915 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001916 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001917 static void
Bram Moolenaar33570922005-01-25 22:26:29 +00001918list_hashtable_vars(ht, prefix, empty)
1919 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001920 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001921 int empty;
Bram Moolenaara7043832005-01-21 11:56:39 +00001922{
Bram Moolenaar33570922005-01-25 22:26:29 +00001923 hashitem_T *hi;
1924 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001925 int todo;
1926
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001927 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001928 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1929 {
1930 if (!HASHITEM_EMPTY(hi))
1931 {
1932 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001933 di = HI2DI(hi);
1934 if (empty || di->di_tv.v_type != VAR_STRING
1935 || di->di_tv.vval.v_string != NULL)
1936 list_one_var(di, prefix);
Bram Moolenaara7043832005-01-21 11:56:39 +00001937 }
1938 }
1939}
1940
1941/*
1942 * List global variables.
1943 */
1944 static void
1945list_glob_vars()
1946{
Bram Moolenaar33570922005-01-25 22:26:29 +00001947 list_hashtable_vars(&globvarht, (char_u *)"", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001948}
1949
1950/*
1951 * List buffer variables.
1952 */
1953 static void
1954list_buf_vars()
1955{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001956 char_u numbuf[NUMBUFLEN];
1957
Bram Moolenaar33570922005-01-25 22:26:29 +00001958 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001959
1960 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
1961 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER, numbuf);
Bram Moolenaara7043832005-01-21 11:56:39 +00001962}
1963
1964/*
1965 * List window variables.
1966 */
1967 static void
1968list_win_vars()
1969{
Bram Moolenaar33570922005-01-25 22:26:29 +00001970 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001971}
1972
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001973#ifdef FEAT_WINDOWS
1974/*
1975 * List tab page variables.
1976 */
1977 static void
1978list_tab_vars()
1979{
1980 list_hashtable_vars(&curtab->tp_vars.dv_hashtab, (char_u *)"t:", TRUE);
1981}
1982#endif
1983
Bram Moolenaara7043832005-01-21 11:56:39 +00001984/*
1985 * List Vim variables.
1986 */
1987 static void
1988list_vim_vars()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001989{
Bram Moolenaar33570922005-01-25 22:26:29 +00001990 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001991}
1992
1993/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001994 * List script-local variables, if there is a script.
1995 */
1996 static void
1997list_script_vars()
1998{
1999 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
2000 list_hashtable_vars(&SCRIPT_VARS(current_SID), (char_u *)"s:", FALSE);
2001}
2002
2003/*
2004 * List function variables, if there is a function.
2005 */
2006 static void
2007list_func_vars()
2008{
2009 if (current_funccal != NULL)
2010 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
2011 (char_u *)"l:", FALSE);
2012}
2013
2014/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002015 * List variables in "arg".
2016 */
2017 static char_u *
2018list_arg_vars(eap, arg)
2019 exarg_T *eap;
2020 char_u *arg;
2021{
2022 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002023 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002024 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002025 char_u *name_start;
2026 char_u *arg_subsc;
2027 char_u *tofree;
2028 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002029
2030 while (!ends_excmd(*arg) && !got_int)
2031 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002032 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002033 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002034 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002035 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2036 {
2037 emsg_severe = TRUE;
2038 EMSG(_(e_trailing));
2039 break;
2040 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002041 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002042 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002043 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002044 /* get_name_len() takes care of expanding curly braces */
2045 name_start = name = arg;
2046 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2047 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002048 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002049 /* This is mainly to keep test 49 working: when expanding
2050 * curly braces fails overrule the exception error message. */
2051 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002052 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002053 emsg_severe = TRUE;
2054 EMSG2(_(e_invarg2), arg);
2055 break;
2056 }
2057 error = TRUE;
2058 }
2059 else
2060 {
2061 if (tofree != NULL)
2062 name = tofree;
2063 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002064 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002065 else
2066 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002067 /* handle d.key, l[idx], f(expr) */
2068 arg_subsc = arg;
2069 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002070 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002071 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002072 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002073 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002074 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002075 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002076 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002077 case 'g': list_glob_vars(); break;
2078 case 'b': list_buf_vars(); break;
2079 case 'w': list_win_vars(); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002080#ifdef FEAT_WINDOWS
2081 case 't': list_tab_vars(); break;
2082#endif
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002083 case 'v': list_vim_vars(); break;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002084 case 's': list_script_vars(); break;
2085 case 'l': list_func_vars(); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002086 default:
2087 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002088 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002089 }
2090 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002091 {
2092 char_u numbuf[NUMBUFLEN];
2093 char_u *tf;
2094 int c;
2095 char_u *s;
2096
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002097 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002098 c = *arg;
2099 *arg = NUL;
2100 list_one_var_a((char_u *)"",
2101 arg == arg_subsc ? name : name_start,
2102 tv.v_type, s == NULL ? (char_u *)"" : s);
2103 *arg = c;
2104 vim_free(tf);
2105 }
2106 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002107 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002108 }
2109 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002110
2111 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002112 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002113
2114 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002115 }
2116
2117 return arg;
2118}
2119
2120/*
2121 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2122 * Returns a pointer to the char just after the var name.
2123 * Returns NULL if there is an error.
2124 */
2125 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002126ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002127 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002128 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002129 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002130 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002131 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002132{
2133 int c1;
2134 char_u *name;
2135 char_u *p;
2136 char_u *arg_end = NULL;
2137 int len;
2138 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002139 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002140
2141 /*
2142 * ":let $VAR = expr": Set environment variable.
2143 */
2144 if (*arg == '$')
2145 {
2146 /* Find the end of the name. */
2147 ++arg;
2148 name = arg;
2149 len = get_env_len(&arg);
2150 if (len == 0)
2151 EMSG2(_(e_invarg2), name - 1);
2152 else
2153 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002154 if (op != NULL && (*op == '+' || *op == '-'))
2155 EMSG2(_(e_letwrong), op);
2156 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002157 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002158 EMSG(_(e_letunexp));
2159 else
2160 {
2161 c1 = name[len];
2162 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002163 p = get_tv_string_chk(tv);
2164 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002165 {
2166 int mustfree = FALSE;
2167 char_u *s = vim_getenv(name, &mustfree);
2168
2169 if (s != NULL)
2170 {
2171 p = tofree = concat_str(s, p);
2172 if (mustfree)
2173 vim_free(s);
2174 }
2175 }
2176 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002177 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002178 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002179 if (STRICMP(name, "HOME") == 0)
2180 init_homedir();
2181 else if (didset_vim && STRICMP(name, "VIM") == 0)
2182 didset_vim = FALSE;
2183 else if (didset_vimruntime
2184 && STRICMP(name, "VIMRUNTIME") == 0)
2185 didset_vimruntime = FALSE;
2186 arg_end = arg;
2187 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002188 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002189 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002190 }
2191 }
2192 }
2193
2194 /*
2195 * ":let &option = expr": Set option value.
2196 * ":let &l:option = expr": Set local option value.
2197 * ":let &g:option = expr": Set global option value.
2198 */
2199 else if (*arg == '&')
2200 {
2201 /* Find the end of the name. */
2202 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002203 if (p == NULL || (endchars != NULL
2204 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002205 EMSG(_(e_letunexp));
2206 else
2207 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002208 long n;
2209 int opt_type;
2210 long numval;
2211 char_u *stringval = NULL;
2212 char_u *s;
2213
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002214 c1 = *p;
2215 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002216
2217 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002218 s = get_tv_string_chk(tv); /* != NULL if number or string */
2219 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002220 {
2221 opt_type = get_option_value(arg, &numval,
2222 &stringval, opt_flags);
2223 if ((opt_type == 1 && *op == '.')
2224 || (opt_type == 0 && *op != '.'))
2225 EMSG2(_(e_letwrong), op);
2226 else
2227 {
2228 if (opt_type == 1) /* number */
2229 {
2230 if (*op == '+')
2231 n = numval + n;
2232 else
2233 n = numval - n;
2234 }
2235 else if (opt_type == 0 && stringval != NULL) /* string */
2236 {
2237 s = concat_str(stringval, s);
2238 vim_free(stringval);
2239 stringval = s;
2240 }
2241 }
2242 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002243 if (s != NULL)
2244 {
2245 set_option_value(arg, n, s, opt_flags);
2246 arg_end = p;
2247 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002248 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002249 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002250 }
2251 }
2252
2253 /*
2254 * ":let @r = expr": Set register contents.
2255 */
2256 else if (*arg == '@')
2257 {
2258 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002259 if (op != NULL && (*op == '+' || *op == '-'))
2260 EMSG2(_(e_letwrong), op);
2261 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002262 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002263 EMSG(_(e_letunexp));
2264 else
2265 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002266 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002267 char_u *s;
2268
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002269 p = get_tv_string_chk(tv);
2270 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002271 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002272 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002273 if (s != NULL)
2274 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002275 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002276 vim_free(s);
2277 }
2278 }
2279 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002280 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002281 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002282 arg_end = arg + 1;
2283 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002284 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002285 }
2286 }
2287
2288 /*
2289 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002290 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002291 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002292 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002293 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002294 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002295
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002296 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002297 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002298 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002299 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2300 EMSG(_(e_letunexp));
2301 else
2302 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002303 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002304 arg_end = p;
2305 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002306 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002307 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002308 }
2309
2310 else
2311 EMSG2(_(e_invarg2), arg);
2312
2313 return arg_end;
2314}
2315
2316/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002317 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2318 */
2319 static int
2320check_changedtick(arg)
2321 char_u *arg;
2322{
2323 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2324 {
2325 EMSG2(_(e_readonlyvar), arg);
2326 return TRUE;
2327 }
2328 return FALSE;
2329}
2330
2331/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002332 * Get an lval: variable, Dict item or List item that can be assigned a value
2333 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2334 * "name.key", "name.key[expr]" etc.
2335 * Indexing only works if "name" is an existing List or Dictionary.
2336 * "name" points to the start of the name.
2337 * If "rettv" is not NULL it points to the value to be assigned.
2338 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2339 * wrong; must end in space or cmd separator.
2340 *
2341 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002342 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002343 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002344 */
2345 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002346get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002347 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002348 typval_T *rettv;
2349 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002350 int unlet;
2351 int skip;
2352 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002353 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002354{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002355 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002356 char_u *expr_start, *expr_end;
2357 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002358 dictitem_T *v;
2359 typval_T var1;
2360 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002361 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002362 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002363 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002364 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002365 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002366
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002367 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002368 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002369
2370 if (skip)
2371 {
2372 /* When skipping just find the end of the name. */
2373 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002374 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002375 }
2376
2377 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002378 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002379 if (expr_start != NULL)
2380 {
2381 /* Don't expand the name when we already know there is an error. */
2382 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2383 && *p != '[' && *p != '.')
2384 {
2385 EMSG(_(e_trailing));
2386 return NULL;
2387 }
2388
2389 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2390 if (lp->ll_exp_name == NULL)
2391 {
2392 /* Report an invalid expression in braces, unless the
2393 * expression evaluation has been cancelled due to an
2394 * aborting error, an interrupt, or an exception. */
2395 if (!aborting() && !quiet)
2396 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002397 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002398 EMSG2(_(e_invarg2), name);
2399 return NULL;
2400 }
2401 }
2402 lp->ll_name = lp->ll_exp_name;
2403 }
2404 else
2405 lp->ll_name = name;
2406
2407 /* Without [idx] or .key we are done. */
2408 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2409 return p;
2410
2411 cc = *p;
2412 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002413 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002414 if (v == NULL && !quiet)
2415 EMSG2(_(e_undefvar), lp->ll_name);
2416 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002417 if (v == NULL)
2418 return NULL;
2419
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002420 /*
2421 * Loop until no more [idx] or .key is following.
2422 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002423 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002424 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002425 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002426 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2427 && !(lp->ll_tv->v_type == VAR_DICT
2428 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002429 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002430 if (!quiet)
2431 EMSG(_("E689: Can only index a List or Dictionary"));
2432 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002433 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002434 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002435 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002436 if (!quiet)
2437 EMSG(_("E708: [:] must come last"));
2438 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002439 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002440
Bram Moolenaar8c711452005-01-14 21:53:12 +00002441 len = -1;
2442 if (*p == '.')
2443 {
2444 key = p + 1;
2445 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2446 ;
2447 if (len == 0)
2448 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002449 if (!quiet)
2450 EMSG(_(e_emptykey));
2451 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002452 }
2453 p = key + len;
2454 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002455 else
2456 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002457 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002458 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002459 if (*p == ':')
2460 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002461 else
2462 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002463 empty1 = FALSE;
2464 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002465 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002466 if (get_tv_string_chk(&var1) == NULL)
2467 {
2468 /* not a number or string */
2469 clear_tv(&var1);
2470 return NULL;
2471 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002472 }
2473
2474 /* Optionally get the second index [ :expr]. */
2475 if (*p == ':')
2476 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002477 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002478 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002479 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002480 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002481 if (!empty1)
2482 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002483 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002484 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002485 if (rettv != NULL && (rettv->v_type != VAR_LIST
2486 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002487 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002488 if (!quiet)
2489 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002490 if (!empty1)
2491 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002492 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002493 }
2494 p = skipwhite(p + 1);
2495 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002496 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002497 else
2498 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002499 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002500 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2501 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002502 if (!empty1)
2503 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002504 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002505 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002506 if (get_tv_string_chk(&var2) == NULL)
2507 {
2508 /* not a number or string */
2509 if (!empty1)
2510 clear_tv(&var1);
2511 clear_tv(&var2);
2512 return NULL;
2513 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002514 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002515 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002516 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002517 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002518 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002519
Bram Moolenaar8c711452005-01-14 21:53:12 +00002520 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002521 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002522 if (!quiet)
2523 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002524 if (!empty1)
2525 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002526 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002527 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002528 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002529 }
2530
2531 /* Skip to past ']'. */
2532 ++p;
2533 }
2534
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002535 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002536 {
2537 if (len == -1)
2538 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002539 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002540 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002541 if (*key == NUL)
2542 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002543 if (!quiet)
2544 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002545 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002546 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002547 }
2548 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002549 lp->ll_list = NULL;
2550 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002551 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002552 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002553 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002554 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002555 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002556 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002557 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002558 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002559 if (len == -1)
2560 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002561 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002562 }
2563 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002564 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002565 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002566 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002567 if (len == -1)
2568 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002569 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002570 p = NULL;
2571 break;
2572 }
2573 if (len == -1)
2574 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002575 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002576 }
2577 else
2578 {
2579 /*
2580 * Get the number and item for the only or first index of the List.
2581 */
2582 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002583 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002584 else
2585 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002586 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002587 clear_tv(&var1);
2588 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002589 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002590 lp->ll_list = lp->ll_tv->vval.v_list;
2591 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2592 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002593 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002594 if (lp->ll_n1 < 0)
2595 {
2596 lp->ll_n1 = 0;
2597 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2598 }
2599 }
2600 if (lp->ll_li == NULL)
2601 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002602 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002603 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002604 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002605 }
2606
2607 /*
2608 * May need to find the item or absolute index for the second
2609 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002610 * When no index given: "lp->ll_empty2" is TRUE.
2611 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002612 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002613 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002614 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002615 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002616 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002617 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002618 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002619 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002620 if (ni == NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002621 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002622 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002623 }
2624
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002625 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2626 if (lp->ll_n1 < 0)
2627 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2628 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002629 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002630 }
2631
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002632 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002633 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002634 }
2635
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002636 return p;
2637}
2638
2639/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002640 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002641 */
2642 static void
2643clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002644 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002645{
2646 vim_free(lp->ll_exp_name);
2647 vim_free(lp->ll_newkey);
2648}
2649
2650/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002651 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002652 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002653 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002654 */
2655 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002656set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002657 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002658 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002659 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002660 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002661 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002662{
2663 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002664 listitem_T *ri;
2665 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002666
2667 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002668 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002669 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002670 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002671 cc = *endp;
2672 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002673 if (op != NULL && *op != '=')
2674 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002675 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002676
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002677 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002678 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002679 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002680 {
2681 if (tv_op(&tv, rettv, op) == OK)
2682 set_var(lp->ll_name, &tv, FALSE);
2683 clear_tv(&tv);
2684 }
2685 }
2686 else
2687 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002688 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002689 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002690 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002691 else if (tv_check_lock(lp->ll_newkey == NULL
2692 ? lp->ll_tv->v_lock
2693 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2694 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002695 else if (lp->ll_range)
2696 {
2697 /*
2698 * Assign the List values to the list items.
2699 */
2700 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002701 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002702 if (op != NULL && *op != '=')
2703 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2704 else
2705 {
2706 clear_tv(&lp->ll_li->li_tv);
2707 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2708 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002709 ri = ri->li_next;
2710 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2711 break;
2712 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002713 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002714 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002715 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002716 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002717 ri = NULL;
2718 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002719 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002720 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002721 lp->ll_li = lp->ll_li->li_next;
2722 ++lp->ll_n1;
2723 }
2724 if (ri != NULL)
2725 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002726 else if (lp->ll_empty2
2727 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002728 : lp->ll_n1 != lp->ll_n2)
2729 EMSG(_("E711: List value has not enough items"));
2730 }
2731 else
2732 {
2733 /*
2734 * Assign to a List or Dictionary item.
2735 */
2736 if (lp->ll_newkey != NULL)
2737 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002738 if (op != NULL && *op != '=')
2739 {
2740 EMSG2(_(e_letwrong), op);
2741 return;
2742 }
2743
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002744 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002745 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002746 if (di == NULL)
2747 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002748 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2749 {
2750 vim_free(di);
2751 return;
2752 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002753 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002754 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002755 else if (op != NULL && *op != '=')
2756 {
2757 tv_op(lp->ll_tv, rettv, op);
2758 return;
2759 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002760 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002761 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002762
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002763 /*
2764 * Assign the value to the variable or list item.
2765 */
2766 if (copy)
2767 copy_tv(rettv, lp->ll_tv);
2768 else
2769 {
2770 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002771 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002772 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002773 }
2774 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002775}
2776
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002777/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002778 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2779 * Returns OK or FAIL.
2780 */
2781 static int
2782tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002783 typval_T *tv1;
2784 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002785 char_u *op;
2786{
2787 long n;
2788 char_u numbuf[NUMBUFLEN];
2789 char_u *s;
2790
2791 /* Can't do anything with a Funcref or a Dict on the right. */
2792 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2793 {
2794 switch (tv1->v_type)
2795 {
2796 case VAR_DICT:
2797 case VAR_FUNC:
2798 break;
2799
2800 case VAR_LIST:
2801 if (*op != '+' || tv2->v_type != VAR_LIST)
2802 break;
2803 /* List += List */
2804 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2805 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2806 return OK;
2807
2808 case VAR_NUMBER:
2809 case VAR_STRING:
2810 if (tv2->v_type == VAR_LIST)
2811 break;
2812 if (*op == '+' || *op == '-')
2813 {
2814 /* nr += nr or nr -= nr*/
2815 n = get_tv_number(tv1);
2816 if (*op == '+')
2817 n += get_tv_number(tv2);
2818 else
2819 n -= get_tv_number(tv2);
2820 clear_tv(tv1);
2821 tv1->v_type = VAR_NUMBER;
2822 tv1->vval.v_number = n;
2823 }
2824 else
2825 {
2826 /* str .= str */
2827 s = get_tv_string(tv1);
2828 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2829 clear_tv(tv1);
2830 tv1->v_type = VAR_STRING;
2831 tv1->vval.v_string = s;
2832 }
2833 return OK;
2834 }
2835 }
2836
2837 EMSG2(_(e_letwrong), op);
2838 return FAIL;
2839}
2840
2841/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002842 * Add a watcher to a list.
2843 */
2844 static void
2845list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002846 list_T *l;
2847 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002848{
2849 lw->lw_next = l->lv_watch;
2850 l->lv_watch = lw;
2851}
2852
2853/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002854 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002855 * No warning when it isn't found...
2856 */
2857 static void
2858list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002859 list_T *l;
2860 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002861{
Bram Moolenaar33570922005-01-25 22:26:29 +00002862 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002863
2864 lwp = &l->lv_watch;
2865 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2866 {
2867 if (lw == lwrem)
2868 {
2869 *lwp = lw->lw_next;
2870 break;
2871 }
2872 lwp = &lw->lw_next;
2873 }
2874}
2875
2876/*
2877 * Just before removing an item from a list: advance watchers to the next
2878 * item.
2879 */
2880 static void
2881list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002882 list_T *l;
2883 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002884{
Bram Moolenaar33570922005-01-25 22:26:29 +00002885 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002886
2887 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2888 if (lw->lw_item == item)
2889 lw->lw_item = item->li_next;
2890}
2891
2892/*
2893 * Evaluate the expression used in a ":for var in expr" command.
2894 * "arg" points to "var".
2895 * Set "*errp" to TRUE for an error, FALSE otherwise;
2896 * Return a pointer that holds the info. Null when there is an error.
2897 */
2898 void *
2899eval_for_line(arg, errp, nextcmdp, skip)
2900 char_u *arg;
2901 int *errp;
2902 char_u **nextcmdp;
2903 int skip;
2904{
Bram Moolenaar33570922005-01-25 22:26:29 +00002905 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002906 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002907 typval_T tv;
2908 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002909
2910 *errp = TRUE; /* default: there is an error */
2911
Bram Moolenaar33570922005-01-25 22:26:29 +00002912 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002913 if (fi == NULL)
2914 return NULL;
2915
2916 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2917 if (expr == NULL)
2918 return fi;
2919
2920 expr = skipwhite(expr);
2921 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2922 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002923 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002924 return fi;
2925 }
2926
2927 if (skip)
2928 ++emsg_skip;
2929 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2930 {
2931 *errp = FALSE;
2932 if (!skip)
2933 {
2934 l = tv.vval.v_list;
2935 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002936 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002937 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002938 clear_tv(&tv);
2939 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002940 else
2941 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002942 /* No need to increment the refcount, it's already set for the
2943 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002944 fi->fi_list = l;
2945 list_add_watch(l, &fi->fi_lw);
2946 fi->fi_lw.lw_item = l->lv_first;
2947 }
2948 }
2949 }
2950 if (skip)
2951 --emsg_skip;
2952
2953 return fi;
2954}
2955
2956/*
2957 * Use the first item in a ":for" list. Advance to the next.
2958 * Assign the values to the variable (list). "arg" points to the first one.
2959 * Return TRUE when a valid item was found, FALSE when at end of list or
2960 * something wrong.
2961 */
2962 int
2963next_for_item(fi_void, arg)
2964 void *fi_void;
2965 char_u *arg;
2966{
Bram Moolenaar33570922005-01-25 22:26:29 +00002967 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002968 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002969 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002970
2971 item = fi->fi_lw.lw_item;
2972 if (item == NULL)
2973 result = FALSE;
2974 else
2975 {
2976 fi->fi_lw.lw_item = item->li_next;
2977 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2978 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2979 }
2980 return result;
2981}
2982
2983/*
2984 * Free the structure used to store info used by ":for".
2985 */
2986 void
2987free_for_info(fi_void)
2988 void *fi_void;
2989{
Bram Moolenaar33570922005-01-25 22:26:29 +00002990 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002991
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002992 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002993 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002994 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002995 list_unref(fi->fi_list);
2996 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002997 vim_free(fi);
2998}
2999
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3001
3002 void
3003set_context_for_expression(xp, arg, cmdidx)
3004 expand_T *xp;
3005 char_u *arg;
3006 cmdidx_T cmdidx;
3007{
3008 int got_eq = FALSE;
3009 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003010 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003012 if (cmdidx == CMD_let)
3013 {
3014 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003015 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003016 {
3017 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003018 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003019 {
3020 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003021 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003022 if (vim_iswhite(*p))
3023 break;
3024 }
3025 return;
3026 }
3027 }
3028 else
3029 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3030 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031 while ((xp->xp_pattern = vim_strpbrk(arg,
3032 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3033 {
3034 c = *xp->xp_pattern;
3035 if (c == '&')
3036 {
3037 c = xp->xp_pattern[1];
3038 if (c == '&')
3039 {
3040 ++xp->xp_pattern;
3041 xp->xp_context = cmdidx != CMD_let || got_eq
3042 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3043 }
3044 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003045 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003047 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3048 xp->xp_pattern += 2;
3049
3050 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 }
3052 else if (c == '$')
3053 {
3054 /* environment variable */
3055 xp->xp_context = EXPAND_ENV_VARS;
3056 }
3057 else if (c == '=')
3058 {
3059 got_eq = TRUE;
3060 xp->xp_context = EXPAND_EXPRESSION;
3061 }
3062 else if (c == '<'
3063 && xp->xp_context == EXPAND_FUNCTIONS
3064 && vim_strchr(xp->xp_pattern, '(') == NULL)
3065 {
3066 /* Function name can start with "<SNR>" */
3067 break;
3068 }
3069 else if (cmdidx != CMD_let || got_eq)
3070 {
3071 if (c == '"') /* string */
3072 {
3073 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3074 if (c == '\\' && xp->xp_pattern[1] != NUL)
3075 ++xp->xp_pattern;
3076 xp->xp_context = EXPAND_NOTHING;
3077 }
3078 else if (c == '\'') /* literal string */
3079 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003080 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3082 /* skip */ ;
3083 xp->xp_context = EXPAND_NOTHING;
3084 }
3085 else if (c == '|')
3086 {
3087 if (xp->xp_pattern[1] == '|')
3088 {
3089 ++xp->xp_pattern;
3090 xp->xp_context = EXPAND_EXPRESSION;
3091 }
3092 else
3093 xp->xp_context = EXPAND_COMMANDS;
3094 }
3095 else
3096 xp->xp_context = EXPAND_EXPRESSION;
3097 }
3098 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003099 /* Doesn't look like something valid, expand as an expression
3100 * anyway. */
3101 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 arg = xp->xp_pattern;
3103 if (*arg != NUL)
3104 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3105 /* skip */ ;
3106 }
3107 xp->xp_pattern = arg;
3108}
3109
3110#endif /* FEAT_CMDL_COMPL */
3111
3112/*
3113 * ":1,25call func(arg1, arg2)" function call.
3114 */
3115 void
3116ex_call(eap)
3117 exarg_T *eap;
3118{
3119 char_u *arg = eap->arg;
3120 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003122 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003124 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 linenr_T lnum;
3126 int doesrange;
3127 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003128 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003130 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003131 if (fudi.fd_newkey != NULL)
3132 {
3133 /* Still need to give an error message for missing key. */
3134 EMSG2(_(e_dictkey), fudi.fd_newkey);
3135 vim_free(fudi.fd_newkey);
3136 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003137 if (tofree == NULL)
3138 return;
3139
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003140 /* Increase refcount on dictionary, it could get deleted when evaluating
3141 * the arguments. */
3142 if (fudi.fd_dict != NULL)
3143 ++fudi.fd_dict->dv_refcount;
3144
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003145 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003146 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003147 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148
Bram Moolenaar532c7802005-01-27 14:44:31 +00003149 /* Skip white space to allow ":call func ()". Not good, but required for
3150 * backward compatibility. */
3151 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003152 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153
3154 if (*startarg != '(')
3155 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003156 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 goto end;
3158 }
3159
3160 /*
3161 * When skipping, evaluate the function once, to find the end of the
3162 * arguments.
3163 * When the function takes a range, this is discovered after the first
3164 * call, and the loop is broken.
3165 */
3166 if (eap->skip)
3167 {
3168 ++emsg_skip;
3169 lnum = eap->line2; /* do it once, also with an invalid range */
3170 }
3171 else
3172 lnum = eap->line1;
3173 for ( ; lnum <= eap->line2; ++lnum)
3174 {
3175 if (!eap->skip && eap->addr_count > 0)
3176 {
3177 curwin->w_cursor.lnum = lnum;
3178 curwin->w_cursor.col = 0;
3179 }
3180 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003181 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003182 eap->line1, eap->line2, &doesrange,
3183 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 {
3185 failed = TRUE;
3186 break;
3187 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003188 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 if (doesrange || eap->skip)
3190 break;
3191 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003192 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003193 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003194 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 if (aborting())
3196 break;
3197 }
3198 if (eap->skip)
3199 --emsg_skip;
3200
3201 if (!failed)
3202 {
3203 /* Check for trailing illegal characters and a following command. */
3204 if (!ends_excmd(*arg))
3205 {
3206 emsg_severe = TRUE;
3207 EMSG(_(e_trailing));
3208 }
3209 else
3210 eap->nextcmd = check_nextcmd(arg);
3211 }
3212
3213end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003214 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003215 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216}
3217
3218/*
3219 * ":unlet[!] var1 ... " command.
3220 */
3221 void
3222ex_unlet(eap)
3223 exarg_T *eap;
3224{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003225 ex_unletlock(eap, eap->arg, 0);
3226}
3227
3228/*
3229 * ":lockvar" and ":unlockvar" commands
3230 */
3231 void
3232ex_lockvar(eap)
3233 exarg_T *eap;
3234{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003236 int deep = 2;
3237
3238 if (eap->forceit)
3239 deep = -1;
3240 else if (vim_isdigit(*arg))
3241 {
3242 deep = getdigits(&arg);
3243 arg = skipwhite(arg);
3244 }
3245
3246 ex_unletlock(eap, arg, deep);
3247}
3248
3249/*
3250 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3251 */
3252 static void
3253ex_unletlock(eap, argstart, deep)
3254 exarg_T *eap;
3255 char_u *argstart;
3256 int deep;
3257{
3258 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003261 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262
3263 do
3264 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003265 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003266 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3267 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003268 if (lv.ll_name == NULL)
3269 error = TRUE; /* error but continue parsing */
3270 if (name_end == NULL || (!vim_iswhite(*name_end)
3271 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003273 if (name_end != NULL)
3274 {
3275 emsg_severe = TRUE;
3276 EMSG(_(e_trailing));
3277 }
3278 if (!(eap->skip || error))
3279 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 break;
3281 }
3282
3283 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003284 {
3285 if (eap->cmdidx == CMD_unlet)
3286 {
3287 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3288 error = TRUE;
3289 }
3290 else
3291 {
3292 if (do_lock_var(&lv, name_end, deep,
3293 eap->cmdidx == CMD_lockvar) == FAIL)
3294 error = TRUE;
3295 }
3296 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003298 if (!eap->skip)
3299 clear_lval(&lv);
3300
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 arg = skipwhite(name_end);
3302 } while (!ends_excmd(*arg));
3303
3304 eap->nextcmd = check_nextcmd(arg);
3305}
3306
Bram Moolenaar8c711452005-01-14 21:53:12 +00003307 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003308do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003309 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003310 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003311 int forceit;
3312{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003313 int ret = OK;
3314 int cc;
3315
3316 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003317 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003318 cc = *name_end;
3319 *name_end = NUL;
3320
3321 /* Normal name or expanded name. */
3322 if (check_changedtick(lp->ll_name))
3323 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003324 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003325 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003326 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003327 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003328 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3329 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003330 else if (lp->ll_range)
3331 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003332 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003333
3334 /* Delete a range of List items. */
3335 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3336 {
3337 li = lp->ll_li->li_next;
3338 listitem_remove(lp->ll_list, lp->ll_li);
3339 lp->ll_li = li;
3340 ++lp->ll_n1;
3341 }
3342 }
3343 else
3344 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003345 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003346 /* unlet a List item. */
3347 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003348 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003349 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003350 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003351 }
3352
3353 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003354}
3355
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356/*
3357 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003358 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 */
3360 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003361do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003363 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364{
Bram Moolenaar33570922005-01-25 22:26:29 +00003365 hashtab_T *ht;
3366 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003367 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368
Bram Moolenaar33570922005-01-25 22:26:29 +00003369 ht = find_var_ht(name, &varname);
3370 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003372 hi = hash_find(ht, varname);
3373 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003374 {
Bram Moolenaar4e957af2006-09-02 11:41:07 +00003375 if (var_check_fixed(HI2DI(hi)->di_flags, name))
3376 return FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003377 if (var_check_ro(HI2DI(hi)->di_flags, name))
3378 return FAIL;
3379 delete_var(ht, hi);
3380 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003383 if (forceit)
3384 return OK;
3385 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 return FAIL;
3387}
3388
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003389/*
3390 * Lock or unlock variable indicated by "lp".
3391 * "deep" is the levels to go (-1 for unlimited);
3392 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3393 */
3394 static int
3395do_lock_var(lp, name_end, deep, lock)
3396 lval_T *lp;
3397 char_u *name_end;
3398 int deep;
3399 int lock;
3400{
3401 int ret = OK;
3402 int cc;
3403 dictitem_T *di;
3404
3405 if (deep == 0) /* nothing to do */
3406 return OK;
3407
3408 if (lp->ll_tv == NULL)
3409 {
3410 cc = *name_end;
3411 *name_end = NUL;
3412
3413 /* Normal name or expanded name. */
3414 if (check_changedtick(lp->ll_name))
3415 ret = FAIL;
3416 else
3417 {
3418 di = find_var(lp->ll_name, NULL);
3419 if (di == NULL)
3420 ret = FAIL;
3421 else
3422 {
3423 if (lock)
3424 di->di_flags |= DI_FLAGS_LOCK;
3425 else
3426 di->di_flags &= ~DI_FLAGS_LOCK;
3427 item_lock(&di->di_tv, deep, lock);
3428 }
3429 }
3430 *name_end = cc;
3431 }
3432 else if (lp->ll_range)
3433 {
3434 listitem_T *li = lp->ll_li;
3435
3436 /* (un)lock a range of List items. */
3437 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3438 {
3439 item_lock(&li->li_tv, deep, lock);
3440 li = li->li_next;
3441 ++lp->ll_n1;
3442 }
3443 }
3444 else if (lp->ll_list != NULL)
3445 /* (un)lock a List item. */
3446 item_lock(&lp->ll_li->li_tv, deep, lock);
3447 else
3448 /* un(lock) a Dictionary item. */
3449 item_lock(&lp->ll_di->di_tv, deep, lock);
3450
3451 return ret;
3452}
3453
3454/*
3455 * Lock or unlock an item. "deep" is nr of levels to go.
3456 */
3457 static void
3458item_lock(tv, deep, lock)
3459 typval_T *tv;
3460 int deep;
3461 int lock;
3462{
3463 static int recurse = 0;
3464 list_T *l;
3465 listitem_T *li;
3466 dict_T *d;
3467 hashitem_T *hi;
3468 int todo;
3469
3470 if (recurse >= DICT_MAXNEST)
3471 {
3472 EMSG(_("E743: variable nested too deep for (un)lock"));
3473 return;
3474 }
3475 if (deep == 0)
3476 return;
3477 ++recurse;
3478
3479 /* lock/unlock the item itself */
3480 if (lock)
3481 tv->v_lock |= VAR_LOCKED;
3482 else
3483 tv->v_lock &= ~VAR_LOCKED;
3484
3485 switch (tv->v_type)
3486 {
3487 case VAR_LIST:
3488 if ((l = tv->vval.v_list) != NULL)
3489 {
3490 if (lock)
3491 l->lv_lock |= VAR_LOCKED;
3492 else
3493 l->lv_lock &= ~VAR_LOCKED;
3494 if (deep < 0 || deep > 1)
3495 /* recursive: lock/unlock the items the List contains */
3496 for (li = l->lv_first; li != NULL; li = li->li_next)
3497 item_lock(&li->li_tv, deep - 1, lock);
3498 }
3499 break;
3500 case VAR_DICT:
3501 if ((d = tv->vval.v_dict) != NULL)
3502 {
3503 if (lock)
3504 d->dv_lock |= VAR_LOCKED;
3505 else
3506 d->dv_lock &= ~VAR_LOCKED;
3507 if (deep < 0 || deep > 1)
3508 {
3509 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003510 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003511 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3512 {
3513 if (!HASHITEM_EMPTY(hi))
3514 {
3515 --todo;
3516 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3517 }
3518 }
3519 }
3520 }
3521 }
3522 --recurse;
3523}
3524
Bram Moolenaara40058a2005-07-11 22:42:07 +00003525/*
3526 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3527 * it refers to a List or Dictionary that is locked.
3528 */
3529 static int
3530tv_islocked(tv)
3531 typval_T *tv;
3532{
3533 return (tv->v_lock & VAR_LOCKED)
3534 || (tv->v_type == VAR_LIST
3535 && tv->vval.v_list != NULL
3536 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3537 || (tv->v_type == VAR_DICT
3538 && tv->vval.v_dict != NULL
3539 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3540}
3541
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3543/*
3544 * Delete all "menutrans_" variables.
3545 */
3546 void
3547del_menutrans_vars()
3548{
Bram Moolenaar33570922005-01-25 22:26:29 +00003549 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003550 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551
Bram Moolenaar33570922005-01-25 22:26:29 +00003552 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003553 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003554 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003555 {
3556 if (!HASHITEM_EMPTY(hi))
3557 {
3558 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003559 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3560 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003561 }
3562 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003563 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564}
3565#endif
3566
3567#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3568
3569/*
3570 * Local string buffer for the next two functions to store a variable name
3571 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3572 * get_user_var_name().
3573 */
3574
3575static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3576
3577static char_u *varnamebuf = NULL;
3578static int varnamebuflen = 0;
3579
3580/*
3581 * Function to concatenate a prefix and a variable name.
3582 */
3583 static char_u *
3584cat_prefix_varname(prefix, name)
3585 int prefix;
3586 char_u *name;
3587{
3588 int len;
3589
3590 len = (int)STRLEN(name) + 3;
3591 if (len > varnamebuflen)
3592 {
3593 vim_free(varnamebuf);
3594 len += 10; /* some additional space */
3595 varnamebuf = alloc(len);
3596 if (varnamebuf == NULL)
3597 {
3598 varnamebuflen = 0;
3599 return NULL;
3600 }
3601 varnamebuflen = len;
3602 }
3603 *varnamebuf = prefix;
3604 varnamebuf[1] = ':';
3605 STRCPY(varnamebuf + 2, name);
3606 return varnamebuf;
3607}
3608
3609/*
3610 * Function given to ExpandGeneric() to obtain the list of user defined
3611 * (global/buffer/window/built-in) variable names.
3612 */
3613/*ARGSUSED*/
3614 char_u *
3615get_user_var_name(xp, idx)
3616 expand_T *xp;
3617 int idx;
3618{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003619 static long_u gdone;
3620 static long_u bdone;
3621 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003622#ifdef FEAT_WINDOWS
3623 static long_u tdone;
3624#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003625 static int vidx;
3626 static hashitem_T *hi;
3627 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628
3629 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003630 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003631 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003632#ifdef FEAT_WINDOWS
3633 tdone = 0;
3634#endif
3635 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003636
3637 /* Global variables */
3638 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003640 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003641 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003642 else
3643 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003644 while (HASHITEM_EMPTY(hi))
3645 ++hi;
3646 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3647 return cat_prefix_varname('g', hi->hi_key);
3648 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003650
3651 /* b: variables */
3652 ht = &curbuf->b_vars.dv_hashtab;
3653 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003655 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003656 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003657 else
3658 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003659 while (HASHITEM_EMPTY(hi))
3660 ++hi;
3661 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003663 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003665 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 return (char_u *)"b:changedtick";
3667 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003668
3669 /* w: variables */
3670 ht = &curwin->w_vars.dv_hashtab;
3671 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003673 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003674 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003675 else
3676 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003677 while (HASHITEM_EMPTY(hi))
3678 ++hi;
3679 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003681
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003682#ifdef FEAT_WINDOWS
3683 /* t: variables */
3684 ht = &curtab->tp_vars.dv_hashtab;
3685 if (tdone < ht->ht_used)
3686 {
3687 if (tdone++ == 0)
3688 hi = ht->ht_array;
3689 else
3690 ++hi;
3691 while (HASHITEM_EMPTY(hi))
3692 ++hi;
3693 return cat_prefix_varname('t', hi->hi_key);
3694 }
3695#endif
3696
Bram Moolenaar33570922005-01-25 22:26:29 +00003697 /* v: variables */
3698 if (vidx < VV_LEN)
3699 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700
3701 vim_free(varnamebuf);
3702 varnamebuf = NULL;
3703 varnamebuflen = 0;
3704 return NULL;
3705}
3706
3707#endif /* FEAT_CMDL_COMPL */
3708
3709/*
3710 * types for expressions.
3711 */
3712typedef enum
3713{
3714 TYPE_UNKNOWN = 0
3715 , TYPE_EQUAL /* == */
3716 , TYPE_NEQUAL /* != */
3717 , TYPE_GREATER /* > */
3718 , TYPE_GEQUAL /* >= */
3719 , TYPE_SMALLER /* < */
3720 , TYPE_SEQUAL /* <= */
3721 , TYPE_MATCH /* =~ */
3722 , TYPE_NOMATCH /* !~ */
3723} exptype_T;
3724
3725/*
3726 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003727 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3729 */
3730
3731/*
3732 * Handle zero level expression.
3733 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003734 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003735 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 * Return OK or FAIL.
3737 */
3738 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003739eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003741 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 char_u **nextcmd;
3743 int evaluate;
3744{
3745 int ret;
3746 char_u *p;
3747
3748 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003749 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 if (ret == FAIL || !ends_excmd(*p))
3751 {
3752 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003753 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 /*
3755 * Report the invalid expression unless the expression evaluation has
3756 * been cancelled due to an aborting error, an interrupt, or an
3757 * exception.
3758 */
3759 if (!aborting())
3760 EMSG2(_(e_invexpr2), arg);
3761 ret = FAIL;
3762 }
3763 if (nextcmd != NULL)
3764 *nextcmd = check_nextcmd(p);
3765
3766 return ret;
3767}
3768
3769/*
3770 * Handle top level expression:
3771 * expr1 ? expr0 : expr0
3772 *
3773 * "arg" must point to the first non-white of the expression.
3774 * "arg" is advanced to the next non-white after the recognized expression.
3775 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003776 * Note: "rettv.v_lock" is not set.
3777 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 * Return OK or FAIL.
3779 */
3780 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003781eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003783 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 int evaluate;
3785{
3786 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003787 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788
3789 /*
3790 * Get the first variable.
3791 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003792 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 return FAIL;
3794
3795 if ((*arg)[0] == '?')
3796 {
3797 result = FALSE;
3798 if (evaluate)
3799 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003800 int error = FALSE;
3801
3802 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003804 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003805 if (error)
3806 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 }
3808
3809 /*
3810 * Get the second variable.
3811 */
3812 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003813 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 return FAIL;
3815
3816 /*
3817 * Check for the ":".
3818 */
3819 if ((*arg)[0] != ':')
3820 {
3821 EMSG(_("E109: Missing ':' after '?'"));
3822 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003823 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 return FAIL;
3825 }
3826
3827 /*
3828 * Get the third variable.
3829 */
3830 *arg = skipwhite(*arg + 1);
3831 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3832 {
3833 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003834 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 return FAIL;
3836 }
3837 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003838 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 }
3840
3841 return OK;
3842}
3843
3844/*
3845 * Handle first level expression:
3846 * expr2 || expr2 || expr2 logical OR
3847 *
3848 * "arg" must point to the first non-white of the expression.
3849 * "arg" is advanced to the next non-white after the recognized expression.
3850 *
3851 * Return OK or FAIL.
3852 */
3853 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003854eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003856 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 int evaluate;
3858{
Bram Moolenaar33570922005-01-25 22:26:29 +00003859 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 long result;
3861 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003862 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863
3864 /*
3865 * Get the first variable.
3866 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003867 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 return FAIL;
3869
3870 /*
3871 * Repeat until there is no following "||".
3872 */
3873 first = TRUE;
3874 result = FALSE;
3875 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3876 {
3877 if (evaluate && first)
3878 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003879 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003881 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003882 if (error)
3883 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 first = FALSE;
3885 }
3886
3887 /*
3888 * Get the second variable.
3889 */
3890 *arg = skipwhite(*arg + 2);
3891 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3892 return FAIL;
3893
3894 /*
3895 * Compute the result.
3896 */
3897 if (evaluate && !result)
3898 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003899 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003901 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003902 if (error)
3903 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 }
3905 if (evaluate)
3906 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003907 rettv->v_type = VAR_NUMBER;
3908 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 }
3910 }
3911
3912 return OK;
3913}
3914
3915/*
3916 * Handle second level expression:
3917 * expr3 && expr3 && expr3 logical AND
3918 *
3919 * "arg" must point to the first non-white of the expression.
3920 * "arg" is advanced to the next non-white after the recognized expression.
3921 *
3922 * Return OK or FAIL.
3923 */
3924 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003925eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003927 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 int evaluate;
3929{
Bram Moolenaar33570922005-01-25 22:26:29 +00003930 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 long result;
3932 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003933 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934
3935 /*
3936 * Get the first variable.
3937 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003938 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 return FAIL;
3940
3941 /*
3942 * Repeat until there is no following "&&".
3943 */
3944 first = TRUE;
3945 result = TRUE;
3946 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3947 {
3948 if (evaluate && first)
3949 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003950 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003952 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003953 if (error)
3954 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 first = FALSE;
3956 }
3957
3958 /*
3959 * Get the second variable.
3960 */
3961 *arg = skipwhite(*arg + 2);
3962 if (eval4(arg, &var2, evaluate && result) == FAIL)
3963 return FAIL;
3964
3965 /*
3966 * Compute the result.
3967 */
3968 if (evaluate && result)
3969 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003970 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003972 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003973 if (error)
3974 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 }
3976 if (evaluate)
3977 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003978 rettv->v_type = VAR_NUMBER;
3979 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 }
3981 }
3982
3983 return OK;
3984}
3985
3986/*
3987 * Handle third level expression:
3988 * var1 == var2
3989 * var1 =~ var2
3990 * var1 != var2
3991 * var1 !~ var2
3992 * var1 > var2
3993 * var1 >= var2
3994 * var1 < var2
3995 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003996 * var1 is var2
3997 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 *
3999 * "arg" must point to the first non-white of the expression.
4000 * "arg" is advanced to the next non-white after the recognized expression.
4001 *
4002 * Return OK or FAIL.
4003 */
4004 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004005eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004007 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 int evaluate;
4009{
Bram Moolenaar33570922005-01-25 22:26:29 +00004010 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 char_u *p;
4012 int i;
4013 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004014 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 int len = 2;
4016 long n1, n2;
4017 char_u *s1, *s2;
4018 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4019 regmatch_T regmatch;
4020 int ic;
4021 char_u *save_cpo;
4022
4023 /*
4024 * Get the first variable.
4025 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004026 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 return FAIL;
4028
4029 p = *arg;
4030 switch (p[0])
4031 {
4032 case '=': if (p[1] == '=')
4033 type = TYPE_EQUAL;
4034 else if (p[1] == '~')
4035 type = TYPE_MATCH;
4036 break;
4037 case '!': if (p[1] == '=')
4038 type = TYPE_NEQUAL;
4039 else if (p[1] == '~')
4040 type = TYPE_NOMATCH;
4041 break;
4042 case '>': if (p[1] != '=')
4043 {
4044 type = TYPE_GREATER;
4045 len = 1;
4046 }
4047 else
4048 type = TYPE_GEQUAL;
4049 break;
4050 case '<': if (p[1] != '=')
4051 {
4052 type = TYPE_SMALLER;
4053 len = 1;
4054 }
4055 else
4056 type = TYPE_SEQUAL;
4057 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004058 case 'i': if (p[1] == 's')
4059 {
4060 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4061 len = 5;
4062 if (!vim_isIDc(p[len]))
4063 {
4064 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4065 type_is = TRUE;
4066 }
4067 }
4068 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 }
4070
4071 /*
4072 * If there is a comparitive operator, use it.
4073 */
4074 if (type != TYPE_UNKNOWN)
4075 {
4076 /* extra question mark appended: ignore case */
4077 if (p[len] == '?')
4078 {
4079 ic = TRUE;
4080 ++len;
4081 }
4082 /* extra '#' appended: match case */
4083 else if (p[len] == '#')
4084 {
4085 ic = FALSE;
4086 ++len;
4087 }
4088 /* nothing appened: use 'ignorecase' */
4089 else
4090 ic = p_ic;
4091
4092 /*
4093 * Get the second variable.
4094 */
4095 *arg = skipwhite(p + len);
4096 if (eval5(arg, &var2, evaluate) == FAIL)
4097 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004098 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 return FAIL;
4100 }
4101
4102 if (evaluate)
4103 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004104 if (type_is && rettv->v_type != var2.v_type)
4105 {
4106 /* For "is" a different type always means FALSE, for "notis"
4107 * it means TRUE. */
4108 n1 = (type == TYPE_NEQUAL);
4109 }
4110 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4111 {
4112 if (type_is)
4113 {
4114 n1 = (rettv->v_type == var2.v_type
4115 && rettv->vval.v_list == var2.vval.v_list);
4116 if (type == TYPE_NEQUAL)
4117 n1 = !n1;
4118 }
4119 else if (rettv->v_type != var2.v_type
4120 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4121 {
4122 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004123 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004124 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004125 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004126 clear_tv(rettv);
4127 clear_tv(&var2);
4128 return FAIL;
4129 }
4130 else
4131 {
4132 /* Compare two Lists for being equal or unequal. */
4133 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4134 if (type == TYPE_NEQUAL)
4135 n1 = !n1;
4136 }
4137 }
4138
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004139 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4140 {
4141 if (type_is)
4142 {
4143 n1 = (rettv->v_type == var2.v_type
4144 && rettv->vval.v_dict == var2.vval.v_dict);
4145 if (type == TYPE_NEQUAL)
4146 n1 = !n1;
4147 }
4148 else if (rettv->v_type != var2.v_type
4149 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4150 {
4151 if (rettv->v_type != var2.v_type)
4152 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4153 else
4154 EMSG(_("E736: Invalid operation for Dictionary"));
4155 clear_tv(rettv);
4156 clear_tv(&var2);
4157 return FAIL;
4158 }
4159 else
4160 {
4161 /* Compare two Dictionaries for being equal or unequal. */
4162 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4163 if (type == TYPE_NEQUAL)
4164 n1 = !n1;
4165 }
4166 }
4167
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004168 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4169 {
4170 if (rettv->v_type != var2.v_type
4171 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4172 {
4173 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004174 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004175 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004176 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004177 clear_tv(rettv);
4178 clear_tv(&var2);
4179 return FAIL;
4180 }
4181 else
4182 {
4183 /* Compare two Funcrefs for being equal or unequal. */
4184 if (rettv->vval.v_string == NULL
4185 || var2.vval.v_string == NULL)
4186 n1 = FALSE;
4187 else
4188 n1 = STRCMP(rettv->vval.v_string,
4189 var2.vval.v_string) == 0;
4190 if (type == TYPE_NEQUAL)
4191 n1 = !n1;
4192 }
4193 }
4194
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 /*
4196 * If one of the two variables is a number, compare as a number.
4197 * When using "=~" or "!~", always compare as string.
4198 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004199 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4201 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004202 n1 = get_tv_number(rettv);
4203 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 switch (type)
4205 {
4206 case TYPE_EQUAL: n1 = (n1 == n2); break;
4207 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4208 case TYPE_GREATER: n1 = (n1 > n2); break;
4209 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4210 case TYPE_SMALLER: n1 = (n1 < n2); break;
4211 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4212 case TYPE_UNKNOWN:
4213 case TYPE_MATCH:
4214 case TYPE_NOMATCH: break; /* avoid gcc warning */
4215 }
4216 }
4217 else
4218 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004219 s1 = get_tv_string_buf(rettv, buf1);
4220 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4222 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4223 else
4224 i = 0;
4225 n1 = FALSE;
4226 switch (type)
4227 {
4228 case TYPE_EQUAL: n1 = (i == 0); break;
4229 case TYPE_NEQUAL: n1 = (i != 0); break;
4230 case TYPE_GREATER: n1 = (i > 0); break;
4231 case TYPE_GEQUAL: n1 = (i >= 0); break;
4232 case TYPE_SMALLER: n1 = (i < 0); break;
4233 case TYPE_SEQUAL: n1 = (i <= 0); break;
4234
4235 case TYPE_MATCH:
4236 case TYPE_NOMATCH:
4237 /* avoid 'l' flag in 'cpoptions' */
4238 save_cpo = p_cpo;
4239 p_cpo = (char_u *)"";
4240 regmatch.regprog = vim_regcomp(s2,
4241 RE_MAGIC + RE_STRING);
4242 regmatch.rm_ic = ic;
4243 if (regmatch.regprog != NULL)
4244 {
4245 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4246 vim_free(regmatch.regprog);
4247 if (type == TYPE_NOMATCH)
4248 n1 = !n1;
4249 }
4250 p_cpo = save_cpo;
4251 break;
4252
4253 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4254 }
4255 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004256 clear_tv(rettv);
4257 clear_tv(&var2);
4258 rettv->v_type = VAR_NUMBER;
4259 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 }
4261 }
4262
4263 return OK;
4264}
4265
4266/*
4267 * Handle fourth level expression:
4268 * + number addition
4269 * - number subtraction
4270 * . string concatenation
4271 *
4272 * "arg" must point to the first non-white of the expression.
4273 * "arg" is advanced to the next non-white after the recognized expression.
4274 *
4275 * Return OK or FAIL.
4276 */
4277 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004278eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004280 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 int evaluate;
4282{
Bram Moolenaar33570922005-01-25 22:26:29 +00004283 typval_T var2;
4284 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 int op;
4286 long n1, n2;
4287 char_u *s1, *s2;
4288 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4289 char_u *p;
4290
4291 /*
4292 * Get the first variable.
4293 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004294 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 return FAIL;
4296
4297 /*
4298 * Repeat computing, until no '+', '-' or '.' is following.
4299 */
4300 for (;;)
4301 {
4302 op = **arg;
4303 if (op != '+' && op != '-' && op != '.')
4304 break;
4305
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004306 if (op != '+' || rettv->v_type != VAR_LIST)
4307 {
4308 /* For "list + ...", an illegal use of the first operand as
4309 * a number cannot be determined before evaluating the 2nd
4310 * operand: if this is also a list, all is ok.
4311 * For "something . ...", "something - ..." or "non-list + ...",
4312 * we know that the first operand needs to be a string or number
4313 * without evaluating the 2nd operand. So check before to avoid
4314 * side effects after an error. */
4315 if (evaluate && get_tv_string_chk(rettv) == NULL)
4316 {
4317 clear_tv(rettv);
4318 return FAIL;
4319 }
4320 }
4321
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 /*
4323 * Get the second variable.
4324 */
4325 *arg = skipwhite(*arg + 1);
4326 if (eval6(arg, &var2, evaluate) == FAIL)
4327 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004328 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 return FAIL;
4330 }
4331
4332 if (evaluate)
4333 {
4334 /*
4335 * Compute the result.
4336 */
4337 if (op == '.')
4338 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004339 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4340 s2 = get_tv_string_buf_chk(&var2, buf2);
4341 if (s2 == NULL) /* type error ? */
4342 {
4343 clear_tv(rettv);
4344 clear_tv(&var2);
4345 return FAIL;
4346 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004347 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004348 clear_tv(rettv);
4349 rettv->v_type = VAR_STRING;
4350 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004352 else if (op == '+' && rettv->v_type == VAR_LIST
4353 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004354 {
4355 /* concatenate Lists */
4356 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4357 &var3) == FAIL)
4358 {
4359 clear_tv(rettv);
4360 clear_tv(&var2);
4361 return FAIL;
4362 }
4363 clear_tv(rettv);
4364 *rettv = var3;
4365 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 else
4367 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004368 int error = FALSE;
4369
4370 n1 = get_tv_number_chk(rettv, &error);
4371 if (error)
4372 {
4373 /* This can only happen for "list + non-list".
4374 * For "non-list + ..." or "something - ...", we returned
4375 * before evaluating the 2nd operand. */
4376 clear_tv(rettv);
4377 return FAIL;
4378 }
4379 n2 = get_tv_number_chk(&var2, &error);
4380 if (error)
4381 {
4382 clear_tv(rettv);
4383 clear_tv(&var2);
4384 return FAIL;
4385 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004386 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 if (op == '+')
4388 n1 = n1 + n2;
4389 else
4390 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004391 rettv->v_type = VAR_NUMBER;
4392 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004394 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 }
4396 }
4397 return OK;
4398}
4399
4400/*
4401 * Handle fifth level expression:
4402 * * number multiplication
4403 * / number division
4404 * % number modulo
4405 *
4406 * "arg" must point to the first non-white of the expression.
4407 * "arg" is advanced to the next non-white after the recognized expression.
4408 *
4409 * Return OK or FAIL.
4410 */
4411 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004412eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004414 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 int evaluate;
4416{
Bram Moolenaar33570922005-01-25 22:26:29 +00004417 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 int op;
4419 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004420 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421
4422 /*
4423 * Get the first variable.
4424 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004425 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 return FAIL;
4427
4428 /*
4429 * Repeat computing, until no '*', '/' or '%' is following.
4430 */
4431 for (;;)
4432 {
4433 op = **arg;
4434 if (op != '*' && op != '/' && op != '%')
4435 break;
4436
4437 if (evaluate)
4438 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004439 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004440 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004441 if (error)
4442 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 }
4444 else
4445 n1 = 0;
4446
4447 /*
4448 * Get the second variable.
4449 */
4450 *arg = skipwhite(*arg + 1);
4451 if (eval7(arg, &var2, evaluate) == FAIL)
4452 return FAIL;
4453
4454 if (evaluate)
4455 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004456 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004457 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004458 if (error)
4459 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460
4461 /*
4462 * Compute the result.
4463 */
4464 if (op == '*')
4465 n1 = n1 * n2;
4466 else if (op == '/')
4467 {
4468 if (n2 == 0) /* give an error message? */
4469 n1 = 0x7fffffffL;
4470 else
4471 n1 = n1 / n2;
4472 }
4473 else
4474 {
4475 if (n2 == 0) /* give an error message? */
4476 n1 = 0;
4477 else
4478 n1 = n1 % n2;
4479 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004480 rettv->v_type = VAR_NUMBER;
4481 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 }
4483 }
4484
4485 return OK;
4486}
4487
4488/*
4489 * Handle sixth level expression:
4490 * number number constant
4491 * "string" string contstant
4492 * 'string' literal string contstant
4493 * &option-name option value
4494 * @r register contents
4495 * identifier variable value
4496 * function() function call
4497 * $VAR environment variable
4498 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004499 * [expr, expr] List
4500 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 *
4502 * Also handle:
4503 * ! in front logical NOT
4504 * - in front unary minus
4505 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004506 * trailing [] subscript in String or List
4507 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 *
4509 * "arg" must point to the first non-white of the expression.
4510 * "arg" is advanced to the next non-white after the recognized expression.
4511 *
4512 * Return OK or FAIL.
4513 */
4514 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004515eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004517 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 int evaluate;
4519{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 long n;
4521 int len;
4522 char_u *s;
4523 int val;
4524 char_u *start_leader, *end_leader;
4525 int ret = OK;
4526 char_u *alias;
4527
4528 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004529 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004530 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004532 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533
4534 /*
4535 * Skip '!' and '-' characters. They are handled later.
4536 */
4537 start_leader = *arg;
4538 while (**arg == '!' || **arg == '-' || **arg == '+')
4539 *arg = skipwhite(*arg + 1);
4540 end_leader = *arg;
4541
4542 switch (**arg)
4543 {
4544 /*
4545 * Number constant.
4546 */
4547 case '0':
4548 case '1':
4549 case '2':
4550 case '3':
4551 case '4':
4552 case '5':
4553 case '6':
4554 case '7':
4555 case '8':
4556 case '9':
4557 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4558 *arg += len;
4559 if (evaluate)
4560 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004561 rettv->v_type = VAR_NUMBER;
4562 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 }
4564 break;
4565
4566 /*
4567 * String constant: "string".
4568 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004569 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 break;
4571
4572 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004573 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004575 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004576 break;
4577
4578 /*
4579 * List: [expr, expr]
4580 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004581 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 break;
4583
4584 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004585 * Dictionary: {key: val, key: val}
4586 */
4587 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4588 break;
4589
4590 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004591 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004593 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 break;
4595
4596 /*
4597 * Environment variable: $VAR.
4598 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004599 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 break;
4601
4602 /*
4603 * Register contents: @r.
4604 */
4605 case '@': ++*arg;
4606 if (evaluate)
4607 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004608 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004609 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 }
4611 if (**arg != NUL)
4612 ++*arg;
4613 break;
4614
4615 /*
4616 * nested expression: (expression).
4617 */
4618 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004619 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 if (**arg == ')')
4621 ++*arg;
4622 else if (ret == OK)
4623 {
4624 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004625 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 ret = FAIL;
4627 }
4628 break;
4629
Bram Moolenaar8c711452005-01-14 21:53:12 +00004630 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 break;
4632 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004633
4634 if (ret == NOTDONE)
4635 {
4636 /*
4637 * Must be a variable or function name.
4638 * Can also be a curly-braces kind of name: {expr}.
4639 */
4640 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004641 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004642 if (alias != NULL)
4643 s = alias;
4644
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004645 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004646 ret = FAIL;
4647 else
4648 {
4649 if (**arg == '(') /* recursive! */
4650 {
4651 /* If "s" is the name of a variable of type VAR_FUNC
4652 * use its contents. */
4653 s = deref_func_name(s, &len);
4654
4655 /* Invoke the function. */
4656 ret = get_func_tv(s, len, rettv, arg,
4657 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004658 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004659 /* Stop the expression evaluation when immediately
4660 * aborting on error, or when an interrupt occurred or
4661 * an exception was thrown but not caught. */
4662 if (aborting())
4663 {
4664 if (ret == OK)
4665 clear_tv(rettv);
4666 ret = FAIL;
4667 }
4668 }
4669 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004670 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004671 else
4672 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004673 }
4674
4675 if (alias != NULL)
4676 vim_free(alias);
4677 }
4678
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 *arg = skipwhite(*arg);
4680
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004681 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4682 * expr(expr). */
4683 if (ret == OK)
4684 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685
4686 /*
4687 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4688 */
4689 if (ret == OK && evaluate && end_leader > start_leader)
4690 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004691 int error = FALSE;
4692
4693 val = get_tv_number_chk(rettv, &error);
4694 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004696 clear_tv(rettv);
4697 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004699 else
4700 {
4701 while (end_leader > start_leader)
4702 {
4703 --end_leader;
4704 if (*end_leader == '!')
4705 val = !val;
4706 else if (*end_leader == '-')
4707 val = -val;
4708 }
4709 clear_tv(rettv);
4710 rettv->v_type = VAR_NUMBER;
4711 rettv->vval.v_number = val;
4712 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 }
4714
4715 return ret;
4716}
4717
4718/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004719 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4720 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004721 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4722 */
4723 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004724eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004725 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004726 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004727 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004728 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004729{
4730 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004731 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004732 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004733 long len = -1;
4734 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004735 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004736 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004737
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004738 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004739 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004740 if (verbose)
4741 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004742 return FAIL;
4743 }
4744
Bram Moolenaar8c711452005-01-14 21:53:12 +00004745 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004746 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004747 /*
4748 * dict.name
4749 */
4750 key = *arg + 1;
4751 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4752 ;
4753 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004754 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004755 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004756 }
4757 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004758 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004759 /*
4760 * something[idx]
4761 *
4762 * Get the (first) variable from inside the [].
4763 */
4764 *arg = skipwhite(*arg + 1);
4765 if (**arg == ':')
4766 empty1 = TRUE;
4767 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4768 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004769 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4770 {
4771 /* not a number or string */
4772 clear_tv(&var1);
4773 return FAIL;
4774 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004775
4776 /*
4777 * Get the second variable from inside the [:].
4778 */
4779 if (**arg == ':')
4780 {
4781 range = TRUE;
4782 *arg = skipwhite(*arg + 1);
4783 if (**arg == ']')
4784 empty2 = TRUE;
4785 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4786 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004787 if (!empty1)
4788 clear_tv(&var1);
4789 return FAIL;
4790 }
4791 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4792 {
4793 /* not a number or string */
4794 if (!empty1)
4795 clear_tv(&var1);
4796 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004797 return FAIL;
4798 }
4799 }
4800
4801 /* Check for the ']'. */
4802 if (**arg != ']')
4803 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004804 if (verbose)
4805 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004806 clear_tv(&var1);
4807 if (range)
4808 clear_tv(&var2);
4809 return FAIL;
4810 }
4811 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004812 }
4813
4814 if (evaluate)
4815 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004816 n1 = 0;
4817 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004818 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004819 n1 = get_tv_number(&var1);
4820 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004821 }
4822 if (range)
4823 {
4824 if (empty2)
4825 n2 = -1;
4826 else
4827 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004828 n2 = get_tv_number(&var2);
4829 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004830 }
4831 }
4832
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004833 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004834 {
4835 case VAR_NUMBER:
4836 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004837 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004838 len = (long)STRLEN(s);
4839 if (range)
4840 {
4841 /* The resulting variable is a substring. If the indexes
4842 * are out of range the result is empty. */
4843 if (n1 < 0)
4844 {
4845 n1 = len + n1;
4846 if (n1 < 0)
4847 n1 = 0;
4848 }
4849 if (n2 < 0)
4850 n2 = len + n2;
4851 else if (n2 >= len)
4852 n2 = len;
4853 if (n1 >= len || n2 < 0 || n1 > n2)
4854 s = NULL;
4855 else
4856 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4857 }
4858 else
4859 {
4860 /* The resulting variable is a string of a single
4861 * character. If the index is too big or negative the
4862 * result is empty. */
4863 if (n1 >= len || n1 < 0)
4864 s = NULL;
4865 else
4866 s = vim_strnsave(s + n1, 1);
4867 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004868 clear_tv(rettv);
4869 rettv->v_type = VAR_STRING;
4870 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004871 break;
4872
4873 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004874 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004875 if (n1 < 0)
4876 n1 = len + n1;
4877 if (!empty1 && (n1 < 0 || n1 >= len))
4878 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004879 /* For a range we allow invalid values and return an empty
4880 * list. A list index out of range is an error. */
4881 if (!range)
4882 {
4883 if (verbose)
4884 EMSGN(_(e_listidx), n1);
4885 return FAIL;
4886 }
4887 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004888 }
4889 if (range)
4890 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004891 list_T *l;
4892 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004893
4894 if (n2 < 0)
4895 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004896 else if (n2 >= len)
4897 n2 = len - 1;
4898 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004899 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004900 l = list_alloc();
4901 if (l == NULL)
4902 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004903 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004904 n1 <= n2; ++n1)
4905 {
4906 if (list_append_tv(l, &item->li_tv) == FAIL)
4907 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00004908 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004909 return FAIL;
4910 }
4911 item = item->li_next;
4912 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004913 clear_tv(rettv);
4914 rettv->v_type = VAR_LIST;
4915 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004916 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004917 }
4918 else
4919 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004920 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004921 clear_tv(rettv);
4922 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004923 }
4924 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004925
4926 case VAR_DICT:
4927 if (range)
4928 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004929 if (verbose)
4930 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004931 if (len == -1)
4932 clear_tv(&var1);
4933 return FAIL;
4934 }
4935 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004936 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004937
4938 if (len == -1)
4939 {
4940 key = get_tv_string(&var1);
4941 if (*key == NUL)
4942 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004943 if (verbose)
4944 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004945 clear_tv(&var1);
4946 return FAIL;
4947 }
4948 }
4949
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004950 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004951
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004952 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004953 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004954 if (len == -1)
4955 clear_tv(&var1);
4956 if (item == NULL)
4957 return FAIL;
4958
4959 copy_tv(&item->di_tv, &var1);
4960 clear_tv(rettv);
4961 *rettv = var1;
4962 }
4963 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004964 }
4965 }
4966
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004967 return OK;
4968}
4969
4970/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 * Get an option value.
4972 * "arg" points to the '&' or '+' before the option name.
4973 * "arg" is advanced to character after the option name.
4974 * Return OK or FAIL.
4975 */
4976 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004977get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004979 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 int evaluate;
4981{
4982 char_u *option_end;
4983 long numval;
4984 char_u *stringval;
4985 int opt_type;
4986 int c;
4987 int working = (**arg == '+'); /* has("+option") */
4988 int ret = OK;
4989 int opt_flags;
4990
4991 /*
4992 * Isolate the option name and find its value.
4993 */
4994 option_end = find_option_end(arg, &opt_flags);
4995 if (option_end == NULL)
4996 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004997 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 EMSG2(_("E112: Option name missing: %s"), *arg);
4999 return FAIL;
5000 }
5001
5002 if (!evaluate)
5003 {
5004 *arg = option_end;
5005 return OK;
5006 }
5007
5008 c = *option_end;
5009 *option_end = NUL;
5010 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005011 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012
5013 if (opt_type == -3) /* invalid name */
5014 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005015 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 EMSG2(_("E113: Unknown option: %s"), *arg);
5017 ret = FAIL;
5018 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005019 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 {
5021 if (opt_type == -2) /* hidden string option */
5022 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005023 rettv->v_type = VAR_STRING;
5024 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 }
5026 else if (opt_type == -1) /* hidden number option */
5027 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005028 rettv->v_type = VAR_NUMBER;
5029 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 }
5031 else if (opt_type == 1) /* number option */
5032 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005033 rettv->v_type = VAR_NUMBER;
5034 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 }
5036 else /* string option */
5037 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005038 rettv->v_type = VAR_STRING;
5039 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 }
5041 }
5042 else if (working && (opt_type == -2 || opt_type == -1))
5043 ret = FAIL;
5044
5045 *option_end = c; /* put back for error messages */
5046 *arg = option_end;
5047
5048 return ret;
5049}
5050
5051/*
5052 * Allocate a variable for a string constant.
5053 * Return OK or FAIL.
5054 */
5055 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005056get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005058 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 int evaluate;
5060{
5061 char_u *p;
5062 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 int extra = 0;
5064
5065 /*
5066 * Find the end of the string, skipping backslashed characters.
5067 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005068 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 {
5070 if (*p == '\\' && p[1] != NUL)
5071 {
5072 ++p;
5073 /* A "\<x>" form occupies at least 4 characters, and produces up
5074 * to 6 characters: reserve space for 2 extra */
5075 if (*p == '<')
5076 extra += 2;
5077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 }
5079
5080 if (*p != '"')
5081 {
5082 EMSG2(_("E114: Missing quote: %s"), *arg);
5083 return FAIL;
5084 }
5085
5086 /* If only parsing, set *arg and return here */
5087 if (!evaluate)
5088 {
5089 *arg = p + 1;
5090 return OK;
5091 }
5092
5093 /*
5094 * Copy the string into allocated memory, handling backslashed
5095 * characters.
5096 */
5097 name = alloc((unsigned)(p - *arg + extra));
5098 if (name == NULL)
5099 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005100 rettv->v_type = VAR_STRING;
5101 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102
Bram Moolenaar8c711452005-01-14 21:53:12 +00005103 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 {
5105 if (*p == '\\')
5106 {
5107 switch (*++p)
5108 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005109 case 'b': *name++ = BS; ++p; break;
5110 case 'e': *name++ = ESC; ++p; break;
5111 case 'f': *name++ = FF; ++p; break;
5112 case 'n': *name++ = NL; ++p; break;
5113 case 'r': *name++ = CAR; ++p; break;
5114 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115
5116 case 'X': /* hex: "\x1", "\x12" */
5117 case 'x':
5118 case 'u': /* Unicode: "\u0023" */
5119 case 'U':
5120 if (vim_isxdigit(p[1]))
5121 {
5122 int n, nr;
5123 int c = toupper(*p);
5124
5125 if (c == 'X')
5126 n = 2;
5127 else
5128 n = 4;
5129 nr = 0;
5130 while (--n >= 0 && vim_isxdigit(p[1]))
5131 {
5132 ++p;
5133 nr = (nr << 4) + hex2nr(*p);
5134 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005135 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136#ifdef FEAT_MBYTE
5137 /* For "\u" store the number according to
5138 * 'encoding'. */
5139 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005140 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 else
5142#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005143 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 break;
5146
5147 /* octal: "\1", "\12", "\123" */
5148 case '0':
5149 case '1':
5150 case '2':
5151 case '3':
5152 case '4':
5153 case '5':
5154 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005155 case '7': *name = *p++ - '0';
5156 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005158 *name = (*name << 3) + *p++ - '0';
5159 if (*p >= '0' && *p <= '7')
5160 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005162 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 break;
5164
5165 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005166 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 if (extra != 0)
5168 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005169 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 break;
5171 }
5172 /* FALLTHROUGH */
5173
Bram Moolenaar8c711452005-01-14 21:53:12 +00005174 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 break;
5176 }
5177 }
5178 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005179 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005182 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 *arg = p + 1;
5184
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 return OK;
5186}
5187
5188/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005189 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 * Return OK or FAIL.
5191 */
5192 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005193get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005195 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 int evaluate;
5197{
5198 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005199 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005200 int reduce = 0;
5201
5202 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005203 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005204 */
5205 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5206 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005207 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005208 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005209 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005210 break;
5211 ++reduce;
5212 ++p;
5213 }
5214 }
5215
Bram Moolenaar8c711452005-01-14 21:53:12 +00005216 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005217 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005218 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005219 return FAIL;
5220 }
5221
Bram Moolenaar8c711452005-01-14 21:53:12 +00005222 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005223 if (!evaluate)
5224 {
5225 *arg = p + 1;
5226 return OK;
5227 }
5228
5229 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005230 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005231 */
5232 str = alloc((unsigned)((p - *arg) - reduce));
5233 if (str == NULL)
5234 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005235 rettv->v_type = VAR_STRING;
5236 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005237
Bram Moolenaar8c711452005-01-14 21:53:12 +00005238 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005239 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005240 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005241 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005242 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005243 break;
5244 ++p;
5245 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005246 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005247 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005248 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005249 *arg = p + 1;
5250
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005251 return OK;
5252}
5253
5254/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005255 * Allocate a variable for a List and fill it from "*arg".
5256 * Return OK or FAIL.
5257 */
5258 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005259get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005260 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005261 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005262 int evaluate;
5263{
Bram Moolenaar33570922005-01-25 22:26:29 +00005264 list_T *l = NULL;
5265 typval_T tv;
5266 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005267
5268 if (evaluate)
5269 {
5270 l = list_alloc();
5271 if (l == NULL)
5272 return FAIL;
5273 }
5274
5275 *arg = skipwhite(*arg + 1);
5276 while (**arg != ']' && **arg != NUL)
5277 {
5278 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5279 goto failret;
5280 if (evaluate)
5281 {
5282 item = listitem_alloc();
5283 if (item != NULL)
5284 {
5285 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005286 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005287 list_append(l, item);
5288 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005289 else
5290 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005291 }
5292
5293 if (**arg == ']')
5294 break;
5295 if (**arg != ',')
5296 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005297 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005298 goto failret;
5299 }
5300 *arg = skipwhite(*arg + 1);
5301 }
5302
5303 if (**arg != ']')
5304 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005305 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005306failret:
5307 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005308 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005309 return FAIL;
5310 }
5311
5312 *arg = skipwhite(*arg + 1);
5313 if (evaluate)
5314 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005315 rettv->v_type = VAR_LIST;
5316 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005317 ++l->lv_refcount;
5318 }
5319
5320 return OK;
5321}
5322
5323/*
5324 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005325 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005326 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005327 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005328list_alloc()
5329{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005330 list_T *l;
5331
5332 l = (list_T *)alloc_clear(sizeof(list_T));
5333 if (l != NULL)
5334 {
5335 /* Prepend the list to the list of lists for garbage collection. */
5336 if (first_list != NULL)
5337 first_list->lv_used_prev = l;
5338 l->lv_used_prev = NULL;
5339 l->lv_used_next = first_list;
5340 first_list = l;
5341 }
5342 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005343}
5344
5345/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005346 * Allocate an empty list for a return value.
5347 * Returns OK or FAIL.
5348 */
5349 static int
5350rettv_list_alloc(rettv)
5351 typval_T *rettv;
5352{
5353 list_T *l = list_alloc();
5354
5355 if (l == NULL)
5356 return FAIL;
5357
5358 rettv->vval.v_list = l;
5359 rettv->v_type = VAR_LIST;
5360 ++l->lv_refcount;
5361 return OK;
5362}
5363
5364/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005365 * Unreference a list: decrement the reference count and free it when it
5366 * becomes zero.
5367 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005368 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005369list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005370 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005371{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005372 if (l != NULL && --l->lv_refcount <= 0)
5373 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005374}
5375
5376/*
5377 * Free a list, including all items it points to.
5378 * Ignores the reference count.
5379 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005380 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005381list_free(l, recurse)
5382 list_T *l;
5383 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005384{
Bram Moolenaar33570922005-01-25 22:26:29 +00005385 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005386
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005387 /* Remove the list from the list of lists for garbage collection. */
5388 if (l->lv_used_prev == NULL)
5389 first_list = l->lv_used_next;
5390 else
5391 l->lv_used_prev->lv_used_next = l->lv_used_next;
5392 if (l->lv_used_next != NULL)
5393 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5394
Bram Moolenaard9fba312005-06-26 22:34:35 +00005395 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005396 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005397 /* Remove the item before deleting it. */
5398 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005399 if (recurse || (item->li_tv.v_type != VAR_LIST
5400 && item->li_tv.v_type != VAR_DICT))
5401 clear_tv(&item->li_tv);
5402 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005403 }
5404 vim_free(l);
5405}
5406
5407/*
5408 * Allocate a list item.
5409 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005410 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005411listitem_alloc()
5412{
Bram Moolenaar33570922005-01-25 22:26:29 +00005413 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005414}
5415
5416/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005417 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005418 */
5419 static void
5420listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005421 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005422{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005423 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005424 vim_free(item);
5425}
5426
5427/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005428 * Remove a list item from a List and free it. Also clears the value.
5429 */
5430 static void
5431listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005432 list_T *l;
5433 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005434{
5435 list_remove(l, item, item);
5436 listitem_free(item);
5437}
5438
5439/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005440 * Get the number of items in a list.
5441 */
5442 static long
5443list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005444 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005445{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005446 if (l == NULL)
5447 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005448 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005449}
5450
5451/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005452 * Return TRUE when two lists have exactly the same values.
5453 */
5454 static int
5455list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005456 list_T *l1;
5457 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005458 int ic; /* ignore case for strings */
5459{
Bram Moolenaar33570922005-01-25 22:26:29 +00005460 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005461
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005462 if (l1 == l2)
5463 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005464 if (list_len(l1) != list_len(l2))
5465 return FALSE;
5466
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005467 for (item1 = l1->lv_first, item2 = l2->lv_first;
5468 item1 != NULL && item2 != NULL;
5469 item1 = item1->li_next, item2 = item2->li_next)
5470 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5471 return FALSE;
5472 return item1 == NULL && item2 == NULL;
5473}
5474
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005475#if defined(FEAT_PYTHON) || defined(PROTO)
5476/*
5477 * Return the dictitem that an entry in a hashtable points to.
5478 */
5479 dictitem_T *
5480dict_lookup(hi)
5481 hashitem_T *hi;
5482{
5483 return HI2DI(hi);
5484}
5485#endif
5486
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005487/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005488 * Return TRUE when two dictionaries have exactly the same key/values.
5489 */
5490 static int
5491dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005492 dict_T *d1;
5493 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005494 int ic; /* ignore case for strings */
5495{
Bram Moolenaar33570922005-01-25 22:26:29 +00005496 hashitem_T *hi;
5497 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005498 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005499
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005500 if (d1 == d2)
5501 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005502 if (dict_len(d1) != dict_len(d2))
5503 return FALSE;
5504
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005505 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00005506 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005507 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005508 if (!HASHITEM_EMPTY(hi))
5509 {
5510 item2 = dict_find(d2, hi->hi_key, -1);
5511 if (item2 == NULL)
5512 return FALSE;
5513 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5514 return FALSE;
5515 --todo;
5516 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005517 }
5518 return TRUE;
5519}
5520
5521/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005522 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005523 * Compares the items just like "==" would compare them, but strings and
5524 * numbers are different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005525 */
5526 static int
5527tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005528 typval_T *tv1;
5529 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005530 int ic; /* ignore case */
5531{
5532 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005533 char_u *s1, *s2;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005534 static int recursive = 0; /* cach recursive loops */
5535 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005536
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005537 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005538 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005539 /* Catch lists and dicts that have an endless loop by limiting
5540 * recursiveness to 1000. We guess they are equal then. */
5541 if (recursive >= 1000)
5542 return TRUE;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005543
5544 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005545 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005546 case VAR_LIST:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005547 ++recursive;
5548 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5549 --recursive;
5550 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005551
5552 case VAR_DICT:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005553 ++recursive;
5554 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5555 --recursive;
5556 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005557
5558 case VAR_FUNC:
5559 return (tv1->vval.v_string != NULL
5560 && tv2->vval.v_string != NULL
5561 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5562
5563 case VAR_NUMBER:
5564 return tv1->vval.v_number == tv2->vval.v_number;
5565
5566 case VAR_STRING:
5567 s1 = get_tv_string_buf(tv1, buf1);
5568 s2 = get_tv_string_buf(tv2, buf2);
5569 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005570 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005571
5572 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005573 return TRUE;
5574}
5575
5576/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005577 * Locate item with index "n" in list "l" and return it.
5578 * A negative index is counted from the end; -1 is the last item.
5579 * Returns NULL when "n" is out of range.
5580 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005581 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005582list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005583 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005584 long n;
5585{
Bram Moolenaar33570922005-01-25 22:26:29 +00005586 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005587 long idx;
5588
5589 if (l == NULL)
5590 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005591
5592 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005593 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005594 n = l->lv_len + n;
5595
5596 /* Check for index out of range. */
5597 if (n < 0 || n >= l->lv_len)
5598 return NULL;
5599
5600 /* When there is a cached index may start search from there. */
5601 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005602 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005603 if (n < l->lv_idx / 2)
5604 {
5605 /* closest to the start of the list */
5606 item = l->lv_first;
5607 idx = 0;
5608 }
5609 else if (n > (l->lv_idx + l->lv_len) / 2)
5610 {
5611 /* closest to the end of the list */
5612 item = l->lv_last;
5613 idx = l->lv_len - 1;
5614 }
5615 else
5616 {
5617 /* closest to the cached index */
5618 item = l->lv_idx_item;
5619 idx = l->lv_idx;
5620 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005621 }
5622 else
5623 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005624 if (n < l->lv_len / 2)
5625 {
5626 /* closest to the start of the list */
5627 item = l->lv_first;
5628 idx = 0;
5629 }
5630 else
5631 {
5632 /* closest to the end of the list */
5633 item = l->lv_last;
5634 idx = l->lv_len - 1;
5635 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005636 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005637
5638 while (n > idx)
5639 {
5640 /* search forward */
5641 item = item->li_next;
5642 ++idx;
5643 }
5644 while (n < idx)
5645 {
5646 /* search backward */
5647 item = item->li_prev;
5648 --idx;
5649 }
5650
5651 /* cache the used index */
5652 l->lv_idx = idx;
5653 l->lv_idx_item = item;
5654
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005655 return item;
5656}
5657
5658/*
Bram Moolenaara5525202006-03-02 22:52:09 +00005659 * Get list item "l[idx]" as a number.
5660 */
5661 static long
5662list_find_nr(l, idx, errorp)
5663 list_T *l;
5664 long idx;
5665 int *errorp; /* set to TRUE when something wrong */
5666{
5667 listitem_T *li;
5668
5669 li = list_find(l, idx);
5670 if (li == NULL)
5671 {
5672 if (errorp != NULL)
5673 *errorp = TRUE;
5674 return -1L;
5675 }
5676 return get_tv_number_chk(&li->li_tv, errorp);
5677}
5678
5679/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005680 * Locate "item" list "l" and return its index.
5681 * Returns -1 when "item" is not in the list.
5682 */
5683 static long
5684list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005685 list_T *l;
5686 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005687{
5688 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005689 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005690
5691 if (l == NULL)
5692 return -1;
5693 idx = 0;
5694 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5695 ++idx;
5696 if (li == NULL)
5697 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005698 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005699}
5700
5701/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005702 * Append item "item" to the end of list "l".
5703 */
5704 static void
5705list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005706 list_T *l;
5707 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005708{
5709 if (l->lv_last == NULL)
5710 {
5711 /* empty list */
5712 l->lv_first = item;
5713 l->lv_last = item;
5714 item->li_prev = NULL;
5715 }
5716 else
5717 {
5718 l->lv_last->li_next = item;
5719 item->li_prev = l->lv_last;
5720 l->lv_last = item;
5721 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005722 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005723 item->li_next = NULL;
5724}
5725
5726/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005727 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005728 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005729 */
5730 static int
5731list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005732 list_T *l;
5733 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005734{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005735 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005736
Bram Moolenaar05159a02005-02-26 23:04:13 +00005737 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005738 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005739 copy_tv(tv, &li->li_tv);
5740 list_append(l, li);
5741 return OK;
5742}
5743
5744/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005745 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005746 * Return FAIL when out of memory.
5747 */
5748 int
5749list_append_dict(list, dict)
5750 list_T *list;
5751 dict_T *dict;
5752{
5753 listitem_T *li = listitem_alloc();
5754
5755 if (li == NULL)
5756 return FAIL;
5757 li->li_tv.v_type = VAR_DICT;
5758 li->li_tv.v_lock = 0;
5759 li->li_tv.vval.v_dict = dict;
5760 list_append(list, li);
5761 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005762 return OK;
5763}
5764
5765/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005766 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00005767 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005768 * Returns FAIL when out of memory.
5769 */
5770 static int
Bram Moolenaar4463f292005-09-25 22:20:24 +00005771list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005772 list_T *l;
5773 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00005774 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005775{
5776 listitem_T *li = listitem_alloc();
5777
5778 if (li == NULL)
5779 return FAIL;
5780 list_append(l, li);
5781 li->li_tv.v_type = VAR_STRING;
5782 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005783 if (str == NULL)
5784 li->li_tv.vval.v_string = NULL;
5785 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005786 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005787 return FAIL;
5788 return OK;
5789}
5790
5791/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00005792 * Append "n" to list "l".
5793 * Returns FAIL when out of memory.
5794 */
5795 static int
5796list_append_number(l, n)
5797 list_T *l;
5798 varnumber_T n;
5799{
5800 listitem_T *li;
5801
5802 li = listitem_alloc();
5803 if (li == NULL)
5804 return FAIL;
5805 li->li_tv.v_type = VAR_NUMBER;
5806 li->li_tv.v_lock = 0;
5807 li->li_tv.vval.v_number = n;
5808 list_append(l, li);
5809 return OK;
5810}
5811
5812/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005813 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005814 * If "item" is NULL append at the end.
5815 * Return FAIL when out of memory.
5816 */
5817 static int
5818list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005819 list_T *l;
5820 typval_T *tv;
5821 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005822{
Bram Moolenaar33570922005-01-25 22:26:29 +00005823 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005824
5825 if (ni == NULL)
5826 return FAIL;
5827 copy_tv(tv, &ni->li_tv);
5828 if (item == NULL)
5829 /* Append new item at end of list. */
5830 list_append(l, ni);
5831 else
5832 {
5833 /* Insert new item before existing item. */
5834 ni->li_prev = item->li_prev;
5835 ni->li_next = item;
5836 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005837 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005838 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005839 ++l->lv_idx;
5840 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005841 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005842 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005843 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005844 l->lv_idx_item = NULL;
5845 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005846 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005847 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005848 }
5849 return OK;
5850}
5851
5852/*
5853 * Extend "l1" with "l2".
5854 * If "bef" is NULL append at the end, otherwise insert before this item.
5855 * Returns FAIL when out of memory.
5856 */
5857 static int
5858list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005859 list_T *l1;
5860 list_T *l2;
5861 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005862{
Bram Moolenaar33570922005-01-25 22:26:29 +00005863 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005864
5865 for (item = l2->lv_first; item != NULL; item = item->li_next)
5866 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5867 return FAIL;
5868 return OK;
5869}
5870
5871/*
5872 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5873 * Return FAIL when out of memory.
5874 */
5875 static int
5876list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005877 list_T *l1;
5878 list_T *l2;
5879 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005880{
Bram Moolenaar33570922005-01-25 22:26:29 +00005881 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005882
5883 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005884 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005885 if (l == NULL)
5886 return FAIL;
5887 tv->v_type = VAR_LIST;
5888 tv->vval.v_list = l;
5889
5890 /* append all items from the second list */
5891 return list_extend(l, l2, NULL);
5892}
5893
5894/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005895 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005896 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005897 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005898 * Returns NULL when out of memory.
5899 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005900 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005901list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005902 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005903 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005904 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005905{
Bram Moolenaar33570922005-01-25 22:26:29 +00005906 list_T *copy;
5907 listitem_T *item;
5908 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005909
5910 if (orig == NULL)
5911 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005912
5913 copy = list_alloc();
5914 if (copy != NULL)
5915 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005916 if (copyID != 0)
5917 {
5918 /* Do this before adding the items, because one of the items may
5919 * refer back to this list. */
5920 orig->lv_copyID = copyID;
5921 orig->lv_copylist = copy;
5922 }
5923 for (item = orig->lv_first; item != NULL && !got_int;
5924 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005925 {
5926 ni = listitem_alloc();
5927 if (ni == NULL)
5928 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005929 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005930 {
5931 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5932 {
5933 vim_free(ni);
5934 break;
5935 }
5936 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005937 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005938 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005939 list_append(copy, ni);
5940 }
5941 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005942 if (item != NULL)
5943 {
5944 list_unref(copy);
5945 copy = NULL;
5946 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005947 }
5948
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005949 return copy;
5950}
5951
5952/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005953 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005954 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005955 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005956 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005957list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005958 list_T *l;
5959 listitem_T *item;
5960 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005961{
Bram Moolenaar33570922005-01-25 22:26:29 +00005962 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005963
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005964 /* notify watchers */
5965 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005966 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005967 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005968 list_fix_watch(l, ip);
5969 if (ip == item2)
5970 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005971 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005972
5973 if (item2->li_next == NULL)
5974 l->lv_last = item->li_prev;
5975 else
5976 item2->li_next->li_prev = item->li_prev;
5977 if (item->li_prev == NULL)
5978 l->lv_first = item2->li_next;
5979 else
5980 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005981 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005982}
5983
5984/*
5985 * Return an allocated string with the string representation of a list.
5986 * May return NULL.
5987 */
5988 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005989list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005990 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005991 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005992{
5993 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005994
5995 if (tv->vval.v_list == NULL)
5996 return NULL;
5997 ga_init2(&ga, (int)sizeof(char), 80);
5998 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005999 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006000 {
6001 vim_free(ga.ga_data);
6002 return NULL;
6003 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006004 ga_append(&ga, ']');
6005 ga_append(&ga, NUL);
6006 return (char_u *)ga.ga_data;
6007}
6008
6009/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006010 * Join list "l" into a string in "*gap", using separator "sep".
6011 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006012 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006013 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006014 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006015list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006016 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006017 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006018 char_u *sep;
6019 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006020 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006021{
6022 int first = TRUE;
6023 char_u *tofree;
6024 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006025 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006026 char_u *s;
6027
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006028 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006029 {
6030 if (first)
6031 first = FALSE;
6032 else
6033 ga_concat(gap, sep);
6034
6035 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006036 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006037 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006038 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006039 if (s != NULL)
6040 ga_concat(gap, s);
6041 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006042 if (s == NULL)
6043 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006044 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006045 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006046}
6047
6048/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006049 * Garbage collection for lists and dictionaries.
6050 *
6051 * We use reference counts to be able to free most items right away when they
6052 * are no longer used. But for composite items it's possible that it becomes
6053 * unused while the reference count is > 0: When there is a recursive
6054 * reference. Example:
6055 * :let l = [1, 2, 3]
6056 * :let d = {9: l}
6057 * :let l[1] = d
6058 *
6059 * Since this is quite unusual we handle this with garbage collection: every
6060 * once in a while find out which lists and dicts are not referenced from any
6061 * variable.
6062 *
6063 * Here is a good reference text about garbage collection (refers to Python
6064 * but it applies to all reference-counting mechanisms):
6065 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006066 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006067
6068/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006069 * Do garbage collection for lists and dicts.
6070 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006071 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006072 int
6073garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006074{
6075 dict_T *dd;
6076 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006077 int copyID = ++current_copyID;
6078 buf_T *buf;
6079 win_T *wp;
6080 int i;
6081 funccall_T *fc;
6082 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006083#ifdef FEAT_WINDOWS
6084 tabpage_T *tp;
6085#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006086
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006087 /* Only do this once. */
6088 want_garbage_collect = FALSE;
6089 may_garbage_collect = FALSE;
6090
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006091 /*
6092 * 1. Go through all accessible variables and mark all lists and dicts
6093 * with copyID.
6094 */
6095 /* script-local variables */
6096 for (i = 1; i <= ga_scripts.ga_len; ++i)
6097 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6098
6099 /* buffer-local variables */
6100 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6101 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6102
6103 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006104 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006105 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6106
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006107#ifdef FEAT_WINDOWS
6108 /* tabpage-local variables */
6109 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6110 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6111#endif
6112
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006113 /* global variables */
6114 set_ref_in_ht(&globvarht, copyID);
6115
6116 /* function-local variables */
6117 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6118 {
6119 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6120 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6121 }
6122
6123 /*
6124 * 2. Go through the list of dicts and free items without the copyID.
6125 */
6126 for (dd = first_dict; dd != NULL; )
6127 if (dd->dv_copyID != copyID)
6128 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006129 /* Free the Dictionary and ordinary items it contains, but don't
6130 * recurse into Lists and Dictionaries, they will be in the list
6131 * of dicts or list of lists. */
6132 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006133 did_free = TRUE;
6134
6135 /* restart, next dict may also have been freed */
6136 dd = first_dict;
6137 }
6138 else
6139 dd = dd->dv_used_next;
6140
6141 /*
6142 * 3. Go through the list of lists and free items without the copyID.
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006143 * But don't free a list that has a watcher (used in a for loop), these
6144 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006145 */
6146 for (ll = first_list; ll != NULL; )
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006147 if (ll->lv_copyID != copyID && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006148 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006149 /* Free the List and ordinary items it contains, but don't recurse
6150 * into Lists and Dictionaries, they will be in the list of dicts
6151 * or list of lists. */
6152 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006153 did_free = TRUE;
6154
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006155 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006156 ll = first_list;
6157 }
6158 else
6159 ll = ll->lv_used_next;
6160
6161 return did_free;
6162}
6163
6164/*
6165 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6166 */
6167 static void
6168set_ref_in_ht(ht, copyID)
6169 hashtab_T *ht;
6170 int copyID;
6171{
6172 int todo;
6173 hashitem_T *hi;
6174
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006175 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006176 for (hi = ht->ht_array; todo > 0; ++hi)
6177 if (!HASHITEM_EMPTY(hi))
6178 {
6179 --todo;
6180 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6181 }
6182}
6183
6184/*
6185 * Mark all lists and dicts referenced through list "l" with "copyID".
6186 */
6187 static void
6188set_ref_in_list(l, copyID)
6189 list_T *l;
6190 int copyID;
6191{
6192 listitem_T *li;
6193
6194 for (li = l->lv_first; li != NULL; li = li->li_next)
6195 set_ref_in_item(&li->li_tv, copyID);
6196}
6197
6198/*
6199 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6200 */
6201 static void
6202set_ref_in_item(tv, copyID)
6203 typval_T *tv;
6204 int copyID;
6205{
6206 dict_T *dd;
6207 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006208
6209 switch (tv->v_type)
6210 {
6211 case VAR_DICT:
6212 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006213 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006214 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006215 /* Didn't see this dict yet. */
6216 dd->dv_copyID = copyID;
6217 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006218 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006219 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006220
6221 case VAR_LIST:
6222 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006223 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006224 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006225 /* Didn't see this list yet. */
6226 ll->lv_copyID = copyID;
6227 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006228 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006229 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006230 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006231 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006232}
6233
6234/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006235 * Allocate an empty header for a dictionary.
6236 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006237 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006238dict_alloc()
6239{
Bram Moolenaar33570922005-01-25 22:26:29 +00006240 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006241
Bram Moolenaar33570922005-01-25 22:26:29 +00006242 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006243 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006244 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006245 /* Add the list to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006246 if (first_dict != NULL)
6247 first_dict->dv_used_prev = d;
6248 d->dv_used_next = first_dict;
6249 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006250 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006251
Bram Moolenaar33570922005-01-25 22:26:29 +00006252 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006253 d->dv_lock = 0;
6254 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006255 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006256 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006257 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006258}
6259
6260/*
6261 * Unreference a Dictionary: decrement the reference count and free it when it
6262 * becomes zero.
6263 */
6264 static void
6265dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006266 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006267{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006268 if (d != NULL && --d->dv_refcount <= 0)
6269 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006270}
6271
6272/*
6273 * Free a Dictionary, including all items it contains.
6274 * Ignores the reference count.
6275 */
6276 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006277dict_free(d, recurse)
6278 dict_T *d;
6279 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006280{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006281 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006282 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006283 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006284
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006285 /* Remove the dict from the list of dicts for garbage collection. */
6286 if (d->dv_used_prev == NULL)
6287 first_dict = d->dv_used_next;
6288 else
6289 d->dv_used_prev->dv_used_next = d->dv_used_next;
6290 if (d->dv_used_next != NULL)
6291 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6292
6293 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006294 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006295 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006296 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006297 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006298 if (!HASHITEM_EMPTY(hi))
6299 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006300 /* Remove the item before deleting it, just in case there is
6301 * something recursive causing trouble. */
6302 di = HI2DI(hi);
6303 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00006304 if (recurse || (di->di_tv.v_type != VAR_LIST
6305 && di->di_tv.v_type != VAR_DICT))
6306 clear_tv(&di->di_tv);
6307 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006308 --todo;
6309 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006310 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006311 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006312 vim_free(d);
6313}
6314
6315/*
6316 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006317 * The "key" is copied to the new item.
6318 * Note that the value of the item "di_tv" still needs to be initialized!
6319 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006320 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006321 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006322dictitem_alloc(key)
6323 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006324{
Bram Moolenaar33570922005-01-25 22:26:29 +00006325 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006326
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006327 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006328 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006329 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006330 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006331 di->di_flags = 0;
6332 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006333 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006334}
6335
6336/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006337 * Make a copy of a Dictionary item.
6338 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006339 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006340dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006341 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006342{
Bram Moolenaar33570922005-01-25 22:26:29 +00006343 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006344
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006345 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
6346 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006347 if (di != NULL)
6348 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006349 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006350 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006351 copy_tv(&org->di_tv, &di->di_tv);
6352 }
6353 return di;
6354}
6355
6356/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006357 * Remove item "item" from Dictionary "dict" and free it.
6358 */
6359 static void
6360dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006361 dict_T *dict;
6362 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006363{
Bram Moolenaar33570922005-01-25 22:26:29 +00006364 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006365
Bram Moolenaar33570922005-01-25 22:26:29 +00006366 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006367 if (HASHITEM_EMPTY(hi))
6368 EMSG2(_(e_intern2), "dictitem_remove()");
6369 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006370 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006371 dictitem_free(item);
6372}
6373
6374/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006375 * Free a dict item. Also clears the value.
6376 */
6377 static void
6378dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006379 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006380{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006381 clear_tv(&item->di_tv);
6382 vim_free(item);
6383}
6384
6385/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006386 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6387 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006388 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006389 * Returns NULL when out of memory.
6390 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006391 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006392dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006393 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006394 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006395 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006396{
Bram Moolenaar33570922005-01-25 22:26:29 +00006397 dict_T *copy;
6398 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006399 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006400 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006401
6402 if (orig == NULL)
6403 return NULL;
6404
6405 copy = dict_alloc();
6406 if (copy != NULL)
6407 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006408 if (copyID != 0)
6409 {
6410 orig->dv_copyID = copyID;
6411 orig->dv_copydict = copy;
6412 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006413 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006414 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006415 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006416 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006417 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006418 --todo;
6419
6420 di = dictitem_alloc(hi->hi_key);
6421 if (di == NULL)
6422 break;
6423 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006424 {
6425 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6426 copyID) == FAIL)
6427 {
6428 vim_free(di);
6429 break;
6430 }
6431 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006432 else
6433 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6434 if (dict_add(copy, di) == FAIL)
6435 {
6436 dictitem_free(di);
6437 break;
6438 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006439 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006440 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006441
Bram Moolenaare9a41262005-01-15 22:18:47 +00006442 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006443 if (todo > 0)
6444 {
6445 dict_unref(copy);
6446 copy = NULL;
6447 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006448 }
6449
6450 return copy;
6451}
6452
6453/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006454 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006455 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006456 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006457 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006458dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006459 dict_T *d;
6460 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006461{
Bram Moolenaar33570922005-01-25 22:26:29 +00006462 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006463}
6464
Bram Moolenaar8c711452005-01-14 21:53:12 +00006465/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006466 * Add a number or string entry to dictionary "d".
6467 * When "str" is NULL use number "nr", otherwise use "str".
6468 * Returns FAIL when out of memory and when key already exists.
6469 */
6470 int
6471dict_add_nr_str(d, key, nr, str)
6472 dict_T *d;
6473 char *key;
6474 long nr;
6475 char_u *str;
6476{
6477 dictitem_T *item;
6478
6479 item = dictitem_alloc((char_u *)key);
6480 if (item == NULL)
6481 return FAIL;
6482 item->di_tv.v_lock = 0;
6483 if (str == NULL)
6484 {
6485 item->di_tv.v_type = VAR_NUMBER;
6486 item->di_tv.vval.v_number = nr;
6487 }
6488 else
6489 {
6490 item->di_tv.v_type = VAR_STRING;
6491 item->di_tv.vval.v_string = vim_strsave(str);
6492 }
6493 if (dict_add(d, item) == FAIL)
6494 {
6495 dictitem_free(item);
6496 return FAIL;
6497 }
6498 return OK;
6499}
6500
6501/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006502 * Get the number of items in a Dictionary.
6503 */
6504 static long
6505dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006506 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006507{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006508 if (d == NULL)
6509 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006510 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006511}
6512
6513/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006514 * Find item "key[len]" in Dictionary "d".
6515 * If "len" is negative use strlen(key).
6516 * Returns NULL when not found.
6517 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006518 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006519dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006520 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006521 char_u *key;
6522 int len;
6523{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006524#define AKEYLEN 200
6525 char_u buf[AKEYLEN];
6526 char_u *akey;
6527 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006528 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006529
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006530 if (len < 0)
6531 akey = key;
6532 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006533 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006534 tofree = akey = vim_strnsave(key, len);
6535 if (akey == NULL)
6536 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006537 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006538 else
6539 {
6540 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006541 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006542 akey = buf;
6543 }
6544
Bram Moolenaar33570922005-01-25 22:26:29 +00006545 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006546 vim_free(tofree);
6547 if (HASHITEM_EMPTY(hi))
6548 return NULL;
6549 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006550}
6551
6552/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006553 * Get a string item from a dictionary.
6554 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00006555 * Returns NULL if the entry doesn't exist or out of memory.
6556 */
6557 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006558get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00006559 dict_T *d;
6560 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006561 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006562{
6563 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006564 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006565
6566 di = dict_find(d, key, -1);
6567 if (di == NULL)
6568 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006569 s = get_tv_string(&di->di_tv);
6570 if (save && s != NULL)
6571 s = vim_strsave(s);
6572 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006573}
6574
6575/*
6576 * Get a number item from a dictionary.
6577 * Returns 0 if the entry doesn't exist or out of memory.
6578 */
6579 long
6580get_dict_number(d, key)
6581 dict_T *d;
6582 char_u *key;
6583{
6584 dictitem_T *di;
6585
6586 di = dict_find(d, key, -1);
6587 if (di == NULL)
6588 return 0;
6589 return get_tv_number(&di->di_tv);
6590}
6591
6592/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006593 * Return an allocated string with the string representation of a Dictionary.
6594 * May return NULL.
6595 */
6596 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006597dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006598 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006599 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006600{
6601 garray_T ga;
6602 int first = TRUE;
6603 char_u *tofree;
6604 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006605 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006606 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006607 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006608 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006609
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006610 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006611 return NULL;
6612 ga_init2(&ga, (int)sizeof(char), 80);
6613 ga_append(&ga, '{');
6614
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006615 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006616 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006617 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006618 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006619 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006620 --todo;
6621
6622 if (first)
6623 first = FALSE;
6624 else
6625 ga_concat(&ga, (char_u *)", ");
6626
6627 tofree = string_quote(hi->hi_key, FALSE);
6628 if (tofree != NULL)
6629 {
6630 ga_concat(&ga, tofree);
6631 vim_free(tofree);
6632 }
6633 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006634 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006635 if (s != NULL)
6636 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006637 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006638 if (s == NULL)
6639 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006640 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006641 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006642 if (todo > 0)
6643 {
6644 vim_free(ga.ga_data);
6645 return NULL;
6646 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006647
6648 ga_append(&ga, '}');
6649 ga_append(&ga, NUL);
6650 return (char_u *)ga.ga_data;
6651}
6652
6653/*
6654 * Allocate a variable for a Dictionary and fill it from "*arg".
6655 * Return OK or FAIL. Returns NOTDONE for {expr}.
6656 */
6657 static int
6658get_dict_tv(arg, rettv, evaluate)
6659 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006660 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006661 int evaluate;
6662{
Bram Moolenaar33570922005-01-25 22:26:29 +00006663 dict_T *d = NULL;
6664 typval_T tvkey;
6665 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006666 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00006667 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006668 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006669 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00006670
6671 /*
6672 * First check if it's not a curly-braces thing: {expr}.
6673 * Must do this without evaluating, otherwise a function may be called
6674 * twice. Unfortunately this means we need to call eval1() twice for the
6675 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006676 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006677 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006678 if (*start != '}')
6679 {
6680 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
6681 return FAIL;
6682 if (*start == '}')
6683 return NOTDONE;
6684 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006685
6686 if (evaluate)
6687 {
6688 d = dict_alloc();
6689 if (d == NULL)
6690 return FAIL;
6691 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006692 tvkey.v_type = VAR_UNKNOWN;
6693 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006694
6695 *arg = skipwhite(*arg + 1);
6696 while (**arg != '}' && **arg != NUL)
6697 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006698 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006699 goto failret;
6700 if (**arg != ':')
6701 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006702 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006703 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006704 goto failret;
6705 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006706 key = get_tv_string_buf_chk(&tvkey, buf);
6707 if (key == NULL || *key == NUL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006708 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006709 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6710 if (key != NULL)
6711 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006712 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006713 goto failret;
6714 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006715
6716 *arg = skipwhite(*arg + 1);
6717 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6718 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006719 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006720 goto failret;
6721 }
6722 if (evaluate)
6723 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006724 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006725 if (item != NULL)
6726 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00006727 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006728 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006729 clear_tv(&tv);
6730 goto failret;
6731 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006732 item = dictitem_alloc(key);
6733 clear_tv(&tvkey);
6734 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006735 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006736 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006737 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006738 if (dict_add(d, item) == FAIL)
6739 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006740 }
6741 }
6742
6743 if (**arg == '}')
6744 break;
6745 if (**arg != ',')
6746 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006747 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006748 goto failret;
6749 }
6750 *arg = skipwhite(*arg + 1);
6751 }
6752
6753 if (**arg != '}')
6754 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006755 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006756failret:
6757 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00006758 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006759 return FAIL;
6760 }
6761
6762 *arg = skipwhite(*arg + 1);
6763 if (evaluate)
6764 {
6765 rettv->v_type = VAR_DICT;
6766 rettv->vval.v_dict = d;
6767 ++d->dv_refcount;
6768 }
6769
6770 return OK;
6771}
6772
Bram Moolenaar8c711452005-01-14 21:53:12 +00006773/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006774 * Return a string with the string representation of a variable.
6775 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006776 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006777 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006778 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006779 * May return NULL;
6780 */
6781 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006782echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006783 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006784 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006785 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006786 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006787{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006788 static int recurse = 0;
6789 char_u *r = NULL;
6790
Bram Moolenaar33570922005-01-25 22:26:29 +00006791 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006792 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006793 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006794 *tofree = NULL;
6795 return NULL;
6796 }
6797 ++recurse;
6798
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006799 switch (tv->v_type)
6800 {
6801 case VAR_FUNC:
6802 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006803 r = tv->vval.v_string;
6804 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006805
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006806 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006807 if (tv->vval.v_list == NULL)
6808 {
6809 *tofree = NULL;
6810 r = NULL;
6811 }
6812 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
6813 {
6814 *tofree = NULL;
6815 r = (char_u *)"[...]";
6816 }
6817 else
6818 {
6819 tv->vval.v_list->lv_copyID = copyID;
6820 *tofree = list2string(tv, copyID);
6821 r = *tofree;
6822 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006823 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006824
Bram Moolenaar8c711452005-01-14 21:53:12 +00006825 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006826 if (tv->vval.v_dict == NULL)
6827 {
6828 *tofree = NULL;
6829 r = NULL;
6830 }
6831 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
6832 {
6833 *tofree = NULL;
6834 r = (char_u *)"{...}";
6835 }
6836 else
6837 {
6838 tv->vval.v_dict->dv_copyID = copyID;
6839 *tofree = dict2string(tv, copyID);
6840 r = *tofree;
6841 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006842 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006843
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006844 case VAR_STRING:
6845 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006846 *tofree = NULL;
6847 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006848 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006849
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006850 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006851 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006852 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006853 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006854
6855 --recurse;
6856 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006857}
6858
6859/*
6860 * Return a string with the string representation of a variable.
6861 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6862 * "numbuf" is used for a number.
6863 * Puts quotes around strings, so that they can be parsed back by eval().
6864 * May return NULL;
6865 */
6866 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006867tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006868 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006869 char_u **tofree;
6870 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006871 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006872{
6873 switch (tv->v_type)
6874 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006875 case VAR_FUNC:
6876 *tofree = string_quote(tv->vval.v_string, TRUE);
6877 return *tofree;
6878 case VAR_STRING:
6879 *tofree = string_quote(tv->vval.v_string, FALSE);
6880 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006881 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006882 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006883 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006884 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006885 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006886 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006887 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006888 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006889}
6890
6891/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006892 * Return string "str" in ' quotes, doubling ' characters.
6893 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006894 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006895 */
6896 static char_u *
6897string_quote(str, function)
6898 char_u *str;
6899 int function;
6900{
Bram Moolenaar33570922005-01-25 22:26:29 +00006901 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006902 char_u *p, *r, *s;
6903
Bram Moolenaar33570922005-01-25 22:26:29 +00006904 len = (function ? 13 : 3);
6905 if (str != NULL)
6906 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006907 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00006908 for (p = str; *p != NUL; mb_ptr_adv(p))
6909 if (*p == '\'')
6910 ++len;
6911 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006912 s = r = alloc(len);
6913 if (r != NULL)
6914 {
6915 if (function)
6916 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006917 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006918 r += 10;
6919 }
6920 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006921 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006922 if (str != NULL)
6923 for (p = str; *p != NUL; )
6924 {
6925 if (*p == '\'')
6926 *r++ = '\'';
6927 MB_COPY_CHAR(p, r);
6928 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006929 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006930 if (function)
6931 *r++ = ')';
6932 *r++ = NUL;
6933 }
6934 return s;
6935}
6936
6937/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938 * Get the value of an environment variable.
6939 * "arg" is pointing to the '$'. It is advanced to after the name.
6940 * If the environment variable was not set, silently assume it is empty.
6941 * Always return OK.
6942 */
6943 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006944get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006946 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947 int evaluate;
6948{
6949 char_u *string = NULL;
6950 int len;
6951 int cc;
6952 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006953 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954
6955 ++*arg;
6956 name = *arg;
6957 len = get_env_len(arg);
6958 if (evaluate)
6959 {
6960 if (len != 0)
6961 {
6962 cc = name[len];
6963 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006964 /* first try vim_getenv(), fast for normal environment vars */
6965 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006967 {
6968 if (!mustfree)
6969 string = vim_strsave(string);
6970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 else
6972 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00006973 if (mustfree)
6974 vim_free(string);
6975
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 /* next try expanding things like $VIM and ${HOME} */
6977 string = expand_env_save(name - 1);
6978 if (string != NULL && *string == '$')
6979 {
6980 vim_free(string);
6981 string = NULL;
6982 }
6983 }
6984 name[len] = cc;
6985 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006986 rettv->v_type = VAR_STRING;
6987 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 }
6989
6990 return OK;
6991}
6992
6993/*
6994 * Array with names and number of arguments of all internal functions
6995 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
6996 */
6997static struct fst
6998{
6999 char *f_name; /* function name */
7000 char f_min_argc; /* minimal number of arguments */
7001 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007002 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007003 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004} functions[] =
7005{
Bram Moolenaar0d660222005-01-07 21:51:51 +00007006 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 {"append", 2, 2, f_append},
7008 {"argc", 0, 0, f_argc},
7009 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007010 {"argv", 0, 1, f_argv},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007012 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 {"bufexists", 1, 1, f_bufexists},
7014 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7015 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7016 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7017 {"buflisted", 1, 1, f_buflisted},
7018 {"bufloaded", 1, 1, f_bufloaded},
7019 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007020 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 {"bufwinnr", 1, 1, f_bufwinnr},
7022 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007023 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007024 {"call", 2, 3, f_call},
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007025 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 {"char2nr", 1, 1, f_char2nr},
7027 {"cindent", 1, 1, f_cindent},
7028 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007029#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007030 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007031 {"complete_add", 1, 1, f_complete_add},
7032 {"complete_check", 0, 0, f_complete_check},
7033#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007035 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007036 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007038 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007039 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040 {"delete", 1, 1, f_delete},
7041 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007042 {"diff_filler", 1, 1, f_diff_filler},
7043 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007044 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007046 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047 {"eventhandler", 0, 0, f_eventhandler},
7048 {"executable", 1, 1, f_executable},
7049 {"exists", 1, 1, f_exists},
7050 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007051 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007052 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7054 {"filereadable", 1, 1, f_filereadable},
7055 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007056 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007057 {"finddir", 1, 3, f_finddir},
7058 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 {"fnamemodify", 2, 2, f_fnamemodify},
7060 {"foldclosed", 1, 1, f_foldclosed},
7061 {"foldclosedend", 1, 1, f_foldclosedend},
7062 {"foldlevel", 1, 1, f_foldlevel},
7063 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007064 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007066 {"function", 1, 1, f_function},
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007067 {"garbagecollect", 0, 0, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007068 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007069 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 {"getbufvar", 2, 2, f_getbufvar},
7071 {"getchar", 0, 1, f_getchar},
7072 {"getcharmod", 0, 0, f_getcharmod},
7073 {"getcmdline", 0, 0, f_getcmdline},
7074 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007075 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007077 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007078 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 {"getfsize", 1, 1, f_getfsize},
7080 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007081 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007082 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007083 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaara5525202006-03-02 22:52:09 +00007084 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007085 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007086 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007088 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089 {"getwinposx", 0, 0, f_getwinposx},
7090 {"getwinposy", 0, 0, f_getwinposy},
7091 {"getwinvar", 2, 2, f_getwinvar},
7092 {"glob", 1, 1, f_glob},
7093 {"globpath", 2, 2, f_globpath},
7094 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007095 {"has_key", 2, 2, f_has_key},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007096 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7098 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7099 {"histadd", 2, 2, f_histadd},
7100 {"histdel", 1, 2, f_histdel},
7101 {"histget", 1, 2, f_histget},
7102 {"histnr", 1, 1, f_histnr},
7103 {"hlID", 1, 1, f_hlID},
7104 {"hlexists", 1, 1, f_hlexists},
7105 {"hostname", 0, 0, f_hostname},
7106 {"iconv", 3, 3, f_iconv},
7107 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007108 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007109 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007111 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112 {"inputrestore", 0, 0, f_inputrestore},
7113 {"inputsave", 0, 0, f_inputsave},
7114 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007115 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007117 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007118 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007119 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007120 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007122 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123 {"libcall", 3, 3, f_libcall},
7124 {"libcallnr", 3, 3, f_libcallnr},
7125 {"line", 1, 1, f_line},
7126 {"line2byte", 1, 1, f_line2byte},
7127 {"lispindent", 1, 1, f_lispindent},
7128 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007129 {"map", 2, 2, f_map},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007130 {"maparg", 1, 3, f_maparg},
7131 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007132 {"match", 2, 4, f_match},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007133 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007134 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007135 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007136 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007137 {"max", 1, 1, f_max},
7138 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007139#ifdef vim_mkdir
7140 {"mkdir", 1, 3, f_mkdir},
7141#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 {"mode", 0, 0, f_mode},
7143 {"nextnonblank", 1, 1, f_nextnonblank},
7144 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007145 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007147 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007148 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007149 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007150 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007151 {"reltime", 0, 2, f_reltime},
7152 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153 {"remote_expr", 2, 3, f_remote_expr},
7154 {"remote_foreground", 1, 1, f_remote_foreground},
7155 {"remote_peek", 1, 2, f_remote_peek},
7156 {"remote_read", 1, 1, f_remote_read},
7157 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007158 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007160 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007162 {"reverse", 1, 1, f_reverse},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00007163 {"search", 1, 3, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007164 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00007165 {"searchpair", 3, 6, f_searchpair},
7166 {"searchpairpos", 3, 6, f_searchpairpos},
7167 {"searchpos", 1, 3, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168 {"server2client", 2, 2, f_server2client},
7169 {"serverlist", 0, 0, f_serverlist},
7170 {"setbufvar", 3, 3, f_setbufvar},
7171 {"setcmdpos", 1, 1, f_setcmdpos},
7172 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007173 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007174 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007175 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 {"setreg", 2, 3, f_setreg},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007177 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar60a495f2006-10-03 12:44:42 +00007179 {"shellescape", 1, 1, f_shellescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007181 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007182 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007183 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007184 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007185 {"split", 1, 3, f_split},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007186 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187#ifdef HAVE_STRFTIME
7188 {"strftime", 1, 2, f_strftime},
7189#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007190 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007191 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 {"strlen", 1, 1, f_strlen},
7193 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007194 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195 {"strtrans", 1, 1, f_strtrans},
7196 {"submatch", 1, 1, f_submatch},
7197 {"substitute", 4, 4, f_substitute},
7198 {"synID", 3, 3, f_synID},
7199 {"synIDattr", 2, 3, f_synIDattr},
7200 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007201 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007202 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007203 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007204 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007205 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007206 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007208 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 {"tolower", 1, 1, f_tolower},
7210 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007211 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007213 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214 {"virtcol", 1, 1, f_virtcol},
7215 {"visualmode", 0, 1, f_visualmode},
7216 {"winbufnr", 1, 1, f_winbufnr},
7217 {"wincol", 0, 0, f_wincol},
7218 {"winheight", 1, 1, f_winheight},
7219 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007220 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007222 {"winrestview", 1, 1, f_winrestview},
7223 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007225 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226};
7227
7228#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7229
7230/*
7231 * Function given to ExpandGeneric() to obtain the list of internal
7232 * or user defined function names.
7233 */
7234 char_u *
7235get_function_name(xp, idx)
7236 expand_T *xp;
7237 int idx;
7238{
7239 static int intidx = -1;
7240 char_u *name;
7241
7242 if (idx == 0)
7243 intidx = -1;
7244 if (intidx < 0)
7245 {
7246 name = get_user_func_name(xp, idx);
7247 if (name != NULL)
7248 return name;
7249 }
7250 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7251 {
7252 STRCPY(IObuff, functions[intidx].f_name);
7253 STRCAT(IObuff, "(");
7254 if (functions[intidx].f_max_argc == 0)
7255 STRCAT(IObuff, ")");
7256 return IObuff;
7257 }
7258
7259 return NULL;
7260}
7261
7262/*
7263 * Function given to ExpandGeneric() to obtain the list of internal or
7264 * user defined variable or function names.
7265 */
7266/*ARGSUSED*/
7267 char_u *
7268get_expr_name(xp, idx)
7269 expand_T *xp;
7270 int idx;
7271{
7272 static int intidx = -1;
7273 char_u *name;
7274
7275 if (idx == 0)
7276 intidx = -1;
7277 if (intidx < 0)
7278 {
7279 name = get_function_name(xp, idx);
7280 if (name != NULL)
7281 return name;
7282 }
7283 return get_user_var_name(xp, ++intidx);
7284}
7285
7286#endif /* FEAT_CMDL_COMPL */
7287
7288/*
7289 * Find internal function in table above.
7290 * Return index, or -1 if not found
7291 */
7292 static int
7293find_internal_func(name)
7294 char_u *name; /* name of the function */
7295{
7296 int first = 0;
7297 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7298 int cmp;
7299 int x;
7300
7301 /*
7302 * Find the function name in the table. Binary search.
7303 */
7304 while (first <= last)
7305 {
7306 x = first + ((unsigned)(last - first) >> 1);
7307 cmp = STRCMP(name, functions[x].f_name);
7308 if (cmp < 0)
7309 last = x - 1;
7310 else if (cmp > 0)
7311 first = x + 1;
7312 else
7313 return x;
7314 }
7315 return -1;
7316}
7317
7318/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007319 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7320 * name it contains, otherwise return "name".
7321 */
7322 static char_u *
7323deref_func_name(name, lenp)
7324 char_u *name;
7325 int *lenp;
7326{
Bram Moolenaar33570922005-01-25 22:26:29 +00007327 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007328 int cc;
7329
7330 cc = name[*lenp];
7331 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007332 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007333 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007334 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007335 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007336 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007337 {
7338 *lenp = 0;
7339 return (char_u *)""; /* just in case */
7340 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007341 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00007342 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007343 }
7344
7345 return name;
7346}
7347
7348/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349 * Allocate a variable for the result of a function.
7350 * Return OK or FAIL.
7351 */
7352 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007353get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7354 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 char_u *name; /* name of the function */
7356 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007357 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358 char_u **arg; /* argument, pointing to the '(' */
7359 linenr_T firstline; /* first line of range */
7360 linenr_T lastline; /* last line of range */
7361 int *doesrange; /* return: function handled range */
7362 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007363 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364{
7365 char_u *argp;
7366 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007367 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368 int argcount = 0; /* number of arguments found */
7369
7370 /*
7371 * Get the arguments.
7372 */
7373 argp = *arg;
7374 while (argcount < MAX_FUNC_ARGS)
7375 {
7376 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7377 if (*argp == ')' || *argp == ',' || *argp == NUL)
7378 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7380 {
7381 ret = FAIL;
7382 break;
7383 }
7384 ++argcount;
7385 if (*argp != ',')
7386 break;
7387 }
7388 if (*argp == ')')
7389 ++argp;
7390 else
7391 ret = FAIL;
7392
7393 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007394 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007395 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007397 {
7398 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007399 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007400 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007401 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403
7404 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007405 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406
7407 *arg = skipwhite(argp);
7408 return ret;
7409}
7410
7411
7412/*
7413 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00007414 * Return OK when the function can't be called, FAIL otherwise.
7415 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416 */
7417 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007418call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007419 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 char_u *name; /* name of the function */
7421 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007422 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007424 typval_T *argvars; /* vars for arguments, must have "argcount"
7425 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426 linenr_T firstline; /* first line of range */
7427 linenr_T lastline; /* last line of range */
7428 int *doesrange; /* return: function handled range */
7429 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007430 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431{
7432 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433#define ERROR_UNKNOWN 0
7434#define ERROR_TOOMANY 1
7435#define ERROR_TOOFEW 2
7436#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007437#define ERROR_DICT 4
7438#define ERROR_NONE 5
7439#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440 int error = ERROR_NONE;
7441 int i;
7442 int llen;
7443 ufunc_T *fp;
7444 int cc;
7445#define FLEN_FIXED 40
7446 char_u fname_buf[FLEN_FIXED + 1];
7447 char_u *fname;
7448
7449 /*
7450 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7451 * Change <SNR>123_name() to K_SNR 123_name().
7452 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7453 */
7454 cc = name[len];
7455 name[len] = NUL;
7456 llen = eval_fname_script(name);
7457 if (llen > 0)
7458 {
7459 fname_buf[0] = K_SPECIAL;
7460 fname_buf[1] = KS_EXTRA;
7461 fname_buf[2] = (int)KE_SNR;
7462 i = 3;
7463 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7464 {
7465 if (current_SID <= 0)
7466 error = ERROR_SCRIPT;
7467 else
7468 {
7469 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7470 i = (int)STRLEN(fname_buf);
7471 }
7472 }
7473 if (i + STRLEN(name + llen) < FLEN_FIXED)
7474 {
7475 STRCPY(fname_buf + i, name + llen);
7476 fname = fname_buf;
7477 }
7478 else
7479 {
7480 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7481 if (fname == NULL)
7482 error = ERROR_OTHER;
7483 else
7484 {
7485 mch_memmove(fname, fname_buf, (size_t)i);
7486 STRCPY(fname + i, name + llen);
7487 }
7488 }
7489 }
7490 else
7491 fname = name;
7492
7493 *doesrange = FALSE;
7494
7495
7496 /* execute the function if no errors detected and executing */
7497 if (evaluate && error == ERROR_NONE)
7498 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007499 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500 error = ERROR_UNKNOWN;
7501
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007502 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503 {
7504 /*
7505 * User defined function.
7506 */
7507 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007508
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007510 /* Trigger FuncUndefined event, may load the function. */
7511 if (fp == NULL
7512 && apply_autocmds(EVENT_FUNCUNDEFINED,
7513 fname, fname, TRUE, NULL)
7514 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007516 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517 fp = find_func(fname);
7518 }
7519#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007520 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007521 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007522 {
7523 /* loaded a package, search for the function again */
7524 fp = find_func(fname);
7525 }
7526
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527 if (fp != NULL)
7528 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007529 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007531 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007533 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007535 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007536 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537 else
7538 {
7539 /*
7540 * Call the user function.
7541 * Save and restore search patterns, script variables and
7542 * redo buffer.
7543 */
7544 save_search_patterns();
7545 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007546 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007547 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007548 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007549 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7550 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7551 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007552 /* Function was unreferenced while being used, free it
7553 * now. */
7554 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007555 restoreRedobuff();
7556 restore_search_patterns();
7557 error = ERROR_NONE;
7558 }
7559 }
7560 }
7561 else
7562 {
7563 /*
7564 * Find the function name in the table, call its implementation.
7565 */
7566 i = find_internal_func(fname);
7567 if (i >= 0)
7568 {
7569 if (argcount < functions[i].f_min_argc)
7570 error = ERROR_TOOFEW;
7571 else if (argcount > functions[i].f_max_argc)
7572 error = ERROR_TOOMANY;
7573 else
7574 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007575 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007576 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007577 error = ERROR_NONE;
7578 }
7579 }
7580 }
7581 /*
7582 * The function call (or "FuncUndefined" autocommand sequence) might
7583 * have been aborted by an error, an interrupt, or an explicitly thrown
7584 * exception that has not been caught so far. This situation can be
7585 * tested for by calling aborting(). For an error in an internal
7586 * function or for the "E132" error in call_user_func(), however, the
7587 * throw point at which the "force_abort" flag (temporarily reset by
7588 * emsg()) is normally updated has not been reached yet. We need to
7589 * update that flag first to make aborting() reliable.
7590 */
7591 update_force_abort();
7592 }
7593 if (error == ERROR_NONE)
7594 ret = OK;
7595
7596 /*
7597 * Report an error unless the argument evaluation or function call has been
7598 * cancelled due to an aborting error, an interrupt, or an exception.
7599 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007600 if (!aborting())
7601 {
7602 switch (error)
7603 {
7604 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007605 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007606 break;
7607 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007608 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007609 break;
7610 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007611 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00007612 name);
7613 break;
7614 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007615 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00007616 name);
7617 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007618 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007619 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00007620 name);
7621 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007622 }
7623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624
7625 name[len] = cc;
7626 if (fname != name && fname != fname_buf)
7627 vim_free(fname);
7628
7629 return ret;
7630}
7631
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007632/*
7633 * Give an error message with a function name. Handle <SNR> things.
7634 */
7635 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00007636emsg_funcname(ermsg, name)
7637 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007638 char_u *name;
7639{
7640 char_u *p;
7641
7642 if (*name == K_SPECIAL)
7643 p = concat_str((char_u *)"<SNR>", name + 3);
7644 else
7645 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00007646 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007647 if (p != name)
7648 vim_free(p);
7649}
7650
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651/*********************************************
7652 * Implementation of the built-in functions
7653 */
7654
7655/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007656 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657 */
7658 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007659f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007660 typval_T *argvars;
7661 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662{
Bram Moolenaar33570922005-01-25 22:26:29 +00007663 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007665 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007666 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007668 if ((l = argvars[0].vval.v_list) != NULL
7669 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7670 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007671 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007672 }
7673 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007674 EMSG(_(e_listreq));
7675}
7676
7677/*
7678 * "append(lnum, string/list)" function
7679 */
7680 static void
7681f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007682 typval_T *argvars;
7683 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007684{
7685 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007686 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007687 list_T *l = NULL;
7688 listitem_T *li = NULL;
7689 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007690 long added = 0;
7691
Bram Moolenaar0d660222005-01-07 21:51:51 +00007692 lnum = get_tv_lnum(argvars);
7693 if (lnum >= 0
7694 && lnum <= curbuf->b_ml.ml_line_count
7695 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007696 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007697 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007698 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007699 l = argvars[1].vval.v_list;
7700 if (l == NULL)
7701 return;
7702 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007703 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007704 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007705 for (;;)
7706 {
7707 if (l == NULL)
7708 tv = &argvars[1]; /* append a string */
7709 else if (li == NULL)
7710 break; /* end of list */
7711 else
7712 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007713 line = get_tv_string_chk(tv);
7714 if (line == NULL) /* type error */
7715 {
7716 rettv->vval.v_number = 1; /* Failed */
7717 break;
7718 }
7719 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007720 ++added;
7721 if (l == NULL)
7722 break;
7723 li = li->li_next;
7724 }
7725
7726 appended_lines_mark(lnum, added);
7727 if (curwin->w_cursor.lnum > lnum)
7728 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007730 else
7731 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732}
7733
7734/*
7735 * "argc()" function
7736 */
7737/* ARGSUSED */
7738 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007739f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007740 typval_T *argvars;
7741 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007743 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744}
7745
7746/*
7747 * "argidx()" function
7748 */
7749/* ARGSUSED */
7750 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007751f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007752 typval_T *argvars;
7753 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007755 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756}
7757
7758/*
7759 * "argv(nr)" function
7760 */
7761 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007762f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007763 typval_T *argvars;
7764 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765{
7766 int idx;
7767
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007768 if (argvars[0].v_type != VAR_UNKNOWN)
7769 {
7770 idx = get_tv_number_chk(&argvars[0], NULL);
7771 if (idx >= 0 && idx < ARGCOUNT)
7772 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
7773 else
7774 rettv->vval.v_string = NULL;
7775 rettv->v_type = VAR_STRING;
7776 }
7777 else if (rettv_list_alloc(rettv) == OK)
7778 for (idx = 0; idx < ARGCOUNT; ++idx)
7779 list_append_string(rettv->vval.v_list,
7780 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781}
7782
7783/*
7784 * "browse(save, title, initdir, default)" function
7785 */
7786/* ARGSUSED */
7787 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007788f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007789 typval_T *argvars;
7790 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791{
7792#ifdef FEAT_BROWSE
7793 int save;
7794 char_u *title;
7795 char_u *initdir;
7796 char_u *defname;
7797 char_u buf[NUMBUFLEN];
7798 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007799 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007801 save = get_tv_number_chk(&argvars[0], &error);
7802 title = get_tv_string_chk(&argvars[1]);
7803 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7804 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007806 if (error || title == NULL || initdir == NULL || defname == NULL)
7807 rettv->vval.v_string = NULL;
7808 else
7809 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007810 do_browse(save ? BROWSE_SAVE : 0,
7811 title, defname, NULL, initdir, NULL, curbuf);
7812#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007813 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007814#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007815 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007816}
7817
7818/*
7819 * "browsedir(title, initdir)" function
7820 */
7821/* ARGSUSED */
7822 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007823f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007824 typval_T *argvars;
7825 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007826{
7827#ifdef FEAT_BROWSE
7828 char_u *title;
7829 char_u *initdir;
7830 char_u buf[NUMBUFLEN];
7831
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007832 title = get_tv_string_chk(&argvars[0]);
7833 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007834
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007835 if (title == NULL || initdir == NULL)
7836 rettv->vval.v_string = NULL;
7837 else
7838 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007839 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007841 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007843 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844}
7845
Bram Moolenaar33570922005-01-25 22:26:29 +00007846static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007847
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848/*
7849 * Find a buffer by number or exact name.
7850 */
7851 static buf_T *
7852find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007853 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854{
7855 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007857 if (avar->v_type == VAR_NUMBER)
7858 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007859 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007861 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007862 if (buf == NULL)
7863 {
7864 /* No full path name match, try a match with a URL or a "nofile"
7865 * buffer, these don't use the full path. */
7866 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7867 if (buf->b_fname != NULL
7868 && (path_with_url(buf->b_fname)
7869#ifdef FEAT_QUICKFIX
7870 || bt_nofile(buf)
7871#endif
7872 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007873 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007874 break;
7875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876 }
7877 return buf;
7878}
7879
7880/*
7881 * "bufexists(expr)" function
7882 */
7883 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007884f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007885 typval_T *argvars;
7886 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007888 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889}
7890
7891/*
7892 * "buflisted(expr)" function
7893 */
7894 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007895f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007896 typval_T *argvars;
7897 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898{
7899 buf_T *buf;
7900
7901 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007902 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903}
7904
7905/*
7906 * "bufloaded(expr)" function
7907 */
7908 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007909f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007910 typval_T *argvars;
7911 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912{
7913 buf_T *buf;
7914
7915 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007916 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007917}
7918
Bram Moolenaar33570922005-01-25 22:26:29 +00007919static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007920
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921/*
7922 * Get buffer by number or pattern.
7923 */
7924 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007925get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007926 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007928 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929 int save_magic;
7930 char_u *save_cpo;
7931 buf_T *buf;
7932
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007933 if (tv->v_type == VAR_NUMBER)
7934 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007935 if (tv->v_type != VAR_STRING)
7936 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937 if (name == NULL || *name == NUL)
7938 return curbuf;
7939 if (name[0] == '$' && name[1] == NUL)
7940 return lastbuf;
7941
7942 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7943 save_magic = p_magic;
7944 p_magic = TRUE;
7945 save_cpo = p_cpo;
7946 p_cpo = (char_u *)"";
7947
7948 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
7949 TRUE, FALSE));
7950
7951 p_magic = save_magic;
7952 p_cpo = save_cpo;
7953
7954 /* If not found, try expanding the name, like done for bufexists(). */
7955 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007956 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957
7958 return buf;
7959}
7960
7961/*
7962 * "bufname(expr)" function
7963 */
7964 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007965f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007966 typval_T *argvars;
7967 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968{
7969 buf_T *buf;
7970
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007971 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007973 buf = get_buf_tv(&argvars[0]);
7974 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007976 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007978 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979 --emsg_off;
7980}
7981
7982/*
7983 * "bufnr(expr)" function
7984 */
7985 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007986f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007987 typval_T *argvars;
7988 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989{
7990 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007991 int error = FALSE;
7992 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007994 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007996 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007997 --emsg_off;
7998
7999 /* If the buffer isn't found and the second argument is not zero create a
8000 * new buffer. */
8001 if (buf == NULL
8002 && argvars[1].v_type != VAR_UNKNOWN
8003 && get_tv_number_chk(&argvars[1], &error) != 0
8004 && !error
8005 && (name = get_tv_string_chk(&argvars[0])) != NULL
8006 && !error)
8007 buf = buflist_new(name, NULL, (linenr_T)1, 0);
8008
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008010 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008012 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013}
8014
8015/*
8016 * "bufwinnr(nr)" function
8017 */
8018 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008019f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008020 typval_T *argvars;
8021 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022{
8023#ifdef FEAT_WINDOWS
8024 win_T *wp;
8025 int winnr = 0;
8026#endif
8027 buf_T *buf;
8028
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008029 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008031 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032#ifdef FEAT_WINDOWS
8033 for (wp = firstwin; wp; wp = wp->w_next)
8034 {
8035 ++winnr;
8036 if (wp->w_buffer == buf)
8037 break;
8038 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008039 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008041 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042#endif
8043 --emsg_off;
8044}
8045
8046/*
8047 * "byte2line(byte)" function
8048 */
8049/*ARGSUSED*/
8050 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008051f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008052 typval_T *argvars;
8053 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008054{
8055#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008056 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057#else
8058 long boff = 0;
8059
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008060 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008062 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008064 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065 (linenr_T)0, &boff);
8066#endif
8067}
8068
8069/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008070 * "byteidx()" function
8071 */
8072/*ARGSUSED*/
8073 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008074f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008075 typval_T *argvars;
8076 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008077{
8078#ifdef FEAT_MBYTE
8079 char_u *t;
8080#endif
8081 char_u *str;
8082 long idx;
8083
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008084 str = get_tv_string_chk(&argvars[0]);
8085 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008086 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008087 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008088 return;
8089
8090#ifdef FEAT_MBYTE
8091 t = str;
8092 for ( ; idx > 0; idx--)
8093 {
8094 if (*t == NUL) /* EOL reached */
8095 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008096 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008097 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008098 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008099#else
8100 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008101 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008102#endif
8103}
8104
8105/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008106 * "call(func, arglist)" function
8107 */
8108 static void
8109f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008110 typval_T *argvars;
8111 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008112{
8113 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008114 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008115 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008116 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008117 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00008118 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008119
8120 rettv->vval.v_number = 0;
8121 if (argvars[1].v_type != VAR_LIST)
8122 {
8123 EMSG(_(e_listreq));
8124 return;
8125 }
8126 if (argvars[1].vval.v_list == NULL)
8127 return;
8128
8129 if (argvars[0].v_type == VAR_FUNC)
8130 func = argvars[0].vval.v_string;
8131 else
8132 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008133 if (*func == NUL)
8134 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008135
Bram Moolenaare9a41262005-01-15 22:18:47 +00008136 if (argvars[2].v_type != VAR_UNKNOWN)
8137 {
8138 if (argvars[2].v_type != VAR_DICT)
8139 {
8140 EMSG(_(e_dictreq));
8141 return;
8142 }
8143 selfdict = argvars[2].vval.v_dict;
8144 }
8145
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008146 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
8147 item = item->li_next)
8148 {
8149 if (argc == MAX_FUNC_ARGS)
8150 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008151 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008152 break;
8153 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008154 /* Make a copy of each argument. This is needed to be able to set
8155 * v_lock to VAR_FIXED in the copy without changing the original list.
8156 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008157 copy_tv(&item->li_tv, &argv[argc++]);
8158 }
8159
8160 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008161 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008162 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
8163 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008164
8165 /* Free the arguments. */
8166 while (argc > 0)
8167 clear_tv(&argv[--argc]);
8168}
8169
8170/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008171 * "changenr()" function
8172 */
8173/*ARGSUSED*/
8174 static void
8175f_changenr(argvars, rettv)
8176 typval_T *argvars;
8177 typval_T *rettv;
8178{
8179 rettv->vval.v_number = curbuf->b_u_seq_cur;
8180}
8181
8182/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183 * "char2nr(string)" function
8184 */
8185 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008186f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008187 typval_T *argvars;
8188 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189{
8190#ifdef FEAT_MBYTE
8191 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008192 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193 else
8194#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008195 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196}
8197
8198/*
8199 * "cindent(lnum)" function
8200 */
8201 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008202f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008203 typval_T *argvars;
8204 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008205{
8206#ifdef FEAT_CINDENT
8207 pos_T pos;
8208 linenr_T lnum;
8209
8210 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008211 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8213 {
8214 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008215 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 curwin->w_cursor = pos;
8217 }
8218 else
8219#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008220 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221}
8222
8223/*
8224 * "col(string)" function
8225 */
8226 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008227f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008228 typval_T *argvars;
8229 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230{
8231 colnr_T col = 0;
8232 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008233 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008235 fp = var2fpos(&argvars[0], FALSE, &fnum);
8236 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237 {
8238 if (fp->col == MAXCOL)
8239 {
8240 /* '> can be MAXCOL, get the length of the line then */
8241 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008242 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243 else
8244 col = MAXCOL;
8245 }
8246 else
8247 {
8248 col = fp->col + 1;
8249#ifdef FEAT_VIRTUALEDIT
8250 /* col(".") when the cursor is on the NUL at the end of the line
8251 * because of "coladd" can be seen as an extra column. */
8252 if (virtual_active() && fp == &curwin->w_cursor)
8253 {
8254 char_u *p = ml_get_cursor();
8255
8256 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
8257 curwin->w_virtcol - curwin->w_cursor.coladd))
8258 {
8259# ifdef FEAT_MBYTE
8260 int l;
8261
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008262 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263 col += l;
8264# else
8265 if (*p != NUL && p[1] == NUL)
8266 ++col;
8267# endif
8268 }
8269 }
8270#endif
8271 }
8272 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008273 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274}
8275
Bram Moolenaar572cb562005-08-05 21:35:02 +00008276#if defined(FEAT_INS_EXPAND)
8277/*
Bram Moolenaarade00832006-03-10 21:46:58 +00008278 * "complete()" function
8279 */
8280/*ARGSUSED*/
8281 static void
8282f_complete(argvars, rettv)
8283 typval_T *argvars;
8284 typval_T *rettv;
8285{
8286 int startcol;
8287
8288 if ((State & INSERT) == 0)
8289 {
8290 EMSG(_("E785: complete() can only be used in Insert mode"));
8291 return;
8292 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00008293
8294 /* Check for undo allowed here, because if something was already inserted
8295 * the line was already saved for undo and this check isn't done. */
8296 if (!undo_allowed())
8297 return;
8298
Bram Moolenaarade00832006-03-10 21:46:58 +00008299 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
8300 {
8301 EMSG(_(e_invarg));
8302 return;
8303 }
8304
8305 startcol = get_tv_number_chk(&argvars[0], NULL);
8306 if (startcol <= 0)
8307 return;
8308
8309 set_completion(startcol - 1, argvars[1].vval.v_list);
8310}
8311
8312/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00008313 * "complete_add()" function
8314 */
8315/*ARGSUSED*/
8316 static void
8317f_complete_add(argvars, rettv)
8318 typval_T *argvars;
8319 typval_T *rettv;
8320{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00008321 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00008322}
8323
8324/*
8325 * "complete_check()" function
8326 */
8327/*ARGSUSED*/
8328 static void
8329f_complete_check(argvars, rettv)
8330 typval_T *argvars;
8331 typval_T *rettv;
8332{
8333 int saved = RedrawingDisabled;
8334
8335 RedrawingDisabled = 0;
8336 ins_compl_check_keys(0);
8337 rettv->vval.v_number = compl_interrupted;
8338 RedrawingDisabled = saved;
8339}
8340#endif
8341
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342/*
8343 * "confirm(message, buttons[, default [, type]])" function
8344 */
8345/*ARGSUSED*/
8346 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008347f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008348 typval_T *argvars;
8349 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350{
8351#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
8352 char_u *message;
8353 char_u *buttons = NULL;
8354 char_u buf[NUMBUFLEN];
8355 char_u buf2[NUMBUFLEN];
8356 int def = 1;
8357 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008358 char_u *typestr;
8359 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008361 message = get_tv_string_chk(&argvars[0]);
8362 if (message == NULL)
8363 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008364 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008366 buttons = get_tv_string_buf_chk(&argvars[1], buf);
8367 if (buttons == NULL)
8368 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008369 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008371 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008372 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008374 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
8375 if (typestr == NULL)
8376 error = TRUE;
8377 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008379 switch (TOUPPER_ASC(*typestr))
8380 {
8381 case 'E': type = VIM_ERROR; break;
8382 case 'Q': type = VIM_QUESTION; break;
8383 case 'I': type = VIM_INFO; break;
8384 case 'W': type = VIM_WARNING; break;
8385 case 'G': type = VIM_GENERIC; break;
8386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387 }
8388 }
8389 }
8390 }
8391
8392 if (buttons == NULL || *buttons == NUL)
8393 buttons = (char_u *)_("&Ok");
8394
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008395 if (error)
8396 rettv->vval.v_number = 0;
8397 else
8398 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399 def, NULL);
8400#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008401 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402#endif
8403}
8404
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008405/*
8406 * "copy()" function
8407 */
8408 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008409f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008410 typval_T *argvars;
8411 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008412{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008413 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008414}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008415
8416/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008417 * "count()" function
8418 */
8419 static void
8420f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008421 typval_T *argvars;
8422 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008423{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008424 long n = 0;
8425 int ic = FALSE;
8426
Bram Moolenaare9a41262005-01-15 22:18:47 +00008427 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008428 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008429 listitem_T *li;
8430 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008431 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008432
Bram Moolenaare9a41262005-01-15 22:18:47 +00008433 if ((l = argvars[0].vval.v_list) != NULL)
8434 {
8435 li = l->lv_first;
8436 if (argvars[2].v_type != VAR_UNKNOWN)
8437 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008438 int error = FALSE;
8439
8440 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008441 if (argvars[3].v_type != VAR_UNKNOWN)
8442 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008443 idx = get_tv_number_chk(&argvars[3], &error);
8444 if (!error)
8445 {
8446 li = list_find(l, idx);
8447 if (li == NULL)
8448 EMSGN(_(e_listidx), idx);
8449 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008450 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008451 if (error)
8452 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008453 }
8454
8455 for ( ; li != NULL; li = li->li_next)
8456 if (tv_equal(&li->li_tv, &argvars[1], ic))
8457 ++n;
8458 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008459 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008460 else if (argvars[0].v_type == VAR_DICT)
8461 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008462 int todo;
8463 dict_T *d;
8464 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008465
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008466 if ((d = argvars[0].vval.v_dict) != NULL)
8467 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008468 int error = FALSE;
8469
Bram Moolenaare9a41262005-01-15 22:18:47 +00008470 if (argvars[2].v_type != VAR_UNKNOWN)
8471 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008472 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008473 if (argvars[3].v_type != VAR_UNKNOWN)
8474 EMSG(_(e_invarg));
8475 }
8476
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008477 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008478 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008479 {
8480 if (!HASHITEM_EMPTY(hi))
8481 {
8482 --todo;
8483 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
8484 ++n;
8485 }
8486 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008487 }
8488 }
8489 else
8490 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008491 rettv->vval.v_number = n;
8492}
8493
8494/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008495 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8496 *
8497 * Checks the existence of a cscope connection.
8498 */
8499/*ARGSUSED*/
8500 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008501f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008502 typval_T *argvars;
8503 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504{
8505#ifdef FEAT_CSCOPE
8506 int num = 0;
8507 char_u *dbpath = NULL;
8508 char_u *prepend = NULL;
8509 char_u buf[NUMBUFLEN];
8510
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008511 if (argvars[0].v_type != VAR_UNKNOWN
8512 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008514 num = (int)get_tv_number(&argvars[0]);
8515 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008516 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008517 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518 }
8519
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008520 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008522 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523#endif
8524}
8525
8526/*
8527 * "cursor(lnum, col)" function
8528 *
8529 * Moves the cursor to the specified line and column
8530 */
8531/*ARGSUSED*/
8532 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008533f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008534 typval_T *argvars;
8535 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536{
8537 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008538#ifdef FEAT_VIRTUALEDIT
8539 long coladd = 0;
8540#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541
Bram Moolenaara5525202006-03-02 22:52:09 +00008542 if (argvars[1].v_type == VAR_UNKNOWN)
8543 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008544 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00008545
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008546 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00008547 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008548 line = pos.lnum;
8549 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008550#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008551 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00008552#endif
8553 }
8554 else
8555 {
8556 line = get_tv_lnum(argvars);
8557 col = get_tv_number_chk(&argvars[1], NULL);
8558#ifdef FEAT_VIRTUALEDIT
8559 if (argvars[2].v_type != VAR_UNKNOWN)
8560 coladd = get_tv_number_chk(&argvars[2], NULL);
8561#endif
8562 }
8563 if (line < 0 || col < 0
8564#ifdef FEAT_VIRTUALEDIT
8565 || coladd < 0
8566#endif
8567 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008568 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569 if (line > 0)
8570 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571 if (col > 0)
8572 curwin->w_cursor.col = col - 1;
8573#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00008574 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575#endif
8576
8577 /* Make sure the cursor is in a valid position. */
8578 check_cursor();
8579#ifdef FEAT_MBYTE
8580 /* Correct cursor for multi-byte character. */
8581 if (has_mbyte)
8582 mb_adjust_cursor();
8583#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008584
8585 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586}
8587
8588/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008589 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590 */
8591 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008592f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008593 typval_T *argvars;
8594 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008596 int noref = 0;
8597
8598 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008599 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008600 if (noref < 0 || noref > 1)
8601 EMSG(_(e_invarg));
8602 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008603 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604}
8605
8606/*
8607 * "delete()" function
8608 */
8609 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008610f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008611 typval_T *argvars;
8612 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613{
8614 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008615 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008617 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618}
8619
8620/*
8621 * "did_filetype()" function
8622 */
8623/*ARGSUSED*/
8624 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008625f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008626 typval_T *argvars;
8627 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628{
8629#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008630 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008631#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008632 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633#endif
8634}
8635
8636/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008637 * "diff_filler()" function
8638 */
8639/*ARGSUSED*/
8640 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008641f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008642 typval_T *argvars;
8643 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008644{
8645#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008646 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008647#endif
8648}
8649
8650/*
8651 * "diff_hlID()" function
8652 */
8653/*ARGSUSED*/
8654 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008655f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008656 typval_T *argvars;
8657 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008658{
8659#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008660 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008661 static linenr_T prev_lnum = 0;
8662 static int changedtick = 0;
8663 static int fnum = 0;
8664 static int change_start = 0;
8665 static int change_end = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008666 static hlf_T hlID = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008667 int filler_lines;
8668 int col;
8669
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008670 if (lnum < 0) /* ignore type error in {lnum} arg */
8671 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008672 if (lnum != prev_lnum
8673 || changedtick != curbuf->b_changedtick
8674 || fnum != curbuf->b_fnum)
8675 {
8676 /* New line, buffer, change: need to get the values. */
8677 filler_lines = diff_check(curwin, lnum);
8678 if (filler_lines < 0)
8679 {
8680 if (filler_lines == -1)
8681 {
8682 change_start = MAXCOL;
8683 change_end = -1;
8684 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8685 hlID = HLF_ADD; /* added line */
8686 else
8687 hlID = HLF_CHD; /* changed line */
8688 }
8689 else
8690 hlID = HLF_ADD; /* added line */
8691 }
8692 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008693 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008694 prev_lnum = lnum;
8695 changedtick = curbuf->b_changedtick;
8696 fnum = curbuf->b_fnum;
8697 }
8698
8699 if (hlID == HLF_CHD || hlID == HLF_TXD)
8700 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008701 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008702 if (col >= change_start && col <= change_end)
8703 hlID = HLF_TXD; /* changed text */
8704 else
8705 hlID = HLF_CHD; /* changed line */
8706 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008707 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008708#endif
8709}
8710
8711/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008712 * "empty({expr})" function
8713 */
8714 static void
8715f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008716 typval_T *argvars;
8717 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008718{
8719 int n;
8720
8721 switch (argvars[0].v_type)
8722 {
8723 case VAR_STRING:
8724 case VAR_FUNC:
8725 n = argvars[0].vval.v_string == NULL
8726 || *argvars[0].vval.v_string == NUL;
8727 break;
8728 case VAR_NUMBER:
8729 n = argvars[0].vval.v_number == 0;
8730 break;
8731 case VAR_LIST:
8732 n = argvars[0].vval.v_list == NULL
8733 || argvars[0].vval.v_list->lv_first == NULL;
8734 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008735 case VAR_DICT:
8736 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008737 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008738 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008739 default:
8740 EMSG2(_(e_intern2), "f_empty()");
8741 n = 0;
8742 }
8743
8744 rettv->vval.v_number = n;
8745}
8746
8747/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748 * "escape({string}, {chars})" function
8749 */
8750 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008751f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008752 typval_T *argvars;
8753 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754{
8755 char_u buf[NUMBUFLEN];
8756
Bram Moolenaar758711c2005-02-02 23:11:38 +00008757 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8758 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008759 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760}
8761
8762/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008763 * "eval()" function
8764 */
8765/*ARGSUSED*/
8766 static void
8767f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008768 typval_T *argvars;
8769 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008770{
8771 char_u *s;
8772
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008773 s = get_tv_string_chk(&argvars[0]);
8774 if (s != NULL)
8775 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008776
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008777 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8778 {
8779 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008780 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008781 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008782 else if (*s != NUL)
8783 EMSG(_(e_trailing));
8784}
8785
8786/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787 * "eventhandler()" function
8788 */
8789/*ARGSUSED*/
8790 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008791f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008792 typval_T *argvars;
8793 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008795 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008796}
8797
8798/*
8799 * "executable()" function
8800 */
8801 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008802f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008803 typval_T *argvars;
8804 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008806 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807}
8808
8809/*
8810 * "exists()" function
8811 */
8812 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008813f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008814 typval_T *argvars;
8815 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816{
8817 char_u *p;
8818 char_u *name;
8819 int n = FALSE;
8820 int len = 0;
8821
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008822 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008823 if (*p == '$') /* environment variable */
8824 {
8825 /* first try "normal" environment variables (fast) */
8826 if (mch_getenv(p + 1) != NULL)
8827 n = TRUE;
8828 else
8829 {
8830 /* try expanding things like $VIM and ${HOME} */
8831 p = expand_env_save(p);
8832 if (p != NULL && *p != '$')
8833 n = TRUE;
8834 vim_free(p);
8835 }
8836 }
8837 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00008838 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008839 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00008840 if (*skipwhite(p) != NUL)
8841 n = FALSE; /* trailing garbage */
8842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843 else if (*p == '*') /* internal or user defined function */
8844 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008845 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846 }
8847 else if (*p == ':')
8848 {
8849 n = cmd_exists(p + 1);
8850 }
8851 else if (*p == '#')
8852 {
8853#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00008854 if (p[1] == '#')
8855 n = autocmd_supported(p + 2);
8856 else
8857 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858#endif
8859 }
8860 else /* internal variable */
8861 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008862 char_u *tofree;
8863 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008864
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008865 /* get_name_len() takes care of expanding curly braces */
8866 name = p;
8867 len = get_name_len(&p, &tofree, TRUE, FALSE);
8868 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008870 if (tofree != NULL)
8871 name = tofree;
8872 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8873 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008875 /* handle d.key, l[idx], f(expr) */
8876 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8877 if (n)
8878 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008879 }
8880 }
Bram Moolenaar79783442006-05-05 21:18:03 +00008881 if (*p != NUL)
8882 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008884 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885 }
8886
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008887 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888}
8889
8890/*
8891 * "expand()" function
8892 */
8893 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008894f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008895 typval_T *argvars;
8896 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008897{
8898 char_u *s;
8899 int len;
8900 char_u *errormsg;
8901 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8902 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008903 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008905 rettv->v_type = VAR_STRING;
8906 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008907 if (*s == '%' || *s == '#' || *s == '<')
8908 {
8909 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008910 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008911 --emsg_off;
8912 }
8913 else
8914 {
8915 /* When the optional second argument is non-zero, don't remove matches
8916 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008917 if (argvars[1].v_type != VAR_UNKNOWN
8918 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008920 if (!error)
8921 {
8922 ExpandInit(&xpc);
8923 xpc.xp_context = EXPAND_FILES;
8924 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008925 }
8926 else
8927 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928 }
8929}
8930
8931/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008932 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00008933 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008934 */
8935 static void
8936f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008937 typval_T *argvars;
8938 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008939{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008940 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008941 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008942 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008943 list_T *l1, *l2;
8944 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008945 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008946 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008947
Bram Moolenaare9a41262005-01-15 22:18:47 +00008948 l1 = argvars[0].vval.v_list;
8949 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008950 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
8951 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008952 {
8953 if (argvars[2].v_type != VAR_UNKNOWN)
8954 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008955 before = get_tv_number_chk(&argvars[2], &error);
8956 if (error)
8957 return; /* type error; errmsg already given */
8958
Bram Moolenaar758711c2005-02-02 23:11:38 +00008959 if (before == l1->lv_len)
8960 item = NULL;
8961 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008962 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00008963 item = list_find(l1, before);
8964 if (item == NULL)
8965 {
8966 EMSGN(_(e_listidx), before);
8967 return;
8968 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008969 }
8970 }
8971 else
8972 item = NULL;
8973 list_extend(l1, l2, item);
8974
Bram Moolenaare9a41262005-01-15 22:18:47 +00008975 copy_tv(&argvars[0], rettv);
8976 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008977 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008978 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
8979 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008980 dict_T *d1, *d2;
8981 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008982 char_u *action;
8983 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00008984 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008985 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008986
8987 d1 = argvars[0].vval.v_dict;
8988 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008989 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
8990 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008991 {
8992 /* Check the third argument. */
8993 if (argvars[2].v_type != VAR_UNKNOWN)
8994 {
8995 static char *(av[]) = {"keep", "force", "error"};
8996
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008997 action = get_tv_string_chk(&argvars[2]);
8998 if (action == NULL)
8999 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009000 for (i = 0; i < 3; ++i)
9001 if (STRCMP(action, av[i]) == 0)
9002 break;
9003 if (i == 3)
9004 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009005 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009006 return;
9007 }
9008 }
9009 else
9010 action = (char_u *)"force";
9011
9012 /* Go over all entries in the second dict and add them to the
9013 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009014 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009015 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009016 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009017 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009018 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009019 --todo;
9020 di1 = dict_find(d1, hi2->hi_key, -1);
9021 if (di1 == NULL)
9022 {
9023 di1 = dictitem_copy(HI2DI(hi2));
9024 if (di1 != NULL && dict_add(d1, di1) == FAIL)
9025 dictitem_free(di1);
9026 }
9027 else if (*action == 'e')
9028 {
9029 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
9030 break;
9031 }
9032 else if (*action == 'f')
9033 {
9034 clear_tv(&di1->di_tv);
9035 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
9036 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009037 }
9038 }
9039
Bram Moolenaare9a41262005-01-15 22:18:47 +00009040 copy_tv(&argvars[0], rettv);
9041 }
9042 }
9043 else
9044 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009045}
9046
9047/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009048 * "feedkeys()" function
9049 */
9050/*ARGSUSED*/
9051 static void
9052f_feedkeys(argvars, rettv)
9053 typval_T *argvars;
9054 typval_T *rettv;
9055{
9056 int remap = TRUE;
9057 char_u *keys, *flags;
9058 char_u nbuf[NUMBUFLEN];
9059 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009060 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009061
9062 rettv->vval.v_number = 0;
9063 keys = get_tv_string(&argvars[0]);
9064 if (*keys != NUL)
9065 {
9066 if (argvars[1].v_type != VAR_UNKNOWN)
9067 {
9068 flags = get_tv_string_buf(&argvars[1], nbuf);
9069 for ( ; *flags != NUL; ++flags)
9070 {
9071 switch (*flags)
9072 {
9073 case 'n': remap = FALSE; break;
9074 case 'm': remap = TRUE; break;
9075 case 't': typed = TRUE; break;
9076 }
9077 }
9078 }
9079
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009080 /* Need to escape K_SPECIAL and CSI before putting the string in the
9081 * typeahead buffer. */
9082 keys_esc = vim_strsave_escape_csi(keys);
9083 if (keys_esc != NULL)
9084 {
9085 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009086 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009087 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009088 if (vgetc_busy)
9089 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009090 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009091 }
9092}
9093
9094/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009095 * "filereadable()" function
9096 */
9097 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009098f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009099 typval_T *argvars;
9100 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101{
9102 FILE *fd;
9103 char_u *p;
9104 int n;
9105
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009106 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
9108 {
9109 n = TRUE;
9110 fclose(fd);
9111 }
9112 else
9113 n = FALSE;
9114
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009115 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009116}
9117
9118/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009119 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00009120 * rights to write into.
9121 */
9122 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009123f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009124 typval_T *argvars;
9125 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009126{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009127 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128}
9129
Bram Moolenaar33570922005-01-25 22:26:29 +00009130static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009131
9132 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00009133findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00009134 typval_T *argvars;
9135 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009136 int dir;
9137{
9138#ifdef FEAT_SEARCHPATH
9139 char_u *fname;
9140 char_u *fresult = NULL;
9141 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
9142 char_u *p;
9143 char_u pathbuf[NUMBUFLEN];
9144 int count = 1;
9145 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009146 int error = FALSE;
9147#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009148
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009149 rettv->vval.v_string = NULL;
9150 rettv->v_type = VAR_STRING;
9151
9152#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009153 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009154
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009155 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009156 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009157 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
9158 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009159 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009160 else
9161 {
9162 if (*p != NUL)
9163 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009164
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009165 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009166 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009167 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009168 }
9169
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009170 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
9171 error = TRUE;
9172
9173 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009174 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009175 do
9176 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009177 if (rettv->v_type == VAR_STRING)
9178 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009179 fresult = find_file_in_path_option(first ? fname : NULL,
9180 first ? (int)STRLEN(fname) : 0,
Bram Moolenaare580b0c2006-03-21 21:33:03 +00009181 0, first, path, dir, NULL,
9182 dir ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009183 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009184
9185 if (fresult != NULL && rettv->v_type == VAR_LIST)
9186 list_append_string(rettv->vval.v_list, fresult, -1);
9187
9188 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009189 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009190
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009191 if (rettv->v_type == VAR_STRING)
9192 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009193#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009194}
9195
Bram Moolenaar33570922005-01-25 22:26:29 +00009196static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
9197static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009198
9199/*
9200 * Implementation of map() and filter().
9201 */
9202 static void
9203filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00009204 typval_T *argvars;
9205 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009206 int map;
9207{
9208 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00009209 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00009210 listitem_T *li, *nli;
9211 list_T *l = NULL;
9212 dictitem_T *di;
9213 hashtab_T *ht;
9214 hashitem_T *hi;
9215 dict_T *d = NULL;
9216 typval_T save_val;
9217 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009218 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009219 int todo;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009220 char_u *ermsg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009221 int save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009222
9223 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009224 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009225 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009226 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009227 || (map && tv_check_lock(l->lv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009228 return;
9229 }
9230 else if (argvars[0].v_type == VAR_DICT)
9231 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009232 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009233 || (map && tv_check_lock(d->dv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009234 return;
9235 }
9236 else
9237 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009238 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009239 return;
9240 }
9241
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009242 expr = get_tv_string_buf_chk(&argvars[1], buf);
9243 /* On type errors, the preceding call has already displayed an error
9244 * message. Avoid a misleading error message for an empty string that
9245 * was not passed as argument. */
9246 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009247 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009248 prepare_vimvar(VV_VAL, &save_val);
9249 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009250
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009251 /* We reset "did_emsg" to be able to detect whether an error
9252 * occurred during evaluation of the expression. */
9253 save_did_emsg = did_emsg;
9254 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009255
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009256 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009257 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009258 prepare_vimvar(VV_KEY, &save_key);
9259 vimvars[VV_KEY].vv_type = VAR_STRING;
9260
9261 ht = &d->dv_hashtab;
9262 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009263 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009264 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009265 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009266 if (!HASHITEM_EMPTY(hi))
9267 {
9268 --todo;
9269 di = HI2DI(hi);
Bram Moolenaar89d40322006-08-29 15:30:07 +00009270 if (tv_check_lock(di->di_tv.v_lock, ermsg))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009271 break;
9272 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009273 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009274 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009275 break;
9276 if (!map && rem)
9277 dictitem_remove(d, di);
9278 clear_tv(&vimvars[VV_KEY].vv_tv);
9279 }
9280 }
9281 hash_unlock(ht);
9282
9283 restore_vimvar(VV_KEY, &save_key);
9284 }
9285 else
9286 {
9287 for (li = l->lv_first; li != NULL; li = nli)
9288 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009289 if (tv_check_lock(li->li_tv.v_lock, ermsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009290 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009291 nli = li->li_next;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009292 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009293 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009294 break;
9295 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009296 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009297 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009298 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009299
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009300 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009301
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009302 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009303 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009304
9305 copy_tv(&argvars[0], rettv);
9306}
9307
9308 static int
9309filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009310 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009311 char_u *expr;
9312 int map;
9313 int *remp;
9314{
Bram Moolenaar33570922005-01-25 22:26:29 +00009315 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009316 char_u *s;
9317
Bram Moolenaar33570922005-01-25 22:26:29 +00009318 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009319 s = expr;
9320 if (eval1(&s, &rettv, TRUE) == FAIL)
9321 return FAIL;
9322 if (*s != NUL) /* check for trailing chars after expr */
9323 {
9324 EMSG2(_(e_invexpr2), s);
9325 return FAIL;
9326 }
9327 if (map)
9328 {
9329 /* map(): replace the list item value */
9330 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +00009331 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009332 *tv = rettv;
9333 }
9334 else
9335 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009336 int error = FALSE;
9337
Bram Moolenaare9a41262005-01-15 22:18:47 +00009338 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009339 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009340 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009341 /* On type error, nothing has been removed; return FAIL to stop the
9342 * loop. The error message was given by get_tv_number_chk(). */
9343 if (error)
9344 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009345 }
Bram Moolenaar33570922005-01-25 22:26:29 +00009346 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009347 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009348}
9349
9350/*
9351 * "filter()" function
9352 */
9353 static void
9354f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009355 typval_T *argvars;
9356 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009357{
9358 filter_map(argvars, rettv, FALSE);
9359}
9360
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009361/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009362 * "finddir({fname}[, {path}[, {count}]])" function
9363 */
9364 static void
9365f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009366 typval_T *argvars;
9367 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009368{
9369 findfilendir(argvars, rettv, TRUE);
9370}
9371
9372/*
9373 * "findfile({fname}[, {path}[, {count}]])" function
9374 */
9375 static void
9376f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009377 typval_T *argvars;
9378 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009379{
9380 findfilendir(argvars, rettv, FALSE);
9381}
9382
9383/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009384 * "fnamemodify({fname}, {mods})" function
9385 */
9386 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009387f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009388 typval_T *argvars;
9389 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009390{
9391 char_u *fname;
9392 char_u *mods;
9393 int usedlen = 0;
9394 int len;
9395 char_u *fbuf = NULL;
9396 char_u buf[NUMBUFLEN];
9397
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009398 fname = get_tv_string_chk(&argvars[0]);
9399 mods = get_tv_string_buf_chk(&argvars[1], buf);
9400 if (fname == NULL || mods == NULL)
9401 fname = NULL;
9402 else
9403 {
9404 len = (int)STRLEN(fname);
9405 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
9406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009407
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009408 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009409 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009410 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009411 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009412 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009413 vim_free(fbuf);
9414}
9415
Bram Moolenaar33570922005-01-25 22:26:29 +00009416static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009417
9418/*
9419 * "foldclosed()" function
9420 */
9421 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009422foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00009423 typval_T *argvars;
9424 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009425 int end;
9426{
9427#ifdef FEAT_FOLDING
9428 linenr_T lnum;
9429 linenr_T first, last;
9430
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009431 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009432 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9433 {
9434 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
9435 {
9436 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009437 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009438 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009439 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009440 return;
9441 }
9442 }
9443#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009444 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009445}
9446
9447/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009448 * "foldclosed()" function
9449 */
9450 static void
9451f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009452 typval_T *argvars;
9453 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009454{
9455 foldclosed_both(argvars, rettv, FALSE);
9456}
9457
9458/*
9459 * "foldclosedend()" function
9460 */
9461 static void
9462f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009463 typval_T *argvars;
9464 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009465{
9466 foldclosed_both(argvars, rettv, TRUE);
9467}
9468
9469/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009470 * "foldlevel()" function
9471 */
9472 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009473f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009474 typval_T *argvars;
9475 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009476{
9477#ifdef FEAT_FOLDING
9478 linenr_T lnum;
9479
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009480 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009482 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483 else
9484#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009485 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009486}
9487
9488/*
9489 * "foldtext()" function
9490 */
9491/*ARGSUSED*/
9492 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009493f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009494 typval_T *argvars;
9495 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009496{
9497#ifdef FEAT_FOLDING
9498 linenr_T lnum;
9499 char_u *s;
9500 char_u *r;
9501 int len;
9502 char *txt;
9503#endif
9504
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009505 rettv->v_type = VAR_STRING;
9506 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009507#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00009508 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
9509 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
9510 <= curbuf->b_ml.ml_line_count
9511 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009512 {
9513 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009514 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
9515 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009516 {
9517 if (!linewhite(lnum))
9518 break;
9519 ++lnum;
9520 }
9521
9522 /* Find interesting text in this line. */
9523 s = skipwhite(ml_get(lnum));
9524 /* skip C comment-start */
9525 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009526 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009527 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009528 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00009529 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009530 {
9531 s = skipwhite(ml_get(lnum + 1));
9532 if (*s == '*')
9533 s = skipwhite(s + 1);
9534 }
9535 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009536 txt = _("+-%s%3ld lines: ");
9537 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009538 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009539 + 20 /* for %3ld */
9540 + STRLEN(s))); /* concatenated */
9541 if (r != NULL)
9542 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009543 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
9544 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
9545 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009546 len = (int)STRLEN(r);
9547 STRCAT(r, s);
9548 /* remove 'foldmarker' and 'commentstring' */
9549 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009550 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009551 }
9552 }
9553#endif
9554}
9555
9556/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009557 * "foldtextresult(lnum)" function
9558 */
9559/*ARGSUSED*/
9560 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009561f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009562 typval_T *argvars;
9563 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009564{
9565#ifdef FEAT_FOLDING
9566 linenr_T lnum;
9567 char_u *text;
9568 char_u buf[51];
9569 foldinfo_T foldinfo;
9570 int fold_count;
9571#endif
9572
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009573 rettv->v_type = VAR_STRING;
9574 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009575#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009576 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009577 /* treat illegal types and illegal string values for {lnum} the same */
9578 if (lnum < 0)
9579 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009580 fold_count = foldedCount(curwin, lnum, &foldinfo);
9581 if (fold_count > 0)
9582 {
9583 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
9584 &foldinfo, buf);
9585 if (text == buf)
9586 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009587 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009588 }
9589#endif
9590}
9591
9592/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009593 * "foreground()" function
9594 */
9595/*ARGSUSED*/
9596 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009597f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009598 typval_T *argvars;
9599 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009600{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009601 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009602#ifdef FEAT_GUI
9603 if (gui.in_use)
9604 gui_mch_set_foreground();
9605#else
9606# ifdef WIN32
9607 win32_set_foreground();
9608# endif
9609#endif
9610}
9611
9612/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009613 * "function()" function
9614 */
9615/*ARGSUSED*/
9616 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009617f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009618 typval_T *argvars;
9619 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009620{
9621 char_u *s;
9622
Bram Moolenaara7043832005-01-21 11:56:39 +00009623 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009624 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009625 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009626 EMSG2(_(e_invarg2), s);
9627 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009628 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009629 else
9630 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009631 rettv->vval.v_string = vim_strsave(s);
9632 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009633 }
9634}
9635
9636/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009637 * "garbagecollect()" function
9638 */
9639/*ARGSUSED*/
9640 static void
9641f_garbagecollect(argvars, rettv)
9642 typval_T *argvars;
9643 typval_T *rettv;
9644{
Bram Moolenaar9fecb462006-09-05 10:59:47 +00009645 /* This is postponed until we are back at the toplevel, because we may be
9646 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
9647 want_garbage_collect = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009648}
9649
9650/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009651 * "get()" function
9652 */
9653 static void
9654f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009655 typval_T *argvars;
9656 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009657{
Bram Moolenaar33570922005-01-25 22:26:29 +00009658 listitem_T *li;
9659 list_T *l;
9660 dictitem_T *di;
9661 dict_T *d;
9662 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009663
Bram Moolenaare9a41262005-01-15 22:18:47 +00009664 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009665 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009666 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009667 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009668 int error = FALSE;
9669
9670 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9671 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009672 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009673 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009674 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009675 else if (argvars[0].v_type == VAR_DICT)
9676 {
9677 if ((d = argvars[0].vval.v_dict) != NULL)
9678 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009679 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009680 if (di != NULL)
9681 tv = &di->di_tv;
9682 }
9683 }
9684 else
9685 EMSG2(_(e_listdictarg), "get()");
9686
9687 if (tv == NULL)
9688 {
9689 if (argvars[2].v_type == VAR_UNKNOWN)
9690 rettv->vval.v_number = 0;
9691 else
9692 copy_tv(&argvars[2], rettv);
9693 }
9694 else
9695 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009696}
9697
Bram Moolenaar342337a2005-07-21 21:11:17 +00009698static 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 +00009699
9700/*
9701 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +00009702 * Return a range (from start to end) of lines in rettv from the specified
9703 * buffer.
9704 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009705 */
9706 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +00009707get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009708 buf_T *buf;
9709 linenr_T start;
9710 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009711 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009712 typval_T *rettv;
9713{
9714 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009715
Bram Moolenaar342337a2005-07-21 21:11:17 +00009716 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009717 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009718 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009719 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009720 }
9721 else
9722 rettv->vval.v_number = 0;
9723
9724 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
9725 return;
9726
9727 if (!retlist)
9728 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009729 if (start >= 1 && start <= buf->b_ml.ml_line_count)
9730 p = ml_get_buf(buf, start, FALSE);
9731 else
9732 p = (char_u *)"";
9733
9734 rettv->v_type = VAR_STRING;
9735 rettv->vval.v_string = vim_strsave(p);
9736 }
9737 else
9738 {
9739 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009740 return;
9741
9742 if (start < 1)
9743 start = 1;
9744 if (end > buf->b_ml.ml_line_count)
9745 end = buf->b_ml.ml_line_count;
9746 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009747 if (list_append_string(rettv->vval.v_list,
9748 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009749 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009750 }
9751}
9752
9753/*
9754 * "getbufline()" function
9755 */
9756 static void
9757f_getbufline(argvars, rettv)
9758 typval_T *argvars;
9759 typval_T *rettv;
9760{
9761 linenr_T lnum;
9762 linenr_T end;
9763 buf_T *buf;
9764
9765 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9766 ++emsg_off;
9767 buf = get_buf_tv(&argvars[0]);
9768 --emsg_off;
9769
Bram Moolenaar661b1822005-07-28 22:36:45 +00009770 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009771 if (argvars[2].v_type == VAR_UNKNOWN)
9772 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009773 else
Bram Moolenaar661b1822005-07-28 22:36:45 +00009774 end = get_tv_lnum_buf(&argvars[2], buf);
9775
Bram Moolenaar342337a2005-07-21 21:11:17 +00009776 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009777}
9778
Bram Moolenaar0d660222005-01-07 21:51:51 +00009779/*
9780 * "getbufvar()" function
9781 */
9782 static void
9783f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009784 typval_T *argvars;
9785 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009786{
9787 buf_T *buf;
9788 buf_T *save_curbuf;
9789 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009790 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009791
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009792 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9793 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009794 ++emsg_off;
9795 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009796
9797 rettv->v_type = VAR_STRING;
9798 rettv->vval.v_string = NULL;
9799
9800 if (buf != NULL && varname != NULL)
9801 {
9802 if (*varname == '&') /* buffer-local-option */
9803 {
9804 /* set curbuf to be our buf, temporarily */
9805 save_curbuf = curbuf;
9806 curbuf = buf;
9807
9808 get_option_tv(&varname, rettv, TRUE);
9809
9810 /* restore previous notion of curbuf */
9811 curbuf = save_curbuf;
9812 }
9813 else
9814 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009815 if (*varname == NUL)
9816 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9817 * scope prefix before the NUL byte is required by
9818 * find_var_in_ht(). */
9819 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009820 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009821 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009822 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009823 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009824 }
9825 }
9826
9827 --emsg_off;
9828}
9829
9830/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009831 * "getchar()" function
9832 */
9833 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009834f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009835 typval_T *argvars;
9836 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009837{
9838 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009839 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009840
Bram Moolenaar4015b2c2006-06-22 19:01:34 +00009841 /* Position the cursor. Needed after a message that ends in a space. */
9842 windgoto(msg_row, msg_col);
9843
Bram Moolenaar071d4272004-06-13 20:20:40 +00009844 ++no_mapping;
9845 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009846 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009847 /* getchar(): blocking wait. */
9848 n = safe_vgetc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009849 else if (get_tv_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009850 /* getchar(1): only check if char avail */
9851 n = vpeekc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009852 else if (error || vpeekc() == NUL)
9853 /* illegal argument or getchar(0) and no char avail: return zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009854 n = 0;
9855 else
9856 /* getchar(0) and char avail: return char */
9857 n = safe_vgetc();
9858 --no_mapping;
9859 --allow_keys;
9860
Bram Moolenaar219b8702006-11-01 14:32:36 +00009861 vimvars[VV_MOUSE_WIN].vv_nr = 0;
9862 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
9863 vimvars[VV_MOUSE_COL].vv_nr = 0;
9864
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009865 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866 if (IS_SPECIAL(n) || mod_mask != 0)
9867 {
9868 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9869 int i = 0;
9870
9871 /* Turn a special key into three bytes, plus modifier. */
9872 if (mod_mask != 0)
9873 {
9874 temp[i++] = K_SPECIAL;
9875 temp[i++] = KS_MODIFIER;
9876 temp[i++] = mod_mask;
9877 }
9878 if (IS_SPECIAL(n))
9879 {
9880 temp[i++] = K_SPECIAL;
9881 temp[i++] = K_SECOND(n);
9882 temp[i++] = K_THIRD(n);
9883 }
9884#ifdef FEAT_MBYTE
9885 else if (has_mbyte)
9886 i += (*mb_char2bytes)(n, temp + i);
9887#endif
9888 else
9889 temp[i++] = n;
9890 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009891 rettv->v_type = VAR_STRING;
9892 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +00009893
9894#ifdef FEAT_MOUSE
9895 if (n == K_LEFTMOUSE
9896 || n == K_LEFTMOUSE_NM
9897 || n == K_LEFTDRAG
9898 || n == K_LEFTRELEASE
9899 || n == K_LEFTRELEASE_NM
9900 || n == K_MIDDLEMOUSE
9901 || n == K_MIDDLEDRAG
9902 || n == K_MIDDLERELEASE
9903 || n == K_RIGHTMOUSE
9904 || n == K_RIGHTDRAG
9905 || n == K_RIGHTRELEASE
9906 || n == K_X1MOUSE
9907 || n == K_X1DRAG
9908 || n == K_X1RELEASE
9909 || n == K_X2MOUSE
9910 || n == K_X2DRAG
9911 || n == K_X2RELEASE
9912 || n == K_MOUSEDOWN
9913 || n == K_MOUSEUP)
9914 {
9915 int row = mouse_row;
9916 int col = mouse_col;
9917 win_T *win;
9918 linenr_T lnum;
9919# ifdef FEAT_WINDOWS
9920 win_T *wp;
9921# endif
9922 int n = 1;
9923
9924 if (row >= 0 && col >= 0)
9925 {
9926 /* Find the window at the mouse coordinates and compute the
9927 * text position. */
9928 win = mouse_find_win(&row, &col);
9929 (void)mouse_comp_pos(win, &row, &col, &lnum);
9930# ifdef FEAT_WINDOWS
9931 for (wp = firstwin; wp != win; wp = wp->w_next)
9932 ++n;
9933# endif
9934 vimvars[VV_MOUSE_WIN].vv_nr = n;
9935 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
9936 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
9937 }
9938 }
9939#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009940 }
9941}
9942
9943/*
9944 * "getcharmod()" function
9945 */
9946/*ARGSUSED*/
9947 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009948f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009949 typval_T *argvars;
9950 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009951{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009952 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009953}
9954
9955/*
9956 * "getcmdline()" function
9957 */
9958/*ARGSUSED*/
9959 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009960f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009961 typval_T *argvars;
9962 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009963{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009964 rettv->v_type = VAR_STRING;
9965 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009966}
9967
9968/*
9969 * "getcmdpos()" function
9970 */
9971/*ARGSUSED*/
9972 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009973f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009974 typval_T *argvars;
9975 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009976{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009977 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009978}
9979
9980/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00009981 * "getcmdtype()" function
9982 */
9983/*ARGSUSED*/
9984 static void
9985f_getcmdtype(argvars, rettv)
9986 typval_T *argvars;
9987 typval_T *rettv;
9988{
9989 rettv->v_type = VAR_STRING;
9990 rettv->vval.v_string = alloc(2);
9991 if (rettv->vval.v_string != NULL)
9992 {
9993 rettv->vval.v_string[0] = get_cmdline_type();
9994 rettv->vval.v_string[1] = NUL;
9995 }
9996}
9997
9998/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009999 * "getcwd()" function
10000 */
10001/*ARGSUSED*/
10002 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010003f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010004 typval_T *argvars;
10005 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010006{
10007 char_u cwd[MAXPATHL];
10008
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010009 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010010 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010011 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010012 else
10013 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010014 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010015#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +000010016 if (rettv->vval.v_string != NULL)
10017 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010018#endif
10019 }
10020}
10021
10022/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010023 * "getfontname()" function
10024 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010025/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010026 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010027f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010028 typval_T *argvars;
10029 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010030{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010031 rettv->v_type = VAR_STRING;
10032 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010033#ifdef FEAT_GUI
10034 if (gui.in_use)
10035 {
10036 GuiFont font;
10037 char_u *name = NULL;
10038
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010039 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010040 {
10041 /* Get the "Normal" font. Either the name saved by
10042 * hl_set_font_name() or from the font ID. */
10043 font = gui.norm_font;
10044 name = hl_get_font_name();
10045 }
10046 else
10047 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010048 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010049 if (STRCMP(name, "*") == 0) /* don't use font dialog */
10050 return;
10051 font = gui_mch_get_font(name, FALSE);
10052 if (font == NOFONT)
10053 return; /* Invalid font name, return empty string. */
10054 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010055 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010056 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010057 gui_mch_free_font(font);
10058 }
10059#endif
10060}
10061
10062/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010063 * "getfperm({fname})" function
10064 */
10065 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010066f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010067 typval_T *argvars;
10068 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010069{
10070 char_u *fname;
10071 struct stat st;
10072 char_u *perm = NULL;
10073 char_u flags[] = "rwx";
10074 int i;
10075
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010076 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010077
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010078 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010079 if (mch_stat((char *)fname, &st) >= 0)
10080 {
10081 perm = vim_strsave((char_u *)"---------");
10082 if (perm != NULL)
10083 {
10084 for (i = 0; i < 9; i++)
10085 {
10086 if (st.st_mode & (1 << (8 - i)))
10087 perm[i] = flags[i % 3];
10088 }
10089 }
10090 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010091 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010092}
10093
10094/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010095 * "getfsize({fname})" function
10096 */
10097 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010098f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010099 typval_T *argvars;
10100 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010101{
10102 char_u *fname;
10103 struct stat st;
10104
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010105 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010106
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010107 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010108
10109 if (mch_stat((char *)fname, &st) >= 0)
10110 {
10111 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010112 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010113 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010114 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010115 }
10116 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010117 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010118}
10119
10120/*
10121 * "getftime({fname})" function
10122 */
10123 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010124f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010125 typval_T *argvars;
10126 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010127{
10128 char_u *fname;
10129 struct stat st;
10130
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010131 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010132
10133 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010134 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010135 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010136 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010137}
10138
10139/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010140 * "getftype({fname})" function
10141 */
10142 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010143f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010144 typval_T *argvars;
10145 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010146{
10147 char_u *fname;
10148 struct stat st;
10149 char_u *type = NULL;
10150 char *t;
10151
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010152 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010153
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010154 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010155 if (mch_lstat((char *)fname, &st) >= 0)
10156 {
10157#ifdef S_ISREG
10158 if (S_ISREG(st.st_mode))
10159 t = "file";
10160 else if (S_ISDIR(st.st_mode))
10161 t = "dir";
10162# ifdef S_ISLNK
10163 else if (S_ISLNK(st.st_mode))
10164 t = "link";
10165# endif
10166# ifdef S_ISBLK
10167 else if (S_ISBLK(st.st_mode))
10168 t = "bdev";
10169# endif
10170# ifdef S_ISCHR
10171 else if (S_ISCHR(st.st_mode))
10172 t = "cdev";
10173# endif
10174# ifdef S_ISFIFO
10175 else if (S_ISFIFO(st.st_mode))
10176 t = "fifo";
10177# endif
10178# ifdef S_ISSOCK
10179 else if (S_ISSOCK(st.st_mode))
10180 t = "fifo";
10181# endif
10182 else
10183 t = "other";
10184#else
10185# ifdef S_IFMT
10186 switch (st.st_mode & S_IFMT)
10187 {
10188 case S_IFREG: t = "file"; break;
10189 case S_IFDIR: t = "dir"; break;
10190# ifdef S_IFLNK
10191 case S_IFLNK: t = "link"; break;
10192# endif
10193# ifdef S_IFBLK
10194 case S_IFBLK: t = "bdev"; break;
10195# endif
10196# ifdef S_IFCHR
10197 case S_IFCHR: t = "cdev"; break;
10198# endif
10199# ifdef S_IFIFO
10200 case S_IFIFO: t = "fifo"; break;
10201# endif
10202# ifdef S_IFSOCK
10203 case S_IFSOCK: t = "socket"; break;
10204# endif
10205 default: t = "other";
10206 }
10207# else
10208 if (mch_isdir(fname))
10209 t = "dir";
10210 else
10211 t = "file";
10212# endif
10213#endif
10214 type = vim_strsave((char_u *)t);
10215 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010216 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010217}
10218
10219/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010220 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000010221 */
10222 static void
10223f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010224 typval_T *argvars;
10225 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010226{
10227 linenr_T lnum;
10228 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010229 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010230
10231 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010232 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010233 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010234 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010235 retlist = FALSE;
10236 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010237 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000010238 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000010239 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010240 retlist = TRUE;
10241 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010242
Bram Moolenaar342337a2005-07-21 21:11:17 +000010243 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010244}
10245
10246/*
Bram Moolenaara5525202006-03-02 22:52:09 +000010247 * "getpos(string)" function
10248 */
10249 static void
10250f_getpos(argvars, rettv)
10251 typval_T *argvars;
10252 typval_T *rettv;
10253{
10254 pos_T *fp;
10255 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010256 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010257
10258 if (rettv_list_alloc(rettv) == OK)
10259 {
10260 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010261 fp = var2fpos(&argvars[0], TRUE, &fnum);
10262 if (fnum != -1)
10263 list_append_number(l, (varnumber_T)fnum);
10264 else
10265 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000010266 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
10267 : (varnumber_T)0);
10268 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->col + 1
10269 : (varnumber_T)0);
10270 list_append_number(l,
10271#ifdef FEAT_VIRTUALEDIT
10272 (fp != NULL) ? (varnumber_T)fp->coladd :
10273#endif
10274 (varnumber_T)0);
10275 }
10276 else
10277 rettv->vval.v_number = FALSE;
10278}
10279
10280/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000010281 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000010282 */
Bram Moolenaar280f1262006-01-30 00:14:18 +000010283/*ARGSUSED*/
Bram Moolenaar2641f772005-03-25 21:58:17 +000010284 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000010285f_getqflist(argvars, rettv)
10286 typval_T *argvars;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010287 typval_T *rettv;
10288{
10289#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000010290 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010291#endif
10292
10293 rettv->vval.v_number = FALSE;
10294#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010295 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000010296 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000010297 wp = NULL;
10298 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
10299 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010300 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010301 if (wp == NULL)
10302 return;
10303 }
10304
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010305 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000010306 }
10307#endif
10308}
10309
10310/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010311 * "getreg()" function
10312 */
10313 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010314f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010315 typval_T *argvars;
10316 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010317{
10318 char_u *strregname;
10319 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010320 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010321 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010322
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010323 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010324 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010325 strregname = get_tv_string_chk(&argvars[0]);
10326 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010327 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010328 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010330 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010331 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010332 regname = (strregname == NULL ? '"' : *strregname);
10333 if (regname == 0)
10334 regname = '"';
10335
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010336 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010337 rettv->vval.v_string = error ? NULL :
10338 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010339}
10340
10341/*
10342 * "getregtype()" function
10343 */
10344 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010345f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010346 typval_T *argvars;
10347 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010348{
10349 char_u *strregname;
10350 int regname;
10351 char_u buf[NUMBUFLEN + 2];
10352 long reglen = 0;
10353
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010354 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010355 {
10356 strregname = get_tv_string_chk(&argvars[0]);
10357 if (strregname == NULL) /* type error; errmsg already given */
10358 {
10359 rettv->v_type = VAR_STRING;
10360 rettv->vval.v_string = NULL;
10361 return;
10362 }
10363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010364 else
10365 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010366 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010367
10368 regname = (strregname == NULL ? '"' : *strregname);
10369 if (regname == 0)
10370 regname = '"';
10371
10372 buf[0] = NUL;
10373 buf[1] = NUL;
10374 switch (get_reg_type(regname, &reglen))
10375 {
10376 case MLINE: buf[0] = 'V'; break;
10377 case MCHAR: buf[0] = 'v'; break;
10378#ifdef FEAT_VISUAL
10379 case MBLOCK:
10380 buf[0] = Ctrl_V;
10381 sprintf((char *)buf + 1, "%ld", reglen + 1);
10382 break;
10383#endif
10384 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010385 rettv->v_type = VAR_STRING;
10386 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010387}
10388
10389/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010390 * "gettabwinvar()" function
10391 */
10392 static void
10393f_gettabwinvar(argvars, rettv)
10394 typval_T *argvars;
10395 typval_T *rettv;
10396{
10397 getwinvar(argvars, rettv, 1);
10398}
10399
10400/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010401 * "getwinposx()" function
10402 */
10403/*ARGSUSED*/
10404 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010405f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010406 typval_T *argvars;
10407 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010408{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010409 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010410#ifdef FEAT_GUI
10411 if (gui.in_use)
10412 {
10413 int x, y;
10414
10415 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010416 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010417 }
10418#endif
10419}
10420
10421/*
10422 * "getwinposy()" function
10423 */
10424/*ARGSUSED*/
10425 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010426f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010427 typval_T *argvars;
10428 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010429{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010430 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010431#ifdef FEAT_GUI
10432 if (gui.in_use)
10433 {
10434 int x, y;
10435
10436 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010437 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010438 }
10439#endif
10440}
10441
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010442/*
10443 * Find window specifed by "vp" in tabpage "tp".
10444 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000010445 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010446find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000010447 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010448 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000010449{
10450#ifdef FEAT_WINDOWS
10451 win_T *wp;
10452#endif
10453 int nr;
10454
10455 nr = get_tv_number_chk(vp, NULL);
10456
10457#ifdef FEAT_WINDOWS
10458 if (nr < 0)
10459 return NULL;
10460 if (nr == 0)
10461 return curwin;
10462
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010463 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
10464 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000010465 if (--nr <= 0)
10466 break;
10467 return wp;
10468#else
10469 if (nr == 0 || nr == 1)
10470 return curwin;
10471 return NULL;
10472#endif
10473}
10474
Bram Moolenaar071d4272004-06-13 20:20:40 +000010475/*
10476 * "getwinvar()" function
10477 */
10478 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010479f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010480 typval_T *argvars;
10481 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010482{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010483 getwinvar(argvars, rettv, 0);
10484}
10485
10486/*
10487 * getwinvar() and gettabwinvar()
10488 */
10489 static void
10490getwinvar(argvars, rettv, off)
10491 typval_T *argvars;
10492 typval_T *rettv;
10493 int off; /* 1 for gettabwinvar() */
10494{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010495 win_T *win, *oldcurwin;
10496 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010497 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010498 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010499
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010500#ifdef FEAT_WINDOWS
10501 if (off == 1)
10502 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
10503 else
10504 tp = curtab;
10505#endif
10506 win = find_win_by_nr(&argvars[off], tp);
10507 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010508 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010509
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010510 rettv->v_type = VAR_STRING;
10511 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010512
10513 if (win != NULL && varname != NULL)
10514 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000010515 /* Set curwin to be our win, temporarily. Also set curbuf, so
10516 * that we can get buffer-local options. */
10517 oldcurwin = curwin;
10518 curwin = win;
10519 curbuf = win->w_buffer;
10520
Bram Moolenaar071d4272004-06-13 20:20:40 +000010521 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010522 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010523 else
10524 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010525 if (*varname == NUL)
10526 /* let getwinvar({nr}, "") return the "w:" dictionary. The
10527 * scope prefix before the NUL byte is required by
10528 * find_var_in_ht(). */
10529 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010530 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010531 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010532 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010533 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010534 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000010535
10536 /* restore previous notion of curwin */
10537 curwin = oldcurwin;
10538 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010539 }
10540
10541 --emsg_off;
10542}
10543
10544/*
10545 * "glob()" function
10546 */
10547 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010548f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010549 typval_T *argvars;
10550 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010551{
10552 expand_T xpc;
10553
10554 ExpandInit(&xpc);
10555 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010556 rettv->v_type = VAR_STRING;
10557 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010558 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010559}
10560
10561/*
10562 * "globpath()" function
10563 */
10564 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010565f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010566 typval_T *argvars;
10567 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010568{
10569 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010570 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010571
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010572 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010573 if (file == NULL)
10574 rettv->vval.v_string = NULL;
10575 else
10576 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010577}
10578
10579/*
10580 * "has()" function
10581 */
10582 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010583f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010584 typval_T *argvars;
10585 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010586{
10587 int i;
10588 char_u *name;
10589 int n = FALSE;
10590 static char *(has_list[]) =
10591 {
10592#ifdef AMIGA
10593 "amiga",
10594# ifdef FEAT_ARP
10595 "arp",
10596# endif
10597#endif
10598#ifdef __BEOS__
10599 "beos",
10600#endif
10601#ifdef MSDOS
10602# ifdef DJGPP
10603 "dos32",
10604# else
10605 "dos16",
10606# endif
10607#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000010608#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010609 "mac",
10610#endif
10611#if defined(MACOS_X_UNIX)
10612 "macunix",
10613#endif
10614#ifdef OS2
10615 "os2",
10616#endif
10617#ifdef __QNX__
10618 "qnx",
10619#endif
10620#ifdef RISCOS
10621 "riscos",
10622#endif
10623#ifdef UNIX
10624 "unix",
10625#endif
10626#ifdef VMS
10627 "vms",
10628#endif
10629#ifdef WIN16
10630 "win16",
10631#endif
10632#ifdef WIN32
10633 "win32",
10634#endif
10635#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
10636 "win32unix",
10637#endif
10638#ifdef WIN64
10639 "win64",
10640#endif
10641#ifdef EBCDIC
10642 "ebcdic",
10643#endif
10644#ifndef CASE_INSENSITIVE_FILENAME
10645 "fname_case",
10646#endif
10647#ifdef FEAT_ARABIC
10648 "arabic",
10649#endif
10650#ifdef FEAT_AUTOCMD
10651 "autocmd",
10652#endif
10653#ifdef FEAT_BEVAL
10654 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000010655# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
10656 "balloon_multiline",
10657# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010658#endif
10659#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
10660 "builtin_terms",
10661# ifdef ALL_BUILTIN_TCAPS
10662 "all_builtin_terms",
10663# endif
10664#endif
10665#ifdef FEAT_BYTEOFF
10666 "byte_offset",
10667#endif
10668#ifdef FEAT_CINDENT
10669 "cindent",
10670#endif
10671#ifdef FEAT_CLIENTSERVER
10672 "clientserver",
10673#endif
10674#ifdef FEAT_CLIPBOARD
10675 "clipboard",
10676#endif
10677#ifdef FEAT_CMDL_COMPL
10678 "cmdline_compl",
10679#endif
10680#ifdef FEAT_CMDHIST
10681 "cmdline_hist",
10682#endif
10683#ifdef FEAT_COMMENTS
10684 "comments",
10685#endif
10686#ifdef FEAT_CRYPT
10687 "cryptv",
10688#endif
10689#ifdef FEAT_CSCOPE
10690 "cscope",
10691#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010692#ifdef CURSOR_SHAPE
10693 "cursorshape",
10694#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010695#ifdef DEBUG
10696 "debug",
10697#endif
10698#ifdef FEAT_CON_DIALOG
10699 "dialog_con",
10700#endif
10701#ifdef FEAT_GUI_DIALOG
10702 "dialog_gui",
10703#endif
10704#ifdef FEAT_DIFF
10705 "diff",
10706#endif
10707#ifdef FEAT_DIGRAPHS
10708 "digraphs",
10709#endif
10710#ifdef FEAT_DND
10711 "dnd",
10712#endif
10713#ifdef FEAT_EMACS_TAGS
10714 "emacs_tags",
10715#endif
10716 "eval", /* always present, of course! */
10717#ifdef FEAT_EX_EXTRA
10718 "ex_extra",
10719#endif
10720#ifdef FEAT_SEARCH_EXTRA
10721 "extra_search",
10722#endif
10723#ifdef FEAT_FKMAP
10724 "farsi",
10725#endif
10726#ifdef FEAT_SEARCHPATH
10727 "file_in_path",
10728#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010729#if defined(UNIX) && !defined(USE_SYSTEM)
10730 "filterpipe",
10731#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010732#ifdef FEAT_FIND_ID
10733 "find_in_path",
10734#endif
10735#ifdef FEAT_FOLDING
10736 "folding",
10737#endif
10738#ifdef FEAT_FOOTER
10739 "footer",
10740#endif
10741#if !defined(USE_SYSTEM) && defined(UNIX)
10742 "fork",
10743#endif
10744#ifdef FEAT_GETTEXT
10745 "gettext",
10746#endif
10747#ifdef FEAT_GUI
10748 "gui",
10749#endif
10750#ifdef FEAT_GUI_ATHENA
10751# ifdef FEAT_GUI_NEXTAW
10752 "gui_neXtaw",
10753# else
10754 "gui_athena",
10755# endif
10756#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010757#ifdef FEAT_GUI_GTK
10758 "gui_gtk",
10759# ifdef HAVE_GTK2
10760 "gui_gtk2",
10761# endif
10762#endif
10763#ifdef FEAT_GUI_MAC
10764 "gui_mac",
10765#endif
10766#ifdef FEAT_GUI_MOTIF
10767 "gui_motif",
10768#endif
10769#ifdef FEAT_GUI_PHOTON
10770 "gui_photon",
10771#endif
10772#ifdef FEAT_GUI_W16
10773 "gui_win16",
10774#endif
10775#ifdef FEAT_GUI_W32
10776 "gui_win32",
10777#endif
10778#ifdef FEAT_HANGULIN
10779 "hangul_input",
10780#endif
10781#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10782 "iconv",
10783#endif
10784#ifdef FEAT_INS_EXPAND
10785 "insert_expand",
10786#endif
10787#ifdef FEAT_JUMPLIST
10788 "jumplist",
10789#endif
10790#ifdef FEAT_KEYMAP
10791 "keymap",
10792#endif
10793#ifdef FEAT_LANGMAP
10794 "langmap",
10795#endif
10796#ifdef FEAT_LIBCALL
10797 "libcall",
10798#endif
10799#ifdef FEAT_LINEBREAK
10800 "linebreak",
10801#endif
10802#ifdef FEAT_LISP
10803 "lispindent",
10804#endif
10805#ifdef FEAT_LISTCMDS
10806 "listcmds",
10807#endif
10808#ifdef FEAT_LOCALMAP
10809 "localmap",
10810#endif
10811#ifdef FEAT_MENU
10812 "menu",
10813#endif
10814#ifdef FEAT_SESSION
10815 "mksession",
10816#endif
10817#ifdef FEAT_MODIFY_FNAME
10818 "modify_fname",
10819#endif
10820#ifdef FEAT_MOUSE
10821 "mouse",
10822#endif
10823#ifdef FEAT_MOUSESHAPE
10824 "mouseshape",
10825#endif
10826#if defined(UNIX) || defined(VMS)
10827# ifdef FEAT_MOUSE_DEC
10828 "mouse_dec",
10829# endif
10830# ifdef FEAT_MOUSE_GPM
10831 "mouse_gpm",
10832# endif
10833# ifdef FEAT_MOUSE_JSB
10834 "mouse_jsbterm",
10835# endif
10836# ifdef FEAT_MOUSE_NET
10837 "mouse_netterm",
10838# endif
10839# ifdef FEAT_MOUSE_PTERM
10840 "mouse_pterm",
10841# endif
10842# ifdef FEAT_MOUSE_XTERM
10843 "mouse_xterm",
10844# endif
10845#endif
10846#ifdef FEAT_MBYTE
10847 "multi_byte",
10848#endif
10849#ifdef FEAT_MBYTE_IME
10850 "multi_byte_ime",
10851#endif
10852#ifdef FEAT_MULTI_LANG
10853 "multi_lang",
10854#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010855#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010856#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010857 "mzscheme",
10858#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010859#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010860#ifdef FEAT_OLE
10861 "ole",
10862#endif
10863#ifdef FEAT_OSFILETYPE
10864 "osfiletype",
10865#endif
10866#ifdef FEAT_PATH_EXTRA
10867 "path_extra",
10868#endif
10869#ifdef FEAT_PERL
10870#ifndef DYNAMIC_PERL
10871 "perl",
10872#endif
10873#endif
10874#ifdef FEAT_PYTHON
10875#ifndef DYNAMIC_PYTHON
10876 "python",
10877#endif
10878#endif
10879#ifdef FEAT_POSTSCRIPT
10880 "postscript",
10881#endif
10882#ifdef FEAT_PRINTER
10883 "printer",
10884#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000010885#ifdef FEAT_PROFILE
10886 "profile",
10887#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000010888#ifdef FEAT_RELTIME
10889 "reltime",
10890#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010891#ifdef FEAT_QUICKFIX
10892 "quickfix",
10893#endif
10894#ifdef FEAT_RIGHTLEFT
10895 "rightleft",
10896#endif
10897#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
10898 "ruby",
10899#endif
10900#ifdef FEAT_SCROLLBIND
10901 "scrollbind",
10902#endif
10903#ifdef FEAT_CMDL_INFO
10904 "showcmd",
10905 "cmdline_info",
10906#endif
10907#ifdef FEAT_SIGNS
10908 "signs",
10909#endif
10910#ifdef FEAT_SMARTINDENT
10911 "smartindent",
10912#endif
10913#ifdef FEAT_SNIFF
10914 "sniff",
10915#endif
10916#ifdef FEAT_STL_OPT
10917 "statusline",
10918#endif
10919#ifdef FEAT_SUN_WORKSHOP
10920 "sun_workshop",
10921#endif
10922#ifdef FEAT_NETBEANS_INTG
10923 "netbeans_intg",
10924#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000010925#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010926 "spell",
10927#endif
10928#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010929 "syntax",
10930#endif
10931#if defined(USE_SYSTEM) || !defined(UNIX)
10932 "system",
10933#endif
10934#ifdef FEAT_TAG_BINS
10935 "tag_binary",
10936#endif
10937#ifdef FEAT_TAG_OLDSTATIC
10938 "tag_old_static",
10939#endif
10940#ifdef FEAT_TAG_ANYWHITE
10941 "tag_any_white",
10942#endif
10943#ifdef FEAT_TCL
10944# ifndef DYNAMIC_TCL
10945 "tcl",
10946# endif
10947#endif
10948#ifdef TERMINFO
10949 "terminfo",
10950#endif
10951#ifdef FEAT_TERMRESPONSE
10952 "termresponse",
10953#endif
10954#ifdef FEAT_TEXTOBJ
10955 "textobjects",
10956#endif
10957#ifdef HAVE_TGETENT
10958 "tgetent",
10959#endif
10960#ifdef FEAT_TITLE
10961 "title",
10962#endif
10963#ifdef FEAT_TOOLBAR
10964 "toolbar",
10965#endif
10966#ifdef FEAT_USR_CMDS
10967 "user-commands", /* was accidentally included in 5.4 */
10968 "user_commands",
10969#endif
10970#ifdef FEAT_VIMINFO
10971 "viminfo",
10972#endif
10973#ifdef FEAT_VERTSPLIT
10974 "vertsplit",
10975#endif
10976#ifdef FEAT_VIRTUALEDIT
10977 "virtualedit",
10978#endif
10979#ifdef FEAT_VISUAL
10980 "visual",
10981#endif
10982#ifdef FEAT_VISUALEXTRA
10983 "visualextra",
10984#endif
10985#ifdef FEAT_VREPLACE
10986 "vreplace",
10987#endif
10988#ifdef FEAT_WILDIGN
10989 "wildignore",
10990#endif
10991#ifdef FEAT_WILDMENU
10992 "wildmenu",
10993#endif
10994#ifdef FEAT_WINDOWS
10995 "windows",
10996#endif
10997#ifdef FEAT_WAK
10998 "winaltkeys",
10999#endif
11000#ifdef FEAT_WRITEBACKUP
11001 "writebackup",
11002#endif
11003#ifdef FEAT_XIM
11004 "xim",
11005#endif
11006#ifdef FEAT_XFONTSET
11007 "xfontset",
11008#endif
11009#ifdef USE_XSMP
11010 "xsmp",
11011#endif
11012#ifdef USE_XSMP_INTERACT
11013 "xsmp_interact",
11014#endif
11015#ifdef FEAT_XCLIPBOARD
11016 "xterm_clipboard",
11017#endif
11018#ifdef FEAT_XTERM_SAVE
11019 "xterm_save",
11020#endif
11021#if defined(UNIX) && defined(FEAT_X11)
11022 "X11",
11023#endif
11024 NULL
11025 };
11026
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011027 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011028 for (i = 0; has_list[i] != NULL; ++i)
11029 if (STRICMP(name, has_list[i]) == 0)
11030 {
11031 n = TRUE;
11032 break;
11033 }
11034
11035 if (n == FALSE)
11036 {
11037 if (STRNICMP(name, "patch", 5) == 0)
11038 n = has_patch(atoi((char *)name + 5));
11039 else if (STRICMP(name, "vim_starting") == 0)
11040 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011041#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
11042 else if (STRICMP(name, "balloon_multiline") == 0)
11043 n = multiline_balloon_available();
11044#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011045#ifdef DYNAMIC_TCL
11046 else if (STRICMP(name, "tcl") == 0)
11047 n = tcl_enabled(FALSE);
11048#endif
11049#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
11050 else if (STRICMP(name, "iconv") == 0)
11051 n = iconv_enabled(FALSE);
11052#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000011053#ifdef DYNAMIC_MZSCHEME
11054 else if (STRICMP(name, "mzscheme") == 0)
11055 n = mzscheme_enabled(FALSE);
11056#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011057#ifdef DYNAMIC_RUBY
11058 else if (STRICMP(name, "ruby") == 0)
11059 n = ruby_enabled(FALSE);
11060#endif
11061#ifdef DYNAMIC_PYTHON
11062 else if (STRICMP(name, "python") == 0)
11063 n = python_enabled(FALSE);
11064#endif
11065#ifdef DYNAMIC_PERL
11066 else if (STRICMP(name, "perl") == 0)
11067 n = perl_enabled(FALSE);
11068#endif
11069#ifdef FEAT_GUI
11070 else if (STRICMP(name, "gui_running") == 0)
11071 n = (gui.in_use || gui.starting);
11072# ifdef FEAT_GUI_W32
11073 else if (STRICMP(name, "gui_win32s") == 0)
11074 n = gui_is_win32s();
11075# endif
11076# ifdef FEAT_BROWSE
11077 else if (STRICMP(name, "browse") == 0)
11078 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
11079# endif
11080#endif
11081#ifdef FEAT_SYN_HL
11082 else if (STRICMP(name, "syntax_items") == 0)
11083 n = syntax_present(curbuf);
11084#endif
11085#if defined(WIN3264)
11086 else if (STRICMP(name, "win95") == 0)
11087 n = mch_windows95();
11088#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000011089#ifdef FEAT_NETBEANS_INTG
11090 else if (STRICMP(name, "netbeans_enabled") == 0)
11091 n = usingNetbeans;
11092#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011093 }
11094
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011095 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011096}
11097
11098/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000011099 * "has_key()" function
11100 */
11101 static void
11102f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011103 typval_T *argvars;
11104 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011105{
11106 rettv->vval.v_number = 0;
11107 if (argvars[0].v_type != VAR_DICT)
11108 {
11109 EMSG(_(e_dictreq));
11110 return;
11111 }
11112 if (argvars[0].vval.v_dict == NULL)
11113 return;
11114
11115 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011116 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011117}
11118
11119/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011120 * "hasmapto()" function
11121 */
11122 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011123f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011124 typval_T *argvars;
11125 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011126{
11127 char_u *name;
11128 char_u *mode;
11129 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000011130 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011131
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011132 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011133 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011134 mode = (char_u *)"nvo";
11135 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000011136 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011137 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000011138 if (argvars[2].v_type != VAR_UNKNOWN)
11139 abbr = get_tv_number(&argvars[2]);
11140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011141
Bram Moolenaar2c932302006-03-18 21:42:09 +000011142 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011143 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011144 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011145 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011146}
11147
11148/*
11149 * "histadd()" function
11150 */
11151/*ARGSUSED*/
11152 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011153f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011154 typval_T *argvars;
11155 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011156{
11157#ifdef FEAT_CMDHIST
11158 int histype;
11159 char_u *str;
11160 char_u buf[NUMBUFLEN];
11161#endif
11162
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011163 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011164 if (check_restricted() || check_secure())
11165 return;
11166#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011167 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11168 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011169 if (histype >= 0)
11170 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011171 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011172 if (*str != NUL)
11173 {
11174 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011175 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011176 return;
11177 }
11178 }
11179#endif
11180}
11181
11182/*
11183 * "histdel()" function
11184 */
11185/*ARGSUSED*/
11186 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011187f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011188 typval_T *argvars;
11189 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011190{
11191#ifdef FEAT_CMDHIST
11192 int n;
11193 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011194 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011195
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011196 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11197 if (str == NULL)
11198 n = 0;
11199 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011200 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011201 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011202 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011203 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011204 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011205 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011206 else
11207 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011208 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011209 get_tv_string_buf(&argvars[1], buf));
11210 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011211#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011212 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011213#endif
11214}
11215
11216/*
11217 * "histget()" function
11218 */
11219/*ARGSUSED*/
11220 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011221f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011222 typval_T *argvars;
11223 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011224{
11225#ifdef FEAT_CMDHIST
11226 int type;
11227 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011228 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011229
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011230 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11231 if (str == NULL)
11232 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011233 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011234 {
11235 type = get_histtype(str);
11236 if (argvars[1].v_type == VAR_UNKNOWN)
11237 idx = get_history_idx(type);
11238 else
11239 idx = (int)get_tv_number_chk(&argvars[1], NULL);
11240 /* -1 on type error */
11241 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
11242 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011243#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011244 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011245#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011246 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011247}
11248
11249/*
11250 * "histnr()" function
11251 */
11252/*ARGSUSED*/
11253 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011254f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011255 typval_T *argvars;
11256 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011257{
11258 int i;
11259
11260#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011261 char_u *history = get_tv_string_chk(&argvars[0]);
11262
11263 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011264 if (i >= HIST_CMD && i < HIST_COUNT)
11265 i = get_history_idx(i);
11266 else
11267#endif
11268 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011269 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011270}
11271
11272/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011273 * "highlightID(name)" function
11274 */
11275 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011276f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011277 typval_T *argvars;
11278 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011279{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011280 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011281}
11282
11283/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011284 * "highlight_exists()" function
11285 */
11286 static void
11287f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011288 typval_T *argvars;
11289 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011290{
11291 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
11292}
11293
11294/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011295 * "hostname()" function
11296 */
11297/*ARGSUSED*/
11298 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011299f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011300 typval_T *argvars;
11301 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011302{
11303 char_u hostname[256];
11304
11305 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011306 rettv->v_type = VAR_STRING;
11307 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011308}
11309
11310/*
11311 * iconv() function
11312 */
11313/*ARGSUSED*/
11314 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011315f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011316 typval_T *argvars;
11317 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011318{
11319#ifdef FEAT_MBYTE
11320 char_u buf1[NUMBUFLEN];
11321 char_u buf2[NUMBUFLEN];
11322 char_u *from, *to, *str;
11323 vimconv_T vimconv;
11324#endif
11325
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011326 rettv->v_type = VAR_STRING;
11327 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011328
11329#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011330 str = get_tv_string(&argvars[0]);
11331 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
11332 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011333 vimconv.vc_type = CONV_NONE;
11334 convert_setup(&vimconv, from, to);
11335
11336 /* If the encodings are equal, no conversion needed. */
11337 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011338 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011339 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011340 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011341
11342 convert_setup(&vimconv, NULL, NULL);
11343 vim_free(from);
11344 vim_free(to);
11345#endif
11346}
11347
11348/*
11349 * "indent()" function
11350 */
11351 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011352f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011353 typval_T *argvars;
11354 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011355{
11356 linenr_T lnum;
11357
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011358 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011359 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011360 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011361 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011362 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011363}
11364
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011365/*
11366 * "index()" function
11367 */
11368 static void
11369f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011370 typval_T *argvars;
11371 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011372{
Bram Moolenaar33570922005-01-25 22:26:29 +000011373 list_T *l;
11374 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011375 long idx = 0;
11376 int ic = FALSE;
11377
11378 rettv->vval.v_number = -1;
11379 if (argvars[0].v_type != VAR_LIST)
11380 {
11381 EMSG(_(e_listreq));
11382 return;
11383 }
11384 l = argvars[0].vval.v_list;
11385 if (l != NULL)
11386 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011387 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011388 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011389 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011390 int error = FALSE;
11391
Bram Moolenaar758711c2005-02-02 23:11:38 +000011392 /* Start at specified item. Use the cached index that list_find()
11393 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011394 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000011395 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011396 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011397 ic = get_tv_number_chk(&argvars[3], &error);
11398 if (error)
11399 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011400 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011401
Bram Moolenaar758711c2005-02-02 23:11:38 +000011402 for ( ; item != NULL; item = item->li_next, ++idx)
11403 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011404 {
11405 rettv->vval.v_number = idx;
11406 break;
11407 }
11408 }
11409}
11410
Bram Moolenaar071d4272004-06-13 20:20:40 +000011411static int inputsecret_flag = 0;
11412
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011413static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
11414
Bram Moolenaar071d4272004-06-13 20:20:40 +000011415/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011416 * This function is used by f_input() and f_inputdialog() functions. The third
11417 * argument to f_input() specifies the type of completion to use at the
11418 * prompt. The third argument to f_inputdialog() specifies the value to return
11419 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011420 */
11421 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011422get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000011423 typval_T *argvars;
11424 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011425 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011426{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011427 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011428 char_u *p = NULL;
11429 int c;
11430 char_u buf[NUMBUFLEN];
11431 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011432 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011433 int xp_type = EXPAND_NOTHING;
11434 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011435
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011436 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011437
11438#ifdef NO_CONSOLE_INPUT
11439 /* While starting up, there is no place to enter text. */
11440 if (no_console_input())
11441 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011442 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011443 return;
11444 }
11445#endif
11446
11447 cmd_silent = FALSE; /* Want to see the prompt. */
11448 if (prompt != NULL)
11449 {
11450 /* Only the part of the message after the last NL is considered as
11451 * prompt for the command line */
11452 p = vim_strrchr(prompt, '\n');
11453 if (p == NULL)
11454 p = prompt;
11455 else
11456 {
11457 ++p;
11458 c = *p;
11459 *p = NUL;
11460 msg_start();
11461 msg_clr_eos();
11462 msg_puts_attr(prompt, echo_attr);
11463 msg_didout = FALSE;
11464 msg_starthere();
11465 *p = c;
11466 }
11467 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011468
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011469 if (argvars[1].v_type != VAR_UNKNOWN)
11470 {
11471 defstr = get_tv_string_buf_chk(&argvars[1], buf);
11472 if (defstr != NULL)
11473 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011474
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011475 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000011476 {
11477 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000011478 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000011479 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011480
Bram Moolenaar4463f292005-09-25 22:20:24 +000011481 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011482
Bram Moolenaar4463f292005-09-25 22:20:24 +000011483 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
11484 if (xp_name == NULL)
11485 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011486
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011487 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011488
Bram Moolenaar4463f292005-09-25 22:20:24 +000011489 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
11490 &xp_arg) == FAIL)
11491 return;
11492 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011493 }
11494
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011495 if (defstr != NULL)
11496 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011497 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
11498 xp_type, xp_arg);
11499
11500 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011501
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011502 /* since the user typed this, no need to wait for return */
11503 need_wait_return = FALSE;
11504 msg_didout = FALSE;
11505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011506 cmd_silent = cmd_silent_save;
11507}
11508
11509/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011510 * "input()" function
11511 * Also handles inputsecret() when inputsecret is set.
11512 */
11513 static void
11514f_input(argvars, rettv)
11515 typval_T *argvars;
11516 typval_T *rettv;
11517{
11518 get_user_input(argvars, rettv, FALSE);
11519}
11520
11521/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011522 * "inputdialog()" function
11523 */
11524 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011525f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011526 typval_T *argvars;
11527 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011528{
11529#if defined(FEAT_GUI_TEXTDIALOG)
11530 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
11531 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
11532 {
11533 char_u *message;
11534 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011535 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000011536
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011537 message = get_tv_string_chk(&argvars[0]);
11538 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000011539 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000011540 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011541 else
11542 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011543 if (message != NULL && defstr != NULL
11544 && do_dialog(VIM_QUESTION, NULL, message,
11545 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011546 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011547 else
11548 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011549 if (message != NULL && defstr != NULL
11550 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011551 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011552 rettv->vval.v_string = vim_strsave(
11553 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011554 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011555 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011556 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011557 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011558 }
11559 else
11560#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011561 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011562}
11563
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011564/*
11565 * "inputlist()" function
11566 */
11567 static void
11568f_inputlist(argvars, rettv)
11569 typval_T *argvars;
11570 typval_T *rettv;
11571{
11572 listitem_T *li;
11573 int selected;
11574 int mouse_used;
11575
11576 rettv->vval.v_number = 0;
11577#ifdef NO_CONSOLE_INPUT
11578 /* While starting up, there is no place to enter text. */
11579 if (no_console_input())
11580 return;
11581#endif
11582 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
11583 {
11584 EMSG2(_(e_listarg), "inputlist()");
11585 return;
11586 }
11587
11588 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000011589 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011590 lines_left = Rows; /* avoid more prompt */
11591 msg_scroll = TRUE;
11592 msg_clr_eos();
11593
11594 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
11595 {
11596 msg_puts(get_tv_string(&li->li_tv));
11597 msg_putchar('\n');
11598 }
11599
11600 /* Ask for choice. */
11601 selected = prompt_for_number(&mouse_used);
11602 if (mouse_used)
11603 selected -= lines_left;
11604
11605 rettv->vval.v_number = selected;
11606}
11607
11608
Bram Moolenaar071d4272004-06-13 20:20:40 +000011609static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
11610
11611/*
11612 * "inputrestore()" function
11613 */
11614/*ARGSUSED*/
11615 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011616f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011617 typval_T *argvars;
11618 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011619{
11620 if (ga_userinput.ga_len > 0)
11621 {
11622 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011623 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
11624 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011625 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011626 }
11627 else if (p_verbose > 1)
11628 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000011629 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011630 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011631 }
11632}
11633
11634/*
11635 * "inputsave()" function
11636 */
11637/*ARGSUSED*/
11638 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011639f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011640 typval_T *argvars;
11641 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011642{
11643 /* Add an entry to the stack of typehead storage. */
11644 if (ga_grow(&ga_userinput, 1) == OK)
11645 {
11646 save_typeahead((tasave_T *)(ga_userinput.ga_data)
11647 + ga_userinput.ga_len);
11648 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011649 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011650 }
11651 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011652 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011653}
11654
11655/*
11656 * "inputsecret()" function
11657 */
11658 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011659f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011660 typval_T *argvars;
11661 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011662{
11663 ++cmdline_star;
11664 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011665 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011666 --cmdline_star;
11667 --inputsecret_flag;
11668}
11669
11670/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011671 * "insert()" function
11672 */
11673 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011674f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011675 typval_T *argvars;
11676 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011677{
11678 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011679 listitem_T *item;
11680 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011681 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011682
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011683 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011684 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011685 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011686 else if ((l = argvars[0].vval.v_list) != NULL
11687 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011688 {
11689 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011690 before = get_tv_number_chk(&argvars[2], &error);
11691 if (error)
11692 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011693
Bram Moolenaar758711c2005-02-02 23:11:38 +000011694 if (before == l->lv_len)
11695 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011696 else
11697 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011698 item = list_find(l, before);
11699 if (item == NULL)
11700 {
11701 EMSGN(_(e_listidx), before);
11702 l = NULL;
11703 }
11704 }
11705 if (l != NULL)
11706 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011707 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011708 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011709 }
11710 }
11711}
11712
11713/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011714 * "isdirectory()" function
11715 */
11716 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011717f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011718 typval_T *argvars;
11719 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011720{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011721 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011722}
11723
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011724/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011725 * "islocked()" function
11726 */
11727 static void
11728f_islocked(argvars, rettv)
11729 typval_T *argvars;
11730 typval_T *rettv;
11731{
11732 lval_T lv;
11733 char_u *end;
11734 dictitem_T *di;
11735
11736 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000011737 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
11738 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011739 if (end != NULL && lv.ll_name != NULL)
11740 {
11741 if (*end != NUL)
11742 EMSG(_(e_trailing));
11743 else
11744 {
11745 if (lv.ll_tv == NULL)
11746 {
11747 if (check_changedtick(lv.ll_name))
11748 rettv->vval.v_number = 1; /* always locked */
11749 else
11750 {
11751 di = find_var(lv.ll_name, NULL);
11752 if (di != NULL)
11753 {
11754 /* Consider a variable locked when:
11755 * 1. the variable itself is locked
11756 * 2. the value of the variable is locked.
11757 * 3. the List or Dict value is locked.
11758 */
11759 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
11760 || tv_islocked(&di->di_tv));
11761 }
11762 }
11763 }
11764 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011765 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011766 else if (lv.ll_newkey != NULL)
11767 EMSG2(_(e_dictkey), lv.ll_newkey);
11768 else if (lv.ll_list != NULL)
11769 /* List item. */
11770 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
11771 else
11772 /* Dictionary item. */
11773 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
11774 }
11775 }
11776
11777 clear_lval(&lv);
11778}
11779
Bram Moolenaar33570922005-01-25 22:26:29 +000011780static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011781
11782/*
11783 * Turn a dict into a list:
11784 * "what" == 0: list of keys
11785 * "what" == 1: list of values
11786 * "what" == 2: list of items
11787 */
11788 static void
11789dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000011790 typval_T *argvars;
11791 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011792 int what;
11793{
Bram Moolenaar33570922005-01-25 22:26:29 +000011794 list_T *l2;
11795 dictitem_T *di;
11796 hashitem_T *hi;
11797 listitem_T *li;
11798 listitem_T *li2;
11799 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011800 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011801
11802 rettv->vval.v_number = 0;
11803 if (argvars[0].v_type != VAR_DICT)
11804 {
11805 EMSG(_(e_dictreq));
11806 return;
11807 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011808 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011809 return;
11810
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011811 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011812 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011813
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011814 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000011815 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011816 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011817 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011818 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011819 --todo;
11820 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011821
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011822 li = listitem_alloc();
11823 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011824 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011825 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011826
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011827 if (what == 0)
11828 {
11829 /* keys() */
11830 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011831 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011832 li->li_tv.vval.v_string = vim_strsave(di->di_key);
11833 }
11834 else if (what == 1)
11835 {
11836 /* values() */
11837 copy_tv(&di->di_tv, &li->li_tv);
11838 }
11839 else
11840 {
11841 /* items() */
11842 l2 = list_alloc();
11843 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011844 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011845 li->li_tv.vval.v_list = l2;
11846 if (l2 == NULL)
11847 break;
11848 ++l2->lv_refcount;
11849
11850 li2 = listitem_alloc();
11851 if (li2 == NULL)
11852 break;
11853 list_append(l2, li2);
11854 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011855 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011856 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
11857
11858 li2 = listitem_alloc();
11859 if (li2 == NULL)
11860 break;
11861 list_append(l2, li2);
11862 copy_tv(&di->di_tv, &li2->li_tv);
11863 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011864 }
11865 }
11866}
11867
11868/*
11869 * "items(dict)" function
11870 */
11871 static void
11872f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011873 typval_T *argvars;
11874 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011875{
11876 dict_list(argvars, rettv, 2);
11877}
11878
Bram Moolenaar071d4272004-06-13 20:20:40 +000011879/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011880 * "join()" function
11881 */
11882 static void
11883f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011884 typval_T *argvars;
11885 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011886{
11887 garray_T ga;
11888 char_u *sep;
11889
11890 rettv->vval.v_number = 0;
11891 if (argvars[0].v_type != VAR_LIST)
11892 {
11893 EMSG(_(e_listreq));
11894 return;
11895 }
11896 if (argvars[0].vval.v_list == NULL)
11897 return;
11898 if (argvars[1].v_type == VAR_UNKNOWN)
11899 sep = (char_u *)" ";
11900 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011901 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011902
11903 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011904
11905 if (sep != NULL)
11906 {
11907 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000011908 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011909 ga_append(&ga, NUL);
11910 rettv->vval.v_string = (char_u *)ga.ga_data;
11911 }
11912 else
11913 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011914}
11915
11916/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011917 * "keys()" function
11918 */
11919 static void
11920f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011921 typval_T *argvars;
11922 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011923{
11924 dict_list(argvars, rettv, 0);
11925}
11926
11927/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011928 * "last_buffer_nr()" function.
11929 */
11930/*ARGSUSED*/
11931 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011932f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011933 typval_T *argvars;
11934 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011935{
11936 int n = 0;
11937 buf_T *buf;
11938
11939 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
11940 if (n < buf->b_fnum)
11941 n = buf->b_fnum;
11942
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011943 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011944}
11945
11946/*
11947 * "len()" function
11948 */
11949 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011950f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011951 typval_T *argvars;
11952 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011953{
11954 switch (argvars[0].v_type)
11955 {
11956 case VAR_STRING:
11957 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011958 rettv->vval.v_number = (varnumber_T)STRLEN(
11959 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011960 break;
11961 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011962 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011963 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011964 case VAR_DICT:
11965 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
11966 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011967 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011968 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011969 break;
11970 }
11971}
11972
Bram Moolenaar33570922005-01-25 22:26:29 +000011973static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011974
11975 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011976libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011977 typval_T *argvars;
11978 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011979 int type;
11980{
11981#ifdef FEAT_LIBCALL
11982 char_u *string_in;
11983 char_u **string_result;
11984 int nr_result;
11985#endif
11986
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011987 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011988 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011989 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011990 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011991 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011992
11993 if (check_restricted() || check_secure())
11994 return;
11995
11996#ifdef FEAT_LIBCALL
11997 /* The first two args must be strings, otherwise its meaningless */
11998 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
11999 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012000 string_in = NULL;
12001 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012002 string_in = argvars[2].vval.v_string;
12003 if (type == VAR_NUMBER)
12004 string_result = NULL;
12005 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012006 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012007 if (mch_libcall(argvars[0].vval.v_string,
12008 argvars[1].vval.v_string,
12009 string_in,
12010 argvars[2].vval.v_number,
12011 string_result,
12012 &nr_result) == OK
12013 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012014 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012015 }
12016#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012017}
12018
12019/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012020 * "libcall()" function
12021 */
12022 static void
12023f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012024 typval_T *argvars;
12025 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012026{
12027 libcall_common(argvars, rettv, VAR_STRING);
12028}
12029
12030/*
12031 * "libcallnr()" function
12032 */
12033 static void
12034f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012035 typval_T *argvars;
12036 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012037{
12038 libcall_common(argvars, rettv, VAR_NUMBER);
12039}
12040
12041/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012042 * "line(string)" function
12043 */
12044 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012045f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012046 typval_T *argvars;
12047 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012048{
12049 linenr_T lnum = 0;
12050 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012051 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012052
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012053 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012054 if (fp != NULL)
12055 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012056 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012057}
12058
12059/*
12060 * "line2byte(lnum)" function
12061 */
12062/*ARGSUSED*/
12063 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012064f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012065 typval_T *argvars;
12066 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012067{
12068#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012069 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012070#else
12071 linenr_T lnum;
12072
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012073 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012074 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012075 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012076 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012077 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
12078 if (rettv->vval.v_number >= 0)
12079 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012080#endif
12081}
12082
12083/*
12084 * "lispindent(lnum)" function
12085 */
12086 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012087f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012088 typval_T *argvars;
12089 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012090{
12091#ifdef FEAT_LISP
12092 pos_T pos;
12093 linenr_T lnum;
12094
12095 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012096 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012097 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12098 {
12099 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012100 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012101 curwin->w_cursor = pos;
12102 }
12103 else
12104#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012105 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012106}
12107
12108/*
12109 * "localtime()" function
12110 */
12111/*ARGSUSED*/
12112 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012113f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012114 typval_T *argvars;
12115 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012116{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012117 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012118}
12119
Bram Moolenaar33570922005-01-25 22:26:29 +000012120static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012121
12122 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012123get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000012124 typval_T *argvars;
12125 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012126 int exact;
12127{
12128 char_u *keys;
12129 char_u *which;
12130 char_u buf[NUMBUFLEN];
12131 char_u *keys_buf = NULL;
12132 char_u *rhs;
12133 int mode;
12134 garray_T ga;
Bram Moolenaar2c932302006-03-18 21:42:09 +000012135 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012136
12137 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012138 rettv->v_type = VAR_STRING;
12139 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012140
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012141 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012142 if (*keys == NUL)
12143 return;
12144
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012145 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000012146 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012147 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012148 if (argvars[2].v_type != VAR_UNKNOWN)
12149 abbr = get_tv_number(&argvars[2]);
12150 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012151 else
12152 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012153 if (which == NULL)
12154 return;
12155
Bram Moolenaar071d4272004-06-13 20:20:40 +000012156 mode = get_map_mode(&which, 0);
12157
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000012158 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012159 rhs = check_map(keys, mode, exact, FALSE, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012160 vim_free(keys_buf);
12161 if (rhs != NULL)
12162 {
12163 ga_init(&ga);
12164 ga.ga_itemsize = 1;
12165 ga.ga_growsize = 40;
12166
12167 while (*rhs != NUL)
12168 ga_concat(&ga, str2special(&rhs, FALSE));
12169
12170 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012171 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012172 }
12173}
12174
12175/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012176 * "map()" function
12177 */
12178 static void
12179f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012180 typval_T *argvars;
12181 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012182{
12183 filter_map(argvars, rettv, TRUE);
12184}
12185
12186/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012187 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012188 */
12189 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012190f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012191 typval_T *argvars;
12192 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012193{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012194 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012195}
12196
12197/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012198 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012199 */
12200 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012201f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012202 typval_T *argvars;
12203 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012204{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012205 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012206}
12207
Bram Moolenaar33570922005-01-25 22:26:29 +000012208static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012209
12210 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012211find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000012212 typval_T *argvars;
12213 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012214 int type;
12215{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012216 char_u *str = NULL;
12217 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012218 char_u *pat;
12219 regmatch_T regmatch;
12220 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012221 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000012222 char_u *save_cpo;
12223 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012224 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012225 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012226 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012227 list_T *l = NULL;
12228 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012229 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012230 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012231
12232 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12233 save_cpo = p_cpo;
12234 p_cpo = (char_u *)"";
12235
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012236 rettv->vval.v_number = -1;
12237 if (type == 3)
12238 {
12239 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012240 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012241 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012242 }
12243 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012244 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012245 rettv->v_type = VAR_STRING;
12246 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012248
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012249 if (argvars[0].v_type == VAR_LIST)
12250 {
12251 if ((l = argvars[0].vval.v_list) == NULL)
12252 goto theend;
12253 li = l->lv_first;
12254 }
12255 else
12256 expr = str = get_tv_string(&argvars[0]);
12257
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012258 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
12259 if (pat == NULL)
12260 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012261
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012262 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012263 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012264 int error = FALSE;
12265
12266 start = get_tv_number_chk(&argvars[2], &error);
12267 if (error)
12268 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012269 if (l != NULL)
12270 {
12271 li = list_find(l, start);
12272 if (li == NULL)
12273 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012274 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012275 }
12276 else
12277 {
12278 if (start < 0)
12279 start = 0;
12280 if (start > (long)STRLEN(str))
12281 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012282 /* When "count" argument is there ignore matches before "start",
12283 * otherwise skip part of the string. Differs when pattern is "^"
12284 * or "\<". */
12285 if (argvars[3].v_type != VAR_UNKNOWN)
12286 startcol = start;
12287 else
12288 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012289 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012290
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012291 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012292 nth = get_tv_number_chk(&argvars[3], &error);
12293 if (error)
12294 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012295 }
12296
12297 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12298 if (regmatch.regprog != NULL)
12299 {
12300 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012301
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012302 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012303 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012304 if (l != NULL)
12305 {
12306 if (li == NULL)
12307 {
12308 match = FALSE;
12309 break;
12310 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012311 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012312 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012313 if (str == NULL)
12314 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012315 }
12316
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012317 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012318
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012319 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012320 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012321 if (l == NULL && !match)
12322 break;
12323
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012324 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012325 if (l != NULL)
12326 {
12327 li = li->li_next;
12328 ++idx;
12329 }
12330 else
12331 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012332#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012333 startcol = (colnr_T)(regmatch.startp[0]
12334 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012335#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012336 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012337#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012338 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012339 }
12340
12341 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012342 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012343 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012344 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012345 int i;
12346
12347 /* return list with matched string and submatches */
12348 for (i = 0; i < NSUBEXP; ++i)
12349 {
12350 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000012351 {
12352 if (list_append_string(rettv->vval.v_list,
12353 (char_u *)"", 0) == FAIL)
12354 break;
12355 }
12356 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000012357 regmatch.startp[i],
12358 (int)(regmatch.endp[i] - regmatch.startp[i]))
12359 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012360 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012361 }
12362 }
12363 else if (type == 2)
12364 {
12365 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012366 if (l != NULL)
12367 copy_tv(&li->li_tv, rettv);
12368 else
12369 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000012370 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012371 }
12372 else if (l != NULL)
12373 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012374 else
12375 {
12376 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012377 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012378 (varnumber_T)(regmatch.startp[0] - str);
12379 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012380 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012381 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012382 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012383 }
12384 }
12385 vim_free(regmatch.regprog);
12386 }
12387
12388theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012389 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012390 p_cpo = save_cpo;
12391}
12392
12393/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012394 * "match()" function
12395 */
12396 static void
12397f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012398 typval_T *argvars;
12399 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012400{
12401 find_some_match(argvars, rettv, 1);
12402}
12403
12404/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012405 * "matcharg()" function
12406 */
12407 static void
12408f_matcharg(argvars, rettv)
12409 typval_T *argvars;
12410 typval_T *rettv;
12411{
12412 if (rettv_list_alloc(rettv) == OK)
12413 {
12414#ifdef FEAT_SEARCH_EXTRA
12415 int mi = get_tv_number(&argvars[0]);
12416
12417 if (mi >= 1 && mi <= 3)
12418 {
12419 list_append_string(rettv->vval.v_list,
12420 syn_id2name(curwin->w_match_id[mi - 1]), -1);
12421 list_append_string(rettv->vval.v_list,
12422 curwin->w_match_pat[mi - 1], -1);
12423 }
12424#endif
12425 }
12426}
12427
12428/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012429 * "matchend()" function
12430 */
12431 static void
12432f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012433 typval_T *argvars;
12434 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012435{
12436 find_some_match(argvars, rettv, 0);
12437}
12438
12439/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012440 * "matchlist()" function
12441 */
12442 static void
12443f_matchlist(argvars, rettv)
12444 typval_T *argvars;
12445 typval_T *rettv;
12446{
12447 find_some_match(argvars, rettv, 3);
12448}
12449
12450/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012451 * "matchstr()" function
12452 */
12453 static void
12454f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012455 typval_T *argvars;
12456 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012457{
12458 find_some_match(argvars, rettv, 2);
12459}
12460
Bram Moolenaar33570922005-01-25 22:26:29 +000012461static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012462
12463 static void
12464max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000012465 typval_T *argvars;
12466 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012467 int domax;
12468{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012469 long n = 0;
12470 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012471 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012472
12473 if (argvars[0].v_type == VAR_LIST)
12474 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012475 list_T *l;
12476 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012477
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012478 l = argvars[0].vval.v_list;
12479 if (l != NULL)
12480 {
12481 li = l->lv_first;
12482 if (li != NULL)
12483 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012484 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012485 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012486 {
12487 li = li->li_next;
12488 if (li == NULL)
12489 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012490 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012491 if (domax ? i > n : i < n)
12492 n = i;
12493 }
12494 }
12495 }
12496 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012497 else if (argvars[0].v_type == VAR_DICT)
12498 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012499 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012500 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000012501 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012502 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012503
12504 d = argvars[0].vval.v_dict;
12505 if (d != NULL)
12506 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012507 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000012508 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012509 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012510 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000012511 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012512 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012513 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012514 if (first)
12515 {
12516 n = i;
12517 first = FALSE;
12518 }
12519 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012520 n = i;
12521 }
12522 }
12523 }
12524 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012525 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000012526 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012527 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012528}
12529
12530/*
12531 * "max()" function
12532 */
12533 static void
12534f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012535 typval_T *argvars;
12536 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012537{
12538 max_min(argvars, rettv, TRUE);
12539}
12540
12541/*
12542 * "min()" function
12543 */
12544 static void
12545f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012546 typval_T *argvars;
12547 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012548{
12549 max_min(argvars, rettv, FALSE);
12550}
12551
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012552static int mkdir_recurse __ARGS((char_u *dir, int prot));
12553
12554/*
12555 * Create the directory in which "dir" is located, and higher levels when
12556 * needed.
12557 */
12558 static int
12559mkdir_recurse(dir, prot)
12560 char_u *dir;
12561 int prot;
12562{
12563 char_u *p;
12564 char_u *updir;
12565 int r = FAIL;
12566
12567 /* Get end of directory name in "dir".
12568 * We're done when it's "/" or "c:/". */
12569 p = gettail_sep(dir);
12570 if (p <= get_past_head(dir))
12571 return OK;
12572
12573 /* If the directory exists we're done. Otherwise: create it.*/
12574 updir = vim_strnsave(dir, (int)(p - dir));
12575 if (updir == NULL)
12576 return FAIL;
12577 if (mch_isdir(updir))
12578 r = OK;
12579 else if (mkdir_recurse(updir, prot) == OK)
12580 r = vim_mkdir_emsg(updir, prot);
12581 vim_free(updir);
12582 return r;
12583}
12584
12585#ifdef vim_mkdir
12586/*
12587 * "mkdir()" function
12588 */
12589 static void
12590f_mkdir(argvars, rettv)
12591 typval_T *argvars;
12592 typval_T *rettv;
12593{
12594 char_u *dir;
12595 char_u buf[NUMBUFLEN];
12596 int prot = 0755;
12597
12598 rettv->vval.v_number = FAIL;
12599 if (check_restricted() || check_secure())
12600 return;
12601
12602 dir = get_tv_string_buf(&argvars[0], buf);
12603 if (argvars[1].v_type != VAR_UNKNOWN)
12604 {
12605 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012606 prot = get_tv_number_chk(&argvars[2], NULL);
12607 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012608 mkdir_recurse(dir, prot);
12609 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012610 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012611}
12612#endif
12613
Bram Moolenaar0d660222005-01-07 21:51:51 +000012614/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012615 * "mode()" function
12616 */
12617/*ARGSUSED*/
12618 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012619f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012620 typval_T *argvars;
12621 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012622{
12623 char_u buf[2];
12624
12625#ifdef FEAT_VISUAL
12626 if (VIsual_active)
12627 {
12628 if (VIsual_select)
12629 buf[0] = VIsual_mode + 's' - 'v';
12630 else
12631 buf[0] = VIsual_mode;
12632 }
12633 else
12634#endif
12635 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
12636 buf[0] = 'r';
12637 else if (State & INSERT)
12638 {
12639 if (State & REPLACE_FLAG)
12640 buf[0] = 'R';
12641 else
12642 buf[0] = 'i';
12643 }
12644 else if (State & CMDLINE)
12645 buf[0] = 'c';
12646 else
12647 buf[0] = 'n';
12648
12649 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012650 rettv->vval.v_string = vim_strsave(buf);
12651 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012652}
12653
12654/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012655 * "nextnonblank()" function
12656 */
12657 static void
12658f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012659 typval_T *argvars;
12660 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012661{
12662 linenr_T lnum;
12663
12664 for (lnum = get_tv_lnum(argvars); ; ++lnum)
12665 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012666 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012667 {
12668 lnum = 0;
12669 break;
12670 }
12671 if (*skipwhite(ml_get(lnum)) != NUL)
12672 break;
12673 }
12674 rettv->vval.v_number = lnum;
12675}
12676
12677/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012678 * "nr2char()" function
12679 */
12680 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012681f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012682 typval_T *argvars;
12683 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012684{
12685 char_u buf[NUMBUFLEN];
12686
12687#ifdef FEAT_MBYTE
12688 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012689 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012690 else
12691#endif
12692 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012693 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012694 buf[1] = NUL;
12695 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012696 rettv->v_type = VAR_STRING;
12697 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012698}
12699
12700/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012701 * "pathshorten()" function
12702 */
12703 static void
12704f_pathshorten(argvars, rettv)
12705 typval_T *argvars;
12706 typval_T *rettv;
12707{
12708 char_u *p;
12709
12710 rettv->v_type = VAR_STRING;
12711 p = get_tv_string_chk(&argvars[0]);
12712 if (p == NULL)
12713 rettv->vval.v_string = NULL;
12714 else
12715 {
12716 p = vim_strsave(p);
12717 rettv->vval.v_string = p;
12718 if (p != NULL)
12719 shorten_dir(p);
12720 }
12721}
12722
12723/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012724 * "prevnonblank()" function
12725 */
12726 static void
12727f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012728 typval_T *argvars;
12729 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012730{
12731 linenr_T lnum;
12732
12733 lnum = get_tv_lnum(argvars);
12734 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
12735 lnum = 0;
12736 else
12737 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
12738 --lnum;
12739 rettv->vval.v_number = lnum;
12740}
12741
Bram Moolenaara6c840d2005-08-22 22:59:46 +000012742#ifdef HAVE_STDARG_H
12743/* This dummy va_list is here because:
12744 * - passing a NULL pointer doesn't work when va_list isn't a pointer
12745 * - locally in the function results in a "used before set" warning
12746 * - using va_start() to initialize it gives "function with fixed args" error */
12747static va_list ap;
12748#endif
12749
Bram Moolenaar8c711452005-01-14 21:53:12 +000012750/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012751 * "printf()" function
12752 */
12753 static void
12754f_printf(argvars, rettv)
12755 typval_T *argvars;
12756 typval_T *rettv;
12757{
12758 rettv->v_type = VAR_STRING;
12759 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000012760#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012761 {
12762 char_u buf[NUMBUFLEN];
12763 int len;
12764 char_u *s;
12765 int saved_did_emsg = did_emsg;
12766 char *fmt;
12767
12768 /* Get the required length, allocate the buffer and do it for real. */
12769 did_emsg = FALSE;
12770 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012771 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012772 if (!did_emsg)
12773 {
12774 s = alloc(len + 1);
12775 if (s != NULL)
12776 {
12777 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012778 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012779 }
12780 }
12781 did_emsg |= saved_did_emsg;
12782 }
12783#endif
12784}
12785
12786/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000012787 * "pumvisible()" function
12788 */
12789/*ARGSUSED*/
12790 static void
12791f_pumvisible(argvars, rettv)
12792 typval_T *argvars;
12793 typval_T *rettv;
12794{
12795 rettv->vval.v_number = 0;
12796#ifdef FEAT_INS_EXPAND
12797 if (pum_visible())
12798 rettv->vval.v_number = 1;
12799#endif
12800}
12801
12802/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012803 * "range()" function
12804 */
12805 static void
12806f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012807 typval_T *argvars;
12808 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012809{
12810 long start;
12811 long end;
12812 long stride = 1;
12813 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012814 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012815
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012816 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012817 if (argvars[1].v_type == VAR_UNKNOWN)
12818 {
12819 end = start - 1;
12820 start = 0;
12821 }
12822 else
12823 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012824 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012825 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012826 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012827 }
12828
12829 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012830 if (error)
12831 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000012832 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012833 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000012834 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012835 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012836 else
12837 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012838 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012839 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012840 if (list_append_number(rettv->vval.v_list,
12841 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012842 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012843 }
12844}
12845
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012846/*
12847 * "readfile()" function
12848 */
12849 static void
12850f_readfile(argvars, rettv)
12851 typval_T *argvars;
12852 typval_T *rettv;
12853{
12854 int binary = FALSE;
12855 char_u *fname;
12856 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012857 listitem_T *li;
12858#define FREAD_SIZE 200 /* optimized for text lines */
12859 char_u buf[FREAD_SIZE];
12860 int readlen; /* size of last fread() */
12861 int buflen; /* nr of valid chars in buf[] */
12862 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
12863 int tolist; /* first byte in buf[] still to be put in list */
12864 int chop; /* how many CR to chop off */
12865 char_u *prev = NULL; /* previously read bytes, if any */
12866 int prevlen = 0; /* length of "prev" if not NULL */
12867 char_u *s;
12868 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012869 long maxline = MAXLNUM;
12870 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012871
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012872 if (argvars[1].v_type != VAR_UNKNOWN)
12873 {
12874 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
12875 binary = TRUE;
12876 if (argvars[2].v_type != VAR_UNKNOWN)
12877 maxline = get_tv_number(&argvars[2]);
12878 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012879
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012880 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012881 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012882
12883 /* Always open the file in binary mode, library functions have a mind of
12884 * their own about CR-LF conversion. */
12885 fname = get_tv_string(&argvars[0]);
12886 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
12887 {
12888 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
12889 return;
12890 }
12891
12892 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012893 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012894 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012895 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012896 buflen = filtd + readlen;
12897 tolist = 0;
12898 for ( ; filtd < buflen || readlen <= 0; ++filtd)
12899 {
12900 if (buf[filtd] == '\n' || readlen <= 0)
12901 {
12902 /* Only when in binary mode add an empty list item when the
12903 * last line ends in a '\n'. */
12904 if (!binary && readlen == 0 && filtd == 0)
12905 break;
12906
12907 /* Found end-of-line or end-of-file: add a text line to the
12908 * list. */
12909 chop = 0;
12910 if (!binary)
12911 while (filtd - chop - 1 >= tolist
12912 && buf[filtd - chop - 1] == '\r')
12913 ++chop;
12914 len = filtd - tolist - chop;
12915 if (prev == NULL)
12916 s = vim_strnsave(buf + tolist, len);
12917 else
12918 {
12919 s = alloc((unsigned)(prevlen + len + 1));
12920 if (s != NULL)
12921 {
12922 mch_memmove(s, prev, prevlen);
12923 vim_free(prev);
12924 prev = NULL;
12925 mch_memmove(s + prevlen, buf + tolist, len);
12926 s[prevlen + len] = NUL;
12927 }
12928 }
12929 tolist = filtd + 1;
12930
12931 li = listitem_alloc();
12932 if (li == NULL)
12933 {
12934 vim_free(s);
12935 break;
12936 }
12937 li->li_tv.v_type = VAR_STRING;
12938 li->li_tv.v_lock = 0;
12939 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012940 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012941
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012942 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012943 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012944 if (readlen <= 0)
12945 break;
12946 }
12947 else if (buf[filtd] == NUL)
12948 buf[filtd] = '\n';
12949 }
12950 if (readlen <= 0)
12951 break;
12952
12953 if (tolist == 0)
12954 {
12955 /* "buf" is full, need to move text to an allocated buffer */
12956 if (prev == NULL)
12957 {
12958 prev = vim_strnsave(buf, buflen);
12959 prevlen = buflen;
12960 }
12961 else
12962 {
12963 s = alloc((unsigned)(prevlen + buflen));
12964 if (s != NULL)
12965 {
12966 mch_memmove(s, prev, prevlen);
12967 mch_memmove(s + prevlen, buf, buflen);
12968 vim_free(prev);
12969 prev = s;
12970 prevlen += buflen;
12971 }
12972 }
12973 filtd = 0;
12974 }
12975 else
12976 {
12977 mch_memmove(buf, buf + tolist, buflen - tolist);
12978 filtd -= tolist;
12979 }
12980 }
12981
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012982 /*
12983 * For a negative line count use only the lines at the end of the file,
12984 * free the rest.
12985 */
12986 if (maxline < 0)
12987 while (cnt > -maxline)
12988 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012989 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012990 --cnt;
12991 }
12992
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012993 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012994 fclose(fd);
12995}
12996
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012997#if defined(FEAT_RELTIME)
12998static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
12999
13000/*
13001 * Convert a List to proftime_T.
13002 * Return FAIL when there is something wrong.
13003 */
13004 static int
13005list2proftime(arg, tm)
13006 typval_T *arg;
13007 proftime_T *tm;
13008{
13009 long n1, n2;
13010 int error = FALSE;
13011
13012 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
13013 || arg->vval.v_list->lv_len != 2)
13014 return FAIL;
13015 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
13016 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
13017# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000013018 tm->HighPart = n1;
13019 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013020# else
13021 tm->tv_sec = n1;
13022 tm->tv_usec = n2;
13023# endif
13024 return error ? FAIL : OK;
13025}
13026#endif /* FEAT_RELTIME */
13027
13028/*
13029 * "reltime()" function
13030 */
13031 static void
13032f_reltime(argvars, rettv)
13033 typval_T *argvars;
13034 typval_T *rettv;
13035{
13036#ifdef FEAT_RELTIME
13037 proftime_T res;
13038 proftime_T start;
13039
13040 if (argvars[0].v_type == VAR_UNKNOWN)
13041 {
13042 /* No arguments: get current time. */
13043 profile_start(&res);
13044 }
13045 else if (argvars[1].v_type == VAR_UNKNOWN)
13046 {
13047 if (list2proftime(&argvars[0], &res) == FAIL)
13048 return;
13049 profile_end(&res);
13050 }
13051 else
13052 {
13053 /* Two arguments: compute the difference. */
13054 if (list2proftime(&argvars[0], &start) == FAIL
13055 || list2proftime(&argvars[1], &res) == FAIL)
13056 return;
13057 profile_sub(&res, &start);
13058 }
13059
13060 if (rettv_list_alloc(rettv) == OK)
13061 {
13062 long n1, n2;
13063
13064# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000013065 n1 = res.HighPart;
13066 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013067# else
13068 n1 = res.tv_sec;
13069 n2 = res.tv_usec;
13070# endif
13071 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
13072 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
13073 }
13074#endif
13075}
13076
13077/*
13078 * "reltimestr()" function
13079 */
13080 static void
13081f_reltimestr(argvars, rettv)
13082 typval_T *argvars;
13083 typval_T *rettv;
13084{
13085#ifdef FEAT_RELTIME
13086 proftime_T tm;
13087#endif
13088
13089 rettv->v_type = VAR_STRING;
13090 rettv->vval.v_string = NULL;
13091#ifdef FEAT_RELTIME
13092 if (list2proftime(&argvars[0], &tm) == OK)
13093 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
13094#endif
13095}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013096
Bram Moolenaar0d660222005-01-07 21:51:51 +000013097#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
13098static void make_connection __ARGS((void));
13099static int check_connection __ARGS((void));
13100
13101 static void
13102make_connection()
13103{
13104 if (X_DISPLAY == NULL
13105# ifdef FEAT_GUI
13106 && !gui.in_use
13107# endif
13108 )
13109 {
13110 x_force_connect = TRUE;
13111 setup_term_clip();
13112 x_force_connect = FALSE;
13113 }
13114}
13115
13116 static int
13117check_connection()
13118{
13119 make_connection();
13120 if (X_DISPLAY == NULL)
13121 {
13122 EMSG(_("E240: No connection to Vim server"));
13123 return FAIL;
13124 }
13125 return OK;
13126}
13127#endif
13128
13129#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000013130static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013131
13132 static void
13133remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000013134 typval_T *argvars;
13135 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013136 int expr;
13137{
13138 char_u *server_name;
13139 char_u *keys;
13140 char_u *r = NULL;
13141 char_u buf[NUMBUFLEN];
13142# ifdef WIN32
13143 HWND w;
13144# else
13145 Window w;
13146# endif
13147
13148 if (check_restricted() || check_secure())
13149 return;
13150
13151# ifdef FEAT_X11
13152 if (check_connection() == FAIL)
13153 return;
13154# endif
13155
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013156 server_name = get_tv_string_chk(&argvars[0]);
13157 if (server_name == NULL)
13158 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013159 keys = get_tv_string_buf(&argvars[1], buf);
13160# ifdef WIN32
13161 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
13162# else
13163 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
13164 < 0)
13165# endif
13166 {
13167 if (r != NULL)
13168 EMSG(r); /* sending worked but evaluation failed */
13169 else
13170 EMSG2(_("E241: Unable to send to %s"), server_name);
13171 return;
13172 }
13173
13174 rettv->vval.v_string = r;
13175
13176 if (argvars[2].v_type != VAR_UNKNOWN)
13177 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013178 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000013179 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013180 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013181
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013182 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000013183 v.di_tv.v_type = VAR_STRING;
13184 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013185 idvar = get_tv_string_chk(&argvars[2]);
13186 if (idvar != NULL)
13187 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000013188 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013189 }
13190}
13191#endif
13192
13193/*
13194 * "remote_expr()" function
13195 */
13196/*ARGSUSED*/
13197 static void
13198f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013199 typval_T *argvars;
13200 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013201{
13202 rettv->v_type = VAR_STRING;
13203 rettv->vval.v_string = NULL;
13204#ifdef FEAT_CLIENTSERVER
13205 remote_common(argvars, rettv, TRUE);
13206#endif
13207}
13208
13209/*
13210 * "remote_foreground()" function
13211 */
13212/*ARGSUSED*/
13213 static void
13214f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013215 typval_T *argvars;
13216 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013217{
13218 rettv->vval.v_number = 0;
13219#ifdef FEAT_CLIENTSERVER
13220# ifdef WIN32
13221 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013222 {
13223 char_u *server_name = get_tv_string_chk(&argvars[0]);
13224
13225 if (server_name != NULL)
13226 serverForeground(server_name);
13227 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013228# else
13229 /* Send a foreground() expression to the server. */
13230 argvars[1].v_type = VAR_STRING;
13231 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
13232 argvars[2].v_type = VAR_UNKNOWN;
13233 remote_common(argvars, rettv, TRUE);
13234 vim_free(argvars[1].vval.v_string);
13235# endif
13236#endif
13237}
13238
13239/*ARGSUSED*/
13240 static void
13241f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013242 typval_T *argvars;
13243 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013244{
13245#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000013246 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013247 char_u *s = NULL;
13248# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013249 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013250# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013251 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013252
13253 if (check_restricted() || check_secure())
13254 {
13255 rettv->vval.v_number = -1;
13256 return;
13257 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013258 serverid = get_tv_string_chk(&argvars[0]);
13259 if (serverid == NULL)
13260 {
13261 rettv->vval.v_number = -1;
13262 return; /* type error; errmsg already given */
13263 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013264# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013265 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013266 if (n == 0)
13267 rettv->vval.v_number = -1;
13268 else
13269 {
13270 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
13271 rettv->vval.v_number = (s != NULL);
13272 }
13273# else
13274 rettv->vval.v_number = 0;
13275 if (check_connection() == FAIL)
13276 return;
13277
13278 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013279 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013280# endif
13281
13282 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
13283 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013284 char_u *retvar;
13285
Bram Moolenaar33570922005-01-25 22:26:29 +000013286 v.di_tv.v_type = VAR_STRING;
13287 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013288 retvar = get_tv_string_chk(&argvars[1]);
13289 if (retvar != NULL)
13290 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000013291 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013292 }
13293#else
13294 rettv->vval.v_number = -1;
13295#endif
13296}
13297
13298/*ARGSUSED*/
13299 static void
13300f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013301 typval_T *argvars;
13302 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013303{
13304 char_u *r = NULL;
13305
13306#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013307 char_u *serverid = get_tv_string_chk(&argvars[0]);
13308
13309 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000013310 {
13311# ifdef WIN32
13312 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013313 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013314
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013315 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013316 if (n != 0)
13317 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
13318 if (r == NULL)
13319# else
13320 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013321 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013322# endif
13323 EMSG(_("E277: Unable to read a server reply"));
13324 }
13325#endif
13326 rettv->v_type = VAR_STRING;
13327 rettv->vval.v_string = r;
13328}
13329
13330/*
13331 * "remote_send()" function
13332 */
13333/*ARGSUSED*/
13334 static void
13335f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013336 typval_T *argvars;
13337 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013338{
13339 rettv->v_type = VAR_STRING;
13340 rettv->vval.v_string = NULL;
13341#ifdef FEAT_CLIENTSERVER
13342 remote_common(argvars, rettv, FALSE);
13343#endif
13344}
13345
13346/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013347 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013348 */
13349 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013350f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013351 typval_T *argvars;
13352 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013353{
Bram Moolenaar33570922005-01-25 22:26:29 +000013354 list_T *l;
13355 listitem_T *item, *item2;
13356 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013357 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013358 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013359 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000013360 dict_T *d;
13361 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013362
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013363 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013364 if (argvars[0].v_type == VAR_DICT)
13365 {
13366 if (argvars[2].v_type != VAR_UNKNOWN)
13367 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013368 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000013369 && !tv_check_lock(d->dv_lock, (char_u *)"remove() argument"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013370 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013371 key = get_tv_string_chk(&argvars[1]);
13372 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013373 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013374 di = dict_find(d, key, -1);
13375 if (di == NULL)
13376 EMSG2(_(e_dictkey), key);
13377 else
13378 {
13379 *rettv = di->di_tv;
13380 init_tv(&di->di_tv);
13381 dictitem_remove(d, di);
13382 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013383 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013384 }
13385 }
13386 else if (argvars[0].v_type != VAR_LIST)
13387 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013388 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000013389 && !tv_check_lock(l->lv_lock, (char_u *)"remove() argument"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013390 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013391 int error = FALSE;
13392
13393 idx = get_tv_number_chk(&argvars[1], &error);
13394 if (error)
13395 ; /* type error: do nothing, errmsg already given */
13396 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013397 EMSGN(_(e_listidx), idx);
13398 else
13399 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013400 if (argvars[2].v_type == VAR_UNKNOWN)
13401 {
13402 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013403 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013404 *rettv = item->li_tv;
13405 vim_free(item);
13406 }
13407 else
13408 {
13409 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013410 end = get_tv_number_chk(&argvars[2], &error);
13411 if (error)
13412 ; /* type error: do nothing */
13413 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013414 EMSGN(_(e_listidx), end);
13415 else
13416 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013417 int cnt = 0;
13418
13419 for (li = item; li != NULL; li = li->li_next)
13420 {
13421 ++cnt;
13422 if (li == item2)
13423 break;
13424 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013425 if (li == NULL) /* didn't find "item2" after "item" */
13426 EMSG(_(e_invrange));
13427 else
13428 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013429 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013430 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013431 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013432 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013433 l->lv_first = item;
13434 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013435 item->li_prev = NULL;
13436 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013437 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013438 }
13439 }
13440 }
13441 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013442 }
13443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013444}
13445
13446/*
13447 * "rename({from}, {to})" function
13448 */
13449 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013450f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013451 typval_T *argvars;
13452 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013453{
13454 char_u buf[NUMBUFLEN];
13455
13456 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013457 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013458 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013459 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
13460 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013461}
13462
13463/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013464 * "repeat()" function
13465 */
13466/*ARGSUSED*/
13467 static void
13468f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013469 typval_T *argvars;
13470 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013471{
13472 char_u *p;
13473 int n;
13474 int slen;
13475 int len;
13476 char_u *r;
13477 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013478
13479 n = get_tv_number(&argvars[1]);
13480 if (argvars[0].v_type == VAR_LIST)
13481 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013482 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013483 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013484 if (list_extend(rettv->vval.v_list,
13485 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013486 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013487 }
13488 else
13489 {
13490 p = get_tv_string(&argvars[0]);
13491 rettv->v_type = VAR_STRING;
13492 rettv->vval.v_string = NULL;
13493
13494 slen = (int)STRLEN(p);
13495 len = slen * n;
13496 if (len <= 0)
13497 return;
13498
13499 r = alloc(len + 1);
13500 if (r != NULL)
13501 {
13502 for (i = 0; i < n; i++)
13503 mch_memmove(r + i * slen, p, (size_t)slen);
13504 r[len] = NUL;
13505 }
13506
13507 rettv->vval.v_string = r;
13508 }
13509}
13510
13511/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013512 * "resolve()" function
13513 */
13514 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013515f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013516 typval_T *argvars;
13517 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013518{
13519 char_u *p;
13520
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013521 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013522#ifdef FEAT_SHORTCUT
13523 {
13524 char_u *v = NULL;
13525
13526 v = mch_resolve_shortcut(p);
13527 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013528 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013529 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013530 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013531 }
13532#else
13533# ifdef HAVE_READLINK
13534 {
13535 char_u buf[MAXPATHL + 1];
13536 char_u *cpy;
13537 int len;
13538 char_u *remain = NULL;
13539 char_u *q;
13540 int is_relative_to_current = FALSE;
13541 int has_trailing_pathsep = FALSE;
13542 int limit = 100;
13543
13544 p = vim_strsave(p);
13545
13546 if (p[0] == '.' && (vim_ispathsep(p[1])
13547 || (p[1] == '.' && (vim_ispathsep(p[2])))))
13548 is_relative_to_current = TRUE;
13549
13550 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013551 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013552 has_trailing_pathsep = TRUE;
13553
13554 q = getnextcomp(p);
13555 if (*q != NUL)
13556 {
13557 /* Separate the first path component in "p", and keep the
13558 * remainder (beginning with the path separator). */
13559 remain = vim_strsave(q - 1);
13560 q[-1] = NUL;
13561 }
13562
13563 for (;;)
13564 {
13565 for (;;)
13566 {
13567 len = readlink((char *)p, (char *)buf, MAXPATHL);
13568 if (len <= 0)
13569 break;
13570 buf[len] = NUL;
13571
13572 if (limit-- == 0)
13573 {
13574 vim_free(p);
13575 vim_free(remain);
13576 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013577 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013578 goto fail;
13579 }
13580
13581 /* Ensure that the result will have a trailing path separator
13582 * if the argument has one. */
13583 if (remain == NULL && has_trailing_pathsep)
13584 add_pathsep(buf);
13585
13586 /* Separate the first path component in the link value and
13587 * concatenate the remainders. */
13588 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
13589 if (*q != NUL)
13590 {
13591 if (remain == NULL)
13592 remain = vim_strsave(q - 1);
13593 else
13594 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000013595 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013596 if (cpy != NULL)
13597 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013598 vim_free(remain);
13599 remain = cpy;
13600 }
13601 }
13602 q[-1] = NUL;
13603 }
13604
13605 q = gettail(p);
13606 if (q > p && *q == NUL)
13607 {
13608 /* Ignore trailing path separator. */
13609 q[-1] = NUL;
13610 q = gettail(p);
13611 }
13612 if (q > p && !mch_isFullName(buf))
13613 {
13614 /* symlink is relative to directory of argument */
13615 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
13616 if (cpy != NULL)
13617 {
13618 STRCPY(cpy, p);
13619 STRCPY(gettail(cpy), buf);
13620 vim_free(p);
13621 p = cpy;
13622 }
13623 }
13624 else
13625 {
13626 vim_free(p);
13627 p = vim_strsave(buf);
13628 }
13629 }
13630
13631 if (remain == NULL)
13632 break;
13633
13634 /* Append the first path component of "remain" to "p". */
13635 q = getnextcomp(remain + 1);
13636 len = q - remain - (*q != NUL);
13637 cpy = vim_strnsave(p, STRLEN(p) + len);
13638 if (cpy != NULL)
13639 {
13640 STRNCAT(cpy, remain, len);
13641 vim_free(p);
13642 p = cpy;
13643 }
13644 /* Shorten "remain". */
13645 if (*q != NUL)
13646 STRCPY(remain, q - 1);
13647 else
13648 {
13649 vim_free(remain);
13650 remain = NULL;
13651 }
13652 }
13653
13654 /* If the result is a relative path name, make it explicitly relative to
13655 * the current directory if and only if the argument had this form. */
13656 if (!vim_ispathsep(*p))
13657 {
13658 if (is_relative_to_current
13659 && *p != NUL
13660 && !(p[0] == '.'
13661 && (p[1] == NUL
13662 || vim_ispathsep(p[1])
13663 || (p[1] == '.'
13664 && (p[2] == NUL
13665 || vim_ispathsep(p[2]))))))
13666 {
13667 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013668 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013669 if (cpy != NULL)
13670 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013671 vim_free(p);
13672 p = cpy;
13673 }
13674 }
13675 else if (!is_relative_to_current)
13676 {
13677 /* Strip leading "./". */
13678 q = p;
13679 while (q[0] == '.' && vim_ispathsep(q[1]))
13680 q += 2;
13681 if (q > p)
13682 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
13683 }
13684 }
13685
13686 /* Ensure that the result will have no trailing path separator
13687 * if the argument had none. But keep "/" or "//". */
13688 if (!has_trailing_pathsep)
13689 {
13690 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013691 if (after_pathsep(p, q))
13692 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013693 }
13694
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013695 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013696 }
13697# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013698 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013699# endif
13700#endif
13701
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013702 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013703
13704#ifdef HAVE_READLINK
13705fail:
13706#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013707 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013708}
13709
13710/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013711 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013712 */
13713 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013714f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013715 typval_T *argvars;
13716 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013717{
Bram Moolenaar33570922005-01-25 22:26:29 +000013718 list_T *l;
13719 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013720
Bram Moolenaar0d660222005-01-07 21:51:51 +000013721 rettv->vval.v_number = 0;
13722 if (argvars[0].v_type != VAR_LIST)
13723 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013724 else if ((l = argvars[0].vval.v_list) != NULL
13725 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013726 {
13727 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013728 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013729 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013730 while (li != NULL)
13731 {
13732 ni = li->li_prev;
13733 list_append(l, li);
13734 li = ni;
13735 }
13736 rettv->vval.v_list = l;
13737 rettv->v_type = VAR_LIST;
13738 ++l->lv_refcount;
13739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013740}
13741
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013742#define SP_NOMOVE 0x01 /* don't move cursor */
13743#define SP_REPEAT 0x02 /* repeat to find outer pair */
13744#define SP_RETCOUNT 0x04 /* return matchcount */
13745#define SP_SETPCMARK 0x08 /* set previous context mark */
13746#define SP_START 0x10 /* accept match at start position */
13747#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
13748#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013749
Bram Moolenaar33570922005-01-25 22:26:29 +000013750static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013751
13752/*
13753 * Get flags for a search function.
13754 * Possibly sets "p_ws".
13755 * Returns BACKWARD, FORWARD or zero (for an error).
13756 */
13757 static int
13758get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013759 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013760 int *flagsp;
13761{
13762 int dir = FORWARD;
13763 char_u *flags;
13764 char_u nbuf[NUMBUFLEN];
13765 int mask;
13766
13767 if (varp->v_type != VAR_UNKNOWN)
13768 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013769 flags = get_tv_string_buf_chk(varp, nbuf);
13770 if (flags == NULL)
13771 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013772 while (*flags != NUL)
13773 {
13774 switch (*flags)
13775 {
13776 case 'b': dir = BACKWARD; break;
13777 case 'w': p_ws = TRUE; break;
13778 case 'W': p_ws = FALSE; break;
13779 default: mask = 0;
13780 if (flagsp != NULL)
13781 switch (*flags)
13782 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013783 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013784 case 'e': mask = SP_END; break;
13785 case 'm': mask = SP_RETCOUNT; break;
13786 case 'n': mask = SP_NOMOVE; break;
13787 case 'p': mask = SP_SUBPAT; break;
13788 case 'r': mask = SP_REPEAT; break;
13789 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013790 }
13791 if (mask == 0)
13792 {
13793 EMSG2(_(e_invarg2), flags);
13794 dir = 0;
13795 }
13796 else
13797 *flagsp |= mask;
13798 }
13799 if (dir == 0)
13800 break;
13801 ++flags;
13802 }
13803 }
13804 return dir;
13805}
13806
Bram Moolenaar071d4272004-06-13 20:20:40 +000013807/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013808 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000013809 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013810 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013811search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013812 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013813 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013814 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013815{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013816 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013817 char_u *pat;
13818 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013819 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013820 int save_p_ws = p_ws;
13821 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013822 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013823 long lnum_stop = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013824 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013825 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013826
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013827 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013828 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013829 if (dir == 0)
13830 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013831 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013832 if (flags & SP_START)
13833 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013834 if (flags & SP_END)
13835 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013836
13837 /* Optional extra argument: line number to stop searching. */
13838 if (argvars[1].v_type != VAR_UNKNOWN
13839 && argvars[2].v_type != VAR_UNKNOWN)
13840 {
13841 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
13842 if (lnum_stop < 0)
13843 goto theend;
13844 }
13845
Bram Moolenaar231334e2005-07-25 20:46:57 +000013846 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013847 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000013848 * Check to make sure only those flags are set.
13849 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
13850 * flags cannot be set. Check for that condition also.
13851 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013852 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013853 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013854 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013855 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013856 goto theend;
13857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013858
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013859 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013860 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
13861 options, RE_SEARCH, (linenr_T)lnum_stop);
13862 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013863 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013864 if (flags & SP_SUBPAT)
13865 retval = subpatnum;
13866 else
13867 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013868 if (flags & SP_SETPCMARK)
13869 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013870 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013871 if (match_pos != NULL)
13872 {
13873 /* Store the match cursor position */
13874 match_pos->lnum = pos.lnum;
13875 match_pos->col = pos.col + 1;
13876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013877 /* "/$" will put the cursor after the end of the line, may need to
13878 * correct that here */
13879 check_cursor();
13880 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013881
13882 /* If 'n' flag is used: restore cursor position. */
13883 if (flags & SP_NOMOVE)
13884 curwin->w_cursor = save_cursor;
13885theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000013886 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013887
13888 return retval;
13889}
13890
13891/*
13892 * "search()" function
13893 */
13894 static void
13895f_search(argvars, rettv)
13896 typval_T *argvars;
13897 typval_T *rettv;
13898{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013899 int flags = 0;
13900
13901 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013902}
13903
Bram Moolenaar071d4272004-06-13 20:20:40 +000013904/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013905 * "searchdecl()" function
13906 */
13907 static void
13908f_searchdecl(argvars, rettv)
13909 typval_T *argvars;
13910 typval_T *rettv;
13911{
13912 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013913 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013914 int error = FALSE;
13915 char_u *name;
13916
13917 rettv->vval.v_number = 1; /* default: FAIL */
13918
13919 name = get_tv_string_chk(&argvars[0]);
13920 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000013921 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013922 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013923 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13924 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
13925 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013926 if (!error && name != NULL)
13927 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000013928 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013929}
13930
13931/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013932 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000013933 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013934 static int
13935searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000013936 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013937 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013938{
13939 char_u *spat, *mpat, *epat;
13940 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013941 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013942 int dir;
13943 int flags = 0;
13944 char_u nbuf1[NUMBUFLEN];
13945 char_u nbuf2[NUMBUFLEN];
13946 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013947 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013948 long lnum_stop = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013949
Bram Moolenaar071d4272004-06-13 20:20:40 +000013950 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013951 spat = get_tv_string_chk(&argvars[0]);
13952 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
13953 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
13954 if (spat == NULL || mpat == NULL || epat == NULL)
13955 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013956
Bram Moolenaar071d4272004-06-13 20:20:40 +000013957 /* Handle the optional fourth argument: flags */
13958 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013959 if (dir == 0)
13960 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013961
13962 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000013963 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
13964 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013965 if ((flags & (SP_END | SP_SUBPAT)) != 0
13966 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000013967 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013968 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000013969 goto theend;
13970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013971
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013972 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013973 if (argvars[3].v_type == VAR_UNKNOWN
13974 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013975 skip = (char_u *)"";
13976 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013977 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013978 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013979 if (argvars[5].v_type != VAR_UNKNOWN)
13980 {
13981 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
13982 if (lnum_stop < 0)
13983 goto theend;
13984 }
13985 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013986 if (skip == NULL)
13987 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013988
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013989 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
13990 match_pos, lnum_stop);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013991
13992theend:
13993 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013994
13995 return retval;
13996}
13997
13998/*
13999 * "searchpair()" function
14000 */
14001 static void
14002f_searchpair(argvars, rettv)
14003 typval_T *argvars;
14004 typval_T *rettv;
14005{
14006 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
14007}
14008
14009/*
14010 * "searchpairpos()" function
14011 */
14012 static void
14013f_searchpairpos(argvars, rettv)
14014 typval_T *argvars;
14015 typval_T *rettv;
14016{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014017 pos_T match_pos;
14018 int lnum = 0;
14019 int col = 0;
14020
14021 rettv->vval.v_number = 0;
14022
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014023 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014024 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014025
14026 if (searchpair_cmn(argvars, &match_pos) > 0)
14027 {
14028 lnum = match_pos.lnum;
14029 col = match_pos.col;
14030 }
14031
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014032 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
14033 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014034}
14035
14036/*
14037 * Search for a start/middle/end thing.
14038 * Used by searchpair(), see its documentation for the details.
14039 * Returns 0 or -1 for no match,
14040 */
14041 long
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014042do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos, lnum_stop)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014043 char_u *spat; /* start pattern */
14044 char_u *mpat; /* middle pattern */
14045 char_u *epat; /* end pattern */
14046 int dir; /* BACKWARD or FORWARD */
14047 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014048 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014049 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014050 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014051{
14052 char_u *save_cpo;
14053 char_u *pat, *pat2 = NULL, *pat3 = NULL;
14054 long retval = 0;
14055 pos_T pos;
14056 pos_T firstpos;
14057 pos_T foundpos;
14058 pos_T save_cursor;
14059 pos_T save_pos;
14060 int n;
14061 int r;
14062 int nest = 1;
14063 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014064 int options = SEARCH_KEEP;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014065
14066 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14067 save_cpo = p_cpo;
14068 p_cpo = (char_u *)"";
14069
14070 /* Make two search patterns: start/end (pat2, for in nested pairs) and
14071 * start/middle/end (pat3, for the top pair). */
14072 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
14073 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
14074 if (pat2 == NULL || pat3 == NULL)
14075 goto theend;
14076 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
14077 if (*mpat == NUL)
14078 STRCPY(pat3, pat2);
14079 else
14080 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
14081 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014082 if (flags & SP_START)
14083 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014084
Bram Moolenaar071d4272004-06-13 20:20:40 +000014085 save_cursor = curwin->w_cursor;
14086 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000014087 clearpos(&firstpos);
14088 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014089 pat = pat3;
14090 for (;;)
14091 {
14092 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014093 options, RE_SEARCH, lnum_stop);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014094 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
14095 /* didn't find it or found the first match again: FAIL */
14096 break;
14097
14098 if (firstpos.lnum == 0)
14099 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000014100 if (equalpos(pos, foundpos))
14101 {
14102 /* Found the same position again. Can happen with a pattern that
14103 * has "\zs" at the end and searching backwards. Advance one
14104 * character and try again. */
14105 if (dir == BACKWARD)
14106 decl(&pos);
14107 else
14108 incl(&pos);
14109 }
14110 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014111
14112 /* If the skip pattern matches, ignore this match. */
14113 if (*skip != NUL)
14114 {
14115 save_pos = curwin->w_cursor;
14116 curwin->w_cursor = pos;
14117 r = eval_to_bool(skip, &err, NULL, FALSE);
14118 curwin->w_cursor = save_pos;
14119 if (err)
14120 {
14121 /* Evaluating {skip} caused an error, break here. */
14122 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014123 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014124 break;
14125 }
14126 if (r)
14127 continue;
14128 }
14129
14130 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
14131 {
14132 /* Found end when searching backwards or start when searching
14133 * forward: nested pair. */
14134 ++nest;
14135 pat = pat2; /* nested, don't search for middle */
14136 }
14137 else
14138 {
14139 /* Found end when searching forward or start when searching
14140 * backward: end of (nested) pair; or found middle in outer pair. */
14141 if (--nest == 1)
14142 pat = pat3; /* outer level, search for middle */
14143 }
14144
14145 if (nest == 0)
14146 {
14147 /* Found the match: return matchcount or line number. */
14148 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014149 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014150 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014151 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000014152 if (flags & SP_SETPCMARK)
14153 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014154 curwin->w_cursor = pos;
14155 if (!(flags & SP_REPEAT))
14156 break;
14157 nest = 1; /* search for next unmatched */
14158 }
14159 }
14160
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014161 if (match_pos != NULL)
14162 {
14163 /* Store the match cursor position */
14164 match_pos->lnum = curwin->w_cursor.lnum;
14165 match_pos->col = curwin->w_cursor.col + 1;
14166 }
14167
Bram Moolenaar071d4272004-06-13 20:20:40 +000014168 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014169 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014170 curwin->w_cursor = save_cursor;
14171
14172theend:
14173 vim_free(pat2);
14174 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014175 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014176
14177 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014178}
14179
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014180/*
14181 * "searchpos()" function
14182 */
14183 static void
14184f_searchpos(argvars, rettv)
14185 typval_T *argvars;
14186 typval_T *rettv;
14187{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014188 pos_T match_pos;
14189 int lnum = 0;
14190 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014191 int n;
14192 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014193
14194 rettv->vval.v_number = 0;
14195
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014196 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014197 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014198
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014199 n = search_cmn(argvars, &match_pos, &flags);
14200 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014201 {
14202 lnum = match_pos.lnum;
14203 col = match_pos.col;
14204 }
14205
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014206 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
14207 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014208 if (flags & SP_SUBPAT)
14209 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014210}
14211
14212
Bram Moolenaar0d660222005-01-07 21:51:51 +000014213/*ARGSUSED*/
14214 static void
14215f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014216 typval_T *argvars;
14217 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014218{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014219#ifdef FEAT_CLIENTSERVER
14220 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014221 char_u *server = get_tv_string_chk(&argvars[0]);
14222 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014223
Bram Moolenaar0d660222005-01-07 21:51:51 +000014224 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014225 if (server == NULL || reply == NULL)
14226 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014227 if (check_restricted() || check_secure())
14228 return;
14229# ifdef FEAT_X11
14230 if (check_connection() == FAIL)
14231 return;
14232# endif
14233
14234 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014235 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014236 EMSG(_("E258: Unable to send to client"));
14237 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014238 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014239 rettv->vval.v_number = 0;
14240#else
14241 rettv->vval.v_number = -1;
14242#endif
14243}
14244
14245/*ARGSUSED*/
14246 static void
14247f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014248 typval_T *argvars;
14249 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014250{
14251 char_u *r = NULL;
14252
14253#ifdef FEAT_CLIENTSERVER
14254# ifdef WIN32
14255 r = serverGetVimNames();
14256# else
14257 make_connection();
14258 if (X_DISPLAY != NULL)
14259 r = serverGetVimNames(X_DISPLAY);
14260# endif
14261#endif
14262 rettv->v_type = VAR_STRING;
14263 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014264}
14265
14266/*
14267 * "setbufvar()" function
14268 */
14269/*ARGSUSED*/
14270 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014271f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014272 typval_T *argvars;
14273 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014274{
14275 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014276 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014277 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014278 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014279 char_u nbuf[NUMBUFLEN];
14280
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014281 rettv->vval.v_number = 0;
14282
Bram Moolenaar071d4272004-06-13 20:20:40 +000014283 if (check_restricted() || check_secure())
14284 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014285 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
14286 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014287 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014288 varp = &argvars[2];
14289
14290 if (buf != NULL && varname != NULL && varp != NULL)
14291 {
14292 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014293 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014294
14295 if (*varname == '&')
14296 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014297 long numval;
14298 char_u *strval;
14299 int error = FALSE;
14300
Bram Moolenaar071d4272004-06-13 20:20:40 +000014301 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014302 numval = get_tv_number_chk(varp, &error);
14303 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014304 if (!error && strval != NULL)
14305 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014306 }
14307 else
14308 {
14309 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
14310 if (bufvarname != NULL)
14311 {
14312 STRCPY(bufvarname, "b:");
14313 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014314 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014315 vim_free(bufvarname);
14316 }
14317 }
14318
14319 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014320 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014322}
14323
14324/*
14325 * "setcmdpos()" function
14326 */
14327 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014328f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014329 typval_T *argvars;
14330 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014331{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014332 int pos = (int)get_tv_number(&argvars[0]) - 1;
14333
14334 if (pos >= 0)
14335 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014336}
14337
14338/*
14339 * "setline()" function
14340 */
14341 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014342f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014343 typval_T *argvars;
14344 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014345{
14346 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000014347 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014348 list_T *l = NULL;
14349 listitem_T *li = NULL;
14350 long added = 0;
14351 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014352
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014353 lnum = get_tv_lnum(&argvars[0]);
14354 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014355 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014356 l = argvars[1].vval.v_list;
14357 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014358 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014359 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014360 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014361
14362 rettv->vval.v_number = 0; /* OK */
14363 for (;;)
14364 {
14365 if (l != NULL)
14366 {
14367 /* list argument, get next string */
14368 if (li == NULL)
14369 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014370 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014371 li = li->li_next;
14372 }
14373
14374 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014375 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014376 break;
14377 if (lnum <= curbuf->b_ml.ml_line_count)
14378 {
14379 /* existing line, replace it */
14380 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
14381 {
14382 changed_bytes(lnum, 0);
14383 check_cursor_col();
14384 rettv->vval.v_number = 0; /* OK */
14385 }
14386 }
14387 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
14388 {
14389 /* lnum is one past the last line, append the line */
14390 ++added;
14391 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
14392 rettv->vval.v_number = 0; /* OK */
14393 }
14394
14395 if (l == NULL) /* only one string argument */
14396 break;
14397 ++lnum;
14398 }
14399
14400 if (added > 0)
14401 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014402}
14403
14404/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014405 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000014406 */
14407/*ARGSUSED*/
14408 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014409set_qf_ll_list(wp, list_arg, action_arg, rettv)
14410 win_T *wp;
14411 typval_T *list_arg;
14412 typval_T *action_arg;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014413 typval_T *rettv;
14414{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014415#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014416 char_u *act;
14417 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014418#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014419
Bram Moolenaar2641f772005-03-25 21:58:17 +000014420 rettv->vval.v_number = -1;
14421
14422#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014423 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014424 EMSG(_(e_listreq));
14425 else
14426 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014427 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014428
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014429 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014430 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014431 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014432 if (act == NULL)
14433 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014434 if (*act == 'a' || *act == 'r')
14435 action = *act;
14436 }
14437
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014438 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014439 rettv->vval.v_number = 0;
14440 }
14441#endif
14442}
14443
14444/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014445 * "setloclist()" function
14446 */
14447/*ARGSUSED*/
14448 static void
14449f_setloclist(argvars, rettv)
14450 typval_T *argvars;
14451 typval_T *rettv;
14452{
14453 win_T *win;
14454
14455 rettv->vval.v_number = -1;
14456
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014457 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014458 if (win != NULL)
14459 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
14460}
14461
14462/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014463 * "setpos()" function
14464 */
14465/*ARGSUSED*/
14466 static void
14467f_setpos(argvars, rettv)
14468 typval_T *argvars;
14469 typval_T *rettv;
14470{
14471 pos_T pos;
14472 int fnum;
14473 char_u *name;
14474
14475 name = get_tv_string_chk(argvars);
14476 if (name != NULL)
14477 {
14478 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
14479 {
14480 --pos.col;
14481 if (name[0] == '.') /* cursor */
14482 {
14483 if (fnum == curbuf->b_fnum)
14484 {
14485 curwin->w_cursor = pos;
14486 check_cursor();
14487 }
14488 else
14489 EMSG(_(e_invarg));
14490 }
14491 else if (name[0] == '\'') /* mark */
14492 (void)setmark_pos(name[1], &pos, fnum);
14493 else
14494 EMSG(_(e_invarg));
14495 }
14496 }
14497}
14498
14499/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014500 * "setqflist()" function
14501 */
14502/*ARGSUSED*/
14503 static void
14504f_setqflist(argvars, rettv)
14505 typval_T *argvars;
14506 typval_T *rettv;
14507{
14508 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
14509}
14510
14511/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014512 * "setreg()" function
14513 */
14514 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014515f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014516 typval_T *argvars;
14517 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014518{
14519 int regname;
14520 char_u *strregname;
14521 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014522 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014523 int append;
14524 char_u yank_type;
14525 long block_len;
14526
14527 block_len = -1;
14528 yank_type = MAUTO;
14529 append = FALSE;
14530
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014531 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014532 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014533
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014534 if (strregname == NULL)
14535 return; /* type error; errmsg already given */
14536 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014537 if (regname == 0 || regname == '@')
14538 regname = '"';
14539 else if (regname == '=')
14540 return;
14541
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014542 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014543 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014544 stropt = get_tv_string_chk(&argvars[2]);
14545 if (stropt == NULL)
14546 return; /* type error */
14547 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014548 switch (*stropt)
14549 {
14550 case 'a': case 'A': /* append */
14551 append = TRUE;
14552 break;
14553 case 'v': case 'c': /* character-wise selection */
14554 yank_type = MCHAR;
14555 break;
14556 case 'V': case 'l': /* line-wise selection */
14557 yank_type = MLINE;
14558 break;
14559#ifdef FEAT_VISUAL
14560 case 'b': case Ctrl_V: /* block-wise selection */
14561 yank_type = MBLOCK;
14562 if (VIM_ISDIGIT(stropt[1]))
14563 {
14564 ++stropt;
14565 block_len = getdigits(&stropt) - 1;
14566 --stropt;
14567 }
14568 break;
14569#endif
14570 }
14571 }
14572
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014573 strval = get_tv_string_chk(&argvars[1]);
14574 if (strval != NULL)
14575 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000014576 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014577 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014578}
14579
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014580/*
14581 * "settabwinvar()" function
14582 */
14583 static void
14584f_settabwinvar(argvars, rettv)
14585 typval_T *argvars;
14586 typval_T *rettv;
14587{
14588 setwinvar(argvars, rettv, 1);
14589}
Bram Moolenaar071d4272004-06-13 20:20:40 +000014590
14591/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014592 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014593 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014594 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014595f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014596 typval_T *argvars;
14597 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014598{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014599 setwinvar(argvars, rettv, 0);
14600}
14601
14602/*
14603 * "setwinvar()" and "settabwinvar()" functions
14604 */
14605 static void
14606setwinvar(argvars, rettv, off)
14607 typval_T *argvars;
14608 typval_T *rettv;
14609 int off;
14610{
Bram Moolenaar071d4272004-06-13 20:20:40 +000014611 win_T *win;
14612#ifdef FEAT_WINDOWS
14613 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014614 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014615#endif
14616 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014617 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014618 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014619 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014620
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014621 rettv->vval.v_number = 0;
14622
Bram Moolenaar071d4272004-06-13 20:20:40 +000014623 if (check_restricted() || check_secure())
14624 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014625
14626#ifdef FEAT_WINDOWS
14627 if (off == 1)
14628 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
14629 else
14630 tp = curtab;
14631#endif
14632 win = find_win_by_nr(&argvars[off], tp);
14633 varname = get_tv_string_chk(&argvars[off + 1]);
14634 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014635
14636 if (win != NULL && varname != NULL && varp != NULL)
14637 {
14638#ifdef FEAT_WINDOWS
14639 /* set curwin to be our win, temporarily */
14640 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014641 save_curtab = curtab;
14642 goto_tabpage_tp(tp);
14643 if (!win_valid(win))
14644 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014645 curwin = win;
14646 curbuf = curwin->w_buffer;
14647#endif
14648
14649 if (*varname == '&')
14650 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014651 long numval;
14652 char_u *strval;
14653 int error = FALSE;
14654
Bram Moolenaar071d4272004-06-13 20:20:40 +000014655 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014656 numval = get_tv_number_chk(varp, &error);
14657 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014658 if (!error && strval != NULL)
14659 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014660 }
14661 else
14662 {
14663 winvarname = alloc((unsigned)STRLEN(varname) + 3);
14664 if (winvarname != NULL)
14665 {
14666 STRCPY(winvarname, "w:");
14667 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014668 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014669 vim_free(winvarname);
14670 }
14671 }
14672
14673#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014674 /* Restore current tabpage and window, if still valid (autocomands can
14675 * make them invalid). */
14676 if (valid_tabpage(save_curtab))
14677 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014678 if (win_valid(save_curwin))
14679 {
14680 curwin = save_curwin;
14681 curbuf = curwin->w_buffer;
14682 }
14683#endif
14684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014685}
14686
14687/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000014688 * "shellescape({string})" function
14689 */
14690 static void
14691f_shellescape(argvars, rettv)
14692 typval_T *argvars;
14693 typval_T *rettv;
14694{
14695 rettv->vval.v_string = vim_strsave_shellescape(get_tv_string(&argvars[0]));
14696 rettv->v_type = VAR_STRING;
14697}
14698
14699/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014700 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014701 */
14702 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014703f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014704 typval_T *argvars;
14705 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014706{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014707 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014708
Bram Moolenaar0d660222005-01-07 21:51:51 +000014709 p = get_tv_string(&argvars[0]);
14710 rettv->vval.v_string = vim_strsave(p);
14711 simplify_filename(rettv->vval.v_string); /* simplify in place */
14712 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014713}
14714
Bram Moolenaar0d660222005-01-07 21:51:51 +000014715static int
14716#ifdef __BORLANDC__
14717 _RTLENTRYF
14718#endif
14719 item_compare __ARGS((const void *s1, const void *s2));
14720static int
14721#ifdef __BORLANDC__
14722 _RTLENTRYF
14723#endif
14724 item_compare2 __ARGS((const void *s1, const void *s2));
14725
14726static int item_compare_ic;
14727static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014728static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014729#define ITEM_COMPARE_FAIL 999
14730
Bram Moolenaar071d4272004-06-13 20:20:40 +000014731/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014732 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014733 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014734 static int
14735#ifdef __BORLANDC__
14736_RTLENTRYF
14737#endif
14738item_compare(s1, s2)
14739 const void *s1;
14740 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014741{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014742 char_u *p1, *p2;
14743 char_u *tofree1, *tofree2;
14744 int res;
14745 char_u numbuf1[NUMBUFLEN];
14746 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014747
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014748 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
14749 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014750 if (item_compare_ic)
14751 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014752 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014753 res = STRCMP(p1, p2);
14754 vim_free(tofree1);
14755 vim_free(tofree2);
14756 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014757}
14758
14759 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000014760#ifdef __BORLANDC__
14761_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014762#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000014763item_compare2(s1, s2)
14764 const void *s1;
14765 const void *s2;
14766{
14767 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000014768 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014769 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000014770 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014771
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014772 /* shortcut after failure in previous call; compare all items equal */
14773 if (item_compare_func_err)
14774 return 0;
14775
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014776 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
14777 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014778 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
14779 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014780
14781 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014782 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000014783 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014784 clear_tv(&argv[0]);
14785 clear_tv(&argv[1]);
14786
14787 if (res == FAIL)
14788 res = ITEM_COMPARE_FAIL;
14789 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014790 /* return value has wrong type */
14791 res = get_tv_number_chk(&rettv, &item_compare_func_err);
14792 if (item_compare_func_err)
14793 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014794 clear_tv(&rettv);
14795 return res;
14796}
14797
14798/*
14799 * "sort({list})" function
14800 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014801 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014802f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014803 typval_T *argvars;
14804 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014805{
Bram Moolenaar33570922005-01-25 22:26:29 +000014806 list_T *l;
14807 listitem_T *li;
14808 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014809 long len;
14810 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014811
Bram Moolenaar0d660222005-01-07 21:51:51 +000014812 rettv->vval.v_number = 0;
14813 if (argvars[0].v_type != VAR_LIST)
14814 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000014815 else
14816 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014817 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014818 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014819 return;
14820 rettv->vval.v_list = l;
14821 rettv->v_type = VAR_LIST;
14822 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014823
Bram Moolenaar0d660222005-01-07 21:51:51 +000014824 len = list_len(l);
14825 if (len <= 1)
14826 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014827
Bram Moolenaar0d660222005-01-07 21:51:51 +000014828 item_compare_ic = FALSE;
14829 item_compare_func = NULL;
14830 if (argvars[1].v_type != VAR_UNKNOWN)
14831 {
14832 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014833 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014834 else
14835 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014836 int error = FALSE;
14837
14838 i = get_tv_number_chk(&argvars[1], &error);
14839 if (error)
14840 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014841 if (i == 1)
14842 item_compare_ic = TRUE;
14843 else
14844 item_compare_func = get_tv_string(&argvars[1]);
14845 }
14846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014847
Bram Moolenaar0d660222005-01-07 21:51:51 +000014848 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014849 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014850 if (ptrs == NULL)
14851 return;
14852 i = 0;
14853 for (li = l->lv_first; li != NULL; li = li->li_next)
14854 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014855
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014856 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014857 /* test the compare function */
14858 if (item_compare_func != NULL
14859 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
14860 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014861 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014862 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014863 {
14864 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014865 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000014866 item_compare_func == NULL ? item_compare : item_compare2);
14867
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014868 if (!item_compare_func_err)
14869 {
14870 /* Clear the List and append the items in the sorted order. */
14871 l->lv_first = l->lv_last = NULL;
14872 l->lv_len = 0;
14873 for (i = 0; i < len; ++i)
14874 list_append(l, ptrs[i]);
14875 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014876 }
14877
14878 vim_free(ptrs);
14879 }
14880}
14881
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014882/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000014883 * "soundfold({word})" function
14884 */
14885 static void
14886f_soundfold(argvars, rettv)
14887 typval_T *argvars;
14888 typval_T *rettv;
14889{
14890 char_u *s;
14891
14892 rettv->v_type = VAR_STRING;
14893 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014894#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000014895 rettv->vval.v_string = eval_soundfold(s);
14896#else
14897 rettv->vval.v_string = vim_strsave(s);
14898#endif
14899}
14900
14901/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014902 * "spellbadword()" function
14903 */
14904/* ARGSUSED */
14905 static void
14906f_spellbadword(argvars, rettv)
14907 typval_T *argvars;
14908 typval_T *rettv;
14909{
Bram Moolenaar4463f292005-09-25 22:20:24 +000014910 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014911 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014912 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014913
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014914 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014915 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014916
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014917#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000014918 if (argvars[0].v_type == VAR_UNKNOWN)
14919 {
14920 /* Find the start and length of the badly spelled word. */
14921 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
14922 if (len != 0)
14923 word = ml_get_cursor();
14924 }
14925 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14926 {
14927 char_u *str = get_tv_string_chk(&argvars[0]);
14928 int capcol = -1;
14929
14930 if (str != NULL)
14931 {
14932 /* Check the argument for spelling. */
14933 while (*str != NUL)
14934 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014935 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014936 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014937 {
14938 word = str;
14939 break;
14940 }
14941 str += len;
14942 }
14943 }
14944 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014945#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000014946
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014947 list_append_string(rettv->vval.v_list, word, len);
14948 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014949 attr == HLF_SPB ? "bad" :
14950 attr == HLF_SPR ? "rare" :
14951 attr == HLF_SPL ? "local" :
14952 attr == HLF_SPC ? "caps" :
14953 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014954}
14955
14956/*
14957 * "spellsuggest()" function
14958 */
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014959/*ARGSUSED*/
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014960 static void
14961f_spellsuggest(argvars, rettv)
14962 typval_T *argvars;
14963 typval_T *rettv;
14964{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014965#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014966 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014967 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014968 int maxcount;
14969 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014970 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014971 listitem_T *li;
14972 int need_capital = FALSE;
14973#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014974
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014975 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014976 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014977
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014978#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014979 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14980 {
14981 str = get_tv_string(&argvars[0]);
14982 if (argvars[1].v_type != VAR_UNKNOWN)
14983 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014984 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014985 if (maxcount <= 0)
14986 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014987 if (argvars[2].v_type != VAR_UNKNOWN)
14988 {
14989 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
14990 if (typeerr)
14991 return;
14992 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014993 }
14994 else
14995 maxcount = 25;
14996
Bram Moolenaar4770d092006-01-12 23:22:24 +000014997 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014998
14999 for (i = 0; i < ga.ga_len; ++i)
15000 {
15001 str = ((char_u **)ga.ga_data)[i];
15002
15003 li = listitem_alloc();
15004 if (li == NULL)
15005 vim_free(str);
15006 else
15007 {
15008 li->li_tv.v_type = VAR_STRING;
15009 li->li_tv.v_lock = 0;
15010 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015011 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015012 }
15013 }
15014 ga_clear(&ga);
15015 }
15016#endif
15017}
15018
Bram Moolenaar0d660222005-01-07 21:51:51 +000015019 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015020f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015021 typval_T *argvars;
15022 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015023{
15024 char_u *str;
15025 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015026 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015027 regmatch_T regmatch;
15028 char_u patbuf[NUMBUFLEN];
15029 char_u *save_cpo;
15030 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015031 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015032 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015033 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015034
15035 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15036 save_cpo = p_cpo;
15037 p_cpo = (char_u *)"";
15038
15039 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015040 if (argvars[1].v_type != VAR_UNKNOWN)
15041 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015042 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15043 if (pat == NULL)
15044 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015045 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015046 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015047 }
15048 if (pat == NULL || *pat == NUL)
15049 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000015050
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015051 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015052 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015053 if (typeerr)
15054 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015055
Bram Moolenaar0d660222005-01-07 21:51:51 +000015056 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15057 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015058 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015059 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015060 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015061 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015062 if (*str == NUL)
15063 match = FALSE; /* empty item at the end */
15064 else
15065 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015066 if (match)
15067 end = regmatch.startp[0];
15068 else
15069 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015070 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
15071 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015072 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015073 if (list_append_string(rettv->vval.v_list, str,
15074 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015075 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015076 }
15077 if (!match)
15078 break;
15079 /* Advance to just after the match. */
15080 if (regmatch.endp[0] > str)
15081 col = 0;
15082 else
15083 {
15084 /* Don't get stuck at the same match. */
15085#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015086 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015087#else
15088 col = 1;
15089#endif
15090 }
15091 str = regmatch.endp[0];
15092 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015093
Bram Moolenaar0d660222005-01-07 21:51:51 +000015094 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015095 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015096
Bram Moolenaar0d660222005-01-07 21:51:51 +000015097 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015098}
15099
Bram Moolenaar2c932302006-03-18 21:42:09 +000015100/*
15101 * "str2nr()" function
15102 */
15103 static void
15104f_str2nr(argvars, rettv)
15105 typval_T *argvars;
15106 typval_T *rettv;
15107{
15108 int base = 10;
15109 char_u *p;
15110 long n;
15111
15112 if (argvars[1].v_type != VAR_UNKNOWN)
15113 {
15114 base = get_tv_number(&argvars[1]);
15115 if (base != 8 && base != 10 && base != 16)
15116 {
15117 EMSG(_(e_invarg));
15118 return;
15119 }
15120 }
15121
15122 p = skipwhite(get_tv_string(&argvars[0]));
15123 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
15124 rettv->vval.v_number = n;
15125}
15126
Bram Moolenaar071d4272004-06-13 20:20:40 +000015127#ifdef HAVE_STRFTIME
15128/*
15129 * "strftime({format}[, {time}])" function
15130 */
15131 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015132f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015133 typval_T *argvars;
15134 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015135{
15136 char_u result_buf[256];
15137 struct tm *curtime;
15138 time_t seconds;
15139 char_u *p;
15140
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015141 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015142
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015143 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015144 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015145 seconds = time(NULL);
15146 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015147 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015148 curtime = localtime(&seconds);
15149 /* MSVC returns NULL for an invalid value of seconds. */
15150 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015151 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015152 else
15153 {
15154# ifdef FEAT_MBYTE
15155 vimconv_T conv;
15156 char_u *enc;
15157
15158 conv.vc_type = CONV_NONE;
15159 enc = enc_locale();
15160 convert_setup(&conv, p_enc, enc);
15161 if (conv.vc_type != CONV_NONE)
15162 p = string_convert(&conv, p, NULL);
15163# endif
15164 if (p != NULL)
15165 (void)strftime((char *)result_buf, sizeof(result_buf),
15166 (char *)p, curtime);
15167 else
15168 result_buf[0] = NUL;
15169
15170# ifdef FEAT_MBYTE
15171 if (conv.vc_type != CONV_NONE)
15172 vim_free(p);
15173 convert_setup(&conv, enc, p_enc);
15174 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015175 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015176 else
15177# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015178 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015179
15180# ifdef FEAT_MBYTE
15181 /* Release conversion descriptors */
15182 convert_setup(&conv, NULL, NULL);
15183 vim_free(enc);
15184# endif
15185 }
15186}
15187#endif
15188
15189/*
15190 * "stridx()" function
15191 */
15192 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015193f_stridx(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{
15197 char_u buf[NUMBUFLEN];
15198 char_u *needle;
15199 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000015200 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015201 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000015202 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015203
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015204 needle = get_tv_string_chk(&argvars[1]);
15205 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000015206 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015207 if (needle == NULL || haystack == NULL)
15208 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015209
Bram Moolenaar33570922005-01-25 22:26:29 +000015210 if (argvars[2].v_type != VAR_UNKNOWN)
15211 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015212 int error = FALSE;
15213
15214 start_idx = get_tv_number_chk(&argvars[2], &error);
15215 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000015216 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015217 if (start_idx >= 0)
15218 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000015219 }
15220
15221 pos = (char_u *)strstr((char *)haystack, (char *)needle);
15222 if (pos != NULL)
15223 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015224}
15225
15226/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015227 * "string()" function
15228 */
15229 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015230f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015231 typval_T *argvars;
15232 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015233{
15234 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015235 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015236
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015237 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015238 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015239 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015240 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015241}
15242
15243/*
15244 * "strlen()" function
15245 */
15246 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015247f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015248 typval_T *argvars;
15249 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015250{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015251 rettv->vval.v_number = (varnumber_T)(STRLEN(
15252 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015253}
15254
15255/*
15256 * "strpart()" function
15257 */
15258 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015259f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015260 typval_T *argvars;
15261 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015262{
15263 char_u *p;
15264 int n;
15265 int len;
15266 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015267 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015268
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015269 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015270 slen = (int)STRLEN(p);
15271
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015272 n = get_tv_number_chk(&argvars[1], &error);
15273 if (error)
15274 len = 0;
15275 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015276 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015277 else
15278 len = slen - n; /* default len: all bytes that are available. */
15279
15280 /*
15281 * Only return the overlap between the specified part and the actual
15282 * string.
15283 */
15284 if (n < 0)
15285 {
15286 len += n;
15287 n = 0;
15288 }
15289 else if (n > slen)
15290 n = slen;
15291 if (len < 0)
15292 len = 0;
15293 else if (n + len > slen)
15294 len = slen - n;
15295
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015296 rettv->v_type = VAR_STRING;
15297 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015298}
15299
15300/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015301 * "strridx()" function
15302 */
15303 static void
15304f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015305 typval_T *argvars;
15306 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015307{
15308 char_u buf[NUMBUFLEN];
15309 char_u *needle;
15310 char_u *haystack;
15311 char_u *rest;
15312 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015313 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015314
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015315 needle = get_tv_string_chk(&argvars[1]);
15316 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015317
15318 rettv->vval.v_number = -1;
15319 if (needle == NULL || haystack == NULL)
15320 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015321
15322 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000015323 if (argvars[2].v_type != VAR_UNKNOWN)
15324 {
15325 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015326 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000015327 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015328 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000015329 }
15330 else
15331 end_idx = haystack_len;
15332
Bram Moolenaar0d660222005-01-07 21:51:51 +000015333 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000015334 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015335 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000015336 lastmatch = haystack + end_idx;
15337 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015338 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000015339 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015340 for (rest = haystack; *rest != '\0'; ++rest)
15341 {
15342 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000015343 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015344 break;
15345 lastmatch = rest;
15346 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000015347 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015348
15349 if (lastmatch == NULL)
15350 rettv->vval.v_number = -1;
15351 else
15352 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
15353}
15354
15355/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015356 * "strtrans()" function
15357 */
15358 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015359f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015360 typval_T *argvars;
15361 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015362{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015363 rettv->v_type = VAR_STRING;
15364 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015365}
15366
15367/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015368 * "submatch()" function
15369 */
15370 static void
15371f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015372 typval_T *argvars;
15373 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015374{
15375 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015376 rettv->vval.v_string =
15377 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015378}
15379
15380/*
15381 * "substitute()" function
15382 */
15383 static void
15384f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015385 typval_T *argvars;
15386 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015387{
15388 char_u patbuf[NUMBUFLEN];
15389 char_u subbuf[NUMBUFLEN];
15390 char_u flagsbuf[NUMBUFLEN];
15391
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015392 char_u *str = get_tv_string_chk(&argvars[0]);
15393 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15394 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
15395 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
15396
Bram Moolenaar0d660222005-01-07 21:51:51 +000015397 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015398 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
15399 rettv->vval.v_string = NULL;
15400 else
15401 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015402}
15403
15404/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015405 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015406 */
15407/*ARGSUSED*/
15408 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015409f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015410 typval_T *argvars;
15411 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015412{
15413 int id = 0;
15414#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015415 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015416 long col;
15417 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000015418 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015419
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015420 lnum = get_tv_lnum(argvars); /* -1 on type error */
15421 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
15422 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015423
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015424 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015425 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +000015426 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015427#endif
15428
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015429 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015430}
15431
15432/*
15433 * "synIDattr(id, what [, mode])" function
15434 */
15435/*ARGSUSED*/
15436 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015437f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015438 typval_T *argvars;
15439 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015440{
15441 char_u *p = NULL;
15442#ifdef FEAT_SYN_HL
15443 int id;
15444 char_u *what;
15445 char_u *mode;
15446 char_u modebuf[NUMBUFLEN];
15447 int modec;
15448
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015449 id = get_tv_number(&argvars[0]);
15450 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015451 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015452 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015453 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015454 modec = TOLOWER_ASC(mode[0]);
15455 if (modec != 't' && modec != 'c'
15456#ifdef FEAT_GUI
15457 && modec != 'g'
15458#endif
15459 )
15460 modec = 0; /* replace invalid with current */
15461 }
15462 else
15463 {
15464#ifdef FEAT_GUI
15465 if (gui.in_use)
15466 modec = 'g';
15467 else
15468#endif
15469 if (t_colors > 1)
15470 modec = 'c';
15471 else
15472 modec = 't';
15473 }
15474
15475
15476 switch (TOLOWER_ASC(what[0]))
15477 {
15478 case 'b':
15479 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
15480 p = highlight_color(id, what, modec);
15481 else /* bold */
15482 p = highlight_has_attr(id, HL_BOLD, modec);
15483 break;
15484
15485 case 'f': /* fg[#] */
15486 p = highlight_color(id, what, modec);
15487 break;
15488
15489 case 'i':
15490 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
15491 p = highlight_has_attr(id, HL_INVERSE, modec);
15492 else /* italic */
15493 p = highlight_has_attr(id, HL_ITALIC, modec);
15494 break;
15495
15496 case 'n': /* name */
15497 p = get_highlight_name(NULL, id - 1);
15498 break;
15499
15500 case 'r': /* reverse */
15501 p = highlight_has_attr(id, HL_INVERSE, modec);
15502 break;
15503
15504 case 's': /* standout */
15505 p = highlight_has_attr(id, HL_STANDOUT, modec);
15506 break;
15507
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000015508 case 'u':
15509 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
15510 /* underline */
15511 p = highlight_has_attr(id, HL_UNDERLINE, modec);
15512 else
15513 /* undercurl */
15514 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015515 break;
15516 }
15517
15518 if (p != NULL)
15519 p = vim_strsave(p);
15520#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015521 rettv->v_type = VAR_STRING;
15522 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015523}
15524
15525/*
15526 * "synIDtrans(id)" function
15527 */
15528/*ARGSUSED*/
15529 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015530f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015531 typval_T *argvars;
15532 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015533{
15534 int id;
15535
15536#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015537 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015538
15539 if (id > 0)
15540 id = syn_get_final_id(id);
15541 else
15542#endif
15543 id = 0;
15544
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015545 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015546}
15547
15548/*
15549 * "system()" function
15550 */
15551 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015552f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015553 typval_T *argvars;
15554 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015555{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015556 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015557 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015558 char_u *infile = NULL;
15559 char_u buf[NUMBUFLEN];
15560 int err = FALSE;
15561 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015562
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015563 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015564 {
15565 /*
15566 * Write the string to a temp file, to be used for input of the shell
15567 * command.
15568 */
15569 if ((infile = vim_tempname('i')) == NULL)
15570 {
15571 EMSG(_(e_notmp));
15572 return;
15573 }
15574
15575 fd = mch_fopen((char *)infile, WRITEBIN);
15576 if (fd == NULL)
15577 {
15578 EMSG2(_(e_notopen), infile);
15579 goto done;
15580 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015581 p = get_tv_string_buf_chk(&argvars[1], buf);
15582 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015583 {
15584 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015585 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015586 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015587 if (fwrite(p, STRLEN(p), 1, fd) != 1)
15588 err = TRUE;
15589 if (fclose(fd) != 0)
15590 err = TRUE;
15591 if (err)
15592 {
15593 EMSG(_("E677: Error writing temp file"));
15594 goto done;
15595 }
15596 }
15597
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015598 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
15599 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015600
Bram Moolenaar071d4272004-06-13 20:20:40 +000015601#ifdef USE_CR
15602 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015603 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015604 {
15605 char_u *s;
15606
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015607 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015608 {
15609 if (*s == CAR)
15610 *s = NL;
15611 }
15612 }
15613#else
15614# ifdef USE_CRNL
15615 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015616 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015617 {
15618 char_u *s, *d;
15619
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015620 d = res;
15621 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015622 {
15623 if (s[0] == CAR && s[1] == NL)
15624 ++s;
15625 *d++ = *s;
15626 }
15627 *d = NUL;
15628 }
15629# endif
15630#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015631
15632done:
15633 if (infile != NULL)
15634 {
15635 mch_remove(infile);
15636 vim_free(infile);
15637 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015638 rettv->v_type = VAR_STRING;
15639 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015640}
15641
15642/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015643 * "tabpagebuflist()" function
15644 */
15645/* ARGSUSED */
15646 static void
15647f_tabpagebuflist(argvars, rettv)
15648 typval_T *argvars;
15649 typval_T *rettv;
15650{
15651#ifndef FEAT_WINDOWS
15652 rettv->vval.v_number = 0;
15653#else
15654 tabpage_T *tp;
15655 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015656
15657 if (argvars[0].v_type == VAR_UNKNOWN)
15658 wp = firstwin;
15659 else
15660 {
15661 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15662 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000015663 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015664 }
15665 if (wp == NULL)
15666 rettv->vval.v_number = 0;
15667 else
15668 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015669 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015670 rettv->vval.v_number = 0;
15671 else
15672 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015673 for (; wp != NULL; wp = wp->w_next)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015674 if (list_append_number(rettv->vval.v_list,
15675 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015676 break;
15677 }
15678 }
15679#endif
15680}
15681
15682
15683/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015684 * "tabpagenr()" function
15685 */
15686/* ARGSUSED */
15687 static void
15688f_tabpagenr(argvars, rettv)
15689 typval_T *argvars;
15690 typval_T *rettv;
15691{
15692 int nr = 1;
15693#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015694 char_u *arg;
15695
15696 if (argvars[0].v_type != VAR_UNKNOWN)
15697 {
15698 arg = get_tv_string_chk(&argvars[0]);
15699 nr = 0;
15700 if (arg != NULL)
15701 {
15702 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000015703 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015704 else
15705 EMSG2(_(e_invexpr2), arg);
15706 }
15707 }
15708 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015709 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015710#endif
15711 rettv->vval.v_number = nr;
15712}
15713
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015714
15715#ifdef FEAT_WINDOWS
15716static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
15717
15718/*
15719 * Common code for tabpagewinnr() and winnr().
15720 */
15721 static int
15722get_winnr(tp, argvar)
15723 tabpage_T *tp;
15724 typval_T *argvar;
15725{
15726 win_T *twin;
15727 int nr = 1;
15728 win_T *wp;
15729 char_u *arg;
15730
15731 twin = (tp == curtab) ? curwin : tp->tp_curwin;
15732 if (argvar->v_type != VAR_UNKNOWN)
15733 {
15734 arg = get_tv_string_chk(argvar);
15735 if (arg == NULL)
15736 nr = 0; /* type error; errmsg already given */
15737 else if (STRCMP(arg, "$") == 0)
15738 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
15739 else if (STRCMP(arg, "#") == 0)
15740 {
15741 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
15742 if (twin == NULL)
15743 nr = 0;
15744 }
15745 else
15746 {
15747 EMSG2(_(e_invexpr2), arg);
15748 nr = 0;
15749 }
15750 }
15751
15752 if (nr > 0)
15753 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
15754 wp != twin; wp = wp->w_next)
15755 ++nr;
15756 return nr;
15757}
15758#endif
15759
15760/*
15761 * "tabpagewinnr()" function
15762 */
15763/* ARGSUSED */
15764 static void
15765f_tabpagewinnr(argvars, rettv)
15766 typval_T *argvars;
15767 typval_T *rettv;
15768{
15769 int nr = 1;
15770#ifdef FEAT_WINDOWS
15771 tabpage_T *tp;
15772
15773 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15774 if (tp == NULL)
15775 nr = 0;
15776 else
15777 nr = get_winnr(tp, &argvars[1]);
15778#endif
15779 rettv->vval.v_number = nr;
15780}
15781
15782
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015783/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015784 * "tagfiles()" function
15785 */
15786/*ARGSUSED*/
15787 static void
15788f_tagfiles(argvars, rettv)
15789 typval_T *argvars;
15790 typval_T *rettv;
15791{
15792 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015793 tagname_T tn;
15794 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015795
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015796 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015797 {
15798 rettv->vval.v_number = 0;
15799 return;
15800 }
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015801
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015802 for (first = TRUE; ; first = FALSE)
15803 if (get_tagfname(&tn, first, fname) == FAIL
15804 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015805 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015806 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015807}
15808
15809/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000015810 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015811 */
15812 static void
15813f_taglist(argvars, rettv)
15814 typval_T *argvars;
15815 typval_T *rettv;
15816{
15817 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015818
15819 tag_pattern = get_tv_string(&argvars[0]);
15820
15821 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015822 if (*tag_pattern == NUL)
15823 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015824
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015825 if (rettv_list_alloc(rettv) == OK)
15826 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015827}
15828
15829/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015830 * "tempname()" function
15831 */
15832/*ARGSUSED*/
15833 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015834f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015835 typval_T *argvars;
15836 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015837{
15838 static int x = 'A';
15839
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015840 rettv->v_type = VAR_STRING;
15841 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015842
15843 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
15844 * names. Skip 'I' and 'O', they are used for shell redirection. */
15845 do
15846 {
15847 if (x == 'Z')
15848 x = '0';
15849 else if (x == '9')
15850 x = 'A';
15851 else
15852 {
15853#ifdef EBCDIC
15854 if (x == 'I')
15855 x = 'J';
15856 else if (x == 'R')
15857 x = 'S';
15858 else
15859#endif
15860 ++x;
15861 }
15862 } while (x == 'I' || x == 'O');
15863}
15864
15865/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000015866 * "test(list)" function: Just checking the walls...
15867 */
15868/*ARGSUSED*/
15869 static void
15870f_test(argvars, rettv)
15871 typval_T *argvars;
15872 typval_T *rettv;
15873{
15874 /* Used for unit testing. Change the code below to your liking. */
15875#if 0
15876 listitem_T *li;
15877 list_T *l;
15878 char_u *bad, *good;
15879
15880 if (argvars[0].v_type != VAR_LIST)
15881 return;
15882 l = argvars[0].vval.v_list;
15883 if (l == NULL)
15884 return;
15885 li = l->lv_first;
15886 if (li == NULL)
15887 return;
15888 bad = get_tv_string(&li->li_tv);
15889 li = li->li_next;
15890 if (li == NULL)
15891 return;
15892 good = get_tv_string(&li->li_tv);
15893 rettv->vval.v_number = test_edit_score(bad, good);
15894#endif
15895}
15896
15897/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015898 * "tolower(string)" function
15899 */
15900 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015901f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015902 typval_T *argvars;
15903 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015904{
15905 char_u *p;
15906
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015907 p = vim_strsave(get_tv_string(&argvars[0]));
15908 rettv->v_type = VAR_STRING;
15909 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015910
15911 if (p != NULL)
15912 while (*p != NUL)
15913 {
15914#ifdef FEAT_MBYTE
15915 int l;
15916
15917 if (enc_utf8)
15918 {
15919 int c, lc;
15920
15921 c = utf_ptr2char(p);
15922 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015923 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015924 /* TODO: reallocate string when byte count changes. */
15925 if (utf_char2len(lc) == l)
15926 utf_char2bytes(lc, p);
15927 p += l;
15928 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015929 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015930 p += l; /* skip multi-byte character */
15931 else
15932#endif
15933 {
15934 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
15935 ++p;
15936 }
15937 }
15938}
15939
15940/*
15941 * "toupper(string)" function
15942 */
15943 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015944f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015945 typval_T *argvars;
15946 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015947{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015948 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015949 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015950}
15951
15952/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000015953 * "tr(string, fromstr, tostr)" function
15954 */
15955 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015956f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015957 typval_T *argvars;
15958 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015959{
15960 char_u *instr;
15961 char_u *fromstr;
15962 char_u *tostr;
15963 char_u *p;
15964#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000015965 int inlen;
15966 int fromlen;
15967 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015968 int idx;
15969 char_u *cpstr;
15970 int cplen;
15971 int first = TRUE;
15972#endif
15973 char_u buf[NUMBUFLEN];
15974 char_u buf2[NUMBUFLEN];
15975 garray_T ga;
15976
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015977 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015978 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
15979 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015980
15981 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015982 rettv->v_type = VAR_STRING;
15983 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015984 if (fromstr == NULL || tostr == NULL)
15985 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000015986 ga_init2(&ga, (int)sizeof(char), 80);
15987
15988#ifdef FEAT_MBYTE
15989 if (!has_mbyte)
15990#endif
15991 /* not multi-byte: fromstr and tostr must be the same length */
15992 if (STRLEN(fromstr) != STRLEN(tostr))
15993 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015994#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000015995error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015996#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000015997 EMSG2(_(e_invarg2), fromstr);
15998 ga_clear(&ga);
15999 return;
16000 }
16001
16002 /* fromstr and tostr have to contain the same number of chars */
16003 while (*instr != NUL)
16004 {
16005#ifdef FEAT_MBYTE
16006 if (has_mbyte)
16007 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016008 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016009 cpstr = instr;
16010 cplen = inlen;
16011 idx = 0;
16012 for (p = fromstr; *p != NUL; p += fromlen)
16013 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016014 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016015 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
16016 {
16017 for (p = tostr; *p != NUL; p += tolen)
16018 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016019 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016020 if (idx-- == 0)
16021 {
16022 cplen = tolen;
16023 cpstr = p;
16024 break;
16025 }
16026 }
16027 if (*p == NUL) /* tostr is shorter than fromstr */
16028 goto error;
16029 break;
16030 }
16031 ++idx;
16032 }
16033
16034 if (first && cpstr == instr)
16035 {
16036 /* Check that fromstr and tostr have the same number of
16037 * (multi-byte) characters. Done only once when a character
16038 * of instr doesn't appear in fromstr. */
16039 first = FALSE;
16040 for (p = tostr; *p != NUL; p += tolen)
16041 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016042 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016043 --idx;
16044 }
16045 if (idx != 0)
16046 goto error;
16047 }
16048
16049 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000016050 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016051 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000016052
16053 instr += inlen;
16054 }
16055 else
16056#endif
16057 {
16058 /* When not using multi-byte chars we can do it faster. */
16059 p = vim_strchr(fromstr, *instr);
16060 if (p != NULL)
16061 ga_append(&ga, tostr[p - fromstr]);
16062 else
16063 ga_append(&ga, *instr);
16064 ++instr;
16065 }
16066 }
16067
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016068 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000016069}
16070
16071/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016072 * "type(expr)" function
16073 */
16074 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016075f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016076 typval_T *argvars;
16077 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016078{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016079 int n;
16080
16081 switch (argvars[0].v_type)
16082 {
16083 case VAR_NUMBER: n = 0; break;
16084 case VAR_STRING: n = 1; break;
16085 case VAR_FUNC: n = 2; break;
16086 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016087 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016088 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
16089 }
16090 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016091}
16092
16093/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016094 * "values(dict)" function
16095 */
16096 static void
16097f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016098 typval_T *argvars;
16099 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016100{
16101 dict_list(argvars, rettv, 1);
16102}
16103
16104/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016105 * "virtcol(string)" function
16106 */
16107 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016108f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016109 typval_T *argvars;
16110 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016111{
16112 colnr_T vcol = 0;
16113 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016114 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016115
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016116 fp = var2fpos(&argvars[0], FALSE, &fnum);
16117 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
16118 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016119 {
16120 getvvcol(curwin, fp, NULL, NULL, &vcol);
16121 ++vcol;
16122 }
16123
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016124 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016125}
16126
16127/*
16128 * "visualmode()" function
16129 */
16130/*ARGSUSED*/
16131 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016132f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016133 typval_T *argvars;
16134 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016135{
16136#ifdef FEAT_VISUAL
16137 char_u str[2];
16138
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016139 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016140 str[0] = curbuf->b_visual_mode_eval;
16141 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016142 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016143
16144 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016145 if ((argvars[0].v_type == VAR_NUMBER
16146 && argvars[0].vval.v_number != 0)
16147 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016148 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016149 curbuf->b_visual_mode_eval = NUL;
16150#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016151 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016152#endif
16153}
16154
16155/*
16156 * "winbufnr(nr)" function
16157 */
16158 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016159f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016160 typval_T *argvars;
16161 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016162{
16163 win_T *wp;
16164
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016165 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016166 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016167 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016168 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016169 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016170}
16171
16172/*
16173 * "wincol()" function
16174 */
16175/*ARGSUSED*/
16176 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016177f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016178 typval_T *argvars;
16179 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016180{
16181 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016182 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016183}
16184
16185/*
16186 * "winheight(nr)" function
16187 */
16188 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016189f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016190 typval_T *argvars;
16191 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016192{
16193 win_T *wp;
16194
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016195 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016196 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016197 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016198 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016199 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016200}
16201
16202/*
16203 * "winline()" function
16204 */
16205/*ARGSUSED*/
16206 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016207f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016208 typval_T *argvars;
16209 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016210{
16211 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016212 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016213}
16214
16215/*
16216 * "winnr()" function
16217 */
16218/* ARGSUSED */
16219 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016220f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016221 typval_T *argvars;
16222 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016223{
16224 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016225
Bram Moolenaar071d4272004-06-13 20:20:40 +000016226#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016227 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016228#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016229 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016230}
16231
16232/*
16233 * "winrestcmd()" function
16234 */
16235/* ARGSUSED */
16236 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016237f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016238 typval_T *argvars;
16239 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016240{
16241#ifdef FEAT_WINDOWS
16242 win_T *wp;
16243 int winnr = 1;
16244 garray_T ga;
16245 char_u buf[50];
16246
16247 ga_init2(&ga, (int)sizeof(char), 70);
16248 for (wp = firstwin; wp != NULL; wp = wp->w_next)
16249 {
16250 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
16251 ga_concat(&ga, buf);
16252# ifdef FEAT_VERTSPLIT
16253 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
16254 ga_concat(&ga, buf);
16255# endif
16256 ++winnr;
16257 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000016258 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016259
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016260 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016261#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016262 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016263#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016264 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016265}
16266
16267/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016268 * "winrestview()" function
16269 */
16270/* ARGSUSED */
16271 static void
16272f_winrestview(argvars, rettv)
16273 typval_T *argvars;
16274 typval_T *rettv;
16275{
16276 dict_T *dict;
16277
16278 if (argvars[0].v_type != VAR_DICT
16279 || (dict = argvars[0].vval.v_dict) == NULL)
16280 EMSG(_(e_invarg));
16281 else
16282 {
16283 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
16284 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
16285#ifdef FEAT_VIRTUALEDIT
16286 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
16287#endif
16288 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016289 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016290
Bram Moolenaar6f11a412006-09-06 20:16:42 +000016291 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016292#ifdef FEAT_DIFF
16293 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
16294#endif
16295 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
16296 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
16297
16298 check_cursor();
16299 changed_cline_bef_curs();
16300 invalidate_botline();
16301 redraw_later(VALID);
16302
16303 if (curwin->w_topline == 0)
16304 curwin->w_topline = 1;
16305 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
16306 curwin->w_topline = curbuf->b_ml.ml_line_count;
16307#ifdef FEAT_DIFF
16308 check_topfill(curwin, TRUE);
16309#endif
16310 }
16311}
16312
16313/*
16314 * "winsaveview()" function
16315 */
16316/* ARGSUSED */
16317 static void
16318f_winsaveview(argvars, rettv)
16319 typval_T *argvars;
16320 typval_T *rettv;
16321{
16322 dict_T *dict;
16323
16324 dict = dict_alloc();
16325 if (dict == NULL)
16326 return;
16327 rettv->v_type = VAR_DICT;
16328 rettv->vval.v_dict = dict;
16329 ++dict->dv_refcount;
16330
16331 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
16332 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
16333#ifdef FEAT_VIRTUALEDIT
16334 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
16335#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000016336 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016337 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
16338
16339 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
16340#ifdef FEAT_DIFF
16341 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
16342#endif
16343 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
16344 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
16345}
16346
16347/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016348 * "winwidth(nr)" function
16349 */
16350 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016351f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016352 typval_T *argvars;
16353 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016354{
16355 win_T *wp;
16356
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016357 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016358 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016359 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016360 else
16361#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016362 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016363#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016364 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016365#endif
16366}
16367
Bram Moolenaar071d4272004-06-13 20:20:40 +000016368/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016369 * "writefile()" function
16370 */
16371 static void
16372f_writefile(argvars, rettv)
16373 typval_T *argvars;
16374 typval_T *rettv;
16375{
16376 int binary = FALSE;
16377 char_u *fname;
16378 FILE *fd;
16379 listitem_T *li;
16380 char_u *s;
16381 int ret = 0;
16382 int c;
16383
16384 if (argvars[0].v_type != VAR_LIST)
16385 {
16386 EMSG2(_(e_listarg), "writefile()");
16387 return;
16388 }
16389 if (argvars[0].vval.v_list == NULL)
16390 return;
16391
16392 if (argvars[2].v_type != VAR_UNKNOWN
16393 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
16394 binary = TRUE;
16395
16396 /* Always open the file in binary mode, library functions have a mind of
16397 * their own about CR-LF conversion. */
16398 fname = get_tv_string(&argvars[1]);
16399 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
16400 {
16401 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
16402 ret = -1;
16403 }
16404 else
16405 {
16406 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
16407 li = li->li_next)
16408 {
16409 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
16410 {
16411 if (*s == '\n')
16412 c = putc(NUL, fd);
16413 else
16414 c = putc(*s, fd);
16415 if (c == EOF)
16416 {
16417 ret = -1;
16418 break;
16419 }
16420 }
16421 if (!binary || li->li_next != NULL)
16422 if (putc('\n', fd) == EOF)
16423 {
16424 ret = -1;
16425 break;
16426 }
16427 if (ret < 0)
16428 {
16429 EMSG(_(e_write));
16430 break;
16431 }
16432 }
16433 fclose(fd);
16434 }
16435
16436 rettv->vval.v_number = ret;
16437}
16438
16439/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016440 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016441 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016442 */
16443 static pos_T *
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016444var2fpos(varp, lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000016445 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016446 int lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016447 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016448{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016449 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016450 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016451 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016452
Bram Moolenaara5525202006-03-02 22:52:09 +000016453 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016454 if (varp->v_type == VAR_LIST)
16455 {
16456 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016457 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000016458 int error = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016459
16460 l = varp->vval.v_list;
16461 if (l == NULL)
16462 return NULL;
16463
16464 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016465 pos.lnum = list_find_nr(l, 0L, &error);
16466 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016467 return NULL; /* invalid line number */
16468
16469 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016470 pos.col = list_find_nr(l, 1L, &error);
16471 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016472 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016473 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaara5525202006-03-02 22:52:09 +000016474 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000016475 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016476 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016477 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016478
Bram Moolenaara5525202006-03-02 22:52:09 +000016479#ifdef FEAT_VIRTUALEDIT
16480 /* Get the virtual offset. Defaults to zero. */
16481 pos.coladd = list_find_nr(l, 2L, &error);
16482 if (error)
16483 pos.coladd = 0;
16484#endif
16485
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016486 return &pos;
16487 }
16488
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016489 name = get_tv_string_chk(varp);
16490 if (name == NULL)
16491 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016492 if (name[0] == '.') /* cursor */
16493 return &curwin->w_cursor;
16494 if (name[0] == '\'') /* mark */
16495 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016496 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016497 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
16498 return NULL;
16499 return pp;
16500 }
Bram Moolenaara5525202006-03-02 22:52:09 +000016501
16502#ifdef FEAT_VIRTUALEDIT
16503 pos.coladd = 0;
16504#endif
16505
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016506 if (name[0] == 'w' && lnum)
16507 {
16508 pos.col = 0;
16509 if (name[1] == '0') /* "w0": first visible line */
16510 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016511 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016512 pos.lnum = curwin->w_topline;
16513 return &pos;
16514 }
16515 else if (name[1] == '$') /* "w$": last visible line */
16516 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016517 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016518 pos.lnum = curwin->w_botline - 1;
16519 return &pos;
16520 }
16521 }
16522 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016523 {
16524 if (lnum)
16525 {
16526 pos.lnum = curbuf->b_ml.ml_line_count;
16527 pos.col = 0;
16528 }
16529 else
16530 {
16531 pos.lnum = curwin->w_cursor.lnum;
16532 pos.col = (colnr_T)STRLEN(ml_get_curline());
16533 }
16534 return &pos;
16535 }
16536 return NULL;
16537}
16538
16539/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016540 * Convert list in "arg" into a position and optional file number.
16541 * When "fnump" is NULL there is no file number, only 3 items.
16542 * Note that the column is passed on as-is, the caller may want to decrement
16543 * it to use 1 for the first column.
16544 * Return FAIL when conversion is not possible, doesn't check the position for
16545 * validity.
16546 */
16547 static int
16548list2fpos(arg, posp, fnump)
16549 typval_T *arg;
16550 pos_T *posp;
16551 int *fnump;
16552{
16553 list_T *l = arg->vval.v_list;
16554 long i = 0;
16555 long n;
16556
Bram Moolenaarbde35262006-07-23 20:12:24 +000016557 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
16558 * when "fnump" isn't NULL and "coladd" is optional. */
16559 if (arg->v_type != VAR_LIST
16560 || l == NULL
16561 || l->lv_len < (fnump == NULL ? 2 : 3)
16562 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016563 return FAIL;
16564
16565 if (fnump != NULL)
16566 {
16567 n = list_find_nr(l, i++, NULL); /* fnum */
16568 if (n < 0)
16569 return FAIL;
16570 if (n == 0)
16571 n = curbuf->b_fnum; /* current buffer */
16572 *fnump = n;
16573 }
16574
16575 n = list_find_nr(l, i++, NULL); /* lnum */
16576 if (n < 0)
16577 return FAIL;
16578 posp->lnum = n;
16579
16580 n = list_find_nr(l, i++, NULL); /* col */
16581 if (n < 0)
16582 return FAIL;
16583 posp->col = n;
16584
16585#ifdef FEAT_VIRTUALEDIT
16586 n = list_find_nr(l, i, NULL);
16587 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000016588 posp->coladd = 0;
16589 else
16590 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016591#endif
16592
16593 return OK;
16594}
16595
16596/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016597 * Get the length of an environment variable name.
16598 * Advance "arg" to the first character after the name.
16599 * Return 0 for error.
16600 */
16601 static int
16602get_env_len(arg)
16603 char_u **arg;
16604{
16605 char_u *p;
16606 int len;
16607
16608 for (p = *arg; vim_isIDc(*p); ++p)
16609 ;
16610 if (p == *arg) /* no name found */
16611 return 0;
16612
16613 len = (int)(p - *arg);
16614 *arg = p;
16615 return len;
16616}
16617
16618/*
16619 * Get the length of the name of a function or internal variable.
16620 * "arg" is advanced to the first non-white character after the name.
16621 * Return 0 if something is wrong.
16622 */
16623 static int
16624get_id_len(arg)
16625 char_u **arg;
16626{
16627 char_u *p;
16628 int len;
16629
16630 /* Find the end of the name. */
16631 for (p = *arg; eval_isnamec(*p); ++p)
16632 ;
16633 if (p == *arg) /* no name found */
16634 return 0;
16635
16636 len = (int)(p - *arg);
16637 *arg = skipwhite(p);
16638
16639 return len;
16640}
16641
16642/*
Bram Moolenaara7043832005-01-21 11:56:39 +000016643 * Get the length of the name of a variable or function.
16644 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000016645 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016646 * Return -1 if curly braces expansion failed.
16647 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016648 * If the name contains 'magic' {}'s, expand them and return the
16649 * expanded name in an allocated string via 'alias' - caller must free.
16650 */
16651 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016652get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016653 char_u **arg;
16654 char_u **alias;
16655 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016656 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016657{
16658 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016659 char_u *p;
16660 char_u *expr_start;
16661 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016662
16663 *alias = NULL; /* default to no alias */
16664
16665 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
16666 && (*arg)[2] == (int)KE_SNR)
16667 {
16668 /* hard coded <SNR>, already translated */
16669 *arg += 3;
16670 return get_id_len(arg) + 3;
16671 }
16672 len = eval_fname_script(*arg);
16673 if (len > 0)
16674 {
16675 /* literal "<SID>", "s:" or "<SNR>" */
16676 *arg += len;
16677 }
16678
Bram Moolenaar071d4272004-06-13 20:20:40 +000016679 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016680 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016681 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016682 p = find_name_end(*arg, &expr_start, &expr_end,
16683 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016684 if (expr_start != NULL)
16685 {
16686 char_u *temp_string;
16687
16688 if (!evaluate)
16689 {
16690 len += (int)(p - *arg);
16691 *arg = skipwhite(p);
16692 return len;
16693 }
16694
16695 /*
16696 * Include any <SID> etc in the expanded string:
16697 * Thus the -len here.
16698 */
16699 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
16700 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016701 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016702 *alias = temp_string;
16703 *arg = skipwhite(p);
16704 return (int)STRLEN(temp_string);
16705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016706
16707 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016708 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016709 EMSG2(_(e_invexpr2), *arg);
16710
16711 return len;
16712}
16713
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016714/*
16715 * Find the end of a variable or function name, taking care of magic braces.
16716 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
16717 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016718 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016719 * Return a pointer to just after the name. Equal to "arg" if there is no
16720 * valid name.
16721 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016722 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016723find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016724 char_u *arg;
16725 char_u **expr_start;
16726 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016727 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016728{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016729 int mb_nest = 0;
16730 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016731 char_u *p;
16732
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016733 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016734 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016735 *expr_start = NULL;
16736 *expr_end = NULL;
16737 }
16738
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016739 /* Quick check for valid starting character. */
16740 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
16741 return arg;
16742
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016743 for (p = arg; *p != NUL
16744 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016745 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016746 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016747 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000016748 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016749 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000016750 if (*p == '\'')
16751 {
16752 /* skip over 'string' to avoid counting [ and ] inside it. */
16753 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
16754 ;
16755 if (*p == NUL)
16756 break;
16757 }
16758 else if (*p == '"')
16759 {
16760 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
16761 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
16762 if (*p == '\\' && p[1] != NUL)
16763 ++p;
16764 if (*p == NUL)
16765 break;
16766 }
16767
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016768 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016769 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016770 if (*p == '[')
16771 ++br_nest;
16772 else if (*p == ']')
16773 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016774 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000016775
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016776 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016777 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016778 if (*p == '{')
16779 {
16780 mb_nest++;
16781 if (expr_start != NULL && *expr_start == NULL)
16782 *expr_start = p;
16783 }
16784 else if (*p == '}')
16785 {
16786 mb_nest--;
16787 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
16788 *expr_end = p;
16789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016791 }
16792
16793 return p;
16794}
16795
16796/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016797 * Expands out the 'magic' {}'s in a variable/function name.
16798 * Note that this can call itself recursively, to deal with
16799 * constructs like foo{bar}{baz}{bam}
16800 * The four pointer arguments point to "foo{expre}ss{ion}bar"
16801 * "in_start" ^
16802 * "expr_start" ^
16803 * "expr_end" ^
16804 * "in_end" ^
16805 *
16806 * Returns a new allocated string, which the caller must free.
16807 * Returns NULL for failure.
16808 */
16809 static char_u *
16810make_expanded_name(in_start, expr_start, expr_end, in_end)
16811 char_u *in_start;
16812 char_u *expr_start;
16813 char_u *expr_end;
16814 char_u *in_end;
16815{
16816 char_u c1;
16817 char_u *retval = NULL;
16818 char_u *temp_result;
16819 char_u *nextcmd = NULL;
16820
16821 if (expr_end == NULL || in_end == NULL)
16822 return NULL;
16823 *expr_start = NUL;
16824 *expr_end = NUL;
16825 c1 = *in_end;
16826 *in_end = NUL;
16827
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016828 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016829 if (temp_result != NULL && nextcmd == NULL)
16830 {
16831 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
16832 + (in_end - expr_end) + 1));
16833 if (retval != NULL)
16834 {
16835 STRCPY(retval, in_start);
16836 STRCAT(retval, temp_result);
16837 STRCAT(retval, expr_end + 1);
16838 }
16839 }
16840 vim_free(temp_result);
16841
16842 *in_end = c1; /* put char back for error messages */
16843 *expr_start = '{';
16844 *expr_end = '}';
16845
16846 if (retval != NULL)
16847 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016848 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016849 if (expr_start != NULL)
16850 {
16851 /* Further expansion! */
16852 temp_result = make_expanded_name(retval, expr_start,
16853 expr_end, temp_result);
16854 vim_free(retval);
16855 retval = temp_result;
16856 }
16857 }
16858
16859 return retval;
16860}
16861
16862/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016863 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016864 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016865 */
16866 static int
16867eval_isnamec(c)
16868 int c;
16869{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016870 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
16871}
16872
16873/*
16874 * Return TRUE if character "c" can be used as the first character in a
16875 * variable or function name (excluding '{' and '}').
16876 */
16877 static int
16878eval_isnamec1(c)
16879 int c;
16880{
16881 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000016882}
16883
16884/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016885 * Set number v: variable to "val".
16886 */
16887 void
16888set_vim_var_nr(idx, val)
16889 int idx;
16890 long val;
16891{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016892 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016893}
16894
16895/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016896 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016897 */
16898 long
16899get_vim_var_nr(idx)
16900 int idx;
16901{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016902 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016903}
16904
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016905#if defined(FEAT_AUTOCMD) || defined(PROTO)
16906/*
16907 * Get string v: variable value. Uses a static buffer, can only be used once.
16908 */
16909 char_u *
16910get_vim_var_str(idx)
16911 int idx;
16912{
16913 return get_tv_string(&vimvars[idx].vv_tv);
16914}
16915#endif
16916
Bram Moolenaar071d4272004-06-13 20:20:40 +000016917/*
16918 * Set v:count, v:count1 and v:prevcount.
16919 */
16920 void
16921set_vcount(count, count1)
16922 long count;
16923 long count1;
16924{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016925 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
16926 vimvars[VV_COUNT].vv_nr = count;
16927 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016928}
16929
16930/*
16931 * Set string v: variable to a copy of "val".
16932 */
16933 void
16934set_vim_var_string(idx, val, len)
16935 int idx;
16936 char_u *val;
16937 int len; /* length of "val" to use or -1 (whole string) */
16938{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016939 /* Need to do this (at least) once, since we can't initialize a union.
16940 * Will always be invoked when "v:progname" is set. */
16941 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
16942
Bram Moolenaare9a41262005-01-15 22:18:47 +000016943 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016944 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016945 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016946 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016947 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016948 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000016949 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016950}
16951
16952/*
16953 * Set v:register if needed.
16954 */
16955 void
16956set_reg_var(c)
16957 int c;
16958{
16959 char_u regname;
16960
16961 if (c == 0 || c == ' ')
16962 regname = '"';
16963 else
16964 regname = c;
16965 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000016966 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016967 set_vim_var_string(VV_REG, &regname, 1);
16968}
16969
16970/*
16971 * Get or set v:exception. If "oldval" == NULL, return the current value.
16972 * Otherwise, restore the value to "oldval" and return NULL.
16973 * Must always be called in pairs to save and restore v:exception! Does not
16974 * take care of memory allocations.
16975 */
16976 char_u *
16977v_exception(oldval)
16978 char_u *oldval;
16979{
16980 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016981 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016982
Bram Moolenaare9a41262005-01-15 22:18:47 +000016983 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016984 return NULL;
16985}
16986
16987/*
16988 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
16989 * Otherwise, restore the value to "oldval" and return NULL.
16990 * Must always be called in pairs to save and restore v:throwpoint! Does not
16991 * take care of memory allocations.
16992 */
16993 char_u *
16994v_throwpoint(oldval)
16995 char_u *oldval;
16996{
16997 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016998 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016999
Bram Moolenaare9a41262005-01-15 22:18:47 +000017000 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017001 return NULL;
17002}
17003
17004#if defined(FEAT_AUTOCMD) || defined(PROTO)
17005/*
17006 * Set v:cmdarg.
17007 * If "eap" != NULL, use "eap" to generate the value and return the old value.
17008 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
17009 * Must always be called in pairs!
17010 */
17011 char_u *
17012set_cmdarg(eap, oldarg)
17013 exarg_T *eap;
17014 char_u *oldarg;
17015{
17016 char_u *oldval;
17017 char_u *newval;
17018 unsigned len;
17019
Bram Moolenaare9a41262005-01-15 22:18:47 +000017020 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017021 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017022 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017023 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000017024 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017025 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017026 }
17027
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017028 if (eap->force_bin == FORCE_BIN)
17029 len = 6;
17030 else if (eap->force_bin == FORCE_NOBIN)
17031 len = 8;
17032 else
17033 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017034
17035 if (eap->read_edit)
17036 len += 7;
17037
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017038 if (eap->force_ff != 0)
17039 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
17040# ifdef FEAT_MBYTE
17041 if (eap->force_enc != 0)
17042 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000017043 if (eap->bad_char != 0)
17044 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017045# endif
17046
17047 newval = alloc(len + 1);
17048 if (newval == NULL)
17049 return NULL;
17050
17051 if (eap->force_bin == FORCE_BIN)
17052 sprintf((char *)newval, " ++bin");
17053 else if (eap->force_bin == FORCE_NOBIN)
17054 sprintf((char *)newval, " ++nobin");
17055 else
17056 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017057
17058 if (eap->read_edit)
17059 STRCAT(newval, " ++edit");
17060
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017061 if (eap->force_ff != 0)
17062 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
17063 eap->cmd + eap->force_ff);
17064# ifdef FEAT_MBYTE
17065 if (eap->force_enc != 0)
17066 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
17067 eap->cmd + eap->force_enc);
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000017068 if (eap->bad_char != 0)
17069 sprintf((char *)newval + STRLEN(newval), " ++bad=%s",
17070 eap->cmd + eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017071# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000017072 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017073 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017074}
17075#endif
17076
17077/*
17078 * Get the value of internal variable "name".
17079 * Return OK or FAIL.
17080 */
17081 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017082get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017083 char_u *name;
17084 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000017085 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017086 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017087{
17088 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000017089 typval_T *tv = NULL;
17090 typval_T atv;
17091 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017092 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017093
17094 /* truncate the name, so that we can use strcmp() */
17095 cc = name[len];
17096 name[len] = NUL;
17097
17098 /*
17099 * Check for "b:changedtick".
17100 */
17101 if (STRCMP(name, "b:changedtick") == 0)
17102 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000017103 atv.v_type = VAR_NUMBER;
17104 atv.vval.v_number = curbuf->b_changedtick;
17105 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017106 }
17107
17108 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017109 * Check for user-defined variables.
17110 */
17111 else
17112 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017113 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017114 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017115 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017116 }
17117
Bram Moolenaare9a41262005-01-15 22:18:47 +000017118 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017119 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017120 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017121 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017122 ret = FAIL;
17123 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017124 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017125 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017126
17127 name[len] = cc;
17128
17129 return ret;
17130}
17131
17132/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017133 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
17134 * Also handle function call with Funcref variable: func(expr)
17135 * Can all be combined: dict.func(expr)[idx]['func'](expr)
17136 */
17137 static int
17138handle_subscript(arg, rettv, evaluate, verbose)
17139 char_u **arg;
17140 typval_T *rettv;
17141 int evaluate; /* do more than finding the end */
17142 int verbose; /* give error messages */
17143{
17144 int ret = OK;
17145 dict_T *selfdict = NULL;
17146 char_u *s;
17147 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000017148 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017149
17150 while (ret == OK
17151 && (**arg == '['
17152 || (**arg == '.' && rettv->v_type == VAR_DICT)
17153 || (**arg == '(' && rettv->v_type == VAR_FUNC))
17154 && !vim_iswhite(*(*arg - 1)))
17155 {
17156 if (**arg == '(')
17157 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000017158 /* need to copy the funcref so that we can clear rettv */
17159 functv = *rettv;
17160 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017161
17162 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000017163 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017164 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000017165 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
17166 &len, evaluate, selfdict);
17167
17168 /* Clear the funcref afterwards, so that deleting it while
17169 * evaluating the arguments is possible (see test55). */
17170 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017171
17172 /* Stop the expression evaluation when immediately aborting on
17173 * error, or when an interrupt occurred or an exception was thrown
17174 * but not caught. */
17175 if (aborting())
17176 {
17177 if (ret == OK)
17178 clear_tv(rettv);
17179 ret = FAIL;
17180 }
17181 dict_unref(selfdict);
17182 selfdict = NULL;
17183 }
17184 else /* **arg == '[' || **arg == '.' */
17185 {
17186 dict_unref(selfdict);
17187 if (rettv->v_type == VAR_DICT)
17188 {
17189 selfdict = rettv->vval.v_dict;
17190 if (selfdict != NULL)
17191 ++selfdict->dv_refcount;
17192 }
17193 else
17194 selfdict = NULL;
17195 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
17196 {
17197 clear_tv(rettv);
17198 ret = FAIL;
17199 }
17200 }
17201 }
17202 dict_unref(selfdict);
17203 return ret;
17204}
17205
17206/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017207 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
17208 * value).
17209 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017210 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017211alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017212{
Bram Moolenaar33570922005-01-25 22:26:29 +000017213 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017214}
17215
17216/*
17217 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017218 * The string "s" must have been allocated, it is consumed.
17219 * Return NULL for out of memory, the variable otherwise.
17220 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017221 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017222alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017223 char_u *s;
17224{
Bram Moolenaar33570922005-01-25 22:26:29 +000017225 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017226
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017227 rettv = alloc_tv();
17228 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017229 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017230 rettv->v_type = VAR_STRING;
17231 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017232 }
17233 else
17234 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017235 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017236}
17237
17238/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017239 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017240 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000017241 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017242free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017243 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017244{
17245 if (varp != NULL)
17246 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017247 switch (varp->v_type)
17248 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017249 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017250 func_unref(varp->vval.v_string);
17251 /*FALLTHROUGH*/
17252 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017253 vim_free(varp->vval.v_string);
17254 break;
17255 case VAR_LIST:
17256 list_unref(varp->vval.v_list);
17257 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017258 case VAR_DICT:
17259 dict_unref(varp->vval.v_dict);
17260 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017261 case VAR_NUMBER:
17262 case VAR_UNKNOWN:
17263 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017264 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000017265 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017266 break;
17267 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017268 vim_free(varp);
17269 }
17270}
17271
17272/*
17273 * Free the memory for a variable value and set the value to NULL or 0.
17274 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017275 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017276clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017277 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017278{
17279 if (varp != NULL)
17280 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017281 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017282 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017283 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017284 func_unref(varp->vval.v_string);
17285 /*FALLTHROUGH*/
17286 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017287 vim_free(varp->vval.v_string);
17288 varp->vval.v_string = NULL;
17289 break;
17290 case VAR_LIST:
17291 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017292 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017293 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017294 case VAR_DICT:
17295 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017296 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017297 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017298 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017299 varp->vval.v_number = 0;
17300 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017301 case VAR_UNKNOWN:
17302 break;
17303 default:
17304 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017305 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017306 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017307 }
17308}
17309
17310/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017311 * Set the value of a variable to NULL without freeing items.
17312 */
17313 static void
17314init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017315 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017316{
17317 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017318 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017319}
17320
17321/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017322 * Get the number value of a variable.
17323 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017324 * For incompatible types, return 0.
17325 * get_tv_number_chk() is similar to get_tv_number(), but informs the
17326 * caller of incompatible types: it sets *denote to TRUE if "denote"
17327 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017328 */
17329 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017330get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017331 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017332{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017333 int error = FALSE;
17334
17335 return get_tv_number_chk(varp, &error); /* return 0L on error */
17336}
17337
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017338 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017339get_tv_number_chk(varp, denote)
17340 typval_T *varp;
17341 int *denote;
17342{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017343 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017344
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017345 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017346 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017347 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017348 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017349 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017350 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017351 break;
17352 case VAR_STRING:
17353 if (varp->vval.v_string != NULL)
17354 vim_str2nr(varp->vval.v_string, NULL, NULL,
17355 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017356 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017357 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000017358 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017359 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017360 case VAR_DICT:
17361 EMSG(_("E728: Using a Dictionary as a number"));
17362 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017363 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017364 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017365 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017366 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017367 if (denote == NULL) /* useful for values that must be unsigned */
17368 n = -1;
17369 else
17370 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017371 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017372}
17373
17374/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000017375 * Get the lnum from the first argument.
17376 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017377 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017378 */
17379 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017380get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000017381 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017382{
Bram Moolenaar33570922005-01-25 22:26:29 +000017383 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017384 linenr_T lnum;
17385
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017386 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017387 if (lnum == 0) /* no valid number, try using line() */
17388 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017389 rettv.v_type = VAR_NUMBER;
17390 f_line(argvars, &rettv);
17391 lnum = rettv.vval.v_number;
17392 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017393 }
17394 return lnum;
17395}
17396
17397/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000017398 * Get the lnum from the first argument.
17399 * Also accepts "$", then "buf" is used.
17400 * Returns 0 on error.
17401 */
17402 static linenr_T
17403get_tv_lnum_buf(argvars, buf)
17404 typval_T *argvars;
17405 buf_T *buf;
17406{
17407 if (argvars[0].v_type == VAR_STRING
17408 && argvars[0].vval.v_string != NULL
17409 && argvars[0].vval.v_string[0] == '$'
17410 && buf != NULL)
17411 return buf->b_ml.ml_line_count;
17412 return get_tv_number_chk(&argvars[0], NULL);
17413}
17414
17415/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017416 * Get the string value of a variable.
17417 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000017418 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
17419 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017420 * If the String variable has never been set, return an empty string.
17421 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017422 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
17423 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017424 */
17425 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017426get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017427 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017428{
17429 static char_u mybuf[NUMBUFLEN];
17430
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017431 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017432}
17433
17434 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017435get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000017436 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017437 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017438{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017439 char_u *res = get_tv_string_buf_chk(varp, buf);
17440
17441 return res != NULL ? res : (char_u *)"";
17442}
17443
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017444 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017445get_tv_string_chk(varp)
17446 typval_T *varp;
17447{
17448 static char_u mybuf[NUMBUFLEN];
17449
17450 return get_tv_string_buf_chk(varp, mybuf);
17451}
17452
17453 static char_u *
17454get_tv_string_buf_chk(varp, buf)
17455 typval_T *varp;
17456 char_u *buf;
17457{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017458 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017459 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017460 case VAR_NUMBER:
17461 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
17462 return buf;
17463 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017464 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017465 break;
17466 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017467 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000017468 break;
17469 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017470 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017471 break;
17472 case VAR_STRING:
17473 if (varp->vval.v_string != NULL)
17474 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017475 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017476 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017477 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017478 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017479 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017480 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017481}
17482
17483/*
17484 * Find variable "name" in the list of variables.
17485 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017486 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017487 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000017488 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017489 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017490 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017491find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017492 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017493 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017494{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017495 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017496 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017497
Bram Moolenaara7043832005-01-21 11:56:39 +000017498 ht = find_var_ht(name, &varname);
17499 if (htp != NULL)
17500 *htp = ht;
17501 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017502 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017503 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017504}
17505
17506/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017507 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000017508 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017509 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017510 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017511find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000017512 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000017513 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017514 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000017515{
Bram Moolenaar33570922005-01-25 22:26:29 +000017516 hashitem_T *hi;
17517
17518 if (*varname == NUL)
17519 {
17520 /* Must be something like "s:", otherwise "ht" would be NULL. */
17521 switch (varname[-2])
17522 {
17523 case 's': return &SCRIPT_SV(current_SID).sv_var;
17524 case 'g': return &globvars_var;
17525 case 'v': return &vimvars_var;
17526 case 'b': return &curbuf->b_bufvar;
17527 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017528#ifdef FEAT_WINDOWS
17529 case 't': return &curtab->tp_winvar;
17530#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017531 case 'l': return current_funccal == NULL
17532 ? NULL : &current_funccal->l_vars_var;
17533 case 'a': return current_funccal == NULL
17534 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000017535 }
17536 return NULL;
17537 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017538
17539 hi = hash_find(ht, varname);
17540 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017541 {
17542 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017543 * worked find the variable again. Don't auto-load a script if it was
17544 * loaded already, otherwise it would be loaded every time when
17545 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017546 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017547 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017548 hi = hash_find(ht, varname);
17549 if (HASHITEM_EMPTY(hi))
17550 return NULL;
17551 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017552 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017553}
17554
17555/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017556 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017557 * Set "varname" to the start of name without ':'.
17558 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017559 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017560find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017561 char_u *name;
17562 char_u **varname;
17563{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017564 hashitem_T *hi;
17565
Bram Moolenaar071d4272004-06-13 20:20:40 +000017566 if (name[1] != ':')
17567 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017568 /* The name must not start with a colon or #. */
17569 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017570 return NULL;
17571 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017572
17573 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017574 hi = hash_find(&compat_hashtab, name);
17575 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000017576 return &compat_hashtab;
17577
Bram Moolenaar071d4272004-06-13 20:20:40 +000017578 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017579 return &globvarht; /* global variable */
17580 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017581 }
17582 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017583 if (*name == 'g') /* global variable */
17584 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017585 /* There must be no ':' or '#' in the rest of the name, unless g: is used
17586 */
17587 if (vim_strchr(name + 2, ':') != NULL
17588 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017589 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017590 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017591 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017592 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017593 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017594#ifdef FEAT_WINDOWS
17595 if (*name == 't') /* tab page variable */
17596 return &curtab->tp_vars.dv_hashtab;
17597#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000017598 if (*name == 'v') /* v: variable */
17599 return &vimvarht;
17600 if (*name == 'a' && current_funccal != NULL) /* function argument */
17601 return &current_funccal->l_avars.dv_hashtab;
17602 if (*name == 'l' && current_funccal != NULL) /* local function variable */
17603 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017604 if (*name == 's' /* script variable */
17605 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
17606 return &SCRIPT_VARS(current_SID);
17607 return NULL;
17608}
17609
17610/*
17611 * Get the string value of a (global/local) variable.
17612 * Returns NULL when it doesn't exist.
17613 */
17614 char_u *
17615get_var_value(name)
17616 char_u *name;
17617{
Bram Moolenaar33570922005-01-25 22:26:29 +000017618 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017619
Bram Moolenaara7043832005-01-21 11:56:39 +000017620 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017621 if (v == NULL)
17622 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000017623 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017624}
17625
17626/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017627 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000017628 * sourcing this script and when executing functions defined in the script.
17629 */
17630 void
17631new_script_vars(id)
17632 scid_T id;
17633{
Bram Moolenaara7043832005-01-21 11:56:39 +000017634 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000017635 hashtab_T *ht;
17636 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000017637
Bram Moolenaar071d4272004-06-13 20:20:40 +000017638 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
17639 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017640 /* Re-allocating ga_data means that an ht_array pointing to
17641 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000017642 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000017643 for (i = 1; i <= ga_scripts.ga_len; ++i)
17644 {
17645 ht = &SCRIPT_VARS(i);
17646 if (ht->ht_mask == HT_INIT_SIZE - 1)
17647 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000017648 sv = &SCRIPT_SV(i);
17649 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000017650 }
17651
Bram Moolenaar071d4272004-06-13 20:20:40 +000017652 while (ga_scripts.ga_len < id)
17653 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017654 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
17655 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017656 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017657 }
17658 }
17659}
17660
17661/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017662 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
17663 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017664 */
17665 void
Bram Moolenaar33570922005-01-25 22:26:29 +000017666init_var_dict(dict, dict_var)
17667 dict_T *dict;
17668 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017669{
Bram Moolenaar33570922005-01-25 22:26:29 +000017670 hash_init(&dict->dv_hashtab);
17671 dict->dv_refcount = 99999;
17672 dict_var->di_tv.vval.v_dict = dict;
17673 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017674 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017675 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
17676 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017677}
17678
17679/*
17680 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000017681 * Frees all allocated variables and the value they contain.
17682 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017683 */
17684 void
Bram Moolenaara7043832005-01-21 11:56:39 +000017685vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000017686 hashtab_T *ht;
17687{
17688 vars_clear_ext(ht, TRUE);
17689}
17690
17691/*
17692 * Like vars_clear(), but only free the value if "free_val" is TRUE.
17693 */
17694 static void
17695vars_clear_ext(ht, free_val)
17696 hashtab_T *ht;
17697 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017698{
Bram Moolenaara7043832005-01-21 11:56:39 +000017699 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000017700 hashitem_T *hi;
17701 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017702
Bram Moolenaar33570922005-01-25 22:26:29 +000017703 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017704 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000017705 for (hi = ht->ht_array; todo > 0; ++hi)
17706 {
17707 if (!HASHITEM_EMPTY(hi))
17708 {
17709 --todo;
17710
Bram Moolenaar33570922005-01-25 22:26:29 +000017711 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000017712 * ht_array might change then. hash_clear() takes care of it
17713 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017714 v = HI2DI(hi);
17715 if (free_val)
17716 clear_tv(&v->di_tv);
17717 if ((v->di_flags & DI_FLAGS_FIX) == 0)
17718 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000017719 }
17720 }
17721 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017722 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017723}
17724
Bram Moolenaara7043832005-01-21 11:56:39 +000017725/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017726 * Delete a variable from hashtab "ht" at item "hi".
17727 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000017728 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017729 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000017730delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000017731 hashtab_T *ht;
17732 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017733{
Bram Moolenaar33570922005-01-25 22:26:29 +000017734 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017735
17736 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000017737 clear_tv(&di->di_tv);
17738 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017739}
17740
17741/*
17742 * List the value of one internal variable.
17743 */
17744 static void
17745list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000017746 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017747 char_u *prefix;
17748{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017749 char_u *tofree;
17750 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017751 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017752
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017753 s = echo_string(&v->di_tv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000017754 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017755 s == NULL ? (char_u *)"" : s);
17756 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017757}
17758
Bram Moolenaar071d4272004-06-13 20:20:40 +000017759 static void
17760list_one_var_a(prefix, name, type, string)
17761 char_u *prefix;
17762 char_u *name;
17763 int type;
17764 char_u *string;
17765{
17766 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
17767 if (name != NULL) /* "a:" vars don't have a name stored */
17768 msg_puts(name);
17769 msg_putchar(' ');
17770 msg_advance(22);
17771 if (type == VAR_NUMBER)
17772 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017773 else if (type == VAR_FUNC)
17774 msg_putchar('*');
17775 else if (type == VAR_LIST)
17776 {
17777 msg_putchar('[');
17778 if (*string == '[')
17779 ++string;
17780 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017781 else if (type == VAR_DICT)
17782 {
17783 msg_putchar('{');
17784 if (*string == '{')
17785 ++string;
17786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017787 else
17788 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017789
Bram Moolenaar071d4272004-06-13 20:20:40 +000017790 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017791
17792 if (type == VAR_FUNC)
17793 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017794}
17795
17796/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017797 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000017798 * If the variable already exists, the value is updated.
17799 * Otherwise the variable is created.
17800 */
17801 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017802set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017803 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017804 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017805 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017806{
Bram Moolenaar33570922005-01-25 22:26:29 +000017807 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017808 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017809 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017810 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017811
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017812 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017813 {
17814 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
17815 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
17816 ? name[2] : name[0]))
17817 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017818 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017819 return;
17820 }
17821 if (function_exists(name))
17822 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017823 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017824 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017825 return;
17826 }
17827 }
17828
Bram Moolenaara7043832005-01-21 11:56:39 +000017829 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000017830 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000017831 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000017832 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000017833 return;
17834 }
17835
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017836 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017837 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017838 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017839 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017840 if (var_check_ro(v->di_flags, name)
17841 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000017842 return;
17843 if (v->di_tv.v_type != tv->v_type
17844 && !((v->di_tv.v_type == VAR_STRING
17845 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017846 && (tv->v_type == VAR_STRING
17847 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017848 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017849 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017850 return;
17851 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017852
17853 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000017854 * Handle setting internal v: variables separately: we don't change
17855 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000017856 */
17857 if (ht == &vimvarht)
17858 {
17859 if (v->di_tv.v_type == VAR_STRING)
17860 {
17861 vim_free(v->di_tv.vval.v_string);
17862 if (copy || tv->v_type != VAR_STRING)
17863 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
17864 else
17865 {
17866 /* Take over the string to avoid an extra alloc/free. */
17867 v->di_tv.vval.v_string = tv->vval.v_string;
17868 tv->vval.v_string = NULL;
17869 }
17870 }
17871 else if (v->di_tv.v_type != VAR_NUMBER)
17872 EMSG2(_(e_intern2), "set_var()");
17873 else
17874 v->di_tv.vval.v_number = get_tv_number(tv);
17875 return;
17876 }
17877
17878 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017879 }
17880 else /* add a new variable */
17881 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000017882 /* Can't add "v:" variable. */
17883 if (ht == &vimvarht)
17884 {
17885 EMSG2(_(e_illvar), name);
17886 return;
17887 }
17888
Bram Moolenaar92124a32005-06-17 22:03:40 +000017889 /* Make sure the variable name is valid. */
17890 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000017891 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
17892 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000017893 {
17894 EMSG2(_(e_illvar), varname);
17895 return;
17896 }
17897
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017898 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
17899 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000017900 if (v == NULL)
17901 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000017902 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000017903 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017904 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017905 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017906 return;
17907 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017908 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017909 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017910
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017911 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000017912 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017913 else
17914 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017915 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017916 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017917 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017919}
17920
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017921/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000017922 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000017923 * Also give an error message.
17924 */
17925 static int
17926var_check_ro(flags, name)
17927 int flags;
17928 char_u *name;
17929{
17930 if (flags & DI_FLAGS_RO)
17931 {
17932 EMSG2(_(e_readonlyvar), name);
17933 return TRUE;
17934 }
17935 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
17936 {
17937 EMSG2(_(e_readonlysbx), name);
17938 return TRUE;
17939 }
17940 return FALSE;
17941}
17942
17943/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000017944 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
17945 * Also give an error message.
17946 */
17947 static int
17948var_check_fixed(flags, name)
17949 int flags;
17950 char_u *name;
17951{
17952 if (flags & DI_FLAGS_FIX)
17953 {
17954 EMSG2(_("E795: Cannot delete variable %s"), name);
17955 return TRUE;
17956 }
17957 return FALSE;
17958}
17959
17960/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017961 * Return TRUE if typeval "tv" is set to be locked (immutable).
17962 * Also give an error message, using "name".
17963 */
17964 static int
17965tv_check_lock(lock, name)
17966 int lock;
17967 char_u *name;
17968{
17969 if (lock & VAR_LOCKED)
17970 {
17971 EMSG2(_("E741: Value is locked: %s"),
17972 name == NULL ? (char_u *)_("Unknown") : name);
17973 return TRUE;
17974 }
17975 if (lock & VAR_FIXED)
17976 {
17977 EMSG2(_("E742: Cannot change value of %s"),
17978 name == NULL ? (char_u *)_("Unknown") : name);
17979 return TRUE;
17980 }
17981 return FALSE;
17982}
17983
17984/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017985 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017986 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017987 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017988 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017989 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017990copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000017991 typval_T *from;
17992 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017993{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017994 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017995 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017996 switch (from->v_type)
17997 {
17998 case VAR_NUMBER:
17999 to->vval.v_number = from->vval.v_number;
18000 break;
18001 case VAR_STRING:
18002 case VAR_FUNC:
18003 if (from->vval.v_string == NULL)
18004 to->vval.v_string = NULL;
18005 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018006 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018007 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018008 if (from->v_type == VAR_FUNC)
18009 func_ref(to->vval.v_string);
18010 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018011 break;
18012 case VAR_LIST:
18013 if (from->vval.v_list == NULL)
18014 to->vval.v_list = NULL;
18015 else
18016 {
18017 to->vval.v_list = from->vval.v_list;
18018 ++to->vval.v_list->lv_refcount;
18019 }
18020 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018021 case VAR_DICT:
18022 if (from->vval.v_dict == NULL)
18023 to->vval.v_dict = NULL;
18024 else
18025 {
18026 to->vval.v_dict = from->vval.v_dict;
18027 ++to->vval.v_dict->dv_refcount;
18028 }
18029 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018030 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018031 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018032 break;
18033 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018034}
18035
18036/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000018037 * Make a copy of an item.
18038 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018039 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
18040 * reference to an already copied list/dict can be used.
18041 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018042 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018043 static int
18044item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000018045 typval_T *from;
18046 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018047 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018048 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018049{
18050 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018051 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018052
Bram Moolenaar33570922005-01-25 22:26:29 +000018053 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018054 {
18055 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018056 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018057 }
18058 ++recurse;
18059
18060 switch (from->v_type)
18061 {
18062 case VAR_NUMBER:
18063 case VAR_STRING:
18064 case VAR_FUNC:
18065 copy_tv(from, to);
18066 break;
18067 case VAR_LIST:
18068 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018069 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018070 if (from->vval.v_list == NULL)
18071 to->vval.v_list = NULL;
18072 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
18073 {
18074 /* use the copy made earlier */
18075 to->vval.v_list = from->vval.v_list->lv_copylist;
18076 ++to->vval.v_list->lv_refcount;
18077 }
18078 else
18079 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
18080 if (to->vval.v_list == NULL)
18081 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018082 break;
18083 case VAR_DICT:
18084 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018085 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018086 if (from->vval.v_dict == NULL)
18087 to->vval.v_dict = NULL;
18088 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
18089 {
18090 /* use the copy made earlier */
18091 to->vval.v_dict = from->vval.v_dict->dv_copydict;
18092 ++to->vval.v_dict->dv_refcount;
18093 }
18094 else
18095 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
18096 if (to->vval.v_dict == NULL)
18097 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018098 break;
18099 default:
18100 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018101 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018102 }
18103 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018104 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018105}
18106
18107/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018108 * ":echo expr1 ..." print each argument separated with a space, add a
18109 * newline at the end.
18110 * ":echon expr1 ..." print each argument plain.
18111 */
18112 void
18113ex_echo(eap)
18114 exarg_T *eap;
18115{
18116 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018117 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018118 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018119 char_u *p;
18120 int needclr = TRUE;
18121 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018122 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018123
18124 if (eap->skip)
18125 ++emsg_skip;
18126 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
18127 {
18128 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018129 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018130 {
18131 /*
18132 * Report the invalid expression unless the expression evaluation
18133 * has been cancelled due to an aborting error, an interrupt, or an
18134 * exception.
18135 */
18136 if (!aborting())
18137 EMSG2(_(e_invexpr2), p);
18138 break;
18139 }
18140 if (!eap->skip)
18141 {
18142 if (atstart)
18143 {
18144 atstart = FALSE;
18145 /* Call msg_start() after eval1(), evaluating the expression
18146 * may cause a message to appear. */
18147 if (eap->cmdidx == CMD_echo)
18148 msg_start();
18149 }
18150 else if (eap->cmdidx == CMD_echo)
18151 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018152 p = echo_string(&rettv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018153 if (p != NULL)
18154 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018155 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018156 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018157 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018158 if (*p != TAB && needclr)
18159 {
18160 /* remove any text still there from the command */
18161 msg_clr_eos();
18162 needclr = FALSE;
18163 }
18164 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018165 }
18166 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018167 {
18168#ifdef FEAT_MBYTE
18169 if (has_mbyte)
18170 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018171 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018172
18173 (void)msg_outtrans_len_attr(p, i, echo_attr);
18174 p += i - 1;
18175 }
18176 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000018177#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018178 (void)msg_outtrans_len_attr(p, 1, echo_attr);
18179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018180 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018181 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018182 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018183 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018184 arg = skipwhite(arg);
18185 }
18186 eap->nextcmd = check_nextcmd(arg);
18187
18188 if (eap->skip)
18189 --emsg_skip;
18190 else
18191 {
18192 /* remove text that may still be there from the command */
18193 if (needclr)
18194 msg_clr_eos();
18195 if (eap->cmdidx == CMD_echo)
18196 msg_end();
18197 }
18198}
18199
18200/*
18201 * ":echohl {name}".
18202 */
18203 void
18204ex_echohl(eap)
18205 exarg_T *eap;
18206{
18207 int id;
18208
18209 id = syn_name2id(eap->arg);
18210 if (id == 0)
18211 echo_attr = 0;
18212 else
18213 echo_attr = syn_id2attr(id);
18214}
18215
18216/*
18217 * ":execute expr1 ..." execute the result of an expression.
18218 * ":echomsg expr1 ..." Print a message
18219 * ":echoerr expr1 ..." Print an error
18220 * Each gets spaces around each argument and a newline at the end for
18221 * echo commands
18222 */
18223 void
18224ex_execute(eap)
18225 exarg_T *eap;
18226{
18227 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018228 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018229 int ret = OK;
18230 char_u *p;
18231 garray_T ga;
18232 int len;
18233 int save_did_emsg;
18234
18235 ga_init2(&ga, 1, 80);
18236
18237 if (eap->skip)
18238 ++emsg_skip;
18239 while (*arg != NUL && *arg != '|' && *arg != '\n')
18240 {
18241 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018242 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018243 {
18244 /*
18245 * Report the invalid expression unless the expression evaluation
18246 * has been cancelled due to an aborting error, an interrupt, or an
18247 * exception.
18248 */
18249 if (!aborting())
18250 EMSG2(_(e_invexpr2), p);
18251 ret = FAIL;
18252 break;
18253 }
18254
18255 if (!eap->skip)
18256 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018257 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018258 len = (int)STRLEN(p);
18259 if (ga_grow(&ga, len + 2) == FAIL)
18260 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018261 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018262 ret = FAIL;
18263 break;
18264 }
18265 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018266 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018267 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018268 ga.ga_len += len;
18269 }
18270
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018271 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018272 arg = skipwhite(arg);
18273 }
18274
18275 if (ret != FAIL && ga.ga_data != NULL)
18276 {
18277 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000018278 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018279 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000018280 out_flush();
18281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018282 else if (eap->cmdidx == CMD_echoerr)
18283 {
18284 /* We don't want to abort following commands, restore did_emsg. */
18285 save_did_emsg = did_emsg;
18286 EMSG((char_u *)ga.ga_data);
18287 if (!force_abort)
18288 did_emsg = save_did_emsg;
18289 }
18290 else if (eap->cmdidx == CMD_execute)
18291 do_cmdline((char_u *)ga.ga_data,
18292 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
18293 }
18294
18295 ga_clear(&ga);
18296
18297 if (eap->skip)
18298 --emsg_skip;
18299
18300 eap->nextcmd = check_nextcmd(arg);
18301}
18302
18303/*
18304 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
18305 * "arg" points to the "&" or '+' when called, to "option" when returning.
18306 * Returns NULL when no option name found. Otherwise pointer to the char
18307 * after the option name.
18308 */
18309 static char_u *
18310find_option_end(arg, opt_flags)
18311 char_u **arg;
18312 int *opt_flags;
18313{
18314 char_u *p = *arg;
18315
18316 ++p;
18317 if (*p == 'g' && p[1] == ':')
18318 {
18319 *opt_flags = OPT_GLOBAL;
18320 p += 2;
18321 }
18322 else if (*p == 'l' && p[1] == ':')
18323 {
18324 *opt_flags = OPT_LOCAL;
18325 p += 2;
18326 }
18327 else
18328 *opt_flags = 0;
18329
18330 if (!ASCII_ISALPHA(*p))
18331 return NULL;
18332 *arg = p;
18333
18334 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
18335 p += 4; /* termcap option */
18336 else
18337 while (ASCII_ISALPHA(*p))
18338 ++p;
18339 return p;
18340}
18341
18342/*
18343 * ":function"
18344 */
18345 void
18346ex_function(eap)
18347 exarg_T *eap;
18348{
18349 char_u *theline;
18350 int j;
18351 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018352 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018353 char_u *name = NULL;
18354 char_u *p;
18355 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018356 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018357 garray_T newargs;
18358 garray_T newlines;
18359 int varargs = FALSE;
18360 int mustend = FALSE;
18361 int flags = 0;
18362 ufunc_T *fp;
18363 int indent;
18364 int nesting;
18365 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000018366 dictitem_T *v;
18367 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018368 static int func_nr = 0; /* number for nameless function */
18369 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018370 hashtab_T *ht;
18371 int todo;
18372 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018373 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018374
18375 /*
18376 * ":function" without argument: list functions.
18377 */
18378 if (ends_excmd(*eap->arg))
18379 {
18380 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018381 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018382 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000018383 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018384 {
18385 if (!HASHITEM_EMPTY(hi))
18386 {
18387 --todo;
18388 fp = HI2UF(hi);
18389 if (!isdigit(*fp->uf_name))
18390 list_func_head(fp, FALSE);
18391 }
18392 }
18393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018394 eap->nextcmd = check_nextcmd(eap->arg);
18395 return;
18396 }
18397
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018398 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018399 * ":function /pat": list functions matching pattern.
18400 */
18401 if (*eap->arg == '/')
18402 {
18403 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
18404 if (!eap->skip)
18405 {
18406 regmatch_T regmatch;
18407
18408 c = *p;
18409 *p = NUL;
18410 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
18411 *p = c;
18412 if (regmatch.regprog != NULL)
18413 {
18414 regmatch.rm_ic = p_ic;
18415
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018416 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018417 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
18418 {
18419 if (!HASHITEM_EMPTY(hi))
18420 {
18421 --todo;
18422 fp = HI2UF(hi);
18423 if (!isdigit(*fp->uf_name)
18424 && vim_regexec(&regmatch, fp->uf_name, 0))
18425 list_func_head(fp, FALSE);
18426 }
18427 }
18428 }
18429 }
18430 if (*p == '/')
18431 ++p;
18432 eap->nextcmd = check_nextcmd(p);
18433 return;
18434 }
18435
18436 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018437 * Get the function name. There are these situations:
18438 * func normal function name
18439 * "name" == func, "fudi.fd_dict" == NULL
18440 * dict.func new dictionary entry
18441 * "name" == NULL, "fudi.fd_dict" set,
18442 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
18443 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018444 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018445 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18446 * dict.func existing dict entry that's not a Funcref
18447 * "name" == NULL, "fudi.fd_dict" set,
18448 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18449 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018450 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018451 name = trans_function_name(&p, eap->skip, 0, &fudi);
18452 paren = (vim_strchr(p, '(') != NULL);
18453 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018454 {
18455 /*
18456 * Return on an invalid expression in braces, unless the expression
18457 * evaluation has been cancelled due to an aborting error, an
18458 * interrupt, or an exception.
18459 */
18460 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018461 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018462 if (!eap->skip && fudi.fd_newkey != NULL)
18463 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018464 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018465 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018466 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018467 else
18468 eap->skip = TRUE;
18469 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000018470
Bram Moolenaar071d4272004-06-13 20:20:40 +000018471 /* An error in a function call during evaluation of an expression in magic
18472 * braces should not cause the function not to be defined. */
18473 saved_did_emsg = did_emsg;
18474 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018475
18476 /*
18477 * ":function func" with only function name: list function.
18478 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018479 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018480 {
18481 if (!ends_excmd(*skipwhite(p)))
18482 {
18483 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018484 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018485 }
18486 eap->nextcmd = check_nextcmd(p);
18487 if (eap->nextcmd != NULL)
18488 *p = NUL;
18489 if (!eap->skip && !got_int)
18490 {
18491 fp = find_func(name);
18492 if (fp != NULL)
18493 {
18494 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018495 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018496 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018497 if (FUNCLINE(fp, j) == NULL)
18498 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018499 msg_putchar('\n');
18500 msg_outnum((long)(j + 1));
18501 if (j < 9)
18502 msg_putchar(' ');
18503 if (j < 99)
18504 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018505 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018506 out_flush(); /* show a line at a time */
18507 ui_breakcheck();
18508 }
18509 if (!got_int)
18510 {
18511 msg_putchar('\n');
18512 msg_puts((char_u *)" endfunction");
18513 }
18514 }
18515 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018516 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018517 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018518 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018519 }
18520
18521 /*
18522 * ":function name(arg1, arg2)" Define function.
18523 */
18524 p = skipwhite(p);
18525 if (*p != '(')
18526 {
18527 if (!eap->skip)
18528 {
18529 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018530 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018531 }
18532 /* attempt to continue by skipping some text */
18533 if (vim_strchr(p, '(') != NULL)
18534 p = vim_strchr(p, '(');
18535 }
18536 p = skipwhite(p + 1);
18537
18538 ga_init2(&newargs, (int)sizeof(char_u *), 3);
18539 ga_init2(&newlines, (int)sizeof(char_u *), 3);
18540
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018541 if (!eap->skip)
18542 {
18543 /* Check the name of the function. */
18544 if (name != NULL)
18545 arg = name;
18546 else
18547 arg = fudi.fd_newkey;
18548 if (arg != NULL)
18549 {
18550 if (*arg == K_SPECIAL)
18551 j = 3;
18552 else
18553 j = 0;
18554 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
18555 : eval_isnamec(arg[j])))
18556 ++j;
18557 if (arg[j] != NUL)
18558 emsg_funcname(_(e_invarg2), arg);
18559 }
18560 }
18561
Bram Moolenaar071d4272004-06-13 20:20:40 +000018562 /*
18563 * Isolate the arguments: "arg1, arg2, ...)"
18564 */
18565 while (*p != ')')
18566 {
18567 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
18568 {
18569 varargs = TRUE;
18570 p += 3;
18571 mustend = TRUE;
18572 }
18573 else
18574 {
18575 arg = p;
18576 while (ASCII_ISALNUM(*p) || *p == '_')
18577 ++p;
18578 if (arg == p || isdigit(*arg)
18579 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
18580 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
18581 {
18582 if (!eap->skip)
18583 EMSG2(_("E125: Illegal argument: %s"), arg);
18584 break;
18585 }
18586 if (ga_grow(&newargs, 1) == FAIL)
18587 goto erret;
18588 c = *p;
18589 *p = NUL;
18590 arg = vim_strsave(arg);
18591 if (arg == NULL)
18592 goto erret;
18593 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
18594 *p = c;
18595 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018596 if (*p == ',')
18597 ++p;
18598 else
18599 mustend = TRUE;
18600 }
18601 p = skipwhite(p);
18602 if (mustend && *p != ')')
18603 {
18604 if (!eap->skip)
18605 EMSG2(_(e_invarg2), eap->arg);
18606 break;
18607 }
18608 }
18609 ++p; /* skip the ')' */
18610
Bram Moolenaare9a41262005-01-15 22:18:47 +000018611 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018612 for (;;)
18613 {
18614 p = skipwhite(p);
18615 if (STRNCMP(p, "range", 5) == 0)
18616 {
18617 flags |= FC_RANGE;
18618 p += 5;
18619 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000018620 else if (STRNCMP(p, "dict", 4) == 0)
18621 {
18622 flags |= FC_DICT;
18623 p += 4;
18624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018625 else if (STRNCMP(p, "abort", 5) == 0)
18626 {
18627 flags |= FC_ABORT;
18628 p += 5;
18629 }
18630 else
18631 break;
18632 }
18633
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018634 /* When there is a line break use what follows for the function body.
18635 * Makes 'exe "func Test()\n...\nendfunc"' work. */
18636 if (*p == '\n')
18637 line_arg = p + 1;
18638 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018639 EMSG(_(e_trailing));
18640
18641 /*
18642 * Read the body of the function, until ":endfunction" is found.
18643 */
18644 if (KeyTyped)
18645 {
18646 /* Check if the function already exists, don't let the user type the
18647 * whole function before telling him it doesn't work! For a script we
18648 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018649 if (!eap->skip && !eap->forceit)
18650 {
18651 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
18652 EMSG(_(e_funcdict));
18653 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018654 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018656
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018657 if (!eap->skip && did_emsg)
18658 goto erret;
18659
Bram Moolenaar071d4272004-06-13 20:20:40 +000018660 msg_putchar('\n'); /* don't overwrite the function name */
18661 cmdline_row = msg_row;
18662 }
18663
18664 indent = 2;
18665 nesting = 0;
18666 for (;;)
18667 {
18668 msg_scroll = TRUE;
18669 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018670 sourcing_lnum_off = sourcing_lnum;
18671
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018672 if (line_arg != NULL)
18673 {
18674 /* Use eap->arg, split up in parts by line breaks. */
18675 theline = line_arg;
18676 p = vim_strchr(theline, '\n');
18677 if (p == NULL)
18678 line_arg += STRLEN(line_arg);
18679 else
18680 {
18681 *p = NUL;
18682 line_arg = p + 1;
18683 }
18684 }
18685 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018686 theline = getcmdline(':', 0L, indent);
18687 else
18688 theline = eap->getline(':', eap->cookie, indent);
18689 if (KeyTyped)
18690 lines_left = Rows - 1;
18691 if (theline == NULL)
18692 {
18693 EMSG(_("E126: Missing :endfunction"));
18694 goto erret;
18695 }
18696
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018697 /* Detect line continuation: sourcing_lnum increased more than one. */
18698 if (sourcing_lnum > sourcing_lnum_off + 1)
18699 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
18700 else
18701 sourcing_lnum_off = 0;
18702
Bram Moolenaar071d4272004-06-13 20:20:40 +000018703 if (skip_until != NULL)
18704 {
18705 /* between ":append" and "." and between ":python <<EOF" and "EOF"
18706 * don't check for ":endfunc". */
18707 if (STRCMP(theline, skip_until) == 0)
18708 {
18709 vim_free(skip_until);
18710 skip_until = NULL;
18711 }
18712 }
18713 else
18714 {
18715 /* skip ':' and blanks*/
18716 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
18717 ;
18718
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018719 /* Check for "endfunction". */
18720 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018721 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018722 if (line_arg == NULL)
18723 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018724 break;
18725 }
18726
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018727 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000018728 * at "end". */
18729 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
18730 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018731 else if (STRNCMP(p, "if", 2) == 0
18732 || STRNCMP(p, "wh", 2) == 0
18733 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000018734 || STRNCMP(p, "try", 3) == 0)
18735 indent += 2;
18736
18737 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018738 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018739 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018740 if (*p == '!')
18741 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018742 p += eval_fname_script(p);
18743 if (ASCII_ISALPHA(*p))
18744 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018745 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018746 if (*skipwhite(p) == '(')
18747 {
18748 ++nesting;
18749 indent += 2;
18750 }
18751 }
18752 }
18753
18754 /* Check for ":append" or ":insert". */
18755 p = skip_range(p, NULL);
18756 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
18757 || (p[0] == 'i'
18758 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
18759 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
18760 skip_until = vim_strsave((char_u *)".");
18761
18762 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
18763 arg = skipwhite(skiptowhite(p));
18764 if (arg[0] == '<' && arg[1] =='<'
18765 && ((p[0] == 'p' && p[1] == 'y'
18766 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
18767 || (p[0] == 'p' && p[1] == 'e'
18768 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
18769 || (p[0] == 't' && p[1] == 'c'
18770 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
18771 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
18772 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000018773 || (p[0] == 'm' && p[1] == 'z'
18774 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018775 ))
18776 {
18777 /* ":python <<" continues until a dot, like ":append" */
18778 p = skipwhite(arg + 2);
18779 if (*p == NUL)
18780 skip_until = vim_strsave((char_u *)".");
18781 else
18782 skip_until = vim_strsave(p);
18783 }
18784 }
18785
18786 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018787 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018788 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018789 if (line_arg == NULL)
18790 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018791 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018792 }
18793
18794 /* Copy the line to newly allocated memory. get_one_sourceline()
18795 * allocates 250 bytes per line, this saves 80% on average. The cost
18796 * is an extra alloc/free. */
18797 p = vim_strsave(theline);
18798 if (p != NULL)
18799 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018800 if (line_arg == NULL)
18801 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018802 theline = p;
18803 }
18804
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018805 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
18806
18807 /* Add NULL lines for continuation lines, so that the line count is
18808 * equal to the index in the growarray. */
18809 while (sourcing_lnum_off-- > 0)
18810 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018811
18812 /* Check for end of eap->arg. */
18813 if (line_arg != NULL && *line_arg == NUL)
18814 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018815 }
18816
18817 /* Don't define the function when skipping commands or when an error was
18818 * detected. */
18819 if (eap->skip || did_emsg)
18820 goto erret;
18821
18822 /*
18823 * If there are no errors, add the function
18824 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018825 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018826 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018827 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000018828 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018829 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018830 emsg_funcname("E707: Function name conflicts with variable: %s",
18831 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018832 goto erret;
18833 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018834
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018835 fp = find_func(name);
18836 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018837 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018838 if (!eap->forceit)
18839 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018840 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018841 goto erret;
18842 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018843 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018844 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018845 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018846 name);
18847 goto erret;
18848 }
18849 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018850 ga_clear_strings(&(fp->uf_args));
18851 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018852 vim_free(name);
18853 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018855 }
18856 else
18857 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018858 char numbuf[20];
18859
18860 fp = NULL;
18861 if (fudi.fd_newkey == NULL && !eap->forceit)
18862 {
18863 EMSG(_(e_funcdict));
18864 goto erret;
18865 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000018866 if (fudi.fd_di == NULL)
18867 {
18868 /* Can't add a function to a locked dictionary */
18869 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
18870 goto erret;
18871 }
18872 /* Can't change an existing function if it is locked */
18873 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
18874 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018875
18876 /* Give the function a sequential number. Can only be used with a
18877 * Funcref! */
18878 vim_free(name);
18879 sprintf(numbuf, "%d", ++func_nr);
18880 name = vim_strsave((char_u *)numbuf);
18881 if (name == NULL)
18882 goto erret;
18883 }
18884
18885 if (fp == NULL)
18886 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018887 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018888 {
18889 int slen, plen;
18890 char_u *scriptname;
18891
18892 /* Check that the autoload name matches the script name. */
18893 j = FAIL;
18894 if (sourcing_name != NULL)
18895 {
18896 scriptname = autoload_name(name);
18897 if (scriptname != NULL)
18898 {
18899 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018900 plen = (int)STRLEN(p);
18901 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018902 if (slen > plen && fnamecmp(p,
18903 sourcing_name + slen - plen) == 0)
18904 j = OK;
18905 vim_free(scriptname);
18906 }
18907 }
18908 if (j == FAIL)
18909 {
18910 EMSG2(_("E746: Function name does not match script file name: %s"), name);
18911 goto erret;
18912 }
18913 }
18914
18915 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018916 if (fp == NULL)
18917 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018918
18919 if (fudi.fd_dict != NULL)
18920 {
18921 if (fudi.fd_di == NULL)
18922 {
18923 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018924 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018925 if (fudi.fd_di == NULL)
18926 {
18927 vim_free(fp);
18928 goto erret;
18929 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018930 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
18931 {
18932 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000018933 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018934 goto erret;
18935 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018936 }
18937 else
18938 /* overwrite existing dict entry */
18939 clear_tv(&fudi.fd_di->di_tv);
18940 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018941 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018942 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018943 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018944
18945 /* behave like "dict" was used */
18946 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018947 }
18948
Bram Moolenaar071d4272004-06-13 20:20:40 +000018949 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018950 STRCPY(fp->uf_name, name);
18951 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018952 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018953 fp->uf_args = newargs;
18954 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018955#ifdef FEAT_PROFILE
18956 fp->uf_tml_count = NULL;
18957 fp->uf_tml_total = NULL;
18958 fp->uf_tml_self = NULL;
18959 fp->uf_profiling = FALSE;
18960 if (prof_def_func())
18961 func_do_profile(fp);
18962#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018963 fp->uf_varargs = varargs;
18964 fp->uf_flags = flags;
18965 fp->uf_calls = 0;
18966 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018967 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018968
18969erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000018970 ga_clear_strings(&newargs);
18971 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018972ret_free:
18973 vim_free(skip_until);
18974 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018975 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018976 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018977}
18978
18979/*
18980 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000018981 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018982 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018983 * flags:
18984 * TFN_INT: internal function name OK
18985 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000018986 * Advances "pp" to just after the function name (if no error).
18987 */
18988 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018989trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018990 char_u **pp;
18991 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018992 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000018993 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018994{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018995 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018996 char_u *start;
18997 char_u *end;
18998 int lead;
18999 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019000 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000019001 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019002
19003 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019004 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019005 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000019006
19007 /* Check for hard coded <SNR>: already translated function ID (from a user
19008 * command). */
19009 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
19010 && (*pp)[2] == (int)KE_SNR)
19011 {
19012 *pp += 3;
19013 len = get_id_len(pp) + 3;
19014 return vim_strnsave(start, len);
19015 }
19016
19017 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
19018 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019019 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000019020 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019021 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019022
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019023 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
19024 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019025 if (end == start)
19026 {
19027 if (!skip)
19028 EMSG(_("E129: Function name required"));
19029 goto theend;
19030 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019031 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019032 {
19033 /*
19034 * Report an invalid expression in braces, unless the expression
19035 * evaluation has been cancelled due to an aborting error, an
19036 * interrupt, or an exception.
19037 */
19038 if (!aborting())
19039 {
19040 if (end != NULL)
19041 EMSG2(_(e_invarg2), start);
19042 }
19043 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019044 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019045 goto theend;
19046 }
19047
19048 if (lv.ll_tv != NULL)
19049 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019050 if (fdp != NULL)
19051 {
19052 fdp->fd_dict = lv.ll_dict;
19053 fdp->fd_newkey = lv.ll_newkey;
19054 lv.ll_newkey = NULL;
19055 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019056 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019057 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
19058 {
19059 name = vim_strsave(lv.ll_tv->vval.v_string);
19060 *pp = end;
19061 }
19062 else
19063 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019064 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
19065 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019066 EMSG(_(e_funcref));
19067 else
19068 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019069 name = NULL;
19070 }
19071 goto theend;
19072 }
19073
19074 if (lv.ll_name == NULL)
19075 {
19076 /* Error found, but continue after the function name. */
19077 *pp = end;
19078 goto theend;
19079 }
19080
19081 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000019082 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019083 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000019084 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
19085 && STRNCMP(lv.ll_name, "s:", 2) == 0)
19086 {
19087 /* When there was "s:" already or the name expanded to get a
19088 * leading "s:" then remove it. */
19089 lv.ll_name += 2;
19090 len -= 2;
19091 lead = 2;
19092 }
19093 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019094 else
Bram Moolenaara7043832005-01-21 11:56:39 +000019095 {
19096 if (lead == 2) /* skip over "s:" */
19097 lv.ll_name += 2;
19098 len = (int)(end - lv.ll_name);
19099 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019100
19101 /*
19102 * Copy the function name to allocated memory.
19103 * Accept <SID>name() inside a script, translate into <SNR>123_name().
19104 * Accept <SNR>123_name() outside a script.
19105 */
19106 if (skip)
19107 lead = 0; /* do nothing */
19108 else if (lead > 0)
19109 {
19110 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000019111 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
19112 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019113 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000019114 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019115 if (current_SID <= 0)
19116 {
19117 EMSG(_(e_usingsid));
19118 goto theend;
19119 }
19120 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
19121 lead += (int)STRLEN(sid_buf);
19122 }
19123 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019124 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019125 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019126 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019127 goto theend;
19128 }
19129 name = alloc((unsigned)(len + lead + 1));
19130 if (name != NULL)
19131 {
19132 if (lead > 0)
19133 {
19134 name[0] = K_SPECIAL;
19135 name[1] = KS_EXTRA;
19136 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000019137 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019138 STRCPY(name + 3, sid_buf);
19139 }
19140 mch_memmove(name + lead, lv.ll_name, (size_t)len);
19141 name[len + lead] = NUL;
19142 }
19143 *pp = end;
19144
19145theend:
19146 clear_lval(&lv);
19147 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019148}
19149
19150/*
19151 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
19152 * Return 2 if "p" starts with "s:".
19153 * Return 0 otherwise.
19154 */
19155 static int
19156eval_fname_script(p)
19157 char_u *p;
19158{
19159 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
19160 || STRNICMP(p + 1, "SNR>", 4) == 0))
19161 return 5;
19162 if (p[0] == 's' && p[1] == ':')
19163 return 2;
19164 return 0;
19165}
19166
19167/*
19168 * Return TRUE if "p" starts with "<SID>" or "s:".
19169 * Only works if eval_fname_script() returned non-zero for "p"!
19170 */
19171 static int
19172eval_fname_sid(p)
19173 char_u *p;
19174{
19175 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
19176}
19177
19178/*
19179 * List the head of the function: "name(arg1, arg2)".
19180 */
19181 static void
19182list_func_head(fp, indent)
19183 ufunc_T *fp;
19184 int indent;
19185{
19186 int j;
19187
19188 msg_start();
19189 if (indent)
19190 MSG_PUTS(" ");
19191 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019192 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019193 {
19194 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019195 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019196 }
19197 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019198 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019199 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019200 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019201 {
19202 if (j)
19203 MSG_PUTS(", ");
19204 msg_puts(FUNCARG(fp, j));
19205 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019206 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019207 {
19208 if (j)
19209 MSG_PUTS(", ");
19210 MSG_PUTS("...");
19211 }
19212 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019213 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000019214 if (p_verbose > 0)
19215 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019216}
19217
19218/*
19219 * Find a function by name, return pointer to it in ufuncs.
19220 * Return NULL for unknown function.
19221 */
19222 static ufunc_T *
19223find_func(name)
19224 char_u *name;
19225{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019226 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019227
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019228 hi = hash_find(&func_hashtab, name);
19229 if (!HASHITEM_EMPTY(hi))
19230 return HI2UF(hi);
19231 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019232}
19233
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019234#if defined(EXITFREE) || defined(PROTO)
19235 void
19236free_all_functions()
19237{
19238 hashitem_T *hi;
19239
19240 /* Need to start all over every time, because func_free() may change the
19241 * hash table. */
19242 while (func_hashtab.ht_used > 0)
19243 for (hi = func_hashtab.ht_array; ; ++hi)
19244 if (!HASHITEM_EMPTY(hi))
19245 {
19246 func_free(HI2UF(hi));
19247 break;
19248 }
19249}
19250#endif
19251
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019252/*
19253 * Return TRUE if a function "name" exists.
19254 */
19255 static int
19256function_exists(name)
19257 char_u *name;
19258{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000019259 char_u *nm = name;
19260 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019261 int n = FALSE;
19262
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000019263 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000019264 nm = skipwhite(nm);
19265
19266 /* Only accept "funcname", "funcname ", "funcname (..." and
19267 * "funcname(...", not "funcname!...". */
19268 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019269 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019270 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019271 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019272 else
19273 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019274 }
Bram Moolenaar79783442006-05-05 21:18:03 +000019275 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019276 return n;
19277}
19278
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019279/*
19280 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019281 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019282 */
19283 static int
19284builtin_function(name)
19285 char_u *name;
19286{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019287 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
19288 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019289}
19290
Bram Moolenaar05159a02005-02-26 23:04:13 +000019291#if defined(FEAT_PROFILE) || defined(PROTO)
19292/*
19293 * Start profiling function "fp".
19294 */
19295 static void
19296func_do_profile(fp)
19297 ufunc_T *fp;
19298{
19299 fp->uf_tm_count = 0;
19300 profile_zero(&fp->uf_tm_self);
19301 profile_zero(&fp->uf_tm_total);
19302 if (fp->uf_tml_count == NULL)
19303 fp->uf_tml_count = (int *)alloc_clear((unsigned)
19304 (sizeof(int) * fp->uf_lines.ga_len));
19305 if (fp->uf_tml_total == NULL)
19306 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
19307 (sizeof(proftime_T) * fp->uf_lines.ga_len));
19308 if (fp->uf_tml_self == NULL)
19309 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
19310 (sizeof(proftime_T) * fp->uf_lines.ga_len));
19311 fp->uf_tml_idx = -1;
19312 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
19313 || fp->uf_tml_self == NULL)
19314 return; /* out of memory */
19315
19316 fp->uf_profiling = TRUE;
19317}
19318
19319/*
19320 * Dump the profiling results for all functions in file "fd".
19321 */
19322 void
19323func_dump_profile(fd)
19324 FILE *fd;
19325{
19326 hashitem_T *hi;
19327 int todo;
19328 ufunc_T *fp;
19329 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000019330 ufunc_T **sorttab;
19331 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019332
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019333 todo = (int)func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000019334 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
19335
Bram Moolenaar05159a02005-02-26 23:04:13 +000019336 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
19337 {
19338 if (!HASHITEM_EMPTY(hi))
19339 {
19340 --todo;
19341 fp = HI2UF(hi);
19342 if (fp->uf_profiling)
19343 {
Bram Moolenaar73830342005-02-28 22:48:19 +000019344 if (sorttab != NULL)
19345 sorttab[st_len++] = fp;
19346
Bram Moolenaar05159a02005-02-26 23:04:13 +000019347 if (fp->uf_name[0] == K_SPECIAL)
19348 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
19349 else
19350 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
19351 if (fp->uf_tm_count == 1)
19352 fprintf(fd, "Called 1 time\n");
19353 else
19354 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
19355 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
19356 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
19357 fprintf(fd, "\n");
19358 fprintf(fd, "count total (s) self (s)\n");
19359
19360 for (i = 0; i < fp->uf_lines.ga_len; ++i)
19361 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019362 if (FUNCLINE(fp, i) == NULL)
19363 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000019364 prof_func_line(fd, fp->uf_tml_count[i],
19365 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019366 fprintf(fd, "%s\n", FUNCLINE(fp, i));
19367 }
19368 fprintf(fd, "\n");
19369 }
19370 }
19371 }
Bram Moolenaar73830342005-02-28 22:48:19 +000019372
19373 if (sorttab != NULL && st_len > 0)
19374 {
19375 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
19376 prof_total_cmp);
19377 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
19378 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
19379 prof_self_cmp);
19380 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
19381 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019382}
Bram Moolenaar73830342005-02-28 22:48:19 +000019383
19384 static void
19385prof_sort_list(fd, sorttab, st_len, title, prefer_self)
19386 FILE *fd;
19387 ufunc_T **sorttab;
19388 int st_len;
19389 char *title;
19390 int prefer_self; /* when equal print only self time */
19391{
19392 int i;
19393 ufunc_T *fp;
19394
19395 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
19396 fprintf(fd, "count total (s) self (s) function\n");
19397 for (i = 0; i < 20 && i < st_len; ++i)
19398 {
19399 fp = sorttab[i];
19400 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
19401 prefer_self);
19402 if (fp->uf_name[0] == K_SPECIAL)
19403 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
19404 else
19405 fprintf(fd, " %s()\n", fp->uf_name);
19406 }
19407 fprintf(fd, "\n");
19408}
19409
19410/*
19411 * Print the count and times for one function or function line.
19412 */
19413 static void
19414prof_func_line(fd, count, total, self, prefer_self)
19415 FILE *fd;
19416 int count;
19417 proftime_T *total;
19418 proftime_T *self;
19419 int prefer_self; /* when equal print only self time */
19420{
19421 if (count > 0)
19422 {
19423 fprintf(fd, "%5d ", count);
19424 if (prefer_self && profile_equal(total, self))
19425 fprintf(fd, " ");
19426 else
19427 fprintf(fd, "%s ", profile_msg(total));
19428 if (!prefer_self && profile_equal(total, self))
19429 fprintf(fd, " ");
19430 else
19431 fprintf(fd, "%s ", profile_msg(self));
19432 }
19433 else
19434 fprintf(fd, " ");
19435}
19436
19437/*
19438 * Compare function for total time sorting.
19439 */
19440 static int
19441#ifdef __BORLANDC__
19442_RTLENTRYF
19443#endif
19444prof_total_cmp(s1, s2)
19445 const void *s1;
19446 const void *s2;
19447{
19448 ufunc_T *p1, *p2;
19449
19450 p1 = *(ufunc_T **)s1;
19451 p2 = *(ufunc_T **)s2;
19452 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
19453}
19454
19455/*
19456 * Compare function for self time sorting.
19457 */
19458 static int
19459#ifdef __BORLANDC__
19460_RTLENTRYF
19461#endif
19462prof_self_cmp(s1, s2)
19463 const void *s1;
19464 const void *s2;
19465{
19466 ufunc_T *p1, *p2;
19467
19468 p1 = *(ufunc_T **)s1;
19469 p2 = *(ufunc_T **)s2;
19470 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
19471}
19472
Bram Moolenaar05159a02005-02-26 23:04:13 +000019473#endif
19474
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019475/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019476 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019477 * Return TRUE if a package was loaded.
19478 */
19479 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019480script_autoload(name, reload)
19481 char_u *name;
19482 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019483{
19484 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019485 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019486 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019487 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019488
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019489 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019490 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019491 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019492 return FALSE;
19493
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019494 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019495
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019496 /* Find the name in the list of previously loaded package names. Skip
19497 * "autoload/", it's always the same. */
19498 for (i = 0; i < ga_loaded.ga_len; ++i)
19499 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
19500 break;
19501 if (!reload && i < ga_loaded.ga_len)
19502 ret = FALSE; /* was loaded already */
19503 else
19504 {
19505 /* Remember the name if it wasn't loaded already. */
19506 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
19507 {
19508 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
19509 tofree = NULL;
19510 }
19511
19512 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000019513 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019514 ret = TRUE;
19515 }
19516
19517 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019518 return ret;
19519}
19520
19521/*
19522 * Return the autoload script name for a function or variable name.
19523 * Returns NULL when out of memory.
19524 */
19525 static char_u *
19526autoload_name(name)
19527 char_u *name;
19528{
19529 char_u *p;
19530 char_u *scriptname;
19531
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019532 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019533 scriptname = alloc((unsigned)(STRLEN(name) + 14));
19534 if (scriptname == NULL)
19535 return FALSE;
19536 STRCPY(scriptname, "autoload/");
19537 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019538 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019539 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019540 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019541 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019542 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019543}
19544
Bram Moolenaar071d4272004-06-13 20:20:40 +000019545#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
19546
19547/*
19548 * Function given to ExpandGeneric() to obtain the list of user defined
19549 * function names.
19550 */
19551 char_u *
19552get_user_func_name(xp, idx)
19553 expand_T *xp;
19554 int idx;
19555{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019556 static long_u done;
19557 static hashitem_T *hi;
19558 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019559
19560 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019561 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019562 done = 0;
19563 hi = func_hashtab.ht_array;
19564 }
19565 if (done < func_hashtab.ht_used)
19566 {
19567 if (done++ > 0)
19568 ++hi;
19569 while (HASHITEM_EMPTY(hi))
19570 ++hi;
19571 fp = HI2UF(hi);
19572
19573 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
19574 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019575
19576 cat_func_name(IObuff, fp);
19577 if (xp->xp_context != EXPAND_USER_FUNC)
19578 {
19579 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019580 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019581 STRCAT(IObuff, ")");
19582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019583 return IObuff;
19584 }
19585 return NULL;
19586}
19587
19588#endif /* FEAT_CMDL_COMPL */
19589
19590/*
19591 * Copy the function name of "fp" to buffer "buf".
19592 * "buf" must be able to hold the function name plus three bytes.
19593 * Takes care of script-local function names.
19594 */
19595 static void
19596cat_func_name(buf, fp)
19597 char_u *buf;
19598 ufunc_T *fp;
19599{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019600 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019601 {
19602 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019603 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019604 }
19605 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019606 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019607}
19608
19609/*
19610 * ":delfunction {name}"
19611 */
19612 void
19613ex_delfunction(eap)
19614 exarg_T *eap;
19615{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019616 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019617 char_u *p;
19618 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019619 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019620
19621 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019622 name = trans_function_name(&p, eap->skip, 0, &fudi);
19623 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019624 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019625 {
19626 if (fudi.fd_dict != NULL && !eap->skip)
19627 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019628 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019630 if (!ends_excmd(*skipwhite(p)))
19631 {
19632 vim_free(name);
19633 EMSG(_(e_trailing));
19634 return;
19635 }
19636 eap->nextcmd = check_nextcmd(p);
19637 if (eap->nextcmd != NULL)
19638 *p = NUL;
19639
19640 if (!eap->skip)
19641 fp = find_func(name);
19642 vim_free(name);
19643
19644 if (!eap->skip)
19645 {
19646 if (fp == NULL)
19647 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019648 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019649 return;
19650 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019651 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019652 {
19653 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
19654 return;
19655 }
19656
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019657 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019658 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019659 /* Delete the dict item that refers to the function, it will
19660 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019661 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019662 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019663 else
19664 func_free(fp);
19665 }
19666}
19667
19668/*
19669 * Free a function and remove it from the list of functions.
19670 */
19671 static void
19672func_free(fp)
19673 ufunc_T *fp;
19674{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019675 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019676
19677 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019678 ga_clear_strings(&(fp->uf_args));
19679 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000019680#ifdef FEAT_PROFILE
19681 vim_free(fp->uf_tml_count);
19682 vim_free(fp->uf_tml_total);
19683 vim_free(fp->uf_tml_self);
19684#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019685
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019686 /* remove the function from the function hashtable */
19687 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
19688 if (HASHITEM_EMPTY(hi))
19689 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019690 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019691 hash_remove(&func_hashtab, hi);
19692
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019693 vim_free(fp);
19694}
19695
19696/*
19697 * Unreference a Function: decrement the reference count and free it when it
19698 * becomes zero. Only for numbered functions.
19699 */
19700 static void
19701func_unref(name)
19702 char_u *name;
19703{
19704 ufunc_T *fp;
19705
19706 if (name != NULL && isdigit(*name))
19707 {
19708 fp = find_func(name);
19709 if (fp == NULL)
19710 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019711 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019712 {
19713 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019714 * when "uf_calls" becomes zero. */
19715 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019716 func_free(fp);
19717 }
19718 }
19719}
19720
19721/*
19722 * Count a reference to a Function.
19723 */
19724 static void
19725func_ref(name)
19726 char_u *name;
19727{
19728 ufunc_T *fp;
19729
19730 if (name != NULL && isdigit(*name))
19731 {
19732 fp = find_func(name);
19733 if (fp == NULL)
19734 EMSG2(_(e_intern2), "func_ref()");
19735 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019736 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019737 }
19738}
19739
19740/*
19741 * Call a user function.
19742 */
19743 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000019744call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019745 ufunc_T *fp; /* pointer to function */
19746 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000019747 typval_T *argvars; /* arguments */
19748 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019749 linenr_T firstline; /* first line of range */
19750 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000019751 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019752{
Bram Moolenaar33570922005-01-25 22:26:29 +000019753 char_u *save_sourcing_name;
19754 linenr_T save_sourcing_lnum;
19755 scid_T save_current_SID;
19756 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000019757 int save_did_emsg;
19758 static int depth = 0;
19759 dictitem_T *v;
19760 int fixvar_idx = 0; /* index in fixvar[] */
19761 int i;
19762 int ai;
19763 char_u numbuf[NUMBUFLEN];
19764 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019765#ifdef FEAT_PROFILE
19766 proftime_T wait_start;
19767#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019768
19769 /* If depth of calling is getting too high, don't execute the function */
19770 if (depth >= p_mfd)
19771 {
19772 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019773 rettv->v_type = VAR_NUMBER;
19774 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019775 return;
19776 }
19777 ++depth;
19778
19779 line_breakcheck(); /* check for CTRL-C hit */
19780
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019781 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000019782 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019783 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019784 fc.rettv = rettv;
19785 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019786 fc.linenr = 0;
19787 fc.returned = FALSE;
19788 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019789 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019790 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019791 fc.dbg_tick = debug_tick;
19792
Bram Moolenaar33570922005-01-25 22:26:29 +000019793 /*
19794 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
19795 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
19796 * each argument variable and saves a lot of time.
19797 */
19798 /*
19799 * Init l: variables.
19800 */
19801 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000019802 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019803 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000019804 /* Set l:self to "selfdict". Use "name" to avoid a warning from
19805 * some compiler that checks the destination size. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019806 v = &fc.fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000019807 name = v->di_key;
19808 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000019809 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
19810 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
19811 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019812 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000019813 v->di_tv.vval.v_dict = selfdict;
19814 ++selfdict->dv_refcount;
19815 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000019816
Bram Moolenaar33570922005-01-25 22:26:29 +000019817 /*
19818 * Init a: variables.
19819 * Set a:0 to "argcount".
19820 * Set a:000 to a list with room for the "..." arguments.
19821 */
19822 init_var_dict(&fc.l_avars, &fc.l_avars_var);
19823 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019824 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000019825 v = &fc.fixvar[fixvar_idx++].var;
19826 STRCPY(v->di_key, "000");
19827 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19828 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
19829 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019830 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019831 v->di_tv.vval.v_list = &fc.l_varlist;
19832 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
19833 fc.l_varlist.lv_refcount = 99999;
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000019834 fc.l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019835
19836 /*
19837 * Set a:firstline to "firstline" and a:lastline to "lastline".
19838 * Set a:name to named arguments.
19839 * Set a:N to the "..." arguments.
19840 */
19841 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
19842 (varnumber_T)firstline);
19843 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
19844 (varnumber_T)lastline);
19845 for (i = 0; i < argcount; ++i)
19846 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019847 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000019848 if (ai < 0)
19849 /* named argument a:name */
19850 name = FUNCARG(fp, i);
19851 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019852 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019853 /* "..." argument a:1, a:2, etc. */
19854 sprintf((char *)numbuf, "%d", ai + 1);
19855 name = numbuf;
19856 }
19857 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
19858 {
19859 v = &fc.fixvar[fixvar_idx++].var;
19860 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19861 }
19862 else
19863 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019864 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19865 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000019866 if (v == NULL)
19867 break;
19868 v->di_flags = DI_FLAGS_RO;
19869 }
19870 STRCPY(v->di_key, name);
19871 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
19872
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019873 /* Note: the values are copied directly to avoid alloc/free.
19874 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019875 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019876 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019877
19878 if (ai >= 0 && ai < MAX_FUNC_ARGS)
19879 {
19880 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
19881 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019882 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019883 }
19884 }
19885
Bram Moolenaar071d4272004-06-13 20:20:40 +000019886 /* Don't redraw while executing the function. */
19887 ++RedrawingDisabled;
19888 save_sourcing_name = sourcing_name;
19889 save_sourcing_lnum = sourcing_lnum;
19890 sourcing_lnum = 1;
19891 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019892 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019893 if (sourcing_name != NULL)
19894 {
19895 if (save_sourcing_name != NULL
19896 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
19897 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
19898 else
19899 STRCPY(sourcing_name, "function ");
19900 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
19901
19902 if (p_verbose >= 12)
19903 {
19904 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019905 verbose_enter_scroll();
19906
Bram Moolenaar555b2802005-05-19 21:08:39 +000019907 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019908 if (p_verbose >= 14)
19909 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019910 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000019911 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000019912 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019913
19914 msg_puts((char_u *)"(");
19915 for (i = 0; i < argcount; ++i)
19916 {
19917 if (i > 0)
19918 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019919 if (argvars[i].v_type == VAR_NUMBER)
19920 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019921 else
19922 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000019923 trunc_string(tv2string(&argvars[i], &tofree,
19924 numbuf2, 0), buf, MSG_BUF_CLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019925 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019926 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019927 }
19928 }
19929 msg_puts((char_u *)")");
19930 }
19931 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019932
19933 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019934 --no_wait_return;
19935 }
19936 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019937#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000019938 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019939 {
19940 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
19941 func_do_profile(fp);
19942 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019943 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000019944 {
19945 ++fp->uf_tm_count;
19946 profile_start(&fp->uf_tm_start);
19947 profile_zero(&fp->uf_tm_children);
19948 }
19949 script_prof_save(&wait_start);
19950 }
19951#endif
19952
Bram Moolenaar071d4272004-06-13 20:20:40 +000019953 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019954 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019955 save_did_emsg = did_emsg;
19956 did_emsg = FALSE;
19957
19958 /* call do_cmdline() to execute the lines */
19959 do_cmdline(NULL, get_func_line, (void *)&fc,
19960 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
19961
19962 --RedrawingDisabled;
19963
19964 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019965 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019966 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019967 clear_tv(rettv);
19968 rettv->v_type = VAR_NUMBER;
19969 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019970 }
19971
Bram Moolenaar05159a02005-02-26 23:04:13 +000019972#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000019973 if (do_profiling == PROF_YES && (fp->uf_profiling
19974 || (fc.caller != NULL && &fc.caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000019975 {
19976 profile_end(&fp->uf_tm_start);
19977 profile_sub_wait(&wait_start, &fp->uf_tm_start);
19978 profile_add(&fp->uf_tm_total, &fp->uf_tm_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000019979 profile_self(&fp->uf_tm_self, &fp->uf_tm_start, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019980 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019981 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019982 profile_add(&fc.caller->func->uf_tm_children, &fp->uf_tm_start);
19983 profile_add(&fc.caller->func->uf_tml_children, &fp->uf_tm_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019984 }
19985 }
19986#endif
19987
Bram Moolenaar071d4272004-06-13 20:20:40 +000019988 /* when being verbose, mention the return value */
19989 if (p_verbose >= 12)
19990 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019991 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019992 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019993
Bram Moolenaar071d4272004-06-13 20:20:40 +000019994 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000019995 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019996 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000019997 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
19998 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019999 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020000 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000020001 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000020002 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000020003 char_u *tofree;
20004
Bram Moolenaar555b2802005-05-19 21:08:39 +000020005 /* The value may be very long. Skip the middle part, so that we
20006 * have some idea how it starts and ends. smsg() would always
20007 * truncate it at the end. */
Bram Moolenaar89d40322006-08-29 15:30:07 +000020008 trunc_string(tv2string(fc.rettv, &tofree, numbuf2, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020009 buf, MSG_BUF_CLEN);
Bram Moolenaar555b2802005-05-19 21:08:39 +000020010 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000020011 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020012 }
20013 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020014
20015 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020016 --no_wait_return;
20017 }
20018
20019 vim_free(sourcing_name);
20020 sourcing_name = save_sourcing_name;
20021 sourcing_lnum = save_sourcing_lnum;
20022 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020023#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020024 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020025 script_prof_restore(&wait_start);
20026#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020027
20028 if (p_verbose >= 12 && sourcing_name != NULL)
20029 {
20030 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020031 verbose_enter_scroll();
20032
Bram Moolenaar555b2802005-05-19 21:08:39 +000020033 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020034 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020035
20036 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020037 --no_wait_return;
20038 }
20039
20040 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020041 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020042
Bram Moolenaar33570922005-01-25 22:26:29 +000020043 /* The a: variables typevals were not alloced, only free the allocated
20044 * variables. */
20045 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
20046
20047 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020048 --depth;
20049}
20050
20051/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020052 * Add a number variable "name" to dict "dp" with value "nr".
20053 */
20054 static void
20055add_nr_var(dp, v, name, nr)
20056 dict_T *dp;
20057 dictitem_T *v;
20058 char *name;
20059 varnumber_T nr;
20060{
20061 STRCPY(v->di_key, name);
20062 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20063 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
20064 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020065 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020066 v->di_tv.vval.v_number = nr;
20067}
20068
20069/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020070 * ":return [expr]"
20071 */
20072 void
20073ex_return(eap)
20074 exarg_T *eap;
20075{
20076 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020077 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020078 int returning = FALSE;
20079
20080 if (current_funccal == NULL)
20081 {
20082 EMSG(_("E133: :return not inside a function"));
20083 return;
20084 }
20085
20086 if (eap->skip)
20087 ++emsg_skip;
20088
20089 eap->nextcmd = NULL;
20090 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020091 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020092 {
20093 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020094 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020095 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020096 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020097 }
20098 /* It's safer to return also on error. */
20099 else if (!eap->skip)
20100 {
20101 /*
20102 * Return unless the expression evaluation has been cancelled due to an
20103 * aborting error, an interrupt, or an exception.
20104 */
20105 if (!aborting())
20106 returning = do_return(eap, FALSE, TRUE, NULL);
20107 }
20108
20109 /* When skipping or the return gets pending, advance to the next command
20110 * in this line (!returning). Otherwise, ignore the rest of the line.
20111 * Following lines will be ignored by get_func_line(). */
20112 if (returning)
20113 eap->nextcmd = NULL;
20114 else if (eap->nextcmd == NULL) /* no argument */
20115 eap->nextcmd = check_nextcmd(arg);
20116
20117 if (eap->skip)
20118 --emsg_skip;
20119}
20120
20121/*
20122 * Return from a function. Possibly makes the return pending. Also called
20123 * for a pending return at the ":endtry" or after returning from an extra
20124 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000020125 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020126 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020127 * FALSE when the return gets pending.
20128 */
20129 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020130do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020131 exarg_T *eap;
20132 int reanimate;
20133 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020134 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020135{
20136 int idx;
20137 struct condstack *cstack = eap->cstack;
20138
20139 if (reanimate)
20140 /* Undo the return. */
20141 current_funccal->returned = FALSE;
20142
20143 /*
20144 * Cleanup (and inactivate) conditionals, but stop when a try conditional
20145 * not in its finally clause (which then is to be executed next) is found.
20146 * In this case, make the ":return" pending for execution at the ":endtry".
20147 * Otherwise, return normally.
20148 */
20149 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
20150 if (idx >= 0)
20151 {
20152 cstack->cs_pending[idx] = CSTP_RETURN;
20153
20154 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020155 /* A pending return again gets pending. "rettv" points to an
20156 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000020157 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020158 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020159 else
20160 {
20161 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020162 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020163 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020164 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020165
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020166 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020167 {
20168 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020169 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020170 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020171 else
20172 EMSG(_(e_outofmem));
20173 }
20174 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020175 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020176
20177 if (reanimate)
20178 {
20179 /* The pending return value could be overwritten by a ":return"
20180 * without argument in a finally clause; reset the default
20181 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020182 current_funccal->rettv->v_type = VAR_NUMBER;
20183 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020184 }
20185 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020186 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020187 }
20188 else
20189 {
20190 current_funccal->returned = TRUE;
20191
20192 /* If the return is carried out now, store the return value. For
20193 * a return immediately after reanimation, the value is already
20194 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020195 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020196 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020197 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000020198 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020199 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020200 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020201 }
20202 }
20203
20204 return idx < 0;
20205}
20206
20207/*
20208 * Free the variable with a pending return value.
20209 */
20210 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020211discard_pending_return(rettv)
20212 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020213{
Bram Moolenaar33570922005-01-25 22:26:29 +000020214 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020215}
20216
20217/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020218 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000020219 * is an allocated string. Used by report_pending() for verbose messages.
20220 */
20221 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020222get_return_cmd(rettv)
20223 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020224{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020225 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020226 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020227 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020228
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020229 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000020230 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020231 if (s == NULL)
20232 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020233
20234 STRCPY(IObuff, ":return ");
20235 STRNCPY(IObuff + 8, s, IOSIZE - 8);
20236 if (STRLEN(s) + 8 >= IOSIZE)
20237 STRCPY(IObuff + IOSIZE - 4, "...");
20238 vim_free(tofree);
20239 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020240}
20241
20242/*
20243 * Get next function line.
20244 * Called by do_cmdline() to get the next line.
20245 * Returns allocated string, or NULL for end of function.
20246 */
20247/* ARGSUSED */
20248 char_u *
20249get_func_line(c, cookie, indent)
20250 int c; /* not used */
20251 void *cookie;
20252 int indent; /* not used */
20253{
Bram Moolenaar33570922005-01-25 22:26:29 +000020254 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020255 ufunc_T *fp = fcp->func;
20256 char_u *retval;
20257 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020258
20259 /* If breakpoints have been added/deleted need to check for it. */
20260 if (fcp->dbg_tick != debug_tick)
20261 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020262 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020263 sourcing_lnum);
20264 fcp->dbg_tick = debug_tick;
20265 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000020266#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020267 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020268 func_line_end(cookie);
20269#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020270
Bram Moolenaar05159a02005-02-26 23:04:13 +000020271 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020272 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
20273 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020274 retval = NULL;
20275 else
20276 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020277 /* Skip NULL lines (continuation lines). */
20278 while (fcp->linenr < gap->ga_len
20279 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
20280 ++fcp->linenr;
20281 if (fcp->linenr >= gap->ga_len)
20282 retval = NULL;
20283 else
20284 {
20285 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
20286 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020287#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020288 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020289 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020290#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020291 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020292 }
20293
20294 /* Did we encounter a breakpoint? */
20295 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
20296 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020297 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020298 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020299 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020300 sourcing_lnum);
20301 fcp->dbg_tick = debug_tick;
20302 }
20303
20304 return retval;
20305}
20306
Bram Moolenaar05159a02005-02-26 23:04:13 +000020307#if defined(FEAT_PROFILE) || defined(PROTO)
20308/*
20309 * Called when starting to read a function line.
20310 * "sourcing_lnum" must be correct!
20311 * When skipping lines it may not actually be executed, but we won't find out
20312 * until later and we need to store the time now.
20313 */
20314 void
20315func_line_start(cookie)
20316 void *cookie;
20317{
20318 funccall_T *fcp = (funccall_T *)cookie;
20319 ufunc_T *fp = fcp->func;
20320
20321 if (fp->uf_profiling && sourcing_lnum >= 1
20322 && sourcing_lnum <= fp->uf_lines.ga_len)
20323 {
20324 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020325 /* Skip continuation lines. */
20326 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
20327 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020328 fp->uf_tml_execed = FALSE;
20329 profile_start(&fp->uf_tml_start);
20330 profile_zero(&fp->uf_tml_children);
20331 profile_get_wait(&fp->uf_tml_wait);
20332 }
20333}
20334
20335/*
20336 * Called when actually executing a function line.
20337 */
20338 void
20339func_line_exec(cookie)
20340 void *cookie;
20341{
20342 funccall_T *fcp = (funccall_T *)cookie;
20343 ufunc_T *fp = fcp->func;
20344
20345 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
20346 fp->uf_tml_execed = TRUE;
20347}
20348
20349/*
20350 * Called when done with a function line.
20351 */
20352 void
20353func_line_end(cookie)
20354 void *cookie;
20355{
20356 funccall_T *fcp = (funccall_T *)cookie;
20357 ufunc_T *fp = fcp->func;
20358
20359 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
20360 {
20361 if (fp->uf_tml_execed)
20362 {
20363 ++fp->uf_tml_count[fp->uf_tml_idx];
20364 profile_end(&fp->uf_tml_start);
20365 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020366 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000020367 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
20368 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020369 }
20370 fp->uf_tml_idx = -1;
20371 }
20372}
20373#endif
20374
Bram Moolenaar071d4272004-06-13 20:20:40 +000020375/*
20376 * Return TRUE if the currently active function should be ended, because a
20377 * return was encountered or an error occured. Used inside a ":while".
20378 */
20379 int
20380func_has_ended(cookie)
20381 void *cookie;
20382{
Bram Moolenaar33570922005-01-25 22:26:29 +000020383 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020384
20385 /* Ignore the "abort" flag if the abortion behavior has been changed due to
20386 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020387 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000020388 || fcp->returned);
20389}
20390
20391/*
20392 * return TRUE if cookie indicates a function which "abort"s on errors.
20393 */
20394 int
20395func_has_abort(cookie)
20396 void *cookie;
20397{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020398 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020399}
20400
20401#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
20402typedef enum
20403{
20404 VAR_FLAVOUR_DEFAULT,
20405 VAR_FLAVOUR_SESSION,
20406 VAR_FLAVOUR_VIMINFO
20407} var_flavour_T;
20408
20409static var_flavour_T var_flavour __ARGS((char_u *varname));
20410
20411 static var_flavour_T
20412var_flavour(varname)
20413 char_u *varname;
20414{
20415 char_u *p = varname;
20416
20417 if (ASCII_ISUPPER(*p))
20418 {
20419 while (*(++p))
20420 if (ASCII_ISLOWER(*p))
20421 return VAR_FLAVOUR_SESSION;
20422 return VAR_FLAVOUR_VIMINFO;
20423 }
20424 else
20425 return VAR_FLAVOUR_DEFAULT;
20426}
20427#endif
20428
20429#if defined(FEAT_VIMINFO) || defined(PROTO)
20430/*
20431 * Restore global vars that start with a capital from the viminfo file
20432 */
20433 int
20434read_viminfo_varlist(virp, writing)
20435 vir_T *virp;
20436 int writing;
20437{
20438 char_u *tab;
20439 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000020440 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020441
20442 if (!writing && (find_viminfo_parameter('!') != NULL))
20443 {
20444 tab = vim_strchr(virp->vir_line + 1, '\t');
20445 if (tab != NULL)
20446 {
20447 *tab++ = '\0'; /* isolate the variable name */
20448 if (*tab == 'S') /* string var */
20449 is_string = TRUE;
20450
20451 tab = vim_strchr(tab, '\t');
20452 if (tab != NULL)
20453 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020454 if (is_string)
20455 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020456 tv.v_type = VAR_STRING;
20457 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020458 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020459 }
20460 else
20461 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020462 tv.v_type = VAR_NUMBER;
20463 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020464 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020465 set_var(virp->vir_line + 1, &tv, FALSE);
20466 if (is_string)
20467 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020468 }
20469 }
20470 }
20471
20472 return viminfo_readline(virp);
20473}
20474
20475/*
20476 * Write global vars that start with a capital to the viminfo file
20477 */
20478 void
20479write_viminfo_varlist(fp)
20480 FILE *fp;
20481{
Bram Moolenaar33570922005-01-25 22:26:29 +000020482 hashitem_T *hi;
20483 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020484 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020485 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020486 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020487 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020488 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020489
20490 if (find_viminfo_parameter('!') == NULL)
20491 return;
20492
20493 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000020494
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020495 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000020496 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020497 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020498 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020499 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020500 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020501 this_var = HI2DI(hi);
20502 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020503 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020504 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000020505 {
20506 case VAR_STRING: s = "STR"; break;
20507 case VAR_NUMBER: s = "NUM"; break;
20508 default: continue;
20509 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020510 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000020511 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020512 if (p != NULL)
20513 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000020514 vim_free(tofree);
20515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020516 }
20517 }
20518}
20519#endif
20520
20521#if defined(FEAT_SESSION) || defined(PROTO)
20522 int
20523store_session_globals(fd)
20524 FILE *fd;
20525{
Bram Moolenaar33570922005-01-25 22:26:29 +000020526 hashitem_T *hi;
20527 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020528 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020529 char_u *p, *t;
20530
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020531 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000020532 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020533 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020534 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020535 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020536 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020537 this_var = HI2DI(hi);
20538 if ((this_var->di_tv.v_type == VAR_NUMBER
20539 || this_var->di_tv.v_type == VAR_STRING)
20540 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020541 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020542 /* Escape special characters with a backslash. Turn a LF and
20543 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020544 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000020545 (char_u *)"\\\"\n\r");
20546 if (p == NULL) /* out of memory */
20547 break;
20548 for (t = p; *t != NUL; ++t)
20549 if (*t == '\n')
20550 *t = 'n';
20551 else if (*t == '\r')
20552 *t = 'r';
20553 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000020554 this_var->di_key,
20555 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20556 : ' ',
20557 p,
20558 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20559 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000020560 || put_eol(fd) == FAIL)
20561 {
20562 vim_free(p);
20563 return FAIL;
20564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020565 vim_free(p);
20566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020567 }
20568 }
20569 return OK;
20570}
20571#endif
20572
Bram Moolenaar661b1822005-07-28 22:36:45 +000020573/*
20574 * Display script name where an item was last set.
20575 * Should only be invoked when 'verbose' is non-zero.
20576 */
20577 void
20578last_set_msg(scriptID)
20579 scid_T scriptID;
20580{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020581 char_u *p;
20582
Bram Moolenaar661b1822005-07-28 22:36:45 +000020583 if (scriptID != 0)
20584 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020585 p = home_replace_save(NULL, get_scriptname(scriptID));
20586 if (p != NULL)
20587 {
20588 verbose_enter();
20589 MSG_PUTS(_("\n\tLast set from "));
20590 MSG_PUTS(p);
20591 vim_free(p);
20592 verbose_leave();
20593 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000020594 }
20595}
20596
Bram Moolenaar071d4272004-06-13 20:20:40 +000020597#endif /* FEAT_EVAL */
20598
20599#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
20600
20601
20602#ifdef WIN3264
20603/*
20604 * Functions for ":8" filename modifier: get 8.3 version of a filename.
20605 */
20606static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
20607static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
20608static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
20609
20610/*
20611 * Get the short pathname of a file.
20612 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
20613 */
20614 static int
20615get_short_pathname(fnamep, bufp, fnamelen)
20616 char_u **fnamep;
20617 char_u **bufp;
20618 int *fnamelen;
20619{
20620 int l,len;
20621 char_u *newbuf;
20622
20623 len = *fnamelen;
20624
20625 l = GetShortPathName(*fnamep, *fnamep, len);
20626 if (l > len - 1)
20627 {
20628 /* If that doesn't work (not enough space), then save the string
20629 * and try again with a new buffer big enough
20630 */
20631 newbuf = vim_strnsave(*fnamep, l);
20632 if (newbuf == NULL)
20633 return 0;
20634
20635 vim_free(*bufp);
20636 *fnamep = *bufp = newbuf;
20637
20638 l = GetShortPathName(*fnamep,*fnamep,l+1);
20639
20640 /* Really should always succeed, as the buffer is big enough */
20641 }
20642
20643 *fnamelen = l;
20644 return 1;
20645}
20646
20647/*
20648 * Create a short path name. Returns the length of the buffer it needs.
20649 * Doesn't copy over the end of the buffer passed in.
20650 */
20651 static int
20652shortpath_for_invalid_fname(fname, bufp, fnamelen)
20653 char_u **fname;
20654 char_u **bufp;
20655 int *fnamelen;
20656{
20657 char_u *s, *p, *pbuf2, *pbuf3;
20658 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020659 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020660
20661 /* Make a copy */
20662 len2 = *fnamelen;
20663 pbuf2 = vim_strnsave(*fname, len2);
20664 pbuf3 = NULL;
20665
20666 s = pbuf2 + len2 - 1; /* Find the end */
20667 slen = 1;
20668 plen = len2;
20669
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020670 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020671 {
20672 --s;
20673 ++slen;
20674 --plen;
20675 }
20676
20677 do
20678 {
20679 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020680 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020681 {
20682 --s;
20683 ++slen;
20684 --plen;
20685 }
20686 if (s <= pbuf2)
20687 break;
20688
20689 /* Remeber the character that is about to be blatted */
20690 ch = *s;
20691 *s = 0; /* get_short_pathname requires a null-terminated string */
20692
20693 /* Try it in situ */
20694 p = pbuf2;
20695 if (!get_short_pathname(&p, &pbuf3, &plen))
20696 {
20697 vim_free(pbuf2);
20698 return -1;
20699 }
20700 *s = ch; /* Preserve the string */
20701 } while (plen == 0);
20702
20703 if (plen > 0)
20704 {
20705 /* Remeber the length of the new string. */
20706 *fnamelen = len = plen + slen;
20707 vim_free(*bufp);
20708 if (len > len2)
20709 {
20710 /* If there's not enough space in the currently allocated string,
20711 * then copy it to a buffer big enough.
20712 */
20713 *fname= *bufp = vim_strnsave(p, len);
20714 if (*fname == NULL)
20715 return -1;
20716 }
20717 else
20718 {
20719 /* Transfer pbuf2 to being the main buffer (it's big enough) */
20720 *fname = *bufp = pbuf2;
20721 if (p != pbuf2)
20722 strncpy(*fname, p, plen);
20723 pbuf2 = NULL;
20724 }
20725 /* Concat the next bit */
20726 strncpy(*fname + plen, s, slen);
20727 (*fname)[len] = '\0';
20728 }
20729 vim_free(pbuf3);
20730 vim_free(pbuf2);
20731 return 0;
20732}
20733
20734/*
20735 * Get a pathname for a partial path.
20736 */
20737 static int
20738shortpath_for_partial(fnamep, bufp, fnamelen)
20739 char_u **fnamep;
20740 char_u **bufp;
20741 int *fnamelen;
20742{
20743 int sepcount, len, tflen;
20744 char_u *p;
20745 char_u *pbuf, *tfname;
20746 int hasTilde;
20747
20748 /* Count up the path seperators from the RHS.. so we know which part
20749 * of the path to return.
20750 */
20751 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020752 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020753 if (vim_ispathsep(*p))
20754 ++sepcount;
20755
20756 /* Need full path first (use expand_env() to remove a "~/") */
20757 hasTilde = (**fnamep == '~');
20758 if (hasTilde)
20759 pbuf = tfname = expand_env_save(*fnamep);
20760 else
20761 pbuf = tfname = FullName_save(*fnamep, FALSE);
20762
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020763 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020764
20765 if (!get_short_pathname(&tfname, &pbuf, &len))
20766 return -1;
20767
20768 if (len == 0)
20769 {
20770 /* Don't have a valid filename, so shorten the rest of the
20771 * path if we can. This CAN give us invalid 8.3 filenames, but
20772 * there's not a lot of point in guessing what it might be.
20773 */
20774 len = tflen;
20775 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
20776 return -1;
20777 }
20778
20779 /* Count the paths backward to find the beginning of the desired string. */
20780 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020781 {
20782#ifdef FEAT_MBYTE
20783 if (has_mbyte)
20784 p -= mb_head_off(tfname, p);
20785#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020786 if (vim_ispathsep(*p))
20787 {
20788 if (sepcount == 0 || (hasTilde && sepcount == 1))
20789 break;
20790 else
20791 sepcount --;
20792 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020794 if (hasTilde)
20795 {
20796 --p;
20797 if (p >= tfname)
20798 *p = '~';
20799 else
20800 return -1;
20801 }
20802 else
20803 ++p;
20804
20805 /* Copy in the string - p indexes into tfname - allocated at pbuf */
20806 vim_free(*bufp);
20807 *fnamelen = (int)STRLEN(p);
20808 *bufp = pbuf;
20809 *fnamep = p;
20810
20811 return 0;
20812}
20813#endif /* WIN3264 */
20814
20815/*
20816 * Adjust a filename, according to a string of modifiers.
20817 * *fnamep must be NUL terminated when called. When returning, the length is
20818 * determined by *fnamelen.
20819 * Returns valid flags.
20820 * When there is an error, *fnamep is set to NULL.
20821 */
20822 int
20823modify_fname(src, usedlen, fnamep, bufp, fnamelen)
20824 char_u *src; /* string with modifiers */
20825 int *usedlen; /* characters after src that are used */
20826 char_u **fnamep; /* file name so far */
20827 char_u **bufp; /* buffer for allocated file name or NULL */
20828 int *fnamelen; /* length of fnamep */
20829{
20830 int valid = 0;
20831 char_u *tail;
20832 char_u *s, *p, *pbuf;
20833 char_u dirname[MAXPATHL];
20834 int c;
20835 int has_fullname = 0;
20836#ifdef WIN3264
20837 int has_shortname = 0;
20838#endif
20839
20840repeat:
20841 /* ":p" - full path/file_name */
20842 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
20843 {
20844 has_fullname = 1;
20845
20846 valid |= VALID_PATH;
20847 *usedlen += 2;
20848
20849 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
20850 if ((*fnamep)[0] == '~'
20851#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
20852 && ((*fnamep)[1] == '/'
20853# ifdef BACKSLASH_IN_FILENAME
20854 || (*fnamep)[1] == '\\'
20855# endif
20856 || (*fnamep)[1] == NUL)
20857
20858#endif
20859 )
20860 {
20861 *fnamep = expand_env_save(*fnamep);
20862 vim_free(*bufp); /* free any allocated file name */
20863 *bufp = *fnamep;
20864 if (*fnamep == NULL)
20865 return -1;
20866 }
20867
20868 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020869 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020870 {
20871 if (vim_ispathsep(*p)
20872 && p[1] == '.'
20873 && (p[2] == NUL
20874 || vim_ispathsep(p[2])
20875 || (p[2] == '.'
20876 && (p[3] == NUL || vim_ispathsep(p[3])))))
20877 break;
20878 }
20879
20880 /* FullName_save() is slow, don't use it when not needed. */
20881 if (*p != NUL || !vim_isAbsName(*fnamep))
20882 {
20883 *fnamep = FullName_save(*fnamep, *p != NUL);
20884 vim_free(*bufp); /* free any allocated file name */
20885 *bufp = *fnamep;
20886 if (*fnamep == NULL)
20887 return -1;
20888 }
20889
20890 /* Append a path separator to a directory. */
20891 if (mch_isdir(*fnamep))
20892 {
20893 /* Make room for one or two extra characters. */
20894 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
20895 vim_free(*bufp); /* free any allocated file name */
20896 *bufp = *fnamep;
20897 if (*fnamep == NULL)
20898 return -1;
20899 add_pathsep(*fnamep);
20900 }
20901 }
20902
20903 /* ":." - path relative to the current directory */
20904 /* ":~" - path relative to the home directory */
20905 /* ":8" - shortname path - postponed till after */
20906 while (src[*usedlen] == ':'
20907 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
20908 {
20909 *usedlen += 2;
20910 if (c == '8')
20911 {
20912#ifdef WIN3264
20913 has_shortname = 1; /* Postpone this. */
20914#endif
20915 continue;
20916 }
20917 pbuf = NULL;
20918 /* Need full path first (use expand_env() to remove a "~/") */
20919 if (!has_fullname)
20920 {
20921 if (c == '.' && **fnamep == '~')
20922 p = pbuf = expand_env_save(*fnamep);
20923 else
20924 p = pbuf = FullName_save(*fnamep, FALSE);
20925 }
20926 else
20927 p = *fnamep;
20928
20929 has_fullname = 0;
20930
20931 if (p != NULL)
20932 {
20933 if (c == '.')
20934 {
20935 mch_dirname(dirname, MAXPATHL);
20936 s = shorten_fname(p, dirname);
20937 if (s != NULL)
20938 {
20939 *fnamep = s;
20940 if (pbuf != NULL)
20941 {
20942 vim_free(*bufp); /* free any allocated file name */
20943 *bufp = pbuf;
20944 pbuf = NULL;
20945 }
20946 }
20947 }
20948 else
20949 {
20950 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
20951 /* Only replace it when it starts with '~' */
20952 if (*dirname == '~')
20953 {
20954 s = vim_strsave(dirname);
20955 if (s != NULL)
20956 {
20957 *fnamep = s;
20958 vim_free(*bufp);
20959 *bufp = s;
20960 }
20961 }
20962 }
20963 vim_free(pbuf);
20964 }
20965 }
20966
20967 tail = gettail(*fnamep);
20968 *fnamelen = (int)STRLEN(*fnamep);
20969
20970 /* ":h" - head, remove "/file_name", can be repeated */
20971 /* Don't remove the first "/" or "c:\" */
20972 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
20973 {
20974 valid |= VALID_HEAD;
20975 *usedlen += 2;
20976 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020977 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020978 --tail;
20979 *fnamelen = (int)(tail - *fnamep);
20980#ifdef VMS
20981 if (*fnamelen > 0)
20982 *fnamelen += 1; /* the path separator is part of the path */
20983#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020984 while (tail > s && !after_pathsep(s, tail))
20985 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020986 }
20987
20988 /* ":8" - shortname */
20989 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
20990 {
20991 *usedlen += 2;
20992#ifdef WIN3264
20993 has_shortname = 1;
20994#endif
20995 }
20996
20997#ifdef WIN3264
20998 /* Check shortname after we have done 'heads' and before we do 'tails'
20999 */
21000 if (has_shortname)
21001 {
21002 pbuf = NULL;
21003 /* Copy the string if it is shortened by :h */
21004 if (*fnamelen < (int)STRLEN(*fnamep))
21005 {
21006 p = vim_strnsave(*fnamep, *fnamelen);
21007 if (p == 0)
21008 return -1;
21009 vim_free(*bufp);
21010 *bufp = *fnamep = p;
21011 }
21012
21013 /* Split into two implementations - makes it easier. First is where
21014 * there isn't a full name already, second is where there is.
21015 */
21016 if (!has_fullname && !vim_isAbsName(*fnamep))
21017 {
21018 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
21019 return -1;
21020 }
21021 else
21022 {
21023 int l;
21024
21025 /* Simple case, already have the full-name
21026 * Nearly always shorter, so try first time. */
21027 l = *fnamelen;
21028 if (!get_short_pathname(fnamep, bufp, &l))
21029 return -1;
21030
21031 if (l == 0)
21032 {
21033 /* Couldn't find the filename.. search the paths.
21034 */
21035 l = *fnamelen;
21036 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
21037 return -1;
21038 }
21039 *fnamelen = l;
21040 }
21041 }
21042#endif /* WIN3264 */
21043
21044 /* ":t" - tail, just the basename */
21045 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
21046 {
21047 *usedlen += 2;
21048 *fnamelen -= (int)(tail - *fnamep);
21049 *fnamep = tail;
21050 }
21051
21052 /* ":e" - extension, can be repeated */
21053 /* ":r" - root, without extension, can be repeated */
21054 while (src[*usedlen] == ':'
21055 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
21056 {
21057 /* find a '.' in the tail:
21058 * - for second :e: before the current fname
21059 * - otherwise: The last '.'
21060 */
21061 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
21062 s = *fnamep - 2;
21063 else
21064 s = *fnamep + *fnamelen - 1;
21065 for ( ; s > tail; --s)
21066 if (s[0] == '.')
21067 break;
21068 if (src[*usedlen + 1] == 'e') /* :e */
21069 {
21070 if (s > tail)
21071 {
21072 *fnamelen += (int)(*fnamep - (s + 1));
21073 *fnamep = s + 1;
21074#ifdef VMS
21075 /* cut version from the extension */
21076 s = *fnamep + *fnamelen - 1;
21077 for ( ; s > *fnamep; --s)
21078 if (s[0] == ';')
21079 break;
21080 if (s > *fnamep)
21081 *fnamelen = s - *fnamep;
21082#endif
21083 }
21084 else if (*fnamep <= tail)
21085 *fnamelen = 0;
21086 }
21087 else /* :r */
21088 {
21089 if (s > tail) /* remove one extension */
21090 *fnamelen = (int)(s - *fnamep);
21091 }
21092 *usedlen += 2;
21093 }
21094
21095 /* ":s?pat?foo?" - substitute */
21096 /* ":gs?pat?foo?" - global substitute */
21097 if (src[*usedlen] == ':'
21098 && (src[*usedlen + 1] == 's'
21099 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
21100 {
21101 char_u *str;
21102 char_u *pat;
21103 char_u *sub;
21104 int sep;
21105 char_u *flags;
21106 int didit = FALSE;
21107
21108 flags = (char_u *)"";
21109 s = src + *usedlen + 2;
21110 if (src[*usedlen + 1] == 'g')
21111 {
21112 flags = (char_u *)"g";
21113 ++s;
21114 }
21115
21116 sep = *s++;
21117 if (sep)
21118 {
21119 /* find end of pattern */
21120 p = vim_strchr(s, sep);
21121 if (p != NULL)
21122 {
21123 pat = vim_strnsave(s, (int)(p - s));
21124 if (pat != NULL)
21125 {
21126 s = p + 1;
21127 /* find end of substitution */
21128 p = vim_strchr(s, sep);
21129 if (p != NULL)
21130 {
21131 sub = vim_strnsave(s, (int)(p - s));
21132 str = vim_strnsave(*fnamep, *fnamelen);
21133 if (sub != NULL && str != NULL)
21134 {
21135 *usedlen = (int)(p + 1 - src);
21136 s = do_string_sub(str, pat, sub, flags);
21137 if (s != NULL)
21138 {
21139 *fnamep = s;
21140 *fnamelen = (int)STRLEN(s);
21141 vim_free(*bufp);
21142 *bufp = s;
21143 didit = TRUE;
21144 }
21145 }
21146 vim_free(sub);
21147 vim_free(str);
21148 }
21149 vim_free(pat);
21150 }
21151 }
21152 /* after using ":s", repeat all the modifiers */
21153 if (didit)
21154 goto repeat;
21155 }
21156 }
21157
21158 return valid;
21159}
21160
21161/*
21162 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
21163 * "flags" can be "g" to do a global substitute.
21164 * Returns an allocated string, NULL for error.
21165 */
21166 char_u *
21167do_string_sub(str, pat, sub, flags)
21168 char_u *str;
21169 char_u *pat;
21170 char_u *sub;
21171 char_u *flags;
21172{
21173 int sublen;
21174 regmatch_T regmatch;
21175 int i;
21176 int do_all;
21177 char_u *tail;
21178 garray_T ga;
21179 char_u *ret;
21180 char_u *save_cpo;
21181
21182 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
21183 save_cpo = p_cpo;
21184 p_cpo = (char_u *)"";
21185
21186 ga_init2(&ga, 1, 200);
21187
21188 do_all = (flags[0] == 'g');
21189
21190 regmatch.rm_ic = p_ic;
21191 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
21192 if (regmatch.regprog != NULL)
21193 {
21194 tail = str;
21195 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
21196 {
21197 /*
21198 * Get some space for a temporary buffer to do the substitution
21199 * into. It will contain:
21200 * - The text up to where the match is.
21201 * - The substituted text.
21202 * - The text after the match.
21203 */
21204 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
21205 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
21206 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
21207 {
21208 ga_clear(&ga);
21209 break;
21210 }
21211
21212 /* copy the text up to where the match is */
21213 i = (int)(regmatch.startp[0] - tail);
21214 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
21215 /* add the substituted text */
21216 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
21217 + ga.ga_len + i, TRUE, TRUE, FALSE);
21218 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021219 /* avoid getting stuck on a match with an empty string */
21220 if (tail == regmatch.endp[0])
21221 {
21222 if (*tail == NUL)
21223 break;
21224 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
21225 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021226 }
21227 else
21228 {
21229 tail = regmatch.endp[0];
21230 if (*tail == NUL)
21231 break;
21232 }
21233 if (!do_all)
21234 break;
21235 }
21236
21237 if (ga.ga_data != NULL)
21238 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
21239
21240 vim_free(regmatch.regprog);
21241 }
21242
21243 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
21244 ga_clear(&ga);
21245 p_cpo = save_cpo;
21246
21247 return ret;
21248}
21249
21250#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */