blob: 7a5ca1d6005634d8edf8c9c3fc50f857b5535dfc [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));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000544static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000545static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000558static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000559static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000564static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000565static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000580static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000581static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000582static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000583static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000586#ifdef vim_mkdir
587static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
588#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000589static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000592static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000593static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000594static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000595static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000596static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000597static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000598static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000600static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000611static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000612static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000613static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000615static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
619static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000620static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000621static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000622static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000623static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000624static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000625static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000626static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000627static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
628static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000629static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000630static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
631static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000632static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2c932302006-03-18 21:42:09 +0000633static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000634#ifdef HAVE_STRFTIME
635static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
636#endif
637static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
638static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
639static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
640static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
641static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
642static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
643static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
644static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
645static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
646static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
647static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
648static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000649static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000650static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000651static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000652static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000653static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000654static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000655static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000656static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
657static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
658static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
659static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
660static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
661static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
662static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
664static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
665static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
666static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
667static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
668static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000669static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
670static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000671static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000672static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000673
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000674static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
675static pos_T *var2fpos __ARGS((typval_T *varp, int lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000676static int get_env_len __ARGS((char_u **arg));
677static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000678static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000679static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
680#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
681#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
682 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000683static char_u * make_expanded_name __ARGS((char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end));
Bram Moolenaar33570922005-01-25 22:26:29 +0000684static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000685static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000686static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
687static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000688static typval_T *alloc_tv __ARGS((void));
689static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000690static void init_tv __ARGS((typval_T *varp));
691static long get_tv_number __ARGS((typval_T *varp));
692static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000693static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000694static char_u *get_tv_string __ARGS((typval_T *varp));
695static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000696static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000697static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000698static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000699static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
700static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
701static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
702static void list_one_var __ARGS((dictitem_T *v, char_u *prefix));
703static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
704static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
705static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000706static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000707static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000708static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000709static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000710static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
711static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
712static int eval_fname_script __ARGS((char_u *p));
713static int eval_fname_sid __ARGS((char_u *p));
714static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000715static ufunc_T *find_func __ARGS((char_u *name));
716static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000717static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000718#ifdef FEAT_PROFILE
719static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000720static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
721static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
722static int
723# ifdef __BORLANDC__
724 _RTLENTRYF
725# endif
726 prof_total_cmp __ARGS((const void *s1, const void *s2));
727static int
728# ifdef __BORLANDC__
729 _RTLENTRYF
730# endif
731 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000732#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000733static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000734static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000735static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000736static void func_free __ARGS((ufunc_T *fp));
737static void func_unref __ARGS((char_u *name));
738static void func_ref __ARGS((char_u *name));
739static void call_user_func __ARGS((ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict));
740static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000741static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
742static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000743static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000744static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000745static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000746
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000747/* Character used as separated in autoload function/variable names. */
748#define AUTOLOAD_CHAR '#'
749
Bram Moolenaar33570922005-01-25 22:26:29 +0000750/*
751 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000752 */
753 void
754eval_init()
755{
Bram Moolenaar33570922005-01-25 22:26:29 +0000756 int i;
757 struct vimvar *p;
758
759 init_var_dict(&globvardict, &globvars_var);
760 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000761 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000762 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000763
764 for (i = 0; i < VV_LEN; ++i)
765 {
766 p = &vimvars[i];
767 STRCPY(p->vv_di.di_key, p->vv_name);
768 if (p->vv_flags & VV_RO)
769 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
770 else if (p->vv_flags & VV_RO_SBX)
771 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
772 else
773 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000774
775 /* add to v: scope dict, unless the value is not always available */
776 if (p->vv_type != VAR_UNKNOWN)
777 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000778 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000779 /* add to compat scope dict */
780 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000781 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000782}
783
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000784#if defined(EXITFREE) || defined(PROTO)
785 void
786eval_clear()
787{
788 int i;
789 struct vimvar *p;
790
791 for (i = 0; i < VV_LEN; ++i)
792 {
793 p = &vimvars[i];
794 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000795 {
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000796 vim_free(p->vv_di.di_tv.vval.v_string);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000797 p->vv_di.di_tv.vval.v_string = NULL;
798 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000799 }
800 hash_clear(&vimvarht);
801 hash_clear(&compat_hashtab);
802
803 /* script-local variables */
804 for (i = 1; i <= ga_scripts.ga_len; ++i)
805 vars_clear(&SCRIPT_VARS(i));
806 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000807 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000808
809 /* global variables */
810 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000811
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000812 /* functions */
Bram Moolenaard9fba312005-06-26 22:34:35 +0000813 free_all_functions();
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000814 hash_clear(&func_hashtab);
815
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000816 /* autoloaded script names */
817 ga_clear_strings(&ga_loaded);
818
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000819 /* unreferenced lists and dicts */
820 (void)garbage_collect();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000821}
822#endif
823
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000824/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 * Return the name of the executed function.
826 */
827 char_u *
828func_name(cookie)
829 void *cookie;
830{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000831 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832}
833
834/*
835 * Return the address holding the next breakpoint line for a funccall cookie.
836 */
837 linenr_T *
838func_breakpoint(cookie)
839 void *cookie;
840{
Bram Moolenaar33570922005-01-25 22:26:29 +0000841 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842}
843
844/*
845 * Return the address holding the debug tick for a funccall cookie.
846 */
847 int *
848func_dbg_tick(cookie)
849 void *cookie;
850{
Bram Moolenaar33570922005-01-25 22:26:29 +0000851 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852}
853
854/*
855 * Return the nesting level for a funccall cookie.
856 */
857 int
858func_level(cookie)
859 void *cookie;
860{
Bram Moolenaar33570922005-01-25 22:26:29 +0000861 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862}
863
864/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000865funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866
867/*
868 * Return TRUE when a function was ended by a ":return" command.
869 */
870 int
871current_func_returned()
872{
873 return current_funccal->returned;
874}
875
876
877/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 * Set an internal variable to a string value. Creates the variable if it does
879 * not already exist.
880 */
881 void
882set_internal_string_var(name, value)
883 char_u *name;
884 char_u *value;
885{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000886 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000887 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888
889 val = vim_strsave(value);
890 if (val != NULL)
891 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000892 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000893 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000895 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000896 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 }
898 }
899}
900
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000901static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000902static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000903static char_u *redir_endp = NULL;
904static char_u *redir_varname = NULL;
905
906/*
907 * Start recording command output to a variable
908 * Returns OK if successfully completed the setup. FAIL otherwise.
909 */
910 int
911var_redir_start(name, append)
912 char_u *name;
913 int append; /* append to an existing variable */
914{
915 int save_emsg;
916 int err;
917 typval_T tv;
918
919 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000920 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000921 {
922 EMSG(_(e_invarg));
923 return FAIL;
924 }
925
926 redir_varname = vim_strsave(name);
927 if (redir_varname == NULL)
928 return FAIL;
929
930 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
931 if (redir_lval == NULL)
932 {
933 var_redir_stop();
934 return FAIL;
935 }
936
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000937 /* The output is stored in growarray "redir_ga" until redirection ends. */
938 ga_init2(&redir_ga, (int)sizeof(char), 500);
939
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000940 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000941 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
942 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000943 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
944 {
945 if (redir_endp != NULL && *redir_endp != NUL)
946 /* Trailing characters are present after the variable name */
947 EMSG(_(e_trailing));
948 else
949 EMSG(_(e_invarg));
950 var_redir_stop();
951 return FAIL;
952 }
953
954 /* check if we can write to the variable: set it to or append an empty
955 * string */
956 save_emsg = did_emsg;
957 did_emsg = FALSE;
958 tv.v_type = VAR_STRING;
959 tv.vval.v_string = (char_u *)"";
960 if (append)
961 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
962 else
963 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
964 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000965 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000966 if (err)
967 {
968 var_redir_stop();
969 return FAIL;
970 }
971 if (redir_lval->ll_newkey != NULL)
972 {
973 /* Dictionary item was created, don't do it again. */
974 vim_free(redir_lval->ll_newkey);
975 redir_lval->ll_newkey = NULL;
976 }
977
978 return OK;
979}
980
981/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000982 * Append "value[value_len]" to the variable set by var_redir_start().
983 * The actual appending is postponed until redirection ends, because the value
984 * appended may in fact be the string we write to, changing it may cause freed
985 * memory to be used:
986 * :redir => foo
987 * :let foo
988 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000989 */
990 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000991var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000992 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000993 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000994{
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000995 size_t len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000996
997 if (redir_lval == NULL)
998 return;
999
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001000 if (value_len == -1)
1001 len = STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001002 else
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001003 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001004
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001005 if (ga_grow(&redir_ga, (int)len) == OK)
1006 {
1007 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar77f66d62007-02-20 02:16:18 +00001008 redir_ga.ga_len += (int)len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001009 }
1010 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001011 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001012}
1013
1014/*
1015 * Stop redirecting command output to a variable.
1016 */
1017 void
1018var_redir_stop()
1019{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001020 typval_T tv;
1021
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001022 if (redir_lval != NULL)
1023 {
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001024 /* Append the trailing NUL. */
1025 ga_append(&redir_ga, NUL);
1026
1027 /* Assign the text to the variable. */
1028 tv.v_type = VAR_STRING;
1029 tv.vval.v_string = redir_ga.ga_data;
1030 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1031 vim_free(tv.vval.v_string);
1032
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001033 clear_lval(redir_lval);
1034 vim_free(redir_lval);
1035 redir_lval = NULL;
1036 }
1037 vim_free(redir_varname);
1038 redir_varname = NULL;
1039}
1040
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041# if defined(FEAT_MBYTE) || defined(PROTO)
1042 int
1043eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1044 char_u *enc_from;
1045 char_u *enc_to;
1046 char_u *fname_from;
1047 char_u *fname_to;
1048{
1049 int err = FALSE;
1050
1051 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1052 set_vim_var_string(VV_CC_TO, enc_to, -1);
1053 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1054 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1055 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1056 err = TRUE;
1057 set_vim_var_string(VV_CC_FROM, NULL, -1);
1058 set_vim_var_string(VV_CC_TO, NULL, -1);
1059 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1060 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1061
1062 if (err)
1063 return FAIL;
1064 return OK;
1065}
1066# endif
1067
1068# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1069 int
1070eval_printexpr(fname, args)
1071 char_u *fname;
1072 char_u *args;
1073{
1074 int err = FALSE;
1075
1076 set_vim_var_string(VV_FNAME_IN, fname, -1);
1077 set_vim_var_string(VV_CMDARG, args, -1);
1078 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1079 err = TRUE;
1080 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1081 set_vim_var_string(VV_CMDARG, NULL, -1);
1082
1083 if (err)
1084 {
1085 mch_remove(fname);
1086 return FAIL;
1087 }
1088 return OK;
1089}
1090# endif
1091
1092# if defined(FEAT_DIFF) || defined(PROTO)
1093 void
1094eval_diff(origfile, newfile, outfile)
1095 char_u *origfile;
1096 char_u *newfile;
1097 char_u *outfile;
1098{
1099 int err = FALSE;
1100
1101 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1102 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1103 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1104 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1105 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1106 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1107 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1108}
1109
1110 void
1111eval_patch(origfile, difffile, outfile)
1112 char_u *origfile;
1113 char_u *difffile;
1114 char_u *outfile;
1115{
1116 int err;
1117
1118 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1119 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1120 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1121 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1122 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1123 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1124 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1125}
1126# endif
1127
1128/*
1129 * Top level evaluation function, returning a boolean.
1130 * Sets "error" to TRUE if there was an error.
1131 * Return TRUE or FALSE.
1132 */
1133 int
1134eval_to_bool(arg, error, nextcmd, skip)
1135 char_u *arg;
1136 int *error;
1137 char_u **nextcmd;
1138 int skip; /* only parse, don't execute */
1139{
Bram Moolenaar33570922005-01-25 22:26:29 +00001140 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 int retval = FALSE;
1142
1143 if (skip)
1144 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001145 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 else
1148 {
1149 *error = FALSE;
1150 if (!skip)
1151 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001152 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001153 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 }
1155 }
1156 if (skip)
1157 --emsg_skip;
1158
1159 return retval;
1160}
1161
1162/*
1163 * Top level evaluation function, returning a string. If "skip" is TRUE,
1164 * only parsing to "nextcmd" is done, without reporting errors. Return
1165 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1166 */
1167 char_u *
1168eval_to_string_skip(arg, nextcmd, skip)
1169 char_u *arg;
1170 char_u **nextcmd;
1171 int skip; /* only parse, don't execute */
1172{
Bram Moolenaar33570922005-01-25 22:26:29 +00001173 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 char_u *retval;
1175
1176 if (skip)
1177 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001178 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 retval = NULL;
1180 else
1181 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001182 retval = vim_strsave(get_tv_string(&tv));
1183 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 }
1185 if (skip)
1186 --emsg_skip;
1187
1188 return retval;
1189}
1190
1191/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001192 * Skip over an expression at "*pp".
1193 * Return FAIL for an error, OK otherwise.
1194 */
1195 int
1196skip_expr(pp)
1197 char_u **pp;
1198{
Bram Moolenaar33570922005-01-25 22:26:29 +00001199 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001200
1201 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001202 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001203}
1204
1205/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 * Top level evaluation function, returning a string.
1207 * Return pointer to allocated memory, or NULL for failure.
1208 */
1209 char_u *
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001210eval_to_string(arg, nextcmd, dolist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 char_u *arg;
1212 char_u **nextcmd;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001213 int dolist; /* turn List into sequence of lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214{
Bram Moolenaar33570922005-01-25 22:26:29 +00001215 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001217 garray_T ga;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001219 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 retval = NULL;
1221 else
1222 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001223 if (dolist && tv.v_type == VAR_LIST)
1224 {
1225 ga_init2(&ga, (int)sizeof(char), 80);
1226 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
1227 ga_append(&ga, NUL);
1228 retval = (char_u *)ga.ga_data;
1229 }
1230 else
1231 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001232 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 }
1234
1235 return retval;
1236}
1237
1238/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001239 * Call eval_to_string() without using current local variables and using
1240 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 */
1242 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001243eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 char_u *arg;
1245 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001246 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247{
1248 char_u *retval;
1249 void *save_funccalp;
1250
1251 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001252 if (use_sandbox)
1253 ++sandbox;
1254 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001255 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001256 if (use_sandbox)
1257 --sandbox;
1258 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 restore_funccal(save_funccalp);
1260 return retval;
1261}
1262
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263/*
1264 * Top level evaluation function, returning a number.
1265 * Evaluates "expr" silently.
1266 * Returns -1 for an error.
1267 */
1268 int
1269eval_to_number(expr)
1270 char_u *expr;
1271{
Bram Moolenaar33570922005-01-25 22:26:29 +00001272 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001274 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275
1276 ++emsg_off;
1277
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001278 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 retval = -1;
1280 else
1281 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001282 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001283 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 }
1285 --emsg_off;
1286
1287 return retval;
1288}
1289
Bram Moolenaara40058a2005-07-11 22:42:07 +00001290/*
1291 * Prepare v: variable "idx" to be used.
1292 * Save the current typeval in "save_tv".
1293 * When not used yet add the variable to the v: hashtable.
1294 */
1295 static void
1296prepare_vimvar(idx, save_tv)
1297 int idx;
1298 typval_T *save_tv;
1299{
1300 *save_tv = vimvars[idx].vv_tv;
1301 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1302 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1303}
1304
1305/*
1306 * Restore v: variable "idx" to typeval "save_tv".
1307 * When no longer defined, remove the variable from the v: hashtable.
1308 */
1309 static void
1310restore_vimvar(idx, save_tv)
1311 int idx;
1312 typval_T *save_tv;
1313{
1314 hashitem_T *hi;
1315
1316 clear_tv(&vimvars[idx].vv_tv);
1317 vimvars[idx].vv_tv = *save_tv;
1318 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1319 {
1320 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1321 if (HASHITEM_EMPTY(hi))
1322 EMSG2(_(e_intern2), "restore_vimvar()");
1323 else
1324 hash_remove(&vimvarht, hi);
1325 }
1326}
1327
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001328#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001329/*
1330 * Evaluate an expression to a list with suggestions.
1331 * For the "expr:" part of 'spellsuggest'.
1332 */
1333 list_T *
1334eval_spell_expr(badword, expr)
1335 char_u *badword;
1336 char_u *expr;
1337{
1338 typval_T save_val;
1339 typval_T rettv;
1340 list_T *list = NULL;
1341 char_u *p = skipwhite(expr);
1342
1343 /* Set "v:val" to the bad word. */
1344 prepare_vimvar(VV_VAL, &save_val);
1345 vimvars[VV_VAL].vv_type = VAR_STRING;
1346 vimvars[VV_VAL].vv_str = badword;
1347 if (p_verbose == 0)
1348 ++emsg_off;
1349
1350 if (eval1(&p, &rettv, TRUE) == OK)
1351 {
1352 if (rettv.v_type != VAR_LIST)
1353 clear_tv(&rettv);
1354 else
1355 list = rettv.vval.v_list;
1356 }
1357
1358 if (p_verbose == 0)
1359 --emsg_off;
1360 vimvars[VV_VAL].vv_str = NULL;
1361 restore_vimvar(VV_VAL, &save_val);
1362
1363 return list;
1364}
1365
1366/*
1367 * "list" is supposed to contain two items: a word and a number. Return the
1368 * word in "pp" and the number as the return value.
1369 * Return -1 if anything isn't right.
1370 * Used to get the good word and score from the eval_spell_expr() result.
1371 */
1372 int
1373get_spellword(list, pp)
1374 list_T *list;
1375 char_u **pp;
1376{
1377 listitem_T *li;
1378
1379 li = list->lv_first;
1380 if (li == NULL)
1381 return -1;
1382 *pp = get_tv_string(&li->li_tv);
1383
1384 li = li->li_next;
1385 if (li == NULL)
1386 return -1;
1387 return get_tv_number(&li->li_tv);
1388}
1389#endif
1390
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001391/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001392 * Top level evaluation function.
1393 * Returns an allocated typval_T with the result.
1394 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001395 */
1396 typval_T *
1397eval_expr(arg, nextcmd)
1398 char_u *arg;
1399 char_u **nextcmd;
1400{
1401 typval_T *tv;
1402
1403 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001404 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001405 {
1406 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001407 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001408 }
1409
1410 return tv;
1411}
1412
1413
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1415/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001416 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 * Uses argv[argc] for the function arguments.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001418 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001420 static int
1421call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 char_u *func;
1423 int argc;
1424 char_u **argv;
1425 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001426 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427{
Bram Moolenaar33570922005-01-25 22:26:29 +00001428 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 long n;
1430 int len;
1431 int i;
1432 int doesrange;
1433 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001434 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001436 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001438 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439
1440 for (i = 0; i < argc; i++)
1441 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001442 /* Pass a NULL or empty argument as an empty string */
1443 if (argv[i] == NULL || *argv[i] == NUL)
1444 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001445 argvars[i].v_type = VAR_STRING;
1446 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001447 continue;
1448 }
1449
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 /* Recognize a number argument, the others must be strings. */
1451 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1452 if (len != 0 && len == (int)STRLEN(argv[i]))
1453 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001454 argvars[i].v_type = VAR_NUMBER;
1455 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 }
1457 else
1458 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001459 argvars[i].v_type = VAR_STRING;
1460 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 }
1462 }
1463
1464 if (safe)
1465 {
1466 save_funccalp = save_funccal();
1467 ++sandbox;
1468 }
1469
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001470 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1471 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001473 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 if (safe)
1475 {
1476 --sandbox;
1477 restore_funccal(save_funccalp);
1478 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001479 vim_free(argvars);
1480
1481 if (ret == FAIL)
1482 clear_tv(rettv);
1483
1484 return ret;
1485}
1486
1487/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001488 * Call vimL function "func" and return the result as a string.
1489 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001490 * Uses argv[argc] for the function arguments.
1491 */
1492 void *
1493call_func_retstr(func, argc, argv, safe)
1494 char_u *func;
1495 int argc;
1496 char_u **argv;
1497 int safe; /* use the sandbox */
1498{
1499 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001500 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001501
1502 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1503 return NULL;
1504
1505 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001506 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 return retval;
1508}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001509
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001510#if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001511/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001512 * Call vimL function "func" and return the result as a number.
1513 * Returns -1 when calling the function fails.
1514 * Uses argv[argc] for the function arguments.
1515 */
1516 long
1517call_func_retnr(func, argc, argv, safe)
1518 char_u *func;
1519 int argc;
1520 char_u **argv;
1521 int safe; /* use the sandbox */
1522{
1523 typval_T rettv;
1524 long retval;
1525
1526 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1527 return -1;
1528
1529 retval = get_tv_number_chk(&rettv, NULL);
1530 clear_tv(&rettv);
1531 return retval;
1532}
1533#endif
1534
1535/*
1536 * Call vimL function "func" and return the result as a list
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001537 * Uses argv[argc] for the function arguments.
1538 */
1539 void *
1540call_func_retlist(func, argc, argv, safe)
1541 char_u *func;
1542 int argc;
1543 char_u **argv;
1544 int safe; /* use the sandbox */
1545{
1546 typval_T rettv;
1547
1548 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1549 return NULL;
1550
1551 if (rettv.v_type != VAR_LIST)
1552 {
1553 clear_tv(&rettv);
1554 return NULL;
1555 }
1556
1557 return rettv.vval.v_list;
1558}
1559
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560#endif
1561
1562/*
1563 * Save the current function call pointer, and set it to NULL.
1564 * Used when executing autocommands and for ":source".
1565 */
1566 void *
1567save_funccal()
1568{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001569 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 current_funccal = NULL;
1572 return (void *)fc;
1573}
1574
1575 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001576restore_funccal(vfc)
1577 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001579 funccall_T *fc = (funccall_T *)vfc;
1580
1581 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582}
1583
Bram Moolenaar05159a02005-02-26 23:04:13 +00001584#if defined(FEAT_PROFILE) || defined(PROTO)
1585/*
1586 * Prepare profiling for entering a child or something else that is not
1587 * counted for the script/function itself.
1588 * Should always be called in pair with prof_child_exit().
1589 */
1590 void
1591prof_child_enter(tm)
1592 proftime_T *tm; /* place to store waittime */
1593{
1594 funccall_T *fc = current_funccal;
1595
1596 if (fc != NULL && fc->func->uf_profiling)
1597 profile_start(&fc->prof_child);
1598 script_prof_save(tm);
1599}
1600
1601/*
1602 * Take care of time spent in a child.
1603 * Should always be called after prof_child_enter().
1604 */
1605 void
1606prof_child_exit(tm)
1607 proftime_T *tm; /* where waittime was stored */
1608{
1609 funccall_T *fc = current_funccal;
1610
1611 if (fc != NULL && fc->func->uf_profiling)
1612 {
1613 profile_end(&fc->prof_child);
1614 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1615 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1616 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1617 }
1618 script_prof_restore(tm);
1619}
1620#endif
1621
1622
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623#ifdef FEAT_FOLDING
1624/*
1625 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1626 * it in "*cp". Doesn't give error messages.
1627 */
1628 int
1629eval_foldexpr(arg, cp)
1630 char_u *arg;
1631 int *cp;
1632{
Bram Moolenaar33570922005-01-25 22:26:29 +00001633 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 int retval;
1635 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001636 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1637 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638
1639 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001640 if (use_sandbox)
1641 ++sandbox;
1642 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001644 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 retval = 0;
1646 else
1647 {
1648 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001649 if (tv.v_type == VAR_NUMBER)
1650 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001651 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 retval = 0;
1653 else
1654 {
1655 /* If the result is a string, check if there is a non-digit before
1656 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001657 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 if (!VIM_ISDIGIT(*s) && *s != '-')
1659 *cp = *s++;
1660 retval = atol((char *)s);
1661 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001662 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 }
1664 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001665 if (use_sandbox)
1666 --sandbox;
1667 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668
1669 return retval;
1670}
1671#endif
1672
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001674 * ":let" list all variable values
1675 * ":let var1 var2" list variable values
1676 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001677 * ":let var += expr" assignment command.
1678 * ":let var -= expr" assignment command.
1679 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001680 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 */
1682 void
1683ex_let(eap)
1684 exarg_T *eap;
1685{
1686 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001687 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001688 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001690 int var_count = 0;
1691 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001692 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001693 char_u *argend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694
Bram Moolenaardb552d602006-03-23 22:59:57 +00001695 argend = skip_var_list(arg, &var_count, &semicolon);
1696 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001697 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001698 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1699 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001700 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001701 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001703 /*
1704 * ":let" without "=": list variables
1705 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001706 if (*arg == '[')
1707 EMSG(_(e_invarg));
1708 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001709 /* ":let var1 var2" */
1710 arg = list_arg_vars(eap, arg);
1711 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001712 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001713 /* ":let" */
Bram Moolenaara7043832005-01-21 11:56:39 +00001714 list_glob_vars();
1715 list_buf_vars();
1716 list_win_vars();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001717#ifdef FEAT_WINDOWS
1718 list_tab_vars();
1719#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001720 list_script_vars();
1721 list_func_vars();
Bram Moolenaara7043832005-01-21 11:56:39 +00001722 list_vim_vars();
1723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 eap->nextcmd = check_nextcmd(arg);
1725 }
1726 else
1727 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001728 op[0] = '=';
1729 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001730 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001731 {
1732 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1733 op[0] = expr[-1]; /* +=, -= or .= */
1734 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001735 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001736
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 if (eap->skip)
1738 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001739 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 if (eap->skip)
1741 {
1742 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001743 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 --emsg_skip;
1745 }
1746 else if (i != FAIL)
1747 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001748 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001749 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001750 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 }
1752 }
1753}
1754
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001755/*
1756 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1757 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001758 * When "nextchars" is not NULL it points to a string with characters that
1759 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1760 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001761 * Returns OK or FAIL;
1762 */
1763 static int
1764ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1765 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001766 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001767 int copy; /* copy values from "tv", don't move */
1768 int semicolon; /* from skip_var_list() */
1769 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001770 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001771{
1772 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001773 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001774 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001775 listitem_T *item;
1776 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001777
1778 if (*arg != '[')
1779 {
1780 /*
1781 * ":let var = expr" or ":for var in list"
1782 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001783 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001784 return FAIL;
1785 return OK;
1786 }
1787
1788 /*
1789 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1790 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001791 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001792 {
1793 EMSG(_(e_listreq));
1794 return FAIL;
1795 }
1796
1797 i = list_len(l);
1798 if (semicolon == 0 && var_count < i)
1799 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001800 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001801 return FAIL;
1802 }
1803 if (var_count - semicolon > i)
1804 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001805 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001806 return FAIL;
1807 }
1808
1809 item = l->lv_first;
1810 while (*arg != ']')
1811 {
1812 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001813 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001814 item = item->li_next;
1815 if (arg == NULL)
1816 return FAIL;
1817
1818 arg = skipwhite(arg);
1819 if (*arg == ';')
1820 {
1821 /* Put the rest of the list (may be empty) in the var after ';'.
1822 * Create a new list for this. */
1823 l = list_alloc();
1824 if (l == NULL)
1825 return FAIL;
1826 while (item != NULL)
1827 {
1828 list_append_tv(l, &item->li_tv);
1829 item = item->li_next;
1830 }
1831
1832 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001833 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001834 ltv.vval.v_list = l;
1835 l->lv_refcount = 1;
1836
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001837 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1838 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001839 clear_tv(&ltv);
1840 if (arg == NULL)
1841 return FAIL;
1842 break;
1843 }
1844 else if (*arg != ',' && *arg != ']')
1845 {
1846 EMSG2(_(e_intern2), "ex_let_vars()");
1847 return FAIL;
1848 }
1849 }
1850
1851 return OK;
1852}
1853
1854/*
1855 * Skip over assignable variable "var" or list of variables "[var, var]".
1856 * Used for ":let varvar = expr" and ":for varvar in expr".
1857 * For "[var, var]" increment "*var_count" for each variable.
1858 * for "[var, var; var]" set "semicolon".
1859 * Return NULL for an error.
1860 */
1861 static char_u *
1862skip_var_list(arg, var_count, semicolon)
1863 char_u *arg;
1864 int *var_count;
1865 int *semicolon;
1866{
1867 char_u *p, *s;
1868
1869 if (*arg == '[')
1870 {
1871 /* "[var, var]": find the matching ']'. */
1872 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001873 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001874 {
1875 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1876 s = skip_var_one(p);
1877 if (s == p)
1878 {
1879 EMSG2(_(e_invarg2), p);
1880 return NULL;
1881 }
1882 ++*var_count;
1883
1884 p = skipwhite(s);
1885 if (*p == ']')
1886 break;
1887 else if (*p == ';')
1888 {
1889 if (*semicolon == 1)
1890 {
1891 EMSG(_("Double ; in list of variables"));
1892 return NULL;
1893 }
1894 *semicolon = 1;
1895 }
1896 else if (*p != ',')
1897 {
1898 EMSG2(_(e_invarg2), p);
1899 return NULL;
1900 }
1901 }
1902 return p + 1;
1903 }
1904 else
1905 return skip_var_one(arg);
1906}
1907
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001908/*
Bram Moolenaar92124a32005-06-17 22:03:40 +00001909 * Skip one (assignable) variable name, includig @r, $VAR, &option, d.key,
1910 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001911 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001912 static char_u *
1913skip_var_one(arg)
1914 char_u *arg;
1915{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001916 if (*arg == '@' && arg[1] != NUL)
1917 return arg + 2;
1918 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1919 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001920}
1921
Bram Moolenaara7043832005-01-21 11:56:39 +00001922/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001923 * List variables for hashtab "ht" with prefix "prefix".
1924 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001925 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001926 static void
Bram Moolenaar33570922005-01-25 22:26:29 +00001927list_hashtable_vars(ht, prefix, empty)
1928 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001929 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001930 int empty;
Bram Moolenaara7043832005-01-21 11:56:39 +00001931{
Bram Moolenaar33570922005-01-25 22:26:29 +00001932 hashitem_T *hi;
1933 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001934 int todo;
1935
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001936 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001937 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1938 {
1939 if (!HASHITEM_EMPTY(hi))
1940 {
1941 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001942 di = HI2DI(hi);
1943 if (empty || di->di_tv.v_type != VAR_STRING
1944 || di->di_tv.vval.v_string != NULL)
1945 list_one_var(di, prefix);
Bram Moolenaara7043832005-01-21 11:56:39 +00001946 }
1947 }
1948}
1949
1950/*
1951 * List global variables.
1952 */
1953 static void
1954list_glob_vars()
1955{
Bram Moolenaar33570922005-01-25 22:26:29 +00001956 list_hashtable_vars(&globvarht, (char_u *)"", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001957}
1958
1959/*
1960 * List buffer variables.
1961 */
1962 static void
1963list_buf_vars()
1964{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001965 char_u numbuf[NUMBUFLEN];
1966
Bram Moolenaar33570922005-01-25 22:26:29 +00001967 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001968
1969 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
1970 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER, numbuf);
Bram Moolenaara7043832005-01-21 11:56:39 +00001971}
1972
1973/*
1974 * List window variables.
1975 */
1976 static void
1977list_win_vars()
1978{
Bram Moolenaar33570922005-01-25 22:26:29 +00001979 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001980}
1981
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001982#ifdef FEAT_WINDOWS
1983/*
1984 * List tab page variables.
1985 */
1986 static void
1987list_tab_vars()
1988{
1989 list_hashtable_vars(&curtab->tp_vars.dv_hashtab, (char_u *)"t:", TRUE);
1990}
1991#endif
1992
Bram Moolenaara7043832005-01-21 11:56:39 +00001993/*
1994 * List Vim variables.
1995 */
1996 static void
1997list_vim_vars()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001998{
Bram Moolenaar33570922005-01-25 22:26:29 +00001999 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002000}
2001
2002/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002003 * List script-local variables, if there is a script.
2004 */
2005 static void
2006list_script_vars()
2007{
2008 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
2009 list_hashtable_vars(&SCRIPT_VARS(current_SID), (char_u *)"s:", FALSE);
2010}
2011
2012/*
2013 * List function variables, if there is a function.
2014 */
2015 static void
2016list_func_vars()
2017{
2018 if (current_funccal != NULL)
2019 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
2020 (char_u *)"l:", FALSE);
2021}
2022
2023/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002024 * List variables in "arg".
2025 */
2026 static char_u *
2027list_arg_vars(eap, arg)
2028 exarg_T *eap;
2029 char_u *arg;
2030{
2031 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002032 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002033 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002034 char_u *name_start;
2035 char_u *arg_subsc;
2036 char_u *tofree;
2037 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002038
2039 while (!ends_excmd(*arg) && !got_int)
2040 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002041 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002042 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002043 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002044 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2045 {
2046 emsg_severe = TRUE;
2047 EMSG(_(e_trailing));
2048 break;
2049 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002050 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002051 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002052 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002053 /* get_name_len() takes care of expanding curly braces */
2054 name_start = name = arg;
2055 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2056 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002057 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002058 /* This is mainly to keep test 49 working: when expanding
2059 * curly braces fails overrule the exception error message. */
2060 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002061 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002062 emsg_severe = TRUE;
2063 EMSG2(_(e_invarg2), arg);
2064 break;
2065 }
2066 error = TRUE;
2067 }
2068 else
2069 {
2070 if (tofree != NULL)
2071 name = tofree;
2072 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002073 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002074 else
2075 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002076 /* handle d.key, l[idx], f(expr) */
2077 arg_subsc = arg;
2078 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002079 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002080 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002081 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002082 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002083 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002084 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002085 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002086 case 'g': list_glob_vars(); break;
2087 case 'b': list_buf_vars(); break;
2088 case 'w': list_win_vars(); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002089#ifdef FEAT_WINDOWS
2090 case 't': list_tab_vars(); break;
2091#endif
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002092 case 'v': list_vim_vars(); break;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002093 case 's': list_script_vars(); break;
2094 case 'l': list_func_vars(); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002095 default:
2096 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002097 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002098 }
2099 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002100 {
2101 char_u numbuf[NUMBUFLEN];
2102 char_u *tf;
2103 int c;
2104 char_u *s;
2105
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002106 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002107 c = *arg;
2108 *arg = NUL;
2109 list_one_var_a((char_u *)"",
2110 arg == arg_subsc ? name : name_start,
2111 tv.v_type, s == NULL ? (char_u *)"" : s);
2112 *arg = c;
2113 vim_free(tf);
2114 }
2115 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002116 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002117 }
2118 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002119
2120 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002121 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002122
2123 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002124 }
2125
2126 return arg;
2127}
2128
2129/*
2130 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2131 * Returns a pointer to the char just after the var name.
2132 * Returns NULL if there is an error.
2133 */
2134 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002135ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002136 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002137 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002138 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002139 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002140 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002141{
2142 int c1;
2143 char_u *name;
2144 char_u *p;
2145 char_u *arg_end = NULL;
2146 int len;
2147 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002148 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002149
2150 /*
2151 * ":let $VAR = expr": Set environment variable.
2152 */
2153 if (*arg == '$')
2154 {
2155 /* Find the end of the name. */
2156 ++arg;
2157 name = arg;
2158 len = get_env_len(&arg);
2159 if (len == 0)
2160 EMSG2(_(e_invarg2), name - 1);
2161 else
2162 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002163 if (op != NULL && (*op == '+' || *op == '-'))
2164 EMSG2(_(e_letwrong), op);
2165 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002166 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002167 EMSG(_(e_letunexp));
2168 else
2169 {
2170 c1 = name[len];
2171 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002172 p = get_tv_string_chk(tv);
2173 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002174 {
2175 int mustfree = FALSE;
2176 char_u *s = vim_getenv(name, &mustfree);
2177
2178 if (s != NULL)
2179 {
2180 p = tofree = concat_str(s, p);
2181 if (mustfree)
2182 vim_free(s);
2183 }
2184 }
2185 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002186 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002187 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002188 if (STRICMP(name, "HOME") == 0)
2189 init_homedir();
2190 else if (didset_vim && STRICMP(name, "VIM") == 0)
2191 didset_vim = FALSE;
2192 else if (didset_vimruntime
2193 && STRICMP(name, "VIMRUNTIME") == 0)
2194 didset_vimruntime = FALSE;
2195 arg_end = arg;
2196 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002197 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002198 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002199 }
2200 }
2201 }
2202
2203 /*
2204 * ":let &option = expr": Set option value.
2205 * ":let &l:option = expr": Set local option value.
2206 * ":let &g:option = expr": Set global option value.
2207 */
2208 else if (*arg == '&')
2209 {
2210 /* Find the end of the name. */
2211 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002212 if (p == NULL || (endchars != NULL
2213 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002214 EMSG(_(e_letunexp));
2215 else
2216 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002217 long n;
2218 int opt_type;
2219 long numval;
2220 char_u *stringval = NULL;
2221 char_u *s;
2222
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002223 c1 = *p;
2224 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002225
2226 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002227 s = get_tv_string_chk(tv); /* != NULL if number or string */
2228 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002229 {
2230 opt_type = get_option_value(arg, &numval,
2231 &stringval, opt_flags);
2232 if ((opt_type == 1 && *op == '.')
2233 || (opt_type == 0 && *op != '.'))
2234 EMSG2(_(e_letwrong), op);
2235 else
2236 {
2237 if (opt_type == 1) /* number */
2238 {
2239 if (*op == '+')
2240 n = numval + n;
2241 else
2242 n = numval - n;
2243 }
2244 else if (opt_type == 0 && stringval != NULL) /* string */
2245 {
2246 s = concat_str(stringval, s);
2247 vim_free(stringval);
2248 stringval = s;
2249 }
2250 }
2251 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002252 if (s != NULL)
2253 {
2254 set_option_value(arg, n, s, opt_flags);
2255 arg_end = p;
2256 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002257 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002258 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002259 }
2260 }
2261
2262 /*
2263 * ":let @r = expr": Set register contents.
2264 */
2265 else if (*arg == '@')
2266 {
2267 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002268 if (op != NULL && (*op == '+' || *op == '-'))
2269 EMSG2(_(e_letwrong), op);
2270 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002271 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002272 EMSG(_(e_letunexp));
2273 else
2274 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002275 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002276 char_u *s;
2277
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002278 p = get_tv_string_chk(tv);
2279 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002280 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002281 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002282 if (s != NULL)
2283 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002284 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002285 vim_free(s);
2286 }
2287 }
2288 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002289 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002290 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002291 arg_end = arg + 1;
2292 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002293 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002294 }
2295 }
2296
2297 /*
2298 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002299 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002300 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002301 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002302 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002303 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002304
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002305 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002306 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002307 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002308 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2309 EMSG(_(e_letunexp));
2310 else
2311 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002312 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002313 arg_end = p;
2314 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002315 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002316 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002317 }
2318
2319 else
2320 EMSG2(_(e_invarg2), arg);
2321
2322 return arg_end;
2323}
2324
2325/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002326 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2327 */
2328 static int
2329check_changedtick(arg)
2330 char_u *arg;
2331{
2332 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2333 {
2334 EMSG2(_(e_readonlyvar), arg);
2335 return TRUE;
2336 }
2337 return FALSE;
2338}
2339
2340/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002341 * Get an lval: variable, Dict item or List item that can be assigned a value
2342 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2343 * "name.key", "name.key[expr]" etc.
2344 * Indexing only works if "name" is an existing List or Dictionary.
2345 * "name" points to the start of the name.
2346 * If "rettv" is not NULL it points to the value to be assigned.
2347 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2348 * wrong; must end in space or cmd separator.
2349 *
2350 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002351 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002352 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002353 */
2354 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002355get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002356 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002357 typval_T *rettv;
2358 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002359 int unlet;
2360 int skip;
2361 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002362 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002363{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002364 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002365 char_u *expr_start, *expr_end;
2366 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002367 dictitem_T *v;
2368 typval_T var1;
2369 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002370 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002371 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002372 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002373 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002374 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002375
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002376 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002377 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002378
2379 if (skip)
2380 {
2381 /* When skipping just find the end of the name. */
2382 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002383 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002384 }
2385
2386 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002387 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002388 if (expr_start != NULL)
2389 {
2390 /* Don't expand the name when we already know there is an error. */
2391 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2392 && *p != '[' && *p != '.')
2393 {
2394 EMSG(_(e_trailing));
2395 return NULL;
2396 }
2397
2398 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2399 if (lp->ll_exp_name == NULL)
2400 {
2401 /* Report an invalid expression in braces, unless the
2402 * expression evaluation has been cancelled due to an
2403 * aborting error, an interrupt, or an exception. */
2404 if (!aborting() && !quiet)
2405 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002406 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002407 EMSG2(_(e_invarg2), name);
2408 return NULL;
2409 }
2410 }
2411 lp->ll_name = lp->ll_exp_name;
2412 }
2413 else
2414 lp->ll_name = name;
2415
2416 /* Without [idx] or .key we are done. */
2417 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2418 return p;
2419
2420 cc = *p;
2421 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002422 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002423 if (v == NULL && !quiet)
2424 EMSG2(_(e_undefvar), lp->ll_name);
2425 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002426 if (v == NULL)
2427 return NULL;
2428
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002429 /*
2430 * Loop until no more [idx] or .key is following.
2431 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002432 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002433 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002434 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002435 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2436 && !(lp->ll_tv->v_type == VAR_DICT
2437 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002438 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002439 if (!quiet)
2440 EMSG(_("E689: Can only index a List or Dictionary"));
2441 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002442 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002443 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002444 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002445 if (!quiet)
2446 EMSG(_("E708: [:] must come last"));
2447 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002448 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002449
Bram Moolenaar8c711452005-01-14 21:53:12 +00002450 len = -1;
2451 if (*p == '.')
2452 {
2453 key = p + 1;
2454 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2455 ;
2456 if (len == 0)
2457 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002458 if (!quiet)
2459 EMSG(_(e_emptykey));
2460 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002461 }
2462 p = key + len;
2463 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002464 else
2465 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002466 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002467 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002468 if (*p == ':')
2469 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002470 else
2471 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002472 empty1 = FALSE;
2473 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002474 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002475 if (get_tv_string_chk(&var1) == NULL)
2476 {
2477 /* not a number or string */
2478 clear_tv(&var1);
2479 return NULL;
2480 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002481 }
2482
2483 /* Optionally get the second index [ :expr]. */
2484 if (*p == ':')
2485 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002486 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002487 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002488 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002489 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002490 if (!empty1)
2491 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002492 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002493 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002494 if (rettv != NULL && (rettv->v_type != VAR_LIST
2495 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002496 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002497 if (!quiet)
2498 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002499 if (!empty1)
2500 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002501 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002502 }
2503 p = skipwhite(p + 1);
2504 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002505 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002506 else
2507 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002508 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002509 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2510 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002511 if (!empty1)
2512 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002513 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002514 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002515 if (get_tv_string_chk(&var2) == NULL)
2516 {
2517 /* not a number or string */
2518 if (!empty1)
2519 clear_tv(&var1);
2520 clear_tv(&var2);
2521 return NULL;
2522 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002523 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002524 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002525 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002526 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002527 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002528
Bram Moolenaar8c711452005-01-14 21:53:12 +00002529 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002530 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002531 if (!quiet)
2532 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002533 if (!empty1)
2534 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002535 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002536 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002537 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002538 }
2539
2540 /* Skip to past ']'. */
2541 ++p;
2542 }
2543
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002544 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002545 {
2546 if (len == -1)
2547 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002548 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002549 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002550 if (*key == NUL)
2551 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002552 if (!quiet)
2553 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002554 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002555 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002556 }
2557 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002558 lp->ll_list = NULL;
2559 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002560 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002561 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002562 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002563 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002564 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002565 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002566 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002567 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002568 if (len == -1)
2569 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002570 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002571 }
2572 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002573 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002574 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002575 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002576 if (len == -1)
2577 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002578 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002579 p = NULL;
2580 break;
2581 }
2582 if (len == -1)
2583 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002584 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002585 }
2586 else
2587 {
2588 /*
2589 * Get the number and item for the only or first index of the List.
2590 */
2591 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002592 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002593 else
2594 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002595 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002596 clear_tv(&var1);
2597 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002598 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002599 lp->ll_list = lp->ll_tv->vval.v_list;
2600 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2601 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002602 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002603 if (lp->ll_n1 < 0)
2604 {
2605 lp->ll_n1 = 0;
2606 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2607 }
2608 }
2609 if (lp->ll_li == NULL)
2610 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002611 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002612 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002613 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002614 }
2615
2616 /*
2617 * May need to find the item or absolute index for the second
2618 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002619 * When no index given: "lp->ll_empty2" is TRUE.
2620 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002621 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002622 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002623 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002624 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002625 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002626 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002627 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002628 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002629 if (ni == NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002630 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002631 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002632 }
2633
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002634 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2635 if (lp->ll_n1 < 0)
2636 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2637 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002638 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002639 }
2640
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002641 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002642 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002643 }
2644
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002645 return p;
2646}
2647
2648/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002649 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002650 */
2651 static void
2652clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002653 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002654{
2655 vim_free(lp->ll_exp_name);
2656 vim_free(lp->ll_newkey);
2657}
2658
2659/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002660 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002661 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002662 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002663 */
2664 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002665set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002666 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002667 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002668 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002669 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002670 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002671{
2672 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002673 listitem_T *ri;
2674 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002675
2676 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002677 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002678 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002679 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002680 cc = *endp;
2681 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002682 if (op != NULL && *op != '=')
2683 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002684 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002685
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002686 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002687 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002688 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002689 {
2690 if (tv_op(&tv, rettv, op) == OK)
2691 set_var(lp->ll_name, &tv, FALSE);
2692 clear_tv(&tv);
2693 }
2694 }
2695 else
2696 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002697 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002698 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002699 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002700 else if (tv_check_lock(lp->ll_newkey == NULL
2701 ? lp->ll_tv->v_lock
2702 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2703 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002704 else if (lp->ll_range)
2705 {
2706 /*
2707 * Assign the List values to the list items.
2708 */
2709 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002710 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002711 if (op != NULL && *op != '=')
2712 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2713 else
2714 {
2715 clear_tv(&lp->ll_li->li_tv);
2716 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2717 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002718 ri = ri->li_next;
2719 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2720 break;
2721 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002722 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002723 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002724 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002725 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002726 ri = NULL;
2727 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002728 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002729 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002730 lp->ll_li = lp->ll_li->li_next;
2731 ++lp->ll_n1;
2732 }
2733 if (ri != NULL)
2734 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002735 else if (lp->ll_empty2
2736 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002737 : lp->ll_n1 != lp->ll_n2)
2738 EMSG(_("E711: List value has not enough items"));
2739 }
2740 else
2741 {
2742 /*
2743 * Assign to a List or Dictionary item.
2744 */
2745 if (lp->ll_newkey != NULL)
2746 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002747 if (op != NULL && *op != '=')
2748 {
2749 EMSG2(_(e_letwrong), op);
2750 return;
2751 }
2752
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002753 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002754 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002755 if (di == NULL)
2756 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002757 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2758 {
2759 vim_free(di);
2760 return;
2761 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002762 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002763 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002764 else if (op != NULL && *op != '=')
2765 {
2766 tv_op(lp->ll_tv, rettv, op);
2767 return;
2768 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002769 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002770 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002771
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002772 /*
2773 * Assign the value to the variable or list item.
2774 */
2775 if (copy)
2776 copy_tv(rettv, lp->ll_tv);
2777 else
2778 {
2779 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002780 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002781 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002782 }
2783 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002784}
2785
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002786/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002787 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2788 * Returns OK or FAIL.
2789 */
2790 static int
2791tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002792 typval_T *tv1;
2793 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002794 char_u *op;
2795{
2796 long n;
2797 char_u numbuf[NUMBUFLEN];
2798 char_u *s;
2799
2800 /* Can't do anything with a Funcref or a Dict on the right. */
2801 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2802 {
2803 switch (tv1->v_type)
2804 {
2805 case VAR_DICT:
2806 case VAR_FUNC:
2807 break;
2808
2809 case VAR_LIST:
2810 if (*op != '+' || tv2->v_type != VAR_LIST)
2811 break;
2812 /* List += List */
2813 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2814 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2815 return OK;
2816
2817 case VAR_NUMBER:
2818 case VAR_STRING:
2819 if (tv2->v_type == VAR_LIST)
2820 break;
2821 if (*op == '+' || *op == '-')
2822 {
2823 /* nr += nr or nr -= nr*/
2824 n = get_tv_number(tv1);
2825 if (*op == '+')
2826 n += get_tv_number(tv2);
2827 else
2828 n -= get_tv_number(tv2);
2829 clear_tv(tv1);
2830 tv1->v_type = VAR_NUMBER;
2831 tv1->vval.v_number = n;
2832 }
2833 else
2834 {
2835 /* str .= str */
2836 s = get_tv_string(tv1);
2837 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2838 clear_tv(tv1);
2839 tv1->v_type = VAR_STRING;
2840 tv1->vval.v_string = s;
2841 }
2842 return OK;
2843 }
2844 }
2845
2846 EMSG2(_(e_letwrong), op);
2847 return FAIL;
2848}
2849
2850/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002851 * Add a watcher to a list.
2852 */
2853 static void
2854list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002855 list_T *l;
2856 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002857{
2858 lw->lw_next = l->lv_watch;
2859 l->lv_watch = lw;
2860}
2861
2862/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002863 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002864 * No warning when it isn't found...
2865 */
2866 static void
2867list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002868 list_T *l;
2869 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002870{
Bram Moolenaar33570922005-01-25 22:26:29 +00002871 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002872
2873 lwp = &l->lv_watch;
2874 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2875 {
2876 if (lw == lwrem)
2877 {
2878 *lwp = lw->lw_next;
2879 break;
2880 }
2881 lwp = &lw->lw_next;
2882 }
2883}
2884
2885/*
2886 * Just before removing an item from a list: advance watchers to the next
2887 * item.
2888 */
2889 static void
2890list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002891 list_T *l;
2892 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002893{
Bram Moolenaar33570922005-01-25 22:26:29 +00002894 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002895
2896 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2897 if (lw->lw_item == item)
2898 lw->lw_item = item->li_next;
2899}
2900
2901/*
2902 * Evaluate the expression used in a ":for var in expr" command.
2903 * "arg" points to "var".
2904 * Set "*errp" to TRUE for an error, FALSE otherwise;
2905 * Return a pointer that holds the info. Null when there is an error.
2906 */
2907 void *
2908eval_for_line(arg, errp, nextcmdp, skip)
2909 char_u *arg;
2910 int *errp;
2911 char_u **nextcmdp;
2912 int skip;
2913{
Bram Moolenaar33570922005-01-25 22:26:29 +00002914 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002915 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002916 typval_T tv;
2917 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002918
2919 *errp = TRUE; /* default: there is an error */
2920
Bram Moolenaar33570922005-01-25 22:26:29 +00002921 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002922 if (fi == NULL)
2923 return NULL;
2924
2925 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2926 if (expr == NULL)
2927 return fi;
2928
2929 expr = skipwhite(expr);
2930 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2931 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002932 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002933 return fi;
2934 }
2935
2936 if (skip)
2937 ++emsg_skip;
2938 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2939 {
2940 *errp = FALSE;
2941 if (!skip)
2942 {
2943 l = tv.vval.v_list;
2944 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002945 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002946 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002947 clear_tv(&tv);
2948 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002949 else
2950 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002951 /* No need to increment the refcount, it's already set for the
2952 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002953 fi->fi_list = l;
2954 list_add_watch(l, &fi->fi_lw);
2955 fi->fi_lw.lw_item = l->lv_first;
2956 }
2957 }
2958 }
2959 if (skip)
2960 --emsg_skip;
2961
2962 return fi;
2963}
2964
2965/*
2966 * Use the first item in a ":for" list. Advance to the next.
2967 * Assign the values to the variable (list). "arg" points to the first one.
2968 * Return TRUE when a valid item was found, FALSE when at end of list or
2969 * something wrong.
2970 */
2971 int
2972next_for_item(fi_void, arg)
2973 void *fi_void;
2974 char_u *arg;
2975{
Bram Moolenaar33570922005-01-25 22:26:29 +00002976 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002977 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002978 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002979
2980 item = fi->fi_lw.lw_item;
2981 if (item == NULL)
2982 result = FALSE;
2983 else
2984 {
2985 fi->fi_lw.lw_item = item->li_next;
2986 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2987 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2988 }
2989 return result;
2990}
2991
2992/*
2993 * Free the structure used to store info used by ":for".
2994 */
2995 void
2996free_for_info(fi_void)
2997 void *fi_void;
2998{
Bram Moolenaar33570922005-01-25 22:26:29 +00002999 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003000
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003001 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003002 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003003 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003004 list_unref(fi->fi_list);
3005 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003006 vim_free(fi);
3007}
3008
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3010
3011 void
3012set_context_for_expression(xp, arg, cmdidx)
3013 expand_T *xp;
3014 char_u *arg;
3015 cmdidx_T cmdidx;
3016{
3017 int got_eq = FALSE;
3018 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003019 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003021 if (cmdidx == CMD_let)
3022 {
3023 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003024 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003025 {
3026 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003027 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003028 {
3029 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003030 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003031 if (vim_iswhite(*p))
3032 break;
3033 }
3034 return;
3035 }
3036 }
3037 else
3038 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3039 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 while ((xp->xp_pattern = vim_strpbrk(arg,
3041 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3042 {
3043 c = *xp->xp_pattern;
3044 if (c == '&')
3045 {
3046 c = xp->xp_pattern[1];
3047 if (c == '&')
3048 {
3049 ++xp->xp_pattern;
3050 xp->xp_context = cmdidx != CMD_let || got_eq
3051 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3052 }
3053 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003054 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003056 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3057 xp->xp_pattern += 2;
3058
3059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 }
3061 else if (c == '$')
3062 {
3063 /* environment variable */
3064 xp->xp_context = EXPAND_ENV_VARS;
3065 }
3066 else if (c == '=')
3067 {
3068 got_eq = TRUE;
3069 xp->xp_context = EXPAND_EXPRESSION;
3070 }
3071 else if (c == '<'
3072 && xp->xp_context == EXPAND_FUNCTIONS
3073 && vim_strchr(xp->xp_pattern, '(') == NULL)
3074 {
3075 /* Function name can start with "<SNR>" */
3076 break;
3077 }
3078 else if (cmdidx != CMD_let || got_eq)
3079 {
3080 if (c == '"') /* string */
3081 {
3082 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3083 if (c == '\\' && xp->xp_pattern[1] != NUL)
3084 ++xp->xp_pattern;
3085 xp->xp_context = EXPAND_NOTHING;
3086 }
3087 else if (c == '\'') /* literal string */
3088 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003089 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3091 /* skip */ ;
3092 xp->xp_context = EXPAND_NOTHING;
3093 }
3094 else if (c == '|')
3095 {
3096 if (xp->xp_pattern[1] == '|')
3097 {
3098 ++xp->xp_pattern;
3099 xp->xp_context = EXPAND_EXPRESSION;
3100 }
3101 else
3102 xp->xp_context = EXPAND_COMMANDS;
3103 }
3104 else
3105 xp->xp_context = EXPAND_EXPRESSION;
3106 }
3107 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003108 /* Doesn't look like something valid, expand as an expression
3109 * anyway. */
3110 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 arg = xp->xp_pattern;
3112 if (*arg != NUL)
3113 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3114 /* skip */ ;
3115 }
3116 xp->xp_pattern = arg;
3117}
3118
3119#endif /* FEAT_CMDL_COMPL */
3120
3121/*
3122 * ":1,25call func(arg1, arg2)" function call.
3123 */
3124 void
3125ex_call(eap)
3126 exarg_T *eap;
3127{
3128 char_u *arg = eap->arg;
3129 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003131 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003133 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 linenr_T lnum;
3135 int doesrange;
3136 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003137 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003139 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003140 if (fudi.fd_newkey != NULL)
3141 {
3142 /* Still need to give an error message for missing key. */
3143 EMSG2(_(e_dictkey), fudi.fd_newkey);
3144 vim_free(fudi.fd_newkey);
3145 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003146 if (tofree == NULL)
3147 return;
3148
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003149 /* Increase refcount on dictionary, it could get deleted when evaluating
3150 * the arguments. */
3151 if (fudi.fd_dict != NULL)
3152 ++fudi.fd_dict->dv_refcount;
3153
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003154 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003155 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003156 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157
Bram Moolenaar532c7802005-01-27 14:44:31 +00003158 /* Skip white space to allow ":call func ()". Not good, but required for
3159 * backward compatibility. */
3160 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003161 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162
3163 if (*startarg != '(')
3164 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003165 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 goto end;
3167 }
3168
3169 /*
3170 * When skipping, evaluate the function once, to find the end of the
3171 * arguments.
3172 * When the function takes a range, this is discovered after the first
3173 * call, and the loop is broken.
3174 */
3175 if (eap->skip)
3176 {
3177 ++emsg_skip;
3178 lnum = eap->line2; /* do it once, also with an invalid range */
3179 }
3180 else
3181 lnum = eap->line1;
3182 for ( ; lnum <= eap->line2; ++lnum)
3183 {
3184 if (!eap->skip && eap->addr_count > 0)
3185 {
3186 curwin->w_cursor.lnum = lnum;
3187 curwin->w_cursor.col = 0;
3188 }
3189 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003190 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003191 eap->line1, eap->line2, &doesrange,
3192 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 {
3194 failed = TRUE;
3195 break;
3196 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003197
3198 /* Handle a function returning a Funcref, Dictionary or List. */
3199 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3200 {
3201 failed = TRUE;
3202 break;
3203 }
3204
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003205 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 if (doesrange || eap->skip)
3207 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003208
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003210 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003211 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003212 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 if (aborting())
3214 break;
3215 }
3216 if (eap->skip)
3217 --emsg_skip;
3218
3219 if (!failed)
3220 {
3221 /* Check for trailing illegal characters and a following command. */
3222 if (!ends_excmd(*arg))
3223 {
3224 emsg_severe = TRUE;
3225 EMSG(_(e_trailing));
3226 }
3227 else
3228 eap->nextcmd = check_nextcmd(arg);
3229 }
3230
3231end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003232 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003233 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234}
3235
3236/*
3237 * ":unlet[!] var1 ... " command.
3238 */
3239 void
3240ex_unlet(eap)
3241 exarg_T *eap;
3242{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003243 ex_unletlock(eap, eap->arg, 0);
3244}
3245
3246/*
3247 * ":lockvar" and ":unlockvar" commands
3248 */
3249 void
3250ex_lockvar(eap)
3251 exarg_T *eap;
3252{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003254 int deep = 2;
3255
3256 if (eap->forceit)
3257 deep = -1;
3258 else if (vim_isdigit(*arg))
3259 {
3260 deep = getdigits(&arg);
3261 arg = skipwhite(arg);
3262 }
3263
3264 ex_unletlock(eap, arg, deep);
3265}
3266
3267/*
3268 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3269 */
3270 static void
3271ex_unletlock(eap, argstart, deep)
3272 exarg_T *eap;
3273 char_u *argstart;
3274 int deep;
3275{
3276 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003279 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280
3281 do
3282 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003283 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003284 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3285 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003286 if (lv.ll_name == NULL)
3287 error = TRUE; /* error but continue parsing */
3288 if (name_end == NULL || (!vim_iswhite(*name_end)
3289 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003291 if (name_end != NULL)
3292 {
3293 emsg_severe = TRUE;
3294 EMSG(_(e_trailing));
3295 }
3296 if (!(eap->skip || error))
3297 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 break;
3299 }
3300
3301 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003302 {
3303 if (eap->cmdidx == CMD_unlet)
3304 {
3305 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3306 error = TRUE;
3307 }
3308 else
3309 {
3310 if (do_lock_var(&lv, name_end, deep,
3311 eap->cmdidx == CMD_lockvar) == FAIL)
3312 error = TRUE;
3313 }
3314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003316 if (!eap->skip)
3317 clear_lval(&lv);
3318
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 arg = skipwhite(name_end);
3320 } while (!ends_excmd(*arg));
3321
3322 eap->nextcmd = check_nextcmd(arg);
3323}
3324
Bram Moolenaar8c711452005-01-14 21:53:12 +00003325 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003326do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003327 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003328 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003329 int forceit;
3330{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003331 int ret = OK;
3332 int cc;
3333
3334 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003335 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003336 cc = *name_end;
3337 *name_end = NUL;
3338
3339 /* Normal name or expanded name. */
3340 if (check_changedtick(lp->ll_name))
3341 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003342 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003343 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003344 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003345 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003346 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3347 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003348 else if (lp->ll_range)
3349 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003350 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003351
3352 /* Delete a range of List items. */
3353 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3354 {
3355 li = lp->ll_li->li_next;
3356 listitem_remove(lp->ll_list, lp->ll_li);
3357 lp->ll_li = li;
3358 ++lp->ll_n1;
3359 }
3360 }
3361 else
3362 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003363 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003364 /* unlet a List item. */
3365 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003366 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003367 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003368 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003369 }
3370
3371 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003372}
3373
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374/*
3375 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003376 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 */
3378 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003379do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003381 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382{
Bram Moolenaar33570922005-01-25 22:26:29 +00003383 hashtab_T *ht;
3384 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003385 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386
Bram Moolenaar33570922005-01-25 22:26:29 +00003387 ht = find_var_ht(name, &varname);
3388 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003390 hi = hash_find(ht, varname);
3391 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003392 {
Bram Moolenaar4e957af2006-09-02 11:41:07 +00003393 if (var_check_fixed(HI2DI(hi)->di_flags, name))
3394 return FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003395 if (var_check_ro(HI2DI(hi)->di_flags, name))
3396 return FAIL;
3397 delete_var(ht, hi);
3398 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003399 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003401 if (forceit)
3402 return OK;
3403 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 return FAIL;
3405}
3406
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003407/*
3408 * Lock or unlock variable indicated by "lp".
3409 * "deep" is the levels to go (-1 for unlimited);
3410 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3411 */
3412 static int
3413do_lock_var(lp, name_end, deep, lock)
3414 lval_T *lp;
3415 char_u *name_end;
3416 int deep;
3417 int lock;
3418{
3419 int ret = OK;
3420 int cc;
3421 dictitem_T *di;
3422
3423 if (deep == 0) /* nothing to do */
3424 return OK;
3425
3426 if (lp->ll_tv == NULL)
3427 {
3428 cc = *name_end;
3429 *name_end = NUL;
3430
3431 /* Normal name or expanded name. */
3432 if (check_changedtick(lp->ll_name))
3433 ret = FAIL;
3434 else
3435 {
3436 di = find_var(lp->ll_name, NULL);
3437 if (di == NULL)
3438 ret = FAIL;
3439 else
3440 {
3441 if (lock)
3442 di->di_flags |= DI_FLAGS_LOCK;
3443 else
3444 di->di_flags &= ~DI_FLAGS_LOCK;
3445 item_lock(&di->di_tv, deep, lock);
3446 }
3447 }
3448 *name_end = cc;
3449 }
3450 else if (lp->ll_range)
3451 {
3452 listitem_T *li = lp->ll_li;
3453
3454 /* (un)lock a range of List items. */
3455 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3456 {
3457 item_lock(&li->li_tv, deep, lock);
3458 li = li->li_next;
3459 ++lp->ll_n1;
3460 }
3461 }
3462 else if (lp->ll_list != NULL)
3463 /* (un)lock a List item. */
3464 item_lock(&lp->ll_li->li_tv, deep, lock);
3465 else
3466 /* un(lock) a Dictionary item. */
3467 item_lock(&lp->ll_di->di_tv, deep, lock);
3468
3469 return ret;
3470}
3471
3472/*
3473 * Lock or unlock an item. "deep" is nr of levels to go.
3474 */
3475 static void
3476item_lock(tv, deep, lock)
3477 typval_T *tv;
3478 int deep;
3479 int lock;
3480{
3481 static int recurse = 0;
3482 list_T *l;
3483 listitem_T *li;
3484 dict_T *d;
3485 hashitem_T *hi;
3486 int todo;
3487
3488 if (recurse >= DICT_MAXNEST)
3489 {
3490 EMSG(_("E743: variable nested too deep for (un)lock"));
3491 return;
3492 }
3493 if (deep == 0)
3494 return;
3495 ++recurse;
3496
3497 /* lock/unlock the item itself */
3498 if (lock)
3499 tv->v_lock |= VAR_LOCKED;
3500 else
3501 tv->v_lock &= ~VAR_LOCKED;
3502
3503 switch (tv->v_type)
3504 {
3505 case VAR_LIST:
3506 if ((l = tv->vval.v_list) != NULL)
3507 {
3508 if (lock)
3509 l->lv_lock |= VAR_LOCKED;
3510 else
3511 l->lv_lock &= ~VAR_LOCKED;
3512 if (deep < 0 || deep > 1)
3513 /* recursive: lock/unlock the items the List contains */
3514 for (li = l->lv_first; li != NULL; li = li->li_next)
3515 item_lock(&li->li_tv, deep - 1, lock);
3516 }
3517 break;
3518 case VAR_DICT:
3519 if ((d = tv->vval.v_dict) != NULL)
3520 {
3521 if (lock)
3522 d->dv_lock |= VAR_LOCKED;
3523 else
3524 d->dv_lock &= ~VAR_LOCKED;
3525 if (deep < 0 || deep > 1)
3526 {
3527 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003528 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003529 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3530 {
3531 if (!HASHITEM_EMPTY(hi))
3532 {
3533 --todo;
3534 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3535 }
3536 }
3537 }
3538 }
3539 }
3540 --recurse;
3541}
3542
Bram Moolenaara40058a2005-07-11 22:42:07 +00003543/*
3544 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3545 * it refers to a List or Dictionary that is locked.
3546 */
3547 static int
3548tv_islocked(tv)
3549 typval_T *tv;
3550{
3551 return (tv->v_lock & VAR_LOCKED)
3552 || (tv->v_type == VAR_LIST
3553 && tv->vval.v_list != NULL
3554 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3555 || (tv->v_type == VAR_DICT
3556 && tv->vval.v_dict != NULL
3557 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3558}
3559
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3561/*
3562 * Delete all "menutrans_" variables.
3563 */
3564 void
3565del_menutrans_vars()
3566{
Bram Moolenaar33570922005-01-25 22:26:29 +00003567 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003568 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569
Bram Moolenaar33570922005-01-25 22:26:29 +00003570 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003571 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003572 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003573 {
3574 if (!HASHITEM_EMPTY(hi))
3575 {
3576 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003577 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3578 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003579 }
3580 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003581 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582}
3583#endif
3584
3585#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3586
3587/*
3588 * Local string buffer for the next two functions to store a variable name
3589 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3590 * get_user_var_name().
3591 */
3592
3593static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3594
3595static char_u *varnamebuf = NULL;
3596static int varnamebuflen = 0;
3597
3598/*
3599 * Function to concatenate a prefix and a variable name.
3600 */
3601 static char_u *
3602cat_prefix_varname(prefix, name)
3603 int prefix;
3604 char_u *name;
3605{
3606 int len;
3607
3608 len = (int)STRLEN(name) + 3;
3609 if (len > varnamebuflen)
3610 {
3611 vim_free(varnamebuf);
3612 len += 10; /* some additional space */
3613 varnamebuf = alloc(len);
3614 if (varnamebuf == NULL)
3615 {
3616 varnamebuflen = 0;
3617 return NULL;
3618 }
3619 varnamebuflen = len;
3620 }
3621 *varnamebuf = prefix;
3622 varnamebuf[1] = ':';
3623 STRCPY(varnamebuf + 2, name);
3624 return varnamebuf;
3625}
3626
3627/*
3628 * Function given to ExpandGeneric() to obtain the list of user defined
3629 * (global/buffer/window/built-in) variable names.
3630 */
3631/*ARGSUSED*/
3632 char_u *
3633get_user_var_name(xp, idx)
3634 expand_T *xp;
3635 int idx;
3636{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003637 static long_u gdone;
3638 static long_u bdone;
3639 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003640#ifdef FEAT_WINDOWS
3641 static long_u tdone;
3642#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003643 static int vidx;
3644 static hashitem_T *hi;
3645 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646
3647 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003648 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003649 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003650#ifdef FEAT_WINDOWS
3651 tdone = 0;
3652#endif
3653 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003654
3655 /* Global variables */
3656 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003658 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003659 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003660 else
3661 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003662 while (HASHITEM_EMPTY(hi))
3663 ++hi;
3664 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3665 return cat_prefix_varname('g', hi->hi_key);
3666 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003668
3669 /* b: variables */
3670 ht = &curbuf->b_vars.dv_hashtab;
3671 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003673 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003674 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003675 else
3676 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003677 while (HASHITEM_EMPTY(hi))
3678 ++hi;
3679 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003681 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003683 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 return (char_u *)"b:changedtick";
3685 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003686
3687 /* w: variables */
3688 ht = &curwin->w_vars.dv_hashtab;
3689 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003691 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003692 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003693 else
3694 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003695 while (HASHITEM_EMPTY(hi))
3696 ++hi;
3697 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003699
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003700#ifdef FEAT_WINDOWS
3701 /* t: variables */
3702 ht = &curtab->tp_vars.dv_hashtab;
3703 if (tdone < ht->ht_used)
3704 {
3705 if (tdone++ == 0)
3706 hi = ht->ht_array;
3707 else
3708 ++hi;
3709 while (HASHITEM_EMPTY(hi))
3710 ++hi;
3711 return cat_prefix_varname('t', hi->hi_key);
3712 }
3713#endif
3714
Bram Moolenaar33570922005-01-25 22:26:29 +00003715 /* v: variables */
3716 if (vidx < VV_LEN)
3717 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718
3719 vim_free(varnamebuf);
3720 varnamebuf = NULL;
3721 varnamebuflen = 0;
3722 return NULL;
3723}
3724
3725#endif /* FEAT_CMDL_COMPL */
3726
3727/*
3728 * types for expressions.
3729 */
3730typedef enum
3731{
3732 TYPE_UNKNOWN = 0
3733 , TYPE_EQUAL /* == */
3734 , TYPE_NEQUAL /* != */
3735 , TYPE_GREATER /* > */
3736 , TYPE_GEQUAL /* >= */
3737 , TYPE_SMALLER /* < */
3738 , TYPE_SEQUAL /* <= */
3739 , TYPE_MATCH /* =~ */
3740 , TYPE_NOMATCH /* !~ */
3741} exptype_T;
3742
3743/*
3744 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003745 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3747 */
3748
3749/*
3750 * Handle zero level expression.
3751 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003752 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003753 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 * Return OK or FAIL.
3755 */
3756 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003757eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003759 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 char_u **nextcmd;
3761 int evaluate;
3762{
3763 int ret;
3764 char_u *p;
3765
3766 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003767 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 if (ret == FAIL || !ends_excmd(*p))
3769 {
3770 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003771 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 /*
3773 * Report the invalid expression unless the expression evaluation has
3774 * been cancelled due to an aborting error, an interrupt, or an
3775 * exception.
3776 */
3777 if (!aborting())
3778 EMSG2(_(e_invexpr2), arg);
3779 ret = FAIL;
3780 }
3781 if (nextcmd != NULL)
3782 *nextcmd = check_nextcmd(p);
3783
3784 return ret;
3785}
3786
3787/*
3788 * Handle top level expression:
3789 * expr1 ? expr0 : expr0
3790 *
3791 * "arg" must point to the first non-white of the expression.
3792 * "arg" is advanced to the next non-white after the recognized expression.
3793 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003794 * Note: "rettv.v_lock" is not set.
3795 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 * Return OK or FAIL.
3797 */
3798 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003799eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003801 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 int evaluate;
3803{
3804 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003805 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806
3807 /*
3808 * Get the first variable.
3809 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003810 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 return FAIL;
3812
3813 if ((*arg)[0] == '?')
3814 {
3815 result = FALSE;
3816 if (evaluate)
3817 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003818 int error = FALSE;
3819
3820 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003822 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003823 if (error)
3824 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 }
3826
3827 /*
3828 * Get the second variable.
3829 */
3830 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003831 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 return FAIL;
3833
3834 /*
3835 * Check for the ":".
3836 */
3837 if ((*arg)[0] != ':')
3838 {
3839 EMSG(_("E109: Missing ':' after '?'"));
3840 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003841 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 return FAIL;
3843 }
3844
3845 /*
3846 * Get the third variable.
3847 */
3848 *arg = skipwhite(*arg + 1);
3849 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3850 {
3851 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003852 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 return FAIL;
3854 }
3855 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003856 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 }
3858
3859 return OK;
3860}
3861
3862/*
3863 * Handle first level expression:
3864 * expr2 || expr2 || expr2 logical OR
3865 *
3866 * "arg" must point to the first non-white of the expression.
3867 * "arg" is advanced to the next non-white after the recognized expression.
3868 *
3869 * Return OK or FAIL.
3870 */
3871 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003872eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003874 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 int evaluate;
3876{
Bram Moolenaar33570922005-01-25 22:26:29 +00003877 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 long result;
3879 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003880 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881
3882 /*
3883 * Get the first variable.
3884 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003885 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 return FAIL;
3887
3888 /*
3889 * Repeat until there is no following "||".
3890 */
3891 first = TRUE;
3892 result = FALSE;
3893 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3894 {
3895 if (evaluate && first)
3896 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003897 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003899 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003900 if (error)
3901 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 first = FALSE;
3903 }
3904
3905 /*
3906 * Get the second variable.
3907 */
3908 *arg = skipwhite(*arg + 2);
3909 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3910 return FAIL;
3911
3912 /*
3913 * Compute the result.
3914 */
3915 if (evaluate && !result)
3916 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003917 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003919 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003920 if (error)
3921 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 }
3923 if (evaluate)
3924 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003925 rettv->v_type = VAR_NUMBER;
3926 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 }
3928 }
3929
3930 return OK;
3931}
3932
3933/*
3934 * Handle second level expression:
3935 * expr3 && expr3 && expr3 logical AND
3936 *
3937 * "arg" must point to the first non-white of the expression.
3938 * "arg" is advanced to the next non-white after the recognized expression.
3939 *
3940 * Return OK or FAIL.
3941 */
3942 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003943eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003945 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 int evaluate;
3947{
Bram Moolenaar33570922005-01-25 22:26:29 +00003948 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 long result;
3950 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003951 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952
3953 /*
3954 * Get the first variable.
3955 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003956 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 return FAIL;
3958
3959 /*
3960 * Repeat until there is no following "&&".
3961 */
3962 first = TRUE;
3963 result = TRUE;
3964 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3965 {
3966 if (evaluate && first)
3967 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003968 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003970 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003971 if (error)
3972 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 first = FALSE;
3974 }
3975
3976 /*
3977 * Get the second variable.
3978 */
3979 *arg = skipwhite(*arg + 2);
3980 if (eval4(arg, &var2, evaluate && result) == FAIL)
3981 return FAIL;
3982
3983 /*
3984 * Compute the result.
3985 */
3986 if (evaluate && result)
3987 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003988 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003990 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003991 if (error)
3992 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 }
3994 if (evaluate)
3995 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003996 rettv->v_type = VAR_NUMBER;
3997 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 }
3999 }
4000
4001 return OK;
4002}
4003
4004/*
4005 * Handle third level expression:
4006 * var1 == var2
4007 * var1 =~ var2
4008 * var1 != var2
4009 * var1 !~ var2
4010 * var1 > var2
4011 * var1 >= var2
4012 * var1 < var2
4013 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004014 * var1 is var2
4015 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 *
4017 * "arg" must point to the first non-white of the expression.
4018 * "arg" is advanced to the next non-white after the recognized expression.
4019 *
4020 * Return OK or FAIL.
4021 */
4022 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004023eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004025 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 int evaluate;
4027{
Bram Moolenaar33570922005-01-25 22:26:29 +00004028 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 char_u *p;
4030 int i;
4031 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004032 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 int len = 2;
4034 long n1, n2;
4035 char_u *s1, *s2;
4036 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4037 regmatch_T regmatch;
4038 int ic;
4039 char_u *save_cpo;
4040
4041 /*
4042 * Get the first variable.
4043 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004044 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 return FAIL;
4046
4047 p = *arg;
4048 switch (p[0])
4049 {
4050 case '=': if (p[1] == '=')
4051 type = TYPE_EQUAL;
4052 else if (p[1] == '~')
4053 type = TYPE_MATCH;
4054 break;
4055 case '!': if (p[1] == '=')
4056 type = TYPE_NEQUAL;
4057 else if (p[1] == '~')
4058 type = TYPE_NOMATCH;
4059 break;
4060 case '>': if (p[1] != '=')
4061 {
4062 type = TYPE_GREATER;
4063 len = 1;
4064 }
4065 else
4066 type = TYPE_GEQUAL;
4067 break;
4068 case '<': if (p[1] != '=')
4069 {
4070 type = TYPE_SMALLER;
4071 len = 1;
4072 }
4073 else
4074 type = TYPE_SEQUAL;
4075 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004076 case 'i': if (p[1] == 's')
4077 {
4078 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4079 len = 5;
4080 if (!vim_isIDc(p[len]))
4081 {
4082 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4083 type_is = TRUE;
4084 }
4085 }
4086 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 }
4088
4089 /*
4090 * If there is a comparitive operator, use it.
4091 */
4092 if (type != TYPE_UNKNOWN)
4093 {
4094 /* extra question mark appended: ignore case */
4095 if (p[len] == '?')
4096 {
4097 ic = TRUE;
4098 ++len;
4099 }
4100 /* extra '#' appended: match case */
4101 else if (p[len] == '#')
4102 {
4103 ic = FALSE;
4104 ++len;
4105 }
4106 /* nothing appened: use 'ignorecase' */
4107 else
4108 ic = p_ic;
4109
4110 /*
4111 * Get the second variable.
4112 */
4113 *arg = skipwhite(p + len);
4114 if (eval5(arg, &var2, evaluate) == FAIL)
4115 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004116 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 return FAIL;
4118 }
4119
4120 if (evaluate)
4121 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004122 if (type_is && rettv->v_type != var2.v_type)
4123 {
4124 /* For "is" a different type always means FALSE, for "notis"
4125 * it means TRUE. */
4126 n1 = (type == TYPE_NEQUAL);
4127 }
4128 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4129 {
4130 if (type_is)
4131 {
4132 n1 = (rettv->v_type == var2.v_type
4133 && rettv->vval.v_list == var2.vval.v_list);
4134 if (type == TYPE_NEQUAL)
4135 n1 = !n1;
4136 }
4137 else if (rettv->v_type != var2.v_type
4138 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4139 {
4140 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004141 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004142 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004143 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004144 clear_tv(rettv);
4145 clear_tv(&var2);
4146 return FAIL;
4147 }
4148 else
4149 {
4150 /* Compare two Lists for being equal or unequal. */
4151 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4152 if (type == TYPE_NEQUAL)
4153 n1 = !n1;
4154 }
4155 }
4156
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004157 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4158 {
4159 if (type_is)
4160 {
4161 n1 = (rettv->v_type == var2.v_type
4162 && rettv->vval.v_dict == var2.vval.v_dict);
4163 if (type == TYPE_NEQUAL)
4164 n1 = !n1;
4165 }
4166 else if (rettv->v_type != var2.v_type
4167 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4168 {
4169 if (rettv->v_type != var2.v_type)
4170 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4171 else
4172 EMSG(_("E736: Invalid operation for Dictionary"));
4173 clear_tv(rettv);
4174 clear_tv(&var2);
4175 return FAIL;
4176 }
4177 else
4178 {
4179 /* Compare two Dictionaries for being equal or unequal. */
4180 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4181 if (type == TYPE_NEQUAL)
4182 n1 = !n1;
4183 }
4184 }
4185
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004186 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4187 {
4188 if (rettv->v_type != var2.v_type
4189 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4190 {
4191 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004192 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004193 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004194 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004195 clear_tv(rettv);
4196 clear_tv(&var2);
4197 return FAIL;
4198 }
4199 else
4200 {
4201 /* Compare two Funcrefs for being equal or unequal. */
4202 if (rettv->vval.v_string == NULL
4203 || var2.vval.v_string == NULL)
4204 n1 = FALSE;
4205 else
4206 n1 = STRCMP(rettv->vval.v_string,
4207 var2.vval.v_string) == 0;
4208 if (type == TYPE_NEQUAL)
4209 n1 = !n1;
4210 }
4211 }
4212
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 /*
4214 * If one of the two variables is a number, compare as a number.
4215 * When using "=~" or "!~", always compare as string.
4216 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004217 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4219 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004220 n1 = get_tv_number(rettv);
4221 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 switch (type)
4223 {
4224 case TYPE_EQUAL: n1 = (n1 == n2); break;
4225 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4226 case TYPE_GREATER: n1 = (n1 > n2); break;
4227 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4228 case TYPE_SMALLER: n1 = (n1 < n2); break;
4229 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4230 case TYPE_UNKNOWN:
4231 case TYPE_MATCH:
4232 case TYPE_NOMATCH: break; /* avoid gcc warning */
4233 }
4234 }
4235 else
4236 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004237 s1 = get_tv_string_buf(rettv, buf1);
4238 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4240 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4241 else
4242 i = 0;
4243 n1 = FALSE;
4244 switch (type)
4245 {
4246 case TYPE_EQUAL: n1 = (i == 0); break;
4247 case TYPE_NEQUAL: n1 = (i != 0); break;
4248 case TYPE_GREATER: n1 = (i > 0); break;
4249 case TYPE_GEQUAL: n1 = (i >= 0); break;
4250 case TYPE_SMALLER: n1 = (i < 0); break;
4251 case TYPE_SEQUAL: n1 = (i <= 0); break;
4252
4253 case TYPE_MATCH:
4254 case TYPE_NOMATCH:
4255 /* avoid 'l' flag in 'cpoptions' */
4256 save_cpo = p_cpo;
4257 p_cpo = (char_u *)"";
4258 regmatch.regprog = vim_regcomp(s2,
4259 RE_MAGIC + RE_STRING);
4260 regmatch.rm_ic = ic;
4261 if (regmatch.regprog != NULL)
4262 {
4263 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4264 vim_free(regmatch.regprog);
4265 if (type == TYPE_NOMATCH)
4266 n1 = !n1;
4267 }
4268 p_cpo = save_cpo;
4269 break;
4270
4271 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4272 }
4273 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004274 clear_tv(rettv);
4275 clear_tv(&var2);
4276 rettv->v_type = VAR_NUMBER;
4277 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 }
4279 }
4280
4281 return OK;
4282}
4283
4284/*
4285 * Handle fourth level expression:
4286 * + number addition
4287 * - number subtraction
4288 * . string concatenation
4289 *
4290 * "arg" must point to the first non-white of the expression.
4291 * "arg" is advanced to the next non-white after the recognized expression.
4292 *
4293 * Return OK or FAIL.
4294 */
4295 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004296eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004298 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 int evaluate;
4300{
Bram Moolenaar33570922005-01-25 22:26:29 +00004301 typval_T var2;
4302 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 int op;
4304 long n1, n2;
4305 char_u *s1, *s2;
4306 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4307 char_u *p;
4308
4309 /*
4310 * Get the first variable.
4311 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004312 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 return FAIL;
4314
4315 /*
4316 * Repeat computing, until no '+', '-' or '.' is following.
4317 */
4318 for (;;)
4319 {
4320 op = **arg;
4321 if (op != '+' && op != '-' && op != '.')
4322 break;
4323
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004324 if (op != '+' || rettv->v_type != VAR_LIST)
4325 {
4326 /* For "list + ...", an illegal use of the first operand as
4327 * a number cannot be determined before evaluating the 2nd
4328 * operand: if this is also a list, all is ok.
4329 * For "something . ...", "something - ..." or "non-list + ...",
4330 * we know that the first operand needs to be a string or number
4331 * without evaluating the 2nd operand. So check before to avoid
4332 * side effects after an error. */
4333 if (evaluate && get_tv_string_chk(rettv) == NULL)
4334 {
4335 clear_tv(rettv);
4336 return FAIL;
4337 }
4338 }
4339
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 /*
4341 * Get the second variable.
4342 */
4343 *arg = skipwhite(*arg + 1);
4344 if (eval6(arg, &var2, evaluate) == FAIL)
4345 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004346 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 return FAIL;
4348 }
4349
4350 if (evaluate)
4351 {
4352 /*
4353 * Compute the result.
4354 */
4355 if (op == '.')
4356 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004357 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4358 s2 = get_tv_string_buf_chk(&var2, buf2);
4359 if (s2 == NULL) /* type error ? */
4360 {
4361 clear_tv(rettv);
4362 clear_tv(&var2);
4363 return FAIL;
4364 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004365 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004366 clear_tv(rettv);
4367 rettv->v_type = VAR_STRING;
4368 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004370 else if (op == '+' && rettv->v_type == VAR_LIST
4371 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004372 {
4373 /* concatenate Lists */
4374 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4375 &var3) == FAIL)
4376 {
4377 clear_tv(rettv);
4378 clear_tv(&var2);
4379 return FAIL;
4380 }
4381 clear_tv(rettv);
4382 *rettv = var3;
4383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 else
4385 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004386 int error = FALSE;
4387
4388 n1 = get_tv_number_chk(rettv, &error);
4389 if (error)
4390 {
4391 /* This can only happen for "list + non-list".
4392 * For "non-list + ..." or "something - ...", we returned
4393 * before evaluating the 2nd operand. */
4394 clear_tv(rettv);
4395 return FAIL;
4396 }
4397 n2 = get_tv_number_chk(&var2, &error);
4398 if (error)
4399 {
4400 clear_tv(rettv);
4401 clear_tv(&var2);
4402 return FAIL;
4403 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004404 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 if (op == '+')
4406 n1 = n1 + n2;
4407 else
4408 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004409 rettv->v_type = VAR_NUMBER;
4410 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004412 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 }
4414 }
4415 return OK;
4416}
4417
4418/*
4419 * Handle fifth level expression:
4420 * * number multiplication
4421 * / number division
4422 * % number modulo
4423 *
4424 * "arg" must point to the first non-white of the expression.
4425 * "arg" is advanced to the next non-white after the recognized expression.
4426 *
4427 * Return OK or FAIL.
4428 */
4429 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004430eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004432 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 int evaluate;
4434{
Bram Moolenaar33570922005-01-25 22:26:29 +00004435 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 int op;
4437 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004438 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439
4440 /*
4441 * Get the first variable.
4442 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004443 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 return FAIL;
4445
4446 /*
4447 * Repeat computing, until no '*', '/' or '%' is following.
4448 */
4449 for (;;)
4450 {
4451 op = **arg;
4452 if (op != '*' && op != '/' && op != '%')
4453 break;
4454
4455 if (evaluate)
4456 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004457 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004458 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004459 if (error)
4460 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 }
4462 else
4463 n1 = 0;
4464
4465 /*
4466 * Get the second variable.
4467 */
4468 *arg = skipwhite(*arg + 1);
4469 if (eval7(arg, &var2, evaluate) == FAIL)
4470 return FAIL;
4471
4472 if (evaluate)
4473 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004474 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004475 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004476 if (error)
4477 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478
4479 /*
4480 * Compute the result.
4481 */
4482 if (op == '*')
4483 n1 = n1 * n2;
4484 else if (op == '/')
4485 {
4486 if (n2 == 0) /* give an error message? */
4487 n1 = 0x7fffffffL;
4488 else
4489 n1 = n1 / n2;
4490 }
4491 else
4492 {
4493 if (n2 == 0) /* give an error message? */
4494 n1 = 0;
4495 else
4496 n1 = n1 % n2;
4497 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004498 rettv->v_type = VAR_NUMBER;
4499 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 }
4501 }
4502
4503 return OK;
4504}
4505
4506/*
4507 * Handle sixth level expression:
4508 * number number constant
4509 * "string" string contstant
4510 * 'string' literal string contstant
4511 * &option-name option value
4512 * @r register contents
4513 * identifier variable value
4514 * function() function call
4515 * $VAR environment variable
4516 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004517 * [expr, expr] List
4518 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 *
4520 * Also handle:
4521 * ! in front logical NOT
4522 * - in front unary minus
4523 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004524 * trailing [] subscript in String or List
4525 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 *
4527 * "arg" must point to the first non-white of the expression.
4528 * "arg" is advanced to the next non-white after the recognized expression.
4529 *
4530 * Return OK or FAIL.
4531 */
4532 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004533eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004535 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 int evaluate;
4537{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 long n;
4539 int len;
4540 char_u *s;
4541 int val;
4542 char_u *start_leader, *end_leader;
4543 int ret = OK;
4544 char_u *alias;
4545
4546 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004547 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004548 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004550 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551
4552 /*
4553 * Skip '!' and '-' characters. They are handled later.
4554 */
4555 start_leader = *arg;
4556 while (**arg == '!' || **arg == '-' || **arg == '+')
4557 *arg = skipwhite(*arg + 1);
4558 end_leader = *arg;
4559
4560 switch (**arg)
4561 {
4562 /*
4563 * Number constant.
4564 */
4565 case '0':
4566 case '1':
4567 case '2':
4568 case '3':
4569 case '4':
4570 case '5':
4571 case '6':
4572 case '7':
4573 case '8':
4574 case '9':
4575 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4576 *arg += len;
4577 if (evaluate)
4578 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004579 rettv->v_type = VAR_NUMBER;
4580 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 }
4582 break;
4583
4584 /*
4585 * String constant: "string".
4586 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004587 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 break;
4589
4590 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004591 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004593 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004594 break;
4595
4596 /*
4597 * List: [expr, expr]
4598 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004599 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 break;
4601
4602 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004603 * Dictionary: {key: val, key: val}
4604 */
4605 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4606 break;
4607
4608 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004609 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004611 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 break;
4613
4614 /*
4615 * Environment variable: $VAR.
4616 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004617 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 break;
4619
4620 /*
4621 * Register contents: @r.
4622 */
4623 case '@': ++*arg;
4624 if (evaluate)
4625 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004626 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004627 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 }
4629 if (**arg != NUL)
4630 ++*arg;
4631 break;
4632
4633 /*
4634 * nested expression: (expression).
4635 */
4636 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004637 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 if (**arg == ')')
4639 ++*arg;
4640 else if (ret == OK)
4641 {
4642 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004643 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 ret = FAIL;
4645 }
4646 break;
4647
Bram Moolenaar8c711452005-01-14 21:53:12 +00004648 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 break;
4650 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004651
4652 if (ret == NOTDONE)
4653 {
4654 /*
4655 * Must be a variable or function name.
4656 * Can also be a curly-braces kind of name: {expr}.
4657 */
4658 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004659 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004660 if (alias != NULL)
4661 s = alias;
4662
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004663 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004664 ret = FAIL;
4665 else
4666 {
4667 if (**arg == '(') /* recursive! */
4668 {
4669 /* If "s" is the name of a variable of type VAR_FUNC
4670 * use its contents. */
4671 s = deref_func_name(s, &len);
4672
4673 /* Invoke the function. */
4674 ret = get_func_tv(s, len, rettv, arg,
4675 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004676 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004677 /* Stop the expression evaluation when immediately
4678 * aborting on error, or when an interrupt occurred or
4679 * an exception was thrown but not caught. */
4680 if (aborting())
4681 {
4682 if (ret == OK)
4683 clear_tv(rettv);
4684 ret = FAIL;
4685 }
4686 }
4687 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004688 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004689 else
4690 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004691 }
4692
4693 if (alias != NULL)
4694 vim_free(alias);
4695 }
4696
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 *arg = skipwhite(*arg);
4698
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004699 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4700 * expr(expr). */
4701 if (ret == OK)
4702 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703
4704 /*
4705 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4706 */
4707 if (ret == OK && evaluate && end_leader > start_leader)
4708 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004709 int error = FALSE;
4710
4711 val = get_tv_number_chk(rettv, &error);
4712 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004714 clear_tv(rettv);
4715 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004717 else
4718 {
4719 while (end_leader > start_leader)
4720 {
4721 --end_leader;
4722 if (*end_leader == '!')
4723 val = !val;
4724 else if (*end_leader == '-')
4725 val = -val;
4726 }
4727 clear_tv(rettv);
4728 rettv->v_type = VAR_NUMBER;
4729 rettv->vval.v_number = val;
4730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 }
4732
4733 return ret;
4734}
4735
4736/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004737 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4738 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004739 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4740 */
4741 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004742eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004743 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004744 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004745 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004746 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004747{
4748 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004749 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004750 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004751 long len = -1;
4752 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004753 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004754 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004755
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004756 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004757 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004758 if (verbose)
4759 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004760 return FAIL;
4761 }
4762
Bram Moolenaar8c711452005-01-14 21:53:12 +00004763 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004764 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004765 /*
4766 * dict.name
4767 */
4768 key = *arg + 1;
4769 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4770 ;
4771 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004772 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004773 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004774 }
4775 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004776 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004777 /*
4778 * something[idx]
4779 *
4780 * Get the (first) variable from inside the [].
4781 */
4782 *arg = skipwhite(*arg + 1);
4783 if (**arg == ':')
4784 empty1 = TRUE;
4785 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4786 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004787 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4788 {
4789 /* not a number or string */
4790 clear_tv(&var1);
4791 return FAIL;
4792 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004793
4794 /*
4795 * Get the second variable from inside the [:].
4796 */
4797 if (**arg == ':')
4798 {
4799 range = TRUE;
4800 *arg = skipwhite(*arg + 1);
4801 if (**arg == ']')
4802 empty2 = TRUE;
4803 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4804 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004805 if (!empty1)
4806 clear_tv(&var1);
4807 return FAIL;
4808 }
4809 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4810 {
4811 /* not a number or string */
4812 if (!empty1)
4813 clear_tv(&var1);
4814 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004815 return FAIL;
4816 }
4817 }
4818
4819 /* Check for the ']'. */
4820 if (**arg != ']')
4821 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004822 if (verbose)
4823 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004824 clear_tv(&var1);
4825 if (range)
4826 clear_tv(&var2);
4827 return FAIL;
4828 }
4829 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004830 }
4831
4832 if (evaluate)
4833 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004834 n1 = 0;
4835 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004836 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004837 n1 = get_tv_number(&var1);
4838 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004839 }
4840 if (range)
4841 {
4842 if (empty2)
4843 n2 = -1;
4844 else
4845 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004846 n2 = get_tv_number(&var2);
4847 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004848 }
4849 }
4850
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004851 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004852 {
4853 case VAR_NUMBER:
4854 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004855 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004856 len = (long)STRLEN(s);
4857 if (range)
4858 {
4859 /* The resulting variable is a substring. If the indexes
4860 * are out of range the result is empty. */
4861 if (n1 < 0)
4862 {
4863 n1 = len + n1;
4864 if (n1 < 0)
4865 n1 = 0;
4866 }
4867 if (n2 < 0)
4868 n2 = len + n2;
4869 else if (n2 >= len)
4870 n2 = len;
4871 if (n1 >= len || n2 < 0 || n1 > n2)
4872 s = NULL;
4873 else
4874 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4875 }
4876 else
4877 {
4878 /* The resulting variable is a string of a single
4879 * character. If the index is too big or negative the
4880 * result is empty. */
4881 if (n1 >= len || n1 < 0)
4882 s = NULL;
4883 else
4884 s = vim_strnsave(s + n1, 1);
4885 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004886 clear_tv(rettv);
4887 rettv->v_type = VAR_STRING;
4888 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004889 break;
4890
4891 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004892 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004893 if (n1 < 0)
4894 n1 = len + n1;
4895 if (!empty1 && (n1 < 0 || n1 >= len))
4896 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004897 /* For a range we allow invalid values and return an empty
4898 * list. A list index out of range is an error. */
4899 if (!range)
4900 {
4901 if (verbose)
4902 EMSGN(_(e_listidx), n1);
4903 return FAIL;
4904 }
4905 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004906 }
4907 if (range)
4908 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004909 list_T *l;
4910 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004911
4912 if (n2 < 0)
4913 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004914 else if (n2 >= len)
4915 n2 = len - 1;
4916 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004917 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004918 l = list_alloc();
4919 if (l == NULL)
4920 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004921 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004922 n1 <= n2; ++n1)
4923 {
4924 if (list_append_tv(l, &item->li_tv) == FAIL)
4925 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00004926 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004927 return FAIL;
4928 }
4929 item = item->li_next;
4930 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004931 clear_tv(rettv);
4932 rettv->v_type = VAR_LIST;
4933 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004934 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004935 }
4936 else
4937 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004938 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004939 clear_tv(rettv);
4940 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004941 }
4942 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004943
4944 case VAR_DICT:
4945 if (range)
4946 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004947 if (verbose)
4948 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004949 if (len == -1)
4950 clear_tv(&var1);
4951 return FAIL;
4952 }
4953 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004954 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004955
4956 if (len == -1)
4957 {
4958 key = get_tv_string(&var1);
4959 if (*key == NUL)
4960 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004961 if (verbose)
4962 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004963 clear_tv(&var1);
4964 return FAIL;
4965 }
4966 }
4967
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004968 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004969
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004970 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004971 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004972 if (len == -1)
4973 clear_tv(&var1);
4974 if (item == NULL)
4975 return FAIL;
4976
4977 copy_tv(&item->di_tv, &var1);
4978 clear_tv(rettv);
4979 *rettv = var1;
4980 }
4981 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004982 }
4983 }
4984
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004985 return OK;
4986}
4987
4988/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 * Get an option value.
4990 * "arg" points to the '&' or '+' before the option name.
4991 * "arg" is advanced to character after the option name.
4992 * Return OK or FAIL.
4993 */
4994 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004995get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004997 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 int evaluate;
4999{
5000 char_u *option_end;
5001 long numval;
5002 char_u *stringval;
5003 int opt_type;
5004 int c;
5005 int working = (**arg == '+'); /* has("+option") */
5006 int ret = OK;
5007 int opt_flags;
5008
5009 /*
5010 * Isolate the option name and find its value.
5011 */
5012 option_end = find_option_end(arg, &opt_flags);
5013 if (option_end == NULL)
5014 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005015 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 EMSG2(_("E112: Option name missing: %s"), *arg);
5017 return FAIL;
5018 }
5019
5020 if (!evaluate)
5021 {
5022 *arg = option_end;
5023 return OK;
5024 }
5025
5026 c = *option_end;
5027 *option_end = NUL;
5028 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005029 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030
5031 if (opt_type == -3) /* invalid name */
5032 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005033 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 EMSG2(_("E113: Unknown option: %s"), *arg);
5035 ret = FAIL;
5036 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005037 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 {
5039 if (opt_type == -2) /* hidden string option */
5040 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005041 rettv->v_type = VAR_STRING;
5042 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 }
5044 else if (opt_type == -1) /* hidden number option */
5045 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005046 rettv->v_type = VAR_NUMBER;
5047 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 }
5049 else if (opt_type == 1) /* number option */
5050 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005051 rettv->v_type = VAR_NUMBER;
5052 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 }
5054 else /* string option */
5055 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005056 rettv->v_type = VAR_STRING;
5057 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 }
5059 }
5060 else if (working && (opt_type == -2 || opt_type == -1))
5061 ret = FAIL;
5062
5063 *option_end = c; /* put back for error messages */
5064 *arg = option_end;
5065
5066 return ret;
5067}
5068
5069/*
5070 * Allocate a variable for a string constant.
5071 * Return OK or FAIL.
5072 */
5073 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005074get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005076 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 int evaluate;
5078{
5079 char_u *p;
5080 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 int extra = 0;
5082
5083 /*
5084 * Find the end of the string, skipping backslashed characters.
5085 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005086 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 {
5088 if (*p == '\\' && p[1] != NUL)
5089 {
5090 ++p;
5091 /* A "\<x>" form occupies at least 4 characters, and produces up
5092 * to 6 characters: reserve space for 2 extra */
5093 if (*p == '<')
5094 extra += 2;
5095 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 }
5097
5098 if (*p != '"')
5099 {
5100 EMSG2(_("E114: Missing quote: %s"), *arg);
5101 return FAIL;
5102 }
5103
5104 /* If only parsing, set *arg and return here */
5105 if (!evaluate)
5106 {
5107 *arg = p + 1;
5108 return OK;
5109 }
5110
5111 /*
5112 * Copy the string into allocated memory, handling backslashed
5113 * characters.
5114 */
5115 name = alloc((unsigned)(p - *arg + extra));
5116 if (name == NULL)
5117 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005118 rettv->v_type = VAR_STRING;
5119 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120
Bram Moolenaar8c711452005-01-14 21:53:12 +00005121 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 {
5123 if (*p == '\\')
5124 {
5125 switch (*++p)
5126 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005127 case 'b': *name++ = BS; ++p; break;
5128 case 'e': *name++ = ESC; ++p; break;
5129 case 'f': *name++ = FF; ++p; break;
5130 case 'n': *name++ = NL; ++p; break;
5131 case 'r': *name++ = CAR; ++p; break;
5132 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133
5134 case 'X': /* hex: "\x1", "\x12" */
5135 case 'x':
5136 case 'u': /* Unicode: "\u0023" */
5137 case 'U':
5138 if (vim_isxdigit(p[1]))
5139 {
5140 int n, nr;
5141 int c = toupper(*p);
5142
5143 if (c == 'X')
5144 n = 2;
5145 else
5146 n = 4;
5147 nr = 0;
5148 while (--n >= 0 && vim_isxdigit(p[1]))
5149 {
5150 ++p;
5151 nr = (nr << 4) + hex2nr(*p);
5152 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005153 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154#ifdef FEAT_MBYTE
5155 /* For "\u" store the number according to
5156 * 'encoding'. */
5157 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005158 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 else
5160#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005161 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 break;
5164
5165 /* octal: "\1", "\12", "\123" */
5166 case '0':
5167 case '1':
5168 case '2':
5169 case '3':
5170 case '4':
5171 case '5':
5172 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005173 case '7': *name = *p++ - '0';
5174 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005176 *name = (*name << 3) + *p++ - '0';
5177 if (*p >= '0' && *p <= '7')
5178 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005180 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 break;
5182
5183 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005184 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 if (extra != 0)
5186 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005187 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 break;
5189 }
5190 /* FALLTHROUGH */
5191
Bram Moolenaar8c711452005-01-14 21:53:12 +00005192 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 break;
5194 }
5195 }
5196 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005197 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005200 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 *arg = p + 1;
5202
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 return OK;
5204}
5205
5206/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005207 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 * Return OK or FAIL.
5209 */
5210 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005211get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005213 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 int evaluate;
5215{
5216 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005217 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005218 int reduce = 0;
5219
5220 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005221 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005222 */
5223 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5224 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005225 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005226 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005227 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005228 break;
5229 ++reduce;
5230 ++p;
5231 }
5232 }
5233
Bram Moolenaar8c711452005-01-14 21:53:12 +00005234 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005235 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005236 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005237 return FAIL;
5238 }
5239
Bram Moolenaar8c711452005-01-14 21:53:12 +00005240 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005241 if (!evaluate)
5242 {
5243 *arg = p + 1;
5244 return OK;
5245 }
5246
5247 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005248 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005249 */
5250 str = alloc((unsigned)((p - *arg) - reduce));
5251 if (str == NULL)
5252 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005253 rettv->v_type = VAR_STRING;
5254 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005255
Bram Moolenaar8c711452005-01-14 21:53:12 +00005256 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005257 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005258 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005259 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005260 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005261 break;
5262 ++p;
5263 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005264 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005265 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005266 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005267 *arg = p + 1;
5268
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005269 return OK;
5270}
5271
5272/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005273 * Allocate a variable for a List and fill it from "*arg".
5274 * Return OK or FAIL.
5275 */
5276 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005277get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005278 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005279 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005280 int evaluate;
5281{
Bram Moolenaar33570922005-01-25 22:26:29 +00005282 list_T *l = NULL;
5283 typval_T tv;
5284 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005285
5286 if (evaluate)
5287 {
5288 l = list_alloc();
5289 if (l == NULL)
5290 return FAIL;
5291 }
5292
5293 *arg = skipwhite(*arg + 1);
5294 while (**arg != ']' && **arg != NUL)
5295 {
5296 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5297 goto failret;
5298 if (evaluate)
5299 {
5300 item = listitem_alloc();
5301 if (item != NULL)
5302 {
5303 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005304 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005305 list_append(l, item);
5306 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005307 else
5308 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005309 }
5310
5311 if (**arg == ']')
5312 break;
5313 if (**arg != ',')
5314 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005315 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005316 goto failret;
5317 }
5318 *arg = skipwhite(*arg + 1);
5319 }
5320
5321 if (**arg != ']')
5322 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005323 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005324failret:
5325 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005326 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005327 return FAIL;
5328 }
5329
5330 *arg = skipwhite(*arg + 1);
5331 if (evaluate)
5332 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005333 rettv->v_type = VAR_LIST;
5334 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005335 ++l->lv_refcount;
5336 }
5337
5338 return OK;
5339}
5340
5341/*
5342 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005343 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005344 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005345 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005346list_alloc()
5347{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005348 list_T *l;
5349
5350 l = (list_T *)alloc_clear(sizeof(list_T));
5351 if (l != NULL)
5352 {
5353 /* Prepend the list to the list of lists for garbage collection. */
5354 if (first_list != NULL)
5355 first_list->lv_used_prev = l;
5356 l->lv_used_prev = NULL;
5357 l->lv_used_next = first_list;
5358 first_list = l;
5359 }
5360 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005361}
5362
5363/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005364 * Allocate an empty list for a return value.
5365 * Returns OK or FAIL.
5366 */
5367 static int
5368rettv_list_alloc(rettv)
5369 typval_T *rettv;
5370{
5371 list_T *l = list_alloc();
5372
5373 if (l == NULL)
5374 return FAIL;
5375
5376 rettv->vval.v_list = l;
5377 rettv->v_type = VAR_LIST;
5378 ++l->lv_refcount;
5379 return OK;
5380}
5381
5382/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005383 * Unreference a list: decrement the reference count and free it when it
5384 * becomes zero.
5385 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005386 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005387list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005388 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005389{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005390 if (l != NULL && --l->lv_refcount <= 0)
5391 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005392}
5393
5394/*
5395 * Free a list, including all items it points to.
5396 * Ignores the reference count.
5397 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005398 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005399list_free(l, recurse)
5400 list_T *l;
5401 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005402{
Bram Moolenaar33570922005-01-25 22:26:29 +00005403 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005404
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005405 /* Remove the list from the list of lists for garbage collection. */
5406 if (l->lv_used_prev == NULL)
5407 first_list = l->lv_used_next;
5408 else
5409 l->lv_used_prev->lv_used_next = l->lv_used_next;
5410 if (l->lv_used_next != NULL)
5411 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5412
Bram Moolenaard9fba312005-06-26 22:34:35 +00005413 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005414 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005415 /* Remove the item before deleting it. */
5416 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005417 if (recurse || (item->li_tv.v_type != VAR_LIST
5418 && item->li_tv.v_type != VAR_DICT))
5419 clear_tv(&item->li_tv);
5420 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005421 }
5422 vim_free(l);
5423}
5424
5425/*
5426 * Allocate a list item.
5427 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005428 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005429listitem_alloc()
5430{
Bram Moolenaar33570922005-01-25 22:26:29 +00005431 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005432}
5433
5434/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005435 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005436 */
5437 static void
5438listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005439 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005440{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005441 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005442 vim_free(item);
5443}
5444
5445/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005446 * Remove a list item from a List and free it. Also clears the value.
5447 */
5448 static void
5449listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005450 list_T *l;
5451 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005452{
5453 list_remove(l, item, item);
5454 listitem_free(item);
5455}
5456
5457/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005458 * Get the number of items in a list.
5459 */
5460 static long
5461list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005462 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005463{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005464 if (l == NULL)
5465 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005466 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005467}
5468
5469/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005470 * Return TRUE when two lists have exactly the same values.
5471 */
5472 static int
5473list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005474 list_T *l1;
5475 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005476 int ic; /* ignore case for strings */
5477{
Bram Moolenaar33570922005-01-25 22:26:29 +00005478 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005479
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005480 if (l1 == l2)
5481 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005482 if (list_len(l1) != list_len(l2))
5483 return FALSE;
5484
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005485 for (item1 = l1->lv_first, item2 = l2->lv_first;
5486 item1 != NULL && item2 != NULL;
5487 item1 = item1->li_next, item2 = item2->li_next)
5488 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5489 return FALSE;
5490 return item1 == NULL && item2 == NULL;
5491}
5492
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005493#if defined(FEAT_PYTHON) || defined(PROTO)
5494/*
5495 * Return the dictitem that an entry in a hashtable points to.
5496 */
5497 dictitem_T *
5498dict_lookup(hi)
5499 hashitem_T *hi;
5500{
5501 return HI2DI(hi);
5502}
5503#endif
5504
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005505/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005506 * Return TRUE when two dictionaries have exactly the same key/values.
5507 */
5508 static int
5509dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005510 dict_T *d1;
5511 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005512 int ic; /* ignore case for strings */
5513{
Bram Moolenaar33570922005-01-25 22:26:29 +00005514 hashitem_T *hi;
5515 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005516 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005517
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005518 if (d1 == d2)
5519 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005520 if (dict_len(d1) != dict_len(d2))
5521 return FALSE;
5522
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005523 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00005524 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005525 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005526 if (!HASHITEM_EMPTY(hi))
5527 {
5528 item2 = dict_find(d2, hi->hi_key, -1);
5529 if (item2 == NULL)
5530 return FALSE;
5531 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5532 return FALSE;
5533 --todo;
5534 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005535 }
5536 return TRUE;
5537}
5538
5539/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005540 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005541 * Compares the items just like "==" would compare them, but strings and
5542 * numbers are different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005543 */
5544 static int
5545tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005546 typval_T *tv1;
5547 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005548 int ic; /* ignore case */
5549{
5550 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005551 char_u *s1, *s2;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005552 static int recursive = 0; /* cach recursive loops */
5553 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005554
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005555 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005556 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005557 /* Catch lists and dicts that have an endless loop by limiting
5558 * recursiveness to 1000. We guess they are equal then. */
5559 if (recursive >= 1000)
5560 return TRUE;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005561
5562 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005563 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005564 case VAR_LIST:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005565 ++recursive;
5566 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5567 --recursive;
5568 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005569
5570 case VAR_DICT:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005571 ++recursive;
5572 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5573 --recursive;
5574 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005575
5576 case VAR_FUNC:
5577 return (tv1->vval.v_string != NULL
5578 && tv2->vval.v_string != NULL
5579 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5580
5581 case VAR_NUMBER:
5582 return tv1->vval.v_number == tv2->vval.v_number;
5583
5584 case VAR_STRING:
5585 s1 = get_tv_string_buf(tv1, buf1);
5586 s2 = get_tv_string_buf(tv2, buf2);
5587 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005588 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005589
5590 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005591 return TRUE;
5592}
5593
5594/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005595 * Locate item with index "n" in list "l" and return it.
5596 * A negative index is counted from the end; -1 is the last item.
5597 * Returns NULL when "n" is out of range.
5598 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005599 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005600list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005601 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005602 long n;
5603{
Bram Moolenaar33570922005-01-25 22:26:29 +00005604 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005605 long idx;
5606
5607 if (l == NULL)
5608 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005609
5610 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005611 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005612 n = l->lv_len + n;
5613
5614 /* Check for index out of range. */
5615 if (n < 0 || n >= l->lv_len)
5616 return NULL;
5617
5618 /* When there is a cached index may start search from there. */
5619 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005620 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005621 if (n < l->lv_idx / 2)
5622 {
5623 /* closest to the start of the list */
5624 item = l->lv_first;
5625 idx = 0;
5626 }
5627 else if (n > (l->lv_idx + l->lv_len) / 2)
5628 {
5629 /* closest to the end of the list */
5630 item = l->lv_last;
5631 idx = l->lv_len - 1;
5632 }
5633 else
5634 {
5635 /* closest to the cached index */
5636 item = l->lv_idx_item;
5637 idx = l->lv_idx;
5638 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005639 }
5640 else
5641 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005642 if (n < l->lv_len / 2)
5643 {
5644 /* closest to the start of the list */
5645 item = l->lv_first;
5646 idx = 0;
5647 }
5648 else
5649 {
5650 /* closest to the end of the list */
5651 item = l->lv_last;
5652 idx = l->lv_len - 1;
5653 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005654 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005655
5656 while (n > idx)
5657 {
5658 /* search forward */
5659 item = item->li_next;
5660 ++idx;
5661 }
5662 while (n < idx)
5663 {
5664 /* search backward */
5665 item = item->li_prev;
5666 --idx;
5667 }
5668
5669 /* cache the used index */
5670 l->lv_idx = idx;
5671 l->lv_idx_item = item;
5672
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005673 return item;
5674}
5675
5676/*
Bram Moolenaara5525202006-03-02 22:52:09 +00005677 * Get list item "l[idx]" as a number.
5678 */
5679 static long
5680list_find_nr(l, idx, errorp)
5681 list_T *l;
5682 long idx;
5683 int *errorp; /* set to TRUE when something wrong */
5684{
5685 listitem_T *li;
5686
5687 li = list_find(l, idx);
5688 if (li == NULL)
5689 {
5690 if (errorp != NULL)
5691 *errorp = TRUE;
5692 return -1L;
5693 }
5694 return get_tv_number_chk(&li->li_tv, errorp);
5695}
5696
5697/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005698 * Locate "item" list "l" and return its index.
5699 * Returns -1 when "item" is not in the list.
5700 */
5701 static long
5702list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005703 list_T *l;
5704 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005705{
5706 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005707 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005708
5709 if (l == NULL)
5710 return -1;
5711 idx = 0;
5712 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5713 ++idx;
5714 if (li == NULL)
5715 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005716 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005717}
5718
5719/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005720 * Append item "item" to the end of list "l".
5721 */
5722 static void
5723list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005724 list_T *l;
5725 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005726{
5727 if (l->lv_last == NULL)
5728 {
5729 /* empty list */
5730 l->lv_first = item;
5731 l->lv_last = item;
5732 item->li_prev = NULL;
5733 }
5734 else
5735 {
5736 l->lv_last->li_next = item;
5737 item->li_prev = l->lv_last;
5738 l->lv_last = item;
5739 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005740 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005741 item->li_next = NULL;
5742}
5743
5744/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005745 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005746 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005747 */
5748 static int
5749list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005750 list_T *l;
5751 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005752{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005753 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005754
Bram Moolenaar05159a02005-02-26 23:04:13 +00005755 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005756 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005757 copy_tv(tv, &li->li_tv);
5758 list_append(l, li);
5759 return OK;
5760}
5761
5762/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005763 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005764 * Return FAIL when out of memory.
5765 */
5766 int
5767list_append_dict(list, dict)
5768 list_T *list;
5769 dict_T *dict;
5770{
5771 listitem_T *li = listitem_alloc();
5772
5773 if (li == NULL)
5774 return FAIL;
5775 li->li_tv.v_type = VAR_DICT;
5776 li->li_tv.v_lock = 0;
5777 li->li_tv.vval.v_dict = dict;
5778 list_append(list, li);
5779 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005780 return OK;
5781}
5782
5783/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005784 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00005785 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005786 * Returns FAIL when out of memory.
5787 */
5788 static int
Bram Moolenaar4463f292005-09-25 22:20:24 +00005789list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005790 list_T *l;
5791 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00005792 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005793{
5794 listitem_T *li = listitem_alloc();
5795
5796 if (li == NULL)
5797 return FAIL;
5798 list_append(l, li);
5799 li->li_tv.v_type = VAR_STRING;
5800 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005801 if (str == NULL)
5802 li->li_tv.vval.v_string = NULL;
5803 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005804 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005805 return FAIL;
5806 return OK;
5807}
5808
5809/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00005810 * Append "n" to list "l".
5811 * Returns FAIL when out of memory.
5812 */
5813 static int
5814list_append_number(l, n)
5815 list_T *l;
5816 varnumber_T n;
5817{
5818 listitem_T *li;
5819
5820 li = listitem_alloc();
5821 if (li == NULL)
5822 return FAIL;
5823 li->li_tv.v_type = VAR_NUMBER;
5824 li->li_tv.v_lock = 0;
5825 li->li_tv.vval.v_number = n;
5826 list_append(l, li);
5827 return OK;
5828}
5829
5830/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005831 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005832 * If "item" is NULL append at the end.
5833 * Return FAIL when out of memory.
5834 */
5835 static int
5836list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005837 list_T *l;
5838 typval_T *tv;
5839 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005840{
Bram Moolenaar33570922005-01-25 22:26:29 +00005841 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005842
5843 if (ni == NULL)
5844 return FAIL;
5845 copy_tv(tv, &ni->li_tv);
5846 if (item == NULL)
5847 /* Append new item at end of list. */
5848 list_append(l, ni);
5849 else
5850 {
5851 /* Insert new item before existing item. */
5852 ni->li_prev = item->li_prev;
5853 ni->li_next = item;
5854 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005855 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005856 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005857 ++l->lv_idx;
5858 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005859 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005860 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005861 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005862 l->lv_idx_item = NULL;
5863 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005864 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005865 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005866 }
5867 return OK;
5868}
5869
5870/*
5871 * Extend "l1" with "l2".
5872 * If "bef" is NULL append at the end, otherwise insert before this item.
5873 * Returns FAIL when out of memory.
5874 */
5875 static int
5876list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005877 list_T *l1;
5878 list_T *l2;
5879 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005880{
Bram Moolenaar33570922005-01-25 22:26:29 +00005881 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005882
5883 for (item = l2->lv_first; item != NULL; item = item->li_next)
5884 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5885 return FAIL;
5886 return OK;
5887}
5888
5889/*
5890 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5891 * Return FAIL when out of memory.
5892 */
5893 static int
5894list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005895 list_T *l1;
5896 list_T *l2;
5897 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005898{
Bram Moolenaar33570922005-01-25 22:26:29 +00005899 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005900
5901 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005902 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005903 if (l == NULL)
5904 return FAIL;
5905 tv->v_type = VAR_LIST;
5906 tv->vval.v_list = l;
5907
5908 /* append all items from the second list */
5909 return list_extend(l, l2, NULL);
5910}
5911
5912/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005913 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005914 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005915 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005916 * Returns NULL when out of memory.
5917 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005918 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005919list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005920 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005921 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005922 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005923{
Bram Moolenaar33570922005-01-25 22:26:29 +00005924 list_T *copy;
5925 listitem_T *item;
5926 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005927
5928 if (orig == NULL)
5929 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005930
5931 copy = list_alloc();
5932 if (copy != NULL)
5933 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005934 if (copyID != 0)
5935 {
5936 /* Do this before adding the items, because one of the items may
5937 * refer back to this list. */
5938 orig->lv_copyID = copyID;
5939 orig->lv_copylist = copy;
5940 }
5941 for (item = orig->lv_first; item != NULL && !got_int;
5942 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005943 {
5944 ni = listitem_alloc();
5945 if (ni == NULL)
5946 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005947 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005948 {
5949 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5950 {
5951 vim_free(ni);
5952 break;
5953 }
5954 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005955 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005956 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005957 list_append(copy, ni);
5958 }
5959 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005960 if (item != NULL)
5961 {
5962 list_unref(copy);
5963 copy = NULL;
5964 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005965 }
5966
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005967 return copy;
5968}
5969
5970/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005971 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005972 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005973 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005974 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005975list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005976 list_T *l;
5977 listitem_T *item;
5978 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005979{
Bram Moolenaar33570922005-01-25 22:26:29 +00005980 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005981
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005982 /* notify watchers */
5983 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005984 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005985 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005986 list_fix_watch(l, ip);
5987 if (ip == item2)
5988 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005989 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005990
5991 if (item2->li_next == NULL)
5992 l->lv_last = item->li_prev;
5993 else
5994 item2->li_next->li_prev = item->li_prev;
5995 if (item->li_prev == NULL)
5996 l->lv_first = item2->li_next;
5997 else
5998 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005999 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006000}
6001
6002/*
6003 * Return an allocated string with the string representation of a list.
6004 * May return NULL.
6005 */
6006 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006007list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006008 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006009 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006010{
6011 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006012
6013 if (tv->vval.v_list == NULL)
6014 return NULL;
6015 ga_init2(&ga, (int)sizeof(char), 80);
6016 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006017 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006018 {
6019 vim_free(ga.ga_data);
6020 return NULL;
6021 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006022 ga_append(&ga, ']');
6023 ga_append(&ga, NUL);
6024 return (char_u *)ga.ga_data;
6025}
6026
6027/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006028 * Join list "l" into a string in "*gap", using separator "sep".
6029 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006030 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006031 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006032 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006033list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006034 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006035 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006036 char_u *sep;
6037 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006038 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006039{
6040 int first = TRUE;
6041 char_u *tofree;
6042 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006043 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006044 char_u *s;
6045
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006046 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006047 {
6048 if (first)
6049 first = FALSE;
6050 else
6051 ga_concat(gap, sep);
6052
6053 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006054 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006055 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006056 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006057 if (s != NULL)
6058 ga_concat(gap, s);
6059 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006060 if (s == NULL)
6061 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006062 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006063 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006064}
6065
6066/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006067 * Garbage collection for lists and dictionaries.
6068 *
6069 * We use reference counts to be able to free most items right away when they
6070 * are no longer used. But for composite items it's possible that it becomes
6071 * unused while the reference count is > 0: When there is a recursive
6072 * reference. Example:
6073 * :let l = [1, 2, 3]
6074 * :let d = {9: l}
6075 * :let l[1] = d
6076 *
6077 * Since this is quite unusual we handle this with garbage collection: every
6078 * once in a while find out which lists and dicts are not referenced from any
6079 * variable.
6080 *
6081 * Here is a good reference text about garbage collection (refers to Python
6082 * but it applies to all reference-counting mechanisms):
6083 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006084 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006085
6086/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006087 * Do garbage collection for lists and dicts.
6088 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006089 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006090 int
6091garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006092{
6093 dict_T *dd;
6094 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006095 int copyID = ++current_copyID;
6096 buf_T *buf;
6097 win_T *wp;
6098 int i;
6099 funccall_T *fc;
6100 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006101#ifdef FEAT_WINDOWS
6102 tabpage_T *tp;
6103#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006104
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006105 /* Only do this once. */
6106 want_garbage_collect = FALSE;
6107 may_garbage_collect = FALSE;
6108
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006109 /*
6110 * 1. Go through all accessible variables and mark all lists and dicts
6111 * with copyID.
6112 */
6113 /* script-local variables */
6114 for (i = 1; i <= ga_scripts.ga_len; ++i)
6115 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6116
6117 /* buffer-local variables */
6118 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6119 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6120
6121 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006122 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006123 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6124
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006125#ifdef FEAT_WINDOWS
6126 /* tabpage-local variables */
6127 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6128 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6129#endif
6130
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006131 /* global variables */
6132 set_ref_in_ht(&globvarht, copyID);
6133
6134 /* function-local variables */
6135 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6136 {
6137 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6138 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6139 }
6140
6141 /*
6142 * 2. Go through the list of dicts and free items without the copyID.
6143 */
6144 for (dd = first_dict; dd != NULL; )
6145 if (dd->dv_copyID != copyID)
6146 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006147 /* Free the Dictionary and ordinary items it contains, but don't
6148 * recurse into Lists and Dictionaries, they will be in the list
6149 * of dicts or list of lists. */
6150 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006151 did_free = TRUE;
6152
6153 /* restart, next dict may also have been freed */
6154 dd = first_dict;
6155 }
6156 else
6157 dd = dd->dv_used_next;
6158
6159 /*
6160 * 3. Go through the list of lists and free items without the copyID.
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006161 * But don't free a list that has a watcher (used in a for loop), these
6162 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006163 */
6164 for (ll = first_list; ll != NULL; )
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006165 if (ll->lv_copyID != copyID && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006166 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006167 /* Free the List and ordinary items it contains, but don't recurse
6168 * into Lists and Dictionaries, they will be in the list of dicts
6169 * or list of lists. */
6170 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006171 did_free = TRUE;
6172
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006173 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006174 ll = first_list;
6175 }
6176 else
6177 ll = ll->lv_used_next;
6178
6179 return did_free;
6180}
6181
6182/*
6183 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6184 */
6185 static void
6186set_ref_in_ht(ht, copyID)
6187 hashtab_T *ht;
6188 int copyID;
6189{
6190 int todo;
6191 hashitem_T *hi;
6192
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006193 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006194 for (hi = ht->ht_array; todo > 0; ++hi)
6195 if (!HASHITEM_EMPTY(hi))
6196 {
6197 --todo;
6198 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6199 }
6200}
6201
6202/*
6203 * Mark all lists and dicts referenced through list "l" with "copyID".
6204 */
6205 static void
6206set_ref_in_list(l, copyID)
6207 list_T *l;
6208 int copyID;
6209{
6210 listitem_T *li;
6211
6212 for (li = l->lv_first; li != NULL; li = li->li_next)
6213 set_ref_in_item(&li->li_tv, copyID);
6214}
6215
6216/*
6217 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6218 */
6219 static void
6220set_ref_in_item(tv, copyID)
6221 typval_T *tv;
6222 int copyID;
6223{
6224 dict_T *dd;
6225 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006226
6227 switch (tv->v_type)
6228 {
6229 case VAR_DICT:
6230 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006231 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006232 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006233 /* Didn't see this dict yet. */
6234 dd->dv_copyID = copyID;
6235 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006236 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006237 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006238
6239 case VAR_LIST:
6240 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006241 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006242 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006243 /* Didn't see this list yet. */
6244 ll->lv_copyID = copyID;
6245 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006246 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006247 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006248 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006249 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006250}
6251
6252/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006253 * Allocate an empty header for a dictionary.
6254 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006255 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006256dict_alloc()
6257{
Bram Moolenaar33570922005-01-25 22:26:29 +00006258 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006259
Bram Moolenaar33570922005-01-25 22:26:29 +00006260 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006261 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006262 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006263 /* Add the list to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006264 if (first_dict != NULL)
6265 first_dict->dv_used_prev = d;
6266 d->dv_used_next = first_dict;
6267 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006268 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006269
Bram Moolenaar33570922005-01-25 22:26:29 +00006270 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006271 d->dv_lock = 0;
6272 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006273 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006274 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006275 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006276}
6277
6278/*
6279 * Unreference a Dictionary: decrement the reference count and free it when it
6280 * becomes zero.
6281 */
6282 static void
6283dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006284 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006285{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006286 if (d != NULL && --d->dv_refcount <= 0)
6287 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006288}
6289
6290/*
6291 * Free a Dictionary, including all items it contains.
6292 * Ignores the reference count.
6293 */
6294 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006295dict_free(d, recurse)
6296 dict_T *d;
6297 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006298{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006299 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006300 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006301 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006302
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006303 /* Remove the dict from the list of dicts for garbage collection. */
6304 if (d->dv_used_prev == NULL)
6305 first_dict = d->dv_used_next;
6306 else
6307 d->dv_used_prev->dv_used_next = d->dv_used_next;
6308 if (d->dv_used_next != NULL)
6309 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6310
6311 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006312 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006313 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006314 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006315 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006316 if (!HASHITEM_EMPTY(hi))
6317 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006318 /* Remove the item before deleting it, just in case there is
6319 * something recursive causing trouble. */
6320 di = HI2DI(hi);
6321 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00006322 if (recurse || (di->di_tv.v_type != VAR_LIST
6323 && di->di_tv.v_type != VAR_DICT))
6324 clear_tv(&di->di_tv);
6325 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006326 --todo;
6327 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006328 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006329 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006330 vim_free(d);
6331}
6332
6333/*
6334 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006335 * The "key" is copied to the new item.
6336 * Note that the value of the item "di_tv" still needs to be initialized!
6337 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006338 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006339 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006340dictitem_alloc(key)
6341 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006342{
Bram Moolenaar33570922005-01-25 22:26:29 +00006343 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006344
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006345 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006346 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006347 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006348 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006349 di->di_flags = 0;
6350 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006351 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006352}
6353
6354/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006355 * Make a copy of a Dictionary item.
6356 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006357 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006358dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006359 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006360{
Bram Moolenaar33570922005-01-25 22:26:29 +00006361 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006362
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006363 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
6364 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006365 if (di != NULL)
6366 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006367 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006368 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006369 copy_tv(&org->di_tv, &di->di_tv);
6370 }
6371 return di;
6372}
6373
6374/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006375 * Remove item "item" from Dictionary "dict" and free it.
6376 */
6377 static void
6378dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006379 dict_T *dict;
6380 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006381{
Bram Moolenaar33570922005-01-25 22:26:29 +00006382 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006383
Bram Moolenaar33570922005-01-25 22:26:29 +00006384 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006385 if (HASHITEM_EMPTY(hi))
6386 EMSG2(_(e_intern2), "dictitem_remove()");
6387 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006388 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006389 dictitem_free(item);
6390}
6391
6392/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006393 * Free a dict item. Also clears the value.
6394 */
6395 static void
6396dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006397 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006398{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006399 clear_tv(&item->di_tv);
6400 vim_free(item);
6401}
6402
6403/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006404 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6405 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006406 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006407 * Returns NULL when out of memory.
6408 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006409 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006410dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006411 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006412 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006413 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006414{
Bram Moolenaar33570922005-01-25 22:26:29 +00006415 dict_T *copy;
6416 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006417 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006418 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006419
6420 if (orig == NULL)
6421 return NULL;
6422
6423 copy = dict_alloc();
6424 if (copy != NULL)
6425 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006426 if (copyID != 0)
6427 {
6428 orig->dv_copyID = copyID;
6429 orig->dv_copydict = copy;
6430 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006431 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006432 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006433 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006434 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006435 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006436 --todo;
6437
6438 di = dictitem_alloc(hi->hi_key);
6439 if (di == NULL)
6440 break;
6441 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006442 {
6443 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6444 copyID) == FAIL)
6445 {
6446 vim_free(di);
6447 break;
6448 }
6449 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006450 else
6451 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6452 if (dict_add(copy, di) == FAIL)
6453 {
6454 dictitem_free(di);
6455 break;
6456 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006457 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006458 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006459
Bram Moolenaare9a41262005-01-15 22:18:47 +00006460 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006461 if (todo > 0)
6462 {
6463 dict_unref(copy);
6464 copy = NULL;
6465 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006466 }
6467
6468 return copy;
6469}
6470
6471/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006472 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006473 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006474 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006475 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006476dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006477 dict_T *d;
6478 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006479{
Bram Moolenaar33570922005-01-25 22:26:29 +00006480 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006481}
6482
Bram Moolenaar8c711452005-01-14 21:53:12 +00006483/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006484 * Add a number or string entry to dictionary "d".
6485 * When "str" is NULL use number "nr", otherwise use "str".
6486 * Returns FAIL when out of memory and when key already exists.
6487 */
6488 int
6489dict_add_nr_str(d, key, nr, str)
6490 dict_T *d;
6491 char *key;
6492 long nr;
6493 char_u *str;
6494{
6495 dictitem_T *item;
6496
6497 item = dictitem_alloc((char_u *)key);
6498 if (item == NULL)
6499 return FAIL;
6500 item->di_tv.v_lock = 0;
6501 if (str == NULL)
6502 {
6503 item->di_tv.v_type = VAR_NUMBER;
6504 item->di_tv.vval.v_number = nr;
6505 }
6506 else
6507 {
6508 item->di_tv.v_type = VAR_STRING;
6509 item->di_tv.vval.v_string = vim_strsave(str);
6510 }
6511 if (dict_add(d, item) == FAIL)
6512 {
6513 dictitem_free(item);
6514 return FAIL;
6515 }
6516 return OK;
6517}
6518
6519/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006520 * Get the number of items in a Dictionary.
6521 */
6522 static long
6523dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006524 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006525{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006526 if (d == NULL)
6527 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006528 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006529}
6530
6531/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006532 * Find item "key[len]" in Dictionary "d".
6533 * If "len" is negative use strlen(key).
6534 * Returns NULL when not found.
6535 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006536 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006537dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006538 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006539 char_u *key;
6540 int len;
6541{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006542#define AKEYLEN 200
6543 char_u buf[AKEYLEN];
6544 char_u *akey;
6545 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006546 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006547
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006548 if (len < 0)
6549 akey = key;
6550 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006551 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006552 tofree = akey = vim_strnsave(key, len);
6553 if (akey == NULL)
6554 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006555 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006556 else
6557 {
6558 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006559 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006560 akey = buf;
6561 }
6562
Bram Moolenaar33570922005-01-25 22:26:29 +00006563 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006564 vim_free(tofree);
6565 if (HASHITEM_EMPTY(hi))
6566 return NULL;
6567 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006568}
6569
6570/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006571 * Get a string item from a dictionary.
6572 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00006573 * Returns NULL if the entry doesn't exist or out of memory.
6574 */
6575 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006576get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00006577 dict_T *d;
6578 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006579 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006580{
6581 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006582 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006583
6584 di = dict_find(d, key, -1);
6585 if (di == NULL)
6586 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006587 s = get_tv_string(&di->di_tv);
6588 if (save && s != NULL)
6589 s = vim_strsave(s);
6590 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006591}
6592
6593/*
6594 * Get a number item from a dictionary.
6595 * Returns 0 if the entry doesn't exist or out of memory.
6596 */
6597 long
6598get_dict_number(d, key)
6599 dict_T *d;
6600 char_u *key;
6601{
6602 dictitem_T *di;
6603
6604 di = dict_find(d, key, -1);
6605 if (di == NULL)
6606 return 0;
6607 return get_tv_number(&di->di_tv);
6608}
6609
6610/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006611 * Return an allocated string with the string representation of a Dictionary.
6612 * May return NULL.
6613 */
6614 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006615dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006616 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006617 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006618{
6619 garray_T ga;
6620 int first = TRUE;
6621 char_u *tofree;
6622 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006623 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006624 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006625 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006626 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006627
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006628 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006629 return NULL;
6630 ga_init2(&ga, (int)sizeof(char), 80);
6631 ga_append(&ga, '{');
6632
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006633 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006634 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006635 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006636 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006637 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006638 --todo;
6639
6640 if (first)
6641 first = FALSE;
6642 else
6643 ga_concat(&ga, (char_u *)", ");
6644
6645 tofree = string_quote(hi->hi_key, FALSE);
6646 if (tofree != NULL)
6647 {
6648 ga_concat(&ga, tofree);
6649 vim_free(tofree);
6650 }
6651 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006652 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006653 if (s != NULL)
6654 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006655 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006656 if (s == NULL)
6657 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006658 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006659 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006660 if (todo > 0)
6661 {
6662 vim_free(ga.ga_data);
6663 return NULL;
6664 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006665
6666 ga_append(&ga, '}');
6667 ga_append(&ga, NUL);
6668 return (char_u *)ga.ga_data;
6669}
6670
6671/*
6672 * Allocate a variable for a Dictionary and fill it from "*arg".
6673 * Return OK or FAIL. Returns NOTDONE for {expr}.
6674 */
6675 static int
6676get_dict_tv(arg, rettv, evaluate)
6677 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006678 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006679 int evaluate;
6680{
Bram Moolenaar33570922005-01-25 22:26:29 +00006681 dict_T *d = NULL;
6682 typval_T tvkey;
6683 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006684 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00006685 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006686 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006687 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00006688
6689 /*
6690 * First check if it's not a curly-braces thing: {expr}.
6691 * Must do this without evaluating, otherwise a function may be called
6692 * twice. Unfortunately this means we need to call eval1() twice for the
6693 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006694 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006695 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006696 if (*start != '}')
6697 {
6698 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
6699 return FAIL;
6700 if (*start == '}')
6701 return NOTDONE;
6702 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006703
6704 if (evaluate)
6705 {
6706 d = dict_alloc();
6707 if (d == NULL)
6708 return FAIL;
6709 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006710 tvkey.v_type = VAR_UNKNOWN;
6711 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006712
6713 *arg = skipwhite(*arg + 1);
6714 while (**arg != '}' && **arg != NUL)
6715 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006716 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006717 goto failret;
6718 if (**arg != ':')
6719 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006720 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006721 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006722 goto failret;
6723 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006724 key = get_tv_string_buf_chk(&tvkey, buf);
6725 if (key == NULL || *key == NUL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006726 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006727 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6728 if (key != NULL)
6729 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006730 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006731 goto failret;
6732 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006733
6734 *arg = skipwhite(*arg + 1);
6735 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6736 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006737 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006738 goto failret;
6739 }
6740 if (evaluate)
6741 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006742 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006743 if (item != NULL)
6744 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00006745 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006746 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006747 clear_tv(&tv);
6748 goto failret;
6749 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006750 item = dictitem_alloc(key);
6751 clear_tv(&tvkey);
6752 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006753 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006754 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006755 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006756 if (dict_add(d, item) == FAIL)
6757 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006758 }
6759 }
6760
6761 if (**arg == '}')
6762 break;
6763 if (**arg != ',')
6764 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006765 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006766 goto failret;
6767 }
6768 *arg = skipwhite(*arg + 1);
6769 }
6770
6771 if (**arg != '}')
6772 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006773 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006774failret:
6775 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00006776 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006777 return FAIL;
6778 }
6779
6780 *arg = skipwhite(*arg + 1);
6781 if (evaluate)
6782 {
6783 rettv->v_type = VAR_DICT;
6784 rettv->vval.v_dict = d;
6785 ++d->dv_refcount;
6786 }
6787
6788 return OK;
6789}
6790
Bram Moolenaar8c711452005-01-14 21:53:12 +00006791/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006792 * Return a string with the string representation of a variable.
6793 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006794 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006795 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006796 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006797 * May return NULL;
6798 */
6799 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006800echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006801 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006802 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006803 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006804 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006805{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006806 static int recurse = 0;
6807 char_u *r = NULL;
6808
Bram Moolenaar33570922005-01-25 22:26:29 +00006809 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006810 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006811 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006812 *tofree = NULL;
6813 return NULL;
6814 }
6815 ++recurse;
6816
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006817 switch (tv->v_type)
6818 {
6819 case VAR_FUNC:
6820 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006821 r = tv->vval.v_string;
6822 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006823
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006824 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006825 if (tv->vval.v_list == NULL)
6826 {
6827 *tofree = NULL;
6828 r = NULL;
6829 }
6830 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
6831 {
6832 *tofree = NULL;
6833 r = (char_u *)"[...]";
6834 }
6835 else
6836 {
6837 tv->vval.v_list->lv_copyID = copyID;
6838 *tofree = list2string(tv, copyID);
6839 r = *tofree;
6840 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006841 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006842
Bram Moolenaar8c711452005-01-14 21:53:12 +00006843 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006844 if (tv->vval.v_dict == NULL)
6845 {
6846 *tofree = NULL;
6847 r = NULL;
6848 }
6849 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
6850 {
6851 *tofree = NULL;
6852 r = (char_u *)"{...}";
6853 }
6854 else
6855 {
6856 tv->vval.v_dict->dv_copyID = copyID;
6857 *tofree = dict2string(tv, copyID);
6858 r = *tofree;
6859 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006860 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006861
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006862 case VAR_STRING:
6863 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006864 *tofree = NULL;
6865 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006866 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006867
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006868 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006869 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006870 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006871 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006872
6873 --recurse;
6874 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006875}
6876
6877/*
6878 * Return a string with the string representation of a variable.
6879 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6880 * "numbuf" is used for a number.
6881 * Puts quotes around strings, so that they can be parsed back by eval().
6882 * May return NULL;
6883 */
6884 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006885tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006886 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006887 char_u **tofree;
6888 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006889 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006890{
6891 switch (tv->v_type)
6892 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006893 case VAR_FUNC:
6894 *tofree = string_quote(tv->vval.v_string, TRUE);
6895 return *tofree;
6896 case VAR_STRING:
6897 *tofree = string_quote(tv->vval.v_string, FALSE);
6898 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006899 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006900 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006901 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006902 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006903 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006904 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006905 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006906 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006907}
6908
6909/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006910 * Return string "str" in ' quotes, doubling ' characters.
6911 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006912 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006913 */
6914 static char_u *
6915string_quote(str, function)
6916 char_u *str;
6917 int function;
6918{
Bram Moolenaar33570922005-01-25 22:26:29 +00006919 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006920 char_u *p, *r, *s;
6921
Bram Moolenaar33570922005-01-25 22:26:29 +00006922 len = (function ? 13 : 3);
6923 if (str != NULL)
6924 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006925 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00006926 for (p = str; *p != NUL; mb_ptr_adv(p))
6927 if (*p == '\'')
6928 ++len;
6929 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006930 s = r = alloc(len);
6931 if (r != NULL)
6932 {
6933 if (function)
6934 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006935 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006936 r += 10;
6937 }
6938 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006939 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006940 if (str != NULL)
6941 for (p = str; *p != NUL; )
6942 {
6943 if (*p == '\'')
6944 *r++ = '\'';
6945 MB_COPY_CHAR(p, r);
6946 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006947 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006948 if (function)
6949 *r++ = ')';
6950 *r++ = NUL;
6951 }
6952 return s;
6953}
6954
6955/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 * Get the value of an environment variable.
6957 * "arg" is pointing to the '$'. It is advanced to after the name.
6958 * If the environment variable was not set, silently assume it is empty.
6959 * Always return OK.
6960 */
6961 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006962get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006964 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 int evaluate;
6966{
6967 char_u *string = NULL;
6968 int len;
6969 int cc;
6970 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006971 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972
6973 ++*arg;
6974 name = *arg;
6975 len = get_env_len(arg);
6976 if (evaluate)
6977 {
6978 if (len != 0)
6979 {
6980 cc = name[len];
6981 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006982 /* first try vim_getenv(), fast for normal environment vars */
6983 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006985 {
6986 if (!mustfree)
6987 string = vim_strsave(string);
6988 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 else
6990 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00006991 if (mustfree)
6992 vim_free(string);
6993
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 /* next try expanding things like $VIM and ${HOME} */
6995 string = expand_env_save(name - 1);
6996 if (string != NULL && *string == '$')
6997 {
6998 vim_free(string);
6999 string = NULL;
7000 }
7001 }
7002 name[len] = cc;
7003 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007004 rettv->v_type = VAR_STRING;
7005 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 }
7007
7008 return OK;
7009}
7010
7011/*
7012 * Array with names and number of arguments of all internal functions
7013 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7014 */
7015static struct fst
7016{
7017 char *f_name; /* function name */
7018 char f_min_argc; /* minimal number of arguments */
7019 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007020 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007021 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022} functions[] =
7023{
Bram Moolenaar0d660222005-01-07 21:51:51 +00007024 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025 {"append", 2, 2, f_append},
7026 {"argc", 0, 0, f_argc},
7027 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007028 {"argv", 0, 1, f_argv},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007030 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 {"bufexists", 1, 1, f_bufexists},
7032 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7033 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7034 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7035 {"buflisted", 1, 1, f_buflisted},
7036 {"bufloaded", 1, 1, f_bufloaded},
7037 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007038 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039 {"bufwinnr", 1, 1, f_bufwinnr},
7040 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007041 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007042 {"call", 2, 3, f_call},
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007043 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 {"char2nr", 1, 1, f_char2nr},
7045 {"cindent", 1, 1, f_cindent},
7046 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007047#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007048 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007049 {"complete_add", 1, 1, f_complete_add},
7050 {"complete_check", 0, 0, f_complete_check},
7051#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007053 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007054 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007056 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007057 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 {"delete", 1, 1, f_delete},
7059 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007060 {"diff_filler", 1, 1, f_diff_filler},
7061 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007062 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007064 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 {"eventhandler", 0, 0, f_eventhandler},
7066 {"executable", 1, 1, f_executable},
7067 {"exists", 1, 1, f_exists},
7068 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007069 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007070 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7072 {"filereadable", 1, 1, f_filereadable},
7073 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007074 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007075 {"finddir", 1, 3, f_finddir},
7076 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077 {"fnamemodify", 2, 2, f_fnamemodify},
7078 {"foldclosed", 1, 1, f_foldclosed},
7079 {"foldclosedend", 1, 1, f_foldclosedend},
7080 {"foldlevel", 1, 1, f_foldlevel},
7081 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007082 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007084 {"function", 1, 1, f_function},
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007085 {"garbagecollect", 0, 0, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007086 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007087 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 {"getbufvar", 2, 2, f_getbufvar},
7089 {"getchar", 0, 1, f_getchar},
7090 {"getcharmod", 0, 0, f_getcharmod},
7091 {"getcmdline", 0, 0, f_getcmdline},
7092 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007093 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007095 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007096 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097 {"getfsize", 1, 1, f_getfsize},
7098 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007099 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007100 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007101 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaara5525202006-03-02 22:52:09 +00007102 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007103 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007104 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007106 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107 {"getwinposx", 0, 0, f_getwinposx},
7108 {"getwinposy", 0, 0, f_getwinposy},
7109 {"getwinvar", 2, 2, f_getwinvar},
7110 {"glob", 1, 1, f_glob},
7111 {"globpath", 2, 2, f_globpath},
7112 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007113 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007114 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007115 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7117 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7118 {"histadd", 2, 2, f_histadd},
7119 {"histdel", 1, 2, f_histdel},
7120 {"histget", 1, 2, f_histget},
7121 {"histnr", 1, 1, f_histnr},
7122 {"hlID", 1, 1, f_hlID},
7123 {"hlexists", 1, 1, f_hlexists},
7124 {"hostname", 0, 0, f_hostname},
7125 {"iconv", 3, 3, f_iconv},
7126 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007127 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007128 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007130 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131 {"inputrestore", 0, 0, f_inputrestore},
7132 {"inputsave", 0, 0, f_inputsave},
7133 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007134 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007136 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007137 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007138 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007139 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007141 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 {"libcall", 3, 3, f_libcall},
7143 {"libcallnr", 3, 3, f_libcallnr},
7144 {"line", 1, 1, f_line},
7145 {"line2byte", 1, 1, f_line2byte},
7146 {"lispindent", 1, 1, f_lispindent},
7147 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007148 {"map", 2, 2, f_map},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007149 {"maparg", 1, 3, f_maparg},
7150 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007151 {"match", 2, 4, f_match},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007152 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007153 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007154 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007155 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007156 {"max", 1, 1, f_max},
7157 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007158#ifdef vim_mkdir
7159 {"mkdir", 1, 3, f_mkdir},
7160#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 {"mode", 0, 0, f_mode},
7162 {"nextnonblank", 1, 1, f_nextnonblank},
7163 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007164 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007166 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007167 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007168 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007169 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007170 {"reltime", 0, 2, f_reltime},
7171 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172 {"remote_expr", 2, 3, f_remote_expr},
7173 {"remote_foreground", 1, 1, f_remote_foreground},
7174 {"remote_peek", 1, 2, f_remote_peek},
7175 {"remote_read", 1, 1, f_remote_read},
7176 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007177 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007179 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007181 {"reverse", 1, 1, f_reverse},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00007182 {"search", 1, 3, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007183 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00007184 {"searchpair", 3, 6, f_searchpair},
7185 {"searchpairpos", 3, 6, f_searchpairpos},
7186 {"searchpos", 1, 3, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 {"server2client", 2, 2, f_server2client},
7188 {"serverlist", 0, 0, f_serverlist},
7189 {"setbufvar", 3, 3, f_setbufvar},
7190 {"setcmdpos", 1, 1, f_setcmdpos},
7191 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007192 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007193 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007194 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195 {"setreg", 2, 3, f_setreg},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007196 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar60a495f2006-10-03 12:44:42 +00007198 {"shellescape", 1, 1, f_shellescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007200 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007201 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007202 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007203 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007204 {"split", 1, 3, f_split},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007205 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206#ifdef HAVE_STRFTIME
7207 {"strftime", 1, 2, f_strftime},
7208#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007209 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007210 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211 {"strlen", 1, 1, f_strlen},
7212 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007213 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214 {"strtrans", 1, 1, f_strtrans},
7215 {"submatch", 1, 1, f_submatch},
7216 {"substitute", 4, 4, f_substitute},
7217 {"synID", 3, 3, f_synID},
7218 {"synIDattr", 2, 3, f_synIDattr},
7219 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007220 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007221 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007222 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007223 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007224 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007225 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007227 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228 {"tolower", 1, 1, f_tolower},
7229 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007230 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007232 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 {"virtcol", 1, 1, f_virtcol},
7234 {"visualmode", 0, 1, f_visualmode},
7235 {"winbufnr", 1, 1, f_winbufnr},
7236 {"wincol", 0, 0, f_wincol},
7237 {"winheight", 1, 1, f_winheight},
7238 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007239 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007241 {"winrestview", 1, 1, f_winrestview},
7242 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007244 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245};
7246
7247#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7248
7249/*
7250 * Function given to ExpandGeneric() to obtain the list of internal
7251 * or user defined function names.
7252 */
7253 char_u *
7254get_function_name(xp, idx)
7255 expand_T *xp;
7256 int idx;
7257{
7258 static int intidx = -1;
7259 char_u *name;
7260
7261 if (idx == 0)
7262 intidx = -1;
7263 if (intidx < 0)
7264 {
7265 name = get_user_func_name(xp, idx);
7266 if (name != NULL)
7267 return name;
7268 }
7269 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7270 {
7271 STRCPY(IObuff, functions[intidx].f_name);
7272 STRCAT(IObuff, "(");
7273 if (functions[intidx].f_max_argc == 0)
7274 STRCAT(IObuff, ")");
7275 return IObuff;
7276 }
7277
7278 return NULL;
7279}
7280
7281/*
7282 * Function given to ExpandGeneric() to obtain the list of internal or
7283 * user defined variable or function names.
7284 */
7285/*ARGSUSED*/
7286 char_u *
7287get_expr_name(xp, idx)
7288 expand_T *xp;
7289 int idx;
7290{
7291 static int intidx = -1;
7292 char_u *name;
7293
7294 if (idx == 0)
7295 intidx = -1;
7296 if (intidx < 0)
7297 {
7298 name = get_function_name(xp, idx);
7299 if (name != NULL)
7300 return name;
7301 }
7302 return get_user_var_name(xp, ++intidx);
7303}
7304
7305#endif /* FEAT_CMDL_COMPL */
7306
7307/*
7308 * Find internal function in table above.
7309 * Return index, or -1 if not found
7310 */
7311 static int
7312find_internal_func(name)
7313 char_u *name; /* name of the function */
7314{
7315 int first = 0;
7316 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7317 int cmp;
7318 int x;
7319
7320 /*
7321 * Find the function name in the table. Binary search.
7322 */
7323 while (first <= last)
7324 {
7325 x = first + ((unsigned)(last - first) >> 1);
7326 cmp = STRCMP(name, functions[x].f_name);
7327 if (cmp < 0)
7328 last = x - 1;
7329 else if (cmp > 0)
7330 first = x + 1;
7331 else
7332 return x;
7333 }
7334 return -1;
7335}
7336
7337/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007338 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7339 * name it contains, otherwise return "name".
7340 */
7341 static char_u *
7342deref_func_name(name, lenp)
7343 char_u *name;
7344 int *lenp;
7345{
Bram Moolenaar33570922005-01-25 22:26:29 +00007346 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007347 int cc;
7348
7349 cc = name[*lenp];
7350 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007351 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007352 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007353 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007354 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007355 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007356 {
7357 *lenp = 0;
7358 return (char_u *)""; /* just in case */
7359 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007360 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00007361 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007362 }
7363
7364 return name;
7365}
7366
7367/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368 * Allocate a variable for the result of a function.
7369 * Return OK or FAIL.
7370 */
7371 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007372get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7373 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374 char_u *name; /* name of the function */
7375 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007376 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377 char_u **arg; /* argument, pointing to the '(' */
7378 linenr_T firstline; /* first line of range */
7379 linenr_T lastline; /* last line of range */
7380 int *doesrange; /* return: function handled range */
7381 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007382 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383{
7384 char_u *argp;
7385 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007386 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387 int argcount = 0; /* number of arguments found */
7388
7389 /*
7390 * Get the arguments.
7391 */
7392 argp = *arg;
7393 while (argcount < MAX_FUNC_ARGS)
7394 {
7395 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7396 if (*argp == ')' || *argp == ',' || *argp == NUL)
7397 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7399 {
7400 ret = FAIL;
7401 break;
7402 }
7403 ++argcount;
7404 if (*argp != ',')
7405 break;
7406 }
7407 if (*argp == ')')
7408 ++argp;
7409 else
7410 ret = FAIL;
7411
7412 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007413 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007414 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007416 {
7417 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007418 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007419 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007420 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422
7423 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007424 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425
7426 *arg = skipwhite(argp);
7427 return ret;
7428}
7429
7430
7431/*
7432 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00007433 * Return OK when the function can't be called, FAIL otherwise.
7434 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435 */
7436 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007437call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007438 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439 char_u *name; /* name of the function */
7440 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007441 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007443 typval_T *argvars; /* vars for arguments, must have "argcount"
7444 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445 linenr_T firstline; /* first line of range */
7446 linenr_T lastline; /* last line of range */
7447 int *doesrange; /* return: function handled range */
7448 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007449 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450{
7451 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452#define ERROR_UNKNOWN 0
7453#define ERROR_TOOMANY 1
7454#define ERROR_TOOFEW 2
7455#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007456#define ERROR_DICT 4
7457#define ERROR_NONE 5
7458#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459 int error = ERROR_NONE;
7460 int i;
7461 int llen;
7462 ufunc_T *fp;
7463 int cc;
7464#define FLEN_FIXED 40
7465 char_u fname_buf[FLEN_FIXED + 1];
7466 char_u *fname;
7467
7468 /*
7469 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7470 * Change <SNR>123_name() to K_SNR 123_name().
7471 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7472 */
7473 cc = name[len];
7474 name[len] = NUL;
7475 llen = eval_fname_script(name);
7476 if (llen > 0)
7477 {
7478 fname_buf[0] = K_SPECIAL;
7479 fname_buf[1] = KS_EXTRA;
7480 fname_buf[2] = (int)KE_SNR;
7481 i = 3;
7482 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7483 {
7484 if (current_SID <= 0)
7485 error = ERROR_SCRIPT;
7486 else
7487 {
7488 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7489 i = (int)STRLEN(fname_buf);
7490 }
7491 }
7492 if (i + STRLEN(name + llen) < FLEN_FIXED)
7493 {
7494 STRCPY(fname_buf + i, name + llen);
7495 fname = fname_buf;
7496 }
7497 else
7498 {
7499 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7500 if (fname == NULL)
7501 error = ERROR_OTHER;
7502 else
7503 {
7504 mch_memmove(fname, fname_buf, (size_t)i);
7505 STRCPY(fname + i, name + llen);
7506 }
7507 }
7508 }
7509 else
7510 fname = name;
7511
7512 *doesrange = FALSE;
7513
7514
7515 /* execute the function if no errors detected and executing */
7516 if (evaluate && error == ERROR_NONE)
7517 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007518 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519 error = ERROR_UNKNOWN;
7520
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007521 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522 {
7523 /*
7524 * User defined function.
7525 */
7526 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007527
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007529 /* Trigger FuncUndefined event, may load the function. */
7530 if (fp == NULL
7531 && apply_autocmds(EVENT_FUNCUNDEFINED,
7532 fname, fname, TRUE, NULL)
7533 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007535 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536 fp = find_func(fname);
7537 }
7538#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007539 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007540 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007541 {
7542 /* loaded a package, search for the function again */
7543 fp = find_func(fname);
7544 }
7545
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546 if (fp != NULL)
7547 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007548 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007550 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007552 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007554 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007555 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556 else
7557 {
7558 /*
7559 * Call the user function.
7560 * Save and restore search patterns, script variables and
7561 * redo buffer.
7562 */
7563 save_search_patterns();
7564 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007565 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007566 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007567 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007568 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7569 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7570 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007571 /* Function was unreferenced while being used, free it
7572 * now. */
7573 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 restoreRedobuff();
7575 restore_search_patterns();
7576 error = ERROR_NONE;
7577 }
7578 }
7579 }
7580 else
7581 {
7582 /*
7583 * Find the function name in the table, call its implementation.
7584 */
7585 i = find_internal_func(fname);
7586 if (i >= 0)
7587 {
7588 if (argcount < functions[i].f_min_argc)
7589 error = ERROR_TOOFEW;
7590 else if (argcount > functions[i].f_max_argc)
7591 error = ERROR_TOOMANY;
7592 else
7593 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007594 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007595 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596 error = ERROR_NONE;
7597 }
7598 }
7599 }
7600 /*
7601 * The function call (or "FuncUndefined" autocommand sequence) might
7602 * have been aborted by an error, an interrupt, or an explicitly thrown
7603 * exception that has not been caught so far. This situation can be
7604 * tested for by calling aborting(). For an error in an internal
7605 * function or for the "E132" error in call_user_func(), however, the
7606 * throw point at which the "force_abort" flag (temporarily reset by
7607 * emsg()) is normally updated has not been reached yet. We need to
7608 * update that flag first to make aborting() reliable.
7609 */
7610 update_force_abort();
7611 }
7612 if (error == ERROR_NONE)
7613 ret = OK;
7614
7615 /*
7616 * Report an error unless the argument evaluation or function call has been
7617 * cancelled due to an aborting error, an interrupt, or an exception.
7618 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007619 if (!aborting())
7620 {
7621 switch (error)
7622 {
7623 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007624 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007625 break;
7626 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007627 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007628 break;
7629 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007630 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00007631 name);
7632 break;
7633 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007634 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00007635 name);
7636 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007637 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007638 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00007639 name);
7640 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007641 }
7642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643
7644 name[len] = cc;
7645 if (fname != name && fname != fname_buf)
7646 vim_free(fname);
7647
7648 return ret;
7649}
7650
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007651/*
7652 * Give an error message with a function name. Handle <SNR> things.
7653 */
7654 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00007655emsg_funcname(ermsg, name)
7656 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007657 char_u *name;
7658{
7659 char_u *p;
7660
7661 if (*name == K_SPECIAL)
7662 p = concat_str((char_u *)"<SNR>", name + 3);
7663 else
7664 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00007665 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007666 if (p != name)
7667 vim_free(p);
7668}
7669
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670/*********************************************
7671 * Implementation of the built-in functions
7672 */
7673
7674/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007675 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676 */
7677 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007678f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007679 typval_T *argvars;
7680 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681{
Bram Moolenaar33570922005-01-25 22:26:29 +00007682 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007684 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007685 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007687 if ((l = argvars[0].vval.v_list) != NULL
7688 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7689 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007690 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007691 }
7692 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007693 EMSG(_(e_listreq));
7694}
7695
7696/*
7697 * "append(lnum, string/list)" function
7698 */
7699 static void
7700f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007701 typval_T *argvars;
7702 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007703{
7704 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007705 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007706 list_T *l = NULL;
7707 listitem_T *li = NULL;
7708 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007709 long added = 0;
7710
Bram Moolenaar0d660222005-01-07 21:51:51 +00007711 lnum = get_tv_lnum(argvars);
7712 if (lnum >= 0
7713 && lnum <= curbuf->b_ml.ml_line_count
7714 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007715 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007716 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007717 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007718 l = argvars[1].vval.v_list;
7719 if (l == NULL)
7720 return;
7721 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007722 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007723 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007724 for (;;)
7725 {
7726 if (l == NULL)
7727 tv = &argvars[1]; /* append a string */
7728 else if (li == NULL)
7729 break; /* end of list */
7730 else
7731 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007732 line = get_tv_string_chk(tv);
7733 if (line == NULL) /* type error */
7734 {
7735 rettv->vval.v_number = 1; /* Failed */
7736 break;
7737 }
7738 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007739 ++added;
7740 if (l == NULL)
7741 break;
7742 li = li->li_next;
7743 }
7744
7745 appended_lines_mark(lnum, added);
7746 if (curwin->w_cursor.lnum > lnum)
7747 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007749 else
7750 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751}
7752
7753/*
7754 * "argc()" function
7755 */
7756/* ARGSUSED */
7757 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007758f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007759 typval_T *argvars;
7760 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007762 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763}
7764
7765/*
7766 * "argidx()" function
7767 */
7768/* ARGSUSED */
7769 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007770f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007771 typval_T *argvars;
7772 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007774 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775}
7776
7777/*
7778 * "argv(nr)" function
7779 */
7780 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007781f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007782 typval_T *argvars;
7783 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784{
7785 int idx;
7786
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007787 if (argvars[0].v_type != VAR_UNKNOWN)
7788 {
7789 idx = get_tv_number_chk(&argvars[0], NULL);
7790 if (idx >= 0 && idx < ARGCOUNT)
7791 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
7792 else
7793 rettv->vval.v_string = NULL;
7794 rettv->v_type = VAR_STRING;
7795 }
7796 else if (rettv_list_alloc(rettv) == OK)
7797 for (idx = 0; idx < ARGCOUNT; ++idx)
7798 list_append_string(rettv->vval.v_list,
7799 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800}
7801
7802/*
7803 * "browse(save, title, initdir, default)" function
7804 */
7805/* ARGSUSED */
7806 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007807f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007808 typval_T *argvars;
7809 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810{
7811#ifdef FEAT_BROWSE
7812 int save;
7813 char_u *title;
7814 char_u *initdir;
7815 char_u *defname;
7816 char_u buf[NUMBUFLEN];
7817 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007818 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007820 save = get_tv_number_chk(&argvars[0], &error);
7821 title = get_tv_string_chk(&argvars[1]);
7822 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7823 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007825 if (error || title == NULL || initdir == NULL || defname == NULL)
7826 rettv->vval.v_string = NULL;
7827 else
7828 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007829 do_browse(save ? BROWSE_SAVE : 0,
7830 title, defname, NULL, initdir, NULL, curbuf);
7831#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007832 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007833#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007834 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007835}
7836
7837/*
7838 * "browsedir(title, initdir)" function
7839 */
7840/* ARGSUSED */
7841 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007842f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007843 typval_T *argvars;
7844 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007845{
7846#ifdef FEAT_BROWSE
7847 char_u *title;
7848 char_u *initdir;
7849 char_u buf[NUMBUFLEN];
7850
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007851 title = get_tv_string_chk(&argvars[0]);
7852 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007853
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007854 if (title == NULL || initdir == NULL)
7855 rettv->vval.v_string = NULL;
7856 else
7857 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007858 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007860 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007862 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863}
7864
Bram Moolenaar33570922005-01-25 22:26:29 +00007865static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007866
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867/*
7868 * Find a buffer by number or exact name.
7869 */
7870 static buf_T *
7871find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007872 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873{
7874 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007876 if (avar->v_type == VAR_NUMBER)
7877 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007878 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007880 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007881 if (buf == NULL)
7882 {
7883 /* No full path name match, try a match with a URL or a "nofile"
7884 * buffer, these don't use the full path. */
7885 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7886 if (buf->b_fname != NULL
7887 && (path_with_url(buf->b_fname)
7888#ifdef FEAT_QUICKFIX
7889 || bt_nofile(buf)
7890#endif
7891 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007892 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007893 break;
7894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 }
7896 return buf;
7897}
7898
7899/*
7900 * "bufexists(expr)" function
7901 */
7902 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007903f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007904 typval_T *argvars;
7905 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007907 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908}
7909
7910/*
7911 * "buflisted(expr)" function
7912 */
7913 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007914f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007915 typval_T *argvars;
7916 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007917{
7918 buf_T *buf;
7919
7920 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007921 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922}
7923
7924/*
7925 * "bufloaded(expr)" function
7926 */
7927 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007928f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007929 typval_T *argvars;
7930 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931{
7932 buf_T *buf;
7933
7934 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007935 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936}
7937
Bram Moolenaar33570922005-01-25 22:26:29 +00007938static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007939
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940/*
7941 * Get buffer by number or pattern.
7942 */
7943 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007944get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007945 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007947 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 int save_magic;
7949 char_u *save_cpo;
7950 buf_T *buf;
7951
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007952 if (tv->v_type == VAR_NUMBER)
7953 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007954 if (tv->v_type != VAR_STRING)
7955 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956 if (name == NULL || *name == NUL)
7957 return curbuf;
7958 if (name[0] == '$' && name[1] == NUL)
7959 return lastbuf;
7960
7961 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7962 save_magic = p_magic;
7963 p_magic = TRUE;
7964 save_cpo = p_cpo;
7965 p_cpo = (char_u *)"";
7966
7967 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
7968 TRUE, FALSE));
7969
7970 p_magic = save_magic;
7971 p_cpo = save_cpo;
7972
7973 /* If not found, try expanding the name, like done for bufexists(). */
7974 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007975 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976
7977 return buf;
7978}
7979
7980/*
7981 * "bufname(expr)" function
7982 */
7983 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007984f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007985 typval_T *argvars;
7986 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987{
7988 buf_T *buf;
7989
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007990 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007992 buf = get_buf_tv(&argvars[0]);
7993 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007995 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007997 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998 --emsg_off;
7999}
8000
8001/*
8002 * "bufnr(expr)" function
8003 */
8004 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008005f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008006 typval_T *argvars;
8007 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008{
8009 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008010 int error = FALSE;
8011 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008013 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008015 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008016 --emsg_off;
8017
8018 /* If the buffer isn't found and the second argument is not zero create a
8019 * new buffer. */
8020 if (buf == NULL
8021 && argvars[1].v_type != VAR_UNKNOWN
8022 && get_tv_number_chk(&argvars[1], &error) != 0
8023 && !error
8024 && (name = get_tv_string_chk(&argvars[0])) != NULL
8025 && !error)
8026 buf = buflist_new(name, NULL, (linenr_T)1, 0);
8027
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008029 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008031 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032}
8033
8034/*
8035 * "bufwinnr(nr)" function
8036 */
8037 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008038f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008039 typval_T *argvars;
8040 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041{
8042#ifdef FEAT_WINDOWS
8043 win_T *wp;
8044 int winnr = 0;
8045#endif
8046 buf_T *buf;
8047
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008048 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008050 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051#ifdef FEAT_WINDOWS
8052 for (wp = firstwin; wp; wp = wp->w_next)
8053 {
8054 ++winnr;
8055 if (wp->w_buffer == buf)
8056 break;
8057 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008058 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008060 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061#endif
8062 --emsg_off;
8063}
8064
8065/*
8066 * "byte2line(byte)" function
8067 */
8068/*ARGSUSED*/
8069 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008070f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008071 typval_T *argvars;
8072 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073{
8074#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008075 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076#else
8077 long boff = 0;
8078
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008079 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008081 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008083 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084 (linenr_T)0, &boff);
8085#endif
8086}
8087
8088/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008089 * "byteidx()" function
8090 */
8091/*ARGSUSED*/
8092 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008093f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008094 typval_T *argvars;
8095 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008096{
8097#ifdef FEAT_MBYTE
8098 char_u *t;
8099#endif
8100 char_u *str;
8101 long idx;
8102
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008103 str = get_tv_string_chk(&argvars[0]);
8104 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008105 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008106 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008107 return;
8108
8109#ifdef FEAT_MBYTE
8110 t = str;
8111 for ( ; idx > 0; idx--)
8112 {
8113 if (*t == NUL) /* EOL reached */
8114 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008115 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008116 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008117 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008118#else
8119 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008120 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008121#endif
8122}
8123
8124/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008125 * "call(func, arglist)" function
8126 */
8127 static void
8128f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008129 typval_T *argvars;
8130 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008131{
8132 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008133 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008134 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008135 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008136 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00008137 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008138
8139 rettv->vval.v_number = 0;
8140 if (argvars[1].v_type != VAR_LIST)
8141 {
8142 EMSG(_(e_listreq));
8143 return;
8144 }
8145 if (argvars[1].vval.v_list == NULL)
8146 return;
8147
8148 if (argvars[0].v_type == VAR_FUNC)
8149 func = argvars[0].vval.v_string;
8150 else
8151 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008152 if (*func == NUL)
8153 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008154
Bram Moolenaare9a41262005-01-15 22:18:47 +00008155 if (argvars[2].v_type != VAR_UNKNOWN)
8156 {
8157 if (argvars[2].v_type != VAR_DICT)
8158 {
8159 EMSG(_(e_dictreq));
8160 return;
8161 }
8162 selfdict = argvars[2].vval.v_dict;
8163 }
8164
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008165 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
8166 item = item->li_next)
8167 {
8168 if (argc == MAX_FUNC_ARGS)
8169 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008170 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008171 break;
8172 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008173 /* Make a copy of each argument. This is needed to be able to set
8174 * v_lock to VAR_FIXED in the copy without changing the original list.
8175 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008176 copy_tv(&item->li_tv, &argv[argc++]);
8177 }
8178
8179 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008180 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008181 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
8182 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008183
8184 /* Free the arguments. */
8185 while (argc > 0)
8186 clear_tv(&argv[--argc]);
8187}
8188
8189/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008190 * "changenr()" function
8191 */
8192/*ARGSUSED*/
8193 static void
8194f_changenr(argvars, rettv)
8195 typval_T *argvars;
8196 typval_T *rettv;
8197{
8198 rettv->vval.v_number = curbuf->b_u_seq_cur;
8199}
8200
8201/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202 * "char2nr(string)" function
8203 */
8204 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008205f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008206 typval_T *argvars;
8207 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208{
8209#ifdef FEAT_MBYTE
8210 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008211 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212 else
8213#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008214 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215}
8216
8217/*
8218 * "cindent(lnum)" function
8219 */
8220 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008221f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008222 typval_T *argvars;
8223 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224{
8225#ifdef FEAT_CINDENT
8226 pos_T pos;
8227 linenr_T lnum;
8228
8229 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008230 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8232 {
8233 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008234 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235 curwin->w_cursor = pos;
8236 }
8237 else
8238#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008239 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240}
8241
8242/*
8243 * "col(string)" function
8244 */
8245 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008246f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008247 typval_T *argvars;
8248 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249{
8250 colnr_T col = 0;
8251 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008252 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008254 fp = var2fpos(&argvars[0], FALSE, &fnum);
8255 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256 {
8257 if (fp->col == MAXCOL)
8258 {
8259 /* '> can be MAXCOL, get the length of the line then */
8260 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008261 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262 else
8263 col = MAXCOL;
8264 }
8265 else
8266 {
8267 col = fp->col + 1;
8268#ifdef FEAT_VIRTUALEDIT
8269 /* col(".") when the cursor is on the NUL at the end of the line
8270 * because of "coladd" can be seen as an extra column. */
8271 if (virtual_active() && fp == &curwin->w_cursor)
8272 {
8273 char_u *p = ml_get_cursor();
8274
8275 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
8276 curwin->w_virtcol - curwin->w_cursor.coladd))
8277 {
8278# ifdef FEAT_MBYTE
8279 int l;
8280
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008281 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282 col += l;
8283# else
8284 if (*p != NUL && p[1] == NUL)
8285 ++col;
8286# endif
8287 }
8288 }
8289#endif
8290 }
8291 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008292 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293}
8294
Bram Moolenaar572cb562005-08-05 21:35:02 +00008295#if defined(FEAT_INS_EXPAND)
8296/*
Bram Moolenaarade00832006-03-10 21:46:58 +00008297 * "complete()" function
8298 */
8299/*ARGSUSED*/
8300 static void
8301f_complete(argvars, rettv)
8302 typval_T *argvars;
8303 typval_T *rettv;
8304{
8305 int startcol;
8306
8307 if ((State & INSERT) == 0)
8308 {
8309 EMSG(_("E785: complete() can only be used in Insert mode"));
8310 return;
8311 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00008312
8313 /* Check for undo allowed here, because if something was already inserted
8314 * the line was already saved for undo and this check isn't done. */
8315 if (!undo_allowed())
8316 return;
8317
Bram Moolenaarade00832006-03-10 21:46:58 +00008318 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
8319 {
8320 EMSG(_(e_invarg));
8321 return;
8322 }
8323
8324 startcol = get_tv_number_chk(&argvars[0], NULL);
8325 if (startcol <= 0)
8326 return;
8327
8328 set_completion(startcol - 1, argvars[1].vval.v_list);
8329}
8330
8331/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00008332 * "complete_add()" function
8333 */
8334/*ARGSUSED*/
8335 static void
8336f_complete_add(argvars, rettv)
8337 typval_T *argvars;
8338 typval_T *rettv;
8339{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00008340 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00008341}
8342
8343/*
8344 * "complete_check()" function
8345 */
8346/*ARGSUSED*/
8347 static void
8348f_complete_check(argvars, rettv)
8349 typval_T *argvars;
8350 typval_T *rettv;
8351{
8352 int saved = RedrawingDisabled;
8353
8354 RedrawingDisabled = 0;
8355 ins_compl_check_keys(0);
8356 rettv->vval.v_number = compl_interrupted;
8357 RedrawingDisabled = saved;
8358}
8359#endif
8360
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361/*
8362 * "confirm(message, buttons[, default [, type]])" function
8363 */
8364/*ARGSUSED*/
8365 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008366f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008367 typval_T *argvars;
8368 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369{
8370#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
8371 char_u *message;
8372 char_u *buttons = NULL;
8373 char_u buf[NUMBUFLEN];
8374 char_u buf2[NUMBUFLEN];
8375 int def = 1;
8376 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008377 char_u *typestr;
8378 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008380 message = get_tv_string_chk(&argvars[0]);
8381 if (message == NULL)
8382 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008383 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008385 buttons = get_tv_string_buf_chk(&argvars[1], buf);
8386 if (buttons == NULL)
8387 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008388 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008390 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008391 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008393 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
8394 if (typestr == NULL)
8395 error = TRUE;
8396 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008398 switch (TOUPPER_ASC(*typestr))
8399 {
8400 case 'E': type = VIM_ERROR; break;
8401 case 'Q': type = VIM_QUESTION; break;
8402 case 'I': type = VIM_INFO; break;
8403 case 'W': type = VIM_WARNING; break;
8404 case 'G': type = VIM_GENERIC; break;
8405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406 }
8407 }
8408 }
8409 }
8410
8411 if (buttons == NULL || *buttons == NUL)
8412 buttons = (char_u *)_("&Ok");
8413
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008414 if (error)
8415 rettv->vval.v_number = 0;
8416 else
8417 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418 def, NULL);
8419#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008420 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421#endif
8422}
8423
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008424/*
8425 * "copy()" function
8426 */
8427 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008428f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008429 typval_T *argvars;
8430 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008431{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008432 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008433}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434
8435/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008436 * "count()" function
8437 */
8438 static void
8439f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008440 typval_T *argvars;
8441 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008442{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008443 long n = 0;
8444 int ic = FALSE;
8445
Bram Moolenaare9a41262005-01-15 22:18:47 +00008446 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008447 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008448 listitem_T *li;
8449 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008450 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008451
Bram Moolenaare9a41262005-01-15 22:18:47 +00008452 if ((l = argvars[0].vval.v_list) != NULL)
8453 {
8454 li = l->lv_first;
8455 if (argvars[2].v_type != VAR_UNKNOWN)
8456 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008457 int error = FALSE;
8458
8459 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008460 if (argvars[3].v_type != VAR_UNKNOWN)
8461 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008462 idx = get_tv_number_chk(&argvars[3], &error);
8463 if (!error)
8464 {
8465 li = list_find(l, idx);
8466 if (li == NULL)
8467 EMSGN(_(e_listidx), idx);
8468 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008469 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008470 if (error)
8471 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008472 }
8473
8474 for ( ; li != NULL; li = li->li_next)
8475 if (tv_equal(&li->li_tv, &argvars[1], ic))
8476 ++n;
8477 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008478 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008479 else if (argvars[0].v_type == VAR_DICT)
8480 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008481 int todo;
8482 dict_T *d;
8483 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008484
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008485 if ((d = argvars[0].vval.v_dict) != NULL)
8486 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008487 int error = FALSE;
8488
Bram Moolenaare9a41262005-01-15 22:18:47 +00008489 if (argvars[2].v_type != VAR_UNKNOWN)
8490 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008491 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008492 if (argvars[3].v_type != VAR_UNKNOWN)
8493 EMSG(_(e_invarg));
8494 }
8495
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008496 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008497 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008498 {
8499 if (!HASHITEM_EMPTY(hi))
8500 {
8501 --todo;
8502 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
8503 ++n;
8504 }
8505 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008506 }
8507 }
8508 else
8509 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008510 rettv->vval.v_number = n;
8511}
8512
8513/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8515 *
8516 * Checks the existence of a cscope connection.
8517 */
8518/*ARGSUSED*/
8519 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008520f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008521 typval_T *argvars;
8522 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523{
8524#ifdef FEAT_CSCOPE
8525 int num = 0;
8526 char_u *dbpath = NULL;
8527 char_u *prepend = NULL;
8528 char_u buf[NUMBUFLEN];
8529
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008530 if (argvars[0].v_type != VAR_UNKNOWN
8531 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008533 num = (int)get_tv_number(&argvars[0]);
8534 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008535 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008536 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537 }
8538
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008539 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008541 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542#endif
8543}
8544
8545/*
8546 * "cursor(lnum, col)" function
8547 *
8548 * Moves the cursor to the specified line and column
8549 */
8550/*ARGSUSED*/
8551 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008552f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008553 typval_T *argvars;
8554 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555{
8556 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008557#ifdef FEAT_VIRTUALEDIT
8558 long coladd = 0;
8559#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008560
Bram Moolenaara5525202006-03-02 22:52:09 +00008561 if (argvars[1].v_type == VAR_UNKNOWN)
8562 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008563 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00008564
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008565 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00008566 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008567 line = pos.lnum;
8568 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008569#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008570 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00008571#endif
8572 }
8573 else
8574 {
8575 line = get_tv_lnum(argvars);
8576 col = get_tv_number_chk(&argvars[1], NULL);
8577#ifdef FEAT_VIRTUALEDIT
8578 if (argvars[2].v_type != VAR_UNKNOWN)
8579 coladd = get_tv_number_chk(&argvars[2], NULL);
8580#endif
8581 }
8582 if (line < 0 || col < 0
8583#ifdef FEAT_VIRTUALEDIT
8584 || coladd < 0
8585#endif
8586 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008587 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588 if (line > 0)
8589 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590 if (col > 0)
8591 curwin->w_cursor.col = col - 1;
8592#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00008593 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594#endif
8595
8596 /* Make sure the cursor is in a valid position. */
8597 check_cursor();
8598#ifdef FEAT_MBYTE
8599 /* Correct cursor for multi-byte character. */
8600 if (has_mbyte)
8601 mb_adjust_cursor();
8602#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008603
8604 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605}
8606
8607/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008608 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609 */
8610 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008611f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008612 typval_T *argvars;
8613 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008615 int noref = 0;
8616
8617 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008618 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008619 if (noref < 0 || noref > 1)
8620 EMSG(_(e_invarg));
8621 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008622 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623}
8624
8625/*
8626 * "delete()" function
8627 */
8628 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008629f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008630 typval_T *argvars;
8631 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632{
8633 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008634 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008636 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637}
8638
8639/*
8640 * "did_filetype()" function
8641 */
8642/*ARGSUSED*/
8643 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008644f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008645 typval_T *argvars;
8646 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647{
8648#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008649 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008651 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652#endif
8653}
8654
8655/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008656 * "diff_filler()" function
8657 */
8658/*ARGSUSED*/
8659 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008660f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008661 typval_T *argvars;
8662 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008663{
8664#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008665 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008666#endif
8667}
8668
8669/*
8670 * "diff_hlID()" function
8671 */
8672/*ARGSUSED*/
8673 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008674f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008675 typval_T *argvars;
8676 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008677{
8678#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008679 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008680 static linenr_T prev_lnum = 0;
8681 static int changedtick = 0;
8682 static int fnum = 0;
8683 static int change_start = 0;
8684 static int change_end = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008685 static hlf_T hlID = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008686 int filler_lines;
8687 int col;
8688
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008689 if (lnum < 0) /* ignore type error in {lnum} arg */
8690 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008691 if (lnum != prev_lnum
8692 || changedtick != curbuf->b_changedtick
8693 || fnum != curbuf->b_fnum)
8694 {
8695 /* New line, buffer, change: need to get the values. */
8696 filler_lines = diff_check(curwin, lnum);
8697 if (filler_lines < 0)
8698 {
8699 if (filler_lines == -1)
8700 {
8701 change_start = MAXCOL;
8702 change_end = -1;
8703 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8704 hlID = HLF_ADD; /* added line */
8705 else
8706 hlID = HLF_CHD; /* changed line */
8707 }
8708 else
8709 hlID = HLF_ADD; /* added line */
8710 }
8711 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008712 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008713 prev_lnum = lnum;
8714 changedtick = curbuf->b_changedtick;
8715 fnum = curbuf->b_fnum;
8716 }
8717
8718 if (hlID == HLF_CHD || hlID == HLF_TXD)
8719 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008720 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008721 if (col >= change_start && col <= change_end)
8722 hlID = HLF_TXD; /* changed text */
8723 else
8724 hlID = HLF_CHD; /* changed line */
8725 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008726 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008727#endif
8728}
8729
8730/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008731 * "empty({expr})" function
8732 */
8733 static void
8734f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008735 typval_T *argvars;
8736 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008737{
8738 int n;
8739
8740 switch (argvars[0].v_type)
8741 {
8742 case VAR_STRING:
8743 case VAR_FUNC:
8744 n = argvars[0].vval.v_string == NULL
8745 || *argvars[0].vval.v_string == NUL;
8746 break;
8747 case VAR_NUMBER:
8748 n = argvars[0].vval.v_number == 0;
8749 break;
8750 case VAR_LIST:
8751 n = argvars[0].vval.v_list == NULL
8752 || argvars[0].vval.v_list->lv_first == NULL;
8753 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008754 case VAR_DICT:
8755 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008756 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008757 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008758 default:
8759 EMSG2(_(e_intern2), "f_empty()");
8760 n = 0;
8761 }
8762
8763 rettv->vval.v_number = n;
8764}
8765
8766/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767 * "escape({string}, {chars})" function
8768 */
8769 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008770f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008771 typval_T *argvars;
8772 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773{
8774 char_u buf[NUMBUFLEN];
8775
Bram Moolenaar758711c2005-02-02 23:11:38 +00008776 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8777 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008778 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779}
8780
8781/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008782 * "eval()" function
8783 */
8784/*ARGSUSED*/
8785 static void
8786f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008787 typval_T *argvars;
8788 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008789{
8790 char_u *s;
8791
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008792 s = get_tv_string_chk(&argvars[0]);
8793 if (s != NULL)
8794 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008795
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008796 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8797 {
8798 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008799 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008800 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008801 else if (*s != NUL)
8802 EMSG(_(e_trailing));
8803}
8804
8805/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008806 * "eventhandler()" function
8807 */
8808/*ARGSUSED*/
8809 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008810f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008811 typval_T *argvars;
8812 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008814 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815}
8816
8817/*
8818 * "executable()" function
8819 */
8820 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008821f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008822 typval_T *argvars;
8823 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008825 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826}
8827
8828/*
8829 * "exists()" function
8830 */
8831 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008832f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008833 typval_T *argvars;
8834 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835{
8836 char_u *p;
8837 char_u *name;
8838 int n = FALSE;
8839 int len = 0;
8840
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008841 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842 if (*p == '$') /* environment variable */
8843 {
8844 /* first try "normal" environment variables (fast) */
8845 if (mch_getenv(p + 1) != NULL)
8846 n = TRUE;
8847 else
8848 {
8849 /* try expanding things like $VIM and ${HOME} */
8850 p = expand_env_save(p);
8851 if (p != NULL && *p != '$')
8852 n = TRUE;
8853 vim_free(p);
8854 }
8855 }
8856 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00008857 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008858 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00008859 if (*skipwhite(p) != NUL)
8860 n = FALSE; /* trailing garbage */
8861 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862 else if (*p == '*') /* internal or user defined function */
8863 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008864 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865 }
8866 else if (*p == ':')
8867 {
8868 n = cmd_exists(p + 1);
8869 }
8870 else if (*p == '#')
8871 {
8872#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00008873 if (p[1] == '#')
8874 n = autocmd_supported(p + 2);
8875 else
8876 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877#endif
8878 }
8879 else /* internal variable */
8880 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008881 char_u *tofree;
8882 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008884 /* get_name_len() takes care of expanding curly braces */
8885 name = p;
8886 len = get_name_len(&p, &tofree, TRUE, FALSE);
8887 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008889 if (tofree != NULL)
8890 name = tofree;
8891 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8892 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008894 /* handle d.key, l[idx], f(expr) */
8895 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8896 if (n)
8897 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898 }
8899 }
Bram Moolenaar79783442006-05-05 21:18:03 +00008900 if (*p != NUL)
8901 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008902
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008903 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904 }
8905
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008906 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008907}
8908
8909/*
8910 * "expand()" function
8911 */
8912 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008913f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008914 typval_T *argvars;
8915 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916{
8917 char_u *s;
8918 int len;
8919 char_u *errormsg;
8920 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8921 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008922 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008924 rettv->v_type = VAR_STRING;
8925 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926 if (*s == '%' || *s == '#' || *s == '<')
8927 {
8928 ++emsg_off;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008929 rettv->vval.v_string = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008930 --emsg_off;
8931 }
8932 else
8933 {
8934 /* When the optional second argument is non-zero, don't remove matches
8935 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008936 if (argvars[1].v_type != VAR_UNKNOWN
8937 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008938 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008939 if (!error)
8940 {
8941 ExpandInit(&xpc);
8942 xpc.xp_context = EXPAND_FILES;
8943 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008944 }
8945 else
8946 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947 }
8948}
8949
8950/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008951 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00008952 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008953 */
8954 static void
8955f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008956 typval_T *argvars;
8957 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008958{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008959 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008960 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008961 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008962 list_T *l1, *l2;
8963 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008964 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008965 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008966
Bram Moolenaare9a41262005-01-15 22:18:47 +00008967 l1 = argvars[0].vval.v_list;
8968 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008969 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
8970 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008971 {
8972 if (argvars[2].v_type != VAR_UNKNOWN)
8973 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008974 before = get_tv_number_chk(&argvars[2], &error);
8975 if (error)
8976 return; /* type error; errmsg already given */
8977
Bram Moolenaar758711c2005-02-02 23:11:38 +00008978 if (before == l1->lv_len)
8979 item = NULL;
8980 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008981 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00008982 item = list_find(l1, before);
8983 if (item == NULL)
8984 {
8985 EMSGN(_(e_listidx), before);
8986 return;
8987 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008988 }
8989 }
8990 else
8991 item = NULL;
8992 list_extend(l1, l2, item);
8993
Bram Moolenaare9a41262005-01-15 22:18:47 +00008994 copy_tv(&argvars[0], rettv);
8995 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008996 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008997 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
8998 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008999 dict_T *d1, *d2;
9000 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009001 char_u *action;
9002 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00009003 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009004 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009005
9006 d1 = argvars[0].vval.v_dict;
9007 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009008 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
9009 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009010 {
9011 /* Check the third argument. */
9012 if (argvars[2].v_type != VAR_UNKNOWN)
9013 {
9014 static char *(av[]) = {"keep", "force", "error"};
9015
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009016 action = get_tv_string_chk(&argvars[2]);
9017 if (action == NULL)
9018 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009019 for (i = 0; i < 3; ++i)
9020 if (STRCMP(action, av[i]) == 0)
9021 break;
9022 if (i == 3)
9023 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009024 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009025 return;
9026 }
9027 }
9028 else
9029 action = (char_u *)"force";
9030
9031 /* Go over all entries in the second dict and add them to the
9032 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009033 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009034 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009035 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009036 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009037 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009038 --todo;
9039 di1 = dict_find(d1, hi2->hi_key, -1);
9040 if (di1 == NULL)
9041 {
9042 di1 = dictitem_copy(HI2DI(hi2));
9043 if (di1 != NULL && dict_add(d1, di1) == FAIL)
9044 dictitem_free(di1);
9045 }
9046 else if (*action == 'e')
9047 {
9048 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
9049 break;
9050 }
9051 else if (*action == 'f')
9052 {
9053 clear_tv(&di1->di_tv);
9054 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
9055 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009056 }
9057 }
9058
Bram Moolenaare9a41262005-01-15 22:18:47 +00009059 copy_tv(&argvars[0], rettv);
9060 }
9061 }
9062 else
9063 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009064}
9065
9066/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009067 * "feedkeys()" function
9068 */
9069/*ARGSUSED*/
9070 static void
9071f_feedkeys(argvars, rettv)
9072 typval_T *argvars;
9073 typval_T *rettv;
9074{
9075 int remap = TRUE;
9076 char_u *keys, *flags;
9077 char_u nbuf[NUMBUFLEN];
9078 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009079 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009080
9081 rettv->vval.v_number = 0;
9082 keys = get_tv_string(&argvars[0]);
9083 if (*keys != NUL)
9084 {
9085 if (argvars[1].v_type != VAR_UNKNOWN)
9086 {
9087 flags = get_tv_string_buf(&argvars[1], nbuf);
9088 for ( ; *flags != NUL; ++flags)
9089 {
9090 switch (*flags)
9091 {
9092 case 'n': remap = FALSE; break;
9093 case 'm': remap = TRUE; break;
9094 case 't': typed = TRUE; break;
9095 }
9096 }
9097 }
9098
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009099 /* Need to escape K_SPECIAL and CSI before putting the string in the
9100 * typeahead buffer. */
9101 keys_esc = vim_strsave_escape_csi(keys);
9102 if (keys_esc != NULL)
9103 {
9104 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009105 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009106 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009107 if (vgetc_busy)
9108 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009109 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009110 }
9111}
9112
9113/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009114 * "filereadable()" function
9115 */
9116 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009117f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009118 typval_T *argvars;
9119 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009120{
9121 FILE *fd;
9122 char_u *p;
9123 int n;
9124
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009125 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009126 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
9127 {
9128 n = TRUE;
9129 fclose(fd);
9130 }
9131 else
9132 n = FALSE;
9133
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009134 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009135}
9136
9137/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009138 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00009139 * rights to write into.
9140 */
9141 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009142f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009143 typval_T *argvars;
9144 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009146 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009147}
9148
Bram Moolenaar33570922005-01-25 22:26:29 +00009149static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009150
9151 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00009152findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00009153 typval_T *argvars;
9154 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009155 int dir;
9156{
9157#ifdef FEAT_SEARCHPATH
9158 char_u *fname;
9159 char_u *fresult = NULL;
9160 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
9161 char_u *p;
9162 char_u pathbuf[NUMBUFLEN];
9163 int count = 1;
9164 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009165 int error = FALSE;
9166#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009167
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009168 rettv->vval.v_string = NULL;
9169 rettv->v_type = VAR_STRING;
9170
9171#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009172 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009173
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009174 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009175 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009176 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
9177 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009178 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009179 else
9180 {
9181 if (*p != NUL)
9182 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009183
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009184 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009185 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009186 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009187 }
9188
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009189 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
9190 error = TRUE;
9191
9192 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009193 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009194 do
9195 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009196 if (rettv->v_type == VAR_STRING)
9197 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009198 fresult = find_file_in_path_option(first ? fname : NULL,
9199 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar5b6b1ca2007-03-27 08:19:43 +00009200 0, first, path, dir, curbuf->b_ffname,
Bram Moolenaare580b0c2006-03-21 21:33:03 +00009201 dir ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009202 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009203
9204 if (fresult != NULL && rettv->v_type == VAR_LIST)
9205 list_append_string(rettv->vval.v_list, fresult, -1);
9206
9207 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009208 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009209
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009210 if (rettv->v_type == VAR_STRING)
9211 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009212#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009213}
9214
Bram Moolenaar33570922005-01-25 22:26:29 +00009215static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
9216static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009217
9218/*
9219 * Implementation of map() and filter().
9220 */
9221 static void
9222filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00009223 typval_T *argvars;
9224 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009225 int map;
9226{
9227 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00009228 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00009229 listitem_T *li, *nli;
9230 list_T *l = NULL;
9231 dictitem_T *di;
9232 hashtab_T *ht;
9233 hashitem_T *hi;
9234 dict_T *d = NULL;
9235 typval_T save_val;
9236 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009237 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009238 int todo;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009239 char_u *ermsg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009240 int save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009241
9242 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009243 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009244 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009245 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009246 || (map && tv_check_lock(l->lv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009247 return;
9248 }
9249 else if (argvars[0].v_type == VAR_DICT)
9250 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009251 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009252 || (map && tv_check_lock(d->dv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009253 return;
9254 }
9255 else
9256 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009257 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009258 return;
9259 }
9260
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009261 expr = get_tv_string_buf_chk(&argvars[1], buf);
9262 /* On type errors, the preceding call has already displayed an error
9263 * message. Avoid a misleading error message for an empty string that
9264 * was not passed as argument. */
9265 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009266 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009267 prepare_vimvar(VV_VAL, &save_val);
9268 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009269
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009270 /* We reset "did_emsg" to be able to detect whether an error
9271 * occurred during evaluation of the expression. */
9272 save_did_emsg = did_emsg;
9273 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009274
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009275 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009276 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009277 prepare_vimvar(VV_KEY, &save_key);
9278 vimvars[VV_KEY].vv_type = VAR_STRING;
9279
9280 ht = &d->dv_hashtab;
9281 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009282 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009283 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009284 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009285 if (!HASHITEM_EMPTY(hi))
9286 {
9287 --todo;
9288 di = HI2DI(hi);
Bram Moolenaar89d40322006-08-29 15:30:07 +00009289 if (tv_check_lock(di->di_tv.v_lock, ermsg))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009290 break;
9291 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009292 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009293 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009294 break;
9295 if (!map && rem)
9296 dictitem_remove(d, di);
9297 clear_tv(&vimvars[VV_KEY].vv_tv);
9298 }
9299 }
9300 hash_unlock(ht);
9301
9302 restore_vimvar(VV_KEY, &save_key);
9303 }
9304 else
9305 {
9306 for (li = l->lv_first; li != NULL; li = nli)
9307 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009308 if (tv_check_lock(li->li_tv.v_lock, ermsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009309 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009310 nli = li->li_next;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009311 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009312 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009313 break;
9314 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009315 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009316 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009317 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009318
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009319 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009320
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009321 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009322 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009323
9324 copy_tv(&argvars[0], rettv);
9325}
9326
9327 static int
9328filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009329 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009330 char_u *expr;
9331 int map;
9332 int *remp;
9333{
Bram Moolenaar33570922005-01-25 22:26:29 +00009334 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009335 char_u *s;
9336
Bram Moolenaar33570922005-01-25 22:26:29 +00009337 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009338 s = expr;
9339 if (eval1(&s, &rettv, TRUE) == FAIL)
9340 return FAIL;
9341 if (*s != NUL) /* check for trailing chars after expr */
9342 {
9343 EMSG2(_(e_invexpr2), s);
9344 return FAIL;
9345 }
9346 if (map)
9347 {
9348 /* map(): replace the list item value */
9349 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +00009350 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009351 *tv = rettv;
9352 }
9353 else
9354 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009355 int error = FALSE;
9356
Bram Moolenaare9a41262005-01-15 22:18:47 +00009357 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009358 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009359 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009360 /* On type error, nothing has been removed; return FAIL to stop the
9361 * loop. The error message was given by get_tv_number_chk(). */
9362 if (error)
9363 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009364 }
Bram Moolenaar33570922005-01-25 22:26:29 +00009365 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009366 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009367}
9368
9369/*
9370 * "filter()" function
9371 */
9372 static void
9373f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009374 typval_T *argvars;
9375 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009376{
9377 filter_map(argvars, rettv, FALSE);
9378}
9379
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009380/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009381 * "finddir({fname}[, {path}[, {count}]])" function
9382 */
9383 static void
9384f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009385 typval_T *argvars;
9386 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009387{
9388 findfilendir(argvars, rettv, TRUE);
9389}
9390
9391/*
9392 * "findfile({fname}[, {path}[, {count}]])" function
9393 */
9394 static void
9395f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009396 typval_T *argvars;
9397 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009398{
9399 findfilendir(argvars, rettv, FALSE);
9400}
9401
9402/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009403 * "fnamemodify({fname}, {mods})" function
9404 */
9405 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009406f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009407 typval_T *argvars;
9408 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009409{
9410 char_u *fname;
9411 char_u *mods;
9412 int usedlen = 0;
9413 int len;
9414 char_u *fbuf = NULL;
9415 char_u buf[NUMBUFLEN];
9416
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009417 fname = get_tv_string_chk(&argvars[0]);
9418 mods = get_tv_string_buf_chk(&argvars[1], buf);
9419 if (fname == NULL || mods == NULL)
9420 fname = NULL;
9421 else
9422 {
9423 len = (int)STRLEN(fname);
9424 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
9425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009426
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009427 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009428 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009429 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009431 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009432 vim_free(fbuf);
9433}
9434
Bram Moolenaar33570922005-01-25 22:26:29 +00009435static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009436
9437/*
9438 * "foldclosed()" function
9439 */
9440 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009441foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00009442 typval_T *argvars;
9443 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009444 int end;
9445{
9446#ifdef FEAT_FOLDING
9447 linenr_T lnum;
9448 linenr_T first, last;
9449
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009450 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009451 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9452 {
9453 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
9454 {
9455 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009456 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009457 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009458 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009459 return;
9460 }
9461 }
9462#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009463 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009464}
9465
9466/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009467 * "foldclosed()" function
9468 */
9469 static void
9470f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009471 typval_T *argvars;
9472 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009473{
9474 foldclosed_both(argvars, rettv, FALSE);
9475}
9476
9477/*
9478 * "foldclosedend()" function
9479 */
9480 static void
9481f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009482 typval_T *argvars;
9483 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009484{
9485 foldclosed_both(argvars, rettv, TRUE);
9486}
9487
9488/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009489 * "foldlevel()" function
9490 */
9491 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009492f_foldlevel(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
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009499 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009500 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009501 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009502 else
9503#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009504 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505}
9506
9507/*
9508 * "foldtext()" function
9509 */
9510/*ARGSUSED*/
9511 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009512f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009513 typval_T *argvars;
9514 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009515{
9516#ifdef FEAT_FOLDING
9517 linenr_T lnum;
9518 char_u *s;
9519 char_u *r;
9520 int len;
9521 char *txt;
9522#endif
9523
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009524 rettv->v_type = VAR_STRING;
9525 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009526#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00009527 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
9528 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
9529 <= curbuf->b_ml.ml_line_count
9530 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531 {
9532 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009533 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
9534 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009535 {
9536 if (!linewhite(lnum))
9537 break;
9538 ++lnum;
9539 }
9540
9541 /* Find interesting text in this line. */
9542 s = skipwhite(ml_get(lnum));
9543 /* skip C comment-start */
9544 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009545 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009546 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009547 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00009548 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009549 {
9550 s = skipwhite(ml_get(lnum + 1));
9551 if (*s == '*')
9552 s = skipwhite(s + 1);
9553 }
9554 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009555 txt = _("+-%s%3ld lines: ");
9556 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009557 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558 + 20 /* for %3ld */
9559 + STRLEN(s))); /* concatenated */
9560 if (r != NULL)
9561 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009562 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
9563 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
9564 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009565 len = (int)STRLEN(r);
9566 STRCAT(r, s);
9567 /* remove 'foldmarker' and 'commentstring' */
9568 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009569 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009570 }
9571 }
9572#endif
9573}
9574
9575/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009576 * "foldtextresult(lnum)" function
9577 */
9578/*ARGSUSED*/
9579 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009580f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009581 typval_T *argvars;
9582 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009583{
9584#ifdef FEAT_FOLDING
9585 linenr_T lnum;
9586 char_u *text;
9587 char_u buf[51];
9588 foldinfo_T foldinfo;
9589 int fold_count;
9590#endif
9591
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009592 rettv->v_type = VAR_STRING;
9593 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009594#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009595 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009596 /* treat illegal types and illegal string values for {lnum} the same */
9597 if (lnum < 0)
9598 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009599 fold_count = foldedCount(curwin, lnum, &foldinfo);
9600 if (fold_count > 0)
9601 {
9602 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
9603 &foldinfo, buf);
9604 if (text == buf)
9605 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009606 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009607 }
9608#endif
9609}
9610
9611/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009612 * "foreground()" function
9613 */
9614/*ARGSUSED*/
9615 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009616f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009617 typval_T *argvars;
9618 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009619{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009620 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009621#ifdef FEAT_GUI
9622 if (gui.in_use)
9623 gui_mch_set_foreground();
9624#else
9625# ifdef WIN32
9626 win32_set_foreground();
9627# endif
9628#endif
9629}
9630
9631/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009632 * "function()" function
9633 */
9634/*ARGSUSED*/
9635 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009636f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009637 typval_T *argvars;
9638 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009639{
9640 char_u *s;
9641
Bram Moolenaara7043832005-01-21 11:56:39 +00009642 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009643 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009644 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009645 EMSG2(_(e_invarg2), s);
9646 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009647 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009648 else
9649 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009650 rettv->vval.v_string = vim_strsave(s);
9651 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009652 }
9653}
9654
9655/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009656 * "garbagecollect()" function
9657 */
9658/*ARGSUSED*/
9659 static void
9660f_garbagecollect(argvars, rettv)
9661 typval_T *argvars;
9662 typval_T *rettv;
9663{
Bram Moolenaar9fecb462006-09-05 10:59:47 +00009664 /* This is postponed until we are back at the toplevel, because we may be
9665 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
9666 want_garbage_collect = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009667}
9668
9669/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009670 * "get()" function
9671 */
9672 static void
9673f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009674 typval_T *argvars;
9675 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009676{
Bram Moolenaar33570922005-01-25 22:26:29 +00009677 listitem_T *li;
9678 list_T *l;
9679 dictitem_T *di;
9680 dict_T *d;
9681 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009682
Bram Moolenaare9a41262005-01-15 22:18:47 +00009683 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009684 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009685 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009686 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009687 int error = FALSE;
9688
9689 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9690 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009691 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009692 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009693 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009694 else if (argvars[0].v_type == VAR_DICT)
9695 {
9696 if ((d = argvars[0].vval.v_dict) != NULL)
9697 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009698 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009699 if (di != NULL)
9700 tv = &di->di_tv;
9701 }
9702 }
9703 else
9704 EMSG2(_(e_listdictarg), "get()");
9705
9706 if (tv == NULL)
9707 {
9708 if (argvars[2].v_type == VAR_UNKNOWN)
9709 rettv->vval.v_number = 0;
9710 else
9711 copy_tv(&argvars[2], rettv);
9712 }
9713 else
9714 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009715}
9716
Bram Moolenaar342337a2005-07-21 21:11:17 +00009717static 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 +00009718
9719/*
9720 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +00009721 * Return a range (from start to end) of lines in rettv from the specified
9722 * buffer.
9723 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009724 */
9725 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +00009726get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009727 buf_T *buf;
9728 linenr_T start;
9729 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009730 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009731 typval_T *rettv;
9732{
9733 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009734
Bram Moolenaar342337a2005-07-21 21:11:17 +00009735 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009736 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009737 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009738 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009739 }
9740 else
9741 rettv->vval.v_number = 0;
9742
9743 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
9744 return;
9745
9746 if (!retlist)
9747 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009748 if (start >= 1 && start <= buf->b_ml.ml_line_count)
9749 p = ml_get_buf(buf, start, FALSE);
9750 else
9751 p = (char_u *)"";
9752
9753 rettv->v_type = VAR_STRING;
9754 rettv->vval.v_string = vim_strsave(p);
9755 }
9756 else
9757 {
9758 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009759 return;
9760
9761 if (start < 1)
9762 start = 1;
9763 if (end > buf->b_ml.ml_line_count)
9764 end = buf->b_ml.ml_line_count;
9765 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009766 if (list_append_string(rettv->vval.v_list,
9767 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009768 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009769 }
9770}
9771
9772/*
9773 * "getbufline()" function
9774 */
9775 static void
9776f_getbufline(argvars, rettv)
9777 typval_T *argvars;
9778 typval_T *rettv;
9779{
9780 linenr_T lnum;
9781 linenr_T end;
9782 buf_T *buf;
9783
9784 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9785 ++emsg_off;
9786 buf = get_buf_tv(&argvars[0]);
9787 --emsg_off;
9788
Bram Moolenaar661b1822005-07-28 22:36:45 +00009789 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009790 if (argvars[2].v_type == VAR_UNKNOWN)
9791 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009792 else
Bram Moolenaar661b1822005-07-28 22:36:45 +00009793 end = get_tv_lnum_buf(&argvars[2], buf);
9794
Bram Moolenaar342337a2005-07-21 21:11:17 +00009795 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009796}
9797
Bram Moolenaar0d660222005-01-07 21:51:51 +00009798/*
9799 * "getbufvar()" function
9800 */
9801 static void
9802f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009803 typval_T *argvars;
9804 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009805{
9806 buf_T *buf;
9807 buf_T *save_curbuf;
9808 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009809 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009810
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009811 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9812 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009813 ++emsg_off;
9814 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009815
9816 rettv->v_type = VAR_STRING;
9817 rettv->vval.v_string = NULL;
9818
9819 if (buf != NULL && varname != NULL)
9820 {
9821 if (*varname == '&') /* buffer-local-option */
9822 {
9823 /* set curbuf to be our buf, temporarily */
9824 save_curbuf = curbuf;
9825 curbuf = buf;
9826
9827 get_option_tv(&varname, rettv, TRUE);
9828
9829 /* restore previous notion of curbuf */
9830 curbuf = save_curbuf;
9831 }
9832 else
9833 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009834 if (*varname == NUL)
9835 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9836 * scope prefix before the NUL byte is required by
9837 * find_var_in_ht(). */
9838 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009839 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009840 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009841 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009842 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009843 }
9844 }
9845
9846 --emsg_off;
9847}
9848
9849/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009850 * "getchar()" function
9851 */
9852 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009853f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009854 typval_T *argvars;
9855 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009856{
9857 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009858 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009859
Bram Moolenaar4015b2c2006-06-22 19:01:34 +00009860 /* Position the cursor. Needed after a message that ends in a space. */
9861 windgoto(msg_row, msg_col);
9862
Bram Moolenaar071d4272004-06-13 20:20:40 +00009863 ++no_mapping;
9864 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009865 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866 /* getchar(): blocking wait. */
9867 n = safe_vgetc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009868 else if (get_tv_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009869 /* getchar(1): only check if char avail */
9870 n = vpeekc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009871 else if (error || vpeekc() == NUL)
9872 /* illegal argument or getchar(0) and no char avail: return zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009873 n = 0;
9874 else
9875 /* getchar(0) and char avail: return char */
9876 n = safe_vgetc();
9877 --no_mapping;
9878 --allow_keys;
9879
Bram Moolenaar219b8702006-11-01 14:32:36 +00009880 vimvars[VV_MOUSE_WIN].vv_nr = 0;
9881 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
9882 vimvars[VV_MOUSE_COL].vv_nr = 0;
9883
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009884 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009885 if (IS_SPECIAL(n) || mod_mask != 0)
9886 {
9887 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9888 int i = 0;
9889
9890 /* Turn a special key into three bytes, plus modifier. */
9891 if (mod_mask != 0)
9892 {
9893 temp[i++] = K_SPECIAL;
9894 temp[i++] = KS_MODIFIER;
9895 temp[i++] = mod_mask;
9896 }
9897 if (IS_SPECIAL(n))
9898 {
9899 temp[i++] = K_SPECIAL;
9900 temp[i++] = K_SECOND(n);
9901 temp[i++] = K_THIRD(n);
9902 }
9903#ifdef FEAT_MBYTE
9904 else if (has_mbyte)
9905 i += (*mb_char2bytes)(n, temp + i);
9906#endif
9907 else
9908 temp[i++] = n;
9909 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009910 rettv->v_type = VAR_STRING;
9911 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +00009912
9913#ifdef FEAT_MOUSE
9914 if (n == K_LEFTMOUSE
9915 || n == K_LEFTMOUSE_NM
9916 || n == K_LEFTDRAG
9917 || n == K_LEFTRELEASE
9918 || n == K_LEFTRELEASE_NM
9919 || n == K_MIDDLEMOUSE
9920 || n == K_MIDDLEDRAG
9921 || n == K_MIDDLERELEASE
9922 || n == K_RIGHTMOUSE
9923 || n == K_RIGHTDRAG
9924 || n == K_RIGHTRELEASE
9925 || n == K_X1MOUSE
9926 || n == K_X1DRAG
9927 || n == K_X1RELEASE
9928 || n == K_X2MOUSE
9929 || n == K_X2DRAG
9930 || n == K_X2RELEASE
9931 || n == K_MOUSEDOWN
9932 || n == K_MOUSEUP)
9933 {
9934 int row = mouse_row;
9935 int col = mouse_col;
9936 win_T *win;
9937 linenr_T lnum;
9938# ifdef FEAT_WINDOWS
9939 win_T *wp;
9940# endif
9941 int n = 1;
9942
9943 if (row >= 0 && col >= 0)
9944 {
9945 /* Find the window at the mouse coordinates and compute the
9946 * text position. */
9947 win = mouse_find_win(&row, &col);
9948 (void)mouse_comp_pos(win, &row, &col, &lnum);
9949# ifdef FEAT_WINDOWS
9950 for (wp = firstwin; wp != win; wp = wp->w_next)
9951 ++n;
9952# endif
9953 vimvars[VV_MOUSE_WIN].vv_nr = n;
9954 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
9955 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
9956 }
9957 }
9958#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009959 }
9960}
9961
9962/*
9963 * "getcharmod()" function
9964 */
9965/*ARGSUSED*/
9966 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009967f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009968 typval_T *argvars;
9969 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009970{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009971 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009972}
9973
9974/*
9975 * "getcmdline()" function
9976 */
9977/*ARGSUSED*/
9978 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009979f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009980 typval_T *argvars;
9981 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009982{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009983 rettv->v_type = VAR_STRING;
9984 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009985}
9986
9987/*
9988 * "getcmdpos()" function
9989 */
9990/*ARGSUSED*/
9991 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009992f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009993 typval_T *argvars;
9994 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009995{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009996 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009997}
9998
9999/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010000 * "getcmdtype()" function
10001 */
10002/*ARGSUSED*/
10003 static void
10004f_getcmdtype(argvars, rettv)
10005 typval_T *argvars;
10006 typval_T *rettv;
10007{
10008 rettv->v_type = VAR_STRING;
10009 rettv->vval.v_string = alloc(2);
10010 if (rettv->vval.v_string != NULL)
10011 {
10012 rettv->vval.v_string[0] = get_cmdline_type();
10013 rettv->vval.v_string[1] = NUL;
10014 }
10015}
10016
10017/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010018 * "getcwd()" function
10019 */
10020/*ARGSUSED*/
10021 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010022f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010023 typval_T *argvars;
10024 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010025{
10026 char_u cwd[MAXPATHL];
10027
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010028 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010029 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010030 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010031 else
10032 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010033 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010034#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +000010035 if (rettv->vval.v_string != NULL)
10036 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010037#endif
10038 }
10039}
10040
10041/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010042 * "getfontname()" function
10043 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010044/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010045 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010046f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010047 typval_T *argvars;
10048 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010049{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010050 rettv->v_type = VAR_STRING;
10051 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010052#ifdef FEAT_GUI
10053 if (gui.in_use)
10054 {
10055 GuiFont font;
10056 char_u *name = NULL;
10057
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010058 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010059 {
10060 /* Get the "Normal" font. Either the name saved by
10061 * hl_set_font_name() or from the font ID. */
10062 font = gui.norm_font;
10063 name = hl_get_font_name();
10064 }
10065 else
10066 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010067 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010068 if (STRCMP(name, "*") == 0) /* don't use font dialog */
10069 return;
10070 font = gui_mch_get_font(name, FALSE);
10071 if (font == NOFONT)
10072 return; /* Invalid font name, return empty string. */
10073 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010074 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010075 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010076 gui_mch_free_font(font);
10077 }
10078#endif
10079}
10080
10081/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010082 * "getfperm({fname})" function
10083 */
10084 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010085f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010086 typval_T *argvars;
10087 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010088{
10089 char_u *fname;
10090 struct stat st;
10091 char_u *perm = NULL;
10092 char_u flags[] = "rwx";
10093 int i;
10094
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010095 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010096
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010097 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010098 if (mch_stat((char *)fname, &st) >= 0)
10099 {
10100 perm = vim_strsave((char_u *)"---------");
10101 if (perm != NULL)
10102 {
10103 for (i = 0; i < 9; i++)
10104 {
10105 if (st.st_mode & (1 << (8 - i)))
10106 perm[i] = flags[i % 3];
10107 }
10108 }
10109 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010110 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010111}
10112
10113/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010114 * "getfsize({fname})" function
10115 */
10116 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010117f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010118 typval_T *argvars;
10119 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120{
10121 char_u *fname;
10122 struct stat st;
10123
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010124 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010125
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010126 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010127
10128 if (mch_stat((char *)fname, &st) >= 0)
10129 {
10130 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010131 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010132 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010133 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010134 }
10135 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010136 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010137}
10138
10139/*
10140 * "getftime({fname})" function
10141 */
10142 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010143f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010144 typval_T *argvars;
10145 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010146{
10147 char_u *fname;
10148 struct stat st;
10149
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010150 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010151
10152 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010153 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010154 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010155 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010156}
10157
10158/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010159 * "getftype({fname})" function
10160 */
10161 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010162f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010163 typval_T *argvars;
10164 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010165{
10166 char_u *fname;
10167 struct stat st;
10168 char_u *type = NULL;
10169 char *t;
10170
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010171 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010172
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010173 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010174 if (mch_lstat((char *)fname, &st) >= 0)
10175 {
10176#ifdef S_ISREG
10177 if (S_ISREG(st.st_mode))
10178 t = "file";
10179 else if (S_ISDIR(st.st_mode))
10180 t = "dir";
10181# ifdef S_ISLNK
10182 else if (S_ISLNK(st.st_mode))
10183 t = "link";
10184# endif
10185# ifdef S_ISBLK
10186 else if (S_ISBLK(st.st_mode))
10187 t = "bdev";
10188# endif
10189# ifdef S_ISCHR
10190 else if (S_ISCHR(st.st_mode))
10191 t = "cdev";
10192# endif
10193# ifdef S_ISFIFO
10194 else if (S_ISFIFO(st.st_mode))
10195 t = "fifo";
10196# endif
10197# ifdef S_ISSOCK
10198 else if (S_ISSOCK(st.st_mode))
10199 t = "fifo";
10200# endif
10201 else
10202 t = "other";
10203#else
10204# ifdef S_IFMT
10205 switch (st.st_mode & S_IFMT)
10206 {
10207 case S_IFREG: t = "file"; break;
10208 case S_IFDIR: t = "dir"; break;
10209# ifdef S_IFLNK
10210 case S_IFLNK: t = "link"; break;
10211# endif
10212# ifdef S_IFBLK
10213 case S_IFBLK: t = "bdev"; break;
10214# endif
10215# ifdef S_IFCHR
10216 case S_IFCHR: t = "cdev"; break;
10217# endif
10218# ifdef S_IFIFO
10219 case S_IFIFO: t = "fifo"; break;
10220# endif
10221# ifdef S_IFSOCK
10222 case S_IFSOCK: t = "socket"; break;
10223# endif
10224 default: t = "other";
10225 }
10226# else
10227 if (mch_isdir(fname))
10228 t = "dir";
10229 else
10230 t = "file";
10231# endif
10232#endif
10233 type = vim_strsave((char_u *)t);
10234 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010235 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010236}
10237
10238/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010239 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000010240 */
10241 static void
10242f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010243 typval_T *argvars;
10244 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010245{
10246 linenr_T lnum;
10247 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010248 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010249
10250 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010251 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010252 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010253 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010254 retlist = FALSE;
10255 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010256 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000010257 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000010258 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010259 retlist = TRUE;
10260 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010261
Bram Moolenaar342337a2005-07-21 21:11:17 +000010262 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010263}
10264
10265/*
Bram Moolenaara5525202006-03-02 22:52:09 +000010266 * "getpos(string)" function
10267 */
10268 static void
10269f_getpos(argvars, rettv)
10270 typval_T *argvars;
10271 typval_T *rettv;
10272{
10273 pos_T *fp;
10274 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010275 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010276
10277 if (rettv_list_alloc(rettv) == OK)
10278 {
10279 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010280 fp = var2fpos(&argvars[0], TRUE, &fnum);
10281 if (fnum != -1)
10282 list_append_number(l, (varnumber_T)fnum);
10283 else
10284 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000010285 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
10286 : (varnumber_T)0);
10287 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->col + 1
10288 : (varnumber_T)0);
10289 list_append_number(l,
10290#ifdef FEAT_VIRTUALEDIT
10291 (fp != NULL) ? (varnumber_T)fp->coladd :
10292#endif
10293 (varnumber_T)0);
10294 }
10295 else
10296 rettv->vval.v_number = FALSE;
10297}
10298
10299/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000010300 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000010301 */
Bram Moolenaar280f1262006-01-30 00:14:18 +000010302/*ARGSUSED*/
Bram Moolenaar2641f772005-03-25 21:58:17 +000010303 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000010304f_getqflist(argvars, rettv)
10305 typval_T *argvars;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010306 typval_T *rettv;
10307{
10308#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000010309 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010310#endif
10311
Bram Moolenaar77f66d62007-02-20 02:16:18 +000010312 rettv->vval.v_number = 0;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010313#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010314 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000010315 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000010316 wp = NULL;
10317 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
10318 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010319 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010320 if (wp == NULL)
10321 return;
10322 }
10323
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010324 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000010325 }
10326#endif
10327}
10328
10329/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010330 * "getreg()" function
10331 */
10332 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010333f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010334 typval_T *argvars;
10335 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010336{
10337 char_u *strregname;
10338 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010339 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010340 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010341
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010342 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010343 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010344 strregname = get_tv_string_chk(&argvars[0]);
10345 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010346 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010347 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010349 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010350 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010351 regname = (strregname == NULL ? '"' : *strregname);
10352 if (regname == 0)
10353 regname = '"';
10354
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010355 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010356 rettv->vval.v_string = error ? NULL :
10357 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010358}
10359
10360/*
10361 * "getregtype()" function
10362 */
10363 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010364f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010365 typval_T *argvars;
10366 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010367{
10368 char_u *strregname;
10369 int regname;
10370 char_u buf[NUMBUFLEN + 2];
10371 long reglen = 0;
10372
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010373 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010374 {
10375 strregname = get_tv_string_chk(&argvars[0]);
10376 if (strregname == NULL) /* type error; errmsg already given */
10377 {
10378 rettv->v_type = VAR_STRING;
10379 rettv->vval.v_string = NULL;
10380 return;
10381 }
10382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010383 else
10384 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010385 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010386
10387 regname = (strregname == NULL ? '"' : *strregname);
10388 if (regname == 0)
10389 regname = '"';
10390
10391 buf[0] = NUL;
10392 buf[1] = NUL;
10393 switch (get_reg_type(regname, &reglen))
10394 {
10395 case MLINE: buf[0] = 'V'; break;
10396 case MCHAR: buf[0] = 'v'; break;
10397#ifdef FEAT_VISUAL
10398 case MBLOCK:
10399 buf[0] = Ctrl_V;
10400 sprintf((char *)buf + 1, "%ld", reglen + 1);
10401 break;
10402#endif
10403 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010404 rettv->v_type = VAR_STRING;
10405 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010406}
10407
10408/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010409 * "gettabwinvar()" function
10410 */
10411 static void
10412f_gettabwinvar(argvars, rettv)
10413 typval_T *argvars;
10414 typval_T *rettv;
10415{
10416 getwinvar(argvars, rettv, 1);
10417}
10418
10419/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010420 * "getwinposx()" function
10421 */
10422/*ARGSUSED*/
10423 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010424f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010425 typval_T *argvars;
10426 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010427{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010428 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010429#ifdef FEAT_GUI
10430 if (gui.in_use)
10431 {
10432 int x, y;
10433
10434 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010435 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010436 }
10437#endif
10438}
10439
10440/*
10441 * "getwinposy()" function
10442 */
10443/*ARGSUSED*/
10444 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010445f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010446 typval_T *argvars;
10447 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010448{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010449 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010450#ifdef FEAT_GUI
10451 if (gui.in_use)
10452 {
10453 int x, y;
10454
10455 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010456 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010457 }
10458#endif
10459}
10460
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010461/*
10462 * Find window specifed by "vp" in tabpage "tp".
10463 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000010464 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010465find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000010466 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010467 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000010468{
10469#ifdef FEAT_WINDOWS
10470 win_T *wp;
10471#endif
10472 int nr;
10473
10474 nr = get_tv_number_chk(vp, NULL);
10475
10476#ifdef FEAT_WINDOWS
10477 if (nr < 0)
10478 return NULL;
10479 if (nr == 0)
10480 return curwin;
10481
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010482 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
10483 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000010484 if (--nr <= 0)
10485 break;
10486 return wp;
10487#else
10488 if (nr == 0 || nr == 1)
10489 return curwin;
10490 return NULL;
10491#endif
10492}
10493
Bram Moolenaar071d4272004-06-13 20:20:40 +000010494/*
10495 * "getwinvar()" function
10496 */
10497 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010498f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010499 typval_T *argvars;
10500 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010501{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010502 getwinvar(argvars, rettv, 0);
10503}
10504
10505/*
10506 * getwinvar() and gettabwinvar()
10507 */
10508 static void
10509getwinvar(argvars, rettv, off)
10510 typval_T *argvars;
10511 typval_T *rettv;
10512 int off; /* 1 for gettabwinvar() */
10513{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010514 win_T *win, *oldcurwin;
10515 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010516 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010517 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010518
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010519#ifdef FEAT_WINDOWS
10520 if (off == 1)
10521 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
10522 else
10523 tp = curtab;
10524#endif
10525 win = find_win_by_nr(&argvars[off], tp);
10526 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010527 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010528
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010529 rettv->v_type = VAR_STRING;
10530 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010531
10532 if (win != NULL && varname != NULL)
10533 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000010534 /* Set curwin to be our win, temporarily. Also set curbuf, so
10535 * that we can get buffer-local options. */
10536 oldcurwin = curwin;
10537 curwin = win;
10538 curbuf = win->w_buffer;
10539
Bram Moolenaar071d4272004-06-13 20:20:40 +000010540 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010541 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010542 else
10543 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010544 if (*varname == NUL)
10545 /* let getwinvar({nr}, "") return the "w:" dictionary. The
10546 * scope prefix before the NUL byte is required by
10547 * find_var_in_ht(). */
10548 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010549 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010550 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010551 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010552 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010553 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000010554
10555 /* restore previous notion of curwin */
10556 curwin = oldcurwin;
10557 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010558 }
10559
10560 --emsg_off;
10561}
10562
10563/*
10564 * "glob()" function
10565 */
10566 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010567f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010568 typval_T *argvars;
10569 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010570{
10571 expand_T xpc;
10572
10573 ExpandInit(&xpc);
10574 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010575 rettv->v_type = VAR_STRING;
10576 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010577 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010578}
10579
10580/*
10581 * "globpath()" function
10582 */
10583 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010584f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010585 typval_T *argvars;
10586 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010587{
10588 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010589 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010590
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010591 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010592 if (file == NULL)
10593 rettv->vval.v_string = NULL;
10594 else
10595 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010596}
10597
10598/*
10599 * "has()" function
10600 */
10601 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010602f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010603 typval_T *argvars;
10604 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010605{
10606 int i;
10607 char_u *name;
10608 int n = FALSE;
10609 static char *(has_list[]) =
10610 {
10611#ifdef AMIGA
10612 "amiga",
10613# ifdef FEAT_ARP
10614 "arp",
10615# endif
10616#endif
10617#ifdef __BEOS__
10618 "beos",
10619#endif
10620#ifdef MSDOS
10621# ifdef DJGPP
10622 "dos32",
10623# else
10624 "dos16",
10625# endif
10626#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000010627#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010628 "mac",
10629#endif
10630#if defined(MACOS_X_UNIX)
10631 "macunix",
10632#endif
10633#ifdef OS2
10634 "os2",
10635#endif
10636#ifdef __QNX__
10637 "qnx",
10638#endif
10639#ifdef RISCOS
10640 "riscos",
10641#endif
10642#ifdef UNIX
10643 "unix",
10644#endif
10645#ifdef VMS
10646 "vms",
10647#endif
10648#ifdef WIN16
10649 "win16",
10650#endif
10651#ifdef WIN32
10652 "win32",
10653#endif
10654#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
10655 "win32unix",
10656#endif
10657#ifdef WIN64
10658 "win64",
10659#endif
10660#ifdef EBCDIC
10661 "ebcdic",
10662#endif
10663#ifndef CASE_INSENSITIVE_FILENAME
10664 "fname_case",
10665#endif
10666#ifdef FEAT_ARABIC
10667 "arabic",
10668#endif
10669#ifdef FEAT_AUTOCMD
10670 "autocmd",
10671#endif
10672#ifdef FEAT_BEVAL
10673 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000010674# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
10675 "balloon_multiline",
10676# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010677#endif
10678#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
10679 "builtin_terms",
10680# ifdef ALL_BUILTIN_TCAPS
10681 "all_builtin_terms",
10682# endif
10683#endif
10684#ifdef FEAT_BYTEOFF
10685 "byte_offset",
10686#endif
10687#ifdef FEAT_CINDENT
10688 "cindent",
10689#endif
10690#ifdef FEAT_CLIENTSERVER
10691 "clientserver",
10692#endif
10693#ifdef FEAT_CLIPBOARD
10694 "clipboard",
10695#endif
10696#ifdef FEAT_CMDL_COMPL
10697 "cmdline_compl",
10698#endif
10699#ifdef FEAT_CMDHIST
10700 "cmdline_hist",
10701#endif
10702#ifdef FEAT_COMMENTS
10703 "comments",
10704#endif
10705#ifdef FEAT_CRYPT
10706 "cryptv",
10707#endif
10708#ifdef FEAT_CSCOPE
10709 "cscope",
10710#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010711#ifdef CURSOR_SHAPE
10712 "cursorshape",
10713#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010714#ifdef DEBUG
10715 "debug",
10716#endif
10717#ifdef FEAT_CON_DIALOG
10718 "dialog_con",
10719#endif
10720#ifdef FEAT_GUI_DIALOG
10721 "dialog_gui",
10722#endif
10723#ifdef FEAT_DIFF
10724 "diff",
10725#endif
10726#ifdef FEAT_DIGRAPHS
10727 "digraphs",
10728#endif
10729#ifdef FEAT_DND
10730 "dnd",
10731#endif
10732#ifdef FEAT_EMACS_TAGS
10733 "emacs_tags",
10734#endif
10735 "eval", /* always present, of course! */
10736#ifdef FEAT_EX_EXTRA
10737 "ex_extra",
10738#endif
10739#ifdef FEAT_SEARCH_EXTRA
10740 "extra_search",
10741#endif
10742#ifdef FEAT_FKMAP
10743 "farsi",
10744#endif
10745#ifdef FEAT_SEARCHPATH
10746 "file_in_path",
10747#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010748#if defined(UNIX) && !defined(USE_SYSTEM)
10749 "filterpipe",
10750#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010751#ifdef FEAT_FIND_ID
10752 "find_in_path",
10753#endif
10754#ifdef FEAT_FOLDING
10755 "folding",
10756#endif
10757#ifdef FEAT_FOOTER
10758 "footer",
10759#endif
10760#if !defined(USE_SYSTEM) && defined(UNIX)
10761 "fork",
10762#endif
10763#ifdef FEAT_GETTEXT
10764 "gettext",
10765#endif
10766#ifdef FEAT_GUI
10767 "gui",
10768#endif
10769#ifdef FEAT_GUI_ATHENA
10770# ifdef FEAT_GUI_NEXTAW
10771 "gui_neXtaw",
10772# else
10773 "gui_athena",
10774# endif
10775#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010776#ifdef FEAT_GUI_GTK
10777 "gui_gtk",
10778# ifdef HAVE_GTK2
10779 "gui_gtk2",
10780# endif
10781#endif
10782#ifdef FEAT_GUI_MAC
10783 "gui_mac",
10784#endif
10785#ifdef FEAT_GUI_MOTIF
10786 "gui_motif",
10787#endif
10788#ifdef FEAT_GUI_PHOTON
10789 "gui_photon",
10790#endif
10791#ifdef FEAT_GUI_W16
10792 "gui_win16",
10793#endif
10794#ifdef FEAT_GUI_W32
10795 "gui_win32",
10796#endif
10797#ifdef FEAT_HANGULIN
10798 "hangul_input",
10799#endif
10800#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10801 "iconv",
10802#endif
10803#ifdef FEAT_INS_EXPAND
10804 "insert_expand",
10805#endif
10806#ifdef FEAT_JUMPLIST
10807 "jumplist",
10808#endif
10809#ifdef FEAT_KEYMAP
10810 "keymap",
10811#endif
10812#ifdef FEAT_LANGMAP
10813 "langmap",
10814#endif
10815#ifdef FEAT_LIBCALL
10816 "libcall",
10817#endif
10818#ifdef FEAT_LINEBREAK
10819 "linebreak",
10820#endif
10821#ifdef FEAT_LISP
10822 "lispindent",
10823#endif
10824#ifdef FEAT_LISTCMDS
10825 "listcmds",
10826#endif
10827#ifdef FEAT_LOCALMAP
10828 "localmap",
10829#endif
10830#ifdef FEAT_MENU
10831 "menu",
10832#endif
10833#ifdef FEAT_SESSION
10834 "mksession",
10835#endif
10836#ifdef FEAT_MODIFY_FNAME
10837 "modify_fname",
10838#endif
10839#ifdef FEAT_MOUSE
10840 "mouse",
10841#endif
10842#ifdef FEAT_MOUSESHAPE
10843 "mouseshape",
10844#endif
10845#if defined(UNIX) || defined(VMS)
10846# ifdef FEAT_MOUSE_DEC
10847 "mouse_dec",
10848# endif
10849# ifdef FEAT_MOUSE_GPM
10850 "mouse_gpm",
10851# endif
10852# ifdef FEAT_MOUSE_JSB
10853 "mouse_jsbterm",
10854# endif
10855# ifdef FEAT_MOUSE_NET
10856 "mouse_netterm",
10857# endif
10858# ifdef FEAT_MOUSE_PTERM
10859 "mouse_pterm",
10860# endif
10861# ifdef FEAT_MOUSE_XTERM
10862 "mouse_xterm",
10863# endif
10864#endif
10865#ifdef FEAT_MBYTE
10866 "multi_byte",
10867#endif
10868#ifdef FEAT_MBYTE_IME
10869 "multi_byte_ime",
10870#endif
10871#ifdef FEAT_MULTI_LANG
10872 "multi_lang",
10873#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010874#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010875#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010876 "mzscheme",
10877#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010878#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010879#ifdef FEAT_OLE
10880 "ole",
10881#endif
10882#ifdef FEAT_OSFILETYPE
10883 "osfiletype",
10884#endif
10885#ifdef FEAT_PATH_EXTRA
10886 "path_extra",
10887#endif
10888#ifdef FEAT_PERL
10889#ifndef DYNAMIC_PERL
10890 "perl",
10891#endif
10892#endif
10893#ifdef FEAT_PYTHON
10894#ifndef DYNAMIC_PYTHON
10895 "python",
10896#endif
10897#endif
10898#ifdef FEAT_POSTSCRIPT
10899 "postscript",
10900#endif
10901#ifdef FEAT_PRINTER
10902 "printer",
10903#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000010904#ifdef FEAT_PROFILE
10905 "profile",
10906#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000010907#ifdef FEAT_RELTIME
10908 "reltime",
10909#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010910#ifdef FEAT_QUICKFIX
10911 "quickfix",
10912#endif
10913#ifdef FEAT_RIGHTLEFT
10914 "rightleft",
10915#endif
10916#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
10917 "ruby",
10918#endif
10919#ifdef FEAT_SCROLLBIND
10920 "scrollbind",
10921#endif
10922#ifdef FEAT_CMDL_INFO
10923 "showcmd",
10924 "cmdline_info",
10925#endif
10926#ifdef FEAT_SIGNS
10927 "signs",
10928#endif
10929#ifdef FEAT_SMARTINDENT
10930 "smartindent",
10931#endif
10932#ifdef FEAT_SNIFF
10933 "sniff",
10934#endif
10935#ifdef FEAT_STL_OPT
10936 "statusline",
10937#endif
10938#ifdef FEAT_SUN_WORKSHOP
10939 "sun_workshop",
10940#endif
10941#ifdef FEAT_NETBEANS_INTG
10942 "netbeans_intg",
10943#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000010944#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010945 "spell",
10946#endif
10947#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010948 "syntax",
10949#endif
10950#if defined(USE_SYSTEM) || !defined(UNIX)
10951 "system",
10952#endif
10953#ifdef FEAT_TAG_BINS
10954 "tag_binary",
10955#endif
10956#ifdef FEAT_TAG_OLDSTATIC
10957 "tag_old_static",
10958#endif
10959#ifdef FEAT_TAG_ANYWHITE
10960 "tag_any_white",
10961#endif
10962#ifdef FEAT_TCL
10963# ifndef DYNAMIC_TCL
10964 "tcl",
10965# endif
10966#endif
10967#ifdef TERMINFO
10968 "terminfo",
10969#endif
10970#ifdef FEAT_TERMRESPONSE
10971 "termresponse",
10972#endif
10973#ifdef FEAT_TEXTOBJ
10974 "textobjects",
10975#endif
10976#ifdef HAVE_TGETENT
10977 "tgetent",
10978#endif
10979#ifdef FEAT_TITLE
10980 "title",
10981#endif
10982#ifdef FEAT_TOOLBAR
10983 "toolbar",
10984#endif
10985#ifdef FEAT_USR_CMDS
10986 "user-commands", /* was accidentally included in 5.4 */
10987 "user_commands",
10988#endif
10989#ifdef FEAT_VIMINFO
10990 "viminfo",
10991#endif
10992#ifdef FEAT_VERTSPLIT
10993 "vertsplit",
10994#endif
10995#ifdef FEAT_VIRTUALEDIT
10996 "virtualedit",
10997#endif
10998#ifdef FEAT_VISUAL
10999 "visual",
11000#endif
11001#ifdef FEAT_VISUALEXTRA
11002 "visualextra",
11003#endif
11004#ifdef FEAT_VREPLACE
11005 "vreplace",
11006#endif
11007#ifdef FEAT_WILDIGN
11008 "wildignore",
11009#endif
11010#ifdef FEAT_WILDMENU
11011 "wildmenu",
11012#endif
11013#ifdef FEAT_WINDOWS
11014 "windows",
11015#endif
11016#ifdef FEAT_WAK
11017 "winaltkeys",
11018#endif
11019#ifdef FEAT_WRITEBACKUP
11020 "writebackup",
11021#endif
11022#ifdef FEAT_XIM
11023 "xim",
11024#endif
11025#ifdef FEAT_XFONTSET
11026 "xfontset",
11027#endif
11028#ifdef USE_XSMP
11029 "xsmp",
11030#endif
11031#ifdef USE_XSMP_INTERACT
11032 "xsmp_interact",
11033#endif
11034#ifdef FEAT_XCLIPBOARD
11035 "xterm_clipboard",
11036#endif
11037#ifdef FEAT_XTERM_SAVE
11038 "xterm_save",
11039#endif
11040#if defined(UNIX) && defined(FEAT_X11)
11041 "X11",
11042#endif
11043 NULL
11044 };
11045
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011046 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011047 for (i = 0; has_list[i] != NULL; ++i)
11048 if (STRICMP(name, has_list[i]) == 0)
11049 {
11050 n = TRUE;
11051 break;
11052 }
11053
11054 if (n == FALSE)
11055 {
11056 if (STRNICMP(name, "patch", 5) == 0)
11057 n = has_patch(atoi((char *)name + 5));
11058 else if (STRICMP(name, "vim_starting") == 0)
11059 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011060#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
11061 else if (STRICMP(name, "balloon_multiline") == 0)
11062 n = multiline_balloon_available();
11063#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011064#ifdef DYNAMIC_TCL
11065 else if (STRICMP(name, "tcl") == 0)
11066 n = tcl_enabled(FALSE);
11067#endif
11068#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
11069 else if (STRICMP(name, "iconv") == 0)
11070 n = iconv_enabled(FALSE);
11071#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000011072#ifdef DYNAMIC_MZSCHEME
11073 else if (STRICMP(name, "mzscheme") == 0)
11074 n = mzscheme_enabled(FALSE);
11075#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011076#ifdef DYNAMIC_RUBY
11077 else if (STRICMP(name, "ruby") == 0)
11078 n = ruby_enabled(FALSE);
11079#endif
11080#ifdef DYNAMIC_PYTHON
11081 else if (STRICMP(name, "python") == 0)
11082 n = python_enabled(FALSE);
11083#endif
11084#ifdef DYNAMIC_PERL
11085 else if (STRICMP(name, "perl") == 0)
11086 n = perl_enabled(FALSE);
11087#endif
11088#ifdef FEAT_GUI
11089 else if (STRICMP(name, "gui_running") == 0)
11090 n = (gui.in_use || gui.starting);
11091# ifdef FEAT_GUI_W32
11092 else if (STRICMP(name, "gui_win32s") == 0)
11093 n = gui_is_win32s();
11094# endif
11095# ifdef FEAT_BROWSE
11096 else if (STRICMP(name, "browse") == 0)
11097 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
11098# endif
11099#endif
11100#ifdef FEAT_SYN_HL
11101 else if (STRICMP(name, "syntax_items") == 0)
11102 n = syntax_present(curbuf);
11103#endif
11104#if defined(WIN3264)
11105 else if (STRICMP(name, "win95") == 0)
11106 n = mch_windows95();
11107#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000011108#ifdef FEAT_NETBEANS_INTG
11109 else if (STRICMP(name, "netbeans_enabled") == 0)
11110 n = usingNetbeans;
11111#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011112 }
11113
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011114 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011115}
11116
11117/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000011118 * "has_key()" function
11119 */
11120 static void
11121f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011122 typval_T *argvars;
11123 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011124{
11125 rettv->vval.v_number = 0;
11126 if (argvars[0].v_type != VAR_DICT)
11127 {
11128 EMSG(_(e_dictreq));
11129 return;
11130 }
11131 if (argvars[0].vval.v_dict == NULL)
11132 return;
11133
11134 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011135 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011136}
11137
11138/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000011139 * "haslocaldir()" function
11140 */
11141/*ARGSUSED*/
11142 static void
11143f_haslocaldir(argvars, rettv)
11144 typval_T *argvars;
11145 typval_T *rettv;
11146{
11147 rettv->vval.v_number = (curwin->w_localdir != NULL);
11148}
11149
11150/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011151 * "hasmapto()" function
11152 */
11153 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011154f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011155 typval_T *argvars;
11156 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011157{
11158 char_u *name;
11159 char_u *mode;
11160 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000011161 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011162
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011163 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011164 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011165 mode = (char_u *)"nvo";
11166 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000011167 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011168 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000011169 if (argvars[2].v_type != VAR_UNKNOWN)
11170 abbr = get_tv_number(&argvars[2]);
11171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011172
Bram Moolenaar2c932302006-03-18 21:42:09 +000011173 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011174 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011175 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011176 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011177}
11178
11179/*
11180 * "histadd()" function
11181 */
11182/*ARGSUSED*/
11183 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011184f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011185 typval_T *argvars;
11186 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011187{
11188#ifdef FEAT_CMDHIST
11189 int histype;
11190 char_u *str;
11191 char_u buf[NUMBUFLEN];
11192#endif
11193
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011194 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011195 if (check_restricted() || check_secure())
11196 return;
11197#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011198 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11199 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011200 if (histype >= 0)
11201 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011202 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011203 if (*str != NUL)
11204 {
11205 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011206 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011207 return;
11208 }
11209 }
11210#endif
11211}
11212
11213/*
11214 * "histdel()" function
11215 */
11216/*ARGSUSED*/
11217 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011218f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011219 typval_T *argvars;
11220 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011221{
11222#ifdef FEAT_CMDHIST
11223 int n;
11224 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011225 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011226
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011227 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11228 if (str == NULL)
11229 n = 0;
11230 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011231 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011232 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011233 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011234 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011235 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011236 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011237 else
11238 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011239 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011240 get_tv_string_buf(&argvars[1], buf));
11241 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011242#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011243 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011244#endif
11245}
11246
11247/*
11248 * "histget()" function
11249 */
11250/*ARGSUSED*/
11251 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011252f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011253 typval_T *argvars;
11254 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011255{
11256#ifdef FEAT_CMDHIST
11257 int type;
11258 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011259 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011260
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011261 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11262 if (str == NULL)
11263 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011264 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011265 {
11266 type = get_histtype(str);
11267 if (argvars[1].v_type == VAR_UNKNOWN)
11268 idx = get_history_idx(type);
11269 else
11270 idx = (int)get_tv_number_chk(&argvars[1], NULL);
11271 /* -1 on type error */
11272 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
11273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011274#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011275 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011276#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011277 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011278}
11279
11280/*
11281 * "histnr()" function
11282 */
11283/*ARGSUSED*/
11284 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011285f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011286 typval_T *argvars;
11287 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011288{
11289 int i;
11290
11291#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011292 char_u *history = get_tv_string_chk(&argvars[0]);
11293
11294 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011295 if (i >= HIST_CMD && i < HIST_COUNT)
11296 i = get_history_idx(i);
11297 else
11298#endif
11299 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011300 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011301}
11302
11303/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011304 * "highlightID(name)" function
11305 */
11306 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011307f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011308 typval_T *argvars;
11309 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011310{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011311 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011312}
11313
11314/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011315 * "highlight_exists()" function
11316 */
11317 static void
11318f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011319 typval_T *argvars;
11320 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011321{
11322 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
11323}
11324
11325/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011326 * "hostname()" function
11327 */
11328/*ARGSUSED*/
11329 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011330f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011331 typval_T *argvars;
11332 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011333{
11334 char_u hostname[256];
11335
11336 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011337 rettv->v_type = VAR_STRING;
11338 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011339}
11340
11341/*
11342 * iconv() function
11343 */
11344/*ARGSUSED*/
11345 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011346f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011347 typval_T *argvars;
11348 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011349{
11350#ifdef FEAT_MBYTE
11351 char_u buf1[NUMBUFLEN];
11352 char_u buf2[NUMBUFLEN];
11353 char_u *from, *to, *str;
11354 vimconv_T vimconv;
11355#endif
11356
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011357 rettv->v_type = VAR_STRING;
11358 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011359
11360#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011361 str = get_tv_string(&argvars[0]);
11362 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
11363 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011364 vimconv.vc_type = CONV_NONE;
11365 convert_setup(&vimconv, from, to);
11366
11367 /* If the encodings are equal, no conversion needed. */
11368 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011369 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011370 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011371 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011372
11373 convert_setup(&vimconv, NULL, NULL);
11374 vim_free(from);
11375 vim_free(to);
11376#endif
11377}
11378
11379/*
11380 * "indent()" function
11381 */
11382 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011383f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011384 typval_T *argvars;
11385 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011386{
11387 linenr_T lnum;
11388
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011389 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011390 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011391 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011392 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011393 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011394}
11395
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011396/*
11397 * "index()" function
11398 */
11399 static void
11400f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011401 typval_T *argvars;
11402 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011403{
Bram Moolenaar33570922005-01-25 22:26:29 +000011404 list_T *l;
11405 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011406 long idx = 0;
11407 int ic = FALSE;
11408
11409 rettv->vval.v_number = -1;
11410 if (argvars[0].v_type != VAR_LIST)
11411 {
11412 EMSG(_(e_listreq));
11413 return;
11414 }
11415 l = argvars[0].vval.v_list;
11416 if (l != NULL)
11417 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011418 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011419 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011420 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011421 int error = FALSE;
11422
Bram Moolenaar758711c2005-02-02 23:11:38 +000011423 /* Start at specified item. Use the cached index that list_find()
11424 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011425 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000011426 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011427 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011428 ic = get_tv_number_chk(&argvars[3], &error);
11429 if (error)
11430 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011431 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011432
Bram Moolenaar758711c2005-02-02 23:11:38 +000011433 for ( ; item != NULL; item = item->li_next, ++idx)
11434 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011435 {
11436 rettv->vval.v_number = idx;
11437 break;
11438 }
11439 }
11440}
11441
Bram Moolenaar071d4272004-06-13 20:20:40 +000011442static int inputsecret_flag = 0;
11443
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011444static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
11445
Bram Moolenaar071d4272004-06-13 20:20:40 +000011446/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011447 * This function is used by f_input() and f_inputdialog() functions. The third
11448 * argument to f_input() specifies the type of completion to use at the
11449 * prompt. The third argument to f_inputdialog() specifies the value to return
11450 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011451 */
11452 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011453get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000011454 typval_T *argvars;
11455 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011456 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011457{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011458 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011459 char_u *p = NULL;
11460 int c;
11461 char_u buf[NUMBUFLEN];
11462 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011463 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011464 int xp_type = EXPAND_NOTHING;
11465 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011466
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011467 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011468
11469#ifdef NO_CONSOLE_INPUT
11470 /* While starting up, there is no place to enter text. */
11471 if (no_console_input())
11472 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011473 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011474 return;
11475 }
11476#endif
11477
11478 cmd_silent = FALSE; /* Want to see the prompt. */
11479 if (prompt != NULL)
11480 {
11481 /* Only the part of the message after the last NL is considered as
11482 * prompt for the command line */
11483 p = vim_strrchr(prompt, '\n');
11484 if (p == NULL)
11485 p = prompt;
11486 else
11487 {
11488 ++p;
11489 c = *p;
11490 *p = NUL;
11491 msg_start();
11492 msg_clr_eos();
11493 msg_puts_attr(prompt, echo_attr);
11494 msg_didout = FALSE;
11495 msg_starthere();
11496 *p = c;
11497 }
11498 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011499
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011500 if (argvars[1].v_type != VAR_UNKNOWN)
11501 {
11502 defstr = get_tv_string_buf_chk(&argvars[1], buf);
11503 if (defstr != NULL)
11504 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011505
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011506 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000011507 {
11508 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000011509 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000011510 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011511
Bram Moolenaar4463f292005-09-25 22:20:24 +000011512 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011513
Bram Moolenaar4463f292005-09-25 22:20:24 +000011514 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
11515 if (xp_name == NULL)
11516 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011517
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011518 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011519
Bram Moolenaar4463f292005-09-25 22:20:24 +000011520 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
11521 &xp_arg) == FAIL)
11522 return;
11523 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011524 }
11525
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011526 if (defstr != NULL)
11527 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011528 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
11529 xp_type, xp_arg);
11530
11531 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011532
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011533 /* since the user typed this, no need to wait for return */
11534 need_wait_return = FALSE;
11535 msg_didout = FALSE;
11536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011537 cmd_silent = cmd_silent_save;
11538}
11539
11540/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011541 * "input()" function
11542 * Also handles inputsecret() when inputsecret is set.
11543 */
11544 static void
11545f_input(argvars, rettv)
11546 typval_T *argvars;
11547 typval_T *rettv;
11548{
11549 get_user_input(argvars, rettv, FALSE);
11550}
11551
11552/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011553 * "inputdialog()" function
11554 */
11555 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011556f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011557 typval_T *argvars;
11558 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011559{
11560#if defined(FEAT_GUI_TEXTDIALOG)
11561 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
11562 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
11563 {
11564 char_u *message;
11565 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011566 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000011567
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011568 message = get_tv_string_chk(&argvars[0]);
11569 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000011570 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000011571 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011572 else
11573 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011574 if (message != NULL && defstr != NULL
11575 && do_dialog(VIM_QUESTION, NULL, message,
11576 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011577 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011578 else
11579 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011580 if (message != NULL && defstr != NULL
11581 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011582 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011583 rettv->vval.v_string = vim_strsave(
11584 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011585 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011586 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011587 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011588 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011589 }
11590 else
11591#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011592 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011593}
11594
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011595/*
11596 * "inputlist()" function
11597 */
11598 static void
11599f_inputlist(argvars, rettv)
11600 typval_T *argvars;
11601 typval_T *rettv;
11602{
11603 listitem_T *li;
11604 int selected;
11605 int mouse_used;
11606
11607 rettv->vval.v_number = 0;
11608#ifdef NO_CONSOLE_INPUT
11609 /* While starting up, there is no place to enter text. */
11610 if (no_console_input())
11611 return;
11612#endif
11613 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
11614 {
11615 EMSG2(_(e_listarg), "inputlist()");
11616 return;
11617 }
11618
11619 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000011620 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011621 lines_left = Rows; /* avoid more prompt */
11622 msg_scroll = TRUE;
11623 msg_clr_eos();
11624
11625 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
11626 {
11627 msg_puts(get_tv_string(&li->li_tv));
11628 msg_putchar('\n');
11629 }
11630
11631 /* Ask for choice. */
11632 selected = prompt_for_number(&mouse_used);
11633 if (mouse_used)
11634 selected -= lines_left;
11635
11636 rettv->vval.v_number = selected;
11637}
11638
11639
Bram Moolenaar071d4272004-06-13 20:20:40 +000011640static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
11641
11642/*
11643 * "inputrestore()" function
11644 */
11645/*ARGSUSED*/
11646 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011647f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011648 typval_T *argvars;
11649 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011650{
11651 if (ga_userinput.ga_len > 0)
11652 {
11653 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011654 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
11655 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011656 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011657 }
11658 else if (p_verbose > 1)
11659 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000011660 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011661 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011662 }
11663}
11664
11665/*
11666 * "inputsave()" function
11667 */
11668/*ARGSUSED*/
11669 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011670f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011671 typval_T *argvars;
11672 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011673{
11674 /* Add an entry to the stack of typehead storage. */
11675 if (ga_grow(&ga_userinput, 1) == OK)
11676 {
11677 save_typeahead((tasave_T *)(ga_userinput.ga_data)
11678 + ga_userinput.ga_len);
11679 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011680 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011681 }
11682 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011683 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011684}
11685
11686/*
11687 * "inputsecret()" function
11688 */
11689 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011690f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011691 typval_T *argvars;
11692 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011693{
11694 ++cmdline_star;
11695 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011696 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011697 --cmdline_star;
11698 --inputsecret_flag;
11699}
11700
11701/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011702 * "insert()" function
11703 */
11704 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011705f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011706 typval_T *argvars;
11707 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011708{
11709 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011710 listitem_T *item;
11711 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011712 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011713
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011714 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011715 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011716 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011717 else if ((l = argvars[0].vval.v_list) != NULL
11718 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011719 {
11720 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011721 before = get_tv_number_chk(&argvars[2], &error);
11722 if (error)
11723 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011724
Bram Moolenaar758711c2005-02-02 23:11:38 +000011725 if (before == l->lv_len)
11726 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011727 else
11728 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011729 item = list_find(l, before);
11730 if (item == NULL)
11731 {
11732 EMSGN(_(e_listidx), before);
11733 l = NULL;
11734 }
11735 }
11736 if (l != NULL)
11737 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011738 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011739 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011740 }
11741 }
11742}
11743
11744/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011745 * "isdirectory()" function
11746 */
11747 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011748f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011749 typval_T *argvars;
11750 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011751{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011752 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011753}
11754
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011755/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011756 * "islocked()" function
11757 */
11758 static void
11759f_islocked(argvars, rettv)
11760 typval_T *argvars;
11761 typval_T *rettv;
11762{
11763 lval_T lv;
11764 char_u *end;
11765 dictitem_T *di;
11766
11767 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000011768 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
11769 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011770 if (end != NULL && lv.ll_name != NULL)
11771 {
11772 if (*end != NUL)
11773 EMSG(_(e_trailing));
11774 else
11775 {
11776 if (lv.ll_tv == NULL)
11777 {
11778 if (check_changedtick(lv.ll_name))
11779 rettv->vval.v_number = 1; /* always locked */
11780 else
11781 {
11782 di = find_var(lv.ll_name, NULL);
11783 if (di != NULL)
11784 {
11785 /* Consider a variable locked when:
11786 * 1. the variable itself is locked
11787 * 2. the value of the variable is locked.
11788 * 3. the List or Dict value is locked.
11789 */
11790 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
11791 || tv_islocked(&di->di_tv));
11792 }
11793 }
11794 }
11795 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011796 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011797 else if (lv.ll_newkey != NULL)
11798 EMSG2(_(e_dictkey), lv.ll_newkey);
11799 else if (lv.ll_list != NULL)
11800 /* List item. */
11801 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
11802 else
11803 /* Dictionary item. */
11804 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
11805 }
11806 }
11807
11808 clear_lval(&lv);
11809}
11810
Bram Moolenaar33570922005-01-25 22:26:29 +000011811static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011812
11813/*
11814 * Turn a dict into a list:
11815 * "what" == 0: list of keys
11816 * "what" == 1: list of values
11817 * "what" == 2: list of items
11818 */
11819 static void
11820dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000011821 typval_T *argvars;
11822 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011823 int what;
11824{
Bram Moolenaar33570922005-01-25 22:26:29 +000011825 list_T *l2;
11826 dictitem_T *di;
11827 hashitem_T *hi;
11828 listitem_T *li;
11829 listitem_T *li2;
11830 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011831 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011832
11833 rettv->vval.v_number = 0;
11834 if (argvars[0].v_type != VAR_DICT)
11835 {
11836 EMSG(_(e_dictreq));
11837 return;
11838 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011839 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011840 return;
11841
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011842 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011843 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011844
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011845 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000011846 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011847 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011848 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011849 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011850 --todo;
11851 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011852
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011853 li = listitem_alloc();
11854 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011855 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011856 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011857
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011858 if (what == 0)
11859 {
11860 /* keys() */
11861 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011862 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011863 li->li_tv.vval.v_string = vim_strsave(di->di_key);
11864 }
11865 else if (what == 1)
11866 {
11867 /* values() */
11868 copy_tv(&di->di_tv, &li->li_tv);
11869 }
11870 else
11871 {
11872 /* items() */
11873 l2 = list_alloc();
11874 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011875 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011876 li->li_tv.vval.v_list = l2;
11877 if (l2 == NULL)
11878 break;
11879 ++l2->lv_refcount;
11880
11881 li2 = listitem_alloc();
11882 if (li2 == NULL)
11883 break;
11884 list_append(l2, li2);
11885 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011886 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011887 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
11888
11889 li2 = listitem_alloc();
11890 if (li2 == NULL)
11891 break;
11892 list_append(l2, li2);
11893 copy_tv(&di->di_tv, &li2->li_tv);
11894 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011895 }
11896 }
11897}
11898
11899/*
11900 * "items(dict)" function
11901 */
11902 static void
11903f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011904 typval_T *argvars;
11905 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011906{
11907 dict_list(argvars, rettv, 2);
11908}
11909
Bram Moolenaar071d4272004-06-13 20:20:40 +000011910/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011911 * "join()" function
11912 */
11913 static void
11914f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011915 typval_T *argvars;
11916 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011917{
11918 garray_T ga;
11919 char_u *sep;
11920
11921 rettv->vval.v_number = 0;
11922 if (argvars[0].v_type != VAR_LIST)
11923 {
11924 EMSG(_(e_listreq));
11925 return;
11926 }
11927 if (argvars[0].vval.v_list == NULL)
11928 return;
11929 if (argvars[1].v_type == VAR_UNKNOWN)
11930 sep = (char_u *)" ";
11931 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011932 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011933
11934 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011935
11936 if (sep != NULL)
11937 {
11938 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000011939 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011940 ga_append(&ga, NUL);
11941 rettv->vval.v_string = (char_u *)ga.ga_data;
11942 }
11943 else
11944 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011945}
11946
11947/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011948 * "keys()" function
11949 */
11950 static void
11951f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011952 typval_T *argvars;
11953 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011954{
11955 dict_list(argvars, rettv, 0);
11956}
11957
11958/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011959 * "last_buffer_nr()" function.
11960 */
11961/*ARGSUSED*/
11962 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011963f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011964 typval_T *argvars;
11965 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011966{
11967 int n = 0;
11968 buf_T *buf;
11969
11970 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
11971 if (n < buf->b_fnum)
11972 n = buf->b_fnum;
11973
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011974 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011975}
11976
11977/*
11978 * "len()" function
11979 */
11980 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011981f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011982 typval_T *argvars;
11983 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011984{
11985 switch (argvars[0].v_type)
11986 {
11987 case VAR_STRING:
11988 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011989 rettv->vval.v_number = (varnumber_T)STRLEN(
11990 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011991 break;
11992 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011993 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011994 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011995 case VAR_DICT:
11996 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
11997 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011998 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011999 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012000 break;
12001 }
12002}
12003
Bram Moolenaar33570922005-01-25 22:26:29 +000012004static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012005
12006 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012007libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000012008 typval_T *argvars;
12009 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012010 int type;
12011{
12012#ifdef FEAT_LIBCALL
12013 char_u *string_in;
12014 char_u **string_result;
12015 int nr_result;
12016#endif
12017
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012018 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012019 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012020 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012021 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012022 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012023
12024 if (check_restricted() || check_secure())
12025 return;
12026
12027#ifdef FEAT_LIBCALL
12028 /* The first two args must be strings, otherwise its meaningless */
12029 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
12030 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012031 string_in = NULL;
12032 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012033 string_in = argvars[2].vval.v_string;
12034 if (type == VAR_NUMBER)
12035 string_result = NULL;
12036 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012037 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012038 if (mch_libcall(argvars[0].vval.v_string,
12039 argvars[1].vval.v_string,
12040 string_in,
12041 argvars[2].vval.v_number,
12042 string_result,
12043 &nr_result) == OK
12044 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012045 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012046 }
12047#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012048}
12049
12050/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012051 * "libcall()" function
12052 */
12053 static void
12054f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012055 typval_T *argvars;
12056 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012057{
12058 libcall_common(argvars, rettv, VAR_STRING);
12059}
12060
12061/*
12062 * "libcallnr()" function
12063 */
12064 static void
12065f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012066 typval_T *argvars;
12067 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012068{
12069 libcall_common(argvars, rettv, VAR_NUMBER);
12070}
12071
12072/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012073 * "line(string)" function
12074 */
12075 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012076f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012077 typval_T *argvars;
12078 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012079{
12080 linenr_T lnum = 0;
12081 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012082 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012083
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012084 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012085 if (fp != NULL)
12086 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012087 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012088}
12089
12090/*
12091 * "line2byte(lnum)" function
12092 */
12093/*ARGSUSED*/
12094 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012095f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012096 typval_T *argvars;
12097 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012098{
12099#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012100 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012101#else
12102 linenr_T lnum;
12103
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012104 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012105 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012106 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012107 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012108 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
12109 if (rettv->vval.v_number >= 0)
12110 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012111#endif
12112}
12113
12114/*
12115 * "lispindent(lnum)" function
12116 */
12117 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012118f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012119 typval_T *argvars;
12120 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012121{
12122#ifdef FEAT_LISP
12123 pos_T pos;
12124 linenr_T lnum;
12125
12126 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012127 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012128 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12129 {
12130 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012131 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012132 curwin->w_cursor = pos;
12133 }
12134 else
12135#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012136 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012137}
12138
12139/*
12140 * "localtime()" function
12141 */
12142/*ARGSUSED*/
12143 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012144f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012145 typval_T *argvars;
12146 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012147{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012148 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012149}
12150
Bram Moolenaar33570922005-01-25 22:26:29 +000012151static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012152
12153 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012154get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000012155 typval_T *argvars;
12156 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012157 int exact;
12158{
12159 char_u *keys;
12160 char_u *which;
12161 char_u buf[NUMBUFLEN];
12162 char_u *keys_buf = NULL;
12163 char_u *rhs;
12164 int mode;
12165 garray_T ga;
Bram Moolenaar2c932302006-03-18 21:42:09 +000012166 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012167
12168 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012169 rettv->v_type = VAR_STRING;
12170 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012171
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012172 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012173 if (*keys == NUL)
12174 return;
12175
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012176 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000012177 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012178 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012179 if (argvars[2].v_type != VAR_UNKNOWN)
12180 abbr = get_tv_number(&argvars[2]);
12181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012182 else
12183 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012184 if (which == NULL)
12185 return;
12186
Bram Moolenaar071d4272004-06-13 20:20:40 +000012187 mode = get_map_mode(&which, 0);
12188
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000012189 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012190 rhs = check_map(keys, mode, exact, FALSE, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012191 vim_free(keys_buf);
12192 if (rhs != NULL)
12193 {
12194 ga_init(&ga);
12195 ga.ga_itemsize = 1;
12196 ga.ga_growsize = 40;
12197
12198 while (*rhs != NUL)
12199 ga_concat(&ga, str2special(&rhs, FALSE));
12200
12201 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012202 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012203 }
12204}
12205
12206/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012207 * "map()" function
12208 */
12209 static void
12210f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012211 typval_T *argvars;
12212 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012213{
12214 filter_map(argvars, rettv, TRUE);
12215}
12216
12217/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012218 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012219 */
12220 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012221f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012222 typval_T *argvars;
12223 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012224{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012225 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012226}
12227
12228/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012229 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012230 */
12231 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012232f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012233 typval_T *argvars;
12234 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012235{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012236 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012237}
12238
Bram Moolenaar33570922005-01-25 22:26:29 +000012239static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012240
12241 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012242find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000012243 typval_T *argvars;
12244 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012245 int type;
12246{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012247 char_u *str = NULL;
12248 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012249 char_u *pat;
12250 regmatch_T regmatch;
12251 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012252 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000012253 char_u *save_cpo;
12254 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012255 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012256 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012257 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012258 list_T *l = NULL;
12259 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012260 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012261 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012262
12263 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12264 save_cpo = p_cpo;
12265 p_cpo = (char_u *)"";
12266
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012267 rettv->vval.v_number = -1;
12268 if (type == 3)
12269 {
12270 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012271 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012272 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012273 }
12274 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012275 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012276 rettv->v_type = VAR_STRING;
12277 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012279
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012280 if (argvars[0].v_type == VAR_LIST)
12281 {
12282 if ((l = argvars[0].vval.v_list) == NULL)
12283 goto theend;
12284 li = l->lv_first;
12285 }
12286 else
12287 expr = str = get_tv_string(&argvars[0]);
12288
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012289 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
12290 if (pat == NULL)
12291 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012292
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012293 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012294 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012295 int error = FALSE;
12296
12297 start = get_tv_number_chk(&argvars[2], &error);
12298 if (error)
12299 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012300 if (l != NULL)
12301 {
12302 li = list_find(l, start);
12303 if (li == NULL)
12304 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012305 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012306 }
12307 else
12308 {
12309 if (start < 0)
12310 start = 0;
12311 if (start > (long)STRLEN(str))
12312 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012313 /* When "count" argument is there ignore matches before "start",
12314 * otherwise skip part of the string. Differs when pattern is "^"
12315 * or "\<". */
12316 if (argvars[3].v_type != VAR_UNKNOWN)
12317 startcol = start;
12318 else
12319 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012320 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012321
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012322 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012323 nth = get_tv_number_chk(&argvars[3], &error);
12324 if (error)
12325 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012326 }
12327
12328 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12329 if (regmatch.regprog != NULL)
12330 {
12331 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012332
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012333 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012334 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012335 if (l != NULL)
12336 {
12337 if (li == NULL)
12338 {
12339 match = FALSE;
12340 break;
12341 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012342 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012343 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012344 if (str == NULL)
12345 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012346 }
12347
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012348 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012349
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012350 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012351 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012352 if (l == NULL && !match)
12353 break;
12354
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012355 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012356 if (l != NULL)
12357 {
12358 li = li->li_next;
12359 ++idx;
12360 }
12361 else
12362 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012363#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012364 startcol = (colnr_T)(regmatch.startp[0]
12365 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012366#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012367 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012368#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012369 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012370 }
12371
12372 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012373 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012374 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012375 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012376 int i;
12377
12378 /* return list with matched string and submatches */
12379 for (i = 0; i < NSUBEXP; ++i)
12380 {
12381 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000012382 {
12383 if (list_append_string(rettv->vval.v_list,
12384 (char_u *)"", 0) == FAIL)
12385 break;
12386 }
12387 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000012388 regmatch.startp[i],
12389 (int)(regmatch.endp[i] - regmatch.startp[i]))
12390 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012391 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012392 }
12393 }
12394 else if (type == 2)
12395 {
12396 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012397 if (l != NULL)
12398 copy_tv(&li->li_tv, rettv);
12399 else
12400 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000012401 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012402 }
12403 else if (l != NULL)
12404 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012405 else
12406 {
12407 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012408 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012409 (varnumber_T)(regmatch.startp[0] - str);
12410 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012411 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012412 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012413 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012414 }
12415 }
12416 vim_free(regmatch.regprog);
12417 }
12418
12419theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012420 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012421 p_cpo = save_cpo;
12422}
12423
12424/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012425 * "match()" function
12426 */
12427 static void
12428f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012429 typval_T *argvars;
12430 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012431{
12432 find_some_match(argvars, rettv, 1);
12433}
12434
12435/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012436 * "matcharg()" function
12437 */
12438 static void
12439f_matcharg(argvars, rettv)
12440 typval_T *argvars;
12441 typval_T *rettv;
12442{
12443 if (rettv_list_alloc(rettv) == OK)
12444 {
12445#ifdef FEAT_SEARCH_EXTRA
12446 int mi = get_tv_number(&argvars[0]);
12447
12448 if (mi >= 1 && mi <= 3)
12449 {
12450 list_append_string(rettv->vval.v_list,
12451 syn_id2name(curwin->w_match_id[mi - 1]), -1);
12452 list_append_string(rettv->vval.v_list,
12453 curwin->w_match_pat[mi - 1], -1);
12454 }
12455#endif
12456 }
12457}
12458
12459/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012460 * "matchend()" function
12461 */
12462 static void
12463f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012464 typval_T *argvars;
12465 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012466{
12467 find_some_match(argvars, rettv, 0);
12468}
12469
12470/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012471 * "matchlist()" function
12472 */
12473 static void
12474f_matchlist(argvars, rettv)
12475 typval_T *argvars;
12476 typval_T *rettv;
12477{
12478 find_some_match(argvars, rettv, 3);
12479}
12480
12481/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012482 * "matchstr()" function
12483 */
12484 static void
12485f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012486 typval_T *argvars;
12487 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012488{
12489 find_some_match(argvars, rettv, 2);
12490}
12491
Bram Moolenaar33570922005-01-25 22:26:29 +000012492static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012493
12494 static void
12495max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000012496 typval_T *argvars;
12497 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012498 int domax;
12499{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012500 long n = 0;
12501 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012502 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012503
12504 if (argvars[0].v_type == VAR_LIST)
12505 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012506 list_T *l;
12507 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012508
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012509 l = argvars[0].vval.v_list;
12510 if (l != NULL)
12511 {
12512 li = l->lv_first;
12513 if (li != NULL)
12514 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012515 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012516 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012517 {
12518 li = li->li_next;
12519 if (li == NULL)
12520 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012521 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012522 if (domax ? i > n : i < n)
12523 n = i;
12524 }
12525 }
12526 }
12527 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012528 else if (argvars[0].v_type == VAR_DICT)
12529 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012530 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012531 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000012532 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012533 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012534
12535 d = argvars[0].vval.v_dict;
12536 if (d != NULL)
12537 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012538 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000012539 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012540 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012541 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000012542 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012543 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012544 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012545 if (first)
12546 {
12547 n = i;
12548 first = FALSE;
12549 }
12550 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012551 n = i;
12552 }
12553 }
12554 }
12555 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012556 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000012557 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012558 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012559}
12560
12561/*
12562 * "max()" function
12563 */
12564 static void
12565f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012566 typval_T *argvars;
12567 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012568{
12569 max_min(argvars, rettv, TRUE);
12570}
12571
12572/*
12573 * "min()" function
12574 */
12575 static void
12576f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012577 typval_T *argvars;
12578 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012579{
12580 max_min(argvars, rettv, FALSE);
12581}
12582
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012583static int mkdir_recurse __ARGS((char_u *dir, int prot));
12584
12585/*
12586 * Create the directory in which "dir" is located, and higher levels when
12587 * needed.
12588 */
12589 static int
12590mkdir_recurse(dir, prot)
12591 char_u *dir;
12592 int prot;
12593{
12594 char_u *p;
12595 char_u *updir;
12596 int r = FAIL;
12597
12598 /* Get end of directory name in "dir".
12599 * We're done when it's "/" or "c:/". */
12600 p = gettail_sep(dir);
12601 if (p <= get_past_head(dir))
12602 return OK;
12603
12604 /* If the directory exists we're done. Otherwise: create it.*/
12605 updir = vim_strnsave(dir, (int)(p - dir));
12606 if (updir == NULL)
12607 return FAIL;
12608 if (mch_isdir(updir))
12609 r = OK;
12610 else if (mkdir_recurse(updir, prot) == OK)
12611 r = vim_mkdir_emsg(updir, prot);
12612 vim_free(updir);
12613 return r;
12614}
12615
12616#ifdef vim_mkdir
12617/*
12618 * "mkdir()" function
12619 */
12620 static void
12621f_mkdir(argvars, rettv)
12622 typval_T *argvars;
12623 typval_T *rettv;
12624{
12625 char_u *dir;
12626 char_u buf[NUMBUFLEN];
12627 int prot = 0755;
12628
12629 rettv->vval.v_number = FAIL;
12630 if (check_restricted() || check_secure())
12631 return;
12632
12633 dir = get_tv_string_buf(&argvars[0], buf);
12634 if (argvars[1].v_type != VAR_UNKNOWN)
12635 {
12636 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012637 prot = get_tv_number_chk(&argvars[2], NULL);
12638 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012639 mkdir_recurse(dir, prot);
12640 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012641 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012642}
12643#endif
12644
Bram Moolenaar0d660222005-01-07 21:51:51 +000012645/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012646 * "mode()" function
12647 */
12648/*ARGSUSED*/
12649 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012650f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012651 typval_T *argvars;
12652 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012653{
12654 char_u buf[2];
12655
12656#ifdef FEAT_VISUAL
12657 if (VIsual_active)
12658 {
12659 if (VIsual_select)
12660 buf[0] = VIsual_mode + 's' - 'v';
12661 else
12662 buf[0] = VIsual_mode;
12663 }
12664 else
12665#endif
12666 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
12667 buf[0] = 'r';
12668 else if (State & INSERT)
12669 {
12670 if (State & REPLACE_FLAG)
12671 buf[0] = 'R';
12672 else
12673 buf[0] = 'i';
12674 }
12675 else if (State & CMDLINE)
12676 buf[0] = 'c';
12677 else
12678 buf[0] = 'n';
12679
12680 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012681 rettv->vval.v_string = vim_strsave(buf);
12682 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012683}
12684
12685/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012686 * "nextnonblank()" function
12687 */
12688 static void
12689f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012690 typval_T *argvars;
12691 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012692{
12693 linenr_T lnum;
12694
12695 for (lnum = get_tv_lnum(argvars); ; ++lnum)
12696 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012697 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012698 {
12699 lnum = 0;
12700 break;
12701 }
12702 if (*skipwhite(ml_get(lnum)) != NUL)
12703 break;
12704 }
12705 rettv->vval.v_number = lnum;
12706}
12707
12708/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012709 * "nr2char()" function
12710 */
12711 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012712f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012713 typval_T *argvars;
12714 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012715{
12716 char_u buf[NUMBUFLEN];
12717
12718#ifdef FEAT_MBYTE
12719 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012720 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012721 else
12722#endif
12723 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012724 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012725 buf[1] = NUL;
12726 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012727 rettv->v_type = VAR_STRING;
12728 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012729}
12730
12731/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012732 * "pathshorten()" function
12733 */
12734 static void
12735f_pathshorten(argvars, rettv)
12736 typval_T *argvars;
12737 typval_T *rettv;
12738{
12739 char_u *p;
12740
12741 rettv->v_type = VAR_STRING;
12742 p = get_tv_string_chk(&argvars[0]);
12743 if (p == NULL)
12744 rettv->vval.v_string = NULL;
12745 else
12746 {
12747 p = vim_strsave(p);
12748 rettv->vval.v_string = p;
12749 if (p != NULL)
12750 shorten_dir(p);
12751 }
12752}
12753
12754/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012755 * "prevnonblank()" function
12756 */
12757 static void
12758f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012759 typval_T *argvars;
12760 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012761{
12762 linenr_T lnum;
12763
12764 lnum = get_tv_lnum(argvars);
12765 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
12766 lnum = 0;
12767 else
12768 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
12769 --lnum;
12770 rettv->vval.v_number = lnum;
12771}
12772
Bram Moolenaara6c840d2005-08-22 22:59:46 +000012773#ifdef HAVE_STDARG_H
12774/* This dummy va_list is here because:
12775 * - passing a NULL pointer doesn't work when va_list isn't a pointer
12776 * - locally in the function results in a "used before set" warning
12777 * - using va_start() to initialize it gives "function with fixed args" error */
12778static va_list ap;
12779#endif
12780
Bram Moolenaar8c711452005-01-14 21:53:12 +000012781/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012782 * "printf()" function
12783 */
12784 static void
12785f_printf(argvars, rettv)
12786 typval_T *argvars;
12787 typval_T *rettv;
12788{
12789 rettv->v_type = VAR_STRING;
12790 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000012791#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012792 {
12793 char_u buf[NUMBUFLEN];
12794 int len;
12795 char_u *s;
12796 int saved_did_emsg = did_emsg;
12797 char *fmt;
12798
12799 /* Get the required length, allocate the buffer and do it for real. */
12800 did_emsg = FALSE;
12801 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012802 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012803 if (!did_emsg)
12804 {
12805 s = alloc(len + 1);
12806 if (s != NULL)
12807 {
12808 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012809 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012810 }
12811 }
12812 did_emsg |= saved_did_emsg;
12813 }
12814#endif
12815}
12816
12817/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000012818 * "pumvisible()" function
12819 */
12820/*ARGSUSED*/
12821 static void
12822f_pumvisible(argvars, rettv)
12823 typval_T *argvars;
12824 typval_T *rettv;
12825{
12826 rettv->vval.v_number = 0;
12827#ifdef FEAT_INS_EXPAND
12828 if (pum_visible())
12829 rettv->vval.v_number = 1;
12830#endif
12831}
12832
12833/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012834 * "range()" function
12835 */
12836 static void
12837f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012838 typval_T *argvars;
12839 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012840{
12841 long start;
12842 long end;
12843 long stride = 1;
12844 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012845 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012846
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012847 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012848 if (argvars[1].v_type == VAR_UNKNOWN)
12849 {
12850 end = start - 1;
12851 start = 0;
12852 }
12853 else
12854 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012855 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012856 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012857 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012858 }
12859
12860 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012861 if (error)
12862 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000012863 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012864 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000012865 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012866 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012867 else
12868 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012869 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012870 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012871 if (list_append_number(rettv->vval.v_list,
12872 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012873 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012874 }
12875}
12876
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012877/*
12878 * "readfile()" function
12879 */
12880 static void
12881f_readfile(argvars, rettv)
12882 typval_T *argvars;
12883 typval_T *rettv;
12884{
12885 int binary = FALSE;
12886 char_u *fname;
12887 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012888 listitem_T *li;
12889#define FREAD_SIZE 200 /* optimized for text lines */
12890 char_u buf[FREAD_SIZE];
12891 int readlen; /* size of last fread() */
12892 int buflen; /* nr of valid chars in buf[] */
12893 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
12894 int tolist; /* first byte in buf[] still to be put in list */
12895 int chop; /* how many CR to chop off */
12896 char_u *prev = NULL; /* previously read bytes, if any */
12897 int prevlen = 0; /* length of "prev" if not NULL */
12898 char_u *s;
12899 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012900 long maxline = MAXLNUM;
12901 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012902
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012903 if (argvars[1].v_type != VAR_UNKNOWN)
12904 {
12905 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
12906 binary = TRUE;
12907 if (argvars[2].v_type != VAR_UNKNOWN)
12908 maxline = get_tv_number(&argvars[2]);
12909 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012910
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012911 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012912 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012913
12914 /* Always open the file in binary mode, library functions have a mind of
12915 * their own about CR-LF conversion. */
12916 fname = get_tv_string(&argvars[0]);
12917 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
12918 {
12919 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
12920 return;
12921 }
12922
12923 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012924 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012925 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012926 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012927 buflen = filtd + readlen;
12928 tolist = 0;
12929 for ( ; filtd < buflen || readlen <= 0; ++filtd)
12930 {
12931 if (buf[filtd] == '\n' || readlen <= 0)
12932 {
12933 /* Only when in binary mode add an empty list item when the
12934 * last line ends in a '\n'. */
12935 if (!binary && readlen == 0 && filtd == 0)
12936 break;
12937
12938 /* Found end-of-line or end-of-file: add a text line to the
12939 * list. */
12940 chop = 0;
12941 if (!binary)
12942 while (filtd - chop - 1 >= tolist
12943 && buf[filtd - chop - 1] == '\r')
12944 ++chop;
12945 len = filtd - tolist - chop;
12946 if (prev == NULL)
12947 s = vim_strnsave(buf + tolist, len);
12948 else
12949 {
12950 s = alloc((unsigned)(prevlen + len + 1));
12951 if (s != NULL)
12952 {
12953 mch_memmove(s, prev, prevlen);
12954 vim_free(prev);
12955 prev = NULL;
12956 mch_memmove(s + prevlen, buf + tolist, len);
12957 s[prevlen + len] = NUL;
12958 }
12959 }
12960 tolist = filtd + 1;
12961
12962 li = listitem_alloc();
12963 if (li == NULL)
12964 {
12965 vim_free(s);
12966 break;
12967 }
12968 li->li_tv.v_type = VAR_STRING;
12969 li->li_tv.v_lock = 0;
12970 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012971 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012972
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012973 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012974 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012975 if (readlen <= 0)
12976 break;
12977 }
12978 else if (buf[filtd] == NUL)
12979 buf[filtd] = '\n';
12980 }
12981 if (readlen <= 0)
12982 break;
12983
12984 if (tolist == 0)
12985 {
12986 /* "buf" is full, need to move text to an allocated buffer */
12987 if (prev == NULL)
12988 {
12989 prev = vim_strnsave(buf, buflen);
12990 prevlen = buflen;
12991 }
12992 else
12993 {
12994 s = alloc((unsigned)(prevlen + buflen));
12995 if (s != NULL)
12996 {
12997 mch_memmove(s, prev, prevlen);
12998 mch_memmove(s + prevlen, buf, buflen);
12999 vim_free(prev);
13000 prev = s;
13001 prevlen += buflen;
13002 }
13003 }
13004 filtd = 0;
13005 }
13006 else
13007 {
13008 mch_memmove(buf, buf + tolist, buflen - tolist);
13009 filtd -= tolist;
13010 }
13011 }
13012
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013013 /*
13014 * For a negative line count use only the lines at the end of the file,
13015 * free the rest.
13016 */
13017 if (maxline < 0)
13018 while (cnt > -maxline)
13019 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013020 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013021 --cnt;
13022 }
13023
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013024 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013025 fclose(fd);
13026}
13027
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013028#if defined(FEAT_RELTIME)
13029static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
13030
13031/*
13032 * Convert a List to proftime_T.
13033 * Return FAIL when there is something wrong.
13034 */
13035 static int
13036list2proftime(arg, tm)
13037 typval_T *arg;
13038 proftime_T *tm;
13039{
13040 long n1, n2;
13041 int error = FALSE;
13042
13043 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
13044 || arg->vval.v_list->lv_len != 2)
13045 return FAIL;
13046 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
13047 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
13048# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000013049 tm->HighPart = n1;
13050 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013051# else
13052 tm->tv_sec = n1;
13053 tm->tv_usec = n2;
13054# endif
13055 return error ? FAIL : OK;
13056}
13057#endif /* FEAT_RELTIME */
13058
13059/*
13060 * "reltime()" function
13061 */
13062 static void
13063f_reltime(argvars, rettv)
13064 typval_T *argvars;
13065 typval_T *rettv;
13066{
13067#ifdef FEAT_RELTIME
13068 proftime_T res;
13069 proftime_T start;
13070
13071 if (argvars[0].v_type == VAR_UNKNOWN)
13072 {
13073 /* No arguments: get current time. */
13074 profile_start(&res);
13075 }
13076 else if (argvars[1].v_type == VAR_UNKNOWN)
13077 {
13078 if (list2proftime(&argvars[0], &res) == FAIL)
13079 return;
13080 profile_end(&res);
13081 }
13082 else
13083 {
13084 /* Two arguments: compute the difference. */
13085 if (list2proftime(&argvars[0], &start) == FAIL
13086 || list2proftime(&argvars[1], &res) == FAIL)
13087 return;
13088 profile_sub(&res, &start);
13089 }
13090
13091 if (rettv_list_alloc(rettv) == OK)
13092 {
13093 long n1, n2;
13094
13095# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000013096 n1 = res.HighPart;
13097 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013098# else
13099 n1 = res.tv_sec;
13100 n2 = res.tv_usec;
13101# endif
13102 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
13103 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
13104 }
13105#endif
13106}
13107
13108/*
13109 * "reltimestr()" function
13110 */
13111 static void
13112f_reltimestr(argvars, rettv)
13113 typval_T *argvars;
13114 typval_T *rettv;
13115{
13116#ifdef FEAT_RELTIME
13117 proftime_T tm;
13118#endif
13119
13120 rettv->v_type = VAR_STRING;
13121 rettv->vval.v_string = NULL;
13122#ifdef FEAT_RELTIME
13123 if (list2proftime(&argvars[0], &tm) == OK)
13124 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
13125#endif
13126}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013127
Bram Moolenaar0d660222005-01-07 21:51:51 +000013128#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
13129static void make_connection __ARGS((void));
13130static int check_connection __ARGS((void));
13131
13132 static void
13133make_connection()
13134{
13135 if (X_DISPLAY == NULL
13136# ifdef FEAT_GUI
13137 && !gui.in_use
13138# endif
13139 )
13140 {
13141 x_force_connect = TRUE;
13142 setup_term_clip();
13143 x_force_connect = FALSE;
13144 }
13145}
13146
13147 static int
13148check_connection()
13149{
13150 make_connection();
13151 if (X_DISPLAY == NULL)
13152 {
13153 EMSG(_("E240: No connection to Vim server"));
13154 return FAIL;
13155 }
13156 return OK;
13157}
13158#endif
13159
13160#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000013161static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013162
13163 static void
13164remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000013165 typval_T *argvars;
13166 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013167 int expr;
13168{
13169 char_u *server_name;
13170 char_u *keys;
13171 char_u *r = NULL;
13172 char_u buf[NUMBUFLEN];
13173# ifdef WIN32
13174 HWND w;
13175# else
13176 Window w;
13177# endif
13178
13179 if (check_restricted() || check_secure())
13180 return;
13181
13182# ifdef FEAT_X11
13183 if (check_connection() == FAIL)
13184 return;
13185# endif
13186
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013187 server_name = get_tv_string_chk(&argvars[0]);
13188 if (server_name == NULL)
13189 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013190 keys = get_tv_string_buf(&argvars[1], buf);
13191# ifdef WIN32
13192 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
13193# else
13194 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
13195 < 0)
13196# endif
13197 {
13198 if (r != NULL)
13199 EMSG(r); /* sending worked but evaluation failed */
13200 else
13201 EMSG2(_("E241: Unable to send to %s"), server_name);
13202 return;
13203 }
13204
13205 rettv->vval.v_string = r;
13206
13207 if (argvars[2].v_type != VAR_UNKNOWN)
13208 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013209 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000013210 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013211 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013212
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013213 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000013214 v.di_tv.v_type = VAR_STRING;
13215 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013216 idvar = get_tv_string_chk(&argvars[2]);
13217 if (idvar != NULL)
13218 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000013219 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013220 }
13221}
13222#endif
13223
13224/*
13225 * "remote_expr()" function
13226 */
13227/*ARGSUSED*/
13228 static void
13229f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013230 typval_T *argvars;
13231 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013232{
13233 rettv->v_type = VAR_STRING;
13234 rettv->vval.v_string = NULL;
13235#ifdef FEAT_CLIENTSERVER
13236 remote_common(argvars, rettv, TRUE);
13237#endif
13238}
13239
13240/*
13241 * "remote_foreground()" function
13242 */
13243/*ARGSUSED*/
13244 static void
13245f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013246 typval_T *argvars;
13247 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013248{
13249 rettv->vval.v_number = 0;
13250#ifdef FEAT_CLIENTSERVER
13251# ifdef WIN32
13252 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013253 {
13254 char_u *server_name = get_tv_string_chk(&argvars[0]);
13255
13256 if (server_name != NULL)
13257 serverForeground(server_name);
13258 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013259# else
13260 /* Send a foreground() expression to the server. */
13261 argvars[1].v_type = VAR_STRING;
13262 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
13263 argvars[2].v_type = VAR_UNKNOWN;
13264 remote_common(argvars, rettv, TRUE);
13265 vim_free(argvars[1].vval.v_string);
13266# endif
13267#endif
13268}
13269
13270/*ARGSUSED*/
13271 static void
13272f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013273 typval_T *argvars;
13274 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013275{
13276#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000013277 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013278 char_u *s = NULL;
13279# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013280 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013281# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013282 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013283
13284 if (check_restricted() || check_secure())
13285 {
13286 rettv->vval.v_number = -1;
13287 return;
13288 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013289 serverid = get_tv_string_chk(&argvars[0]);
13290 if (serverid == NULL)
13291 {
13292 rettv->vval.v_number = -1;
13293 return; /* type error; errmsg already given */
13294 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013295# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013296 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013297 if (n == 0)
13298 rettv->vval.v_number = -1;
13299 else
13300 {
13301 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
13302 rettv->vval.v_number = (s != NULL);
13303 }
13304# else
13305 rettv->vval.v_number = 0;
13306 if (check_connection() == FAIL)
13307 return;
13308
13309 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013310 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013311# endif
13312
13313 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
13314 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013315 char_u *retvar;
13316
Bram Moolenaar33570922005-01-25 22:26:29 +000013317 v.di_tv.v_type = VAR_STRING;
13318 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013319 retvar = get_tv_string_chk(&argvars[1]);
13320 if (retvar != NULL)
13321 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000013322 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013323 }
13324#else
13325 rettv->vval.v_number = -1;
13326#endif
13327}
13328
13329/*ARGSUSED*/
13330 static void
13331f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013332 typval_T *argvars;
13333 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013334{
13335 char_u *r = NULL;
13336
13337#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013338 char_u *serverid = get_tv_string_chk(&argvars[0]);
13339
13340 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000013341 {
13342# ifdef WIN32
13343 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013344 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013345
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013346 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013347 if (n != 0)
13348 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
13349 if (r == NULL)
13350# else
13351 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013352 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013353# endif
13354 EMSG(_("E277: Unable to read a server reply"));
13355 }
13356#endif
13357 rettv->v_type = VAR_STRING;
13358 rettv->vval.v_string = r;
13359}
13360
13361/*
13362 * "remote_send()" function
13363 */
13364/*ARGSUSED*/
13365 static void
13366f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013367 typval_T *argvars;
13368 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013369{
13370 rettv->v_type = VAR_STRING;
13371 rettv->vval.v_string = NULL;
13372#ifdef FEAT_CLIENTSERVER
13373 remote_common(argvars, rettv, FALSE);
13374#endif
13375}
13376
13377/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013378 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013379 */
13380 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013381f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013382 typval_T *argvars;
13383 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013384{
Bram Moolenaar33570922005-01-25 22:26:29 +000013385 list_T *l;
13386 listitem_T *item, *item2;
13387 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013388 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013389 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013390 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000013391 dict_T *d;
13392 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013393
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013394 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013395 if (argvars[0].v_type == VAR_DICT)
13396 {
13397 if (argvars[2].v_type != VAR_UNKNOWN)
13398 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013399 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000013400 && !tv_check_lock(d->dv_lock, (char_u *)"remove() argument"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013401 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013402 key = get_tv_string_chk(&argvars[1]);
13403 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013404 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013405 di = dict_find(d, key, -1);
13406 if (di == NULL)
13407 EMSG2(_(e_dictkey), key);
13408 else
13409 {
13410 *rettv = di->di_tv;
13411 init_tv(&di->di_tv);
13412 dictitem_remove(d, di);
13413 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013414 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013415 }
13416 }
13417 else if (argvars[0].v_type != VAR_LIST)
13418 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013419 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000013420 && !tv_check_lock(l->lv_lock, (char_u *)"remove() argument"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013421 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013422 int error = FALSE;
13423
13424 idx = get_tv_number_chk(&argvars[1], &error);
13425 if (error)
13426 ; /* type error: do nothing, errmsg already given */
13427 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013428 EMSGN(_(e_listidx), idx);
13429 else
13430 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013431 if (argvars[2].v_type == VAR_UNKNOWN)
13432 {
13433 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013434 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013435 *rettv = item->li_tv;
13436 vim_free(item);
13437 }
13438 else
13439 {
13440 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013441 end = get_tv_number_chk(&argvars[2], &error);
13442 if (error)
13443 ; /* type error: do nothing */
13444 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013445 EMSGN(_(e_listidx), end);
13446 else
13447 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013448 int cnt = 0;
13449
13450 for (li = item; li != NULL; li = li->li_next)
13451 {
13452 ++cnt;
13453 if (li == item2)
13454 break;
13455 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013456 if (li == NULL) /* didn't find "item2" after "item" */
13457 EMSG(_(e_invrange));
13458 else
13459 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013460 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013461 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013462 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013463 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013464 l->lv_first = item;
13465 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013466 item->li_prev = NULL;
13467 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013468 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013469 }
13470 }
13471 }
13472 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013473 }
13474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013475}
13476
13477/*
13478 * "rename({from}, {to})" function
13479 */
13480 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013481f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013482 typval_T *argvars;
13483 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013484{
13485 char_u buf[NUMBUFLEN];
13486
13487 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013488 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013489 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013490 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
13491 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013492}
13493
13494/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013495 * "repeat()" function
13496 */
13497/*ARGSUSED*/
13498 static void
13499f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013500 typval_T *argvars;
13501 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013502{
13503 char_u *p;
13504 int n;
13505 int slen;
13506 int len;
13507 char_u *r;
13508 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013509
13510 n = get_tv_number(&argvars[1]);
13511 if (argvars[0].v_type == VAR_LIST)
13512 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013513 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013514 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013515 if (list_extend(rettv->vval.v_list,
13516 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013517 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013518 }
13519 else
13520 {
13521 p = get_tv_string(&argvars[0]);
13522 rettv->v_type = VAR_STRING;
13523 rettv->vval.v_string = NULL;
13524
13525 slen = (int)STRLEN(p);
13526 len = slen * n;
13527 if (len <= 0)
13528 return;
13529
13530 r = alloc(len + 1);
13531 if (r != NULL)
13532 {
13533 for (i = 0; i < n; i++)
13534 mch_memmove(r + i * slen, p, (size_t)slen);
13535 r[len] = NUL;
13536 }
13537
13538 rettv->vval.v_string = r;
13539 }
13540}
13541
13542/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013543 * "resolve()" function
13544 */
13545 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013546f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013547 typval_T *argvars;
13548 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013549{
13550 char_u *p;
13551
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013552 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013553#ifdef FEAT_SHORTCUT
13554 {
13555 char_u *v = NULL;
13556
13557 v = mch_resolve_shortcut(p);
13558 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013559 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013560 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013561 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013562 }
13563#else
13564# ifdef HAVE_READLINK
13565 {
13566 char_u buf[MAXPATHL + 1];
13567 char_u *cpy;
13568 int len;
13569 char_u *remain = NULL;
13570 char_u *q;
13571 int is_relative_to_current = FALSE;
13572 int has_trailing_pathsep = FALSE;
13573 int limit = 100;
13574
13575 p = vim_strsave(p);
13576
13577 if (p[0] == '.' && (vim_ispathsep(p[1])
13578 || (p[1] == '.' && (vim_ispathsep(p[2])))))
13579 is_relative_to_current = TRUE;
13580
13581 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013582 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013583 has_trailing_pathsep = TRUE;
13584
13585 q = getnextcomp(p);
13586 if (*q != NUL)
13587 {
13588 /* Separate the first path component in "p", and keep the
13589 * remainder (beginning with the path separator). */
13590 remain = vim_strsave(q - 1);
13591 q[-1] = NUL;
13592 }
13593
13594 for (;;)
13595 {
13596 for (;;)
13597 {
13598 len = readlink((char *)p, (char *)buf, MAXPATHL);
13599 if (len <= 0)
13600 break;
13601 buf[len] = NUL;
13602
13603 if (limit-- == 0)
13604 {
13605 vim_free(p);
13606 vim_free(remain);
13607 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013608 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013609 goto fail;
13610 }
13611
13612 /* Ensure that the result will have a trailing path separator
13613 * if the argument has one. */
13614 if (remain == NULL && has_trailing_pathsep)
13615 add_pathsep(buf);
13616
13617 /* Separate the first path component in the link value and
13618 * concatenate the remainders. */
13619 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
13620 if (*q != NUL)
13621 {
13622 if (remain == NULL)
13623 remain = vim_strsave(q - 1);
13624 else
13625 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000013626 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013627 if (cpy != NULL)
13628 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013629 vim_free(remain);
13630 remain = cpy;
13631 }
13632 }
13633 q[-1] = NUL;
13634 }
13635
13636 q = gettail(p);
13637 if (q > p && *q == NUL)
13638 {
13639 /* Ignore trailing path separator. */
13640 q[-1] = NUL;
13641 q = gettail(p);
13642 }
13643 if (q > p && !mch_isFullName(buf))
13644 {
13645 /* symlink is relative to directory of argument */
13646 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
13647 if (cpy != NULL)
13648 {
13649 STRCPY(cpy, p);
13650 STRCPY(gettail(cpy), buf);
13651 vim_free(p);
13652 p = cpy;
13653 }
13654 }
13655 else
13656 {
13657 vim_free(p);
13658 p = vim_strsave(buf);
13659 }
13660 }
13661
13662 if (remain == NULL)
13663 break;
13664
13665 /* Append the first path component of "remain" to "p". */
13666 q = getnextcomp(remain + 1);
13667 len = q - remain - (*q != NUL);
13668 cpy = vim_strnsave(p, STRLEN(p) + len);
13669 if (cpy != NULL)
13670 {
13671 STRNCAT(cpy, remain, len);
13672 vim_free(p);
13673 p = cpy;
13674 }
13675 /* Shorten "remain". */
13676 if (*q != NUL)
13677 STRCPY(remain, q - 1);
13678 else
13679 {
13680 vim_free(remain);
13681 remain = NULL;
13682 }
13683 }
13684
13685 /* If the result is a relative path name, make it explicitly relative to
13686 * the current directory if and only if the argument had this form. */
13687 if (!vim_ispathsep(*p))
13688 {
13689 if (is_relative_to_current
13690 && *p != NUL
13691 && !(p[0] == '.'
13692 && (p[1] == NUL
13693 || vim_ispathsep(p[1])
13694 || (p[1] == '.'
13695 && (p[2] == NUL
13696 || vim_ispathsep(p[2]))))))
13697 {
13698 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013699 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013700 if (cpy != NULL)
13701 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013702 vim_free(p);
13703 p = cpy;
13704 }
13705 }
13706 else if (!is_relative_to_current)
13707 {
13708 /* Strip leading "./". */
13709 q = p;
13710 while (q[0] == '.' && vim_ispathsep(q[1]))
13711 q += 2;
13712 if (q > p)
13713 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
13714 }
13715 }
13716
13717 /* Ensure that the result will have no trailing path separator
13718 * if the argument had none. But keep "/" or "//". */
13719 if (!has_trailing_pathsep)
13720 {
13721 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013722 if (after_pathsep(p, q))
13723 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013724 }
13725
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013726 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013727 }
13728# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013729 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013730# endif
13731#endif
13732
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013733 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013734
13735#ifdef HAVE_READLINK
13736fail:
13737#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013738 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013739}
13740
13741/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013742 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013743 */
13744 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013745f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013746 typval_T *argvars;
13747 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013748{
Bram Moolenaar33570922005-01-25 22:26:29 +000013749 list_T *l;
13750 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013751
Bram Moolenaar0d660222005-01-07 21:51:51 +000013752 rettv->vval.v_number = 0;
13753 if (argvars[0].v_type != VAR_LIST)
13754 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013755 else if ((l = argvars[0].vval.v_list) != NULL
13756 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013757 {
13758 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013759 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013760 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013761 while (li != NULL)
13762 {
13763 ni = li->li_prev;
13764 list_append(l, li);
13765 li = ni;
13766 }
13767 rettv->vval.v_list = l;
13768 rettv->v_type = VAR_LIST;
13769 ++l->lv_refcount;
13770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013771}
13772
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013773#define SP_NOMOVE 0x01 /* don't move cursor */
13774#define SP_REPEAT 0x02 /* repeat to find outer pair */
13775#define SP_RETCOUNT 0x04 /* return matchcount */
13776#define SP_SETPCMARK 0x08 /* set previous context mark */
13777#define SP_START 0x10 /* accept match at start position */
13778#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
13779#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013780
Bram Moolenaar33570922005-01-25 22:26:29 +000013781static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013782
13783/*
13784 * Get flags for a search function.
13785 * Possibly sets "p_ws".
13786 * Returns BACKWARD, FORWARD or zero (for an error).
13787 */
13788 static int
13789get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013790 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013791 int *flagsp;
13792{
13793 int dir = FORWARD;
13794 char_u *flags;
13795 char_u nbuf[NUMBUFLEN];
13796 int mask;
13797
13798 if (varp->v_type != VAR_UNKNOWN)
13799 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013800 flags = get_tv_string_buf_chk(varp, nbuf);
13801 if (flags == NULL)
13802 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013803 while (*flags != NUL)
13804 {
13805 switch (*flags)
13806 {
13807 case 'b': dir = BACKWARD; break;
13808 case 'w': p_ws = TRUE; break;
13809 case 'W': p_ws = FALSE; break;
13810 default: mask = 0;
13811 if (flagsp != NULL)
13812 switch (*flags)
13813 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013814 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013815 case 'e': mask = SP_END; break;
13816 case 'm': mask = SP_RETCOUNT; break;
13817 case 'n': mask = SP_NOMOVE; break;
13818 case 'p': mask = SP_SUBPAT; break;
13819 case 'r': mask = SP_REPEAT; break;
13820 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013821 }
13822 if (mask == 0)
13823 {
13824 EMSG2(_(e_invarg2), flags);
13825 dir = 0;
13826 }
13827 else
13828 *flagsp |= mask;
13829 }
13830 if (dir == 0)
13831 break;
13832 ++flags;
13833 }
13834 }
13835 return dir;
13836}
13837
Bram Moolenaar071d4272004-06-13 20:20:40 +000013838/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013839 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000013840 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013841 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013842search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013843 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013844 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013845 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013846{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013847 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013848 char_u *pat;
13849 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013850 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013851 int save_p_ws = p_ws;
13852 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013853 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013854 long lnum_stop = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013855 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013856 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013857
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013858 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013859 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013860 if (dir == 0)
13861 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013862 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013863 if (flags & SP_START)
13864 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013865 if (flags & SP_END)
13866 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013867
13868 /* Optional extra argument: line number to stop searching. */
13869 if (argvars[1].v_type != VAR_UNKNOWN
13870 && argvars[2].v_type != VAR_UNKNOWN)
13871 {
13872 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
13873 if (lnum_stop < 0)
13874 goto theend;
13875 }
13876
Bram Moolenaar231334e2005-07-25 20:46:57 +000013877 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013878 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000013879 * Check to make sure only those flags are set.
13880 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
13881 * flags cannot be set. Check for that condition also.
13882 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013883 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013884 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013885 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013886 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013887 goto theend;
13888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013889
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013890 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013891 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
13892 options, RE_SEARCH, (linenr_T)lnum_stop);
13893 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013894 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013895 if (flags & SP_SUBPAT)
13896 retval = subpatnum;
13897 else
13898 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013899 if (flags & SP_SETPCMARK)
13900 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013901 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013902 if (match_pos != NULL)
13903 {
13904 /* Store the match cursor position */
13905 match_pos->lnum = pos.lnum;
13906 match_pos->col = pos.col + 1;
13907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013908 /* "/$" will put the cursor after the end of the line, may need to
13909 * correct that here */
13910 check_cursor();
13911 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013912
13913 /* If 'n' flag is used: restore cursor position. */
13914 if (flags & SP_NOMOVE)
13915 curwin->w_cursor = save_cursor;
13916theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000013917 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013918
13919 return retval;
13920}
13921
13922/*
13923 * "search()" function
13924 */
13925 static void
13926f_search(argvars, rettv)
13927 typval_T *argvars;
13928 typval_T *rettv;
13929{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013930 int flags = 0;
13931
13932 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013933}
13934
Bram Moolenaar071d4272004-06-13 20:20:40 +000013935/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013936 * "searchdecl()" function
13937 */
13938 static void
13939f_searchdecl(argvars, rettv)
13940 typval_T *argvars;
13941 typval_T *rettv;
13942{
13943 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013944 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013945 int error = FALSE;
13946 char_u *name;
13947
13948 rettv->vval.v_number = 1; /* default: FAIL */
13949
13950 name = get_tv_string_chk(&argvars[0]);
13951 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000013952 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013953 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013954 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13955 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
13956 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013957 if (!error && name != NULL)
13958 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000013959 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013960}
13961
13962/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013963 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000013964 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013965 static int
13966searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000013967 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013968 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013969{
13970 char_u *spat, *mpat, *epat;
13971 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013972 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013973 int dir;
13974 int flags = 0;
13975 char_u nbuf1[NUMBUFLEN];
13976 char_u nbuf2[NUMBUFLEN];
13977 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013978 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013979 long lnum_stop = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013980
Bram Moolenaar071d4272004-06-13 20:20:40 +000013981 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013982 spat = get_tv_string_chk(&argvars[0]);
13983 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
13984 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
13985 if (spat == NULL || mpat == NULL || epat == NULL)
13986 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013987
Bram Moolenaar071d4272004-06-13 20:20:40 +000013988 /* Handle the optional fourth argument: flags */
13989 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013990 if (dir == 0)
13991 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013992
13993 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000013994 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
13995 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013996 if ((flags & (SP_END | SP_SUBPAT)) != 0
13997 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000013998 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013999 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000014000 goto theend;
14001 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014002
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014003 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014004 if (argvars[3].v_type == VAR_UNKNOWN
14005 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014006 skip = (char_u *)"";
14007 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014008 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014009 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014010 if (argvars[5].v_type != VAR_UNKNOWN)
14011 {
14012 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
14013 if (lnum_stop < 0)
14014 goto theend;
14015 }
14016 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014017 if (skip == NULL)
14018 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014019
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014020 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
14021 match_pos, lnum_stop);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014022
14023theend:
14024 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014025
14026 return retval;
14027}
14028
14029/*
14030 * "searchpair()" function
14031 */
14032 static void
14033f_searchpair(argvars, rettv)
14034 typval_T *argvars;
14035 typval_T *rettv;
14036{
14037 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
14038}
14039
14040/*
14041 * "searchpairpos()" function
14042 */
14043 static void
14044f_searchpairpos(argvars, rettv)
14045 typval_T *argvars;
14046 typval_T *rettv;
14047{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014048 pos_T match_pos;
14049 int lnum = 0;
14050 int col = 0;
14051
14052 rettv->vval.v_number = 0;
14053
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014054 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014055 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014056
14057 if (searchpair_cmn(argvars, &match_pos) > 0)
14058 {
14059 lnum = match_pos.lnum;
14060 col = match_pos.col;
14061 }
14062
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014063 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
14064 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014065}
14066
14067/*
14068 * Search for a start/middle/end thing.
14069 * Used by searchpair(), see its documentation for the details.
14070 * Returns 0 or -1 for no match,
14071 */
14072 long
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014073do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos, lnum_stop)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014074 char_u *spat; /* start pattern */
14075 char_u *mpat; /* middle pattern */
14076 char_u *epat; /* end pattern */
14077 int dir; /* BACKWARD or FORWARD */
14078 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014079 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014080 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014081 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014082{
14083 char_u *save_cpo;
14084 char_u *pat, *pat2 = NULL, *pat3 = NULL;
14085 long retval = 0;
14086 pos_T pos;
14087 pos_T firstpos;
14088 pos_T foundpos;
14089 pos_T save_cursor;
14090 pos_T save_pos;
14091 int n;
14092 int r;
14093 int nest = 1;
14094 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014095 int options = SEARCH_KEEP;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014096
14097 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14098 save_cpo = p_cpo;
14099 p_cpo = (char_u *)"";
14100
14101 /* Make two search patterns: start/end (pat2, for in nested pairs) and
14102 * start/middle/end (pat3, for the top pair). */
14103 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
14104 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
14105 if (pat2 == NULL || pat3 == NULL)
14106 goto theend;
14107 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
14108 if (*mpat == NUL)
14109 STRCPY(pat3, pat2);
14110 else
14111 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
14112 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014113 if (flags & SP_START)
14114 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014115
Bram Moolenaar071d4272004-06-13 20:20:40 +000014116 save_cursor = curwin->w_cursor;
14117 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000014118 clearpos(&firstpos);
14119 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014120 pat = pat3;
14121 for (;;)
14122 {
14123 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014124 options, RE_SEARCH, lnum_stop);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014125 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
14126 /* didn't find it or found the first match again: FAIL */
14127 break;
14128
14129 if (firstpos.lnum == 0)
14130 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000014131 if (equalpos(pos, foundpos))
14132 {
14133 /* Found the same position again. Can happen with a pattern that
14134 * has "\zs" at the end and searching backwards. Advance one
14135 * character and try again. */
14136 if (dir == BACKWARD)
14137 decl(&pos);
14138 else
14139 incl(&pos);
14140 }
14141 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014142
14143 /* If the skip pattern matches, ignore this match. */
14144 if (*skip != NUL)
14145 {
14146 save_pos = curwin->w_cursor;
14147 curwin->w_cursor = pos;
14148 r = eval_to_bool(skip, &err, NULL, FALSE);
14149 curwin->w_cursor = save_pos;
14150 if (err)
14151 {
14152 /* Evaluating {skip} caused an error, break here. */
14153 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014154 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014155 break;
14156 }
14157 if (r)
14158 continue;
14159 }
14160
14161 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
14162 {
14163 /* Found end when searching backwards or start when searching
14164 * forward: nested pair. */
14165 ++nest;
14166 pat = pat2; /* nested, don't search for middle */
14167 }
14168 else
14169 {
14170 /* Found end when searching forward or start when searching
14171 * backward: end of (nested) pair; or found middle in outer pair. */
14172 if (--nest == 1)
14173 pat = pat3; /* outer level, search for middle */
14174 }
14175
14176 if (nest == 0)
14177 {
14178 /* Found the match: return matchcount or line number. */
14179 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014180 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014181 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014182 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000014183 if (flags & SP_SETPCMARK)
14184 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014185 curwin->w_cursor = pos;
14186 if (!(flags & SP_REPEAT))
14187 break;
14188 nest = 1; /* search for next unmatched */
14189 }
14190 }
14191
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014192 if (match_pos != NULL)
14193 {
14194 /* Store the match cursor position */
14195 match_pos->lnum = curwin->w_cursor.lnum;
14196 match_pos->col = curwin->w_cursor.col + 1;
14197 }
14198
Bram Moolenaar071d4272004-06-13 20:20:40 +000014199 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014200 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014201 curwin->w_cursor = save_cursor;
14202
14203theend:
14204 vim_free(pat2);
14205 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014206 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014207
14208 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014209}
14210
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014211/*
14212 * "searchpos()" function
14213 */
14214 static void
14215f_searchpos(argvars, rettv)
14216 typval_T *argvars;
14217 typval_T *rettv;
14218{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014219 pos_T match_pos;
14220 int lnum = 0;
14221 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014222 int n;
14223 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014224
14225 rettv->vval.v_number = 0;
14226
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014227 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014228 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014229
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014230 n = search_cmn(argvars, &match_pos, &flags);
14231 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014232 {
14233 lnum = match_pos.lnum;
14234 col = match_pos.col;
14235 }
14236
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014237 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
14238 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014239 if (flags & SP_SUBPAT)
14240 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014241}
14242
14243
Bram Moolenaar0d660222005-01-07 21:51:51 +000014244/*ARGSUSED*/
14245 static void
14246f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014247 typval_T *argvars;
14248 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014249{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014250#ifdef FEAT_CLIENTSERVER
14251 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014252 char_u *server = get_tv_string_chk(&argvars[0]);
14253 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014254
Bram Moolenaar0d660222005-01-07 21:51:51 +000014255 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014256 if (server == NULL || reply == NULL)
14257 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014258 if (check_restricted() || check_secure())
14259 return;
14260# ifdef FEAT_X11
14261 if (check_connection() == FAIL)
14262 return;
14263# endif
14264
14265 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014266 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014267 EMSG(_("E258: Unable to send to client"));
14268 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014269 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014270 rettv->vval.v_number = 0;
14271#else
14272 rettv->vval.v_number = -1;
14273#endif
14274}
14275
14276/*ARGSUSED*/
14277 static void
14278f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014279 typval_T *argvars;
14280 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014281{
14282 char_u *r = NULL;
14283
14284#ifdef FEAT_CLIENTSERVER
14285# ifdef WIN32
14286 r = serverGetVimNames();
14287# else
14288 make_connection();
14289 if (X_DISPLAY != NULL)
14290 r = serverGetVimNames(X_DISPLAY);
14291# endif
14292#endif
14293 rettv->v_type = VAR_STRING;
14294 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014295}
14296
14297/*
14298 * "setbufvar()" function
14299 */
14300/*ARGSUSED*/
14301 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014302f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014303 typval_T *argvars;
14304 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014305{
14306 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014307 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014308 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014309 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014310 char_u nbuf[NUMBUFLEN];
14311
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014312 rettv->vval.v_number = 0;
14313
Bram Moolenaar071d4272004-06-13 20:20:40 +000014314 if (check_restricted() || check_secure())
14315 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014316 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
14317 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014318 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014319 varp = &argvars[2];
14320
14321 if (buf != NULL && varname != NULL && varp != NULL)
14322 {
14323 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014324 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014325
14326 if (*varname == '&')
14327 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014328 long numval;
14329 char_u *strval;
14330 int error = FALSE;
14331
Bram Moolenaar071d4272004-06-13 20:20:40 +000014332 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014333 numval = get_tv_number_chk(varp, &error);
14334 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014335 if (!error && strval != NULL)
14336 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014337 }
14338 else
14339 {
14340 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
14341 if (bufvarname != NULL)
14342 {
14343 STRCPY(bufvarname, "b:");
14344 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014345 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014346 vim_free(bufvarname);
14347 }
14348 }
14349
14350 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014351 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014353}
14354
14355/*
14356 * "setcmdpos()" function
14357 */
14358 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014359f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014360 typval_T *argvars;
14361 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014362{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014363 int pos = (int)get_tv_number(&argvars[0]) - 1;
14364
14365 if (pos >= 0)
14366 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014367}
14368
14369/*
14370 * "setline()" function
14371 */
14372 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014373f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014374 typval_T *argvars;
14375 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014376{
14377 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000014378 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014379 list_T *l = NULL;
14380 listitem_T *li = NULL;
14381 long added = 0;
14382 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014383
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014384 lnum = get_tv_lnum(&argvars[0]);
14385 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014386 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014387 l = argvars[1].vval.v_list;
14388 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014389 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014390 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014391 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014392
14393 rettv->vval.v_number = 0; /* OK */
14394 for (;;)
14395 {
14396 if (l != NULL)
14397 {
14398 /* list argument, get next string */
14399 if (li == NULL)
14400 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014401 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014402 li = li->li_next;
14403 }
14404
14405 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014406 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014407 break;
14408 if (lnum <= curbuf->b_ml.ml_line_count)
14409 {
14410 /* existing line, replace it */
14411 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
14412 {
14413 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000014414 if (lnum == curwin->w_cursor.lnum)
14415 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014416 rettv->vval.v_number = 0; /* OK */
14417 }
14418 }
14419 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
14420 {
14421 /* lnum is one past the last line, append the line */
14422 ++added;
14423 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
14424 rettv->vval.v_number = 0; /* OK */
14425 }
14426
14427 if (l == NULL) /* only one string argument */
14428 break;
14429 ++lnum;
14430 }
14431
14432 if (added > 0)
14433 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014434}
14435
14436/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014437 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000014438 */
14439/*ARGSUSED*/
14440 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014441set_qf_ll_list(wp, list_arg, action_arg, rettv)
14442 win_T *wp;
14443 typval_T *list_arg;
14444 typval_T *action_arg;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014445 typval_T *rettv;
14446{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014447#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014448 char_u *act;
14449 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014450#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014451
Bram Moolenaar2641f772005-03-25 21:58:17 +000014452 rettv->vval.v_number = -1;
14453
14454#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014455 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014456 EMSG(_(e_listreq));
14457 else
14458 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014459 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014460
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014461 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014462 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014463 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014464 if (act == NULL)
14465 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014466 if (*act == 'a' || *act == 'r')
14467 action = *act;
14468 }
14469
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014470 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014471 rettv->vval.v_number = 0;
14472 }
14473#endif
14474}
14475
14476/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014477 * "setloclist()" function
14478 */
14479/*ARGSUSED*/
14480 static void
14481f_setloclist(argvars, rettv)
14482 typval_T *argvars;
14483 typval_T *rettv;
14484{
14485 win_T *win;
14486
14487 rettv->vval.v_number = -1;
14488
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014489 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014490 if (win != NULL)
14491 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
14492}
14493
14494/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014495 * "setpos()" function
14496 */
14497/*ARGSUSED*/
14498 static void
14499f_setpos(argvars, rettv)
14500 typval_T *argvars;
14501 typval_T *rettv;
14502{
14503 pos_T pos;
14504 int fnum;
14505 char_u *name;
14506
14507 name = get_tv_string_chk(argvars);
14508 if (name != NULL)
14509 {
14510 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
14511 {
14512 --pos.col;
14513 if (name[0] == '.') /* cursor */
14514 {
14515 if (fnum == curbuf->b_fnum)
14516 {
14517 curwin->w_cursor = pos;
14518 check_cursor();
14519 }
14520 else
14521 EMSG(_(e_invarg));
14522 }
14523 else if (name[0] == '\'') /* mark */
14524 (void)setmark_pos(name[1], &pos, fnum);
14525 else
14526 EMSG(_(e_invarg));
14527 }
14528 }
14529}
14530
14531/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014532 * "setqflist()" function
14533 */
14534/*ARGSUSED*/
14535 static void
14536f_setqflist(argvars, rettv)
14537 typval_T *argvars;
14538 typval_T *rettv;
14539{
14540 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
14541}
14542
14543/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014544 * "setreg()" function
14545 */
14546 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014547f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014548 typval_T *argvars;
14549 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014550{
14551 int regname;
14552 char_u *strregname;
14553 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014554 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014555 int append;
14556 char_u yank_type;
14557 long block_len;
14558
14559 block_len = -1;
14560 yank_type = MAUTO;
14561 append = FALSE;
14562
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014563 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014564 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014565
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014566 if (strregname == NULL)
14567 return; /* type error; errmsg already given */
14568 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014569 if (regname == 0 || regname == '@')
14570 regname = '"';
14571 else if (regname == '=')
14572 return;
14573
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014574 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014575 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014576 stropt = get_tv_string_chk(&argvars[2]);
14577 if (stropt == NULL)
14578 return; /* type error */
14579 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014580 switch (*stropt)
14581 {
14582 case 'a': case 'A': /* append */
14583 append = TRUE;
14584 break;
14585 case 'v': case 'c': /* character-wise selection */
14586 yank_type = MCHAR;
14587 break;
14588 case 'V': case 'l': /* line-wise selection */
14589 yank_type = MLINE;
14590 break;
14591#ifdef FEAT_VISUAL
14592 case 'b': case Ctrl_V: /* block-wise selection */
14593 yank_type = MBLOCK;
14594 if (VIM_ISDIGIT(stropt[1]))
14595 {
14596 ++stropt;
14597 block_len = getdigits(&stropt) - 1;
14598 --stropt;
14599 }
14600 break;
14601#endif
14602 }
14603 }
14604
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014605 strval = get_tv_string_chk(&argvars[1]);
14606 if (strval != NULL)
14607 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000014608 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014609 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014610}
14611
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014612/*
14613 * "settabwinvar()" function
14614 */
14615 static void
14616f_settabwinvar(argvars, rettv)
14617 typval_T *argvars;
14618 typval_T *rettv;
14619{
14620 setwinvar(argvars, rettv, 1);
14621}
Bram Moolenaar071d4272004-06-13 20:20:40 +000014622
14623/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014624 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014625 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014626 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014627f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014628 typval_T *argvars;
14629 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014630{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014631 setwinvar(argvars, rettv, 0);
14632}
14633
14634/*
14635 * "setwinvar()" and "settabwinvar()" functions
14636 */
14637 static void
14638setwinvar(argvars, rettv, off)
14639 typval_T *argvars;
14640 typval_T *rettv;
14641 int off;
14642{
Bram Moolenaar071d4272004-06-13 20:20:40 +000014643 win_T *win;
14644#ifdef FEAT_WINDOWS
14645 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014646 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014647#endif
14648 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014649 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014650 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014651 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014652
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014653 rettv->vval.v_number = 0;
14654
Bram Moolenaar071d4272004-06-13 20:20:40 +000014655 if (check_restricted() || check_secure())
14656 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014657
14658#ifdef FEAT_WINDOWS
14659 if (off == 1)
14660 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
14661 else
14662 tp = curtab;
14663#endif
14664 win = find_win_by_nr(&argvars[off], tp);
14665 varname = get_tv_string_chk(&argvars[off + 1]);
14666 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014667
14668 if (win != NULL && varname != NULL && varp != NULL)
14669 {
14670#ifdef FEAT_WINDOWS
14671 /* set curwin to be our win, temporarily */
14672 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014673 save_curtab = curtab;
14674 goto_tabpage_tp(tp);
14675 if (!win_valid(win))
14676 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014677 curwin = win;
14678 curbuf = curwin->w_buffer;
14679#endif
14680
14681 if (*varname == '&')
14682 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014683 long numval;
14684 char_u *strval;
14685 int error = FALSE;
14686
Bram Moolenaar071d4272004-06-13 20:20:40 +000014687 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014688 numval = get_tv_number_chk(varp, &error);
14689 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014690 if (!error && strval != NULL)
14691 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014692 }
14693 else
14694 {
14695 winvarname = alloc((unsigned)STRLEN(varname) + 3);
14696 if (winvarname != NULL)
14697 {
14698 STRCPY(winvarname, "w:");
14699 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014700 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014701 vim_free(winvarname);
14702 }
14703 }
14704
14705#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014706 /* Restore current tabpage and window, if still valid (autocomands can
14707 * make them invalid). */
14708 if (valid_tabpage(save_curtab))
14709 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014710 if (win_valid(save_curwin))
14711 {
14712 curwin = save_curwin;
14713 curbuf = curwin->w_buffer;
14714 }
14715#endif
14716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014717}
14718
14719/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000014720 * "shellescape({string})" function
14721 */
14722 static void
14723f_shellescape(argvars, rettv)
14724 typval_T *argvars;
14725 typval_T *rettv;
14726{
14727 rettv->vval.v_string = vim_strsave_shellescape(get_tv_string(&argvars[0]));
14728 rettv->v_type = VAR_STRING;
14729}
14730
14731/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014732 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014733 */
14734 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014735f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014736 typval_T *argvars;
14737 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014738{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014739 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014740
Bram Moolenaar0d660222005-01-07 21:51:51 +000014741 p = get_tv_string(&argvars[0]);
14742 rettv->vval.v_string = vim_strsave(p);
14743 simplify_filename(rettv->vval.v_string); /* simplify in place */
14744 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014745}
14746
Bram Moolenaar0d660222005-01-07 21:51:51 +000014747static int
14748#ifdef __BORLANDC__
14749 _RTLENTRYF
14750#endif
14751 item_compare __ARGS((const void *s1, const void *s2));
14752static int
14753#ifdef __BORLANDC__
14754 _RTLENTRYF
14755#endif
14756 item_compare2 __ARGS((const void *s1, const void *s2));
14757
14758static int item_compare_ic;
14759static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014760static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014761#define ITEM_COMPARE_FAIL 999
14762
Bram Moolenaar071d4272004-06-13 20:20:40 +000014763/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014764 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014765 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014766 static int
14767#ifdef __BORLANDC__
14768_RTLENTRYF
14769#endif
14770item_compare(s1, s2)
14771 const void *s1;
14772 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014773{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014774 char_u *p1, *p2;
14775 char_u *tofree1, *tofree2;
14776 int res;
14777 char_u numbuf1[NUMBUFLEN];
14778 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014779
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014780 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
14781 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014782 if (item_compare_ic)
14783 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014784 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014785 res = STRCMP(p1, p2);
14786 vim_free(tofree1);
14787 vim_free(tofree2);
14788 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014789}
14790
14791 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000014792#ifdef __BORLANDC__
14793_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014794#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000014795item_compare2(s1, s2)
14796 const void *s1;
14797 const void *s2;
14798{
14799 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000014800 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014801 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000014802 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014803
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014804 /* shortcut after failure in previous call; compare all items equal */
14805 if (item_compare_func_err)
14806 return 0;
14807
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014808 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
14809 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014810 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
14811 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014812
14813 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014814 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000014815 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014816 clear_tv(&argv[0]);
14817 clear_tv(&argv[1]);
14818
14819 if (res == FAIL)
14820 res = ITEM_COMPARE_FAIL;
14821 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014822 /* return value has wrong type */
14823 res = get_tv_number_chk(&rettv, &item_compare_func_err);
14824 if (item_compare_func_err)
14825 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014826 clear_tv(&rettv);
14827 return res;
14828}
14829
14830/*
14831 * "sort({list})" function
14832 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014833 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014834f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014835 typval_T *argvars;
14836 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014837{
Bram Moolenaar33570922005-01-25 22:26:29 +000014838 list_T *l;
14839 listitem_T *li;
14840 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014841 long len;
14842 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014843
Bram Moolenaar0d660222005-01-07 21:51:51 +000014844 rettv->vval.v_number = 0;
14845 if (argvars[0].v_type != VAR_LIST)
14846 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000014847 else
14848 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014849 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014850 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014851 return;
14852 rettv->vval.v_list = l;
14853 rettv->v_type = VAR_LIST;
14854 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014855
Bram Moolenaar0d660222005-01-07 21:51:51 +000014856 len = list_len(l);
14857 if (len <= 1)
14858 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014859
Bram Moolenaar0d660222005-01-07 21:51:51 +000014860 item_compare_ic = FALSE;
14861 item_compare_func = NULL;
14862 if (argvars[1].v_type != VAR_UNKNOWN)
14863 {
14864 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014865 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014866 else
14867 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014868 int error = FALSE;
14869
14870 i = get_tv_number_chk(&argvars[1], &error);
14871 if (error)
14872 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014873 if (i == 1)
14874 item_compare_ic = TRUE;
14875 else
14876 item_compare_func = get_tv_string(&argvars[1]);
14877 }
14878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014879
Bram Moolenaar0d660222005-01-07 21:51:51 +000014880 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014881 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014882 if (ptrs == NULL)
14883 return;
14884 i = 0;
14885 for (li = l->lv_first; li != NULL; li = li->li_next)
14886 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014887
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014888 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014889 /* test the compare function */
14890 if (item_compare_func != NULL
14891 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
14892 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014893 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014894 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014895 {
14896 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014897 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000014898 item_compare_func == NULL ? item_compare : item_compare2);
14899
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014900 if (!item_compare_func_err)
14901 {
14902 /* Clear the List and append the items in the sorted order. */
14903 l->lv_first = l->lv_last = NULL;
14904 l->lv_len = 0;
14905 for (i = 0; i < len; ++i)
14906 list_append(l, ptrs[i]);
14907 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014908 }
14909
14910 vim_free(ptrs);
14911 }
14912}
14913
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014914/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000014915 * "soundfold({word})" function
14916 */
14917 static void
14918f_soundfold(argvars, rettv)
14919 typval_T *argvars;
14920 typval_T *rettv;
14921{
14922 char_u *s;
14923
14924 rettv->v_type = VAR_STRING;
14925 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014926#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000014927 rettv->vval.v_string = eval_soundfold(s);
14928#else
14929 rettv->vval.v_string = vim_strsave(s);
14930#endif
14931}
14932
14933/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014934 * "spellbadword()" function
14935 */
14936/* ARGSUSED */
14937 static void
14938f_spellbadword(argvars, rettv)
14939 typval_T *argvars;
14940 typval_T *rettv;
14941{
Bram Moolenaar4463f292005-09-25 22:20:24 +000014942 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014943 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014944 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014945
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014946 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014947 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014948
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014949#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000014950 if (argvars[0].v_type == VAR_UNKNOWN)
14951 {
14952 /* Find the start and length of the badly spelled word. */
14953 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
14954 if (len != 0)
14955 word = ml_get_cursor();
14956 }
14957 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14958 {
14959 char_u *str = get_tv_string_chk(&argvars[0]);
14960 int capcol = -1;
14961
14962 if (str != NULL)
14963 {
14964 /* Check the argument for spelling. */
14965 while (*str != NUL)
14966 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014967 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014968 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014969 {
14970 word = str;
14971 break;
14972 }
14973 str += len;
14974 }
14975 }
14976 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014977#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000014978
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014979 list_append_string(rettv->vval.v_list, word, len);
14980 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014981 attr == HLF_SPB ? "bad" :
14982 attr == HLF_SPR ? "rare" :
14983 attr == HLF_SPL ? "local" :
14984 attr == HLF_SPC ? "caps" :
14985 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014986}
14987
14988/*
14989 * "spellsuggest()" function
14990 */
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014991/*ARGSUSED*/
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014992 static void
14993f_spellsuggest(argvars, rettv)
14994 typval_T *argvars;
14995 typval_T *rettv;
14996{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014997#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014998 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014999 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015000 int maxcount;
15001 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015002 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015003 listitem_T *li;
15004 int need_capital = FALSE;
15005#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015006
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015007 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015008 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015009
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015010#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015011 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
15012 {
15013 str = get_tv_string(&argvars[0]);
15014 if (argvars[1].v_type != VAR_UNKNOWN)
15015 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015016 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015017 if (maxcount <= 0)
15018 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015019 if (argvars[2].v_type != VAR_UNKNOWN)
15020 {
15021 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
15022 if (typeerr)
15023 return;
15024 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015025 }
15026 else
15027 maxcount = 25;
15028
Bram Moolenaar4770d092006-01-12 23:22:24 +000015029 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015030
15031 for (i = 0; i < ga.ga_len; ++i)
15032 {
15033 str = ((char_u **)ga.ga_data)[i];
15034
15035 li = listitem_alloc();
15036 if (li == NULL)
15037 vim_free(str);
15038 else
15039 {
15040 li->li_tv.v_type = VAR_STRING;
15041 li->li_tv.v_lock = 0;
15042 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015043 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015044 }
15045 }
15046 ga_clear(&ga);
15047 }
15048#endif
15049}
15050
Bram Moolenaar0d660222005-01-07 21:51:51 +000015051 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015052f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015053 typval_T *argvars;
15054 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015055{
15056 char_u *str;
15057 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015058 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015059 regmatch_T regmatch;
15060 char_u patbuf[NUMBUFLEN];
15061 char_u *save_cpo;
15062 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015063 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015064 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015065 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015066
15067 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15068 save_cpo = p_cpo;
15069 p_cpo = (char_u *)"";
15070
15071 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015072 if (argvars[1].v_type != VAR_UNKNOWN)
15073 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015074 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15075 if (pat == NULL)
15076 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015077 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015078 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015079 }
15080 if (pat == NULL || *pat == NUL)
15081 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000015082
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015083 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015084 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015085 if (typeerr)
15086 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015087
Bram Moolenaar0d660222005-01-07 21:51:51 +000015088 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15089 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015090 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015091 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015092 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015093 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015094 if (*str == NUL)
15095 match = FALSE; /* empty item at the end */
15096 else
15097 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015098 if (match)
15099 end = regmatch.startp[0];
15100 else
15101 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015102 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
15103 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015104 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015105 if (list_append_string(rettv->vval.v_list, str,
15106 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015107 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015108 }
15109 if (!match)
15110 break;
15111 /* Advance to just after the match. */
15112 if (regmatch.endp[0] > str)
15113 col = 0;
15114 else
15115 {
15116 /* Don't get stuck at the same match. */
15117#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015118 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015119#else
15120 col = 1;
15121#endif
15122 }
15123 str = regmatch.endp[0];
15124 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015125
Bram Moolenaar0d660222005-01-07 21:51:51 +000015126 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015127 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015128
Bram Moolenaar0d660222005-01-07 21:51:51 +000015129 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015130}
15131
Bram Moolenaar2c932302006-03-18 21:42:09 +000015132/*
15133 * "str2nr()" function
15134 */
15135 static void
15136f_str2nr(argvars, rettv)
15137 typval_T *argvars;
15138 typval_T *rettv;
15139{
15140 int base = 10;
15141 char_u *p;
15142 long n;
15143
15144 if (argvars[1].v_type != VAR_UNKNOWN)
15145 {
15146 base = get_tv_number(&argvars[1]);
15147 if (base != 8 && base != 10 && base != 16)
15148 {
15149 EMSG(_(e_invarg));
15150 return;
15151 }
15152 }
15153
15154 p = skipwhite(get_tv_string(&argvars[0]));
15155 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
15156 rettv->vval.v_number = n;
15157}
15158
Bram Moolenaar071d4272004-06-13 20:20:40 +000015159#ifdef HAVE_STRFTIME
15160/*
15161 * "strftime({format}[, {time}])" function
15162 */
15163 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015164f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015165 typval_T *argvars;
15166 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015167{
15168 char_u result_buf[256];
15169 struct tm *curtime;
15170 time_t seconds;
15171 char_u *p;
15172
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015173 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015174
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015175 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015176 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015177 seconds = time(NULL);
15178 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015179 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015180 curtime = localtime(&seconds);
15181 /* MSVC returns NULL for an invalid value of seconds. */
15182 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015183 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015184 else
15185 {
15186# ifdef FEAT_MBYTE
15187 vimconv_T conv;
15188 char_u *enc;
15189
15190 conv.vc_type = CONV_NONE;
15191 enc = enc_locale();
15192 convert_setup(&conv, p_enc, enc);
15193 if (conv.vc_type != CONV_NONE)
15194 p = string_convert(&conv, p, NULL);
15195# endif
15196 if (p != NULL)
15197 (void)strftime((char *)result_buf, sizeof(result_buf),
15198 (char *)p, curtime);
15199 else
15200 result_buf[0] = NUL;
15201
15202# ifdef FEAT_MBYTE
15203 if (conv.vc_type != CONV_NONE)
15204 vim_free(p);
15205 convert_setup(&conv, enc, p_enc);
15206 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015207 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015208 else
15209# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015210 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015211
15212# ifdef FEAT_MBYTE
15213 /* Release conversion descriptors */
15214 convert_setup(&conv, NULL, NULL);
15215 vim_free(enc);
15216# endif
15217 }
15218}
15219#endif
15220
15221/*
15222 * "stridx()" function
15223 */
15224 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015225f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015226 typval_T *argvars;
15227 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015228{
15229 char_u buf[NUMBUFLEN];
15230 char_u *needle;
15231 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000015232 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015233 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000015234 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015235
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015236 needle = get_tv_string_chk(&argvars[1]);
15237 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000015238 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015239 if (needle == NULL || haystack == NULL)
15240 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015241
Bram Moolenaar33570922005-01-25 22:26:29 +000015242 if (argvars[2].v_type != VAR_UNKNOWN)
15243 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015244 int error = FALSE;
15245
15246 start_idx = get_tv_number_chk(&argvars[2], &error);
15247 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000015248 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015249 if (start_idx >= 0)
15250 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000015251 }
15252
15253 pos = (char_u *)strstr((char *)haystack, (char *)needle);
15254 if (pos != NULL)
15255 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015256}
15257
15258/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015259 * "string()" function
15260 */
15261 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015262f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015263 typval_T *argvars;
15264 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015265{
15266 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015267 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015268
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015269 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015270 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015271 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015272 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015273}
15274
15275/*
15276 * "strlen()" function
15277 */
15278 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015279f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015280 typval_T *argvars;
15281 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015282{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015283 rettv->vval.v_number = (varnumber_T)(STRLEN(
15284 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015285}
15286
15287/*
15288 * "strpart()" function
15289 */
15290 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015291f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015292 typval_T *argvars;
15293 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015294{
15295 char_u *p;
15296 int n;
15297 int len;
15298 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015299 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015300
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015301 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015302 slen = (int)STRLEN(p);
15303
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015304 n = get_tv_number_chk(&argvars[1], &error);
15305 if (error)
15306 len = 0;
15307 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015308 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015309 else
15310 len = slen - n; /* default len: all bytes that are available. */
15311
15312 /*
15313 * Only return the overlap between the specified part and the actual
15314 * string.
15315 */
15316 if (n < 0)
15317 {
15318 len += n;
15319 n = 0;
15320 }
15321 else if (n > slen)
15322 n = slen;
15323 if (len < 0)
15324 len = 0;
15325 else if (n + len > slen)
15326 len = slen - n;
15327
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015328 rettv->v_type = VAR_STRING;
15329 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015330}
15331
15332/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015333 * "strridx()" function
15334 */
15335 static void
15336f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015337 typval_T *argvars;
15338 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015339{
15340 char_u buf[NUMBUFLEN];
15341 char_u *needle;
15342 char_u *haystack;
15343 char_u *rest;
15344 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015345 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015346
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015347 needle = get_tv_string_chk(&argvars[1]);
15348 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015349
15350 rettv->vval.v_number = -1;
15351 if (needle == NULL || haystack == NULL)
15352 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015353
15354 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000015355 if (argvars[2].v_type != VAR_UNKNOWN)
15356 {
15357 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015358 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000015359 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015360 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000015361 }
15362 else
15363 end_idx = haystack_len;
15364
Bram Moolenaar0d660222005-01-07 21:51:51 +000015365 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000015366 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015367 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000015368 lastmatch = haystack + end_idx;
15369 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015370 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000015371 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015372 for (rest = haystack; *rest != '\0'; ++rest)
15373 {
15374 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000015375 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015376 break;
15377 lastmatch = rest;
15378 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000015379 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015380
15381 if (lastmatch == NULL)
15382 rettv->vval.v_number = -1;
15383 else
15384 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
15385}
15386
15387/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015388 * "strtrans()" function
15389 */
15390 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015391f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015392 typval_T *argvars;
15393 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015394{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015395 rettv->v_type = VAR_STRING;
15396 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015397}
15398
15399/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015400 * "submatch()" function
15401 */
15402 static void
15403f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015404 typval_T *argvars;
15405 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015406{
15407 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015408 rettv->vval.v_string =
15409 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015410}
15411
15412/*
15413 * "substitute()" function
15414 */
15415 static void
15416f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015417 typval_T *argvars;
15418 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015419{
15420 char_u patbuf[NUMBUFLEN];
15421 char_u subbuf[NUMBUFLEN];
15422 char_u flagsbuf[NUMBUFLEN];
15423
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015424 char_u *str = get_tv_string_chk(&argvars[0]);
15425 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15426 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
15427 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
15428
Bram Moolenaar0d660222005-01-07 21:51:51 +000015429 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015430 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
15431 rettv->vval.v_string = NULL;
15432 else
15433 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015434}
15435
15436/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015437 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015438 */
15439/*ARGSUSED*/
15440 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015441f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015442 typval_T *argvars;
15443 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015444{
15445 int id = 0;
15446#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015447 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015448 long col;
15449 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000015450 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015451
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015452 lnum = get_tv_lnum(argvars); /* -1 on type error */
15453 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
15454 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015455
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015456 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015457 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +000015458 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015459#endif
15460
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015461 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015462}
15463
15464/*
15465 * "synIDattr(id, what [, mode])" function
15466 */
15467/*ARGSUSED*/
15468 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015469f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015470 typval_T *argvars;
15471 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015472{
15473 char_u *p = NULL;
15474#ifdef FEAT_SYN_HL
15475 int id;
15476 char_u *what;
15477 char_u *mode;
15478 char_u modebuf[NUMBUFLEN];
15479 int modec;
15480
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015481 id = get_tv_number(&argvars[0]);
15482 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015483 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015484 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015485 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015486 modec = TOLOWER_ASC(mode[0]);
15487 if (modec != 't' && modec != 'c'
15488#ifdef FEAT_GUI
15489 && modec != 'g'
15490#endif
15491 )
15492 modec = 0; /* replace invalid with current */
15493 }
15494 else
15495 {
15496#ifdef FEAT_GUI
15497 if (gui.in_use)
15498 modec = 'g';
15499 else
15500#endif
15501 if (t_colors > 1)
15502 modec = 'c';
15503 else
15504 modec = 't';
15505 }
15506
15507
15508 switch (TOLOWER_ASC(what[0]))
15509 {
15510 case 'b':
15511 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
15512 p = highlight_color(id, what, modec);
15513 else /* bold */
15514 p = highlight_has_attr(id, HL_BOLD, modec);
15515 break;
15516
15517 case 'f': /* fg[#] */
15518 p = highlight_color(id, what, modec);
15519 break;
15520
15521 case 'i':
15522 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
15523 p = highlight_has_attr(id, HL_INVERSE, modec);
15524 else /* italic */
15525 p = highlight_has_attr(id, HL_ITALIC, modec);
15526 break;
15527
15528 case 'n': /* name */
15529 p = get_highlight_name(NULL, id - 1);
15530 break;
15531
15532 case 'r': /* reverse */
15533 p = highlight_has_attr(id, HL_INVERSE, modec);
15534 break;
15535
15536 case 's': /* standout */
15537 p = highlight_has_attr(id, HL_STANDOUT, modec);
15538 break;
15539
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000015540 case 'u':
15541 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
15542 /* underline */
15543 p = highlight_has_attr(id, HL_UNDERLINE, modec);
15544 else
15545 /* undercurl */
15546 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015547 break;
15548 }
15549
15550 if (p != NULL)
15551 p = vim_strsave(p);
15552#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015553 rettv->v_type = VAR_STRING;
15554 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015555}
15556
15557/*
15558 * "synIDtrans(id)" function
15559 */
15560/*ARGSUSED*/
15561 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015562f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015563 typval_T *argvars;
15564 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015565{
15566 int id;
15567
15568#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015569 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015570
15571 if (id > 0)
15572 id = syn_get_final_id(id);
15573 else
15574#endif
15575 id = 0;
15576
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015577 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015578}
15579
15580/*
15581 * "system()" function
15582 */
15583 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015584f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015585 typval_T *argvars;
15586 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015587{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015588 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015589 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015590 char_u *infile = NULL;
15591 char_u buf[NUMBUFLEN];
15592 int err = FALSE;
15593 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015594
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015595 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015596 {
15597 /*
15598 * Write the string to a temp file, to be used for input of the shell
15599 * command.
15600 */
15601 if ((infile = vim_tempname('i')) == NULL)
15602 {
15603 EMSG(_(e_notmp));
15604 return;
15605 }
15606
15607 fd = mch_fopen((char *)infile, WRITEBIN);
15608 if (fd == NULL)
15609 {
15610 EMSG2(_(e_notopen), infile);
15611 goto done;
15612 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015613 p = get_tv_string_buf_chk(&argvars[1], buf);
15614 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015615 {
15616 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015617 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015618 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015619 if (fwrite(p, STRLEN(p), 1, fd) != 1)
15620 err = TRUE;
15621 if (fclose(fd) != 0)
15622 err = TRUE;
15623 if (err)
15624 {
15625 EMSG(_("E677: Error writing temp file"));
15626 goto done;
15627 }
15628 }
15629
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015630 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
15631 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015632
Bram Moolenaar071d4272004-06-13 20:20:40 +000015633#ifdef USE_CR
15634 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015635 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015636 {
15637 char_u *s;
15638
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015639 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015640 {
15641 if (*s == CAR)
15642 *s = NL;
15643 }
15644 }
15645#else
15646# ifdef USE_CRNL
15647 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015648 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015649 {
15650 char_u *s, *d;
15651
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015652 d = res;
15653 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015654 {
15655 if (s[0] == CAR && s[1] == NL)
15656 ++s;
15657 *d++ = *s;
15658 }
15659 *d = NUL;
15660 }
15661# endif
15662#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015663
15664done:
15665 if (infile != NULL)
15666 {
15667 mch_remove(infile);
15668 vim_free(infile);
15669 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015670 rettv->v_type = VAR_STRING;
15671 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015672}
15673
15674/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015675 * "tabpagebuflist()" function
15676 */
15677/* ARGSUSED */
15678 static void
15679f_tabpagebuflist(argvars, rettv)
15680 typval_T *argvars;
15681 typval_T *rettv;
15682{
15683#ifndef FEAT_WINDOWS
15684 rettv->vval.v_number = 0;
15685#else
15686 tabpage_T *tp;
15687 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015688
15689 if (argvars[0].v_type == VAR_UNKNOWN)
15690 wp = firstwin;
15691 else
15692 {
15693 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15694 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000015695 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015696 }
15697 if (wp == NULL)
15698 rettv->vval.v_number = 0;
15699 else
15700 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015701 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015702 rettv->vval.v_number = 0;
15703 else
15704 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015705 for (; wp != NULL; wp = wp->w_next)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015706 if (list_append_number(rettv->vval.v_list,
15707 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015708 break;
15709 }
15710 }
15711#endif
15712}
15713
15714
15715/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015716 * "tabpagenr()" function
15717 */
15718/* ARGSUSED */
15719 static void
15720f_tabpagenr(argvars, rettv)
15721 typval_T *argvars;
15722 typval_T *rettv;
15723{
15724 int nr = 1;
15725#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015726 char_u *arg;
15727
15728 if (argvars[0].v_type != VAR_UNKNOWN)
15729 {
15730 arg = get_tv_string_chk(&argvars[0]);
15731 nr = 0;
15732 if (arg != NULL)
15733 {
15734 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000015735 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015736 else
15737 EMSG2(_(e_invexpr2), arg);
15738 }
15739 }
15740 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015741 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015742#endif
15743 rettv->vval.v_number = nr;
15744}
15745
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015746
15747#ifdef FEAT_WINDOWS
15748static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
15749
15750/*
15751 * Common code for tabpagewinnr() and winnr().
15752 */
15753 static int
15754get_winnr(tp, argvar)
15755 tabpage_T *tp;
15756 typval_T *argvar;
15757{
15758 win_T *twin;
15759 int nr = 1;
15760 win_T *wp;
15761 char_u *arg;
15762
15763 twin = (tp == curtab) ? curwin : tp->tp_curwin;
15764 if (argvar->v_type != VAR_UNKNOWN)
15765 {
15766 arg = get_tv_string_chk(argvar);
15767 if (arg == NULL)
15768 nr = 0; /* type error; errmsg already given */
15769 else if (STRCMP(arg, "$") == 0)
15770 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
15771 else if (STRCMP(arg, "#") == 0)
15772 {
15773 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
15774 if (twin == NULL)
15775 nr = 0;
15776 }
15777 else
15778 {
15779 EMSG2(_(e_invexpr2), arg);
15780 nr = 0;
15781 }
15782 }
15783
15784 if (nr > 0)
15785 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
15786 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000015787 {
15788 if (wp == NULL)
15789 {
15790 /* didn't find it in this tabpage */
15791 nr = 0;
15792 break;
15793 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015794 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000015795 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015796 return nr;
15797}
15798#endif
15799
15800/*
15801 * "tabpagewinnr()" function
15802 */
15803/* ARGSUSED */
15804 static void
15805f_tabpagewinnr(argvars, rettv)
15806 typval_T *argvars;
15807 typval_T *rettv;
15808{
15809 int nr = 1;
15810#ifdef FEAT_WINDOWS
15811 tabpage_T *tp;
15812
15813 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15814 if (tp == NULL)
15815 nr = 0;
15816 else
15817 nr = get_winnr(tp, &argvars[1]);
15818#endif
15819 rettv->vval.v_number = nr;
15820}
15821
15822
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015823/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015824 * "tagfiles()" function
15825 */
15826/*ARGSUSED*/
15827 static void
15828f_tagfiles(argvars, rettv)
15829 typval_T *argvars;
15830 typval_T *rettv;
15831{
15832 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015833 tagname_T tn;
15834 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015835
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015836 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015837 {
15838 rettv->vval.v_number = 0;
15839 return;
15840 }
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015841
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015842 for (first = TRUE; ; first = FALSE)
15843 if (get_tagfname(&tn, first, fname) == FAIL
15844 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015845 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015846 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015847}
15848
15849/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000015850 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015851 */
15852 static void
15853f_taglist(argvars, rettv)
15854 typval_T *argvars;
15855 typval_T *rettv;
15856{
15857 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015858
15859 tag_pattern = get_tv_string(&argvars[0]);
15860
15861 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015862 if (*tag_pattern == NUL)
15863 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015864
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015865 if (rettv_list_alloc(rettv) == OK)
15866 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015867}
15868
15869/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015870 * "tempname()" function
15871 */
15872/*ARGSUSED*/
15873 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015874f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015875 typval_T *argvars;
15876 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015877{
15878 static int x = 'A';
15879
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015880 rettv->v_type = VAR_STRING;
15881 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015882
15883 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
15884 * names. Skip 'I' and 'O', they are used for shell redirection. */
15885 do
15886 {
15887 if (x == 'Z')
15888 x = '0';
15889 else if (x == '9')
15890 x = 'A';
15891 else
15892 {
15893#ifdef EBCDIC
15894 if (x == 'I')
15895 x = 'J';
15896 else if (x == 'R')
15897 x = 'S';
15898 else
15899#endif
15900 ++x;
15901 }
15902 } while (x == 'I' || x == 'O');
15903}
15904
15905/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000015906 * "test(list)" function: Just checking the walls...
15907 */
15908/*ARGSUSED*/
15909 static void
15910f_test(argvars, rettv)
15911 typval_T *argvars;
15912 typval_T *rettv;
15913{
15914 /* Used for unit testing. Change the code below to your liking. */
15915#if 0
15916 listitem_T *li;
15917 list_T *l;
15918 char_u *bad, *good;
15919
15920 if (argvars[0].v_type != VAR_LIST)
15921 return;
15922 l = argvars[0].vval.v_list;
15923 if (l == NULL)
15924 return;
15925 li = l->lv_first;
15926 if (li == NULL)
15927 return;
15928 bad = get_tv_string(&li->li_tv);
15929 li = li->li_next;
15930 if (li == NULL)
15931 return;
15932 good = get_tv_string(&li->li_tv);
15933 rettv->vval.v_number = test_edit_score(bad, good);
15934#endif
15935}
15936
15937/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015938 * "tolower(string)" function
15939 */
15940 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015941f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015942 typval_T *argvars;
15943 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015944{
15945 char_u *p;
15946
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015947 p = vim_strsave(get_tv_string(&argvars[0]));
15948 rettv->v_type = VAR_STRING;
15949 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015950
15951 if (p != NULL)
15952 while (*p != NUL)
15953 {
15954#ifdef FEAT_MBYTE
15955 int l;
15956
15957 if (enc_utf8)
15958 {
15959 int c, lc;
15960
15961 c = utf_ptr2char(p);
15962 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015963 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015964 /* TODO: reallocate string when byte count changes. */
15965 if (utf_char2len(lc) == l)
15966 utf_char2bytes(lc, p);
15967 p += l;
15968 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015969 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015970 p += l; /* skip multi-byte character */
15971 else
15972#endif
15973 {
15974 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
15975 ++p;
15976 }
15977 }
15978}
15979
15980/*
15981 * "toupper(string)" function
15982 */
15983 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015984f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015985 typval_T *argvars;
15986 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015987{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015988 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015989 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015990}
15991
15992/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000015993 * "tr(string, fromstr, tostr)" function
15994 */
15995 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015996f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015997 typval_T *argvars;
15998 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015999{
16000 char_u *instr;
16001 char_u *fromstr;
16002 char_u *tostr;
16003 char_u *p;
16004#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000016005 int inlen;
16006 int fromlen;
16007 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000016008 int idx;
16009 char_u *cpstr;
16010 int cplen;
16011 int first = TRUE;
16012#endif
16013 char_u buf[NUMBUFLEN];
16014 char_u buf2[NUMBUFLEN];
16015 garray_T ga;
16016
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016017 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016018 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
16019 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016020
16021 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016022 rettv->v_type = VAR_STRING;
16023 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016024 if (fromstr == NULL || tostr == NULL)
16025 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000016026 ga_init2(&ga, (int)sizeof(char), 80);
16027
16028#ifdef FEAT_MBYTE
16029 if (!has_mbyte)
16030#endif
16031 /* not multi-byte: fromstr and tostr must be the same length */
16032 if (STRLEN(fromstr) != STRLEN(tostr))
16033 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016034#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000016035error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016036#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000016037 EMSG2(_(e_invarg2), fromstr);
16038 ga_clear(&ga);
16039 return;
16040 }
16041
16042 /* fromstr and tostr have to contain the same number of chars */
16043 while (*instr != NUL)
16044 {
16045#ifdef FEAT_MBYTE
16046 if (has_mbyte)
16047 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016048 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016049 cpstr = instr;
16050 cplen = inlen;
16051 idx = 0;
16052 for (p = fromstr; *p != NUL; p += fromlen)
16053 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016054 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016055 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
16056 {
16057 for (p = tostr; *p != NUL; p += tolen)
16058 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016059 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016060 if (idx-- == 0)
16061 {
16062 cplen = tolen;
16063 cpstr = p;
16064 break;
16065 }
16066 }
16067 if (*p == NUL) /* tostr is shorter than fromstr */
16068 goto error;
16069 break;
16070 }
16071 ++idx;
16072 }
16073
16074 if (first && cpstr == instr)
16075 {
16076 /* Check that fromstr and tostr have the same number of
16077 * (multi-byte) characters. Done only once when a character
16078 * of instr doesn't appear in fromstr. */
16079 first = FALSE;
16080 for (p = tostr; *p != NUL; p += tolen)
16081 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016082 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016083 --idx;
16084 }
16085 if (idx != 0)
16086 goto error;
16087 }
16088
16089 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000016090 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016091 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000016092
16093 instr += inlen;
16094 }
16095 else
16096#endif
16097 {
16098 /* When not using multi-byte chars we can do it faster. */
16099 p = vim_strchr(fromstr, *instr);
16100 if (p != NULL)
16101 ga_append(&ga, tostr[p - fromstr]);
16102 else
16103 ga_append(&ga, *instr);
16104 ++instr;
16105 }
16106 }
16107
Bram Moolenaar61b974b2006-12-05 09:32:29 +000016108 /* add a terminating NUL */
16109 ga_grow(&ga, 1);
16110 ga_append(&ga, NUL);
16111
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016112 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000016113}
16114
16115/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016116 * "type(expr)" function
16117 */
16118 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016119f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016120 typval_T *argvars;
16121 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016122{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016123 int n;
16124
16125 switch (argvars[0].v_type)
16126 {
16127 case VAR_NUMBER: n = 0; break;
16128 case VAR_STRING: n = 1; break;
16129 case VAR_FUNC: n = 2; break;
16130 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016131 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016132 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
16133 }
16134 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016135}
16136
16137/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016138 * "values(dict)" function
16139 */
16140 static void
16141f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016142 typval_T *argvars;
16143 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016144{
16145 dict_list(argvars, rettv, 1);
16146}
16147
16148/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016149 * "virtcol(string)" function
16150 */
16151 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016152f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016153 typval_T *argvars;
16154 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016155{
16156 colnr_T vcol = 0;
16157 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016158 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016159
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016160 fp = var2fpos(&argvars[0], FALSE, &fnum);
16161 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
16162 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016163 {
16164 getvvcol(curwin, fp, NULL, NULL, &vcol);
16165 ++vcol;
16166 }
16167
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016168 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016169}
16170
16171/*
16172 * "visualmode()" function
16173 */
16174/*ARGSUSED*/
16175 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016176f_visualmode(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#ifdef FEAT_VISUAL
16181 char_u str[2];
16182
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016183 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016184 str[0] = curbuf->b_visual_mode_eval;
16185 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016186 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016187
16188 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016189 if ((argvars[0].v_type == VAR_NUMBER
16190 && argvars[0].vval.v_number != 0)
16191 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016192 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016193 curbuf->b_visual_mode_eval = NUL;
16194#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016195 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016196#endif
16197}
16198
16199/*
16200 * "winbufnr(nr)" function
16201 */
16202 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016203f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016204 typval_T *argvars;
16205 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016206{
16207 win_T *wp;
16208
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016209 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016210 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016211 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016212 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016213 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016214}
16215
16216/*
16217 * "wincol()" function
16218 */
16219/*ARGSUSED*/
16220 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016221f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016222 typval_T *argvars;
16223 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016224{
16225 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016226 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016227}
16228
16229/*
16230 * "winheight(nr)" function
16231 */
16232 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016233f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016234 typval_T *argvars;
16235 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016236{
16237 win_T *wp;
16238
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016239 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016240 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016241 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016242 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016243 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016244}
16245
16246/*
16247 * "winline()" function
16248 */
16249/*ARGSUSED*/
16250 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016251f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016252 typval_T *argvars;
16253 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016254{
16255 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016256 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016257}
16258
16259/*
16260 * "winnr()" function
16261 */
16262/* ARGSUSED */
16263 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016264f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016265 typval_T *argvars;
16266 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016267{
16268 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016269
Bram Moolenaar071d4272004-06-13 20:20:40 +000016270#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016271 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016272#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016273 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016274}
16275
16276/*
16277 * "winrestcmd()" function
16278 */
16279/* ARGSUSED */
16280 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016281f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016282 typval_T *argvars;
16283 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016284{
16285#ifdef FEAT_WINDOWS
16286 win_T *wp;
16287 int winnr = 1;
16288 garray_T ga;
16289 char_u buf[50];
16290
16291 ga_init2(&ga, (int)sizeof(char), 70);
16292 for (wp = firstwin; wp != NULL; wp = wp->w_next)
16293 {
16294 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
16295 ga_concat(&ga, buf);
16296# ifdef FEAT_VERTSPLIT
16297 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
16298 ga_concat(&ga, buf);
16299# endif
16300 ++winnr;
16301 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000016302 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016303
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016304 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016305#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016306 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016307#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016308 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016309}
16310
16311/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016312 * "winrestview()" function
16313 */
16314/* ARGSUSED */
16315 static void
16316f_winrestview(argvars, rettv)
16317 typval_T *argvars;
16318 typval_T *rettv;
16319{
16320 dict_T *dict;
16321
16322 if (argvars[0].v_type != VAR_DICT
16323 || (dict = argvars[0].vval.v_dict) == NULL)
16324 EMSG(_(e_invarg));
16325 else
16326 {
16327 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
16328 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
16329#ifdef FEAT_VIRTUALEDIT
16330 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
16331#endif
16332 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016333 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016334
Bram Moolenaar6f11a412006-09-06 20:16:42 +000016335 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016336#ifdef FEAT_DIFF
16337 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
16338#endif
16339 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
16340 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
16341
16342 check_cursor();
16343 changed_cline_bef_curs();
16344 invalidate_botline();
16345 redraw_later(VALID);
16346
16347 if (curwin->w_topline == 0)
16348 curwin->w_topline = 1;
16349 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
16350 curwin->w_topline = curbuf->b_ml.ml_line_count;
16351#ifdef FEAT_DIFF
16352 check_topfill(curwin, TRUE);
16353#endif
16354 }
16355}
16356
16357/*
16358 * "winsaveview()" function
16359 */
16360/* ARGSUSED */
16361 static void
16362f_winsaveview(argvars, rettv)
16363 typval_T *argvars;
16364 typval_T *rettv;
16365{
16366 dict_T *dict;
16367
16368 dict = dict_alloc();
16369 if (dict == NULL)
16370 return;
16371 rettv->v_type = VAR_DICT;
16372 rettv->vval.v_dict = dict;
16373 ++dict->dv_refcount;
16374
16375 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
16376 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
16377#ifdef FEAT_VIRTUALEDIT
16378 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
16379#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000016380 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016381 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
16382
16383 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
16384#ifdef FEAT_DIFF
16385 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
16386#endif
16387 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
16388 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
16389}
16390
16391/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016392 * "winwidth(nr)" function
16393 */
16394 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016395f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016396 typval_T *argvars;
16397 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016398{
16399 win_T *wp;
16400
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016401 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016402 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016403 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016404 else
16405#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016406 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016407#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016408 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016409#endif
16410}
16411
Bram Moolenaar071d4272004-06-13 20:20:40 +000016412/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016413 * "writefile()" function
16414 */
16415 static void
16416f_writefile(argvars, rettv)
16417 typval_T *argvars;
16418 typval_T *rettv;
16419{
16420 int binary = FALSE;
16421 char_u *fname;
16422 FILE *fd;
16423 listitem_T *li;
16424 char_u *s;
16425 int ret = 0;
16426 int c;
16427
16428 if (argvars[0].v_type != VAR_LIST)
16429 {
16430 EMSG2(_(e_listarg), "writefile()");
16431 return;
16432 }
16433 if (argvars[0].vval.v_list == NULL)
16434 return;
16435
16436 if (argvars[2].v_type != VAR_UNKNOWN
16437 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
16438 binary = TRUE;
16439
16440 /* Always open the file in binary mode, library functions have a mind of
16441 * their own about CR-LF conversion. */
16442 fname = get_tv_string(&argvars[1]);
16443 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
16444 {
16445 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
16446 ret = -1;
16447 }
16448 else
16449 {
16450 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
16451 li = li->li_next)
16452 {
16453 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
16454 {
16455 if (*s == '\n')
16456 c = putc(NUL, fd);
16457 else
16458 c = putc(*s, fd);
16459 if (c == EOF)
16460 {
16461 ret = -1;
16462 break;
16463 }
16464 }
16465 if (!binary || li->li_next != NULL)
16466 if (putc('\n', fd) == EOF)
16467 {
16468 ret = -1;
16469 break;
16470 }
16471 if (ret < 0)
16472 {
16473 EMSG(_(e_write));
16474 break;
16475 }
16476 }
16477 fclose(fd);
16478 }
16479
16480 rettv->vval.v_number = ret;
16481}
16482
16483/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016484 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016485 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016486 */
16487 static pos_T *
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016488var2fpos(varp, lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000016489 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016490 int lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016491 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016492{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016493 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016494 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016495 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016496
Bram Moolenaara5525202006-03-02 22:52:09 +000016497 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016498 if (varp->v_type == VAR_LIST)
16499 {
16500 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016501 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000016502 int error = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016503
16504 l = varp->vval.v_list;
16505 if (l == NULL)
16506 return NULL;
16507
16508 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016509 pos.lnum = list_find_nr(l, 0L, &error);
16510 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016511 return NULL; /* invalid line number */
16512
16513 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016514 pos.col = list_find_nr(l, 1L, &error);
16515 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016516 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016517 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaara5525202006-03-02 22:52:09 +000016518 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000016519 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016520 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016521 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016522
Bram Moolenaara5525202006-03-02 22:52:09 +000016523#ifdef FEAT_VIRTUALEDIT
16524 /* Get the virtual offset. Defaults to zero. */
16525 pos.coladd = list_find_nr(l, 2L, &error);
16526 if (error)
16527 pos.coladd = 0;
16528#endif
16529
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016530 return &pos;
16531 }
16532
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016533 name = get_tv_string_chk(varp);
16534 if (name == NULL)
16535 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016536 if (name[0] == '.') /* cursor */
16537 return &curwin->w_cursor;
16538 if (name[0] == '\'') /* mark */
16539 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016540 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016541 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
16542 return NULL;
16543 return pp;
16544 }
Bram Moolenaara5525202006-03-02 22:52:09 +000016545
16546#ifdef FEAT_VIRTUALEDIT
16547 pos.coladd = 0;
16548#endif
16549
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016550 if (name[0] == 'w' && lnum)
16551 {
16552 pos.col = 0;
16553 if (name[1] == '0') /* "w0": first visible line */
16554 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016555 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016556 pos.lnum = curwin->w_topline;
16557 return &pos;
16558 }
16559 else if (name[1] == '$') /* "w$": last visible line */
16560 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016561 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016562 pos.lnum = curwin->w_botline - 1;
16563 return &pos;
16564 }
16565 }
16566 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016567 {
16568 if (lnum)
16569 {
16570 pos.lnum = curbuf->b_ml.ml_line_count;
16571 pos.col = 0;
16572 }
16573 else
16574 {
16575 pos.lnum = curwin->w_cursor.lnum;
16576 pos.col = (colnr_T)STRLEN(ml_get_curline());
16577 }
16578 return &pos;
16579 }
16580 return NULL;
16581}
16582
16583/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016584 * Convert list in "arg" into a position and optional file number.
16585 * When "fnump" is NULL there is no file number, only 3 items.
16586 * Note that the column is passed on as-is, the caller may want to decrement
16587 * it to use 1 for the first column.
16588 * Return FAIL when conversion is not possible, doesn't check the position for
16589 * validity.
16590 */
16591 static int
16592list2fpos(arg, posp, fnump)
16593 typval_T *arg;
16594 pos_T *posp;
16595 int *fnump;
16596{
16597 list_T *l = arg->vval.v_list;
16598 long i = 0;
16599 long n;
16600
Bram Moolenaarbde35262006-07-23 20:12:24 +000016601 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
16602 * when "fnump" isn't NULL and "coladd" is optional. */
16603 if (arg->v_type != VAR_LIST
16604 || l == NULL
16605 || l->lv_len < (fnump == NULL ? 2 : 3)
16606 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016607 return FAIL;
16608
16609 if (fnump != NULL)
16610 {
16611 n = list_find_nr(l, i++, NULL); /* fnum */
16612 if (n < 0)
16613 return FAIL;
16614 if (n == 0)
16615 n = curbuf->b_fnum; /* current buffer */
16616 *fnump = n;
16617 }
16618
16619 n = list_find_nr(l, i++, NULL); /* lnum */
16620 if (n < 0)
16621 return FAIL;
16622 posp->lnum = n;
16623
16624 n = list_find_nr(l, i++, NULL); /* col */
16625 if (n < 0)
16626 return FAIL;
16627 posp->col = n;
16628
16629#ifdef FEAT_VIRTUALEDIT
16630 n = list_find_nr(l, i, NULL);
16631 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000016632 posp->coladd = 0;
16633 else
16634 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016635#endif
16636
16637 return OK;
16638}
16639
16640/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016641 * Get the length of an environment variable name.
16642 * Advance "arg" to the first character after the name.
16643 * Return 0 for error.
16644 */
16645 static int
16646get_env_len(arg)
16647 char_u **arg;
16648{
16649 char_u *p;
16650 int len;
16651
16652 for (p = *arg; vim_isIDc(*p); ++p)
16653 ;
16654 if (p == *arg) /* no name found */
16655 return 0;
16656
16657 len = (int)(p - *arg);
16658 *arg = p;
16659 return len;
16660}
16661
16662/*
16663 * Get the length of the name of a function or internal variable.
16664 * "arg" is advanced to the first non-white character after the name.
16665 * Return 0 if something is wrong.
16666 */
16667 static int
16668get_id_len(arg)
16669 char_u **arg;
16670{
16671 char_u *p;
16672 int len;
16673
16674 /* Find the end of the name. */
16675 for (p = *arg; eval_isnamec(*p); ++p)
16676 ;
16677 if (p == *arg) /* no name found */
16678 return 0;
16679
16680 len = (int)(p - *arg);
16681 *arg = skipwhite(p);
16682
16683 return len;
16684}
16685
16686/*
Bram Moolenaara7043832005-01-21 11:56:39 +000016687 * Get the length of the name of a variable or function.
16688 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000016689 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016690 * Return -1 if curly braces expansion failed.
16691 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016692 * If the name contains 'magic' {}'s, expand them and return the
16693 * expanded name in an allocated string via 'alias' - caller must free.
16694 */
16695 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016696get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016697 char_u **arg;
16698 char_u **alias;
16699 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016700 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016701{
16702 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016703 char_u *p;
16704 char_u *expr_start;
16705 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016706
16707 *alias = NULL; /* default to no alias */
16708
16709 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
16710 && (*arg)[2] == (int)KE_SNR)
16711 {
16712 /* hard coded <SNR>, already translated */
16713 *arg += 3;
16714 return get_id_len(arg) + 3;
16715 }
16716 len = eval_fname_script(*arg);
16717 if (len > 0)
16718 {
16719 /* literal "<SID>", "s:" or "<SNR>" */
16720 *arg += len;
16721 }
16722
Bram Moolenaar071d4272004-06-13 20:20:40 +000016723 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016724 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016725 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016726 p = find_name_end(*arg, &expr_start, &expr_end,
16727 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016728 if (expr_start != NULL)
16729 {
16730 char_u *temp_string;
16731
16732 if (!evaluate)
16733 {
16734 len += (int)(p - *arg);
16735 *arg = skipwhite(p);
16736 return len;
16737 }
16738
16739 /*
16740 * Include any <SID> etc in the expanded string:
16741 * Thus the -len here.
16742 */
16743 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
16744 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016745 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016746 *alias = temp_string;
16747 *arg = skipwhite(p);
16748 return (int)STRLEN(temp_string);
16749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016750
16751 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016752 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016753 EMSG2(_(e_invexpr2), *arg);
16754
16755 return len;
16756}
16757
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016758/*
16759 * Find the end of a variable or function name, taking care of magic braces.
16760 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
16761 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016762 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016763 * Return a pointer to just after the name. Equal to "arg" if there is no
16764 * valid name.
16765 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016766 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016767find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016768 char_u *arg;
16769 char_u **expr_start;
16770 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016771 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016772{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016773 int mb_nest = 0;
16774 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016775 char_u *p;
16776
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016777 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016778 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016779 *expr_start = NULL;
16780 *expr_end = NULL;
16781 }
16782
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016783 /* Quick check for valid starting character. */
16784 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
16785 return arg;
16786
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016787 for (p = arg; *p != NUL
16788 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016789 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016790 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016791 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000016792 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016793 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000016794 if (*p == '\'')
16795 {
16796 /* skip over 'string' to avoid counting [ and ] inside it. */
16797 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
16798 ;
16799 if (*p == NUL)
16800 break;
16801 }
16802 else if (*p == '"')
16803 {
16804 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
16805 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
16806 if (*p == '\\' && p[1] != NUL)
16807 ++p;
16808 if (*p == NUL)
16809 break;
16810 }
16811
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016812 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016813 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016814 if (*p == '[')
16815 ++br_nest;
16816 else if (*p == ']')
16817 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016818 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000016819
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016820 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016821 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016822 if (*p == '{')
16823 {
16824 mb_nest++;
16825 if (expr_start != NULL && *expr_start == NULL)
16826 *expr_start = p;
16827 }
16828 else if (*p == '}')
16829 {
16830 mb_nest--;
16831 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
16832 *expr_end = p;
16833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016835 }
16836
16837 return p;
16838}
16839
16840/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016841 * Expands out the 'magic' {}'s in a variable/function name.
16842 * Note that this can call itself recursively, to deal with
16843 * constructs like foo{bar}{baz}{bam}
16844 * The four pointer arguments point to "foo{expre}ss{ion}bar"
16845 * "in_start" ^
16846 * "expr_start" ^
16847 * "expr_end" ^
16848 * "in_end" ^
16849 *
16850 * Returns a new allocated string, which the caller must free.
16851 * Returns NULL for failure.
16852 */
16853 static char_u *
16854make_expanded_name(in_start, expr_start, expr_end, in_end)
16855 char_u *in_start;
16856 char_u *expr_start;
16857 char_u *expr_end;
16858 char_u *in_end;
16859{
16860 char_u c1;
16861 char_u *retval = NULL;
16862 char_u *temp_result;
16863 char_u *nextcmd = NULL;
16864
16865 if (expr_end == NULL || in_end == NULL)
16866 return NULL;
16867 *expr_start = NUL;
16868 *expr_end = NUL;
16869 c1 = *in_end;
16870 *in_end = NUL;
16871
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016872 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016873 if (temp_result != NULL && nextcmd == NULL)
16874 {
16875 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
16876 + (in_end - expr_end) + 1));
16877 if (retval != NULL)
16878 {
16879 STRCPY(retval, in_start);
16880 STRCAT(retval, temp_result);
16881 STRCAT(retval, expr_end + 1);
16882 }
16883 }
16884 vim_free(temp_result);
16885
16886 *in_end = c1; /* put char back for error messages */
16887 *expr_start = '{';
16888 *expr_end = '}';
16889
16890 if (retval != NULL)
16891 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016892 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016893 if (expr_start != NULL)
16894 {
16895 /* Further expansion! */
16896 temp_result = make_expanded_name(retval, expr_start,
16897 expr_end, temp_result);
16898 vim_free(retval);
16899 retval = temp_result;
16900 }
16901 }
16902
16903 return retval;
16904}
16905
16906/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016907 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016908 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016909 */
16910 static int
16911eval_isnamec(c)
16912 int c;
16913{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016914 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
16915}
16916
16917/*
16918 * Return TRUE if character "c" can be used as the first character in a
16919 * variable or function name (excluding '{' and '}').
16920 */
16921 static int
16922eval_isnamec1(c)
16923 int c;
16924{
16925 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000016926}
16927
16928/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016929 * Set number v: variable to "val".
16930 */
16931 void
16932set_vim_var_nr(idx, val)
16933 int idx;
16934 long val;
16935{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016936 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016937}
16938
16939/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016940 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016941 */
16942 long
16943get_vim_var_nr(idx)
16944 int idx;
16945{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016946 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016947}
16948
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016949#if defined(FEAT_AUTOCMD) || defined(PROTO)
16950/*
16951 * Get string v: variable value. Uses a static buffer, can only be used once.
16952 */
16953 char_u *
16954get_vim_var_str(idx)
16955 int idx;
16956{
16957 return get_tv_string(&vimvars[idx].vv_tv);
16958}
16959#endif
16960
Bram Moolenaar071d4272004-06-13 20:20:40 +000016961/*
16962 * Set v:count, v:count1 and v:prevcount.
16963 */
16964 void
16965set_vcount(count, count1)
16966 long count;
16967 long count1;
16968{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016969 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
16970 vimvars[VV_COUNT].vv_nr = count;
16971 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016972}
16973
16974/*
16975 * Set string v: variable to a copy of "val".
16976 */
16977 void
16978set_vim_var_string(idx, val, len)
16979 int idx;
16980 char_u *val;
16981 int len; /* length of "val" to use or -1 (whole string) */
16982{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016983 /* Need to do this (at least) once, since we can't initialize a union.
16984 * Will always be invoked when "v:progname" is set. */
16985 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
16986
Bram Moolenaare9a41262005-01-15 22:18:47 +000016987 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016988 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016989 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016990 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016991 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016992 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000016993 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016994}
16995
16996/*
16997 * Set v:register if needed.
16998 */
16999 void
17000set_reg_var(c)
17001 int c;
17002{
17003 char_u regname;
17004
17005 if (c == 0 || c == ' ')
17006 regname = '"';
17007 else
17008 regname = c;
17009 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000017010 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017011 set_vim_var_string(VV_REG, &regname, 1);
17012}
17013
17014/*
17015 * Get or set v:exception. If "oldval" == NULL, return the current value.
17016 * Otherwise, restore the value to "oldval" and return NULL.
17017 * Must always be called in pairs to save and restore v:exception! Does not
17018 * take care of memory allocations.
17019 */
17020 char_u *
17021v_exception(oldval)
17022 char_u *oldval;
17023{
17024 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017025 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017026
Bram Moolenaare9a41262005-01-15 22:18:47 +000017027 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017028 return NULL;
17029}
17030
17031/*
17032 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
17033 * Otherwise, restore the value to "oldval" and return NULL.
17034 * Must always be called in pairs to save and restore v:throwpoint! Does not
17035 * take care of memory allocations.
17036 */
17037 char_u *
17038v_throwpoint(oldval)
17039 char_u *oldval;
17040{
17041 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017042 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017043
Bram Moolenaare9a41262005-01-15 22:18:47 +000017044 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017045 return NULL;
17046}
17047
17048#if defined(FEAT_AUTOCMD) || defined(PROTO)
17049/*
17050 * Set v:cmdarg.
17051 * If "eap" != NULL, use "eap" to generate the value and return the old value.
17052 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
17053 * Must always be called in pairs!
17054 */
17055 char_u *
17056set_cmdarg(eap, oldarg)
17057 exarg_T *eap;
17058 char_u *oldarg;
17059{
17060 char_u *oldval;
17061 char_u *newval;
17062 unsigned len;
17063
Bram Moolenaare9a41262005-01-15 22:18:47 +000017064 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017065 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017066 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017067 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000017068 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017069 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017070 }
17071
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017072 if (eap->force_bin == FORCE_BIN)
17073 len = 6;
17074 else if (eap->force_bin == FORCE_NOBIN)
17075 len = 8;
17076 else
17077 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017078
17079 if (eap->read_edit)
17080 len += 7;
17081
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017082 if (eap->force_ff != 0)
17083 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
17084# ifdef FEAT_MBYTE
17085 if (eap->force_enc != 0)
17086 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000017087 if (eap->bad_char != 0)
17088 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017089# endif
17090
17091 newval = alloc(len + 1);
17092 if (newval == NULL)
17093 return NULL;
17094
17095 if (eap->force_bin == FORCE_BIN)
17096 sprintf((char *)newval, " ++bin");
17097 else if (eap->force_bin == FORCE_NOBIN)
17098 sprintf((char *)newval, " ++nobin");
17099 else
17100 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017101
17102 if (eap->read_edit)
17103 STRCAT(newval, " ++edit");
17104
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017105 if (eap->force_ff != 0)
17106 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
17107 eap->cmd + eap->force_ff);
17108# ifdef FEAT_MBYTE
17109 if (eap->force_enc != 0)
17110 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
17111 eap->cmd + eap->force_enc);
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000017112 if (eap->bad_char != 0)
17113 sprintf((char *)newval + STRLEN(newval), " ++bad=%s",
17114 eap->cmd + eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017115# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000017116 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017117 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017118}
17119#endif
17120
17121/*
17122 * Get the value of internal variable "name".
17123 * Return OK or FAIL.
17124 */
17125 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017126get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017127 char_u *name;
17128 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000017129 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017130 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017131{
17132 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000017133 typval_T *tv = NULL;
17134 typval_T atv;
17135 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017136 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017137
17138 /* truncate the name, so that we can use strcmp() */
17139 cc = name[len];
17140 name[len] = NUL;
17141
17142 /*
17143 * Check for "b:changedtick".
17144 */
17145 if (STRCMP(name, "b:changedtick") == 0)
17146 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000017147 atv.v_type = VAR_NUMBER;
17148 atv.vval.v_number = curbuf->b_changedtick;
17149 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017150 }
17151
17152 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017153 * Check for user-defined variables.
17154 */
17155 else
17156 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017157 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017158 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017159 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017160 }
17161
Bram Moolenaare9a41262005-01-15 22:18:47 +000017162 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017163 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017164 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017165 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017166 ret = FAIL;
17167 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017168 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017169 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017170
17171 name[len] = cc;
17172
17173 return ret;
17174}
17175
17176/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017177 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
17178 * Also handle function call with Funcref variable: func(expr)
17179 * Can all be combined: dict.func(expr)[idx]['func'](expr)
17180 */
17181 static int
17182handle_subscript(arg, rettv, evaluate, verbose)
17183 char_u **arg;
17184 typval_T *rettv;
17185 int evaluate; /* do more than finding the end */
17186 int verbose; /* give error messages */
17187{
17188 int ret = OK;
17189 dict_T *selfdict = NULL;
17190 char_u *s;
17191 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000017192 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017193
17194 while (ret == OK
17195 && (**arg == '['
17196 || (**arg == '.' && rettv->v_type == VAR_DICT)
17197 || (**arg == '(' && rettv->v_type == VAR_FUNC))
17198 && !vim_iswhite(*(*arg - 1)))
17199 {
17200 if (**arg == '(')
17201 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000017202 /* need to copy the funcref so that we can clear rettv */
17203 functv = *rettv;
17204 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017205
17206 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000017207 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017208 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000017209 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
17210 &len, evaluate, selfdict);
17211
17212 /* Clear the funcref afterwards, so that deleting it while
17213 * evaluating the arguments is possible (see test55). */
17214 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017215
17216 /* Stop the expression evaluation when immediately aborting on
17217 * error, or when an interrupt occurred or an exception was thrown
17218 * but not caught. */
17219 if (aborting())
17220 {
17221 if (ret == OK)
17222 clear_tv(rettv);
17223 ret = FAIL;
17224 }
17225 dict_unref(selfdict);
17226 selfdict = NULL;
17227 }
17228 else /* **arg == '[' || **arg == '.' */
17229 {
17230 dict_unref(selfdict);
17231 if (rettv->v_type == VAR_DICT)
17232 {
17233 selfdict = rettv->vval.v_dict;
17234 if (selfdict != NULL)
17235 ++selfdict->dv_refcount;
17236 }
17237 else
17238 selfdict = NULL;
17239 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
17240 {
17241 clear_tv(rettv);
17242 ret = FAIL;
17243 }
17244 }
17245 }
17246 dict_unref(selfdict);
17247 return ret;
17248}
17249
17250/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017251 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
17252 * value).
17253 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017254 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017255alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017256{
Bram Moolenaar33570922005-01-25 22:26:29 +000017257 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017258}
17259
17260/*
17261 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017262 * The string "s" must have been allocated, it is consumed.
17263 * Return NULL for out of memory, the variable otherwise.
17264 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017265 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017266alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017267 char_u *s;
17268{
Bram Moolenaar33570922005-01-25 22:26:29 +000017269 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017270
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017271 rettv = alloc_tv();
17272 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017273 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017274 rettv->v_type = VAR_STRING;
17275 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017276 }
17277 else
17278 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017279 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017280}
17281
17282/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017283 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017284 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000017285 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017286free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017287 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017288{
17289 if (varp != NULL)
17290 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017291 switch (varp->v_type)
17292 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017293 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017294 func_unref(varp->vval.v_string);
17295 /*FALLTHROUGH*/
17296 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017297 vim_free(varp->vval.v_string);
17298 break;
17299 case VAR_LIST:
17300 list_unref(varp->vval.v_list);
17301 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017302 case VAR_DICT:
17303 dict_unref(varp->vval.v_dict);
17304 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017305 case VAR_NUMBER:
17306 case VAR_UNKNOWN:
17307 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017308 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000017309 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017310 break;
17311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017312 vim_free(varp);
17313 }
17314}
17315
17316/*
17317 * Free the memory for a variable value and set the value to NULL or 0.
17318 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017319 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017320clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017321 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017322{
17323 if (varp != NULL)
17324 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017325 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017326 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017327 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017328 func_unref(varp->vval.v_string);
17329 /*FALLTHROUGH*/
17330 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017331 vim_free(varp->vval.v_string);
17332 varp->vval.v_string = NULL;
17333 break;
17334 case VAR_LIST:
17335 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017336 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017337 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017338 case VAR_DICT:
17339 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017340 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017341 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017342 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017343 varp->vval.v_number = 0;
17344 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017345 case VAR_UNKNOWN:
17346 break;
17347 default:
17348 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017349 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017350 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017351 }
17352}
17353
17354/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017355 * Set the value of a variable to NULL without freeing items.
17356 */
17357 static void
17358init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017359 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017360{
17361 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017362 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017363}
17364
17365/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017366 * Get the number value of a variable.
17367 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017368 * For incompatible types, return 0.
17369 * get_tv_number_chk() is similar to get_tv_number(), but informs the
17370 * caller of incompatible types: it sets *denote to TRUE if "denote"
17371 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017372 */
17373 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017374get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017375 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017376{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017377 int error = FALSE;
17378
17379 return get_tv_number_chk(varp, &error); /* return 0L on error */
17380}
17381
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017382 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017383get_tv_number_chk(varp, denote)
17384 typval_T *varp;
17385 int *denote;
17386{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017387 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017388
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017389 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017390 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017391 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017392 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017393 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017394 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017395 break;
17396 case VAR_STRING:
17397 if (varp->vval.v_string != NULL)
17398 vim_str2nr(varp->vval.v_string, NULL, NULL,
17399 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017400 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017401 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000017402 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017403 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017404 case VAR_DICT:
17405 EMSG(_("E728: Using a Dictionary as a number"));
17406 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017407 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017408 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017409 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017410 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017411 if (denote == NULL) /* useful for values that must be unsigned */
17412 n = -1;
17413 else
17414 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017415 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017416}
17417
17418/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000017419 * Get the lnum from the first argument.
17420 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017421 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017422 */
17423 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017424get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000017425 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017426{
Bram Moolenaar33570922005-01-25 22:26:29 +000017427 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017428 linenr_T lnum;
17429
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017430 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017431 if (lnum == 0) /* no valid number, try using line() */
17432 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017433 rettv.v_type = VAR_NUMBER;
17434 f_line(argvars, &rettv);
17435 lnum = rettv.vval.v_number;
17436 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017437 }
17438 return lnum;
17439}
17440
17441/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000017442 * Get the lnum from the first argument.
17443 * Also accepts "$", then "buf" is used.
17444 * Returns 0 on error.
17445 */
17446 static linenr_T
17447get_tv_lnum_buf(argvars, buf)
17448 typval_T *argvars;
17449 buf_T *buf;
17450{
17451 if (argvars[0].v_type == VAR_STRING
17452 && argvars[0].vval.v_string != NULL
17453 && argvars[0].vval.v_string[0] == '$'
17454 && buf != NULL)
17455 return buf->b_ml.ml_line_count;
17456 return get_tv_number_chk(&argvars[0], NULL);
17457}
17458
17459/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017460 * Get the string value of a variable.
17461 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000017462 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
17463 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017464 * If the String variable has never been set, return an empty string.
17465 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017466 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
17467 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017468 */
17469 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017470get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017471 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017472{
17473 static char_u mybuf[NUMBUFLEN];
17474
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017475 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017476}
17477
17478 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017479get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000017480 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017481 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017482{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017483 char_u *res = get_tv_string_buf_chk(varp, buf);
17484
17485 return res != NULL ? res : (char_u *)"";
17486}
17487
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017488 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017489get_tv_string_chk(varp)
17490 typval_T *varp;
17491{
17492 static char_u mybuf[NUMBUFLEN];
17493
17494 return get_tv_string_buf_chk(varp, mybuf);
17495}
17496
17497 static char_u *
17498get_tv_string_buf_chk(varp, buf)
17499 typval_T *varp;
17500 char_u *buf;
17501{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017502 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017503 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017504 case VAR_NUMBER:
17505 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
17506 return buf;
17507 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017508 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017509 break;
17510 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017511 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000017512 break;
17513 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017514 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017515 break;
17516 case VAR_STRING:
17517 if (varp->vval.v_string != NULL)
17518 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017519 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017520 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017521 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017522 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017523 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017524 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017525}
17526
17527/*
17528 * Find variable "name" in the list of variables.
17529 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017530 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017531 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000017532 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017533 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017534 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017535find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017536 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017537 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017538{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017539 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017540 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017541
Bram Moolenaara7043832005-01-21 11:56:39 +000017542 ht = find_var_ht(name, &varname);
17543 if (htp != NULL)
17544 *htp = ht;
17545 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017546 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017547 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017548}
17549
17550/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017551 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000017552 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017553 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017554 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017555find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000017556 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000017557 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017558 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000017559{
Bram Moolenaar33570922005-01-25 22:26:29 +000017560 hashitem_T *hi;
17561
17562 if (*varname == NUL)
17563 {
17564 /* Must be something like "s:", otherwise "ht" would be NULL. */
17565 switch (varname[-2])
17566 {
17567 case 's': return &SCRIPT_SV(current_SID).sv_var;
17568 case 'g': return &globvars_var;
17569 case 'v': return &vimvars_var;
17570 case 'b': return &curbuf->b_bufvar;
17571 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017572#ifdef FEAT_WINDOWS
17573 case 't': return &curtab->tp_winvar;
17574#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017575 case 'l': return current_funccal == NULL
17576 ? NULL : &current_funccal->l_vars_var;
17577 case 'a': return current_funccal == NULL
17578 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000017579 }
17580 return NULL;
17581 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017582
17583 hi = hash_find(ht, varname);
17584 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017585 {
17586 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017587 * worked find the variable again. Don't auto-load a script if it was
17588 * loaded already, otherwise it would be loaded every time when
17589 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017590 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017591 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017592 hi = hash_find(ht, varname);
17593 if (HASHITEM_EMPTY(hi))
17594 return NULL;
17595 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017596 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017597}
17598
17599/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017600 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017601 * Set "varname" to the start of name without ':'.
17602 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017603 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017604find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017605 char_u *name;
17606 char_u **varname;
17607{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017608 hashitem_T *hi;
17609
Bram Moolenaar071d4272004-06-13 20:20:40 +000017610 if (name[1] != ':')
17611 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017612 /* The name must not start with a colon or #. */
17613 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017614 return NULL;
17615 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017616
17617 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017618 hi = hash_find(&compat_hashtab, name);
17619 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000017620 return &compat_hashtab;
17621
Bram Moolenaar071d4272004-06-13 20:20:40 +000017622 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017623 return &globvarht; /* global variable */
17624 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017625 }
17626 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017627 if (*name == 'g') /* global variable */
17628 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017629 /* There must be no ':' or '#' in the rest of the name, unless g: is used
17630 */
17631 if (vim_strchr(name + 2, ':') != NULL
17632 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017633 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017634 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017635 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017636 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017637 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017638#ifdef FEAT_WINDOWS
17639 if (*name == 't') /* tab page variable */
17640 return &curtab->tp_vars.dv_hashtab;
17641#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000017642 if (*name == 'v') /* v: variable */
17643 return &vimvarht;
17644 if (*name == 'a' && current_funccal != NULL) /* function argument */
17645 return &current_funccal->l_avars.dv_hashtab;
17646 if (*name == 'l' && current_funccal != NULL) /* local function variable */
17647 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017648 if (*name == 's' /* script variable */
17649 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
17650 return &SCRIPT_VARS(current_SID);
17651 return NULL;
17652}
17653
17654/*
17655 * Get the string value of a (global/local) variable.
17656 * Returns NULL when it doesn't exist.
17657 */
17658 char_u *
17659get_var_value(name)
17660 char_u *name;
17661{
Bram Moolenaar33570922005-01-25 22:26:29 +000017662 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017663
Bram Moolenaara7043832005-01-21 11:56:39 +000017664 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017665 if (v == NULL)
17666 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000017667 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017668}
17669
17670/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017671 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000017672 * sourcing this script and when executing functions defined in the script.
17673 */
17674 void
17675new_script_vars(id)
17676 scid_T id;
17677{
Bram Moolenaara7043832005-01-21 11:56:39 +000017678 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000017679 hashtab_T *ht;
17680 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000017681
Bram Moolenaar071d4272004-06-13 20:20:40 +000017682 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
17683 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017684 /* Re-allocating ga_data means that an ht_array pointing to
17685 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000017686 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000017687 for (i = 1; i <= ga_scripts.ga_len; ++i)
17688 {
17689 ht = &SCRIPT_VARS(i);
17690 if (ht->ht_mask == HT_INIT_SIZE - 1)
17691 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000017692 sv = &SCRIPT_SV(i);
17693 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000017694 }
17695
Bram Moolenaar071d4272004-06-13 20:20:40 +000017696 while (ga_scripts.ga_len < id)
17697 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017698 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
17699 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017700 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017701 }
17702 }
17703}
17704
17705/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017706 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
17707 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017708 */
17709 void
Bram Moolenaar33570922005-01-25 22:26:29 +000017710init_var_dict(dict, dict_var)
17711 dict_T *dict;
17712 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017713{
Bram Moolenaar33570922005-01-25 22:26:29 +000017714 hash_init(&dict->dv_hashtab);
17715 dict->dv_refcount = 99999;
17716 dict_var->di_tv.vval.v_dict = dict;
17717 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017718 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017719 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
17720 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017721}
17722
17723/*
17724 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000017725 * Frees all allocated variables and the value they contain.
17726 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017727 */
17728 void
Bram Moolenaara7043832005-01-21 11:56:39 +000017729vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000017730 hashtab_T *ht;
17731{
17732 vars_clear_ext(ht, TRUE);
17733}
17734
17735/*
17736 * Like vars_clear(), but only free the value if "free_val" is TRUE.
17737 */
17738 static void
17739vars_clear_ext(ht, free_val)
17740 hashtab_T *ht;
17741 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017742{
Bram Moolenaara7043832005-01-21 11:56:39 +000017743 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000017744 hashitem_T *hi;
17745 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017746
Bram Moolenaar33570922005-01-25 22:26:29 +000017747 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017748 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000017749 for (hi = ht->ht_array; todo > 0; ++hi)
17750 {
17751 if (!HASHITEM_EMPTY(hi))
17752 {
17753 --todo;
17754
Bram Moolenaar33570922005-01-25 22:26:29 +000017755 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000017756 * ht_array might change then. hash_clear() takes care of it
17757 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017758 v = HI2DI(hi);
17759 if (free_val)
17760 clear_tv(&v->di_tv);
17761 if ((v->di_flags & DI_FLAGS_FIX) == 0)
17762 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000017763 }
17764 }
17765 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017766 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017767}
17768
Bram Moolenaara7043832005-01-21 11:56:39 +000017769/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017770 * Delete a variable from hashtab "ht" at item "hi".
17771 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000017772 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017773 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000017774delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000017775 hashtab_T *ht;
17776 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017777{
Bram Moolenaar33570922005-01-25 22:26:29 +000017778 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017779
17780 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000017781 clear_tv(&di->di_tv);
17782 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017783}
17784
17785/*
17786 * List the value of one internal variable.
17787 */
17788 static void
17789list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000017790 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017791 char_u *prefix;
17792{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017793 char_u *tofree;
17794 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017795 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017796
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017797 s = echo_string(&v->di_tv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000017798 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017799 s == NULL ? (char_u *)"" : s);
17800 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017801}
17802
Bram Moolenaar071d4272004-06-13 20:20:40 +000017803 static void
17804list_one_var_a(prefix, name, type, string)
17805 char_u *prefix;
17806 char_u *name;
17807 int type;
17808 char_u *string;
17809{
17810 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
17811 if (name != NULL) /* "a:" vars don't have a name stored */
17812 msg_puts(name);
17813 msg_putchar(' ');
17814 msg_advance(22);
17815 if (type == VAR_NUMBER)
17816 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017817 else if (type == VAR_FUNC)
17818 msg_putchar('*');
17819 else if (type == VAR_LIST)
17820 {
17821 msg_putchar('[');
17822 if (*string == '[')
17823 ++string;
17824 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017825 else if (type == VAR_DICT)
17826 {
17827 msg_putchar('{');
17828 if (*string == '{')
17829 ++string;
17830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017831 else
17832 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017833
Bram Moolenaar071d4272004-06-13 20:20:40 +000017834 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017835
17836 if (type == VAR_FUNC)
17837 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017838}
17839
17840/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017841 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000017842 * If the variable already exists, the value is updated.
17843 * Otherwise the variable is created.
17844 */
17845 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017846set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017847 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017848 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017849 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017850{
Bram Moolenaar33570922005-01-25 22:26:29 +000017851 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017852 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017853 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017854 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017855
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017856 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017857 {
17858 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
17859 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
17860 ? name[2] : name[0]))
17861 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017862 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017863 return;
17864 }
17865 if (function_exists(name))
17866 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017867 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017868 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017869 return;
17870 }
17871 }
17872
Bram Moolenaara7043832005-01-21 11:56:39 +000017873 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000017874 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000017875 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000017876 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000017877 return;
17878 }
17879
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017880 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017881 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017882 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017883 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017884 if (var_check_ro(v->di_flags, name)
17885 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000017886 return;
17887 if (v->di_tv.v_type != tv->v_type
17888 && !((v->di_tv.v_type == VAR_STRING
17889 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017890 && (tv->v_type == VAR_STRING
17891 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017892 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017893 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017894 return;
17895 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017896
17897 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000017898 * Handle setting internal v: variables separately: we don't change
17899 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000017900 */
17901 if (ht == &vimvarht)
17902 {
17903 if (v->di_tv.v_type == VAR_STRING)
17904 {
17905 vim_free(v->di_tv.vval.v_string);
17906 if (copy || tv->v_type != VAR_STRING)
17907 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
17908 else
17909 {
17910 /* Take over the string to avoid an extra alloc/free. */
17911 v->di_tv.vval.v_string = tv->vval.v_string;
17912 tv->vval.v_string = NULL;
17913 }
17914 }
17915 else if (v->di_tv.v_type != VAR_NUMBER)
17916 EMSG2(_(e_intern2), "set_var()");
17917 else
17918 v->di_tv.vval.v_number = get_tv_number(tv);
17919 return;
17920 }
17921
17922 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017923 }
17924 else /* add a new variable */
17925 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000017926 /* Can't add "v:" variable. */
17927 if (ht == &vimvarht)
17928 {
17929 EMSG2(_(e_illvar), name);
17930 return;
17931 }
17932
Bram Moolenaar92124a32005-06-17 22:03:40 +000017933 /* Make sure the variable name is valid. */
17934 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000017935 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
17936 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000017937 {
17938 EMSG2(_(e_illvar), varname);
17939 return;
17940 }
17941
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017942 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
17943 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000017944 if (v == NULL)
17945 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000017946 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000017947 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017948 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017949 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017950 return;
17951 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017952 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017953 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017954
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017955 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000017956 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017957 else
17958 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017959 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017960 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017961 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017962 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017963}
17964
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017965/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000017966 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000017967 * Also give an error message.
17968 */
17969 static int
17970var_check_ro(flags, name)
17971 int flags;
17972 char_u *name;
17973{
17974 if (flags & DI_FLAGS_RO)
17975 {
17976 EMSG2(_(e_readonlyvar), name);
17977 return TRUE;
17978 }
17979 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
17980 {
17981 EMSG2(_(e_readonlysbx), name);
17982 return TRUE;
17983 }
17984 return FALSE;
17985}
17986
17987/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000017988 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
17989 * Also give an error message.
17990 */
17991 static int
17992var_check_fixed(flags, name)
17993 int flags;
17994 char_u *name;
17995{
17996 if (flags & DI_FLAGS_FIX)
17997 {
17998 EMSG2(_("E795: Cannot delete variable %s"), name);
17999 return TRUE;
18000 }
18001 return FALSE;
18002}
18003
18004/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018005 * Return TRUE if typeval "tv" is set to be locked (immutable).
18006 * Also give an error message, using "name".
18007 */
18008 static int
18009tv_check_lock(lock, name)
18010 int lock;
18011 char_u *name;
18012{
18013 if (lock & VAR_LOCKED)
18014 {
18015 EMSG2(_("E741: Value is locked: %s"),
18016 name == NULL ? (char_u *)_("Unknown") : name);
18017 return TRUE;
18018 }
18019 if (lock & VAR_FIXED)
18020 {
18021 EMSG2(_("E742: Cannot change value of %s"),
18022 name == NULL ? (char_u *)_("Unknown") : name);
18023 return TRUE;
18024 }
18025 return FALSE;
18026}
18027
18028/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018029 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018030 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018031 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018032 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018033 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018034copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000018035 typval_T *from;
18036 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018037{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018038 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018039 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018040 switch (from->v_type)
18041 {
18042 case VAR_NUMBER:
18043 to->vval.v_number = from->vval.v_number;
18044 break;
18045 case VAR_STRING:
18046 case VAR_FUNC:
18047 if (from->vval.v_string == NULL)
18048 to->vval.v_string = NULL;
18049 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018050 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018051 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018052 if (from->v_type == VAR_FUNC)
18053 func_ref(to->vval.v_string);
18054 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018055 break;
18056 case VAR_LIST:
18057 if (from->vval.v_list == NULL)
18058 to->vval.v_list = NULL;
18059 else
18060 {
18061 to->vval.v_list = from->vval.v_list;
18062 ++to->vval.v_list->lv_refcount;
18063 }
18064 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018065 case VAR_DICT:
18066 if (from->vval.v_dict == NULL)
18067 to->vval.v_dict = NULL;
18068 else
18069 {
18070 to->vval.v_dict = from->vval.v_dict;
18071 ++to->vval.v_dict->dv_refcount;
18072 }
18073 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018074 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018075 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018076 break;
18077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018078}
18079
18080/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000018081 * Make a copy of an item.
18082 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018083 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
18084 * reference to an already copied list/dict can be used.
18085 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018086 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018087 static int
18088item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000018089 typval_T *from;
18090 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018091 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018092 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018093{
18094 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018095 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018096
Bram Moolenaar33570922005-01-25 22:26:29 +000018097 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018098 {
18099 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018100 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018101 }
18102 ++recurse;
18103
18104 switch (from->v_type)
18105 {
18106 case VAR_NUMBER:
18107 case VAR_STRING:
18108 case VAR_FUNC:
18109 copy_tv(from, to);
18110 break;
18111 case VAR_LIST:
18112 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018113 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018114 if (from->vval.v_list == NULL)
18115 to->vval.v_list = NULL;
18116 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
18117 {
18118 /* use the copy made earlier */
18119 to->vval.v_list = from->vval.v_list->lv_copylist;
18120 ++to->vval.v_list->lv_refcount;
18121 }
18122 else
18123 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
18124 if (to->vval.v_list == NULL)
18125 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018126 break;
18127 case VAR_DICT:
18128 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018129 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018130 if (from->vval.v_dict == NULL)
18131 to->vval.v_dict = NULL;
18132 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
18133 {
18134 /* use the copy made earlier */
18135 to->vval.v_dict = from->vval.v_dict->dv_copydict;
18136 ++to->vval.v_dict->dv_refcount;
18137 }
18138 else
18139 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
18140 if (to->vval.v_dict == NULL)
18141 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018142 break;
18143 default:
18144 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018145 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018146 }
18147 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018148 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018149}
18150
18151/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018152 * ":echo expr1 ..." print each argument separated with a space, add a
18153 * newline at the end.
18154 * ":echon expr1 ..." print each argument plain.
18155 */
18156 void
18157ex_echo(eap)
18158 exarg_T *eap;
18159{
18160 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018161 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018162 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018163 char_u *p;
18164 int needclr = TRUE;
18165 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018166 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018167
18168 if (eap->skip)
18169 ++emsg_skip;
18170 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
18171 {
18172 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018173 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018174 {
18175 /*
18176 * Report the invalid expression unless the expression evaluation
18177 * has been cancelled due to an aborting error, an interrupt, or an
18178 * exception.
18179 */
18180 if (!aborting())
18181 EMSG2(_(e_invexpr2), p);
18182 break;
18183 }
18184 if (!eap->skip)
18185 {
18186 if (atstart)
18187 {
18188 atstart = FALSE;
18189 /* Call msg_start() after eval1(), evaluating the expression
18190 * may cause a message to appear. */
18191 if (eap->cmdidx == CMD_echo)
18192 msg_start();
18193 }
18194 else if (eap->cmdidx == CMD_echo)
18195 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018196 p = echo_string(&rettv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018197 if (p != NULL)
18198 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018199 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018200 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018201 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018202 if (*p != TAB && needclr)
18203 {
18204 /* remove any text still there from the command */
18205 msg_clr_eos();
18206 needclr = FALSE;
18207 }
18208 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018209 }
18210 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018211 {
18212#ifdef FEAT_MBYTE
18213 if (has_mbyte)
18214 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018215 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018216
18217 (void)msg_outtrans_len_attr(p, i, echo_attr);
18218 p += i - 1;
18219 }
18220 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000018221#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018222 (void)msg_outtrans_len_attr(p, 1, echo_attr);
18223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018224 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018225 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018226 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018227 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018228 arg = skipwhite(arg);
18229 }
18230 eap->nextcmd = check_nextcmd(arg);
18231
18232 if (eap->skip)
18233 --emsg_skip;
18234 else
18235 {
18236 /* remove text that may still be there from the command */
18237 if (needclr)
18238 msg_clr_eos();
18239 if (eap->cmdidx == CMD_echo)
18240 msg_end();
18241 }
18242}
18243
18244/*
18245 * ":echohl {name}".
18246 */
18247 void
18248ex_echohl(eap)
18249 exarg_T *eap;
18250{
18251 int id;
18252
18253 id = syn_name2id(eap->arg);
18254 if (id == 0)
18255 echo_attr = 0;
18256 else
18257 echo_attr = syn_id2attr(id);
18258}
18259
18260/*
18261 * ":execute expr1 ..." execute the result of an expression.
18262 * ":echomsg expr1 ..." Print a message
18263 * ":echoerr expr1 ..." Print an error
18264 * Each gets spaces around each argument and a newline at the end for
18265 * echo commands
18266 */
18267 void
18268ex_execute(eap)
18269 exarg_T *eap;
18270{
18271 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018272 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018273 int ret = OK;
18274 char_u *p;
18275 garray_T ga;
18276 int len;
18277 int save_did_emsg;
18278
18279 ga_init2(&ga, 1, 80);
18280
18281 if (eap->skip)
18282 ++emsg_skip;
18283 while (*arg != NUL && *arg != '|' && *arg != '\n')
18284 {
18285 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018286 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018287 {
18288 /*
18289 * Report the invalid expression unless the expression evaluation
18290 * has been cancelled due to an aborting error, an interrupt, or an
18291 * exception.
18292 */
18293 if (!aborting())
18294 EMSG2(_(e_invexpr2), p);
18295 ret = FAIL;
18296 break;
18297 }
18298
18299 if (!eap->skip)
18300 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018301 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018302 len = (int)STRLEN(p);
18303 if (ga_grow(&ga, len + 2) == FAIL)
18304 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018305 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018306 ret = FAIL;
18307 break;
18308 }
18309 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018310 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018311 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018312 ga.ga_len += len;
18313 }
18314
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018315 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018316 arg = skipwhite(arg);
18317 }
18318
18319 if (ret != FAIL && ga.ga_data != NULL)
18320 {
18321 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000018322 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018323 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000018324 out_flush();
18325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018326 else if (eap->cmdidx == CMD_echoerr)
18327 {
18328 /* We don't want to abort following commands, restore did_emsg. */
18329 save_did_emsg = did_emsg;
18330 EMSG((char_u *)ga.ga_data);
18331 if (!force_abort)
18332 did_emsg = save_did_emsg;
18333 }
18334 else if (eap->cmdidx == CMD_execute)
18335 do_cmdline((char_u *)ga.ga_data,
18336 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
18337 }
18338
18339 ga_clear(&ga);
18340
18341 if (eap->skip)
18342 --emsg_skip;
18343
18344 eap->nextcmd = check_nextcmd(arg);
18345}
18346
18347/*
18348 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
18349 * "arg" points to the "&" or '+' when called, to "option" when returning.
18350 * Returns NULL when no option name found. Otherwise pointer to the char
18351 * after the option name.
18352 */
18353 static char_u *
18354find_option_end(arg, opt_flags)
18355 char_u **arg;
18356 int *opt_flags;
18357{
18358 char_u *p = *arg;
18359
18360 ++p;
18361 if (*p == 'g' && p[1] == ':')
18362 {
18363 *opt_flags = OPT_GLOBAL;
18364 p += 2;
18365 }
18366 else if (*p == 'l' && p[1] == ':')
18367 {
18368 *opt_flags = OPT_LOCAL;
18369 p += 2;
18370 }
18371 else
18372 *opt_flags = 0;
18373
18374 if (!ASCII_ISALPHA(*p))
18375 return NULL;
18376 *arg = p;
18377
18378 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
18379 p += 4; /* termcap option */
18380 else
18381 while (ASCII_ISALPHA(*p))
18382 ++p;
18383 return p;
18384}
18385
18386/*
18387 * ":function"
18388 */
18389 void
18390ex_function(eap)
18391 exarg_T *eap;
18392{
18393 char_u *theline;
18394 int j;
18395 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018396 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018397 char_u *name = NULL;
18398 char_u *p;
18399 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018400 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018401 garray_T newargs;
18402 garray_T newlines;
18403 int varargs = FALSE;
18404 int mustend = FALSE;
18405 int flags = 0;
18406 ufunc_T *fp;
18407 int indent;
18408 int nesting;
18409 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000018410 dictitem_T *v;
18411 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018412 static int func_nr = 0; /* number for nameless function */
18413 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018414 hashtab_T *ht;
18415 int todo;
18416 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018417 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018418
18419 /*
18420 * ":function" without argument: list functions.
18421 */
18422 if (ends_excmd(*eap->arg))
18423 {
18424 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018425 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018426 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000018427 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018428 {
18429 if (!HASHITEM_EMPTY(hi))
18430 {
18431 --todo;
18432 fp = HI2UF(hi);
18433 if (!isdigit(*fp->uf_name))
18434 list_func_head(fp, FALSE);
18435 }
18436 }
18437 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018438 eap->nextcmd = check_nextcmd(eap->arg);
18439 return;
18440 }
18441
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018442 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018443 * ":function /pat": list functions matching pattern.
18444 */
18445 if (*eap->arg == '/')
18446 {
18447 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
18448 if (!eap->skip)
18449 {
18450 regmatch_T regmatch;
18451
18452 c = *p;
18453 *p = NUL;
18454 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
18455 *p = c;
18456 if (regmatch.regprog != NULL)
18457 {
18458 regmatch.rm_ic = p_ic;
18459
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018460 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018461 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
18462 {
18463 if (!HASHITEM_EMPTY(hi))
18464 {
18465 --todo;
18466 fp = HI2UF(hi);
18467 if (!isdigit(*fp->uf_name)
18468 && vim_regexec(&regmatch, fp->uf_name, 0))
18469 list_func_head(fp, FALSE);
18470 }
18471 }
18472 }
18473 }
18474 if (*p == '/')
18475 ++p;
18476 eap->nextcmd = check_nextcmd(p);
18477 return;
18478 }
18479
18480 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018481 * Get the function name. There are these situations:
18482 * func normal function name
18483 * "name" == func, "fudi.fd_dict" == NULL
18484 * dict.func new dictionary entry
18485 * "name" == NULL, "fudi.fd_dict" set,
18486 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
18487 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018488 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018489 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18490 * dict.func existing dict entry that's not a Funcref
18491 * "name" == NULL, "fudi.fd_dict" set,
18492 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18493 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018494 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018495 name = trans_function_name(&p, eap->skip, 0, &fudi);
18496 paren = (vim_strchr(p, '(') != NULL);
18497 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018498 {
18499 /*
18500 * Return on an invalid expression in braces, unless the expression
18501 * evaluation has been cancelled due to an aborting error, an
18502 * interrupt, or an exception.
18503 */
18504 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018505 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018506 if (!eap->skip && fudi.fd_newkey != NULL)
18507 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018508 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018509 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018511 else
18512 eap->skip = TRUE;
18513 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000018514
Bram Moolenaar071d4272004-06-13 20:20:40 +000018515 /* An error in a function call during evaluation of an expression in magic
18516 * braces should not cause the function not to be defined. */
18517 saved_did_emsg = did_emsg;
18518 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018519
18520 /*
18521 * ":function func" with only function name: list function.
18522 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018523 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018524 {
18525 if (!ends_excmd(*skipwhite(p)))
18526 {
18527 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018528 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018529 }
18530 eap->nextcmd = check_nextcmd(p);
18531 if (eap->nextcmd != NULL)
18532 *p = NUL;
18533 if (!eap->skip && !got_int)
18534 {
18535 fp = find_func(name);
18536 if (fp != NULL)
18537 {
18538 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018539 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018540 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018541 if (FUNCLINE(fp, j) == NULL)
18542 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018543 msg_putchar('\n');
18544 msg_outnum((long)(j + 1));
18545 if (j < 9)
18546 msg_putchar(' ');
18547 if (j < 99)
18548 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018549 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018550 out_flush(); /* show a line at a time */
18551 ui_breakcheck();
18552 }
18553 if (!got_int)
18554 {
18555 msg_putchar('\n');
18556 msg_puts((char_u *)" endfunction");
18557 }
18558 }
18559 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018560 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018561 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018562 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018563 }
18564
18565 /*
18566 * ":function name(arg1, arg2)" Define function.
18567 */
18568 p = skipwhite(p);
18569 if (*p != '(')
18570 {
18571 if (!eap->skip)
18572 {
18573 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018574 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018575 }
18576 /* attempt to continue by skipping some text */
18577 if (vim_strchr(p, '(') != NULL)
18578 p = vim_strchr(p, '(');
18579 }
18580 p = skipwhite(p + 1);
18581
18582 ga_init2(&newargs, (int)sizeof(char_u *), 3);
18583 ga_init2(&newlines, (int)sizeof(char_u *), 3);
18584
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018585 if (!eap->skip)
18586 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000018587 /* Check the name of the function. Unless it's a dictionary function
18588 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018589 if (name != NULL)
18590 arg = name;
18591 else
18592 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000018593 if (arg != NULL && (fudi.fd_di == NULL
18594 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018595 {
18596 if (*arg == K_SPECIAL)
18597 j = 3;
18598 else
18599 j = 0;
18600 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
18601 : eval_isnamec(arg[j])))
18602 ++j;
18603 if (arg[j] != NUL)
18604 emsg_funcname(_(e_invarg2), arg);
18605 }
18606 }
18607
Bram Moolenaar071d4272004-06-13 20:20:40 +000018608 /*
18609 * Isolate the arguments: "arg1, arg2, ...)"
18610 */
18611 while (*p != ')')
18612 {
18613 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
18614 {
18615 varargs = TRUE;
18616 p += 3;
18617 mustend = TRUE;
18618 }
18619 else
18620 {
18621 arg = p;
18622 while (ASCII_ISALNUM(*p) || *p == '_')
18623 ++p;
18624 if (arg == p || isdigit(*arg)
18625 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
18626 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
18627 {
18628 if (!eap->skip)
18629 EMSG2(_("E125: Illegal argument: %s"), arg);
18630 break;
18631 }
18632 if (ga_grow(&newargs, 1) == FAIL)
18633 goto erret;
18634 c = *p;
18635 *p = NUL;
18636 arg = vim_strsave(arg);
18637 if (arg == NULL)
18638 goto erret;
18639 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
18640 *p = c;
18641 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018642 if (*p == ',')
18643 ++p;
18644 else
18645 mustend = TRUE;
18646 }
18647 p = skipwhite(p);
18648 if (mustend && *p != ')')
18649 {
18650 if (!eap->skip)
18651 EMSG2(_(e_invarg2), eap->arg);
18652 break;
18653 }
18654 }
18655 ++p; /* skip the ')' */
18656
Bram Moolenaare9a41262005-01-15 22:18:47 +000018657 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018658 for (;;)
18659 {
18660 p = skipwhite(p);
18661 if (STRNCMP(p, "range", 5) == 0)
18662 {
18663 flags |= FC_RANGE;
18664 p += 5;
18665 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000018666 else if (STRNCMP(p, "dict", 4) == 0)
18667 {
18668 flags |= FC_DICT;
18669 p += 4;
18670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018671 else if (STRNCMP(p, "abort", 5) == 0)
18672 {
18673 flags |= FC_ABORT;
18674 p += 5;
18675 }
18676 else
18677 break;
18678 }
18679
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018680 /* When there is a line break use what follows for the function body.
18681 * Makes 'exe "func Test()\n...\nendfunc"' work. */
18682 if (*p == '\n')
18683 line_arg = p + 1;
18684 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018685 EMSG(_(e_trailing));
18686
18687 /*
18688 * Read the body of the function, until ":endfunction" is found.
18689 */
18690 if (KeyTyped)
18691 {
18692 /* Check if the function already exists, don't let the user type the
18693 * whole function before telling him it doesn't work! For a script we
18694 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018695 if (!eap->skip && !eap->forceit)
18696 {
18697 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
18698 EMSG(_(e_funcdict));
18699 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018700 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018702
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018703 if (!eap->skip && did_emsg)
18704 goto erret;
18705
Bram Moolenaar071d4272004-06-13 20:20:40 +000018706 msg_putchar('\n'); /* don't overwrite the function name */
18707 cmdline_row = msg_row;
18708 }
18709
18710 indent = 2;
18711 nesting = 0;
18712 for (;;)
18713 {
18714 msg_scroll = TRUE;
18715 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018716 sourcing_lnum_off = sourcing_lnum;
18717
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018718 if (line_arg != NULL)
18719 {
18720 /* Use eap->arg, split up in parts by line breaks. */
18721 theline = line_arg;
18722 p = vim_strchr(theline, '\n');
18723 if (p == NULL)
18724 line_arg += STRLEN(line_arg);
18725 else
18726 {
18727 *p = NUL;
18728 line_arg = p + 1;
18729 }
18730 }
18731 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018732 theline = getcmdline(':', 0L, indent);
18733 else
18734 theline = eap->getline(':', eap->cookie, indent);
18735 if (KeyTyped)
18736 lines_left = Rows - 1;
18737 if (theline == NULL)
18738 {
18739 EMSG(_("E126: Missing :endfunction"));
18740 goto erret;
18741 }
18742
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018743 /* Detect line continuation: sourcing_lnum increased more than one. */
18744 if (sourcing_lnum > sourcing_lnum_off + 1)
18745 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
18746 else
18747 sourcing_lnum_off = 0;
18748
Bram Moolenaar071d4272004-06-13 20:20:40 +000018749 if (skip_until != NULL)
18750 {
18751 /* between ":append" and "." and between ":python <<EOF" and "EOF"
18752 * don't check for ":endfunc". */
18753 if (STRCMP(theline, skip_until) == 0)
18754 {
18755 vim_free(skip_until);
18756 skip_until = NULL;
18757 }
18758 }
18759 else
18760 {
18761 /* skip ':' and blanks*/
18762 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
18763 ;
18764
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018765 /* Check for "endfunction". */
18766 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018767 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018768 if (line_arg == NULL)
18769 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018770 break;
18771 }
18772
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018773 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000018774 * at "end". */
18775 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
18776 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018777 else if (STRNCMP(p, "if", 2) == 0
18778 || STRNCMP(p, "wh", 2) == 0
18779 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000018780 || STRNCMP(p, "try", 3) == 0)
18781 indent += 2;
18782
18783 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018784 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018785 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018786 if (*p == '!')
18787 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018788 p += eval_fname_script(p);
18789 if (ASCII_ISALPHA(*p))
18790 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018791 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018792 if (*skipwhite(p) == '(')
18793 {
18794 ++nesting;
18795 indent += 2;
18796 }
18797 }
18798 }
18799
18800 /* Check for ":append" or ":insert". */
18801 p = skip_range(p, NULL);
18802 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
18803 || (p[0] == 'i'
18804 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
18805 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
18806 skip_until = vim_strsave((char_u *)".");
18807
18808 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
18809 arg = skipwhite(skiptowhite(p));
18810 if (arg[0] == '<' && arg[1] =='<'
18811 && ((p[0] == 'p' && p[1] == 'y'
18812 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
18813 || (p[0] == 'p' && p[1] == 'e'
18814 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
18815 || (p[0] == 't' && p[1] == 'c'
18816 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
18817 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
18818 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000018819 || (p[0] == 'm' && p[1] == 'z'
18820 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018821 ))
18822 {
18823 /* ":python <<" continues until a dot, like ":append" */
18824 p = skipwhite(arg + 2);
18825 if (*p == NUL)
18826 skip_until = vim_strsave((char_u *)".");
18827 else
18828 skip_until = vim_strsave(p);
18829 }
18830 }
18831
18832 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018833 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018834 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018835 if (line_arg == NULL)
18836 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018837 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018838 }
18839
18840 /* Copy the line to newly allocated memory. get_one_sourceline()
18841 * allocates 250 bytes per line, this saves 80% on average. The cost
18842 * is an extra alloc/free. */
18843 p = vim_strsave(theline);
18844 if (p != NULL)
18845 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018846 if (line_arg == NULL)
18847 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018848 theline = p;
18849 }
18850
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018851 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
18852
18853 /* Add NULL lines for continuation lines, so that the line count is
18854 * equal to the index in the growarray. */
18855 while (sourcing_lnum_off-- > 0)
18856 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018857
18858 /* Check for end of eap->arg. */
18859 if (line_arg != NULL && *line_arg == NUL)
18860 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018861 }
18862
18863 /* Don't define the function when skipping commands or when an error was
18864 * detected. */
18865 if (eap->skip || did_emsg)
18866 goto erret;
18867
18868 /*
18869 * If there are no errors, add the function
18870 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018871 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018872 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018873 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000018874 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018875 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018876 emsg_funcname("E707: Function name conflicts with variable: %s",
18877 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018878 goto erret;
18879 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018880
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018881 fp = find_func(name);
18882 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018883 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018884 if (!eap->forceit)
18885 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018886 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018887 goto erret;
18888 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018889 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018890 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018891 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018892 name);
18893 goto erret;
18894 }
18895 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018896 ga_clear_strings(&(fp->uf_args));
18897 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018898 vim_free(name);
18899 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018901 }
18902 else
18903 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018904 char numbuf[20];
18905
18906 fp = NULL;
18907 if (fudi.fd_newkey == NULL && !eap->forceit)
18908 {
18909 EMSG(_(e_funcdict));
18910 goto erret;
18911 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000018912 if (fudi.fd_di == NULL)
18913 {
18914 /* Can't add a function to a locked dictionary */
18915 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
18916 goto erret;
18917 }
18918 /* Can't change an existing function if it is locked */
18919 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
18920 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018921
18922 /* Give the function a sequential number. Can only be used with a
18923 * Funcref! */
18924 vim_free(name);
18925 sprintf(numbuf, "%d", ++func_nr);
18926 name = vim_strsave((char_u *)numbuf);
18927 if (name == NULL)
18928 goto erret;
18929 }
18930
18931 if (fp == NULL)
18932 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018933 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018934 {
18935 int slen, plen;
18936 char_u *scriptname;
18937
18938 /* Check that the autoload name matches the script name. */
18939 j = FAIL;
18940 if (sourcing_name != NULL)
18941 {
18942 scriptname = autoload_name(name);
18943 if (scriptname != NULL)
18944 {
18945 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018946 plen = (int)STRLEN(p);
18947 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018948 if (slen > plen && fnamecmp(p,
18949 sourcing_name + slen - plen) == 0)
18950 j = OK;
18951 vim_free(scriptname);
18952 }
18953 }
18954 if (j == FAIL)
18955 {
18956 EMSG2(_("E746: Function name does not match script file name: %s"), name);
18957 goto erret;
18958 }
18959 }
18960
18961 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018962 if (fp == NULL)
18963 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018964
18965 if (fudi.fd_dict != NULL)
18966 {
18967 if (fudi.fd_di == NULL)
18968 {
18969 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018970 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018971 if (fudi.fd_di == NULL)
18972 {
18973 vim_free(fp);
18974 goto erret;
18975 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018976 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
18977 {
18978 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000018979 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018980 goto erret;
18981 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018982 }
18983 else
18984 /* overwrite existing dict entry */
18985 clear_tv(&fudi.fd_di->di_tv);
18986 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018987 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018988 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018989 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018990
18991 /* behave like "dict" was used */
18992 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018993 }
18994
Bram Moolenaar071d4272004-06-13 20:20:40 +000018995 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018996 STRCPY(fp->uf_name, name);
18997 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018998 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018999 fp->uf_args = newargs;
19000 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019001#ifdef FEAT_PROFILE
19002 fp->uf_tml_count = NULL;
19003 fp->uf_tml_total = NULL;
19004 fp->uf_tml_self = NULL;
19005 fp->uf_profiling = FALSE;
19006 if (prof_def_func())
19007 func_do_profile(fp);
19008#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019009 fp->uf_varargs = varargs;
19010 fp->uf_flags = flags;
19011 fp->uf_calls = 0;
19012 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019013 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019014
19015erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000019016 ga_clear_strings(&newargs);
19017 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019018ret_free:
19019 vim_free(skip_until);
19020 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019021 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019022 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019023}
19024
19025/*
19026 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000019027 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019028 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019029 * flags:
19030 * TFN_INT: internal function name OK
19031 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000019032 * Advances "pp" to just after the function name (if no error).
19033 */
19034 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019035trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019036 char_u **pp;
19037 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019038 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000019039 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019040{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019041 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019042 char_u *start;
19043 char_u *end;
19044 int lead;
19045 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019046 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000019047 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019048
19049 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019050 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019051 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000019052
19053 /* Check for hard coded <SNR>: already translated function ID (from a user
19054 * command). */
19055 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
19056 && (*pp)[2] == (int)KE_SNR)
19057 {
19058 *pp += 3;
19059 len = get_id_len(pp) + 3;
19060 return vim_strnsave(start, len);
19061 }
19062
19063 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
19064 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019065 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000019066 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019067 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019068
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019069 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
19070 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019071 if (end == start)
19072 {
19073 if (!skip)
19074 EMSG(_("E129: Function name required"));
19075 goto theend;
19076 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019077 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019078 {
19079 /*
19080 * Report an invalid expression in braces, unless the expression
19081 * evaluation has been cancelled due to an aborting error, an
19082 * interrupt, or an exception.
19083 */
19084 if (!aborting())
19085 {
19086 if (end != NULL)
19087 EMSG2(_(e_invarg2), start);
19088 }
19089 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019090 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019091 goto theend;
19092 }
19093
19094 if (lv.ll_tv != NULL)
19095 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019096 if (fdp != NULL)
19097 {
19098 fdp->fd_dict = lv.ll_dict;
19099 fdp->fd_newkey = lv.ll_newkey;
19100 lv.ll_newkey = NULL;
19101 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019102 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019103 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
19104 {
19105 name = vim_strsave(lv.ll_tv->vval.v_string);
19106 *pp = end;
19107 }
19108 else
19109 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019110 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
19111 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019112 EMSG(_(e_funcref));
19113 else
19114 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019115 name = NULL;
19116 }
19117 goto theend;
19118 }
19119
19120 if (lv.ll_name == NULL)
19121 {
19122 /* Error found, but continue after the function name. */
19123 *pp = end;
19124 goto theend;
19125 }
19126
19127 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000019128 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019129 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000019130 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
19131 && STRNCMP(lv.ll_name, "s:", 2) == 0)
19132 {
19133 /* When there was "s:" already or the name expanded to get a
19134 * leading "s:" then remove it. */
19135 lv.ll_name += 2;
19136 len -= 2;
19137 lead = 2;
19138 }
19139 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019140 else
Bram Moolenaara7043832005-01-21 11:56:39 +000019141 {
19142 if (lead == 2) /* skip over "s:" */
19143 lv.ll_name += 2;
19144 len = (int)(end - lv.ll_name);
19145 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019146
19147 /*
19148 * Copy the function name to allocated memory.
19149 * Accept <SID>name() inside a script, translate into <SNR>123_name().
19150 * Accept <SNR>123_name() outside a script.
19151 */
19152 if (skip)
19153 lead = 0; /* do nothing */
19154 else if (lead > 0)
19155 {
19156 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000019157 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
19158 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019159 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000019160 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019161 if (current_SID <= 0)
19162 {
19163 EMSG(_(e_usingsid));
19164 goto theend;
19165 }
19166 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
19167 lead += (int)STRLEN(sid_buf);
19168 }
19169 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019170 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019171 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019172 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019173 goto theend;
19174 }
19175 name = alloc((unsigned)(len + lead + 1));
19176 if (name != NULL)
19177 {
19178 if (lead > 0)
19179 {
19180 name[0] = K_SPECIAL;
19181 name[1] = KS_EXTRA;
19182 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000019183 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019184 STRCPY(name + 3, sid_buf);
19185 }
19186 mch_memmove(name + lead, lv.ll_name, (size_t)len);
19187 name[len + lead] = NUL;
19188 }
19189 *pp = end;
19190
19191theend:
19192 clear_lval(&lv);
19193 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019194}
19195
19196/*
19197 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
19198 * Return 2 if "p" starts with "s:".
19199 * Return 0 otherwise.
19200 */
19201 static int
19202eval_fname_script(p)
19203 char_u *p;
19204{
19205 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
19206 || STRNICMP(p + 1, "SNR>", 4) == 0))
19207 return 5;
19208 if (p[0] == 's' && p[1] == ':')
19209 return 2;
19210 return 0;
19211}
19212
19213/*
19214 * Return TRUE if "p" starts with "<SID>" or "s:".
19215 * Only works if eval_fname_script() returned non-zero for "p"!
19216 */
19217 static int
19218eval_fname_sid(p)
19219 char_u *p;
19220{
19221 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
19222}
19223
19224/*
19225 * List the head of the function: "name(arg1, arg2)".
19226 */
19227 static void
19228list_func_head(fp, indent)
19229 ufunc_T *fp;
19230 int indent;
19231{
19232 int j;
19233
19234 msg_start();
19235 if (indent)
19236 MSG_PUTS(" ");
19237 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019238 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019239 {
19240 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019241 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019242 }
19243 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019244 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019245 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019246 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019247 {
19248 if (j)
19249 MSG_PUTS(", ");
19250 msg_puts(FUNCARG(fp, j));
19251 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019252 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019253 {
19254 if (j)
19255 MSG_PUTS(", ");
19256 MSG_PUTS("...");
19257 }
19258 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019259 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000019260 if (p_verbose > 0)
19261 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019262}
19263
19264/*
19265 * Find a function by name, return pointer to it in ufuncs.
19266 * Return NULL for unknown function.
19267 */
19268 static ufunc_T *
19269find_func(name)
19270 char_u *name;
19271{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019272 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019273
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019274 hi = hash_find(&func_hashtab, name);
19275 if (!HASHITEM_EMPTY(hi))
19276 return HI2UF(hi);
19277 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019278}
19279
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019280#if defined(EXITFREE) || defined(PROTO)
19281 void
19282free_all_functions()
19283{
19284 hashitem_T *hi;
19285
19286 /* Need to start all over every time, because func_free() may change the
19287 * hash table. */
19288 while (func_hashtab.ht_used > 0)
19289 for (hi = func_hashtab.ht_array; ; ++hi)
19290 if (!HASHITEM_EMPTY(hi))
19291 {
19292 func_free(HI2UF(hi));
19293 break;
19294 }
19295}
19296#endif
19297
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019298/*
19299 * Return TRUE if a function "name" exists.
19300 */
19301 static int
19302function_exists(name)
19303 char_u *name;
19304{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000019305 char_u *nm = name;
19306 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019307 int n = FALSE;
19308
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000019309 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000019310 nm = skipwhite(nm);
19311
19312 /* Only accept "funcname", "funcname ", "funcname (..." and
19313 * "funcname(...", not "funcname!...". */
19314 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019315 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019316 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019317 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019318 else
19319 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019320 }
Bram Moolenaar79783442006-05-05 21:18:03 +000019321 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019322 return n;
19323}
19324
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019325/*
19326 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019327 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019328 */
19329 static int
19330builtin_function(name)
19331 char_u *name;
19332{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019333 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
19334 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019335}
19336
Bram Moolenaar05159a02005-02-26 23:04:13 +000019337#if defined(FEAT_PROFILE) || defined(PROTO)
19338/*
19339 * Start profiling function "fp".
19340 */
19341 static void
19342func_do_profile(fp)
19343 ufunc_T *fp;
19344{
19345 fp->uf_tm_count = 0;
19346 profile_zero(&fp->uf_tm_self);
19347 profile_zero(&fp->uf_tm_total);
19348 if (fp->uf_tml_count == NULL)
19349 fp->uf_tml_count = (int *)alloc_clear((unsigned)
19350 (sizeof(int) * fp->uf_lines.ga_len));
19351 if (fp->uf_tml_total == NULL)
19352 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
19353 (sizeof(proftime_T) * fp->uf_lines.ga_len));
19354 if (fp->uf_tml_self == NULL)
19355 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
19356 (sizeof(proftime_T) * fp->uf_lines.ga_len));
19357 fp->uf_tml_idx = -1;
19358 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
19359 || fp->uf_tml_self == NULL)
19360 return; /* out of memory */
19361
19362 fp->uf_profiling = TRUE;
19363}
19364
19365/*
19366 * Dump the profiling results for all functions in file "fd".
19367 */
19368 void
19369func_dump_profile(fd)
19370 FILE *fd;
19371{
19372 hashitem_T *hi;
19373 int todo;
19374 ufunc_T *fp;
19375 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000019376 ufunc_T **sorttab;
19377 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019378
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019379 todo = (int)func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000019380 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
19381
Bram Moolenaar05159a02005-02-26 23:04:13 +000019382 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
19383 {
19384 if (!HASHITEM_EMPTY(hi))
19385 {
19386 --todo;
19387 fp = HI2UF(hi);
19388 if (fp->uf_profiling)
19389 {
Bram Moolenaar73830342005-02-28 22:48:19 +000019390 if (sorttab != NULL)
19391 sorttab[st_len++] = fp;
19392
Bram Moolenaar05159a02005-02-26 23:04:13 +000019393 if (fp->uf_name[0] == K_SPECIAL)
19394 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
19395 else
19396 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
19397 if (fp->uf_tm_count == 1)
19398 fprintf(fd, "Called 1 time\n");
19399 else
19400 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
19401 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
19402 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
19403 fprintf(fd, "\n");
19404 fprintf(fd, "count total (s) self (s)\n");
19405
19406 for (i = 0; i < fp->uf_lines.ga_len; ++i)
19407 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019408 if (FUNCLINE(fp, i) == NULL)
19409 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000019410 prof_func_line(fd, fp->uf_tml_count[i],
19411 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019412 fprintf(fd, "%s\n", FUNCLINE(fp, i));
19413 }
19414 fprintf(fd, "\n");
19415 }
19416 }
19417 }
Bram Moolenaar73830342005-02-28 22:48:19 +000019418
19419 if (sorttab != NULL && st_len > 0)
19420 {
19421 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
19422 prof_total_cmp);
19423 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
19424 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
19425 prof_self_cmp);
19426 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
19427 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019428}
Bram Moolenaar73830342005-02-28 22:48:19 +000019429
19430 static void
19431prof_sort_list(fd, sorttab, st_len, title, prefer_self)
19432 FILE *fd;
19433 ufunc_T **sorttab;
19434 int st_len;
19435 char *title;
19436 int prefer_self; /* when equal print only self time */
19437{
19438 int i;
19439 ufunc_T *fp;
19440
19441 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
19442 fprintf(fd, "count total (s) self (s) function\n");
19443 for (i = 0; i < 20 && i < st_len; ++i)
19444 {
19445 fp = sorttab[i];
19446 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
19447 prefer_self);
19448 if (fp->uf_name[0] == K_SPECIAL)
19449 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
19450 else
19451 fprintf(fd, " %s()\n", fp->uf_name);
19452 }
19453 fprintf(fd, "\n");
19454}
19455
19456/*
19457 * Print the count and times for one function or function line.
19458 */
19459 static void
19460prof_func_line(fd, count, total, self, prefer_self)
19461 FILE *fd;
19462 int count;
19463 proftime_T *total;
19464 proftime_T *self;
19465 int prefer_self; /* when equal print only self time */
19466{
19467 if (count > 0)
19468 {
19469 fprintf(fd, "%5d ", count);
19470 if (prefer_self && profile_equal(total, self))
19471 fprintf(fd, " ");
19472 else
19473 fprintf(fd, "%s ", profile_msg(total));
19474 if (!prefer_self && profile_equal(total, self))
19475 fprintf(fd, " ");
19476 else
19477 fprintf(fd, "%s ", profile_msg(self));
19478 }
19479 else
19480 fprintf(fd, " ");
19481}
19482
19483/*
19484 * Compare function for total time sorting.
19485 */
19486 static int
19487#ifdef __BORLANDC__
19488_RTLENTRYF
19489#endif
19490prof_total_cmp(s1, s2)
19491 const void *s1;
19492 const void *s2;
19493{
19494 ufunc_T *p1, *p2;
19495
19496 p1 = *(ufunc_T **)s1;
19497 p2 = *(ufunc_T **)s2;
19498 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
19499}
19500
19501/*
19502 * Compare function for self time sorting.
19503 */
19504 static int
19505#ifdef __BORLANDC__
19506_RTLENTRYF
19507#endif
19508prof_self_cmp(s1, s2)
19509 const void *s1;
19510 const void *s2;
19511{
19512 ufunc_T *p1, *p2;
19513
19514 p1 = *(ufunc_T **)s1;
19515 p2 = *(ufunc_T **)s2;
19516 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
19517}
19518
Bram Moolenaar05159a02005-02-26 23:04:13 +000019519#endif
19520
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019521/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019522 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019523 * Return TRUE if a package was loaded.
19524 */
19525 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019526script_autoload(name, reload)
19527 char_u *name;
19528 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019529{
19530 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019531 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019532 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019533 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019534
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019535 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019536 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019537 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019538 return FALSE;
19539
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019540 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019541
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019542 /* Find the name in the list of previously loaded package names. Skip
19543 * "autoload/", it's always the same. */
19544 for (i = 0; i < ga_loaded.ga_len; ++i)
19545 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
19546 break;
19547 if (!reload && i < ga_loaded.ga_len)
19548 ret = FALSE; /* was loaded already */
19549 else
19550 {
19551 /* Remember the name if it wasn't loaded already. */
19552 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
19553 {
19554 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
19555 tofree = NULL;
19556 }
19557
19558 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000019559 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019560 ret = TRUE;
19561 }
19562
19563 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019564 return ret;
19565}
19566
19567/*
19568 * Return the autoload script name for a function or variable name.
19569 * Returns NULL when out of memory.
19570 */
19571 static char_u *
19572autoload_name(name)
19573 char_u *name;
19574{
19575 char_u *p;
19576 char_u *scriptname;
19577
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019578 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019579 scriptname = alloc((unsigned)(STRLEN(name) + 14));
19580 if (scriptname == NULL)
19581 return FALSE;
19582 STRCPY(scriptname, "autoload/");
19583 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019584 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019585 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019586 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019587 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019588 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019589}
19590
Bram Moolenaar071d4272004-06-13 20:20:40 +000019591#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
19592
19593/*
19594 * Function given to ExpandGeneric() to obtain the list of user defined
19595 * function names.
19596 */
19597 char_u *
19598get_user_func_name(xp, idx)
19599 expand_T *xp;
19600 int idx;
19601{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019602 static long_u done;
19603 static hashitem_T *hi;
19604 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019605
19606 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019607 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019608 done = 0;
19609 hi = func_hashtab.ht_array;
19610 }
19611 if (done < func_hashtab.ht_used)
19612 {
19613 if (done++ > 0)
19614 ++hi;
19615 while (HASHITEM_EMPTY(hi))
19616 ++hi;
19617 fp = HI2UF(hi);
19618
19619 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
19620 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019621
19622 cat_func_name(IObuff, fp);
19623 if (xp->xp_context != EXPAND_USER_FUNC)
19624 {
19625 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019626 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019627 STRCAT(IObuff, ")");
19628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019629 return IObuff;
19630 }
19631 return NULL;
19632}
19633
19634#endif /* FEAT_CMDL_COMPL */
19635
19636/*
19637 * Copy the function name of "fp" to buffer "buf".
19638 * "buf" must be able to hold the function name plus three bytes.
19639 * Takes care of script-local function names.
19640 */
19641 static void
19642cat_func_name(buf, fp)
19643 char_u *buf;
19644 ufunc_T *fp;
19645{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019646 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019647 {
19648 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019649 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019650 }
19651 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019652 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019653}
19654
19655/*
19656 * ":delfunction {name}"
19657 */
19658 void
19659ex_delfunction(eap)
19660 exarg_T *eap;
19661{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019662 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019663 char_u *p;
19664 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019665 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019666
19667 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019668 name = trans_function_name(&p, eap->skip, 0, &fudi);
19669 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019670 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019671 {
19672 if (fudi.fd_dict != NULL && !eap->skip)
19673 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019674 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019675 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019676 if (!ends_excmd(*skipwhite(p)))
19677 {
19678 vim_free(name);
19679 EMSG(_(e_trailing));
19680 return;
19681 }
19682 eap->nextcmd = check_nextcmd(p);
19683 if (eap->nextcmd != NULL)
19684 *p = NUL;
19685
19686 if (!eap->skip)
19687 fp = find_func(name);
19688 vim_free(name);
19689
19690 if (!eap->skip)
19691 {
19692 if (fp == NULL)
19693 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019694 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019695 return;
19696 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019697 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019698 {
19699 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
19700 return;
19701 }
19702
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019703 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019704 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019705 /* Delete the dict item that refers to the function, it will
19706 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019707 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019708 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019709 else
19710 func_free(fp);
19711 }
19712}
19713
19714/*
19715 * Free a function and remove it from the list of functions.
19716 */
19717 static void
19718func_free(fp)
19719 ufunc_T *fp;
19720{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019721 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019722
19723 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019724 ga_clear_strings(&(fp->uf_args));
19725 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000019726#ifdef FEAT_PROFILE
19727 vim_free(fp->uf_tml_count);
19728 vim_free(fp->uf_tml_total);
19729 vim_free(fp->uf_tml_self);
19730#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019731
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019732 /* remove the function from the function hashtable */
19733 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
19734 if (HASHITEM_EMPTY(hi))
19735 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019736 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019737 hash_remove(&func_hashtab, hi);
19738
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019739 vim_free(fp);
19740}
19741
19742/*
19743 * Unreference a Function: decrement the reference count and free it when it
19744 * becomes zero. Only for numbered functions.
19745 */
19746 static void
19747func_unref(name)
19748 char_u *name;
19749{
19750 ufunc_T *fp;
19751
19752 if (name != NULL && isdigit(*name))
19753 {
19754 fp = find_func(name);
19755 if (fp == NULL)
19756 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019757 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019758 {
19759 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019760 * when "uf_calls" becomes zero. */
19761 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019762 func_free(fp);
19763 }
19764 }
19765}
19766
19767/*
19768 * Count a reference to a Function.
19769 */
19770 static void
19771func_ref(name)
19772 char_u *name;
19773{
19774 ufunc_T *fp;
19775
19776 if (name != NULL && isdigit(*name))
19777 {
19778 fp = find_func(name);
19779 if (fp == NULL)
19780 EMSG2(_(e_intern2), "func_ref()");
19781 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019782 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019783 }
19784}
19785
19786/*
19787 * Call a user function.
19788 */
19789 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000019790call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019791 ufunc_T *fp; /* pointer to function */
19792 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000019793 typval_T *argvars; /* arguments */
19794 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019795 linenr_T firstline; /* first line of range */
19796 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000019797 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019798{
Bram Moolenaar33570922005-01-25 22:26:29 +000019799 char_u *save_sourcing_name;
19800 linenr_T save_sourcing_lnum;
19801 scid_T save_current_SID;
19802 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000019803 int save_did_emsg;
19804 static int depth = 0;
19805 dictitem_T *v;
19806 int fixvar_idx = 0; /* index in fixvar[] */
19807 int i;
19808 int ai;
19809 char_u numbuf[NUMBUFLEN];
19810 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019811#ifdef FEAT_PROFILE
19812 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000019813 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019814#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019815
19816 /* If depth of calling is getting too high, don't execute the function */
19817 if (depth >= p_mfd)
19818 {
19819 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019820 rettv->v_type = VAR_NUMBER;
19821 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019822 return;
19823 }
19824 ++depth;
19825
19826 line_breakcheck(); /* check for CTRL-C hit */
19827
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019828 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000019829 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019830 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019831 fc.rettv = rettv;
19832 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019833 fc.linenr = 0;
19834 fc.returned = FALSE;
19835 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019836 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019837 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019838 fc.dbg_tick = debug_tick;
19839
Bram Moolenaar33570922005-01-25 22:26:29 +000019840 /*
19841 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
19842 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
19843 * each argument variable and saves a lot of time.
19844 */
19845 /*
19846 * Init l: variables.
19847 */
19848 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000019849 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019850 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000019851 /* Set l:self to "selfdict". Use "name" to avoid a warning from
19852 * some compiler that checks the destination size. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019853 v = &fc.fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000019854 name = v->di_key;
19855 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000019856 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
19857 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
19858 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019859 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000019860 v->di_tv.vval.v_dict = selfdict;
19861 ++selfdict->dv_refcount;
19862 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000019863
Bram Moolenaar33570922005-01-25 22:26:29 +000019864 /*
19865 * Init a: variables.
19866 * Set a:0 to "argcount".
19867 * Set a:000 to a list with room for the "..." arguments.
19868 */
19869 init_var_dict(&fc.l_avars, &fc.l_avars_var);
19870 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019871 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000019872 v = &fc.fixvar[fixvar_idx++].var;
19873 STRCPY(v->di_key, "000");
19874 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19875 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
19876 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019877 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019878 v->di_tv.vval.v_list = &fc.l_varlist;
19879 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
19880 fc.l_varlist.lv_refcount = 99999;
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000019881 fc.l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019882
19883 /*
19884 * Set a:firstline to "firstline" and a:lastline to "lastline".
19885 * Set a:name to named arguments.
19886 * Set a:N to the "..." arguments.
19887 */
19888 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
19889 (varnumber_T)firstline);
19890 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
19891 (varnumber_T)lastline);
19892 for (i = 0; i < argcount; ++i)
19893 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019894 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000019895 if (ai < 0)
19896 /* named argument a:name */
19897 name = FUNCARG(fp, i);
19898 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019899 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019900 /* "..." argument a:1, a:2, etc. */
19901 sprintf((char *)numbuf, "%d", ai + 1);
19902 name = numbuf;
19903 }
19904 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
19905 {
19906 v = &fc.fixvar[fixvar_idx++].var;
19907 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19908 }
19909 else
19910 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019911 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19912 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000019913 if (v == NULL)
19914 break;
19915 v->di_flags = DI_FLAGS_RO;
19916 }
19917 STRCPY(v->di_key, name);
19918 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
19919
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019920 /* Note: the values are copied directly to avoid alloc/free.
19921 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019922 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019923 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019924
19925 if (ai >= 0 && ai < MAX_FUNC_ARGS)
19926 {
19927 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
19928 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019929 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019930 }
19931 }
19932
Bram Moolenaar071d4272004-06-13 20:20:40 +000019933 /* Don't redraw while executing the function. */
19934 ++RedrawingDisabled;
19935 save_sourcing_name = sourcing_name;
19936 save_sourcing_lnum = sourcing_lnum;
19937 sourcing_lnum = 1;
19938 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019939 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019940 if (sourcing_name != NULL)
19941 {
19942 if (save_sourcing_name != NULL
19943 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
19944 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
19945 else
19946 STRCPY(sourcing_name, "function ");
19947 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
19948
19949 if (p_verbose >= 12)
19950 {
19951 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019952 verbose_enter_scroll();
19953
Bram Moolenaar555b2802005-05-19 21:08:39 +000019954 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019955 if (p_verbose >= 14)
19956 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019957 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000019958 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000019959 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019960
19961 msg_puts((char_u *)"(");
19962 for (i = 0; i < argcount; ++i)
19963 {
19964 if (i > 0)
19965 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019966 if (argvars[i].v_type == VAR_NUMBER)
19967 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019968 else
19969 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000019970 trunc_string(tv2string(&argvars[i], &tofree,
19971 numbuf2, 0), buf, MSG_BUF_CLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019972 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019973 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019974 }
19975 }
19976 msg_puts((char_u *)")");
19977 }
19978 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019979
19980 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019981 --no_wait_return;
19982 }
19983 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019984#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000019985 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019986 {
19987 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
19988 func_do_profile(fp);
19989 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019990 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000019991 {
19992 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000019993 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019994 profile_zero(&fp->uf_tm_children);
19995 }
19996 script_prof_save(&wait_start);
19997 }
19998#endif
19999
Bram Moolenaar071d4272004-06-13 20:20:40 +000020000 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020001 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020002 save_did_emsg = did_emsg;
20003 did_emsg = FALSE;
20004
20005 /* call do_cmdline() to execute the lines */
20006 do_cmdline(NULL, get_func_line, (void *)&fc,
20007 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
20008
20009 --RedrawingDisabled;
20010
20011 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020012 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020013 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020014 clear_tv(rettv);
20015 rettv->v_type = VAR_NUMBER;
20016 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020017 }
20018
Bram Moolenaar05159a02005-02-26 23:04:13 +000020019#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020020 if (do_profiling == PROF_YES && (fp->uf_profiling
20021 || (fc.caller != NULL && &fc.caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000020022 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000020023 profile_end(&call_start);
20024 profile_sub_wait(&wait_start, &call_start);
20025 profile_add(&fp->uf_tm_total, &call_start);
20026 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020027 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020028 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000020029 profile_add(&fc.caller->func->uf_tm_children, &call_start);
20030 profile_add(&fc.caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020031 }
20032 }
20033#endif
20034
Bram Moolenaar071d4272004-06-13 20:20:40 +000020035 /* when being verbose, mention the return value */
20036 if (p_verbose >= 12)
20037 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020038 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020039 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020040
Bram Moolenaar071d4272004-06-13 20:20:40 +000020041 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000020042 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020043 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000020044 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
20045 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000020046 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020047 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000020048 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000020049 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000020050 char_u *tofree;
20051
Bram Moolenaar555b2802005-05-19 21:08:39 +000020052 /* The value may be very long. Skip the middle part, so that we
20053 * have some idea how it starts and ends. smsg() would always
20054 * truncate it at the end. */
Bram Moolenaar89d40322006-08-29 15:30:07 +000020055 trunc_string(tv2string(fc.rettv, &tofree, numbuf2, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020056 buf, MSG_BUF_CLEN);
Bram Moolenaar555b2802005-05-19 21:08:39 +000020057 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000020058 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020059 }
20060 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020061
20062 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020063 --no_wait_return;
20064 }
20065
20066 vim_free(sourcing_name);
20067 sourcing_name = save_sourcing_name;
20068 sourcing_lnum = save_sourcing_lnum;
20069 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020070#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020071 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020072 script_prof_restore(&wait_start);
20073#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020074
20075 if (p_verbose >= 12 && sourcing_name != NULL)
20076 {
20077 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020078 verbose_enter_scroll();
20079
Bram Moolenaar555b2802005-05-19 21:08:39 +000020080 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020081 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020082
20083 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020084 --no_wait_return;
20085 }
20086
20087 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020088 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020089
Bram Moolenaar33570922005-01-25 22:26:29 +000020090 /* The a: variables typevals were not alloced, only free the allocated
20091 * variables. */
20092 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
20093
20094 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020095 --depth;
20096}
20097
20098/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020099 * Add a number variable "name" to dict "dp" with value "nr".
20100 */
20101 static void
20102add_nr_var(dp, v, name, nr)
20103 dict_T *dp;
20104 dictitem_T *v;
20105 char *name;
20106 varnumber_T nr;
20107{
20108 STRCPY(v->di_key, name);
20109 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20110 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
20111 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020112 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020113 v->di_tv.vval.v_number = nr;
20114}
20115
20116/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020117 * ":return [expr]"
20118 */
20119 void
20120ex_return(eap)
20121 exarg_T *eap;
20122{
20123 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020124 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020125 int returning = FALSE;
20126
20127 if (current_funccal == NULL)
20128 {
20129 EMSG(_("E133: :return not inside a function"));
20130 return;
20131 }
20132
20133 if (eap->skip)
20134 ++emsg_skip;
20135
20136 eap->nextcmd = NULL;
20137 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020138 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020139 {
20140 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020141 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020142 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020143 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020144 }
20145 /* It's safer to return also on error. */
20146 else if (!eap->skip)
20147 {
20148 /*
20149 * Return unless the expression evaluation has been cancelled due to an
20150 * aborting error, an interrupt, or an exception.
20151 */
20152 if (!aborting())
20153 returning = do_return(eap, FALSE, TRUE, NULL);
20154 }
20155
20156 /* When skipping or the return gets pending, advance to the next command
20157 * in this line (!returning). Otherwise, ignore the rest of the line.
20158 * Following lines will be ignored by get_func_line(). */
20159 if (returning)
20160 eap->nextcmd = NULL;
20161 else if (eap->nextcmd == NULL) /* no argument */
20162 eap->nextcmd = check_nextcmd(arg);
20163
20164 if (eap->skip)
20165 --emsg_skip;
20166}
20167
20168/*
20169 * Return from a function. Possibly makes the return pending. Also called
20170 * for a pending return at the ":endtry" or after returning from an extra
20171 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000020172 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020173 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020174 * FALSE when the return gets pending.
20175 */
20176 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020177do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020178 exarg_T *eap;
20179 int reanimate;
20180 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020181 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020182{
20183 int idx;
20184 struct condstack *cstack = eap->cstack;
20185
20186 if (reanimate)
20187 /* Undo the return. */
20188 current_funccal->returned = FALSE;
20189
20190 /*
20191 * Cleanup (and inactivate) conditionals, but stop when a try conditional
20192 * not in its finally clause (which then is to be executed next) is found.
20193 * In this case, make the ":return" pending for execution at the ":endtry".
20194 * Otherwise, return normally.
20195 */
20196 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
20197 if (idx >= 0)
20198 {
20199 cstack->cs_pending[idx] = CSTP_RETURN;
20200
20201 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020202 /* A pending return again gets pending. "rettv" points to an
20203 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000020204 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020205 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020206 else
20207 {
20208 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020209 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020210 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020211 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020212
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020213 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020214 {
20215 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020216 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020217 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020218 else
20219 EMSG(_(e_outofmem));
20220 }
20221 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020222 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020223
20224 if (reanimate)
20225 {
20226 /* The pending return value could be overwritten by a ":return"
20227 * without argument in a finally clause; reset the default
20228 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020229 current_funccal->rettv->v_type = VAR_NUMBER;
20230 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020231 }
20232 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020233 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020234 }
20235 else
20236 {
20237 current_funccal->returned = TRUE;
20238
20239 /* If the return is carried out now, store the return value. For
20240 * a return immediately after reanimation, the value is already
20241 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020242 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020243 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020244 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000020245 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020246 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020247 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020248 }
20249 }
20250
20251 return idx < 0;
20252}
20253
20254/*
20255 * Free the variable with a pending return value.
20256 */
20257 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020258discard_pending_return(rettv)
20259 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020260{
Bram Moolenaar33570922005-01-25 22:26:29 +000020261 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020262}
20263
20264/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020265 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000020266 * is an allocated string. Used by report_pending() for verbose messages.
20267 */
20268 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020269get_return_cmd(rettv)
20270 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020271{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020272 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020273 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020274 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020275
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020276 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000020277 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020278 if (s == NULL)
20279 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020280
20281 STRCPY(IObuff, ":return ");
20282 STRNCPY(IObuff + 8, s, IOSIZE - 8);
20283 if (STRLEN(s) + 8 >= IOSIZE)
20284 STRCPY(IObuff + IOSIZE - 4, "...");
20285 vim_free(tofree);
20286 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020287}
20288
20289/*
20290 * Get next function line.
20291 * Called by do_cmdline() to get the next line.
20292 * Returns allocated string, or NULL for end of function.
20293 */
20294/* ARGSUSED */
20295 char_u *
20296get_func_line(c, cookie, indent)
20297 int c; /* not used */
20298 void *cookie;
20299 int indent; /* not used */
20300{
Bram Moolenaar33570922005-01-25 22:26:29 +000020301 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020302 ufunc_T *fp = fcp->func;
20303 char_u *retval;
20304 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020305
20306 /* If breakpoints have been added/deleted need to check for it. */
20307 if (fcp->dbg_tick != debug_tick)
20308 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020309 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020310 sourcing_lnum);
20311 fcp->dbg_tick = debug_tick;
20312 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000020313#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020314 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020315 func_line_end(cookie);
20316#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020317
Bram Moolenaar05159a02005-02-26 23:04:13 +000020318 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020319 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
20320 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020321 retval = NULL;
20322 else
20323 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020324 /* Skip NULL lines (continuation lines). */
20325 while (fcp->linenr < gap->ga_len
20326 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
20327 ++fcp->linenr;
20328 if (fcp->linenr >= gap->ga_len)
20329 retval = NULL;
20330 else
20331 {
20332 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
20333 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020334#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020335 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020336 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020337#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020339 }
20340
20341 /* Did we encounter a breakpoint? */
20342 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
20343 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020344 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020345 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020346 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020347 sourcing_lnum);
20348 fcp->dbg_tick = debug_tick;
20349 }
20350
20351 return retval;
20352}
20353
Bram Moolenaar05159a02005-02-26 23:04:13 +000020354#if defined(FEAT_PROFILE) || defined(PROTO)
20355/*
20356 * Called when starting to read a function line.
20357 * "sourcing_lnum" must be correct!
20358 * When skipping lines it may not actually be executed, but we won't find out
20359 * until later and we need to store the time now.
20360 */
20361 void
20362func_line_start(cookie)
20363 void *cookie;
20364{
20365 funccall_T *fcp = (funccall_T *)cookie;
20366 ufunc_T *fp = fcp->func;
20367
20368 if (fp->uf_profiling && sourcing_lnum >= 1
20369 && sourcing_lnum <= fp->uf_lines.ga_len)
20370 {
20371 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020372 /* Skip continuation lines. */
20373 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
20374 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020375 fp->uf_tml_execed = FALSE;
20376 profile_start(&fp->uf_tml_start);
20377 profile_zero(&fp->uf_tml_children);
20378 profile_get_wait(&fp->uf_tml_wait);
20379 }
20380}
20381
20382/*
20383 * Called when actually executing a function line.
20384 */
20385 void
20386func_line_exec(cookie)
20387 void *cookie;
20388{
20389 funccall_T *fcp = (funccall_T *)cookie;
20390 ufunc_T *fp = fcp->func;
20391
20392 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
20393 fp->uf_tml_execed = TRUE;
20394}
20395
20396/*
20397 * Called when done with a function line.
20398 */
20399 void
20400func_line_end(cookie)
20401 void *cookie;
20402{
20403 funccall_T *fcp = (funccall_T *)cookie;
20404 ufunc_T *fp = fcp->func;
20405
20406 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
20407 {
20408 if (fp->uf_tml_execed)
20409 {
20410 ++fp->uf_tml_count[fp->uf_tml_idx];
20411 profile_end(&fp->uf_tml_start);
20412 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020413 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000020414 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
20415 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020416 }
20417 fp->uf_tml_idx = -1;
20418 }
20419}
20420#endif
20421
Bram Moolenaar071d4272004-06-13 20:20:40 +000020422/*
20423 * Return TRUE if the currently active function should be ended, because a
20424 * return was encountered or an error occured. Used inside a ":while".
20425 */
20426 int
20427func_has_ended(cookie)
20428 void *cookie;
20429{
Bram Moolenaar33570922005-01-25 22:26:29 +000020430 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020431
20432 /* Ignore the "abort" flag if the abortion behavior has been changed due to
20433 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020434 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000020435 || fcp->returned);
20436}
20437
20438/*
20439 * return TRUE if cookie indicates a function which "abort"s on errors.
20440 */
20441 int
20442func_has_abort(cookie)
20443 void *cookie;
20444{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020445 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020446}
20447
20448#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
20449typedef enum
20450{
20451 VAR_FLAVOUR_DEFAULT,
20452 VAR_FLAVOUR_SESSION,
20453 VAR_FLAVOUR_VIMINFO
20454} var_flavour_T;
20455
20456static var_flavour_T var_flavour __ARGS((char_u *varname));
20457
20458 static var_flavour_T
20459var_flavour(varname)
20460 char_u *varname;
20461{
20462 char_u *p = varname;
20463
20464 if (ASCII_ISUPPER(*p))
20465 {
20466 while (*(++p))
20467 if (ASCII_ISLOWER(*p))
20468 return VAR_FLAVOUR_SESSION;
20469 return VAR_FLAVOUR_VIMINFO;
20470 }
20471 else
20472 return VAR_FLAVOUR_DEFAULT;
20473}
20474#endif
20475
20476#if defined(FEAT_VIMINFO) || defined(PROTO)
20477/*
20478 * Restore global vars that start with a capital from the viminfo file
20479 */
20480 int
20481read_viminfo_varlist(virp, writing)
20482 vir_T *virp;
20483 int writing;
20484{
20485 char_u *tab;
20486 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000020487 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020488
20489 if (!writing && (find_viminfo_parameter('!') != NULL))
20490 {
20491 tab = vim_strchr(virp->vir_line + 1, '\t');
20492 if (tab != NULL)
20493 {
20494 *tab++ = '\0'; /* isolate the variable name */
20495 if (*tab == 'S') /* string var */
20496 is_string = TRUE;
20497
20498 tab = vim_strchr(tab, '\t');
20499 if (tab != NULL)
20500 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020501 if (is_string)
20502 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020503 tv.v_type = VAR_STRING;
20504 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020505 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020506 }
20507 else
20508 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020509 tv.v_type = VAR_NUMBER;
20510 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020511 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020512 set_var(virp->vir_line + 1, &tv, FALSE);
20513 if (is_string)
20514 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020515 }
20516 }
20517 }
20518
20519 return viminfo_readline(virp);
20520}
20521
20522/*
20523 * Write global vars that start with a capital to the viminfo file
20524 */
20525 void
20526write_viminfo_varlist(fp)
20527 FILE *fp;
20528{
Bram Moolenaar33570922005-01-25 22:26:29 +000020529 hashitem_T *hi;
20530 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020531 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020532 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020533 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020534 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020535 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020536
20537 if (find_viminfo_parameter('!') == NULL)
20538 return;
20539
20540 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000020541
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020542 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000020543 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020544 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020545 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020546 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020547 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020548 this_var = HI2DI(hi);
20549 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020550 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020551 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000020552 {
20553 case VAR_STRING: s = "STR"; break;
20554 case VAR_NUMBER: s = "NUM"; break;
20555 default: continue;
20556 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020557 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000020558 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020559 if (p != NULL)
20560 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000020561 vim_free(tofree);
20562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020563 }
20564 }
20565}
20566#endif
20567
20568#if defined(FEAT_SESSION) || defined(PROTO)
20569 int
20570store_session_globals(fd)
20571 FILE *fd;
20572{
Bram Moolenaar33570922005-01-25 22:26:29 +000020573 hashitem_T *hi;
20574 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020575 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020576 char_u *p, *t;
20577
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020578 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000020579 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020580 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020581 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020582 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020583 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020584 this_var = HI2DI(hi);
20585 if ((this_var->di_tv.v_type == VAR_NUMBER
20586 || this_var->di_tv.v_type == VAR_STRING)
20587 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020588 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020589 /* Escape special characters with a backslash. Turn a LF and
20590 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020591 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000020592 (char_u *)"\\\"\n\r");
20593 if (p == NULL) /* out of memory */
20594 break;
20595 for (t = p; *t != NUL; ++t)
20596 if (*t == '\n')
20597 *t = 'n';
20598 else if (*t == '\r')
20599 *t = 'r';
20600 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000020601 this_var->di_key,
20602 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20603 : ' ',
20604 p,
20605 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20606 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000020607 || put_eol(fd) == FAIL)
20608 {
20609 vim_free(p);
20610 return FAIL;
20611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020612 vim_free(p);
20613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020614 }
20615 }
20616 return OK;
20617}
20618#endif
20619
Bram Moolenaar661b1822005-07-28 22:36:45 +000020620/*
20621 * Display script name where an item was last set.
20622 * Should only be invoked when 'verbose' is non-zero.
20623 */
20624 void
20625last_set_msg(scriptID)
20626 scid_T scriptID;
20627{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020628 char_u *p;
20629
Bram Moolenaar661b1822005-07-28 22:36:45 +000020630 if (scriptID != 0)
20631 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020632 p = home_replace_save(NULL, get_scriptname(scriptID));
20633 if (p != NULL)
20634 {
20635 verbose_enter();
20636 MSG_PUTS(_("\n\tLast set from "));
20637 MSG_PUTS(p);
20638 vim_free(p);
20639 verbose_leave();
20640 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000020641 }
20642}
20643
Bram Moolenaar071d4272004-06-13 20:20:40 +000020644#endif /* FEAT_EVAL */
20645
20646#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
20647
20648
20649#ifdef WIN3264
20650/*
20651 * Functions for ":8" filename modifier: get 8.3 version of a filename.
20652 */
20653static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
20654static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
20655static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
20656
20657/*
20658 * Get the short pathname of a file.
20659 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
20660 */
20661 static int
20662get_short_pathname(fnamep, bufp, fnamelen)
20663 char_u **fnamep;
20664 char_u **bufp;
20665 int *fnamelen;
20666{
20667 int l,len;
20668 char_u *newbuf;
20669
20670 len = *fnamelen;
20671
20672 l = GetShortPathName(*fnamep, *fnamep, len);
20673 if (l > len - 1)
20674 {
20675 /* If that doesn't work (not enough space), then save the string
20676 * and try again with a new buffer big enough
20677 */
20678 newbuf = vim_strnsave(*fnamep, l);
20679 if (newbuf == NULL)
20680 return 0;
20681
20682 vim_free(*bufp);
20683 *fnamep = *bufp = newbuf;
20684
20685 l = GetShortPathName(*fnamep,*fnamep,l+1);
20686
20687 /* Really should always succeed, as the buffer is big enough */
20688 }
20689
20690 *fnamelen = l;
20691 return 1;
20692}
20693
20694/*
20695 * Create a short path name. Returns the length of the buffer it needs.
20696 * Doesn't copy over the end of the buffer passed in.
20697 */
20698 static int
20699shortpath_for_invalid_fname(fname, bufp, fnamelen)
20700 char_u **fname;
20701 char_u **bufp;
20702 int *fnamelen;
20703{
20704 char_u *s, *p, *pbuf2, *pbuf3;
20705 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020706 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020707
20708 /* Make a copy */
20709 len2 = *fnamelen;
20710 pbuf2 = vim_strnsave(*fname, len2);
20711 pbuf3 = NULL;
20712
20713 s = pbuf2 + len2 - 1; /* Find the end */
20714 slen = 1;
20715 plen = len2;
20716
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020717 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020718 {
20719 --s;
20720 ++slen;
20721 --plen;
20722 }
20723
20724 do
20725 {
20726 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020727 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020728 {
20729 --s;
20730 ++slen;
20731 --plen;
20732 }
20733 if (s <= pbuf2)
20734 break;
20735
20736 /* Remeber the character that is about to be blatted */
20737 ch = *s;
20738 *s = 0; /* get_short_pathname requires a null-terminated string */
20739
20740 /* Try it in situ */
20741 p = pbuf2;
20742 if (!get_short_pathname(&p, &pbuf3, &plen))
20743 {
20744 vim_free(pbuf2);
20745 return -1;
20746 }
20747 *s = ch; /* Preserve the string */
20748 } while (plen == 0);
20749
20750 if (plen > 0)
20751 {
20752 /* Remeber the length of the new string. */
20753 *fnamelen = len = plen + slen;
20754 vim_free(*bufp);
20755 if (len > len2)
20756 {
20757 /* If there's not enough space in the currently allocated string,
20758 * then copy it to a buffer big enough.
20759 */
20760 *fname= *bufp = vim_strnsave(p, len);
20761 if (*fname == NULL)
20762 return -1;
20763 }
20764 else
20765 {
20766 /* Transfer pbuf2 to being the main buffer (it's big enough) */
20767 *fname = *bufp = pbuf2;
20768 if (p != pbuf2)
20769 strncpy(*fname, p, plen);
20770 pbuf2 = NULL;
20771 }
20772 /* Concat the next bit */
20773 strncpy(*fname + plen, s, slen);
20774 (*fname)[len] = '\0';
20775 }
20776 vim_free(pbuf3);
20777 vim_free(pbuf2);
20778 return 0;
20779}
20780
20781/*
20782 * Get a pathname for a partial path.
20783 */
20784 static int
20785shortpath_for_partial(fnamep, bufp, fnamelen)
20786 char_u **fnamep;
20787 char_u **bufp;
20788 int *fnamelen;
20789{
20790 int sepcount, len, tflen;
20791 char_u *p;
20792 char_u *pbuf, *tfname;
20793 int hasTilde;
20794
20795 /* Count up the path seperators from the RHS.. so we know which part
20796 * of the path to return.
20797 */
20798 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020799 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020800 if (vim_ispathsep(*p))
20801 ++sepcount;
20802
20803 /* Need full path first (use expand_env() to remove a "~/") */
20804 hasTilde = (**fnamep == '~');
20805 if (hasTilde)
20806 pbuf = tfname = expand_env_save(*fnamep);
20807 else
20808 pbuf = tfname = FullName_save(*fnamep, FALSE);
20809
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020810 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020811
20812 if (!get_short_pathname(&tfname, &pbuf, &len))
20813 return -1;
20814
20815 if (len == 0)
20816 {
20817 /* Don't have a valid filename, so shorten the rest of the
20818 * path if we can. This CAN give us invalid 8.3 filenames, but
20819 * there's not a lot of point in guessing what it might be.
20820 */
20821 len = tflen;
20822 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
20823 return -1;
20824 }
20825
20826 /* Count the paths backward to find the beginning of the desired string. */
20827 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020828 {
20829#ifdef FEAT_MBYTE
20830 if (has_mbyte)
20831 p -= mb_head_off(tfname, p);
20832#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020833 if (vim_ispathsep(*p))
20834 {
20835 if (sepcount == 0 || (hasTilde && sepcount == 1))
20836 break;
20837 else
20838 sepcount --;
20839 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020841 if (hasTilde)
20842 {
20843 --p;
20844 if (p >= tfname)
20845 *p = '~';
20846 else
20847 return -1;
20848 }
20849 else
20850 ++p;
20851
20852 /* Copy in the string - p indexes into tfname - allocated at pbuf */
20853 vim_free(*bufp);
20854 *fnamelen = (int)STRLEN(p);
20855 *bufp = pbuf;
20856 *fnamep = p;
20857
20858 return 0;
20859}
20860#endif /* WIN3264 */
20861
20862/*
20863 * Adjust a filename, according to a string of modifiers.
20864 * *fnamep must be NUL terminated when called. When returning, the length is
20865 * determined by *fnamelen.
20866 * Returns valid flags.
20867 * When there is an error, *fnamep is set to NULL.
20868 */
20869 int
20870modify_fname(src, usedlen, fnamep, bufp, fnamelen)
20871 char_u *src; /* string with modifiers */
20872 int *usedlen; /* characters after src that are used */
20873 char_u **fnamep; /* file name so far */
20874 char_u **bufp; /* buffer for allocated file name or NULL */
20875 int *fnamelen; /* length of fnamep */
20876{
20877 int valid = 0;
20878 char_u *tail;
20879 char_u *s, *p, *pbuf;
20880 char_u dirname[MAXPATHL];
20881 int c;
20882 int has_fullname = 0;
20883#ifdef WIN3264
20884 int has_shortname = 0;
20885#endif
20886
20887repeat:
20888 /* ":p" - full path/file_name */
20889 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
20890 {
20891 has_fullname = 1;
20892
20893 valid |= VALID_PATH;
20894 *usedlen += 2;
20895
20896 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
20897 if ((*fnamep)[0] == '~'
20898#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
20899 && ((*fnamep)[1] == '/'
20900# ifdef BACKSLASH_IN_FILENAME
20901 || (*fnamep)[1] == '\\'
20902# endif
20903 || (*fnamep)[1] == NUL)
20904
20905#endif
20906 )
20907 {
20908 *fnamep = expand_env_save(*fnamep);
20909 vim_free(*bufp); /* free any allocated file name */
20910 *bufp = *fnamep;
20911 if (*fnamep == NULL)
20912 return -1;
20913 }
20914
20915 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020916 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020917 {
20918 if (vim_ispathsep(*p)
20919 && p[1] == '.'
20920 && (p[2] == NUL
20921 || vim_ispathsep(p[2])
20922 || (p[2] == '.'
20923 && (p[3] == NUL || vim_ispathsep(p[3])))))
20924 break;
20925 }
20926
20927 /* FullName_save() is slow, don't use it when not needed. */
20928 if (*p != NUL || !vim_isAbsName(*fnamep))
20929 {
20930 *fnamep = FullName_save(*fnamep, *p != NUL);
20931 vim_free(*bufp); /* free any allocated file name */
20932 *bufp = *fnamep;
20933 if (*fnamep == NULL)
20934 return -1;
20935 }
20936
20937 /* Append a path separator to a directory. */
20938 if (mch_isdir(*fnamep))
20939 {
20940 /* Make room for one or two extra characters. */
20941 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
20942 vim_free(*bufp); /* free any allocated file name */
20943 *bufp = *fnamep;
20944 if (*fnamep == NULL)
20945 return -1;
20946 add_pathsep(*fnamep);
20947 }
20948 }
20949
20950 /* ":." - path relative to the current directory */
20951 /* ":~" - path relative to the home directory */
20952 /* ":8" - shortname path - postponed till after */
20953 while (src[*usedlen] == ':'
20954 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
20955 {
20956 *usedlen += 2;
20957 if (c == '8')
20958 {
20959#ifdef WIN3264
20960 has_shortname = 1; /* Postpone this. */
20961#endif
20962 continue;
20963 }
20964 pbuf = NULL;
20965 /* Need full path first (use expand_env() to remove a "~/") */
20966 if (!has_fullname)
20967 {
20968 if (c == '.' && **fnamep == '~')
20969 p = pbuf = expand_env_save(*fnamep);
20970 else
20971 p = pbuf = FullName_save(*fnamep, FALSE);
20972 }
20973 else
20974 p = *fnamep;
20975
20976 has_fullname = 0;
20977
20978 if (p != NULL)
20979 {
20980 if (c == '.')
20981 {
20982 mch_dirname(dirname, MAXPATHL);
20983 s = shorten_fname(p, dirname);
20984 if (s != NULL)
20985 {
20986 *fnamep = s;
20987 if (pbuf != NULL)
20988 {
20989 vim_free(*bufp); /* free any allocated file name */
20990 *bufp = pbuf;
20991 pbuf = NULL;
20992 }
20993 }
20994 }
20995 else
20996 {
20997 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
20998 /* Only replace it when it starts with '~' */
20999 if (*dirname == '~')
21000 {
21001 s = vim_strsave(dirname);
21002 if (s != NULL)
21003 {
21004 *fnamep = s;
21005 vim_free(*bufp);
21006 *bufp = s;
21007 }
21008 }
21009 }
21010 vim_free(pbuf);
21011 }
21012 }
21013
21014 tail = gettail(*fnamep);
21015 *fnamelen = (int)STRLEN(*fnamep);
21016
21017 /* ":h" - head, remove "/file_name", can be repeated */
21018 /* Don't remove the first "/" or "c:\" */
21019 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
21020 {
21021 valid |= VALID_HEAD;
21022 *usedlen += 2;
21023 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021024 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021025 --tail;
21026 *fnamelen = (int)(tail - *fnamep);
21027#ifdef VMS
21028 if (*fnamelen > 0)
21029 *fnamelen += 1; /* the path separator is part of the path */
21030#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021031 while (tail > s && !after_pathsep(s, tail))
21032 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021033 }
21034
21035 /* ":8" - shortname */
21036 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
21037 {
21038 *usedlen += 2;
21039#ifdef WIN3264
21040 has_shortname = 1;
21041#endif
21042 }
21043
21044#ifdef WIN3264
21045 /* Check shortname after we have done 'heads' and before we do 'tails'
21046 */
21047 if (has_shortname)
21048 {
21049 pbuf = NULL;
21050 /* Copy the string if it is shortened by :h */
21051 if (*fnamelen < (int)STRLEN(*fnamep))
21052 {
21053 p = vim_strnsave(*fnamep, *fnamelen);
21054 if (p == 0)
21055 return -1;
21056 vim_free(*bufp);
21057 *bufp = *fnamep = p;
21058 }
21059
21060 /* Split into two implementations - makes it easier. First is where
21061 * there isn't a full name already, second is where there is.
21062 */
21063 if (!has_fullname && !vim_isAbsName(*fnamep))
21064 {
21065 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
21066 return -1;
21067 }
21068 else
21069 {
21070 int l;
21071
21072 /* Simple case, already have the full-name
21073 * Nearly always shorter, so try first time. */
21074 l = *fnamelen;
21075 if (!get_short_pathname(fnamep, bufp, &l))
21076 return -1;
21077
21078 if (l == 0)
21079 {
21080 /* Couldn't find the filename.. search the paths.
21081 */
21082 l = *fnamelen;
21083 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
21084 return -1;
21085 }
21086 *fnamelen = l;
21087 }
21088 }
21089#endif /* WIN3264 */
21090
21091 /* ":t" - tail, just the basename */
21092 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
21093 {
21094 *usedlen += 2;
21095 *fnamelen -= (int)(tail - *fnamep);
21096 *fnamep = tail;
21097 }
21098
21099 /* ":e" - extension, can be repeated */
21100 /* ":r" - root, without extension, can be repeated */
21101 while (src[*usedlen] == ':'
21102 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
21103 {
21104 /* find a '.' in the tail:
21105 * - for second :e: before the current fname
21106 * - otherwise: The last '.'
21107 */
21108 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
21109 s = *fnamep - 2;
21110 else
21111 s = *fnamep + *fnamelen - 1;
21112 for ( ; s > tail; --s)
21113 if (s[0] == '.')
21114 break;
21115 if (src[*usedlen + 1] == 'e') /* :e */
21116 {
21117 if (s > tail)
21118 {
21119 *fnamelen += (int)(*fnamep - (s + 1));
21120 *fnamep = s + 1;
21121#ifdef VMS
21122 /* cut version from the extension */
21123 s = *fnamep + *fnamelen - 1;
21124 for ( ; s > *fnamep; --s)
21125 if (s[0] == ';')
21126 break;
21127 if (s > *fnamep)
21128 *fnamelen = s - *fnamep;
21129#endif
21130 }
21131 else if (*fnamep <= tail)
21132 *fnamelen = 0;
21133 }
21134 else /* :r */
21135 {
21136 if (s > tail) /* remove one extension */
21137 *fnamelen = (int)(s - *fnamep);
21138 }
21139 *usedlen += 2;
21140 }
21141
21142 /* ":s?pat?foo?" - substitute */
21143 /* ":gs?pat?foo?" - global substitute */
21144 if (src[*usedlen] == ':'
21145 && (src[*usedlen + 1] == 's'
21146 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
21147 {
21148 char_u *str;
21149 char_u *pat;
21150 char_u *sub;
21151 int sep;
21152 char_u *flags;
21153 int didit = FALSE;
21154
21155 flags = (char_u *)"";
21156 s = src + *usedlen + 2;
21157 if (src[*usedlen + 1] == 'g')
21158 {
21159 flags = (char_u *)"g";
21160 ++s;
21161 }
21162
21163 sep = *s++;
21164 if (sep)
21165 {
21166 /* find end of pattern */
21167 p = vim_strchr(s, sep);
21168 if (p != NULL)
21169 {
21170 pat = vim_strnsave(s, (int)(p - s));
21171 if (pat != NULL)
21172 {
21173 s = p + 1;
21174 /* find end of substitution */
21175 p = vim_strchr(s, sep);
21176 if (p != NULL)
21177 {
21178 sub = vim_strnsave(s, (int)(p - s));
21179 str = vim_strnsave(*fnamep, *fnamelen);
21180 if (sub != NULL && str != NULL)
21181 {
21182 *usedlen = (int)(p + 1 - src);
21183 s = do_string_sub(str, pat, sub, flags);
21184 if (s != NULL)
21185 {
21186 *fnamep = s;
21187 *fnamelen = (int)STRLEN(s);
21188 vim_free(*bufp);
21189 *bufp = s;
21190 didit = TRUE;
21191 }
21192 }
21193 vim_free(sub);
21194 vim_free(str);
21195 }
21196 vim_free(pat);
21197 }
21198 }
21199 /* after using ":s", repeat all the modifiers */
21200 if (didit)
21201 goto repeat;
21202 }
21203 }
21204
21205 return valid;
21206}
21207
21208/*
21209 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
21210 * "flags" can be "g" to do a global substitute.
21211 * Returns an allocated string, NULL for error.
21212 */
21213 char_u *
21214do_string_sub(str, pat, sub, flags)
21215 char_u *str;
21216 char_u *pat;
21217 char_u *sub;
21218 char_u *flags;
21219{
21220 int sublen;
21221 regmatch_T regmatch;
21222 int i;
21223 int do_all;
21224 char_u *tail;
21225 garray_T ga;
21226 char_u *ret;
21227 char_u *save_cpo;
21228
21229 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
21230 save_cpo = p_cpo;
21231 p_cpo = (char_u *)"";
21232
21233 ga_init2(&ga, 1, 200);
21234
21235 do_all = (flags[0] == 'g');
21236
21237 regmatch.rm_ic = p_ic;
21238 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
21239 if (regmatch.regprog != NULL)
21240 {
21241 tail = str;
21242 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
21243 {
21244 /*
21245 * Get some space for a temporary buffer to do the substitution
21246 * into. It will contain:
21247 * - The text up to where the match is.
21248 * - The substituted text.
21249 * - The text after the match.
21250 */
21251 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
21252 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
21253 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
21254 {
21255 ga_clear(&ga);
21256 break;
21257 }
21258
21259 /* copy the text up to where the match is */
21260 i = (int)(regmatch.startp[0] - tail);
21261 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
21262 /* add the substituted text */
21263 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
21264 + ga.ga_len + i, TRUE, TRUE, FALSE);
21265 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021266 /* avoid getting stuck on a match with an empty string */
21267 if (tail == regmatch.endp[0])
21268 {
21269 if (*tail == NUL)
21270 break;
21271 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
21272 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021273 }
21274 else
21275 {
21276 tail = regmatch.endp[0];
21277 if (*tail == NUL)
21278 break;
21279 }
21280 if (!do_all)
21281 break;
21282 }
21283
21284 if (ga.ga_data != NULL)
21285 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
21286
21287 vim_free(regmatch.regprog);
21288 }
21289
21290 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
21291 ga_clear(&ga);
21292 p_cpo = save_cpo;
21293
21294 return ret;
21295}
21296
21297#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */