blob: f0d38df5a5aad66916e49a495d705a7013447d76 [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 */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000169 proftime_T uf_tm_children; /* time spent in children this call */
170 /* profiling the function per line */
171 int *uf_tml_count; /* nr of times line was executed */
172 proftime_T *uf_tml_total; /* time spend in a line + children */
173 proftime_T *uf_tml_self; /* time spend in a line itself */
174 proftime_T uf_tml_start; /* start time for current line */
175 proftime_T uf_tml_children; /* time spent in children for this line */
176 proftime_T uf_tml_wait; /* start wait time for current line */
177 int uf_tml_idx; /* index of line being timed; -1 if none */
178 int uf_tml_execed; /* line being timed was executed */
179#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000180 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000182 int uf_refcount; /* for numbered function: reference count */
183 char_u uf_name[1]; /* name of function (actually longer); can
184 start with <SNR>123_ (<SNR> is K_SPECIAL
185 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186};
187
188/* function flags */
189#define FC_ABORT 1 /* abort function on error */
190#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000191#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192
193/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000194 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000196static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000198/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000199static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
200
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000201/* list heads for garbage collection */
202static dict_T *first_dict = NULL; /* list of all dicts */
203static list_T *first_list = NULL; /* list of all lists */
204
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000205/* From user function to hashitem and back. */
206static ufunc_T dumuf;
207#define UF2HIKEY(fp) ((fp)->uf_name)
208#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
209#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
210
211#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
212#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213
Bram Moolenaar33570922005-01-25 22:26:29 +0000214#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
215#define VAR_SHORT_LEN 20 /* short variable name length */
216#define FIXVAR_CNT 12 /* number of fixed variables */
217
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000219typedef struct funccall_S funccall_T;
220
221struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222{
223 ufunc_T *func; /* function being called */
224 int linenr; /* next line to be executed */
225 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000226 struct /* fixed variables for arguments */
227 {
228 dictitem_T var; /* variable (without room for name) */
229 char_u room[VAR_SHORT_LEN]; /* room for the name */
230 } fixvar[FIXVAR_CNT];
231 dict_T l_vars; /* l: local function variables */
232 dictitem_T l_vars_var; /* variable for l: scope */
233 dict_T l_avars; /* a: argument variables */
234 dictitem_T l_avars_var; /* variable for a: scope */
235 list_T l_varlist; /* list for a:000 */
236 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
237 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238 linenr_T breakpoint; /* next line with breakpoint or zero */
239 int dbg_tick; /* debug_tick when breakpoint was set */
240 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000241#ifdef FEAT_PROFILE
242 proftime_T prof_child; /* time spent in a child */
243#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000244 funccall_T *caller; /* calling function or NULL */
245};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246
247/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000248 * Info used by a ":for" loop.
249 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000250typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000251{
252 int fi_semicolon; /* TRUE if ending in '; var]' */
253 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000254 listwatch_T fi_lw; /* keep an eye on the item used. */
255 list_T *fi_list; /* list being used */
256} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000257
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000258/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000259 * Struct used by trans_function_name()
260 */
261typedef struct
262{
Bram Moolenaar33570922005-01-25 22:26:29 +0000263 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000264 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000265 dictitem_T *fd_di; /* Dictionary item used */
266} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000267
Bram Moolenaara7043832005-01-21 11:56:39 +0000268
269/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000270 * Array to hold the value of v: variables.
271 * The value is in a dictitem, so that it can also be used in the v: scope.
272 * The reason to use this table anyway is for very quick access to the
273 * variables with the VV_ defines.
274 */
275#include "version.h"
276
277/* values for vv_flags: */
278#define VV_COMPAT 1 /* compatible, also used without "v:" */
279#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000280#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000281
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000282#define VV_NAME(s, t) s, {{t}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000283
284static struct vimvar
285{
286 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000287 dictitem_T vv_di; /* value and name for key */
288 char vv_filler[16]; /* space for LONGEST name below!!! */
289 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
290} vimvars[VV_LEN] =
291{
292 /*
293 * The order here must match the VV_ defines in vim.h!
294 * Initializing a union does not work, leave tv.vval empty to get zero's.
295 */
296 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
297 {VV_NAME("count1", VAR_NUMBER), VV_RO},
298 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
299 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
300 {VV_NAME("warningmsg", VAR_STRING), 0},
301 {VV_NAME("statusmsg", VAR_STRING), 0},
302 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
303 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
304 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
305 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
306 {VV_NAME("termresponse", VAR_STRING), VV_RO},
307 {VV_NAME("fname", VAR_STRING), VV_RO},
308 {VV_NAME("lang", VAR_STRING), VV_RO},
309 {VV_NAME("lc_time", VAR_STRING), VV_RO},
310 {VV_NAME("ctype", VAR_STRING), VV_RO},
311 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
312 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
313 {VV_NAME("fname_in", VAR_STRING), VV_RO},
314 {VV_NAME("fname_out", VAR_STRING), VV_RO},
315 {VV_NAME("fname_new", VAR_STRING), VV_RO},
316 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
317 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
318 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
319 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
320 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
321 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
322 {VV_NAME("progname", VAR_STRING), VV_RO},
323 {VV_NAME("servername", VAR_STRING), VV_RO},
324 {VV_NAME("dying", VAR_NUMBER), VV_RO},
325 {VV_NAME("exception", VAR_STRING), VV_RO},
326 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
327 {VV_NAME("register", VAR_STRING), VV_RO},
328 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
329 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000330 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
331 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000332 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000333 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
334 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000335 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
336 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
337 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
338 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
339 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000340 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000341 {VV_NAME("swapname", VAR_STRING), VV_RO},
342 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000343 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000344 {VV_NAME("char", VAR_STRING), VV_RO},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000345 {VV_NAME("mouse_win", VAR_NUMBER), 0},
346 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
347 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar33570922005-01-25 22:26:29 +0000348};
349
350/* shorthand */
351#define vv_type vv_di.di_tv.v_type
352#define vv_nr vv_di.di_tv.vval.v_number
353#define vv_str vv_di.di_tv.vval.v_string
354#define vv_tv vv_di.di_tv
355
356/*
357 * The v: variables are stored in dictionary "vimvardict".
358 * "vimvars_var" is the variable that is used for the "l:" scope.
359 */
360static dict_T vimvardict;
361static dictitem_T vimvars_var;
362#define vimvarht vimvardict.dv_hashtab
363
Bram Moolenaara40058a2005-07-11 22:42:07 +0000364static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
365static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
366#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
367static int call_vim_function __ARGS((char_u *func, int argc, char_u **argv, int safe, typval_T *rettv));
368#endif
369static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
370static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
371static char_u *skip_var_one __ARGS((char_u *arg));
372static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty));
373static void list_glob_vars __ARGS((void));
374static void list_buf_vars __ARGS((void));
375static void list_win_vars __ARGS((void));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000376#ifdef FEAT_WINDOWS
377static void list_tab_vars __ARGS((void));
378#endif
Bram Moolenaara40058a2005-07-11 22:42:07 +0000379static void list_vim_vars __ARGS((void));
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000380static void list_script_vars __ARGS((void));
381static void list_func_vars __ARGS((void));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000382static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg));
383static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
384static int check_changedtick __ARGS((char_u *arg));
385static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
386static void clear_lval __ARGS((lval_T *lp));
387static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
388static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
389static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
390static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
391static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
392static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
393static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
394static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
395static void item_lock __ARGS((typval_T *tv, int deep, int lock));
396static int tv_islocked __ARGS((typval_T *tv));
397
Bram Moolenaar33570922005-01-25 22:26:29 +0000398static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
399static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
400static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
401static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
402static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
403static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
404static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
405static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000406
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000407static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000408static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
409static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
410static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
411static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000412static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000413static listitem_T *listitem_alloc __ARGS((void));
414static void listitem_free __ARGS((listitem_T *item));
415static void listitem_remove __ARGS((list_T *l, listitem_T *item));
416static long list_len __ARGS((list_T *l));
417static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
418static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
419static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
Bram Moolenaar33570922005-01-25 22:26:29 +0000420static listitem_T *list_find __ARGS((list_T *l, long n));
Bram Moolenaara5525202006-03-02 22:52:09 +0000421static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000422static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000423static void list_append __ARGS((list_T *l, listitem_T *item));
424static int list_append_tv __ARGS((list_T *l, typval_T *tv));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000425static int list_append_string __ARGS((list_T *l, char_u *str, int len));
426static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000427static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
428static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
429static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000430static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000431static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000432static char_u *list2string __ARGS((typval_T *tv, int copyID));
433static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000434static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
435static void set_ref_in_list __ARGS((list_T *l, int copyID));
436static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000437static void dict_unref __ARGS((dict_T *d));
Bram Moolenaar685295c2006-10-15 20:37:38 +0000438static void dict_free __ARGS((dict_T *d, int recurse));
Bram Moolenaar33570922005-01-25 22:26:29 +0000439static dictitem_T *dictitem_alloc __ARGS((char_u *key));
440static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
441static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
442static void dictitem_free __ARGS((dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000443static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000444static int dict_add __ARGS((dict_T *d, dictitem_T *item));
445static long dict_len __ARGS((dict_T *d));
446static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000447static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000448static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000449static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
450static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000451static char_u *string_quote __ARGS((char_u *str, int function));
452static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
453static int find_internal_func __ARGS((char_u *name));
454static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
455static 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));
456static 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 +0000457static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000458
459static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
460static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
463static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
464static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
465static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
467static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
468static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
469static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
470static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000475static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000476static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000479#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000480static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000481static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
483#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000484static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
489static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000502static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000503static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000516static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000517static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000518static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000519static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000524static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000525static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000532static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000533static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000534static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000536static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000537static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000557static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000558static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000563static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000564static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000579static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000580static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000581static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000582static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000585#ifdef vim_mkdir
586static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
587#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000588static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000591static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000592static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000593static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000594static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000595static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000596static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000597static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000599static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000610static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000611static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000612static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000614static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000619static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000620static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000621static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000622static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000623static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000624static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000625static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000626static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
627static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000628static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000629static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
630static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000631static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2c932302006-03-18 21:42:09 +0000632static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000633#ifdef HAVE_STRFTIME
634static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
635#endif
636static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
637static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
638static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
639static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
640static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
641static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
642static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
643static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
644static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
645static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
646static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
647static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000648static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000649static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000650static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000651static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000652static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000653static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000654static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000655static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
656static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
657static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
658static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
659static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
660static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
661static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
662static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
664static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
665static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
666static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
667static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000668static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
669static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000670static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000671static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000672
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000673static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
674static pos_T *var2fpos __ARGS((typval_T *varp, int lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000675static int get_env_len __ARGS((char_u **arg));
676static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000677static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000678static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
679#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
680#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
681 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000682static 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 +0000683static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000684static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000685static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
686static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000687static typval_T *alloc_tv __ARGS((void));
688static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000689static void init_tv __ARGS((typval_T *varp));
690static long get_tv_number __ARGS((typval_T *varp));
691static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000692static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000693static char_u *get_tv_string __ARGS((typval_T *varp));
694static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000695static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000696static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000697static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000698static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
699static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
700static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
701static void list_one_var __ARGS((dictitem_T *v, char_u *prefix));
702static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
703static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
704static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000705static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000706static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000707static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000708static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000709static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
710static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
711static int eval_fname_script __ARGS((char_u *p));
712static int eval_fname_sid __ARGS((char_u *p));
713static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000714static ufunc_T *find_func __ARGS((char_u *name));
715static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000716static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000717#ifdef FEAT_PROFILE
718static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000719static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
720static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
721static int
722# ifdef __BORLANDC__
723 _RTLENTRYF
724# endif
725 prof_total_cmp __ARGS((const void *s1, const void *s2));
726static int
727# ifdef __BORLANDC__
728 _RTLENTRYF
729# endif
730 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000731#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000732static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000733static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000734static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000735static void func_free __ARGS((ufunc_T *fp));
736static void func_unref __ARGS((char_u *name));
737static void func_ref __ARGS((char_u *name));
738static 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));
739static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000740static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
741static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000742static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000743static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000744static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000745
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000746/* Character used as separated in autoload function/variable names. */
747#define AUTOLOAD_CHAR '#'
748
Bram Moolenaar33570922005-01-25 22:26:29 +0000749/*
750 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000751 */
752 void
753eval_init()
754{
Bram Moolenaar33570922005-01-25 22:26:29 +0000755 int i;
756 struct vimvar *p;
757
758 init_var_dict(&globvardict, &globvars_var);
759 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000760 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000761 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000762
763 for (i = 0; i < VV_LEN; ++i)
764 {
765 p = &vimvars[i];
766 STRCPY(p->vv_di.di_key, p->vv_name);
767 if (p->vv_flags & VV_RO)
768 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
769 else if (p->vv_flags & VV_RO_SBX)
770 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
771 else
772 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000773
774 /* add to v: scope dict, unless the value is not always available */
775 if (p->vv_type != VAR_UNKNOWN)
776 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000777 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000778 /* add to compat scope dict */
779 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000780 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000781}
782
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000783#if defined(EXITFREE) || defined(PROTO)
784 void
785eval_clear()
786{
787 int i;
788 struct vimvar *p;
789
790 for (i = 0; i < VV_LEN; ++i)
791 {
792 p = &vimvars[i];
793 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000794 {
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000795 vim_free(p->vv_di.di_tv.vval.v_string);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000796 p->vv_di.di_tv.vval.v_string = NULL;
797 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000798 }
799 hash_clear(&vimvarht);
800 hash_clear(&compat_hashtab);
801
802 /* script-local variables */
803 for (i = 1; i <= ga_scripts.ga_len; ++i)
804 vars_clear(&SCRIPT_VARS(i));
805 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000806 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000807
808 /* global variables */
809 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000810
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000811 /* functions */
Bram Moolenaard9fba312005-06-26 22:34:35 +0000812 free_all_functions();
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000813 hash_clear(&func_hashtab);
814
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000815 /* autoloaded script names */
816 ga_clear_strings(&ga_loaded);
817
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000818 /* unreferenced lists and dicts */
819 (void)garbage_collect();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000820}
821#endif
822
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000823/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 * Return the name of the executed function.
825 */
826 char_u *
827func_name(cookie)
828 void *cookie;
829{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000830 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831}
832
833/*
834 * Return the address holding the next breakpoint line for a funccall cookie.
835 */
836 linenr_T *
837func_breakpoint(cookie)
838 void *cookie;
839{
Bram Moolenaar33570922005-01-25 22:26:29 +0000840 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841}
842
843/*
844 * Return the address holding the debug tick for a funccall cookie.
845 */
846 int *
847func_dbg_tick(cookie)
848 void *cookie;
849{
Bram Moolenaar33570922005-01-25 22:26:29 +0000850 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851}
852
853/*
854 * Return the nesting level for a funccall cookie.
855 */
856 int
857func_level(cookie)
858 void *cookie;
859{
Bram Moolenaar33570922005-01-25 22:26:29 +0000860 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861}
862
863/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000864funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865
866/*
867 * Return TRUE when a function was ended by a ":return" command.
868 */
869 int
870current_func_returned()
871{
872 return current_funccal->returned;
873}
874
875
876/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 * Set an internal variable to a string value. Creates the variable if it does
878 * not already exist.
879 */
880 void
881set_internal_string_var(name, value)
882 char_u *name;
883 char_u *value;
884{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000885 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000886 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887
888 val = vim_strsave(value);
889 if (val != NULL)
890 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000891 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000892 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000894 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000895 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 }
897 }
898}
899
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000900static lval_T *redir_lval = NULL;
901static char_u *redir_endp = NULL;
902static char_u *redir_varname = NULL;
903
904/*
905 * Start recording command output to a variable
906 * Returns OK if successfully completed the setup. FAIL otherwise.
907 */
908 int
909var_redir_start(name, append)
910 char_u *name;
911 int append; /* append to an existing variable */
912{
913 int save_emsg;
914 int err;
915 typval_T tv;
916
917 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000918 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000919 {
920 EMSG(_(e_invarg));
921 return FAIL;
922 }
923
924 redir_varname = vim_strsave(name);
925 if (redir_varname == NULL)
926 return FAIL;
927
928 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
929 if (redir_lval == NULL)
930 {
931 var_redir_stop();
932 return FAIL;
933 }
934
935 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000936 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
937 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000938 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
939 {
940 if (redir_endp != NULL && *redir_endp != NUL)
941 /* Trailing characters are present after the variable name */
942 EMSG(_(e_trailing));
943 else
944 EMSG(_(e_invarg));
945 var_redir_stop();
946 return FAIL;
947 }
948
949 /* check if we can write to the variable: set it to or append an empty
950 * string */
951 save_emsg = did_emsg;
952 did_emsg = FALSE;
953 tv.v_type = VAR_STRING;
954 tv.vval.v_string = (char_u *)"";
955 if (append)
956 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
957 else
958 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
959 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000960 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000961 if (err)
962 {
963 var_redir_stop();
964 return FAIL;
965 }
966 if (redir_lval->ll_newkey != NULL)
967 {
968 /* Dictionary item was created, don't do it again. */
969 vim_free(redir_lval->ll_newkey);
970 redir_lval->ll_newkey = NULL;
971 }
972
973 return OK;
974}
975
976/*
977 * Append "value[len]" to the variable set by var_redir_start().
978 */
979 void
980var_redir_str(value, len)
981 char_u *value;
982 int len;
983{
984 char_u *val;
985 typval_T tv;
986 int save_emsg;
987 int err;
988
989 if (redir_lval == NULL)
990 return;
991
992 if (len == -1)
993 /* Append the entire string */
994 val = vim_strsave(value);
995 else
996 /* Append only the specified number of characters */
997 val = vim_strnsave(value, len);
998 if (val == NULL)
999 return;
1000
1001 tv.v_type = VAR_STRING;
1002 tv.vval.v_string = val;
1003
1004 save_emsg = did_emsg;
1005 did_emsg = FALSE;
1006 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1007 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001008 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001009 if (err)
1010 var_redir_stop();
1011
1012 vim_free(tv.vval.v_string);
1013}
1014
1015/*
1016 * Stop redirecting command output to a variable.
1017 */
1018 void
1019var_redir_stop()
1020{
1021 if (redir_lval != NULL)
1022 {
1023 clear_lval(redir_lval);
1024 vim_free(redir_lval);
1025 redir_lval = NULL;
1026 }
1027 vim_free(redir_varname);
1028 redir_varname = NULL;
1029}
1030
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031# if defined(FEAT_MBYTE) || defined(PROTO)
1032 int
1033eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1034 char_u *enc_from;
1035 char_u *enc_to;
1036 char_u *fname_from;
1037 char_u *fname_to;
1038{
1039 int err = FALSE;
1040
1041 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1042 set_vim_var_string(VV_CC_TO, enc_to, -1);
1043 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1044 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1045 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1046 err = TRUE;
1047 set_vim_var_string(VV_CC_FROM, NULL, -1);
1048 set_vim_var_string(VV_CC_TO, NULL, -1);
1049 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1050 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1051
1052 if (err)
1053 return FAIL;
1054 return OK;
1055}
1056# endif
1057
1058# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1059 int
1060eval_printexpr(fname, args)
1061 char_u *fname;
1062 char_u *args;
1063{
1064 int err = FALSE;
1065
1066 set_vim_var_string(VV_FNAME_IN, fname, -1);
1067 set_vim_var_string(VV_CMDARG, args, -1);
1068 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1069 err = TRUE;
1070 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1071 set_vim_var_string(VV_CMDARG, NULL, -1);
1072
1073 if (err)
1074 {
1075 mch_remove(fname);
1076 return FAIL;
1077 }
1078 return OK;
1079}
1080# endif
1081
1082# if defined(FEAT_DIFF) || defined(PROTO)
1083 void
1084eval_diff(origfile, newfile, outfile)
1085 char_u *origfile;
1086 char_u *newfile;
1087 char_u *outfile;
1088{
1089 int err = FALSE;
1090
1091 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1092 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1093 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1094 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1095 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1096 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1097 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1098}
1099
1100 void
1101eval_patch(origfile, difffile, outfile)
1102 char_u *origfile;
1103 char_u *difffile;
1104 char_u *outfile;
1105{
1106 int err;
1107
1108 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1109 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1110 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1111 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1112 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1113 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1114 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1115}
1116# endif
1117
1118/*
1119 * Top level evaluation function, returning a boolean.
1120 * Sets "error" to TRUE if there was an error.
1121 * Return TRUE or FALSE.
1122 */
1123 int
1124eval_to_bool(arg, error, nextcmd, skip)
1125 char_u *arg;
1126 int *error;
1127 char_u **nextcmd;
1128 int skip; /* only parse, don't execute */
1129{
Bram Moolenaar33570922005-01-25 22:26:29 +00001130 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 int retval = FALSE;
1132
1133 if (skip)
1134 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001135 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 else
1138 {
1139 *error = FALSE;
1140 if (!skip)
1141 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001142 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001143 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 }
1145 }
1146 if (skip)
1147 --emsg_skip;
1148
1149 return retval;
1150}
1151
1152/*
1153 * Top level evaluation function, returning a string. If "skip" is TRUE,
1154 * only parsing to "nextcmd" is done, without reporting errors. Return
1155 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1156 */
1157 char_u *
1158eval_to_string_skip(arg, nextcmd, skip)
1159 char_u *arg;
1160 char_u **nextcmd;
1161 int skip; /* only parse, don't execute */
1162{
Bram Moolenaar33570922005-01-25 22:26:29 +00001163 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 char_u *retval;
1165
1166 if (skip)
1167 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001168 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 retval = NULL;
1170 else
1171 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001172 retval = vim_strsave(get_tv_string(&tv));
1173 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 }
1175 if (skip)
1176 --emsg_skip;
1177
1178 return retval;
1179}
1180
1181/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001182 * Skip over an expression at "*pp".
1183 * Return FAIL for an error, OK otherwise.
1184 */
1185 int
1186skip_expr(pp)
1187 char_u **pp;
1188{
Bram Moolenaar33570922005-01-25 22:26:29 +00001189 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001190
1191 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001192 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001193}
1194
1195/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 * Top level evaluation function, returning a string.
1197 * Return pointer to allocated memory, or NULL for failure.
1198 */
1199 char_u *
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001200eval_to_string(arg, nextcmd, dolist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 char_u *arg;
1202 char_u **nextcmd;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001203 int dolist; /* turn List into sequence of lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204{
Bram Moolenaar33570922005-01-25 22:26:29 +00001205 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001207 garray_T ga;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001209 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 retval = NULL;
1211 else
1212 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001213 if (dolist && tv.v_type == VAR_LIST)
1214 {
1215 ga_init2(&ga, (int)sizeof(char), 80);
1216 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
1217 ga_append(&ga, NUL);
1218 retval = (char_u *)ga.ga_data;
1219 }
1220 else
1221 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001222 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 }
1224
1225 return retval;
1226}
1227
1228/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001229 * Call eval_to_string() without using current local variables and using
1230 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 */
1232 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001233eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 char_u *arg;
1235 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001236 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237{
1238 char_u *retval;
1239 void *save_funccalp;
1240
1241 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001242 if (use_sandbox)
1243 ++sandbox;
1244 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001245 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001246 if (use_sandbox)
1247 --sandbox;
1248 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 restore_funccal(save_funccalp);
1250 return retval;
1251}
1252
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253/*
1254 * Top level evaluation function, returning a number.
1255 * Evaluates "expr" silently.
1256 * Returns -1 for an error.
1257 */
1258 int
1259eval_to_number(expr)
1260 char_u *expr;
1261{
Bram Moolenaar33570922005-01-25 22:26:29 +00001262 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001264 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265
1266 ++emsg_off;
1267
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001268 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 retval = -1;
1270 else
1271 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001272 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001273 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 }
1275 --emsg_off;
1276
1277 return retval;
1278}
1279
Bram Moolenaara40058a2005-07-11 22:42:07 +00001280/*
1281 * Prepare v: variable "idx" to be used.
1282 * Save the current typeval in "save_tv".
1283 * When not used yet add the variable to the v: hashtable.
1284 */
1285 static void
1286prepare_vimvar(idx, save_tv)
1287 int idx;
1288 typval_T *save_tv;
1289{
1290 *save_tv = vimvars[idx].vv_tv;
1291 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1292 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1293}
1294
1295/*
1296 * Restore v: variable "idx" to typeval "save_tv".
1297 * When no longer defined, remove the variable from the v: hashtable.
1298 */
1299 static void
1300restore_vimvar(idx, save_tv)
1301 int idx;
1302 typval_T *save_tv;
1303{
1304 hashitem_T *hi;
1305
1306 clear_tv(&vimvars[idx].vv_tv);
1307 vimvars[idx].vv_tv = *save_tv;
1308 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1309 {
1310 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1311 if (HASHITEM_EMPTY(hi))
1312 EMSG2(_(e_intern2), "restore_vimvar()");
1313 else
1314 hash_remove(&vimvarht, hi);
1315 }
1316}
1317
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001318#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001319/*
1320 * Evaluate an expression to a list with suggestions.
1321 * For the "expr:" part of 'spellsuggest'.
1322 */
1323 list_T *
1324eval_spell_expr(badword, expr)
1325 char_u *badword;
1326 char_u *expr;
1327{
1328 typval_T save_val;
1329 typval_T rettv;
1330 list_T *list = NULL;
1331 char_u *p = skipwhite(expr);
1332
1333 /* Set "v:val" to the bad word. */
1334 prepare_vimvar(VV_VAL, &save_val);
1335 vimvars[VV_VAL].vv_type = VAR_STRING;
1336 vimvars[VV_VAL].vv_str = badword;
1337 if (p_verbose == 0)
1338 ++emsg_off;
1339
1340 if (eval1(&p, &rettv, TRUE) == OK)
1341 {
1342 if (rettv.v_type != VAR_LIST)
1343 clear_tv(&rettv);
1344 else
1345 list = rettv.vval.v_list;
1346 }
1347
1348 if (p_verbose == 0)
1349 --emsg_off;
1350 vimvars[VV_VAL].vv_str = NULL;
1351 restore_vimvar(VV_VAL, &save_val);
1352
1353 return list;
1354}
1355
1356/*
1357 * "list" is supposed to contain two items: a word and a number. Return the
1358 * word in "pp" and the number as the return value.
1359 * Return -1 if anything isn't right.
1360 * Used to get the good word and score from the eval_spell_expr() result.
1361 */
1362 int
1363get_spellword(list, pp)
1364 list_T *list;
1365 char_u **pp;
1366{
1367 listitem_T *li;
1368
1369 li = list->lv_first;
1370 if (li == NULL)
1371 return -1;
1372 *pp = get_tv_string(&li->li_tv);
1373
1374 li = li->li_next;
1375 if (li == NULL)
1376 return -1;
1377 return get_tv_number(&li->li_tv);
1378}
1379#endif
1380
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001381/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001382 * Top level evaluation function.
1383 * Returns an allocated typval_T with the result.
1384 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001385 */
1386 typval_T *
1387eval_expr(arg, nextcmd)
1388 char_u *arg;
1389 char_u **nextcmd;
1390{
1391 typval_T *tv;
1392
1393 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001394 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001395 {
1396 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001397 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001398 }
1399
1400 return tv;
1401}
1402
1403
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1405/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001406 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 * Uses argv[argc] for the function arguments.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001408 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001410 static int
1411call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 char_u *func;
1413 int argc;
1414 char_u **argv;
1415 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001416 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417{
Bram Moolenaar33570922005-01-25 22:26:29 +00001418 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 long n;
1420 int len;
1421 int i;
1422 int doesrange;
1423 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001424 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001426 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001428 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429
1430 for (i = 0; i < argc; i++)
1431 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001432 /* Pass a NULL or empty argument as an empty string */
1433 if (argv[i] == NULL || *argv[i] == NUL)
1434 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001435 argvars[i].v_type = VAR_STRING;
1436 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001437 continue;
1438 }
1439
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 /* Recognize a number argument, the others must be strings. */
1441 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1442 if (len != 0 && len == (int)STRLEN(argv[i]))
1443 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001444 argvars[i].v_type = VAR_NUMBER;
1445 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 }
1447 else
1448 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001449 argvars[i].v_type = VAR_STRING;
1450 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 }
1452 }
1453
1454 if (safe)
1455 {
1456 save_funccalp = save_funccal();
1457 ++sandbox;
1458 }
1459
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001460 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1461 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001463 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 if (safe)
1465 {
1466 --sandbox;
1467 restore_funccal(save_funccalp);
1468 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001469 vim_free(argvars);
1470
1471 if (ret == FAIL)
1472 clear_tv(rettv);
1473
1474 return ret;
1475}
1476
1477/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001478 * Call vimL function "func" and return the result as a string.
1479 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001480 * Uses argv[argc] for the function arguments.
1481 */
1482 void *
1483call_func_retstr(func, argc, argv, safe)
1484 char_u *func;
1485 int argc;
1486 char_u **argv;
1487 int safe; /* use the sandbox */
1488{
1489 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001490 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001491
1492 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1493 return NULL;
1494
1495 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001496 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 return retval;
1498}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001499
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001500#if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001501/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001502 * Call vimL function "func" and return the result as a number.
1503 * Returns -1 when calling the function fails.
1504 * Uses argv[argc] for the function arguments.
1505 */
1506 long
1507call_func_retnr(func, argc, argv, safe)
1508 char_u *func;
1509 int argc;
1510 char_u **argv;
1511 int safe; /* use the sandbox */
1512{
1513 typval_T rettv;
1514 long retval;
1515
1516 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1517 return -1;
1518
1519 retval = get_tv_number_chk(&rettv, NULL);
1520 clear_tv(&rettv);
1521 return retval;
1522}
1523#endif
1524
1525/*
1526 * Call vimL function "func" and return the result as a list
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001527 * Uses argv[argc] for the function arguments.
1528 */
1529 void *
1530call_func_retlist(func, argc, argv, safe)
1531 char_u *func;
1532 int argc;
1533 char_u **argv;
1534 int safe; /* use the sandbox */
1535{
1536 typval_T rettv;
1537
1538 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1539 return NULL;
1540
1541 if (rettv.v_type != VAR_LIST)
1542 {
1543 clear_tv(&rettv);
1544 return NULL;
1545 }
1546
1547 return rettv.vval.v_list;
1548}
1549
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550#endif
1551
1552/*
1553 * Save the current function call pointer, and set it to NULL.
1554 * Used when executing autocommands and for ":source".
1555 */
1556 void *
1557save_funccal()
1558{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001559 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 current_funccal = NULL;
1562 return (void *)fc;
1563}
1564
1565 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001566restore_funccal(vfc)
1567 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001569 funccall_T *fc = (funccall_T *)vfc;
1570
1571 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572}
1573
Bram Moolenaar05159a02005-02-26 23:04:13 +00001574#if defined(FEAT_PROFILE) || defined(PROTO)
1575/*
1576 * Prepare profiling for entering a child or something else that is not
1577 * counted for the script/function itself.
1578 * Should always be called in pair with prof_child_exit().
1579 */
1580 void
1581prof_child_enter(tm)
1582 proftime_T *tm; /* place to store waittime */
1583{
1584 funccall_T *fc = current_funccal;
1585
1586 if (fc != NULL && fc->func->uf_profiling)
1587 profile_start(&fc->prof_child);
1588 script_prof_save(tm);
1589}
1590
1591/*
1592 * Take care of time spent in a child.
1593 * Should always be called after prof_child_enter().
1594 */
1595 void
1596prof_child_exit(tm)
1597 proftime_T *tm; /* where waittime was stored */
1598{
1599 funccall_T *fc = current_funccal;
1600
1601 if (fc != NULL && fc->func->uf_profiling)
1602 {
1603 profile_end(&fc->prof_child);
1604 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1605 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1606 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1607 }
1608 script_prof_restore(tm);
1609}
1610#endif
1611
1612
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613#ifdef FEAT_FOLDING
1614/*
1615 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1616 * it in "*cp". Doesn't give error messages.
1617 */
1618 int
1619eval_foldexpr(arg, cp)
1620 char_u *arg;
1621 int *cp;
1622{
Bram Moolenaar33570922005-01-25 22:26:29 +00001623 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 int retval;
1625 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001626 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1627 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628
1629 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001630 if (use_sandbox)
1631 ++sandbox;
1632 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001634 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 retval = 0;
1636 else
1637 {
1638 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001639 if (tv.v_type == VAR_NUMBER)
1640 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001641 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 retval = 0;
1643 else
1644 {
1645 /* If the result is a string, check if there is a non-digit before
1646 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001647 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 if (!VIM_ISDIGIT(*s) && *s != '-')
1649 *cp = *s++;
1650 retval = atol((char *)s);
1651 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001652 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 }
1654 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001655 if (use_sandbox)
1656 --sandbox;
1657 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658
1659 return retval;
1660}
1661#endif
1662
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001664 * ":let" list all variable values
1665 * ":let var1 var2" list variable values
1666 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001667 * ":let var += expr" assignment command.
1668 * ":let var -= expr" assignment command.
1669 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001670 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 */
1672 void
1673ex_let(eap)
1674 exarg_T *eap;
1675{
1676 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001677 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001678 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001680 int var_count = 0;
1681 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001682 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001683 char_u *argend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684
Bram Moolenaardb552d602006-03-23 22:59:57 +00001685 argend = skip_var_list(arg, &var_count, &semicolon);
1686 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001687 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001688 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1689 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001690 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001691 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001693 /*
1694 * ":let" without "=": list variables
1695 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001696 if (*arg == '[')
1697 EMSG(_(e_invarg));
1698 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001699 /* ":let var1 var2" */
1700 arg = list_arg_vars(eap, arg);
1701 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001702 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001703 /* ":let" */
Bram Moolenaara7043832005-01-21 11:56:39 +00001704 list_glob_vars();
1705 list_buf_vars();
1706 list_win_vars();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001707#ifdef FEAT_WINDOWS
1708 list_tab_vars();
1709#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001710 list_script_vars();
1711 list_func_vars();
Bram Moolenaara7043832005-01-21 11:56:39 +00001712 list_vim_vars();
1713 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 eap->nextcmd = check_nextcmd(arg);
1715 }
1716 else
1717 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001718 op[0] = '=';
1719 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001720 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001721 {
1722 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1723 op[0] = expr[-1]; /* +=, -= or .= */
1724 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001725 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001726
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 if (eap->skip)
1728 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001729 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 if (eap->skip)
1731 {
1732 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001733 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 --emsg_skip;
1735 }
1736 else if (i != FAIL)
1737 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001738 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001739 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001740 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 }
1742 }
1743}
1744
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001745/*
1746 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1747 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001748 * When "nextchars" is not NULL it points to a string with characters that
1749 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1750 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001751 * Returns OK or FAIL;
1752 */
1753 static int
1754ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1755 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001756 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001757 int copy; /* copy values from "tv", don't move */
1758 int semicolon; /* from skip_var_list() */
1759 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001760 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001761{
1762 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001763 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001764 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001765 listitem_T *item;
1766 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001767
1768 if (*arg != '[')
1769 {
1770 /*
1771 * ":let var = expr" or ":for var in list"
1772 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001773 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001774 return FAIL;
1775 return OK;
1776 }
1777
1778 /*
1779 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1780 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001781 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001782 {
1783 EMSG(_(e_listreq));
1784 return FAIL;
1785 }
1786
1787 i = list_len(l);
1788 if (semicolon == 0 && var_count < i)
1789 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001790 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001791 return FAIL;
1792 }
1793 if (var_count - semicolon > i)
1794 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001795 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001796 return FAIL;
1797 }
1798
1799 item = l->lv_first;
1800 while (*arg != ']')
1801 {
1802 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001803 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001804 item = item->li_next;
1805 if (arg == NULL)
1806 return FAIL;
1807
1808 arg = skipwhite(arg);
1809 if (*arg == ';')
1810 {
1811 /* Put the rest of the list (may be empty) in the var after ';'.
1812 * Create a new list for this. */
1813 l = list_alloc();
1814 if (l == NULL)
1815 return FAIL;
1816 while (item != NULL)
1817 {
1818 list_append_tv(l, &item->li_tv);
1819 item = item->li_next;
1820 }
1821
1822 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001823 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001824 ltv.vval.v_list = l;
1825 l->lv_refcount = 1;
1826
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001827 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1828 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001829 clear_tv(&ltv);
1830 if (arg == NULL)
1831 return FAIL;
1832 break;
1833 }
1834 else if (*arg != ',' && *arg != ']')
1835 {
1836 EMSG2(_(e_intern2), "ex_let_vars()");
1837 return FAIL;
1838 }
1839 }
1840
1841 return OK;
1842}
1843
1844/*
1845 * Skip over assignable variable "var" or list of variables "[var, var]".
1846 * Used for ":let varvar = expr" and ":for varvar in expr".
1847 * For "[var, var]" increment "*var_count" for each variable.
1848 * for "[var, var; var]" set "semicolon".
1849 * Return NULL for an error.
1850 */
1851 static char_u *
1852skip_var_list(arg, var_count, semicolon)
1853 char_u *arg;
1854 int *var_count;
1855 int *semicolon;
1856{
1857 char_u *p, *s;
1858
1859 if (*arg == '[')
1860 {
1861 /* "[var, var]": find the matching ']'. */
1862 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001863 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001864 {
1865 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1866 s = skip_var_one(p);
1867 if (s == p)
1868 {
1869 EMSG2(_(e_invarg2), p);
1870 return NULL;
1871 }
1872 ++*var_count;
1873
1874 p = skipwhite(s);
1875 if (*p == ']')
1876 break;
1877 else if (*p == ';')
1878 {
1879 if (*semicolon == 1)
1880 {
1881 EMSG(_("Double ; in list of variables"));
1882 return NULL;
1883 }
1884 *semicolon = 1;
1885 }
1886 else if (*p != ',')
1887 {
1888 EMSG2(_(e_invarg2), p);
1889 return NULL;
1890 }
1891 }
1892 return p + 1;
1893 }
1894 else
1895 return skip_var_one(arg);
1896}
1897
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001898/*
Bram Moolenaar92124a32005-06-17 22:03:40 +00001899 * Skip one (assignable) variable name, includig @r, $VAR, &option, d.key,
1900 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001901 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001902 static char_u *
1903skip_var_one(arg)
1904 char_u *arg;
1905{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001906 if (*arg == '@' && arg[1] != NUL)
1907 return arg + 2;
1908 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1909 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001910}
1911
Bram Moolenaara7043832005-01-21 11:56:39 +00001912/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001913 * List variables for hashtab "ht" with prefix "prefix".
1914 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001915 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001916 static void
Bram Moolenaar33570922005-01-25 22:26:29 +00001917list_hashtable_vars(ht, prefix, empty)
1918 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001919 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001920 int empty;
Bram Moolenaara7043832005-01-21 11:56:39 +00001921{
Bram Moolenaar33570922005-01-25 22:26:29 +00001922 hashitem_T *hi;
1923 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001924 int todo;
1925
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001926 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001927 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1928 {
1929 if (!HASHITEM_EMPTY(hi))
1930 {
1931 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001932 di = HI2DI(hi);
1933 if (empty || di->di_tv.v_type != VAR_STRING
1934 || di->di_tv.vval.v_string != NULL)
1935 list_one_var(di, prefix);
Bram Moolenaara7043832005-01-21 11:56:39 +00001936 }
1937 }
1938}
1939
1940/*
1941 * List global variables.
1942 */
1943 static void
1944list_glob_vars()
1945{
Bram Moolenaar33570922005-01-25 22:26:29 +00001946 list_hashtable_vars(&globvarht, (char_u *)"", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001947}
1948
1949/*
1950 * List buffer variables.
1951 */
1952 static void
1953list_buf_vars()
1954{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001955 char_u numbuf[NUMBUFLEN];
1956
Bram Moolenaar33570922005-01-25 22:26:29 +00001957 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001958
1959 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
1960 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER, numbuf);
Bram Moolenaara7043832005-01-21 11:56:39 +00001961}
1962
1963/*
1964 * List window variables.
1965 */
1966 static void
1967list_win_vars()
1968{
Bram Moolenaar33570922005-01-25 22:26:29 +00001969 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001970}
1971
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001972#ifdef FEAT_WINDOWS
1973/*
1974 * List tab page variables.
1975 */
1976 static void
1977list_tab_vars()
1978{
1979 list_hashtable_vars(&curtab->tp_vars.dv_hashtab, (char_u *)"t:", TRUE);
1980}
1981#endif
1982
Bram Moolenaara7043832005-01-21 11:56:39 +00001983/*
1984 * List Vim variables.
1985 */
1986 static void
1987list_vim_vars()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001988{
Bram Moolenaar33570922005-01-25 22:26:29 +00001989 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001990}
1991
1992/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001993 * List script-local variables, if there is a script.
1994 */
1995 static void
1996list_script_vars()
1997{
1998 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
1999 list_hashtable_vars(&SCRIPT_VARS(current_SID), (char_u *)"s:", FALSE);
2000}
2001
2002/*
2003 * List function variables, if there is a function.
2004 */
2005 static void
2006list_func_vars()
2007{
2008 if (current_funccal != NULL)
2009 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
2010 (char_u *)"l:", FALSE);
2011}
2012
2013/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002014 * List variables in "arg".
2015 */
2016 static char_u *
2017list_arg_vars(eap, arg)
2018 exarg_T *eap;
2019 char_u *arg;
2020{
2021 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002022 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002023 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002024 char_u *name_start;
2025 char_u *arg_subsc;
2026 char_u *tofree;
2027 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002028
2029 while (!ends_excmd(*arg) && !got_int)
2030 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002031 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002032 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002033 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002034 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2035 {
2036 emsg_severe = TRUE;
2037 EMSG(_(e_trailing));
2038 break;
2039 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002040 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002041 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002042 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002043 /* get_name_len() takes care of expanding curly braces */
2044 name_start = name = arg;
2045 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2046 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002047 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002048 /* This is mainly to keep test 49 working: when expanding
2049 * curly braces fails overrule the exception error message. */
2050 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002051 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002052 emsg_severe = TRUE;
2053 EMSG2(_(e_invarg2), arg);
2054 break;
2055 }
2056 error = TRUE;
2057 }
2058 else
2059 {
2060 if (tofree != NULL)
2061 name = tofree;
2062 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002063 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002064 else
2065 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002066 /* handle d.key, l[idx], f(expr) */
2067 arg_subsc = arg;
2068 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002069 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002070 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002071 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002072 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002073 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002074 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002075 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002076 case 'g': list_glob_vars(); break;
2077 case 'b': list_buf_vars(); break;
2078 case 'w': list_win_vars(); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002079#ifdef FEAT_WINDOWS
2080 case 't': list_tab_vars(); break;
2081#endif
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002082 case 'v': list_vim_vars(); break;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002083 case 's': list_script_vars(); break;
2084 case 'l': list_func_vars(); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002085 default:
2086 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002087 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002088 }
2089 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002090 {
2091 char_u numbuf[NUMBUFLEN];
2092 char_u *tf;
2093 int c;
2094 char_u *s;
2095
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002096 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002097 c = *arg;
2098 *arg = NUL;
2099 list_one_var_a((char_u *)"",
2100 arg == arg_subsc ? name : name_start,
2101 tv.v_type, s == NULL ? (char_u *)"" : s);
2102 *arg = c;
2103 vim_free(tf);
2104 }
2105 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002106 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002107 }
2108 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002109
2110 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002111 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002112
2113 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002114 }
2115
2116 return arg;
2117}
2118
2119/*
2120 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2121 * Returns a pointer to the char just after the var name.
2122 * Returns NULL if there is an error.
2123 */
2124 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002125ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002126 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002127 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002128 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002129 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002130 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002131{
2132 int c1;
2133 char_u *name;
2134 char_u *p;
2135 char_u *arg_end = NULL;
2136 int len;
2137 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002138 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002139
2140 /*
2141 * ":let $VAR = expr": Set environment variable.
2142 */
2143 if (*arg == '$')
2144 {
2145 /* Find the end of the name. */
2146 ++arg;
2147 name = arg;
2148 len = get_env_len(&arg);
2149 if (len == 0)
2150 EMSG2(_(e_invarg2), name - 1);
2151 else
2152 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002153 if (op != NULL && (*op == '+' || *op == '-'))
2154 EMSG2(_(e_letwrong), op);
2155 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002156 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002157 EMSG(_(e_letunexp));
2158 else
2159 {
2160 c1 = name[len];
2161 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002162 p = get_tv_string_chk(tv);
2163 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002164 {
2165 int mustfree = FALSE;
2166 char_u *s = vim_getenv(name, &mustfree);
2167
2168 if (s != NULL)
2169 {
2170 p = tofree = concat_str(s, p);
2171 if (mustfree)
2172 vim_free(s);
2173 }
2174 }
2175 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002176 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002177 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002178 if (STRICMP(name, "HOME") == 0)
2179 init_homedir();
2180 else if (didset_vim && STRICMP(name, "VIM") == 0)
2181 didset_vim = FALSE;
2182 else if (didset_vimruntime
2183 && STRICMP(name, "VIMRUNTIME") == 0)
2184 didset_vimruntime = FALSE;
2185 arg_end = arg;
2186 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002187 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002188 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002189 }
2190 }
2191 }
2192
2193 /*
2194 * ":let &option = expr": Set option value.
2195 * ":let &l:option = expr": Set local option value.
2196 * ":let &g:option = expr": Set global option value.
2197 */
2198 else if (*arg == '&')
2199 {
2200 /* Find the end of the name. */
2201 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002202 if (p == NULL || (endchars != NULL
2203 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002204 EMSG(_(e_letunexp));
2205 else
2206 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002207 long n;
2208 int opt_type;
2209 long numval;
2210 char_u *stringval = NULL;
2211 char_u *s;
2212
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002213 c1 = *p;
2214 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002215
2216 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002217 s = get_tv_string_chk(tv); /* != NULL if number or string */
2218 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002219 {
2220 opt_type = get_option_value(arg, &numval,
2221 &stringval, opt_flags);
2222 if ((opt_type == 1 && *op == '.')
2223 || (opt_type == 0 && *op != '.'))
2224 EMSG2(_(e_letwrong), op);
2225 else
2226 {
2227 if (opt_type == 1) /* number */
2228 {
2229 if (*op == '+')
2230 n = numval + n;
2231 else
2232 n = numval - n;
2233 }
2234 else if (opt_type == 0 && stringval != NULL) /* string */
2235 {
2236 s = concat_str(stringval, s);
2237 vim_free(stringval);
2238 stringval = s;
2239 }
2240 }
2241 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002242 if (s != NULL)
2243 {
2244 set_option_value(arg, n, s, opt_flags);
2245 arg_end = p;
2246 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002247 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002248 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002249 }
2250 }
2251
2252 /*
2253 * ":let @r = expr": Set register contents.
2254 */
2255 else if (*arg == '@')
2256 {
2257 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002258 if (op != NULL && (*op == '+' || *op == '-'))
2259 EMSG2(_(e_letwrong), op);
2260 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002261 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002262 EMSG(_(e_letunexp));
2263 else
2264 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002265 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002266 char_u *s;
2267
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002268 p = get_tv_string_chk(tv);
2269 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002270 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002271 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002272 if (s != NULL)
2273 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002274 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002275 vim_free(s);
2276 }
2277 }
2278 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002279 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002280 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002281 arg_end = arg + 1;
2282 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002283 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002284 }
2285 }
2286
2287 /*
2288 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002289 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002290 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002291 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002292 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002293 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002294
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002295 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002296 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002297 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002298 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2299 EMSG(_(e_letunexp));
2300 else
2301 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002302 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002303 arg_end = p;
2304 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002305 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002306 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002307 }
2308
2309 else
2310 EMSG2(_(e_invarg2), arg);
2311
2312 return arg_end;
2313}
2314
2315/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002316 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2317 */
2318 static int
2319check_changedtick(arg)
2320 char_u *arg;
2321{
2322 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2323 {
2324 EMSG2(_(e_readonlyvar), arg);
2325 return TRUE;
2326 }
2327 return FALSE;
2328}
2329
2330/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002331 * Get an lval: variable, Dict item or List item that can be assigned a value
2332 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2333 * "name.key", "name.key[expr]" etc.
2334 * Indexing only works if "name" is an existing List or Dictionary.
2335 * "name" points to the start of the name.
2336 * If "rettv" is not NULL it points to the value to be assigned.
2337 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2338 * wrong; must end in space or cmd separator.
2339 *
2340 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002341 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002342 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002343 */
2344 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002345get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002346 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002347 typval_T *rettv;
2348 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002349 int unlet;
2350 int skip;
2351 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002352 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002353{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002354 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002355 char_u *expr_start, *expr_end;
2356 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002357 dictitem_T *v;
2358 typval_T var1;
2359 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002360 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002361 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002362 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002363 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002364 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002365
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002366 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002367 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002368
2369 if (skip)
2370 {
2371 /* When skipping just find the end of the name. */
2372 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002373 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002374 }
2375
2376 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002377 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002378 if (expr_start != NULL)
2379 {
2380 /* Don't expand the name when we already know there is an error. */
2381 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2382 && *p != '[' && *p != '.')
2383 {
2384 EMSG(_(e_trailing));
2385 return NULL;
2386 }
2387
2388 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2389 if (lp->ll_exp_name == NULL)
2390 {
2391 /* Report an invalid expression in braces, unless the
2392 * expression evaluation has been cancelled due to an
2393 * aborting error, an interrupt, or an exception. */
2394 if (!aborting() && !quiet)
2395 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002396 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002397 EMSG2(_(e_invarg2), name);
2398 return NULL;
2399 }
2400 }
2401 lp->ll_name = lp->ll_exp_name;
2402 }
2403 else
2404 lp->ll_name = name;
2405
2406 /* Without [idx] or .key we are done. */
2407 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2408 return p;
2409
2410 cc = *p;
2411 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002412 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002413 if (v == NULL && !quiet)
2414 EMSG2(_(e_undefvar), lp->ll_name);
2415 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002416 if (v == NULL)
2417 return NULL;
2418
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002419 /*
2420 * Loop until no more [idx] or .key is following.
2421 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002422 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002423 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002424 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002425 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2426 && !(lp->ll_tv->v_type == VAR_DICT
2427 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002428 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002429 if (!quiet)
2430 EMSG(_("E689: Can only index a List or Dictionary"));
2431 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002432 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002433 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002434 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002435 if (!quiet)
2436 EMSG(_("E708: [:] must come last"));
2437 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002438 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002439
Bram Moolenaar8c711452005-01-14 21:53:12 +00002440 len = -1;
2441 if (*p == '.')
2442 {
2443 key = p + 1;
2444 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2445 ;
2446 if (len == 0)
2447 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002448 if (!quiet)
2449 EMSG(_(e_emptykey));
2450 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002451 }
2452 p = key + len;
2453 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002454 else
2455 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002456 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002457 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002458 if (*p == ':')
2459 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002460 else
2461 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002462 empty1 = FALSE;
2463 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002464 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002465 if (get_tv_string_chk(&var1) == NULL)
2466 {
2467 /* not a number or string */
2468 clear_tv(&var1);
2469 return NULL;
2470 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002471 }
2472
2473 /* Optionally get the second index [ :expr]. */
2474 if (*p == ':')
2475 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002476 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002477 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002478 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002479 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002480 if (!empty1)
2481 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002482 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002483 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002484 if (rettv != NULL && (rettv->v_type != VAR_LIST
2485 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002486 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002487 if (!quiet)
2488 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002489 if (!empty1)
2490 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002491 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002492 }
2493 p = skipwhite(p + 1);
2494 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002495 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002496 else
2497 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002498 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002499 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2500 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002501 if (!empty1)
2502 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002503 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002504 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002505 if (get_tv_string_chk(&var2) == NULL)
2506 {
2507 /* not a number or string */
2508 if (!empty1)
2509 clear_tv(&var1);
2510 clear_tv(&var2);
2511 return NULL;
2512 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002513 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002514 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002515 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002516 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002517 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002518
Bram Moolenaar8c711452005-01-14 21:53:12 +00002519 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002520 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002521 if (!quiet)
2522 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002523 if (!empty1)
2524 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002525 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002526 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002527 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002528 }
2529
2530 /* Skip to past ']'. */
2531 ++p;
2532 }
2533
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002534 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002535 {
2536 if (len == -1)
2537 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002538 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002539 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002540 if (*key == NUL)
2541 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002542 if (!quiet)
2543 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002544 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002545 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002546 }
2547 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002548 lp->ll_list = NULL;
2549 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002550 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002551 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002552 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002553 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002554 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002555 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002556 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002557 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002558 if (len == -1)
2559 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002560 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002561 }
2562 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002563 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002564 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002565 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002566 if (len == -1)
2567 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002568 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002569 p = NULL;
2570 break;
2571 }
2572 if (len == -1)
2573 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002574 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002575 }
2576 else
2577 {
2578 /*
2579 * Get the number and item for the only or first index of the List.
2580 */
2581 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002582 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002583 else
2584 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002585 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002586 clear_tv(&var1);
2587 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002588 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002589 lp->ll_list = lp->ll_tv->vval.v_list;
2590 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2591 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002592 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002593 if (lp->ll_n1 < 0)
2594 {
2595 lp->ll_n1 = 0;
2596 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2597 }
2598 }
2599 if (lp->ll_li == NULL)
2600 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002601 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002602 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002603 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002604 }
2605
2606 /*
2607 * May need to find the item or absolute index for the second
2608 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002609 * When no index given: "lp->ll_empty2" is TRUE.
2610 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002611 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002612 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002613 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002614 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002615 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002616 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002617 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002618 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002619 if (ni == NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002620 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002621 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002622 }
2623
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002624 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2625 if (lp->ll_n1 < 0)
2626 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2627 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002628 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002629 }
2630
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002631 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002632 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002633 }
2634
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002635 return p;
2636}
2637
2638/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002639 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002640 */
2641 static void
2642clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002643 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002644{
2645 vim_free(lp->ll_exp_name);
2646 vim_free(lp->ll_newkey);
2647}
2648
2649/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002650 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002651 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002652 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653 */
2654 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002655set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002656 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002657 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002658 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002659 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002660 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002661{
2662 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002663 listitem_T *ri;
2664 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002665
2666 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002667 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002668 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002669 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002670 cc = *endp;
2671 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002672 if (op != NULL && *op != '=')
2673 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002674 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002675
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002676 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002677 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002678 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002679 {
2680 if (tv_op(&tv, rettv, op) == OK)
2681 set_var(lp->ll_name, &tv, FALSE);
2682 clear_tv(&tv);
2683 }
2684 }
2685 else
2686 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002687 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002688 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002689 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002690 else if (tv_check_lock(lp->ll_newkey == NULL
2691 ? lp->ll_tv->v_lock
2692 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2693 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002694 else if (lp->ll_range)
2695 {
2696 /*
2697 * Assign the List values to the list items.
2698 */
2699 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002700 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002701 if (op != NULL && *op != '=')
2702 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2703 else
2704 {
2705 clear_tv(&lp->ll_li->li_tv);
2706 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2707 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002708 ri = ri->li_next;
2709 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2710 break;
2711 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002712 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002713 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002714 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002715 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002716 ri = NULL;
2717 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002718 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002719 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002720 lp->ll_li = lp->ll_li->li_next;
2721 ++lp->ll_n1;
2722 }
2723 if (ri != NULL)
2724 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002725 else if (lp->ll_empty2
2726 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002727 : lp->ll_n1 != lp->ll_n2)
2728 EMSG(_("E711: List value has not enough items"));
2729 }
2730 else
2731 {
2732 /*
2733 * Assign to a List or Dictionary item.
2734 */
2735 if (lp->ll_newkey != NULL)
2736 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002737 if (op != NULL && *op != '=')
2738 {
2739 EMSG2(_(e_letwrong), op);
2740 return;
2741 }
2742
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002743 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002744 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002745 if (di == NULL)
2746 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002747 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2748 {
2749 vim_free(di);
2750 return;
2751 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002752 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002753 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002754 else if (op != NULL && *op != '=')
2755 {
2756 tv_op(lp->ll_tv, rettv, op);
2757 return;
2758 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002759 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002760 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002761
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002762 /*
2763 * Assign the value to the variable or list item.
2764 */
2765 if (copy)
2766 copy_tv(rettv, lp->ll_tv);
2767 else
2768 {
2769 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002770 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002771 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002772 }
2773 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002774}
2775
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002776/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002777 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2778 * Returns OK or FAIL.
2779 */
2780 static int
2781tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002782 typval_T *tv1;
2783 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002784 char_u *op;
2785{
2786 long n;
2787 char_u numbuf[NUMBUFLEN];
2788 char_u *s;
2789
2790 /* Can't do anything with a Funcref or a Dict on the right. */
2791 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2792 {
2793 switch (tv1->v_type)
2794 {
2795 case VAR_DICT:
2796 case VAR_FUNC:
2797 break;
2798
2799 case VAR_LIST:
2800 if (*op != '+' || tv2->v_type != VAR_LIST)
2801 break;
2802 /* List += List */
2803 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2804 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2805 return OK;
2806
2807 case VAR_NUMBER:
2808 case VAR_STRING:
2809 if (tv2->v_type == VAR_LIST)
2810 break;
2811 if (*op == '+' || *op == '-')
2812 {
2813 /* nr += nr or nr -= nr*/
2814 n = get_tv_number(tv1);
2815 if (*op == '+')
2816 n += get_tv_number(tv2);
2817 else
2818 n -= get_tv_number(tv2);
2819 clear_tv(tv1);
2820 tv1->v_type = VAR_NUMBER;
2821 tv1->vval.v_number = n;
2822 }
2823 else
2824 {
2825 /* str .= str */
2826 s = get_tv_string(tv1);
2827 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2828 clear_tv(tv1);
2829 tv1->v_type = VAR_STRING;
2830 tv1->vval.v_string = s;
2831 }
2832 return OK;
2833 }
2834 }
2835
2836 EMSG2(_(e_letwrong), op);
2837 return FAIL;
2838}
2839
2840/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002841 * Add a watcher to a list.
2842 */
2843 static void
2844list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002845 list_T *l;
2846 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002847{
2848 lw->lw_next = l->lv_watch;
2849 l->lv_watch = lw;
2850}
2851
2852/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002853 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002854 * No warning when it isn't found...
2855 */
2856 static void
2857list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002858 list_T *l;
2859 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002860{
Bram Moolenaar33570922005-01-25 22:26:29 +00002861 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002862
2863 lwp = &l->lv_watch;
2864 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2865 {
2866 if (lw == lwrem)
2867 {
2868 *lwp = lw->lw_next;
2869 break;
2870 }
2871 lwp = &lw->lw_next;
2872 }
2873}
2874
2875/*
2876 * Just before removing an item from a list: advance watchers to the next
2877 * item.
2878 */
2879 static void
2880list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002881 list_T *l;
2882 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002883{
Bram Moolenaar33570922005-01-25 22:26:29 +00002884 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002885
2886 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2887 if (lw->lw_item == item)
2888 lw->lw_item = item->li_next;
2889}
2890
2891/*
2892 * Evaluate the expression used in a ":for var in expr" command.
2893 * "arg" points to "var".
2894 * Set "*errp" to TRUE for an error, FALSE otherwise;
2895 * Return a pointer that holds the info. Null when there is an error.
2896 */
2897 void *
2898eval_for_line(arg, errp, nextcmdp, skip)
2899 char_u *arg;
2900 int *errp;
2901 char_u **nextcmdp;
2902 int skip;
2903{
Bram Moolenaar33570922005-01-25 22:26:29 +00002904 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002905 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002906 typval_T tv;
2907 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002908
2909 *errp = TRUE; /* default: there is an error */
2910
Bram Moolenaar33570922005-01-25 22:26:29 +00002911 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002912 if (fi == NULL)
2913 return NULL;
2914
2915 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2916 if (expr == NULL)
2917 return fi;
2918
2919 expr = skipwhite(expr);
2920 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2921 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002922 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002923 return fi;
2924 }
2925
2926 if (skip)
2927 ++emsg_skip;
2928 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2929 {
2930 *errp = FALSE;
2931 if (!skip)
2932 {
2933 l = tv.vval.v_list;
2934 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002935 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002936 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002937 clear_tv(&tv);
2938 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002939 else
2940 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002941 /* No need to increment the refcount, it's already set for the
2942 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002943 fi->fi_list = l;
2944 list_add_watch(l, &fi->fi_lw);
2945 fi->fi_lw.lw_item = l->lv_first;
2946 }
2947 }
2948 }
2949 if (skip)
2950 --emsg_skip;
2951
2952 return fi;
2953}
2954
2955/*
2956 * Use the first item in a ":for" list. Advance to the next.
2957 * Assign the values to the variable (list). "arg" points to the first one.
2958 * Return TRUE when a valid item was found, FALSE when at end of list or
2959 * something wrong.
2960 */
2961 int
2962next_for_item(fi_void, arg)
2963 void *fi_void;
2964 char_u *arg;
2965{
Bram Moolenaar33570922005-01-25 22:26:29 +00002966 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002967 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002968 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002969
2970 item = fi->fi_lw.lw_item;
2971 if (item == NULL)
2972 result = FALSE;
2973 else
2974 {
2975 fi->fi_lw.lw_item = item->li_next;
2976 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2977 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2978 }
2979 return result;
2980}
2981
2982/*
2983 * Free the structure used to store info used by ":for".
2984 */
2985 void
2986free_for_info(fi_void)
2987 void *fi_void;
2988{
Bram Moolenaar33570922005-01-25 22:26:29 +00002989 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002990
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002991 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002992 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002993 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002994 list_unref(fi->fi_list);
2995 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002996 vim_free(fi);
2997}
2998
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3000
3001 void
3002set_context_for_expression(xp, arg, cmdidx)
3003 expand_T *xp;
3004 char_u *arg;
3005 cmdidx_T cmdidx;
3006{
3007 int got_eq = FALSE;
3008 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003009 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003011 if (cmdidx == CMD_let)
3012 {
3013 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003014 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003015 {
3016 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003017 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003018 {
3019 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003020 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003021 if (vim_iswhite(*p))
3022 break;
3023 }
3024 return;
3025 }
3026 }
3027 else
3028 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3029 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030 while ((xp->xp_pattern = vim_strpbrk(arg,
3031 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3032 {
3033 c = *xp->xp_pattern;
3034 if (c == '&')
3035 {
3036 c = xp->xp_pattern[1];
3037 if (c == '&')
3038 {
3039 ++xp->xp_pattern;
3040 xp->xp_context = cmdidx != CMD_let || got_eq
3041 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3042 }
3043 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003044 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003046 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3047 xp->xp_pattern += 2;
3048
3049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 }
3051 else if (c == '$')
3052 {
3053 /* environment variable */
3054 xp->xp_context = EXPAND_ENV_VARS;
3055 }
3056 else if (c == '=')
3057 {
3058 got_eq = TRUE;
3059 xp->xp_context = EXPAND_EXPRESSION;
3060 }
3061 else if (c == '<'
3062 && xp->xp_context == EXPAND_FUNCTIONS
3063 && vim_strchr(xp->xp_pattern, '(') == NULL)
3064 {
3065 /* Function name can start with "<SNR>" */
3066 break;
3067 }
3068 else if (cmdidx != CMD_let || got_eq)
3069 {
3070 if (c == '"') /* string */
3071 {
3072 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3073 if (c == '\\' && xp->xp_pattern[1] != NUL)
3074 ++xp->xp_pattern;
3075 xp->xp_context = EXPAND_NOTHING;
3076 }
3077 else if (c == '\'') /* literal string */
3078 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003079 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3081 /* skip */ ;
3082 xp->xp_context = EXPAND_NOTHING;
3083 }
3084 else if (c == '|')
3085 {
3086 if (xp->xp_pattern[1] == '|')
3087 {
3088 ++xp->xp_pattern;
3089 xp->xp_context = EXPAND_EXPRESSION;
3090 }
3091 else
3092 xp->xp_context = EXPAND_COMMANDS;
3093 }
3094 else
3095 xp->xp_context = EXPAND_EXPRESSION;
3096 }
3097 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003098 /* Doesn't look like something valid, expand as an expression
3099 * anyway. */
3100 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 arg = xp->xp_pattern;
3102 if (*arg != NUL)
3103 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3104 /* skip */ ;
3105 }
3106 xp->xp_pattern = arg;
3107}
3108
3109#endif /* FEAT_CMDL_COMPL */
3110
3111/*
3112 * ":1,25call func(arg1, arg2)" function call.
3113 */
3114 void
3115ex_call(eap)
3116 exarg_T *eap;
3117{
3118 char_u *arg = eap->arg;
3119 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003121 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003123 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 linenr_T lnum;
3125 int doesrange;
3126 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003127 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003129 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003130 if (fudi.fd_newkey != NULL)
3131 {
3132 /* Still need to give an error message for missing key. */
3133 EMSG2(_(e_dictkey), fudi.fd_newkey);
3134 vim_free(fudi.fd_newkey);
3135 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003136 if (tofree == NULL)
3137 return;
3138
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003139 /* Increase refcount on dictionary, it could get deleted when evaluating
3140 * the arguments. */
3141 if (fudi.fd_dict != NULL)
3142 ++fudi.fd_dict->dv_refcount;
3143
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003144 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003145 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003146 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147
Bram Moolenaar532c7802005-01-27 14:44:31 +00003148 /* Skip white space to allow ":call func ()". Not good, but required for
3149 * backward compatibility. */
3150 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003151 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152
3153 if (*startarg != '(')
3154 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003155 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 goto end;
3157 }
3158
3159 /*
3160 * When skipping, evaluate the function once, to find the end of the
3161 * arguments.
3162 * When the function takes a range, this is discovered after the first
3163 * call, and the loop is broken.
3164 */
3165 if (eap->skip)
3166 {
3167 ++emsg_skip;
3168 lnum = eap->line2; /* do it once, also with an invalid range */
3169 }
3170 else
3171 lnum = eap->line1;
3172 for ( ; lnum <= eap->line2; ++lnum)
3173 {
3174 if (!eap->skip && eap->addr_count > 0)
3175 {
3176 curwin->w_cursor.lnum = lnum;
3177 curwin->w_cursor.col = 0;
3178 }
3179 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003180 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003181 eap->line1, eap->line2, &doesrange,
3182 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 {
3184 failed = TRUE;
3185 break;
3186 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003187 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 if (doesrange || eap->skip)
3189 break;
3190 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003191 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003192 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003193 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 if (aborting())
3195 break;
3196 }
3197 if (eap->skip)
3198 --emsg_skip;
3199
3200 if (!failed)
3201 {
3202 /* Check for trailing illegal characters and a following command. */
3203 if (!ends_excmd(*arg))
3204 {
3205 emsg_severe = TRUE;
3206 EMSG(_(e_trailing));
3207 }
3208 else
3209 eap->nextcmd = check_nextcmd(arg);
3210 }
3211
3212end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003213 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003214 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215}
3216
3217/*
3218 * ":unlet[!] var1 ... " command.
3219 */
3220 void
3221ex_unlet(eap)
3222 exarg_T *eap;
3223{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003224 ex_unletlock(eap, eap->arg, 0);
3225}
3226
3227/*
3228 * ":lockvar" and ":unlockvar" commands
3229 */
3230 void
3231ex_lockvar(eap)
3232 exarg_T *eap;
3233{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003235 int deep = 2;
3236
3237 if (eap->forceit)
3238 deep = -1;
3239 else if (vim_isdigit(*arg))
3240 {
3241 deep = getdigits(&arg);
3242 arg = skipwhite(arg);
3243 }
3244
3245 ex_unletlock(eap, arg, deep);
3246}
3247
3248/*
3249 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3250 */
3251 static void
3252ex_unletlock(eap, argstart, deep)
3253 exarg_T *eap;
3254 char_u *argstart;
3255 int deep;
3256{
3257 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003260 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261
3262 do
3263 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003264 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003265 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3266 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003267 if (lv.ll_name == NULL)
3268 error = TRUE; /* error but continue parsing */
3269 if (name_end == NULL || (!vim_iswhite(*name_end)
3270 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003272 if (name_end != NULL)
3273 {
3274 emsg_severe = TRUE;
3275 EMSG(_(e_trailing));
3276 }
3277 if (!(eap->skip || error))
3278 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 break;
3280 }
3281
3282 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003283 {
3284 if (eap->cmdidx == CMD_unlet)
3285 {
3286 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3287 error = TRUE;
3288 }
3289 else
3290 {
3291 if (do_lock_var(&lv, name_end, deep,
3292 eap->cmdidx == CMD_lockvar) == FAIL)
3293 error = TRUE;
3294 }
3295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003297 if (!eap->skip)
3298 clear_lval(&lv);
3299
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 arg = skipwhite(name_end);
3301 } while (!ends_excmd(*arg));
3302
3303 eap->nextcmd = check_nextcmd(arg);
3304}
3305
Bram Moolenaar8c711452005-01-14 21:53:12 +00003306 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003307do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003308 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003309 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003310 int forceit;
3311{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003312 int ret = OK;
3313 int cc;
3314
3315 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003316 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003317 cc = *name_end;
3318 *name_end = NUL;
3319
3320 /* Normal name or expanded name. */
3321 if (check_changedtick(lp->ll_name))
3322 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003323 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003324 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003325 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003326 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003327 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3328 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003329 else if (lp->ll_range)
3330 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003331 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003332
3333 /* Delete a range of List items. */
3334 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3335 {
3336 li = lp->ll_li->li_next;
3337 listitem_remove(lp->ll_list, lp->ll_li);
3338 lp->ll_li = li;
3339 ++lp->ll_n1;
3340 }
3341 }
3342 else
3343 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003344 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003345 /* unlet a List item. */
3346 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003347 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003348 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003349 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003350 }
3351
3352 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003353}
3354
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355/*
3356 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003357 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 */
3359 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003360do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003362 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363{
Bram Moolenaar33570922005-01-25 22:26:29 +00003364 hashtab_T *ht;
3365 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003366 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367
Bram Moolenaar33570922005-01-25 22:26:29 +00003368 ht = find_var_ht(name, &varname);
3369 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003371 hi = hash_find(ht, varname);
3372 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003373 {
Bram Moolenaar4e957af2006-09-02 11:41:07 +00003374 if (var_check_fixed(HI2DI(hi)->di_flags, name))
3375 return FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003376 if (var_check_ro(HI2DI(hi)->di_flags, name))
3377 return FAIL;
3378 delete_var(ht, hi);
3379 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003382 if (forceit)
3383 return OK;
3384 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 return FAIL;
3386}
3387
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003388/*
3389 * Lock or unlock variable indicated by "lp".
3390 * "deep" is the levels to go (-1 for unlimited);
3391 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3392 */
3393 static int
3394do_lock_var(lp, name_end, deep, lock)
3395 lval_T *lp;
3396 char_u *name_end;
3397 int deep;
3398 int lock;
3399{
3400 int ret = OK;
3401 int cc;
3402 dictitem_T *di;
3403
3404 if (deep == 0) /* nothing to do */
3405 return OK;
3406
3407 if (lp->ll_tv == NULL)
3408 {
3409 cc = *name_end;
3410 *name_end = NUL;
3411
3412 /* Normal name or expanded name. */
3413 if (check_changedtick(lp->ll_name))
3414 ret = FAIL;
3415 else
3416 {
3417 di = find_var(lp->ll_name, NULL);
3418 if (di == NULL)
3419 ret = FAIL;
3420 else
3421 {
3422 if (lock)
3423 di->di_flags |= DI_FLAGS_LOCK;
3424 else
3425 di->di_flags &= ~DI_FLAGS_LOCK;
3426 item_lock(&di->di_tv, deep, lock);
3427 }
3428 }
3429 *name_end = cc;
3430 }
3431 else if (lp->ll_range)
3432 {
3433 listitem_T *li = lp->ll_li;
3434
3435 /* (un)lock a range of List items. */
3436 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3437 {
3438 item_lock(&li->li_tv, deep, lock);
3439 li = li->li_next;
3440 ++lp->ll_n1;
3441 }
3442 }
3443 else if (lp->ll_list != NULL)
3444 /* (un)lock a List item. */
3445 item_lock(&lp->ll_li->li_tv, deep, lock);
3446 else
3447 /* un(lock) a Dictionary item. */
3448 item_lock(&lp->ll_di->di_tv, deep, lock);
3449
3450 return ret;
3451}
3452
3453/*
3454 * Lock or unlock an item. "deep" is nr of levels to go.
3455 */
3456 static void
3457item_lock(tv, deep, lock)
3458 typval_T *tv;
3459 int deep;
3460 int lock;
3461{
3462 static int recurse = 0;
3463 list_T *l;
3464 listitem_T *li;
3465 dict_T *d;
3466 hashitem_T *hi;
3467 int todo;
3468
3469 if (recurse >= DICT_MAXNEST)
3470 {
3471 EMSG(_("E743: variable nested too deep for (un)lock"));
3472 return;
3473 }
3474 if (deep == 0)
3475 return;
3476 ++recurse;
3477
3478 /* lock/unlock the item itself */
3479 if (lock)
3480 tv->v_lock |= VAR_LOCKED;
3481 else
3482 tv->v_lock &= ~VAR_LOCKED;
3483
3484 switch (tv->v_type)
3485 {
3486 case VAR_LIST:
3487 if ((l = tv->vval.v_list) != NULL)
3488 {
3489 if (lock)
3490 l->lv_lock |= VAR_LOCKED;
3491 else
3492 l->lv_lock &= ~VAR_LOCKED;
3493 if (deep < 0 || deep > 1)
3494 /* recursive: lock/unlock the items the List contains */
3495 for (li = l->lv_first; li != NULL; li = li->li_next)
3496 item_lock(&li->li_tv, deep - 1, lock);
3497 }
3498 break;
3499 case VAR_DICT:
3500 if ((d = tv->vval.v_dict) != NULL)
3501 {
3502 if (lock)
3503 d->dv_lock |= VAR_LOCKED;
3504 else
3505 d->dv_lock &= ~VAR_LOCKED;
3506 if (deep < 0 || deep > 1)
3507 {
3508 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003509 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003510 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3511 {
3512 if (!HASHITEM_EMPTY(hi))
3513 {
3514 --todo;
3515 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3516 }
3517 }
3518 }
3519 }
3520 }
3521 --recurse;
3522}
3523
Bram Moolenaara40058a2005-07-11 22:42:07 +00003524/*
3525 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3526 * it refers to a List or Dictionary that is locked.
3527 */
3528 static int
3529tv_islocked(tv)
3530 typval_T *tv;
3531{
3532 return (tv->v_lock & VAR_LOCKED)
3533 || (tv->v_type == VAR_LIST
3534 && tv->vval.v_list != NULL
3535 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3536 || (tv->v_type == VAR_DICT
3537 && tv->vval.v_dict != NULL
3538 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3539}
3540
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3542/*
3543 * Delete all "menutrans_" variables.
3544 */
3545 void
3546del_menutrans_vars()
3547{
Bram Moolenaar33570922005-01-25 22:26:29 +00003548 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003549 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550
Bram Moolenaar33570922005-01-25 22:26:29 +00003551 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003552 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003553 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003554 {
3555 if (!HASHITEM_EMPTY(hi))
3556 {
3557 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003558 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3559 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003560 }
3561 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003562 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563}
3564#endif
3565
3566#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3567
3568/*
3569 * Local string buffer for the next two functions to store a variable name
3570 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3571 * get_user_var_name().
3572 */
3573
3574static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3575
3576static char_u *varnamebuf = NULL;
3577static int varnamebuflen = 0;
3578
3579/*
3580 * Function to concatenate a prefix and a variable name.
3581 */
3582 static char_u *
3583cat_prefix_varname(prefix, name)
3584 int prefix;
3585 char_u *name;
3586{
3587 int len;
3588
3589 len = (int)STRLEN(name) + 3;
3590 if (len > varnamebuflen)
3591 {
3592 vim_free(varnamebuf);
3593 len += 10; /* some additional space */
3594 varnamebuf = alloc(len);
3595 if (varnamebuf == NULL)
3596 {
3597 varnamebuflen = 0;
3598 return NULL;
3599 }
3600 varnamebuflen = len;
3601 }
3602 *varnamebuf = prefix;
3603 varnamebuf[1] = ':';
3604 STRCPY(varnamebuf + 2, name);
3605 return varnamebuf;
3606}
3607
3608/*
3609 * Function given to ExpandGeneric() to obtain the list of user defined
3610 * (global/buffer/window/built-in) variable names.
3611 */
3612/*ARGSUSED*/
3613 char_u *
3614get_user_var_name(xp, idx)
3615 expand_T *xp;
3616 int idx;
3617{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003618 static long_u gdone;
3619 static long_u bdone;
3620 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003621#ifdef FEAT_WINDOWS
3622 static long_u tdone;
3623#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003624 static int vidx;
3625 static hashitem_T *hi;
3626 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627
3628 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003629 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003630 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003631#ifdef FEAT_WINDOWS
3632 tdone = 0;
3633#endif
3634 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003635
3636 /* Global variables */
3637 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003639 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003640 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003641 else
3642 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003643 while (HASHITEM_EMPTY(hi))
3644 ++hi;
3645 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3646 return cat_prefix_varname('g', hi->hi_key);
3647 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003649
3650 /* b: variables */
3651 ht = &curbuf->b_vars.dv_hashtab;
3652 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003654 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003655 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003656 else
3657 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003658 while (HASHITEM_EMPTY(hi))
3659 ++hi;
3660 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003662 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003664 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 return (char_u *)"b:changedtick";
3666 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003667
3668 /* w: variables */
3669 ht = &curwin->w_vars.dv_hashtab;
3670 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003672 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003673 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003674 else
3675 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003676 while (HASHITEM_EMPTY(hi))
3677 ++hi;
3678 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003680
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003681#ifdef FEAT_WINDOWS
3682 /* t: variables */
3683 ht = &curtab->tp_vars.dv_hashtab;
3684 if (tdone < ht->ht_used)
3685 {
3686 if (tdone++ == 0)
3687 hi = ht->ht_array;
3688 else
3689 ++hi;
3690 while (HASHITEM_EMPTY(hi))
3691 ++hi;
3692 return cat_prefix_varname('t', hi->hi_key);
3693 }
3694#endif
3695
Bram Moolenaar33570922005-01-25 22:26:29 +00003696 /* v: variables */
3697 if (vidx < VV_LEN)
3698 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699
3700 vim_free(varnamebuf);
3701 varnamebuf = NULL;
3702 varnamebuflen = 0;
3703 return NULL;
3704}
3705
3706#endif /* FEAT_CMDL_COMPL */
3707
3708/*
3709 * types for expressions.
3710 */
3711typedef enum
3712{
3713 TYPE_UNKNOWN = 0
3714 , TYPE_EQUAL /* == */
3715 , TYPE_NEQUAL /* != */
3716 , TYPE_GREATER /* > */
3717 , TYPE_GEQUAL /* >= */
3718 , TYPE_SMALLER /* < */
3719 , TYPE_SEQUAL /* <= */
3720 , TYPE_MATCH /* =~ */
3721 , TYPE_NOMATCH /* !~ */
3722} exptype_T;
3723
3724/*
3725 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003726 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3728 */
3729
3730/*
3731 * Handle zero level expression.
3732 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003733 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003734 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 * Return OK or FAIL.
3736 */
3737 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003738eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003740 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 char_u **nextcmd;
3742 int evaluate;
3743{
3744 int ret;
3745 char_u *p;
3746
3747 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003748 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 if (ret == FAIL || !ends_excmd(*p))
3750 {
3751 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003752 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 /*
3754 * Report the invalid expression unless the expression evaluation has
3755 * been cancelled due to an aborting error, an interrupt, or an
3756 * exception.
3757 */
3758 if (!aborting())
3759 EMSG2(_(e_invexpr2), arg);
3760 ret = FAIL;
3761 }
3762 if (nextcmd != NULL)
3763 *nextcmd = check_nextcmd(p);
3764
3765 return ret;
3766}
3767
3768/*
3769 * Handle top level expression:
3770 * expr1 ? expr0 : expr0
3771 *
3772 * "arg" must point to the first non-white of the expression.
3773 * "arg" is advanced to the next non-white after the recognized expression.
3774 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003775 * Note: "rettv.v_lock" is not set.
3776 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 * Return OK or FAIL.
3778 */
3779 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003780eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003782 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 int evaluate;
3784{
3785 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003786 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787
3788 /*
3789 * Get the first variable.
3790 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003791 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 return FAIL;
3793
3794 if ((*arg)[0] == '?')
3795 {
3796 result = FALSE;
3797 if (evaluate)
3798 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003799 int error = FALSE;
3800
3801 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003803 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003804 if (error)
3805 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 }
3807
3808 /*
3809 * Get the second variable.
3810 */
3811 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003812 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 return FAIL;
3814
3815 /*
3816 * Check for the ":".
3817 */
3818 if ((*arg)[0] != ':')
3819 {
3820 EMSG(_("E109: Missing ':' after '?'"));
3821 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003822 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 return FAIL;
3824 }
3825
3826 /*
3827 * Get the third variable.
3828 */
3829 *arg = skipwhite(*arg + 1);
3830 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3831 {
3832 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003833 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 return FAIL;
3835 }
3836 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003837 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 }
3839
3840 return OK;
3841}
3842
3843/*
3844 * Handle first level expression:
3845 * expr2 || expr2 || expr2 logical OR
3846 *
3847 * "arg" must point to the first non-white of the expression.
3848 * "arg" is advanced to the next non-white after the recognized expression.
3849 *
3850 * Return OK or FAIL.
3851 */
3852 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003853eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003855 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 int evaluate;
3857{
Bram Moolenaar33570922005-01-25 22:26:29 +00003858 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 long result;
3860 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003861 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862
3863 /*
3864 * Get the first variable.
3865 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003866 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 return FAIL;
3868
3869 /*
3870 * Repeat until there is no following "||".
3871 */
3872 first = TRUE;
3873 result = FALSE;
3874 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3875 {
3876 if (evaluate && first)
3877 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003878 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003880 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003881 if (error)
3882 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 first = FALSE;
3884 }
3885
3886 /*
3887 * Get the second variable.
3888 */
3889 *arg = skipwhite(*arg + 2);
3890 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3891 return FAIL;
3892
3893 /*
3894 * Compute the result.
3895 */
3896 if (evaluate && !result)
3897 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003898 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003900 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003901 if (error)
3902 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 }
3904 if (evaluate)
3905 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003906 rettv->v_type = VAR_NUMBER;
3907 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 }
3909 }
3910
3911 return OK;
3912}
3913
3914/*
3915 * Handle second level expression:
3916 * expr3 && expr3 && expr3 logical AND
3917 *
3918 * "arg" must point to the first non-white of the expression.
3919 * "arg" is advanced to the next non-white after the recognized expression.
3920 *
3921 * Return OK or FAIL.
3922 */
3923 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003924eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003926 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 int evaluate;
3928{
Bram Moolenaar33570922005-01-25 22:26:29 +00003929 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 long result;
3931 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003932 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933
3934 /*
3935 * Get the first variable.
3936 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003937 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 return FAIL;
3939
3940 /*
3941 * Repeat until there is no following "&&".
3942 */
3943 first = TRUE;
3944 result = TRUE;
3945 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3946 {
3947 if (evaluate && first)
3948 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003949 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003951 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003952 if (error)
3953 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 first = FALSE;
3955 }
3956
3957 /*
3958 * Get the second variable.
3959 */
3960 *arg = skipwhite(*arg + 2);
3961 if (eval4(arg, &var2, evaluate && result) == FAIL)
3962 return FAIL;
3963
3964 /*
3965 * Compute the result.
3966 */
3967 if (evaluate && result)
3968 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003969 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003971 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003972 if (error)
3973 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 }
3975 if (evaluate)
3976 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003977 rettv->v_type = VAR_NUMBER;
3978 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 }
3980 }
3981
3982 return OK;
3983}
3984
3985/*
3986 * Handle third level expression:
3987 * var1 == var2
3988 * var1 =~ var2
3989 * var1 != var2
3990 * var1 !~ var2
3991 * var1 > var2
3992 * var1 >= var2
3993 * var1 < var2
3994 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003995 * var1 is var2
3996 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 *
3998 * "arg" must point to the first non-white of the expression.
3999 * "arg" is advanced to the next non-white after the recognized expression.
4000 *
4001 * Return OK or FAIL.
4002 */
4003 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004004eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004006 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 int evaluate;
4008{
Bram Moolenaar33570922005-01-25 22:26:29 +00004009 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 char_u *p;
4011 int i;
4012 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004013 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 int len = 2;
4015 long n1, n2;
4016 char_u *s1, *s2;
4017 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4018 regmatch_T regmatch;
4019 int ic;
4020 char_u *save_cpo;
4021
4022 /*
4023 * Get the first variable.
4024 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004025 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 return FAIL;
4027
4028 p = *arg;
4029 switch (p[0])
4030 {
4031 case '=': if (p[1] == '=')
4032 type = TYPE_EQUAL;
4033 else if (p[1] == '~')
4034 type = TYPE_MATCH;
4035 break;
4036 case '!': if (p[1] == '=')
4037 type = TYPE_NEQUAL;
4038 else if (p[1] == '~')
4039 type = TYPE_NOMATCH;
4040 break;
4041 case '>': if (p[1] != '=')
4042 {
4043 type = TYPE_GREATER;
4044 len = 1;
4045 }
4046 else
4047 type = TYPE_GEQUAL;
4048 break;
4049 case '<': if (p[1] != '=')
4050 {
4051 type = TYPE_SMALLER;
4052 len = 1;
4053 }
4054 else
4055 type = TYPE_SEQUAL;
4056 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004057 case 'i': if (p[1] == 's')
4058 {
4059 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4060 len = 5;
4061 if (!vim_isIDc(p[len]))
4062 {
4063 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4064 type_is = TRUE;
4065 }
4066 }
4067 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 }
4069
4070 /*
4071 * If there is a comparitive operator, use it.
4072 */
4073 if (type != TYPE_UNKNOWN)
4074 {
4075 /* extra question mark appended: ignore case */
4076 if (p[len] == '?')
4077 {
4078 ic = TRUE;
4079 ++len;
4080 }
4081 /* extra '#' appended: match case */
4082 else if (p[len] == '#')
4083 {
4084 ic = FALSE;
4085 ++len;
4086 }
4087 /* nothing appened: use 'ignorecase' */
4088 else
4089 ic = p_ic;
4090
4091 /*
4092 * Get the second variable.
4093 */
4094 *arg = skipwhite(p + len);
4095 if (eval5(arg, &var2, evaluate) == FAIL)
4096 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004097 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 return FAIL;
4099 }
4100
4101 if (evaluate)
4102 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004103 if (type_is && rettv->v_type != var2.v_type)
4104 {
4105 /* For "is" a different type always means FALSE, for "notis"
4106 * it means TRUE. */
4107 n1 = (type == TYPE_NEQUAL);
4108 }
4109 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4110 {
4111 if (type_is)
4112 {
4113 n1 = (rettv->v_type == var2.v_type
4114 && rettv->vval.v_list == var2.vval.v_list);
4115 if (type == TYPE_NEQUAL)
4116 n1 = !n1;
4117 }
4118 else if (rettv->v_type != var2.v_type
4119 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4120 {
4121 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004122 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004123 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004124 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004125 clear_tv(rettv);
4126 clear_tv(&var2);
4127 return FAIL;
4128 }
4129 else
4130 {
4131 /* Compare two Lists for being equal or unequal. */
4132 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4133 if (type == TYPE_NEQUAL)
4134 n1 = !n1;
4135 }
4136 }
4137
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004138 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4139 {
4140 if (type_is)
4141 {
4142 n1 = (rettv->v_type == var2.v_type
4143 && rettv->vval.v_dict == var2.vval.v_dict);
4144 if (type == TYPE_NEQUAL)
4145 n1 = !n1;
4146 }
4147 else if (rettv->v_type != var2.v_type
4148 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4149 {
4150 if (rettv->v_type != var2.v_type)
4151 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4152 else
4153 EMSG(_("E736: Invalid operation for Dictionary"));
4154 clear_tv(rettv);
4155 clear_tv(&var2);
4156 return FAIL;
4157 }
4158 else
4159 {
4160 /* Compare two Dictionaries for being equal or unequal. */
4161 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4162 if (type == TYPE_NEQUAL)
4163 n1 = !n1;
4164 }
4165 }
4166
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004167 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4168 {
4169 if (rettv->v_type != var2.v_type
4170 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4171 {
4172 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004173 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004174 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004175 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004176 clear_tv(rettv);
4177 clear_tv(&var2);
4178 return FAIL;
4179 }
4180 else
4181 {
4182 /* Compare two Funcrefs for being equal or unequal. */
4183 if (rettv->vval.v_string == NULL
4184 || var2.vval.v_string == NULL)
4185 n1 = FALSE;
4186 else
4187 n1 = STRCMP(rettv->vval.v_string,
4188 var2.vval.v_string) == 0;
4189 if (type == TYPE_NEQUAL)
4190 n1 = !n1;
4191 }
4192 }
4193
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 /*
4195 * If one of the two variables is a number, compare as a number.
4196 * When using "=~" or "!~", always compare as string.
4197 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004198 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4200 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004201 n1 = get_tv_number(rettv);
4202 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 switch (type)
4204 {
4205 case TYPE_EQUAL: n1 = (n1 == n2); break;
4206 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4207 case TYPE_GREATER: n1 = (n1 > n2); break;
4208 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4209 case TYPE_SMALLER: n1 = (n1 < n2); break;
4210 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4211 case TYPE_UNKNOWN:
4212 case TYPE_MATCH:
4213 case TYPE_NOMATCH: break; /* avoid gcc warning */
4214 }
4215 }
4216 else
4217 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004218 s1 = get_tv_string_buf(rettv, buf1);
4219 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4221 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4222 else
4223 i = 0;
4224 n1 = FALSE;
4225 switch (type)
4226 {
4227 case TYPE_EQUAL: n1 = (i == 0); break;
4228 case TYPE_NEQUAL: n1 = (i != 0); break;
4229 case TYPE_GREATER: n1 = (i > 0); break;
4230 case TYPE_GEQUAL: n1 = (i >= 0); break;
4231 case TYPE_SMALLER: n1 = (i < 0); break;
4232 case TYPE_SEQUAL: n1 = (i <= 0); break;
4233
4234 case TYPE_MATCH:
4235 case TYPE_NOMATCH:
4236 /* avoid 'l' flag in 'cpoptions' */
4237 save_cpo = p_cpo;
4238 p_cpo = (char_u *)"";
4239 regmatch.regprog = vim_regcomp(s2,
4240 RE_MAGIC + RE_STRING);
4241 regmatch.rm_ic = ic;
4242 if (regmatch.regprog != NULL)
4243 {
4244 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4245 vim_free(regmatch.regprog);
4246 if (type == TYPE_NOMATCH)
4247 n1 = !n1;
4248 }
4249 p_cpo = save_cpo;
4250 break;
4251
4252 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4253 }
4254 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004255 clear_tv(rettv);
4256 clear_tv(&var2);
4257 rettv->v_type = VAR_NUMBER;
4258 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 }
4260 }
4261
4262 return OK;
4263}
4264
4265/*
4266 * Handle fourth level expression:
4267 * + number addition
4268 * - number subtraction
4269 * . string concatenation
4270 *
4271 * "arg" must point to the first non-white of the expression.
4272 * "arg" is advanced to the next non-white after the recognized expression.
4273 *
4274 * Return OK or FAIL.
4275 */
4276 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004277eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004279 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 int evaluate;
4281{
Bram Moolenaar33570922005-01-25 22:26:29 +00004282 typval_T var2;
4283 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 int op;
4285 long n1, n2;
4286 char_u *s1, *s2;
4287 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4288 char_u *p;
4289
4290 /*
4291 * Get the first variable.
4292 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004293 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 return FAIL;
4295
4296 /*
4297 * Repeat computing, until no '+', '-' or '.' is following.
4298 */
4299 for (;;)
4300 {
4301 op = **arg;
4302 if (op != '+' && op != '-' && op != '.')
4303 break;
4304
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004305 if (op != '+' || rettv->v_type != VAR_LIST)
4306 {
4307 /* For "list + ...", an illegal use of the first operand as
4308 * a number cannot be determined before evaluating the 2nd
4309 * operand: if this is also a list, all is ok.
4310 * For "something . ...", "something - ..." or "non-list + ...",
4311 * we know that the first operand needs to be a string or number
4312 * without evaluating the 2nd operand. So check before to avoid
4313 * side effects after an error. */
4314 if (evaluate && get_tv_string_chk(rettv) == NULL)
4315 {
4316 clear_tv(rettv);
4317 return FAIL;
4318 }
4319 }
4320
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 /*
4322 * Get the second variable.
4323 */
4324 *arg = skipwhite(*arg + 1);
4325 if (eval6(arg, &var2, evaluate) == FAIL)
4326 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004327 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 return FAIL;
4329 }
4330
4331 if (evaluate)
4332 {
4333 /*
4334 * Compute the result.
4335 */
4336 if (op == '.')
4337 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004338 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4339 s2 = get_tv_string_buf_chk(&var2, buf2);
4340 if (s2 == NULL) /* type error ? */
4341 {
4342 clear_tv(rettv);
4343 clear_tv(&var2);
4344 return FAIL;
4345 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004346 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004347 clear_tv(rettv);
4348 rettv->v_type = VAR_STRING;
4349 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004351 else if (op == '+' && rettv->v_type == VAR_LIST
4352 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004353 {
4354 /* concatenate Lists */
4355 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4356 &var3) == FAIL)
4357 {
4358 clear_tv(rettv);
4359 clear_tv(&var2);
4360 return FAIL;
4361 }
4362 clear_tv(rettv);
4363 *rettv = var3;
4364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 else
4366 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004367 int error = FALSE;
4368
4369 n1 = get_tv_number_chk(rettv, &error);
4370 if (error)
4371 {
4372 /* This can only happen for "list + non-list".
4373 * For "non-list + ..." or "something - ...", we returned
4374 * before evaluating the 2nd operand. */
4375 clear_tv(rettv);
4376 return FAIL;
4377 }
4378 n2 = get_tv_number_chk(&var2, &error);
4379 if (error)
4380 {
4381 clear_tv(rettv);
4382 clear_tv(&var2);
4383 return FAIL;
4384 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004385 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 if (op == '+')
4387 n1 = n1 + n2;
4388 else
4389 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004390 rettv->v_type = VAR_NUMBER;
4391 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004393 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 }
4395 }
4396 return OK;
4397}
4398
4399/*
4400 * Handle fifth level expression:
4401 * * number multiplication
4402 * / number division
4403 * % number modulo
4404 *
4405 * "arg" must point to the first non-white of the expression.
4406 * "arg" is advanced to the next non-white after the recognized expression.
4407 *
4408 * Return OK or FAIL.
4409 */
4410 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004411eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004413 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 int evaluate;
4415{
Bram Moolenaar33570922005-01-25 22:26:29 +00004416 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 int op;
4418 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004419 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420
4421 /*
4422 * Get the first variable.
4423 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004424 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 return FAIL;
4426
4427 /*
4428 * Repeat computing, until no '*', '/' or '%' is following.
4429 */
4430 for (;;)
4431 {
4432 op = **arg;
4433 if (op != '*' && op != '/' && op != '%')
4434 break;
4435
4436 if (evaluate)
4437 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004438 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004439 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004440 if (error)
4441 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 }
4443 else
4444 n1 = 0;
4445
4446 /*
4447 * Get the second variable.
4448 */
4449 *arg = skipwhite(*arg + 1);
4450 if (eval7(arg, &var2, evaluate) == FAIL)
4451 return FAIL;
4452
4453 if (evaluate)
4454 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004455 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004456 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004457 if (error)
4458 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459
4460 /*
4461 * Compute the result.
4462 */
4463 if (op == '*')
4464 n1 = n1 * n2;
4465 else if (op == '/')
4466 {
4467 if (n2 == 0) /* give an error message? */
4468 n1 = 0x7fffffffL;
4469 else
4470 n1 = n1 / n2;
4471 }
4472 else
4473 {
4474 if (n2 == 0) /* give an error message? */
4475 n1 = 0;
4476 else
4477 n1 = n1 % n2;
4478 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004479 rettv->v_type = VAR_NUMBER;
4480 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 }
4482 }
4483
4484 return OK;
4485}
4486
4487/*
4488 * Handle sixth level expression:
4489 * number number constant
4490 * "string" string contstant
4491 * 'string' literal string contstant
4492 * &option-name option value
4493 * @r register contents
4494 * identifier variable value
4495 * function() function call
4496 * $VAR environment variable
4497 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004498 * [expr, expr] List
4499 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 *
4501 * Also handle:
4502 * ! in front logical NOT
4503 * - in front unary minus
4504 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004505 * trailing [] subscript in String or List
4506 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 *
4508 * "arg" must point to the first non-white of the expression.
4509 * "arg" is advanced to the next non-white after the recognized expression.
4510 *
4511 * Return OK or FAIL.
4512 */
4513 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004514eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004516 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 int evaluate;
4518{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 long n;
4520 int len;
4521 char_u *s;
4522 int val;
4523 char_u *start_leader, *end_leader;
4524 int ret = OK;
4525 char_u *alias;
4526
4527 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004528 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004529 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004531 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532
4533 /*
4534 * Skip '!' and '-' characters. They are handled later.
4535 */
4536 start_leader = *arg;
4537 while (**arg == '!' || **arg == '-' || **arg == '+')
4538 *arg = skipwhite(*arg + 1);
4539 end_leader = *arg;
4540
4541 switch (**arg)
4542 {
4543 /*
4544 * Number constant.
4545 */
4546 case '0':
4547 case '1':
4548 case '2':
4549 case '3':
4550 case '4':
4551 case '5':
4552 case '6':
4553 case '7':
4554 case '8':
4555 case '9':
4556 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4557 *arg += len;
4558 if (evaluate)
4559 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004560 rettv->v_type = VAR_NUMBER;
4561 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 }
4563 break;
4564
4565 /*
4566 * String constant: "string".
4567 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004568 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 break;
4570
4571 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004572 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004574 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004575 break;
4576
4577 /*
4578 * List: [expr, expr]
4579 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004580 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 break;
4582
4583 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004584 * Dictionary: {key: val, key: val}
4585 */
4586 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4587 break;
4588
4589 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004590 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004592 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 break;
4594
4595 /*
4596 * Environment variable: $VAR.
4597 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004598 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 break;
4600
4601 /*
4602 * Register contents: @r.
4603 */
4604 case '@': ++*arg;
4605 if (evaluate)
4606 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004607 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004608 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 }
4610 if (**arg != NUL)
4611 ++*arg;
4612 break;
4613
4614 /*
4615 * nested expression: (expression).
4616 */
4617 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004618 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 if (**arg == ')')
4620 ++*arg;
4621 else if (ret == OK)
4622 {
4623 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004624 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 ret = FAIL;
4626 }
4627 break;
4628
Bram Moolenaar8c711452005-01-14 21:53:12 +00004629 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 break;
4631 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004632
4633 if (ret == NOTDONE)
4634 {
4635 /*
4636 * Must be a variable or function name.
4637 * Can also be a curly-braces kind of name: {expr}.
4638 */
4639 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004640 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004641 if (alias != NULL)
4642 s = alias;
4643
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004644 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004645 ret = FAIL;
4646 else
4647 {
4648 if (**arg == '(') /* recursive! */
4649 {
4650 /* If "s" is the name of a variable of type VAR_FUNC
4651 * use its contents. */
4652 s = deref_func_name(s, &len);
4653
4654 /* Invoke the function. */
4655 ret = get_func_tv(s, len, rettv, arg,
4656 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004657 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004658 /* Stop the expression evaluation when immediately
4659 * aborting on error, or when an interrupt occurred or
4660 * an exception was thrown but not caught. */
4661 if (aborting())
4662 {
4663 if (ret == OK)
4664 clear_tv(rettv);
4665 ret = FAIL;
4666 }
4667 }
4668 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004669 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004670 else
4671 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004672 }
4673
4674 if (alias != NULL)
4675 vim_free(alias);
4676 }
4677
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 *arg = skipwhite(*arg);
4679
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004680 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4681 * expr(expr). */
4682 if (ret == OK)
4683 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684
4685 /*
4686 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4687 */
4688 if (ret == OK && evaluate && end_leader > start_leader)
4689 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004690 int error = FALSE;
4691
4692 val = get_tv_number_chk(rettv, &error);
4693 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004695 clear_tv(rettv);
4696 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004698 else
4699 {
4700 while (end_leader > start_leader)
4701 {
4702 --end_leader;
4703 if (*end_leader == '!')
4704 val = !val;
4705 else if (*end_leader == '-')
4706 val = -val;
4707 }
4708 clear_tv(rettv);
4709 rettv->v_type = VAR_NUMBER;
4710 rettv->vval.v_number = val;
4711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 }
4713
4714 return ret;
4715}
4716
4717/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004718 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4719 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004720 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4721 */
4722 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004723eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004724 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004725 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004726 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004727 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004728{
4729 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004730 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004731 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004732 long len = -1;
4733 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004734 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004735 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004736
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004737 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004738 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004739 if (verbose)
4740 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004741 return FAIL;
4742 }
4743
Bram Moolenaar8c711452005-01-14 21:53:12 +00004744 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004745 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004746 /*
4747 * dict.name
4748 */
4749 key = *arg + 1;
4750 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4751 ;
4752 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004753 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004754 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004755 }
4756 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004757 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004758 /*
4759 * something[idx]
4760 *
4761 * Get the (first) variable from inside the [].
4762 */
4763 *arg = skipwhite(*arg + 1);
4764 if (**arg == ':')
4765 empty1 = TRUE;
4766 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4767 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004768 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4769 {
4770 /* not a number or string */
4771 clear_tv(&var1);
4772 return FAIL;
4773 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004774
4775 /*
4776 * Get the second variable from inside the [:].
4777 */
4778 if (**arg == ':')
4779 {
4780 range = TRUE;
4781 *arg = skipwhite(*arg + 1);
4782 if (**arg == ']')
4783 empty2 = TRUE;
4784 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4785 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004786 if (!empty1)
4787 clear_tv(&var1);
4788 return FAIL;
4789 }
4790 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4791 {
4792 /* not a number or string */
4793 if (!empty1)
4794 clear_tv(&var1);
4795 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004796 return FAIL;
4797 }
4798 }
4799
4800 /* Check for the ']'. */
4801 if (**arg != ']')
4802 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004803 if (verbose)
4804 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004805 clear_tv(&var1);
4806 if (range)
4807 clear_tv(&var2);
4808 return FAIL;
4809 }
4810 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004811 }
4812
4813 if (evaluate)
4814 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004815 n1 = 0;
4816 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004817 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004818 n1 = get_tv_number(&var1);
4819 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004820 }
4821 if (range)
4822 {
4823 if (empty2)
4824 n2 = -1;
4825 else
4826 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004827 n2 = get_tv_number(&var2);
4828 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004829 }
4830 }
4831
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004832 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004833 {
4834 case VAR_NUMBER:
4835 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004836 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004837 len = (long)STRLEN(s);
4838 if (range)
4839 {
4840 /* The resulting variable is a substring. If the indexes
4841 * are out of range the result is empty. */
4842 if (n1 < 0)
4843 {
4844 n1 = len + n1;
4845 if (n1 < 0)
4846 n1 = 0;
4847 }
4848 if (n2 < 0)
4849 n2 = len + n2;
4850 else if (n2 >= len)
4851 n2 = len;
4852 if (n1 >= len || n2 < 0 || n1 > n2)
4853 s = NULL;
4854 else
4855 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4856 }
4857 else
4858 {
4859 /* The resulting variable is a string of a single
4860 * character. If the index is too big or negative the
4861 * result is empty. */
4862 if (n1 >= len || n1 < 0)
4863 s = NULL;
4864 else
4865 s = vim_strnsave(s + n1, 1);
4866 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004867 clear_tv(rettv);
4868 rettv->v_type = VAR_STRING;
4869 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004870 break;
4871
4872 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004873 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004874 if (n1 < 0)
4875 n1 = len + n1;
4876 if (!empty1 && (n1 < 0 || n1 >= len))
4877 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004878 /* For a range we allow invalid values and return an empty
4879 * list. A list index out of range is an error. */
4880 if (!range)
4881 {
4882 if (verbose)
4883 EMSGN(_(e_listidx), n1);
4884 return FAIL;
4885 }
4886 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004887 }
4888 if (range)
4889 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004890 list_T *l;
4891 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004892
4893 if (n2 < 0)
4894 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004895 else if (n2 >= len)
4896 n2 = len - 1;
4897 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004898 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004899 l = list_alloc();
4900 if (l == NULL)
4901 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004902 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004903 n1 <= n2; ++n1)
4904 {
4905 if (list_append_tv(l, &item->li_tv) == FAIL)
4906 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00004907 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004908 return FAIL;
4909 }
4910 item = item->li_next;
4911 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004912 clear_tv(rettv);
4913 rettv->v_type = VAR_LIST;
4914 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004915 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004916 }
4917 else
4918 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004919 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004920 clear_tv(rettv);
4921 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004922 }
4923 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004924
4925 case VAR_DICT:
4926 if (range)
4927 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004928 if (verbose)
4929 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004930 if (len == -1)
4931 clear_tv(&var1);
4932 return FAIL;
4933 }
4934 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004935 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004936
4937 if (len == -1)
4938 {
4939 key = get_tv_string(&var1);
4940 if (*key == NUL)
4941 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004942 if (verbose)
4943 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004944 clear_tv(&var1);
4945 return FAIL;
4946 }
4947 }
4948
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004949 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004950
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004951 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004952 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004953 if (len == -1)
4954 clear_tv(&var1);
4955 if (item == NULL)
4956 return FAIL;
4957
4958 copy_tv(&item->di_tv, &var1);
4959 clear_tv(rettv);
4960 *rettv = var1;
4961 }
4962 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004963 }
4964 }
4965
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004966 return OK;
4967}
4968
4969/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 * Get an option value.
4971 * "arg" points to the '&' or '+' before the option name.
4972 * "arg" is advanced to character after the option name.
4973 * Return OK or FAIL.
4974 */
4975 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004976get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004978 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 int evaluate;
4980{
4981 char_u *option_end;
4982 long numval;
4983 char_u *stringval;
4984 int opt_type;
4985 int c;
4986 int working = (**arg == '+'); /* has("+option") */
4987 int ret = OK;
4988 int opt_flags;
4989
4990 /*
4991 * Isolate the option name and find its value.
4992 */
4993 option_end = find_option_end(arg, &opt_flags);
4994 if (option_end == NULL)
4995 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004996 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 EMSG2(_("E112: Option name missing: %s"), *arg);
4998 return FAIL;
4999 }
5000
5001 if (!evaluate)
5002 {
5003 *arg = option_end;
5004 return OK;
5005 }
5006
5007 c = *option_end;
5008 *option_end = NUL;
5009 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005010 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011
5012 if (opt_type == -3) /* invalid name */
5013 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005014 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 EMSG2(_("E113: Unknown option: %s"), *arg);
5016 ret = FAIL;
5017 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005018 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 {
5020 if (opt_type == -2) /* hidden string option */
5021 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005022 rettv->v_type = VAR_STRING;
5023 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 }
5025 else if (opt_type == -1) /* hidden number option */
5026 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005027 rettv->v_type = VAR_NUMBER;
5028 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 }
5030 else if (opt_type == 1) /* number option */
5031 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005032 rettv->v_type = VAR_NUMBER;
5033 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 }
5035 else /* string option */
5036 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005037 rettv->v_type = VAR_STRING;
5038 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 }
5040 }
5041 else if (working && (opt_type == -2 || opt_type == -1))
5042 ret = FAIL;
5043
5044 *option_end = c; /* put back for error messages */
5045 *arg = option_end;
5046
5047 return ret;
5048}
5049
5050/*
5051 * Allocate a variable for a string constant.
5052 * Return OK or FAIL.
5053 */
5054 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005055get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005057 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 int evaluate;
5059{
5060 char_u *p;
5061 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 int extra = 0;
5063
5064 /*
5065 * Find the end of the string, skipping backslashed characters.
5066 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005067 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 {
5069 if (*p == '\\' && p[1] != NUL)
5070 {
5071 ++p;
5072 /* A "\<x>" form occupies at least 4 characters, and produces up
5073 * to 6 characters: reserve space for 2 extra */
5074 if (*p == '<')
5075 extra += 2;
5076 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 }
5078
5079 if (*p != '"')
5080 {
5081 EMSG2(_("E114: Missing quote: %s"), *arg);
5082 return FAIL;
5083 }
5084
5085 /* If only parsing, set *arg and return here */
5086 if (!evaluate)
5087 {
5088 *arg = p + 1;
5089 return OK;
5090 }
5091
5092 /*
5093 * Copy the string into allocated memory, handling backslashed
5094 * characters.
5095 */
5096 name = alloc((unsigned)(p - *arg + extra));
5097 if (name == NULL)
5098 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005099 rettv->v_type = VAR_STRING;
5100 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101
Bram Moolenaar8c711452005-01-14 21:53:12 +00005102 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 {
5104 if (*p == '\\')
5105 {
5106 switch (*++p)
5107 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005108 case 'b': *name++ = BS; ++p; break;
5109 case 'e': *name++ = ESC; ++p; break;
5110 case 'f': *name++ = FF; ++p; break;
5111 case 'n': *name++ = NL; ++p; break;
5112 case 'r': *name++ = CAR; ++p; break;
5113 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114
5115 case 'X': /* hex: "\x1", "\x12" */
5116 case 'x':
5117 case 'u': /* Unicode: "\u0023" */
5118 case 'U':
5119 if (vim_isxdigit(p[1]))
5120 {
5121 int n, nr;
5122 int c = toupper(*p);
5123
5124 if (c == 'X')
5125 n = 2;
5126 else
5127 n = 4;
5128 nr = 0;
5129 while (--n >= 0 && vim_isxdigit(p[1]))
5130 {
5131 ++p;
5132 nr = (nr << 4) + hex2nr(*p);
5133 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005134 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135#ifdef FEAT_MBYTE
5136 /* For "\u" store the number according to
5137 * 'encoding'. */
5138 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005139 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 else
5141#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005142 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 break;
5145
5146 /* octal: "\1", "\12", "\123" */
5147 case '0':
5148 case '1':
5149 case '2':
5150 case '3':
5151 case '4':
5152 case '5':
5153 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005154 case '7': *name = *p++ - '0';
5155 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005157 *name = (*name << 3) + *p++ - '0';
5158 if (*p >= '0' && *p <= '7')
5159 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005161 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 break;
5163
5164 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005165 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 if (extra != 0)
5167 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005168 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 break;
5170 }
5171 /* FALLTHROUGH */
5172
Bram Moolenaar8c711452005-01-14 21:53:12 +00005173 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 break;
5175 }
5176 }
5177 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005178 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005181 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 *arg = p + 1;
5183
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 return OK;
5185}
5186
5187/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005188 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 * Return OK or FAIL.
5190 */
5191 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005192get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005194 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 int evaluate;
5196{
5197 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005198 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005199 int reduce = 0;
5200
5201 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005202 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005203 */
5204 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5205 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005206 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005207 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005208 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005209 break;
5210 ++reduce;
5211 ++p;
5212 }
5213 }
5214
Bram Moolenaar8c711452005-01-14 21:53:12 +00005215 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005216 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005217 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005218 return FAIL;
5219 }
5220
Bram Moolenaar8c711452005-01-14 21:53:12 +00005221 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005222 if (!evaluate)
5223 {
5224 *arg = p + 1;
5225 return OK;
5226 }
5227
5228 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005229 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005230 */
5231 str = alloc((unsigned)((p - *arg) - reduce));
5232 if (str == NULL)
5233 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005234 rettv->v_type = VAR_STRING;
5235 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005236
Bram Moolenaar8c711452005-01-14 21:53:12 +00005237 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005238 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005239 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005240 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005241 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005242 break;
5243 ++p;
5244 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005245 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005246 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005247 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005248 *arg = p + 1;
5249
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005250 return OK;
5251}
5252
5253/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005254 * Allocate a variable for a List and fill it from "*arg".
5255 * Return OK or FAIL.
5256 */
5257 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005258get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005259 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005260 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005261 int evaluate;
5262{
Bram Moolenaar33570922005-01-25 22:26:29 +00005263 list_T *l = NULL;
5264 typval_T tv;
5265 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005266
5267 if (evaluate)
5268 {
5269 l = list_alloc();
5270 if (l == NULL)
5271 return FAIL;
5272 }
5273
5274 *arg = skipwhite(*arg + 1);
5275 while (**arg != ']' && **arg != NUL)
5276 {
5277 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5278 goto failret;
5279 if (evaluate)
5280 {
5281 item = listitem_alloc();
5282 if (item != NULL)
5283 {
5284 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005285 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005286 list_append(l, item);
5287 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005288 else
5289 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005290 }
5291
5292 if (**arg == ']')
5293 break;
5294 if (**arg != ',')
5295 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005296 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005297 goto failret;
5298 }
5299 *arg = skipwhite(*arg + 1);
5300 }
5301
5302 if (**arg != ']')
5303 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005304 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005305failret:
5306 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005307 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005308 return FAIL;
5309 }
5310
5311 *arg = skipwhite(*arg + 1);
5312 if (evaluate)
5313 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005314 rettv->v_type = VAR_LIST;
5315 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005316 ++l->lv_refcount;
5317 }
5318
5319 return OK;
5320}
5321
5322/*
5323 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005324 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005325 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005326 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005327list_alloc()
5328{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005329 list_T *l;
5330
5331 l = (list_T *)alloc_clear(sizeof(list_T));
5332 if (l != NULL)
5333 {
5334 /* Prepend the list to the list of lists for garbage collection. */
5335 if (first_list != NULL)
5336 first_list->lv_used_prev = l;
5337 l->lv_used_prev = NULL;
5338 l->lv_used_next = first_list;
5339 first_list = l;
5340 }
5341 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005342}
5343
5344/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005345 * Allocate an empty list for a return value.
5346 * Returns OK or FAIL.
5347 */
5348 static int
5349rettv_list_alloc(rettv)
5350 typval_T *rettv;
5351{
5352 list_T *l = list_alloc();
5353
5354 if (l == NULL)
5355 return FAIL;
5356
5357 rettv->vval.v_list = l;
5358 rettv->v_type = VAR_LIST;
5359 ++l->lv_refcount;
5360 return OK;
5361}
5362
5363/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005364 * Unreference a list: decrement the reference count and free it when it
5365 * becomes zero.
5366 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005367 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005368list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005369 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005370{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005371 if (l != NULL && --l->lv_refcount <= 0)
5372 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005373}
5374
5375/*
5376 * Free a list, including all items it points to.
5377 * Ignores the reference count.
5378 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005379 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005380list_free(l, recurse)
5381 list_T *l;
5382 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005383{
Bram Moolenaar33570922005-01-25 22:26:29 +00005384 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005385
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005386 /* Remove the list from the list of lists for garbage collection. */
5387 if (l->lv_used_prev == NULL)
5388 first_list = l->lv_used_next;
5389 else
5390 l->lv_used_prev->lv_used_next = l->lv_used_next;
5391 if (l->lv_used_next != NULL)
5392 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5393
Bram Moolenaard9fba312005-06-26 22:34:35 +00005394 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005395 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005396 /* Remove the item before deleting it. */
5397 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005398 if (recurse || (item->li_tv.v_type != VAR_LIST
5399 && item->li_tv.v_type != VAR_DICT))
5400 clear_tv(&item->li_tv);
5401 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005402 }
5403 vim_free(l);
5404}
5405
5406/*
5407 * Allocate a list item.
5408 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005409 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005410listitem_alloc()
5411{
Bram Moolenaar33570922005-01-25 22:26:29 +00005412 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005413}
5414
5415/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005416 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005417 */
5418 static void
5419listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005420 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005421{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005422 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005423 vim_free(item);
5424}
5425
5426/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005427 * Remove a list item from a List and free it. Also clears the value.
5428 */
5429 static void
5430listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005431 list_T *l;
5432 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005433{
5434 list_remove(l, item, item);
5435 listitem_free(item);
5436}
5437
5438/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005439 * Get the number of items in a list.
5440 */
5441 static long
5442list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005443 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005444{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005445 if (l == NULL)
5446 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005447 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005448}
5449
5450/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005451 * Return TRUE when two lists have exactly the same values.
5452 */
5453 static int
5454list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005455 list_T *l1;
5456 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005457 int ic; /* ignore case for strings */
5458{
Bram Moolenaar33570922005-01-25 22:26:29 +00005459 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005460
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005461 if (l1 == l2)
5462 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005463 if (list_len(l1) != list_len(l2))
5464 return FALSE;
5465
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005466 for (item1 = l1->lv_first, item2 = l2->lv_first;
5467 item1 != NULL && item2 != NULL;
5468 item1 = item1->li_next, item2 = item2->li_next)
5469 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5470 return FALSE;
5471 return item1 == NULL && item2 == NULL;
5472}
5473
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005474#if defined(FEAT_PYTHON) || defined(PROTO)
5475/*
5476 * Return the dictitem that an entry in a hashtable points to.
5477 */
5478 dictitem_T *
5479dict_lookup(hi)
5480 hashitem_T *hi;
5481{
5482 return HI2DI(hi);
5483}
5484#endif
5485
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005486/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005487 * Return TRUE when two dictionaries have exactly the same key/values.
5488 */
5489 static int
5490dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005491 dict_T *d1;
5492 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005493 int ic; /* ignore case for strings */
5494{
Bram Moolenaar33570922005-01-25 22:26:29 +00005495 hashitem_T *hi;
5496 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005497 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005498
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005499 if (d1 == d2)
5500 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005501 if (dict_len(d1) != dict_len(d2))
5502 return FALSE;
5503
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005504 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00005505 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005506 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005507 if (!HASHITEM_EMPTY(hi))
5508 {
5509 item2 = dict_find(d2, hi->hi_key, -1);
5510 if (item2 == NULL)
5511 return FALSE;
5512 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5513 return FALSE;
5514 --todo;
5515 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005516 }
5517 return TRUE;
5518}
5519
5520/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005521 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005522 * Compares the items just like "==" would compare them, but strings and
5523 * numbers are different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005524 */
5525 static int
5526tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005527 typval_T *tv1;
5528 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005529 int ic; /* ignore case */
5530{
5531 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005532 char_u *s1, *s2;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005533 static int recursive = 0; /* cach recursive loops */
5534 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005535
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005536 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005537 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005538 /* Catch lists and dicts that have an endless loop by limiting
5539 * recursiveness to 1000. We guess they are equal then. */
5540 if (recursive >= 1000)
5541 return TRUE;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005542
5543 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005544 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005545 case VAR_LIST:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005546 ++recursive;
5547 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5548 --recursive;
5549 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005550
5551 case VAR_DICT:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005552 ++recursive;
5553 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5554 --recursive;
5555 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005556
5557 case VAR_FUNC:
5558 return (tv1->vval.v_string != NULL
5559 && tv2->vval.v_string != NULL
5560 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5561
5562 case VAR_NUMBER:
5563 return tv1->vval.v_number == tv2->vval.v_number;
5564
5565 case VAR_STRING:
5566 s1 = get_tv_string_buf(tv1, buf1);
5567 s2 = get_tv_string_buf(tv2, buf2);
5568 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005569 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005570
5571 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005572 return TRUE;
5573}
5574
5575/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005576 * Locate item with index "n" in list "l" and return it.
5577 * A negative index is counted from the end; -1 is the last item.
5578 * Returns NULL when "n" is out of range.
5579 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005580 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005581list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005582 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005583 long n;
5584{
Bram Moolenaar33570922005-01-25 22:26:29 +00005585 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005586 long idx;
5587
5588 if (l == NULL)
5589 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005590
5591 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005592 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005593 n = l->lv_len + n;
5594
5595 /* Check for index out of range. */
5596 if (n < 0 || n >= l->lv_len)
5597 return NULL;
5598
5599 /* When there is a cached index may start search from there. */
5600 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005601 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005602 if (n < l->lv_idx / 2)
5603 {
5604 /* closest to the start of the list */
5605 item = l->lv_first;
5606 idx = 0;
5607 }
5608 else if (n > (l->lv_idx + l->lv_len) / 2)
5609 {
5610 /* closest to the end of the list */
5611 item = l->lv_last;
5612 idx = l->lv_len - 1;
5613 }
5614 else
5615 {
5616 /* closest to the cached index */
5617 item = l->lv_idx_item;
5618 idx = l->lv_idx;
5619 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005620 }
5621 else
5622 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005623 if (n < l->lv_len / 2)
5624 {
5625 /* closest to the start of the list */
5626 item = l->lv_first;
5627 idx = 0;
5628 }
5629 else
5630 {
5631 /* closest to the end of the list */
5632 item = l->lv_last;
5633 idx = l->lv_len - 1;
5634 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005635 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005636
5637 while (n > idx)
5638 {
5639 /* search forward */
5640 item = item->li_next;
5641 ++idx;
5642 }
5643 while (n < idx)
5644 {
5645 /* search backward */
5646 item = item->li_prev;
5647 --idx;
5648 }
5649
5650 /* cache the used index */
5651 l->lv_idx = idx;
5652 l->lv_idx_item = item;
5653
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005654 return item;
5655}
5656
5657/*
Bram Moolenaara5525202006-03-02 22:52:09 +00005658 * Get list item "l[idx]" as a number.
5659 */
5660 static long
5661list_find_nr(l, idx, errorp)
5662 list_T *l;
5663 long idx;
5664 int *errorp; /* set to TRUE when something wrong */
5665{
5666 listitem_T *li;
5667
5668 li = list_find(l, idx);
5669 if (li == NULL)
5670 {
5671 if (errorp != NULL)
5672 *errorp = TRUE;
5673 return -1L;
5674 }
5675 return get_tv_number_chk(&li->li_tv, errorp);
5676}
5677
5678/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005679 * Locate "item" list "l" and return its index.
5680 * Returns -1 when "item" is not in the list.
5681 */
5682 static long
5683list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005684 list_T *l;
5685 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005686{
5687 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005688 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005689
5690 if (l == NULL)
5691 return -1;
5692 idx = 0;
5693 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5694 ++idx;
5695 if (li == NULL)
5696 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005697 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005698}
5699
5700/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005701 * Append item "item" to the end of list "l".
5702 */
5703 static void
5704list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005705 list_T *l;
5706 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005707{
5708 if (l->lv_last == NULL)
5709 {
5710 /* empty list */
5711 l->lv_first = item;
5712 l->lv_last = item;
5713 item->li_prev = NULL;
5714 }
5715 else
5716 {
5717 l->lv_last->li_next = item;
5718 item->li_prev = l->lv_last;
5719 l->lv_last = item;
5720 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005721 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005722 item->li_next = NULL;
5723}
5724
5725/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005726 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005727 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005728 */
5729 static int
5730list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005731 list_T *l;
5732 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005733{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005734 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005735
Bram Moolenaar05159a02005-02-26 23:04:13 +00005736 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005737 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005738 copy_tv(tv, &li->li_tv);
5739 list_append(l, li);
5740 return OK;
5741}
5742
5743/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005744 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005745 * Return FAIL when out of memory.
5746 */
5747 int
5748list_append_dict(list, dict)
5749 list_T *list;
5750 dict_T *dict;
5751{
5752 listitem_T *li = listitem_alloc();
5753
5754 if (li == NULL)
5755 return FAIL;
5756 li->li_tv.v_type = VAR_DICT;
5757 li->li_tv.v_lock = 0;
5758 li->li_tv.vval.v_dict = dict;
5759 list_append(list, li);
5760 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005761 return OK;
5762}
5763
5764/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005765 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00005766 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005767 * Returns FAIL when out of memory.
5768 */
5769 static int
Bram Moolenaar4463f292005-09-25 22:20:24 +00005770list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005771 list_T *l;
5772 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00005773 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005774{
5775 listitem_T *li = listitem_alloc();
5776
5777 if (li == NULL)
5778 return FAIL;
5779 list_append(l, li);
5780 li->li_tv.v_type = VAR_STRING;
5781 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005782 if (str == NULL)
5783 li->li_tv.vval.v_string = NULL;
5784 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005785 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005786 return FAIL;
5787 return OK;
5788}
5789
5790/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00005791 * Append "n" to list "l".
5792 * Returns FAIL when out of memory.
5793 */
5794 static int
5795list_append_number(l, n)
5796 list_T *l;
5797 varnumber_T n;
5798{
5799 listitem_T *li;
5800
5801 li = listitem_alloc();
5802 if (li == NULL)
5803 return FAIL;
5804 li->li_tv.v_type = VAR_NUMBER;
5805 li->li_tv.v_lock = 0;
5806 li->li_tv.vval.v_number = n;
5807 list_append(l, li);
5808 return OK;
5809}
5810
5811/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005812 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005813 * If "item" is NULL append at the end.
5814 * Return FAIL when out of memory.
5815 */
5816 static int
5817list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005818 list_T *l;
5819 typval_T *tv;
5820 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005821{
Bram Moolenaar33570922005-01-25 22:26:29 +00005822 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005823
5824 if (ni == NULL)
5825 return FAIL;
5826 copy_tv(tv, &ni->li_tv);
5827 if (item == NULL)
5828 /* Append new item at end of list. */
5829 list_append(l, ni);
5830 else
5831 {
5832 /* Insert new item before existing item. */
5833 ni->li_prev = item->li_prev;
5834 ni->li_next = item;
5835 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005836 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005837 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005838 ++l->lv_idx;
5839 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005840 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005841 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005842 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005843 l->lv_idx_item = NULL;
5844 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005845 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005846 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005847 }
5848 return OK;
5849}
5850
5851/*
5852 * Extend "l1" with "l2".
5853 * If "bef" is NULL append at the end, otherwise insert before this item.
5854 * Returns FAIL when out of memory.
5855 */
5856 static int
5857list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005858 list_T *l1;
5859 list_T *l2;
5860 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005861{
Bram Moolenaar33570922005-01-25 22:26:29 +00005862 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005863
5864 for (item = l2->lv_first; item != NULL; item = item->li_next)
5865 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5866 return FAIL;
5867 return OK;
5868}
5869
5870/*
5871 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5872 * Return FAIL when out of memory.
5873 */
5874 static int
5875list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005876 list_T *l1;
5877 list_T *l2;
5878 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005879{
Bram Moolenaar33570922005-01-25 22:26:29 +00005880 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005881
5882 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005883 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005884 if (l == NULL)
5885 return FAIL;
5886 tv->v_type = VAR_LIST;
5887 tv->vval.v_list = l;
5888
5889 /* append all items from the second list */
5890 return list_extend(l, l2, NULL);
5891}
5892
5893/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005894 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005895 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005896 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005897 * Returns NULL when out of memory.
5898 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005899 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005900list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005901 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005902 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005903 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005904{
Bram Moolenaar33570922005-01-25 22:26:29 +00005905 list_T *copy;
5906 listitem_T *item;
5907 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005908
5909 if (orig == NULL)
5910 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005911
5912 copy = list_alloc();
5913 if (copy != NULL)
5914 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005915 if (copyID != 0)
5916 {
5917 /* Do this before adding the items, because one of the items may
5918 * refer back to this list. */
5919 orig->lv_copyID = copyID;
5920 orig->lv_copylist = copy;
5921 }
5922 for (item = orig->lv_first; item != NULL && !got_int;
5923 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005924 {
5925 ni = listitem_alloc();
5926 if (ni == NULL)
5927 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005928 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005929 {
5930 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5931 {
5932 vim_free(ni);
5933 break;
5934 }
5935 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005936 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005937 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005938 list_append(copy, ni);
5939 }
5940 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005941 if (item != NULL)
5942 {
5943 list_unref(copy);
5944 copy = NULL;
5945 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005946 }
5947
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005948 return copy;
5949}
5950
5951/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005952 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005953 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005954 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005955 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005956list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005957 list_T *l;
5958 listitem_T *item;
5959 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005960{
Bram Moolenaar33570922005-01-25 22:26:29 +00005961 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005962
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005963 /* notify watchers */
5964 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005965 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005966 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005967 list_fix_watch(l, ip);
5968 if (ip == item2)
5969 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005970 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005971
5972 if (item2->li_next == NULL)
5973 l->lv_last = item->li_prev;
5974 else
5975 item2->li_next->li_prev = item->li_prev;
5976 if (item->li_prev == NULL)
5977 l->lv_first = item2->li_next;
5978 else
5979 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005980 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005981}
5982
5983/*
5984 * Return an allocated string with the string representation of a list.
5985 * May return NULL.
5986 */
5987 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005988list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005989 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005990 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005991{
5992 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005993
5994 if (tv->vval.v_list == NULL)
5995 return NULL;
5996 ga_init2(&ga, (int)sizeof(char), 80);
5997 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005998 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005999 {
6000 vim_free(ga.ga_data);
6001 return NULL;
6002 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006003 ga_append(&ga, ']');
6004 ga_append(&ga, NUL);
6005 return (char_u *)ga.ga_data;
6006}
6007
6008/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006009 * Join list "l" into a string in "*gap", using separator "sep".
6010 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006011 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006012 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006013 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006014list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006015 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006016 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006017 char_u *sep;
6018 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006019 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006020{
6021 int first = TRUE;
6022 char_u *tofree;
6023 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006024 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006025 char_u *s;
6026
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006027 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006028 {
6029 if (first)
6030 first = FALSE;
6031 else
6032 ga_concat(gap, sep);
6033
6034 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006035 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006036 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006037 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006038 if (s != NULL)
6039 ga_concat(gap, s);
6040 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006041 if (s == NULL)
6042 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006043 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006044 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006045}
6046
6047/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006048 * Garbage collection for lists and dictionaries.
6049 *
6050 * We use reference counts to be able to free most items right away when they
6051 * are no longer used. But for composite items it's possible that it becomes
6052 * unused while the reference count is > 0: When there is a recursive
6053 * reference. Example:
6054 * :let l = [1, 2, 3]
6055 * :let d = {9: l}
6056 * :let l[1] = d
6057 *
6058 * Since this is quite unusual we handle this with garbage collection: every
6059 * once in a while find out which lists and dicts are not referenced from any
6060 * variable.
6061 *
6062 * Here is a good reference text about garbage collection (refers to Python
6063 * but it applies to all reference-counting mechanisms):
6064 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006065 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006066
6067/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006068 * Do garbage collection for lists and dicts.
6069 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006070 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006071 int
6072garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006073{
6074 dict_T *dd;
6075 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006076 int copyID = ++current_copyID;
6077 buf_T *buf;
6078 win_T *wp;
6079 int i;
6080 funccall_T *fc;
6081 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006082#ifdef FEAT_WINDOWS
6083 tabpage_T *tp;
6084#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006085
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006086 /* Only do this once. */
6087 want_garbage_collect = FALSE;
6088 may_garbage_collect = FALSE;
6089
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006090 /*
6091 * 1. Go through all accessible variables and mark all lists and dicts
6092 * with copyID.
6093 */
6094 /* script-local variables */
6095 for (i = 1; i <= ga_scripts.ga_len; ++i)
6096 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6097
6098 /* buffer-local variables */
6099 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6100 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6101
6102 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006103 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006104 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6105
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006106#ifdef FEAT_WINDOWS
6107 /* tabpage-local variables */
6108 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6109 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6110#endif
6111
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006112 /* global variables */
6113 set_ref_in_ht(&globvarht, copyID);
6114
6115 /* function-local variables */
6116 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6117 {
6118 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6119 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6120 }
6121
6122 /*
6123 * 2. Go through the list of dicts and free items without the copyID.
6124 */
6125 for (dd = first_dict; dd != NULL; )
6126 if (dd->dv_copyID != copyID)
6127 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006128 /* Free the Dictionary and ordinary items it contains, but don't
6129 * recurse into Lists and Dictionaries, they will be in the list
6130 * of dicts or list of lists. */
6131 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006132 did_free = TRUE;
6133
6134 /* restart, next dict may also have been freed */
6135 dd = first_dict;
6136 }
6137 else
6138 dd = dd->dv_used_next;
6139
6140 /*
6141 * 3. Go through the list of lists and free items without the copyID.
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006142 * But don't free a list that has a watcher (used in a for loop), these
6143 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006144 */
6145 for (ll = first_list; ll != NULL; )
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006146 if (ll->lv_copyID != copyID && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006147 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006148 /* Free the List and ordinary items it contains, but don't recurse
6149 * into Lists and Dictionaries, they will be in the list of dicts
6150 * or list of lists. */
6151 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006152 did_free = TRUE;
6153
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006154 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006155 ll = first_list;
6156 }
6157 else
6158 ll = ll->lv_used_next;
6159
6160 return did_free;
6161}
6162
6163/*
6164 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6165 */
6166 static void
6167set_ref_in_ht(ht, copyID)
6168 hashtab_T *ht;
6169 int copyID;
6170{
6171 int todo;
6172 hashitem_T *hi;
6173
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006174 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006175 for (hi = ht->ht_array; todo > 0; ++hi)
6176 if (!HASHITEM_EMPTY(hi))
6177 {
6178 --todo;
6179 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6180 }
6181}
6182
6183/*
6184 * Mark all lists and dicts referenced through list "l" with "copyID".
6185 */
6186 static void
6187set_ref_in_list(l, copyID)
6188 list_T *l;
6189 int copyID;
6190{
6191 listitem_T *li;
6192
6193 for (li = l->lv_first; li != NULL; li = li->li_next)
6194 set_ref_in_item(&li->li_tv, copyID);
6195}
6196
6197/*
6198 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6199 */
6200 static void
6201set_ref_in_item(tv, copyID)
6202 typval_T *tv;
6203 int copyID;
6204{
6205 dict_T *dd;
6206 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006207
6208 switch (tv->v_type)
6209 {
6210 case VAR_DICT:
6211 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006212 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006213 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006214 /* Didn't see this dict yet. */
6215 dd->dv_copyID = copyID;
6216 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006217 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006218 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006219
6220 case VAR_LIST:
6221 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006222 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006223 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006224 /* Didn't see this list yet. */
6225 ll->lv_copyID = copyID;
6226 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006227 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006228 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006229 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006230 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006231}
6232
6233/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006234 * Allocate an empty header for a dictionary.
6235 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006236 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006237dict_alloc()
6238{
Bram Moolenaar33570922005-01-25 22:26:29 +00006239 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006240
Bram Moolenaar33570922005-01-25 22:26:29 +00006241 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006242 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006243 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006244 /* Add the list to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006245 if (first_dict != NULL)
6246 first_dict->dv_used_prev = d;
6247 d->dv_used_next = first_dict;
6248 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006249 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006250
Bram Moolenaar33570922005-01-25 22:26:29 +00006251 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006252 d->dv_lock = 0;
6253 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006254 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006255 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006256 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006257}
6258
6259/*
6260 * Unreference a Dictionary: decrement the reference count and free it when it
6261 * becomes zero.
6262 */
6263 static void
6264dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006265 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006266{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006267 if (d != NULL && --d->dv_refcount <= 0)
6268 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006269}
6270
6271/*
6272 * Free a Dictionary, including all items it contains.
6273 * Ignores the reference count.
6274 */
6275 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006276dict_free(d, recurse)
6277 dict_T *d;
6278 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006279{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006280 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006281 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006282 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006283
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006284 /* Remove the dict from the list of dicts for garbage collection. */
6285 if (d->dv_used_prev == NULL)
6286 first_dict = d->dv_used_next;
6287 else
6288 d->dv_used_prev->dv_used_next = d->dv_used_next;
6289 if (d->dv_used_next != NULL)
6290 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6291
6292 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006293 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006294 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006295 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006296 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006297 if (!HASHITEM_EMPTY(hi))
6298 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006299 /* Remove the item before deleting it, just in case there is
6300 * something recursive causing trouble. */
6301 di = HI2DI(hi);
6302 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00006303 if (recurse || (di->di_tv.v_type != VAR_LIST
6304 && di->di_tv.v_type != VAR_DICT))
6305 clear_tv(&di->di_tv);
6306 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006307 --todo;
6308 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006309 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006310 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006311 vim_free(d);
6312}
6313
6314/*
6315 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006316 * The "key" is copied to the new item.
6317 * Note that the value of the item "di_tv" still needs to be initialized!
6318 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006319 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006320 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006321dictitem_alloc(key)
6322 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006323{
Bram Moolenaar33570922005-01-25 22:26:29 +00006324 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006325
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006326 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006327 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006328 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006329 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006330 di->di_flags = 0;
6331 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006332 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006333}
6334
6335/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006336 * Make a copy of a Dictionary item.
6337 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006338 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006339dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006340 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006341{
Bram Moolenaar33570922005-01-25 22:26:29 +00006342 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006343
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006344 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
6345 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006346 if (di != NULL)
6347 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006348 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006349 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006350 copy_tv(&org->di_tv, &di->di_tv);
6351 }
6352 return di;
6353}
6354
6355/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006356 * Remove item "item" from Dictionary "dict" and free it.
6357 */
6358 static void
6359dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006360 dict_T *dict;
6361 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006362{
Bram Moolenaar33570922005-01-25 22:26:29 +00006363 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006364
Bram Moolenaar33570922005-01-25 22:26:29 +00006365 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006366 if (HASHITEM_EMPTY(hi))
6367 EMSG2(_(e_intern2), "dictitem_remove()");
6368 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006369 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006370 dictitem_free(item);
6371}
6372
6373/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006374 * Free a dict item. Also clears the value.
6375 */
6376 static void
6377dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006378 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006379{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006380 clear_tv(&item->di_tv);
6381 vim_free(item);
6382}
6383
6384/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006385 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6386 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006387 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006388 * Returns NULL when out of memory.
6389 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006390 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006391dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006392 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006393 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006394 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006395{
Bram Moolenaar33570922005-01-25 22:26:29 +00006396 dict_T *copy;
6397 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006398 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006399 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006400
6401 if (orig == NULL)
6402 return NULL;
6403
6404 copy = dict_alloc();
6405 if (copy != NULL)
6406 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006407 if (copyID != 0)
6408 {
6409 orig->dv_copyID = copyID;
6410 orig->dv_copydict = copy;
6411 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006412 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006413 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006414 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006415 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006416 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006417 --todo;
6418
6419 di = dictitem_alloc(hi->hi_key);
6420 if (di == NULL)
6421 break;
6422 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006423 {
6424 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6425 copyID) == FAIL)
6426 {
6427 vim_free(di);
6428 break;
6429 }
6430 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006431 else
6432 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6433 if (dict_add(copy, di) == FAIL)
6434 {
6435 dictitem_free(di);
6436 break;
6437 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006438 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006439 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006440
Bram Moolenaare9a41262005-01-15 22:18:47 +00006441 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006442 if (todo > 0)
6443 {
6444 dict_unref(copy);
6445 copy = NULL;
6446 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006447 }
6448
6449 return copy;
6450}
6451
6452/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006453 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006454 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006455 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006456 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006457dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006458 dict_T *d;
6459 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006460{
Bram Moolenaar33570922005-01-25 22:26:29 +00006461 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006462}
6463
Bram Moolenaar8c711452005-01-14 21:53:12 +00006464/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006465 * Add a number or string entry to dictionary "d".
6466 * When "str" is NULL use number "nr", otherwise use "str".
6467 * Returns FAIL when out of memory and when key already exists.
6468 */
6469 int
6470dict_add_nr_str(d, key, nr, str)
6471 dict_T *d;
6472 char *key;
6473 long nr;
6474 char_u *str;
6475{
6476 dictitem_T *item;
6477
6478 item = dictitem_alloc((char_u *)key);
6479 if (item == NULL)
6480 return FAIL;
6481 item->di_tv.v_lock = 0;
6482 if (str == NULL)
6483 {
6484 item->di_tv.v_type = VAR_NUMBER;
6485 item->di_tv.vval.v_number = nr;
6486 }
6487 else
6488 {
6489 item->di_tv.v_type = VAR_STRING;
6490 item->di_tv.vval.v_string = vim_strsave(str);
6491 }
6492 if (dict_add(d, item) == FAIL)
6493 {
6494 dictitem_free(item);
6495 return FAIL;
6496 }
6497 return OK;
6498}
6499
6500/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006501 * Get the number of items in a Dictionary.
6502 */
6503 static long
6504dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006505 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006506{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006507 if (d == NULL)
6508 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006509 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006510}
6511
6512/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006513 * Find item "key[len]" in Dictionary "d".
6514 * If "len" is negative use strlen(key).
6515 * Returns NULL when not found.
6516 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006517 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006518dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006519 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006520 char_u *key;
6521 int len;
6522{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006523#define AKEYLEN 200
6524 char_u buf[AKEYLEN];
6525 char_u *akey;
6526 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006527 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006528
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006529 if (len < 0)
6530 akey = key;
6531 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006532 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006533 tofree = akey = vim_strnsave(key, len);
6534 if (akey == NULL)
6535 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006536 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006537 else
6538 {
6539 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006540 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006541 akey = buf;
6542 }
6543
Bram Moolenaar33570922005-01-25 22:26:29 +00006544 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006545 vim_free(tofree);
6546 if (HASHITEM_EMPTY(hi))
6547 return NULL;
6548 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006549}
6550
6551/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006552 * Get a string item from a dictionary.
6553 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00006554 * Returns NULL if the entry doesn't exist or out of memory.
6555 */
6556 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006557get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00006558 dict_T *d;
6559 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006560 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006561{
6562 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006563 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006564
6565 di = dict_find(d, key, -1);
6566 if (di == NULL)
6567 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006568 s = get_tv_string(&di->di_tv);
6569 if (save && s != NULL)
6570 s = vim_strsave(s);
6571 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006572}
6573
6574/*
6575 * Get a number item from a dictionary.
6576 * Returns 0 if the entry doesn't exist or out of memory.
6577 */
6578 long
6579get_dict_number(d, key)
6580 dict_T *d;
6581 char_u *key;
6582{
6583 dictitem_T *di;
6584
6585 di = dict_find(d, key, -1);
6586 if (di == NULL)
6587 return 0;
6588 return get_tv_number(&di->di_tv);
6589}
6590
6591/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006592 * Return an allocated string with the string representation of a Dictionary.
6593 * May return NULL.
6594 */
6595 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006596dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006597 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006598 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006599{
6600 garray_T ga;
6601 int first = TRUE;
6602 char_u *tofree;
6603 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006604 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006605 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006606 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006607 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006608
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006609 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006610 return NULL;
6611 ga_init2(&ga, (int)sizeof(char), 80);
6612 ga_append(&ga, '{');
6613
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006614 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006615 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006616 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006617 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006618 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006619 --todo;
6620
6621 if (first)
6622 first = FALSE;
6623 else
6624 ga_concat(&ga, (char_u *)", ");
6625
6626 tofree = string_quote(hi->hi_key, FALSE);
6627 if (tofree != NULL)
6628 {
6629 ga_concat(&ga, tofree);
6630 vim_free(tofree);
6631 }
6632 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006633 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006634 if (s != NULL)
6635 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006636 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006637 if (s == NULL)
6638 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006639 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006640 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006641 if (todo > 0)
6642 {
6643 vim_free(ga.ga_data);
6644 return NULL;
6645 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006646
6647 ga_append(&ga, '}');
6648 ga_append(&ga, NUL);
6649 return (char_u *)ga.ga_data;
6650}
6651
6652/*
6653 * Allocate a variable for a Dictionary and fill it from "*arg".
6654 * Return OK or FAIL. Returns NOTDONE for {expr}.
6655 */
6656 static int
6657get_dict_tv(arg, rettv, evaluate)
6658 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006659 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006660 int evaluate;
6661{
Bram Moolenaar33570922005-01-25 22:26:29 +00006662 dict_T *d = NULL;
6663 typval_T tvkey;
6664 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006665 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00006666 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006667 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006668 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00006669
6670 /*
6671 * First check if it's not a curly-braces thing: {expr}.
6672 * Must do this without evaluating, otherwise a function may be called
6673 * twice. Unfortunately this means we need to call eval1() twice for the
6674 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006675 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006676 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006677 if (*start != '}')
6678 {
6679 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
6680 return FAIL;
6681 if (*start == '}')
6682 return NOTDONE;
6683 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006684
6685 if (evaluate)
6686 {
6687 d = dict_alloc();
6688 if (d == NULL)
6689 return FAIL;
6690 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006691 tvkey.v_type = VAR_UNKNOWN;
6692 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006693
6694 *arg = skipwhite(*arg + 1);
6695 while (**arg != '}' && **arg != NUL)
6696 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006697 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006698 goto failret;
6699 if (**arg != ':')
6700 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006701 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006702 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006703 goto failret;
6704 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006705 key = get_tv_string_buf_chk(&tvkey, buf);
6706 if (key == NULL || *key == NUL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006707 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006708 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6709 if (key != NULL)
6710 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006711 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006712 goto failret;
6713 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006714
6715 *arg = skipwhite(*arg + 1);
6716 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6717 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006718 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006719 goto failret;
6720 }
6721 if (evaluate)
6722 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006723 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006724 if (item != NULL)
6725 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00006726 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006727 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006728 clear_tv(&tv);
6729 goto failret;
6730 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006731 item = dictitem_alloc(key);
6732 clear_tv(&tvkey);
6733 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006734 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006735 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006736 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006737 if (dict_add(d, item) == FAIL)
6738 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006739 }
6740 }
6741
6742 if (**arg == '}')
6743 break;
6744 if (**arg != ',')
6745 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006746 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006747 goto failret;
6748 }
6749 *arg = skipwhite(*arg + 1);
6750 }
6751
6752 if (**arg != '}')
6753 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006754 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006755failret:
6756 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00006757 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006758 return FAIL;
6759 }
6760
6761 *arg = skipwhite(*arg + 1);
6762 if (evaluate)
6763 {
6764 rettv->v_type = VAR_DICT;
6765 rettv->vval.v_dict = d;
6766 ++d->dv_refcount;
6767 }
6768
6769 return OK;
6770}
6771
Bram Moolenaar8c711452005-01-14 21:53:12 +00006772/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006773 * Return a string with the string representation of a variable.
6774 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006775 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006776 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006777 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006778 * May return NULL;
6779 */
6780 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006781echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006782 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006783 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006784 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006785 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006786{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006787 static int recurse = 0;
6788 char_u *r = NULL;
6789
Bram Moolenaar33570922005-01-25 22:26:29 +00006790 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006791 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006792 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006793 *tofree = NULL;
6794 return NULL;
6795 }
6796 ++recurse;
6797
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006798 switch (tv->v_type)
6799 {
6800 case VAR_FUNC:
6801 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006802 r = tv->vval.v_string;
6803 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006804
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006805 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006806 if (tv->vval.v_list == NULL)
6807 {
6808 *tofree = NULL;
6809 r = NULL;
6810 }
6811 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
6812 {
6813 *tofree = NULL;
6814 r = (char_u *)"[...]";
6815 }
6816 else
6817 {
6818 tv->vval.v_list->lv_copyID = copyID;
6819 *tofree = list2string(tv, copyID);
6820 r = *tofree;
6821 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006822 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006823
Bram Moolenaar8c711452005-01-14 21:53:12 +00006824 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006825 if (tv->vval.v_dict == NULL)
6826 {
6827 *tofree = NULL;
6828 r = NULL;
6829 }
6830 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
6831 {
6832 *tofree = NULL;
6833 r = (char_u *)"{...}";
6834 }
6835 else
6836 {
6837 tv->vval.v_dict->dv_copyID = copyID;
6838 *tofree = dict2string(tv, copyID);
6839 r = *tofree;
6840 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006841 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006842
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006843 case VAR_STRING:
6844 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006845 *tofree = NULL;
6846 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006847 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006848
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006849 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006850 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006851 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006852 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006853
6854 --recurse;
6855 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006856}
6857
6858/*
6859 * Return a string with the string representation of a variable.
6860 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6861 * "numbuf" is used for a number.
6862 * Puts quotes around strings, so that they can be parsed back by eval().
6863 * May return NULL;
6864 */
6865 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006866tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006867 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006868 char_u **tofree;
6869 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006870 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006871{
6872 switch (tv->v_type)
6873 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006874 case VAR_FUNC:
6875 *tofree = string_quote(tv->vval.v_string, TRUE);
6876 return *tofree;
6877 case VAR_STRING:
6878 *tofree = string_quote(tv->vval.v_string, FALSE);
6879 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006880 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006881 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006882 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006883 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006884 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006885 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006886 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006887 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006888}
6889
6890/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006891 * Return string "str" in ' quotes, doubling ' characters.
6892 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006893 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006894 */
6895 static char_u *
6896string_quote(str, function)
6897 char_u *str;
6898 int function;
6899{
Bram Moolenaar33570922005-01-25 22:26:29 +00006900 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006901 char_u *p, *r, *s;
6902
Bram Moolenaar33570922005-01-25 22:26:29 +00006903 len = (function ? 13 : 3);
6904 if (str != NULL)
6905 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006906 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00006907 for (p = str; *p != NUL; mb_ptr_adv(p))
6908 if (*p == '\'')
6909 ++len;
6910 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006911 s = r = alloc(len);
6912 if (r != NULL)
6913 {
6914 if (function)
6915 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006916 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006917 r += 10;
6918 }
6919 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006920 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006921 if (str != NULL)
6922 for (p = str; *p != NUL; )
6923 {
6924 if (*p == '\'')
6925 *r++ = '\'';
6926 MB_COPY_CHAR(p, r);
6927 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006928 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006929 if (function)
6930 *r++ = ')';
6931 *r++ = NUL;
6932 }
6933 return s;
6934}
6935
6936/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 * Get the value of an environment variable.
6938 * "arg" is pointing to the '$'. It is advanced to after the name.
6939 * If the environment variable was not set, silently assume it is empty.
6940 * Always return OK.
6941 */
6942 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006943get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006945 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946 int evaluate;
6947{
6948 char_u *string = NULL;
6949 int len;
6950 int cc;
6951 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006952 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953
6954 ++*arg;
6955 name = *arg;
6956 len = get_env_len(arg);
6957 if (evaluate)
6958 {
6959 if (len != 0)
6960 {
6961 cc = name[len];
6962 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006963 /* first try vim_getenv(), fast for normal environment vars */
6964 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006966 {
6967 if (!mustfree)
6968 string = vim_strsave(string);
6969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 else
6971 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00006972 if (mustfree)
6973 vim_free(string);
6974
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975 /* next try expanding things like $VIM and ${HOME} */
6976 string = expand_env_save(name - 1);
6977 if (string != NULL && *string == '$')
6978 {
6979 vim_free(string);
6980 string = NULL;
6981 }
6982 }
6983 name[len] = cc;
6984 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006985 rettv->v_type = VAR_STRING;
6986 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987 }
6988
6989 return OK;
6990}
6991
6992/*
6993 * Array with names and number of arguments of all internal functions
6994 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
6995 */
6996static struct fst
6997{
6998 char *f_name; /* function name */
6999 char f_min_argc; /* minimal number of arguments */
7000 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007001 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007002 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003} functions[] =
7004{
Bram Moolenaar0d660222005-01-07 21:51:51 +00007005 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 {"append", 2, 2, f_append},
7007 {"argc", 0, 0, f_argc},
7008 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007009 {"argv", 0, 1, f_argv},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007011 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012 {"bufexists", 1, 1, f_bufexists},
7013 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7014 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7015 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7016 {"buflisted", 1, 1, f_buflisted},
7017 {"bufloaded", 1, 1, f_bufloaded},
7018 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007019 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 {"bufwinnr", 1, 1, f_bufwinnr},
7021 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007022 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007023 {"call", 2, 3, f_call},
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007024 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025 {"char2nr", 1, 1, f_char2nr},
7026 {"cindent", 1, 1, f_cindent},
7027 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007028#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007029 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007030 {"complete_add", 1, 1, f_complete_add},
7031 {"complete_check", 0, 0, f_complete_check},
7032#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007034 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007035 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007037 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007038 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039 {"delete", 1, 1, f_delete},
7040 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007041 {"diff_filler", 1, 1, f_diff_filler},
7042 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007043 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007045 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 {"eventhandler", 0, 0, f_eventhandler},
7047 {"executable", 1, 1, f_executable},
7048 {"exists", 1, 1, f_exists},
7049 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007050 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007051 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7053 {"filereadable", 1, 1, f_filereadable},
7054 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007055 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007056 {"finddir", 1, 3, f_finddir},
7057 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 {"fnamemodify", 2, 2, f_fnamemodify},
7059 {"foldclosed", 1, 1, f_foldclosed},
7060 {"foldclosedend", 1, 1, f_foldclosedend},
7061 {"foldlevel", 1, 1, f_foldlevel},
7062 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007063 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007065 {"function", 1, 1, f_function},
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007066 {"garbagecollect", 0, 0, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007067 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007068 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069 {"getbufvar", 2, 2, f_getbufvar},
7070 {"getchar", 0, 1, f_getchar},
7071 {"getcharmod", 0, 0, f_getcharmod},
7072 {"getcmdline", 0, 0, f_getcmdline},
7073 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007074 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007076 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007077 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 {"getfsize", 1, 1, f_getfsize},
7079 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007080 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007081 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007082 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaara5525202006-03-02 22:52:09 +00007083 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007084 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007085 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007087 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 {"getwinposx", 0, 0, f_getwinposx},
7089 {"getwinposy", 0, 0, f_getwinposy},
7090 {"getwinvar", 2, 2, f_getwinvar},
7091 {"glob", 1, 1, f_glob},
7092 {"globpath", 2, 2, f_globpath},
7093 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007094 {"has_key", 2, 2, f_has_key},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007095 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7097 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7098 {"histadd", 2, 2, f_histadd},
7099 {"histdel", 1, 2, f_histdel},
7100 {"histget", 1, 2, f_histget},
7101 {"histnr", 1, 1, f_histnr},
7102 {"hlID", 1, 1, f_hlID},
7103 {"hlexists", 1, 1, f_hlexists},
7104 {"hostname", 0, 0, f_hostname},
7105 {"iconv", 3, 3, f_iconv},
7106 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007107 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007108 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007110 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111 {"inputrestore", 0, 0, f_inputrestore},
7112 {"inputsave", 0, 0, f_inputsave},
7113 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007114 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007116 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007117 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007118 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007119 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007121 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122 {"libcall", 3, 3, f_libcall},
7123 {"libcallnr", 3, 3, f_libcallnr},
7124 {"line", 1, 1, f_line},
7125 {"line2byte", 1, 1, f_line2byte},
7126 {"lispindent", 1, 1, f_lispindent},
7127 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007128 {"map", 2, 2, f_map},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007129 {"maparg", 1, 3, f_maparg},
7130 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007131 {"match", 2, 4, f_match},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007132 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007133 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007134 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007135 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007136 {"max", 1, 1, f_max},
7137 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007138#ifdef vim_mkdir
7139 {"mkdir", 1, 3, f_mkdir},
7140#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 {"mode", 0, 0, f_mode},
7142 {"nextnonblank", 1, 1, f_nextnonblank},
7143 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007144 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007146 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007147 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007148 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007149 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007150 {"reltime", 0, 2, f_reltime},
7151 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152 {"remote_expr", 2, 3, f_remote_expr},
7153 {"remote_foreground", 1, 1, f_remote_foreground},
7154 {"remote_peek", 1, 2, f_remote_peek},
7155 {"remote_read", 1, 1, f_remote_read},
7156 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007157 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007159 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007161 {"reverse", 1, 1, f_reverse},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00007162 {"search", 1, 3, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007163 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00007164 {"searchpair", 3, 6, f_searchpair},
7165 {"searchpairpos", 3, 6, f_searchpairpos},
7166 {"searchpos", 1, 3, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167 {"server2client", 2, 2, f_server2client},
7168 {"serverlist", 0, 0, f_serverlist},
7169 {"setbufvar", 3, 3, f_setbufvar},
7170 {"setcmdpos", 1, 1, f_setcmdpos},
7171 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007172 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007173 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007174 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175 {"setreg", 2, 3, f_setreg},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007176 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar60a495f2006-10-03 12:44:42 +00007178 {"shellescape", 1, 1, f_shellescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007180 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007181 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007182 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007183 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007184 {"split", 1, 3, f_split},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007185 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186#ifdef HAVE_STRFTIME
7187 {"strftime", 1, 2, f_strftime},
7188#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007189 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007190 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 {"strlen", 1, 1, f_strlen},
7192 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007193 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194 {"strtrans", 1, 1, f_strtrans},
7195 {"submatch", 1, 1, f_submatch},
7196 {"substitute", 4, 4, f_substitute},
7197 {"synID", 3, 3, f_synID},
7198 {"synIDattr", 2, 3, f_synIDattr},
7199 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007200 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007201 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007202 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007203 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007204 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007205 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007207 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208 {"tolower", 1, 1, f_tolower},
7209 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007210 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007212 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 {"virtcol", 1, 1, f_virtcol},
7214 {"visualmode", 0, 1, f_visualmode},
7215 {"winbufnr", 1, 1, f_winbufnr},
7216 {"wincol", 0, 0, f_wincol},
7217 {"winheight", 1, 1, f_winheight},
7218 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007219 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007221 {"winrestview", 1, 1, f_winrestview},
7222 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007224 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225};
7226
7227#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7228
7229/*
7230 * Function given to ExpandGeneric() to obtain the list of internal
7231 * or user defined function names.
7232 */
7233 char_u *
7234get_function_name(xp, idx)
7235 expand_T *xp;
7236 int idx;
7237{
7238 static int intidx = -1;
7239 char_u *name;
7240
7241 if (idx == 0)
7242 intidx = -1;
7243 if (intidx < 0)
7244 {
7245 name = get_user_func_name(xp, idx);
7246 if (name != NULL)
7247 return name;
7248 }
7249 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7250 {
7251 STRCPY(IObuff, functions[intidx].f_name);
7252 STRCAT(IObuff, "(");
7253 if (functions[intidx].f_max_argc == 0)
7254 STRCAT(IObuff, ")");
7255 return IObuff;
7256 }
7257
7258 return NULL;
7259}
7260
7261/*
7262 * Function given to ExpandGeneric() to obtain the list of internal or
7263 * user defined variable or function names.
7264 */
7265/*ARGSUSED*/
7266 char_u *
7267get_expr_name(xp, idx)
7268 expand_T *xp;
7269 int idx;
7270{
7271 static int intidx = -1;
7272 char_u *name;
7273
7274 if (idx == 0)
7275 intidx = -1;
7276 if (intidx < 0)
7277 {
7278 name = get_function_name(xp, idx);
7279 if (name != NULL)
7280 return name;
7281 }
7282 return get_user_var_name(xp, ++intidx);
7283}
7284
7285#endif /* FEAT_CMDL_COMPL */
7286
7287/*
7288 * Find internal function in table above.
7289 * Return index, or -1 if not found
7290 */
7291 static int
7292find_internal_func(name)
7293 char_u *name; /* name of the function */
7294{
7295 int first = 0;
7296 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7297 int cmp;
7298 int x;
7299
7300 /*
7301 * Find the function name in the table. Binary search.
7302 */
7303 while (first <= last)
7304 {
7305 x = first + ((unsigned)(last - first) >> 1);
7306 cmp = STRCMP(name, functions[x].f_name);
7307 if (cmp < 0)
7308 last = x - 1;
7309 else if (cmp > 0)
7310 first = x + 1;
7311 else
7312 return x;
7313 }
7314 return -1;
7315}
7316
7317/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007318 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7319 * name it contains, otherwise return "name".
7320 */
7321 static char_u *
7322deref_func_name(name, lenp)
7323 char_u *name;
7324 int *lenp;
7325{
Bram Moolenaar33570922005-01-25 22:26:29 +00007326 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007327 int cc;
7328
7329 cc = name[*lenp];
7330 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007331 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007332 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007333 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007334 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007335 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007336 {
7337 *lenp = 0;
7338 return (char_u *)""; /* just in case */
7339 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007340 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00007341 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007342 }
7343
7344 return name;
7345}
7346
7347/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348 * Allocate a variable for the result of a function.
7349 * Return OK or FAIL.
7350 */
7351 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007352get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7353 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354 char_u *name; /* name of the function */
7355 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007356 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357 char_u **arg; /* argument, pointing to the '(' */
7358 linenr_T firstline; /* first line of range */
7359 linenr_T lastline; /* last line of range */
7360 int *doesrange; /* return: function handled range */
7361 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007362 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363{
7364 char_u *argp;
7365 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007366 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 int argcount = 0; /* number of arguments found */
7368
7369 /*
7370 * Get the arguments.
7371 */
7372 argp = *arg;
7373 while (argcount < MAX_FUNC_ARGS)
7374 {
7375 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7376 if (*argp == ')' || *argp == ',' || *argp == NUL)
7377 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7379 {
7380 ret = FAIL;
7381 break;
7382 }
7383 ++argcount;
7384 if (*argp != ',')
7385 break;
7386 }
7387 if (*argp == ')')
7388 ++argp;
7389 else
7390 ret = FAIL;
7391
7392 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007393 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007394 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007396 {
7397 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007398 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007399 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007400 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402
7403 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007404 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405
7406 *arg = skipwhite(argp);
7407 return ret;
7408}
7409
7410
7411/*
7412 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00007413 * Return OK when the function can't be called, FAIL otherwise.
7414 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415 */
7416 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007417call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007418 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419 char_u *name; /* name of the function */
7420 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007421 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007423 typval_T *argvars; /* vars for arguments, must have "argcount"
7424 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425 linenr_T firstline; /* first line of range */
7426 linenr_T lastline; /* last line of range */
7427 int *doesrange; /* return: function handled range */
7428 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007429 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430{
7431 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432#define ERROR_UNKNOWN 0
7433#define ERROR_TOOMANY 1
7434#define ERROR_TOOFEW 2
7435#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007436#define ERROR_DICT 4
7437#define ERROR_NONE 5
7438#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439 int error = ERROR_NONE;
7440 int i;
7441 int llen;
7442 ufunc_T *fp;
7443 int cc;
7444#define FLEN_FIXED 40
7445 char_u fname_buf[FLEN_FIXED + 1];
7446 char_u *fname;
7447
7448 /*
7449 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7450 * Change <SNR>123_name() to K_SNR 123_name().
7451 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7452 */
7453 cc = name[len];
7454 name[len] = NUL;
7455 llen = eval_fname_script(name);
7456 if (llen > 0)
7457 {
7458 fname_buf[0] = K_SPECIAL;
7459 fname_buf[1] = KS_EXTRA;
7460 fname_buf[2] = (int)KE_SNR;
7461 i = 3;
7462 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7463 {
7464 if (current_SID <= 0)
7465 error = ERROR_SCRIPT;
7466 else
7467 {
7468 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7469 i = (int)STRLEN(fname_buf);
7470 }
7471 }
7472 if (i + STRLEN(name + llen) < FLEN_FIXED)
7473 {
7474 STRCPY(fname_buf + i, name + llen);
7475 fname = fname_buf;
7476 }
7477 else
7478 {
7479 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7480 if (fname == NULL)
7481 error = ERROR_OTHER;
7482 else
7483 {
7484 mch_memmove(fname, fname_buf, (size_t)i);
7485 STRCPY(fname + i, name + llen);
7486 }
7487 }
7488 }
7489 else
7490 fname = name;
7491
7492 *doesrange = FALSE;
7493
7494
7495 /* execute the function if no errors detected and executing */
7496 if (evaluate && error == ERROR_NONE)
7497 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007498 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499 error = ERROR_UNKNOWN;
7500
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007501 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502 {
7503 /*
7504 * User defined function.
7505 */
7506 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007507
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007509 /* Trigger FuncUndefined event, may load the function. */
7510 if (fp == NULL
7511 && apply_autocmds(EVENT_FUNCUNDEFINED,
7512 fname, fname, TRUE, NULL)
7513 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007515 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516 fp = find_func(fname);
7517 }
7518#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007519 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007520 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007521 {
7522 /* loaded a package, search for the function again */
7523 fp = find_func(fname);
7524 }
7525
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526 if (fp != NULL)
7527 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007528 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007530 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007532 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007534 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007535 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536 else
7537 {
7538 /*
7539 * Call the user function.
7540 * Save and restore search patterns, script variables and
7541 * redo buffer.
7542 */
7543 save_search_patterns();
7544 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007545 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007546 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007547 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007548 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7549 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7550 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007551 /* Function was unreferenced while being used, free it
7552 * now. */
7553 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554 restoreRedobuff();
7555 restore_search_patterns();
7556 error = ERROR_NONE;
7557 }
7558 }
7559 }
7560 else
7561 {
7562 /*
7563 * Find the function name in the table, call its implementation.
7564 */
7565 i = find_internal_func(fname);
7566 if (i >= 0)
7567 {
7568 if (argcount < functions[i].f_min_argc)
7569 error = ERROR_TOOFEW;
7570 else if (argcount > functions[i].f_max_argc)
7571 error = ERROR_TOOMANY;
7572 else
7573 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007574 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007575 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576 error = ERROR_NONE;
7577 }
7578 }
7579 }
7580 /*
7581 * The function call (or "FuncUndefined" autocommand sequence) might
7582 * have been aborted by an error, an interrupt, or an explicitly thrown
7583 * exception that has not been caught so far. This situation can be
7584 * tested for by calling aborting(). For an error in an internal
7585 * function or for the "E132" error in call_user_func(), however, the
7586 * throw point at which the "force_abort" flag (temporarily reset by
7587 * emsg()) is normally updated has not been reached yet. We need to
7588 * update that flag first to make aborting() reliable.
7589 */
7590 update_force_abort();
7591 }
7592 if (error == ERROR_NONE)
7593 ret = OK;
7594
7595 /*
7596 * Report an error unless the argument evaluation or function call has been
7597 * cancelled due to an aborting error, an interrupt, or an exception.
7598 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007599 if (!aborting())
7600 {
7601 switch (error)
7602 {
7603 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007604 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007605 break;
7606 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007607 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007608 break;
7609 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007610 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00007611 name);
7612 break;
7613 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007614 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00007615 name);
7616 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007617 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007618 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00007619 name);
7620 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007621 }
7622 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623
7624 name[len] = cc;
7625 if (fname != name && fname != fname_buf)
7626 vim_free(fname);
7627
7628 return ret;
7629}
7630
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007631/*
7632 * Give an error message with a function name. Handle <SNR> things.
7633 */
7634 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00007635emsg_funcname(ermsg, name)
7636 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007637 char_u *name;
7638{
7639 char_u *p;
7640
7641 if (*name == K_SPECIAL)
7642 p = concat_str((char_u *)"<SNR>", name + 3);
7643 else
7644 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00007645 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007646 if (p != name)
7647 vim_free(p);
7648}
7649
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650/*********************************************
7651 * Implementation of the built-in functions
7652 */
7653
7654/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007655 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656 */
7657 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007658f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007659 typval_T *argvars;
7660 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661{
Bram Moolenaar33570922005-01-25 22:26:29 +00007662 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007664 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007665 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007667 if ((l = argvars[0].vval.v_list) != NULL
7668 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7669 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007670 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007671 }
7672 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007673 EMSG(_(e_listreq));
7674}
7675
7676/*
7677 * "append(lnum, string/list)" function
7678 */
7679 static void
7680f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007681 typval_T *argvars;
7682 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007683{
7684 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007685 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007686 list_T *l = NULL;
7687 listitem_T *li = NULL;
7688 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007689 long added = 0;
7690
Bram Moolenaar0d660222005-01-07 21:51:51 +00007691 lnum = get_tv_lnum(argvars);
7692 if (lnum >= 0
7693 && lnum <= curbuf->b_ml.ml_line_count
7694 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007695 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007696 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007697 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007698 l = argvars[1].vval.v_list;
7699 if (l == NULL)
7700 return;
7701 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007702 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007703 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007704 for (;;)
7705 {
7706 if (l == NULL)
7707 tv = &argvars[1]; /* append a string */
7708 else if (li == NULL)
7709 break; /* end of list */
7710 else
7711 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007712 line = get_tv_string_chk(tv);
7713 if (line == NULL) /* type error */
7714 {
7715 rettv->vval.v_number = 1; /* Failed */
7716 break;
7717 }
7718 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007719 ++added;
7720 if (l == NULL)
7721 break;
7722 li = li->li_next;
7723 }
7724
7725 appended_lines_mark(lnum, added);
7726 if (curwin->w_cursor.lnum > lnum)
7727 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007729 else
7730 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731}
7732
7733/*
7734 * "argc()" function
7735 */
7736/* ARGSUSED */
7737 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007738f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007739 typval_T *argvars;
7740 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007742 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743}
7744
7745/*
7746 * "argidx()" function
7747 */
7748/* ARGSUSED */
7749 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007750f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007751 typval_T *argvars;
7752 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007754 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755}
7756
7757/*
7758 * "argv(nr)" function
7759 */
7760 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007761f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007762 typval_T *argvars;
7763 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007764{
7765 int idx;
7766
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007767 if (argvars[0].v_type != VAR_UNKNOWN)
7768 {
7769 idx = get_tv_number_chk(&argvars[0], NULL);
7770 if (idx >= 0 && idx < ARGCOUNT)
7771 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
7772 else
7773 rettv->vval.v_string = NULL;
7774 rettv->v_type = VAR_STRING;
7775 }
7776 else if (rettv_list_alloc(rettv) == OK)
7777 for (idx = 0; idx < ARGCOUNT; ++idx)
7778 list_append_string(rettv->vval.v_list,
7779 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780}
7781
7782/*
7783 * "browse(save, title, initdir, default)" function
7784 */
7785/* ARGSUSED */
7786 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007787f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007788 typval_T *argvars;
7789 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790{
7791#ifdef FEAT_BROWSE
7792 int save;
7793 char_u *title;
7794 char_u *initdir;
7795 char_u *defname;
7796 char_u buf[NUMBUFLEN];
7797 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007798 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007800 save = get_tv_number_chk(&argvars[0], &error);
7801 title = get_tv_string_chk(&argvars[1]);
7802 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7803 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007805 if (error || title == NULL || initdir == NULL || defname == NULL)
7806 rettv->vval.v_string = NULL;
7807 else
7808 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007809 do_browse(save ? BROWSE_SAVE : 0,
7810 title, defname, NULL, initdir, NULL, curbuf);
7811#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007812 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007813#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007814 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007815}
7816
7817/*
7818 * "browsedir(title, initdir)" function
7819 */
7820/* ARGSUSED */
7821 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007822f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007823 typval_T *argvars;
7824 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007825{
7826#ifdef FEAT_BROWSE
7827 char_u *title;
7828 char_u *initdir;
7829 char_u buf[NUMBUFLEN];
7830
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007831 title = get_tv_string_chk(&argvars[0]);
7832 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007833
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007834 if (title == NULL || initdir == NULL)
7835 rettv->vval.v_string = NULL;
7836 else
7837 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007838 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007840 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007842 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843}
7844
Bram Moolenaar33570922005-01-25 22:26:29 +00007845static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007846
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847/*
7848 * Find a buffer by number or exact name.
7849 */
7850 static buf_T *
7851find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007852 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853{
7854 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007856 if (avar->v_type == VAR_NUMBER)
7857 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007858 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007860 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007861 if (buf == NULL)
7862 {
7863 /* No full path name match, try a match with a URL or a "nofile"
7864 * buffer, these don't use the full path. */
7865 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7866 if (buf->b_fname != NULL
7867 && (path_with_url(buf->b_fname)
7868#ifdef FEAT_QUICKFIX
7869 || bt_nofile(buf)
7870#endif
7871 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007872 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007873 break;
7874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875 }
7876 return buf;
7877}
7878
7879/*
7880 * "bufexists(expr)" function
7881 */
7882 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007883f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007884 typval_T *argvars;
7885 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007887 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888}
7889
7890/*
7891 * "buflisted(expr)" function
7892 */
7893 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007894f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007895 typval_T *argvars;
7896 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897{
7898 buf_T *buf;
7899
7900 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007901 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902}
7903
7904/*
7905 * "bufloaded(expr)" function
7906 */
7907 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007908f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007909 typval_T *argvars;
7910 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911{
7912 buf_T *buf;
7913
7914 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007915 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916}
7917
Bram Moolenaar33570922005-01-25 22:26:29 +00007918static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007919
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920/*
7921 * Get buffer by number or pattern.
7922 */
7923 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007924get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007925 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007927 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928 int save_magic;
7929 char_u *save_cpo;
7930 buf_T *buf;
7931
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007932 if (tv->v_type == VAR_NUMBER)
7933 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007934 if (tv->v_type != VAR_STRING)
7935 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936 if (name == NULL || *name == NUL)
7937 return curbuf;
7938 if (name[0] == '$' && name[1] == NUL)
7939 return lastbuf;
7940
7941 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7942 save_magic = p_magic;
7943 p_magic = TRUE;
7944 save_cpo = p_cpo;
7945 p_cpo = (char_u *)"";
7946
7947 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
7948 TRUE, FALSE));
7949
7950 p_magic = save_magic;
7951 p_cpo = save_cpo;
7952
7953 /* If not found, try expanding the name, like done for bufexists(). */
7954 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007955 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956
7957 return buf;
7958}
7959
7960/*
7961 * "bufname(expr)" function
7962 */
7963 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007964f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007965 typval_T *argvars;
7966 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967{
7968 buf_T *buf;
7969
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007970 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007972 buf = get_buf_tv(&argvars[0]);
7973 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007975 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007977 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978 --emsg_off;
7979}
7980
7981/*
7982 * "bufnr(expr)" function
7983 */
7984 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007985f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007986 typval_T *argvars;
7987 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988{
7989 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007990 int error = FALSE;
7991 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007993 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007995 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007996 --emsg_off;
7997
7998 /* If the buffer isn't found and the second argument is not zero create a
7999 * new buffer. */
8000 if (buf == NULL
8001 && argvars[1].v_type != VAR_UNKNOWN
8002 && get_tv_number_chk(&argvars[1], &error) != 0
8003 && !error
8004 && (name = get_tv_string_chk(&argvars[0])) != NULL
8005 && !error)
8006 buf = buflist_new(name, NULL, (linenr_T)1, 0);
8007
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008009 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008011 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012}
8013
8014/*
8015 * "bufwinnr(nr)" function
8016 */
8017 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008018f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008019 typval_T *argvars;
8020 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021{
8022#ifdef FEAT_WINDOWS
8023 win_T *wp;
8024 int winnr = 0;
8025#endif
8026 buf_T *buf;
8027
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008028 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008030 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031#ifdef FEAT_WINDOWS
8032 for (wp = firstwin; wp; wp = wp->w_next)
8033 {
8034 ++winnr;
8035 if (wp->w_buffer == buf)
8036 break;
8037 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008038 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008040 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041#endif
8042 --emsg_off;
8043}
8044
8045/*
8046 * "byte2line(byte)" function
8047 */
8048/*ARGSUSED*/
8049 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008050f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008051 typval_T *argvars;
8052 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053{
8054#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008055 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056#else
8057 long boff = 0;
8058
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008059 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008061 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008063 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064 (linenr_T)0, &boff);
8065#endif
8066}
8067
8068/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008069 * "byteidx()" function
8070 */
8071/*ARGSUSED*/
8072 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008073f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008074 typval_T *argvars;
8075 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008076{
8077#ifdef FEAT_MBYTE
8078 char_u *t;
8079#endif
8080 char_u *str;
8081 long idx;
8082
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008083 str = get_tv_string_chk(&argvars[0]);
8084 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008085 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008086 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008087 return;
8088
8089#ifdef FEAT_MBYTE
8090 t = str;
8091 for ( ; idx > 0; idx--)
8092 {
8093 if (*t == NUL) /* EOL reached */
8094 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008095 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008096 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008097 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008098#else
8099 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008100 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008101#endif
8102}
8103
8104/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008105 * "call(func, arglist)" function
8106 */
8107 static void
8108f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008109 typval_T *argvars;
8110 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008111{
8112 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008113 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008114 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008115 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008116 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00008117 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008118
8119 rettv->vval.v_number = 0;
8120 if (argvars[1].v_type != VAR_LIST)
8121 {
8122 EMSG(_(e_listreq));
8123 return;
8124 }
8125 if (argvars[1].vval.v_list == NULL)
8126 return;
8127
8128 if (argvars[0].v_type == VAR_FUNC)
8129 func = argvars[0].vval.v_string;
8130 else
8131 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008132 if (*func == NUL)
8133 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008134
Bram Moolenaare9a41262005-01-15 22:18:47 +00008135 if (argvars[2].v_type != VAR_UNKNOWN)
8136 {
8137 if (argvars[2].v_type != VAR_DICT)
8138 {
8139 EMSG(_(e_dictreq));
8140 return;
8141 }
8142 selfdict = argvars[2].vval.v_dict;
8143 }
8144
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008145 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
8146 item = item->li_next)
8147 {
8148 if (argc == MAX_FUNC_ARGS)
8149 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008150 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008151 break;
8152 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008153 /* Make a copy of each argument. This is needed to be able to set
8154 * v_lock to VAR_FIXED in the copy without changing the original list.
8155 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008156 copy_tv(&item->li_tv, &argv[argc++]);
8157 }
8158
8159 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008160 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008161 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
8162 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008163
8164 /* Free the arguments. */
8165 while (argc > 0)
8166 clear_tv(&argv[--argc]);
8167}
8168
8169/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008170 * "changenr()" function
8171 */
8172/*ARGSUSED*/
8173 static void
8174f_changenr(argvars, rettv)
8175 typval_T *argvars;
8176 typval_T *rettv;
8177{
8178 rettv->vval.v_number = curbuf->b_u_seq_cur;
8179}
8180
8181/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182 * "char2nr(string)" function
8183 */
8184 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008185f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008186 typval_T *argvars;
8187 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008188{
8189#ifdef FEAT_MBYTE
8190 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008191 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192 else
8193#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008194 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195}
8196
8197/*
8198 * "cindent(lnum)" function
8199 */
8200 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008201f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008202 typval_T *argvars;
8203 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204{
8205#ifdef FEAT_CINDENT
8206 pos_T pos;
8207 linenr_T lnum;
8208
8209 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008210 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008211 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8212 {
8213 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008214 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215 curwin->w_cursor = pos;
8216 }
8217 else
8218#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008219 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220}
8221
8222/*
8223 * "col(string)" function
8224 */
8225 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008226f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008227 typval_T *argvars;
8228 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229{
8230 colnr_T col = 0;
8231 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008232 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008234 fp = var2fpos(&argvars[0], FALSE, &fnum);
8235 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236 {
8237 if (fp->col == MAXCOL)
8238 {
8239 /* '> can be MAXCOL, get the length of the line then */
8240 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008241 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242 else
8243 col = MAXCOL;
8244 }
8245 else
8246 {
8247 col = fp->col + 1;
8248#ifdef FEAT_VIRTUALEDIT
8249 /* col(".") when the cursor is on the NUL at the end of the line
8250 * because of "coladd" can be seen as an extra column. */
8251 if (virtual_active() && fp == &curwin->w_cursor)
8252 {
8253 char_u *p = ml_get_cursor();
8254
8255 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
8256 curwin->w_virtcol - curwin->w_cursor.coladd))
8257 {
8258# ifdef FEAT_MBYTE
8259 int l;
8260
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008261 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262 col += l;
8263# else
8264 if (*p != NUL && p[1] == NUL)
8265 ++col;
8266# endif
8267 }
8268 }
8269#endif
8270 }
8271 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008272 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273}
8274
Bram Moolenaar572cb562005-08-05 21:35:02 +00008275#if defined(FEAT_INS_EXPAND)
8276/*
Bram Moolenaarade00832006-03-10 21:46:58 +00008277 * "complete()" function
8278 */
8279/*ARGSUSED*/
8280 static void
8281f_complete(argvars, rettv)
8282 typval_T *argvars;
8283 typval_T *rettv;
8284{
8285 int startcol;
8286
8287 if ((State & INSERT) == 0)
8288 {
8289 EMSG(_("E785: complete() can only be used in Insert mode"));
8290 return;
8291 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00008292
8293 /* Check for undo allowed here, because if something was already inserted
8294 * the line was already saved for undo and this check isn't done. */
8295 if (!undo_allowed())
8296 return;
8297
Bram Moolenaarade00832006-03-10 21:46:58 +00008298 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
8299 {
8300 EMSG(_(e_invarg));
8301 return;
8302 }
8303
8304 startcol = get_tv_number_chk(&argvars[0], NULL);
8305 if (startcol <= 0)
8306 return;
8307
8308 set_completion(startcol - 1, argvars[1].vval.v_list);
8309}
8310
8311/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00008312 * "complete_add()" function
8313 */
8314/*ARGSUSED*/
8315 static void
8316f_complete_add(argvars, rettv)
8317 typval_T *argvars;
8318 typval_T *rettv;
8319{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00008320 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00008321}
8322
8323/*
8324 * "complete_check()" function
8325 */
8326/*ARGSUSED*/
8327 static void
8328f_complete_check(argvars, rettv)
8329 typval_T *argvars;
8330 typval_T *rettv;
8331{
8332 int saved = RedrawingDisabled;
8333
8334 RedrawingDisabled = 0;
8335 ins_compl_check_keys(0);
8336 rettv->vval.v_number = compl_interrupted;
8337 RedrawingDisabled = saved;
8338}
8339#endif
8340
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341/*
8342 * "confirm(message, buttons[, default [, type]])" function
8343 */
8344/*ARGSUSED*/
8345 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008346f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008347 typval_T *argvars;
8348 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349{
8350#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
8351 char_u *message;
8352 char_u *buttons = NULL;
8353 char_u buf[NUMBUFLEN];
8354 char_u buf2[NUMBUFLEN];
8355 int def = 1;
8356 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008357 char_u *typestr;
8358 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008360 message = get_tv_string_chk(&argvars[0]);
8361 if (message == NULL)
8362 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008363 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008365 buttons = get_tv_string_buf_chk(&argvars[1], buf);
8366 if (buttons == NULL)
8367 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008368 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008370 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008371 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008373 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
8374 if (typestr == NULL)
8375 error = TRUE;
8376 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008378 switch (TOUPPER_ASC(*typestr))
8379 {
8380 case 'E': type = VIM_ERROR; break;
8381 case 'Q': type = VIM_QUESTION; break;
8382 case 'I': type = VIM_INFO; break;
8383 case 'W': type = VIM_WARNING; break;
8384 case 'G': type = VIM_GENERIC; break;
8385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386 }
8387 }
8388 }
8389 }
8390
8391 if (buttons == NULL || *buttons == NUL)
8392 buttons = (char_u *)_("&Ok");
8393
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008394 if (error)
8395 rettv->vval.v_number = 0;
8396 else
8397 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008398 def, NULL);
8399#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008400 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008401#endif
8402}
8403
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008404/*
8405 * "copy()" function
8406 */
8407 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008408f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008409 typval_T *argvars;
8410 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008411{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008412 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008413}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414
8415/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008416 * "count()" function
8417 */
8418 static void
8419f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008420 typval_T *argvars;
8421 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008422{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008423 long n = 0;
8424 int ic = FALSE;
8425
Bram Moolenaare9a41262005-01-15 22:18:47 +00008426 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008427 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008428 listitem_T *li;
8429 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008430 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008431
Bram Moolenaare9a41262005-01-15 22:18:47 +00008432 if ((l = argvars[0].vval.v_list) != NULL)
8433 {
8434 li = l->lv_first;
8435 if (argvars[2].v_type != VAR_UNKNOWN)
8436 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008437 int error = FALSE;
8438
8439 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008440 if (argvars[3].v_type != VAR_UNKNOWN)
8441 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008442 idx = get_tv_number_chk(&argvars[3], &error);
8443 if (!error)
8444 {
8445 li = list_find(l, idx);
8446 if (li == NULL)
8447 EMSGN(_(e_listidx), idx);
8448 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008449 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008450 if (error)
8451 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008452 }
8453
8454 for ( ; li != NULL; li = li->li_next)
8455 if (tv_equal(&li->li_tv, &argvars[1], ic))
8456 ++n;
8457 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008458 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008459 else if (argvars[0].v_type == VAR_DICT)
8460 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008461 int todo;
8462 dict_T *d;
8463 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008464
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008465 if ((d = argvars[0].vval.v_dict) != NULL)
8466 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008467 int error = FALSE;
8468
Bram Moolenaare9a41262005-01-15 22:18:47 +00008469 if (argvars[2].v_type != VAR_UNKNOWN)
8470 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008471 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008472 if (argvars[3].v_type != VAR_UNKNOWN)
8473 EMSG(_(e_invarg));
8474 }
8475
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008476 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008477 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008478 {
8479 if (!HASHITEM_EMPTY(hi))
8480 {
8481 --todo;
8482 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
8483 ++n;
8484 }
8485 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008486 }
8487 }
8488 else
8489 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008490 rettv->vval.v_number = n;
8491}
8492
8493/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8495 *
8496 * Checks the existence of a cscope connection.
8497 */
8498/*ARGSUSED*/
8499 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008500f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008501 typval_T *argvars;
8502 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503{
8504#ifdef FEAT_CSCOPE
8505 int num = 0;
8506 char_u *dbpath = NULL;
8507 char_u *prepend = NULL;
8508 char_u buf[NUMBUFLEN];
8509
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008510 if (argvars[0].v_type != VAR_UNKNOWN
8511 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008513 num = (int)get_tv_number(&argvars[0]);
8514 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008515 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008516 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 }
8518
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008519 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008521 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522#endif
8523}
8524
8525/*
8526 * "cursor(lnum, col)" function
8527 *
8528 * Moves the cursor to the specified line and column
8529 */
8530/*ARGSUSED*/
8531 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008532f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008533 typval_T *argvars;
8534 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535{
8536 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008537#ifdef FEAT_VIRTUALEDIT
8538 long coladd = 0;
8539#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540
Bram Moolenaara5525202006-03-02 22:52:09 +00008541 if (argvars[1].v_type == VAR_UNKNOWN)
8542 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008543 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00008544
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008545 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00008546 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008547 line = pos.lnum;
8548 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008549#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008550 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00008551#endif
8552 }
8553 else
8554 {
8555 line = get_tv_lnum(argvars);
8556 col = get_tv_number_chk(&argvars[1], NULL);
8557#ifdef FEAT_VIRTUALEDIT
8558 if (argvars[2].v_type != VAR_UNKNOWN)
8559 coladd = get_tv_number_chk(&argvars[2], NULL);
8560#endif
8561 }
8562 if (line < 0 || col < 0
8563#ifdef FEAT_VIRTUALEDIT
8564 || coladd < 0
8565#endif
8566 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008567 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568 if (line > 0)
8569 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008570 if (col > 0)
8571 curwin->w_cursor.col = col - 1;
8572#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00008573 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574#endif
8575
8576 /* Make sure the cursor is in a valid position. */
8577 check_cursor();
8578#ifdef FEAT_MBYTE
8579 /* Correct cursor for multi-byte character. */
8580 if (has_mbyte)
8581 mb_adjust_cursor();
8582#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008583
8584 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585}
8586
8587/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008588 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589 */
8590 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008591f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008592 typval_T *argvars;
8593 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008595 int noref = 0;
8596
8597 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008598 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008599 if (noref < 0 || noref > 1)
8600 EMSG(_(e_invarg));
8601 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008602 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008603}
8604
8605/*
8606 * "delete()" function
8607 */
8608 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008609f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008610 typval_T *argvars;
8611 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612{
8613 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008614 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008616 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617}
8618
8619/*
8620 * "did_filetype()" function
8621 */
8622/*ARGSUSED*/
8623 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008624f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008625 typval_T *argvars;
8626 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627{
8628#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008629 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008631 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632#endif
8633}
8634
8635/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008636 * "diff_filler()" function
8637 */
8638/*ARGSUSED*/
8639 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008640f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008641 typval_T *argvars;
8642 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008643{
8644#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008645 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008646#endif
8647}
8648
8649/*
8650 * "diff_hlID()" function
8651 */
8652/*ARGSUSED*/
8653 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008654f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008655 typval_T *argvars;
8656 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008657{
8658#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008659 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008660 static linenr_T prev_lnum = 0;
8661 static int changedtick = 0;
8662 static int fnum = 0;
8663 static int change_start = 0;
8664 static int change_end = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008665 static hlf_T hlID = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008666 int filler_lines;
8667 int col;
8668
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008669 if (lnum < 0) /* ignore type error in {lnum} arg */
8670 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008671 if (lnum != prev_lnum
8672 || changedtick != curbuf->b_changedtick
8673 || fnum != curbuf->b_fnum)
8674 {
8675 /* New line, buffer, change: need to get the values. */
8676 filler_lines = diff_check(curwin, lnum);
8677 if (filler_lines < 0)
8678 {
8679 if (filler_lines == -1)
8680 {
8681 change_start = MAXCOL;
8682 change_end = -1;
8683 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8684 hlID = HLF_ADD; /* added line */
8685 else
8686 hlID = HLF_CHD; /* changed line */
8687 }
8688 else
8689 hlID = HLF_ADD; /* added line */
8690 }
8691 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008692 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008693 prev_lnum = lnum;
8694 changedtick = curbuf->b_changedtick;
8695 fnum = curbuf->b_fnum;
8696 }
8697
8698 if (hlID == HLF_CHD || hlID == HLF_TXD)
8699 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008700 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008701 if (col >= change_start && col <= change_end)
8702 hlID = HLF_TXD; /* changed text */
8703 else
8704 hlID = HLF_CHD; /* changed line */
8705 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008706 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008707#endif
8708}
8709
8710/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008711 * "empty({expr})" function
8712 */
8713 static void
8714f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008715 typval_T *argvars;
8716 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008717{
8718 int n;
8719
8720 switch (argvars[0].v_type)
8721 {
8722 case VAR_STRING:
8723 case VAR_FUNC:
8724 n = argvars[0].vval.v_string == NULL
8725 || *argvars[0].vval.v_string == NUL;
8726 break;
8727 case VAR_NUMBER:
8728 n = argvars[0].vval.v_number == 0;
8729 break;
8730 case VAR_LIST:
8731 n = argvars[0].vval.v_list == NULL
8732 || argvars[0].vval.v_list->lv_first == NULL;
8733 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008734 case VAR_DICT:
8735 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008736 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008737 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008738 default:
8739 EMSG2(_(e_intern2), "f_empty()");
8740 n = 0;
8741 }
8742
8743 rettv->vval.v_number = n;
8744}
8745
8746/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747 * "escape({string}, {chars})" function
8748 */
8749 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008750f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008751 typval_T *argvars;
8752 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753{
8754 char_u buf[NUMBUFLEN];
8755
Bram Moolenaar758711c2005-02-02 23:11:38 +00008756 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8757 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008758 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008759}
8760
8761/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008762 * "eval()" function
8763 */
8764/*ARGSUSED*/
8765 static void
8766f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008767 typval_T *argvars;
8768 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008769{
8770 char_u *s;
8771
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008772 s = get_tv_string_chk(&argvars[0]);
8773 if (s != NULL)
8774 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008775
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008776 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8777 {
8778 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008779 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008780 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008781 else if (*s != NUL)
8782 EMSG(_(e_trailing));
8783}
8784
8785/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008786 * "eventhandler()" function
8787 */
8788/*ARGSUSED*/
8789 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008790f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008791 typval_T *argvars;
8792 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008794 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008795}
8796
8797/*
8798 * "executable()" function
8799 */
8800 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008801f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008802 typval_T *argvars;
8803 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008805 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008806}
8807
8808/*
8809 * "exists()" function
8810 */
8811 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008812f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008813 typval_T *argvars;
8814 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815{
8816 char_u *p;
8817 char_u *name;
8818 int n = FALSE;
8819 int len = 0;
8820
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008821 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822 if (*p == '$') /* environment variable */
8823 {
8824 /* first try "normal" environment variables (fast) */
8825 if (mch_getenv(p + 1) != NULL)
8826 n = TRUE;
8827 else
8828 {
8829 /* try expanding things like $VIM and ${HOME} */
8830 p = expand_env_save(p);
8831 if (p != NULL && *p != '$')
8832 n = TRUE;
8833 vim_free(p);
8834 }
8835 }
8836 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00008837 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008838 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00008839 if (*skipwhite(p) != NUL)
8840 n = FALSE; /* trailing garbage */
8841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842 else if (*p == '*') /* internal or user defined function */
8843 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008844 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845 }
8846 else if (*p == ':')
8847 {
8848 n = cmd_exists(p + 1);
8849 }
8850 else if (*p == '#')
8851 {
8852#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00008853 if (p[1] == '#')
8854 n = autocmd_supported(p + 2);
8855 else
8856 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857#endif
8858 }
8859 else /* internal variable */
8860 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008861 char_u *tofree;
8862 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008863
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008864 /* get_name_len() takes care of expanding curly braces */
8865 name = p;
8866 len = get_name_len(&p, &tofree, TRUE, FALSE);
8867 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008869 if (tofree != NULL)
8870 name = tofree;
8871 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8872 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008873 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008874 /* handle d.key, l[idx], f(expr) */
8875 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8876 if (n)
8877 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878 }
8879 }
Bram Moolenaar79783442006-05-05 21:18:03 +00008880 if (*p != NUL)
8881 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008883 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884 }
8885
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008886 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887}
8888
8889/*
8890 * "expand()" function
8891 */
8892 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008893f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008894 typval_T *argvars;
8895 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008896{
8897 char_u *s;
8898 int len;
8899 char_u *errormsg;
8900 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8901 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008902 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008903
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008904 rettv->v_type = VAR_STRING;
8905 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906 if (*s == '%' || *s == '#' || *s == '<')
8907 {
8908 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008909 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008910 --emsg_off;
8911 }
8912 else
8913 {
8914 /* When the optional second argument is non-zero, don't remove matches
8915 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008916 if (argvars[1].v_type != VAR_UNKNOWN
8917 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008919 if (!error)
8920 {
8921 ExpandInit(&xpc);
8922 xpc.xp_context = EXPAND_FILES;
8923 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008924 }
8925 else
8926 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927 }
8928}
8929
8930/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008931 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00008932 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008933 */
8934 static void
8935f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008936 typval_T *argvars;
8937 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008938{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008939 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008940 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008941 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008942 list_T *l1, *l2;
8943 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008944 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008945 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008946
Bram Moolenaare9a41262005-01-15 22:18:47 +00008947 l1 = argvars[0].vval.v_list;
8948 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008949 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
8950 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008951 {
8952 if (argvars[2].v_type != VAR_UNKNOWN)
8953 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008954 before = get_tv_number_chk(&argvars[2], &error);
8955 if (error)
8956 return; /* type error; errmsg already given */
8957
Bram Moolenaar758711c2005-02-02 23:11:38 +00008958 if (before == l1->lv_len)
8959 item = NULL;
8960 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008961 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00008962 item = list_find(l1, before);
8963 if (item == NULL)
8964 {
8965 EMSGN(_(e_listidx), before);
8966 return;
8967 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008968 }
8969 }
8970 else
8971 item = NULL;
8972 list_extend(l1, l2, item);
8973
Bram Moolenaare9a41262005-01-15 22:18:47 +00008974 copy_tv(&argvars[0], rettv);
8975 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008976 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008977 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
8978 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008979 dict_T *d1, *d2;
8980 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008981 char_u *action;
8982 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00008983 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008984 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008985
8986 d1 = argvars[0].vval.v_dict;
8987 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008988 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
8989 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008990 {
8991 /* Check the third argument. */
8992 if (argvars[2].v_type != VAR_UNKNOWN)
8993 {
8994 static char *(av[]) = {"keep", "force", "error"};
8995
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008996 action = get_tv_string_chk(&argvars[2]);
8997 if (action == NULL)
8998 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008999 for (i = 0; i < 3; ++i)
9000 if (STRCMP(action, av[i]) == 0)
9001 break;
9002 if (i == 3)
9003 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009004 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009005 return;
9006 }
9007 }
9008 else
9009 action = (char_u *)"force";
9010
9011 /* Go over all entries in the second dict and add them to the
9012 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009013 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009014 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009015 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009016 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009017 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009018 --todo;
9019 di1 = dict_find(d1, hi2->hi_key, -1);
9020 if (di1 == NULL)
9021 {
9022 di1 = dictitem_copy(HI2DI(hi2));
9023 if (di1 != NULL && dict_add(d1, di1) == FAIL)
9024 dictitem_free(di1);
9025 }
9026 else if (*action == 'e')
9027 {
9028 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
9029 break;
9030 }
9031 else if (*action == 'f')
9032 {
9033 clear_tv(&di1->di_tv);
9034 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
9035 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009036 }
9037 }
9038
Bram Moolenaare9a41262005-01-15 22:18:47 +00009039 copy_tv(&argvars[0], rettv);
9040 }
9041 }
9042 else
9043 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009044}
9045
9046/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009047 * "feedkeys()" function
9048 */
9049/*ARGSUSED*/
9050 static void
9051f_feedkeys(argvars, rettv)
9052 typval_T *argvars;
9053 typval_T *rettv;
9054{
9055 int remap = TRUE;
9056 char_u *keys, *flags;
9057 char_u nbuf[NUMBUFLEN];
9058 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009059 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009060
9061 rettv->vval.v_number = 0;
9062 keys = get_tv_string(&argvars[0]);
9063 if (*keys != NUL)
9064 {
9065 if (argvars[1].v_type != VAR_UNKNOWN)
9066 {
9067 flags = get_tv_string_buf(&argvars[1], nbuf);
9068 for ( ; *flags != NUL; ++flags)
9069 {
9070 switch (*flags)
9071 {
9072 case 'n': remap = FALSE; break;
9073 case 'm': remap = TRUE; break;
9074 case 't': typed = TRUE; break;
9075 }
9076 }
9077 }
9078
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009079 /* Need to escape K_SPECIAL and CSI before putting the string in the
9080 * typeahead buffer. */
9081 keys_esc = vim_strsave_escape_csi(keys);
9082 if (keys_esc != NULL)
9083 {
9084 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009085 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009086 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009087 if (vgetc_busy)
9088 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009089 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009090 }
9091}
9092
9093/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009094 * "filereadable()" function
9095 */
9096 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009097f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009098 typval_T *argvars;
9099 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009100{
9101 FILE *fd;
9102 char_u *p;
9103 int n;
9104
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009105 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
9107 {
9108 n = TRUE;
9109 fclose(fd);
9110 }
9111 else
9112 n = FALSE;
9113
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009114 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009115}
9116
9117/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009118 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00009119 * rights to write into.
9120 */
9121 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009122f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009123 typval_T *argvars;
9124 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009125{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009126 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009127}
9128
Bram Moolenaar33570922005-01-25 22:26:29 +00009129static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009130
9131 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00009132findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00009133 typval_T *argvars;
9134 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009135 int dir;
9136{
9137#ifdef FEAT_SEARCHPATH
9138 char_u *fname;
9139 char_u *fresult = NULL;
9140 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
9141 char_u *p;
9142 char_u pathbuf[NUMBUFLEN];
9143 int count = 1;
9144 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009145 int error = FALSE;
9146#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009147
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009148 rettv->vval.v_string = NULL;
9149 rettv->v_type = VAR_STRING;
9150
9151#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009152 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009153
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009154 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009155 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009156 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
9157 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009158 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009159 else
9160 {
9161 if (*p != NUL)
9162 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009163
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009164 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009165 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009166 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009167 }
9168
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009169 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
9170 error = TRUE;
9171
9172 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009173 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009174 do
9175 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009176 if (rettv->v_type == VAR_STRING)
9177 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009178 fresult = find_file_in_path_option(first ? fname : NULL,
9179 first ? (int)STRLEN(fname) : 0,
Bram Moolenaare580b0c2006-03-21 21:33:03 +00009180 0, first, path, dir, NULL,
9181 dir ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009182 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009183
9184 if (fresult != NULL && rettv->v_type == VAR_LIST)
9185 list_append_string(rettv->vval.v_list, fresult, -1);
9186
9187 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009188 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009189
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009190 if (rettv->v_type == VAR_STRING)
9191 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009192#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009193}
9194
Bram Moolenaar33570922005-01-25 22:26:29 +00009195static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
9196static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009197
9198/*
9199 * Implementation of map() and filter().
9200 */
9201 static void
9202filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00009203 typval_T *argvars;
9204 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009205 int map;
9206{
9207 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00009208 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00009209 listitem_T *li, *nli;
9210 list_T *l = NULL;
9211 dictitem_T *di;
9212 hashtab_T *ht;
9213 hashitem_T *hi;
9214 dict_T *d = NULL;
9215 typval_T save_val;
9216 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009217 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009218 int todo;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009219 char_u *ermsg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009220 int save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009221
9222 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009223 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009224 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009225 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009226 || (map && tv_check_lock(l->lv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009227 return;
9228 }
9229 else if (argvars[0].v_type == VAR_DICT)
9230 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009231 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009232 || (map && tv_check_lock(d->dv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009233 return;
9234 }
9235 else
9236 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009237 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009238 return;
9239 }
9240
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009241 expr = get_tv_string_buf_chk(&argvars[1], buf);
9242 /* On type errors, the preceding call has already displayed an error
9243 * message. Avoid a misleading error message for an empty string that
9244 * was not passed as argument. */
9245 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009246 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009247 prepare_vimvar(VV_VAL, &save_val);
9248 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009249
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009250 /* We reset "did_emsg" to be able to detect whether an error
9251 * occurred during evaluation of the expression. */
9252 save_did_emsg = did_emsg;
9253 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009254
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009255 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009256 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009257 prepare_vimvar(VV_KEY, &save_key);
9258 vimvars[VV_KEY].vv_type = VAR_STRING;
9259
9260 ht = &d->dv_hashtab;
9261 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009262 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009263 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009264 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009265 if (!HASHITEM_EMPTY(hi))
9266 {
9267 --todo;
9268 di = HI2DI(hi);
Bram Moolenaar89d40322006-08-29 15:30:07 +00009269 if (tv_check_lock(di->di_tv.v_lock, ermsg))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009270 break;
9271 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009272 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009273 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009274 break;
9275 if (!map && rem)
9276 dictitem_remove(d, di);
9277 clear_tv(&vimvars[VV_KEY].vv_tv);
9278 }
9279 }
9280 hash_unlock(ht);
9281
9282 restore_vimvar(VV_KEY, &save_key);
9283 }
9284 else
9285 {
9286 for (li = l->lv_first; li != NULL; li = nli)
9287 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009288 if (tv_check_lock(li->li_tv.v_lock, ermsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009289 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009290 nli = li->li_next;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009291 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009292 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009293 break;
9294 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009295 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009296 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009297 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009298
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009299 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009300
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009301 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009302 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009303
9304 copy_tv(&argvars[0], rettv);
9305}
9306
9307 static int
9308filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009309 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009310 char_u *expr;
9311 int map;
9312 int *remp;
9313{
Bram Moolenaar33570922005-01-25 22:26:29 +00009314 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009315 char_u *s;
9316
Bram Moolenaar33570922005-01-25 22:26:29 +00009317 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009318 s = expr;
9319 if (eval1(&s, &rettv, TRUE) == FAIL)
9320 return FAIL;
9321 if (*s != NUL) /* check for trailing chars after expr */
9322 {
9323 EMSG2(_(e_invexpr2), s);
9324 return FAIL;
9325 }
9326 if (map)
9327 {
9328 /* map(): replace the list item value */
9329 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +00009330 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009331 *tv = rettv;
9332 }
9333 else
9334 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009335 int error = FALSE;
9336
Bram Moolenaare9a41262005-01-15 22:18:47 +00009337 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009338 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009339 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009340 /* On type error, nothing has been removed; return FAIL to stop the
9341 * loop. The error message was given by get_tv_number_chk(). */
9342 if (error)
9343 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009344 }
Bram Moolenaar33570922005-01-25 22:26:29 +00009345 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009346 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009347}
9348
9349/*
9350 * "filter()" function
9351 */
9352 static void
9353f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009354 typval_T *argvars;
9355 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009356{
9357 filter_map(argvars, rettv, FALSE);
9358}
9359
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009360/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009361 * "finddir({fname}[, {path}[, {count}]])" function
9362 */
9363 static void
9364f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009365 typval_T *argvars;
9366 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009367{
9368 findfilendir(argvars, rettv, TRUE);
9369}
9370
9371/*
9372 * "findfile({fname}[, {path}[, {count}]])" function
9373 */
9374 static void
9375f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009376 typval_T *argvars;
9377 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009378{
9379 findfilendir(argvars, rettv, FALSE);
9380}
9381
9382/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009383 * "fnamemodify({fname}, {mods})" function
9384 */
9385 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009386f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009387 typval_T *argvars;
9388 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009389{
9390 char_u *fname;
9391 char_u *mods;
9392 int usedlen = 0;
9393 int len;
9394 char_u *fbuf = NULL;
9395 char_u buf[NUMBUFLEN];
9396
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009397 fname = get_tv_string_chk(&argvars[0]);
9398 mods = get_tv_string_buf_chk(&argvars[1], buf);
9399 if (fname == NULL || mods == NULL)
9400 fname = NULL;
9401 else
9402 {
9403 len = (int)STRLEN(fname);
9404 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
9405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009406
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009407 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009408 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009409 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009410 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009411 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009412 vim_free(fbuf);
9413}
9414
Bram Moolenaar33570922005-01-25 22:26:29 +00009415static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009416
9417/*
9418 * "foldclosed()" function
9419 */
9420 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009421foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00009422 typval_T *argvars;
9423 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009424 int end;
9425{
9426#ifdef FEAT_FOLDING
9427 linenr_T lnum;
9428 linenr_T first, last;
9429
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009430 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009431 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9432 {
9433 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
9434 {
9435 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009436 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009437 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009438 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439 return;
9440 }
9441 }
9442#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009443 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009444}
9445
9446/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009447 * "foldclosed()" function
9448 */
9449 static void
9450f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009451 typval_T *argvars;
9452 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009453{
9454 foldclosed_both(argvars, rettv, FALSE);
9455}
9456
9457/*
9458 * "foldclosedend()" function
9459 */
9460 static void
9461f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009462 typval_T *argvars;
9463 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009464{
9465 foldclosed_both(argvars, rettv, TRUE);
9466}
9467
9468/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469 * "foldlevel()" function
9470 */
9471 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009472f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009473 typval_T *argvars;
9474 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009475{
9476#ifdef FEAT_FOLDING
9477 linenr_T lnum;
9478
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009479 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009480 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009481 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009482 else
9483#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009484 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485}
9486
9487/*
9488 * "foldtext()" function
9489 */
9490/*ARGSUSED*/
9491 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009492f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009493 typval_T *argvars;
9494 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009495{
9496#ifdef FEAT_FOLDING
9497 linenr_T lnum;
9498 char_u *s;
9499 char_u *r;
9500 int len;
9501 char *txt;
9502#endif
9503
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009504 rettv->v_type = VAR_STRING;
9505 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009506#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00009507 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
9508 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
9509 <= curbuf->b_ml.ml_line_count
9510 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511 {
9512 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009513 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
9514 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009515 {
9516 if (!linewhite(lnum))
9517 break;
9518 ++lnum;
9519 }
9520
9521 /* Find interesting text in this line. */
9522 s = skipwhite(ml_get(lnum));
9523 /* skip C comment-start */
9524 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009525 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009526 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009527 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00009528 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009529 {
9530 s = skipwhite(ml_get(lnum + 1));
9531 if (*s == '*')
9532 s = skipwhite(s + 1);
9533 }
9534 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009535 txt = _("+-%s%3ld lines: ");
9536 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009537 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538 + 20 /* for %3ld */
9539 + STRLEN(s))); /* concatenated */
9540 if (r != NULL)
9541 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009542 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
9543 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
9544 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009545 len = (int)STRLEN(r);
9546 STRCAT(r, s);
9547 /* remove 'foldmarker' and 'commentstring' */
9548 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009549 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009550 }
9551 }
9552#endif
9553}
9554
9555/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009556 * "foldtextresult(lnum)" function
9557 */
9558/*ARGSUSED*/
9559 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009560f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009561 typval_T *argvars;
9562 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009563{
9564#ifdef FEAT_FOLDING
9565 linenr_T lnum;
9566 char_u *text;
9567 char_u buf[51];
9568 foldinfo_T foldinfo;
9569 int fold_count;
9570#endif
9571
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009572 rettv->v_type = VAR_STRING;
9573 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009574#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009575 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009576 /* treat illegal types and illegal string values for {lnum} the same */
9577 if (lnum < 0)
9578 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009579 fold_count = foldedCount(curwin, lnum, &foldinfo);
9580 if (fold_count > 0)
9581 {
9582 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
9583 &foldinfo, buf);
9584 if (text == buf)
9585 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009586 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009587 }
9588#endif
9589}
9590
9591/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009592 * "foreground()" function
9593 */
9594/*ARGSUSED*/
9595 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009596f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009597 typval_T *argvars;
9598 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009600 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601#ifdef FEAT_GUI
9602 if (gui.in_use)
9603 gui_mch_set_foreground();
9604#else
9605# ifdef WIN32
9606 win32_set_foreground();
9607# endif
9608#endif
9609}
9610
9611/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009612 * "function()" function
9613 */
9614/*ARGSUSED*/
9615 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009616f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009617 typval_T *argvars;
9618 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009619{
9620 char_u *s;
9621
Bram Moolenaara7043832005-01-21 11:56:39 +00009622 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009623 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009624 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009625 EMSG2(_(e_invarg2), s);
9626 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009627 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009628 else
9629 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009630 rettv->vval.v_string = vim_strsave(s);
9631 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009632 }
9633}
9634
9635/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009636 * "garbagecollect()" function
9637 */
9638/*ARGSUSED*/
9639 static void
9640f_garbagecollect(argvars, rettv)
9641 typval_T *argvars;
9642 typval_T *rettv;
9643{
Bram Moolenaar9fecb462006-09-05 10:59:47 +00009644 /* This is postponed until we are back at the toplevel, because we may be
9645 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
9646 want_garbage_collect = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009647}
9648
9649/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009650 * "get()" function
9651 */
9652 static void
9653f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009654 typval_T *argvars;
9655 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009656{
Bram Moolenaar33570922005-01-25 22:26:29 +00009657 listitem_T *li;
9658 list_T *l;
9659 dictitem_T *di;
9660 dict_T *d;
9661 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009662
Bram Moolenaare9a41262005-01-15 22:18:47 +00009663 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009664 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009665 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009666 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009667 int error = FALSE;
9668
9669 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9670 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009671 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009672 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009673 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009674 else if (argvars[0].v_type == VAR_DICT)
9675 {
9676 if ((d = argvars[0].vval.v_dict) != NULL)
9677 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009678 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009679 if (di != NULL)
9680 tv = &di->di_tv;
9681 }
9682 }
9683 else
9684 EMSG2(_(e_listdictarg), "get()");
9685
9686 if (tv == NULL)
9687 {
9688 if (argvars[2].v_type == VAR_UNKNOWN)
9689 rettv->vval.v_number = 0;
9690 else
9691 copy_tv(&argvars[2], rettv);
9692 }
9693 else
9694 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009695}
9696
Bram Moolenaar342337a2005-07-21 21:11:17 +00009697static 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 +00009698
9699/*
9700 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +00009701 * Return a range (from start to end) of lines in rettv from the specified
9702 * buffer.
9703 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009704 */
9705 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +00009706get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009707 buf_T *buf;
9708 linenr_T start;
9709 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009710 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009711 typval_T *rettv;
9712{
9713 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009714
Bram Moolenaar342337a2005-07-21 21:11:17 +00009715 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009716 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009717 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009718 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009719 }
9720 else
9721 rettv->vval.v_number = 0;
9722
9723 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
9724 return;
9725
9726 if (!retlist)
9727 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009728 if (start >= 1 && start <= buf->b_ml.ml_line_count)
9729 p = ml_get_buf(buf, start, FALSE);
9730 else
9731 p = (char_u *)"";
9732
9733 rettv->v_type = VAR_STRING;
9734 rettv->vval.v_string = vim_strsave(p);
9735 }
9736 else
9737 {
9738 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009739 return;
9740
9741 if (start < 1)
9742 start = 1;
9743 if (end > buf->b_ml.ml_line_count)
9744 end = buf->b_ml.ml_line_count;
9745 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009746 if (list_append_string(rettv->vval.v_list,
9747 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009748 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009749 }
9750}
9751
9752/*
9753 * "getbufline()" function
9754 */
9755 static void
9756f_getbufline(argvars, rettv)
9757 typval_T *argvars;
9758 typval_T *rettv;
9759{
9760 linenr_T lnum;
9761 linenr_T end;
9762 buf_T *buf;
9763
9764 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9765 ++emsg_off;
9766 buf = get_buf_tv(&argvars[0]);
9767 --emsg_off;
9768
Bram Moolenaar661b1822005-07-28 22:36:45 +00009769 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009770 if (argvars[2].v_type == VAR_UNKNOWN)
9771 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009772 else
Bram Moolenaar661b1822005-07-28 22:36:45 +00009773 end = get_tv_lnum_buf(&argvars[2], buf);
9774
Bram Moolenaar342337a2005-07-21 21:11:17 +00009775 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009776}
9777
Bram Moolenaar0d660222005-01-07 21:51:51 +00009778/*
9779 * "getbufvar()" function
9780 */
9781 static void
9782f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009783 typval_T *argvars;
9784 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009785{
9786 buf_T *buf;
9787 buf_T *save_curbuf;
9788 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009789 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009790
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009791 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9792 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009793 ++emsg_off;
9794 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009795
9796 rettv->v_type = VAR_STRING;
9797 rettv->vval.v_string = NULL;
9798
9799 if (buf != NULL && varname != NULL)
9800 {
9801 if (*varname == '&') /* buffer-local-option */
9802 {
9803 /* set curbuf to be our buf, temporarily */
9804 save_curbuf = curbuf;
9805 curbuf = buf;
9806
9807 get_option_tv(&varname, rettv, TRUE);
9808
9809 /* restore previous notion of curbuf */
9810 curbuf = save_curbuf;
9811 }
9812 else
9813 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009814 if (*varname == NUL)
9815 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9816 * scope prefix before the NUL byte is required by
9817 * find_var_in_ht(). */
9818 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009819 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009820 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009821 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009822 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009823 }
9824 }
9825
9826 --emsg_off;
9827}
9828
9829/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009830 * "getchar()" function
9831 */
9832 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009833f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009834 typval_T *argvars;
9835 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009836{
9837 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009838 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009839
Bram Moolenaar4015b2c2006-06-22 19:01:34 +00009840 /* Position the cursor. Needed after a message that ends in a space. */
9841 windgoto(msg_row, msg_col);
9842
Bram Moolenaar071d4272004-06-13 20:20:40 +00009843 ++no_mapping;
9844 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009845 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009846 /* getchar(): blocking wait. */
9847 n = safe_vgetc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009848 else if (get_tv_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009849 /* getchar(1): only check if char avail */
9850 n = vpeekc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009851 else if (error || vpeekc() == NUL)
9852 /* illegal argument or getchar(0) and no char avail: return zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009853 n = 0;
9854 else
9855 /* getchar(0) and char avail: return char */
9856 n = safe_vgetc();
9857 --no_mapping;
9858 --allow_keys;
9859
Bram Moolenaar219b8702006-11-01 14:32:36 +00009860 vimvars[VV_MOUSE_WIN].vv_nr = 0;
9861 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
9862 vimvars[VV_MOUSE_COL].vv_nr = 0;
9863
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009864 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009865 if (IS_SPECIAL(n) || mod_mask != 0)
9866 {
9867 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9868 int i = 0;
9869
9870 /* Turn a special key into three bytes, plus modifier. */
9871 if (mod_mask != 0)
9872 {
9873 temp[i++] = K_SPECIAL;
9874 temp[i++] = KS_MODIFIER;
9875 temp[i++] = mod_mask;
9876 }
9877 if (IS_SPECIAL(n))
9878 {
9879 temp[i++] = K_SPECIAL;
9880 temp[i++] = K_SECOND(n);
9881 temp[i++] = K_THIRD(n);
9882 }
9883#ifdef FEAT_MBYTE
9884 else if (has_mbyte)
9885 i += (*mb_char2bytes)(n, temp + i);
9886#endif
9887 else
9888 temp[i++] = n;
9889 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009890 rettv->v_type = VAR_STRING;
9891 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +00009892
9893#ifdef FEAT_MOUSE
9894 if (n == K_LEFTMOUSE
9895 || n == K_LEFTMOUSE_NM
9896 || n == K_LEFTDRAG
9897 || n == K_LEFTRELEASE
9898 || n == K_LEFTRELEASE_NM
9899 || n == K_MIDDLEMOUSE
9900 || n == K_MIDDLEDRAG
9901 || n == K_MIDDLERELEASE
9902 || n == K_RIGHTMOUSE
9903 || n == K_RIGHTDRAG
9904 || n == K_RIGHTRELEASE
9905 || n == K_X1MOUSE
9906 || n == K_X1DRAG
9907 || n == K_X1RELEASE
9908 || n == K_X2MOUSE
9909 || n == K_X2DRAG
9910 || n == K_X2RELEASE
9911 || n == K_MOUSEDOWN
9912 || n == K_MOUSEUP)
9913 {
9914 int row = mouse_row;
9915 int col = mouse_col;
9916 win_T *win;
9917 linenr_T lnum;
9918# ifdef FEAT_WINDOWS
9919 win_T *wp;
9920# endif
9921 int n = 1;
9922
9923 if (row >= 0 && col >= 0)
9924 {
9925 /* Find the window at the mouse coordinates and compute the
9926 * text position. */
9927 win = mouse_find_win(&row, &col);
9928 (void)mouse_comp_pos(win, &row, &col, &lnum);
9929# ifdef FEAT_WINDOWS
9930 for (wp = firstwin; wp != win; wp = wp->w_next)
9931 ++n;
9932# endif
9933 vimvars[VV_MOUSE_WIN].vv_nr = n;
9934 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
9935 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
9936 }
9937 }
9938#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009939 }
9940}
9941
9942/*
9943 * "getcharmod()" function
9944 */
9945/*ARGSUSED*/
9946 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009947f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009948 typval_T *argvars;
9949 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009950{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009951 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009952}
9953
9954/*
9955 * "getcmdline()" function
9956 */
9957/*ARGSUSED*/
9958 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009959f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009960 typval_T *argvars;
9961 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009962{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009963 rettv->v_type = VAR_STRING;
9964 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009965}
9966
9967/*
9968 * "getcmdpos()" function
9969 */
9970/*ARGSUSED*/
9971 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009972f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009973 typval_T *argvars;
9974 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009975{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009976 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009977}
9978
9979/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00009980 * "getcmdtype()" function
9981 */
9982/*ARGSUSED*/
9983 static void
9984f_getcmdtype(argvars, rettv)
9985 typval_T *argvars;
9986 typval_T *rettv;
9987{
9988 rettv->v_type = VAR_STRING;
9989 rettv->vval.v_string = alloc(2);
9990 if (rettv->vval.v_string != NULL)
9991 {
9992 rettv->vval.v_string[0] = get_cmdline_type();
9993 rettv->vval.v_string[1] = NUL;
9994 }
9995}
9996
9997/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009998 * "getcwd()" function
9999 */
10000/*ARGSUSED*/
10001 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010002f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010003 typval_T *argvars;
10004 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010005{
10006 char_u cwd[MAXPATHL];
10007
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010008 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010009 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010010 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010011 else
10012 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010013 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010014#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +000010015 if (rettv->vval.v_string != NULL)
10016 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010017#endif
10018 }
10019}
10020
10021/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010022 * "getfontname()" function
10023 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010024/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010025 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010026f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010027 typval_T *argvars;
10028 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010029{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010030 rettv->v_type = VAR_STRING;
10031 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010032#ifdef FEAT_GUI
10033 if (gui.in_use)
10034 {
10035 GuiFont font;
10036 char_u *name = NULL;
10037
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010038 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010039 {
10040 /* Get the "Normal" font. Either the name saved by
10041 * hl_set_font_name() or from the font ID. */
10042 font = gui.norm_font;
10043 name = hl_get_font_name();
10044 }
10045 else
10046 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010047 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010048 if (STRCMP(name, "*") == 0) /* don't use font dialog */
10049 return;
10050 font = gui_mch_get_font(name, FALSE);
10051 if (font == NOFONT)
10052 return; /* Invalid font name, return empty string. */
10053 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010054 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010055 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010056 gui_mch_free_font(font);
10057 }
10058#endif
10059}
10060
10061/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010062 * "getfperm({fname})" function
10063 */
10064 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010065f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010066 typval_T *argvars;
10067 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010068{
10069 char_u *fname;
10070 struct stat st;
10071 char_u *perm = NULL;
10072 char_u flags[] = "rwx";
10073 int i;
10074
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010075 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010076
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010077 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010078 if (mch_stat((char *)fname, &st) >= 0)
10079 {
10080 perm = vim_strsave((char_u *)"---------");
10081 if (perm != NULL)
10082 {
10083 for (i = 0; i < 9; i++)
10084 {
10085 if (st.st_mode & (1 << (8 - i)))
10086 perm[i] = flags[i % 3];
10087 }
10088 }
10089 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010090 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010091}
10092
10093/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010094 * "getfsize({fname})" function
10095 */
10096 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010097f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010098 typval_T *argvars;
10099 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010100{
10101 char_u *fname;
10102 struct stat st;
10103
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010104 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010105
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010106 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010107
10108 if (mch_stat((char *)fname, &st) >= 0)
10109 {
10110 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010111 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010112 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010113 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010114 }
10115 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010116 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010117}
10118
10119/*
10120 * "getftime({fname})" function
10121 */
10122 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010123f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010124 typval_T *argvars;
10125 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010126{
10127 char_u *fname;
10128 struct stat st;
10129
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010130 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010131
10132 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010133 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010134 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010135 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010136}
10137
10138/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010139 * "getftype({fname})" function
10140 */
10141 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010142f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010143 typval_T *argvars;
10144 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010145{
10146 char_u *fname;
10147 struct stat st;
10148 char_u *type = NULL;
10149 char *t;
10150
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010151 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010152
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010153 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010154 if (mch_lstat((char *)fname, &st) >= 0)
10155 {
10156#ifdef S_ISREG
10157 if (S_ISREG(st.st_mode))
10158 t = "file";
10159 else if (S_ISDIR(st.st_mode))
10160 t = "dir";
10161# ifdef S_ISLNK
10162 else if (S_ISLNK(st.st_mode))
10163 t = "link";
10164# endif
10165# ifdef S_ISBLK
10166 else if (S_ISBLK(st.st_mode))
10167 t = "bdev";
10168# endif
10169# ifdef S_ISCHR
10170 else if (S_ISCHR(st.st_mode))
10171 t = "cdev";
10172# endif
10173# ifdef S_ISFIFO
10174 else if (S_ISFIFO(st.st_mode))
10175 t = "fifo";
10176# endif
10177# ifdef S_ISSOCK
10178 else if (S_ISSOCK(st.st_mode))
10179 t = "fifo";
10180# endif
10181 else
10182 t = "other";
10183#else
10184# ifdef S_IFMT
10185 switch (st.st_mode & S_IFMT)
10186 {
10187 case S_IFREG: t = "file"; break;
10188 case S_IFDIR: t = "dir"; break;
10189# ifdef S_IFLNK
10190 case S_IFLNK: t = "link"; break;
10191# endif
10192# ifdef S_IFBLK
10193 case S_IFBLK: t = "bdev"; break;
10194# endif
10195# ifdef S_IFCHR
10196 case S_IFCHR: t = "cdev"; break;
10197# endif
10198# ifdef S_IFIFO
10199 case S_IFIFO: t = "fifo"; break;
10200# endif
10201# ifdef S_IFSOCK
10202 case S_IFSOCK: t = "socket"; break;
10203# endif
10204 default: t = "other";
10205 }
10206# else
10207 if (mch_isdir(fname))
10208 t = "dir";
10209 else
10210 t = "file";
10211# endif
10212#endif
10213 type = vim_strsave((char_u *)t);
10214 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010215 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010216}
10217
10218/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010219 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000010220 */
10221 static void
10222f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010223 typval_T *argvars;
10224 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010225{
10226 linenr_T lnum;
10227 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010228 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010229
10230 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010231 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010232 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010233 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010234 retlist = FALSE;
10235 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010236 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000010237 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000010238 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010239 retlist = TRUE;
10240 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010241
Bram Moolenaar342337a2005-07-21 21:11:17 +000010242 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010243}
10244
10245/*
Bram Moolenaara5525202006-03-02 22:52:09 +000010246 * "getpos(string)" function
10247 */
10248 static void
10249f_getpos(argvars, rettv)
10250 typval_T *argvars;
10251 typval_T *rettv;
10252{
10253 pos_T *fp;
10254 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010255 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010256
10257 if (rettv_list_alloc(rettv) == OK)
10258 {
10259 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010260 fp = var2fpos(&argvars[0], TRUE, &fnum);
10261 if (fnum != -1)
10262 list_append_number(l, (varnumber_T)fnum);
10263 else
10264 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000010265 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
10266 : (varnumber_T)0);
10267 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->col + 1
10268 : (varnumber_T)0);
10269 list_append_number(l,
10270#ifdef FEAT_VIRTUALEDIT
10271 (fp != NULL) ? (varnumber_T)fp->coladd :
10272#endif
10273 (varnumber_T)0);
10274 }
10275 else
10276 rettv->vval.v_number = FALSE;
10277}
10278
10279/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000010280 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000010281 */
Bram Moolenaar280f1262006-01-30 00:14:18 +000010282/*ARGSUSED*/
Bram Moolenaar2641f772005-03-25 21:58:17 +000010283 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000010284f_getqflist(argvars, rettv)
10285 typval_T *argvars;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010286 typval_T *rettv;
10287{
10288#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000010289 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010290#endif
10291
10292 rettv->vval.v_number = FALSE;
10293#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010294 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000010295 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000010296 wp = NULL;
10297 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
10298 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010299 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010300 if (wp == NULL)
10301 return;
10302 }
10303
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010304 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000010305 }
10306#endif
10307}
10308
10309/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010310 * "getreg()" function
10311 */
10312 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010313f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010314 typval_T *argvars;
10315 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010316{
10317 char_u *strregname;
10318 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010319 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010320 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010321
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010322 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010323 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010324 strregname = get_tv_string_chk(&argvars[0]);
10325 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010326 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010327 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010328 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010329 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010330 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010331 regname = (strregname == NULL ? '"' : *strregname);
10332 if (regname == 0)
10333 regname = '"';
10334
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010335 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010336 rettv->vval.v_string = error ? NULL :
10337 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010338}
10339
10340/*
10341 * "getregtype()" function
10342 */
10343 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010344f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010345 typval_T *argvars;
10346 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010347{
10348 char_u *strregname;
10349 int regname;
10350 char_u buf[NUMBUFLEN + 2];
10351 long reglen = 0;
10352
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010353 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010354 {
10355 strregname = get_tv_string_chk(&argvars[0]);
10356 if (strregname == NULL) /* type error; errmsg already given */
10357 {
10358 rettv->v_type = VAR_STRING;
10359 rettv->vval.v_string = NULL;
10360 return;
10361 }
10362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010363 else
10364 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010365 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010366
10367 regname = (strregname == NULL ? '"' : *strregname);
10368 if (regname == 0)
10369 regname = '"';
10370
10371 buf[0] = NUL;
10372 buf[1] = NUL;
10373 switch (get_reg_type(regname, &reglen))
10374 {
10375 case MLINE: buf[0] = 'V'; break;
10376 case MCHAR: buf[0] = 'v'; break;
10377#ifdef FEAT_VISUAL
10378 case MBLOCK:
10379 buf[0] = Ctrl_V;
10380 sprintf((char *)buf + 1, "%ld", reglen + 1);
10381 break;
10382#endif
10383 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010384 rettv->v_type = VAR_STRING;
10385 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010386}
10387
10388/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010389 * "gettabwinvar()" function
10390 */
10391 static void
10392f_gettabwinvar(argvars, rettv)
10393 typval_T *argvars;
10394 typval_T *rettv;
10395{
10396 getwinvar(argvars, rettv, 1);
10397}
10398
10399/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010400 * "getwinposx()" function
10401 */
10402/*ARGSUSED*/
10403 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010404f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010405 typval_T *argvars;
10406 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010407{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010408 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010409#ifdef FEAT_GUI
10410 if (gui.in_use)
10411 {
10412 int x, y;
10413
10414 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010415 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010416 }
10417#endif
10418}
10419
10420/*
10421 * "getwinposy()" function
10422 */
10423/*ARGSUSED*/
10424 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010425f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010426 typval_T *argvars;
10427 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010428{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010429 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010430#ifdef FEAT_GUI
10431 if (gui.in_use)
10432 {
10433 int x, y;
10434
10435 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010436 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010437 }
10438#endif
10439}
10440
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010441/*
10442 * Find window specifed by "vp" in tabpage "tp".
10443 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000010444 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010445find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000010446 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010447 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000010448{
10449#ifdef FEAT_WINDOWS
10450 win_T *wp;
10451#endif
10452 int nr;
10453
10454 nr = get_tv_number_chk(vp, NULL);
10455
10456#ifdef FEAT_WINDOWS
10457 if (nr < 0)
10458 return NULL;
10459 if (nr == 0)
10460 return curwin;
10461
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010462 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
10463 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000010464 if (--nr <= 0)
10465 break;
10466 return wp;
10467#else
10468 if (nr == 0 || nr == 1)
10469 return curwin;
10470 return NULL;
10471#endif
10472}
10473
Bram Moolenaar071d4272004-06-13 20:20:40 +000010474/*
10475 * "getwinvar()" function
10476 */
10477 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010478f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010479 typval_T *argvars;
10480 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010481{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010482 getwinvar(argvars, rettv, 0);
10483}
10484
10485/*
10486 * getwinvar() and gettabwinvar()
10487 */
10488 static void
10489getwinvar(argvars, rettv, off)
10490 typval_T *argvars;
10491 typval_T *rettv;
10492 int off; /* 1 for gettabwinvar() */
10493{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010494 win_T *win, *oldcurwin;
10495 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010496 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010497 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010498
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010499#ifdef FEAT_WINDOWS
10500 if (off == 1)
10501 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
10502 else
10503 tp = curtab;
10504#endif
10505 win = find_win_by_nr(&argvars[off], tp);
10506 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010507 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010508
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010509 rettv->v_type = VAR_STRING;
10510 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010511
10512 if (win != NULL && varname != NULL)
10513 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000010514 /* Set curwin to be our win, temporarily. Also set curbuf, so
10515 * that we can get buffer-local options. */
10516 oldcurwin = curwin;
10517 curwin = win;
10518 curbuf = win->w_buffer;
10519
Bram Moolenaar071d4272004-06-13 20:20:40 +000010520 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010521 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010522 else
10523 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010524 if (*varname == NUL)
10525 /* let getwinvar({nr}, "") return the "w:" dictionary. The
10526 * scope prefix before the NUL byte is required by
10527 * find_var_in_ht(). */
10528 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010529 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010530 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010531 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010532 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010533 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000010534
10535 /* restore previous notion of curwin */
10536 curwin = oldcurwin;
10537 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010538 }
10539
10540 --emsg_off;
10541}
10542
10543/*
10544 * "glob()" function
10545 */
10546 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010547f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010548 typval_T *argvars;
10549 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010550{
10551 expand_T xpc;
10552
10553 ExpandInit(&xpc);
10554 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010555 rettv->v_type = VAR_STRING;
10556 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010557 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010558}
10559
10560/*
10561 * "globpath()" function
10562 */
10563 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010564f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010565 typval_T *argvars;
10566 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010567{
10568 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010569 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010570
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010571 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010572 if (file == NULL)
10573 rettv->vval.v_string = NULL;
10574 else
10575 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010576}
10577
10578/*
10579 * "has()" function
10580 */
10581 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010582f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010583 typval_T *argvars;
10584 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010585{
10586 int i;
10587 char_u *name;
10588 int n = FALSE;
10589 static char *(has_list[]) =
10590 {
10591#ifdef AMIGA
10592 "amiga",
10593# ifdef FEAT_ARP
10594 "arp",
10595# endif
10596#endif
10597#ifdef __BEOS__
10598 "beos",
10599#endif
10600#ifdef MSDOS
10601# ifdef DJGPP
10602 "dos32",
10603# else
10604 "dos16",
10605# endif
10606#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000010607#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010608 "mac",
10609#endif
10610#if defined(MACOS_X_UNIX)
10611 "macunix",
10612#endif
10613#ifdef OS2
10614 "os2",
10615#endif
10616#ifdef __QNX__
10617 "qnx",
10618#endif
10619#ifdef RISCOS
10620 "riscos",
10621#endif
10622#ifdef UNIX
10623 "unix",
10624#endif
10625#ifdef VMS
10626 "vms",
10627#endif
10628#ifdef WIN16
10629 "win16",
10630#endif
10631#ifdef WIN32
10632 "win32",
10633#endif
10634#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
10635 "win32unix",
10636#endif
10637#ifdef WIN64
10638 "win64",
10639#endif
10640#ifdef EBCDIC
10641 "ebcdic",
10642#endif
10643#ifndef CASE_INSENSITIVE_FILENAME
10644 "fname_case",
10645#endif
10646#ifdef FEAT_ARABIC
10647 "arabic",
10648#endif
10649#ifdef FEAT_AUTOCMD
10650 "autocmd",
10651#endif
10652#ifdef FEAT_BEVAL
10653 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000010654# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
10655 "balloon_multiline",
10656# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010657#endif
10658#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
10659 "builtin_terms",
10660# ifdef ALL_BUILTIN_TCAPS
10661 "all_builtin_terms",
10662# endif
10663#endif
10664#ifdef FEAT_BYTEOFF
10665 "byte_offset",
10666#endif
10667#ifdef FEAT_CINDENT
10668 "cindent",
10669#endif
10670#ifdef FEAT_CLIENTSERVER
10671 "clientserver",
10672#endif
10673#ifdef FEAT_CLIPBOARD
10674 "clipboard",
10675#endif
10676#ifdef FEAT_CMDL_COMPL
10677 "cmdline_compl",
10678#endif
10679#ifdef FEAT_CMDHIST
10680 "cmdline_hist",
10681#endif
10682#ifdef FEAT_COMMENTS
10683 "comments",
10684#endif
10685#ifdef FEAT_CRYPT
10686 "cryptv",
10687#endif
10688#ifdef FEAT_CSCOPE
10689 "cscope",
10690#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010691#ifdef CURSOR_SHAPE
10692 "cursorshape",
10693#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010694#ifdef DEBUG
10695 "debug",
10696#endif
10697#ifdef FEAT_CON_DIALOG
10698 "dialog_con",
10699#endif
10700#ifdef FEAT_GUI_DIALOG
10701 "dialog_gui",
10702#endif
10703#ifdef FEAT_DIFF
10704 "diff",
10705#endif
10706#ifdef FEAT_DIGRAPHS
10707 "digraphs",
10708#endif
10709#ifdef FEAT_DND
10710 "dnd",
10711#endif
10712#ifdef FEAT_EMACS_TAGS
10713 "emacs_tags",
10714#endif
10715 "eval", /* always present, of course! */
10716#ifdef FEAT_EX_EXTRA
10717 "ex_extra",
10718#endif
10719#ifdef FEAT_SEARCH_EXTRA
10720 "extra_search",
10721#endif
10722#ifdef FEAT_FKMAP
10723 "farsi",
10724#endif
10725#ifdef FEAT_SEARCHPATH
10726 "file_in_path",
10727#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010728#if defined(UNIX) && !defined(USE_SYSTEM)
10729 "filterpipe",
10730#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010731#ifdef FEAT_FIND_ID
10732 "find_in_path",
10733#endif
10734#ifdef FEAT_FOLDING
10735 "folding",
10736#endif
10737#ifdef FEAT_FOOTER
10738 "footer",
10739#endif
10740#if !defined(USE_SYSTEM) && defined(UNIX)
10741 "fork",
10742#endif
10743#ifdef FEAT_GETTEXT
10744 "gettext",
10745#endif
10746#ifdef FEAT_GUI
10747 "gui",
10748#endif
10749#ifdef FEAT_GUI_ATHENA
10750# ifdef FEAT_GUI_NEXTAW
10751 "gui_neXtaw",
10752# else
10753 "gui_athena",
10754# endif
10755#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010756#ifdef FEAT_GUI_GTK
10757 "gui_gtk",
10758# ifdef HAVE_GTK2
10759 "gui_gtk2",
10760# endif
10761#endif
10762#ifdef FEAT_GUI_MAC
10763 "gui_mac",
10764#endif
10765#ifdef FEAT_GUI_MOTIF
10766 "gui_motif",
10767#endif
10768#ifdef FEAT_GUI_PHOTON
10769 "gui_photon",
10770#endif
10771#ifdef FEAT_GUI_W16
10772 "gui_win16",
10773#endif
10774#ifdef FEAT_GUI_W32
10775 "gui_win32",
10776#endif
10777#ifdef FEAT_HANGULIN
10778 "hangul_input",
10779#endif
10780#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10781 "iconv",
10782#endif
10783#ifdef FEAT_INS_EXPAND
10784 "insert_expand",
10785#endif
10786#ifdef FEAT_JUMPLIST
10787 "jumplist",
10788#endif
10789#ifdef FEAT_KEYMAP
10790 "keymap",
10791#endif
10792#ifdef FEAT_LANGMAP
10793 "langmap",
10794#endif
10795#ifdef FEAT_LIBCALL
10796 "libcall",
10797#endif
10798#ifdef FEAT_LINEBREAK
10799 "linebreak",
10800#endif
10801#ifdef FEAT_LISP
10802 "lispindent",
10803#endif
10804#ifdef FEAT_LISTCMDS
10805 "listcmds",
10806#endif
10807#ifdef FEAT_LOCALMAP
10808 "localmap",
10809#endif
10810#ifdef FEAT_MENU
10811 "menu",
10812#endif
10813#ifdef FEAT_SESSION
10814 "mksession",
10815#endif
10816#ifdef FEAT_MODIFY_FNAME
10817 "modify_fname",
10818#endif
10819#ifdef FEAT_MOUSE
10820 "mouse",
10821#endif
10822#ifdef FEAT_MOUSESHAPE
10823 "mouseshape",
10824#endif
10825#if defined(UNIX) || defined(VMS)
10826# ifdef FEAT_MOUSE_DEC
10827 "mouse_dec",
10828# endif
10829# ifdef FEAT_MOUSE_GPM
10830 "mouse_gpm",
10831# endif
10832# ifdef FEAT_MOUSE_JSB
10833 "mouse_jsbterm",
10834# endif
10835# ifdef FEAT_MOUSE_NET
10836 "mouse_netterm",
10837# endif
10838# ifdef FEAT_MOUSE_PTERM
10839 "mouse_pterm",
10840# endif
10841# ifdef FEAT_MOUSE_XTERM
10842 "mouse_xterm",
10843# endif
10844#endif
10845#ifdef FEAT_MBYTE
10846 "multi_byte",
10847#endif
10848#ifdef FEAT_MBYTE_IME
10849 "multi_byte_ime",
10850#endif
10851#ifdef FEAT_MULTI_LANG
10852 "multi_lang",
10853#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010854#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010855#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010856 "mzscheme",
10857#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010858#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010859#ifdef FEAT_OLE
10860 "ole",
10861#endif
10862#ifdef FEAT_OSFILETYPE
10863 "osfiletype",
10864#endif
10865#ifdef FEAT_PATH_EXTRA
10866 "path_extra",
10867#endif
10868#ifdef FEAT_PERL
10869#ifndef DYNAMIC_PERL
10870 "perl",
10871#endif
10872#endif
10873#ifdef FEAT_PYTHON
10874#ifndef DYNAMIC_PYTHON
10875 "python",
10876#endif
10877#endif
10878#ifdef FEAT_POSTSCRIPT
10879 "postscript",
10880#endif
10881#ifdef FEAT_PRINTER
10882 "printer",
10883#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000010884#ifdef FEAT_PROFILE
10885 "profile",
10886#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000010887#ifdef FEAT_RELTIME
10888 "reltime",
10889#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010890#ifdef FEAT_QUICKFIX
10891 "quickfix",
10892#endif
10893#ifdef FEAT_RIGHTLEFT
10894 "rightleft",
10895#endif
10896#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
10897 "ruby",
10898#endif
10899#ifdef FEAT_SCROLLBIND
10900 "scrollbind",
10901#endif
10902#ifdef FEAT_CMDL_INFO
10903 "showcmd",
10904 "cmdline_info",
10905#endif
10906#ifdef FEAT_SIGNS
10907 "signs",
10908#endif
10909#ifdef FEAT_SMARTINDENT
10910 "smartindent",
10911#endif
10912#ifdef FEAT_SNIFF
10913 "sniff",
10914#endif
10915#ifdef FEAT_STL_OPT
10916 "statusline",
10917#endif
10918#ifdef FEAT_SUN_WORKSHOP
10919 "sun_workshop",
10920#endif
10921#ifdef FEAT_NETBEANS_INTG
10922 "netbeans_intg",
10923#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000010924#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010925 "spell",
10926#endif
10927#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010928 "syntax",
10929#endif
10930#if defined(USE_SYSTEM) || !defined(UNIX)
10931 "system",
10932#endif
10933#ifdef FEAT_TAG_BINS
10934 "tag_binary",
10935#endif
10936#ifdef FEAT_TAG_OLDSTATIC
10937 "tag_old_static",
10938#endif
10939#ifdef FEAT_TAG_ANYWHITE
10940 "tag_any_white",
10941#endif
10942#ifdef FEAT_TCL
10943# ifndef DYNAMIC_TCL
10944 "tcl",
10945# endif
10946#endif
10947#ifdef TERMINFO
10948 "terminfo",
10949#endif
10950#ifdef FEAT_TERMRESPONSE
10951 "termresponse",
10952#endif
10953#ifdef FEAT_TEXTOBJ
10954 "textobjects",
10955#endif
10956#ifdef HAVE_TGETENT
10957 "tgetent",
10958#endif
10959#ifdef FEAT_TITLE
10960 "title",
10961#endif
10962#ifdef FEAT_TOOLBAR
10963 "toolbar",
10964#endif
10965#ifdef FEAT_USR_CMDS
10966 "user-commands", /* was accidentally included in 5.4 */
10967 "user_commands",
10968#endif
10969#ifdef FEAT_VIMINFO
10970 "viminfo",
10971#endif
10972#ifdef FEAT_VERTSPLIT
10973 "vertsplit",
10974#endif
10975#ifdef FEAT_VIRTUALEDIT
10976 "virtualedit",
10977#endif
10978#ifdef FEAT_VISUAL
10979 "visual",
10980#endif
10981#ifdef FEAT_VISUALEXTRA
10982 "visualextra",
10983#endif
10984#ifdef FEAT_VREPLACE
10985 "vreplace",
10986#endif
10987#ifdef FEAT_WILDIGN
10988 "wildignore",
10989#endif
10990#ifdef FEAT_WILDMENU
10991 "wildmenu",
10992#endif
10993#ifdef FEAT_WINDOWS
10994 "windows",
10995#endif
10996#ifdef FEAT_WAK
10997 "winaltkeys",
10998#endif
10999#ifdef FEAT_WRITEBACKUP
11000 "writebackup",
11001#endif
11002#ifdef FEAT_XIM
11003 "xim",
11004#endif
11005#ifdef FEAT_XFONTSET
11006 "xfontset",
11007#endif
11008#ifdef USE_XSMP
11009 "xsmp",
11010#endif
11011#ifdef USE_XSMP_INTERACT
11012 "xsmp_interact",
11013#endif
11014#ifdef FEAT_XCLIPBOARD
11015 "xterm_clipboard",
11016#endif
11017#ifdef FEAT_XTERM_SAVE
11018 "xterm_save",
11019#endif
11020#if defined(UNIX) && defined(FEAT_X11)
11021 "X11",
11022#endif
11023 NULL
11024 };
11025
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011026 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011027 for (i = 0; has_list[i] != NULL; ++i)
11028 if (STRICMP(name, has_list[i]) == 0)
11029 {
11030 n = TRUE;
11031 break;
11032 }
11033
11034 if (n == FALSE)
11035 {
11036 if (STRNICMP(name, "patch", 5) == 0)
11037 n = has_patch(atoi((char *)name + 5));
11038 else if (STRICMP(name, "vim_starting") == 0)
11039 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011040#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
11041 else if (STRICMP(name, "balloon_multiline") == 0)
11042 n = multiline_balloon_available();
11043#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011044#ifdef DYNAMIC_TCL
11045 else if (STRICMP(name, "tcl") == 0)
11046 n = tcl_enabled(FALSE);
11047#endif
11048#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
11049 else if (STRICMP(name, "iconv") == 0)
11050 n = iconv_enabled(FALSE);
11051#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000011052#ifdef DYNAMIC_MZSCHEME
11053 else if (STRICMP(name, "mzscheme") == 0)
11054 n = mzscheme_enabled(FALSE);
11055#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011056#ifdef DYNAMIC_RUBY
11057 else if (STRICMP(name, "ruby") == 0)
11058 n = ruby_enabled(FALSE);
11059#endif
11060#ifdef DYNAMIC_PYTHON
11061 else if (STRICMP(name, "python") == 0)
11062 n = python_enabled(FALSE);
11063#endif
11064#ifdef DYNAMIC_PERL
11065 else if (STRICMP(name, "perl") == 0)
11066 n = perl_enabled(FALSE);
11067#endif
11068#ifdef FEAT_GUI
11069 else if (STRICMP(name, "gui_running") == 0)
11070 n = (gui.in_use || gui.starting);
11071# ifdef FEAT_GUI_W32
11072 else if (STRICMP(name, "gui_win32s") == 0)
11073 n = gui_is_win32s();
11074# endif
11075# ifdef FEAT_BROWSE
11076 else if (STRICMP(name, "browse") == 0)
11077 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
11078# endif
11079#endif
11080#ifdef FEAT_SYN_HL
11081 else if (STRICMP(name, "syntax_items") == 0)
11082 n = syntax_present(curbuf);
11083#endif
11084#if defined(WIN3264)
11085 else if (STRICMP(name, "win95") == 0)
11086 n = mch_windows95();
11087#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000011088#ifdef FEAT_NETBEANS_INTG
11089 else if (STRICMP(name, "netbeans_enabled") == 0)
11090 n = usingNetbeans;
11091#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011092 }
11093
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011094 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011095}
11096
11097/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000011098 * "has_key()" function
11099 */
11100 static void
11101f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011102 typval_T *argvars;
11103 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011104{
11105 rettv->vval.v_number = 0;
11106 if (argvars[0].v_type != VAR_DICT)
11107 {
11108 EMSG(_(e_dictreq));
11109 return;
11110 }
11111 if (argvars[0].vval.v_dict == NULL)
11112 return;
11113
11114 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011115 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011116}
11117
11118/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011119 * "hasmapto()" function
11120 */
11121 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011122f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011123 typval_T *argvars;
11124 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011125{
11126 char_u *name;
11127 char_u *mode;
11128 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000011129 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011130
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011131 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011132 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011133 mode = (char_u *)"nvo";
11134 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000011135 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011136 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000011137 if (argvars[2].v_type != VAR_UNKNOWN)
11138 abbr = get_tv_number(&argvars[2]);
11139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011140
Bram Moolenaar2c932302006-03-18 21:42:09 +000011141 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011142 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011143 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011144 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011145}
11146
11147/*
11148 * "histadd()" function
11149 */
11150/*ARGSUSED*/
11151 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011152f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011153 typval_T *argvars;
11154 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011155{
11156#ifdef FEAT_CMDHIST
11157 int histype;
11158 char_u *str;
11159 char_u buf[NUMBUFLEN];
11160#endif
11161
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011162 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011163 if (check_restricted() || check_secure())
11164 return;
11165#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011166 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11167 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011168 if (histype >= 0)
11169 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011170 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011171 if (*str != NUL)
11172 {
11173 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011174 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011175 return;
11176 }
11177 }
11178#endif
11179}
11180
11181/*
11182 * "histdel()" function
11183 */
11184/*ARGSUSED*/
11185 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011186f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011187 typval_T *argvars;
11188 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011189{
11190#ifdef FEAT_CMDHIST
11191 int n;
11192 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011193 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011194
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011195 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11196 if (str == NULL)
11197 n = 0;
11198 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011199 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011200 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011201 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011202 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011203 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011204 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011205 else
11206 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011207 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011208 get_tv_string_buf(&argvars[1], buf));
11209 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011210#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011211 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011212#endif
11213}
11214
11215/*
11216 * "histget()" function
11217 */
11218/*ARGSUSED*/
11219 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011220f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011221 typval_T *argvars;
11222 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011223{
11224#ifdef FEAT_CMDHIST
11225 int type;
11226 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011227 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011228
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011229 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11230 if (str == NULL)
11231 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011232 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011233 {
11234 type = get_histtype(str);
11235 if (argvars[1].v_type == VAR_UNKNOWN)
11236 idx = get_history_idx(type);
11237 else
11238 idx = (int)get_tv_number_chk(&argvars[1], NULL);
11239 /* -1 on type error */
11240 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
11241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011242#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011243 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011244#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011245 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011246}
11247
11248/*
11249 * "histnr()" function
11250 */
11251/*ARGSUSED*/
11252 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011253f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011254 typval_T *argvars;
11255 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011256{
11257 int i;
11258
11259#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011260 char_u *history = get_tv_string_chk(&argvars[0]);
11261
11262 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011263 if (i >= HIST_CMD && i < HIST_COUNT)
11264 i = get_history_idx(i);
11265 else
11266#endif
11267 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011268 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011269}
11270
11271/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011272 * "highlightID(name)" function
11273 */
11274 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011275f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011276 typval_T *argvars;
11277 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011278{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011279 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011280}
11281
11282/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011283 * "highlight_exists()" function
11284 */
11285 static void
11286f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011287 typval_T *argvars;
11288 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011289{
11290 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
11291}
11292
11293/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011294 * "hostname()" function
11295 */
11296/*ARGSUSED*/
11297 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011298f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011299 typval_T *argvars;
11300 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011301{
11302 char_u hostname[256];
11303
11304 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011305 rettv->v_type = VAR_STRING;
11306 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011307}
11308
11309/*
11310 * iconv() function
11311 */
11312/*ARGSUSED*/
11313 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011314f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011315 typval_T *argvars;
11316 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011317{
11318#ifdef FEAT_MBYTE
11319 char_u buf1[NUMBUFLEN];
11320 char_u buf2[NUMBUFLEN];
11321 char_u *from, *to, *str;
11322 vimconv_T vimconv;
11323#endif
11324
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011325 rettv->v_type = VAR_STRING;
11326 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011327
11328#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011329 str = get_tv_string(&argvars[0]);
11330 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
11331 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011332 vimconv.vc_type = CONV_NONE;
11333 convert_setup(&vimconv, from, to);
11334
11335 /* If the encodings are equal, no conversion needed. */
11336 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011337 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011338 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011339 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011340
11341 convert_setup(&vimconv, NULL, NULL);
11342 vim_free(from);
11343 vim_free(to);
11344#endif
11345}
11346
11347/*
11348 * "indent()" function
11349 */
11350 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011351f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011352 typval_T *argvars;
11353 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011354{
11355 linenr_T lnum;
11356
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011357 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011358 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011359 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011360 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011361 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011362}
11363
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011364/*
11365 * "index()" function
11366 */
11367 static void
11368f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011369 typval_T *argvars;
11370 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011371{
Bram Moolenaar33570922005-01-25 22:26:29 +000011372 list_T *l;
11373 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011374 long idx = 0;
11375 int ic = FALSE;
11376
11377 rettv->vval.v_number = -1;
11378 if (argvars[0].v_type != VAR_LIST)
11379 {
11380 EMSG(_(e_listreq));
11381 return;
11382 }
11383 l = argvars[0].vval.v_list;
11384 if (l != NULL)
11385 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011386 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011387 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011388 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011389 int error = FALSE;
11390
Bram Moolenaar758711c2005-02-02 23:11:38 +000011391 /* Start at specified item. Use the cached index that list_find()
11392 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011393 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000011394 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011395 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011396 ic = get_tv_number_chk(&argvars[3], &error);
11397 if (error)
11398 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011399 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011400
Bram Moolenaar758711c2005-02-02 23:11:38 +000011401 for ( ; item != NULL; item = item->li_next, ++idx)
11402 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011403 {
11404 rettv->vval.v_number = idx;
11405 break;
11406 }
11407 }
11408}
11409
Bram Moolenaar071d4272004-06-13 20:20:40 +000011410static int inputsecret_flag = 0;
11411
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011412static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
11413
Bram Moolenaar071d4272004-06-13 20:20:40 +000011414/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011415 * This function is used by f_input() and f_inputdialog() functions. The third
11416 * argument to f_input() specifies the type of completion to use at the
11417 * prompt. The third argument to f_inputdialog() specifies the value to return
11418 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011419 */
11420 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011421get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000011422 typval_T *argvars;
11423 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011424 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011425{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011426 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011427 char_u *p = NULL;
11428 int c;
11429 char_u buf[NUMBUFLEN];
11430 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011431 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011432 int xp_type = EXPAND_NOTHING;
11433 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011434
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011435 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011436
11437#ifdef NO_CONSOLE_INPUT
11438 /* While starting up, there is no place to enter text. */
11439 if (no_console_input())
11440 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011441 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011442 return;
11443 }
11444#endif
11445
11446 cmd_silent = FALSE; /* Want to see the prompt. */
11447 if (prompt != NULL)
11448 {
11449 /* Only the part of the message after the last NL is considered as
11450 * prompt for the command line */
11451 p = vim_strrchr(prompt, '\n');
11452 if (p == NULL)
11453 p = prompt;
11454 else
11455 {
11456 ++p;
11457 c = *p;
11458 *p = NUL;
11459 msg_start();
11460 msg_clr_eos();
11461 msg_puts_attr(prompt, echo_attr);
11462 msg_didout = FALSE;
11463 msg_starthere();
11464 *p = c;
11465 }
11466 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011467
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011468 if (argvars[1].v_type != VAR_UNKNOWN)
11469 {
11470 defstr = get_tv_string_buf_chk(&argvars[1], buf);
11471 if (defstr != NULL)
11472 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011473
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011474 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000011475 {
11476 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000011477 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000011478 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011479
Bram Moolenaar4463f292005-09-25 22:20:24 +000011480 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011481
Bram Moolenaar4463f292005-09-25 22:20:24 +000011482 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
11483 if (xp_name == NULL)
11484 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011485
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011486 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011487
Bram Moolenaar4463f292005-09-25 22:20:24 +000011488 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
11489 &xp_arg) == FAIL)
11490 return;
11491 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011492 }
11493
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011494 if (defstr != NULL)
11495 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011496 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
11497 xp_type, xp_arg);
11498
11499 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011500
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011501 /* since the user typed this, no need to wait for return */
11502 need_wait_return = FALSE;
11503 msg_didout = FALSE;
11504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011505 cmd_silent = cmd_silent_save;
11506}
11507
11508/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011509 * "input()" function
11510 * Also handles inputsecret() when inputsecret is set.
11511 */
11512 static void
11513f_input(argvars, rettv)
11514 typval_T *argvars;
11515 typval_T *rettv;
11516{
11517 get_user_input(argvars, rettv, FALSE);
11518}
11519
11520/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011521 * "inputdialog()" function
11522 */
11523 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011524f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011525 typval_T *argvars;
11526 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011527{
11528#if defined(FEAT_GUI_TEXTDIALOG)
11529 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
11530 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
11531 {
11532 char_u *message;
11533 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011534 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000011535
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011536 message = get_tv_string_chk(&argvars[0]);
11537 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000011538 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000011539 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011540 else
11541 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011542 if (message != NULL && defstr != NULL
11543 && do_dialog(VIM_QUESTION, NULL, message,
11544 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011545 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011546 else
11547 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011548 if (message != NULL && defstr != NULL
11549 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011550 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011551 rettv->vval.v_string = vim_strsave(
11552 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011553 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011554 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011555 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011556 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011557 }
11558 else
11559#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011560 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011561}
11562
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011563/*
11564 * "inputlist()" function
11565 */
11566 static void
11567f_inputlist(argvars, rettv)
11568 typval_T *argvars;
11569 typval_T *rettv;
11570{
11571 listitem_T *li;
11572 int selected;
11573 int mouse_used;
11574
11575 rettv->vval.v_number = 0;
11576#ifdef NO_CONSOLE_INPUT
11577 /* While starting up, there is no place to enter text. */
11578 if (no_console_input())
11579 return;
11580#endif
11581 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
11582 {
11583 EMSG2(_(e_listarg), "inputlist()");
11584 return;
11585 }
11586
11587 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000011588 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011589 lines_left = Rows; /* avoid more prompt */
11590 msg_scroll = TRUE;
11591 msg_clr_eos();
11592
11593 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
11594 {
11595 msg_puts(get_tv_string(&li->li_tv));
11596 msg_putchar('\n');
11597 }
11598
11599 /* Ask for choice. */
11600 selected = prompt_for_number(&mouse_used);
11601 if (mouse_used)
11602 selected -= lines_left;
11603
11604 rettv->vval.v_number = selected;
11605}
11606
11607
Bram Moolenaar071d4272004-06-13 20:20:40 +000011608static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
11609
11610/*
11611 * "inputrestore()" function
11612 */
11613/*ARGSUSED*/
11614 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011615f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011616 typval_T *argvars;
11617 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011618{
11619 if (ga_userinput.ga_len > 0)
11620 {
11621 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011622 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
11623 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011624 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011625 }
11626 else if (p_verbose > 1)
11627 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000011628 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011629 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011630 }
11631}
11632
11633/*
11634 * "inputsave()" function
11635 */
11636/*ARGSUSED*/
11637 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011638f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011639 typval_T *argvars;
11640 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011641{
11642 /* Add an entry to the stack of typehead storage. */
11643 if (ga_grow(&ga_userinput, 1) == OK)
11644 {
11645 save_typeahead((tasave_T *)(ga_userinput.ga_data)
11646 + ga_userinput.ga_len);
11647 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011648 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011649 }
11650 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011651 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011652}
11653
11654/*
11655 * "inputsecret()" function
11656 */
11657 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011658f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011659 typval_T *argvars;
11660 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011661{
11662 ++cmdline_star;
11663 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011664 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011665 --cmdline_star;
11666 --inputsecret_flag;
11667}
11668
11669/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011670 * "insert()" function
11671 */
11672 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011673f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011674 typval_T *argvars;
11675 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011676{
11677 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011678 listitem_T *item;
11679 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011680 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011681
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011682 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011683 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011684 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011685 else if ((l = argvars[0].vval.v_list) != NULL
11686 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011687 {
11688 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011689 before = get_tv_number_chk(&argvars[2], &error);
11690 if (error)
11691 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011692
Bram Moolenaar758711c2005-02-02 23:11:38 +000011693 if (before == l->lv_len)
11694 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011695 else
11696 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011697 item = list_find(l, before);
11698 if (item == NULL)
11699 {
11700 EMSGN(_(e_listidx), before);
11701 l = NULL;
11702 }
11703 }
11704 if (l != NULL)
11705 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011706 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011707 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011708 }
11709 }
11710}
11711
11712/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011713 * "isdirectory()" function
11714 */
11715 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011716f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011717 typval_T *argvars;
11718 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011719{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011720 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011721}
11722
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011723/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011724 * "islocked()" function
11725 */
11726 static void
11727f_islocked(argvars, rettv)
11728 typval_T *argvars;
11729 typval_T *rettv;
11730{
11731 lval_T lv;
11732 char_u *end;
11733 dictitem_T *di;
11734
11735 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000011736 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
11737 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011738 if (end != NULL && lv.ll_name != NULL)
11739 {
11740 if (*end != NUL)
11741 EMSG(_(e_trailing));
11742 else
11743 {
11744 if (lv.ll_tv == NULL)
11745 {
11746 if (check_changedtick(lv.ll_name))
11747 rettv->vval.v_number = 1; /* always locked */
11748 else
11749 {
11750 di = find_var(lv.ll_name, NULL);
11751 if (di != NULL)
11752 {
11753 /* Consider a variable locked when:
11754 * 1. the variable itself is locked
11755 * 2. the value of the variable is locked.
11756 * 3. the List or Dict value is locked.
11757 */
11758 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
11759 || tv_islocked(&di->di_tv));
11760 }
11761 }
11762 }
11763 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011764 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011765 else if (lv.ll_newkey != NULL)
11766 EMSG2(_(e_dictkey), lv.ll_newkey);
11767 else if (lv.ll_list != NULL)
11768 /* List item. */
11769 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
11770 else
11771 /* Dictionary item. */
11772 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
11773 }
11774 }
11775
11776 clear_lval(&lv);
11777}
11778
Bram Moolenaar33570922005-01-25 22:26:29 +000011779static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011780
11781/*
11782 * Turn a dict into a list:
11783 * "what" == 0: list of keys
11784 * "what" == 1: list of values
11785 * "what" == 2: list of items
11786 */
11787 static void
11788dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000011789 typval_T *argvars;
11790 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011791 int what;
11792{
Bram Moolenaar33570922005-01-25 22:26:29 +000011793 list_T *l2;
11794 dictitem_T *di;
11795 hashitem_T *hi;
11796 listitem_T *li;
11797 listitem_T *li2;
11798 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011799 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011800
11801 rettv->vval.v_number = 0;
11802 if (argvars[0].v_type != VAR_DICT)
11803 {
11804 EMSG(_(e_dictreq));
11805 return;
11806 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011807 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011808 return;
11809
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011810 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011811 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011812
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011813 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000011814 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011815 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011816 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011817 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011818 --todo;
11819 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011820
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011821 li = listitem_alloc();
11822 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011823 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011824 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011825
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011826 if (what == 0)
11827 {
11828 /* keys() */
11829 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011830 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011831 li->li_tv.vval.v_string = vim_strsave(di->di_key);
11832 }
11833 else if (what == 1)
11834 {
11835 /* values() */
11836 copy_tv(&di->di_tv, &li->li_tv);
11837 }
11838 else
11839 {
11840 /* items() */
11841 l2 = list_alloc();
11842 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011843 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011844 li->li_tv.vval.v_list = l2;
11845 if (l2 == NULL)
11846 break;
11847 ++l2->lv_refcount;
11848
11849 li2 = listitem_alloc();
11850 if (li2 == NULL)
11851 break;
11852 list_append(l2, li2);
11853 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011854 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011855 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
11856
11857 li2 = listitem_alloc();
11858 if (li2 == NULL)
11859 break;
11860 list_append(l2, li2);
11861 copy_tv(&di->di_tv, &li2->li_tv);
11862 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011863 }
11864 }
11865}
11866
11867/*
11868 * "items(dict)" function
11869 */
11870 static void
11871f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011872 typval_T *argvars;
11873 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011874{
11875 dict_list(argvars, rettv, 2);
11876}
11877
Bram Moolenaar071d4272004-06-13 20:20:40 +000011878/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011879 * "join()" function
11880 */
11881 static void
11882f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011883 typval_T *argvars;
11884 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011885{
11886 garray_T ga;
11887 char_u *sep;
11888
11889 rettv->vval.v_number = 0;
11890 if (argvars[0].v_type != VAR_LIST)
11891 {
11892 EMSG(_(e_listreq));
11893 return;
11894 }
11895 if (argvars[0].vval.v_list == NULL)
11896 return;
11897 if (argvars[1].v_type == VAR_UNKNOWN)
11898 sep = (char_u *)" ";
11899 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011900 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011901
11902 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011903
11904 if (sep != NULL)
11905 {
11906 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000011907 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011908 ga_append(&ga, NUL);
11909 rettv->vval.v_string = (char_u *)ga.ga_data;
11910 }
11911 else
11912 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011913}
11914
11915/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011916 * "keys()" function
11917 */
11918 static void
11919f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011920 typval_T *argvars;
11921 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011922{
11923 dict_list(argvars, rettv, 0);
11924}
11925
11926/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011927 * "last_buffer_nr()" function.
11928 */
11929/*ARGSUSED*/
11930 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011931f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011932 typval_T *argvars;
11933 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011934{
11935 int n = 0;
11936 buf_T *buf;
11937
11938 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
11939 if (n < buf->b_fnum)
11940 n = buf->b_fnum;
11941
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011942 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011943}
11944
11945/*
11946 * "len()" function
11947 */
11948 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011949f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011950 typval_T *argvars;
11951 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011952{
11953 switch (argvars[0].v_type)
11954 {
11955 case VAR_STRING:
11956 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011957 rettv->vval.v_number = (varnumber_T)STRLEN(
11958 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011959 break;
11960 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011961 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011962 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011963 case VAR_DICT:
11964 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
11965 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011966 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011967 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011968 break;
11969 }
11970}
11971
Bram Moolenaar33570922005-01-25 22:26:29 +000011972static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011973
11974 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011975libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011976 typval_T *argvars;
11977 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011978 int type;
11979{
11980#ifdef FEAT_LIBCALL
11981 char_u *string_in;
11982 char_u **string_result;
11983 int nr_result;
11984#endif
11985
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011986 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011987 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011988 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011989 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011990 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011991
11992 if (check_restricted() || check_secure())
11993 return;
11994
11995#ifdef FEAT_LIBCALL
11996 /* The first two args must be strings, otherwise its meaningless */
11997 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
11998 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011999 string_in = NULL;
12000 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012001 string_in = argvars[2].vval.v_string;
12002 if (type == VAR_NUMBER)
12003 string_result = NULL;
12004 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012005 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012006 if (mch_libcall(argvars[0].vval.v_string,
12007 argvars[1].vval.v_string,
12008 string_in,
12009 argvars[2].vval.v_number,
12010 string_result,
12011 &nr_result) == OK
12012 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012013 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012014 }
12015#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012016}
12017
12018/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012019 * "libcall()" function
12020 */
12021 static void
12022f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012023 typval_T *argvars;
12024 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012025{
12026 libcall_common(argvars, rettv, VAR_STRING);
12027}
12028
12029/*
12030 * "libcallnr()" function
12031 */
12032 static void
12033f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012034 typval_T *argvars;
12035 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012036{
12037 libcall_common(argvars, rettv, VAR_NUMBER);
12038}
12039
12040/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012041 * "line(string)" function
12042 */
12043 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012044f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012045 typval_T *argvars;
12046 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012047{
12048 linenr_T lnum = 0;
12049 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012050 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012051
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012052 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012053 if (fp != NULL)
12054 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012055 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012056}
12057
12058/*
12059 * "line2byte(lnum)" function
12060 */
12061/*ARGSUSED*/
12062 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012063f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012064 typval_T *argvars;
12065 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012066{
12067#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012068 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012069#else
12070 linenr_T lnum;
12071
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012072 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012073 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012074 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012075 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012076 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
12077 if (rettv->vval.v_number >= 0)
12078 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012079#endif
12080}
12081
12082/*
12083 * "lispindent(lnum)" function
12084 */
12085 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012086f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012087 typval_T *argvars;
12088 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012089{
12090#ifdef FEAT_LISP
12091 pos_T pos;
12092 linenr_T lnum;
12093
12094 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012095 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012096 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12097 {
12098 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012099 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012100 curwin->w_cursor = pos;
12101 }
12102 else
12103#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012104 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012105}
12106
12107/*
12108 * "localtime()" function
12109 */
12110/*ARGSUSED*/
12111 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012112f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012113 typval_T *argvars;
12114 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012115{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012116 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012117}
12118
Bram Moolenaar33570922005-01-25 22:26:29 +000012119static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012120
12121 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012122get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000012123 typval_T *argvars;
12124 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012125 int exact;
12126{
12127 char_u *keys;
12128 char_u *which;
12129 char_u buf[NUMBUFLEN];
12130 char_u *keys_buf = NULL;
12131 char_u *rhs;
12132 int mode;
12133 garray_T ga;
Bram Moolenaar2c932302006-03-18 21:42:09 +000012134 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012135
12136 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012137 rettv->v_type = VAR_STRING;
12138 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012139
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012140 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012141 if (*keys == NUL)
12142 return;
12143
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012144 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000012145 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012146 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012147 if (argvars[2].v_type != VAR_UNKNOWN)
12148 abbr = get_tv_number(&argvars[2]);
12149 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012150 else
12151 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012152 if (which == NULL)
12153 return;
12154
Bram Moolenaar071d4272004-06-13 20:20:40 +000012155 mode = get_map_mode(&which, 0);
12156
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000012157 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012158 rhs = check_map(keys, mode, exact, FALSE, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012159 vim_free(keys_buf);
12160 if (rhs != NULL)
12161 {
12162 ga_init(&ga);
12163 ga.ga_itemsize = 1;
12164 ga.ga_growsize = 40;
12165
12166 while (*rhs != NUL)
12167 ga_concat(&ga, str2special(&rhs, FALSE));
12168
12169 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012170 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012171 }
12172}
12173
12174/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012175 * "map()" function
12176 */
12177 static void
12178f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012179 typval_T *argvars;
12180 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012181{
12182 filter_map(argvars, rettv, TRUE);
12183}
12184
12185/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012186 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012187 */
12188 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012189f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012190 typval_T *argvars;
12191 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012192{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012193 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012194}
12195
12196/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012197 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012198 */
12199 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012200f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012201 typval_T *argvars;
12202 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012203{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012204 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012205}
12206
Bram Moolenaar33570922005-01-25 22:26:29 +000012207static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012208
12209 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012210find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000012211 typval_T *argvars;
12212 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012213 int type;
12214{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012215 char_u *str = NULL;
12216 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012217 char_u *pat;
12218 regmatch_T regmatch;
12219 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012220 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000012221 char_u *save_cpo;
12222 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012223 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012224 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012225 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012226 list_T *l = NULL;
12227 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012228 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012229 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012230
12231 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12232 save_cpo = p_cpo;
12233 p_cpo = (char_u *)"";
12234
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012235 rettv->vval.v_number = -1;
12236 if (type == 3)
12237 {
12238 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012239 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012240 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012241 }
12242 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012243 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012244 rettv->v_type = VAR_STRING;
12245 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012246 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012247
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012248 if (argvars[0].v_type == VAR_LIST)
12249 {
12250 if ((l = argvars[0].vval.v_list) == NULL)
12251 goto theend;
12252 li = l->lv_first;
12253 }
12254 else
12255 expr = str = get_tv_string(&argvars[0]);
12256
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012257 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
12258 if (pat == NULL)
12259 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012260
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012261 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012262 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012263 int error = FALSE;
12264
12265 start = get_tv_number_chk(&argvars[2], &error);
12266 if (error)
12267 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012268 if (l != NULL)
12269 {
12270 li = list_find(l, start);
12271 if (li == NULL)
12272 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012273 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012274 }
12275 else
12276 {
12277 if (start < 0)
12278 start = 0;
12279 if (start > (long)STRLEN(str))
12280 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012281 /* When "count" argument is there ignore matches before "start",
12282 * otherwise skip part of the string. Differs when pattern is "^"
12283 * or "\<". */
12284 if (argvars[3].v_type != VAR_UNKNOWN)
12285 startcol = start;
12286 else
12287 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012288 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012289
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012290 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012291 nth = get_tv_number_chk(&argvars[3], &error);
12292 if (error)
12293 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012294 }
12295
12296 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12297 if (regmatch.regprog != NULL)
12298 {
12299 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012300
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012301 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012302 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012303 if (l != NULL)
12304 {
12305 if (li == NULL)
12306 {
12307 match = FALSE;
12308 break;
12309 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012310 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012311 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012312 if (str == NULL)
12313 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012314 }
12315
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012316 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012317
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012318 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012319 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012320 if (l == NULL && !match)
12321 break;
12322
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012323 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012324 if (l != NULL)
12325 {
12326 li = li->li_next;
12327 ++idx;
12328 }
12329 else
12330 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012331#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012332 startcol = (colnr_T)(regmatch.startp[0]
12333 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012334#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012335 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012336#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012337 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012338 }
12339
12340 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012341 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012342 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012343 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012344 int i;
12345
12346 /* return list with matched string and submatches */
12347 for (i = 0; i < NSUBEXP; ++i)
12348 {
12349 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000012350 {
12351 if (list_append_string(rettv->vval.v_list,
12352 (char_u *)"", 0) == FAIL)
12353 break;
12354 }
12355 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000012356 regmatch.startp[i],
12357 (int)(regmatch.endp[i] - regmatch.startp[i]))
12358 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012359 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012360 }
12361 }
12362 else if (type == 2)
12363 {
12364 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012365 if (l != NULL)
12366 copy_tv(&li->li_tv, rettv);
12367 else
12368 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000012369 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012370 }
12371 else if (l != NULL)
12372 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012373 else
12374 {
12375 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012376 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012377 (varnumber_T)(regmatch.startp[0] - str);
12378 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012379 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012380 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012381 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012382 }
12383 }
12384 vim_free(regmatch.regprog);
12385 }
12386
12387theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012388 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012389 p_cpo = save_cpo;
12390}
12391
12392/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012393 * "match()" function
12394 */
12395 static void
12396f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012397 typval_T *argvars;
12398 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012399{
12400 find_some_match(argvars, rettv, 1);
12401}
12402
12403/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012404 * "matcharg()" function
12405 */
12406 static void
12407f_matcharg(argvars, rettv)
12408 typval_T *argvars;
12409 typval_T *rettv;
12410{
12411 if (rettv_list_alloc(rettv) == OK)
12412 {
12413#ifdef FEAT_SEARCH_EXTRA
12414 int mi = get_tv_number(&argvars[0]);
12415
12416 if (mi >= 1 && mi <= 3)
12417 {
12418 list_append_string(rettv->vval.v_list,
12419 syn_id2name(curwin->w_match_id[mi - 1]), -1);
12420 list_append_string(rettv->vval.v_list,
12421 curwin->w_match_pat[mi - 1], -1);
12422 }
12423#endif
12424 }
12425}
12426
12427/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012428 * "matchend()" function
12429 */
12430 static void
12431f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012432 typval_T *argvars;
12433 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012434{
12435 find_some_match(argvars, rettv, 0);
12436}
12437
12438/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012439 * "matchlist()" function
12440 */
12441 static void
12442f_matchlist(argvars, rettv)
12443 typval_T *argvars;
12444 typval_T *rettv;
12445{
12446 find_some_match(argvars, rettv, 3);
12447}
12448
12449/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012450 * "matchstr()" function
12451 */
12452 static void
12453f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012454 typval_T *argvars;
12455 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012456{
12457 find_some_match(argvars, rettv, 2);
12458}
12459
Bram Moolenaar33570922005-01-25 22:26:29 +000012460static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012461
12462 static void
12463max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000012464 typval_T *argvars;
12465 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012466 int domax;
12467{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012468 long n = 0;
12469 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012470 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012471
12472 if (argvars[0].v_type == VAR_LIST)
12473 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012474 list_T *l;
12475 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012476
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012477 l = argvars[0].vval.v_list;
12478 if (l != NULL)
12479 {
12480 li = l->lv_first;
12481 if (li != NULL)
12482 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012483 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012484 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012485 {
12486 li = li->li_next;
12487 if (li == NULL)
12488 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012489 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012490 if (domax ? i > n : i < n)
12491 n = i;
12492 }
12493 }
12494 }
12495 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012496 else if (argvars[0].v_type == VAR_DICT)
12497 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012498 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012499 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000012500 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012501 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012502
12503 d = argvars[0].vval.v_dict;
12504 if (d != NULL)
12505 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012506 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000012507 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012508 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012509 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000012510 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012511 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012512 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012513 if (first)
12514 {
12515 n = i;
12516 first = FALSE;
12517 }
12518 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012519 n = i;
12520 }
12521 }
12522 }
12523 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012524 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000012525 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012526 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012527}
12528
12529/*
12530 * "max()" function
12531 */
12532 static void
12533f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012534 typval_T *argvars;
12535 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012536{
12537 max_min(argvars, rettv, TRUE);
12538}
12539
12540/*
12541 * "min()" function
12542 */
12543 static void
12544f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012545 typval_T *argvars;
12546 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012547{
12548 max_min(argvars, rettv, FALSE);
12549}
12550
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012551static int mkdir_recurse __ARGS((char_u *dir, int prot));
12552
12553/*
12554 * Create the directory in which "dir" is located, and higher levels when
12555 * needed.
12556 */
12557 static int
12558mkdir_recurse(dir, prot)
12559 char_u *dir;
12560 int prot;
12561{
12562 char_u *p;
12563 char_u *updir;
12564 int r = FAIL;
12565
12566 /* Get end of directory name in "dir".
12567 * We're done when it's "/" or "c:/". */
12568 p = gettail_sep(dir);
12569 if (p <= get_past_head(dir))
12570 return OK;
12571
12572 /* If the directory exists we're done. Otherwise: create it.*/
12573 updir = vim_strnsave(dir, (int)(p - dir));
12574 if (updir == NULL)
12575 return FAIL;
12576 if (mch_isdir(updir))
12577 r = OK;
12578 else if (mkdir_recurse(updir, prot) == OK)
12579 r = vim_mkdir_emsg(updir, prot);
12580 vim_free(updir);
12581 return r;
12582}
12583
12584#ifdef vim_mkdir
12585/*
12586 * "mkdir()" function
12587 */
12588 static void
12589f_mkdir(argvars, rettv)
12590 typval_T *argvars;
12591 typval_T *rettv;
12592{
12593 char_u *dir;
12594 char_u buf[NUMBUFLEN];
12595 int prot = 0755;
12596
12597 rettv->vval.v_number = FAIL;
12598 if (check_restricted() || check_secure())
12599 return;
12600
12601 dir = get_tv_string_buf(&argvars[0], buf);
12602 if (argvars[1].v_type != VAR_UNKNOWN)
12603 {
12604 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012605 prot = get_tv_number_chk(&argvars[2], NULL);
12606 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012607 mkdir_recurse(dir, prot);
12608 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012609 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012610}
12611#endif
12612
Bram Moolenaar0d660222005-01-07 21:51:51 +000012613/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012614 * "mode()" function
12615 */
12616/*ARGSUSED*/
12617 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012618f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012619 typval_T *argvars;
12620 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012621{
12622 char_u buf[2];
12623
12624#ifdef FEAT_VISUAL
12625 if (VIsual_active)
12626 {
12627 if (VIsual_select)
12628 buf[0] = VIsual_mode + 's' - 'v';
12629 else
12630 buf[0] = VIsual_mode;
12631 }
12632 else
12633#endif
12634 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
12635 buf[0] = 'r';
12636 else if (State & INSERT)
12637 {
12638 if (State & REPLACE_FLAG)
12639 buf[0] = 'R';
12640 else
12641 buf[0] = 'i';
12642 }
12643 else if (State & CMDLINE)
12644 buf[0] = 'c';
12645 else
12646 buf[0] = 'n';
12647
12648 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012649 rettv->vval.v_string = vim_strsave(buf);
12650 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012651}
12652
12653/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012654 * "nextnonblank()" function
12655 */
12656 static void
12657f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012658 typval_T *argvars;
12659 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012660{
12661 linenr_T lnum;
12662
12663 for (lnum = get_tv_lnum(argvars); ; ++lnum)
12664 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012665 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012666 {
12667 lnum = 0;
12668 break;
12669 }
12670 if (*skipwhite(ml_get(lnum)) != NUL)
12671 break;
12672 }
12673 rettv->vval.v_number = lnum;
12674}
12675
12676/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012677 * "nr2char()" function
12678 */
12679 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012680f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012681 typval_T *argvars;
12682 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012683{
12684 char_u buf[NUMBUFLEN];
12685
12686#ifdef FEAT_MBYTE
12687 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012688 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012689 else
12690#endif
12691 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012692 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012693 buf[1] = NUL;
12694 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012695 rettv->v_type = VAR_STRING;
12696 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012697}
12698
12699/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012700 * "pathshorten()" function
12701 */
12702 static void
12703f_pathshorten(argvars, rettv)
12704 typval_T *argvars;
12705 typval_T *rettv;
12706{
12707 char_u *p;
12708
12709 rettv->v_type = VAR_STRING;
12710 p = get_tv_string_chk(&argvars[0]);
12711 if (p == NULL)
12712 rettv->vval.v_string = NULL;
12713 else
12714 {
12715 p = vim_strsave(p);
12716 rettv->vval.v_string = p;
12717 if (p != NULL)
12718 shorten_dir(p);
12719 }
12720}
12721
12722/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012723 * "prevnonblank()" function
12724 */
12725 static void
12726f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012727 typval_T *argvars;
12728 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012729{
12730 linenr_T lnum;
12731
12732 lnum = get_tv_lnum(argvars);
12733 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
12734 lnum = 0;
12735 else
12736 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
12737 --lnum;
12738 rettv->vval.v_number = lnum;
12739}
12740
Bram Moolenaara6c840d2005-08-22 22:59:46 +000012741#ifdef HAVE_STDARG_H
12742/* This dummy va_list is here because:
12743 * - passing a NULL pointer doesn't work when va_list isn't a pointer
12744 * - locally in the function results in a "used before set" warning
12745 * - using va_start() to initialize it gives "function with fixed args" error */
12746static va_list ap;
12747#endif
12748
Bram Moolenaar8c711452005-01-14 21:53:12 +000012749/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012750 * "printf()" function
12751 */
12752 static void
12753f_printf(argvars, rettv)
12754 typval_T *argvars;
12755 typval_T *rettv;
12756{
12757 rettv->v_type = VAR_STRING;
12758 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000012759#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012760 {
12761 char_u buf[NUMBUFLEN];
12762 int len;
12763 char_u *s;
12764 int saved_did_emsg = did_emsg;
12765 char *fmt;
12766
12767 /* Get the required length, allocate the buffer and do it for real. */
12768 did_emsg = FALSE;
12769 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012770 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012771 if (!did_emsg)
12772 {
12773 s = alloc(len + 1);
12774 if (s != NULL)
12775 {
12776 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012777 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012778 }
12779 }
12780 did_emsg |= saved_did_emsg;
12781 }
12782#endif
12783}
12784
12785/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000012786 * "pumvisible()" function
12787 */
12788/*ARGSUSED*/
12789 static void
12790f_pumvisible(argvars, rettv)
12791 typval_T *argvars;
12792 typval_T *rettv;
12793{
12794 rettv->vval.v_number = 0;
12795#ifdef FEAT_INS_EXPAND
12796 if (pum_visible())
12797 rettv->vval.v_number = 1;
12798#endif
12799}
12800
12801/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012802 * "range()" function
12803 */
12804 static void
12805f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012806 typval_T *argvars;
12807 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012808{
12809 long start;
12810 long end;
12811 long stride = 1;
12812 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012813 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012814
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012815 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012816 if (argvars[1].v_type == VAR_UNKNOWN)
12817 {
12818 end = start - 1;
12819 start = 0;
12820 }
12821 else
12822 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012823 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012824 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012825 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012826 }
12827
12828 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012829 if (error)
12830 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000012831 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012832 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000012833 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012834 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012835 else
12836 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012837 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012838 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012839 if (list_append_number(rettv->vval.v_list,
12840 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012841 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012842 }
12843}
12844
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012845/*
12846 * "readfile()" function
12847 */
12848 static void
12849f_readfile(argvars, rettv)
12850 typval_T *argvars;
12851 typval_T *rettv;
12852{
12853 int binary = FALSE;
12854 char_u *fname;
12855 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012856 listitem_T *li;
12857#define FREAD_SIZE 200 /* optimized for text lines */
12858 char_u buf[FREAD_SIZE];
12859 int readlen; /* size of last fread() */
12860 int buflen; /* nr of valid chars in buf[] */
12861 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
12862 int tolist; /* first byte in buf[] still to be put in list */
12863 int chop; /* how many CR to chop off */
12864 char_u *prev = NULL; /* previously read bytes, if any */
12865 int prevlen = 0; /* length of "prev" if not NULL */
12866 char_u *s;
12867 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012868 long maxline = MAXLNUM;
12869 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012870
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012871 if (argvars[1].v_type != VAR_UNKNOWN)
12872 {
12873 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
12874 binary = TRUE;
12875 if (argvars[2].v_type != VAR_UNKNOWN)
12876 maxline = get_tv_number(&argvars[2]);
12877 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012878
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012879 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012880 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012881
12882 /* Always open the file in binary mode, library functions have a mind of
12883 * their own about CR-LF conversion. */
12884 fname = get_tv_string(&argvars[0]);
12885 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
12886 {
12887 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
12888 return;
12889 }
12890
12891 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012892 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012893 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012894 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012895 buflen = filtd + readlen;
12896 tolist = 0;
12897 for ( ; filtd < buflen || readlen <= 0; ++filtd)
12898 {
12899 if (buf[filtd] == '\n' || readlen <= 0)
12900 {
12901 /* Only when in binary mode add an empty list item when the
12902 * last line ends in a '\n'. */
12903 if (!binary && readlen == 0 && filtd == 0)
12904 break;
12905
12906 /* Found end-of-line or end-of-file: add a text line to the
12907 * list. */
12908 chop = 0;
12909 if (!binary)
12910 while (filtd - chop - 1 >= tolist
12911 && buf[filtd - chop - 1] == '\r')
12912 ++chop;
12913 len = filtd - tolist - chop;
12914 if (prev == NULL)
12915 s = vim_strnsave(buf + tolist, len);
12916 else
12917 {
12918 s = alloc((unsigned)(prevlen + len + 1));
12919 if (s != NULL)
12920 {
12921 mch_memmove(s, prev, prevlen);
12922 vim_free(prev);
12923 prev = NULL;
12924 mch_memmove(s + prevlen, buf + tolist, len);
12925 s[prevlen + len] = NUL;
12926 }
12927 }
12928 tolist = filtd + 1;
12929
12930 li = listitem_alloc();
12931 if (li == NULL)
12932 {
12933 vim_free(s);
12934 break;
12935 }
12936 li->li_tv.v_type = VAR_STRING;
12937 li->li_tv.v_lock = 0;
12938 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012939 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012940
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012941 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012942 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012943 if (readlen <= 0)
12944 break;
12945 }
12946 else if (buf[filtd] == NUL)
12947 buf[filtd] = '\n';
12948 }
12949 if (readlen <= 0)
12950 break;
12951
12952 if (tolist == 0)
12953 {
12954 /* "buf" is full, need to move text to an allocated buffer */
12955 if (prev == NULL)
12956 {
12957 prev = vim_strnsave(buf, buflen);
12958 prevlen = buflen;
12959 }
12960 else
12961 {
12962 s = alloc((unsigned)(prevlen + buflen));
12963 if (s != NULL)
12964 {
12965 mch_memmove(s, prev, prevlen);
12966 mch_memmove(s + prevlen, buf, buflen);
12967 vim_free(prev);
12968 prev = s;
12969 prevlen += buflen;
12970 }
12971 }
12972 filtd = 0;
12973 }
12974 else
12975 {
12976 mch_memmove(buf, buf + tolist, buflen - tolist);
12977 filtd -= tolist;
12978 }
12979 }
12980
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012981 /*
12982 * For a negative line count use only the lines at the end of the file,
12983 * free the rest.
12984 */
12985 if (maxline < 0)
12986 while (cnt > -maxline)
12987 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012988 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012989 --cnt;
12990 }
12991
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012992 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012993 fclose(fd);
12994}
12995
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012996#if defined(FEAT_RELTIME)
12997static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
12998
12999/*
13000 * Convert a List to proftime_T.
13001 * Return FAIL when there is something wrong.
13002 */
13003 static int
13004list2proftime(arg, tm)
13005 typval_T *arg;
13006 proftime_T *tm;
13007{
13008 long n1, n2;
13009 int error = FALSE;
13010
13011 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
13012 || arg->vval.v_list->lv_len != 2)
13013 return FAIL;
13014 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
13015 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
13016# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000013017 tm->HighPart = n1;
13018 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013019# else
13020 tm->tv_sec = n1;
13021 tm->tv_usec = n2;
13022# endif
13023 return error ? FAIL : OK;
13024}
13025#endif /* FEAT_RELTIME */
13026
13027/*
13028 * "reltime()" function
13029 */
13030 static void
13031f_reltime(argvars, rettv)
13032 typval_T *argvars;
13033 typval_T *rettv;
13034{
13035#ifdef FEAT_RELTIME
13036 proftime_T res;
13037 proftime_T start;
13038
13039 if (argvars[0].v_type == VAR_UNKNOWN)
13040 {
13041 /* No arguments: get current time. */
13042 profile_start(&res);
13043 }
13044 else if (argvars[1].v_type == VAR_UNKNOWN)
13045 {
13046 if (list2proftime(&argvars[0], &res) == FAIL)
13047 return;
13048 profile_end(&res);
13049 }
13050 else
13051 {
13052 /* Two arguments: compute the difference. */
13053 if (list2proftime(&argvars[0], &start) == FAIL
13054 || list2proftime(&argvars[1], &res) == FAIL)
13055 return;
13056 profile_sub(&res, &start);
13057 }
13058
13059 if (rettv_list_alloc(rettv) == OK)
13060 {
13061 long n1, n2;
13062
13063# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000013064 n1 = res.HighPart;
13065 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013066# else
13067 n1 = res.tv_sec;
13068 n2 = res.tv_usec;
13069# endif
13070 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
13071 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
13072 }
13073#endif
13074}
13075
13076/*
13077 * "reltimestr()" function
13078 */
13079 static void
13080f_reltimestr(argvars, rettv)
13081 typval_T *argvars;
13082 typval_T *rettv;
13083{
13084#ifdef FEAT_RELTIME
13085 proftime_T tm;
13086#endif
13087
13088 rettv->v_type = VAR_STRING;
13089 rettv->vval.v_string = NULL;
13090#ifdef FEAT_RELTIME
13091 if (list2proftime(&argvars[0], &tm) == OK)
13092 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
13093#endif
13094}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013095
Bram Moolenaar0d660222005-01-07 21:51:51 +000013096#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
13097static void make_connection __ARGS((void));
13098static int check_connection __ARGS((void));
13099
13100 static void
13101make_connection()
13102{
13103 if (X_DISPLAY == NULL
13104# ifdef FEAT_GUI
13105 && !gui.in_use
13106# endif
13107 )
13108 {
13109 x_force_connect = TRUE;
13110 setup_term_clip();
13111 x_force_connect = FALSE;
13112 }
13113}
13114
13115 static int
13116check_connection()
13117{
13118 make_connection();
13119 if (X_DISPLAY == NULL)
13120 {
13121 EMSG(_("E240: No connection to Vim server"));
13122 return FAIL;
13123 }
13124 return OK;
13125}
13126#endif
13127
13128#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000013129static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013130
13131 static void
13132remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000013133 typval_T *argvars;
13134 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013135 int expr;
13136{
13137 char_u *server_name;
13138 char_u *keys;
13139 char_u *r = NULL;
13140 char_u buf[NUMBUFLEN];
13141# ifdef WIN32
13142 HWND w;
13143# else
13144 Window w;
13145# endif
13146
13147 if (check_restricted() || check_secure())
13148 return;
13149
13150# ifdef FEAT_X11
13151 if (check_connection() == FAIL)
13152 return;
13153# endif
13154
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013155 server_name = get_tv_string_chk(&argvars[0]);
13156 if (server_name == NULL)
13157 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013158 keys = get_tv_string_buf(&argvars[1], buf);
13159# ifdef WIN32
13160 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
13161# else
13162 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
13163 < 0)
13164# endif
13165 {
13166 if (r != NULL)
13167 EMSG(r); /* sending worked but evaluation failed */
13168 else
13169 EMSG2(_("E241: Unable to send to %s"), server_name);
13170 return;
13171 }
13172
13173 rettv->vval.v_string = r;
13174
13175 if (argvars[2].v_type != VAR_UNKNOWN)
13176 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013177 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000013178 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013179 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013180
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013181 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000013182 v.di_tv.v_type = VAR_STRING;
13183 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013184 idvar = get_tv_string_chk(&argvars[2]);
13185 if (idvar != NULL)
13186 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000013187 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013188 }
13189}
13190#endif
13191
13192/*
13193 * "remote_expr()" function
13194 */
13195/*ARGSUSED*/
13196 static void
13197f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013198 typval_T *argvars;
13199 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013200{
13201 rettv->v_type = VAR_STRING;
13202 rettv->vval.v_string = NULL;
13203#ifdef FEAT_CLIENTSERVER
13204 remote_common(argvars, rettv, TRUE);
13205#endif
13206}
13207
13208/*
13209 * "remote_foreground()" function
13210 */
13211/*ARGSUSED*/
13212 static void
13213f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013214 typval_T *argvars;
13215 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013216{
13217 rettv->vval.v_number = 0;
13218#ifdef FEAT_CLIENTSERVER
13219# ifdef WIN32
13220 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013221 {
13222 char_u *server_name = get_tv_string_chk(&argvars[0]);
13223
13224 if (server_name != NULL)
13225 serverForeground(server_name);
13226 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013227# else
13228 /* Send a foreground() expression to the server. */
13229 argvars[1].v_type = VAR_STRING;
13230 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
13231 argvars[2].v_type = VAR_UNKNOWN;
13232 remote_common(argvars, rettv, TRUE);
13233 vim_free(argvars[1].vval.v_string);
13234# endif
13235#endif
13236}
13237
13238/*ARGSUSED*/
13239 static void
13240f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013241 typval_T *argvars;
13242 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013243{
13244#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000013245 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013246 char_u *s = NULL;
13247# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013248 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013249# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013250 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013251
13252 if (check_restricted() || check_secure())
13253 {
13254 rettv->vval.v_number = -1;
13255 return;
13256 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013257 serverid = get_tv_string_chk(&argvars[0]);
13258 if (serverid == NULL)
13259 {
13260 rettv->vval.v_number = -1;
13261 return; /* type error; errmsg already given */
13262 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013263# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013264 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013265 if (n == 0)
13266 rettv->vval.v_number = -1;
13267 else
13268 {
13269 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
13270 rettv->vval.v_number = (s != NULL);
13271 }
13272# else
13273 rettv->vval.v_number = 0;
13274 if (check_connection() == FAIL)
13275 return;
13276
13277 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013278 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013279# endif
13280
13281 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
13282 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013283 char_u *retvar;
13284
Bram Moolenaar33570922005-01-25 22:26:29 +000013285 v.di_tv.v_type = VAR_STRING;
13286 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013287 retvar = get_tv_string_chk(&argvars[1]);
13288 if (retvar != NULL)
13289 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000013290 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013291 }
13292#else
13293 rettv->vval.v_number = -1;
13294#endif
13295}
13296
13297/*ARGSUSED*/
13298 static void
13299f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013300 typval_T *argvars;
13301 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013302{
13303 char_u *r = NULL;
13304
13305#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013306 char_u *serverid = get_tv_string_chk(&argvars[0]);
13307
13308 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000013309 {
13310# ifdef WIN32
13311 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013312 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013313
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013314 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013315 if (n != 0)
13316 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
13317 if (r == NULL)
13318# else
13319 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013320 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013321# endif
13322 EMSG(_("E277: Unable to read a server reply"));
13323 }
13324#endif
13325 rettv->v_type = VAR_STRING;
13326 rettv->vval.v_string = r;
13327}
13328
13329/*
13330 * "remote_send()" function
13331 */
13332/*ARGSUSED*/
13333 static void
13334f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013335 typval_T *argvars;
13336 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013337{
13338 rettv->v_type = VAR_STRING;
13339 rettv->vval.v_string = NULL;
13340#ifdef FEAT_CLIENTSERVER
13341 remote_common(argvars, rettv, FALSE);
13342#endif
13343}
13344
13345/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013346 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013347 */
13348 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013349f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013350 typval_T *argvars;
13351 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013352{
Bram Moolenaar33570922005-01-25 22:26:29 +000013353 list_T *l;
13354 listitem_T *item, *item2;
13355 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013356 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013357 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013358 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000013359 dict_T *d;
13360 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013361
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013362 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013363 if (argvars[0].v_type == VAR_DICT)
13364 {
13365 if (argvars[2].v_type != VAR_UNKNOWN)
13366 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013367 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000013368 && !tv_check_lock(d->dv_lock, (char_u *)"remove() argument"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013369 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013370 key = get_tv_string_chk(&argvars[1]);
13371 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013372 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013373 di = dict_find(d, key, -1);
13374 if (di == NULL)
13375 EMSG2(_(e_dictkey), key);
13376 else
13377 {
13378 *rettv = di->di_tv;
13379 init_tv(&di->di_tv);
13380 dictitem_remove(d, di);
13381 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013382 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013383 }
13384 }
13385 else if (argvars[0].v_type != VAR_LIST)
13386 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013387 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000013388 && !tv_check_lock(l->lv_lock, (char_u *)"remove() argument"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013389 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013390 int error = FALSE;
13391
13392 idx = get_tv_number_chk(&argvars[1], &error);
13393 if (error)
13394 ; /* type error: do nothing, errmsg already given */
13395 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013396 EMSGN(_(e_listidx), idx);
13397 else
13398 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013399 if (argvars[2].v_type == VAR_UNKNOWN)
13400 {
13401 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013402 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013403 *rettv = item->li_tv;
13404 vim_free(item);
13405 }
13406 else
13407 {
13408 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013409 end = get_tv_number_chk(&argvars[2], &error);
13410 if (error)
13411 ; /* type error: do nothing */
13412 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013413 EMSGN(_(e_listidx), end);
13414 else
13415 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013416 int cnt = 0;
13417
13418 for (li = item; li != NULL; li = li->li_next)
13419 {
13420 ++cnt;
13421 if (li == item2)
13422 break;
13423 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013424 if (li == NULL) /* didn't find "item2" after "item" */
13425 EMSG(_(e_invrange));
13426 else
13427 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013428 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013429 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013430 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013431 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013432 l->lv_first = item;
13433 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013434 item->li_prev = NULL;
13435 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013436 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013437 }
13438 }
13439 }
13440 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013441 }
13442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013443}
13444
13445/*
13446 * "rename({from}, {to})" function
13447 */
13448 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013449f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013450 typval_T *argvars;
13451 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013452{
13453 char_u buf[NUMBUFLEN];
13454
13455 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013456 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013457 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013458 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
13459 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013460}
13461
13462/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013463 * "repeat()" function
13464 */
13465/*ARGSUSED*/
13466 static void
13467f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013468 typval_T *argvars;
13469 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013470{
13471 char_u *p;
13472 int n;
13473 int slen;
13474 int len;
13475 char_u *r;
13476 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013477
13478 n = get_tv_number(&argvars[1]);
13479 if (argvars[0].v_type == VAR_LIST)
13480 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013481 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013482 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013483 if (list_extend(rettv->vval.v_list,
13484 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013485 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013486 }
13487 else
13488 {
13489 p = get_tv_string(&argvars[0]);
13490 rettv->v_type = VAR_STRING;
13491 rettv->vval.v_string = NULL;
13492
13493 slen = (int)STRLEN(p);
13494 len = slen * n;
13495 if (len <= 0)
13496 return;
13497
13498 r = alloc(len + 1);
13499 if (r != NULL)
13500 {
13501 for (i = 0; i < n; i++)
13502 mch_memmove(r + i * slen, p, (size_t)slen);
13503 r[len] = NUL;
13504 }
13505
13506 rettv->vval.v_string = r;
13507 }
13508}
13509
13510/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013511 * "resolve()" function
13512 */
13513 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013514f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013515 typval_T *argvars;
13516 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013517{
13518 char_u *p;
13519
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013520 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013521#ifdef FEAT_SHORTCUT
13522 {
13523 char_u *v = NULL;
13524
13525 v = mch_resolve_shortcut(p);
13526 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013527 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013528 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013529 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013530 }
13531#else
13532# ifdef HAVE_READLINK
13533 {
13534 char_u buf[MAXPATHL + 1];
13535 char_u *cpy;
13536 int len;
13537 char_u *remain = NULL;
13538 char_u *q;
13539 int is_relative_to_current = FALSE;
13540 int has_trailing_pathsep = FALSE;
13541 int limit = 100;
13542
13543 p = vim_strsave(p);
13544
13545 if (p[0] == '.' && (vim_ispathsep(p[1])
13546 || (p[1] == '.' && (vim_ispathsep(p[2])))))
13547 is_relative_to_current = TRUE;
13548
13549 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013550 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013551 has_trailing_pathsep = TRUE;
13552
13553 q = getnextcomp(p);
13554 if (*q != NUL)
13555 {
13556 /* Separate the first path component in "p", and keep the
13557 * remainder (beginning with the path separator). */
13558 remain = vim_strsave(q - 1);
13559 q[-1] = NUL;
13560 }
13561
13562 for (;;)
13563 {
13564 for (;;)
13565 {
13566 len = readlink((char *)p, (char *)buf, MAXPATHL);
13567 if (len <= 0)
13568 break;
13569 buf[len] = NUL;
13570
13571 if (limit-- == 0)
13572 {
13573 vim_free(p);
13574 vim_free(remain);
13575 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013576 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013577 goto fail;
13578 }
13579
13580 /* Ensure that the result will have a trailing path separator
13581 * if the argument has one. */
13582 if (remain == NULL && has_trailing_pathsep)
13583 add_pathsep(buf);
13584
13585 /* Separate the first path component in the link value and
13586 * concatenate the remainders. */
13587 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
13588 if (*q != NUL)
13589 {
13590 if (remain == NULL)
13591 remain = vim_strsave(q - 1);
13592 else
13593 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000013594 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013595 if (cpy != NULL)
13596 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013597 vim_free(remain);
13598 remain = cpy;
13599 }
13600 }
13601 q[-1] = NUL;
13602 }
13603
13604 q = gettail(p);
13605 if (q > p && *q == NUL)
13606 {
13607 /* Ignore trailing path separator. */
13608 q[-1] = NUL;
13609 q = gettail(p);
13610 }
13611 if (q > p && !mch_isFullName(buf))
13612 {
13613 /* symlink is relative to directory of argument */
13614 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
13615 if (cpy != NULL)
13616 {
13617 STRCPY(cpy, p);
13618 STRCPY(gettail(cpy), buf);
13619 vim_free(p);
13620 p = cpy;
13621 }
13622 }
13623 else
13624 {
13625 vim_free(p);
13626 p = vim_strsave(buf);
13627 }
13628 }
13629
13630 if (remain == NULL)
13631 break;
13632
13633 /* Append the first path component of "remain" to "p". */
13634 q = getnextcomp(remain + 1);
13635 len = q - remain - (*q != NUL);
13636 cpy = vim_strnsave(p, STRLEN(p) + len);
13637 if (cpy != NULL)
13638 {
13639 STRNCAT(cpy, remain, len);
13640 vim_free(p);
13641 p = cpy;
13642 }
13643 /* Shorten "remain". */
13644 if (*q != NUL)
13645 STRCPY(remain, q - 1);
13646 else
13647 {
13648 vim_free(remain);
13649 remain = NULL;
13650 }
13651 }
13652
13653 /* If the result is a relative path name, make it explicitly relative to
13654 * the current directory if and only if the argument had this form. */
13655 if (!vim_ispathsep(*p))
13656 {
13657 if (is_relative_to_current
13658 && *p != NUL
13659 && !(p[0] == '.'
13660 && (p[1] == NUL
13661 || vim_ispathsep(p[1])
13662 || (p[1] == '.'
13663 && (p[2] == NUL
13664 || vim_ispathsep(p[2]))))))
13665 {
13666 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013667 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013668 if (cpy != NULL)
13669 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013670 vim_free(p);
13671 p = cpy;
13672 }
13673 }
13674 else if (!is_relative_to_current)
13675 {
13676 /* Strip leading "./". */
13677 q = p;
13678 while (q[0] == '.' && vim_ispathsep(q[1]))
13679 q += 2;
13680 if (q > p)
13681 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
13682 }
13683 }
13684
13685 /* Ensure that the result will have no trailing path separator
13686 * if the argument had none. But keep "/" or "//". */
13687 if (!has_trailing_pathsep)
13688 {
13689 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013690 if (after_pathsep(p, q))
13691 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013692 }
13693
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013694 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013695 }
13696# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013697 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013698# endif
13699#endif
13700
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013701 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013702
13703#ifdef HAVE_READLINK
13704fail:
13705#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013706 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013707}
13708
13709/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013710 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013711 */
13712 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013713f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013714 typval_T *argvars;
13715 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013716{
Bram Moolenaar33570922005-01-25 22:26:29 +000013717 list_T *l;
13718 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013719
Bram Moolenaar0d660222005-01-07 21:51:51 +000013720 rettv->vval.v_number = 0;
13721 if (argvars[0].v_type != VAR_LIST)
13722 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013723 else if ((l = argvars[0].vval.v_list) != NULL
13724 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013725 {
13726 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013727 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013728 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013729 while (li != NULL)
13730 {
13731 ni = li->li_prev;
13732 list_append(l, li);
13733 li = ni;
13734 }
13735 rettv->vval.v_list = l;
13736 rettv->v_type = VAR_LIST;
13737 ++l->lv_refcount;
13738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013739}
13740
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013741#define SP_NOMOVE 0x01 /* don't move cursor */
13742#define SP_REPEAT 0x02 /* repeat to find outer pair */
13743#define SP_RETCOUNT 0x04 /* return matchcount */
13744#define SP_SETPCMARK 0x08 /* set previous context mark */
13745#define SP_START 0x10 /* accept match at start position */
13746#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
13747#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013748
Bram Moolenaar33570922005-01-25 22:26:29 +000013749static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013750
13751/*
13752 * Get flags for a search function.
13753 * Possibly sets "p_ws".
13754 * Returns BACKWARD, FORWARD or zero (for an error).
13755 */
13756 static int
13757get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013758 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013759 int *flagsp;
13760{
13761 int dir = FORWARD;
13762 char_u *flags;
13763 char_u nbuf[NUMBUFLEN];
13764 int mask;
13765
13766 if (varp->v_type != VAR_UNKNOWN)
13767 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013768 flags = get_tv_string_buf_chk(varp, nbuf);
13769 if (flags == NULL)
13770 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013771 while (*flags != NUL)
13772 {
13773 switch (*flags)
13774 {
13775 case 'b': dir = BACKWARD; break;
13776 case 'w': p_ws = TRUE; break;
13777 case 'W': p_ws = FALSE; break;
13778 default: mask = 0;
13779 if (flagsp != NULL)
13780 switch (*flags)
13781 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013782 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013783 case 'e': mask = SP_END; break;
13784 case 'm': mask = SP_RETCOUNT; break;
13785 case 'n': mask = SP_NOMOVE; break;
13786 case 'p': mask = SP_SUBPAT; break;
13787 case 'r': mask = SP_REPEAT; break;
13788 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013789 }
13790 if (mask == 0)
13791 {
13792 EMSG2(_(e_invarg2), flags);
13793 dir = 0;
13794 }
13795 else
13796 *flagsp |= mask;
13797 }
13798 if (dir == 0)
13799 break;
13800 ++flags;
13801 }
13802 }
13803 return dir;
13804}
13805
Bram Moolenaar071d4272004-06-13 20:20:40 +000013806/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013807 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000013808 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013809 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013810search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013811 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013812 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013813 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013814{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013815 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013816 char_u *pat;
13817 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013818 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013819 int save_p_ws = p_ws;
13820 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013821 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013822 long lnum_stop = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013823 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013824 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013825
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013826 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013827 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013828 if (dir == 0)
13829 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013830 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013831 if (flags & SP_START)
13832 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013833 if (flags & SP_END)
13834 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013835
13836 /* Optional extra argument: line number to stop searching. */
13837 if (argvars[1].v_type != VAR_UNKNOWN
13838 && argvars[2].v_type != VAR_UNKNOWN)
13839 {
13840 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
13841 if (lnum_stop < 0)
13842 goto theend;
13843 }
13844
Bram Moolenaar231334e2005-07-25 20:46:57 +000013845 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013846 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000013847 * Check to make sure only those flags are set.
13848 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
13849 * flags cannot be set. Check for that condition also.
13850 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013851 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013852 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013853 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013854 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013855 goto theend;
13856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013857
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013858 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013859 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
13860 options, RE_SEARCH, (linenr_T)lnum_stop);
13861 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013862 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013863 if (flags & SP_SUBPAT)
13864 retval = subpatnum;
13865 else
13866 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013867 if (flags & SP_SETPCMARK)
13868 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013869 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013870 if (match_pos != NULL)
13871 {
13872 /* Store the match cursor position */
13873 match_pos->lnum = pos.lnum;
13874 match_pos->col = pos.col + 1;
13875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013876 /* "/$" will put the cursor after the end of the line, may need to
13877 * correct that here */
13878 check_cursor();
13879 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013880
13881 /* If 'n' flag is used: restore cursor position. */
13882 if (flags & SP_NOMOVE)
13883 curwin->w_cursor = save_cursor;
13884theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000013885 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013886
13887 return retval;
13888}
13889
13890/*
13891 * "search()" function
13892 */
13893 static void
13894f_search(argvars, rettv)
13895 typval_T *argvars;
13896 typval_T *rettv;
13897{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013898 int flags = 0;
13899
13900 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013901}
13902
Bram Moolenaar071d4272004-06-13 20:20:40 +000013903/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013904 * "searchdecl()" function
13905 */
13906 static void
13907f_searchdecl(argvars, rettv)
13908 typval_T *argvars;
13909 typval_T *rettv;
13910{
13911 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013912 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013913 int error = FALSE;
13914 char_u *name;
13915
13916 rettv->vval.v_number = 1; /* default: FAIL */
13917
13918 name = get_tv_string_chk(&argvars[0]);
13919 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000013920 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013921 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013922 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13923 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
13924 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013925 if (!error && name != NULL)
13926 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000013927 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013928}
13929
13930/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013931 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000013932 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013933 static int
13934searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000013935 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013936 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013937{
13938 char_u *spat, *mpat, *epat;
13939 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013940 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013941 int dir;
13942 int flags = 0;
13943 char_u nbuf1[NUMBUFLEN];
13944 char_u nbuf2[NUMBUFLEN];
13945 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013946 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013947 long lnum_stop = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013948
Bram Moolenaar071d4272004-06-13 20:20:40 +000013949 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013950 spat = get_tv_string_chk(&argvars[0]);
13951 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
13952 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
13953 if (spat == NULL || mpat == NULL || epat == NULL)
13954 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013955
Bram Moolenaar071d4272004-06-13 20:20:40 +000013956 /* Handle the optional fourth argument: flags */
13957 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013958 if (dir == 0)
13959 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013960
13961 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000013962 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
13963 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013964 if ((flags & (SP_END | SP_SUBPAT)) != 0
13965 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000013966 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013967 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000013968 goto theend;
13969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013970
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013971 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013972 if (argvars[3].v_type == VAR_UNKNOWN
13973 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013974 skip = (char_u *)"";
13975 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013976 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013977 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013978 if (argvars[5].v_type != VAR_UNKNOWN)
13979 {
13980 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
13981 if (lnum_stop < 0)
13982 goto theend;
13983 }
13984 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013985 if (skip == NULL)
13986 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013987
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013988 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
13989 match_pos, lnum_stop);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013990
13991theend:
13992 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013993
13994 return retval;
13995}
13996
13997/*
13998 * "searchpair()" function
13999 */
14000 static void
14001f_searchpair(argvars, rettv)
14002 typval_T *argvars;
14003 typval_T *rettv;
14004{
14005 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
14006}
14007
14008/*
14009 * "searchpairpos()" function
14010 */
14011 static void
14012f_searchpairpos(argvars, rettv)
14013 typval_T *argvars;
14014 typval_T *rettv;
14015{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014016 pos_T match_pos;
14017 int lnum = 0;
14018 int col = 0;
14019
14020 rettv->vval.v_number = 0;
14021
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014022 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014023 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014024
14025 if (searchpair_cmn(argvars, &match_pos) > 0)
14026 {
14027 lnum = match_pos.lnum;
14028 col = match_pos.col;
14029 }
14030
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014031 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
14032 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014033}
14034
14035/*
14036 * Search for a start/middle/end thing.
14037 * Used by searchpair(), see its documentation for the details.
14038 * Returns 0 or -1 for no match,
14039 */
14040 long
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014041do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos, lnum_stop)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014042 char_u *spat; /* start pattern */
14043 char_u *mpat; /* middle pattern */
14044 char_u *epat; /* end pattern */
14045 int dir; /* BACKWARD or FORWARD */
14046 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014047 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014048 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014049 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014050{
14051 char_u *save_cpo;
14052 char_u *pat, *pat2 = NULL, *pat3 = NULL;
14053 long retval = 0;
14054 pos_T pos;
14055 pos_T firstpos;
14056 pos_T foundpos;
14057 pos_T save_cursor;
14058 pos_T save_pos;
14059 int n;
14060 int r;
14061 int nest = 1;
14062 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014063 int options = SEARCH_KEEP;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014064
14065 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14066 save_cpo = p_cpo;
14067 p_cpo = (char_u *)"";
14068
14069 /* Make two search patterns: start/end (pat2, for in nested pairs) and
14070 * start/middle/end (pat3, for the top pair). */
14071 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
14072 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
14073 if (pat2 == NULL || pat3 == NULL)
14074 goto theend;
14075 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
14076 if (*mpat == NUL)
14077 STRCPY(pat3, pat2);
14078 else
14079 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
14080 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014081 if (flags & SP_START)
14082 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014083
Bram Moolenaar071d4272004-06-13 20:20:40 +000014084 save_cursor = curwin->w_cursor;
14085 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000014086 clearpos(&firstpos);
14087 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014088 pat = pat3;
14089 for (;;)
14090 {
14091 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014092 options, RE_SEARCH, lnum_stop);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014093 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
14094 /* didn't find it or found the first match again: FAIL */
14095 break;
14096
14097 if (firstpos.lnum == 0)
14098 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000014099 if (equalpos(pos, foundpos))
14100 {
14101 /* Found the same position again. Can happen with a pattern that
14102 * has "\zs" at the end and searching backwards. Advance one
14103 * character and try again. */
14104 if (dir == BACKWARD)
14105 decl(&pos);
14106 else
14107 incl(&pos);
14108 }
14109 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014110
14111 /* If the skip pattern matches, ignore this match. */
14112 if (*skip != NUL)
14113 {
14114 save_pos = curwin->w_cursor;
14115 curwin->w_cursor = pos;
14116 r = eval_to_bool(skip, &err, NULL, FALSE);
14117 curwin->w_cursor = save_pos;
14118 if (err)
14119 {
14120 /* Evaluating {skip} caused an error, break here. */
14121 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014122 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014123 break;
14124 }
14125 if (r)
14126 continue;
14127 }
14128
14129 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
14130 {
14131 /* Found end when searching backwards or start when searching
14132 * forward: nested pair. */
14133 ++nest;
14134 pat = pat2; /* nested, don't search for middle */
14135 }
14136 else
14137 {
14138 /* Found end when searching forward or start when searching
14139 * backward: end of (nested) pair; or found middle in outer pair. */
14140 if (--nest == 1)
14141 pat = pat3; /* outer level, search for middle */
14142 }
14143
14144 if (nest == 0)
14145 {
14146 /* Found the match: return matchcount or line number. */
14147 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014148 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014149 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014150 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000014151 if (flags & SP_SETPCMARK)
14152 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014153 curwin->w_cursor = pos;
14154 if (!(flags & SP_REPEAT))
14155 break;
14156 nest = 1; /* search for next unmatched */
14157 }
14158 }
14159
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014160 if (match_pos != NULL)
14161 {
14162 /* Store the match cursor position */
14163 match_pos->lnum = curwin->w_cursor.lnum;
14164 match_pos->col = curwin->w_cursor.col + 1;
14165 }
14166
Bram Moolenaar071d4272004-06-13 20:20:40 +000014167 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014168 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014169 curwin->w_cursor = save_cursor;
14170
14171theend:
14172 vim_free(pat2);
14173 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014174 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014175
14176 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014177}
14178
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014179/*
14180 * "searchpos()" function
14181 */
14182 static void
14183f_searchpos(argvars, rettv)
14184 typval_T *argvars;
14185 typval_T *rettv;
14186{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014187 pos_T match_pos;
14188 int lnum = 0;
14189 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014190 int n;
14191 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014192
14193 rettv->vval.v_number = 0;
14194
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014195 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014196 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014197
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014198 n = search_cmn(argvars, &match_pos, &flags);
14199 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014200 {
14201 lnum = match_pos.lnum;
14202 col = match_pos.col;
14203 }
14204
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014205 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
14206 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014207 if (flags & SP_SUBPAT)
14208 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014209}
14210
14211
Bram Moolenaar0d660222005-01-07 21:51:51 +000014212/*ARGSUSED*/
14213 static void
14214f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014215 typval_T *argvars;
14216 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014217{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014218#ifdef FEAT_CLIENTSERVER
14219 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014220 char_u *server = get_tv_string_chk(&argvars[0]);
14221 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014222
Bram Moolenaar0d660222005-01-07 21:51:51 +000014223 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014224 if (server == NULL || reply == NULL)
14225 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014226 if (check_restricted() || check_secure())
14227 return;
14228# ifdef FEAT_X11
14229 if (check_connection() == FAIL)
14230 return;
14231# endif
14232
14233 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014234 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014235 EMSG(_("E258: Unable to send to client"));
14236 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014237 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014238 rettv->vval.v_number = 0;
14239#else
14240 rettv->vval.v_number = -1;
14241#endif
14242}
14243
14244/*ARGSUSED*/
14245 static void
14246f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014247 typval_T *argvars;
14248 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014249{
14250 char_u *r = NULL;
14251
14252#ifdef FEAT_CLIENTSERVER
14253# ifdef WIN32
14254 r = serverGetVimNames();
14255# else
14256 make_connection();
14257 if (X_DISPLAY != NULL)
14258 r = serverGetVimNames(X_DISPLAY);
14259# endif
14260#endif
14261 rettv->v_type = VAR_STRING;
14262 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014263}
14264
14265/*
14266 * "setbufvar()" function
14267 */
14268/*ARGSUSED*/
14269 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014270f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014271 typval_T *argvars;
14272 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014273{
14274 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014275 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014276 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014277 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014278 char_u nbuf[NUMBUFLEN];
14279
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014280 rettv->vval.v_number = 0;
14281
Bram Moolenaar071d4272004-06-13 20:20:40 +000014282 if (check_restricted() || check_secure())
14283 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014284 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
14285 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014286 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014287 varp = &argvars[2];
14288
14289 if (buf != NULL && varname != NULL && varp != NULL)
14290 {
14291 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014292 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014293
14294 if (*varname == '&')
14295 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014296 long numval;
14297 char_u *strval;
14298 int error = FALSE;
14299
Bram Moolenaar071d4272004-06-13 20:20:40 +000014300 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014301 numval = get_tv_number_chk(varp, &error);
14302 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014303 if (!error && strval != NULL)
14304 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014305 }
14306 else
14307 {
14308 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
14309 if (bufvarname != NULL)
14310 {
14311 STRCPY(bufvarname, "b:");
14312 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014313 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014314 vim_free(bufvarname);
14315 }
14316 }
14317
14318 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014319 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014320 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014321}
14322
14323/*
14324 * "setcmdpos()" function
14325 */
14326 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014327f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014328 typval_T *argvars;
14329 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014330{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014331 int pos = (int)get_tv_number(&argvars[0]) - 1;
14332
14333 if (pos >= 0)
14334 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014335}
14336
14337/*
14338 * "setline()" function
14339 */
14340 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014341f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014342 typval_T *argvars;
14343 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014344{
14345 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000014346 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014347 list_T *l = NULL;
14348 listitem_T *li = NULL;
14349 long added = 0;
14350 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014351
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014352 lnum = get_tv_lnum(&argvars[0]);
14353 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014354 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014355 l = argvars[1].vval.v_list;
14356 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014357 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014358 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014359 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014360
14361 rettv->vval.v_number = 0; /* OK */
14362 for (;;)
14363 {
14364 if (l != NULL)
14365 {
14366 /* list argument, get next string */
14367 if (li == NULL)
14368 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014369 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014370 li = li->li_next;
14371 }
14372
14373 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014374 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014375 break;
14376 if (lnum <= curbuf->b_ml.ml_line_count)
14377 {
14378 /* existing line, replace it */
14379 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
14380 {
14381 changed_bytes(lnum, 0);
14382 check_cursor_col();
14383 rettv->vval.v_number = 0; /* OK */
14384 }
14385 }
14386 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
14387 {
14388 /* lnum is one past the last line, append the line */
14389 ++added;
14390 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
14391 rettv->vval.v_number = 0; /* OK */
14392 }
14393
14394 if (l == NULL) /* only one string argument */
14395 break;
14396 ++lnum;
14397 }
14398
14399 if (added > 0)
14400 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014401}
14402
14403/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014404 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000014405 */
14406/*ARGSUSED*/
14407 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014408set_qf_ll_list(wp, list_arg, action_arg, rettv)
14409 win_T *wp;
14410 typval_T *list_arg;
14411 typval_T *action_arg;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014412 typval_T *rettv;
14413{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014414#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014415 char_u *act;
14416 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014417#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014418
Bram Moolenaar2641f772005-03-25 21:58:17 +000014419 rettv->vval.v_number = -1;
14420
14421#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014422 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014423 EMSG(_(e_listreq));
14424 else
14425 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014426 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014427
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014428 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014429 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014430 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014431 if (act == NULL)
14432 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014433 if (*act == 'a' || *act == 'r')
14434 action = *act;
14435 }
14436
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014437 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014438 rettv->vval.v_number = 0;
14439 }
14440#endif
14441}
14442
14443/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014444 * "setloclist()" function
14445 */
14446/*ARGSUSED*/
14447 static void
14448f_setloclist(argvars, rettv)
14449 typval_T *argvars;
14450 typval_T *rettv;
14451{
14452 win_T *win;
14453
14454 rettv->vval.v_number = -1;
14455
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014456 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014457 if (win != NULL)
14458 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
14459}
14460
14461/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014462 * "setpos()" function
14463 */
14464/*ARGSUSED*/
14465 static void
14466f_setpos(argvars, rettv)
14467 typval_T *argvars;
14468 typval_T *rettv;
14469{
14470 pos_T pos;
14471 int fnum;
14472 char_u *name;
14473
14474 name = get_tv_string_chk(argvars);
14475 if (name != NULL)
14476 {
14477 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
14478 {
14479 --pos.col;
14480 if (name[0] == '.') /* cursor */
14481 {
14482 if (fnum == curbuf->b_fnum)
14483 {
14484 curwin->w_cursor = pos;
14485 check_cursor();
14486 }
14487 else
14488 EMSG(_(e_invarg));
14489 }
14490 else if (name[0] == '\'') /* mark */
14491 (void)setmark_pos(name[1], &pos, fnum);
14492 else
14493 EMSG(_(e_invarg));
14494 }
14495 }
14496}
14497
14498/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014499 * "setqflist()" function
14500 */
14501/*ARGSUSED*/
14502 static void
14503f_setqflist(argvars, rettv)
14504 typval_T *argvars;
14505 typval_T *rettv;
14506{
14507 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
14508}
14509
14510/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014511 * "setreg()" function
14512 */
14513 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014514f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014515 typval_T *argvars;
14516 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014517{
14518 int regname;
14519 char_u *strregname;
14520 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014521 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014522 int append;
14523 char_u yank_type;
14524 long block_len;
14525
14526 block_len = -1;
14527 yank_type = MAUTO;
14528 append = FALSE;
14529
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014530 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014531 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014532
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014533 if (strregname == NULL)
14534 return; /* type error; errmsg already given */
14535 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014536 if (regname == 0 || regname == '@')
14537 regname = '"';
14538 else if (regname == '=')
14539 return;
14540
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014541 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014542 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014543 stropt = get_tv_string_chk(&argvars[2]);
14544 if (stropt == NULL)
14545 return; /* type error */
14546 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014547 switch (*stropt)
14548 {
14549 case 'a': case 'A': /* append */
14550 append = TRUE;
14551 break;
14552 case 'v': case 'c': /* character-wise selection */
14553 yank_type = MCHAR;
14554 break;
14555 case 'V': case 'l': /* line-wise selection */
14556 yank_type = MLINE;
14557 break;
14558#ifdef FEAT_VISUAL
14559 case 'b': case Ctrl_V: /* block-wise selection */
14560 yank_type = MBLOCK;
14561 if (VIM_ISDIGIT(stropt[1]))
14562 {
14563 ++stropt;
14564 block_len = getdigits(&stropt) - 1;
14565 --stropt;
14566 }
14567 break;
14568#endif
14569 }
14570 }
14571
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014572 strval = get_tv_string_chk(&argvars[1]);
14573 if (strval != NULL)
14574 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000014575 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014576 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014577}
14578
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014579/*
14580 * "settabwinvar()" function
14581 */
14582 static void
14583f_settabwinvar(argvars, rettv)
14584 typval_T *argvars;
14585 typval_T *rettv;
14586{
14587 setwinvar(argvars, rettv, 1);
14588}
Bram Moolenaar071d4272004-06-13 20:20:40 +000014589
14590/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014591 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014592 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014593 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014594f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014595 typval_T *argvars;
14596 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014597{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014598 setwinvar(argvars, rettv, 0);
14599}
14600
14601/*
14602 * "setwinvar()" and "settabwinvar()" functions
14603 */
14604 static void
14605setwinvar(argvars, rettv, off)
14606 typval_T *argvars;
14607 typval_T *rettv;
14608 int off;
14609{
Bram Moolenaar071d4272004-06-13 20:20:40 +000014610 win_T *win;
14611#ifdef FEAT_WINDOWS
14612 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014613 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014614#endif
14615 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014616 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014617 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014618 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014619
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014620 rettv->vval.v_number = 0;
14621
Bram Moolenaar071d4272004-06-13 20:20:40 +000014622 if (check_restricted() || check_secure())
14623 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014624
14625#ifdef FEAT_WINDOWS
14626 if (off == 1)
14627 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
14628 else
14629 tp = curtab;
14630#endif
14631 win = find_win_by_nr(&argvars[off], tp);
14632 varname = get_tv_string_chk(&argvars[off + 1]);
14633 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014634
14635 if (win != NULL && varname != NULL && varp != NULL)
14636 {
14637#ifdef FEAT_WINDOWS
14638 /* set curwin to be our win, temporarily */
14639 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014640 save_curtab = curtab;
14641 goto_tabpage_tp(tp);
14642 if (!win_valid(win))
14643 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014644 curwin = win;
14645 curbuf = curwin->w_buffer;
14646#endif
14647
14648 if (*varname == '&')
14649 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014650 long numval;
14651 char_u *strval;
14652 int error = FALSE;
14653
Bram Moolenaar071d4272004-06-13 20:20:40 +000014654 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014655 numval = get_tv_number_chk(varp, &error);
14656 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014657 if (!error && strval != NULL)
14658 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014659 }
14660 else
14661 {
14662 winvarname = alloc((unsigned)STRLEN(varname) + 3);
14663 if (winvarname != NULL)
14664 {
14665 STRCPY(winvarname, "w:");
14666 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014667 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014668 vim_free(winvarname);
14669 }
14670 }
14671
14672#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014673 /* Restore current tabpage and window, if still valid (autocomands can
14674 * make them invalid). */
14675 if (valid_tabpage(save_curtab))
14676 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014677 if (win_valid(save_curwin))
14678 {
14679 curwin = save_curwin;
14680 curbuf = curwin->w_buffer;
14681 }
14682#endif
14683 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014684}
14685
14686/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000014687 * "shellescape({string})" function
14688 */
14689 static void
14690f_shellescape(argvars, rettv)
14691 typval_T *argvars;
14692 typval_T *rettv;
14693{
14694 rettv->vval.v_string = vim_strsave_shellescape(get_tv_string(&argvars[0]));
14695 rettv->v_type = VAR_STRING;
14696}
14697
14698/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014699 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014700 */
14701 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014702f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014703 typval_T *argvars;
14704 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014705{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014706 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014707
Bram Moolenaar0d660222005-01-07 21:51:51 +000014708 p = get_tv_string(&argvars[0]);
14709 rettv->vval.v_string = vim_strsave(p);
14710 simplify_filename(rettv->vval.v_string); /* simplify in place */
14711 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014712}
14713
Bram Moolenaar0d660222005-01-07 21:51:51 +000014714static int
14715#ifdef __BORLANDC__
14716 _RTLENTRYF
14717#endif
14718 item_compare __ARGS((const void *s1, const void *s2));
14719static int
14720#ifdef __BORLANDC__
14721 _RTLENTRYF
14722#endif
14723 item_compare2 __ARGS((const void *s1, const void *s2));
14724
14725static int item_compare_ic;
14726static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014727static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014728#define ITEM_COMPARE_FAIL 999
14729
Bram Moolenaar071d4272004-06-13 20:20:40 +000014730/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014731 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014732 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014733 static int
14734#ifdef __BORLANDC__
14735_RTLENTRYF
14736#endif
14737item_compare(s1, s2)
14738 const void *s1;
14739 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014740{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014741 char_u *p1, *p2;
14742 char_u *tofree1, *tofree2;
14743 int res;
14744 char_u numbuf1[NUMBUFLEN];
14745 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014746
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014747 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
14748 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014749 if (item_compare_ic)
14750 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014751 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014752 res = STRCMP(p1, p2);
14753 vim_free(tofree1);
14754 vim_free(tofree2);
14755 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014756}
14757
14758 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000014759#ifdef __BORLANDC__
14760_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014761#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000014762item_compare2(s1, s2)
14763 const void *s1;
14764 const void *s2;
14765{
14766 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000014767 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014768 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000014769 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014770
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014771 /* shortcut after failure in previous call; compare all items equal */
14772 if (item_compare_func_err)
14773 return 0;
14774
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014775 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
14776 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014777 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
14778 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014779
14780 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014781 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000014782 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014783 clear_tv(&argv[0]);
14784 clear_tv(&argv[1]);
14785
14786 if (res == FAIL)
14787 res = ITEM_COMPARE_FAIL;
14788 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014789 /* return value has wrong type */
14790 res = get_tv_number_chk(&rettv, &item_compare_func_err);
14791 if (item_compare_func_err)
14792 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014793 clear_tv(&rettv);
14794 return res;
14795}
14796
14797/*
14798 * "sort({list})" function
14799 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014800 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014801f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014802 typval_T *argvars;
14803 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014804{
Bram Moolenaar33570922005-01-25 22:26:29 +000014805 list_T *l;
14806 listitem_T *li;
14807 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014808 long len;
14809 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014810
Bram Moolenaar0d660222005-01-07 21:51:51 +000014811 rettv->vval.v_number = 0;
14812 if (argvars[0].v_type != VAR_LIST)
14813 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000014814 else
14815 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014816 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014817 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014818 return;
14819 rettv->vval.v_list = l;
14820 rettv->v_type = VAR_LIST;
14821 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014822
Bram Moolenaar0d660222005-01-07 21:51:51 +000014823 len = list_len(l);
14824 if (len <= 1)
14825 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014826
Bram Moolenaar0d660222005-01-07 21:51:51 +000014827 item_compare_ic = FALSE;
14828 item_compare_func = NULL;
14829 if (argvars[1].v_type != VAR_UNKNOWN)
14830 {
14831 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014832 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014833 else
14834 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014835 int error = FALSE;
14836
14837 i = get_tv_number_chk(&argvars[1], &error);
14838 if (error)
14839 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014840 if (i == 1)
14841 item_compare_ic = TRUE;
14842 else
14843 item_compare_func = get_tv_string(&argvars[1]);
14844 }
14845 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014846
Bram Moolenaar0d660222005-01-07 21:51:51 +000014847 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014848 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014849 if (ptrs == NULL)
14850 return;
14851 i = 0;
14852 for (li = l->lv_first; li != NULL; li = li->li_next)
14853 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014854
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014855 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014856 /* test the compare function */
14857 if (item_compare_func != NULL
14858 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
14859 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014860 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014861 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014862 {
14863 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014864 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000014865 item_compare_func == NULL ? item_compare : item_compare2);
14866
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014867 if (!item_compare_func_err)
14868 {
14869 /* Clear the List and append the items in the sorted order. */
14870 l->lv_first = l->lv_last = NULL;
14871 l->lv_len = 0;
14872 for (i = 0; i < len; ++i)
14873 list_append(l, ptrs[i]);
14874 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014875 }
14876
14877 vim_free(ptrs);
14878 }
14879}
14880
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014881/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000014882 * "soundfold({word})" function
14883 */
14884 static void
14885f_soundfold(argvars, rettv)
14886 typval_T *argvars;
14887 typval_T *rettv;
14888{
14889 char_u *s;
14890
14891 rettv->v_type = VAR_STRING;
14892 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014893#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000014894 rettv->vval.v_string = eval_soundfold(s);
14895#else
14896 rettv->vval.v_string = vim_strsave(s);
14897#endif
14898}
14899
14900/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014901 * "spellbadword()" function
14902 */
14903/* ARGSUSED */
14904 static void
14905f_spellbadword(argvars, rettv)
14906 typval_T *argvars;
14907 typval_T *rettv;
14908{
Bram Moolenaar4463f292005-09-25 22:20:24 +000014909 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014910 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014911 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014912
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014913 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014914 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014915
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014916#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000014917 if (argvars[0].v_type == VAR_UNKNOWN)
14918 {
14919 /* Find the start and length of the badly spelled word. */
14920 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
14921 if (len != 0)
14922 word = ml_get_cursor();
14923 }
14924 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14925 {
14926 char_u *str = get_tv_string_chk(&argvars[0]);
14927 int capcol = -1;
14928
14929 if (str != NULL)
14930 {
14931 /* Check the argument for spelling. */
14932 while (*str != NUL)
14933 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014934 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014935 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014936 {
14937 word = str;
14938 break;
14939 }
14940 str += len;
14941 }
14942 }
14943 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014944#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000014945
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014946 list_append_string(rettv->vval.v_list, word, len);
14947 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014948 attr == HLF_SPB ? "bad" :
14949 attr == HLF_SPR ? "rare" :
14950 attr == HLF_SPL ? "local" :
14951 attr == HLF_SPC ? "caps" :
14952 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014953}
14954
14955/*
14956 * "spellsuggest()" function
14957 */
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014958/*ARGSUSED*/
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014959 static void
14960f_spellsuggest(argvars, rettv)
14961 typval_T *argvars;
14962 typval_T *rettv;
14963{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014964#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014965 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014966 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014967 int maxcount;
14968 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014969 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014970 listitem_T *li;
14971 int need_capital = FALSE;
14972#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014973
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014974 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014975 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014976
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014977#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014978 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14979 {
14980 str = get_tv_string(&argvars[0]);
14981 if (argvars[1].v_type != VAR_UNKNOWN)
14982 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014983 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014984 if (maxcount <= 0)
14985 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014986 if (argvars[2].v_type != VAR_UNKNOWN)
14987 {
14988 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
14989 if (typeerr)
14990 return;
14991 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014992 }
14993 else
14994 maxcount = 25;
14995
Bram Moolenaar4770d092006-01-12 23:22:24 +000014996 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014997
14998 for (i = 0; i < ga.ga_len; ++i)
14999 {
15000 str = ((char_u **)ga.ga_data)[i];
15001
15002 li = listitem_alloc();
15003 if (li == NULL)
15004 vim_free(str);
15005 else
15006 {
15007 li->li_tv.v_type = VAR_STRING;
15008 li->li_tv.v_lock = 0;
15009 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015010 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015011 }
15012 }
15013 ga_clear(&ga);
15014 }
15015#endif
15016}
15017
Bram Moolenaar0d660222005-01-07 21:51:51 +000015018 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015019f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015020 typval_T *argvars;
15021 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015022{
15023 char_u *str;
15024 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015025 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015026 regmatch_T regmatch;
15027 char_u patbuf[NUMBUFLEN];
15028 char_u *save_cpo;
15029 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015030 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015031 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015032 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015033
15034 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15035 save_cpo = p_cpo;
15036 p_cpo = (char_u *)"";
15037
15038 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015039 if (argvars[1].v_type != VAR_UNKNOWN)
15040 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015041 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15042 if (pat == NULL)
15043 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015044 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015045 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015046 }
15047 if (pat == NULL || *pat == NUL)
15048 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000015049
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015050 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015051 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015052 if (typeerr)
15053 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015054
Bram Moolenaar0d660222005-01-07 21:51:51 +000015055 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15056 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015057 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015058 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015059 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015060 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015061 if (*str == NUL)
15062 match = FALSE; /* empty item at the end */
15063 else
15064 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015065 if (match)
15066 end = regmatch.startp[0];
15067 else
15068 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015069 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
15070 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015071 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015072 if (list_append_string(rettv->vval.v_list, str,
15073 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015074 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015075 }
15076 if (!match)
15077 break;
15078 /* Advance to just after the match. */
15079 if (regmatch.endp[0] > str)
15080 col = 0;
15081 else
15082 {
15083 /* Don't get stuck at the same match. */
15084#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015085 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015086#else
15087 col = 1;
15088#endif
15089 }
15090 str = regmatch.endp[0];
15091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015092
Bram Moolenaar0d660222005-01-07 21:51:51 +000015093 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015094 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015095
Bram Moolenaar0d660222005-01-07 21:51:51 +000015096 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015097}
15098
Bram Moolenaar2c932302006-03-18 21:42:09 +000015099/*
15100 * "str2nr()" function
15101 */
15102 static void
15103f_str2nr(argvars, rettv)
15104 typval_T *argvars;
15105 typval_T *rettv;
15106{
15107 int base = 10;
15108 char_u *p;
15109 long n;
15110
15111 if (argvars[1].v_type != VAR_UNKNOWN)
15112 {
15113 base = get_tv_number(&argvars[1]);
15114 if (base != 8 && base != 10 && base != 16)
15115 {
15116 EMSG(_(e_invarg));
15117 return;
15118 }
15119 }
15120
15121 p = skipwhite(get_tv_string(&argvars[0]));
15122 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
15123 rettv->vval.v_number = n;
15124}
15125
Bram Moolenaar071d4272004-06-13 20:20:40 +000015126#ifdef HAVE_STRFTIME
15127/*
15128 * "strftime({format}[, {time}])" function
15129 */
15130 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015131f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015132 typval_T *argvars;
15133 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015134{
15135 char_u result_buf[256];
15136 struct tm *curtime;
15137 time_t seconds;
15138 char_u *p;
15139
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015140 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015141
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015142 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015143 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015144 seconds = time(NULL);
15145 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015146 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015147 curtime = localtime(&seconds);
15148 /* MSVC returns NULL for an invalid value of seconds. */
15149 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015150 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015151 else
15152 {
15153# ifdef FEAT_MBYTE
15154 vimconv_T conv;
15155 char_u *enc;
15156
15157 conv.vc_type = CONV_NONE;
15158 enc = enc_locale();
15159 convert_setup(&conv, p_enc, enc);
15160 if (conv.vc_type != CONV_NONE)
15161 p = string_convert(&conv, p, NULL);
15162# endif
15163 if (p != NULL)
15164 (void)strftime((char *)result_buf, sizeof(result_buf),
15165 (char *)p, curtime);
15166 else
15167 result_buf[0] = NUL;
15168
15169# ifdef FEAT_MBYTE
15170 if (conv.vc_type != CONV_NONE)
15171 vim_free(p);
15172 convert_setup(&conv, enc, p_enc);
15173 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015174 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015175 else
15176# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015177 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015178
15179# ifdef FEAT_MBYTE
15180 /* Release conversion descriptors */
15181 convert_setup(&conv, NULL, NULL);
15182 vim_free(enc);
15183# endif
15184 }
15185}
15186#endif
15187
15188/*
15189 * "stridx()" function
15190 */
15191 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015192f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015193 typval_T *argvars;
15194 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015195{
15196 char_u buf[NUMBUFLEN];
15197 char_u *needle;
15198 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000015199 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015200 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000015201 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015202
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015203 needle = get_tv_string_chk(&argvars[1]);
15204 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000015205 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015206 if (needle == NULL || haystack == NULL)
15207 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015208
Bram Moolenaar33570922005-01-25 22:26:29 +000015209 if (argvars[2].v_type != VAR_UNKNOWN)
15210 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015211 int error = FALSE;
15212
15213 start_idx = get_tv_number_chk(&argvars[2], &error);
15214 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000015215 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015216 if (start_idx >= 0)
15217 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000015218 }
15219
15220 pos = (char_u *)strstr((char *)haystack, (char *)needle);
15221 if (pos != NULL)
15222 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015223}
15224
15225/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015226 * "string()" function
15227 */
15228 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015229f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015230 typval_T *argvars;
15231 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015232{
15233 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015234 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015235
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015236 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015237 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015238 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015239 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015240}
15241
15242/*
15243 * "strlen()" function
15244 */
15245 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015246f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015247 typval_T *argvars;
15248 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015249{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015250 rettv->vval.v_number = (varnumber_T)(STRLEN(
15251 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015252}
15253
15254/*
15255 * "strpart()" function
15256 */
15257 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015258f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015259 typval_T *argvars;
15260 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015261{
15262 char_u *p;
15263 int n;
15264 int len;
15265 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015266 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015267
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015268 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015269 slen = (int)STRLEN(p);
15270
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015271 n = get_tv_number_chk(&argvars[1], &error);
15272 if (error)
15273 len = 0;
15274 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015275 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015276 else
15277 len = slen - n; /* default len: all bytes that are available. */
15278
15279 /*
15280 * Only return the overlap between the specified part and the actual
15281 * string.
15282 */
15283 if (n < 0)
15284 {
15285 len += n;
15286 n = 0;
15287 }
15288 else if (n > slen)
15289 n = slen;
15290 if (len < 0)
15291 len = 0;
15292 else if (n + len > slen)
15293 len = slen - n;
15294
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015295 rettv->v_type = VAR_STRING;
15296 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015297}
15298
15299/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015300 * "strridx()" function
15301 */
15302 static void
15303f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015304 typval_T *argvars;
15305 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015306{
15307 char_u buf[NUMBUFLEN];
15308 char_u *needle;
15309 char_u *haystack;
15310 char_u *rest;
15311 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015312 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015313
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015314 needle = get_tv_string_chk(&argvars[1]);
15315 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015316
15317 rettv->vval.v_number = -1;
15318 if (needle == NULL || haystack == NULL)
15319 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015320
15321 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000015322 if (argvars[2].v_type != VAR_UNKNOWN)
15323 {
15324 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015325 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000015326 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015327 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000015328 }
15329 else
15330 end_idx = haystack_len;
15331
Bram Moolenaar0d660222005-01-07 21:51:51 +000015332 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000015333 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015334 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000015335 lastmatch = haystack + end_idx;
15336 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015337 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000015338 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015339 for (rest = haystack; *rest != '\0'; ++rest)
15340 {
15341 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000015342 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015343 break;
15344 lastmatch = rest;
15345 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000015346 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015347
15348 if (lastmatch == NULL)
15349 rettv->vval.v_number = -1;
15350 else
15351 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
15352}
15353
15354/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015355 * "strtrans()" function
15356 */
15357 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015358f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015359 typval_T *argvars;
15360 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015361{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015362 rettv->v_type = VAR_STRING;
15363 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015364}
15365
15366/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015367 * "submatch()" function
15368 */
15369 static void
15370f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015371 typval_T *argvars;
15372 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015373{
15374 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015375 rettv->vval.v_string =
15376 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015377}
15378
15379/*
15380 * "substitute()" function
15381 */
15382 static void
15383f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015384 typval_T *argvars;
15385 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015386{
15387 char_u patbuf[NUMBUFLEN];
15388 char_u subbuf[NUMBUFLEN];
15389 char_u flagsbuf[NUMBUFLEN];
15390
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015391 char_u *str = get_tv_string_chk(&argvars[0]);
15392 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15393 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
15394 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
15395
Bram Moolenaar0d660222005-01-07 21:51:51 +000015396 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015397 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
15398 rettv->vval.v_string = NULL;
15399 else
15400 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015401}
15402
15403/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015404 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015405 */
15406/*ARGSUSED*/
15407 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015408f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015409 typval_T *argvars;
15410 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015411{
15412 int id = 0;
15413#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015414 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015415 long col;
15416 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000015417 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015418
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015419 lnum = get_tv_lnum(argvars); /* -1 on type error */
15420 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
15421 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015422
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015423 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015424 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +000015425 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015426#endif
15427
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015428 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015429}
15430
15431/*
15432 * "synIDattr(id, what [, mode])" function
15433 */
15434/*ARGSUSED*/
15435 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015436f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015437 typval_T *argvars;
15438 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015439{
15440 char_u *p = NULL;
15441#ifdef FEAT_SYN_HL
15442 int id;
15443 char_u *what;
15444 char_u *mode;
15445 char_u modebuf[NUMBUFLEN];
15446 int modec;
15447
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015448 id = get_tv_number(&argvars[0]);
15449 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015450 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015451 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015452 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015453 modec = TOLOWER_ASC(mode[0]);
15454 if (modec != 't' && modec != 'c'
15455#ifdef FEAT_GUI
15456 && modec != 'g'
15457#endif
15458 )
15459 modec = 0; /* replace invalid with current */
15460 }
15461 else
15462 {
15463#ifdef FEAT_GUI
15464 if (gui.in_use)
15465 modec = 'g';
15466 else
15467#endif
15468 if (t_colors > 1)
15469 modec = 'c';
15470 else
15471 modec = 't';
15472 }
15473
15474
15475 switch (TOLOWER_ASC(what[0]))
15476 {
15477 case 'b':
15478 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
15479 p = highlight_color(id, what, modec);
15480 else /* bold */
15481 p = highlight_has_attr(id, HL_BOLD, modec);
15482 break;
15483
15484 case 'f': /* fg[#] */
15485 p = highlight_color(id, what, modec);
15486 break;
15487
15488 case 'i':
15489 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
15490 p = highlight_has_attr(id, HL_INVERSE, modec);
15491 else /* italic */
15492 p = highlight_has_attr(id, HL_ITALIC, modec);
15493 break;
15494
15495 case 'n': /* name */
15496 p = get_highlight_name(NULL, id - 1);
15497 break;
15498
15499 case 'r': /* reverse */
15500 p = highlight_has_attr(id, HL_INVERSE, modec);
15501 break;
15502
15503 case 's': /* standout */
15504 p = highlight_has_attr(id, HL_STANDOUT, modec);
15505 break;
15506
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000015507 case 'u':
15508 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
15509 /* underline */
15510 p = highlight_has_attr(id, HL_UNDERLINE, modec);
15511 else
15512 /* undercurl */
15513 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015514 break;
15515 }
15516
15517 if (p != NULL)
15518 p = vim_strsave(p);
15519#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015520 rettv->v_type = VAR_STRING;
15521 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015522}
15523
15524/*
15525 * "synIDtrans(id)" function
15526 */
15527/*ARGSUSED*/
15528 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015529f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015530 typval_T *argvars;
15531 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015532{
15533 int id;
15534
15535#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015536 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015537
15538 if (id > 0)
15539 id = syn_get_final_id(id);
15540 else
15541#endif
15542 id = 0;
15543
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015544 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015545}
15546
15547/*
15548 * "system()" function
15549 */
15550 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015551f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015552 typval_T *argvars;
15553 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015554{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015555 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015556 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015557 char_u *infile = NULL;
15558 char_u buf[NUMBUFLEN];
15559 int err = FALSE;
15560 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015561
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015562 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015563 {
15564 /*
15565 * Write the string to a temp file, to be used for input of the shell
15566 * command.
15567 */
15568 if ((infile = vim_tempname('i')) == NULL)
15569 {
15570 EMSG(_(e_notmp));
15571 return;
15572 }
15573
15574 fd = mch_fopen((char *)infile, WRITEBIN);
15575 if (fd == NULL)
15576 {
15577 EMSG2(_(e_notopen), infile);
15578 goto done;
15579 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015580 p = get_tv_string_buf_chk(&argvars[1], buf);
15581 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015582 {
15583 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015584 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015585 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015586 if (fwrite(p, STRLEN(p), 1, fd) != 1)
15587 err = TRUE;
15588 if (fclose(fd) != 0)
15589 err = TRUE;
15590 if (err)
15591 {
15592 EMSG(_("E677: Error writing temp file"));
15593 goto done;
15594 }
15595 }
15596
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015597 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
15598 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015599
Bram Moolenaar071d4272004-06-13 20:20:40 +000015600#ifdef USE_CR
15601 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015602 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015603 {
15604 char_u *s;
15605
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015606 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015607 {
15608 if (*s == CAR)
15609 *s = NL;
15610 }
15611 }
15612#else
15613# ifdef USE_CRNL
15614 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015615 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015616 {
15617 char_u *s, *d;
15618
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015619 d = res;
15620 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015621 {
15622 if (s[0] == CAR && s[1] == NL)
15623 ++s;
15624 *d++ = *s;
15625 }
15626 *d = NUL;
15627 }
15628# endif
15629#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015630
15631done:
15632 if (infile != NULL)
15633 {
15634 mch_remove(infile);
15635 vim_free(infile);
15636 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015637 rettv->v_type = VAR_STRING;
15638 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015639}
15640
15641/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015642 * "tabpagebuflist()" function
15643 */
15644/* ARGSUSED */
15645 static void
15646f_tabpagebuflist(argvars, rettv)
15647 typval_T *argvars;
15648 typval_T *rettv;
15649{
15650#ifndef FEAT_WINDOWS
15651 rettv->vval.v_number = 0;
15652#else
15653 tabpage_T *tp;
15654 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015655
15656 if (argvars[0].v_type == VAR_UNKNOWN)
15657 wp = firstwin;
15658 else
15659 {
15660 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15661 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000015662 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015663 }
15664 if (wp == NULL)
15665 rettv->vval.v_number = 0;
15666 else
15667 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015668 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015669 rettv->vval.v_number = 0;
15670 else
15671 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015672 for (; wp != NULL; wp = wp->w_next)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015673 if (list_append_number(rettv->vval.v_list,
15674 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015675 break;
15676 }
15677 }
15678#endif
15679}
15680
15681
15682/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015683 * "tabpagenr()" function
15684 */
15685/* ARGSUSED */
15686 static void
15687f_tabpagenr(argvars, rettv)
15688 typval_T *argvars;
15689 typval_T *rettv;
15690{
15691 int nr = 1;
15692#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015693 char_u *arg;
15694
15695 if (argvars[0].v_type != VAR_UNKNOWN)
15696 {
15697 arg = get_tv_string_chk(&argvars[0]);
15698 nr = 0;
15699 if (arg != NULL)
15700 {
15701 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000015702 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015703 else
15704 EMSG2(_(e_invexpr2), arg);
15705 }
15706 }
15707 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015708 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015709#endif
15710 rettv->vval.v_number = nr;
15711}
15712
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015713
15714#ifdef FEAT_WINDOWS
15715static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
15716
15717/*
15718 * Common code for tabpagewinnr() and winnr().
15719 */
15720 static int
15721get_winnr(tp, argvar)
15722 tabpage_T *tp;
15723 typval_T *argvar;
15724{
15725 win_T *twin;
15726 int nr = 1;
15727 win_T *wp;
15728 char_u *arg;
15729
15730 twin = (tp == curtab) ? curwin : tp->tp_curwin;
15731 if (argvar->v_type != VAR_UNKNOWN)
15732 {
15733 arg = get_tv_string_chk(argvar);
15734 if (arg == NULL)
15735 nr = 0; /* type error; errmsg already given */
15736 else if (STRCMP(arg, "$") == 0)
15737 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
15738 else if (STRCMP(arg, "#") == 0)
15739 {
15740 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
15741 if (twin == NULL)
15742 nr = 0;
15743 }
15744 else
15745 {
15746 EMSG2(_(e_invexpr2), arg);
15747 nr = 0;
15748 }
15749 }
15750
15751 if (nr > 0)
15752 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
15753 wp != twin; wp = wp->w_next)
15754 ++nr;
15755 return nr;
15756}
15757#endif
15758
15759/*
15760 * "tabpagewinnr()" function
15761 */
15762/* ARGSUSED */
15763 static void
15764f_tabpagewinnr(argvars, rettv)
15765 typval_T *argvars;
15766 typval_T *rettv;
15767{
15768 int nr = 1;
15769#ifdef FEAT_WINDOWS
15770 tabpage_T *tp;
15771
15772 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15773 if (tp == NULL)
15774 nr = 0;
15775 else
15776 nr = get_winnr(tp, &argvars[1]);
15777#endif
15778 rettv->vval.v_number = nr;
15779}
15780
15781
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015782/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015783 * "tagfiles()" function
15784 */
15785/*ARGSUSED*/
15786 static void
15787f_tagfiles(argvars, rettv)
15788 typval_T *argvars;
15789 typval_T *rettv;
15790{
15791 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015792 tagname_T tn;
15793 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015794
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015795 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015796 {
15797 rettv->vval.v_number = 0;
15798 return;
15799 }
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015800
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015801 for (first = TRUE; ; first = FALSE)
15802 if (get_tagfname(&tn, first, fname) == FAIL
15803 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015804 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015805 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015806}
15807
15808/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000015809 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015810 */
15811 static void
15812f_taglist(argvars, rettv)
15813 typval_T *argvars;
15814 typval_T *rettv;
15815{
15816 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015817
15818 tag_pattern = get_tv_string(&argvars[0]);
15819
15820 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015821 if (*tag_pattern == NUL)
15822 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015823
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015824 if (rettv_list_alloc(rettv) == OK)
15825 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015826}
15827
15828/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015829 * "tempname()" function
15830 */
15831/*ARGSUSED*/
15832 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015833f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015834 typval_T *argvars;
15835 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015836{
15837 static int x = 'A';
15838
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015839 rettv->v_type = VAR_STRING;
15840 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015841
15842 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
15843 * names. Skip 'I' and 'O', they are used for shell redirection. */
15844 do
15845 {
15846 if (x == 'Z')
15847 x = '0';
15848 else if (x == '9')
15849 x = 'A';
15850 else
15851 {
15852#ifdef EBCDIC
15853 if (x == 'I')
15854 x = 'J';
15855 else if (x == 'R')
15856 x = 'S';
15857 else
15858#endif
15859 ++x;
15860 }
15861 } while (x == 'I' || x == 'O');
15862}
15863
15864/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000015865 * "test(list)" function: Just checking the walls...
15866 */
15867/*ARGSUSED*/
15868 static void
15869f_test(argvars, rettv)
15870 typval_T *argvars;
15871 typval_T *rettv;
15872{
15873 /* Used for unit testing. Change the code below to your liking. */
15874#if 0
15875 listitem_T *li;
15876 list_T *l;
15877 char_u *bad, *good;
15878
15879 if (argvars[0].v_type != VAR_LIST)
15880 return;
15881 l = argvars[0].vval.v_list;
15882 if (l == NULL)
15883 return;
15884 li = l->lv_first;
15885 if (li == NULL)
15886 return;
15887 bad = get_tv_string(&li->li_tv);
15888 li = li->li_next;
15889 if (li == NULL)
15890 return;
15891 good = get_tv_string(&li->li_tv);
15892 rettv->vval.v_number = test_edit_score(bad, good);
15893#endif
15894}
15895
15896/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015897 * "tolower(string)" function
15898 */
15899 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015900f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015901 typval_T *argvars;
15902 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015903{
15904 char_u *p;
15905
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015906 p = vim_strsave(get_tv_string(&argvars[0]));
15907 rettv->v_type = VAR_STRING;
15908 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015909
15910 if (p != NULL)
15911 while (*p != NUL)
15912 {
15913#ifdef FEAT_MBYTE
15914 int l;
15915
15916 if (enc_utf8)
15917 {
15918 int c, lc;
15919
15920 c = utf_ptr2char(p);
15921 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015922 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015923 /* TODO: reallocate string when byte count changes. */
15924 if (utf_char2len(lc) == l)
15925 utf_char2bytes(lc, p);
15926 p += l;
15927 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015928 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015929 p += l; /* skip multi-byte character */
15930 else
15931#endif
15932 {
15933 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
15934 ++p;
15935 }
15936 }
15937}
15938
15939/*
15940 * "toupper(string)" function
15941 */
15942 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015943f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015944 typval_T *argvars;
15945 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015946{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015947 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015948 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015949}
15950
15951/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000015952 * "tr(string, fromstr, tostr)" function
15953 */
15954 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015955f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015956 typval_T *argvars;
15957 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015958{
15959 char_u *instr;
15960 char_u *fromstr;
15961 char_u *tostr;
15962 char_u *p;
15963#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000015964 int inlen;
15965 int fromlen;
15966 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015967 int idx;
15968 char_u *cpstr;
15969 int cplen;
15970 int first = TRUE;
15971#endif
15972 char_u buf[NUMBUFLEN];
15973 char_u buf2[NUMBUFLEN];
15974 garray_T ga;
15975
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015976 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015977 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
15978 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015979
15980 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015981 rettv->v_type = VAR_STRING;
15982 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015983 if (fromstr == NULL || tostr == NULL)
15984 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000015985 ga_init2(&ga, (int)sizeof(char), 80);
15986
15987#ifdef FEAT_MBYTE
15988 if (!has_mbyte)
15989#endif
15990 /* not multi-byte: fromstr and tostr must be the same length */
15991 if (STRLEN(fromstr) != STRLEN(tostr))
15992 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015993#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000015994error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015995#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000015996 EMSG2(_(e_invarg2), fromstr);
15997 ga_clear(&ga);
15998 return;
15999 }
16000
16001 /* fromstr and tostr have to contain the same number of chars */
16002 while (*instr != NUL)
16003 {
16004#ifdef FEAT_MBYTE
16005 if (has_mbyte)
16006 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016007 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016008 cpstr = instr;
16009 cplen = inlen;
16010 idx = 0;
16011 for (p = fromstr; *p != NUL; p += fromlen)
16012 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016013 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016014 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
16015 {
16016 for (p = tostr; *p != NUL; p += tolen)
16017 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016018 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016019 if (idx-- == 0)
16020 {
16021 cplen = tolen;
16022 cpstr = p;
16023 break;
16024 }
16025 }
16026 if (*p == NUL) /* tostr is shorter than fromstr */
16027 goto error;
16028 break;
16029 }
16030 ++idx;
16031 }
16032
16033 if (first && cpstr == instr)
16034 {
16035 /* Check that fromstr and tostr have the same number of
16036 * (multi-byte) characters. Done only once when a character
16037 * of instr doesn't appear in fromstr. */
16038 first = FALSE;
16039 for (p = tostr; *p != NUL; p += tolen)
16040 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016041 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016042 --idx;
16043 }
16044 if (idx != 0)
16045 goto error;
16046 }
16047
16048 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000016049 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016050 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000016051
16052 instr += inlen;
16053 }
16054 else
16055#endif
16056 {
16057 /* When not using multi-byte chars we can do it faster. */
16058 p = vim_strchr(fromstr, *instr);
16059 if (p != NULL)
16060 ga_append(&ga, tostr[p - fromstr]);
16061 else
16062 ga_append(&ga, *instr);
16063 ++instr;
16064 }
16065 }
16066
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016067 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000016068}
16069
16070/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016071 * "type(expr)" function
16072 */
16073 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016074f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016075 typval_T *argvars;
16076 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016077{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016078 int n;
16079
16080 switch (argvars[0].v_type)
16081 {
16082 case VAR_NUMBER: n = 0; break;
16083 case VAR_STRING: n = 1; break;
16084 case VAR_FUNC: n = 2; break;
16085 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016086 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016087 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
16088 }
16089 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016090}
16091
16092/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016093 * "values(dict)" function
16094 */
16095 static void
16096f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016097 typval_T *argvars;
16098 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016099{
16100 dict_list(argvars, rettv, 1);
16101}
16102
16103/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016104 * "virtcol(string)" function
16105 */
16106 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016107f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016108 typval_T *argvars;
16109 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016110{
16111 colnr_T vcol = 0;
16112 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016113 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016114
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016115 fp = var2fpos(&argvars[0], FALSE, &fnum);
16116 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
16117 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016118 {
16119 getvvcol(curwin, fp, NULL, NULL, &vcol);
16120 ++vcol;
16121 }
16122
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016123 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016124}
16125
16126/*
16127 * "visualmode()" function
16128 */
16129/*ARGSUSED*/
16130 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016131f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016132 typval_T *argvars;
16133 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016134{
16135#ifdef FEAT_VISUAL
16136 char_u str[2];
16137
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016138 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016139 str[0] = curbuf->b_visual_mode_eval;
16140 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016141 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016142
16143 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016144 if ((argvars[0].v_type == VAR_NUMBER
16145 && argvars[0].vval.v_number != 0)
16146 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016147 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016148 curbuf->b_visual_mode_eval = NUL;
16149#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016150 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016151#endif
16152}
16153
16154/*
16155 * "winbufnr(nr)" function
16156 */
16157 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016158f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016159 typval_T *argvars;
16160 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016161{
16162 win_T *wp;
16163
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016164 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016165 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016166 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016167 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016168 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016169}
16170
16171/*
16172 * "wincol()" function
16173 */
16174/*ARGSUSED*/
16175 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016176f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016177 typval_T *argvars;
16178 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016179{
16180 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016181 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016182}
16183
16184/*
16185 * "winheight(nr)" function
16186 */
16187 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016188f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016189 typval_T *argvars;
16190 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016191{
16192 win_T *wp;
16193
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016194 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016195 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016196 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016197 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016198 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016199}
16200
16201/*
16202 * "winline()" function
16203 */
16204/*ARGSUSED*/
16205 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016206f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016207 typval_T *argvars;
16208 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016209{
16210 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016211 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016212}
16213
16214/*
16215 * "winnr()" function
16216 */
16217/* ARGSUSED */
16218 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016219f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016220 typval_T *argvars;
16221 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016222{
16223 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016224
Bram Moolenaar071d4272004-06-13 20:20:40 +000016225#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016226 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016227#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016228 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016229}
16230
16231/*
16232 * "winrestcmd()" function
16233 */
16234/* ARGSUSED */
16235 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016236f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016237 typval_T *argvars;
16238 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016239{
16240#ifdef FEAT_WINDOWS
16241 win_T *wp;
16242 int winnr = 1;
16243 garray_T ga;
16244 char_u buf[50];
16245
16246 ga_init2(&ga, (int)sizeof(char), 70);
16247 for (wp = firstwin; wp != NULL; wp = wp->w_next)
16248 {
16249 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
16250 ga_concat(&ga, buf);
16251# ifdef FEAT_VERTSPLIT
16252 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
16253 ga_concat(&ga, buf);
16254# endif
16255 ++winnr;
16256 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000016257 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016258
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016259 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016260#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016261 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016262#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016263 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016264}
16265
16266/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016267 * "winrestview()" function
16268 */
16269/* ARGSUSED */
16270 static void
16271f_winrestview(argvars, rettv)
16272 typval_T *argvars;
16273 typval_T *rettv;
16274{
16275 dict_T *dict;
16276
16277 if (argvars[0].v_type != VAR_DICT
16278 || (dict = argvars[0].vval.v_dict) == NULL)
16279 EMSG(_(e_invarg));
16280 else
16281 {
16282 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
16283 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
16284#ifdef FEAT_VIRTUALEDIT
16285 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
16286#endif
16287 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016288 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016289
Bram Moolenaar6f11a412006-09-06 20:16:42 +000016290 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016291#ifdef FEAT_DIFF
16292 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
16293#endif
16294 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
16295 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
16296
16297 check_cursor();
16298 changed_cline_bef_curs();
16299 invalidate_botline();
16300 redraw_later(VALID);
16301
16302 if (curwin->w_topline == 0)
16303 curwin->w_topline = 1;
16304 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
16305 curwin->w_topline = curbuf->b_ml.ml_line_count;
16306#ifdef FEAT_DIFF
16307 check_topfill(curwin, TRUE);
16308#endif
16309 }
16310}
16311
16312/*
16313 * "winsaveview()" function
16314 */
16315/* ARGSUSED */
16316 static void
16317f_winsaveview(argvars, rettv)
16318 typval_T *argvars;
16319 typval_T *rettv;
16320{
16321 dict_T *dict;
16322
16323 dict = dict_alloc();
16324 if (dict == NULL)
16325 return;
16326 rettv->v_type = VAR_DICT;
16327 rettv->vval.v_dict = dict;
16328 ++dict->dv_refcount;
16329
16330 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
16331 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
16332#ifdef FEAT_VIRTUALEDIT
16333 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
16334#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000016335 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016336 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
16337
16338 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
16339#ifdef FEAT_DIFF
16340 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
16341#endif
16342 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
16343 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
16344}
16345
16346/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016347 * "winwidth(nr)" function
16348 */
16349 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016350f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016351 typval_T *argvars;
16352 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016353{
16354 win_T *wp;
16355
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016356 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016357 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016358 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016359 else
16360#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016361 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016362#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016363 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016364#endif
16365}
16366
Bram Moolenaar071d4272004-06-13 20:20:40 +000016367/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016368 * "writefile()" function
16369 */
16370 static void
16371f_writefile(argvars, rettv)
16372 typval_T *argvars;
16373 typval_T *rettv;
16374{
16375 int binary = FALSE;
16376 char_u *fname;
16377 FILE *fd;
16378 listitem_T *li;
16379 char_u *s;
16380 int ret = 0;
16381 int c;
16382
16383 if (argvars[0].v_type != VAR_LIST)
16384 {
16385 EMSG2(_(e_listarg), "writefile()");
16386 return;
16387 }
16388 if (argvars[0].vval.v_list == NULL)
16389 return;
16390
16391 if (argvars[2].v_type != VAR_UNKNOWN
16392 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
16393 binary = TRUE;
16394
16395 /* Always open the file in binary mode, library functions have a mind of
16396 * their own about CR-LF conversion. */
16397 fname = get_tv_string(&argvars[1]);
16398 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
16399 {
16400 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
16401 ret = -1;
16402 }
16403 else
16404 {
16405 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
16406 li = li->li_next)
16407 {
16408 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
16409 {
16410 if (*s == '\n')
16411 c = putc(NUL, fd);
16412 else
16413 c = putc(*s, fd);
16414 if (c == EOF)
16415 {
16416 ret = -1;
16417 break;
16418 }
16419 }
16420 if (!binary || li->li_next != NULL)
16421 if (putc('\n', fd) == EOF)
16422 {
16423 ret = -1;
16424 break;
16425 }
16426 if (ret < 0)
16427 {
16428 EMSG(_(e_write));
16429 break;
16430 }
16431 }
16432 fclose(fd);
16433 }
16434
16435 rettv->vval.v_number = ret;
16436}
16437
16438/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016439 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016440 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016441 */
16442 static pos_T *
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016443var2fpos(varp, lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000016444 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016445 int lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016446 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016447{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016448 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016449 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016450 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016451
Bram Moolenaara5525202006-03-02 22:52:09 +000016452 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016453 if (varp->v_type == VAR_LIST)
16454 {
16455 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016456 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000016457 int error = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016458
16459 l = varp->vval.v_list;
16460 if (l == NULL)
16461 return NULL;
16462
16463 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016464 pos.lnum = list_find_nr(l, 0L, &error);
16465 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016466 return NULL; /* invalid line number */
16467
16468 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016469 pos.col = list_find_nr(l, 1L, &error);
16470 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016471 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016472 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaara5525202006-03-02 22:52:09 +000016473 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000016474 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016475 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016476 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016477
Bram Moolenaara5525202006-03-02 22:52:09 +000016478#ifdef FEAT_VIRTUALEDIT
16479 /* Get the virtual offset. Defaults to zero. */
16480 pos.coladd = list_find_nr(l, 2L, &error);
16481 if (error)
16482 pos.coladd = 0;
16483#endif
16484
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016485 return &pos;
16486 }
16487
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016488 name = get_tv_string_chk(varp);
16489 if (name == NULL)
16490 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016491 if (name[0] == '.') /* cursor */
16492 return &curwin->w_cursor;
16493 if (name[0] == '\'') /* mark */
16494 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016495 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016496 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
16497 return NULL;
16498 return pp;
16499 }
Bram Moolenaara5525202006-03-02 22:52:09 +000016500
16501#ifdef FEAT_VIRTUALEDIT
16502 pos.coladd = 0;
16503#endif
16504
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016505 if (name[0] == 'w' && lnum)
16506 {
16507 pos.col = 0;
16508 if (name[1] == '0') /* "w0": first visible line */
16509 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016510 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016511 pos.lnum = curwin->w_topline;
16512 return &pos;
16513 }
16514 else if (name[1] == '$') /* "w$": last visible line */
16515 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016516 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016517 pos.lnum = curwin->w_botline - 1;
16518 return &pos;
16519 }
16520 }
16521 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016522 {
16523 if (lnum)
16524 {
16525 pos.lnum = curbuf->b_ml.ml_line_count;
16526 pos.col = 0;
16527 }
16528 else
16529 {
16530 pos.lnum = curwin->w_cursor.lnum;
16531 pos.col = (colnr_T)STRLEN(ml_get_curline());
16532 }
16533 return &pos;
16534 }
16535 return NULL;
16536}
16537
16538/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016539 * Convert list in "arg" into a position and optional file number.
16540 * When "fnump" is NULL there is no file number, only 3 items.
16541 * Note that the column is passed on as-is, the caller may want to decrement
16542 * it to use 1 for the first column.
16543 * Return FAIL when conversion is not possible, doesn't check the position for
16544 * validity.
16545 */
16546 static int
16547list2fpos(arg, posp, fnump)
16548 typval_T *arg;
16549 pos_T *posp;
16550 int *fnump;
16551{
16552 list_T *l = arg->vval.v_list;
16553 long i = 0;
16554 long n;
16555
Bram Moolenaarbde35262006-07-23 20:12:24 +000016556 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
16557 * when "fnump" isn't NULL and "coladd" is optional. */
16558 if (arg->v_type != VAR_LIST
16559 || l == NULL
16560 || l->lv_len < (fnump == NULL ? 2 : 3)
16561 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016562 return FAIL;
16563
16564 if (fnump != NULL)
16565 {
16566 n = list_find_nr(l, i++, NULL); /* fnum */
16567 if (n < 0)
16568 return FAIL;
16569 if (n == 0)
16570 n = curbuf->b_fnum; /* current buffer */
16571 *fnump = n;
16572 }
16573
16574 n = list_find_nr(l, i++, NULL); /* lnum */
16575 if (n < 0)
16576 return FAIL;
16577 posp->lnum = n;
16578
16579 n = list_find_nr(l, i++, NULL); /* col */
16580 if (n < 0)
16581 return FAIL;
16582 posp->col = n;
16583
16584#ifdef FEAT_VIRTUALEDIT
16585 n = list_find_nr(l, i, NULL);
16586 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000016587 posp->coladd = 0;
16588 else
16589 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016590#endif
16591
16592 return OK;
16593}
16594
16595/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016596 * Get the length of an environment variable name.
16597 * Advance "arg" to the first character after the name.
16598 * Return 0 for error.
16599 */
16600 static int
16601get_env_len(arg)
16602 char_u **arg;
16603{
16604 char_u *p;
16605 int len;
16606
16607 for (p = *arg; vim_isIDc(*p); ++p)
16608 ;
16609 if (p == *arg) /* no name found */
16610 return 0;
16611
16612 len = (int)(p - *arg);
16613 *arg = p;
16614 return len;
16615}
16616
16617/*
16618 * Get the length of the name of a function or internal variable.
16619 * "arg" is advanced to the first non-white character after the name.
16620 * Return 0 if something is wrong.
16621 */
16622 static int
16623get_id_len(arg)
16624 char_u **arg;
16625{
16626 char_u *p;
16627 int len;
16628
16629 /* Find the end of the name. */
16630 for (p = *arg; eval_isnamec(*p); ++p)
16631 ;
16632 if (p == *arg) /* no name found */
16633 return 0;
16634
16635 len = (int)(p - *arg);
16636 *arg = skipwhite(p);
16637
16638 return len;
16639}
16640
16641/*
Bram Moolenaara7043832005-01-21 11:56:39 +000016642 * Get the length of the name of a variable or function.
16643 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000016644 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016645 * Return -1 if curly braces expansion failed.
16646 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016647 * If the name contains 'magic' {}'s, expand them and return the
16648 * expanded name in an allocated string via 'alias' - caller must free.
16649 */
16650 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016651get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016652 char_u **arg;
16653 char_u **alias;
16654 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016655 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016656{
16657 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016658 char_u *p;
16659 char_u *expr_start;
16660 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016661
16662 *alias = NULL; /* default to no alias */
16663
16664 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
16665 && (*arg)[2] == (int)KE_SNR)
16666 {
16667 /* hard coded <SNR>, already translated */
16668 *arg += 3;
16669 return get_id_len(arg) + 3;
16670 }
16671 len = eval_fname_script(*arg);
16672 if (len > 0)
16673 {
16674 /* literal "<SID>", "s:" or "<SNR>" */
16675 *arg += len;
16676 }
16677
Bram Moolenaar071d4272004-06-13 20:20:40 +000016678 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016679 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016680 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016681 p = find_name_end(*arg, &expr_start, &expr_end,
16682 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016683 if (expr_start != NULL)
16684 {
16685 char_u *temp_string;
16686
16687 if (!evaluate)
16688 {
16689 len += (int)(p - *arg);
16690 *arg = skipwhite(p);
16691 return len;
16692 }
16693
16694 /*
16695 * Include any <SID> etc in the expanded string:
16696 * Thus the -len here.
16697 */
16698 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
16699 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016700 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016701 *alias = temp_string;
16702 *arg = skipwhite(p);
16703 return (int)STRLEN(temp_string);
16704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016705
16706 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016707 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016708 EMSG2(_(e_invexpr2), *arg);
16709
16710 return len;
16711}
16712
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016713/*
16714 * Find the end of a variable or function name, taking care of magic braces.
16715 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
16716 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016717 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016718 * Return a pointer to just after the name. Equal to "arg" if there is no
16719 * valid name.
16720 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016721 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016722find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016723 char_u *arg;
16724 char_u **expr_start;
16725 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016726 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016727{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016728 int mb_nest = 0;
16729 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016730 char_u *p;
16731
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016732 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016733 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016734 *expr_start = NULL;
16735 *expr_end = NULL;
16736 }
16737
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016738 /* Quick check for valid starting character. */
16739 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
16740 return arg;
16741
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016742 for (p = arg; *p != NUL
16743 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016744 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016745 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016746 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000016747 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016748 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000016749 if (*p == '\'')
16750 {
16751 /* skip over 'string' to avoid counting [ and ] inside it. */
16752 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
16753 ;
16754 if (*p == NUL)
16755 break;
16756 }
16757 else if (*p == '"')
16758 {
16759 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
16760 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
16761 if (*p == '\\' && p[1] != NUL)
16762 ++p;
16763 if (*p == NUL)
16764 break;
16765 }
16766
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016767 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016768 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016769 if (*p == '[')
16770 ++br_nest;
16771 else if (*p == ']')
16772 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016773 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000016774
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016775 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016776 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016777 if (*p == '{')
16778 {
16779 mb_nest++;
16780 if (expr_start != NULL && *expr_start == NULL)
16781 *expr_start = p;
16782 }
16783 else if (*p == '}')
16784 {
16785 mb_nest--;
16786 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
16787 *expr_end = p;
16788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016790 }
16791
16792 return p;
16793}
16794
16795/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016796 * Expands out the 'magic' {}'s in a variable/function name.
16797 * Note that this can call itself recursively, to deal with
16798 * constructs like foo{bar}{baz}{bam}
16799 * The four pointer arguments point to "foo{expre}ss{ion}bar"
16800 * "in_start" ^
16801 * "expr_start" ^
16802 * "expr_end" ^
16803 * "in_end" ^
16804 *
16805 * Returns a new allocated string, which the caller must free.
16806 * Returns NULL for failure.
16807 */
16808 static char_u *
16809make_expanded_name(in_start, expr_start, expr_end, in_end)
16810 char_u *in_start;
16811 char_u *expr_start;
16812 char_u *expr_end;
16813 char_u *in_end;
16814{
16815 char_u c1;
16816 char_u *retval = NULL;
16817 char_u *temp_result;
16818 char_u *nextcmd = NULL;
16819
16820 if (expr_end == NULL || in_end == NULL)
16821 return NULL;
16822 *expr_start = NUL;
16823 *expr_end = NUL;
16824 c1 = *in_end;
16825 *in_end = NUL;
16826
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016827 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016828 if (temp_result != NULL && nextcmd == NULL)
16829 {
16830 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
16831 + (in_end - expr_end) + 1));
16832 if (retval != NULL)
16833 {
16834 STRCPY(retval, in_start);
16835 STRCAT(retval, temp_result);
16836 STRCAT(retval, expr_end + 1);
16837 }
16838 }
16839 vim_free(temp_result);
16840
16841 *in_end = c1; /* put char back for error messages */
16842 *expr_start = '{';
16843 *expr_end = '}';
16844
16845 if (retval != NULL)
16846 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016847 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016848 if (expr_start != NULL)
16849 {
16850 /* Further expansion! */
16851 temp_result = make_expanded_name(retval, expr_start,
16852 expr_end, temp_result);
16853 vim_free(retval);
16854 retval = temp_result;
16855 }
16856 }
16857
16858 return retval;
16859}
16860
16861/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016862 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016863 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016864 */
16865 static int
16866eval_isnamec(c)
16867 int c;
16868{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016869 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
16870}
16871
16872/*
16873 * Return TRUE if character "c" can be used as the first character in a
16874 * variable or function name (excluding '{' and '}').
16875 */
16876 static int
16877eval_isnamec1(c)
16878 int c;
16879{
16880 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000016881}
16882
16883/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016884 * Set number v: variable to "val".
16885 */
16886 void
16887set_vim_var_nr(idx, val)
16888 int idx;
16889 long val;
16890{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016891 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016892}
16893
16894/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016895 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016896 */
16897 long
16898get_vim_var_nr(idx)
16899 int idx;
16900{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016901 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016902}
16903
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016904#if defined(FEAT_AUTOCMD) || defined(PROTO)
16905/*
16906 * Get string v: variable value. Uses a static buffer, can only be used once.
16907 */
16908 char_u *
16909get_vim_var_str(idx)
16910 int idx;
16911{
16912 return get_tv_string(&vimvars[idx].vv_tv);
16913}
16914#endif
16915
Bram Moolenaar071d4272004-06-13 20:20:40 +000016916/*
16917 * Set v:count, v:count1 and v:prevcount.
16918 */
16919 void
16920set_vcount(count, count1)
16921 long count;
16922 long count1;
16923{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016924 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
16925 vimvars[VV_COUNT].vv_nr = count;
16926 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016927}
16928
16929/*
16930 * Set string v: variable to a copy of "val".
16931 */
16932 void
16933set_vim_var_string(idx, val, len)
16934 int idx;
16935 char_u *val;
16936 int len; /* length of "val" to use or -1 (whole string) */
16937{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016938 /* Need to do this (at least) once, since we can't initialize a union.
16939 * Will always be invoked when "v:progname" is set. */
16940 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
16941
Bram Moolenaare9a41262005-01-15 22:18:47 +000016942 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016943 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016944 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016945 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016946 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016947 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000016948 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016949}
16950
16951/*
16952 * Set v:register if needed.
16953 */
16954 void
16955set_reg_var(c)
16956 int c;
16957{
16958 char_u regname;
16959
16960 if (c == 0 || c == ' ')
16961 regname = '"';
16962 else
16963 regname = c;
16964 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000016965 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016966 set_vim_var_string(VV_REG, &regname, 1);
16967}
16968
16969/*
16970 * Get or set v:exception. If "oldval" == NULL, return the current value.
16971 * Otherwise, restore the value to "oldval" and return NULL.
16972 * Must always be called in pairs to save and restore v:exception! Does not
16973 * take care of memory allocations.
16974 */
16975 char_u *
16976v_exception(oldval)
16977 char_u *oldval;
16978{
16979 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016980 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016981
Bram Moolenaare9a41262005-01-15 22:18:47 +000016982 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016983 return NULL;
16984}
16985
16986/*
16987 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
16988 * Otherwise, restore the value to "oldval" and return NULL.
16989 * Must always be called in pairs to save and restore v:throwpoint! Does not
16990 * take care of memory allocations.
16991 */
16992 char_u *
16993v_throwpoint(oldval)
16994 char_u *oldval;
16995{
16996 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016997 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016998
Bram Moolenaare9a41262005-01-15 22:18:47 +000016999 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017000 return NULL;
17001}
17002
17003#if defined(FEAT_AUTOCMD) || defined(PROTO)
17004/*
17005 * Set v:cmdarg.
17006 * If "eap" != NULL, use "eap" to generate the value and return the old value.
17007 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
17008 * Must always be called in pairs!
17009 */
17010 char_u *
17011set_cmdarg(eap, oldarg)
17012 exarg_T *eap;
17013 char_u *oldarg;
17014{
17015 char_u *oldval;
17016 char_u *newval;
17017 unsigned len;
17018
Bram Moolenaare9a41262005-01-15 22:18:47 +000017019 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017020 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017021 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017022 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000017023 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017024 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017025 }
17026
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017027 if (eap->force_bin == FORCE_BIN)
17028 len = 6;
17029 else if (eap->force_bin == FORCE_NOBIN)
17030 len = 8;
17031 else
17032 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017033
17034 if (eap->read_edit)
17035 len += 7;
17036
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017037 if (eap->force_ff != 0)
17038 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
17039# ifdef FEAT_MBYTE
17040 if (eap->force_enc != 0)
17041 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000017042 if (eap->bad_char != 0)
17043 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017044# endif
17045
17046 newval = alloc(len + 1);
17047 if (newval == NULL)
17048 return NULL;
17049
17050 if (eap->force_bin == FORCE_BIN)
17051 sprintf((char *)newval, " ++bin");
17052 else if (eap->force_bin == FORCE_NOBIN)
17053 sprintf((char *)newval, " ++nobin");
17054 else
17055 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017056
17057 if (eap->read_edit)
17058 STRCAT(newval, " ++edit");
17059
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017060 if (eap->force_ff != 0)
17061 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
17062 eap->cmd + eap->force_ff);
17063# ifdef FEAT_MBYTE
17064 if (eap->force_enc != 0)
17065 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
17066 eap->cmd + eap->force_enc);
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000017067 if (eap->bad_char != 0)
17068 sprintf((char *)newval + STRLEN(newval), " ++bad=%s",
17069 eap->cmd + eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017070# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000017071 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017072 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017073}
17074#endif
17075
17076/*
17077 * Get the value of internal variable "name".
17078 * Return OK or FAIL.
17079 */
17080 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017081get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017082 char_u *name;
17083 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000017084 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017085 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017086{
17087 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000017088 typval_T *tv = NULL;
17089 typval_T atv;
17090 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017091 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017092
17093 /* truncate the name, so that we can use strcmp() */
17094 cc = name[len];
17095 name[len] = NUL;
17096
17097 /*
17098 * Check for "b:changedtick".
17099 */
17100 if (STRCMP(name, "b:changedtick") == 0)
17101 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000017102 atv.v_type = VAR_NUMBER;
17103 atv.vval.v_number = curbuf->b_changedtick;
17104 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017105 }
17106
17107 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017108 * Check for user-defined variables.
17109 */
17110 else
17111 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017112 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017113 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017114 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017115 }
17116
Bram Moolenaare9a41262005-01-15 22:18:47 +000017117 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017118 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017119 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017120 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017121 ret = FAIL;
17122 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017123 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017124 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017125
17126 name[len] = cc;
17127
17128 return ret;
17129}
17130
17131/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017132 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
17133 * Also handle function call with Funcref variable: func(expr)
17134 * Can all be combined: dict.func(expr)[idx]['func'](expr)
17135 */
17136 static int
17137handle_subscript(arg, rettv, evaluate, verbose)
17138 char_u **arg;
17139 typval_T *rettv;
17140 int evaluate; /* do more than finding the end */
17141 int verbose; /* give error messages */
17142{
17143 int ret = OK;
17144 dict_T *selfdict = NULL;
17145 char_u *s;
17146 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000017147 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017148
17149 while (ret == OK
17150 && (**arg == '['
17151 || (**arg == '.' && rettv->v_type == VAR_DICT)
17152 || (**arg == '(' && rettv->v_type == VAR_FUNC))
17153 && !vim_iswhite(*(*arg - 1)))
17154 {
17155 if (**arg == '(')
17156 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000017157 /* need to copy the funcref so that we can clear rettv */
17158 functv = *rettv;
17159 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017160
17161 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000017162 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017163 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000017164 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
17165 &len, evaluate, selfdict);
17166
17167 /* Clear the funcref afterwards, so that deleting it while
17168 * evaluating the arguments is possible (see test55). */
17169 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017170
17171 /* Stop the expression evaluation when immediately aborting on
17172 * error, or when an interrupt occurred or an exception was thrown
17173 * but not caught. */
17174 if (aborting())
17175 {
17176 if (ret == OK)
17177 clear_tv(rettv);
17178 ret = FAIL;
17179 }
17180 dict_unref(selfdict);
17181 selfdict = NULL;
17182 }
17183 else /* **arg == '[' || **arg == '.' */
17184 {
17185 dict_unref(selfdict);
17186 if (rettv->v_type == VAR_DICT)
17187 {
17188 selfdict = rettv->vval.v_dict;
17189 if (selfdict != NULL)
17190 ++selfdict->dv_refcount;
17191 }
17192 else
17193 selfdict = NULL;
17194 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
17195 {
17196 clear_tv(rettv);
17197 ret = FAIL;
17198 }
17199 }
17200 }
17201 dict_unref(selfdict);
17202 return ret;
17203}
17204
17205/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017206 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
17207 * value).
17208 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017209 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017210alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017211{
Bram Moolenaar33570922005-01-25 22:26:29 +000017212 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017213}
17214
17215/*
17216 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017217 * The string "s" must have been allocated, it is consumed.
17218 * Return NULL for out of memory, the variable otherwise.
17219 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017220 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017221alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017222 char_u *s;
17223{
Bram Moolenaar33570922005-01-25 22:26:29 +000017224 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017225
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017226 rettv = alloc_tv();
17227 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017228 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017229 rettv->v_type = VAR_STRING;
17230 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017231 }
17232 else
17233 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017234 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017235}
17236
17237/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017238 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017239 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000017240 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017241free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017242 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017243{
17244 if (varp != NULL)
17245 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017246 switch (varp->v_type)
17247 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017248 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017249 func_unref(varp->vval.v_string);
17250 /*FALLTHROUGH*/
17251 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017252 vim_free(varp->vval.v_string);
17253 break;
17254 case VAR_LIST:
17255 list_unref(varp->vval.v_list);
17256 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017257 case VAR_DICT:
17258 dict_unref(varp->vval.v_dict);
17259 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017260 case VAR_NUMBER:
17261 case VAR_UNKNOWN:
17262 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017263 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000017264 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017265 break;
17266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017267 vim_free(varp);
17268 }
17269}
17270
17271/*
17272 * Free the memory for a variable value and set the value to NULL or 0.
17273 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017274 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017275clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017276 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017277{
17278 if (varp != NULL)
17279 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017280 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017281 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017282 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017283 func_unref(varp->vval.v_string);
17284 /*FALLTHROUGH*/
17285 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017286 vim_free(varp->vval.v_string);
17287 varp->vval.v_string = NULL;
17288 break;
17289 case VAR_LIST:
17290 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017291 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017292 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017293 case VAR_DICT:
17294 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017295 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017296 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017297 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017298 varp->vval.v_number = 0;
17299 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017300 case VAR_UNKNOWN:
17301 break;
17302 default:
17303 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017304 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017305 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017306 }
17307}
17308
17309/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017310 * Set the value of a variable to NULL without freeing items.
17311 */
17312 static void
17313init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017314 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017315{
17316 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017317 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017318}
17319
17320/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017321 * Get the number value of a variable.
17322 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017323 * For incompatible types, return 0.
17324 * get_tv_number_chk() is similar to get_tv_number(), but informs the
17325 * caller of incompatible types: it sets *denote to TRUE if "denote"
17326 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017327 */
17328 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017329get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017330 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017331{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017332 int error = FALSE;
17333
17334 return get_tv_number_chk(varp, &error); /* return 0L on error */
17335}
17336
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017337 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017338get_tv_number_chk(varp, denote)
17339 typval_T *varp;
17340 int *denote;
17341{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017342 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017343
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017344 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017345 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017346 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017347 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017348 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017349 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017350 break;
17351 case VAR_STRING:
17352 if (varp->vval.v_string != NULL)
17353 vim_str2nr(varp->vval.v_string, NULL, NULL,
17354 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017355 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017356 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000017357 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017358 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017359 case VAR_DICT:
17360 EMSG(_("E728: Using a Dictionary as a number"));
17361 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017362 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017363 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017364 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017365 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017366 if (denote == NULL) /* useful for values that must be unsigned */
17367 n = -1;
17368 else
17369 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017370 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017371}
17372
17373/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000017374 * Get the lnum from the first argument.
17375 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017376 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017377 */
17378 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017379get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000017380 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017381{
Bram Moolenaar33570922005-01-25 22:26:29 +000017382 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017383 linenr_T lnum;
17384
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017385 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017386 if (lnum == 0) /* no valid number, try using line() */
17387 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017388 rettv.v_type = VAR_NUMBER;
17389 f_line(argvars, &rettv);
17390 lnum = rettv.vval.v_number;
17391 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017392 }
17393 return lnum;
17394}
17395
17396/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000017397 * Get the lnum from the first argument.
17398 * Also accepts "$", then "buf" is used.
17399 * Returns 0 on error.
17400 */
17401 static linenr_T
17402get_tv_lnum_buf(argvars, buf)
17403 typval_T *argvars;
17404 buf_T *buf;
17405{
17406 if (argvars[0].v_type == VAR_STRING
17407 && argvars[0].vval.v_string != NULL
17408 && argvars[0].vval.v_string[0] == '$'
17409 && buf != NULL)
17410 return buf->b_ml.ml_line_count;
17411 return get_tv_number_chk(&argvars[0], NULL);
17412}
17413
17414/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017415 * Get the string value of a variable.
17416 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000017417 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
17418 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017419 * If the String variable has never been set, return an empty string.
17420 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017421 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
17422 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017423 */
17424 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017425get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017426 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017427{
17428 static char_u mybuf[NUMBUFLEN];
17429
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017430 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017431}
17432
17433 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017434get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000017435 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017436 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017437{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017438 char_u *res = get_tv_string_buf_chk(varp, buf);
17439
17440 return res != NULL ? res : (char_u *)"";
17441}
17442
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017443 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017444get_tv_string_chk(varp)
17445 typval_T *varp;
17446{
17447 static char_u mybuf[NUMBUFLEN];
17448
17449 return get_tv_string_buf_chk(varp, mybuf);
17450}
17451
17452 static char_u *
17453get_tv_string_buf_chk(varp, buf)
17454 typval_T *varp;
17455 char_u *buf;
17456{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017457 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017458 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017459 case VAR_NUMBER:
17460 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
17461 return buf;
17462 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017463 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017464 break;
17465 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017466 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000017467 break;
17468 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017469 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017470 break;
17471 case VAR_STRING:
17472 if (varp->vval.v_string != NULL)
17473 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017474 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017475 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017476 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017477 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017478 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017479 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017480}
17481
17482/*
17483 * Find variable "name" in the list of variables.
17484 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017485 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017486 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000017487 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017488 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017489 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017490find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017491 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017492 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017493{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017494 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017495 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017496
Bram Moolenaara7043832005-01-21 11:56:39 +000017497 ht = find_var_ht(name, &varname);
17498 if (htp != NULL)
17499 *htp = ht;
17500 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017501 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017502 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017503}
17504
17505/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017506 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000017507 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017508 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017509 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017510find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000017511 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000017512 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017513 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000017514{
Bram Moolenaar33570922005-01-25 22:26:29 +000017515 hashitem_T *hi;
17516
17517 if (*varname == NUL)
17518 {
17519 /* Must be something like "s:", otherwise "ht" would be NULL. */
17520 switch (varname[-2])
17521 {
17522 case 's': return &SCRIPT_SV(current_SID).sv_var;
17523 case 'g': return &globvars_var;
17524 case 'v': return &vimvars_var;
17525 case 'b': return &curbuf->b_bufvar;
17526 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017527#ifdef FEAT_WINDOWS
17528 case 't': return &curtab->tp_winvar;
17529#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017530 case 'l': return current_funccal == NULL
17531 ? NULL : &current_funccal->l_vars_var;
17532 case 'a': return current_funccal == NULL
17533 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000017534 }
17535 return NULL;
17536 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017537
17538 hi = hash_find(ht, varname);
17539 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017540 {
17541 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017542 * worked find the variable again. Don't auto-load a script if it was
17543 * loaded already, otherwise it would be loaded every time when
17544 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017545 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017546 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017547 hi = hash_find(ht, varname);
17548 if (HASHITEM_EMPTY(hi))
17549 return NULL;
17550 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017551 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017552}
17553
17554/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017555 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017556 * Set "varname" to the start of name without ':'.
17557 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017558 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017559find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017560 char_u *name;
17561 char_u **varname;
17562{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017563 hashitem_T *hi;
17564
Bram Moolenaar071d4272004-06-13 20:20:40 +000017565 if (name[1] != ':')
17566 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017567 /* The name must not start with a colon or #. */
17568 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017569 return NULL;
17570 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017571
17572 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017573 hi = hash_find(&compat_hashtab, name);
17574 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000017575 return &compat_hashtab;
17576
Bram Moolenaar071d4272004-06-13 20:20:40 +000017577 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017578 return &globvarht; /* global variable */
17579 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017580 }
17581 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017582 if (*name == 'g') /* global variable */
17583 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017584 /* There must be no ':' or '#' in the rest of the name, unless g: is used
17585 */
17586 if (vim_strchr(name + 2, ':') != NULL
17587 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017588 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017589 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017590 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017591 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017592 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017593#ifdef FEAT_WINDOWS
17594 if (*name == 't') /* tab page variable */
17595 return &curtab->tp_vars.dv_hashtab;
17596#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000017597 if (*name == 'v') /* v: variable */
17598 return &vimvarht;
17599 if (*name == 'a' && current_funccal != NULL) /* function argument */
17600 return &current_funccal->l_avars.dv_hashtab;
17601 if (*name == 'l' && current_funccal != NULL) /* local function variable */
17602 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017603 if (*name == 's' /* script variable */
17604 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
17605 return &SCRIPT_VARS(current_SID);
17606 return NULL;
17607}
17608
17609/*
17610 * Get the string value of a (global/local) variable.
17611 * Returns NULL when it doesn't exist.
17612 */
17613 char_u *
17614get_var_value(name)
17615 char_u *name;
17616{
Bram Moolenaar33570922005-01-25 22:26:29 +000017617 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017618
Bram Moolenaara7043832005-01-21 11:56:39 +000017619 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017620 if (v == NULL)
17621 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000017622 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017623}
17624
17625/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017626 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000017627 * sourcing this script and when executing functions defined in the script.
17628 */
17629 void
17630new_script_vars(id)
17631 scid_T id;
17632{
Bram Moolenaara7043832005-01-21 11:56:39 +000017633 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000017634 hashtab_T *ht;
17635 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000017636
Bram Moolenaar071d4272004-06-13 20:20:40 +000017637 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
17638 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017639 /* Re-allocating ga_data means that an ht_array pointing to
17640 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000017641 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000017642 for (i = 1; i <= ga_scripts.ga_len; ++i)
17643 {
17644 ht = &SCRIPT_VARS(i);
17645 if (ht->ht_mask == HT_INIT_SIZE - 1)
17646 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000017647 sv = &SCRIPT_SV(i);
17648 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000017649 }
17650
Bram Moolenaar071d4272004-06-13 20:20:40 +000017651 while (ga_scripts.ga_len < id)
17652 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017653 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
17654 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017655 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017656 }
17657 }
17658}
17659
17660/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017661 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
17662 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017663 */
17664 void
Bram Moolenaar33570922005-01-25 22:26:29 +000017665init_var_dict(dict, dict_var)
17666 dict_T *dict;
17667 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017668{
Bram Moolenaar33570922005-01-25 22:26:29 +000017669 hash_init(&dict->dv_hashtab);
17670 dict->dv_refcount = 99999;
17671 dict_var->di_tv.vval.v_dict = dict;
17672 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017673 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017674 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
17675 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017676}
17677
17678/*
17679 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000017680 * Frees all allocated variables and the value they contain.
17681 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017682 */
17683 void
Bram Moolenaara7043832005-01-21 11:56:39 +000017684vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000017685 hashtab_T *ht;
17686{
17687 vars_clear_ext(ht, TRUE);
17688}
17689
17690/*
17691 * Like vars_clear(), but only free the value if "free_val" is TRUE.
17692 */
17693 static void
17694vars_clear_ext(ht, free_val)
17695 hashtab_T *ht;
17696 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017697{
Bram Moolenaara7043832005-01-21 11:56:39 +000017698 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000017699 hashitem_T *hi;
17700 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017701
Bram Moolenaar33570922005-01-25 22:26:29 +000017702 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017703 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000017704 for (hi = ht->ht_array; todo > 0; ++hi)
17705 {
17706 if (!HASHITEM_EMPTY(hi))
17707 {
17708 --todo;
17709
Bram Moolenaar33570922005-01-25 22:26:29 +000017710 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000017711 * ht_array might change then. hash_clear() takes care of it
17712 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017713 v = HI2DI(hi);
17714 if (free_val)
17715 clear_tv(&v->di_tv);
17716 if ((v->di_flags & DI_FLAGS_FIX) == 0)
17717 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000017718 }
17719 }
17720 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017721 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017722}
17723
Bram Moolenaara7043832005-01-21 11:56:39 +000017724/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017725 * Delete a variable from hashtab "ht" at item "hi".
17726 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000017727 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017728 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000017729delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000017730 hashtab_T *ht;
17731 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017732{
Bram Moolenaar33570922005-01-25 22:26:29 +000017733 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017734
17735 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000017736 clear_tv(&di->di_tv);
17737 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017738}
17739
17740/*
17741 * List the value of one internal variable.
17742 */
17743 static void
17744list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000017745 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017746 char_u *prefix;
17747{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017748 char_u *tofree;
17749 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017750 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017751
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017752 s = echo_string(&v->di_tv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000017753 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017754 s == NULL ? (char_u *)"" : s);
17755 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017756}
17757
Bram Moolenaar071d4272004-06-13 20:20:40 +000017758 static void
17759list_one_var_a(prefix, name, type, string)
17760 char_u *prefix;
17761 char_u *name;
17762 int type;
17763 char_u *string;
17764{
17765 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
17766 if (name != NULL) /* "a:" vars don't have a name stored */
17767 msg_puts(name);
17768 msg_putchar(' ');
17769 msg_advance(22);
17770 if (type == VAR_NUMBER)
17771 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017772 else if (type == VAR_FUNC)
17773 msg_putchar('*');
17774 else if (type == VAR_LIST)
17775 {
17776 msg_putchar('[');
17777 if (*string == '[')
17778 ++string;
17779 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017780 else if (type == VAR_DICT)
17781 {
17782 msg_putchar('{');
17783 if (*string == '{')
17784 ++string;
17785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017786 else
17787 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017788
Bram Moolenaar071d4272004-06-13 20:20:40 +000017789 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017790
17791 if (type == VAR_FUNC)
17792 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017793}
17794
17795/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017796 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000017797 * If the variable already exists, the value is updated.
17798 * Otherwise the variable is created.
17799 */
17800 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017801set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017802 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017803 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017804 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017805{
Bram Moolenaar33570922005-01-25 22:26:29 +000017806 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017807 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017808 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017809 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017810
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017811 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017812 {
17813 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
17814 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
17815 ? name[2] : name[0]))
17816 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017817 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017818 return;
17819 }
17820 if (function_exists(name))
17821 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017822 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017823 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017824 return;
17825 }
17826 }
17827
Bram Moolenaara7043832005-01-21 11:56:39 +000017828 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000017829 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000017830 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000017831 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000017832 return;
17833 }
17834
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017835 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017836 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017837 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017838 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017839 if (var_check_ro(v->di_flags, name)
17840 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000017841 return;
17842 if (v->di_tv.v_type != tv->v_type
17843 && !((v->di_tv.v_type == VAR_STRING
17844 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017845 && (tv->v_type == VAR_STRING
17846 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017847 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017848 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017849 return;
17850 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017851
17852 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000017853 * Handle setting internal v: variables separately: we don't change
17854 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000017855 */
17856 if (ht == &vimvarht)
17857 {
17858 if (v->di_tv.v_type == VAR_STRING)
17859 {
17860 vim_free(v->di_tv.vval.v_string);
17861 if (copy || tv->v_type != VAR_STRING)
17862 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
17863 else
17864 {
17865 /* Take over the string to avoid an extra alloc/free. */
17866 v->di_tv.vval.v_string = tv->vval.v_string;
17867 tv->vval.v_string = NULL;
17868 }
17869 }
17870 else if (v->di_tv.v_type != VAR_NUMBER)
17871 EMSG2(_(e_intern2), "set_var()");
17872 else
17873 v->di_tv.vval.v_number = get_tv_number(tv);
17874 return;
17875 }
17876
17877 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017878 }
17879 else /* add a new variable */
17880 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000017881 /* Can't add "v:" variable. */
17882 if (ht == &vimvarht)
17883 {
17884 EMSG2(_(e_illvar), name);
17885 return;
17886 }
17887
Bram Moolenaar92124a32005-06-17 22:03:40 +000017888 /* Make sure the variable name is valid. */
17889 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000017890 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
17891 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000017892 {
17893 EMSG2(_(e_illvar), varname);
17894 return;
17895 }
17896
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017897 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
17898 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000017899 if (v == NULL)
17900 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000017901 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000017902 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017903 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017904 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017905 return;
17906 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017907 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017908 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017909
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017910 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000017911 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017912 else
17913 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017914 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017915 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017916 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017918}
17919
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017920/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000017921 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000017922 * Also give an error message.
17923 */
17924 static int
17925var_check_ro(flags, name)
17926 int flags;
17927 char_u *name;
17928{
17929 if (flags & DI_FLAGS_RO)
17930 {
17931 EMSG2(_(e_readonlyvar), name);
17932 return TRUE;
17933 }
17934 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
17935 {
17936 EMSG2(_(e_readonlysbx), name);
17937 return TRUE;
17938 }
17939 return FALSE;
17940}
17941
17942/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000017943 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
17944 * Also give an error message.
17945 */
17946 static int
17947var_check_fixed(flags, name)
17948 int flags;
17949 char_u *name;
17950{
17951 if (flags & DI_FLAGS_FIX)
17952 {
17953 EMSG2(_("E795: Cannot delete variable %s"), name);
17954 return TRUE;
17955 }
17956 return FALSE;
17957}
17958
17959/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017960 * Return TRUE if typeval "tv" is set to be locked (immutable).
17961 * Also give an error message, using "name".
17962 */
17963 static int
17964tv_check_lock(lock, name)
17965 int lock;
17966 char_u *name;
17967{
17968 if (lock & VAR_LOCKED)
17969 {
17970 EMSG2(_("E741: Value is locked: %s"),
17971 name == NULL ? (char_u *)_("Unknown") : name);
17972 return TRUE;
17973 }
17974 if (lock & VAR_FIXED)
17975 {
17976 EMSG2(_("E742: Cannot change value of %s"),
17977 name == NULL ? (char_u *)_("Unknown") : name);
17978 return TRUE;
17979 }
17980 return FALSE;
17981}
17982
17983/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017984 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017985 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017986 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017987 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017988 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017989copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000017990 typval_T *from;
17991 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017992{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017993 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017994 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017995 switch (from->v_type)
17996 {
17997 case VAR_NUMBER:
17998 to->vval.v_number = from->vval.v_number;
17999 break;
18000 case VAR_STRING:
18001 case VAR_FUNC:
18002 if (from->vval.v_string == NULL)
18003 to->vval.v_string = NULL;
18004 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018005 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018006 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018007 if (from->v_type == VAR_FUNC)
18008 func_ref(to->vval.v_string);
18009 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018010 break;
18011 case VAR_LIST:
18012 if (from->vval.v_list == NULL)
18013 to->vval.v_list = NULL;
18014 else
18015 {
18016 to->vval.v_list = from->vval.v_list;
18017 ++to->vval.v_list->lv_refcount;
18018 }
18019 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018020 case VAR_DICT:
18021 if (from->vval.v_dict == NULL)
18022 to->vval.v_dict = NULL;
18023 else
18024 {
18025 to->vval.v_dict = from->vval.v_dict;
18026 ++to->vval.v_dict->dv_refcount;
18027 }
18028 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018029 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018030 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018031 break;
18032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018033}
18034
18035/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000018036 * Make a copy of an item.
18037 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018038 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
18039 * reference to an already copied list/dict can be used.
18040 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018041 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018042 static int
18043item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000018044 typval_T *from;
18045 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018046 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018047 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018048{
18049 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018050 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018051
Bram Moolenaar33570922005-01-25 22:26:29 +000018052 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018053 {
18054 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018055 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018056 }
18057 ++recurse;
18058
18059 switch (from->v_type)
18060 {
18061 case VAR_NUMBER:
18062 case VAR_STRING:
18063 case VAR_FUNC:
18064 copy_tv(from, to);
18065 break;
18066 case VAR_LIST:
18067 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018068 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018069 if (from->vval.v_list == NULL)
18070 to->vval.v_list = NULL;
18071 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
18072 {
18073 /* use the copy made earlier */
18074 to->vval.v_list = from->vval.v_list->lv_copylist;
18075 ++to->vval.v_list->lv_refcount;
18076 }
18077 else
18078 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
18079 if (to->vval.v_list == NULL)
18080 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018081 break;
18082 case VAR_DICT:
18083 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018084 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018085 if (from->vval.v_dict == NULL)
18086 to->vval.v_dict = NULL;
18087 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
18088 {
18089 /* use the copy made earlier */
18090 to->vval.v_dict = from->vval.v_dict->dv_copydict;
18091 ++to->vval.v_dict->dv_refcount;
18092 }
18093 else
18094 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
18095 if (to->vval.v_dict == NULL)
18096 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018097 break;
18098 default:
18099 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018100 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018101 }
18102 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018103 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018104}
18105
18106/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018107 * ":echo expr1 ..." print each argument separated with a space, add a
18108 * newline at the end.
18109 * ":echon expr1 ..." print each argument plain.
18110 */
18111 void
18112ex_echo(eap)
18113 exarg_T *eap;
18114{
18115 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018116 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018117 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018118 char_u *p;
18119 int needclr = TRUE;
18120 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018121 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018122
18123 if (eap->skip)
18124 ++emsg_skip;
18125 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
18126 {
18127 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018128 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018129 {
18130 /*
18131 * Report the invalid expression unless the expression evaluation
18132 * has been cancelled due to an aborting error, an interrupt, or an
18133 * exception.
18134 */
18135 if (!aborting())
18136 EMSG2(_(e_invexpr2), p);
18137 break;
18138 }
18139 if (!eap->skip)
18140 {
18141 if (atstart)
18142 {
18143 atstart = FALSE;
18144 /* Call msg_start() after eval1(), evaluating the expression
18145 * may cause a message to appear. */
18146 if (eap->cmdidx == CMD_echo)
18147 msg_start();
18148 }
18149 else if (eap->cmdidx == CMD_echo)
18150 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018151 p = echo_string(&rettv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018152 if (p != NULL)
18153 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018154 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018155 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018156 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018157 if (*p != TAB && needclr)
18158 {
18159 /* remove any text still there from the command */
18160 msg_clr_eos();
18161 needclr = FALSE;
18162 }
18163 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018164 }
18165 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018166 {
18167#ifdef FEAT_MBYTE
18168 if (has_mbyte)
18169 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018170 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018171
18172 (void)msg_outtrans_len_attr(p, i, echo_attr);
18173 p += i - 1;
18174 }
18175 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000018176#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018177 (void)msg_outtrans_len_attr(p, 1, echo_attr);
18178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018179 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018180 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018181 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018182 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018183 arg = skipwhite(arg);
18184 }
18185 eap->nextcmd = check_nextcmd(arg);
18186
18187 if (eap->skip)
18188 --emsg_skip;
18189 else
18190 {
18191 /* remove text that may still be there from the command */
18192 if (needclr)
18193 msg_clr_eos();
18194 if (eap->cmdidx == CMD_echo)
18195 msg_end();
18196 }
18197}
18198
18199/*
18200 * ":echohl {name}".
18201 */
18202 void
18203ex_echohl(eap)
18204 exarg_T *eap;
18205{
18206 int id;
18207
18208 id = syn_name2id(eap->arg);
18209 if (id == 0)
18210 echo_attr = 0;
18211 else
18212 echo_attr = syn_id2attr(id);
18213}
18214
18215/*
18216 * ":execute expr1 ..." execute the result of an expression.
18217 * ":echomsg expr1 ..." Print a message
18218 * ":echoerr expr1 ..." Print an error
18219 * Each gets spaces around each argument and a newline at the end for
18220 * echo commands
18221 */
18222 void
18223ex_execute(eap)
18224 exarg_T *eap;
18225{
18226 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018227 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018228 int ret = OK;
18229 char_u *p;
18230 garray_T ga;
18231 int len;
18232 int save_did_emsg;
18233
18234 ga_init2(&ga, 1, 80);
18235
18236 if (eap->skip)
18237 ++emsg_skip;
18238 while (*arg != NUL && *arg != '|' && *arg != '\n')
18239 {
18240 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018241 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018242 {
18243 /*
18244 * Report the invalid expression unless the expression evaluation
18245 * has been cancelled due to an aborting error, an interrupt, or an
18246 * exception.
18247 */
18248 if (!aborting())
18249 EMSG2(_(e_invexpr2), p);
18250 ret = FAIL;
18251 break;
18252 }
18253
18254 if (!eap->skip)
18255 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018256 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018257 len = (int)STRLEN(p);
18258 if (ga_grow(&ga, len + 2) == FAIL)
18259 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018260 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018261 ret = FAIL;
18262 break;
18263 }
18264 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018265 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018266 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018267 ga.ga_len += len;
18268 }
18269
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018270 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018271 arg = skipwhite(arg);
18272 }
18273
18274 if (ret != FAIL && ga.ga_data != NULL)
18275 {
18276 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000018277 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018278 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000018279 out_flush();
18280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018281 else if (eap->cmdidx == CMD_echoerr)
18282 {
18283 /* We don't want to abort following commands, restore did_emsg. */
18284 save_did_emsg = did_emsg;
18285 EMSG((char_u *)ga.ga_data);
18286 if (!force_abort)
18287 did_emsg = save_did_emsg;
18288 }
18289 else if (eap->cmdidx == CMD_execute)
18290 do_cmdline((char_u *)ga.ga_data,
18291 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
18292 }
18293
18294 ga_clear(&ga);
18295
18296 if (eap->skip)
18297 --emsg_skip;
18298
18299 eap->nextcmd = check_nextcmd(arg);
18300}
18301
18302/*
18303 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
18304 * "arg" points to the "&" or '+' when called, to "option" when returning.
18305 * Returns NULL when no option name found. Otherwise pointer to the char
18306 * after the option name.
18307 */
18308 static char_u *
18309find_option_end(arg, opt_flags)
18310 char_u **arg;
18311 int *opt_flags;
18312{
18313 char_u *p = *arg;
18314
18315 ++p;
18316 if (*p == 'g' && p[1] == ':')
18317 {
18318 *opt_flags = OPT_GLOBAL;
18319 p += 2;
18320 }
18321 else if (*p == 'l' && p[1] == ':')
18322 {
18323 *opt_flags = OPT_LOCAL;
18324 p += 2;
18325 }
18326 else
18327 *opt_flags = 0;
18328
18329 if (!ASCII_ISALPHA(*p))
18330 return NULL;
18331 *arg = p;
18332
18333 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
18334 p += 4; /* termcap option */
18335 else
18336 while (ASCII_ISALPHA(*p))
18337 ++p;
18338 return p;
18339}
18340
18341/*
18342 * ":function"
18343 */
18344 void
18345ex_function(eap)
18346 exarg_T *eap;
18347{
18348 char_u *theline;
18349 int j;
18350 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018351 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018352 char_u *name = NULL;
18353 char_u *p;
18354 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018355 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018356 garray_T newargs;
18357 garray_T newlines;
18358 int varargs = FALSE;
18359 int mustend = FALSE;
18360 int flags = 0;
18361 ufunc_T *fp;
18362 int indent;
18363 int nesting;
18364 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000018365 dictitem_T *v;
18366 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018367 static int func_nr = 0; /* number for nameless function */
18368 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018369 hashtab_T *ht;
18370 int todo;
18371 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018372 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018373
18374 /*
18375 * ":function" without argument: list functions.
18376 */
18377 if (ends_excmd(*eap->arg))
18378 {
18379 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018380 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018381 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000018382 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018383 {
18384 if (!HASHITEM_EMPTY(hi))
18385 {
18386 --todo;
18387 fp = HI2UF(hi);
18388 if (!isdigit(*fp->uf_name))
18389 list_func_head(fp, FALSE);
18390 }
18391 }
18392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018393 eap->nextcmd = check_nextcmd(eap->arg);
18394 return;
18395 }
18396
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018397 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018398 * ":function /pat": list functions matching pattern.
18399 */
18400 if (*eap->arg == '/')
18401 {
18402 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
18403 if (!eap->skip)
18404 {
18405 regmatch_T regmatch;
18406
18407 c = *p;
18408 *p = NUL;
18409 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
18410 *p = c;
18411 if (regmatch.regprog != NULL)
18412 {
18413 regmatch.rm_ic = p_ic;
18414
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018415 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018416 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
18417 {
18418 if (!HASHITEM_EMPTY(hi))
18419 {
18420 --todo;
18421 fp = HI2UF(hi);
18422 if (!isdigit(*fp->uf_name)
18423 && vim_regexec(&regmatch, fp->uf_name, 0))
18424 list_func_head(fp, FALSE);
18425 }
18426 }
18427 }
18428 }
18429 if (*p == '/')
18430 ++p;
18431 eap->nextcmd = check_nextcmd(p);
18432 return;
18433 }
18434
18435 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018436 * Get the function name. There are these situations:
18437 * func normal function name
18438 * "name" == func, "fudi.fd_dict" == NULL
18439 * dict.func new dictionary entry
18440 * "name" == NULL, "fudi.fd_dict" set,
18441 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
18442 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018443 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018444 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18445 * dict.func existing dict entry that's not a Funcref
18446 * "name" == NULL, "fudi.fd_dict" set,
18447 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18448 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018449 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018450 name = trans_function_name(&p, eap->skip, 0, &fudi);
18451 paren = (vim_strchr(p, '(') != NULL);
18452 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018453 {
18454 /*
18455 * Return on an invalid expression in braces, unless the expression
18456 * evaluation has been cancelled due to an aborting error, an
18457 * interrupt, or an exception.
18458 */
18459 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018460 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018461 if (!eap->skip && fudi.fd_newkey != NULL)
18462 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018463 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018464 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018466 else
18467 eap->skip = TRUE;
18468 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000018469
Bram Moolenaar071d4272004-06-13 20:20:40 +000018470 /* An error in a function call during evaluation of an expression in magic
18471 * braces should not cause the function not to be defined. */
18472 saved_did_emsg = did_emsg;
18473 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018474
18475 /*
18476 * ":function func" with only function name: list function.
18477 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018478 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018479 {
18480 if (!ends_excmd(*skipwhite(p)))
18481 {
18482 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018483 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018484 }
18485 eap->nextcmd = check_nextcmd(p);
18486 if (eap->nextcmd != NULL)
18487 *p = NUL;
18488 if (!eap->skip && !got_int)
18489 {
18490 fp = find_func(name);
18491 if (fp != NULL)
18492 {
18493 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018494 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018495 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018496 if (FUNCLINE(fp, j) == NULL)
18497 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018498 msg_putchar('\n');
18499 msg_outnum((long)(j + 1));
18500 if (j < 9)
18501 msg_putchar(' ');
18502 if (j < 99)
18503 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018504 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018505 out_flush(); /* show a line at a time */
18506 ui_breakcheck();
18507 }
18508 if (!got_int)
18509 {
18510 msg_putchar('\n');
18511 msg_puts((char_u *)" endfunction");
18512 }
18513 }
18514 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018515 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018516 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018517 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018518 }
18519
18520 /*
18521 * ":function name(arg1, arg2)" Define function.
18522 */
18523 p = skipwhite(p);
18524 if (*p != '(')
18525 {
18526 if (!eap->skip)
18527 {
18528 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018529 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018530 }
18531 /* attempt to continue by skipping some text */
18532 if (vim_strchr(p, '(') != NULL)
18533 p = vim_strchr(p, '(');
18534 }
18535 p = skipwhite(p + 1);
18536
18537 ga_init2(&newargs, (int)sizeof(char_u *), 3);
18538 ga_init2(&newlines, (int)sizeof(char_u *), 3);
18539
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018540 if (!eap->skip)
18541 {
18542 /* Check the name of the function. */
18543 if (name != NULL)
18544 arg = name;
18545 else
18546 arg = fudi.fd_newkey;
18547 if (arg != NULL)
18548 {
18549 if (*arg == K_SPECIAL)
18550 j = 3;
18551 else
18552 j = 0;
18553 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
18554 : eval_isnamec(arg[j])))
18555 ++j;
18556 if (arg[j] != NUL)
18557 emsg_funcname(_(e_invarg2), arg);
18558 }
18559 }
18560
Bram Moolenaar071d4272004-06-13 20:20:40 +000018561 /*
18562 * Isolate the arguments: "arg1, arg2, ...)"
18563 */
18564 while (*p != ')')
18565 {
18566 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
18567 {
18568 varargs = TRUE;
18569 p += 3;
18570 mustend = TRUE;
18571 }
18572 else
18573 {
18574 arg = p;
18575 while (ASCII_ISALNUM(*p) || *p == '_')
18576 ++p;
18577 if (arg == p || isdigit(*arg)
18578 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
18579 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
18580 {
18581 if (!eap->skip)
18582 EMSG2(_("E125: Illegal argument: %s"), arg);
18583 break;
18584 }
18585 if (ga_grow(&newargs, 1) == FAIL)
18586 goto erret;
18587 c = *p;
18588 *p = NUL;
18589 arg = vim_strsave(arg);
18590 if (arg == NULL)
18591 goto erret;
18592 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
18593 *p = c;
18594 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018595 if (*p == ',')
18596 ++p;
18597 else
18598 mustend = TRUE;
18599 }
18600 p = skipwhite(p);
18601 if (mustend && *p != ')')
18602 {
18603 if (!eap->skip)
18604 EMSG2(_(e_invarg2), eap->arg);
18605 break;
18606 }
18607 }
18608 ++p; /* skip the ')' */
18609
Bram Moolenaare9a41262005-01-15 22:18:47 +000018610 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018611 for (;;)
18612 {
18613 p = skipwhite(p);
18614 if (STRNCMP(p, "range", 5) == 0)
18615 {
18616 flags |= FC_RANGE;
18617 p += 5;
18618 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000018619 else if (STRNCMP(p, "dict", 4) == 0)
18620 {
18621 flags |= FC_DICT;
18622 p += 4;
18623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018624 else if (STRNCMP(p, "abort", 5) == 0)
18625 {
18626 flags |= FC_ABORT;
18627 p += 5;
18628 }
18629 else
18630 break;
18631 }
18632
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018633 /* When there is a line break use what follows for the function body.
18634 * Makes 'exe "func Test()\n...\nendfunc"' work. */
18635 if (*p == '\n')
18636 line_arg = p + 1;
18637 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018638 EMSG(_(e_trailing));
18639
18640 /*
18641 * Read the body of the function, until ":endfunction" is found.
18642 */
18643 if (KeyTyped)
18644 {
18645 /* Check if the function already exists, don't let the user type the
18646 * whole function before telling him it doesn't work! For a script we
18647 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018648 if (!eap->skip && !eap->forceit)
18649 {
18650 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
18651 EMSG(_(e_funcdict));
18652 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018653 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018655
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018656 if (!eap->skip && did_emsg)
18657 goto erret;
18658
Bram Moolenaar071d4272004-06-13 20:20:40 +000018659 msg_putchar('\n'); /* don't overwrite the function name */
18660 cmdline_row = msg_row;
18661 }
18662
18663 indent = 2;
18664 nesting = 0;
18665 for (;;)
18666 {
18667 msg_scroll = TRUE;
18668 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018669 sourcing_lnum_off = sourcing_lnum;
18670
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018671 if (line_arg != NULL)
18672 {
18673 /* Use eap->arg, split up in parts by line breaks. */
18674 theline = line_arg;
18675 p = vim_strchr(theline, '\n');
18676 if (p == NULL)
18677 line_arg += STRLEN(line_arg);
18678 else
18679 {
18680 *p = NUL;
18681 line_arg = p + 1;
18682 }
18683 }
18684 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018685 theline = getcmdline(':', 0L, indent);
18686 else
18687 theline = eap->getline(':', eap->cookie, indent);
18688 if (KeyTyped)
18689 lines_left = Rows - 1;
18690 if (theline == NULL)
18691 {
18692 EMSG(_("E126: Missing :endfunction"));
18693 goto erret;
18694 }
18695
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018696 /* Detect line continuation: sourcing_lnum increased more than one. */
18697 if (sourcing_lnum > sourcing_lnum_off + 1)
18698 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
18699 else
18700 sourcing_lnum_off = 0;
18701
Bram Moolenaar071d4272004-06-13 20:20:40 +000018702 if (skip_until != NULL)
18703 {
18704 /* between ":append" and "." and between ":python <<EOF" and "EOF"
18705 * don't check for ":endfunc". */
18706 if (STRCMP(theline, skip_until) == 0)
18707 {
18708 vim_free(skip_until);
18709 skip_until = NULL;
18710 }
18711 }
18712 else
18713 {
18714 /* skip ':' and blanks*/
18715 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
18716 ;
18717
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018718 /* Check for "endfunction". */
18719 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018720 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018721 if (line_arg == NULL)
18722 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018723 break;
18724 }
18725
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018726 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000018727 * at "end". */
18728 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
18729 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018730 else if (STRNCMP(p, "if", 2) == 0
18731 || STRNCMP(p, "wh", 2) == 0
18732 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000018733 || STRNCMP(p, "try", 3) == 0)
18734 indent += 2;
18735
18736 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018737 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018738 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018739 if (*p == '!')
18740 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018741 p += eval_fname_script(p);
18742 if (ASCII_ISALPHA(*p))
18743 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018744 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018745 if (*skipwhite(p) == '(')
18746 {
18747 ++nesting;
18748 indent += 2;
18749 }
18750 }
18751 }
18752
18753 /* Check for ":append" or ":insert". */
18754 p = skip_range(p, NULL);
18755 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
18756 || (p[0] == 'i'
18757 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
18758 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
18759 skip_until = vim_strsave((char_u *)".");
18760
18761 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
18762 arg = skipwhite(skiptowhite(p));
18763 if (arg[0] == '<' && arg[1] =='<'
18764 && ((p[0] == 'p' && p[1] == 'y'
18765 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
18766 || (p[0] == 'p' && p[1] == 'e'
18767 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
18768 || (p[0] == 't' && p[1] == 'c'
18769 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
18770 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
18771 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000018772 || (p[0] == 'm' && p[1] == 'z'
18773 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018774 ))
18775 {
18776 /* ":python <<" continues until a dot, like ":append" */
18777 p = skipwhite(arg + 2);
18778 if (*p == NUL)
18779 skip_until = vim_strsave((char_u *)".");
18780 else
18781 skip_until = vim_strsave(p);
18782 }
18783 }
18784
18785 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018786 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018787 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018788 if (line_arg == NULL)
18789 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018790 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018791 }
18792
18793 /* Copy the line to newly allocated memory. get_one_sourceline()
18794 * allocates 250 bytes per line, this saves 80% on average. The cost
18795 * is an extra alloc/free. */
18796 p = vim_strsave(theline);
18797 if (p != NULL)
18798 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018799 if (line_arg == NULL)
18800 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018801 theline = p;
18802 }
18803
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018804 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
18805
18806 /* Add NULL lines for continuation lines, so that the line count is
18807 * equal to the index in the growarray. */
18808 while (sourcing_lnum_off-- > 0)
18809 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018810
18811 /* Check for end of eap->arg. */
18812 if (line_arg != NULL && *line_arg == NUL)
18813 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018814 }
18815
18816 /* Don't define the function when skipping commands or when an error was
18817 * detected. */
18818 if (eap->skip || did_emsg)
18819 goto erret;
18820
18821 /*
18822 * If there are no errors, add the function
18823 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018824 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018825 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018826 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000018827 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018828 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018829 emsg_funcname("E707: Function name conflicts with variable: %s",
18830 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018831 goto erret;
18832 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018833
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018834 fp = find_func(name);
18835 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018836 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018837 if (!eap->forceit)
18838 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018839 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018840 goto erret;
18841 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018842 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018843 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018844 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018845 name);
18846 goto erret;
18847 }
18848 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018849 ga_clear_strings(&(fp->uf_args));
18850 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018851 vim_free(name);
18852 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018854 }
18855 else
18856 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018857 char numbuf[20];
18858
18859 fp = NULL;
18860 if (fudi.fd_newkey == NULL && !eap->forceit)
18861 {
18862 EMSG(_(e_funcdict));
18863 goto erret;
18864 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000018865 if (fudi.fd_di == NULL)
18866 {
18867 /* Can't add a function to a locked dictionary */
18868 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
18869 goto erret;
18870 }
18871 /* Can't change an existing function if it is locked */
18872 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
18873 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018874
18875 /* Give the function a sequential number. Can only be used with a
18876 * Funcref! */
18877 vim_free(name);
18878 sprintf(numbuf, "%d", ++func_nr);
18879 name = vim_strsave((char_u *)numbuf);
18880 if (name == NULL)
18881 goto erret;
18882 }
18883
18884 if (fp == NULL)
18885 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018886 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018887 {
18888 int slen, plen;
18889 char_u *scriptname;
18890
18891 /* Check that the autoload name matches the script name. */
18892 j = FAIL;
18893 if (sourcing_name != NULL)
18894 {
18895 scriptname = autoload_name(name);
18896 if (scriptname != NULL)
18897 {
18898 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018899 plen = (int)STRLEN(p);
18900 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018901 if (slen > plen && fnamecmp(p,
18902 sourcing_name + slen - plen) == 0)
18903 j = OK;
18904 vim_free(scriptname);
18905 }
18906 }
18907 if (j == FAIL)
18908 {
18909 EMSG2(_("E746: Function name does not match script file name: %s"), name);
18910 goto erret;
18911 }
18912 }
18913
18914 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018915 if (fp == NULL)
18916 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018917
18918 if (fudi.fd_dict != NULL)
18919 {
18920 if (fudi.fd_di == NULL)
18921 {
18922 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018923 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018924 if (fudi.fd_di == NULL)
18925 {
18926 vim_free(fp);
18927 goto erret;
18928 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018929 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
18930 {
18931 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000018932 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018933 goto erret;
18934 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018935 }
18936 else
18937 /* overwrite existing dict entry */
18938 clear_tv(&fudi.fd_di->di_tv);
18939 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018940 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018941 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018942 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018943
18944 /* behave like "dict" was used */
18945 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018946 }
18947
Bram Moolenaar071d4272004-06-13 20:20:40 +000018948 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018949 STRCPY(fp->uf_name, name);
18950 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018951 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018952 fp->uf_args = newargs;
18953 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018954#ifdef FEAT_PROFILE
18955 fp->uf_tml_count = NULL;
18956 fp->uf_tml_total = NULL;
18957 fp->uf_tml_self = NULL;
18958 fp->uf_profiling = FALSE;
18959 if (prof_def_func())
18960 func_do_profile(fp);
18961#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018962 fp->uf_varargs = varargs;
18963 fp->uf_flags = flags;
18964 fp->uf_calls = 0;
18965 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018966 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018967
18968erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000018969 ga_clear_strings(&newargs);
18970 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018971ret_free:
18972 vim_free(skip_until);
18973 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018974 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018975 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018976}
18977
18978/*
18979 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000018980 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018981 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018982 * flags:
18983 * TFN_INT: internal function name OK
18984 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000018985 * Advances "pp" to just after the function name (if no error).
18986 */
18987 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018988trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018989 char_u **pp;
18990 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018991 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000018992 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018993{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018994 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018995 char_u *start;
18996 char_u *end;
18997 int lead;
18998 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018999 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000019000 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019001
19002 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019003 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019004 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000019005
19006 /* Check for hard coded <SNR>: already translated function ID (from a user
19007 * command). */
19008 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
19009 && (*pp)[2] == (int)KE_SNR)
19010 {
19011 *pp += 3;
19012 len = get_id_len(pp) + 3;
19013 return vim_strnsave(start, len);
19014 }
19015
19016 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
19017 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019018 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000019019 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019020 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019021
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019022 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
19023 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019024 if (end == start)
19025 {
19026 if (!skip)
19027 EMSG(_("E129: Function name required"));
19028 goto theend;
19029 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019030 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019031 {
19032 /*
19033 * Report an invalid expression in braces, unless the expression
19034 * evaluation has been cancelled due to an aborting error, an
19035 * interrupt, or an exception.
19036 */
19037 if (!aborting())
19038 {
19039 if (end != NULL)
19040 EMSG2(_(e_invarg2), start);
19041 }
19042 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019043 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019044 goto theend;
19045 }
19046
19047 if (lv.ll_tv != NULL)
19048 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019049 if (fdp != NULL)
19050 {
19051 fdp->fd_dict = lv.ll_dict;
19052 fdp->fd_newkey = lv.ll_newkey;
19053 lv.ll_newkey = NULL;
19054 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019055 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019056 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
19057 {
19058 name = vim_strsave(lv.ll_tv->vval.v_string);
19059 *pp = end;
19060 }
19061 else
19062 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019063 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
19064 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019065 EMSG(_(e_funcref));
19066 else
19067 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019068 name = NULL;
19069 }
19070 goto theend;
19071 }
19072
19073 if (lv.ll_name == NULL)
19074 {
19075 /* Error found, but continue after the function name. */
19076 *pp = end;
19077 goto theend;
19078 }
19079
19080 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000019081 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019082 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000019083 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
19084 && STRNCMP(lv.ll_name, "s:", 2) == 0)
19085 {
19086 /* When there was "s:" already or the name expanded to get a
19087 * leading "s:" then remove it. */
19088 lv.ll_name += 2;
19089 len -= 2;
19090 lead = 2;
19091 }
19092 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019093 else
Bram Moolenaara7043832005-01-21 11:56:39 +000019094 {
19095 if (lead == 2) /* skip over "s:" */
19096 lv.ll_name += 2;
19097 len = (int)(end - lv.ll_name);
19098 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019099
19100 /*
19101 * Copy the function name to allocated memory.
19102 * Accept <SID>name() inside a script, translate into <SNR>123_name().
19103 * Accept <SNR>123_name() outside a script.
19104 */
19105 if (skip)
19106 lead = 0; /* do nothing */
19107 else if (lead > 0)
19108 {
19109 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000019110 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
19111 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019112 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000019113 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019114 if (current_SID <= 0)
19115 {
19116 EMSG(_(e_usingsid));
19117 goto theend;
19118 }
19119 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
19120 lead += (int)STRLEN(sid_buf);
19121 }
19122 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019123 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019124 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019125 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019126 goto theend;
19127 }
19128 name = alloc((unsigned)(len + lead + 1));
19129 if (name != NULL)
19130 {
19131 if (lead > 0)
19132 {
19133 name[0] = K_SPECIAL;
19134 name[1] = KS_EXTRA;
19135 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000019136 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019137 STRCPY(name + 3, sid_buf);
19138 }
19139 mch_memmove(name + lead, lv.ll_name, (size_t)len);
19140 name[len + lead] = NUL;
19141 }
19142 *pp = end;
19143
19144theend:
19145 clear_lval(&lv);
19146 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019147}
19148
19149/*
19150 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
19151 * Return 2 if "p" starts with "s:".
19152 * Return 0 otherwise.
19153 */
19154 static int
19155eval_fname_script(p)
19156 char_u *p;
19157{
19158 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
19159 || STRNICMP(p + 1, "SNR>", 4) == 0))
19160 return 5;
19161 if (p[0] == 's' && p[1] == ':')
19162 return 2;
19163 return 0;
19164}
19165
19166/*
19167 * Return TRUE if "p" starts with "<SID>" or "s:".
19168 * Only works if eval_fname_script() returned non-zero for "p"!
19169 */
19170 static int
19171eval_fname_sid(p)
19172 char_u *p;
19173{
19174 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
19175}
19176
19177/*
19178 * List the head of the function: "name(arg1, arg2)".
19179 */
19180 static void
19181list_func_head(fp, indent)
19182 ufunc_T *fp;
19183 int indent;
19184{
19185 int j;
19186
19187 msg_start();
19188 if (indent)
19189 MSG_PUTS(" ");
19190 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019191 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019192 {
19193 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019194 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019195 }
19196 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019197 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019198 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019199 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019200 {
19201 if (j)
19202 MSG_PUTS(", ");
19203 msg_puts(FUNCARG(fp, j));
19204 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019205 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019206 {
19207 if (j)
19208 MSG_PUTS(", ");
19209 MSG_PUTS("...");
19210 }
19211 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019212 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000019213 if (p_verbose > 0)
19214 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019215}
19216
19217/*
19218 * Find a function by name, return pointer to it in ufuncs.
19219 * Return NULL for unknown function.
19220 */
19221 static ufunc_T *
19222find_func(name)
19223 char_u *name;
19224{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019225 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019226
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019227 hi = hash_find(&func_hashtab, name);
19228 if (!HASHITEM_EMPTY(hi))
19229 return HI2UF(hi);
19230 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019231}
19232
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019233#if defined(EXITFREE) || defined(PROTO)
19234 void
19235free_all_functions()
19236{
19237 hashitem_T *hi;
19238
19239 /* Need to start all over every time, because func_free() may change the
19240 * hash table. */
19241 while (func_hashtab.ht_used > 0)
19242 for (hi = func_hashtab.ht_array; ; ++hi)
19243 if (!HASHITEM_EMPTY(hi))
19244 {
19245 func_free(HI2UF(hi));
19246 break;
19247 }
19248}
19249#endif
19250
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019251/*
19252 * Return TRUE if a function "name" exists.
19253 */
19254 static int
19255function_exists(name)
19256 char_u *name;
19257{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000019258 char_u *nm = name;
19259 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019260 int n = FALSE;
19261
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000019262 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000019263 nm = skipwhite(nm);
19264
19265 /* Only accept "funcname", "funcname ", "funcname (..." and
19266 * "funcname(...", not "funcname!...". */
19267 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019268 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019269 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019270 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019271 else
19272 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019273 }
Bram Moolenaar79783442006-05-05 21:18:03 +000019274 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019275 return n;
19276}
19277
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019278/*
19279 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019280 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019281 */
19282 static int
19283builtin_function(name)
19284 char_u *name;
19285{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019286 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
19287 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019288}
19289
Bram Moolenaar05159a02005-02-26 23:04:13 +000019290#if defined(FEAT_PROFILE) || defined(PROTO)
19291/*
19292 * Start profiling function "fp".
19293 */
19294 static void
19295func_do_profile(fp)
19296 ufunc_T *fp;
19297{
19298 fp->uf_tm_count = 0;
19299 profile_zero(&fp->uf_tm_self);
19300 profile_zero(&fp->uf_tm_total);
19301 if (fp->uf_tml_count == NULL)
19302 fp->uf_tml_count = (int *)alloc_clear((unsigned)
19303 (sizeof(int) * fp->uf_lines.ga_len));
19304 if (fp->uf_tml_total == NULL)
19305 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
19306 (sizeof(proftime_T) * fp->uf_lines.ga_len));
19307 if (fp->uf_tml_self == NULL)
19308 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
19309 (sizeof(proftime_T) * fp->uf_lines.ga_len));
19310 fp->uf_tml_idx = -1;
19311 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
19312 || fp->uf_tml_self == NULL)
19313 return; /* out of memory */
19314
19315 fp->uf_profiling = TRUE;
19316}
19317
19318/*
19319 * Dump the profiling results for all functions in file "fd".
19320 */
19321 void
19322func_dump_profile(fd)
19323 FILE *fd;
19324{
19325 hashitem_T *hi;
19326 int todo;
19327 ufunc_T *fp;
19328 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000019329 ufunc_T **sorttab;
19330 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019331
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019332 todo = (int)func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000019333 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
19334
Bram Moolenaar05159a02005-02-26 23:04:13 +000019335 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
19336 {
19337 if (!HASHITEM_EMPTY(hi))
19338 {
19339 --todo;
19340 fp = HI2UF(hi);
19341 if (fp->uf_profiling)
19342 {
Bram Moolenaar73830342005-02-28 22:48:19 +000019343 if (sorttab != NULL)
19344 sorttab[st_len++] = fp;
19345
Bram Moolenaar05159a02005-02-26 23:04:13 +000019346 if (fp->uf_name[0] == K_SPECIAL)
19347 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
19348 else
19349 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
19350 if (fp->uf_tm_count == 1)
19351 fprintf(fd, "Called 1 time\n");
19352 else
19353 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
19354 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
19355 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
19356 fprintf(fd, "\n");
19357 fprintf(fd, "count total (s) self (s)\n");
19358
19359 for (i = 0; i < fp->uf_lines.ga_len; ++i)
19360 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019361 if (FUNCLINE(fp, i) == NULL)
19362 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000019363 prof_func_line(fd, fp->uf_tml_count[i],
19364 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019365 fprintf(fd, "%s\n", FUNCLINE(fp, i));
19366 }
19367 fprintf(fd, "\n");
19368 }
19369 }
19370 }
Bram Moolenaar73830342005-02-28 22:48:19 +000019371
19372 if (sorttab != NULL && st_len > 0)
19373 {
19374 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
19375 prof_total_cmp);
19376 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
19377 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
19378 prof_self_cmp);
19379 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
19380 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019381}
Bram Moolenaar73830342005-02-28 22:48:19 +000019382
19383 static void
19384prof_sort_list(fd, sorttab, st_len, title, prefer_self)
19385 FILE *fd;
19386 ufunc_T **sorttab;
19387 int st_len;
19388 char *title;
19389 int prefer_self; /* when equal print only self time */
19390{
19391 int i;
19392 ufunc_T *fp;
19393
19394 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
19395 fprintf(fd, "count total (s) self (s) function\n");
19396 for (i = 0; i < 20 && i < st_len; ++i)
19397 {
19398 fp = sorttab[i];
19399 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
19400 prefer_self);
19401 if (fp->uf_name[0] == K_SPECIAL)
19402 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
19403 else
19404 fprintf(fd, " %s()\n", fp->uf_name);
19405 }
19406 fprintf(fd, "\n");
19407}
19408
19409/*
19410 * Print the count and times for one function or function line.
19411 */
19412 static void
19413prof_func_line(fd, count, total, self, prefer_self)
19414 FILE *fd;
19415 int count;
19416 proftime_T *total;
19417 proftime_T *self;
19418 int prefer_self; /* when equal print only self time */
19419{
19420 if (count > 0)
19421 {
19422 fprintf(fd, "%5d ", count);
19423 if (prefer_self && profile_equal(total, self))
19424 fprintf(fd, " ");
19425 else
19426 fprintf(fd, "%s ", profile_msg(total));
19427 if (!prefer_self && profile_equal(total, self))
19428 fprintf(fd, " ");
19429 else
19430 fprintf(fd, "%s ", profile_msg(self));
19431 }
19432 else
19433 fprintf(fd, " ");
19434}
19435
19436/*
19437 * Compare function for total time sorting.
19438 */
19439 static int
19440#ifdef __BORLANDC__
19441_RTLENTRYF
19442#endif
19443prof_total_cmp(s1, s2)
19444 const void *s1;
19445 const void *s2;
19446{
19447 ufunc_T *p1, *p2;
19448
19449 p1 = *(ufunc_T **)s1;
19450 p2 = *(ufunc_T **)s2;
19451 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
19452}
19453
19454/*
19455 * Compare function for self time sorting.
19456 */
19457 static int
19458#ifdef __BORLANDC__
19459_RTLENTRYF
19460#endif
19461prof_self_cmp(s1, s2)
19462 const void *s1;
19463 const void *s2;
19464{
19465 ufunc_T *p1, *p2;
19466
19467 p1 = *(ufunc_T **)s1;
19468 p2 = *(ufunc_T **)s2;
19469 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
19470}
19471
Bram Moolenaar05159a02005-02-26 23:04:13 +000019472#endif
19473
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019474/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019475 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019476 * Return TRUE if a package was loaded.
19477 */
19478 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019479script_autoload(name, reload)
19480 char_u *name;
19481 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019482{
19483 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019484 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019485 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019486 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019487
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019488 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019489 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019490 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019491 return FALSE;
19492
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019493 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019494
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019495 /* Find the name in the list of previously loaded package names. Skip
19496 * "autoload/", it's always the same. */
19497 for (i = 0; i < ga_loaded.ga_len; ++i)
19498 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
19499 break;
19500 if (!reload && i < ga_loaded.ga_len)
19501 ret = FALSE; /* was loaded already */
19502 else
19503 {
19504 /* Remember the name if it wasn't loaded already. */
19505 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
19506 {
19507 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
19508 tofree = NULL;
19509 }
19510
19511 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000019512 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019513 ret = TRUE;
19514 }
19515
19516 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019517 return ret;
19518}
19519
19520/*
19521 * Return the autoload script name for a function or variable name.
19522 * Returns NULL when out of memory.
19523 */
19524 static char_u *
19525autoload_name(name)
19526 char_u *name;
19527{
19528 char_u *p;
19529 char_u *scriptname;
19530
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019531 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019532 scriptname = alloc((unsigned)(STRLEN(name) + 14));
19533 if (scriptname == NULL)
19534 return FALSE;
19535 STRCPY(scriptname, "autoload/");
19536 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019537 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019538 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019539 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019540 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019541 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019542}
19543
Bram Moolenaar071d4272004-06-13 20:20:40 +000019544#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
19545
19546/*
19547 * Function given to ExpandGeneric() to obtain the list of user defined
19548 * function names.
19549 */
19550 char_u *
19551get_user_func_name(xp, idx)
19552 expand_T *xp;
19553 int idx;
19554{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019555 static long_u done;
19556 static hashitem_T *hi;
19557 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019558
19559 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019560 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019561 done = 0;
19562 hi = func_hashtab.ht_array;
19563 }
19564 if (done < func_hashtab.ht_used)
19565 {
19566 if (done++ > 0)
19567 ++hi;
19568 while (HASHITEM_EMPTY(hi))
19569 ++hi;
19570 fp = HI2UF(hi);
19571
19572 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
19573 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019574
19575 cat_func_name(IObuff, fp);
19576 if (xp->xp_context != EXPAND_USER_FUNC)
19577 {
19578 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019579 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019580 STRCAT(IObuff, ")");
19581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019582 return IObuff;
19583 }
19584 return NULL;
19585}
19586
19587#endif /* FEAT_CMDL_COMPL */
19588
19589/*
19590 * Copy the function name of "fp" to buffer "buf".
19591 * "buf" must be able to hold the function name plus three bytes.
19592 * Takes care of script-local function names.
19593 */
19594 static void
19595cat_func_name(buf, fp)
19596 char_u *buf;
19597 ufunc_T *fp;
19598{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019599 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019600 {
19601 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019602 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019603 }
19604 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019605 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019606}
19607
19608/*
19609 * ":delfunction {name}"
19610 */
19611 void
19612ex_delfunction(eap)
19613 exarg_T *eap;
19614{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019615 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019616 char_u *p;
19617 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019618 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019619
19620 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019621 name = trans_function_name(&p, eap->skip, 0, &fudi);
19622 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019623 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019624 {
19625 if (fudi.fd_dict != NULL && !eap->skip)
19626 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019627 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019629 if (!ends_excmd(*skipwhite(p)))
19630 {
19631 vim_free(name);
19632 EMSG(_(e_trailing));
19633 return;
19634 }
19635 eap->nextcmd = check_nextcmd(p);
19636 if (eap->nextcmd != NULL)
19637 *p = NUL;
19638
19639 if (!eap->skip)
19640 fp = find_func(name);
19641 vim_free(name);
19642
19643 if (!eap->skip)
19644 {
19645 if (fp == NULL)
19646 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019647 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019648 return;
19649 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019650 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019651 {
19652 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
19653 return;
19654 }
19655
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019656 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019657 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019658 /* Delete the dict item that refers to the function, it will
19659 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019660 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019661 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019662 else
19663 func_free(fp);
19664 }
19665}
19666
19667/*
19668 * Free a function and remove it from the list of functions.
19669 */
19670 static void
19671func_free(fp)
19672 ufunc_T *fp;
19673{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019674 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019675
19676 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019677 ga_clear_strings(&(fp->uf_args));
19678 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000019679#ifdef FEAT_PROFILE
19680 vim_free(fp->uf_tml_count);
19681 vim_free(fp->uf_tml_total);
19682 vim_free(fp->uf_tml_self);
19683#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019684
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019685 /* remove the function from the function hashtable */
19686 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
19687 if (HASHITEM_EMPTY(hi))
19688 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019689 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019690 hash_remove(&func_hashtab, hi);
19691
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019692 vim_free(fp);
19693}
19694
19695/*
19696 * Unreference a Function: decrement the reference count and free it when it
19697 * becomes zero. Only for numbered functions.
19698 */
19699 static void
19700func_unref(name)
19701 char_u *name;
19702{
19703 ufunc_T *fp;
19704
19705 if (name != NULL && isdigit(*name))
19706 {
19707 fp = find_func(name);
19708 if (fp == NULL)
19709 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019710 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019711 {
19712 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019713 * when "uf_calls" becomes zero. */
19714 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019715 func_free(fp);
19716 }
19717 }
19718}
19719
19720/*
19721 * Count a reference to a Function.
19722 */
19723 static void
19724func_ref(name)
19725 char_u *name;
19726{
19727 ufunc_T *fp;
19728
19729 if (name != NULL && isdigit(*name))
19730 {
19731 fp = find_func(name);
19732 if (fp == NULL)
19733 EMSG2(_(e_intern2), "func_ref()");
19734 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019735 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019736 }
19737}
19738
19739/*
19740 * Call a user function.
19741 */
19742 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000019743call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019744 ufunc_T *fp; /* pointer to function */
19745 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000019746 typval_T *argvars; /* arguments */
19747 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019748 linenr_T firstline; /* first line of range */
19749 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000019750 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019751{
Bram Moolenaar33570922005-01-25 22:26:29 +000019752 char_u *save_sourcing_name;
19753 linenr_T save_sourcing_lnum;
19754 scid_T save_current_SID;
19755 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000019756 int save_did_emsg;
19757 static int depth = 0;
19758 dictitem_T *v;
19759 int fixvar_idx = 0; /* index in fixvar[] */
19760 int i;
19761 int ai;
19762 char_u numbuf[NUMBUFLEN];
19763 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019764#ifdef FEAT_PROFILE
19765 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000019766 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019767#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;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000019946 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019947 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 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000019976 profile_end(&call_start);
19977 profile_sub_wait(&wait_start, &call_start);
19978 profile_add(&fp->uf_tm_total, &call_start);
19979 profile_self(&fp->uf_tm_self, &call_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 Moolenaare9da72e2006-11-01 17:34:40 +000019982 profile_add(&fc.caller->func->uf_tm_children, &call_start);
19983 profile_add(&fc.caller->func->uf_tml_children, &call_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) */